1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Accessibility; use Accessibility;
27 with Atree; use Atree;
28 with Aspects; use Aspects;
29 with Checks; use Checks;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Errout; use Errout;
35 with Elists; use Elists;
36 with Expander; use Expander;
37 with Exp_Aggr; use Exp_Aggr;
38 with Exp_Atag; use Exp_Atag;
39 with Exp_Ch3; use Exp_Ch3;
40 with Exp_Ch4; use Exp_Ch4;
41 with Exp_Ch7; use Exp_Ch7;
42 with Exp_Ch9; use Exp_Ch9;
43 with Exp_Dbug; use Exp_Dbug;
44 with Exp_Disp; use Exp_Disp;
45 with Exp_Dist; use Exp_Dist;
46 with Exp_Intr; use Exp_Intr;
47 with Exp_Pakd; use Exp_Pakd;
48 with Exp_Tss; use Exp_Tss;
49 with Exp_Util; use Exp_Util;
50 with Freeze; use Freeze;
51 with Inline; use Inline;
52 with Itypes; use Itypes;
54 with Namet; use Namet;
55 with Nlists; use Nlists;
56 with Nmake; use Nmake;
58 with Restrict; use Restrict;
59 with Rident; use Rident;
60 with Rtsfind; use Rtsfind;
62 with Sem_Aux; use Sem_Aux;
63 with Sem_Ch6; use Sem_Ch6;
64 with Sem_Ch8; use Sem_Ch8;
65 with Sem_Ch13; use Sem_Ch13;
66 with Sem_Dim; use Sem_Dim;
67 with Sem_Disp; use Sem_Disp;
68 with Sem_Dist; use Sem_Dist;
69 with Sem_Eval; use Sem_Eval;
70 with Sem_Mech; use Sem_Mech;
71 with Sem_Res; use Sem_Res;
72 with Sem_SCIL; use Sem_SCIL;
73 with Sem_Util; use Sem_Util;
74 use Sem_Util.Storage_Model_Support;
75 with Sinfo; use Sinfo;
76 with Sinfo.Nodes; use Sinfo.Nodes;
77 with Sinfo.Utils; use Sinfo.Utils;
78 with Sinput; use Sinput;
79 with Snames; use Snames;
80 with Stand; use Stand;
81 with Stringt; use Stringt;
82 with Tbuild; use Tbuild;
83 with Uintp; use Uintp;
84 with Validsw; use Validsw;
86 package body Exp_Ch6 is
88 --------------------------------
89 -- Function return mechanisms --
90 --------------------------------
92 -- This is a summary of the various function return mechanisms implemented
93 -- in GNAT for Ada 2005 and later versions of the language. In the below
94 -- table, the first column must be read as an if expression: if the result
95 -- type of the function is limited, then the return mechanism is and ...;
96 -- elsif the result type is indefinite or large definite, then ...; elsif
97 -- ...; else ... The different mechanisms are implemented either in the
98 -- front end, or in the back end, or partly in both ends, depending on the
101 -- Result type | Return mechanism | Front end | Back end
102 -- --------------------------------------------------------------------
104 -- Limited Build In Place All
106 -- Indefinite/ Secondary Stack Needs Fin. Others
109 -- Needs Fin. Secondary Stack All
112 -- Needs Fin. Invisible Parameter All All
113 -- (BERS True) (return) (call)
115 -- By Reference Invisible Parameter All
117 -- Others Primary stack/ All
120 -- Needs Fin.: type needs finalization [RM 7.6(9.1/2-9.6/2)]
121 -- BERS: Opt.Back_End_Return_Slot setting
123 -- The table is valid for all calls except for those dispatching on result;
124 -- the latter calls are considered as returning a class-wide type and thus
125 -- always return on the secondary stack, with the help of a small wrapper
126 -- function (thunk) if the original result type is not itself returned on
127 -- the secondary stack as per the above table.
129 -- Suffixes for Build-In-Place extra formals
131 BIP_Alloc_Suffix : constant String := "BIPalloc";
132 BIP_Storage_Pool_Suffix : constant String := "BIPstoragepool";
133 BIP_Finalization_Master_Suffix : constant String := "BIPfinalizationmaster";
134 BIP_Task_Master_Suffix : constant String := "BIPtaskmaster";
135 BIP_Activation_Chain_Suffix : constant String := "BIPactivationchain";
136 BIP_Object_Access_Suffix : constant String := "BIPaccess";
138 -----------------------
139 -- Local Subprograms --
140 -----------------------
142 procedure Add_Access_Actual_To_Build_In_Place_Call
143 (Function_Call : Node_Id;
144 Function_Id : Entity_Id;
145 Return_Object : Node_Id;
146 Is_Access : Boolean := False);
147 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
148 -- object name given by Return_Object and add the attribute to the end of
149 -- the actual parameter list associated with the build-in-place function
150 -- call denoted by Function_Call. However, if Is_Access is True, then
151 -- Return_Object is already an access expression, in which case it's passed
152 -- along directly to the build-in-place function. Finally, if Return_Object
153 -- is empty, then pass a null literal as the actual.
155 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
156 (Function_Call : Node_Id;
157 Function_Id : Entity_Id;
158 Alloc_Form : BIP_Allocation_Form := Unspecified;
159 Alloc_Form_Exp : Node_Id := Empty;
160 Pool_Actual : Node_Id := Make_Null (No_Location));
161 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
162 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
163 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
164 -- otherwise pass a literal corresponding to the Alloc_Form parameter
165 -- (which must not be Unspecified in that case). Pool_Actual is the
166 -- parameter to pass to BIP_Storage_Pool.
168 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
169 (Func_Call : Node_Id;
171 Ptr_Typ : Entity_Id := Empty;
172 Master_Exp : Node_Id := Empty);
173 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
174 -- finalization actions, add an actual parameter which is a pointer to the
175 -- finalization master of the caller. If Master_Exp is not Empty, then that
176 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
177 -- will result in an automatic "null" value for the actual.
179 procedure Add_Task_Actuals_To_Build_In_Place_Call
180 (Function_Call : Node_Id;
181 Function_Id : Entity_Id;
182 Master_Actual : Node_Id;
183 Chain : Node_Id := Empty);
184 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
185 -- contains tasks, add two actual parameters: the master, and a pointer to
186 -- the caller's activation chain. Master_Actual is the actual parameter
187 -- expression to pass for the master. In most cases, this is the current
188 -- master (_master). The two exceptions are: If the function call is the
189 -- initialization expression for an allocator, we pass the master of the
190 -- access type. If the function call is the initialization expression for a
191 -- return object, we pass along the master passed in by the caller. In most
192 -- contexts, the activation chain to pass is the local one, which is
193 -- indicated by No (Chain). However, in an allocator, the caller passes in
194 -- the activation Chain. Note: Master_Actual can be Empty, but only if
195 -- there are no tasks.
197 function Caller_Known_Size
198 (Func_Call : Node_Id;
199 Result_Subt : Entity_Id) return Boolean;
200 -- True if result subtype is definite or has a size that does not require
201 -- secondary stack usage (i.e. no variant part or components whose type
202 -- depends on discriminants). In particular, untagged types with only
203 -- access discriminants do not require secondary stack use. Note we must
204 -- always use the secondary stack for dispatching-on-result calls.
206 function Check_BIP_Actuals
207 (Subp_Call : Node_Id;
208 Subp_Id : Entity_Id) return Boolean;
209 -- Given a subprogram call to the given subprogram return True if the
210 -- names of BIP extra actual and formal parameters match, and the number
211 -- of actuals (including extra actuals) matches the number of formals.
213 function Check_Number_Of_Actuals
214 (Subp_Call : Node_Id;
215 Subp_Id : Entity_Id) return Boolean;
216 -- Given a subprogram call to the given subprogram return True if the
217 -- number of actual parameters (including extra actuals) is correct.
219 procedure Check_Overriding_Operation (Subp : Entity_Id);
220 -- Subp is a dispatching operation. Check whether it may override an
221 -- inherited private operation, in which case its DT entry is that of
222 -- the hidden operation, not the one it may have received earlier.
223 -- This must be done before emitting the code to set the corresponding
224 -- DT to the address of the subprogram. The actual placement of Subp in
225 -- the proper place in the list of primitive operations is done in
226 -- Declare_Inherited_Private_Subprograms, which also has to deal with
227 -- implicit operations. This duplication is unavoidable for now???
229 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
230 -- This procedure is called only if the subprogram body N, whose spec
231 -- has the given entity Spec, contains a parameterless recursive call.
232 -- It attempts to generate runtime code to detect if this a case of
233 -- infinite recursion.
235 -- The body is scanned to determine dependencies. If the only external
236 -- dependencies are on a small set of scalar variables, then the values
237 -- of these variables are captured on entry to the subprogram, and if
238 -- the values are not changed for the call, we know immediately that
239 -- we have an infinite recursion.
241 procedure Expand_Actuals
244 Post_Call : out List_Id);
245 -- Return a list of actions to take place after the call in Post_Call. The
246 -- call will later be rewritten as an Expression_With_Actions, with the
247 -- Post_Call actions inserted, and the call inside.
249 -- For each actual of an in-out or out parameter which is a numeric (view)
250 -- conversion of the form T (A), where A denotes a variable, we insert the
253 -- Temp : T[ := T (A)];
255 -- prior to the call. Then we replace the actual with a reference to Temp,
256 -- and append the assignment:
258 -- A := TypeA (Temp);
260 -- after the call. Here TypeA is the actual type of variable A. For out
261 -- parameters, the initial declaration has no expression. If A is not an
262 -- entity name, we generate instead:
264 -- Var : TypeA renames A;
265 -- Temp : T := Var; -- omitting expression for out parameter.
267 -- Var := TypeA (Temp);
269 -- For other in-out parameters, we emit the required constraint checks
270 -- before and/or after the call.
272 -- For all parameter modes, actuals that denote components and slices of
273 -- packed arrays are expanded into suitable temporaries.
275 -- For nonscalar objects that are possibly unaligned, add call by copy code
276 -- (copy in for IN and IN OUT, copy out for OUT and IN OUT).
278 -- For OUT and IN OUT parameters, add predicate checks after the call
279 -- based on the predicates of the actual type.
281 procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id);
282 -- Does the main work of Expand_Call. Post_Call is as for Expand_Actuals.
284 procedure Expand_Ctrl_Function_Call (N : Node_Id; Use_Sec_Stack : Boolean);
285 -- N is a function call which returns a controlled object. Transform the
286 -- call into a temporary which retrieves the returned object from the
287 -- primary or secondary stack (Use_Sec_Stack says which) using 'reference.
289 procedure Expand_Non_Function_Return (N : Node_Id);
290 -- Expand a simple return statement found in a procedure body, entry body,
291 -- accept statement, or an extended return statement. Note that all non-
292 -- function returns are simple return statements.
294 function Expand_Protected_Object_Reference
296 Scop : Entity_Id) return Node_Id;
298 procedure Expand_Protected_Subprogram_Call
302 -- A call to a protected subprogram within the protected object may appear
303 -- as a regular call. The list of actuals must be expanded to contain a
304 -- reference to the object itself, and the call becomes a call to the
305 -- corresponding protected subprogram.
307 procedure Expand_Simple_Function_Return (N : Node_Id);
308 -- Expand simple return from function. In the case where we are returning
309 -- from a function body this is called by Expand_N_Simple_Return_Statement.
311 procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id);
312 -- Insert the Post_Call list previously produced by routine Expand_Actuals
313 -- or Expand_Call_Helper into the tree.
315 procedure Replace_Renaming_Declaration_Id
317 Orig_Decl : Node_Id);
318 -- Replace the internal identifier of the new renaming declaration New_Decl
319 -- with the identifier of its original declaration Orig_Decl exchanging the
320 -- entities containing their defining identifiers to ensure the correct
321 -- replacement of the object declaration by the object renaming declaration
322 -- to avoid homograph conflicts (since the object declaration's defining
323 -- identifier was already entered in the current scope). The Next_Entity
324 -- links of the two entities are also swapped since the entities are part
325 -- of the return scope's entity list and the list structure would otherwise
326 -- be corrupted. The homonym chain is preserved as well.
328 procedure Rewrite_Function_Call_For_C (N : Node_Id);
329 -- When generating C code, replace a call to a function that returns an
330 -- array into the generated procedure with an additional out parameter.
332 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id);
333 -- N is a return statement for a function that returns its result on the
334 -- secondary stack. This sets the Sec_Stack_Needed_For_Return flag on the
335 -- function and all blocks and loops that the return statement is jumping
336 -- out of. This ensures that the secondary stack is not released; otherwise
337 -- the function result would be reclaimed before returning to the caller.
339 procedure Warn_BIP (Func_Call : Node_Id);
340 -- Give a warning on a build-in-place function call if the -gnatd_B switch
343 ----------------------------------------------
344 -- Add_Access_Actual_To_Build_In_Place_Call --
345 ----------------------------------------------
347 procedure Add_Access_Actual_To_Build_In_Place_Call
348 (Function_Call : Node_Id;
349 Function_Id : Entity_Id;
350 Return_Object : Node_Id;
351 Is_Access : Boolean := False)
353 Loc : constant Source_Ptr := Sloc (Function_Call);
354 Obj_Address : Node_Id;
355 Obj_Acc_Formal : Entity_Id;
358 -- Locate the implicit access parameter in the called function
360 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
362 -- If no return object is provided, then pass null
364 if No (Return_Object) then
365 Obj_Address := Make_Null (Loc);
366 Set_Parent (Obj_Address, Function_Call);
368 -- If Return_Object is already an expression of an access type, then use
369 -- it directly, since it must be an access value denoting the return
370 -- object, and couldn't possibly be the return object itself.
373 Obj_Address := Return_Object;
374 Set_Parent (Obj_Address, Function_Call);
376 -- Apply Unrestricted_Access to caller's return object
380 Make_Attribute_Reference (Loc,
381 Prefix => Return_Object,
382 Attribute_Name => Name_Unrestricted_Access);
384 Set_Parent (Return_Object, Obj_Address);
385 Set_Parent (Obj_Address, Function_Call);
388 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
390 -- Build the parameter association for the new actual and add it to the
391 -- end of the function's actuals.
393 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
394 end Add_Access_Actual_To_Build_In_Place_Call;
396 ------------------------------------------------------
397 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
398 ------------------------------------------------------
400 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
401 (Function_Call : Node_Id;
402 Function_Id : Entity_Id;
403 Alloc_Form : BIP_Allocation_Form := Unspecified;
404 Alloc_Form_Exp : Node_Id := Empty;
405 Pool_Actual : Node_Id := Make_Null (No_Location))
407 Loc : constant Source_Ptr := Sloc (Function_Call);
409 Alloc_Form_Actual : Node_Id;
410 Alloc_Form_Formal : Node_Id;
411 Pool_Formal : Node_Id;
414 -- Nothing to do when the size of the object is known, and the caller is
415 -- in charge of allocating it, and the callee doesn't unconditionally
416 -- require an allocation form (such as due to having a tagged result).
418 if not Needs_BIP_Alloc_Form (Function_Id) then
422 -- Locate the implicit allocation form parameter in the called function.
423 -- Maybe it would be better for each implicit formal of a build-in-place
424 -- function to have a flag or a Uint attribute to identify it. ???
426 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
428 if Present (Alloc_Form_Exp) then
429 pragma Assert (Alloc_Form = Unspecified);
431 Alloc_Form_Actual := Alloc_Form_Exp;
434 pragma Assert (Alloc_Form /= Unspecified);
437 Make_Integer_Literal (Loc,
438 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
441 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
443 -- Build the parameter association for the new actual and add it to the
444 -- end of the function's actuals.
446 Add_Extra_Actual_To_Call
447 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
449 -- Pass the Storage_Pool parameter. This parameter is omitted on ZFP as
450 -- those targets do not support pools.
452 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
453 Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool);
454 Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal));
455 Add_Extra_Actual_To_Call
456 (Function_Call, Pool_Formal, Pool_Actual);
458 end Add_Unconstrained_Actuals_To_Build_In_Place_Call;
460 -----------------------------------------------------------
461 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
462 -----------------------------------------------------------
464 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
465 (Func_Call : Node_Id;
467 Ptr_Typ : Entity_Id := Empty;
468 Master_Exp : Node_Id := Empty)
471 if not Needs_BIP_Finalization_Master (Func_Id) then
476 Formal : constant Entity_Id :=
477 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
478 Loc : constant Source_Ptr := Sloc (Func_Call);
481 Desig_Typ : Entity_Id;
484 -- If there is a finalization master actual, such as the implicit
485 -- finalization master of an enclosing build-in-place function,
486 -- then this must be added as an extra actual of the call.
488 if Present (Master_Exp) then
489 Actual := Master_Exp;
491 -- Case where the context does not require an actual master
493 elsif No (Ptr_Typ) then
494 Actual := Make_Null (Loc);
497 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
499 -- Check for a library-level access type whose designated type has
500 -- suppressed finalization or the access type is subject to pragma
501 -- No_Heap_Finalization. Such an access type lacks a master. Pass
502 -- a null actual to callee in order to signal a missing master.
504 if Is_Library_Level_Entity (Ptr_Typ)
505 and then (Finalize_Storage_Only (Desig_Typ)
506 or else No_Heap_Finalization (Ptr_Typ))
508 Actual := Make_Null (Loc);
510 -- Types in need of finalization actions
512 elsif Needs_Finalization (Desig_Typ) then
514 -- The general mechanism of creating finalization masters for
515 -- anonymous access types is disabled by default, otherwise
516 -- finalization masters will pop all over the place. Such types
517 -- use context-specific masters.
519 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
520 and then No (Finalization_Master (Ptr_Typ))
522 Build_Anonymous_Master (Ptr_Typ);
525 -- Access-to-controlled types should always have a master
527 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
530 Make_Attribute_Reference (Loc,
532 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
533 Attribute_Name => Name_Unrestricted_Access);
538 Actual := Make_Null (Loc);
542 Analyze_And_Resolve (Actual, Etype (Formal));
544 -- Build the parameter association for the new actual and add it to
545 -- the end of the function's actuals.
547 Add_Extra_Actual_To_Call (Func_Call, Formal, Actual);
549 end Add_Finalization_Master_Actual_To_Build_In_Place_Call;
551 ------------------------------
552 -- Add_Extra_Actual_To_Call --
553 ------------------------------
555 procedure Add_Extra_Actual_To_Call
556 (Subprogram_Call : Node_Id;
557 Extra_Formal : Entity_Id;
558 Extra_Actual : Node_Id)
560 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
561 Param_Assoc : Node_Id;
565 Make_Parameter_Association (Loc,
566 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
567 Explicit_Actual_Parameter => Extra_Actual);
569 Set_Parent (Param_Assoc, Subprogram_Call);
570 Set_Parent (Extra_Actual, Param_Assoc);
572 if Present (Parameter_Associations (Subprogram_Call)) then
573 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
574 N_Parameter_Association
577 -- Find last named actual, and append
582 L := First_Actual (Subprogram_Call);
583 while Present (L) loop
584 if No (Next_Actual (L)) then
585 Set_Next_Named_Actual (Parent (L), Extra_Actual);
593 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
596 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
599 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
600 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
602 end Add_Extra_Actual_To_Call;
604 ---------------------------------------------
605 -- Add_Task_Actuals_To_Build_In_Place_Call --
606 ---------------------------------------------
608 procedure Add_Task_Actuals_To_Build_In_Place_Call
609 (Function_Call : Node_Id;
610 Function_Id : Entity_Id;
611 Master_Actual : Node_Id;
612 Chain : Node_Id := Empty)
614 Loc : constant Source_Ptr := Sloc (Function_Call);
616 Chain_Actual : Node_Id;
617 Chain_Formal : Node_Id;
618 Master_Formal : Node_Id;
621 -- No such extra parameters are needed if there are no tasks
623 if not Needs_BIP_Task_Actuals (Function_Id) then
627 Actual := Master_Actual;
629 -- Use a dummy _master actual in case of No_Task_Hierarchy
631 if Restriction_Active (No_Task_Hierarchy) then
632 Actual := Make_Integer_Literal (Loc, Library_Task_Level);
634 -- In the case where we use the master associated with an access type,
635 -- the actual is an entity and requires an explicit reference.
637 elsif Nkind (Actual) = N_Defining_Identifier then
638 Actual := New_Occurrence_Of (Actual, Loc);
641 -- Locate the implicit master parameter in the called function
643 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master);
644 Analyze_And_Resolve (Actual, Etype (Master_Formal));
646 -- Build the parameter association for the new actual and add it to the
647 -- end of the function's actuals.
649 Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual);
651 -- Locate the implicit activation chain parameter in the called function
654 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
656 -- Create the actual which is a pointer to the current activation chain
658 if Restriction_Active (No_Task_Hierarchy) then
659 Chain_Actual := Make_Null (Loc);
661 elsif No (Chain) then
663 Make_Attribute_Reference (Loc,
664 Prefix => Make_Identifier (Loc, Name_uChain),
665 Attribute_Name => Name_Unrestricted_Access);
667 -- Allocator case; make a reference to the Chain passed in by the caller
671 Make_Attribute_Reference (Loc,
672 Prefix => New_Occurrence_Of (Chain, Loc),
673 Attribute_Name => Name_Unrestricted_Access);
676 Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal));
678 -- Build the parameter association for the new actual and add it to the
679 -- end of the function's actuals.
681 Add_Extra_Actual_To_Call (Function_Call, Chain_Formal, Chain_Actual);
682 end Add_Task_Actuals_To_Build_In_Place_Call;
684 ----------------------------------
685 -- Apply_CW_Accessibility_Check --
686 ----------------------------------
688 procedure Apply_CW_Accessibility_Check (Exp : Node_Id; Func : Entity_Id) is
689 Loc : constant Source_Ptr := Sloc (Exp);
692 -- CodePeer does not do anything useful on Ada.Tags.Type_Specific_Data
695 if Ada_Version >= Ada_2005
696 and then not CodePeer_Mode
697 and then Tagged_Type_Expansion
698 and then not Scope_Suppress.Suppress (Accessibility_Check)
700 (Is_Class_Wide_Type (Etype (Exp))
701 or else Nkind (Exp) in
702 N_Type_Conversion | N_Unchecked_Type_Conversion
703 or else (Is_Entity_Name (Exp)
704 and then Is_Formal (Entity (Exp)))
705 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
706 Scope_Depth (Enclosing_Dynamic_Scope (Func)))
712 -- Ada 2005 (AI-251): In class-wide interface objects we displace
713 -- "this" to reference the base of the object. This is required to
714 -- get access to the TSD of the object.
716 if Is_Class_Wide_Type (Etype (Exp))
717 and then Is_Interface (Etype (Exp))
719 -- If the expression is an explicit dereference then we can
720 -- directly displace the pointer to reference the base of
723 if Nkind (Exp) = N_Explicit_Dereference then
725 Make_Explicit_Dereference (Loc,
727 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
728 Make_Function_Call (Loc,
730 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
731 Parameter_Associations => New_List (
732 Unchecked_Convert_To (RTE (RE_Address),
733 Duplicate_Subexpr (Prefix (Exp)))))));
735 -- Similar case to the previous one but the expression is a
736 -- renaming of an explicit dereference.
738 elsif Nkind (Exp) = N_Identifier
739 and then Present (Renamed_Object (Entity (Exp)))
740 and then Nkind (Renamed_Object (Entity (Exp)))
741 = N_Explicit_Dereference
744 Make_Explicit_Dereference (Loc,
746 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
747 Make_Function_Call (Loc,
749 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
750 Parameter_Associations => New_List (
751 Unchecked_Convert_To (RTE (RE_Address),
754 (Renamed_Object (Entity (Exp)))))))));
756 -- Common case: obtain the address of the actual object and
757 -- displace the pointer to reference the base of the object.
761 Make_Explicit_Dereference (Loc,
763 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
764 Make_Function_Call (Loc,
766 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
767 Parameter_Associations => New_List (
768 Make_Attribute_Reference (Loc,
769 Prefix => Duplicate_Subexpr (Exp),
770 Attribute_Name => Name_Address)))));
774 Make_Attribute_Reference (Loc,
775 Prefix => Duplicate_Subexpr (Exp),
776 Attribute_Name => Name_Tag);
779 -- Suppress junk access chacks on RE_Tag_Ptr
782 Make_Raise_Program_Error (Loc,
785 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
787 Make_Integer_Literal (Loc,
788 Scope_Depth (Enclosing_Dynamic_Scope (Func)))),
789 Reason => PE_Accessibility_Check_Failed),
790 Suppress => Access_Check);
793 end Apply_CW_Accessibility_Check;
795 -----------------------
796 -- BIP_Formal_Suffix --
797 -----------------------
799 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
802 when BIP_Alloc_Form =>
803 return BIP_Alloc_Suffix;
805 when BIP_Storage_Pool =>
806 return BIP_Storage_Pool_Suffix;
808 when BIP_Finalization_Master =>
809 return BIP_Finalization_Master_Suffix;
811 when BIP_Task_Master =>
812 return BIP_Task_Master_Suffix;
814 when BIP_Activation_Chain =>
815 return BIP_Activation_Chain_Suffix;
817 when BIP_Object_Access =>
818 return BIP_Object_Access_Suffix;
820 end BIP_Formal_Suffix;
822 ---------------------
823 -- BIP_Suffix_Kind --
824 ---------------------
826 function BIP_Suffix_Kind (E : Entity_Id) return BIP_Formal_Kind is
827 Nam : constant String := Get_Name_String (Chars (E));
829 function Has_Suffix (Suffix : String) return Boolean;
830 -- Return True if Nam has suffix Suffix
832 function Has_Suffix (Suffix : String) return Boolean is
833 Len : constant Natural := Suffix'Length;
835 return Nam'Length > Len
836 and then Nam (Nam'Last - Len + 1 .. Nam'Last) = Suffix;
839 -- Start of processing for BIP_Suffix_Kind
842 if Has_Suffix (BIP_Alloc_Suffix) then
843 return BIP_Alloc_Form;
845 elsif Has_Suffix (BIP_Storage_Pool_Suffix) then
846 return BIP_Storage_Pool;
848 elsif Has_Suffix (BIP_Finalization_Master_Suffix) then
849 return BIP_Finalization_Master;
851 elsif Has_Suffix (BIP_Task_Master_Suffix) then
852 return BIP_Task_Master;
854 elsif Has_Suffix (BIP_Activation_Chain_Suffix) then
855 return BIP_Activation_Chain;
857 elsif Has_Suffix (BIP_Object_Access_Suffix) then
858 return BIP_Object_Access;
865 ---------------------------
866 -- Build_In_Place_Formal --
867 ---------------------------
869 function Build_In_Place_Formal
871 Kind : BIP_Formal_Kind) return Entity_Id
873 Extra_Formal : Entity_Id := Extra_Formals (Func);
874 Formal_Suffix : constant String := BIP_Formal_Suffix (Kind);
877 -- Maybe it would be better for each implicit formal of a build-in-place
878 -- function to have a flag or a Uint attribute to identify it. ???
880 -- The return type in the function declaration may have been a limited
881 -- view, and the extra formals for the function were not generated at
882 -- that point. At the point of call the full view must be available and
883 -- the extra formals can be created and Returns_By_Ref computed.
885 if No (Extra_Formal) then
886 Create_Extra_Formals (Func);
887 Extra_Formal := Extra_Formals (Func);
888 Compute_Returns_By_Ref (Func);
891 -- We search for a formal with a matching suffix. We can't search
892 -- for the full name, because of the code at the end of Sem_Ch6.-
893 -- Create_Extra_Formals, which copies the Extra_Formals over to
894 -- the Alias of an instance, which will cause the formals to have
895 -- "incorrect" names.
898 pragma Assert (Present (Extra_Formal));
900 Name : constant String := Get_Name_String (Chars (Extra_Formal));
902 exit when Name'Length >= Formal_Suffix'Length
903 and then Formal_Suffix =
904 Name (Name'Last - Formal_Suffix'Length + 1 .. Name'Last);
907 Next_Formal_With_Extras (Extra_Formal);
911 end Build_In_Place_Formal;
913 -------------------------------
914 -- Build_Procedure_Body_Form --
915 -------------------------------
917 function Build_Procedure_Body_Form
918 (Func_Id : Entity_Id;
919 Func_Body : Node_Id) return Node_Id
921 Loc : constant Source_Ptr := Sloc (Func_Body);
923 Proc_Decl : constant Node_Id := Prev (Unit_Declaration_Node (Func_Id));
924 -- It is assumed that the node before the declaration of the
925 -- corresponding subprogram spec is the declaration of the procedure
928 Proc_Id : constant Entity_Id := Defining_Entity (Proc_Decl);
930 procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id);
931 -- Replace each return statement found in the list Stmts with an
932 -- assignment of the return expression to parameter Param_Id.
934 ---------------------
935 -- Replace_Returns --
936 ---------------------
938 procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id) is
942 Stmt := First (Stmts);
943 while Present (Stmt) loop
944 if Nkind (Stmt) = N_Block_Statement then
945 Replace_Returns (Param_Id,
946 Statements (Handled_Statement_Sequence (Stmt)));
948 elsif Nkind (Stmt) = N_Case_Statement then
952 Alt := First (Alternatives (Stmt));
953 while Present (Alt) loop
954 Replace_Returns (Param_Id, Statements (Alt));
959 elsif Nkind (Stmt) = N_Extended_Return_Statement then
961 Ret_Obj : constant Entity_Id :=
963 (First (Return_Object_Declarations (Stmt)));
964 Assign : constant Node_Id :=
965 Make_Assignment_Statement (Sloc (Stmt),
967 New_Occurrence_Of (Param_Id, Loc),
969 New_Occurrence_Of (Ret_Obj, Sloc (Stmt)));
973 -- The extended return may just contain the declaration
975 if Present (Handled_Statement_Sequence (Stmt)) then
976 Stmts := Statements (Handled_Statement_Sequence (Stmt));
981 Set_Assignment_OK (Name (Assign));
984 Make_Block_Statement (Sloc (Stmt),
986 Return_Object_Declarations (Stmt),
987 Handled_Statement_Sequence =>
988 Make_Handled_Sequence_Of_Statements (Loc,
989 Statements => Stmts)));
991 Replace_Returns (Param_Id, Stmts);
993 Append_To (Stmts, Assign);
994 Append_To (Stmts, Make_Simple_Return_Statement (Loc));
997 elsif Nkind (Stmt) = N_If_Statement then
998 Replace_Returns (Param_Id, Then_Statements (Stmt));
999 Replace_Returns (Param_Id, Else_Statements (Stmt));
1004 Part := First (Elsif_Parts (Stmt));
1005 while Present (Part) loop
1006 Replace_Returns (Param_Id, Then_Statements (Part));
1011 elsif Nkind (Stmt) = N_Loop_Statement then
1012 Replace_Returns (Param_Id, Statements (Stmt));
1014 elsif Nkind (Stmt) = N_Simple_Return_Statement then
1021 Make_Assignment_Statement (Sloc (Stmt),
1022 Name => New_Occurrence_Of (Param_Id, Loc),
1023 Expression => Relocate_Node (Expression (Stmt))));
1025 Insert_After (Stmt, Make_Simple_Return_Statement (Loc));
1027 -- Skip the added return
1034 end Replace_Returns;
1041 -- Start of processing for Build_Procedure_Body_Form
1044 -- This routine replaces the original function body:
1046 -- function F (...) return Array_Typ is
1049 -- return Something;
1052 -- with the following:
1054 -- procedure P (..., Result : out Array_Typ) is
1057 -- Result := Something;
1061 Statements (Handled_Statement_Sequence (Func_Body));
1062 Replace_Returns (Last_Entity (Proc_Id), Stmts);
1065 Make_Subprogram_Body (Loc,
1067 Copy_Subprogram_Spec (Specification (Proc_Decl)),
1068 Declarations => Declarations (Func_Body),
1069 Handled_Statement_Sequence =>
1070 Make_Handled_Sequence_Of_Statements (Loc,
1071 Statements => Stmts));
1073 -- If the function is a generic instance, so is the new procedure.
1074 -- Set flag accordingly so that the proper renaming declarations are
1077 Set_Is_Generic_Instance (Proc_Id, Is_Generic_Instance (Func_Id));
1079 end Build_Procedure_Body_Form;
1081 -----------------------
1082 -- Caller_Known_Size --
1083 -----------------------
1085 function Caller_Known_Size
1086 (Func_Call : Node_Id;
1087 Result_Subt : Entity_Id) return Boolean
1089 Utyp : constant Entity_Id := Underlying_Type (Result_Subt);
1092 return not Needs_Secondary_Stack (Utyp)
1093 and then not (Is_Tagged_Type (Utyp)
1094 and then Present (Controlling_Argument (Func_Call)));
1095 end Caller_Known_Size;
1097 -----------------------
1098 -- Check_BIP_Actuals --
1099 -----------------------
1101 function Check_BIP_Actuals
1102 (Subp_Call : Node_Id;
1103 Subp_Id : Entity_Id) return Boolean
1109 pragma Assert (Nkind (Subp_Call) in N_Entry_Call_Statement
1111 | N_Procedure_Call_Statement);
1113 -- In CodePeer_Mode, the tree for `'Elab_Spec` procedures will be
1114 -- malformed because GNAT does not perform the usual expansion that
1115 -- results in the importation of external elaboration procedure symbols.
1116 -- This is expected: the CodePeer backend has special handling for this
1118 -- Thus, we do not need to check the tree (and in fact can't, because
1122 and then Nkind (Name (Subp_Call)) = N_Attribute_Reference
1123 and then Attribute_Name (Name (Subp_Call)) in Name_Elab_Spec
1125 | Name_Elab_Subp_Body
1130 Formal := First_Formal_With_Extras (Subp_Id);
1131 Actual := First_Actual (Subp_Call);
1133 while Present (Formal) and then Present (Actual) loop
1134 if Is_Build_In_Place_Entity (Formal)
1135 and then Nkind (Actual) = N_Identifier
1136 and then Is_Build_In_Place_Entity (Entity (Actual))
1137 and then BIP_Suffix_Kind (Formal)
1138 /= BIP_Suffix_Kind (Entity (Actual))
1143 Next_Formal_With_Extras (Formal);
1144 Next_Actual (Actual);
1147 return No (Formal) and then No (Actual);
1148 end Check_BIP_Actuals;
1150 -----------------------------
1151 -- Check_Number_Of_Actuals --
1152 -----------------------------
1154 function Check_Number_Of_Actuals
1155 (Subp_Call : Node_Id;
1156 Subp_Id : Entity_Id) return Boolean
1162 pragma Assert (Nkind (Subp_Call) in N_Entry_Call_Statement
1164 | N_Procedure_Call_Statement);
1166 Formal := First_Formal_With_Extras (Subp_Id);
1167 Actual := First_Actual (Subp_Call);
1169 while Present (Formal) and then Present (Actual) loop
1170 Next_Formal_With_Extras (Formal);
1171 Next_Actual (Actual);
1174 return No (Formal) and then No (Actual);
1175 end Check_Number_Of_Actuals;
1177 --------------------------------
1178 -- Check_Overriding_Operation --
1179 --------------------------------
1181 procedure Check_Overriding_Operation (Subp : Entity_Id) is
1182 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
1183 Op_List : constant Elist_Id := Primitive_Operations (Typ);
1185 Prim_Op : Entity_Id;
1189 if Is_Derived_Type (Typ)
1190 and then not Is_Private_Type (Typ)
1191 and then In_Open_Scopes (Scope (Etype (Typ)))
1192 and then Is_Base_Type (Typ)
1194 -- Subp overrides an inherited private operation if there is an
1195 -- inherited operation with a different name than Subp (see
1196 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
1197 -- same name as Subp.
1199 Op_Elmt := First_Elmt (Op_List);
1200 while Present (Op_Elmt) loop
1201 Prim_Op := Node (Op_Elmt);
1202 Par_Op := Alias (Prim_Op);
1205 and then not Comes_From_Source (Prim_Op)
1206 and then Chars (Prim_Op) /= Chars (Par_Op)
1207 and then Chars (Par_Op) = Chars (Subp)
1208 and then Is_Hidden (Par_Op)
1209 and then Type_Conformant (Prim_Op, Subp)
1211 Set_DT_Position_Value (Subp, DT_Position (Prim_Op));
1214 Next_Elmt (Op_Elmt);
1217 end Check_Overriding_Operation;
1219 -------------------------------
1220 -- Detect_Infinite_Recursion --
1221 -------------------------------
1223 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
1224 Loc : constant Source_Ptr := Sloc (N);
1226 Var_List : constant Elist_Id := New_Elmt_List;
1227 -- List of globals referenced by body of procedure
1229 Call_List : constant Elist_Id := New_Elmt_List;
1230 -- List of recursive calls in body of procedure
1232 Shad_List : constant Elist_Id := New_Elmt_List;
1233 -- List of entity id's for entities created to capture the value of
1234 -- referenced globals on entry to the procedure.
1236 Scop : constant Uint := Scope_Depth (Spec);
1237 -- This is used to record the scope depth of the current procedure, so
1238 -- that we can identify global references.
1240 Max_Vars : constant := 4;
1241 -- Do not test more than four global variables
1243 Count_Vars : Natural := 0;
1244 -- Count variables found so far
1256 function Process (Nod : Node_Id) return Traverse_Result;
1257 -- Function to traverse the subprogram body (using Traverse_Func)
1263 function Process (Nod : Node_Id) return Traverse_Result is
1267 if Nkind (Nod) = N_Procedure_Call_Statement then
1269 -- Case of one of the detected recursive calls
1271 if Is_Entity_Name (Name (Nod))
1272 and then Has_Recursive_Call (Entity (Name (Nod)))
1273 and then Entity (Name (Nod)) = Spec
1275 Append_Elmt (Nod, Call_List);
1278 -- Any other procedure call may have side effects
1284 -- A call to a pure function can always be ignored
1286 elsif Nkind (Nod) = N_Function_Call
1287 and then Is_Entity_Name (Name (Nod))
1288 and then Is_Pure (Entity (Name (Nod)))
1292 -- Case of an identifier reference
1294 elsif Nkind (Nod) = N_Identifier then
1295 Ent := Entity (Nod);
1297 -- If no entity, then ignore the reference
1299 -- Not clear why this can happen. To investigate, remove this
1300 -- test and look at the crash that occurs here in 3401-004 ???
1305 -- Ignore entities with no Scope, again not clear how this
1306 -- can happen, to investigate, look at 4108-008 ???
1308 elsif No (Scope (Ent)) then
1311 -- Ignore the reference if not to a more global object
1313 elsif Scope_Depth (Scope (Ent)) >= Scop then
1316 -- References to types, exceptions and constants are always OK
1319 or else Ekind (Ent) = E_Exception
1320 or else Ekind (Ent) = E_Constant
1324 -- If other than a non-volatile scalar variable, we have some
1325 -- kind of global reference (e.g. to a function) that we cannot
1326 -- deal with so we forget the attempt.
1328 elsif Ekind (Ent) /= E_Variable
1329 or else not Is_Scalar_Type (Etype (Ent))
1330 or else Treat_As_Volatile (Ent)
1334 -- Otherwise we have a reference to a global scalar
1337 -- Loop through global entities already detected
1339 Elm := First_Elmt (Var_List);
1341 -- If not detected before, record this new global reference
1344 Count_Vars := Count_Vars + 1;
1346 if Count_Vars <= Max_Vars then
1347 Append_Elmt (Entity (Nod), Var_List);
1354 -- If recorded before, ignore
1356 elsif Node (Elm) = Entity (Nod) then
1359 -- Otherwise keep looking
1369 -- For all other node kinds, recursively visit syntactic children
1376 function Traverse_Body is new Traverse_Func (Process);
1378 -- Start of processing for Detect_Infinite_Recursion
1381 -- Do not attempt detection in No_Implicit_Conditional mode, since we
1382 -- won't be able to generate the code to handle the recursion in any
1385 if Restriction_Active (No_Implicit_Conditionals) then
1389 -- Otherwise do traversal and quit if we get abandon signal
1391 if Traverse_Body (N) = Abandon then
1394 -- We must have a call, since Has_Recursive_Call was set. If not just
1395 -- ignore (this is only an error check, so if we have a funny situation,
1396 -- due to bugs or errors, we do not want to bomb).
1398 elsif Is_Empty_Elmt_List (Call_List) then
1402 -- Here is the case where we detect recursion at compile time
1404 -- Push our current scope for analyzing the declarations and code that
1405 -- we will insert for the checking.
1409 -- This loop builds temporary variables for each of the referenced
1410 -- globals, so that at the end of the loop the list Shad_List contains
1411 -- these temporaries in one-to-one correspondence with the elements in
1415 Elm := First_Elmt (Var_List);
1416 while Present (Elm) loop
1418 Ent := Make_Temporary (Loc, 'S');
1419 Append_Elmt (Ent, Shad_List);
1421 -- Insert a declaration for this temporary at the start of the
1422 -- declarations for the procedure. The temporaries are declared as
1423 -- constant objects initialized to the current values of the
1424 -- corresponding temporaries.
1427 Make_Object_Declaration (Loc,
1428 Defining_Identifier => Ent,
1429 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
1430 Constant_Present => True,
1431 Expression => New_Occurrence_Of (Var, Loc));
1434 Prepend (Decl, Declarations (N));
1436 Insert_After (Last, Decl);
1444 -- Loop through calls
1446 Call := First_Elmt (Call_List);
1447 while Present (Call) loop
1449 -- Build a predicate expression of the form
1452 -- and then global1 = temp1
1453 -- and then global2 = temp2
1456 -- This predicate determines if any of the global values
1457 -- referenced by the procedure have changed since the
1458 -- current call, if not an infinite recursion is assured.
1460 Test := New_Occurrence_Of (Standard_True, Loc);
1462 Elm1 := First_Elmt (Var_List);
1463 Elm2 := First_Elmt (Shad_List);
1464 while Present (Elm1) loop
1470 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
1471 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
1477 -- Now we replace the call with the sequence
1479 -- if no-changes (see above) then
1480 -- raise Storage_Error;
1485 Rewrite (Node (Call),
1486 Make_If_Statement (Loc,
1488 Then_Statements => New_List (
1489 Make_Raise_Storage_Error (Loc,
1490 Reason => SE_Infinite_Recursion)),
1492 Else_Statements => New_List (
1493 Relocate_Node (Node (Call)))));
1495 Analyze (Node (Call));
1500 -- Remove temporary scope stack entry used for analysis
1503 end Detect_Infinite_Recursion;
1505 --------------------
1506 -- Expand_Actuals --
1507 --------------------
1509 procedure Expand_Actuals
1512 Post_Call : out List_Id)
1514 Loc : constant Source_Ptr := Sloc (N);
1518 E_Actual : Entity_Id;
1519 E_Formal : Entity_Id;
1521 procedure Add_Call_By_Copy_Code;
1522 -- For cases where the parameter must be passed by copy, this routine
1523 -- generates a temporary variable into which the actual is copied and
1524 -- then passes this as the parameter. For an OUT or IN OUT parameter,
1525 -- an assignment is also generated to copy the result back. The call
1526 -- also takes care of any constraint checks required for the type
1527 -- conversion case (on both the way in and the way out).
1529 procedure Add_Simple_Call_By_Copy_Code (Force : Boolean);
1530 -- This is similar to the above, but is used in cases where we know
1531 -- that all that is needed is to simply create a temporary and copy
1532 -- the value in and out of the temporary. If Force is True, then the
1533 -- procedure may disregard legality considerations.
1535 -- ??? We need to do the copy for a bit-packed array because this is
1536 -- where the rewriting into a mask-and-shift sequence is done. But of
1537 -- course this may break the program if it expects bits to be really
1538 -- passed by reference. That's what we have done historically though.
1540 procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id);
1541 -- Perform copy-back for actual parameter Act which denotes a validation
1544 procedure Check_Fortran_Logical;
1545 -- A value of type Logical that is passed through a formal parameter
1546 -- must be normalized because .TRUE. usually does not have the same
1547 -- representation as True. We assume that .FALSE. = False = 0.
1548 -- What about functions that return a logical type ???
1550 function Is_Legal_Copy return Boolean;
1551 -- Check that an actual can be copied before generating the temporary
1552 -- to be used in the call. If the formal is of a by_reference type or
1553 -- is aliased, then the program is illegal (this can only happen in
1554 -- the presence of representation clauses that force a misalignment)
1555 -- If the formal is a by_reference parameter imposed by a DEC pragma,
1556 -- emit a warning that this might lead to unaligned arguments.
1558 function Make_Var (Actual : Node_Id) return Entity_Id;
1559 -- Returns an entity that refers to the given actual parameter, Actual
1560 -- (not including any type conversion). If Actual is an entity name,
1561 -- then this entity is returned unchanged, otherwise a renaming is
1562 -- created to provide an entity for the actual.
1564 procedure Reset_Packed_Prefix;
1565 -- The expansion of a packed array component reference is delayed in
1566 -- the context of a call. Now we need to complete the expansion, so we
1567 -- unmark the analyzed bits in all prefixes.
1569 function Requires_Atomic_Or_Volatile_Copy return Boolean;
1570 -- Returns whether a copy is required as per RM C.6(19) and gives a
1571 -- warning in this case.
1573 ---------------------------
1574 -- Add_Call_By_Copy_Code --
1575 ---------------------------
1577 procedure Add_Call_By_Copy_Code is
1580 F_Typ : Entity_Id := Etype (Formal);
1588 if not Is_Legal_Copy then
1592 Temp := Make_Temporary (Loc, 'T', Actual);
1594 -- Handle formals whose type comes from the limited view
1596 if From_Limited_With (F_Typ)
1597 and then Has_Non_Limited_View (F_Typ)
1599 F_Typ := Non_Limited_View (F_Typ);
1602 -- Use formal type for temp, unless formal type is an unconstrained
1603 -- array, in which case we don't have to worry about bounds checks,
1604 -- and we use the actual type, since that has appropriate bounds.
1606 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1607 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1609 Indic := New_Occurrence_Of (F_Typ, Loc);
1612 -- The new code will be properly analyzed below and the setting of
1613 -- the Do_Range_Check flag recomputed so remove the obsolete one.
1615 Set_Do_Range_Check (Actual, False);
1617 if Nkind (Actual) = N_Type_Conversion then
1618 Set_Do_Range_Check (Expression (Actual), False);
1620 V_Typ := Etype (Expression (Actual));
1622 -- If the formal is an (in-)out parameter, capture the name
1623 -- of the variable in order to build the post-call assignment.
1625 Var := Make_Var (Expression (Actual));
1627 Crep := not Has_Compatible_Representation
1628 (Target_Typ => F_Typ,
1629 Operand_Typ => Etype (Expression (Actual)));
1632 V_Typ := Etype (Actual);
1633 Var := Make_Var (Actual);
1637 -- If the actual denotes a variable which captures the value of an
1638 -- object for validation purposes, we propagate the link with this
1639 -- object to the new variable made from the actual just above.
1641 if Ekind (Formal) /= E_In_Parameter
1642 and then Is_Validation_Variable_Reference (Actual)
1645 Ref : constant Node_Id := Unqual_Conv (Actual);
1648 if Is_Entity_Name (Ref) then
1649 Set_Validated_Object (Var, Validated_Object (Entity (Ref)));
1652 pragma Assert (False);
1658 -- Setup initialization for case of in out parameter, or an out
1659 -- parameter where the formal is an unconstrained array (in the
1660 -- latter case, we have to pass in an object with bounds).
1662 -- If this is an out parameter, the initial copy is wasteful, so as
1663 -- an optimization for the one-dimensional case we extract the
1664 -- bounds of the actual and build an uninitialized temporary of the
1667 -- If the formal is an out parameter with discriminants, the
1668 -- discriminants must be captured even if the rest of the object
1669 -- is in principle uninitialized, because the discriminants may
1670 -- be read by the called subprogram.
1672 if Ekind (Formal) = E_In_Out_Parameter
1673 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1674 or else Has_Discriminants (F_Typ)
1676 if Nkind (Actual) = N_Type_Conversion then
1677 if Conversion_OK (Actual) then
1678 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1680 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1683 elsif Ekind (Formal) = E_Out_Parameter
1684 and then Is_Array_Type (F_Typ)
1685 and then Number_Dimensions (F_Typ) = 1
1686 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1688 -- Actual is a one-dimensional array or slice, and the type
1689 -- requires no initialization. Create a temporary of the
1690 -- right size, but do not copy actual into it (optimization).
1694 Make_Subtype_Indication (Loc,
1695 Subtype_Mark => New_Occurrence_Of (F_Typ, Loc),
1697 Make_Index_Or_Discriminant_Constraint (Loc,
1698 Constraints => New_List (
1701 Make_Attribute_Reference (Loc,
1702 Prefix => New_Occurrence_Of (Var, Loc),
1703 Attribute_Name => Name_First),
1705 Make_Attribute_Reference (Loc,
1706 Prefix => New_Occurrence_Of (Var, Loc),
1707 Attribute_Name => Name_Last)))));
1710 Init := New_Occurrence_Of (Var, Loc);
1713 -- An initialization is created for packed conversions as
1714 -- actuals for out parameters to enable Make_Object_Declaration
1715 -- to determine the proper subtype for N_Node. Note that this
1716 -- is wasteful because the extra copying on the call side is
1717 -- not required for such out parameters. ???
1719 elsif Ekind (Formal) = E_Out_Parameter
1720 and then Nkind (Actual) = N_Type_Conversion
1721 and then (Is_Bit_Packed_Array (F_Typ)
1723 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1725 if Conversion_OK (Actual) then
1726 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1728 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1731 elsif Ekind (Formal) = E_In_Parameter then
1733 -- Handle the case in which the actual is a type conversion
1735 if Nkind (Actual) = N_Type_Conversion then
1736 if Conversion_OK (Actual) then
1737 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1739 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1742 Init := New_Occurrence_Of (Var, Loc);
1745 -- Access types are passed in without checks, but if a copy-back is
1746 -- required for a null-excluding check on an in-out or out parameter,
1747 -- then the initial value is that of the actual.
1749 elsif Is_Access_Type (E_Formal)
1750 and then Can_Never_Be_Null (Etype (Actual))
1751 and then not Can_Never_Be_Null (E_Formal)
1753 Init := New_Occurrence_Of (Var, Loc);
1755 -- View conversions when the formal type has the Default_Value aspect
1756 -- require passing in the value of the conversion's operand. The type
1757 -- of that operand also has Default_Value, as required by AI12-0074
1758 -- (RM 6.4.1(5.3/4)). The subtype denoted by the subtype_indication
1759 -- is changed to the base type of the formal subtype, to ensure that
1760 -- the actual's value can be assigned without a constraint check
1761 -- (note that no check is done on passing to an out parameter). Also
1762 -- note that the two types necessarily share the same ancestor type,
1763 -- as required by 6.4.1(5.2/4), so underlying base types will match.
1765 elsif Ekind (Formal) = E_Out_Parameter
1766 and then Is_Scalar_Type (Etype (F_Typ))
1767 and then Nkind (Actual) = N_Type_Conversion
1768 and then Present (Default_Aspect_Value (Etype (F_Typ)))
1770 Indic := New_Occurrence_Of (Base_Type (F_Typ), Loc);
1772 (Base_Type (F_Typ), New_Occurrence_Of (Var, Loc));
1779 Make_Object_Declaration (Loc,
1780 Defining_Identifier => Temp,
1781 Object_Definition => Indic,
1782 Expression => Init);
1783 Set_Assignment_OK (N_Node);
1784 Insert_Action (N, N_Node);
1786 -- Now, normally the deal here is that we use the defining
1787 -- identifier created by that object declaration. There is
1788 -- one exception to this. In the change of representation case
1789 -- the above declaration will end up looking like:
1791 -- temp : type := identifier;
1793 -- And in this case we might as well use the identifier directly
1794 -- and eliminate the temporary. Note that the analysis of the
1795 -- declaration was not a waste of time in that case, since it is
1796 -- what generated the necessary change of representation code. If
1797 -- the change of representation introduced additional code, as in
1798 -- a fixed-integer conversion, the expression is not an identifier
1799 -- and must be kept.
1802 and then Present (Expression (N_Node))
1803 and then Is_Entity_Name (Expression (N_Node))
1805 Temp := Entity (Expression (N_Node));
1806 Rewrite (N_Node, Make_Null_Statement (Loc));
1809 -- For IN parameter, all we do is to replace the actual
1811 if Ekind (Formal) = E_In_Parameter then
1812 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1815 -- Processing for OUT or IN OUT parameter
1818 -- Kill current value indications for the temporary variable we
1819 -- created, since we just passed it as an OUT parameter.
1821 Kill_Current_Values (Temp);
1822 Set_Is_Known_Valid (Temp, False);
1823 Set_Is_True_Constant (Temp, False);
1825 -- If type conversion, use reverse conversion on exit
1827 if Nkind (Actual) = N_Type_Conversion then
1828 if Conversion_OK (Actual) then
1829 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1831 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1834 Expr := New_Occurrence_Of (Temp, Loc);
1837 Rewrite (Actual, New_Occurrence_Of (Temp, Sloc (Actual)));
1840 -- If the actual is a conversion of a packed reference, it may
1841 -- already have been expanded by Remove_Side_Effects, and the
1842 -- resulting variable is a temporary which does not designate
1843 -- the proper out-parameter, which may not be addressable. In
1844 -- that case, generate an assignment to the original expression
1845 -- (before expansion of the packed reference) so that the proper
1846 -- expansion of assignment to a packed component can take place.
1853 if Is_Renaming_Of_Object (Var)
1854 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1855 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1856 = N_Indexed_Component
1858 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1860 Obj := Renamed_Object (Var);
1862 Make_Selected_Component (Loc,
1864 New_Copy_Tree (Original_Node (Prefix (Obj))),
1865 Selector_Name => New_Copy (Selector_Name (Obj)));
1866 Reset_Analyzed_Flags (Lhs);
1869 Lhs := New_Occurrence_Of (Var, Loc);
1872 Set_Assignment_OK (Lhs);
1874 if Is_Access_Type (E_Formal)
1875 and then Is_Entity_Name (Lhs)
1877 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1878 and then not No_Dynamic_Accessibility_Checks_Enabled (Lhs)
1880 -- Copyback target is an Ada 2012 stand-alone object of an
1881 -- anonymous access type.
1883 pragma Assert (Ada_Version >= Ada_2012);
1885 Apply_Accessibility_Check (Lhs, E_Formal, N);
1887 Append_To (Post_Call,
1888 Make_Assignment_Statement (Loc,
1890 Expression => Expr));
1892 -- We would like to somehow suppress generation of the
1893 -- extra_accessibility assignment generated by the expansion
1894 -- of the above assignment statement. It's not a correctness
1895 -- issue because the following assignment renders it dead,
1896 -- but generating back-to-back assignments to the same
1897 -- target is undesirable. ???
1899 Append_To (Post_Call,
1900 Make_Assignment_Statement (Loc,
1901 Name => New_Occurrence_Of (
1902 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1903 Expression => Make_Integer_Literal (Loc,
1904 Type_Access_Level (E_Formal))));
1907 if Is_Access_Type (E_Formal)
1908 and then Can_Never_Be_Null (Etype (Actual))
1909 and then not Can_Never_Be_Null (E_Formal)
1911 Append_To (Post_Call,
1912 Make_Raise_Constraint_Error (Loc,
1915 Left_Opnd => New_Occurrence_Of (Temp, Loc),
1916 Right_Opnd => Make_Null (Loc)),
1917 Reason => CE_Access_Check_Failed));
1920 Append_To (Post_Call,
1921 Make_Assignment_Statement (Loc,
1923 Expression => Expr));
1926 -- Add a copy-back to reflect any potential changes in value
1927 -- back into the original object, if any.
1929 if Is_Validation_Variable_Reference (Lhs) then
1930 Add_Validation_Call_By_Copy_Code (Lhs);
1934 end Add_Call_By_Copy_Code;
1936 ----------------------------------
1937 -- Add_Simple_Call_By_Copy_Code --
1938 ----------------------------------
1940 procedure Add_Simple_Call_By_Copy_Code (Force : Boolean) is
1941 With_Storage_Model : constant Boolean :=
1942 Nkind (Actual) = N_Explicit_Dereference
1944 Has_Designated_Storage_Model_Aspect (Etype (Prefix (Actual)));
1957 -- Unless forced not to, check the legality of the copy operation
1959 if not Force and then not Is_Legal_Copy then
1963 F_Typ := Etype (Formal);
1965 -- Handle formals whose type comes from the limited view
1967 if From_Limited_With (F_Typ)
1968 and then Has_Non_Limited_View (F_Typ)
1970 F_Typ := Non_Limited_View (F_Typ);
1973 -- Use formal type for temp, unless formal type is an unconstrained
1974 -- composite, in which case we don't have to worry about checks and
1975 -- we can use the actual type, since that has appropriate bounds.
1977 if Is_Composite_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1978 Indic := New_Occurrence_Of (Get_Actual_Subtype (Actual), Loc);
1980 Indic := New_Occurrence_Of (F_Typ, Loc);
1983 -- Prepare to generate code
1985 Reset_Packed_Prefix;
1987 Incod := Relocate_Node (Actual);
1988 Outcod := New_Copy_Tree (Incod);
1990 -- Generate declaration of temporary variable, initializing it
1991 -- with the input parameter unless we have an OUT formal or
1992 -- this is an initialization call.
1994 if Ekind (Formal) = E_Out_Parameter then
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
2006 not Has_Discriminant_Dependent_Constraint
2007 (Entity (Selector_Name (Actual)))
2011 -- Otherwise, keep the component so we can generate the proper
2012 -- actual subtype - since the subtype depends on enclosing
2022 if With_Storage_Model then
2024 Build_Temporary_On_Secondary_Stack (Loc, Entity (Indic), Cpcod);
2026 if Present (Incod) then
2028 Make_Assignment_Statement (Loc,
2030 Make_Explicit_Dereference (Loc,
2031 Prefix => New_Occurrence_Of (Temp, Loc)),
2032 Expression => Incod));
2033 Set_Suppress_Assignment_Checks (Last (Cpcod));
2037 Temp := Make_Temporary (Loc, 'T', Actual);
2040 Make_Object_Declaration (Loc,
2041 Defining_Identifier => Temp,
2042 Object_Definition => Indic,
2043 Expression => Incod);
2045 -- If the call is to initialize a component of a composite type,
2046 -- and the component does not depend on discriminants, use the
2047 -- actual type of the component. This is required in case the
2048 -- component is constrained, because in general the formal of the
2049 -- initialization procedure will be unconstrained. Note that if
2050 -- the component being initialized is constrained by an enclosing
2051 -- discriminant, the presence of the initialization in the
2052 -- declaration will generate an expression for the actual subtype.
2054 if Inside_Init_Proc and then No (Incod) then
2055 Set_No_Initialization (Decl);
2056 Set_Object_Definition (Decl,
2057 New_Occurrence_Of (Etype (Actual), Loc));
2060 Append_To (Cpcod, Decl);
2063 Insert_Actions (N, Cpcod);
2065 -- The actual is simply a reference to the temporary
2067 if With_Storage_Model then
2069 Make_Explicit_Dereference (Loc,
2070 Prefix => New_Occurrence_Of (Temp, Loc)));
2072 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
2077 -- Generate copy out if OUT or IN OUT parameter
2079 if Ekind (Formal) /= E_In_Parameter then
2082 if With_Storage_Model then
2084 Make_Explicit_Dereference (Loc,
2085 Prefix => New_Occurrence_Of (Temp, Loc));
2087 Rhs := New_Occurrence_Of (Temp, Loc);
2088 Set_Is_True_Constant (Temp, False);
2091 -- Deal with conversion
2093 if Nkind (Lhs) = N_Type_Conversion then
2094 Lhs := Expression (Lhs);
2095 Rhs := Convert_To (Etype (Actual), Rhs);
2098 Append_To (Post_Call,
2099 Make_Assignment_Statement (Loc,
2101 Expression => Rhs));
2102 Set_Suppress_Assignment_Checks (Last (Post_Call));
2103 Set_Assignment_OK (Name (Last (Post_Call)));
2105 end Add_Simple_Call_By_Copy_Code;
2107 --------------------------------------
2108 -- Add_Validation_Call_By_Copy_Code --
2109 --------------------------------------
2111 procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id) is
2112 Var : constant Node_Id := Unqual_Conv (Act);
2116 Obj_Typ : Entity_Id;
2120 -- Generate range check if required
2122 if Do_Range_Check (Actual) then
2123 Generate_Range_Check (Actual, E_Formal, CE_Range_Check_Failed);
2126 -- If there is a type conversion in the actual, it will be reinstated
2127 -- below, the new instance will be properly analyzed and the setting
2128 -- of the Do_Range_Check flag recomputed so remove the obsolete one.
2130 if Nkind (Actual) = N_Type_Conversion then
2131 Set_Do_Range_Check (Expression (Actual), False);
2134 -- Copy the value of the validation variable back into the object
2137 if Is_Entity_Name (Var) then
2138 Var_Id := Entity (Var);
2139 Obj := Validated_Object (Var_Id);
2140 Obj_Typ := Etype (Obj);
2142 Expr := New_Occurrence_Of (Var_Id, Loc);
2144 -- A type conversion is needed when the validation variable and
2145 -- the validated object carry different types. This case occurs
2146 -- when the actual is qualified in some fashion.
2149 -- subtype Int is Integer range ...;
2150 -- procedure Call (Val : in out Integer);
2154 -- Call (Integer (Object));
2158 -- Var : Integer := Object; -- conversion to base type
2159 -- if not Var'Valid then -- validity check
2160 -- Call (Var); -- modify Var
2161 -- Object := Int (Var); -- conversion to subtype
2163 if Etype (Var_Id) /= Obj_Typ then
2165 Make_Type_Conversion (Loc,
2166 Subtype_Mark => New_Occurrence_Of (Obj_Typ, Loc),
2167 Expression => Expr);
2173 -- Object := Object_Type (Var);
2175 Append_To (Post_Call,
2176 Make_Assignment_Statement (Loc,
2178 Expression => Expr));
2180 -- If the flow reaches this point, then this routine was invoked with
2181 -- an actual which does not denote a validation variable.
2184 pragma Assert (False);
2187 end Add_Validation_Call_By_Copy_Code;
2189 ---------------------------
2190 -- Check_Fortran_Logical --
2191 ---------------------------
2193 procedure Check_Fortran_Logical is
2194 Logical : constant Entity_Id := Etype (Formal);
2197 -- Note: this is very incomplete, e.g. it does not handle arrays
2198 -- of logical values. This is really not the right approach at all???)
2201 if Convention (Subp) = Convention_Fortran
2202 and then Root_Type (Etype (Formal)) = Standard_Boolean
2203 and then Ekind (Formal) /= E_In_Parameter
2205 Var := Make_Var (Actual);
2206 Append_To (Post_Call,
2207 Make_Assignment_Statement (Loc,
2208 Name => New_Occurrence_Of (Var, Loc),
2210 Unchecked_Convert_To (
2213 Left_Opnd => New_Occurrence_Of (Var, Loc),
2215 Unchecked_Convert_To (
2217 New_Occurrence_Of (Standard_False, Loc))))));
2219 end Check_Fortran_Logical;
2225 function Is_Legal_Copy return Boolean is
2227 -- An attempt to copy a value of such a type can only occur if
2228 -- representation clauses give the actual a misaligned address.
2230 if Is_By_Reference_Type (Etype (Formal))
2231 or else Is_Aliased (Formal)
2232 or else (Mechanism (Formal) = By_Reference
2233 and then not Has_Foreign_Convention (Subp))
2236 -- The actual may in fact be properly aligned but there is not
2237 -- enough front-end information to determine this. In that case
2238 -- gigi will emit an error or a warning if a copy is not legal,
2239 -- or generate the proper code.
2243 -- For users of Starlet, we assume that the specification of by-
2244 -- reference mechanism is mandatory. This may lead to unaligned
2245 -- objects but at least for DEC legacy code it is known to work.
2246 -- The warning will alert users of this code that a problem may
2249 elsif Mechanism (Formal) = By_Reference
2250 and then Ekind (Scope (Formal)) = E_Procedure
2251 and then Is_Valued_Procedure (Scope (Formal))
2254 ("by_reference actual may be misaligned??", Actual);
2266 function Make_Var (Actual : Node_Id) return Entity_Id is
2270 if Is_Entity_Name (Actual) then
2271 return Entity (Actual);
2274 Var := Make_Temporary (Loc, 'T', Actual);
2277 Make_Object_Renaming_Declaration (Loc,
2278 Defining_Identifier => Var,
2280 New_Occurrence_Of (Etype (Actual), Loc),
2281 Name => Relocate_Node (Actual));
2283 Insert_Action (N, N_Node);
2288 -------------------------
2289 -- Reset_Packed_Prefix --
2290 -------------------------
2292 procedure Reset_Packed_Prefix is
2293 Pfx : Node_Id := Actual;
2296 Set_Analyzed (Pfx, False);
2298 Nkind (Pfx) not in N_Selected_Component | N_Indexed_Component;
2299 Pfx := Prefix (Pfx);
2301 end Reset_Packed_Prefix;
2303 ----------------------------------------
2304 -- Requires_Atomic_Or_Volatile_Copy --
2305 ----------------------------------------
2307 function Requires_Atomic_Or_Volatile_Copy return Boolean is
2309 -- If the formal is already passed by copy, no need to do anything
2311 if Is_By_Copy_Type (E_Formal) then
2315 -- There is no requirement inside initialization procedures and this
2316 -- would generate copies for atomic or volatile composite components.
2318 if Inside_Init_Proc then
2322 -- Check for atomicity mismatch
2324 if Is_Atomic_Object (Actual) and then not Is_Atomic (E_Formal)
2326 if Comes_From_Source (N) then
2328 ("??atomic actual passed by copy (RM C.6(19))", Actual);
2333 -- Check for volatility mismatch
2335 if Is_Volatile_Object_Ref (Actual) and then not Is_Volatile (E_Formal)
2337 if Comes_From_Source (N) then
2339 ("??volatile actual passed by copy (RM C.6(19))", Actual);
2345 end Requires_Atomic_Or_Volatile_Copy;
2347 -- Start of processing for Expand_Actuals
2350 Post_Call := New_List;
2352 Formal := First_Formal (Subp);
2353 Actual := First_Actual (N);
2354 while Present (Formal) loop
2355 E_Formal := Etype (Formal);
2356 E_Actual := Etype (Actual);
2358 -- Handle formals whose type comes from the limited view
2360 if From_Limited_With (E_Formal)
2361 and then Has_Non_Limited_View (E_Formal)
2363 E_Formal := Non_Limited_View (E_Formal);
2366 if Is_Scalar_Type (E_Formal)
2367 or else Nkind (Actual) = N_Slice
2369 Check_Fortran_Logical;
2373 elsif Ekind (Formal) /= E_Out_Parameter then
2375 -- The unusual case of the current instance of a protected type
2376 -- requires special handling. This can only occur in the context
2377 -- of a call within the body of a protected operation.
2379 if Is_Entity_Name (Actual)
2380 and then Ekind (Entity (Actual)) = E_Protected_Type
2381 and then In_Open_Scopes (Entity (Actual))
2383 if Scope (Subp) /= Entity (Actual) then
2385 ("operation outside protected type may not "
2386 & "call back its protected operations??", Actual);
2390 Expand_Protected_Object_Reference (N, Entity (Actual)));
2393 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
2394 -- build-in-place function, then a temporary return object needs
2395 -- to be created and access to it must be passed to the function
2396 -- (and ensure that we have an activation chain defined for tasks
2397 -- and a Master variable).
2399 -- Currently we limit such functions to those with inherently
2400 -- limited result subtypes, but eventually we plan to expand the
2401 -- functions that are treated as build-in-place to include other
2402 -- composite result types.
2404 -- But do not do it here for intrinsic subprograms since this will
2405 -- be done properly after the subprogram is expanded.
2407 if Is_Intrinsic_Subprogram (Subp) then
2410 elsif Is_Build_In_Place_Function_Call (Actual) then
2411 if Might_Have_Tasks (Etype (Actual)) then
2412 Build_Activation_Chain_Entity (N);
2413 Build_Master_Entity (Etype (Actual));
2416 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
2418 -- Ada 2005 (AI-318-02): Specialization of the previous case for
2419 -- actuals containing build-in-place function calls whose returned
2420 -- object covers interface types.
2422 elsif Present (Unqual_BIP_Iface_Function_Call (Actual)) then
2423 Build_Activation_Chain_Entity (N);
2424 Build_Master_Entity (Etype (Actual));
2425 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (Actual);
2428 Apply_Constraint_Check (Actual, E_Formal);
2430 -- Out parameter case. No constraint checks on access type
2431 -- RM 6.4.1 (13), but on return a null-excluding check may be
2432 -- required (see below).
2434 elsif Is_Access_Type (E_Formal) then
2439 elsif Has_Discriminants (Base_Type (E_Formal))
2440 or else Has_Non_Null_Base_Init_Proc (E_Formal)
2442 Apply_Constraint_Check (Actual, E_Formal);
2447 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
2450 -- Processing for IN-OUT and OUT parameters
2452 if Ekind (Formal) /= E_In_Parameter then
2454 -- For type conversions of arrays, apply length/range checks
2456 if Is_Array_Type (E_Formal)
2457 and then Nkind (Actual) = N_Type_Conversion
2459 if Is_Constrained (E_Formal) then
2460 Apply_Length_Check (Expression (Actual), E_Formal);
2462 Apply_Range_Check (Expression (Actual), E_Formal);
2466 -- If argument is a type conversion for a type that is passed by
2467 -- copy, then we must pass the parameter by copy.
2469 if Nkind (Actual) = N_Type_Conversion
2471 (Is_Elementary_Type (E_Formal)
2472 or else Is_Bit_Packed_Array (Etype (Formal))
2473 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
2475 -- Also pass by copy if change of representation
2477 or else not Has_Compatible_Representation
2478 (Target_Typ => Etype (Formal),
2479 Operand_Typ => Etype (Expression (Actual))))
2481 Add_Call_By_Copy_Code;
2483 -- References to components of bit-packed arrays are expanded
2484 -- at this point, rather than at the point of analysis of the
2485 -- actuals, to handle the expansion of the assignment to
2486 -- [in] out parameters.
2488 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
2489 Add_Simple_Call_By_Copy_Code (Force => True);
2491 -- If the actual has a nonnative storage model, we need a copy
2493 elsif Nkind (Actual) = N_Explicit_Dereference
2495 Has_Designated_Storage_Model_Aspect (Etype (Prefix (Actual)))
2497 (Present (Storage_Model_Copy_To
2498 (Storage_Model_Object (Etype (Prefix (Actual)))))
2500 (Ekind (Formal) = E_In_Out_Parameter
2502 Present (Storage_Model_Copy_From
2503 (Storage_Model_Object (Etype (Prefix (Actual)))))))
2505 Add_Simple_Call_By_Copy_Code (Force => True);
2507 -- If a nonscalar actual is possibly bit-aligned, we need a copy
2508 -- because the back-end cannot cope with such objects. In other
2509 -- cases where alignment forces a copy, the back-end generates
2510 -- it properly. It should not be generated unconditionally in the
2511 -- front-end because it does not know precisely the alignment
2512 -- requirements of the target, and makes too conservative an
2513 -- estimate, leading to superfluous copies or spurious errors
2514 -- on by-reference parameters.
2516 elsif Nkind (Actual) = N_Selected_Component
2518 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
2519 and then not Represented_As_Scalar (Etype (Formal))
2521 Add_Simple_Call_By_Copy_Code (Force => False);
2523 -- References to slices of bit-packed arrays are expanded
2525 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2526 Add_Call_By_Copy_Code;
2528 -- References to possibly unaligned slices of arrays are expanded
2530 elsif Is_Possibly_Unaligned_Slice (Actual) then
2531 Add_Call_By_Copy_Code;
2533 -- Deal with access types where the actual subtype and the
2534 -- formal subtype are not the same, requiring a check.
2536 -- It is necessary to exclude tagged types because of "downward
2537 -- conversion" errors, but null-excluding checks on return may be
2540 elsif Is_Access_Type (E_Formal)
2541 and then not Is_Tagged_Type (Designated_Type (E_Formal))
2542 and then (not Same_Type (E_Formal, E_Actual)
2543 or else (Can_Never_Be_Null (E_Actual)
2544 and then not Can_Never_Be_Null (E_Formal)))
2546 Add_Call_By_Copy_Code;
2548 -- We may need to force a copy because of atomicity or volatility
2551 elsif Requires_Atomic_Or_Volatile_Copy then
2552 Add_Call_By_Copy_Code;
2554 -- Add call-by-copy code for the case of scalar out parameters
2555 -- when it is not known at compile time that the subtype of the
2556 -- formal is a subrange of the subtype of the actual (or vice
2557 -- versa for in out parameters), in order to get range checks
2558 -- on such actuals. (Maybe this case should be handled earlier
2559 -- in the if statement???)
2561 elsif Is_Scalar_Type (E_Formal)
2563 (not In_Subrange_Of (E_Formal, E_Actual)
2565 (Ekind (Formal) = E_In_Out_Parameter
2566 and then not In_Subrange_Of (E_Actual, E_Formal)))
2568 Add_Call_By_Copy_Code;
2570 -- The actual denotes a variable which captures the value of an
2571 -- object for validation purposes. Add a copy-back to reflect any
2572 -- potential changes in value back into the original object.
2574 -- Var : ... := Object;
2575 -- if not Var'Valid then -- validity check
2576 -- Call (Var); -- modify var
2577 -- Object := Var; -- update Object
2579 elsif Is_Validation_Variable_Reference (Actual) then
2580 Add_Validation_Call_By_Copy_Code (Actual);
2583 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
2584 -- by-reference parameters on exit from the call. If the actual
2585 -- is a derived type and the operation is inherited, the body
2586 -- of the operation will not contain a call to the predicate
2587 -- function, so it must be done explicitly after the call. Ditto
2588 -- if the actual is an entity of a predicated subtype.
2590 -- The rule refers to by-reference types, but a check is needed
2591 -- for by-copy types as well. That check is subsumed by the rule
2592 -- for subtype conversion on assignment, but we can generate the
2593 -- required check now.
2595 -- Note also that Subp may be either a subprogram entity for
2596 -- direct calls, or a type entity for indirect calls, which must
2597 -- be handled separately because the name does not denote an
2598 -- overloadable entity.
2600 By_Ref_Predicate_Check : declare
2601 Aund : constant Entity_Id := Underlying_Type (E_Actual);
2611 if Predicate_Enabled (Atyp)
2613 -- Skip predicate checks for special cases
2615 and then Predicate_Tests_On_Arguments (Subp)
2617 Append_To (Post_Call,
2618 Make_Predicate_Check (Atyp, Actual));
2620 end By_Ref_Predicate_Check;
2622 -- Processing for IN parameters
2625 -- Generate range check if required
2627 if Do_Range_Check (Actual) then
2628 Generate_Range_Check (Actual, E_Formal, CE_Range_Check_Failed);
2631 -- For IN parameters in the bit-packed array case, we expand an
2632 -- indexed component (the circuit in Exp_Ch4 deliberately left
2633 -- indexed components appearing as actuals untouched, so that
2634 -- the special processing above for the OUT and IN OUT cases
2635 -- could be performed. We could make the test in Exp_Ch4 more
2636 -- complex and have it detect the parameter mode, but it is
2637 -- easier simply to handle all cases here.)
2639 if Nkind (Actual) = N_Indexed_Component
2640 and then Is_Bit_Packed_Array (Etype (Prefix (Actual)))
2642 Reset_Packed_Prefix;
2643 Expand_Packed_Element_Reference (Actual);
2645 -- If we have a reference to a bit-packed array, we copy it, since
2646 -- the actual must be byte aligned.
2648 -- Is this really necessary in all cases???
2650 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
2651 Add_Simple_Call_By_Copy_Code (Force => True);
2653 -- If the actual has a nonnative storage model, we need a copy
2655 elsif Nkind (Actual) = N_Explicit_Dereference
2657 Has_Designated_Storage_Model_Aspect (Etype (Prefix (Actual)))
2659 Present (Storage_Model_Copy_From
2660 (Storage_Model_Object (Etype (Prefix (Actual)))))
2662 Add_Simple_Call_By_Copy_Code (Force => True);
2664 -- If we have a C++ constructor call, we need to create the object
2666 elsif Is_CPP_Constructor_Call (Actual) then
2667 Add_Simple_Call_By_Copy_Code (Force => True);
2669 -- If a nonscalar actual is possibly unaligned, we need a copy
2671 elsif Is_Possibly_Unaligned_Object (Actual)
2672 and then not Represented_As_Scalar (Etype (Formal))
2674 Add_Simple_Call_By_Copy_Code (Force => False);
2676 -- Similarly, we have to expand slices of packed arrays here
2677 -- because the result must be byte aligned.
2679 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2680 Add_Call_By_Copy_Code;
2682 -- Only processing remaining is to pass by copy if this is a
2683 -- reference to a possibly unaligned slice, since the caller
2684 -- expects an appropriately aligned argument.
2686 elsif Is_Possibly_Unaligned_Slice (Actual) then
2687 Add_Call_By_Copy_Code;
2689 -- We may need to force a copy because of atomicity or volatility
2692 elsif Requires_Atomic_Or_Volatile_Copy then
2693 Add_Call_By_Copy_Code;
2695 -- An unusual case: a current instance of an enclosing task can be
2696 -- an actual, and must be replaced by a reference to self.
2698 elsif Is_Entity_Name (Actual)
2699 and then Is_Task_Type (Entity (Actual))
2701 if In_Open_Scopes (Entity (Actual)) then
2703 (Make_Function_Call (Loc,
2704 Name => New_Occurrence_Of (RTE (RE_Self), Loc))));
2707 -- A task type cannot otherwise appear as an actual
2710 raise Program_Error;
2715 -- Type-invariant checks for in-out and out parameters, as well as
2716 -- for in parameters of procedures (AI05-0289 and AI12-0044).
2718 if Ekind (Formal) /= E_In_Parameter
2719 or else Ekind (Subp) = E_Procedure
2721 Caller_Side_Invariant_Checks : declare
2723 function Is_Public_Subp return Boolean;
2724 -- Check whether the subprogram being called is a visible
2725 -- operation of the type of the actual. Used to determine
2726 -- whether an invariant check must be generated on the
2729 ---------------------
2730 -- Is_Public_Subp --
2731 ---------------------
2733 function Is_Public_Subp return Boolean is
2734 Pack : constant Entity_Id := Scope (Subp);
2735 Subp_Decl : Node_Id;
2738 if not Is_Subprogram (Subp) then
2741 -- The operation may be inherited, or a primitive of the
2745 Nkind (Parent (Subp)) in N_Private_Extension_Declaration
2746 | N_Full_Type_Declaration
2748 Subp_Decl := Parent (Subp);
2751 Subp_Decl := Unit_Declaration_Node (Subp);
2754 return Ekind (Pack) = E_Package
2756 List_Containing (Subp_Decl) =
2757 Visible_Declarations
2758 (Specification (Unit_Declaration_Node (Pack)));
2761 -- Start of processing for Caller_Side_Invariant_Checks
2764 -- We generate caller-side invariant checks in two cases:
2766 -- a) when calling an inherited operation, where there is an
2767 -- implicit view conversion of the actual to the parent type.
2769 -- b) When the conversion is explicit
2771 -- We treat these cases separately because the required
2772 -- conversion for a) is added later when expanding the call.
2774 if Has_Invariants (Etype (Actual))
2776 Nkind (Parent (Etype (Actual)))
2777 = N_Private_Extension_Declaration
2779 if Comes_From_Source (N) and then Is_Public_Subp then
2780 Append_To (Post_Call, Make_Invariant_Call (Actual));
2783 elsif Nkind (Actual) = N_Type_Conversion
2784 and then Has_Invariants (Etype (Expression (Actual)))
2786 if Comes_From_Source (N) and then Is_Public_Subp then
2788 (Post_Call, Make_Invariant_Call (Expression (Actual)));
2791 end Caller_Side_Invariant_Checks;
2794 Next_Formal (Formal);
2795 Next_Actual (Actual);
2803 procedure Expand_Call (N : Node_Id) is
2804 function Is_Unchecked_Union_Equality (N : Node_Id) return Boolean;
2805 -- Return True if N is a call to the predefined equality operator of an
2806 -- unchecked union type, or a renaming thereof.
2808 ---------------------------------
2809 -- Is_Unchecked_Union_Equality --
2810 ---------------------------------
2812 function Is_Unchecked_Union_Equality (N : Node_Id) return Boolean is
2814 if Is_Entity_Name (Name (N))
2815 and then Ekind (Entity (Name (N))) = E_Function
2816 and then Present (First_Formal (Entity (Name (N))))
2818 Is_Unchecked_Union (Etype (First_Formal (Entity (Name (N)))))
2821 Func : constant Entity_Id := Entity (Name (N));
2822 Typ : constant Entity_Id := Etype (First_Formal (Func));
2823 Decl : constant Node_Id :=
2824 Original_Node (Parent (Declaration_Node (Func)));
2827 return Func = TSS (Typ, TSS_Composite_Equality)
2828 or else (Nkind (Decl) = N_Subprogram_Renaming_Declaration
2829 and then Nkind (Name (Decl)) = N_Operator_Symbol
2830 and then Chars (Name (Decl)) = Name_Op_Eq
2831 and then Ekind (Entity (Name (Decl))) = E_Operator);
2837 end Is_Unchecked_Union_Equality;
2839 -- If this is an indirect call through an Access_To_Subprogram
2840 -- with contract specifications, it is rewritten as a call to
2841 -- the corresponding Access_Subprogram_Wrapper with the same
2842 -- actuals, whose body contains a naked indirect call (which
2843 -- itself must not be rewritten, to prevent infinite recursion).
2845 Must_Rewrite_Indirect_Call : constant Boolean :=
2846 Ada_Version >= Ada_2022
2847 and then Nkind (Name (N)) = N_Explicit_Dereference
2848 and then Ekind (Etype (Name (N))) = E_Subprogram_Type
2850 (Access_Subprogram_Wrapper (Etype (Name (N))));
2852 Post_Call : List_Id;
2854 -- Start of processing for Expand_Call
2857 pragma Assert (Nkind (N) in N_Entry_Call_Statement
2859 | N_Procedure_Call_Statement);
2861 -- Check that this is not the call in the body of the access
2862 -- subprogram wrapper or the postconditions wrapper.
2864 if Must_Rewrite_Indirect_Call
2865 and then (not Is_Overloadable (Current_Scope)
2866 or else not (Is_Access_Subprogram_Wrapper (Current_Scope)
2868 (Chars (Current_Scope) = Name_uWrapped_Statements
2869 and then Is_Access_Subprogram_Wrapper
2870 (Scope (Current_Scope)))))
2873 Loc : constant Source_Ptr := Sloc (N);
2874 Wrapper : constant Entity_Id :=
2875 Access_Subprogram_Wrapper (Etype (Name (N)));
2876 Ptr : constant Node_Id := Prefix (Name (N));
2877 Ptr_Type : constant Entity_Id := Etype (Ptr);
2878 Typ : constant Entity_Id := Etype (N);
2881 Parms : List_Id := Parameter_Associations (N);
2885 -- The last actual in the call is the pointer itself.
2886 -- If the aspect is inherited, convert the pointer to the
2887 -- parent type that specifies the contract.
2888 -- If the original access_to_subprogram has defaults for
2889 -- in_parameters, the call may include named associations, so
2890 -- we create one for the pointer as well.
2892 if Is_Derived_Type (Ptr_Type)
2893 and then Ptr_Type /= Etype (Last_Formal (Wrapper))
2896 Make_Type_Conversion (Loc,
2898 (Etype (Last_Formal (Wrapper)), Loc), Ptr);
2904 -- Handle parameterless subprogram.
2911 (Make_Parameter_Association (Loc,
2912 Selector_Name => Make_Identifier (Loc,
2913 Chars (Last_Formal (Wrapper))),
2914 Explicit_Actual_Parameter => Ptr_Act),
2917 if Nkind (N) = N_Procedure_Call_Statement then
2918 New_N := Make_Procedure_Call_Statement (Loc,
2919 Name => New_Occurrence_Of (Wrapper, Loc),
2920 Parameter_Associations => Parms);
2922 New_N := Make_Function_Call (Loc,
2923 Name => New_Occurrence_Of (Wrapper, Loc),
2924 Parameter_Associations => Parms);
2928 Analyze_And_Resolve (N, Typ);
2931 -- Case of a call to the predefined equality operator of an unchecked
2932 -- union type, which requires specific processing.
2934 elsif Is_Unchecked_Union_Equality (N) then
2936 Eq : constant Entity_Id := Entity (Name (N));
2939 Expand_Unchecked_Union_Equality (N);
2941 -- If the call was not rewritten as a raise, expand the actuals
2943 if Nkind (N) = N_Function_Call then
2944 pragma Assert (Check_Number_Of_Actuals (N, Eq));
2945 Expand_Actuals (N, Eq, Post_Call);
2946 pragma Assert (Is_Empty_List (Post_Call));
2953 Expand_Call_Helper (N, Post_Call);
2954 Insert_Post_Call_Actions (N, Post_Call);
2958 ------------------------
2959 -- Expand_Call_Helper --
2960 ------------------------
2962 -- This procedure handles expansion of function calls and procedure call
2963 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
2964 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
2966 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
2967 -- Provide values of actuals for all formals in Extra_Formals list
2968 -- Replace "call" to enumeration literal function by literal itself
2969 -- Rewrite call to predefined operator as operator
2970 -- Replace actuals to in-out parameters that are numeric conversions,
2971 -- with explicit assignment to temporaries before and after the call.
2973 -- Note that the list of actuals has been filled with default expressions
2974 -- during semantic analysis of the call. Only the extra actuals required
2975 -- for the 'Constrained attribute and for accessibility checks are added
2978 procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id) is
2979 Loc : constant Source_Ptr := Sloc (N);
2980 Call_Node : Node_Id := N;
2981 Extra_Actuals : List_Id := No_List;
2982 Prev : Node_Id := Empty;
2984 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
2985 -- Adds one entry to the end of the actual parameter list. Used for
2986 -- default parameters and for extra actuals (for Extra_Formals). The
2987 -- argument is an N_Parameter_Association node.
2989 procedure Add_Cond_Expression_Extra_Actual (Formal : Entity_Id);
2990 -- Adds extra accessibility actuals in the case of a conditional
2991 -- expression corresponding to Formal.
2993 -- Note: Conditional expressions used as actuals for anonymous access
2994 -- formals complicate the process of propagating extra accessibility
2995 -- actuals and must be handled in a recursive fashion since they can
2996 -- be embedded within each other.
2998 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
2999 -- Adds an extra actual to the list of extra actuals. Expr is the
3000 -- expression for the value of the actual, EF is the entity for the
3003 procedure Add_View_Conversion_Invariants
3004 (Formal : Entity_Id;
3006 -- Adds invariant checks for every intermediate type between the range
3007 -- of a view converted argument to its ancestor (from parent to child).
3009 function Can_Fold_Predicate_Call (P : Entity_Id) return Boolean;
3010 -- Try to constant-fold a predicate check, which often enough is a
3011 -- simple arithmetic expression that can be computed statically if
3012 -- its argument is static. This cleans up the output of CCG, even
3013 -- though useless predicate checks will be generally removed by
3014 -- back-end optimizations.
3016 procedure Check_Subprogram_Variant;
3017 -- Emit a call to the internally generated procedure with checks for
3018 -- aspect Subprogram_Variant, if present and enabled.
3020 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
3021 -- Within an instance, a type derived from an untagged formal derived
3022 -- type inherits from the original parent, not from the actual. The
3023 -- current derivation mechanism has the derived type inherit from the
3024 -- actual, which is only correct outside of the instance. If the
3025 -- subprogram is inherited, we test for this particular case through a
3026 -- convoluted tree traversal before setting the proper subprogram to be
3029 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
3030 -- Return true if E comes from an instance that is not yet frozen
3032 function Is_Class_Wide_Interface_Type (E : Entity_Id) return Boolean;
3033 -- Return True when E is a class-wide interface type or an access to
3034 -- a class-wide interface type.
3036 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
3037 -- Determine if Subp denotes a non-dispatching call to a Deep routine
3039 function New_Value (From : Node_Id) return Node_Id;
3040 -- From is the original Expression. New_Value is equivalent to a call
3041 -- to Duplicate_Subexpr with an explicit dereference when From is an
3042 -- access parameter.
3044 --------------------------
3045 -- Add_Actual_Parameter --
3046 --------------------------
3048 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
3049 Actual_Expr : constant Node_Id :=
3050 Explicit_Actual_Parameter (Insert_Param);
3053 -- Case of insertion is first named actual
3055 if No (Prev) or else
3056 Nkind (Parent (Prev)) /= N_Parameter_Association
3058 Set_Next_Named_Actual
3059 (Insert_Param, First_Named_Actual (Call_Node));
3060 Set_First_Named_Actual (Call_Node, Actual_Expr);
3063 if No (Parameter_Associations (Call_Node)) then
3064 Set_Parameter_Associations (Call_Node, New_List);
3067 Append (Insert_Param, Parameter_Associations (Call_Node));
3070 Insert_After (Prev, Insert_Param);
3073 -- Case of insertion is not first named actual
3076 Set_Next_Named_Actual
3077 (Insert_Param, Next_Named_Actual (Parent (Prev)));
3078 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
3079 Append (Insert_Param, Parameter_Associations (Call_Node));
3082 Prev := Actual_Expr;
3083 end Add_Actual_Parameter;
3085 --------------------------------------
3086 -- Add_Cond_Expression_Extra_Actual --
3087 --------------------------------------
3089 procedure Add_Cond_Expression_Extra_Actual
3090 (Formal : Entity_Id)
3095 procedure Insert_Level_Assign (Branch : Node_Id);
3096 -- Recursively add assignment of the level temporary on each branch
3097 -- while moving through nested conditional expressions.
3099 -------------------------
3100 -- Insert_Level_Assign --
3101 -------------------------
3103 procedure Insert_Level_Assign (Branch : Node_Id) is
3105 procedure Expand_Branch (Res_Assn : Node_Id);
3106 -- Perform expansion or iterate further within nested
3107 -- conditionals given the object declaration or assignment to
3108 -- result object created during expansion which represents a
3109 -- branch of the conditional expression.
3115 procedure Expand_Branch (Res_Assn : Node_Id) is
3117 pragma Assert (Nkind (Res_Assn) in
3118 N_Assignment_Statement |
3119 N_Object_Declaration);
3121 -- There are more nested conditional expressions so we must go
3124 if Nkind (Expression (Res_Assn)) = N_Expression_With_Actions
3126 Nkind (Original_Node (Expression (Res_Assn)))
3127 in N_Case_Expression | N_If_Expression
3130 (Expression (Res_Assn));
3132 -- Add the level assignment
3135 Insert_Before_And_Analyze (Res_Assn,
3136 Make_Assignment_Statement (Loc,
3137 Name => New_Occurrence_Of (Lvl, Loc),
3140 (Expr => Expression (Res_Assn),
3141 Level => Dynamic_Level,
3142 Allow_Alt_Model => False)));
3149 -- Start of processing for Insert_Level_Assign
3152 -- Examine further nested conditionals
3154 pragma Assert (Nkind (Branch) =
3155 N_Expression_With_Actions);
3157 -- Find the relevant statement in the actions
3159 Cond := First (Actions (Branch));
3160 while Present (Cond) loop
3161 exit when Nkind (Cond) in N_Case_Statement | N_If_Statement;
3165 -- The conditional expression may have been optimized away, so
3166 -- examine the actions in the branch.
3169 Expand_Branch (Last (Actions (Branch)));
3171 -- Iterate through if expression branches
3173 elsif Nkind (Cond) = N_If_Statement then
3174 Expand_Branch (Last (Then_Statements (Cond)));
3175 Expand_Branch (Last (Else_Statements (Cond)));
3177 -- Iterate through case alternatives
3179 elsif Nkind (Cond) = N_Case_Statement then
3181 Alt := First (Alternatives (Cond));
3182 while Present (Alt) loop
3183 Expand_Branch (Last (Statements (Alt)));
3187 end Insert_Level_Assign;
3189 -- Start of processing for cond expression case
3192 -- Create declaration of a temporary to store the accessibility
3193 -- level of each branch of the conditional expression.
3195 Lvl := Make_Temporary (Loc, 'L');
3196 Decl := Make_Object_Declaration (Loc,
3197 Defining_Identifier => Lvl,
3198 Object_Definition =>
3199 New_Occurrence_Of (Standard_Natural, Loc));
3201 -- Install the declaration and perform necessary expansion if we
3202 -- are dealing with a procedure call.
3204 if Nkind (Call_Node) = N_Procedure_Call_Statement then
3209 -- If_Exp_Res : Typ;
3211 -- Lvl := 0; -- Access level
3212 -- If_Exp_Res := Exp;
3214 -- in If_Exp_Res end;},
3219 Insert_Before_And_Analyze (Call_Node, Decl);
3221 -- Ditto for a function call. Note that we do not wrap the function
3222 -- call into an expression with action to avoid bad interactions with
3223 -- Exp_Ch4.Process_Transient_In_Expression.
3227 -- Lvl : Natural; -- placed above the function call
3233 -- Lvl := 0; -- Access level
3234 -- If_Exp_Res := Exp;
3235 -- in If_Exp_Res end;},
3240 Insert_Action (Call_Node, Decl);
3241 Analyze (Call_Node);
3244 -- Decorate the conditional expression with assignments to our level
3247 Insert_Level_Assign (Prev);
3249 -- Make our level temporary the passed actual
3252 (Expr => New_Occurrence_Of (Lvl, Loc),
3253 EF => Extra_Accessibility (Formal));
3254 end Add_Cond_Expression_Extra_Actual;
3256 ----------------------
3257 -- Add_Extra_Actual --
3258 ----------------------
3260 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
3261 Loc : constant Source_Ptr := Sloc (Expr);
3264 if Extra_Actuals = No_List then
3265 Extra_Actuals := New_List;
3266 Set_Parent (Extra_Actuals, Call_Node);
3269 Append_To (Extra_Actuals,
3270 Make_Parameter_Association (Loc,
3271 Selector_Name => New_Occurrence_Of (EF, Loc),
3272 Explicit_Actual_Parameter => Expr));
3274 Analyze_And_Resolve (Expr, Etype (EF));
3276 if Nkind (Call_Node) = N_Function_Call then
3277 Set_Is_Accessibility_Actual (Parent (Expr));
3279 end Add_Extra_Actual;
3281 ------------------------------------
3282 -- Add_View_Conversion_Invariants --
3283 ------------------------------------
3285 procedure Add_View_Conversion_Invariants
3286 (Formal : Entity_Id;
3290 Curr_Typ : Entity_Id;
3291 Inv_Checks : List_Id;
3292 Par_Typ : Entity_Id;
3295 Inv_Checks := No_List;
3297 -- Extract the argument from a potentially nested set of view
3301 while Nkind (Arg) = N_Type_Conversion loop
3302 Arg := Expression (Arg);
3305 -- Move up the derivation chain starting with the type of the formal
3306 -- parameter down to the type of the actual object.
3309 Par_Typ := Etype (Arg);
3310 while Par_Typ /= Etype (Formal) and Par_Typ /= Curr_Typ loop
3311 Curr_Typ := Par_Typ;
3313 if Has_Invariants (Curr_Typ)
3314 and then Present (Invariant_Procedure (Curr_Typ))
3316 -- Verify the invariant of the current type. Generate:
3318 -- <Curr_Typ>Invariant (Curr_Typ (Arg));
3320 Prepend_New_To (Inv_Checks,
3321 Make_Procedure_Call_Statement (Loc,
3324 (Invariant_Procedure (Curr_Typ), Loc),
3325 Parameter_Associations => New_List (
3326 Make_Type_Conversion (Loc,
3327 Subtype_Mark => New_Occurrence_Of (Curr_Typ, Loc),
3328 Expression => New_Copy_Tree (Arg)))));
3331 Par_Typ := Base_Type (Etype (Curr_Typ));
3334 -- If the node is a function call the generated tests have been
3335 -- already handled in Insert_Post_Call_Actions.
3337 if not Is_Empty_List (Inv_Checks)
3338 and then Nkind (Call_Node) = N_Procedure_Call_Statement
3340 Insert_Actions_After (Call_Node, Inv_Checks);
3342 end Add_View_Conversion_Invariants;
3344 -----------------------------
3345 -- Can_Fold_Predicate_Call --
3346 -----------------------------
3348 function Can_Fold_Predicate_Call (P : Entity_Id) return Boolean is
3351 function Augments_Other_Dynamic_Predicate (DP_Aspect_Spec : Node_Id)
3353 -- Given a Dynamic_Predicate aspect aspecification for a
3354 -- discrete type, returns True iff another DP specification
3355 -- applies (indirectly, via a subtype type or a derived type)
3356 -- to the same entity that this aspect spec applies to.
3358 function May_Fold (N : Node_Id) return Traverse_Result;
3359 -- The predicate expression is foldable if it only contains operators
3360 -- and literals. During this check, we also replace occurrences of
3361 -- the formal of the constructed predicate function with the static
3362 -- value of the actual. This is done on a copy of the analyzed
3363 -- expression for the predicate.
3365 --------------------------------------
3366 -- Augments_Other_Dynamic_Predicate --
3367 --------------------------------------
3369 function Augments_Other_Dynamic_Predicate (DP_Aspect_Spec : Node_Id)
3372 Aspect_Bearer : Entity_Id := Entity (DP_Aspect_Spec);
3375 Aspect_Bearer := Nearest_Ancestor (Aspect_Bearer);
3377 if No (Aspect_Bearer) then
3382 Aspect_Spec : constant Node_Id :=
3383 Find_Aspect (Aspect_Bearer, Aspect_Dynamic_Predicate);
3385 if Present (Aspect_Spec)
3386 and then Aspect_Spec /= DP_Aspect_Spec
3388 -- Found another Dynamic_Predicate aspect spec
3393 end Augments_Other_Dynamic_Predicate;
3399 function May_Fold (N : Node_Id) return Traverse_Result is
3405 when N_Expanded_Name
3408 if Ekind (Entity (N)) = E_In_Parameter
3409 and then Entity (N) = First_Entity (P)
3411 Rewrite (N, New_Copy (Actual));
3412 Set_Is_Static_Expression (N);
3415 elsif Ekind (Entity (N)) = E_Enumeration_Literal then
3422 when N_Case_Expression
3427 when N_Integer_Literal =>
3435 function Try_Fold is new Traverse_Func (May_Fold);
3437 -- Other Local variables
3439 Subt : constant Entity_Id := Etype (First_Entity (P));
3443 -- Start of processing for Can_Fold_Predicate_Call
3446 -- Folding is only interesting if the actual is static and its type
3447 -- has a Dynamic_Predicate aspect. For CodePeer we preserve the
3450 Actual := First (Parameter_Associations (Call_Node));
3451 Aspect := Find_Aspect (Subt, Aspect_Dynamic_Predicate);
3453 -- If actual is a declared constant, retrieve its value
3455 if Is_Entity_Name (Actual)
3456 and then Ekind (Entity (Actual)) = E_Constant
3458 Actual := Constant_Value (Entity (Actual));
3462 or else Nkind (Actual) /= N_Integer_Literal
3463 or else not Has_Dynamic_Predicate_Aspect (Subt)
3466 -- Do not fold if multiple applicable predicate aspects
3467 or else Has_Ghost_Predicate_Aspect (Subt)
3468 or else Has_Aspect (Subt, Aspect_Static_Predicate)
3469 or else Has_Aspect (Subt, Aspect_Predicate)
3470 or else Augments_Other_Dynamic_Predicate (Aspect)
3471 or else CodePeer_Mode
3476 -- Retrieve the analyzed expression for the predicate
3478 Pred := New_Copy_Tree (Expression (Aspect));
3480 if Try_Fold (Pred) = OK then
3481 Rewrite (Call_Node, Pred);
3482 Analyze_And_Resolve (Call_Node, Standard_Boolean);
3485 -- Otherwise continue the expansion of the function call
3490 end Can_Fold_Predicate_Call;
3492 ------------------------------
3493 -- Check_Subprogram_Variant --
3494 ------------------------------
3496 procedure Check_Subprogram_Variant is
3498 function Duplicate_Params_Without_Extra_Actuals
3499 (Call_Node : Node_Id) return List_Id;
3500 -- Duplicate actual parameters of Call_Node into New_Call without
3503 --------------------------------------------
3504 -- Duplicate_Params_Without_Extra_Actuals --
3505 --------------------------------------------
3507 function Duplicate_Params_Without_Extra_Actuals
3508 (Call_Node : Node_Id) return List_Id
3510 Proc_Id : constant Entity_Id := Entity (Name (Call_Node));
3511 Actuals : constant List_Id := Parameter_Associations (Call_Node);
3513 Actual : Node_Or_Entity_Id;
3517 if Actuals = No_List then
3522 Actual := First (Actuals);
3523 Formal := First_Formal (Proc_Id);
3525 while Present (Formal)
3526 and then Formal /= Extra_Formals (Proc_Id)
3528 Append (New_Copy (Actual), NL);
3531 Next_Formal (Formal);
3536 end Duplicate_Params_Without_Extra_Actuals;
3540 Variant_Prag : constant Node_Id :=
3541 Get_Pragma (Current_Scope, Pragma_Subprogram_Variant);
3544 Pragma_Arg1 : Node_Id;
3545 Variant_Proc : Entity_Id;
3548 if Present (Variant_Prag) and then Is_Checked (Variant_Prag) then
3551 Expression (First (Pragma_Argument_Associations (Variant_Prag)));
3553 -- If pragma parameter is still an aggregate, it comes from a
3554 -- structural variant, which is not expanded and ignored for
3555 -- run-time execution.
3557 if Nkind (Pragma_Arg1) = N_Aggregate then
3562 (First (Component_Associations (Pragma_Arg1))))) =
3567 -- Otherwise, analysis of the pragma rewrites its argument with a
3568 -- reference to the internally generated procedure.
3570 Variant_Proc := Entity (Pragma_Arg1);
3573 Make_Procedure_Call_Statement (Loc,
3575 New_Occurrence_Of (Variant_Proc, Loc),
3576 Parameter_Associations =>
3577 Duplicate_Params_Without_Extra_Actuals (Call_Node));
3579 Insert_Action (Call_Node, New_Call);
3581 pragma Assert (Etype (New_Call) /= Any_Type
3582 or else Serious_Errors_Detected > 0);
3584 end Check_Subprogram_Variant;
3586 ---------------------------
3587 -- Inherited_From_Formal --
3588 ---------------------------
3590 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
3592 Gen_Par : Entity_Id;
3593 Gen_Prim : Elist_Id;
3598 -- If the operation is inherited, it is attached to the corresponding
3599 -- type derivation. If the parent in the derivation is a generic
3600 -- actual, it is a subtype of the actual, and we have to recover the
3601 -- original derived type declaration to find the proper parent.
3603 if Nkind (Parent (S)) /= N_Full_Type_Declaration
3604 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
3605 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
3606 N_Derived_Type_Definition
3607 or else not In_Instance
3614 (Type_Definition (Original_Node (Parent (S))));
3616 if Nkind (Indic) = N_Subtype_Indication then
3617 Par := Entity (Subtype_Mark (Indic));
3619 Par := Entity (Indic);
3623 if not Is_Generic_Actual_Type (Par)
3624 or else Is_Tagged_Type (Par)
3625 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
3626 or else not In_Open_Scopes (Scope (Par))
3630 Gen_Par := Generic_Parent_Type (Parent (Par));
3633 -- If the actual has no generic parent type, the formal is not
3634 -- a formal derived type, so nothing to inherit.
3636 if No (Gen_Par) then
3640 -- If the generic parent type is still the generic type, this is a
3641 -- private formal, not a derived formal, and there are no operations
3642 -- inherited from the formal.
3644 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
3648 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
3650 Elmt := First_Elmt (Gen_Prim);
3651 while Present (Elmt) loop
3652 if Chars (Node (Elmt)) = Chars (S) then
3658 F1 := First_Formal (S);
3659 F2 := First_Formal (Node (Elmt));
3661 and then Present (F2)
3663 if Etype (F1) = Etype (F2)
3664 or else Etype (F2) = Gen_Par
3670 exit; -- not the right subprogram
3682 raise Program_Error;
3683 end Inherited_From_Formal;
3685 --------------------------
3686 -- In_Unfrozen_Instance --
3687 --------------------------
3689 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
3694 while Present (S) and then S /= Standard_Standard loop
3695 if Is_Generic_Instance (S)
3696 and then Present (Freeze_Node (S))
3697 and then not Analyzed (Freeze_Node (S))
3706 end In_Unfrozen_Instance;
3708 ----------------------------------
3709 -- Is_Class_Wide_Interface_Type --
3710 ----------------------------------
3712 function Is_Class_Wide_Interface_Type (E : Entity_Id) return Boolean is
3714 Typ : Entity_Id := E;
3717 if Has_Non_Limited_View (Typ) then
3718 Typ := Non_Limited_View (Typ);
3721 if Ekind (Typ) = E_Anonymous_Access_Type then
3722 DDT := Directly_Designated_Type (Typ);
3724 if Has_Non_Limited_View (DDT) then
3725 DDT := Non_Limited_View (DDT);
3728 return Is_Class_Wide_Type (DDT) and then Is_Interface (DDT);
3730 return Is_Class_Wide_Type (Typ) and then Is_Interface (Typ);
3732 end Is_Class_Wide_Interface_Type;
3734 -------------------------
3735 -- Is_Direct_Deep_Call --
3736 -------------------------
3738 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
3740 if Is_TSS (Subp, TSS_Deep_Adjust)
3741 or else Is_TSS (Subp, TSS_Deep_Finalize)
3742 or else Is_TSS (Subp, TSS_Deep_Initialize)
3749 Actual := First (Parameter_Associations (Call_Node));
3750 Formal := First_Formal (Subp);
3751 while Present (Actual)
3752 and then Present (Formal)
3754 if Nkind (Actual) = N_Identifier
3755 and then Is_Controlling_Actual (Actual)
3756 and then Etype (Actual) = Etype (Formal)
3762 Next_Formal (Formal);
3768 end Is_Direct_Deep_Call;
3774 function New_Value (From : Node_Id) return Node_Id is
3775 Res : constant Node_Id := Duplicate_Subexpr (From);
3777 if Is_Access_Type (Etype (From)) then
3778 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
3786 Remote : constant Boolean := Is_Remote_Call (Call_Node);
3789 Orig_Subp : Entity_Id := Empty;
3790 Param_Count : Positive;
3791 Parent_Formal : Entity_Id;
3792 Parent_Subp : Entity_Id;
3796 CW_Interface_Formals_Present : Boolean := False;
3798 -- Start of processing for Expand_Call_Helper
3801 Post_Call := New_List;
3803 -- Expand the function or procedure call if the first actual has a
3804 -- declared dimension aspect, and the subprogram is declared in one
3805 -- of the dimension I/O packages.
3807 if Ada_Version >= Ada_2012
3808 and then Nkind (Call_Node) in N_Subprogram_Call
3809 and then Present (Parameter_Associations (Call_Node))
3811 Expand_Put_Call_With_Symbol (Call_Node);
3814 -- Ignore if previous error
3816 if Nkind (Call_Node) in N_Has_Etype
3817 and then Etype (Call_Node) = Any_Type
3822 -- Call using access to subprogram with explicit dereference
3824 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3825 Subp := Etype (Name (Call_Node));
3826 Parent_Subp := Empty;
3828 -- Case of call to simple entry, where the Name is a selected component
3829 -- whose prefix is the task, and whose selector name is the entry name
3831 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
3832 Subp := Entity (Selector_Name (Name (Call_Node)));
3833 Parent_Subp := Empty;
3835 -- Case of call to member of entry family, where Name is an indexed
3836 -- component, with the prefix being a selected component giving the
3837 -- task and entry family name, and the index being the entry index.
3839 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
3840 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
3841 Parent_Subp := Empty;
3846 Subp := Entity (Name (Call_Node));
3847 Parent_Subp := Alias (Subp);
3849 -- Replace call to Raise_Exception by call to Raise_Exception_Always
3850 -- if we can tell that the first parameter cannot possibly be null.
3851 -- This improves efficiency by avoiding a run-time test.
3853 -- We do not do this if Raise_Exception_Always does not exist, which
3854 -- can happen in configurable run time profiles which provide only a
3857 if Is_RTE (Subp, RE_Raise_Exception)
3858 and then RTE_Available (RE_Raise_Exception_Always)
3861 FA : constant Node_Id :=
3862 Original_Node (First_Actual (Call_Node));
3865 -- The case we catch is where the first argument is obtained
3866 -- using the Identity attribute (which must always be
3869 if Nkind (FA) = N_Attribute_Reference
3870 and then Attribute_Name (FA) = Name_Identity
3872 Subp := RTE (RE_Raise_Exception_Always);
3873 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
3878 if Ekind (Subp) = E_Entry then
3879 Parent_Subp := Empty;
3883 -- Ensure that the called subprogram has all its formals
3885 if not Is_Frozen (Subp) then
3886 Create_Extra_Formals (Subp);
3889 -- Ada 2005 (AI-345): We have a procedure call as a triggering
3890 -- alternative in an asynchronous select or as an entry call in
3891 -- a conditional or timed select. Check whether the procedure call
3892 -- is a renaming of an entry and rewrite it as an entry call.
3894 if Ada_Version >= Ada_2005
3895 and then Nkind (Call_Node) = N_Procedure_Call_Statement
3897 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
3898 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
3900 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
3901 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
3905 Ren_Root : Entity_Id := Subp;
3908 -- This may be a chain of renamings, find the root
3910 if Present (Alias (Ren_Root)) then
3911 Ren_Root := Alias (Ren_Root);
3914 if Present (Parent (Ren_Root))
3915 and then Present (Original_Node (Parent (Parent (Ren_Root))))
3917 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
3919 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
3921 Make_Entry_Call_Statement (Loc,
3923 New_Copy_Tree (Name (Ren_Decl)),
3924 Parameter_Associations =>
3926 (Parameter_Associations (Call_Node))));
3934 -- If this is a call to a predicate function, try to constant fold it
3936 if Nkind (Call_Node) = N_Function_Call
3937 and then Is_Entity_Name (Name (Call_Node))
3938 and then Is_Predicate_Function (Subp)
3939 and then Can_Fold_Predicate_Call (Subp)
3944 if Transform_Function_Array
3945 and then Nkind (Call_Node) = N_Function_Call
3946 and then Is_Entity_Name (Name (Call_Node))
3949 Func_Id : constant Entity_Id :=
3950 Ultimate_Alias (Entity (Name (Call_Node)));
3952 -- When generating C code, transform a function call that returns
3953 -- a constrained array type into procedure form.
3955 if Rewritten_For_C (Func_Id) then
3957 -- For internally generated calls ensure that they reference
3958 -- the entity of the spec of the called function (needed since
3959 -- the expander may generate calls using the entity of their
3962 if not Comes_From_Source (Call_Node)
3963 and then Nkind (Unit_Declaration_Node (Func_Id)) =
3966 Set_Entity (Name (Call_Node),
3967 Corresponding_Function
3968 (Corresponding_Procedure (Func_Id)));
3971 Rewrite_Function_Call_For_C (Call_Node);
3974 -- Also introduce a temporary for functions that return a record
3975 -- called within another procedure or function call, since records
3976 -- are passed by pointer in the generated C code, and we cannot
3977 -- take a pointer from a subprogram call.
3979 elsif Modify_Tree_For_C
3980 and then Nkind (Parent (Call_Node)) in N_Subprogram_Call
3981 and then Is_Record_Type (Etype (Func_Id))
3984 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
3989 -- Temp : ... := Func_Call (...);
3992 Make_Object_Declaration (Loc,
3993 Defining_Identifier => Temp_Id,
3994 Object_Definition =>
3995 New_Occurrence_Of (Etype (Func_Id), Loc),
3997 Make_Function_Call (Loc,
3999 New_Occurrence_Of (Func_Id, Loc),
4000 Parameter_Associations =>
4001 Parameter_Associations (Call_Node)));
4003 Insert_Action (Parent (Call_Node), Decl);
4004 Rewrite (Call_Node, New_Occurrence_Of (Temp_Id, Loc));
4011 -- First step, compute extra actuals, corresponding to any Extra_Formals
4012 -- present. Note that we do not access Extra_Formals directly, instead
4013 -- we simply note the presence of the extra formals as we process the
4014 -- regular formals collecting corresponding actuals in Extra_Actuals.
4016 -- We also generate any required range checks for actuals for in formals
4017 -- as we go through the loop, since this is a convenient place to do it.
4018 -- (Though it seems that this would be better done in Expand_Actuals???)
4020 -- Special case: Thunks must not compute the extra actuals; they must
4021 -- just propagate to the target primitive their extra actuals.
4023 if Is_Thunk (Current_Scope)
4024 and then Thunk_Entity (Current_Scope) = Subp
4025 and then Present (Extra_Formals (Subp))
4027 pragma Assert (Extra_Formals_Match_OK (Current_Scope, Subp));
4030 Target_Formal : Entity_Id;
4031 Thunk_Formal : Entity_Id;
4034 Target_Formal := Extra_Formals (Subp);
4035 Thunk_Formal := Extra_Formals (Current_Scope);
4036 while Present (Target_Formal) loop
4038 (Expr => New_Occurrence_Of (Thunk_Formal, Loc),
4039 EF => Thunk_Formal);
4041 Target_Formal := Extra_Formal (Target_Formal);
4042 Thunk_Formal := Extra_Formal (Thunk_Formal);
4045 while Is_Non_Empty_List (Extra_Actuals) loop
4046 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
4049 -- Mark the call as processed build-in-place call; required
4050 -- to avoid adding the extra formals twice.
4052 if Nkind (Call_Node) = N_Function_Call then
4053 Set_Is_Expanded_Build_In_Place_Call (Call_Node);
4056 Expand_Actuals (Call_Node, Subp, Post_Call);
4057 pragma Assert (Is_Empty_List (Post_Call));
4058 pragma Assert (Check_Number_Of_Actuals (Call_Node, Subp));
4059 pragma Assert (Check_BIP_Actuals (Call_Node, Subp));
4064 Formal := First_Formal (Subp);
4065 Actual := First_Actual (Call_Node);
4067 while Present (Formal) loop
4068 -- Prepare to examine current entry
4072 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
4073 -- to expand it in a further round.
4075 CW_Interface_Formals_Present :=
4076 CW_Interface_Formals_Present
4077 or else Is_Class_Wide_Interface_Type (Etype (Formal));
4079 -- Create possible extra actual for constrained case. Usually, the
4080 -- extra actual is of the form actual'constrained, but since this
4081 -- attribute is only available for unconstrained records, TRUE is
4082 -- expanded if the type of the formal happens to be constrained (for
4083 -- instance when this procedure is inherited from an unconstrained
4084 -- record to a constrained one) or if the actual has no discriminant
4085 -- (its type is constrained). An exception to this is the case of a
4086 -- private type without discriminants. In this case we pass FALSE
4087 -- because the object has underlying discriminants with defaults.
4089 if Present (Extra_Constrained (Formal)) then
4090 if Is_Private_Type (Etype (Prev))
4091 and then not Has_Discriminants (Base_Type (Etype (Prev)))
4094 (Expr => New_Occurrence_Of (Standard_False, Loc),
4095 EF => Extra_Constrained (Formal));
4097 elsif Is_Constrained (Etype (Formal))
4098 or else not Has_Discriminants (Etype (Prev))
4101 (Expr => New_Occurrence_Of (Standard_True, Loc),
4102 EF => Extra_Constrained (Formal));
4104 -- Do not produce extra actuals for Unchecked_Union parameters.
4105 -- Jump directly to the end of the loop.
4107 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
4108 goto Skip_Extra_Actual_Generation;
4111 -- If the actual is a type conversion, then the constrained
4112 -- test applies to the actual, not the target type.
4118 -- Test for unchecked conversions as well, which can occur
4119 -- as out parameter actuals on calls to stream procedures.
4122 while Nkind (Act_Prev) in N_Type_Conversion
4123 | N_Unchecked_Type_Conversion
4125 Act_Prev := Expression (Act_Prev);
4128 -- If the expression is a conversion of a dereference, this
4129 -- is internally generated code that manipulates addresses,
4130 -- e.g. when building interface tables. No check should
4131 -- occur in this case, and the discriminated object is not
4132 -- directly at hand.
4134 if not Comes_From_Source (Actual)
4135 and then Nkind (Actual) = N_Unchecked_Type_Conversion
4136 and then Nkind (Act_Prev) = N_Explicit_Dereference
4139 (Expr => New_Occurrence_Of (Standard_False, Loc),
4140 EF => Extra_Constrained (Formal));
4145 Make_Attribute_Reference (Sloc (Prev),
4147 Duplicate_Subexpr_No_Checks
4148 (Act_Prev, Name_Req => True),
4149 Attribute_Name => Name_Constrained),
4150 EF => Extra_Constrained (Formal));
4156 -- Create possible extra actual for accessibility level
4158 if Present (Extra_Accessibility (Formal)) then
4159 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
4160 -- accessibility levels.
4162 if Is_Thunk (Current_Scope) then
4164 Parm_Ent : Entity_Id;
4167 if Is_Controlling_Actual (Actual) then
4169 -- Find the corresponding actual of the thunk
4171 Parm_Ent := First_Entity (Current_Scope);
4172 for J in 2 .. Param_Count loop
4173 Next_Entity (Parm_Ent);
4176 -- Handle unchecked conversion of access types generated
4177 -- in thunks (cf. Expand_Interface_Thunk).
4179 elsif Is_Access_Type (Etype (Actual))
4180 and then Nkind (Actual) = N_Unchecked_Type_Conversion
4182 Parm_Ent := Entity (Expression (Actual));
4184 else pragma Assert (Is_Entity_Name (Actual));
4185 Parm_Ent := Entity (Actual);
4189 (Expr => Accessibility_Level
4191 Level => Dynamic_Level,
4192 Allow_Alt_Model => False),
4193 EF => Extra_Accessibility (Formal));
4196 -- Conditional expressions
4198 elsif Nkind (Prev) = N_Expression_With_Actions
4199 and then Nkind (Original_Node (Prev)) in
4200 N_If_Expression | N_Case_Expression
4202 Add_Cond_Expression_Extra_Actual (Formal);
4204 -- Internal constant generated to remove side effects (normally
4205 -- from the expansion of dispatching calls).
4207 -- First verify the actual is internal
4209 elsif not Comes_From_Source (Prev)
4210 and then not Is_Rewrite_Substitution (Prev)
4212 -- Next check that the actual is a constant
4214 and then Nkind (Prev) = N_Identifier
4215 and then Ekind (Entity (Prev)) = E_Constant
4216 and then Nkind (Parent (Entity (Prev))) = N_Object_Declaration
4218 -- Generate the accessibility level based on the expression in
4219 -- the constant's declaration.
4222 (Expr => Accessibility_Level
4224 (Parent (Entity (Prev))),
4225 Level => Dynamic_Level,
4226 Allow_Alt_Model => False),
4227 EF => Extra_Accessibility (Formal));
4233 (Expr => Accessibility_Level
4235 Level => Dynamic_Level,
4236 Allow_Alt_Model => False),
4237 EF => Extra_Accessibility (Formal));
4241 -- Perform the check of 4.6(49) that prevents a null value from being
4242 -- passed as an actual to an access parameter. Note that the check
4243 -- is elided in the common cases of passing an access attribute or
4244 -- access parameter as an actual. Also, we currently don't enforce
4245 -- this check for expander-generated actuals and when -gnatdj is set.
4247 if Ada_Version >= Ada_2005 then
4249 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
4250 -- the intent of 6.4.1(13) is that null-exclusion checks should
4251 -- not be done for 'out' parameters, even though it refers only
4252 -- to constraint checks, and a null_exclusion is not a constraint.
4253 -- Note that AI05-0196-1 corrects this mistake in the RM.
4255 if Is_Access_Type (Etype (Formal))
4256 and then Can_Never_Be_Null (Etype (Formal))
4257 and then Ekind (Formal) /= E_Out_Parameter
4258 and then Nkind (Prev) /= N_Raise_Constraint_Error
4259 and then (Known_Null (Prev)
4260 or else not Can_Never_Be_Null (Etype (Prev)))
4262 Install_Null_Excluding_Check (Prev);
4265 -- Ada_Version < Ada_2005
4268 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
4269 or else Access_Checks_Suppressed (Subp)
4273 elsif Debug_Flag_J then
4276 elsif not Comes_From_Source (Prev) then
4279 elsif Is_Entity_Name (Prev)
4280 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
4284 elsif Nkind (Prev) in N_Allocator | N_Attribute_Reference then
4288 Install_Null_Excluding_Check (Prev);
4292 -- Perform appropriate validity checks on parameters that
4295 if Validity_Checks_On then
4296 if (Ekind (Formal) = E_In_Parameter
4297 and then Validity_Check_In_Params)
4299 (Ekind (Formal) = E_In_Out_Parameter
4300 and then Validity_Check_In_Out_Params)
4302 -- If the actual is an indexed component of a packed type (or
4303 -- is an indexed or selected component whose prefix recursively
4304 -- meets this condition), it has not been expanded yet. It will
4305 -- be copied in the validity code that follows, and has to be
4306 -- expanded appropriately, so reanalyze it.
4308 -- What we do is just to unset analyzed bits on prefixes till
4309 -- we reach something that does not have a prefix.
4316 while Nkind (Nod) in
4317 N_Indexed_Component | N_Selected_Component
4319 Set_Analyzed (Nod, False);
4320 Nod := Prefix (Nod);
4324 Ensure_Valid (Actual);
4328 -- For IN OUT and OUT parameters, ensure that subscripts are valid
4329 -- since this is a left side reference. We only do this for calls
4330 -- from the source program since we assume that compiler generated
4331 -- calls explicitly generate any required checks. We also need it
4332 -- only if we are doing standard validity checks, since clearly it is
4333 -- not needed if validity checks are off, and in subscript validity
4334 -- checking mode, all indexed components are checked with a call
4335 -- directly from Expand_N_Indexed_Component.
4337 if Comes_From_Source (Call_Node)
4338 and then Ekind (Formal) /= E_In_Parameter
4339 and then Validity_Checks_On
4340 and then Validity_Check_Default
4341 and then not Validity_Check_Subscripts
4343 Check_Valid_Lvalue_Subscripts (Actual);
4346 -- Mark any scalar OUT parameter that is a simple variable as no
4347 -- longer known to be valid (unless the type is always valid). This
4348 -- reflects the fact that if an OUT parameter is never set in a
4349 -- procedure, then it can become invalid on the procedure return.
4351 if Ekind (Formal) = E_Out_Parameter
4352 and then Is_Entity_Name (Actual)
4353 and then Ekind (Entity (Actual)) = E_Variable
4354 and then not Is_Known_Valid (Etype (Actual))
4356 Set_Is_Known_Valid (Entity (Actual), False);
4359 -- For an OUT or IN OUT parameter, if the actual is an entity, then
4360 -- clear current values, since they can be clobbered. We are probably
4361 -- doing this in more places than we need to, but better safe than
4362 -- sorry when it comes to retaining bad current values.
4364 if Ekind (Formal) /= E_In_Parameter
4365 and then Is_Entity_Name (Actual)
4366 and then Present (Entity (Actual))
4369 Ent : constant Entity_Id := Entity (Actual);
4373 -- For an OUT or IN OUT parameter that is an assignable entity,
4374 -- we do not want to clobber the Last_Assignment field, since
4375 -- if it is set, it was precisely because it is indeed an OUT
4376 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
4377 -- since the subprogram could have returned in invalid value.
4379 if Is_Assignable (Ent) then
4380 Sav := Last_Assignment (Ent);
4381 Kill_Current_Values (Ent);
4382 Set_Last_Assignment (Ent, Sav);
4383 Set_Is_Known_Valid (Ent, False);
4384 Set_Is_True_Constant (Ent, False);
4386 -- For all other cases, just kill the current values
4389 Kill_Current_Values (Ent);
4394 -- If the formal is class-wide and the actual is an aggregate, force
4395 -- evaluation so that the back end who does not know about class-wide
4396 -- type, does not generate a temporary of the wrong size.
4398 if not Is_Class_Wide_Type (Etype (Formal)) then
4401 elsif Nkind (Actual) = N_Aggregate
4402 or else (Nkind (Actual) = N_Qualified_Expression
4403 and then Nkind (Expression (Actual)) = N_Aggregate)
4405 Force_Evaluation (Actual);
4408 -- In a remote call, if the formal is of a class-wide type, check
4409 -- that the actual meets the requirements described in E.4(18).
4411 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
4412 Insert_Action (Actual,
4413 Make_Transportable_Check (Loc,
4414 Duplicate_Subexpr_Move_Checks (Actual)));
4417 -- Perform invariant checks for all intermediate types in a view
4418 -- conversion after successful return from a call that passes the
4419 -- view conversion as an IN OUT or OUT parameter (RM 7.3.2 (12/3,
4420 -- 13/3, 14/3)). Consider only source conversion in order to avoid
4421 -- generating spurious checks on complex expansion such as object
4422 -- initialization through an extension aggregate.
4424 if Comes_From_Source (Call_Node)
4425 and then Ekind (Formal) /= E_In_Parameter
4426 and then Nkind (Actual) = N_Type_Conversion
4428 Add_View_Conversion_Invariants (Formal, Actual);
4431 -- Generating C the initialization of an allocator is performed by
4432 -- means of individual statements, and hence it must be done before
4435 if Modify_Tree_For_C
4436 and then Nkind (Actual) = N_Allocator
4437 and then Nkind (Expression (Actual)) = N_Qualified_Expression
4439 Remove_Side_Effects (Actual);
4442 -- This label is required when skipping extra actual generation for
4443 -- Unchecked_Union parameters.
4445 <<Skip_Extra_Actual_Generation>>
4447 Param_Count := Param_Count + 1;
4448 Next_Actual (Actual);
4449 Next_Formal (Formal);
4452 -- If we are calling an Ada 2012 function which needs to have the
4453 -- "accessibility level determined by the point of call" (AI05-0234)
4454 -- passed in to it, then pass it in.
4456 if Ekind (Subp) in E_Function | E_Operator | E_Subprogram_Type
4458 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
4461 Extra_Form : Node_Id := Empty;
4462 Level : Node_Id := Empty;
4465 -- Detect cases where the function call has been internally
4466 -- generated by examining the original node and return library
4467 -- level - taking care to avoid ignoring function calls expanded
4468 -- in prefix notation.
4470 if Nkind (Original_Node (Call_Node)) not in N_Function_Call
4471 | N_Selected_Component
4472 | N_Indexed_Component
4474 Level := Make_Integer_Literal
4475 (Loc, Scope_Depth (Standard_Standard));
4477 -- Otherwise get the level normally based on the call node
4480 Level := Accessibility_Level
4482 Level => Dynamic_Level,
4483 Allow_Alt_Model => False);
4486 -- It may be possible that we are re-expanding an already
4487 -- expanded call when are are dealing with dispatching ???
4489 if not Present (Parameter_Associations (Call_Node))
4490 or else Nkind (Last (Parameter_Associations (Call_Node)))
4491 /= N_Parameter_Association
4492 or else not Is_Accessibility_Actual
4493 (Last (Parameter_Associations (Call_Node)))
4495 Extra_Form := Extra_Accessibility_Of_Result
4496 (Ultimate_Alias (Subp));
4505 -- If we are expanding the RHS of an assignment we need to check if tag
4506 -- propagation is needed. You might expect this processing to be in
4507 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
4508 -- assignment might be transformed to a declaration for an unconstrained
4509 -- value if the expression is classwide.
4511 if Nkind (Call_Node) = N_Function_Call
4512 and then Is_Tag_Indeterminate (Call_Node)
4513 and then Is_Entity_Name (Name (Call_Node))
4516 Ass : Node_Id := Empty;
4519 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
4520 Ass := Parent (Call_Node);
4522 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
4523 and then Nkind (Parent (Parent (Call_Node))) =
4524 N_Assignment_Statement
4526 Ass := Parent (Parent (Call_Node));
4528 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
4529 and then Nkind (Parent (Parent (Call_Node))) =
4530 N_Assignment_Statement
4532 Ass := Parent (Parent (Call_Node));
4536 and then Is_Class_Wide_Type (Etype (Name (Ass)))
4538 -- Move the error messages below to sem???
4540 if Is_Access_Type (Etype (Call_Node)) then
4541 if Designated_Type (Etype (Call_Node)) /=
4542 Root_Type (Etype (Name (Ass)))
4545 ("tag-indeterminate expression must have designated "
4546 & "type& (RM 5.2 (6))",
4547 Call_Node, Root_Type (Etype (Name (Ass))));
4549 Propagate_Tag (Name (Ass), Call_Node);
4552 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
4554 ("tag-indeterminate expression must have type & "
4556 Call_Node, Root_Type (Etype (Name (Ass))));
4559 Propagate_Tag (Name (Ass), Call_Node);
4562 -- The call will be rewritten as a dispatching call, and
4563 -- expanded as such.
4570 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
4571 -- it to point to the correct secondary virtual table.
4573 if Nkind (Call_Node) in N_Subprogram_Call
4574 and then CW_Interface_Formals_Present
4576 Expand_Interface_Actuals (Call_Node);
4579 -- Install class-wide preconditions runtime check when this is a
4580 -- dispatching primitive that has or inherits class-wide preconditions;
4581 -- otherwise no runtime check is installed.
4583 if Nkind (Call_Node) in N_Subprogram_Call
4584 and then Is_Dispatching_Operation (Subp)
4586 Install_Class_Preconditions_Check (Call_Node);
4589 -- Deals with Dispatch_Call if we still have a call, before expanding
4590 -- extra actuals since this will be done on the re-analysis of the
4591 -- dispatching call. Note that we do not try to shorten the actual list
4592 -- for a dispatching call, it would not make sense to do so. Expansion
4593 -- of dispatching calls is suppressed for VM targets, because the VM
4594 -- back-ends directly handle the generation of dispatching calls and
4595 -- would have to undo any expansion to an indirect call.
4597 if Nkind (Call_Node) in N_Subprogram_Call
4598 and then Present (Controlling_Argument (Call_Node))
4600 if Tagged_Type_Expansion then
4601 Expand_Dispatching_Call (Call_Node);
4603 -- Expand_Dispatching_Call takes care of all the needed processing
4611 Call_Typ : constant Entity_Id := Etype (Call_Node);
4612 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
4613 Eq_Prim_Op : Entity_Id := Empty;
4616 Prev_Call : Node_Id;
4619 Apply_Tag_Checks (Call_Node);
4621 if not Is_Limited_Type (Typ) then
4622 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
4625 -- If this is a dispatching "=", we must first compare the
4626 -- tags so we generate: x.tag = y.tag and then x = y
4628 if Subp = Eq_Prim_Op then
4630 -- Mark the node as analyzed to avoid reanalyzing this
4631 -- dispatching call (which would cause a never-ending loop)
4633 Prev_Call := Relocate_Node (Call_Node);
4634 Set_Analyzed (Prev_Call);
4636 Param := First_Actual (Call_Node);
4642 Make_Selected_Component (Loc,
4643 Prefix => New_Value (Param),
4646 (First_Tag_Component (Typ), Loc)),
4649 Make_Selected_Component (Loc,
4651 Unchecked_Convert_To (Typ,
4652 New_Value (Next_Actual (Param))),
4655 (First_Tag_Component (Typ), Loc))),
4656 Right_Opnd => Prev_Call);
4658 Rewrite (Call_Node, New_Call);
4660 (Call_Node, Call_Typ, Suppress => All_Checks);
4663 -- Expansion of a dispatching call results in an indirect call,
4664 -- which in turn causes current values to be killed (see
4665 -- Resolve_Call), so on VM targets we do the call here to
4666 -- ensure consistent warnings between VM and non-VM targets.
4668 Kill_Current_Values;
4670 -- If this is a dispatching "=" then we must update the reference
4671 -- to the call node because we generated:
4672 -- x.tag = y.tag and then x = y
4674 if Subp = Eq_Prim_Op then
4675 Call_Node := Right_Opnd (Call_Node);
4680 -- Similarly, expand calls to RCI subprograms on which pragma
4681 -- All_Calls_Remote applies. The rewriting will be reanalyzed
4682 -- later. Do this only when the call comes from source since we
4683 -- do not want such a rewriting to occur in expanded code.
4685 if Is_All_Remote_Call (Call_Node) then
4686 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
4688 -- Similarly, do not add extra actuals for an entry call whose entity
4689 -- is a protected procedure, or for an internal protected subprogram
4690 -- call, because it will be rewritten as a protected subprogram call
4691 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
4693 elsif Is_Protected_Type (Scope (Subp))
4694 and then Ekind (Subp) in E_Procedure | E_Function
4698 -- During that loop we gathered the extra actuals (the ones that
4699 -- correspond to Extra_Formals), so now they can be appended.
4702 while Is_Non_Empty_List (Extra_Actuals) loop
4703 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
4707 -- At this point we have all the actuals, so this is the point at which
4708 -- the various expansion activities for actuals is carried out.
4710 Expand_Actuals (Call_Node, Subp, Post_Call);
4712 -- If it is a recursive call then call the internal procedure that
4713 -- verifies Subprogram_Variant contract (if present and enabled).
4714 -- Detecting calls to subprogram aliases is necessary for recursive
4715 -- calls in instances of generic subprograms, where the renaming of
4716 -- the current subprogram is called.
4718 if Is_Subprogram (Subp)
4719 and then not Is_Ignored_Ghost_Entity (Subp)
4720 and then Same_Or_Aliased_Subprograms (Subp, Current_Scope)
4722 Check_Subprogram_Variant;
4725 -- Verify that the actuals do not share storage. This check must be done
4726 -- on the caller side rather that inside the subprogram to avoid issues
4727 -- of parameter passing.
4729 if Check_Aliasing_Of_Parameters then
4730 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
4733 -- If the subprogram is a renaming, or if it is inherited, replace it in
4734 -- the call with the name of the actual subprogram being called. If this
4735 -- is a dispatching call, the run-time decides what to call. The Alias
4736 -- attribute does not apply to entries.
4738 if Nkind (Call_Node) /= N_Entry_Call_Statement
4739 and then No (Controlling_Argument (Call_Node))
4740 and then Present (Parent_Subp)
4741 and then not Is_Direct_Deep_Call (Subp)
4743 if Present (Inherited_From_Formal (Subp)) then
4744 Parent_Subp := Inherited_From_Formal (Subp);
4746 Parent_Subp := Ultimate_Alias (Parent_Subp);
4749 -- The below setting of Entity is suspect, see F109-018 discussion???
4751 Set_Entity (Name (Call_Node), Parent_Subp);
4753 -- Inspect all formals of derived subprogram Subp. Compare parameter
4754 -- types with the parent subprogram and check whether an actual may
4755 -- need a type conversion to the corresponding formal of the parent
4758 -- Not clear whether intrinsic subprograms need such conversions. ???
4760 if not Is_Intrinsic_Subprogram (Parent_Subp)
4761 or else Is_Generic_Instance (Parent_Subp)
4764 procedure Convert (Act : Node_Id; Typ : Entity_Id);
4765 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
4766 -- and resolve the newly generated construct.
4772 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
4774 Rewrite (Act, OK_Convert_To (Typ, Act));
4775 Analyze_And_Resolve (Act, Typ);
4780 Actual_Typ : Entity_Id;
4781 Formal_Typ : Entity_Id;
4782 Parent_Typ : Entity_Id;
4785 Actual := First_Actual (Call_Node);
4786 Formal := First_Formal (Subp);
4787 Parent_Formal := First_Formal (Parent_Subp);
4788 while Present (Formal) loop
4789 Actual_Typ := Etype (Actual);
4790 Formal_Typ := Etype (Formal);
4791 Parent_Typ := Etype (Parent_Formal);
4793 -- For an IN parameter of a scalar type, the derived formal
4794 -- type and parent formal type differ, and the parent formal
4795 -- type and actual type do not match statically.
4797 if Is_Scalar_Type (Formal_Typ)
4798 and then Ekind (Formal) = E_In_Parameter
4799 and then Formal_Typ /= Parent_Typ
4801 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
4802 and then not Raises_Constraint_Error (Actual)
4804 Convert (Actual, Parent_Typ);
4806 -- For access types, the parent formal type and actual type
4809 elsif Is_Access_Type (Formal_Typ)
4810 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
4812 if Ekind (Formal) /= E_In_Parameter then
4813 Convert (Actual, Parent_Typ);
4815 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
4816 and then Designated_Type (Parent_Typ) /=
4817 Designated_Type (Actual_Typ)
4818 and then not Is_Controlling_Formal (Formal)
4820 -- This unchecked conversion is not necessary unless
4821 -- inlining is enabled, because in that case the type
4822 -- mismatch may become visible in the body about to be
4826 Unchecked_Convert_To (Parent_Typ, Actual));
4827 Analyze_And_Resolve (Actual, Parent_Typ);
4830 -- If there is a change of representation, then generate a
4831 -- warning, and do the change of representation.
4833 elsif not Has_Compatible_Representation
4834 (Target_Typ => Formal_Typ,
4835 Operand_Typ => Parent_Typ)
4838 ("??change of representation required", Actual);
4839 Convert (Actual, Parent_Typ);
4841 -- For array and record types, the parent formal type and
4842 -- derived formal type have different sizes or pragma Pack
4845 elsif ((Is_Array_Type (Formal_Typ)
4846 and then Is_Array_Type (Parent_Typ))
4848 (Is_Record_Type (Formal_Typ)
4849 and then Is_Record_Type (Parent_Typ)))
4850 and then Known_Esize (Formal_Typ)
4851 and then Known_Esize (Parent_Typ)
4853 (Esize (Formal_Typ) /= Esize (Parent_Typ)
4854 or else Has_Pragma_Pack (Formal_Typ) /=
4855 Has_Pragma_Pack (Parent_Typ))
4857 Convert (Actual, Parent_Typ);
4860 Next_Actual (Actual);
4861 Next_Formal (Formal);
4862 Next_Formal (Parent_Formal);
4868 Subp := Parent_Subp;
4871 -- Deal with case where call is an explicit dereference
4873 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
4875 -- Handle case of access to protected subprogram type
4877 if Is_Access_Protected_Subprogram_Type
4878 (Base_Type (Etype (Prefix (Name (Call_Node)))))
4880 -- If this is a call through an access to protected operation, the
4881 -- prefix has the form (object'address, operation'access). Rewrite
4882 -- as a for other protected calls: the object is the 1st parameter
4883 -- of the list of actuals.
4890 Ptr : constant Node_Id := Prefix (Name (Call_Node));
4892 T : constant Entity_Id :=
4893 Equivalent_Type (Base_Type (Etype (Ptr)));
4895 D_T : constant Entity_Id :=
4896 Designated_Type (Base_Type (Etype (Ptr)));
4900 Make_Selected_Component (Loc,
4901 Prefix => Unchecked_Convert_To (T, Ptr),
4903 New_Occurrence_Of (First_Entity (T), Loc));
4906 Make_Selected_Component (Loc,
4907 Prefix => Unchecked_Convert_To (T, Ptr),
4909 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
4912 Make_Explicit_Dereference (Loc,
4915 if Present (Parameter_Associations (Call_Node)) then
4916 Parm := Parameter_Associations (Call_Node);
4921 Prepend (Obj, Parm);
4923 if Etype (D_T) = Standard_Void_Type then
4925 Make_Procedure_Call_Statement (Loc,
4927 Parameter_Associations => Parm);
4930 Make_Function_Call (Loc,
4932 Parameter_Associations => Parm);
4935 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
4936 Set_Etype (Call, Etype (D_T));
4938 -- We do not re-analyze the call to avoid infinite recursion.
4939 -- We analyze separately the prefix and the object, and set
4940 -- the checks on the prefix that would otherwise be emitted
4941 -- when resolving a call.
4943 Rewrite (Call_Node, Call);
4945 Apply_Access_Check (Nam);
4952 -- If this is a call to an intrinsic subprogram, then perform the
4953 -- appropriate expansion to the corresponding tree node and we
4954 -- are all done (since after that the call is gone).
4956 -- In the case where the intrinsic is to be processed by the back end,
4957 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
4958 -- since the idea in this case is to pass the call unchanged. If the
4959 -- intrinsic is an inherited unchecked conversion, and the derived type
4960 -- is the target type of the conversion, we must retain it as the return
4961 -- type of the expression. Otherwise the expansion below, which uses the
4962 -- parent operation, will yield the wrong type.
4964 if Is_Intrinsic_Subprogram (Subp) then
4965 Expand_Intrinsic_Call (Call_Node, Subp);
4967 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
4968 and then Parent_Subp /= Orig_Subp
4969 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
4971 Set_Etype (Call_Node, Etype (Orig_Subp));
4977 if Ekind (Subp) in E_Function | E_Procedure then
4979 -- We perform a simple optimization on calls for To_Address by
4980 -- replacing them with an unchecked conversion. Not only is this
4981 -- efficient, but it also avoids order of elaboration problems when
4982 -- address clauses are inlined (address expression elaborated at the
4985 -- We perform this optimization regardless of whether we are in the
4986 -- main unit or in a unit in the context of the main unit, to ensure
4987 -- that the generated tree is the same in both cases, for CodePeer
4990 if Is_RTE (Subp, RE_To_Address) then
4992 Unchecked_Convert_To
4993 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
4996 -- A call to a null procedure is replaced by a null statement, but we
4997 -- are not allowed to ignore possible side effects of the call, so we
4998 -- make sure that actuals are evaluated.
4999 -- We also suppress this optimization for GNATcoverage.
5001 elsif Is_Null_Procedure (Subp)
5002 and then not Opt.Suppress_Control_Flow_Optimizations
5004 Actual := First_Actual (Call_Node);
5005 while Present (Actual) loop
5006 Remove_Side_Effects (Actual);
5007 Next_Actual (Actual);
5010 Rewrite (Call_Node, Make_Null_Statement (Loc));
5014 -- Handle inlining. No action needed if the subprogram is not inlined
5016 if not Is_Inlined (Subp) then
5019 -- Front-end inlining of expression functions (performed also when
5020 -- back-end inlining is enabled).
5022 elsif Is_Inlinable_Expression_Function (Subp) then
5024 (Call_Node, New_Copy (Expression_Of_Expression_Function (Subp)));
5025 Analyze (Call_Node);
5028 -- Handle front-end inlining
5030 elsif not Back_End_Inlining then
5031 Inlined_Subprogram : declare
5033 Must_Inline : Boolean := False;
5034 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
5037 -- Verify that the body to inline has already been seen, and
5038 -- that if the body is in the current unit the inlining does
5039 -- not occur earlier. This avoids order-of-elaboration problems
5042 -- This should be documented in sinfo/einfo ???
5045 or else Nkind (Spec) /= N_Subprogram_Declaration
5046 or else No (Body_To_Inline (Spec))
5048 Must_Inline := False;
5050 -- If this an inherited function that returns a private type,
5051 -- do not inline if the full view is an unconstrained array,
5052 -- because such calls cannot be inlined.
5054 elsif Present (Orig_Subp)
5055 and then Is_Array_Type (Etype (Orig_Subp))
5056 and then not Is_Constrained (Etype (Orig_Subp))
5058 Must_Inline := False;
5060 elsif In_Unfrozen_Instance (Scope (Subp)) then
5061 Must_Inline := False;
5064 Bod := Body_To_Inline (Spec);
5066 if (In_Extended_Main_Code_Unit (Call_Node)
5067 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
5068 or else Has_Pragma_Inline_Always (Subp))
5069 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
5071 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
5073 Must_Inline := True;
5075 -- If we are compiling a package body that is not the main
5076 -- unit, it must be for inlining/instantiation purposes,
5077 -- in which case we inline the call to insure that the same
5078 -- temporaries are generated when compiling the body by
5079 -- itself. Otherwise link errors can occur.
5081 -- If the function being called is itself in the main unit,
5082 -- we cannot inline, because there is a risk of double
5083 -- elaboration and/or circularity: the inlining can make
5084 -- visible a private entity in the body of the main unit,
5085 -- that gigi will see before its sees its proper definition.
5087 elsif not In_Extended_Main_Code_Unit (Call_Node)
5088 and then In_Package_Body
5090 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
5092 -- Inline calls to _Wrapped_Statements when generating C
5094 elsif Modify_Tree_For_C
5095 and then In_Same_Extended_Unit (Sloc (Bod), Loc)
5096 and then Chars (Name (Call_Node))
5097 = Name_uWrapped_Statements
5099 Must_Inline := True;
5104 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
5107 -- Let the back end handle it
5109 Add_Inlined_Body (Subp, Call_Node);
5111 if Front_End_Inlining
5112 and then Nkind (Spec) = N_Subprogram_Declaration
5113 and then In_Extended_Main_Code_Unit (Call_Node)
5114 and then No (Body_To_Inline (Spec))
5115 and then not Has_Completion (Subp)
5116 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
5119 ("cannot inline& (body not seen yet)?",
5123 end Inlined_Subprogram;
5125 -- Front-end expansion of simple functions returning unconstrained
5126 -- types (see Check_And_Split_Unconstrained_Function). Note that the
5127 -- case of a simple renaming (Body_To_Inline in N_Entity below, see
5128 -- also Build_Renamed_Body) cannot be expanded here because this may
5129 -- give rise to order-of-elaboration issues for the types of the
5130 -- parameters of the subprogram, if any.
5132 elsif Present (Unit_Declaration_Node (Subp))
5133 and then Nkind (Unit_Declaration_Node (Subp)) =
5134 N_Subprogram_Declaration
5135 and then Present (Body_To_Inline (Unit_Declaration_Node (Subp)))
5137 Nkind (Body_To_Inline (Unit_Declaration_Node (Subp))) not in
5140 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
5142 -- Back-end inlining either if optimization is enabled or the call is
5143 -- required to be inlined.
5145 elsif Optimization_Level > 0
5146 or else Has_Pragma_Inline_Always (Subp)
5148 Add_Inlined_Body (Subp, Call_Node);
5152 -- Check for protected subprogram. This is either an intra-object call,
5153 -- or a protected function call. Protected procedure calls are rewritten
5154 -- as entry calls and handled accordingly.
5156 -- In Ada 2005, this may be an indirect call to an access parameter that
5157 -- is an access_to_subprogram. In that case the anonymous type has a
5158 -- scope that is a protected operation, but the call is a regular one.
5159 -- In either case do not expand call if subprogram is eliminated.
5161 Scop := Scope (Subp);
5163 if Nkind (Call_Node) /= N_Entry_Call_Statement
5164 and then Is_Protected_Type (Scop)
5165 and then Ekind (Subp) /= E_Subprogram_Type
5166 and then not Is_Eliminated (Subp)
5168 -- If the call is an internal one, it is rewritten as a call to the
5169 -- corresponding unprotected subprogram.
5171 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
5174 -- Functions returning controlled objects need special attention. If
5175 -- the return type is limited, then the context is initialization and
5176 -- different processing applies. If the call is to a protected function,
5177 -- the expansion above will call Expand_Call recursively. Otherwise the
5178 -- function call is transformed into a reference to the result that has
5179 -- been built either on the primary or the secondary stack.
5181 if Needs_Finalization (Etype (Subp)) then
5182 if not Is_Build_In_Place_Function_Call (Call_Node)
5184 (No (First_Formal (Subp))
5186 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
5188 Expand_Ctrl_Function_Call
5189 (Call_Node, Needs_Secondary_Stack (Etype (Subp)));
5191 -- Build-in-place function calls which appear in anonymous contexts
5192 -- need a transient scope to ensure the proper finalization of the
5193 -- intermediate result after its use.
5195 elsif Is_Build_In_Place_Function_Call (Call_Node)
5196 and then Nkind (Parent (Unqual_Conv (Call_Node))) in
5197 N_Attribute_Reference
5199 | N_Indexed_Component
5200 | N_Object_Renaming_Declaration
5201 | N_Procedure_Call_Statement
5202 | N_Selected_Component
5205 (Ekind (Current_Scope) /= E_Loop
5206 or else Nkind (Parent (Call_Node)) /= N_Function_Call
5208 Is_Build_In_Place_Function_Call (Parent (Call_Node)))
5210 Establish_Transient_Scope
5211 (Call_Node, Needs_Secondary_Stack (Etype (Subp)));
5214 end Expand_Call_Helper;
5216 -------------------------------
5217 -- Expand_Ctrl_Function_Call --
5218 -------------------------------
5220 procedure Expand_Ctrl_Function_Call (N : Node_Id; Use_Sec_Stack : Boolean)
5222 Par : constant Node_Id := Parent (N);
5224 function Is_Element_Reference (N : Node_Id) return Boolean;
5225 -- Determine whether node N denotes a reference to an Ada 2012 container
5228 --------------------------
5229 -- Is_Element_Reference --
5230 --------------------------
5232 function Is_Element_Reference (N : Node_Id) return Boolean is
5233 Ref : constant Node_Id := Original_Node (N);
5236 -- Analysis marks an element reference by setting the generalized
5237 -- indexing attribute of an indexed component before the component
5238 -- is rewritten into a function call.
5241 Nkind (Ref) = N_Indexed_Component
5242 and then Present (Generalized_Indexing (Ref));
5243 end Is_Element_Reference;
5245 -- Start of processing for Expand_Ctrl_Function_Call
5248 -- Optimization: if the returned value is returned again, then no need
5249 -- to copy/readjust/finalize, we can just pass the value through (see
5250 -- Expand_N_Simple_Return_Statement), and thus no attachment is needed.
5251 -- Note that simple return statements are distributed into conditional
5252 -- expressions but we may be invoked before this distribution is done.
5254 if Nkind (Par) = N_Simple_Return_Statement
5255 or else (Nkind (Par) = N_If_Expression
5256 and then Nkind (Parent (Par)) = N_Simple_Return_Statement)
5257 or else (Nkind (Par) = N_Case_Expression_Alternative
5259 Nkind (Parent (Parent (Par))) = N_Simple_Return_Statement)
5264 -- Another optimization: if the returned value is used to initialize an
5265 -- object, then no need to copy/readjust/finalize, we can initialize it
5266 -- in place. However, if the call returns on the secondary stack, then
5267 -- we need the expansion because we'll be renaming the temporary as the
5268 -- (permanent) object.
5270 if Nkind (Par) = N_Object_Declaration and then not Use_Sec_Stack then
5274 -- Resolution is now finished, make sure we don't start analysis again
5275 -- because of the duplication.
5279 -- Apply the transformation, unless it was already applied manually
5281 if Nkind (Par) /= N_Reference then
5282 Remove_Side_Effects (N);
5285 -- The side effect removal of the function call produced a temporary.
5286 -- When the context is a case expression, if expression, or expression
5287 -- with actions, the lifetime of the temporary must be extended to match
5288 -- that of the context. Otherwise the function result will be finalized
5289 -- too early and affect the result of the expression. To prevent this
5290 -- unwanted effect, the temporary should not be considered for clean up
5291 -- actions by the general finalization machinery.
5293 -- Exception to this rule are references to Ada 2012 container elements.
5294 -- Such references must be finalized at the end of each iteration of the
5295 -- related quantified expression, otherwise the container will remain
5298 if Nkind (N) = N_Explicit_Dereference
5299 and then Within_Case_Or_If_Expression (N)
5300 and then not Is_Element_Reference (N)
5302 Set_Is_Ignored_Transient (Entity (Prefix (N)));
5304 end Expand_Ctrl_Function_Call;
5306 ----------------------------------------
5307 -- Expand_N_Extended_Return_Statement --
5308 ----------------------------------------
5310 -- If there is a Handled_Statement_Sequence, we rewrite this:
5312 -- return Result : T := <expression> do
5313 -- <handled_seq_of_stms>
5319 -- Result : T := <expression>;
5321 -- <handled_seq_of_stms>
5325 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
5327 -- return Result : T := <expression>;
5331 -- return <expression>;
5333 -- unless it's build-in-place or there's no <expression>, in which case
5337 -- Result : T := <expression>;
5342 -- Note that this case could have been written by the user as an extended
5343 -- return statement, or could have been transformed to this from a simple
5344 -- return statement.
5346 -- That is, we need to have a reified return object if there are statements
5347 -- (which might refer to it) or if we're doing build-in-place (so we can
5348 -- set its address to the final resting place or if there is no expression
5349 -- (in which case default initial values might need to be set)).
5351 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
5352 Loc : constant Source_Ptr := Sloc (N);
5353 Func_Id : constant Entity_Id :=
5354 Return_Applies_To (Return_Statement_Entity (N));
5355 Is_BIP_Func : constant Boolean :=
5356 Is_Build_In_Place_Function (Func_Id);
5357 Ret_Obj_Id : constant Entity_Id :=
5358 First_Entity (Return_Statement_Entity (N));
5359 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
5360 Ret_Typ : constant Entity_Id := Etype (Func_Id);
5362 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id;
5363 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
5365 -- From current activation chain
5366 -- To activation chain passed in by the caller
5367 -- New_Master master passed in by the caller
5369 -- Func_Id is the entity of the function where the extended return
5370 -- statement appears.
5372 ---------------------------
5373 -- Move_Activation_Chain --
5374 ---------------------------
5376 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id is
5379 Make_Procedure_Call_Statement (Loc,
5381 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
5383 Parameter_Associations => New_List (
5387 Make_Attribute_Reference (Loc,
5388 Prefix => Make_Identifier (Loc, Name_uChain),
5389 Attribute_Name => Name_Unrestricted_Access),
5391 -- Destination chain
5394 (Build_In_Place_Formal (Func_Id, BIP_Activation_Chain), Loc),
5399 (Build_In_Place_Formal (Func_Id, BIP_Task_Master), Loc)));
5400 end Move_Activation_Chain;
5407 Stmts : List_Id := No_List;
5409 Return_Stmt : Node_Id := Empty;
5410 -- Force initialization to facilitate static analysis
5412 -- Start of processing for Expand_N_Extended_Return_Statement
5415 -- Given that functionality of interface thunks is simple (just displace
5416 -- the pointer to the object) they are always handled by means of
5417 -- simple return statements.
5419 pragma Assert (not Is_Thunk (Current_Subprogram));
5421 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
5422 Exp := Expression (Ret_Obj_Decl);
5424 -- Assert that if F says "return R : T := G(...) do..."
5425 -- then F and G are both b-i-p, or neither b-i-p.
5427 if Present (Exp) and then Nkind (Exp) = N_Function_Call then
5428 pragma Assert (Ekind (Current_Subprogram) = E_Function);
5430 (Is_Build_In_Place_Function (Current_Subprogram) =
5431 Is_Build_In_Place_Function_Call (Exp));
5439 HSS := Handled_Statement_Sequence (N);
5441 -- If the returned object needs finalization actions, the function must
5442 -- perform the appropriate cleanup should it fail to return. The state
5443 -- of the function itself is tracked through a flag which is coupled
5444 -- with the scope finalizer. There is one flag per each return object
5445 -- in case of multiple returns.
5447 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
5449 Flag_Decl : Node_Id;
5450 Flag_Id : Entity_Id;
5454 -- Recover the function body
5456 Func_Bod := Unit_Declaration_Node (Func_Id);
5458 if Nkind (Func_Bod) = N_Subprogram_Declaration then
5459 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
5462 if Nkind (Func_Bod) = N_Function_Specification then
5463 Func_Bod := Parent (Func_Bod); -- one more level for child units
5466 pragma Assert (Nkind (Func_Bod) = N_Subprogram_Body);
5468 -- Create a flag to track the function state
5470 Flag_Id := Make_Temporary (Loc, 'F');
5471 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
5473 -- Insert the flag at the beginning of the function declarations,
5475 -- Fnn : Boolean := False;
5478 Make_Object_Declaration (Loc,
5479 Defining_Identifier => Flag_Id,
5480 Object_Definition =>
5481 New_Occurrence_Of (Standard_Boolean, Loc),
5483 New_Occurrence_Of (Standard_False, Loc));
5485 Prepend_To (Declarations (Func_Bod), Flag_Decl);
5486 Analyze (Flag_Decl);
5490 -- Build a simple_return_statement that returns the return object when
5491 -- there is a statement sequence, or no expression, or the analysis of
5492 -- the return object declaration generated extra actions, or the result
5493 -- will be built in place. Note however that we currently do this for
5494 -- all composite cases, even though they are not built in place.
5498 or else List_Length (Return_Object_Declarations (N)) > 1
5499 or else Is_Composite_Type (Ret_Typ)
5504 -- If the extended return has a handled statement sequence, then wrap
5505 -- it in a block and use the block as the first statement.
5509 Make_Block_Statement (Loc,
5510 Declarations => New_List,
5511 Handled_Statement_Sequence => HSS));
5514 -- If the result type contains tasks, we call Move_Activation_Chain.
5515 -- Later, the cleanup code will call Complete_Master, which will
5516 -- terminate any unactivated tasks belonging to the return statement
5517 -- master. But Move_Activation_Chain updates their master to be that
5518 -- of the caller, so they will not be terminated unless the return
5519 -- statement completes unsuccessfully due to exception, abort, goto,
5520 -- or exit. As a formality, we test whether the function requires the
5521 -- result to be built in place, though that's necessarily true for
5522 -- the case of result types with task parts.
5524 if Is_BIP_Func and then Has_Task (Ret_Typ) then
5526 -- The return expression is an aggregate for a complex type which
5527 -- contains tasks. This particular case is left unexpanded since
5528 -- the regular expansion would insert all temporaries and
5529 -- initialization code in the wrong block.
5531 if Nkind (Exp) = N_Aggregate then
5532 Expand_N_Aggregate (Exp);
5535 -- Do not move the activation chain if the return object does not
5538 if Has_Task (Etype (Ret_Obj_Id)) then
5539 Append_To (Stmts, Move_Activation_Chain (Func_Id));
5543 -- Update the state of the function right before the object is
5546 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
5548 Flag_Id : constant Entity_Id :=
5549 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
5556 Make_Assignment_Statement (Loc,
5557 Name => New_Occurrence_Of (Flag_Id, Loc),
5558 Expression => New_Occurrence_Of (Standard_True, Loc)));
5562 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
5565 -- Case where we build a return statement block
5567 if Present (HSS) then
5569 Make_Block_Statement (Loc,
5570 Declarations => Return_Object_Declarations (N),
5571 Handled_Statement_Sequence => HSS);
5573 -- We set the entity of the new block statement to be that of the
5574 -- return statement. This is necessary so that various fields, such
5575 -- as Finalization_Chain_Entity carry over from the return statement
5576 -- to the block. Note that this block is unusual, in that its entity
5577 -- is an E_Return_Statement rather than an E_Block.
5580 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
5582 -- Build a simple_return_statement that returns the return object
5585 Make_Simple_Return_Statement (Loc,
5586 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
5587 Append_To (Stmts, Return_Stmt);
5589 -- Case where we do not need to build a block. But we're about to drop
5590 -- Return_Object_Declarations on the floor, so assert that it contains
5591 -- only the return object declaration.
5593 else pragma Assert (List_Length (Return_Object_Declarations (N)) = 1);
5595 -- Build simple_return_statement that returns the expression directly
5597 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
5598 Result := Return_Stmt;
5601 -- Set the flag to prevent infinite recursion
5603 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
5604 Set_Return_Statement (Ret_Obj_Id, Return_Stmt);
5606 Rewrite (N, Result);
5608 -- AI12-043: The checks of 6.5(8.1/3) and 6.5(21/3) are made immediately
5609 -- before an object is returned. A predicate that applies to the return
5610 -- subtype is checked immediately before an object is returned.
5613 end Expand_N_Extended_Return_Statement;
5615 ----------------------------
5616 -- Expand_N_Function_Call --
5617 ----------------------------
5619 procedure Expand_N_Function_Call (N : Node_Id) is
5622 end Expand_N_Function_Call;
5624 ---------------------------------------
5625 -- Expand_N_Procedure_Call_Statement --
5626 ---------------------------------------
5628 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
5631 end Expand_N_Procedure_Call_Statement;
5633 ------------------------------------
5634 -- Expand_N_Return_When_Statement --
5635 ------------------------------------
5637 procedure Expand_N_Return_When_Statement (N : Node_Id) is
5638 Loc : constant Source_Ptr := Sloc (N);
5641 Make_If_Statement (Loc,
5642 Condition => Condition (N),
5643 Then_Statements => New_List (
5644 Make_Simple_Return_Statement (Loc,
5645 Expression => Expression (N)))));
5648 end Expand_N_Return_When_Statement;
5650 --------------------------------------
5651 -- Expand_N_Simple_Return_Statement --
5652 --------------------------------------
5654 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
5656 -- Defend against previous errors (i.e. the return statement calls a
5657 -- function that is not available in configurable runtime).
5659 if Present (Expression (N))
5660 and then Nkind (Expression (N)) = N_Empty
5662 Check_Error_Detected;
5666 -- Distinguish the function and non-function cases:
5668 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
5670 | E_Generic_Function
5672 Expand_Simple_Function_Return (N);
5676 | E_Generic_Procedure
5678 | E_Return_Statement
5680 Expand_Non_Function_Return (N);
5683 raise Program_Error;
5687 when RE_Not_Available =>
5689 end Expand_N_Simple_Return_Statement;
5691 ------------------------------
5692 -- Expand_N_Subprogram_Body --
5693 ------------------------------
5695 -- Add dummy push/pop label nodes at start and end to clear any local
5696 -- exception indications if local-exception-to-goto optimization is active.
5698 -- Add return statement if last statement in body is not a return statement
5699 -- (this makes things easier on Gigi which does not want to have to handle
5700 -- a missing return).
5702 -- Add call to Activate_Tasks if body is a task activator
5704 -- Deal with possible detection of infinite recursion
5706 -- Eliminate body completely if convention stubbed
5708 -- Encode entity names within body, since we will not need to reference
5709 -- these entities any longer in the front end.
5711 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5713 -- Reset Pure indication if any parameter has root type System.Address
5714 -- or has any parameters of limited types, where limited means that the
5715 -- run-time view is limited (i.e. the full type is limited).
5719 procedure Expand_N_Subprogram_Body (N : Node_Id) is
5720 Body_Id : constant Entity_Id := Defining_Entity (N);
5721 HSS : constant Node_Id := Handled_Statement_Sequence (N);
5722 Loc : constant Source_Ptr := Sloc (N);
5724 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id);
5725 -- Append a return statement to the statement sequence Stmts if the last
5726 -- statement is not already a return or a goto statement. Note that the
5727 -- latter test is not critical, it does not matter if we add a few extra
5728 -- returns, since they get eliminated anyway later on. Spec_Id denotes
5729 -- the corresponding spec of the subprogram body.
5735 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id) is
5736 Last_Stmt : Node_Id;
5741 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5742 -- not relevant in this context since they are not executable.
5744 Last_Stmt := Last (Stmts);
5745 while Nkind (Last_Stmt) in N_Pop_xxx_Label loop
5749 -- Now insert return unless last statement is a transfer
5751 if not Is_Transfer (Last_Stmt) then
5753 -- The source location for the return is the end label of the
5754 -- procedure if present. Otherwise use the sloc of the last
5755 -- statement in the list. If the list comes from a generated
5756 -- exception handler and we are not debugging generated code,
5757 -- all the statements within the handler are made invisible
5760 if Nkind (Parent (Stmts)) = N_Exception_Handler
5761 and then not Comes_From_Source (Parent (Stmts))
5763 Loc := Sloc (Last_Stmt);
5764 elsif Present (End_Label (HSS)) then
5765 Loc := Sloc (End_Label (HSS));
5767 Loc := Sloc (Last_Stmt);
5770 -- Append return statement, and set analyzed manually. We can't
5771 -- call Analyze on this return since the scope is wrong.
5773 -- Note: it almost works to push the scope and then do the Analyze
5774 -- call, but something goes wrong in some weird cases and it is
5775 -- not worth worrying about ???
5777 Stmt := Make_Simple_Return_Statement (Loc);
5779 -- The return statement is handled properly, and the call to the
5780 -- postcondition, inserted below, does not require information
5781 -- from the body either. However, that call is analyzed in the
5782 -- enclosing scope, and an elaboration check might improperly be
5783 -- added to it. A guard in Sem_Elab is needed to prevent that
5784 -- spurious check, see Check_Elab_Call.
5786 Append_To (Stmts, Stmt);
5787 Set_Analyzed (Stmt);
5789 -- Ada 2022 (AI12-0279): append the call to 'Yield unless this is
5790 -- a generic subprogram (since in such case it will be added to
5791 -- the instantiations).
5793 if Has_Yield_Aspect (Spec_Id)
5794 and then Ekind (Spec_Id) /= E_Generic_Procedure
5795 and then RTE_Available (RE_Yield)
5797 Insert_Action (Stmt,
5798 Make_Procedure_Call_Statement (Loc,
5799 New_Occurrence_Of (RTE (RE_Yield), Loc)));
5808 Spec_Id : Entity_Id;
5810 -- Start of processing for Expand_N_Subprogram_Body
5813 if Present (Corresponding_Spec (N)) then
5814 Spec_Id := Corresponding_Spec (N);
5819 -- If this is a Pure function which has any parameters whose root type
5820 -- is System.Address, reset the Pure indication.
5821 -- This check is also performed when the subprogram is frozen, but we
5822 -- repeat it on the body so that the indication is consistent, and so
5823 -- it applies as well to bodies without separate specifications.
5825 if Is_Pure (Spec_Id)
5826 and then Is_Subprogram (Spec_Id)
5827 and then not Has_Pragma_Pure_Function (Spec_Id)
5829 Check_Function_With_Address_Parameter (Spec_Id);
5831 if Spec_Id /= Body_Id then
5832 Set_Is_Pure (Body_Id, Is_Pure (Spec_Id));
5836 -- Set L to either the list of declarations if present, or to the list
5837 -- of statements if no declarations are present. This is used to insert
5838 -- new stuff at the start.
5840 if Is_Non_Empty_List (Declarations (N)) then
5841 L := Declarations (N);
5843 L := Statements (HSS);
5846 -- If local-exception-to-goto optimization active, insert dummy push
5847 -- statements at start, and dummy pop statements at end, but inhibit
5848 -- this if we have No_Exception_Handlers, since they are useless and
5849 -- interfere with analysis, e.g. by CodePeer. We also don't need these
5850 -- if we're unnesting subprograms because the only purpose of these
5851 -- nodes is to ensure we don't set a label in one subprogram and branch
5852 -- to it in another.
5854 if (Debug_Flag_Dot_G
5855 or else Restriction_Active (No_Exception_Propagation))
5856 and then not Restriction_Active (No_Exception_Handlers)
5857 and then not CodePeer_Mode
5858 and then not Unnest_Subprogram_Mode
5859 and then Is_Non_Empty_List (L)
5862 FS : constant Node_Id := First (L);
5863 FL : constant Source_Ptr := Sloc (FS);
5868 -- LS points to either last statement, if statements are present
5869 -- or to the last declaration if there are no statements present.
5870 -- It is the node after which the pop's are generated.
5872 if Is_Non_Empty_List (Statements (HSS)) then
5873 LS := Last (Statements (HSS));
5880 Insert_List_Before_And_Analyze (FS, New_List (
5881 Make_Push_Constraint_Error_Label (FL),
5882 Make_Push_Program_Error_Label (FL),
5883 Make_Push_Storage_Error_Label (FL)));
5885 Insert_List_After_And_Analyze (LS, New_List (
5886 Make_Pop_Constraint_Error_Label (LL),
5887 Make_Pop_Program_Error_Label (LL),
5888 Make_Pop_Storage_Error_Label (LL)));
5892 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5894 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5900 -- Loop through formals
5902 F := First_Formal (Spec_Id);
5903 while Present (F) loop
5904 if Is_Scalar_Type (Etype (F))
5905 and then Ekind (F) = E_Out_Parameter
5907 Check_Restriction (No_Default_Initialization, F);
5909 -- Insert the initialization. We turn off validity checks
5910 -- for this assignment, since we do not want any check on
5911 -- the initial value itself (which may well be invalid).
5912 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5915 Make_Assignment_Statement (Loc,
5916 Name => New_Occurrence_Of (F, Loc),
5917 Expression => Get_Simple_Init_Val (Etype (F), N));
5918 Set_Suppress_Assignment_Checks (A);
5920 Insert_Before_And_Analyze (First (L),
5921 A, Suppress => Validity_Check);
5929 -- Clear out statement list for stubbed procedure
5931 if Present (Corresponding_Spec (N)) then
5932 Set_Elaboration_Flag (N, Spec_Id);
5934 if Convention (Spec_Id) = Convention_Stubbed
5935 or else Is_Eliminated (Spec_Id)
5937 Set_Declarations (N, Empty_List);
5938 Set_Handled_Statement_Sequence (N,
5939 Make_Handled_Sequence_Of_Statements (Loc,
5940 Statements => New_List (Make_Null_Statement (Loc))));
5946 -- Create a set of discriminals for the next protected subprogram body
5948 if Is_List_Member (N)
5949 and then Present (Parent (List_Containing (N)))
5950 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5951 and then Present (Next_Protected_Operation (N))
5953 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5956 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5957 -- subprograms with no specs are not frozen.
5959 Compute_Returns_By_Ref (Spec_Id);
5961 -- For a procedure, we add a return for all possible syntactic ends of
5964 if Ekind (Spec_Id) in E_Procedure | E_Generic_Procedure then
5965 Add_Return (Spec_Id, Statements (HSS));
5967 if Present (Exception_Handlers (HSS)) then
5968 Except_H := First_Non_Pragma (Exception_Handlers (HSS));
5969 while Present (Except_H) loop
5970 Add_Return (Spec_Id, Statements (Except_H));
5971 Next_Non_Pragma (Except_H);
5975 -- For a function, we must deal with the case where there is at least
5976 -- one missing return. What we do is to wrap the entire body of the
5977 -- function in a block:
5990 -- raise Program_Error;
5993 -- This approach is necessary because the raise must be signalled to the
5994 -- caller, not handled by any local handler (RM 6.4(11)).
5996 -- Note: we do not need to analyze the constructed sequence here, since
5997 -- it has no handler, and an attempt to analyze the handled statement
5998 -- sequence twice is risky in various ways (e.g. the issue of expanding
5999 -- cleanup actions twice).
6001 elsif Has_Missing_Return (Spec_Id) then
6003 Hloc : constant Source_Ptr := Sloc (HSS);
6004 Blok : constant Node_Id :=
6005 Make_Block_Statement (Hloc,
6006 Handled_Statement_Sequence => HSS);
6007 Rais : constant Node_Id :=
6008 Make_Raise_Program_Error (Hloc,
6009 Reason => PE_Missing_Return);
6012 Set_Handled_Statement_Sequence (N,
6013 Make_Handled_Sequence_Of_Statements (Hloc,
6014 Statements => New_List (Blok, Rais)));
6016 Push_Scope (Spec_Id);
6023 -- If subprogram contains a parameterless recursive call, then we may
6024 -- have an infinite recursion, so see if we can generate code to check
6025 -- for this possibility if storage checks are not suppressed.
6027 if Ekind (Spec_Id) = E_Procedure
6028 and then Has_Recursive_Call (Spec_Id)
6029 and then not Storage_Checks_Suppressed (Spec_Id)
6031 Detect_Infinite_Recursion (N, Spec_Id);
6034 -- Set to encode entity names in package body before gigi is called
6036 Qualify_Entity_Names (N);
6038 -- If the body belongs to a nonabstract library-level source primitive
6039 -- of a tagged type, install an elaboration check which ensures that a
6040 -- dispatching call targeting the primitive will not execute the body
6041 -- without it being previously elaborated.
6043 Install_Primitive_Elaboration_Check (N);
6044 end Expand_N_Subprogram_Body;
6046 -----------------------------------
6047 -- Expand_N_Subprogram_Body_Stub --
6048 -----------------------------------
6050 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
6054 if Present (Corresponding_Body (N)) then
6055 Bod := Unit_Declaration_Node (Corresponding_Body (N));
6057 -- The body may have been expanded already when it is analyzed
6058 -- through the subunit node. Do no expand again: it interferes
6059 -- with the construction of unnesting tables when generating C.
6061 if not Analyzed (Bod) then
6062 Expand_N_Subprogram_Body (Bod);
6065 -- Add full qualification to entities that may be created late
6066 -- during unnesting.
6068 Qualify_Entity_Names (N);
6070 end Expand_N_Subprogram_Body_Stub;
6072 -------------------------------------
6073 -- Expand_N_Subprogram_Declaration --
6074 -------------------------------------
6076 -- If the declaration appears within a protected body, it is a private
6077 -- operation of the protected type. We must create the corresponding
6078 -- protected subprogram an associated formals. For a normal protected
6079 -- operation, this is done when expanding the protected type declaration.
6081 -- If the declaration is for a null procedure, emit null body
6083 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
6084 Loc : constant Source_Ptr := Sloc (N);
6085 Subp : constant Entity_Id := Defining_Entity (N);
6089 Scop : constant Entity_Id := Scope (Subp);
6091 Prot_Decl : Node_Id;
6092 Prot_Id : Entity_Id;
6096 -- Deal with case of protected subprogram. Do not generate protected
6097 -- operation if operation is flagged as eliminated.
6099 if Is_List_Member (N)
6100 and then Present (Parent (List_Containing (N)))
6101 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
6102 and then Is_Protected_Type (Scop)
6104 if No (Protected_Body_Subprogram (Subp))
6105 and then not Is_Eliminated (Subp)
6108 Make_Subprogram_Declaration (Loc,
6110 Build_Protected_Sub_Specification
6111 (N, Scop, Unprotected_Mode));
6113 -- The protected subprogram is declared outside of the protected
6114 -- body. Given that the body has frozen all entities so far, we
6115 -- analyze the subprogram and perform freezing actions explicitly.
6116 -- including the generation of an explicit freeze node, to ensure
6117 -- that gigi has the proper order of elaboration.
6118 -- If the body is a subunit, the insertion point is before the
6119 -- stub in the parent.
6121 Prot_Bod := Parent (List_Containing (N));
6123 if Nkind (Parent (Prot_Bod)) = N_Subunit then
6124 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
6127 Insert_Before (Prot_Bod, Prot_Decl);
6128 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
6129 Set_Has_Delayed_Freeze (Prot_Id);
6131 Push_Scope (Scope (Scop));
6132 Analyze (Prot_Decl);
6133 Freeze_Before (N, Prot_Id);
6134 Set_Protected_Body_Subprogram (Subp, Prot_Id);
6138 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
6139 -- cases this is superfluous because calls to it will be automatically
6140 -- inlined, but we definitely need the body if preconditions for the
6141 -- procedure are present, or if performing coverage analysis.
6143 elsif Nkind (Specification (N)) = N_Procedure_Specification
6144 and then Null_Present (Specification (N))
6147 Bod : constant Node_Id := Body_To_Inline (N);
6150 Set_Has_Completion (Subp, False);
6151 Append_Freeze_Action (Subp, Bod);
6153 -- The body now contains raise statements, so calls to it will
6156 Set_Is_Inlined (Subp, False);
6160 -- When generating C code, transform a function that returns a
6161 -- constrained array type into a procedure with an out parameter
6162 -- that carries the return value.
6164 -- We skip this transformation for unchecked conversions, since they
6165 -- are not needed by the C generator (and this also produces cleaner
6168 Typ := Get_Fullest_View (Etype (Subp));
6170 if Transform_Function_Array
6171 and then Nkind (Specification (N)) = N_Function_Specification
6172 and then Is_Array_Type (Typ)
6173 and then Is_Constrained (Typ)
6174 and then not Is_Unchecked_Conversion_Instance (Subp)
6176 Build_Procedure_Form (N);
6178 end Expand_N_Subprogram_Declaration;
6180 --------------------------------
6181 -- Expand_Non_Function_Return --
6182 --------------------------------
6184 procedure Expand_Non_Function_Return (N : Node_Id) is
6185 pragma Assert (No (Expression (N)));
6187 Loc : constant Source_Ptr := Sloc (N);
6188 Scope_Id : Entity_Id := Return_Applies_To (Return_Statement_Entity (N));
6189 Kind : constant Entity_Kind := Ekind (Scope_Id);
6192 Goto_Stat : Node_Id;
6196 -- Ada 2022 (AI12-0279)
6198 if Has_Yield_Aspect (Scope_Id)
6199 and then RTE_Available (RE_Yield)
6202 Make_Procedure_Call_Statement (Loc,
6203 New_Occurrence_Of (RTE (RE_Yield), Loc)));
6206 -- If it is a return from a procedure do no extra steps
6208 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
6211 -- If it is a nested return within an extended one, replace it with a
6212 -- return of the previously declared return object.
6214 elsif Kind = E_Return_Statement then
6216 Make_Simple_Return_Statement (Loc,
6218 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
6219 Set_Comes_From_Extended_Return_Statement (N);
6220 Set_Return_Statement_Entity (N, Scope_Id);
6221 Expand_Simple_Function_Return (N);
6225 pragma Assert (Is_Entry (Scope_Id));
6227 -- Look at the enclosing block to see whether the return is from an
6228 -- accept statement or an entry body.
6230 for J in reverse 0 .. Scope_Stack.Last loop
6231 Scope_Id := Scope_Stack.Table (J).Entity;
6232 exit when Is_Concurrent_Type (Scope_Id);
6235 -- If it is a return from accept statement it is expanded as call to
6236 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6238 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6239 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6241 if Is_Task_Type (Scope_Id) then
6244 Make_Procedure_Call_Statement (Loc,
6245 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
6246 Insert_Before (N, Call);
6247 -- why not insert actions here???
6250 Acc_Stat := Parent (N);
6251 while Nkind (Acc_Stat) /= N_Accept_Statement loop
6252 Acc_Stat := Parent (Acc_Stat);
6255 Lab_Node := Last (Statements
6256 (Handled_Statement_Sequence (Acc_Stat)));
6258 Goto_Stat := Make_Goto_Statement (Loc,
6259 Name => New_Occurrence_Of
6260 (Entity (Identifier (Lab_Node)), Loc));
6262 Set_Analyzed (Goto_Stat);
6264 Rewrite (N, Goto_Stat);
6267 -- If it is a return from an entry body, put a Complete_Entry_Body call
6268 -- in front of the return.
6270 elsif Is_Protected_Type (Scope_Id) then
6272 Make_Procedure_Call_Statement (Loc,
6274 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
6275 Parameter_Associations => New_List (
6276 Make_Attribute_Reference (Loc,
6279 (Find_Protection_Object (Current_Scope), Loc),
6280 Attribute_Name => Name_Unchecked_Access)));
6282 Insert_Before (N, Call);
6285 end Expand_Non_Function_Return;
6287 ---------------------------------------
6288 -- Expand_Protected_Object_Reference --
6289 ---------------------------------------
6291 function Expand_Protected_Object_Reference
6293 Scop : Entity_Id) return Node_Id
6295 Loc : constant Source_Ptr := Sloc (N);
6302 Rec := Make_Identifier (Loc, Name_uObject);
6303 Set_Etype (Rec, Corresponding_Record_Type (Scop));
6305 -- Find enclosing protected operation, and retrieve its first parameter,
6306 -- which denotes the enclosing protected object. If the enclosing
6307 -- operation is an entry, we are immediately within the protected body,
6308 -- and we can retrieve the object from the service entries procedure. A
6309 -- barrier function has the same signature as an entry. A barrier
6310 -- function is compiled within the protected object, but unlike
6311 -- protected operations its never needs locks, so that its protected
6312 -- body subprogram points to itself.
6314 Proc := Current_Scope;
6315 while Present (Proc) and then Scope (Proc) /= Scop loop
6316 Proc := Scope (Proc);
6317 if Is_Subprogram (Proc)
6318 and then Present (Protected_Subprogram (Proc))
6320 Proc := Protected_Subprogram (Proc);
6324 Corr := Protected_Body_Subprogram (Proc);
6328 -- Previous error left expansion incomplete.
6329 -- Nothing to do on this call.
6336 (First (Parameter_Specifications (Parent (Corr))));
6338 if Is_Subprogram (Proc) and then Proc /= Corr then
6340 -- Protected function or procedure
6342 Set_Entity (Rec, Param);
6344 -- Rec is a reference to an entity which will not be in scope when
6345 -- the call is reanalyzed, and needs no further analysis.
6350 -- Entry or barrier function for entry body. The first parameter of
6351 -- the entry body procedure is pointer to the object. We create a
6352 -- local variable of the proper type, duplicating what is done to
6353 -- define _object later on.
6357 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
6361 Make_Full_Type_Declaration (Loc,
6362 Defining_Identifier => Obj_Ptr,
6364 Make_Access_To_Object_Definition (Loc,
6365 Subtype_Indication =>
6367 (Corresponding_Record_Type (Scop), Loc))));
6369 Insert_Actions (N, Decls);
6370 Freeze_Before (N, Obj_Ptr);
6373 Make_Explicit_Dereference (Loc,
6375 Unchecked_Convert_To (Obj_Ptr,
6376 New_Occurrence_Of (Param, Loc)));
6378 -- Analyze new actual. Other actuals in calls are already analyzed
6379 -- and the list of actuals is not reanalyzed after rewriting.
6381 Set_Parent (Rec, N);
6387 end Expand_Protected_Object_Reference;
6389 --------------------------------------
6390 -- Expand_Protected_Subprogram_Call --
6391 --------------------------------------
6393 procedure Expand_Protected_Subprogram_Call
6400 procedure Expand_Internal_Init_Call;
6401 -- A call to an operation of the type may occur in the initialization
6402 -- of a private component. In that case the prefix of the call is an
6403 -- entity name and the call is treated as internal even though it
6404 -- appears in code outside of the protected type.
6406 procedure Freeze_Called_Function;
6407 -- If it is a function call it can appear in elaboration code and
6408 -- the called entity must be frozen before the call. This must be
6409 -- done before the call is expanded, as the expansion may rewrite it
6410 -- to something other than a call (e.g. a temporary initialized in a
6411 -- transient block).
6413 -------------------------------
6414 -- Expand_Internal_Init_Call --
6415 -------------------------------
6417 procedure Expand_Internal_Init_Call is
6419 -- If the context is a protected object (rather than a protected
6420 -- type) the call itself is bound to raise program_error because
6421 -- the protected body will not have been elaborated yet. This is
6422 -- diagnosed subsequently in Sem_Elab.
6424 Freeze_Called_Function;
6426 -- The target of the internal call is the first formal of the
6427 -- enclosing initialization procedure.
6429 Rec := New_Occurrence_Of (First_Formal (Current_Scope), Sloc (N));
6430 Build_Protected_Subprogram_Call (N,
6435 Resolve (N, Etype (Subp));
6436 end Expand_Internal_Init_Call;
6438 ----------------------------
6439 -- Freeze_Called_Function --
6440 ----------------------------
6442 procedure Freeze_Called_Function is
6444 if Ekind (Subp) = E_Function then
6445 Freeze_Expression (Name (N));
6447 end Freeze_Called_Function;
6449 -- Start of processing for Expand_Protected_Subprogram_Call
6452 -- If the protected object is not an enclosing scope, this is an inter-
6453 -- object function call. Inter-object procedure calls are expanded by
6454 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
6455 -- subprogram being called is in the protected body being compiled, and
6456 -- if the protected object in the call is statically the enclosing type.
6457 -- The object may be a component of some other data structure, in which
6458 -- case this must be handled as an inter-object call.
6460 if not Scope_Within_Or_Same (Inner => Current_Scope, Outer => Scop)
6461 or else Is_Entry_Wrapper (Current_Scope)
6462 or else not Is_Entity_Name (Name (N))
6464 if Nkind (Name (N)) = N_Selected_Component then
6465 Rec := Prefix (Name (N));
6467 elsif Nkind (Name (N)) = N_Indexed_Component then
6468 Rec := Prefix (Prefix (Name (N)));
6470 -- If this is a call within an entry wrapper, it appears within a
6471 -- precondition that calls another primitive of the synchronized
6472 -- type. The target object of the call is the first actual on the
6473 -- wrapper. Note that this is an external call, because the wrapper
6474 -- is called outside of the synchronized object. This means that
6475 -- an entry call to an entry with preconditions involves two
6476 -- synchronized operations.
6478 elsif Ekind (Current_Scope) = E_Procedure
6479 and then Is_Entry_Wrapper (Current_Scope)
6481 Rec := New_Occurrence_Of (First_Entity (Current_Scope), Sloc (N));
6483 -- A default parameter of a protected operation may be a call to
6484 -- a protected function of the type. This appears as an internal
6485 -- call in the profile of the operation, but if the context is an
6486 -- external call we must convert the call into an external one,
6487 -- using the protected object that is the target, so that:
6490 -- is transformed into
6493 elsif Nkind (Parent (N)) = N_Procedure_Call_Statement
6494 and then Nkind (Name (Parent (N))) = N_Selected_Component
6495 and then Is_Protected_Type (Etype (Prefix (Name (Parent (N)))))
6496 and then Is_Entity_Name (Name (N))
6497 and then Scope (Entity (Name (N))) =
6498 Etype (Prefix (Name (Parent (N))))
6501 Make_Selected_Component (Sloc (N),
6502 Prefix => New_Copy_Tree (Prefix (Name (Parent (N)))),
6503 Selector_Name => Relocate_Node (Name (N))));
6505 Analyze_And_Resolve (N);
6509 -- If the context is the initialization procedure for a protected
6510 -- type, the call is legal because the called entity must be a
6511 -- function of that enclosing type, and this is treated as an
6515 (Is_Entity_Name (Name (N)) and then Inside_Init_Proc);
6517 Expand_Internal_Init_Call;
6521 Freeze_Called_Function;
6522 Build_Protected_Subprogram_Call (N,
6523 Name => New_Occurrence_Of (Subp, Sloc (N)),
6524 Rec => Convert_Concurrent (Rec, Etype (Rec)),
6528 Rec := Expand_Protected_Object_Reference (N, Scop);
6534 Freeze_Called_Function;
6535 Build_Protected_Subprogram_Call (N,
6541 -- Analyze and resolve the new call. The actuals have already been
6542 -- resolved, but expansion of a function call will add extra actuals
6543 -- if needed. Analysis of a procedure call already includes resolution.
6547 if Ekind (Subp) = E_Function then
6548 Resolve (N, Etype (Subp));
6550 end Expand_Protected_Subprogram_Call;
6552 -----------------------------------
6553 -- Expand_Simple_Function_Return --
6554 -----------------------------------
6556 -- The "simple" comes from the syntax rule simple_return_statement. The
6557 -- semantics are not at all simple.
6559 procedure Expand_Simple_Function_Return (N : Node_Id) is
6560 Loc : constant Source_Ptr := Sloc (N);
6562 Scope_Id : constant Entity_Id :=
6563 Return_Applies_To (Return_Statement_Entity (N));
6564 -- The function we are returning from
6566 R_Type : constant Entity_Id := Etype (Scope_Id);
6567 -- The result type of the function
6569 Utyp : constant Entity_Id := Underlying_Type (R_Type);
6570 -- The underlying result type of the function
6572 Exp : Node_Id := Expression (N);
6573 pragma Assert (Present (Exp));
6575 Exp_Is_Function_Call : constant Boolean :=
6576 Nkind (Exp) = N_Function_Call
6578 (Is_Captured_Function_Call (Exp)
6579 and then Is_Related_To_Func_Return (Entity (Prefix (Exp))));
6580 -- If the expression is a captured function call, then we need to make
6581 -- sure that the object doing the capture is properly recognized by the
6582 -- Is_Related_To_Func_Return predicate; otherwise, if it is of a type
6583 -- that needs finalization, Requires_Cleanup_Actions would return true
6584 -- because of this and Build_Finalizer would finalize it prematurely.
6586 Exp_Typ : constant Entity_Id := Etype (Exp);
6587 -- The type of the expression (not necessarily the same as R_Type)
6589 Subtype_Ind : Node_Id;
6590 -- If the result type of the function is class-wide and the expression
6591 -- has a specific type, then we use the expression's type as the type of
6592 -- the return object. In cases where the expression is an aggregate that
6593 -- is built in place, this avoids the need for an expensive conversion
6594 -- of the return object to the specific type on assignments to the
6595 -- individual components.
6597 -- Start of processing for Expand_Simple_Function_Return
6600 if Is_Class_Wide_Type (R_Type)
6601 and then not Is_Class_Wide_Type (Exp_Typ)
6602 and then Nkind (Exp) /= N_Type_Conversion
6604 Subtype_Ind := New_Occurrence_Of (Exp_Typ, Loc);
6606 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
6608 -- If the result type is class-wide and the expression is a view
6609 -- conversion, the conversion plays no role in the expansion because
6610 -- it does not modify the tag of the object. Remove the conversion
6611 -- altogether to prevent tag overwriting.
6613 if Is_Class_Wide_Type (R_Type)
6614 and then not Is_Class_Wide_Type (Exp_Typ)
6615 and then Nkind (Exp) = N_Type_Conversion
6617 Exp := Expression (Exp);
6621 -- Assert that if F says "return G(...);"
6622 -- then F and G are both b-i-p, or neither b-i-p.
6624 if Nkind (Exp) = N_Function_Call then
6625 pragma Assert (Ekind (Scope_Id) = E_Function);
6627 -- This assertion works fine because Is_Build_In_Place_Function_Call
6628 -- returns True for BIP function calls but also for function calls
6629 -- that have BIP formals.
6632 (Has_BIP_Formals (Scope_Id) =
6633 Is_Build_In_Place_Function_Call (Exp));
6637 -- For the case of a simple return that does not come from an
6638 -- extended return, in the case of build-in-place, we rewrite
6639 -- "return <expression>;" to be:
6641 -- return _anon_ : <return_subtype> := <expression>
6643 -- The expansion produced by Expand_N_Extended_Return_Statement will
6644 -- contain simple return statements (for example, a block containing
6645 -- simple return of the return object), which brings us back here with
6646 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
6647 -- checking for a simple return that does not come from an extended
6648 -- return is to avoid this infinite recursion.
6650 -- The reason for this design is that for Ada 2005 limited returns, we
6651 -- need to reify the return object, so we can build it "in place", and
6652 -- we need a block statement to hang finalization and tasking stuff.
6655 (Comes_From_Extended_Return_Statement (N)
6656 or else not Is_Build_In_Place_Function_Call (Exp)
6657 or else Has_BIP_Formals (Scope_Id));
6659 if not Comes_From_Extended_Return_Statement (N)
6660 and then Is_Build_In_Place_Function (Scope_Id)
6662 -- The functionality of interface thunks is simple and it is always
6663 -- handled by means of simple return statements. This leaves their
6664 -- expansion simple and clean.
6666 and then not Is_Thunk (Scope_Id)
6669 Return_Object_Entity : constant Entity_Id :=
6670 Make_Temporary (Loc, 'R', Exp);
6672 Obj_Decl : constant Node_Id :=
6673 Make_Object_Declaration (Loc,
6674 Defining_Identifier => Return_Object_Entity,
6675 Object_Definition => Subtype_Ind,
6678 Ext : constant Node_Id :=
6679 Make_Extended_Return_Statement (Loc,
6680 Return_Object_Declarations => New_List (Obj_Decl));
6681 -- Do not perform this high-level optimization if the result type
6682 -- is an interface because the "this" pointer must be displaced.
6691 -- Here we have a simple return statement that is part of the expansion
6692 -- of an extended return statement (either written by the user, or
6693 -- generated by the above code).
6695 -- Always normalize C/Fortran boolean result. This is not always needed,
6696 -- but it seems a good idea to minimize the passing around of non-
6697 -- normalized values, and in any case this handles the processing of
6698 -- barrier functions for protected types, which turn the condition into
6699 -- a return statement.
6701 if Is_Boolean_Type (Exp_Typ) and then Nonzero_Is_True (Exp_Typ) then
6702 Adjust_Condition (Exp);
6703 Adjust_Result_Type (Exp, Exp_Typ);
6705 -- The adjustment of the expression may have rewritten the return
6706 -- statement itself, e.g. when it is turned into an if expression.
6708 if Nkind (N) /= N_Simple_Return_Statement then
6713 -- Do validity check if enabled for returns
6715 if Validity_Checks_On and then Validity_Check_Returns then
6719 -- Check the result expression of a scalar function against the subtype
6720 -- of the function by inserting a conversion. This conversion must
6721 -- eventually be performed for other classes of types, but for now it's
6722 -- only done for scalars ???
6724 if Is_Scalar_Type (Exp_Typ) and then Exp_Typ /= R_Type then
6725 Rewrite (Exp, Convert_To (R_Type, Exp));
6727 -- The expression is resolved to ensure that the conversion gets
6728 -- expanded to generate a possible constraint check.
6730 Analyze_And_Resolve (Exp, R_Type);
6733 -- Deal with returning variable length objects and controlled types
6735 -- Nothing to do if we are returning by reference
6737 if Is_Build_In_Place_Function (Scope_Id) then
6738 -- Prevent the reclamation of the secondary stack by all enclosing
6739 -- blocks and loops as well as the related function; otherwise the
6740 -- result would be reclaimed too early.
6742 if Needs_BIP_Alloc_Form (Scope_Id) then
6743 Set_Enclosing_Sec_Stack_Return (N);
6746 elsif Is_Limited_View (R_Type) then
6749 -- No copy needed for thunks returning interface type objects since
6750 -- the object is returned by reference and the maximum functionality
6751 -- required is just to displace the pointer.
6753 elsif Is_Thunk (Scope_Id) and then Is_Interface (Exp_Typ) then
6756 -- If the call is within a thunk and the type is a limited view, the
6757 -- back end will eventually see the non-limited view of the type.
6759 elsif Is_Thunk (Scope_Id) and then Is_Incomplete_Type (Exp_Typ) then
6762 -- A return statement from an ignored Ghost function does not use the
6763 -- secondary stack (or any other one).
6765 elsif (not Needs_Secondary_Stack (R_Type)
6766 and then not Is_Secondary_Stack_Thunk (Scope_Id))
6767 or else Is_Ignored_Ghost_Entity (Scope_Id)
6769 -- Mutable records with variable-length components are not returned
6770 -- on the sec-stack, so we need to make sure that the back end will
6771 -- only copy back the size of the actual value, and not the maximum
6772 -- size. We create an actual subtype for this purpose. However we
6773 -- need not do it if the expression is a function call since this
6774 -- will be done in the called function and doing it here too would
6775 -- cause a temporary with maximum size to be created. Likewise for
6776 -- a special return object, since there is no copy in this case.
6779 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exp_Typ));
6784 if not Exp_Is_Function_Call
6785 and then not (Is_Entity_Name (Exp)
6786 and then Is_Special_Return_Object (Entity (Exp)))
6787 and then Has_Defaulted_Discriminants (Ubt)
6788 and then not Is_Constrained (Ubt)
6789 and then not Has_Unchecked_Union (Ubt)
6791 Decl := Build_Actual_Subtype (Ubt, Exp);
6792 Ent := Defining_Identifier (Decl);
6793 Insert_Action (Exp, Decl);
6794 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
6795 Analyze_And_Resolve (Exp);
6799 -- For types which need finalization, do the allocation on the return
6800 -- stack manually in order to call Adjust at the right time:
6802 -- type Ann is access R_Type;
6803 -- for Ann'Storage_pool use rs_pool;
6804 -- Rnn : constant Ann := new Exp_Typ'(Exp);
6807 -- but optimize the case where the result is a function call that
6808 -- also needs finalization. In this case the result can directly be
6809 -- allocated on the return stack of the caller and no further
6810 -- processing is required. Likewise if this is a return object.
6812 if Comes_From_Extended_Return_Statement (N) then
6815 elsif Present (Utyp)
6816 and then Needs_Finalization (Utyp)
6817 and then not (Exp_Is_Function_Call
6818 and then Needs_Finalization (Exp_Typ))
6821 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6823 Alloc_Node : Node_Id;
6827 Mutate_Ekind (Acc_Typ, E_Access_Type);
6829 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_RS_Pool));
6831 -- This is an allocator for the return stack, and it's fine
6832 -- to have Comes_From_Source set False on it, as gigi knows not
6833 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6836 Make_Allocator (Loc,
6838 Make_Qualified_Expression (Loc,
6839 Subtype_Mark => New_Occurrence_Of (Exp_Typ, Loc),
6840 Expression => Relocate_Node (Exp)));
6842 -- We do not want discriminant checks on the declaration,
6843 -- given that it gets its value from the allocator.
6845 Set_No_Initialization (Alloc_Node);
6847 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6849 Insert_Actions (Exp, New_List (
6850 Make_Full_Type_Declaration (Loc,
6851 Defining_Identifier => Acc_Typ,
6853 Make_Access_To_Object_Definition (Loc,
6854 Subtype_Indication => Subtype_Ind)),
6856 Make_Object_Declaration (Loc,
6857 Defining_Identifier => Temp,
6858 Constant_Present => True,
6859 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
6860 Expression => Alloc_Node)));
6863 Make_Explicit_Dereference (Loc,
6864 Prefix => New_Occurrence_Of (Temp, Loc)));
6866 Analyze_And_Resolve (Exp, R_Type);
6870 -- Here if secondary stack is used
6873 -- Prevent the reclamation of the secondary stack by all enclosing
6874 -- blocks and loops as well as the related function; otherwise the
6875 -- result would be reclaimed too early.
6877 Set_Enclosing_Sec_Stack_Return (N);
6879 -- Nothing else to do for a return object
6881 if Comes_From_Extended_Return_Statement (N) then
6884 -- Optimize the case where the result is a function call that also
6885 -- returns on the secondary stack; in this case the result is already
6886 -- on the secondary stack and no further processing is required.
6888 elsif Exp_Is_Function_Call
6889 and then Needs_Secondary_Stack (Exp_Typ)
6891 -- Remove side effects from the expression now so that other parts
6892 -- of the expander do not have to reanalyze this node without this
6895 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
6897 -- Ada 2005 (AI-251): If the type of the returned object is
6898 -- an interface then add an implicit type conversion to force
6899 -- displacement of the "this" pointer.
6901 if Is_Interface (R_Type) then
6902 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6905 Analyze_And_Resolve (Exp, R_Type);
6907 -- For types which both need finalization and are returned on the
6908 -- secondary stack, do the allocation on secondary stack manually
6909 -- in order to call Adjust at the right time:
6911 -- type Ann is access R_Type;
6912 -- for Ann'Storage_pool use ss_pool;
6913 -- Rnn : constant Ann := new Exp_Typ'(Exp);
6916 -- And we do the same for class-wide types that are not potentially
6917 -- controlled (by the virtue of restriction No_Finalization) because
6918 -- gigi is not able to properly allocate class-wide types.
6920 -- But optimize the case where the result is a function call that
6921 -- also needs finalization; in this case the result can directly be
6922 -- allocated on the secondary stack and no further processing is
6923 -- required, unless the returned object is an interface.
6925 elsif CW_Or_Needs_Finalization (Utyp)
6926 and then (Is_Interface (R_Type)
6927 or else not (Exp_Is_Function_Call
6928 and then Needs_Finalization (Exp_Typ)))
6931 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6933 Alloc_Node : Node_Id;
6937 Mutate_Ekind (Acc_Typ, E_Access_Type);
6938 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
6940 -- This is an allocator for the secondary stack, and it's fine
6941 -- to have Comes_From_Source set False on it, as gigi knows not
6942 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6945 Make_Allocator (Loc,
6947 Make_Qualified_Expression (Loc,
6948 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
6949 Expression => Relocate_Node (Exp)));
6951 -- We do not want discriminant checks on the declaration,
6952 -- given that it gets its value from the allocator.
6954 Set_No_Initialization (Alloc_Node);
6956 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6958 Insert_Actions (Exp, New_List (
6959 Make_Full_Type_Declaration (Loc,
6960 Defining_Identifier => Acc_Typ,
6962 Make_Access_To_Object_Definition (Loc,
6963 Subtype_Indication => Subtype_Ind)),
6965 Make_Object_Declaration (Loc,
6966 Defining_Identifier => Temp,
6967 Constant_Present => True,
6968 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
6969 Expression => Alloc_Node)));
6972 Make_Explicit_Dereference (Loc,
6973 Prefix => New_Occurrence_Of (Temp, Loc)));
6975 -- Ada 2005 (AI-251): If the type of the returned object is
6976 -- an interface then add an implicit type conversion to force
6977 -- displacement of the "this" pointer.
6979 if Is_Interface (R_Type) then
6980 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6983 Analyze_And_Resolve (Exp, R_Type);
6986 -- Otherwise use the gigi mechanism to allocate result on the
6990 Check_Restriction (No_Secondary_Stack, N);
6991 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6992 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6996 -- Implement the rules of 6.5(8-10), which require a tag check in
6997 -- the case of a limited tagged return type, and tag reassignment for
6998 -- nonlimited tagged results. These actions are needed when the return
6999 -- type is a specific tagged type and the result expression is a
7000 -- conversion or a formal parameter, because in that case the tag of
7001 -- the expression might differ from the tag of the specific result type.
7003 -- We must also verify an underlying type exists for the return type in
7004 -- case it is incomplete - in which case is not necessary to generate a
7005 -- check anyway since an incomplete limited tagged return type would
7006 -- qualify as a premature usage.
7009 and then Is_Tagged_Type (Utyp)
7010 and then not Is_Class_Wide_Type (Utyp)
7011 and then (Nkind (Exp) in
7012 N_Type_Conversion | N_Unchecked_Type_Conversion
7013 or else (Nkind (Exp) = N_Explicit_Dereference
7014 and then Nkind (Prefix (Exp)) in
7016 N_Unchecked_Type_Conversion)
7017 or else (Is_Entity_Name (Exp)
7018 and then Is_Formal (Entity (Exp))))
7020 -- When the return type is limited, perform a check that the tag of
7021 -- the result is the same as the tag of the return type.
7023 if Is_Limited_Type (R_Type) then
7025 Make_Raise_Constraint_Error (Loc,
7029 Make_Selected_Component (Loc,
7030 Prefix => Duplicate_Subexpr (Exp),
7031 Selector_Name => Make_Identifier (Loc, Name_uTag)),
7033 Make_Attribute_Reference (Loc,
7035 New_Occurrence_Of (Base_Type (Utyp), Loc),
7036 Attribute_Name => Name_Tag)),
7037 Reason => CE_Tag_Check_Failed));
7039 -- If the result type is a specific nonlimited tagged type, then we
7040 -- have to ensure that the tag of the result is that of the result
7041 -- type. This is handled by making a copy of the expression in
7042 -- the case where it might have a different tag, namely when the
7043 -- expression is a conversion or a formal parameter. We create a new
7044 -- object of the result type and initialize it from the expression,
7045 -- which will implicitly force the tag to be set appropriately.
7049 ExpR : constant Node_Id := Relocate_Node (Exp);
7050 Result_Id : constant Entity_Id :=
7051 Make_Temporary (Loc, 'R', ExpR);
7052 Result_Exp : constant Node_Id :=
7053 New_Occurrence_Of (Result_Id, Loc);
7054 Result_Obj : constant Node_Id :=
7055 Make_Object_Declaration (Loc,
7056 Defining_Identifier => Result_Id,
7057 Object_Definition =>
7058 New_Occurrence_Of (R_Type, Loc),
7059 Constant_Present => True,
7060 Expression => ExpR);
7063 Set_Assignment_OK (Result_Obj);
7064 Insert_Action (Exp, Result_Obj);
7066 Rewrite (Exp, Result_Exp);
7067 Analyze_And_Resolve (Exp, R_Type);
7071 -- Ada 2005 (AI95-344): If the result type is class-wide, then insert
7072 -- a check that the level of the return expression's underlying type
7073 -- is not deeper than the level of the master enclosing the function.
7075 -- AI12-043: The check is made immediately after the return object is
7076 -- created. This means that we do not apply it to the simple return
7077 -- generated by the expansion of an extended return statement.
7079 -- No runtime check needed in interface thunks since it is performed
7080 -- by the target primitive associated with the thunk.
7082 elsif Is_Class_Wide_Type (R_Type)
7083 and then not Comes_From_Extended_Return_Statement (N)
7084 and then not Is_Thunk (Scope_Id)
7086 Apply_CW_Accessibility_Check (Exp, Scope_Id);
7088 -- Ada 2012 (AI05-0073): If the result subtype of the function is
7089 -- defined by an access_definition designating a specific tagged
7090 -- type T, a check is made that the result value is null or the tag
7091 -- of the object designated by the result value identifies T.
7093 -- The return expression is referenced twice in the code below, so it
7094 -- must be made free of side effects. Given that different compilers
7095 -- may evaluate these parameters in different order, both occurrences
7098 elsif Ekind (R_Type) = E_Anonymous_Access_Type
7099 and then Is_Tagged_Type (Designated_Type (R_Type))
7100 and then not Is_Class_Wide_Type (Designated_Type (R_Type))
7101 and then Nkind (Original_Node (Exp)) /= N_Null
7102 and then not Tag_Checks_Suppressed (Designated_Type (R_Type))
7105 -- [Constraint_Error
7107 -- and then Exp.all not in Designated_Type]
7110 Make_Raise_Constraint_Error (Loc,
7115 Left_Opnd => Duplicate_Subexpr (Exp),
7116 Right_Opnd => Make_Null (Loc)),
7121 Make_Explicit_Dereference (Loc,
7122 Prefix => Duplicate_Subexpr (Exp)),
7124 New_Occurrence_Of (Designated_Type (R_Type), Loc))),
7126 Reason => CE_Tag_Check_Failed),
7127 Suppress => All_Checks);
7130 -- If the result is of an unconstrained array subtype with fixed lower
7131 -- bound, then sliding to that bound may be needed.
7133 if Is_Fixed_Lower_Bound_Array_Subtype (R_Type) then
7134 Expand_Sliding_Conversion (Exp, R_Type);
7137 -- If we are returning a nonscalar object that is possibly unaligned,
7138 -- then copy the value into a temporary first. This copy may need to
7139 -- expand to a loop of component operations.
7141 if Is_Possibly_Unaligned_Slice (Exp)
7142 or else (Is_Possibly_Unaligned_Object (Exp)
7143 and then not Represented_As_Scalar (Etype (Exp)))
7146 ExpR : constant Node_Id := Relocate_Node (Exp);
7147 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
7150 Make_Object_Declaration (Loc,
7151 Defining_Identifier => Tnn,
7152 Constant_Present => True,
7153 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7154 Expression => ExpR),
7155 Suppress => All_Checks);
7156 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7160 -- Ada 2005 (AI-251): If this return statement corresponds with an
7161 -- simple return statement associated with an extended return statement
7162 -- and the type of the returned object is an interface then generate an
7163 -- implicit conversion to force displacement of the "this" pointer.
7165 if Ada_Version >= Ada_2005
7166 and then Comes_From_Extended_Return_Statement (N)
7167 and then Nkind (Expression (N)) = N_Identifier
7168 and then Is_Interface (Utyp)
7169 and then Utyp /= Underlying_Type (Exp_Typ)
7171 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
7172 Analyze_And_Resolve (Exp);
7175 -- Ada 2022 (AI12-0279)
7177 if Has_Yield_Aspect (Scope_Id)
7178 and then RTE_Available (RE_Yield)
7181 Make_Procedure_Call_Statement (Loc,
7182 New_Occurrence_Of (RTE (RE_Yield), Loc)));
7184 end Expand_Simple_Function_Return;
7186 -----------------------
7187 -- Freeze_Subprogram --
7188 -----------------------
7190 procedure Freeze_Subprogram (N : Node_Id) is
7191 Loc : constant Source_Ptr := Sloc (N);
7192 Subp : constant Entity_Id := Entity (N);
7195 -- We suppress the initialization of the dispatch table entry when
7196 -- not Tagged_Type_Expansion because the dispatching mechanism is
7197 -- handled internally by the target.
7199 if Is_Dispatching_Operation (Subp)
7200 and then not Is_Abstract_Subprogram (Subp)
7201 and then Present (DTC_Entity (Subp))
7202 and then Present (Scope (DTC_Entity (Subp)))
7203 and then Tagged_Type_Expansion
7204 and then not Restriction_Active (No_Dispatching_Calls)
7205 and then RTE_Available (RE_Tag)
7208 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
7213 -- Handle private overridden primitives
7215 if not Is_CPP_Class (Typ) then
7216 Check_Overriding_Operation (Subp);
7219 -- We assume that imported CPP primitives correspond with objects
7220 -- whose constructor is in the CPP side; therefore we don't need
7221 -- to generate code to register them in the dispatch table.
7223 if Is_CPP_Class (Typ) then
7226 -- Handle CPP primitives found in derivations of CPP_Class types.
7227 -- These primitives must have been inherited from some parent, and
7228 -- there is no need to register them in the dispatch table because
7229 -- Build_Inherit_Prims takes care of initializing these slots.
7231 elsif Is_Imported (Subp)
7232 and then Convention (Subp) in Convention_C_Family
7236 -- Generate code to register the primitive in non statically
7237 -- allocated dispatch tables
7239 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
7241 -- When a primitive is frozen, enter its name in its dispatch
7244 if not Is_Interface (Typ)
7245 or else Present (Interface_Alias (Subp))
7247 if Is_Predefined_Dispatching_Operation (Subp) then
7248 L := Register_Predefined_Primitive (Loc, Subp);
7253 Append_List_To (L, Register_Primitive (Loc, Subp));
7255 if Is_Empty_List (L) then
7258 elsif No (Actions (N)) then
7262 Append_List (L, Actions (N));
7269 -- Mark functions that return by reference. Note that it cannot be part
7270 -- of the normal semantic analysis of the spec since the underlying
7271 -- returned type may not be known yet (for private types).
7273 Compute_Returns_By_Ref (Subp);
7274 end Freeze_Subprogram;
7276 --------------------------
7277 -- Has_BIP_Extra_Formal --
7278 --------------------------
7280 function Has_BIP_Extra_Formal
7282 Kind : BIP_Formal_Kind;
7283 Must_Be_Frozen : Boolean := True) return Boolean
7285 Extra_Formal : Entity_Id := Extra_Formals (E);
7288 -- We can only rely on the availability of the extra formals in frozen
7289 -- entities or in subprogram types of dispatching calls (since their
7290 -- extra formals are added when the target subprogram is frozen; see
7291 -- Expand_Dispatching_Call).
7293 pragma Assert ((Is_Frozen (E) or else not Must_Be_Frozen)
7294 or else (Ekind (E) = E_Subprogram_Type
7295 and then Is_Dispatch_Table_Entity (E))
7296 or else (Is_Dispatching_Operation (E)
7297 and then Is_Frozen (Find_Dispatching_Type (E))));
7299 while Present (Extra_Formal) loop
7300 if Is_Build_In_Place_Entity (Extra_Formal)
7301 and then BIP_Suffix_Kind (Extra_Formal) = Kind
7306 Next_Formal_With_Extras (Extra_Formal);
7310 end Has_BIP_Extra_Formal;
7312 ------------------------------
7313 -- Insert_Post_Call_Actions --
7314 ------------------------------
7316 procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id) is
7317 Context : constant Node_Id := Parent (N);
7320 if Is_Empty_List (Post_Call) then
7324 -- Cases where the call is not a member of a statement list. This also
7325 -- includes the cases where the call is an actual in another function
7326 -- call, or is an index, or is an operand of an if-expression, i.e. is
7327 -- in an expression context.
7329 if not Is_List_Member (N)
7330 or else Nkind (Context) in N_Function_Call
7332 | N_Indexed_Component
7334 -- In Ada 2012 the call may be a function call in an expression
7335 -- (since OUT and IN OUT parameters are now allowed for such calls).
7336 -- The write-back of (in)-out parameters is handled by the back-end,
7337 -- but the constraint checks generated when subtypes of formal and
7338 -- actual don't match must be inserted in the form of assignments.
7339 -- Also do this in the case of explicit dereferences, which can occur
7340 -- due to rewritings of function calls with controlled results.
7342 if Nkind (N) = N_Function_Call
7343 or else Nkind (Original_Node (N)) = N_Function_Call
7344 or else Nkind (N) = N_Explicit_Dereference
7346 pragma Assert (Ada_Version >= Ada_2012);
7347 -- Functions with '[in] out' parameters are only allowed in Ada
7350 -- We used to handle this by climbing up parents to a
7351 -- non-statement/declaration and then simply making a call to
7352 -- Insert_Actions_After (P, Post_Call), but that doesn't work
7353 -- for Ada 2012. If we are in the middle of an expression, e.g.
7354 -- the condition of an IF, this call would insert after the IF
7355 -- statement, which is much too late to be doing the write back.
7358 -- if Clobber (X) then
7359 -- Put_Line (X'Img);
7364 -- Now assume Clobber changes X, if we put the write back after
7365 -- the IF, the Put_Line gets the wrong value and the goto causes
7366 -- the write back to be skipped completely.
7368 -- To deal with this, we replace the call by
7371 -- Tnnn : constant function-result-type := function-call;
7372 -- Post_Call actions
7377 -- However, that doesn't work if function-result-type requires
7378 -- finalization (because function-call's result never gets
7379 -- finalized). So in that case, we instead replace the call by
7382 -- type Ref is access all function-result-type;
7383 -- Ptr : constant Ref := function-call'Reference;
7384 -- Tnnn : constant function-result-type := Ptr.all;
7385 -- Finalize (Ptr.all);
7386 -- Post_Call actions
7393 Loc : constant Source_Ptr := Sloc (N);
7394 Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T');
7395 FRTyp : constant Entity_Id := Etype (N);
7396 Name : constant Node_Id := Relocate_Node (N);
7399 if Needs_Finalization (FRTyp) then
7401 Ptr_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
7403 Ptr_Typ_Decl : constant Node_Id :=
7404 Make_Full_Type_Declaration (Loc,
7405 Defining_Identifier => Ptr_Typ,
7407 Make_Access_To_Object_Definition (Loc,
7408 All_Present => True,
7409 Subtype_Indication =>
7410 New_Occurrence_Of (FRTyp, Loc)));
7412 Ptr_Obj : constant Entity_Id :=
7413 Make_Temporary (Loc, 'P');
7415 Ptr_Obj_Decl : constant Node_Id :=
7416 Make_Object_Declaration (Loc,
7417 Defining_Identifier => Ptr_Obj,
7418 Object_Definition =>
7419 New_Occurrence_Of (Ptr_Typ, Loc),
7420 Constant_Present => True,
7422 Make_Attribute_Reference (Loc,
7424 Attribute_Name => Name_Unrestricted_Access));
7426 function Ptr_Dereference return Node_Id is
7427 (Make_Explicit_Dereference (Loc,
7428 Prefix => New_Occurrence_Of (Ptr_Obj, Loc)));
7430 Tnn_Decl : constant Node_Id :=
7431 Make_Object_Declaration (Loc,
7432 Defining_Identifier => Tnnn,
7433 Object_Definition => New_Occurrence_Of (FRTyp, Loc),
7434 Constant_Present => True,
7435 Expression => Ptr_Dereference);
7437 Finalize_Call : constant Node_Id :=
7439 (Obj_Ref => Ptr_Dereference, Typ => FRTyp);
7441 -- Prepend in reverse order
7443 Prepend_To (Post_Call, Finalize_Call);
7444 Prepend_To (Post_Call, Tnn_Decl);
7445 Prepend_To (Post_Call, Ptr_Obj_Decl);
7446 Prepend_To (Post_Call, Ptr_Typ_Decl);
7449 Prepend_To (Post_Call,
7450 Make_Object_Declaration (Loc,
7451 Defining_Identifier => Tnnn,
7452 Object_Definition => New_Occurrence_Of (FRTyp, Loc),
7453 Constant_Present => True,
7454 Expression => Name));
7458 Make_Expression_With_Actions (Loc,
7459 Actions => Post_Call,
7460 Expression => New_Occurrence_Of (Tnnn, Loc)));
7462 -- We don't want to just blindly call Analyze_And_Resolve
7463 -- because that would cause unwanted recursion on the call.
7464 -- So for a moment set the call as analyzed to prevent that
7465 -- recursion, and get the rest analyzed properly, then reset
7466 -- the analyzed flag, so our caller can continue.
7468 Set_Analyzed (Name, True);
7469 Analyze_And_Resolve (N, FRTyp);
7470 Set_Analyzed (Name, False);
7473 -- If not the special Ada 2012 case of a function call, then we must
7474 -- have the triggering statement of a triggering alternative or an
7475 -- entry call alternative, and we can add the post call stuff to the
7476 -- corresponding statement list.
7479 pragma Assert (Nkind (Context) in N_Entry_Call_Alternative
7480 | N_Triggering_Alternative);
7482 if Is_Non_Empty_List (Statements (Context)) then
7483 Insert_List_Before_And_Analyze
7484 (First (Statements (Context)), Post_Call);
7486 Set_Statements (Context, Post_Call);
7490 -- A procedure call is always part of a declarative or statement list,
7491 -- however a function call may appear nested within a construct. Most
7492 -- cases of function call nesting are handled in the special case above.
7493 -- The only exception is when the function call acts as an actual in a
7494 -- procedure call. In this case the function call is in a list, but the
7495 -- post-call actions must be inserted after the procedure call.
7496 -- What if the function call is an aggregate component ???
7498 elsif Nkind (Context) = N_Procedure_Call_Statement then
7499 Insert_Actions_After (Context, Post_Call);
7501 -- Otherwise, normal case where N is in a statement sequence, just put
7502 -- the post-call stuff after the call statement.
7505 Insert_Actions_After (N, Post_Call);
7507 end Insert_Post_Call_Actions;
7509 ---------------------------------------
7510 -- Install_Class_Preconditions_Check --
7511 ---------------------------------------
7513 procedure Install_Class_Preconditions_Check (Call_Node : Node_Id) is
7514 Loc : constant Source_Ptr := Sloc (Call_Node);
7516 function Build_Dynamic_Check_Helper_Call return Node_Id;
7517 -- Build call to the helper runtime function of the nearest ancestor
7518 -- of the target subprogram that dynamically evaluates the merged
7519 -- or-else preconditions.
7521 function Build_Error_Message (Subp_Id : Entity_Id) return Node_Id;
7522 -- Build message associated with the class-wide precondition of Subp_Id
7523 -- indicating the call that caused it.
7525 function Build_Static_Check_Helper_Call return Node_Id;
7526 -- Build call to the helper runtime function of the nearest ancestor
7527 -- of the target subprogram that dynamically evaluates the merged
7528 -- or-else preconditions.
7530 function Class_Preconditions_Subprogram
7531 (Spec_Id : Entity_Id;
7532 Dynamic : Boolean) return Node_Id;
7533 -- Return the nearest ancestor of Spec_Id defining a helper function
7534 -- that evaluates a combined or-else expression containing all the
7535 -- inherited class-wide preconditions; Dynamic enables searching for
7536 -- the helper that dynamically evaluates preconditions using dispatching
7537 -- calls; if False it searches for the helper that statically evaluates
7538 -- preconditions; return Empty when not available (which means that no
7539 -- preconditions check is required).
7541 -------------------------------------
7542 -- Build_Dynamic_Check_Helper_Call --
7543 -------------------------------------
7545 function Build_Dynamic_Check_Helper_Call return Node_Id is
7546 Spec_Id : constant Entity_Id := Entity (Name (Call_Node));
7547 CW_Subp : constant Entity_Id :=
7548 Class_Preconditions_Subprogram (Spec_Id,
7550 Helper_Id : constant Entity_Id :=
7551 Dynamic_Call_Helper (CW_Subp);
7552 Actuals : constant List_Id := New_List;
7553 A : Node_Id := First_Actual (Call_Node);
7554 F : Entity_Id := First_Formal (Helper_Id);
7557 while Present (A) loop
7559 -- Ensure that the evaluation of the actuals will not produce
7562 Remove_Side_Effects (A);
7564 Append_To (Actuals, New_Copy_Tree (A));
7570 Make_Function_Call (Loc,
7571 Name => New_Occurrence_Of (Helper_Id, Loc),
7572 Parameter_Associations => Actuals);
7573 end Build_Dynamic_Check_Helper_Call;
7575 -------------------------
7576 -- Build_Error_Message --
7577 -------------------------
7579 function Build_Error_Message (Subp_Id : Entity_Id) return Node_Id is
7581 procedure Append_Message
7583 Is_First : in out Boolean);
7584 -- Build the fragment of the message associated with subprogram Id;
7585 -- Is_First facilitates identifying continuation messages.
7587 --------------------
7588 -- Append_Message --
7589 --------------------
7591 procedure Append_Message
7593 Is_First : in out Boolean)
7595 Prag : constant Node_Id := Get_Class_Wide_Pragma (Id,
7596 Pragma_Precondition);
7601 if No (Prag) or else Is_Ignored (Prag) then
7605 Msg := Expression (Last (Pragma_Argument_Associations (Prag)));
7606 Str_Id := Strval (Msg);
7611 Append (Global_Name_Buffer, Strval (Msg));
7614 and then Name_Buffer (1 .. 19) = "failed precondition"
7616 Insert_Str_In_Name_Buffer ("inherited ", 8);
7621 Str : constant String := To_String (Str_Id);
7625 Append (Global_Name_Buffer, ASCII.LF);
7626 Append (Global_Name_Buffer, " or ");
7628 From_Idx := Name_Len;
7629 Append (Global_Name_Buffer, Str_Id);
7631 if Str (1 .. 19) = "failed precondition" then
7632 Insert_Str_In_Name_Buffer ("inherited ", From_Idx + 8);
7640 Str_Loc : constant String := Build_Location_String (Loc);
7641 Subps : constant Subprogram_List :=
7642 Inherited_Subprograms (Subp_Id);
7643 Is_First : Boolean := True;
7645 -- Start of processing for Build_Error_Message
7649 Append_Message (Subp_Id, Is_First);
7651 for Index in Subps'Range loop
7652 Append_Message (Subps (Index), Is_First);
7655 if Present (Controlling_Argument (Call_Node)) then
7656 Append (Global_Name_Buffer, " in dispatching call at ");
7658 Append (Global_Name_Buffer, " in call at ");
7661 Append (Global_Name_Buffer, Str_Loc);
7663 return Make_String_Literal (Loc, Name_Buffer (1 .. Name_Len));
7664 end Build_Error_Message;
7666 ------------------------------------
7667 -- Build_Static_Check_Helper_Call --
7668 ------------------------------------
7670 function Build_Static_Check_Helper_Call return Node_Id is
7671 Actuals : constant List_Id := New_List;
7673 Helper_Id : Entity_Id;
7675 CW_Subp : Entity_Id;
7676 Spec_Id : constant Entity_Id := Entity (Name (Call_Node));
7679 -- The target is the wrapper built to support inheriting body but
7680 -- overriding pre/postconditions (AI12-0195).
7682 if Is_Dispatch_Table_Wrapper (Spec_Id) then
7688 CW_Subp := Class_Preconditions_Subprogram (Spec_Id,
7692 Helper_Id := Static_Call_Helper (CW_Subp);
7694 F := First_Formal (Helper_Id);
7695 A := First_Actual (Call_Node);
7696 while Present (A) loop
7698 -- Ensure that the evaluation of the actuals will not produce
7701 Remove_Side_Effects (A);
7703 -- Ensure matching types to avoid reporting spurious errors since
7704 -- the called helper may have been built for a parent type.
7706 if Etype (F) /= Etype (A) then
7708 Unchecked_Convert_To (Etype (F), New_Copy_Tree (A)));
7710 Append_To (Actuals, New_Copy_Tree (A));
7718 Make_Function_Call (Loc,
7719 Name => New_Occurrence_Of (Helper_Id, Loc),
7720 Parameter_Associations => Actuals);
7721 end Build_Static_Check_Helper_Call;
7723 ------------------------------------
7724 -- Class_Preconditions_Subprogram --
7725 ------------------------------------
7727 function Class_Preconditions_Subprogram
7728 (Spec_Id : Entity_Id;
7729 Dynamic : Boolean) return Node_Id
7731 Subp_Id : constant Entity_Id := Ultimate_Alias (Spec_Id);
7734 -- Prevent cascaded errors
7736 if not Is_Dispatching_Operation (Subp_Id) then
7739 -- No need to search if this subprogram has the helper we are
7743 if Present (Dynamic_Call_Helper (Subp_Id)) then
7747 if Present (Static_Call_Helper (Subp_Id)) then
7752 -- Process inherited subprograms looking for class-wide
7756 Subps : constant Subprogram_List :=
7757 Inherited_Subprograms (Subp_Id);
7758 Subp_Id : Entity_Id;
7761 for Index in Subps'Range loop
7762 Subp_Id := Subps (Index);
7764 if Present (Alias (Subp_Id)) then
7765 Subp_Id := Ultimate_Alias (Subp_Id);
7768 -- Wrappers of class-wide pre/postconditions reference the
7769 -- parent primitive that has the inherited contract.
7771 if Is_Wrapper (Subp_Id)
7772 and then Present (LSP_Subprogram (Subp_Id))
7774 Subp_Id := LSP_Subprogram (Subp_Id);
7778 if Present (Dynamic_Call_Helper (Subp_Id)) then
7782 if Present (Static_Call_Helper (Subp_Id)) then
7790 end Class_Preconditions_Subprogram;
7794 Dynamic_Check : constant Boolean :=
7795 Present (Controlling_Argument (Call_Node));
7796 Class_Subp : Entity_Id;
7800 -- Start of processing for Install_Class_Preconditions_Check
7803 -- Do not expand the check if we are compiling under restriction
7804 -- No_Dispatching_Calls; the semantic analyzer has previously
7805 -- notified the violation of this restriction.
7808 and then Restriction_Active (No_Dispatching_Calls)
7812 -- Class-wide precondition check not needed in interface thunks since
7813 -- they are installed in the dispatching call that caused invoking the
7816 elsif Is_Thunk (Current_Scope) then
7820 Subp := Entity (Name (Call_Node));
7822 -- No check needed for this subprogram call if no class-wide
7823 -- preconditions apply (or if the unique available preconditions
7824 -- are ignored preconditions).
7826 Class_Subp := Class_Preconditions_Subprogram (Subp, Dynamic_Check);
7829 or else No (Class_Preconditions (Class_Subp))
7834 -- Build and install the check
7836 if Dynamic_Check then
7837 Cond := Build_Dynamic_Check_Helper_Call;
7839 Cond := Build_Static_Check_Helper_Call;
7842 if Exception_Locations_Suppressed then
7843 Insert_Action (Call_Node,
7844 Make_If_Statement (Loc,
7845 Condition => Make_Op_Not (Loc, Cond),
7846 Then_Statements => New_List (
7847 Make_Raise_Statement (Loc,
7850 (RTE (RE_Assert_Failure), Loc)))));
7852 -- Failed check with message indicating the failed precondition and the
7853 -- call that caused it.
7856 Insert_Action (Call_Node,
7857 Make_If_Statement (Loc,
7858 Condition => Make_Op_Not (Loc, Cond),
7859 Then_Statements => New_List (
7860 Make_Procedure_Call_Statement (Loc,
7863 (RTE (RE_Raise_Assert_Failure), Loc),
7864 Parameter_Associations =>
7865 New_List (Build_Error_Message (Subp))))));
7867 end Install_Class_Preconditions_Check;
7869 ------------------------------
7870 -- Is_Build_In_Place_Entity --
7871 ------------------------------
7873 function Is_Build_In_Place_Entity (E : Entity_Id) return Boolean is
7874 Nam : constant String := Get_Name_String (Chars (E));
7876 function Has_Suffix (Suffix : String) return Boolean;
7877 -- Return True if Nam has suffix Suffix
7879 function Has_Suffix (Suffix : String) return Boolean is
7880 Len : constant Natural := Suffix'Length;
7882 return Nam'Length > Len
7883 and then Nam (Nam'Last - Len + 1 .. Nam'Last) = Suffix;
7886 -- Start of processing for Is_Build_In_Place_Entity
7889 return Has_Suffix (BIP_Alloc_Suffix)
7890 or else Has_Suffix (BIP_Storage_Pool_Suffix)
7891 or else Has_Suffix (BIP_Finalization_Master_Suffix)
7892 or else Has_Suffix (BIP_Task_Master_Suffix)
7893 or else Has_Suffix (BIP_Activation_Chain_Suffix)
7894 or else Has_Suffix (BIP_Object_Access_Suffix);
7895 end Is_Build_In_Place_Entity;
7897 --------------------------------
7898 -- Is_Build_In_Place_Function --
7899 --------------------------------
7901 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
7902 Kind : constant Entity_Kind := Ekind (E);
7903 Typ : constant Entity_Id := Etype (E);
7906 -- This function is called from Expand_Subtype_From_Expr during
7907 -- semantic analysis, even when expansion is off. In those cases
7908 -- the build_in_place expansion will not take place.
7910 if not Expander_Active then
7914 -- We never use build-in-place if the convention is other than Ada,
7915 -- but note that it is OK for a build-in-place function to return a
7916 -- type with a foreign convention because the machinery ensures there
7919 return (Kind in E_Function | E_Generic_Function
7921 (Kind = E_Subprogram_Type and then Typ /= Standard_Void_Type))
7922 and then Is_Build_In_Place_Result_Type (Typ)
7923 and then not Has_Foreign_Convention (E);
7924 end Is_Build_In_Place_Function;
7926 -------------------------------------
7927 -- Is_Build_In_Place_Function_Call --
7928 -------------------------------------
7930 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
7931 Exp_Node : constant Node_Id := Unqual_Conv (N);
7932 Function_Id : Entity_Id;
7935 -- Return False if the expander is currently inactive, since awareness
7936 -- of build-in-place treatment is only relevant during expansion. Note
7937 -- that Is_Build_In_Place_Function, which is called as part of this
7938 -- function, is also conditioned this way, but we need to check here as
7939 -- well to avoid blowing up on processing protected calls when expansion
7940 -- is disabled (such as with -gnatc) since those would trip over the
7941 -- raise of Program_Error below.
7943 -- In SPARK mode, build-in-place calls are not expanded, so that we
7944 -- may end up with a call that is neither resolved to an entity, nor
7945 -- an indirect call.
7947 if not Expander_Active or else Nkind (Exp_Node) /= N_Function_Call then
7951 if Is_Entity_Name (Name (Exp_Node)) then
7952 Function_Id := Entity (Name (Exp_Node));
7954 -- In the case of an explicitly dereferenced call, use the subprogram
7955 -- type generated for the dereference.
7957 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
7958 Function_Id := Etype (Name (Exp_Node));
7960 -- This may be a call to a protected function.
7962 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
7963 -- The selector in question might not have been analyzed due to a
7964 -- previous error, so analyze it here to output the appropriate
7965 -- error message instead of crashing when attempting to fetch its
7968 if not Analyzed (Selector_Name (Name (Exp_Node))) then
7969 Analyze (Selector_Name (Name (Exp_Node)));
7972 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
7975 raise Program_Error;
7978 if Is_Build_In_Place_Function (Function_Id) then
7981 -- True also if the function has BIP Formals
7985 Kind : constant Entity_Kind := Ekind (Function_Id);
7988 if (Kind in E_Function | E_Generic_Function
7989 or else (Kind = E_Subprogram_Type
7991 Etype (Function_Id) /= Standard_Void_Type))
7992 and then Has_BIP_Formals (Function_Id)
7994 -- So we can stop here in the debugger
8001 end Is_Build_In_Place_Function_Call;
8003 -----------------------------------
8004 -- Is_Build_In_Place_Result_Type --
8005 -----------------------------------
8007 function Is_Build_In_Place_Result_Type (Typ : Entity_Id) return Boolean is
8009 if not Expander_Active then
8013 -- In Ada 2005 all functions with an inherently limited return type
8014 -- must be handled using a build-in-place profile, including the case
8015 -- of a function with a limited interface result, where the function
8016 -- may return objects of nonlimited descendants.
8018 return Is_Limited_View (Typ)
8019 and then Ada_Version >= Ada_2005
8020 and then not Debug_Flag_Dot_L;
8021 end Is_Build_In_Place_Result_Type;
8023 -------------------------------------
8024 -- Is_Build_In_Place_Return_Object --
8025 -------------------------------------
8027 function Is_Build_In_Place_Return_Object (E : Entity_Id) return Boolean is
8029 return Is_Return_Object (E)
8030 and then Is_Build_In_Place_Function (Return_Applies_To (Scope (E)));
8031 end Is_Build_In_Place_Return_Object;
8033 -----------------------------------
8034 -- Is_By_Reference_Return_Object --
8035 -----------------------------------
8037 function Is_By_Reference_Return_Object (E : Entity_Id) return Boolean is
8039 return Is_Return_Object (E)
8040 and then Is_By_Reference_Type (Etype (Return_Applies_To (Scope (E))));
8041 end Is_By_Reference_Return_Object;
8043 -----------------------
8044 -- Is_Null_Procedure --
8045 -----------------------
8047 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
8048 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
8051 if Ekind (Subp) /= E_Procedure then
8054 -- Check if this is a declared null procedure
8056 elsif Nkind (Decl) = N_Subprogram_Declaration then
8057 if not Null_Present (Specification (Decl)) then
8060 elsif No (Body_To_Inline (Decl)) then
8063 -- Check if the body contains only a null statement, followed by
8064 -- the return statement added during expansion.
8068 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
8074 if Nkind (Orig_Bod) /= N_Subprogram_Body then
8077 -- We must skip SCIL nodes because they are currently
8078 -- implemented as special N_Null_Statement nodes.
8082 (Statements (Handled_Statement_Sequence (Orig_Bod)));
8083 Stat2 := Next_Non_SCIL_Node (Stat);
8086 Is_Empty_List (Declarations (Orig_Bod))
8087 and then Nkind (Stat) = N_Null_Statement
8091 (Nkind (Stat2) = N_Simple_Return_Statement
8092 and then No (Next (Stat2))));
8100 end Is_Null_Procedure;
8102 --------------------------------------
8103 -- Is_Secondary_Stack_Return_Object --
8104 --------------------------------------
8106 function Is_Secondary_Stack_Return_Object (E : Entity_Id) return Boolean is
8108 return Is_Return_Object (E)
8109 and then Needs_Secondary_Stack (Etype (Return_Applies_To (Scope (E))));
8110 end Is_Secondary_Stack_Return_Object;
8112 ------------------------------
8113 -- Is_Special_Return_Object --
8114 ------------------------------
8116 function Is_Special_Return_Object (E : Entity_Id) return Boolean is
8118 return Is_Build_In_Place_Return_Object (E)
8119 or else Is_Secondary_Stack_Return_Object (E)
8120 or else (Back_End_Return_Slot
8121 and then Is_By_Reference_Return_Object (E));
8122 end Is_Special_Return_Object;
8124 -------------------------------------------
8125 -- Make_Build_In_Place_Call_In_Allocator --
8126 -------------------------------------------
8128 procedure Make_Build_In_Place_Call_In_Allocator
8129 (Allocator : Node_Id;
8130 Function_Call : Node_Id)
8132 Acc_Type : constant Entity_Id := Etype (Allocator);
8133 Loc : constant Source_Ptr := Sloc (Function_Call);
8134 Func_Call : Node_Id := Function_Call;
8135 Ref_Func_Call : Node_Id;
8136 Function_Id : Entity_Id;
8137 Result_Subt : Entity_Id;
8138 New_Allocator : Node_Id;
8139 Return_Obj_Access : Entity_Id; -- temp for function result
8140 Temp_Init : Node_Id; -- initial value of Return_Obj_Access
8141 Alloc_Form : BIP_Allocation_Form;
8142 Pool : Node_Id; -- nonnull if Alloc_Form = User_Storage_Pool
8143 Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case
8144 Chain : Entity_Id; -- activation chain, in case of tasks
8147 -- Step past qualification or unchecked conversion (the latter can occur
8148 -- in cases of calls to 'Input).
8150 if Nkind (Func_Call) in N_Qualified_Expression
8152 | N_Unchecked_Type_Conversion
8154 Func_Call := Expression (Func_Call);
8157 -- Mark the call as processed as a build-in-place call
8159 pragma Assert (not Is_Expanded_Build_In_Place_Call (Func_Call));
8160 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8162 if Is_Entity_Name (Name (Func_Call)) then
8163 Function_Id := Entity (Name (Func_Call));
8165 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8166 Function_Id := Etype (Name (Func_Call));
8169 raise Program_Error;
8172 Warn_BIP (Func_Call);
8174 Result_Subt := Available_View (Etype (Function_Id));
8176 -- Create a temp for the function result. In the caller-allocates case,
8177 -- this will be initialized to the result of a new uninitialized
8178 -- allocator. Note: we do not use Allocator as the Related_Node of
8179 -- Return_Obj_Access in call to Make_Temporary below as this would
8180 -- create a sort of infinite "recursion".
8182 Return_Obj_Access := Make_Temporary (Loc, 'R');
8183 Set_Etype (Return_Obj_Access, Acc_Type);
8184 Set_Can_Never_Be_Null (Acc_Type, False);
8185 -- It gets initialized to null, so we can't have that
8187 -- When the result subtype is constrained, the return object is created
8188 -- on the caller side, and access to it is passed to the function. This
8189 -- optimization is disabled when the result subtype needs finalization
8190 -- actions because the caller side allocation may result in undesirable
8191 -- finalization. Consider the following example:
8193 -- function Make_Lim_Ctrl return Lim_Ctrl is
8195 -- return Result : Lim_Ctrl := raise Program_Error do
8198 -- end Make_Lim_Ctrl;
8200 -- Obj : Lim_Ctrl_Ptr := new Lim_Ctrl'(Make_Lim_Ctrl);
8202 -- Even though the size of limited controlled type Lim_Ctrl is known,
8203 -- allocating Obj at the caller side will chain Obj on Lim_Ctrl_Ptr's
8204 -- finalization master. The subsequent call to Make_Lim_Ctrl will fail
8205 -- during the initialization actions for Result, which implies that
8206 -- Result (and Obj by extension) should not be finalized. However Obj
8207 -- will be finalized when access type Lim_Ctrl_Ptr goes out of scope
8208 -- since it is already attached on the related finalization master.
8210 -- Here and in related routines, we must examine the full view of the
8211 -- type, because the view at the point of call may differ from the
8212 -- one in the function body, and the expansion mechanism depends on
8213 -- the characteristics of the full view.
8215 if Needs_BIP_Alloc_Form (Function_Id) then
8218 -- Case of a user-defined storage pool. Pass an allocation parameter
8219 -- indicating that the function should allocate its result in the
8220 -- pool, and pass the pool. Use 'Unrestricted_Access because the
8221 -- pool may not be aliased.
8223 if Present (Associated_Storage_Pool (Acc_Type)) then
8224 Alloc_Form := User_Storage_Pool;
8226 Make_Attribute_Reference (Loc,
8229 (Associated_Storage_Pool (Acc_Type), Loc),
8230 Attribute_Name => Name_Unrestricted_Access);
8232 -- No user-defined pool; pass an allocation parameter indicating that
8233 -- the function should allocate its result on the heap.
8236 Alloc_Form := Global_Heap;
8237 Pool := Make_Null (No_Location);
8240 -- The caller does not provide the return object in this case, so we
8241 -- have to pass null for the object access actual.
8243 Return_Obj_Actual := Empty;
8246 -- Replace the initialized allocator of form "new T'(Func (...))"
8247 -- with an uninitialized allocator of form "new T", where T is the
8248 -- result subtype of the called function. The call to the function
8249 -- is handled separately further below.
8252 Make_Allocator (Loc,
8253 Expression => New_Occurrence_Of (Result_Subt, Loc));
8254 Set_No_Initialization (New_Allocator);
8256 -- Copy attributes to new allocator. Note that the new allocator
8257 -- logically comes from source if the original one did, so copy the
8258 -- relevant flag. This ensures proper treatment of the restriction
8259 -- No_Implicit_Heap_Allocations in this case.
8261 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
8262 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
8263 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
8265 Rewrite (Allocator, New_Allocator);
8267 -- Initial value of the temp is the result of the uninitialized
8268 -- allocator. Unchecked_Convert is needed for T'Input where T is
8269 -- derived from a controlled type.
8271 Temp_Init := Relocate_Node (Allocator);
8273 if Nkind (Function_Call) in
8274 N_Type_Conversion | N_Unchecked_Type_Conversion
8276 Temp_Init := Unchecked_Convert_To (Acc_Type, Temp_Init);
8279 -- Indicate that caller allocates, and pass in the return object
8281 Alloc_Form := Caller_Allocation;
8282 Pool := Make_Null (No_Location);
8283 Return_Obj_Actual := Unchecked_Convert_To
8285 Make_Explicit_Dereference (Loc,
8286 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)));
8288 -- When the result subtype is unconstrained, the function itself must
8289 -- perform the allocation of the return object, so we pass parameters
8294 -- Declare the temp object
8296 Insert_Action (Allocator,
8297 Make_Object_Declaration (Loc,
8298 Defining_Identifier => Return_Obj_Access,
8299 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
8300 Expression => Temp_Init));
8302 Ref_Func_Call := Make_Reference (Loc, Func_Call);
8304 -- Ada 2005 (AI-251): If the type of the allocator is an interface
8305 -- then generate an implicit conversion to force displacement of the
8308 if Is_Interface (Designated_Type (Acc_Type)) then
8311 OK_Convert_To (Acc_Type, Ref_Func_Call));
8313 -- If the types are incompatible, we need an unchecked conversion. Note
8314 -- that the full types will be compatible, but the types not visibly
8317 elsif Nkind (Function_Call)
8318 in N_Type_Conversion | N_Unchecked_Type_Conversion
8320 Ref_Func_Call := Unchecked_Convert_To (Acc_Type, Ref_Func_Call);
8324 Assign : constant Node_Id :=
8325 Make_Assignment_Statement (Loc,
8326 Name => New_Occurrence_Of (Return_Obj_Access, Loc),
8327 Expression => Ref_Func_Call);
8328 -- Assign the result of the function call into the temp. In the
8329 -- caller-allocates case, this is overwriting the temp with its
8330 -- initial value, which has no effect. In the callee-allocates case,
8331 -- this is setting the temp to point to the object allocated by the
8332 -- callee. Unchecked_Convert is needed for T'Input where T is derived
8333 -- from a controlled type.
8336 -- Actions to be inserted. If there are no tasks, this is just the
8337 -- assignment statement. If the allocated object has tasks, we need
8338 -- to wrap the assignment in a block that activates them. The
8339 -- activation chain of that block must be passed to the function,
8340 -- rather than some outer chain.
8343 if Might_Have_Tasks (Result_Subt) then
8344 Actions := New_List;
8345 Build_Task_Allocate_Block_With_Init_Stmts
8346 (Actions, Allocator, Init_Stmts => New_List (Assign));
8347 Chain := Activation_Chain_Entity (Last (Actions));
8349 Actions := New_List (Assign);
8353 Insert_Actions (Allocator, Actions);
8356 -- When the function has a controlling result, an allocation-form
8357 -- parameter must be passed indicating that the caller is allocating
8358 -- the result object. This is needed because such a function can be
8359 -- called as a dispatching operation and must be treated similarly
8360 -- to functions with unconstrained result subtypes.
8362 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8363 (Func_Call, Function_Id, Alloc_Form, Pool_Actual => Pool);
8365 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8366 (Func_Call, Function_Id, Acc_Type);
8368 Add_Task_Actuals_To_Build_In_Place_Call
8369 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type),
8372 -- Add an implicit actual to the function call that provides access
8373 -- to the allocated object. An unchecked conversion to the (specific)
8374 -- result subtype of the function is inserted to handle cases where
8375 -- the access type of the allocator has a class-wide designated type.
8377 Add_Access_Actual_To_Build_In_Place_Call
8378 (Func_Call, Function_Id, Return_Obj_Actual);
8380 -- Finally, replace the allocator node with a reference to the temp
8382 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8384 Analyze_And_Resolve (Allocator, Acc_Type);
8385 pragma Assert (Check_Number_Of_Actuals (Func_Call, Function_Id));
8386 pragma Assert (Check_BIP_Actuals (Func_Call, Function_Id));
8387 end Make_Build_In_Place_Call_In_Allocator;
8389 ---------------------------------------------------
8390 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8391 ---------------------------------------------------
8393 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8394 (Function_Call : Node_Id)
8396 Loc : constant Source_Ptr := Sloc (Function_Call);
8397 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8398 Function_Id : Entity_Id;
8399 Result_Subt : Entity_Id;
8400 Return_Obj_Id : Entity_Id;
8401 Return_Obj_Decl : Entity_Id;
8404 -- If the call has already been processed to add build-in-place actuals
8405 -- then return. One place this can occur is for calls to build-in-place
8406 -- functions that occur within a call to a protected operation, where
8407 -- due to rewriting and expansion of the protected call there can be
8408 -- more than one call to Expand_Actuals for the same set of actuals.
8410 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8414 -- Mark the call as processed as a build-in-place call
8416 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8418 if Is_Entity_Name (Name (Func_Call)) then
8419 Function_Id := Entity (Name (Func_Call));
8421 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8422 Function_Id := Etype (Name (Func_Call));
8425 raise Program_Error;
8428 Warn_BIP (Func_Call);
8430 Result_Subt := Etype (Function_Id);
8432 -- If the build-in-place function returns a controlled object, then the
8433 -- object needs to be finalized immediately after the context. Since
8434 -- this case produces a transient scope, the servicing finalizer needs
8435 -- to name the returned object. Create a temporary which is initialized
8436 -- with the function call:
8438 -- Temp_Id : Func_Type := BIP_Func_Call;
8440 -- The initialization expression of the temporary will be rewritten by
8441 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8442 -- Call_In_Object_Declaration.
8444 if Needs_Finalization (Result_Subt) then
8446 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
8447 Temp_Decl : Node_Id;
8450 -- Reset the guard on the function call since the following does
8451 -- not perform actual call expansion.
8453 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
8456 Make_Object_Declaration (Loc,
8457 Defining_Identifier => Temp_Id,
8458 Object_Definition =>
8459 New_Occurrence_Of (Result_Subt, Loc),
8461 New_Copy_Tree (Function_Call));
8463 Insert_Action (Function_Call, Temp_Decl);
8465 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
8466 Analyze (Function_Call);
8469 -- When the result subtype is definite, an object of the subtype is
8470 -- declared and an access value designating it is passed as an actual.
8472 elsif Caller_Known_Size (Func_Call, Result_Subt) then
8474 -- Create a temporary object to hold the function result
8476 Return_Obj_Id := Make_Temporary (Loc, 'R');
8477 Set_Etype (Return_Obj_Id, Result_Subt);
8480 Make_Object_Declaration (Loc,
8481 Defining_Identifier => Return_Obj_Id,
8482 Aliased_Present => True,
8483 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
8485 Set_No_Initialization (Return_Obj_Decl);
8487 Insert_Action (Func_Call, Return_Obj_Decl);
8489 -- When the function has a controlling result, an allocation-form
8490 -- parameter must be passed indicating that the caller is allocating
8491 -- the result object. This is needed because such a function can be
8492 -- called as a dispatching operation and must be treated similarly
8493 -- to functions with unconstrained result subtypes.
8495 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8496 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8498 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8499 (Func_Call, Function_Id);
8501 Add_Task_Actuals_To_Build_In_Place_Call
8502 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8504 -- Add an implicit actual to the function call that provides access
8505 -- to the caller's return object.
8507 Add_Access_Actual_To_Build_In_Place_Call
8508 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
8510 pragma Assert (Check_Number_Of_Actuals (Func_Call, Function_Id));
8511 pragma Assert (Check_BIP_Actuals (Func_Call, Function_Id));
8513 -- When the result subtype is unconstrained, the function must allocate
8514 -- the return object in the secondary stack, so appropriate implicit
8515 -- parameters are added to the call to indicate that. A transient
8516 -- scope is established to ensure eventual cleanup of the result.
8519 -- Pass an allocation parameter indicating that the function should
8520 -- allocate its result on the secondary stack.
8522 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8523 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8525 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8526 (Func_Call, Function_Id);
8528 Add_Task_Actuals_To_Build_In_Place_Call
8529 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8531 -- Pass a null value to the function since no return object is
8532 -- available on the caller side.
8534 Add_Access_Actual_To_Build_In_Place_Call
8535 (Func_Call, Function_Id, Empty);
8537 pragma Assert (Check_Number_Of_Actuals (Func_Call, Function_Id));
8538 pragma Assert (Check_BIP_Actuals (Func_Call, Function_Id));
8540 end Make_Build_In_Place_Call_In_Anonymous_Context;
8542 --------------------------------------------
8543 -- Make_Build_In_Place_Call_In_Assignment --
8544 --------------------------------------------
8546 procedure Make_Build_In_Place_Call_In_Assignment
8548 Function_Call : Node_Id)
8550 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8551 Lhs : constant Node_Id := Name (Assign);
8552 Loc : constant Source_Ptr := Sloc (Function_Call);
8553 Func_Id : Entity_Id;
8556 Ptr_Typ : Entity_Id;
8557 Ptr_Typ_Decl : Node_Id;
8559 Result_Subt : Entity_Id;
8562 -- Mark the call as processed as a build-in-place call
8564 pragma Assert (not Is_Expanded_Build_In_Place_Call (Func_Call));
8565 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8567 if Is_Entity_Name (Name (Func_Call)) then
8568 Func_Id := Entity (Name (Func_Call));
8570 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8571 Func_Id := Etype (Name (Func_Call));
8574 raise Program_Error;
8577 Warn_BIP (Func_Call);
8579 Result_Subt := Etype (Func_Id);
8581 -- When the result subtype is unconstrained, an additional actual must
8582 -- be passed to indicate that the caller is providing the return object.
8583 -- This parameter must also be passed when the called function has a
8584 -- controlling result, because dispatching calls to the function needs
8585 -- to be treated effectively the same as calls to class-wide functions.
8587 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8588 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
8590 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8591 (Func_Call, Func_Id);
8593 Add_Task_Actuals_To_Build_In_Place_Call
8594 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
8596 -- Add an implicit actual to the function call that provides access to
8597 -- the caller's return object.
8599 Add_Access_Actual_To_Build_In_Place_Call
8600 (Func_Call, Func_Id, Unchecked_Convert_To (Result_Subt, Lhs));
8602 -- Create an access type designating the function's result subtype
8604 Ptr_Typ := Make_Temporary (Loc, 'A');
8607 Make_Full_Type_Declaration (Loc,
8608 Defining_Identifier => Ptr_Typ,
8610 Make_Access_To_Object_Definition (Loc,
8611 All_Present => True,
8612 Subtype_Indication =>
8613 New_Occurrence_Of (Result_Subt, Loc)));
8614 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
8616 -- Finally, create an access object initialized to a reference to the
8617 -- function call. We know this access value is non-null, so mark the
8618 -- entity accordingly to suppress junk access checks.
8620 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
8622 -- Add a conversion if it's the wrong type
8624 New_Expr := Unchecked_Convert_To (Ptr_Typ, New_Expr);
8626 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
8627 Set_Etype (Obj_Id, Ptr_Typ);
8628 Set_Is_Known_Non_Null (Obj_Id);
8631 Make_Object_Declaration (Loc,
8632 Defining_Identifier => Obj_Id,
8633 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8634 Expression => New_Expr);
8635 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
8637 Rewrite (Assign, Make_Null_Statement (Loc));
8638 pragma Assert (Check_Number_Of_Actuals (Func_Call, Func_Id));
8639 pragma Assert (Check_BIP_Actuals (Func_Call, Func_Id));
8640 end Make_Build_In_Place_Call_In_Assignment;
8642 ----------------------------------------------------
8643 -- Make_Build_In_Place_Call_In_Object_Declaration --
8644 ----------------------------------------------------
8646 procedure Make_Build_In_Place_Call_In_Object_Declaration
8647 (Obj_Decl : Node_Id;
8648 Function_Call : Node_Id)
8650 function Get_Function_Id (Func_Call : Node_Id) return Entity_Id;
8651 -- Get the value of Function_Id, below
8653 ---------------------
8654 -- Get_Function_Id --
8655 ---------------------
8657 function Get_Function_Id (Func_Call : Node_Id) return Entity_Id is
8659 if Is_Entity_Name (Name (Func_Call)) then
8660 return Entity (Name (Func_Call));
8662 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8663 return Etype (Name (Func_Call));
8666 raise Program_Error;
8668 end Get_Function_Id;
8672 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8673 Function_Id : constant Entity_Id := Get_Function_Id (Func_Call);
8674 Loc : constant Source_Ptr := Sloc (Function_Call);
8675 Obj_Loc : constant Source_Ptr := Sloc (Obj_Decl);
8676 Obj_Def_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
8677 Obj_Typ : constant Entity_Id := Etype (Obj_Def_Id);
8678 Encl_Func : constant Entity_Id := Enclosing_Subprogram (Obj_Def_Id);
8679 Result_Subt : constant Entity_Id := Etype (Function_Id);
8681 Call_Deref : Node_Id;
8682 Caller_Object : Node_Id;
8684 Designated_Type : Entity_Id;
8685 Fmaster_Actual : Node_Id := Empty;
8686 Pool_Actual : Node_Id;
8687 Ptr_Typ : Entity_Id;
8688 Ptr_Typ_Decl : Node_Id;
8689 Pass_Caller_Acc : Boolean := False;
8692 Definite : constant Boolean :=
8693 Caller_Known_Size (Func_Call, Result_Subt)
8694 and then not Is_Class_Wide_Type (Obj_Typ);
8695 -- In the case of "X : T'Class := F(...);", where F returns a
8696 -- Caller_Known_Size (specific) tagged type, we treat it as
8697 -- indefinite, because the code for the Definite case below sets the
8698 -- initialization expression of the object to Empty, which would be
8699 -- illegal Ada, and would cause gigi to misallocate X.
8701 Is_OK_Return_Object : constant Boolean :=
8702 Is_Return_Object (Obj_Def_Id)
8704 not Has_Foreign_Convention (Return_Applies_To (Scope (Obj_Def_Id)));
8706 -- Start of processing for Make_Build_In_Place_Call_In_Object_Declaration
8709 -- If the call has already been processed to add build-in-place actuals
8712 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8716 -- Mark the call as processed as a build-in-place call
8718 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8720 Warn_BIP (Func_Call);
8722 -- Create an access type designating the function's result subtype.
8723 -- We use the type of the original call because it may be a call to an
8724 -- inherited operation, which the expansion has replaced with the parent
8725 -- operation that yields the parent type. Note that this access type
8726 -- must be declared before we establish a transient scope, so that it
8727 -- receives the proper accessibility level.
8729 if Is_Class_Wide_Type (Obj_Typ)
8730 and then not Is_Interface (Obj_Typ)
8731 and then not Is_Class_Wide_Type (Etype (Function_Call))
8733 Designated_Type := Obj_Typ;
8735 Designated_Type := Etype (Function_Call);
8738 Ptr_Typ := Make_Temporary (Loc, 'A');
8740 Make_Full_Type_Declaration (Loc,
8741 Defining_Identifier => Ptr_Typ,
8743 Make_Access_To_Object_Definition (Loc,
8744 All_Present => True,
8745 Subtype_Indication =>
8746 New_Occurrence_Of (Designated_Type, Loc)));
8748 -- The access type and its accompanying object must be inserted after
8749 -- the object declaration in the constrained case, so that the function
8750 -- call can be passed access to the object. In the indefinite case, or
8751 -- if the object declaration is for a return object, the access type and
8752 -- object must be inserted before the object, since the object
8753 -- declaration is rewritten to be a renaming of a dereference of the
8754 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8755 -- the result object is in a different (transient) scope, so won't cause
8758 if Definite and then not Is_OK_Return_Object then
8760 -- The presence of an address clause complicates the build-in-place
8761 -- expansion because the indicated address must be processed before
8762 -- the indirect call is generated (including the definition of a
8763 -- local pointer to the object). The address clause may come from
8764 -- an aspect specification or from an explicit attribute
8765 -- specification appearing after the object declaration. These two
8766 -- cases require different processing.
8768 if Has_Aspect (Obj_Def_Id, Aspect_Address) then
8770 -- Skip non-delayed pragmas that correspond to other aspects, if
8771 -- any, to find proper insertion point for freeze node of object.
8774 D : Node_Id := Obj_Decl;
8775 N : Node_Id := Next (D);
8779 and then Nkind (N) in N_Attribute_Reference | N_Pragma
8786 Insert_After (D, Ptr_Typ_Decl);
8788 -- Freeze object before pointer declaration, to ensure that
8789 -- generated attribute for address is inserted at the proper
8792 Freeze_Before (Ptr_Typ_Decl, Obj_Def_Id);
8795 Analyze (Ptr_Typ_Decl);
8797 elsif Present (Following_Address_Clause (Obj_Decl)) then
8799 -- Locate explicit address clause, which may also follow pragmas
8800 -- generated by other aspect specifications.
8803 Addr : constant Node_Id := Following_Address_Clause (Obj_Decl);
8804 D : Node_Id := Next (Obj_Decl);
8807 while Present (D) loop
8813 Insert_After_And_Analyze (Addr, Ptr_Typ_Decl);
8817 Insert_After_And_Analyze (Obj_Decl, Ptr_Typ_Decl);
8820 Insert_Action (Obj_Decl, Ptr_Typ_Decl);
8823 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
8824 -- elaborated in an inner (transient) scope and thus won't cause
8825 -- freezing by itself. It's not an itype, but it needs to be frozen
8826 -- inside the current subprogram (see Freeze_Outside in freeze.adb).
8828 Freeze_Itype (Ptr_Typ, Ptr_Typ_Decl);
8830 -- If the object is a return object of an enclosing build-in-place
8831 -- function, then the implicit build-in-place parameters of the
8832 -- enclosing function are simply passed along to the called function.
8833 -- (Unfortunately, this won't cover the case of extension aggregates
8834 -- where the ancestor part is a build-in-place indefinite function
8835 -- call that should be passed along the caller's parameters.
8836 -- Currently those get mishandled by reassigning the result of the
8837 -- call to the aggregate return object, when the call result should
8838 -- really be directly built in place in the aggregate and not in a
8841 if Is_OK_Return_Object then
8842 Pass_Caller_Acc := True;
8844 -- When the enclosing function has a BIP_Alloc_Form formal then we
8845 -- pass it along to the callee (such as when the enclosing function
8846 -- has an unconstrained or tagged result type).
8848 if Needs_BIP_Alloc_Form (Encl_Func) then
8849 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8852 (Build_In_Place_Formal
8853 (Encl_Func, BIP_Storage_Pool), Loc);
8855 -- The build-in-place pool formal is not built on e.g. ZFP
8858 Pool_Actual := Empty;
8861 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8862 (Function_Call => Func_Call,
8863 Function_Id => Function_Id,
8866 (Build_In_Place_Formal (Encl_Func, BIP_Alloc_Form), Loc),
8867 Pool_Actual => Pool_Actual);
8869 -- Otherwise, if enclosing function has a definite result subtype,
8870 -- then caller allocation will be used.
8873 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8874 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8877 if Needs_BIP_Finalization_Master (Encl_Func) then
8880 (Build_In_Place_Formal
8881 (Encl_Func, BIP_Finalization_Master), Loc);
8884 -- Retrieve the BIPacc formal from the enclosing function and convert
8885 -- it to the access type of the callee's BIP_Object_Access formal.
8888 Unchecked_Convert_To
8889 (Etype (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
8891 (Build_In_Place_Formal (Encl_Func, BIP_Object_Access), Loc));
8893 -- In the definite case, add an implicit actual to the function call
8894 -- that provides access to the declared object. An unchecked conversion
8895 -- to the (specific) result type of the function is inserted to handle
8896 -- the case where the object is declared with a class-wide type.
8899 Caller_Object := Unchecked_Convert_To
8900 (Result_Subt, New_Occurrence_Of (Obj_Def_Id, Loc));
8902 -- When the function has a controlling result, an allocation-form
8903 -- parameter must be passed indicating that the caller is allocating
8904 -- the result object. This is needed because such a function can be
8905 -- called as a dispatching operation and must be treated similarly to
8906 -- functions with indefinite result subtypes.
8908 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8909 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8911 -- The allocation for indefinite library-level objects occurs on the
8912 -- heap as opposed to the secondary stack. This accommodates DLLs where
8913 -- the secondary stack is destroyed after each library unload. This is a
8914 -- hybrid mechanism where a stack-allocated object lives on the heap.
8916 elsif Is_Library_Level_Entity (Obj_Def_Id)
8917 and then not Restriction_Active (No_Implicit_Heap_Allocations)
8919 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8920 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
8921 Caller_Object := Empty;
8923 -- Create a finalization master for the access result type to ensure
8924 -- that the heap allocation can properly chain the object and later
8925 -- finalize it when the library unit goes out of scope.
8927 if Needs_Finalization (Etype (Func_Call)) then
8928 Build_Finalization_Master
8930 For_Lib_Level => True,
8931 Insertion_Node => Ptr_Typ_Decl);
8934 Make_Attribute_Reference (Loc,
8936 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
8937 Attribute_Name => Name_Unrestricted_Access);
8940 -- In other indefinite cases, pass an indication to do the allocation
8941 -- on the secondary stack and set Caller_Object to Empty so that a null
8942 -- value will be passed for the caller's object address. A transient
8943 -- scope is established to ensure eventual cleanup of the result.
8946 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8947 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8948 Caller_Object := Empty;
8950 Establish_Transient_Scope (Obj_Decl, Manage_Sec_Stack => True);
8953 -- Pass along any finalization master actual, which is needed in the
8954 -- case where the called function initializes a return object of an
8955 -- enclosing build-in-place function.
8957 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8958 (Func_Call => Func_Call,
8959 Func_Id => Function_Id,
8960 Master_Exp => Fmaster_Actual);
8962 if Nkind (Parent (Obj_Decl)) = N_Extended_Return_Statement
8963 and then Needs_BIP_Task_Actuals (Function_Id)
8965 -- Here we're passing along the master that was passed in to this
8968 Add_Task_Actuals_To_Build_In_Place_Call
8969 (Func_Call, Function_Id,
8972 (Build_In_Place_Formal (Encl_Func, BIP_Task_Master), Loc));
8975 Add_Task_Actuals_To_Build_In_Place_Call
8976 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8979 Add_Access_Actual_To_Build_In_Place_Call
8983 Is_Access => Pass_Caller_Acc);
8985 -- Finally, create an access object initialized to a reference to the
8986 -- function call. We know this access value cannot be null, so mark the
8987 -- entity accordingly to suppress the access check. We need to suppress
8988 -- warnings, because this can be part of the expansion of "for ... of"
8989 -- and similar constructs that generate finalization actions. Such
8990 -- finalization actions are safe, because they check a count that
8991 -- indicates which objects should be finalized, but the back end
8992 -- nonetheless warns about uninitialized objects.
8994 Def_Id := Make_Temporary (Loc, 'R', Func_Call);
8995 Set_Warnings_Off (Def_Id);
8996 Set_Etype (Def_Id, Ptr_Typ);
8997 Set_Is_Known_Non_Null (Def_Id);
8999 if Nkind (Function_Call) in N_Type_Conversion
9000 | N_Unchecked_Type_Conversion
9003 Make_Object_Declaration (Loc,
9004 Defining_Identifier => Def_Id,
9005 Constant_Present => True,
9006 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
9008 Unchecked_Convert_To
9009 (Ptr_Typ, Make_Reference (Loc, Relocate_Node (Func_Call))));
9012 Make_Object_Declaration (Loc,
9013 Defining_Identifier => Def_Id,
9014 Constant_Present => True,
9015 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
9017 Make_Reference (Loc, Relocate_Node (Func_Call)));
9020 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
9022 -- If the result subtype of the called function is definite and is not
9023 -- itself the return expression of an enclosing BIP function, then mark
9024 -- the object as having no initialization.
9026 if Definite and then not Is_OK_Return_Object then
9028 -- The related object declaration is encased in a transient block
9029 -- because the build-in-place function call contains at least one
9030 -- nested function call that produces a controlled transient
9033 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
9035 -- Since the build-in-place expansion decouples the call from the
9036 -- object declaration, the finalization machinery lacks the context
9037 -- which prompted the generation of the transient block. To resolve
9038 -- this scenario, store the build-in-place call.
9040 if Scope_Is_Transient then
9041 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
9044 Set_Expression (Obj_Decl, Empty);
9045 Set_No_Initialization (Obj_Decl);
9047 -- In case of an indefinite result subtype, or if the call is the
9048 -- return expression of an enclosing BIP function, rewrite the object
9049 -- declaration as an object renaming where the renamed object is a
9050 -- dereference of <function_Call>'reference:
9052 -- Obj : Subt renames <function_call>'Ref.all;
9056 Make_Explicit_Dereference (Obj_Loc,
9057 Prefix => New_Occurrence_Of (Def_Id, Obj_Loc));
9060 Make_Object_Renaming_Declaration (Obj_Loc,
9061 Defining_Identifier => Make_Temporary (Obj_Loc, 'D'),
9063 New_Occurrence_Of (Designated_Type, Obj_Loc),
9064 Name => Call_Deref));
9066 -- At this point, Defining_Identifier (Obj_Decl) is no longer equal
9069 pragma Assert (Ekind (Defining_Identifier (Obj_Decl)) = E_Void);
9070 Set_Renamed_Object_Of_Possibly_Void
9071 (Defining_Identifier (Obj_Decl), Call_Deref);
9073 -- If the original entity comes from source, then mark the new
9074 -- entity as needing debug information, even though it's defined
9075 -- by a generated renaming that does not come from source, so that
9076 -- the Materialize_Entity flag will be set on the entity when
9077 -- Debug_Renaming_Declaration is called during analysis.
9079 if Comes_From_Source (Obj_Def_Id) then
9080 Set_Debug_Info_Needed (Defining_Identifier (Obj_Decl));
9084 Replace_Renaming_Declaration_Id
9085 (Obj_Decl, Original_Node (Obj_Decl));
9088 pragma Assert (Check_Number_Of_Actuals (Func_Call, Function_Id));
9089 pragma Assert (Check_BIP_Actuals (Func_Call, Function_Id));
9090 end Make_Build_In_Place_Call_In_Object_Declaration;
9092 -------------------------------------------------
9093 -- Make_Build_In_Place_Iface_Call_In_Allocator --
9094 -------------------------------------------------
9096 procedure Make_Build_In_Place_Iface_Call_In_Allocator
9097 (Allocator : Node_Id;
9098 Function_Call : Node_Id)
9100 BIP_Func_Call : constant Node_Id :=
9101 Unqual_BIP_Iface_Function_Call (Function_Call);
9102 Loc : constant Source_Ptr := Sloc (Function_Call);
9104 Anon_Type : Entity_Id;
9109 -- No action if the call has already been processed
9111 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
9115 Tmp_Id := Make_Temporary (Loc, 'D');
9117 -- Insert a temporary before N initialized with the BIP function call
9118 -- without its enclosing type conversions and analyze it without its
9119 -- expansion. This temporary facilitates us reusing the BIP machinery,
9120 -- which takes care of adding the extra build-in-place actuals and
9121 -- transforms this object declaration into an object renaming
9124 Anon_Type := Create_Itype (E_Anonymous_Access_Type, Function_Call);
9125 Set_Directly_Designated_Type (Anon_Type, Etype (BIP_Func_Call));
9126 Set_Etype (Anon_Type, Anon_Type);
9127 Build_Class_Wide_Master (Anon_Type);
9130 Make_Object_Declaration (Loc,
9131 Defining_Identifier => Tmp_Id,
9132 Object_Definition => New_Occurrence_Of (Anon_Type, Loc),
9134 Make_Allocator (Loc,
9136 Make_Qualified_Expression (Loc,
9138 New_Occurrence_Of (Etype (BIP_Func_Call), Loc),
9139 Expression => New_Copy_Tree (BIP_Func_Call))));
9141 -- Manually set the associated node for the anonymous access type to
9142 -- be its local declaration, to avoid confusing and complicating
9143 -- the accessibility machinery.
9145 Set_Associated_Node_For_Itype (Anon_Type, Tmp_Decl);
9147 Expander_Mode_Save_And_Set (False);
9148 Insert_Action (Allocator, Tmp_Decl);
9149 Expander_Mode_Restore;
9151 Make_Build_In_Place_Call_In_Allocator
9152 (Allocator => Expression (Tmp_Decl),
9153 Function_Call => Expression (Expression (Tmp_Decl)));
9155 -- Add a conversion to displace the pointer to the allocated object
9156 -- to reference the corresponding dispatch table.
9159 Convert_To (Etype (Allocator),
9160 New_Occurrence_Of (Tmp_Id, Loc)));
9161 end Make_Build_In_Place_Iface_Call_In_Allocator;
9163 ---------------------------------------------------------
9164 -- Make_Build_In_Place_Iface_Call_In_Anonymous_Context --
9165 ---------------------------------------------------------
9167 procedure Make_Build_In_Place_Iface_Call_In_Anonymous_Context
9168 (Function_Call : Node_Id)
9170 BIP_Func_Call : constant Node_Id :=
9171 Unqual_BIP_Iface_Function_Call (Function_Call);
9172 Loc : constant Source_Ptr := Sloc (Function_Call);
9178 -- No action of the call has already been processed
9180 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
9184 pragma Assert (Needs_Finalization (Etype (BIP_Func_Call)));
9186 -- Insert a temporary before the call initialized with function call to
9187 -- reuse the BIP machinery which takes care of adding the extra build-in
9188 -- place actuals and transforms this object declaration into an object
9189 -- renaming declaration.
9191 Tmp_Id := Make_Temporary (Loc, 'D');
9194 Make_Object_Declaration (Loc,
9195 Defining_Identifier => Tmp_Id,
9196 Object_Definition =>
9197 New_Occurrence_Of (Etype (Function_Call), Loc),
9198 Expression => Relocate_Node (Function_Call));
9200 Expander_Mode_Save_And_Set (False);
9201 Insert_Action (Function_Call, Tmp_Decl);
9202 Expander_Mode_Restore;
9204 Make_Build_In_Place_Iface_Call_In_Object_Declaration
9205 (Obj_Decl => Tmp_Decl,
9206 Function_Call => Expression (Tmp_Decl));
9207 end Make_Build_In_Place_Iface_Call_In_Anonymous_Context;
9209 ----------------------------------------------------------
9210 -- Make_Build_In_Place_Iface_Call_In_Object_Declaration --
9211 ----------------------------------------------------------
9213 procedure Make_Build_In_Place_Iface_Call_In_Object_Declaration
9214 (Obj_Decl : Node_Id;
9215 Function_Call : Node_Id)
9217 BIP_Func_Call : constant Node_Id :=
9218 Unqual_BIP_Iface_Function_Call (Function_Call);
9219 Loc : constant Source_Ptr := Sloc (Function_Call);
9220 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
9226 -- No action of the call has already been processed
9228 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
9232 Tmp_Id := Make_Temporary (Loc, 'D');
9234 -- Insert a temporary before N initialized with the BIP function call
9235 -- without its enclosing type conversions and analyze it without its
9236 -- expansion. This temporary facilitates us reusing the BIP machinery,
9237 -- which takes care of adding the extra build-in-place actuals and
9238 -- transforms this object declaration into an object renaming
9242 Make_Object_Declaration (Loc,
9243 Defining_Identifier => Tmp_Id,
9244 Object_Definition =>
9245 New_Occurrence_Of (Etype (BIP_Func_Call), Loc),
9246 Expression => New_Copy_Tree (BIP_Func_Call));
9248 Expander_Mode_Save_And_Set (False);
9249 Insert_Action (Obj_Decl, Tmp_Decl);
9250 Expander_Mode_Restore;
9252 Make_Build_In_Place_Call_In_Object_Declaration
9253 (Obj_Decl => Tmp_Decl,
9254 Function_Call => Expression (Tmp_Decl));
9256 pragma Assert (Nkind (Tmp_Decl) = N_Object_Renaming_Declaration);
9258 -- Replace the original build-in-place function call by a reference to
9259 -- the resulting temporary object renaming declaration. In this way,
9260 -- all the interface conversions performed in the original Function_Call
9261 -- on the build-in-place object are preserved.
9263 Rewrite (BIP_Func_Call, New_Occurrence_Of (Tmp_Id, Loc));
9265 -- Replace the original object declaration by an internal object
9266 -- renaming declaration. This leaves the generated code more clean (the
9267 -- build-in-place function call in an object renaming declaration and
9268 -- displacements of the pointer to the build-in-place object in another
9269 -- renaming declaration) and allows us to invoke the routine that takes
9270 -- care of replacing the identifier of the renaming declaration (routine
9271 -- originally developed for the regular build-in-place management).
9274 Make_Object_Renaming_Declaration (Loc,
9275 Defining_Identifier => Make_Temporary (Loc, 'D'),
9276 Subtype_Mark => New_Occurrence_Of (Etype (Obj_Id), Loc),
9277 Name => Function_Call));
9280 Replace_Renaming_Declaration_Id (Obj_Decl, Original_Node (Obj_Decl));
9281 end Make_Build_In_Place_Iface_Call_In_Object_Declaration;
9283 --------------------------------------------
9284 -- Make_CPP_Constructor_Call_In_Allocator --
9285 --------------------------------------------
9287 procedure Make_CPP_Constructor_Call_In_Allocator
9288 (Allocator : Node_Id;
9289 Function_Call : Node_Id)
9291 Loc : constant Source_Ptr := Sloc (Function_Call);
9292 Acc_Type : constant Entity_Id := Etype (Allocator);
9293 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
9294 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
9296 New_Allocator : Node_Id;
9297 Return_Obj_Access : Entity_Id;
9301 pragma Assert (Nkind (Allocator) = N_Allocator
9302 and then Nkind (Function_Call) = N_Function_Call);
9303 pragma Assert (Convention (Function_Id) = Convention_CPP
9304 and then Is_Constructor (Function_Id));
9305 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
9307 -- Replace the initialized allocator of form "new T'(Func (...))" with
9308 -- an uninitialized allocator of form "new T", where T is the result
9309 -- subtype of the called function. The call to the function is handled
9310 -- separately further below.
9313 Make_Allocator (Loc,
9314 Expression => New_Occurrence_Of (Result_Subt, Loc));
9315 Set_No_Initialization (New_Allocator);
9317 -- Copy attributes to new allocator. Note that the new allocator
9318 -- logically comes from source if the original one did, so copy the
9319 -- relevant flag. This ensures proper treatment of the restriction
9320 -- No_Implicit_Heap_Allocations in this case.
9322 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
9323 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
9324 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
9326 Rewrite (Allocator, New_Allocator);
9328 -- Create a new access object and initialize it to the result of the
9329 -- new uninitialized allocator. Note: we do not use Allocator as the
9330 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9331 -- as this would create a sort of infinite "recursion".
9333 Return_Obj_Access := Make_Temporary (Loc, 'R');
9334 Set_Etype (Return_Obj_Access, Acc_Type);
9337 -- Rnnn : constant ptr_T := new (T);
9338 -- Init (Rnn.all,...);
9341 Make_Object_Declaration (Loc,
9342 Defining_Identifier => Return_Obj_Access,
9343 Constant_Present => True,
9344 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
9345 Expression => Relocate_Node (Allocator));
9346 Insert_Action (Allocator, Tmp_Obj);
9348 Insert_List_After_And_Analyze (Tmp_Obj,
9349 Build_Initialization_Call (Loc,
9351 Make_Explicit_Dereference (Loc,
9352 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
9353 Typ => Etype (Function_Id),
9354 Constructor_Ref => Function_Call));
9356 -- Finally, replace the allocator node with a reference to the result of
9357 -- the function call itself (which will effectively be an access to the
9358 -- object created by the allocator).
9360 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
9362 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9363 -- generate an implicit conversion to force displacement of the "this"
9366 if Is_Interface (Designated_Type (Acc_Type)) then
9367 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
9370 Analyze_And_Resolve (Allocator, Acc_Type);
9371 end Make_CPP_Constructor_Call_In_Allocator;
9373 ----------------------
9374 -- Might_Have_Tasks --
9375 ----------------------
9377 function Might_Have_Tasks (Typ : Entity_Id) return Boolean is
9379 return not Global_No_Tasking
9380 and then not No_Run_Time_Mode
9381 and then (Has_Task (Typ)
9382 or else (Is_Class_Wide_Type (Typ)
9383 and then Is_Limited_Record (Typ)
9384 and then not Has_Aspect
9385 (Etype (Typ), Aspect_No_Task_Parts)));
9386 end Might_Have_Tasks;
9388 ----------------------------
9389 -- Needs_BIP_Task_Actuals --
9390 ----------------------------
9392 function Needs_BIP_Task_Actuals (Func_Id : Entity_Id) return Boolean is
9393 Subp_Id : Entity_Id;
9394 Func_Typ : Entity_Id;
9397 if Global_No_Tasking or else No_Run_Time_Mode then
9401 -- For thunks we must rely on their target entity; otherwise, given that
9402 -- the profile of thunks for functions returning a limited interface
9403 -- type returns a class-wide type, we would erroneously add these extra
9406 if Is_Thunk (Func_Id) then
9407 Subp_Id := Thunk_Target (Func_Id);
9415 Func_Typ := Underlying_Type (Etype (Subp_Id));
9417 -- Functions returning types with foreign convention don't have extra
9420 if Has_Foreign_Convention (Func_Typ) then
9423 -- At first sight, for all the following cases, we could add assertions
9424 -- to ensure that if Func_Id is frozen then the computed result matches
9425 -- with the availability of the task master extra formal; unfortunately
9426 -- this is not feasible because we may be precisely freezing this entity
9427 -- (that is, Is_Frozen has been set by Freeze_Entity but it has not
9428 -- completed its work).
9430 elsif Has_Task (Func_Typ) then
9433 elsif Ekind (Func_Id) = E_Function then
9434 return Might_Have_Tasks (Func_Typ);
9436 -- Handle subprogram type internally generated for dispatching call. We
9437 -- cannot rely on the return type of the subprogram type of dispatching
9438 -- calls since it is always a class-wide type (cf. Expand_Dispatching_
9441 elsif Ekind (Func_Id) = E_Subprogram_Type then
9442 if Is_Dispatch_Table_Entity (Func_Id) then
9443 return Has_BIP_Extra_Formal (Func_Id, BIP_Task_Master);
9445 return Might_Have_Tasks (Func_Typ);
9449 raise Program_Error;
9451 end Needs_BIP_Task_Actuals;
9453 -----------------------------------
9454 -- Needs_BIP_Finalization_Master --
9455 -----------------------------------
9457 function Needs_BIP_Finalization_Master (Func_Id : Entity_Id) return Boolean
9459 Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9462 -- A formal giving the finalization master is needed for build-in-place
9463 -- functions whose result type needs finalization or is a tagged type.
9464 -- Tagged primitive build-in-place functions need such a formal because
9465 -- they can be called by a dispatching call, and extensions may require
9466 -- finalization even if the root type doesn't. This means nonprimitive
9467 -- build-in-place functions with tagged results also need it, since such
9468 -- functions can be called via access-to-function types, and those can
9469 -- be used to call primitives, so the formal needs to be passed to all
9470 -- such build-in-place functions, primitive or not.
9472 return not Restriction_Active (No_Finalization)
9473 and then (Needs_Finalization (Typ) or else Is_Tagged_Type (Typ))
9474 and then not Has_Foreign_Convention (Typ);
9475 end Needs_BIP_Finalization_Master;
9477 --------------------------
9478 -- Needs_BIP_Alloc_Form --
9479 --------------------------
9481 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
9482 Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9485 -- A formal giving the allocation method is needed for build-in-place
9486 -- functions whose result type is returned on the secondary stack or
9487 -- is a tagged type. Tagged primitive build-in-place functions need
9488 -- such a formal because they can be called by a dispatching call, and
9489 -- the secondary stack is always used for dispatching-on-result calls.
9490 -- This means nonprimitive build-in-place functions with tagged results
9491 -- also need it, as such functions can be called via access-to-function
9492 -- types, and those can be used to call primitives, so the formal needs
9493 -- to be passed to all such build-in-place functions, primitive or not.
9495 -- We never use build-in-place if the function has foreign convention,
9496 -- but note that it is OK for a build-in-place function to return a
9497 -- type with a foreign convention because the machinery ensures there
9500 return not Restriction_Active (No_Secondary_Stack)
9501 and then (Needs_Secondary_Stack (Typ) or else Is_Tagged_Type (Typ))
9502 and then not Has_Foreign_Convention (Func_Id);
9503 end Needs_BIP_Alloc_Form;
9505 -------------------------------------
9506 -- Replace_Renaming_Declaration_Id --
9507 -------------------------------------
9509 procedure Replace_Renaming_Declaration_Id
9510 (New_Decl : Node_Id;
9511 Orig_Decl : Node_Id)
9513 New_Id : constant Entity_Id := Defining_Entity (New_Decl);
9514 Orig_Id : constant Entity_Id := Defining_Entity (Orig_Decl);
9517 Set_Chars (New_Id, Chars (Orig_Id));
9519 -- Swap next entity links in preparation for exchanging entities
9522 Next_Id : constant Entity_Id := Next_Entity (New_Id);
9524 Link_Entities (New_Id, Next_Entity (Orig_Id));
9525 Link_Entities (Orig_Id, Next_Id);
9528 Set_Homonym (New_Id, Homonym (Orig_Id));
9529 Exchange_Entities (New_Id, Orig_Id);
9531 -- Preserve source indication of original declaration, so that xref
9532 -- information is properly generated for the right entity.
9534 Preserve_Comes_From_Source (New_Decl, Orig_Decl);
9535 Preserve_Comes_From_Source (Orig_Id, Orig_Decl);
9537 Set_Comes_From_Source (New_Id, False);
9539 -- Preserve aliased indication
9541 Set_Is_Aliased (Orig_Id, Is_Aliased (New_Id));
9542 end Replace_Renaming_Declaration_Id;
9544 ---------------------------------
9545 -- Rewrite_Function_Call_For_C --
9546 ---------------------------------
9548 procedure Rewrite_Function_Call_For_C (N : Node_Id) is
9549 Orig_Func : constant Entity_Id := Entity (Name (N));
9550 Func_Id : constant Entity_Id := Ultimate_Alias (Orig_Func);
9551 Par : constant Node_Id := Parent (N);
9552 Proc_Id : constant Entity_Id := Corresponding_Procedure (Func_Id);
9553 Loc : constant Source_Ptr := Sloc (Par);
9555 Last_Actual : Node_Id;
9556 Last_Formal : Entity_Id;
9558 -- Start of processing for Rewrite_Function_Call_For_C
9561 -- The actuals may be given by named associations, so the added actual
9562 -- that is the target of the return value of the call must be a named
9563 -- association as well, so we retrieve the name of the generated
9566 Last_Formal := First_Formal (Proc_Id);
9567 while Present (Next_Formal (Last_Formal)) loop
9568 Next_Formal (Last_Formal);
9571 Actuals := Parameter_Associations (N);
9573 -- The original function may lack parameters
9575 if No (Actuals) then
9576 Actuals := New_List;
9579 -- If the function call is the expression of an assignment statement,
9580 -- transform the assignment into a procedure call. Generate:
9582 -- LHS := Func_Call (...);
9584 -- Proc_Call (..., LHS);
9586 -- If function is inherited, a conversion may be necessary.
9588 if Nkind (Par) = N_Assignment_Statement then
9589 Last_Actual := Name (Par);
9591 if not Comes_From_Source (Orig_Func)
9592 and then Etype (Orig_Func) /= Etype (Func_Id)
9595 Make_Type_Conversion (Loc,
9596 New_Occurrence_Of (Etype (Func_Id), Loc),
9601 Make_Parameter_Association (Loc,
9603 Make_Identifier (Loc, Chars (Last_Formal)),
9604 Explicit_Actual_Parameter => Last_Actual));
9607 Make_Procedure_Call_Statement (Loc,
9608 Name => New_Occurrence_Of (Proc_Id, Loc),
9609 Parameter_Associations => Actuals));
9612 -- Otherwise the context is an expression. Generate a temporary and a
9613 -- procedure call to obtain the function result. Generate:
9615 -- ... Func_Call (...) ...
9618 -- Proc_Call (..., Temp);
9623 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
9632 Make_Object_Declaration (Loc,
9633 Defining_Identifier => Temp_Id,
9634 Object_Definition =>
9635 New_Occurrence_Of (Etype (Func_Id), Loc));
9638 -- Proc_Call (..., Temp);
9641 Make_Parameter_Association (Loc,
9643 Make_Identifier (Loc, Chars (Last_Formal)),
9644 Explicit_Actual_Parameter =>
9645 New_Occurrence_Of (Temp_Id, Loc)));
9648 Make_Procedure_Call_Statement (Loc,
9649 Name => New_Occurrence_Of (Proc_Id, Loc),
9650 Parameter_Associations => Actuals);
9652 Insert_Actions (Par, New_List (Decl, Call));
9653 Rewrite (N, New_Occurrence_Of (Temp_Id, Loc));
9656 end Rewrite_Function_Call_For_C;
9658 ------------------------------------
9659 -- Set_Enclosing_Sec_Stack_Return --
9660 ------------------------------------
9662 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id) is
9666 -- Due to a possible mix of internally generated blocks, source blocks
9667 -- and loops, the scope stack may not be contiguous as all labels are
9668 -- inserted at the top level within the related function. Instead,
9669 -- perform a parent-based traversal and mark all appropriate constructs.
9671 while Present (P) loop
9673 -- Mark the label of a source or internally generated block or
9676 if Nkind (P) in N_Block_Statement | N_Loop_Statement then
9677 Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P)));
9679 -- Mark the enclosing function
9681 elsif Nkind (P) = N_Subprogram_Body then
9682 if Present (Corresponding_Spec (P)) then
9683 Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P));
9685 Set_Sec_Stack_Needed_For_Return (Defining_Entity (P));
9688 -- Do not go beyond the enclosing function
9695 end Set_Enclosing_Sec_Stack_Return;
9697 ------------------------------------
9698 -- Unqual_BIP_Iface_Function_Call --
9699 ------------------------------------
9701 function Unqual_BIP_Iface_Function_Call (Expr : Node_Id) return Node_Id is
9702 Has_Pointer_Displacement : Boolean := False;
9703 On_Object_Declaration : Boolean := False;
9704 -- Remember if processing the renaming expressions on recursion we have
9705 -- traversed an object declaration, since we can traverse many object
9706 -- declaration renamings but just one regular object declaration.
9708 function Unqual_BIP_Function_Call (Expr : Node_Id) return Node_Id;
9709 -- Search for a build-in-place function call skipping any qualification
9710 -- including qualified expressions, type conversions, references, calls
9711 -- to displace the pointer to the object, and renamings. Return Empty if
9712 -- no build-in-place function call is found.
9714 ------------------------------
9715 -- Unqual_BIP_Function_Call --
9716 ------------------------------
9718 function Unqual_BIP_Function_Call (Expr : Node_Id) return Node_Id is
9720 -- Recurse to handle case of multiple levels of qualification and/or
9723 if Nkind (Expr) in N_Qualified_Expression
9725 | N_Unchecked_Type_Conversion
9727 return Unqual_BIP_Function_Call (Expression (Expr));
9729 -- Recurse to handle case of multiple levels of references and
9730 -- explicit dereferences.
9732 elsif Nkind (Expr) in N_Attribute_Reference
9733 | N_Explicit_Dereference
9736 return Unqual_BIP_Function_Call (Prefix (Expr));
9738 -- Recurse on object renamings
9740 elsif Nkind (Expr) = N_Identifier
9741 and then Present (Entity (Expr))
9742 and then Ekind (Entity (Expr)) in E_Constant | E_Variable
9743 and then Nkind (Parent (Entity (Expr))) =
9744 N_Object_Renaming_Declaration
9745 and then Present (Renamed_Object (Entity (Expr)))
9747 return Unqual_BIP_Function_Call (Renamed_Object (Entity (Expr)));
9749 -- Recurse on the initializing expression of the first reference of
9750 -- an object declaration.
9752 elsif not On_Object_Declaration
9753 and then Nkind (Expr) = N_Identifier
9754 and then Present (Entity (Expr))
9755 and then Ekind (Entity (Expr)) in E_Constant | E_Variable
9756 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
9757 and then Present (Expression (Parent (Entity (Expr))))
9759 On_Object_Declaration := True;
9761 Unqual_BIP_Function_Call (Expression (Parent (Entity (Expr))));
9763 -- Recurse to handle calls to displace the pointer to the object to
9764 -- reference a secondary dispatch table.
9766 elsif Nkind (Expr) = N_Function_Call
9767 and then Nkind (Name (Expr)) in N_Has_Entity
9768 and then Present (Entity (Name (Expr)))
9769 and then Is_RTE (Entity (Name (Expr)), RE_Displace)
9771 Has_Pointer_Displacement := True;
9773 Unqual_BIP_Function_Call (First (Parameter_Associations (Expr)));
9775 -- Normal case: check if the inner expression is a BIP function call
9776 -- and the pointer to the object is displaced.
9778 elsif Has_Pointer_Displacement
9779 and then Is_Build_In_Place_Function_Call (Expr)
9786 end Unqual_BIP_Function_Call;
9788 -- Start of processing for Unqual_BIP_Iface_Function_Call
9791 if Nkind (Expr) = N_Identifier and then No (Entity (Expr)) then
9793 -- Can happen for X'Elab_Spec in the binder-generated file
9798 return Unqual_BIP_Function_Call (Expr);
9799 end Unqual_BIP_Iface_Function_Call;
9801 -------------------------------
9802 -- Validate_Subprogram_Calls --
9803 -------------------------------
9805 procedure Validate_Subprogram_Calls (N : Node_Id) is
9807 function Process_Node (Nod : Node_Id) return Traverse_Result;
9808 -- Function to traverse the subtree of N using Traverse_Proc.
9814 function Process_Node (Nod : Node_Id) return Traverse_Result is
9817 when N_Entry_Call_Statement
9818 | N_Procedure_Call_Statement
9822 Call_Node : Node_Id renames Nod;
9826 -- Call using access to subprogram with explicit dereference
9828 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
9829 Subp := Etype (Name (Call_Node));
9831 -- Prefix notation calls
9833 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
9834 Subp := Entity (Selector_Name (Name (Call_Node)));
9836 -- Call to member of entry family, where Name is an indexed
9837 -- component, with the prefix being a selected component
9838 -- giving the task and entry family name, and the index
9839 -- being the entry index.
9841 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
9843 Entity (Selector_Name (Prefix (Name (Call_Node))));
9848 Subp := Entity (Name (Call_Node));
9851 pragma Assert (Check_BIP_Actuals (Call_Node, Subp));
9854 -- Skip generic bodies
9856 when N_Package_Body =>
9857 if Ekind (Unique_Defining_Entity (Nod)) = E_Generic_Package then
9861 when N_Subprogram_Body =>
9862 if Ekind (Unique_Defining_Entity (Nod)) in E_Generic_Function
9863 | E_Generic_Procedure
9868 -- Nodes we want to ignore
9870 -- Skip calls placed in the full declaration of record types since
9871 -- the call will be performed by their Init Proc; for example,
9872 -- calls initializing default values of discriminants or calls
9873 -- providing the initial value of record type components. Other
9874 -- full type declarations are processed because they may have
9875 -- calls that must be checked. For example:
9877 -- type T is array (1 .. Some_Function_Call (...)) of Some_Type;
9879 -- ??? More work needed here to handle the following case:
9881 -- type Rec is record
9882 -- F : String (1 .. <some complicated expression>);
9885 when N_Full_Type_Declaration =>
9886 if Is_Record_Type (Defining_Entity (Nod)) then
9890 -- Skip calls placed in subprogram specifications since function
9891 -- calls initializing default parameter values will be processed
9892 -- when the call to the subprogram is found (if the default actual
9893 -- parameter is required), and calls found in aspects will be
9894 -- processed when their corresponding pragma is found, or in the
9895 -- specific case of class-wide pre-/postconditions, when their
9896 -- helpers are found.
9898 when N_Procedure_Specification
9899 | N_Function_Specification
9903 when N_Abstract_Subprogram_Declaration
9907 | N_Enumeration_Representation_Clause
9908 | N_Enumeration_Type_Definition
9909 | N_Function_Instantiation
9910 | N_Freeze_Generic_Entity
9911 | N_Generic_Function_Renaming_Declaration
9912 | N_Generic_Package_Renaming_Declaration
9913 | N_Generic_Procedure_Renaming_Declaration
9914 | N_Generic_Package_Declaration
9915 | N_Generic_Subprogram_Declaration
9917 | N_Number_Declaration
9918 | N_Package_Instantiation
9919 | N_Package_Renaming_Declaration
9921 | N_Procedure_Instantiation
9922 | N_Protected_Type_Declaration
9923 | N_Record_Representation_Clause
9924 | N_Validate_Unchecked_Conversion
9925 | N_Variable_Reference_Marker
9926 | N_Use_Package_Clause
9939 procedure Check_Calls is new Traverse_Proc (Process_Node);
9941 -- Start of processing for Validate_Subprogram_Calls
9944 -- No action required if we are not generating code or compiling sources
9945 -- that have errors.
9947 if Serious_Errors_Detected > 0
9948 or else Operating_Mode /= Generate_Code
9954 end Validate_Subprogram_Calls;
9960 procedure Warn_BIP (Func_Call : Node_Id) is
9962 if Debug_Flag_Underscore_BB then
9963 Error_Msg_N ("build-in-place function call??", Func_Call);