1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2021, 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 Atree; use Atree;
27 with Aspects; use Aspects;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Errout; use Errout;
35 with Elists; use Elists;
36 with Expander; use Expander;
37 with Exp_Aggr; use Exp_Aggr;
38 with Exp_Atag; use Exp_Atag;
39 with Exp_Ch3; use Exp_Ch3;
40 with Exp_Ch7; use Exp_Ch7;
41 with Exp_Ch9; use Exp_Ch9;
42 with Exp_Dbug; use Exp_Dbug;
43 with Exp_Disp; use Exp_Disp;
44 with Exp_Dist; use Exp_Dist;
45 with Exp_Intr; use Exp_Intr;
46 with Exp_Pakd; use Exp_Pakd;
47 with Exp_Tss; use Exp_Tss;
48 with Exp_Util; use Exp_Util;
49 with Freeze; use Freeze;
50 with Inline; use Inline;
51 with Itypes; use Itypes;
53 with Namet; use Namet;
54 with Nlists; use Nlists;
55 with Nmake; use Nmake;
57 with Restrict; use Restrict;
58 with Rident; use Rident;
59 with Rtsfind; use Rtsfind;
61 with Sem_Aux; use Sem_Aux;
62 with Sem_Ch6; use Sem_Ch6;
63 with Sem_Ch8; use Sem_Ch8;
64 with Sem_Ch13; use Sem_Ch13;
65 with Sem_Dim; use Sem_Dim;
66 with Sem_Disp; use Sem_Disp;
67 with Sem_Dist; use Sem_Dist;
68 with Sem_Eval; use Sem_Eval;
69 with Sem_Mech; use Sem_Mech;
70 with Sem_Res; use Sem_Res;
71 with Sem_SCIL; use Sem_SCIL;
72 with Sem_Util; use Sem_Util;
73 with Sinfo; use Sinfo;
74 with Sinfo.Nodes; use Sinfo.Nodes;
75 with Sinfo.Utils; use Sinfo.Utils;
76 with Snames; use Snames;
77 with Stand; use Stand;
78 with Tbuild; use Tbuild;
79 with Uintp; use Uintp;
80 with Validsw; use Validsw;
82 package body Exp_Ch6 is
84 -- Suffix for BIP formals
86 BIP_Alloc_Suffix : constant String := "BIPalloc";
87 BIP_Storage_Pool_Suffix : constant String := "BIPstoragepool";
88 BIP_Finalization_Master_Suffix : constant String := "BIPfinalizationmaster";
89 BIP_Task_Master_Suffix : constant String := "BIPtaskmaster";
90 BIP_Activation_Chain_Suffix : constant String := "BIPactivationchain";
91 BIP_Object_Access_Suffix : constant String := "BIPaccess";
93 -----------------------
94 -- Local Subprograms --
95 -----------------------
97 procedure Add_Access_Actual_To_Build_In_Place_Call
98 (Function_Call : Node_Id;
99 Function_Id : Entity_Id;
100 Return_Object : Node_Id;
101 Is_Access : Boolean := False);
102 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
103 -- object name given by Return_Object and add the attribute to the end of
104 -- the actual parameter list associated with the build-in-place function
105 -- call denoted by Function_Call. However, if Is_Access is True, then
106 -- Return_Object is already an access expression, in which case it's passed
107 -- along directly to the build-in-place function. Finally, if Return_Object
108 -- is empty, then pass a null literal as the actual.
110 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
111 (Function_Call : Node_Id;
112 Function_Id : Entity_Id;
113 Alloc_Form : BIP_Allocation_Form := Unspecified;
114 Alloc_Form_Exp : Node_Id := Empty;
115 Pool_Actual : Node_Id := Make_Null (No_Location));
116 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
117 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
118 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
119 -- otherwise pass a literal corresponding to the Alloc_Form parameter
120 -- (which must not be Unspecified in that case). Pool_Actual is the
121 -- parameter to pass to BIP_Storage_Pool.
123 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
124 (Func_Call : Node_Id;
126 Ptr_Typ : Entity_Id := Empty;
127 Master_Exp : Node_Id := Empty);
128 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
129 -- finalization actions, add an actual parameter which is a pointer to the
130 -- finalization master of the caller. If Master_Exp is not Empty, then that
131 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
132 -- will result in an automatic "null" value for the actual.
134 procedure Add_Task_Actuals_To_Build_In_Place_Call
135 (Function_Call : Node_Id;
136 Function_Id : Entity_Id;
137 Master_Actual : Node_Id;
138 Chain : Node_Id := Empty);
139 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
140 -- contains tasks, add two actual parameters: the master, and a pointer to
141 -- the caller's activation chain. Master_Actual is the actual parameter
142 -- expression to pass for the master. In most cases, this is the current
143 -- master (_master). The two exceptions are: If the function call is the
144 -- initialization expression for an allocator, we pass the master of the
145 -- access type. If the function call is the initialization expression for a
146 -- return object, we pass along the master passed in by the caller. In most
147 -- contexts, the activation chain to pass is the local one, which is
148 -- indicated by No (Chain). However, in an allocator, the caller passes in
149 -- the activation Chain. Note: Master_Actual can be Empty, but only if
150 -- there are no tasks.
152 procedure Apply_CW_Accessibility_Check (Exp : Node_Id; Func : Entity_Id);
153 -- Ada 2005 (AI95-344): If the result type is class-wide, insert a check
154 -- that the level of the return expression's underlying type is not deeper
155 -- than the level of the master enclosing the function. Always generate the
156 -- check when the type of the return expression is class-wide, when it's a
157 -- type conversion, or when it's a formal parameter. Otherwise suppress the
158 -- check in the case where the return expression has a specific type whose
159 -- level is known not to be statically deeper than the result type of the
162 function Caller_Known_Size
163 (Func_Call : Node_Id;
164 Result_Subt : Entity_Id) return Boolean;
165 -- True if result subtype is definite, or has a size that does not require
166 -- secondary stack usage (i.e. no variant part or components whose type
167 -- depends on discriminants). In particular, untagged types with only
168 -- access discriminants do not require secondary stack use. Note we must
169 -- always use the secondary stack for dispatching-on-result calls.
171 function Check_BIP_Actuals
172 (Subp_Call : Node_Id;
173 Subp_Id : Entity_Id) return Boolean;
174 -- Given a subprogram call to the given subprogram return True if the
175 -- names of BIP extra actual and formal parameters match.
177 function Check_Number_Of_Actuals
178 (Subp_Call : Node_Id;
179 Subp_Id : Entity_Id) return Boolean;
180 -- Given a subprogram call to the given subprogram return True if the
181 -- number of actual parameters (including extra actuals) is correct.
183 procedure Check_Overriding_Operation (Subp : Entity_Id);
184 -- Subp is a dispatching operation. Check whether it may override an
185 -- inherited private operation, in which case its DT entry is that of
186 -- the hidden operation, not the one it may have received earlier.
187 -- This must be done before emitting the code to set the corresponding
188 -- DT to the address of the subprogram. The actual placement of Subp in
189 -- the proper place in the list of primitive operations is done in
190 -- Declare_Inherited_Private_Subprograms, which also has to deal with
191 -- implicit operations. This duplication is unavoidable for now???
193 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
194 -- This procedure is called only if the subprogram body N, whose spec
195 -- has the given entity Spec, contains a parameterless recursive call.
196 -- It attempts to generate runtime code to detect if this a case of
197 -- infinite recursion.
199 -- The body is scanned to determine dependencies. If the only external
200 -- dependencies are on a small set of scalar variables, then the values
201 -- of these variables are captured on entry to the subprogram, and if
202 -- the values are not changed for the call, we know immediately that
203 -- we have an infinite recursion.
205 procedure Expand_Actuals
208 Post_Call : out List_Id);
209 -- Return a list of actions to take place after the call in Post_Call. The
210 -- call will later be rewritten as an Expression_With_Actions, with the
211 -- Post_Call actions inserted, and the call inside.
213 -- For each actual of an in-out or out parameter which is a numeric (view)
214 -- conversion of the form T (A), where A denotes a variable, we insert the
217 -- Temp : T[ := T (A)];
219 -- prior to the call. Then we replace the actual with a reference to Temp,
220 -- and append the assignment:
222 -- A := TypeA (Temp);
224 -- after the call. Here TypeA is the actual type of variable A. For out
225 -- parameters, the initial declaration has no expression. If A is not an
226 -- entity name, we generate instead:
228 -- Var : TypeA renames A;
229 -- Temp : T := Var; -- omitting expression for out parameter.
231 -- Var := TypeA (Temp);
233 -- For other in-out parameters, we emit the required constraint checks
234 -- before and/or after the call.
236 -- For all parameter modes, actuals that denote components and slices of
237 -- packed arrays are expanded into suitable temporaries.
239 -- For nonscalar objects that are possibly unaligned, add call by copy code
240 -- (copy in for IN and IN OUT, copy out for OUT and IN OUT).
242 -- For OUT and IN OUT parameters, add predicate checks after the call
243 -- based on the predicates of the actual type.
245 procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id);
246 -- Does the main work of Expand_Call. Post_Call is as for Expand_Actuals.
248 procedure Expand_Ctrl_Function_Call (N : Node_Id);
249 -- N is a function call which returns a controlled object. Transform the
250 -- call into a temporary which retrieves the returned object from the
251 -- secondary stack using 'reference.
253 procedure Expand_Non_Function_Return (N : Node_Id);
254 -- Expand a simple return statement found in a procedure body, entry body,
255 -- accept statement, or an extended return statement. Note that all non-
256 -- function returns are simple return statements.
258 function Expand_Protected_Object_Reference
260 Scop : Entity_Id) return Node_Id;
262 procedure Expand_Protected_Subprogram_Call
266 -- A call to a protected subprogram within the protected object may appear
267 -- as a regular call. The list of actuals must be expanded to contain a
268 -- reference to the object itself, and the call becomes a call to the
269 -- corresponding protected subprogram.
271 procedure Expand_Simple_Function_Return (N : Node_Id);
272 -- Expand simple return from function. In the case where we are returning
273 -- from a function body this is called by Expand_N_Simple_Return_Statement.
275 function Has_BIP_Extra_Formal
277 Kind : BIP_Formal_Kind) return Boolean;
278 -- Given a frozen subprogram, subprogram type, entry or entry family,
279 -- return True if E has the BIP extra formal associated with Kind. It must
280 -- be invoked with a frozen entity or a subprogram type of a dispatching
281 -- call since we can only rely on the availability of the extra formals
282 -- on these entities.
284 procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id);
285 -- Insert the Post_Call list previously produced by routine Expand_Actuals
286 -- or Expand_Call_Helper into the tree.
288 procedure Replace_Renaming_Declaration_Id
290 Orig_Decl : Node_Id);
291 -- Replace the internal identifier of the new renaming declaration New_Decl
292 -- with the identifier of its original declaration Orig_Decl exchanging the
293 -- entities containing their defining identifiers to ensure the correct
294 -- replacement of the object declaration by the object renaming declaration
295 -- to avoid homograph conflicts (since the object declaration's defining
296 -- identifier was already entered in the current scope). The Next_Entity
297 -- links of the two entities are also swapped since the entities are part
298 -- of the return scope's entity list and the list structure would otherwise
299 -- be corrupted. The homonym chain is preserved as well.
301 procedure Rewrite_Function_Call_For_C (N : Node_Id);
302 -- When generating C code, replace a call to a function that returns an
303 -- array into the generated procedure with an additional out parameter.
305 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id);
306 -- N is a return statement for a function that returns its result on the
307 -- secondary stack. This sets the Sec_Stack_Needed_For_Return flag on the
308 -- function and all blocks and loops that the return statement is jumping
309 -- out of. This ensures that the secondary stack is not released; otherwise
310 -- the function result would be reclaimed before returning to the caller.
312 procedure Warn_BIP (Func_Call : Node_Id);
313 -- Give a warning on a build-in-place function call if the -gnatd_B switch
316 ----------------------------------------------
317 -- Add_Access_Actual_To_Build_In_Place_Call --
318 ----------------------------------------------
320 procedure Add_Access_Actual_To_Build_In_Place_Call
321 (Function_Call : Node_Id;
322 Function_Id : Entity_Id;
323 Return_Object : Node_Id;
324 Is_Access : Boolean := False)
326 Loc : constant Source_Ptr := Sloc (Function_Call);
327 Obj_Address : Node_Id;
328 Obj_Acc_Formal : Entity_Id;
331 -- Locate the implicit access parameter in the called function
333 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
335 -- If no return object is provided, then pass null
337 if not Present (Return_Object) then
338 Obj_Address := Make_Null (Loc);
339 Set_Parent (Obj_Address, Function_Call);
341 -- If Return_Object is already an expression of an access type, then use
342 -- it directly, since it must be an access value denoting the return
343 -- object, and couldn't possibly be the return object itself.
346 Obj_Address := Return_Object;
347 Set_Parent (Obj_Address, Function_Call);
349 -- Apply Unrestricted_Access to caller's return object
353 Make_Attribute_Reference (Loc,
354 Prefix => Return_Object,
355 Attribute_Name => Name_Unrestricted_Access);
357 Set_Parent (Return_Object, Obj_Address);
358 Set_Parent (Obj_Address, Function_Call);
361 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
363 -- Build the parameter association for the new actual and add it to the
364 -- end of the function's actuals.
366 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
367 end Add_Access_Actual_To_Build_In_Place_Call;
369 ------------------------------------------------------
370 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
371 ------------------------------------------------------
373 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
374 (Function_Call : Node_Id;
375 Function_Id : Entity_Id;
376 Alloc_Form : BIP_Allocation_Form := Unspecified;
377 Alloc_Form_Exp : Node_Id := Empty;
378 Pool_Actual : Node_Id := Make_Null (No_Location))
380 Loc : constant Source_Ptr := Sloc (Function_Call);
382 Alloc_Form_Actual : Node_Id;
383 Alloc_Form_Formal : Node_Id;
384 Pool_Formal : Node_Id;
387 -- Nothing to do when the size of the object is known, and the caller is
388 -- in charge of allocating it, and the callee doesn't unconditionally
389 -- require an allocation form (such as due to having a tagged result).
391 if not Needs_BIP_Alloc_Form (Function_Id) then
395 -- Locate the implicit allocation form parameter in the called function.
396 -- Maybe it would be better for each implicit formal of a build-in-place
397 -- function to have a flag or a Uint attribute to identify it. ???
399 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
401 if Present (Alloc_Form_Exp) then
402 pragma Assert (Alloc_Form = Unspecified);
404 Alloc_Form_Actual := Alloc_Form_Exp;
407 pragma Assert (Alloc_Form /= Unspecified);
410 Make_Integer_Literal (Loc,
411 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
414 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
416 -- Build the parameter association for the new actual and add it to the
417 -- end of the function's actuals.
419 Add_Extra_Actual_To_Call
420 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
422 -- Pass the Storage_Pool parameter. This parameter is omitted on ZFP as
423 -- those targets do not support pools.
425 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
426 Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool);
427 Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal));
428 Add_Extra_Actual_To_Call
429 (Function_Call, Pool_Formal, Pool_Actual);
431 end Add_Unconstrained_Actuals_To_Build_In_Place_Call;
433 -----------------------------------------------------------
434 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
435 -----------------------------------------------------------
437 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
438 (Func_Call : Node_Id;
440 Ptr_Typ : Entity_Id := Empty;
441 Master_Exp : Node_Id := Empty)
444 if not Needs_BIP_Finalization_Master (Func_Id) then
449 Formal : constant Entity_Id :=
450 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
451 Loc : constant Source_Ptr := Sloc (Func_Call);
454 Desig_Typ : Entity_Id;
457 -- If there is a finalization master actual, such as the implicit
458 -- finalization master of an enclosing build-in-place function,
459 -- then this must be added as an extra actual of the call.
461 if Present (Master_Exp) then
462 Actual := Master_Exp;
464 -- Case where the context does not require an actual master
466 elsif No (Ptr_Typ) then
467 Actual := Make_Null (Loc);
470 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
472 -- Check for a library-level access type whose designated type has
473 -- suppressed finalization or the access type is subject to pragma
474 -- No_Heap_Finalization. Such an access type lacks a master. Pass
475 -- a null actual to callee in order to signal a missing master.
477 if Is_Library_Level_Entity (Ptr_Typ)
478 and then (Finalize_Storage_Only (Desig_Typ)
479 or else No_Heap_Finalization (Ptr_Typ))
481 Actual := Make_Null (Loc);
483 -- Types in need of finalization actions
485 elsif Needs_Finalization (Desig_Typ) then
487 -- The general mechanism of creating finalization masters for
488 -- anonymous access types is disabled by default, otherwise
489 -- finalization masters will pop all over the place. Such types
490 -- use context-specific masters.
492 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
493 and then No (Finalization_Master (Ptr_Typ))
495 Build_Anonymous_Master (Ptr_Typ);
498 -- Access-to-controlled types should always have a master
500 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
503 Make_Attribute_Reference (Loc,
505 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
506 Attribute_Name => Name_Unrestricted_Access);
511 Actual := Make_Null (Loc);
515 Analyze_And_Resolve (Actual, Etype (Formal));
517 -- Build the parameter association for the new actual and add it to
518 -- the end of the function's actuals.
520 Add_Extra_Actual_To_Call (Func_Call, Formal, Actual);
522 end Add_Finalization_Master_Actual_To_Build_In_Place_Call;
524 ------------------------------
525 -- Add_Extra_Actual_To_Call --
526 ------------------------------
528 procedure Add_Extra_Actual_To_Call
529 (Subprogram_Call : Node_Id;
530 Extra_Formal : Entity_Id;
531 Extra_Actual : Node_Id)
533 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
534 Param_Assoc : Node_Id;
538 Make_Parameter_Association (Loc,
539 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
540 Explicit_Actual_Parameter => Extra_Actual);
542 Set_Parent (Param_Assoc, Subprogram_Call);
543 Set_Parent (Extra_Actual, Param_Assoc);
545 if Present (Parameter_Associations (Subprogram_Call)) then
546 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
547 N_Parameter_Association
550 -- Find last named actual, and append
555 L := First_Actual (Subprogram_Call);
556 while Present (L) loop
557 if No (Next_Actual (L)) then
558 Set_Next_Named_Actual (Parent (L), Extra_Actual);
566 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
569 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
572 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
573 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
575 end Add_Extra_Actual_To_Call;
577 ---------------------------------------------
578 -- Add_Task_Actuals_To_Build_In_Place_Call --
579 ---------------------------------------------
581 procedure Add_Task_Actuals_To_Build_In_Place_Call
582 (Function_Call : Node_Id;
583 Function_Id : Entity_Id;
584 Master_Actual : Node_Id;
585 Chain : Node_Id := Empty)
587 Loc : constant Source_Ptr := Sloc (Function_Call);
589 Chain_Actual : Node_Id;
590 Chain_Formal : Node_Id;
591 Master_Formal : Node_Id;
594 -- No such extra parameters are needed if there are no tasks
596 if not Needs_BIP_Task_Actuals (Function_Id) then
600 Actual := Master_Actual;
602 -- Use a dummy _master actual in case of No_Task_Hierarchy
604 if Restriction_Active (No_Task_Hierarchy) then
605 Actual := Make_Integer_Literal (Loc, Library_Task_Level);
607 -- In the case where we use the master associated with an access type,
608 -- the actual is an entity and requires an explicit reference.
610 elsif Nkind (Actual) = N_Defining_Identifier then
611 Actual := New_Occurrence_Of (Actual, Loc);
614 -- Locate the implicit master parameter in the called function
616 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master);
617 Analyze_And_Resolve (Actual, Etype (Master_Formal));
619 -- Build the parameter association for the new actual and add it to the
620 -- end of the function's actuals.
622 Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual);
624 -- Locate the implicit activation chain parameter in the called function
627 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
629 -- Create the actual which is a pointer to the current activation chain
633 Make_Attribute_Reference (Loc,
634 Prefix => Make_Identifier (Loc, Name_uChain),
635 Attribute_Name => Name_Unrestricted_Access);
637 -- Allocator case; make a reference to the Chain passed in by the caller
641 Make_Attribute_Reference (Loc,
642 Prefix => New_Occurrence_Of (Chain, Loc),
643 Attribute_Name => Name_Unrestricted_Access);
646 Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal));
648 -- Build the parameter association for the new actual and add it to the
649 -- end of the function's actuals.
651 Add_Extra_Actual_To_Call (Function_Call, Chain_Formal, Chain_Actual);
652 end Add_Task_Actuals_To_Build_In_Place_Call;
654 ----------------------------------
655 -- Apply_CW_Accessibility_Check --
656 ----------------------------------
658 procedure Apply_CW_Accessibility_Check (Exp : Node_Id; Func : Entity_Id) is
659 Loc : constant Source_Ptr := Sloc (Exp);
662 if Ada_Version >= Ada_2005
663 and then Tagged_Type_Expansion
664 and then not Scope_Suppress.Suppress (Accessibility_Check)
666 (Is_Class_Wide_Type (Etype (Exp))
667 or else Nkind (Exp) in
668 N_Type_Conversion | N_Unchecked_Type_Conversion
669 or else (Is_Entity_Name (Exp)
670 and then Is_Formal (Entity (Exp)))
671 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
672 Scope_Depth (Enclosing_Dynamic_Scope (Func)))
678 -- Ada 2005 (AI-251): In class-wide interface objects we displace
679 -- "this" to reference the base of the object. This is required to
680 -- get access to the TSD of the object.
682 if Is_Class_Wide_Type (Etype (Exp))
683 and then Is_Interface (Etype (Exp))
685 -- If the expression is an explicit dereference then we can
686 -- directly displace the pointer to reference the base of
689 if Nkind (Exp) = N_Explicit_Dereference then
691 Make_Explicit_Dereference (Loc,
693 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
694 Make_Function_Call (Loc,
696 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
697 Parameter_Associations => New_List (
698 Unchecked_Convert_To (RTE (RE_Address),
699 Duplicate_Subexpr (Prefix (Exp)))))));
701 -- Similar case to the previous one but the expression is a
702 -- renaming of an explicit dereference.
704 elsif Nkind (Exp) = N_Identifier
705 and then Present (Renamed_Object (Entity (Exp)))
706 and then Nkind (Renamed_Object (Entity (Exp)))
707 = N_Explicit_Dereference
710 Make_Explicit_Dereference (Loc,
712 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
713 Make_Function_Call (Loc,
715 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
716 Parameter_Associations => New_List (
717 Unchecked_Convert_To (RTE (RE_Address),
720 (Renamed_Object (Entity (Exp)))))))));
722 -- Common case: obtain the address of the actual object and
723 -- displace the pointer to reference the base of the object.
727 Make_Explicit_Dereference (Loc,
729 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
730 Make_Function_Call (Loc,
732 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
733 Parameter_Associations => New_List (
734 Make_Attribute_Reference (Loc,
735 Prefix => Duplicate_Subexpr (Exp),
736 Attribute_Name => Name_Address)))));
740 Make_Attribute_Reference (Loc,
741 Prefix => Duplicate_Subexpr (Exp),
742 Attribute_Name => Name_Tag);
745 -- CodePeer does not do anything useful with
746 -- Ada.Tags.Type_Specific_Data components.
748 if not CodePeer_Mode then
750 Make_Raise_Program_Error (Loc,
753 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
755 Make_Integer_Literal (Loc,
756 Scope_Depth (Enclosing_Dynamic_Scope (Func)))),
757 Reason => PE_Accessibility_Check_Failed));
761 end Apply_CW_Accessibility_Check;
763 -----------------------
764 -- BIP_Formal_Suffix --
765 -----------------------
767 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
770 when BIP_Alloc_Form =>
771 return BIP_Alloc_Suffix;
773 when BIP_Storage_Pool =>
774 return BIP_Storage_Pool_Suffix;
776 when BIP_Finalization_Master =>
777 return BIP_Finalization_Master_Suffix;
779 when BIP_Task_Master =>
780 return BIP_Task_Master_Suffix;
782 when BIP_Activation_Chain =>
783 return BIP_Activation_Chain_Suffix;
785 when BIP_Object_Access =>
786 return BIP_Object_Access_Suffix;
788 end BIP_Formal_Suffix;
790 ---------------------
791 -- BIP_Suffix_Kind --
792 ---------------------
794 function BIP_Suffix_Kind (E : Entity_Id) return BIP_Formal_Kind is
795 Nam : constant String := Get_Name_String (Chars (E));
797 function Has_Suffix (Suffix : String) return Boolean;
798 -- Return True if Nam has suffix Suffix
800 function Has_Suffix (Suffix : String) return Boolean is
801 Len : constant Natural := Suffix'Length;
803 return Nam'Length > Len
804 and then Nam (Nam'Last - Len + 1 .. Nam'Last) = Suffix;
807 -- Start of processing for BIP_Suffix_Kind
810 if Has_Suffix (BIP_Alloc_Suffix) then
811 return BIP_Alloc_Form;
813 elsif Has_Suffix (BIP_Storage_Pool_Suffix) then
814 return BIP_Storage_Pool;
816 elsif Has_Suffix (BIP_Finalization_Master_Suffix) then
817 return BIP_Finalization_Master;
819 elsif Has_Suffix (BIP_Task_Master_Suffix) then
820 return BIP_Task_Master;
822 elsif Has_Suffix (BIP_Activation_Chain_Suffix) then
823 return BIP_Activation_Chain;
825 elsif Has_Suffix (BIP_Object_Access_Suffix) then
826 return BIP_Object_Access;
833 ---------------------------
834 -- Build_In_Place_Formal --
835 ---------------------------
837 function Build_In_Place_Formal
839 Kind : BIP_Formal_Kind) return Entity_Id
841 Extra_Formal : Entity_Id := Extra_Formals (Func);
842 Formal_Suffix : constant String := BIP_Formal_Suffix (Kind);
845 -- Maybe it would be better for each implicit formal of a build-in-place
846 -- function to have a flag or a Uint attribute to identify it. ???
848 -- The return type in the function declaration may have been a limited
849 -- view, and the extra formals for the function were not generated at
850 -- that point. At the point of call the full view must be available and
851 -- the extra formals can be created.
853 if No (Extra_Formal) then
854 Create_Extra_Formals (Func);
855 Extra_Formal := Extra_Formals (Func);
858 -- We search for a formal with a matching suffix. We can't search
859 -- for the full name, because of the code at the end of Sem_Ch6.-
860 -- Create_Extra_Formals, which copies the Extra_Formals over to
861 -- the Alias of an instance, which will cause the formals to have
862 -- "incorrect" names.
865 pragma Assert (Present (Extra_Formal));
867 Name : constant String := Get_Name_String (Chars (Extra_Formal));
869 exit when Name'Length >= Formal_Suffix'Length
870 and then Formal_Suffix =
871 Name (Name'Last - Formal_Suffix'Length + 1 .. Name'Last);
874 Next_Formal_With_Extras (Extra_Formal);
878 end Build_In_Place_Formal;
880 -------------------------------
881 -- Build_Procedure_Body_Form --
882 -------------------------------
884 function Build_Procedure_Body_Form
885 (Func_Id : Entity_Id;
886 Func_Body : Node_Id) return Node_Id
888 Loc : constant Source_Ptr := Sloc (Func_Body);
890 Proc_Decl : constant Node_Id := Prev (Unit_Declaration_Node (Func_Id));
891 -- It is assumed that the node before the declaration of the
892 -- corresponding subprogram spec is the declaration of the procedure
895 Proc_Id : constant Entity_Id := Defining_Entity (Proc_Decl);
897 procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id);
898 -- Replace each return statement found in the list Stmts with an
899 -- assignment of the return expression to parameter Param_Id.
901 ---------------------
902 -- Replace_Returns --
903 ---------------------
905 procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id) is
909 Stmt := First (Stmts);
910 while Present (Stmt) loop
911 if Nkind (Stmt) = N_Block_Statement then
912 Replace_Returns (Param_Id,
913 Statements (Handled_Statement_Sequence (Stmt)));
915 elsif Nkind (Stmt) = N_Case_Statement then
919 Alt := First (Alternatives (Stmt));
920 while Present (Alt) loop
921 Replace_Returns (Param_Id, Statements (Alt));
926 elsif Nkind (Stmt) = N_Extended_Return_Statement then
928 Ret_Obj : constant Entity_Id :=
930 (First (Return_Object_Declarations (Stmt)));
931 Assign : constant Node_Id :=
932 Make_Assignment_Statement (Sloc (Stmt),
934 New_Occurrence_Of (Param_Id, Loc),
936 New_Occurrence_Of (Ret_Obj, Sloc (Stmt)));
940 -- The extended return may just contain the declaration
942 if Present (Handled_Statement_Sequence (Stmt)) then
943 Stmts := Statements (Handled_Statement_Sequence (Stmt));
948 Set_Assignment_OK (Name (Assign));
951 Make_Block_Statement (Sloc (Stmt),
953 Return_Object_Declarations (Stmt),
954 Handled_Statement_Sequence =>
955 Make_Handled_Sequence_Of_Statements (Loc,
956 Statements => Stmts)));
958 Replace_Returns (Param_Id, Stmts);
960 Append_To (Stmts, Assign);
961 Append_To (Stmts, Make_Simple_Return_Statement (Loc));
964 elsif Nkind (Stmt) = N_If_Statement then
965 Replace_Returns (Param_Id, Then_Statements (Stmt));
966 Replace_Returns (Param_Id, Else_Statements (Stmt));
971 Part := First (Elsif_Parts (Stmt));
972 while Present (Part) loop
973 Replace_Returns (Param_Id, Then_Statements (Part));
978 elsif Nkind (Stmt) = N_Loop_Statement then
979 Replace_Returns (Param_Id, Statements (Stmt));
981 elsif Nkind (Stmt) = N_Simple_Return_Statement then
988 Make_Assignment_Statement (Sloc (Stmt),
989 Name => New_Occurrence_Of (Param_Id, Loc),
990 Expression => Relocate_Node (Expression (Stmt))));
992 Insert_After (Stmt, Make_Simple_Return_Statement (Loc));
994 -- Skip the added return
1001 end Replace_Returns;
1008 -- Start of processing for Build_Procedure_Body_Form
1011 -- This routine replaces the original function body:
1013 -- function F (...) return Array_Typ is
1016 -- return Something;
1019 -- with the following:
1021 -- procedure P (..., Result : out Array_Typ) is
1024 -- Result := Something;
1028 Statements (Handled_Statement_Sequence (Func_Body));
1029 Replace_Returns (Last_Entity (Proc_Id), Stmts);
1032 Make_Subprogram_Body (Loc,
1034 Copy_Subprogram_Spec (Specification (Proc_Decl)),
1035 Declarations => Declarations (Func_Body),
1036 Handled_Statement_Sequence =>
1037 Make_Handled_Sequence_Of_Statements (Loc,
1038 Statements => Stmts));
1040 -- If the function is a generic instance, so is the new procedure.
1041 -- Set flag accordingly so that the proper renaming declarations are
1044 Set_Is_Generic_Instance (Proc_Id, Is_Generic_Instance (Func_Id));
1046 end Build_Procedure_Body_Form;
1048 -----------------------
1049 -- Caller_Known_Size --
1050 -----------------------
1052 function Caller_Known_Size
1053 (Func_Call : Node_Id;
1054 Result_Subt : Entity_Id) return Boolean
1058 (Is_Definite_Subtype (Underlying_Type (Result_Subt))
1059 and then No (Controlling_Argument (Func_Call)))
1060 or else not Requires_Transient_Scope (Underlying_Type (Result_Subt));
1061 end Caller_Known_Size;
1063 -----------------------
1064 -- Check_BIP_Actuals --
1065 -----------------------
1067 function Check_BIP_Actuals
1068 (Subp_Call : Node_Id;
1069 Subp_Id : Entity_Id) return Boolean
1075 pragma Assert (Nkind (Subp_Call) in N_Entry_Call_Statement
1077 | N_Procedure_Call_Statement);
1079 Formal := First_Formal_With_Extras (Subp_Id);
1080 Actual := First_Actual (Subp_Call);
1082 while Present (Formal) and then Present (Actual) loop
1083 if Is_Build_In_Place_Entity (Formal)
1084 and then Nkind (Actual) = N_Identifier
1085 and then Is_Build_In_Place_Entity (Entity (Actual))
1086 and then BIP_Suffix_Kind (Formal)
1087 /= BIP_Suffix_Kind (Entity (Actual))
1092 Next_Formal_With_Extras (Formal);
1093 Next_Actual (Actual);
1096 return No (Formal) and then No (Actual);
1097 end Check_BIP_Actuals;
1099 -----------------------------
1100 -- Check_Number_Of_Actuals --
1101 -----------------------------
1103 function Check_Number_Of_Actuals
1104 (Subp_Call : Node_Id;
1105 Subp_Id : Entity_Id) return Boolean
1111 pragma Assert (Nkind (Subp_Call) in N_Entry_Call_Statement
1113 | N_Procedure_Call_Statement);
1115 Formal := First_Formal_With_Extras (Subp_Id);
1116 Actual := First_Actual (Subp_Call);
1118 while Present (Formal) and then Present (Actual) loop
1119 Next_Formal_With_Extras (Formal);
1120 Next_Actual (Actual);
1123 return No (Formal) and then No (Actual);
1124 end Check_Number_Of_Actuals;
1126 --------------------------------
1127 -- Check_Overriding_Operation --
1128 --------------------------------
1130 procedure Check_Overriding_Operation (Subp : Entity_Id) is
1131 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
1132 Op_List : constant Elist_Id := Primitive_Operations (Typ);
1134 Prim_Op : Entity_Id;
1138 if Is_Derived_Type (Typ)
1139 and then not Is_Private_Type (Typ)
1140 and then In_Open_Scopes (Scope (Etype (Typ)))
1141 and then Is_Base_Type (Typ)
1143 -- Subp overrides an inherited private operation if there is an
1144 -- inherited operation with a different name than Subp (see
1145 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
1146 -- same name as Subp.
1148 Op_Elmt := First_Elmt (Op_List);
1149 while Present (Op_Elmt) loop
1150 Prim_Op := Node (Op_Elmt);
1151 Par_Op := Alias (Prim_Op);
1154 and then not Comes_From_Source (Prim_Op)
1155 and then Chars (Prim_Op) /= Chars (Par_Op)
1156 and then Chars (Par_Op) = Chars (Subp)
1157 and then Is_Hidden (Par_Op)
1158 and then Type_Conformant (Prim_Op, Subp)
1160 Set_DT_Position_Value (Subp, DT_Position (Prim_Op));
1163 Next_Elmt (Op_Elmt);
1166 end Check_Overriding_Operation;
1168 -------------------------------
1169 -- Detect_Infinite_Recursion --
1170 -------------------------------
1172 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
1173 Loc : constant Source_Ptr := Sloc (N);
1175 Var_List : constant Elist_Id := New_Elmt_List;
1176 -- List of globals referenced by body of procedure
1178 Call_List : constant Elist_Id := New_Elmt_List;
1179 -- List of recursive calls in body of procedure
1181 Shad_List : constant Elist_Id := New_Elmt_List;
1182 -- List of entity id's for entities created to capture the value of
1183 -- referenced globals on entry to the procedure.
1185 Scop : constant Uint := Scope_Depth (Spec);
1186 -- This is used to record the scope depth of the current procedure, so
1187 -- that we can identify global references.
1189 Max_Vars : constant := 4;
1190 -- Do not test more than four global variables
1192 Count_Vars : Natural := 0;
1193 -- Count variables found so far
1205 function Process (Nod : Node_Id) return Traverse_Result;
1206 -- Function to traverse the subprogram body (using Traverse_Func)
1212 function Process (Nod : Node_Id) return Traverse_Result is
1216 if Nkind (Nod) = N_Procedure_Call_Statement then
1218 -- Case of one of the detected recursive calls
1220 if Is_Entity_Name (Name (Nod))
1221 and then Has_Recursive_Call (Entity (Name (Nod)))
1222 and then Entity (Name (Nod)) = Spec
1224 Append_Elmt (Nod, Call_List);
1227 -- Any other procedure call may have side effects
1233 -- A call to a pure function can always be ignored
1235 elsif Nkind (Nod) = N_Function_Call
1236 and then Is_Entity_Name (Name (Nod))
1237 and then Is_Pure (Entity (Name (Nod)))
1241 -- Case of an identifier reference
1243 elsif Nkind (Nod) = N_Identifier then
1244 Ent := Entity (Nod);
1246 -- If no entity, then ignore the reference
1248 -- Not clear why this can happen. To investigate, remove this
1249 -- test and look at the crash that occurs here in 3401-004 ???
1254 -- Ignore entities with no Scope, again not clear how this
1255 -- can happen, to investigate, look at 4108-008 ???
1257 elsif No (Scope (Ent)) then
1260 -- Ignore the reference if not to a more global object
1262 elsif Scope_Depth (Scope (Ent)) >= Scop then
1265 -- References to types, exceptions and constants are always OK
1268 or else Ekind (Ent) = E_Exception
1269 or else Ekind (Ent) = E_Constant
1273 -- If other than a non-volatile scalar variable, we have some
1274 -- kind of global reference (e.g. to a function) that we cannot
1275 -- deal with so we forget the attempt.
1277 elsif Ekind (Ent) /= E_Variable
1278 or else not Is_Scalar_Type (Etype (Ent))
1279 or else Treat_As_Volatile (Ent)
1283 -- Otherwise we have a reference to a global scalar
1286 -- Loop through global entities already detected
1288 Elm := First_Elmt (Var_List);
1290 -- If not detected before, record this new global reference
1293 Count_Vars := Count_Vars + 1;
1295 if Count_Vars <= Max_Vars then
1296 Append_Elmt (Entity (Nod), Var_List);
1303 -- If recorded before, ignore
1305 elsif Node (Elm) = Entity (Nod) then
1308 -- Otherwise keep looking
1318 -- For all other node kinds, recursively visit syntactic children
1325 function Traverse_Body is new Traverse_Func (Process);
1327 -- Start of processing for Detect_Infinite_Recursion
1330 -- Do not attempt detection in No_Implicit_Conditional mode, since we
1331 -- won't be able to generate the code to handle the recursion in any
1334 if Restriction_Active (No_Implicit_Conditionals) then
1338 -- Otherwise do traversal and quit if we get abandon signal
1340 if Traverse_Body (N) = Abandon then
1343 -- We must have a call, since Has_Recursive_Call was set. If not just
1344 -- ignore (this is only an error check, so if we have a funny situation,
1345 -- due to bugs or errors, we do not want to bomb).
1347 elsif Is_Empty_Elmt_List (Call_List) then
1351 -- Here is the case where we detect recursion at compile time
1353 -- Push our current scope for analyzing the declarations and code that
1354 -- we will insert for the checking.
1358 -- This loop builds temporary variables for each of the referenced
1359 -- globals, so that at the end of the loop the list Shad_List contains
1360 -- these temporaries in one-to-one correspondence with the elements in
1364 Elm := First_Elmt (Var_List);
1365 while Present (Elm) loop
1367 Ent := Make_Temporary (Loc, 'S');
1368 Append_Elmt (Ent, Shad_List);
1370 -- Insert a declaration for this temporary at the start of the
1371 -- declarations for the procedure. The temporaries are declared as
1372 -- constant objects initialized to the current values of the
1373 -- corresponding temporaries.
1376 Make_Object_Declaration (Loc,
1377 Defining_Identifier => Ent,
1378 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
1379 Constant_Present => True,
1380 Expression => New_Occurrence_Of (Var, Loc));
1383 Prepend (Decl, Declarations (N));
1385 Insert_After (Last, Decl);
1393 -- Loop through calls
1395 Call := First_Elmt (Call_List);
1396 while Present (Call) loop
1398 -- Build a predicate expression of the form
1401 -- and then global1 = temp1
1402 -- and then global2 = temp2
1405 -- This predicate determines if any of the global values
1406 -- referenced by the procedure have changed since the
1407 -- current call, if not an infinite recursion is assured.
1409 Test := New_Occurrence_Of (Standard_True, Loc);
1411 Elm1 := First_Elmt (Var_List);
1412 Elm2 := First_Elmt (Shad_List);
1413 while Present (Elm1) loop
1419 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
1420 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
1426 -- Now we replace the call with the sequence
1428 -- if no-changes (see above) then
1429 -- raise Storage_Error;
1434 Rewrite (Node (Call),
1435 Make_If_Statement (Loc,
1437 Then_Statements => New_List (
1438 Make_Raise_Storage_Error (Loc,
1439 Reason => SE_Infinite_Recursion)),
1441 Else_Statements => New_List (
1442 Relocate_Node (Node (Call)))));
1444 Analyze (Node (Call));
1449 -- Remove temporary scope stack entry used for analysis
1452 end Detect_Infinite_Recursion;
1454 --------------------
1455 -- Expand_Actuals --
1456 --------------------
1458 procedure Expand_Actuals
1461 Post_Call : out List_Id)
1463 Loc : constant Source_Ptr := Sloc (N);
1467 E_Actual : Entity_Id;
1468 E_Formal : Entity_Id;
1470 procedure Add_Call_By_Copy_Code;
1471 -- For cases where the parameter must be passed by copy, this routine
1472 -- generates a temporary variable into which the actual is copied and
1473 -- then passes this as the parameter. For an OUT or IN OUT parameter,
1474 -- an assignment is also generated to copy the result back. The call
1475 -- also takes care of any constraint checks required for the type
1476 -- conversion case (on both the way in and the way out).
1478 procedure Add_Simple_Call_By_Copy_Code (Force : Boolean);
1479 -- This is similar to the above, but is used in cases where we know
1480 -- that all that is needed is to simply create a temporary and copy
1481 -- the value in and out of the temporary. If Force is True, then the
1482 -- procedure may disregard legality considerations.
1484 -- ??? We need to do the copy for a bit-packed array because this is
1485 -- where the rewriting into a mask-and-shift sequence is done. But of
1486 -- course this may break the program if it expects bits to be really
1487 -- passed by reference. That's what we have done historically though.
1489 procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id);
1490 -- Perform copy-back for actual parameter Act which denotes a validation
1493 procedure Check_Fortran_Logical;
1494 -- A value of type Logical that is passed through a formal parameter
1495 -- must be normalized because .TRUE. usually does not have the same
1496 -- representation as True. We assume that .FALSE. = False = 0.
1497 -- What about functions that return a logical type ???
1499 function Is_Legal_Copy return Boolean;
1500 -- Check that an actual can be copied before generating the temporary
1501 -- to be used in the call. If the formal is of a by_reference type or
1502 -- is aliased, then the program is illegal (this can only happen in
1503 -- the presence of representation clauses that force a misalignment)
1504 -- If the formal is a by_reference parameter imposed by a DEC pragma,
1505 -- emit a warning that this might lead to unaligned arguments.
1507 function Make_Var (Actual : Node_Id) return Entity_Id;
1508 -- Returns an entity that refers to the given actual parameter, Actual
1509 -- (not including any type conversion). If Actual is an entity name,
1510 -- then this entity is returned unchanged, otherwise a renaming is
1511 -- created to provide an entity for the actual.
1513 procedure Reset_Packed_Prefix;
1514 -- The expansion of a packed array component reference is delayed in
1515 -- the context of a call. Now we need to complete the expansion, so we
1516 -- unmark the analyzed bits in all prefixes.
1518 function Requires_Atomic_Or_Volatile_Copy return Boolean;
1519 -- Returns whether a copy is required as per RM C.6(19) and gives a
1520 -- warning in this case.
1522 ---------------------------
1523 -- Add_Call_By_Copy_Code --
1524 ---------------------------
1526 procedure Add_Call_By_Copy_Code is
1529 F_Typ : Entity_Id := Etype (Formal);
1537 if not Is_Legal_Copy then
1541 Temp := Make_Temporary (Loc, 'T', Actual);
1543 -- Handle formals whose type comes from the limited view
1545 if From_Limited_With (F_Typ)
1546 and then Has_Non_Limited_View (F_Typ)
1548 F_Typ := Non_Limited_View (F_Typ);
1551 -- Use formal type for temp, unless formal type is an unconstrained
1552 -- array, in which case we don't have to worry about bounds checks,
1553 -- and we use the actual type, since that has appropriate bounds.
1555 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1556 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1558 Indic := New_Occurrence_Of (F_Typ, Loc);
1561 -- The new code will be properly analyzed below and the setting of
1562 -- the Do_Range_Check flag recomputed so remove the obsolete one.
1564 Set_Do_Range_Check (Actual, False);
1566 if Nkind (Actual) = N_Type_Conversion then
1567 Set_Do_Range_Check (Expression (Actual), False);
1569 V_Typ := Etype (Expression (Actual));
1571 -- If the formal is an (in-)out parameter, capture the name
1572 -- of the variable in order to build the post-call assignment.
1574 Var := Make_Var (Expression (Actual));
1576 Crep := not Has_Compatible_Representation
1577 (Target_Type => F_Typ,
1578 Operand_Type => Etype (Expression (Actual)));
1581 V_Typ := Etype (Actual);
1582 Var := Make_Var (Actual);
1586 -- Setup initialization for case of in out parameter, or an out
1587 -- parameter where the formal is an unconstrained array (in the
1588 -- latter case, we have to pass in an object with bounds).
1590 -- If this is an out parameter, the initial copy is wasteful, so as
1591 -- an optimization for the one-dimensional case we extract the
1592 -- bounds of the actual and build an uninitialized temporary of the
1595 -- If the formal is an out parameter with discriminants, the
1596 -- discriminants must be captured even if the rest of the object
1597 -- is in principle uninitialized, because the discriminants may
1598 -- be read by the called subprogram.
1600 if Ekind (Formal) = E_In_Out_Parameter
1601 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1602 or else Has_Discriminants (F_Typ)
1604 if Nkind (Actual) = N_Type_Conversion then
1605 if Conversion_OK (Actual) then
1606 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1608 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1611 elsif Ekind (Formal) = E_Out_Parameter
1612 and then Is_Array_Type (F_Typ)
1613 and then Number_Dimensions (F_Typ) = 1
1614 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1616 -- Actual is a one-dimensional array or slice, and the type
1617 -- requires no initialization. Create a temporary of the
1618 -- right size, but do not copy actual into it (optimization).
1622 Make_Subtype_Indication (Loc,
1623 Subtype_Mark => New_Occurrence_Of (F_Typ, Loc),
1625 Make_Index_Or_Discriminant_Constraint (Loc,
1626 Constraints => New_List (
1629 Make_Attribute_Reference (Loc,
1630 Prefix => New_Occurrence_Of (Var, Loc),
1631 Attribute_Name => Name_First),
1633 Make_Attribute_Reference (Loc,
1634 Prefix => New_Occurrence_Of (Var, Loc),
1635 Attribute_Name => Name_Last)))));
1638 Init := New_Occurrence_Of (Var, Loc);
1641 -- An initialization is created for packed conversions as
1642 -- actuals for out parameters to enable Make_Object_Declaration
1643 -- to determine the proper subtype for N_Node. Note that this
1644 -- is wasteful because the extra copying on the call side is
1645 -- not required for such out parameters. ???
1647 elsif Ekind (Formal) = E_Out_Parameter
1648 and then Nkind (Actual) = N_Type_Conversion
1649 and then (Is_Bit_Packed_Array (F_Typ)
1651 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1653 if Conversion_OK (Actual) then
1654 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1656 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1659 elsif Ekind (Formal) = E_In_Parameter then
1661 -- Handle the case in which the actual is a type conversion
1663 if Nkind (Actual) = N_Type_Conversion then
1664 if Conversion_OK (Actual) then
1665 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1667 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1670 Init := New_Occurrence_Of (Var, Loc);
1673 -- Access types are passed in without checks, but if a copy-back is
1674 -- required for a null-excluding check on an in-out or out parameter,
1675 -- then the initial value is that of the actual.
1677 elsif Is_Access_Type (E_Formal)
1678 and then Can_Never_Be_Null (Etype (Actual))
1679 and then not Can_Never_Be_Null (E_Formal)
1681 Init := New_Occurrence_Of (Var, Loc);
1683 -- View conversions when the formal type has the Default_Value aspect
1684 -- require passing in the value of the conversion's operand. The type
1685 -- of that operand also has Default_Value, as required by AI12-0074
1686 -- (RM 6.4.1(5.3/4)). The subtype denoted by the subtype_indication
1687 -- is changed to the base type of the formal subtype, to ensure that
1688 -- the actual's value can be assigned without a constraint check
1689 -- (note that no check is done on passing to an out parameter). Also
1690 -- note that the two types necessarily share the same ancestor type,
1691 -- as required by 6.4.1(5.2/4), so underlying base types will match.
1693 elsif Ekind (Formal) = E_Out_Parameter
1694 and then Is_Scalar_Type (Etype (F_Typ))
1695 and then Nkind (Actual) = N_Type_Conversion
1696 and then Present (Default_Aspect_Value (Etype (F_Typ)))
1698 Indic := New_Occurrence_Of (Base_Type (F_Typ), Loc);
1700 (Base_Type (F_Typ), New_Occurrence_Of (Var, Loc));
1707 Make_Object_Declaration (Loc,
1708 Defining_Identifier => Temp,
1709 Object_Definition => Indic,
1710 Expression => Init);
1711 Set_Assignment_OK (N_Node);
1712 Insert_Action (N, N_Node);
1714 -- Now, normally the deal here is that we use the defining
1715 -- identifier created by that object declaration. There is
1716 -- one exception to this. In the change of representation case
1717 -- the above declaration will end up looking like:
1719 -- temp : type := identifier;
1721 -- And in this case we might as well use the identifier directly
1722 -- and eliminate the temporary. Note that the analysis of the
1723 -- declaration was not a waste of time in that case, since it is
1724 -- what generated the necessary change of representation code. If
1725 -- the change of representation introduced additional code, as in
1726 -- a fixed-integer conversion, the expression is not an identifier
1727 -- and must be kept.
1730 and then Present (Expression (N_Node))
1731 and then Is_Entity_Name (Expression (N_Node))
1733 Temp := Entity (Expression (N_Node));
1734 Rewrite (N_Node, Make_Null_Statement (Loc));
1737 -- For IN parameter, all we do is to replace the actual
1739 if Ekind (Formal) = E_In_Parameter then
1740 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1743 -- Processing for OUT or IN OUT parameter
1746 -- Kill current value indications for the temporary variable we
1747 -- created, since we just passed it as an OUT parameter.
1749 Kill_Current_Values (Temp);
1750 Set_Is_Known_Valid (Temp, False);
1751 Set_Is_True_Constant (Temp, False);
1753 -- If type conversion, use reverse conversion on exit
1755 if Nkind (Actual) = N_Type_Conversion then
1756 if Conversion_OK (Actual) then
1757 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1759 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1762 Expr := New_Occurrence_Of (Temp, Loc);
1765 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1768 -- If the actual is a conversion of a packed reference, it may
1769 -- already have been expanded by Remove_Side_Effects, and the
1770 -- resulting variable is a temporary which does not designate
1771 -- the proper out-parameter, which may not be addressable. In
1772 -- that case, generate an assignment to the original expression
1773 -- (before expansion of the packed reference) so that the proper
1774 -- expansion of assignment to a packed component can take place.
1781 if Is_Renaming_Of_Object (Var)
1782 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1783 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1784 = N_Indexed_Component
1786 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1788 Obj := Renamed_Object (Var);
1790 Make_Selected_Component (Loc,
1792 New_Copy_Tree (Original_Node (Prefix (Obj))),
1793 Selector_Name => New_Copy (Selector_Name (Obj)));
1794 Reset_Analyzed_Flags (Lhs);
1797 Lhs := New_Occurrence_Of (Var, Loc);
1800 Set_Assignment_OK (Lhs);
1802 if Is_Access_Type (E_Formal)
1803 and then Is_Entity_Name (Lhs)
1805 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1807 -- Copyback target is an Ada 2012 stand-alone object of an
1808 -- anonymous access type.
1810 pragma Assert (Ada_Version >= Ada_2012);
1812 Apply_Accessibility_Check (Lhs, E_Formal, N);
1814 Append_To (Post_Call,
1815 Make_Assignment_Statement (Loc,
1817 Expression => Expr));
1819 -- We would like to somehow suppress generation of the
1820 -- extra_accessibility assignment generated by the expansion
1821 -- of the above assignment statement. It's not a correctness
1822 -- issue because the following assignment renders it dead,
1823 -- but generating back-to-back assignments to the same
1824 -- target is undesirable. ???
1826 Append_To (Post_Call,
1827 Make_Assignment_Statement (Loc,
1828 Name => New_Occurrence_Of (
1829 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1830 Expression => Make_Integer_Literal (Loc,
1831 Type_Access_Level (E_Formal))));
1834 if Is_Access_Type (E_Formal)
1835 and then Can_Never_Be_Null (Etype (Actual))
1836 and then not Can_Never_Be_Null (E_Formal)
1838 Append_To (Post_Call,
1839 Make_Raise_Constraint_Error (Loc,
1842 Left_Opnd => New_Occurrence_Of (Temp, Loc),
1843 Right_Opnd => Make_Null (Loc)),
1844 Reason => CE_Access_Check_Failed));
1847 Append_To (Post_Call,
1848 Make_Assignment_Statement (Loc,
1850 Expression => Expr));
1854 end Add_Call_By_Copy_Code;
1856 ----------------------------------
1857 -- Add_Simple_Call_By_Copy_Code --
1858 ----------------------------------
1860 procedure Add_Simple_Call_By_Copy_Code (Force : Boolean) is
1862 F_Typ : Entity_Id := Etype (Formal);
1871 -- Unless forced not to, check the legality of the copy operation
1873 if not Force and then not Is_Legal_Copy then
1877 -- Handle formals whose type comes from the limited view
1879 if From_Limited_With (F_Typ)
1880 and then Has_Non_Limited_View (F_Typ)
1882 F_Typ := Non_Limited_View (F_Typ);
1885 -- Use formal type for temp, unless formal type is an unconstrained
1886 -- array, in which case we don't have to worry about bounds checks,
1887 -- and we use the actual type, since that has appropriate bounds.
1889 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1890 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1892 Indic := New_Occurrence_Of (F_Typ, Loc);
1895 -- Prepare to generate code
1897 Reset_Packed_Prefix;
1899 Temp := Make_Temporary (Loc, 'T', Actual);
1900 Incod := Relocate_Node (Actual);
1901 Outcod := New_Copy_Tree (Incod);
1903 -- Generate declaration of temporary variable, initializing it
1904 -- with the input parameter unless we have an OUT formal or
1905 -- this is an initialization call.
1907 -- If the formal is an out parameter with discriminants, the
1908 -- discriminants must be captured even if the rest of the object
1909 -- is in principle uninitialized, because the discriminants may
1910 -- be read by the called subprogram.
1912 if Ekind (Formal) = E_Out_Parameter then
1915 if Has_Discriminants (F_Typ) then
1916 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1919 elsif Inside_Init_Proc then
1921 -- Could use a comment here to match comment below ???
1923 if Nkind (Actual) /= N_Selected_Component
1925 not Has_Discriminant_Dependent_Constraint
1926 (Entity (Selector_Name (Actual)))
1930 -- Otherwise, keep the component in order to generate the proper
1931 -- actual subtype, that depends on enclosing discriminants.
1939 Make_Object_Declaration (Loc,
1940 Defining_Identifier => Temp,
1941 Object_Definition => Indic,
1942 Expression => Incod);
1947 -- If the call is to initialize a component of a composite type,
1948 -- and the component does not depend on discriminants, use the
1949 -- actual type of the component. This is required in case the
1950 -- component is constrained, because in general the formal of the
1951 -- initialization procedure will be unconstrained. Note that if
1952 -- the component being initialized is constrained by an enclosing
1953 -- discriminant, the presence of the initialization in the
1954 -- declaration will generate an expression for the actual subtype.
1956 Set_No_Initialization (Decl);
1957 Set_Object_Definition (Decl,
1958 New_Occurrence_Of (Etype (Actual), Loc));
1961 Insert_Action (N, Decl);
1963 -- The actual is simply a reference to the temporary
1965 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1967 -- Generate copy out if OUT or IN OUT parameter
1969 if Ekind (Formal) /= E_In_Parameter then
1971 Rhs := New_Occurrence_Of (Temp, Loc);
1972 Set_Is_True_Constant (Temp, False);
1974 -- Deal with conversion
1976 if Nkind (Lhs) = N_Type_Conversion then
1977 Lhs := Expression (Lhs);
1978 Rhs := Convert_To (Etype (Actual), Rhs);
1981 Append_To (Post_Call,
1982 Make_Assignment_Statement (Loc,
1984 Expression => Rhs));
1985 Set_Assignment_OK (Name (Last (Post_Call)));
1987 end Add_Simple_Call_By_Copy_Code;
1989 --------------------------------------
1990 -- Add_Validation_Call_By_Copy_Code --
1991 --------------------------------------
1993 procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id) is
1996 Obj_Typ : Entity_Id;
1997 Var : constant Node_Id := Unqual_Conv (Act);
2001 -- Generate range check if required
2003 if Do_Range_Check (Actual) then
2004 Generate_Range_Check (Actual, E_Formal, CE_Range_Check_Failed);
2007 -- If there is a type conversion in the actual, it will be reinstated
2008 -- below, the new instance will be properly analyzed and the setting
2009 -- of the Do_Range_Check flag recomputed so remove the obsolete one.
2011 if Nkind (Actual) = N_Type_Conversion then
2012 Set_Do_Range_Check (Expression (Actual), False);
2015 -- Copy the value of the validation variable back into the object
2018 if Is_Entity_Name (Var) then
2019 Var_Id := Entity (Var);
2020 Obj := Validated_Object (Var_Id);
2021 Obj_Typ := Etype (Obj);
2023 Expr := New_Occurrence_Of (Var_Id, Loc);
2025 -- A type conversion is needed when the validation variable and
2026 -- the validated object carry different types. This case occurs
2027 -- when the actual is qualified in some fashion.
2030 -- subtype Int is Integer range ...;
2031 -- procedure Call (Val : in out Integer);
2035 -- Call (Integer (Object));
2039 -- Var : Integer := Object; -- conversion to base type
2040 -- if not Var'Valid then -- validity check
2041 -- Call (Var); -- modify Var
2042 -- Object := Int (Var); -- conversion to subtype
2044 if Etype (Var_Id) /= Obj_Typ then
2046 Make_Type_Conversion (Loc,
2047 Subtype_Mark => New_Occurrence_Of (Obj_Typ, Loc),
2048 Expression => Expr);
2054 -- Object := Object_Type (Var);
2056 Append_To (Post_Call,
2057 Make_Assignment_Statement (Loc,
2059 Expression => Expr));
2061 -- If the flow reaches this point, then this routine was invoked with
2062 -- an actual which does not denote a validation variable.
2065 pragma Assert (False);
2068 end Add_Validation_Call_By_Copy_Code;
2070 ---------------------------
2071 -- Check_Fortran_Logical --
2072 ---------------------------
2074 procedure Check_Fortran_Logical is
2075 Logical : constant Entity_Id := Etype (Formal);
2078 -- Note: this is very incomplete, e.g. it does not handle arrays
2079 -- of logical values. This is really not the right approach at all???)
2082 if Convention (Subp) = Convention_Fortran
2083 and then Root_Type (Etype (Formal)) = Standard_Boolean
2084 and then Ekind (Formal) /= E_In_Parameter
2086 Var := Make_Var (Actual);
2087 Append_To (Post_Call,
2088 Make_Assignment_Statement (Loc,
2089 Name => New_Occurrence_Of (Var, Loc),
2091 Unchecked_Convert_To (
2094 Left_Opnd => New_Occurrence_Of (Var, Loc),
2096 Unchecked_Convert_To (
2098 New_Occurrence_Of (Standard_False, Loc))))));
2100 end Check_Fortran_Logical;
2106 function Is_Legal_Copy return Boolean is
2108 -- An attempt to copy a value of such a type can only occur if
2109 -- representation clauses give the actual a misaligned address.
2111 if Is_By_Reference_Type (Etype (Formal))
2112 or else Is_Aliased (Formal)
2113 or else (Mechanism (Formal) = By_Reference
2114 and then not Has_Foreign_Convention (Subp))
2117 -- The actual may in fact be properly aligned but there is not
2118 -- enough front-end information to determine this. In that case
2119 -- gigi will emit an error or a warning if a copy is not legal,
2120 -- or generate the proper code.
2124 -- For users of Starlet, we assume that the specification of by-
2125 -- reference mechanism is mandatory. This may lead to unaligned
2126 -- objects but at least for DEC legacy code it is known to work.
2127 -- The warning will alert users of this code that a problem may
2130 elsif Mechanism (Formal) = By_Reference
2131 and then Ekind (Scope (Formal)) = E_Procedure
2132 and then Is_Valued_Procedure (Scope (Formal))
2135 ("by_reference actual may be misaligned??", Actual);
2147 function Make_Var (Actual : Node_Id) return Entity_Id is
2151 if Is_Entity_Name (Actual) then
2152 return Entity (Actual);
2155 Var := Make_Temporary (Loc, 'T', Actual);
2158 Make_Object_Renaming_Declaration (Loc,
2159 Defining_Identifier => Var,
2161 New_Occurrence_Of (Etype (Actual), Loc),
2162 Name => Relocate_Node (Actual));
2164 Insert_Action (N, N_Node);
2169 -------------------------
2170 -- Reset_Packed_Prefix --
2171 -------------------------
2173 procedure Reset_Packed_Prefix is
2174 Pfx : Node_Id := Actual;
2177 Set_Analyzed (Pfx, False);
2179 Nkind (Pfx) not in N_Selected_Component | N_Indexed_Component;
2180 Pfx := Prefix (Pfx);
2182 end Reset_Packed_Prefix;
2184 ----------------------------------------
2185 -- Requires_Atomic_Or_Volatile_Copy --
2186 ----------------------------------------
2188 function Requires_Atomic_Or_Volatile_Copy return Boolean is
2190 -- If the formal is already passed by copy, no need to do anything
2192 if Is_By_Copy_Type (E_Formal) then
2196 -- There is no requirement inside initialization procedures and this
2197 -- would generate copies for atomic or volatile composite components.
2199 if Inside_Init_Proc then
2203 -- Check for atomicity mismatch
2205 if Is_Atomic_Object (Actual) and then not Is_Atomic (E_Formal)
2207 if Comes_From_Source (N) then
2209 ("??atomic actual passed by copy (RM C.6(19))", Actual);
2214 -- Check for volatility mismatch
2216 if Is_Volatile_Object_Ref (Actual) and then not Is_Volatile (E_Formal)
2218 if Comes_From_Source (N) then
2220 ("??volatile actual passed by copy (RM C.6(19))", Actual);
2226 end Requires_Atomic_Or_Volatile_Copy;
2228 -- Start of processing for Expand_Actuals
2231 Post_Call := New_List;
2233 Formal := First_Formal (Subp);
2234 Actual := First_Actual (N);
2235 while Present (Formal) loop
2236 E_Formal := Etype (Formal);
2237 E_Actual := Etype (Actual);
2239 -- Handle formals whose type comes from the limited view
2241 if From_Limited_With (E_Formal)
2242 and then Has_Non_Limited_View (E_Formal)
2244 E_Formal := Non_Limited_View (E_Formal);
2247 if Is_Scalar_Type (E_Formal)
2248 or else Nkind (Actual) = N_Slice
2250 Check_Fortran_Logical;
2254 elsif Ekind (Formal) /= E_Out_Parameter then
2256 -- The unusual case of the current instance of a protected type
2257 -- requires special handling. This can only occur in the context
2258 -- of a call within the body of a protected operation.
2260 if Is_Entity_Name (Actual)
2261 and then Ekind (Entity (Actual)) = E_Protected_Type
2262 and then In_Open_Scopes (Entity (Actual))
2264 if Scope (Subp) /= Entity (Actual) then
2266 ("operation outside protected type may not "
2267 & "call back its protected operations??", Actual);
2271 Expand_Protected_Object_Reference (N, Entity (Actual)));
2274 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
2275 -- build-in-place function, then a temporary return object needs
2276 -- to be created and access to it must be passed to the function
2277 -- (and ensure that we have an activation chain defined for tasks
2278 -- and a Master variable).
2280 -- Currently we limit such functions to those with inherently
2281 -- limited result subtypes, but eventually we plan to expand the
2282 -- functions that are treated as build-in-place to include other
2283 -- composite result types.
2285 -- But do not do it here for intrinsic subprograms since this will
2286 -- be done properly after the subprogram is expanded.
2288 if Is_Intrinsic_Subprogram (Subp) then
2291 elsif Is_Build_In_Place_Function_Call (Actual) then
2292 Build_Activation_Chain_Entity (N);
2293 Build_Master_Entity (Etype (Actual));
2294 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
2296 -- Ada 2005 (AI-318-02): Specialization of the previous case for
2297 -- actuals containing build-in-place function calls whose returned
2298 -- object covers interface types.
2300 elsif Present (Unqual_BIP_Iface_Function_Call (Actual)) then
2301 Build_Activation_Chain_Entity (N);
2302 Build_Master_Entity (Etype (Actual));
2303 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (Actual);
2306 Apply_Constraint_Check (Actual, E_Formal);
2308 -- Out parameter case. No constraint checks on access type
2309 -- RM 6.4.1 (13), but on return a null-excluding check may be
2310 -- required (see below).
2312 elsif Is_Access_Type (E_Formal) then
2317 elsif Has_Discriminants (Base_Type (E_Formal))
2318 or else Has_Non_Null_Base_Init_Proc (E_Formal)
2320 Apply_Constraint_Check (Actual, E_Formal);
2325 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
2328 -- Processing for IN-OUT and OUT parameters
2330 if Ekind (Formal) /= E_In_Parameter then
2332 -- For type conversions of arrays, apply length/range checks
2334 if Is_Array_Type (E_Formal)
2335 and then Nkind (Actual) = N_Type_Conversion
2337 if Is_Constrained (E_Formal) then
2338 Apply_Length_Check (Expression (Actual), E_Formal);
2340 Apply_Range_Check (Expression (Actual), E_Formal);
2344 -- The actual denotes a variable which captures the value of an
2345 -- object for validation purposes. Add a copy-back to reflect any
2346 -- potential changes in value back into the original object.
2348 -- Var : ... := Object;
2349 -- if not Var'Valid then -- validity check
2350 -- Call (Var); -- modify var
2351 -- Object := Var; -- update Object
2353 -- This case is given higher priority because the subsequent check
2354 -- for type conversion may add an extra copy of the variable and
2355 -- prevent proper value propagation back in the original object.
2357 if Is_Validation_Variable_Reference (Actual) then
2358 Add_Validation_Call_By_Copy_Code (Actual);
2360 -- If argument is a type conversion for a type that is passed by
2361 -- copy, then we must pass the parameter by copy.
2363 elsif Nkind (Actual) = N_Type_Conversion
2365 (Is_Elementary_Type (E_Formal)
2366 or else Is_Bit_Packed_Array (Etype (Formal))
2367 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
2369 -- Also pass by copy if change of representation
2371 or else not Has_Compatible_Representation
2372 (Target_Type => Etype (Formal),
2373 Operand_Type => Etype (Expression (Actual))))
2375 Add_Call_By_Copy_Code;
2377 -- References to components of bit-packed arrays are expanded
2378 -- at this point, rather than at the point of analysis of the
2379 -- actuals, to handle the expansion of the assignment to
2380 -- [in] out parameters.
2382 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
2383 Add_Simple_Call_By_Copy_Code (Force => True);
2385 -- If a nonscalar actual is possibly bit-aligned, we need a copy
2386 -- because the back-end cannot cope with such objects. In other
2387 -- cases where alignment forces a copy, the back-end generates
2388 -- it properly. It should not be generated unconditionally in the
2389 -- front-end because it does not know precisely the alignment
2390 -- requirements of the target, and makes too conservative an
2391 -- estimate, leading to superfluous copies or spurious errors
2392 -- on by-reference parameters.
2394 elsif Nkind (Actual) = N_Selected_Component
2396 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
2397 and then not Represented_As_Scalar (Etype (Formal))
2399 Add_Simple_Call_By_Copy_Code (Force => False);
2401 -- References to slices of bit-packed arrays are expanded
2403 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2404 Add_Call_By_Copy_Code;
2406 -- References to possibly unaligned slices of arrays are expanded
2408 elsif Is_Possibly_Unaligned_Slice (Actual) then
2409 Add_Call_By_Copy_Code;
2411 -- Deal with access types where the actual subtype and the
2412 -- formal subtype are not the same, requiring a check.
2414 -- It is necessary to exclude tagged types because of "downward
2415 -- conversion" errors, but null-excluding checks on return may be
2418 elsif Is_Access_Type (E_Formal)
2419 and then not Is_Tagged_Type (Designated_Type (E_Formal))
2420 and then (not Same_Type (E_Formal, E_Actual)
2421 or else (Can_Never_Be_Null (E_Actual)
2422 and then not Can_Never_Be_Null (E_Formal)))
2424 Add_Call_By_Copy_Code;
2426 -- We may need to force a copy because of atomicity or volatility
2429 elsif Requires_Atomic_Or_Volatile_Copy then
2430 Add_Call_By_Copy_Code;
2432 -- Add call-by-copy code for the case of scalar out parameters
2433 -- when it is not known at compile time that the subtype of the
2434 -- formal is a subrange of the subtype of the actual (or vice
2435 -- versa for in out parameters), in order to get range checks
2436 -- on such actuals. (Maybe this case should be handled earlier
2437 -- in the if statement???)
2439 elsif Is_Scalar_Type (E_Formal)
2441 (not In_Subrange_Of (E_Formal, E_Actual)
2443 (Ekind (Formal) = E_In_Out_Parameter
2444 and then not In_Subrange_Of (E_Actual, E_Formal)))
2446 Add_Call_By_Copy_Code;
2449 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
2450 -- by-reference parameters on exit from the call. If the actual
2451 -- is a derived type and the operation is inherited, the body
2452 -- of the operation will not contain a call to the predicate
2453 -- function, so it must be done explicitly after the call. Ditto
2454 -- if the actual is an entity of a predicated subtype.
2456 -- The rule refers to by-reference types, but a check is needed
2457 -- for by-copy types as well. That check is subsumed by the rule
2458 -- for subtype conversion on assignment, but we can generate the
2459 -- required check now.
2461 -- Note also that Subp may be either a subprogram entity for
2462 -- direct calls, or a type entity for indirect calls, which must
2463 -- be handled separately because the name does not denote an
2464 -- overloadable entity.
2466 By_Ref_Predicate_Check : declare
2467 Aund : constant Entity_Id := Underlying_Type (E_Actual);
2477 if Predicate_Enabled (Atyp)
2479 -- Skip predicate checks for special cases
2481 and then Predicate_Tests_On_Arguments (Subp)
2483 Append_To (Post_Call,
2484 Make_Predicate_Check (Atyp, Actual));
2486 end By_Ref_Predicate_Check;
2488 -- Processing for IN parameters
2491 -- Generate range check if required
2493 if Do_Range_Check (Actual) then
2494 Generate_Range_Check (Actual, E_Formal, CE_Range_Check_Failed);
2497 -- For IN parameters in the bit-packed array case, we expand an
2498 -- indexed component (the circuit in Exp_Ch4 deliberately left
2499 -- indexed components appearing as actuals untouched, so that
2500 -- the special processing above for the OUT and IN OUT cases
2501 -- could be performed. We could make the test in Exp_Ch4 more
2502 -- complex and have it detect the parameter mode, but it is
2503 -- easier simply to handle all cases here.)
2505 if Nkind (Actual) = N_Indexed_Component
2506 and then Is_Bit_Packed_Array (Etype (Prefix (Actual)))
2508 Reset_Packed_Prefix;
2509 Expand_Packed_Element_Reference (Actual);
2511 -- If we have a reference to a bit-packed array, we copy it, since
2512 -- the actual must be byte aligned.
2514 -- Is this really necessary in all cases???
2516 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
2517 Add_Simple_Call_By_Copy_Code (Force => True);
2519 -- If we have a C++ constructor call, we need to create the object
2521 elsif Is_CPP_Constructor_Call (Actual) then
2522 Add_Simple_Call_By_Copy_Code (Force => True);
2524 -- If a nonscalar actual is possibly unaligned, we need a copy
2526 elsif Is_Possibly_Unaligned_Object (Actual)
2527 and then not Represented_As_Scalar (Etype (Formal))
2529 Add_Simple_Call_By_Copy_Code (Force => False);
2531 -- Similarly, we have to expand slices of packed arrays here
2532 -- because the result must be byte aligned.
2534 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2535 Add_Call_By_Copy_Code;
2537 -- Only processing remaining is to pass by copy if this is a
2538 -- reference to a possibly unaligned slice, since the caller
2539 -- expects an appropriately aligned argument.
2541 elsif Is_Possibly_Unaligned_Slice (Actual) then
2542 Add_Call_By_Copy_Code;
2544 -- We may need to force a copy because of atomicity or volatility
2547 elsif Requires_Atomic_Or_Volatile_Copy then
2548 Add_Call_By_Copy_Code;
2550 -- An unusual case: a current instance of an enclosing task can be
2551 -- an actual, and must be replaced by a reference to self.
2553 elsif Is_Entity_Name (Actual)
2554 and then Is_Task_Type (Entity (Actual))
2556 if In_Open_Scopes (Entity (Actual)) then
2558 (Make_Function_Call (Loc,
2559 Name => New_Occurrence_Of (RTE (RE_Self), Loc))));
2562 -- A task type cannot otherwise appear as an actual
2565 raise Program_Error;
2570 -- Type-invariant checks for in-out and out parameters, as well as
2571 -- for in parameters of procedures (AI05-0289 and AI12-0044).
2573 if Ekind (Formal) /= E_In_Parameter
2574 or else Ekind (Subp) = E_Procedure
2576 Caller_Side_Invariant_Checks : declare
2578 function Is_Public_Subp return Boolean;
2579 -- Check whether the subprogram being called is a visible
2580 -- operation of the type of the actual. Used to determine
2581 -- whether an invariant check must be generated on the
2584 ---------------------
2585 -- Is_Public_Subp --
2586 ---------------------
2588 function Is_Public_Subp return Boolean is
2589 Pack : constant Entity_Id := Scope (Subp);
2590 Subp_Decl : Node_Id;
2593 if not Is_Subprogram (Subp) then
2596 -- The operation may be inherited, or a primitive of the
2600 Nkind (Parent (Subp)) in N_Private_Extension_Declaration
2601 | N_Full_Type_Declaration
2603 Subp_Decl := Parent (Subp);
2606 Subp_Decl := Unit_Declaration_Node (Subp);
2609 return Ekind (Pack) = E_Package
2611 List_Containing (Subp_Decl) =
2612 Visible_Declarations
2613 (Specification (Unit_Declaration_Node (Pack)));
2616 -- Start of processing for Caller_Side_Invariant_Checks
2619 -- We generate caller-side invariant checks in two cases:
2621 -- a) when calling an inherited operation, where there is an
2622 -- implicit view conversion of the actual to the parent type.
2624 -- b) When the conversion is explicit
2626 -- We treat these cases separately because the required
2627 -- conversion for a) is added later when expanding the call.
2629 if Has_Invariants (Etype (Actual))
2631 Nkind (Parent (Etype (Actual)))
2632 = N_Private_Extension_Declaration
2634 if Comes_From_Source (N) and then Is_Public_Subp then
2635 Append_To (Post_Call, Make_Invariant_Call (Actual));
2638 elsif Nkind (Actual) = N_Type_Conversion
2639 and then Has_Invariants (Etype (Expression (Actual)))
2641 if Comes_From_Source (N) and then Is_Public_Subp then
2643 (Post_Call, Make_Invariant_Call (Expression (Actual)));
2646 end Caller_Side_Invariant_Checks;
2649 Next_Formal (Formal);
2650 Next_Actual (Actual);
2658 procedure Expand_Call (N : Node_Id) is
2659 Post_Call : List_Id;
2661 -- If this is an indirect call through an Access_To_Subprogram
2662 -- with contract specifications, it is rewritten as a call to
2663 -- the corresponding Access_Subprogram_Wrapper with the same
2664 -- actuals, whose body contains a naked indirect call (which
2665 -- itself must not be rewritten, to prevent infinite recursion).
2667 Must_Rewrite_Indirect_Call : constant Boolean :=
2668 Ada_Version >= Ada_2022
2669 and then Nkind (Name (N)) = N_Explicit_Dereference
2670 and then Ekind (Etype (Name (N))) = E_Subprogram_Type
2672 (Access_Subprogram_Wrapper (Etype (Name (N))));
2675 pragma Assert (Nkind (N) in N_Entry_Call_Statement
2677 | N_Procedure_Call_Statement);
2679 -- Check that this is not the call in the body of the wrapper
2681 if Must_Rewrite_Indirect_Call
2682 and then (not Is_Overloadable (Current_Scope)
2683 or else not Is_Access_Subprogram_Wrapper (Current_Scope))
2686 Loc : constant Source_Ptr := Sloc (N);
2687 Wrapper : constant Entity_Id :=
2688 Access_Subprogram_Wrapper (Etype (Name (N)));
2689 Ptr : constant Node_Id := Prefix (Name (N));
2690 Ptr_Type : constant Entity_Id := Etype (Ptr);
2691 Typ : constant Entity_Id := Etype (N);
2694 Parms : List_Id := Parameter_Associations (N);
2698 -- The last actual in the call is the pointer itself.
2699 -- If the aspect is inherited, convert the pointer to the
2700 -- parent type that specifies the contract.
2701 -- If the original access_to_subprogram has defaults for
2702 -- in_parameters, the call may include named associations, so
2703 -- we create one for the pointer as well.
2705 if Is_Derived_Type (Ptr_Type)
2706 and then Ptr_Type /= Etype (Last_Formal (Wrapper))
2709 Make_Type_Conversion (Loc,
2711 (Etype (Last_Formal (Wrapper)), Loc), Ptr);
2717 -- Handle parameterless subprogram.
2724 (Make_Parameter_Association (Loc,
2725 Selector_Name => Make_Identifier (Loc,
2726 Chars (Last_Formal (Wrapper))),
2727 Explicit_Actual_Parameter => Ptr_Act),
2730 if Nkind (N) = N_Procedure_Call_Statement then
2731 New_N := Make_Procedure_Call_Statement (Loc,
2732 Name => New_Occurrence_Of (Wrapper, Loc),
2733 Parameter_Associations => Parms);
2735 New_N := Make_Function_Call (Loc,
2736 Name => New_Occurrence_Of (Wrapper, Loc),
2737 Parameter_Associations => Parms);
2741 Analyze_And_Resolve (N, Typ);
2745 Expand_Call_Helper (N, Post_Call);
2746 Insert_Post_Call_Actions (N, Post_Call);
2750 ------------------------
2751 -- Expand_Call_Helper --
2752 ------------------------
2754 -- This procedure handles expansion of function calls and procedure call
2755 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
2756 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
2758 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
2759 -- Provide values of actuals for all formals in Extra_Formals list
2760 -- Replace "call" to enumeration literal function by literal itself
2761 -- Rewrite call to predefined operator as operator
2762 -- Replace actuals to in-out parameters that are numeric conversions,
2763 -- with explicit assignment to temporaries before and after the call.
2765 -- Note that the list of actuals has been filled with default expressions
2766 -- during semantic analysis of the call. Only the extra actuals required
2767 -- for the 'Constrained attribute and for accessibility checks are added
2770 procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id) is
2771 Loc : constant Source_Ptr := Sloc (N);
2772 Call_Node : Node_Id := N;
2773 Extra_Actuals : List_Id := No_List;
2774 Prev : Node_Id := Empty;
2776 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
2777 -- Adds one entry to the end of the actual parameter list. Used for
2778 -- default parameters and for extra actuals (for Extra_Formals). The
2779 -- argument is an N_Parameter_Association node.
2781 procedure Add_Cond_Expression_Extra_Actual (Formal : Entity_Id);
2782 -- Adds extra accessibility actuals in the case of a conditional
2783 -- expression corresponding to Formal.
2785 -- Note: Conditional expressions used as actuals for anonymous access
2786 -- formals complicate the process of propagating extra accessibility
2787 -- actuals and must be handled in a recursive fashion since they can
2788 -- be embedded within each other.
2790 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
2791 -- Adds an extra actual to the list of extra actuals. Expr is the
2792 -- expression for the value of the actual, EF is the entity for the
2795 procedure Add_View_Conversion_Invariants
2796 (Formal : Entity_Id;
2798 -- Adds invariant checks for every intermediate type between the range
2799 -- of a view converted argument to its ancestor (from parent to child).
2801 function Can_Fold_Predicate_Call (P : Entity_Id) return Boolean;
2802 -- Try to constant-fold a predicate check, which often enough is a
2803 -- simple arithmetic expression that can be computed statically if
2804 -- its argument is static. This cleans up the output of CCG, even
2805 -- though useless predicate checks will be generally removed by
2806 -- back-end optimizations.
2808 procedure Check_Subprogram_Variant;
2809 -- Emit a call to the internally generated procedure with checks for
2810 -- aspect Subprogrgram_Variant, if present and enabled.
2812 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
2813 -- Within an instance, a type derived from an untagged formal derived
2814 -- type inherits from the original parent, not from the actual. The
2815 -- current derivation mechanism has the derived type inherit from the
2816 -- actual, which is only correct outside of the instance. If the
2817 -- subprogram is inherited, we test for this particular case through a
2818 -- convoluted tree traversal before setting the proper subprogram to be
2821 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
2822 -- Return true if E comes from an instance that is not yet frozen
2824 function Is_Class_Wide_Interface_Type (E : Entity_Id) return Boolean;
2825 -- Return True when E is a class-wide interface type or an access to
2826 -- a class-wide interface type.
2828 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
2829 -- Determine if Subp denotes a non-dispatching call to a Deep routine
2831 function New_Value (From : Node_Id) return Node_Id;
2832 -- From is the original Expression. New_Value is equivalent to a call
2833 -- to Duplicate_Subexpr with an explicit dereference when From is an
2834 -- access parameter.
2836 --------------------------
2837 -- Add_Actual_Parameter --
2838 --------------------------
2840 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
2841 Actual_Expr : constant Node_Id :=
2842 Explicit_Actual_Parameter (Insert_Param);
2845 -- Case of insertion is first named actual
2847 if No (Prev) or else
2848 Nkind (Parent (Prev)) /= N_Parameter_Association
2850 Set_Next_Named_Actual
2851 (Insert_Param, First_Named_Actual (Call_Node));
2852 Set_First_Named_Actual (Call_Node, Actual_Expr);
2855 if No (Parameter_Associations (Call_Node)) then
2856 Set_Parameter_Associations (Call_Node, New_List);
2859 Append (Insert_Param, Parameter_Associations (Call_Node));
2862 Insert_After (Prev, Insert_Param);
2865 -- Case of insertion is not first named actual
2868 Set_Next_Named_Actual
2869 (Insert_Param, Next_Named_Actual (Parent (Prev)));
2870 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
2871 Append (Insert_Param, Parameter_Associations (Call_Node));
2874 Prev := Actual_Expr;
2875 end Add_Actual_Parameter;
2877 --------------------------------------
2878 -- Add_Cond_Expression_Extra_Actual --
2879 --------------------------------------
2881 procedure Add_Cond_Expression_Extra_Actual
2882 (Formal : Entity_Id)
2887 procedure Insert_Level_Assign (Branch : Node_Id);
2888 -- Recursively add assignment of the level temporary on each branch
2889 -- while moving through nested conditional expressions.
2891 -------------------------
2892 -- Insert_Level_Assign --
2893 -------------------------
2895 procedure Insert_Level_Assign (Branch : Node_Id) is
2897 procedure Expand_Branch (Res_Assn : Node_Id);
2898 -- Perform expansion or iterate further within nested
2899 -- conditionals given the object declaration or assignment to
2900 -- result object created during expansion which represents a
2901 -- branch of the conditional expression.
2907 procedure Expand_Branch (Res_Assn : Node_Id) is
2909 pragma Assert (Nkind (Res_Assn) in
2910 N_Assignment_Statement |
2911 N_Object_Declaration);
2913 -- There are more nested conditional expressions so we must go
2916 if Nkind (Expression (Res_Assn)) = N_Expression_With_Actions
2918 Nkind (Original_Node (Expression (Res_Assn)))
2919 in N_Case_Expression | N_If_Expression
2922 (Expression (Res_Assn));
2924 -- Add the level assignment
2927 Insert_Before_And_Analyze (Res_Assn,
2928 Make_Assignment_Statement (Loc,
2929 Name => New_Occurrence_Of (Lvl, Loc),
2932 (Expression (Res_Assn), Dynamic_Level)));
2939 -- Start of processing for Insert_Level_Assign
2942 -- Examine further nested condtionals
2944 pragma Assert (Nkind (Branch) =
2945 N_Expression_With_Actions);
2947 -- Find the relevant statement in the actions
2949 Cond := First (Actions (Branch));
2950 while Present (Cond) loop
2951 exit when Nkind (Cond) in N_Case_Statement | N_If_Statement;
2955 -- The conditional expression may have been optimized away, so
2956 -- examine the actions in the branch.
2959 Expand_Branch (Last (Actions (Branch)));
2961 -- Iterate through if expression branches
2963 elsif Nkind (Cond) = N_If_Statement then
2964 Expand_Branch (Last (Then_Statements (Cond)));
2965 Expand_Branch (Last (Else_Statements (Cond)));
2967 -- Iterate through case alternatives
2969 elsif Nkind (Cond) = N_Case_Statement then
2971 Alt := First (Alternatives (Cond));
2972 while Present (Alt) loop
2973 Expand_Branch (Last (Statements (Alt)));
2977 end Insert_Level_Assign;
2979 -- Start of processing for cond expression case
2982 -- Create declaration of a temporary to store the accessibility
2983 -- level of each branch of the conditional expression.
2985 Lvl := Make_Temporary (Loc, 'L');
2986 Decl := Make_Object_Declaration (Loc,
2987 Defining_Identifier => Lvl,
2988 Object_Definition =>
2989 New_Occurrence_Of (Standard_Natural, Loc));
2991 -- Install the declaration and perform necessary expansion if we
2992 -- are dealing with a procedure call.
2994 if Nkind (Call_Node) = N_Procedure_Call_Statement then
2999 -- If_Exp_Res : Typ;
3001 -- Lvl := 0; -- Access level
3002 -- If_Exp_Res := Exp;
3004 -- in If_Exp_Res end;},
3009 Insert_Before_And_Analyze (Call_Node, Decl);
3011 -- Ditto for a function call. Note that we do not wrap the function
3012 -- call into an expression with action to avoid bad interactions with
3013 -- Exp_Ch4.Process_Transient_In_Expression.
3017 -- Lvl : Natural; -- placed above the function call
3023 -- Lvl := 0; -- Access level
3024 -- If_Exp_Res := Exp;
3025 -- in If_Exp_Res end;},
3030 Insert_Action (Call_Node, Decl);
3031 Analyze (Call_Node);
3034 -- Decorate the conditional expression with assignments to our level
3037 Insert_Level_Assign (Prev);
3039 -- Make our level temporary the passed actual
3042 (Expr => New_Occurrence_Of (Lvl, Loc),
3043 EF => Extra_Accessibility (Formal));
3044 end Add_Cond_Expression_Extra_Actual;
3046 ----------------------
3047 -- Add_Extra_Actual --
3048 ----------------------
3050 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
3051 Loc : constant Source_Ptr := Sloc (Expr);
3054 if Extra_Actuals = No_List then
3055 Extra_Actuals := New_List;
3056 Set_Parent (Extra_Actuals, Call_Node);
3059 Append_To (Extra_Actuals,
3060 Make_Parameter_Association (Loc,
3061 Selector_Name => New_Occurrence_Of (EF, Loc),
3062 Explicit_Actual_Parameter => Expr));
3064 Analyze_And_Resolve (Expr, Etype (EF));
3066 if Nkind (Call_Node) = N_Function_Call then
3067 Set_Is_Accessibility_Actual (Parent (Expr));
3069 end Add_Extra_Actual;
3071 ------------------------------------
3072 -- Add_View_Conversion_Invariants --
3073 ------------------------------------
3075 procedure Add_View_Conversion_Invariants
3076 (Formal : Entity_Id;
3080 Curr_Typ : Entity_Id;
3081 Inv_Checks : List_Id;
3082 Par_Typ : Entity_Id;
3085 Inv_Checks := No_List;
3087 -- Extract the argument from a potentially nested set of view
3091 while Nkind (Arg) = N_Type_Conversion loop
3092 Arg := Expression (Arg);
3095 -- Move up the derivation chain starting with the type of the formal
3096 -- parameter down to the type of the actual object.
3099 Par_Typ := Etype (Arg);
3100 while Par_Typ /= Etype (Formal) and Par_Typ /= Curr_Typ loop
3101 Curr_Typ := Par_Typ;
3103 if Has_Invariants (Curr_Typ)
3104 and then Present (Invariant_Procedure (Curr_Typ))
3106 -- Verify the invariant of the current type. Generate:
3108 -- <Curr_Typ>Invariant (Curr_Typ (Arg));
3110 Prepend_New_To (Inv_Checks,
3111 Make_Procedure_Call_Statement (Loc,
3114 (Invariant_Procedure (Curr_Typ), Loc),
3115 Parameter_Associations => New_List (
3116 Make_Type_Conversion (Loc,
3117 Subtype_Mark => New_Occurrence_Of (Curr_Typ, Loc),
3118 Expression => New_Copy_Tree (Arg)))));
3121 Par_Typ := Base_Type (Etype (Curr_Typ));
3124 -- If the node is a function call the generated tests have been
3125 -- already handled in Insert_Post_Call_Actions.
3127 if not Is_Empty_List (Inv_Checks)
3128 and then Nkind (Call_Node) = N_Procedure_Call_Statement
3130 Insert_Actions_After (Call_Node, Inv_Checks);
3132 end Add_View_Conversion_Invariants;
3134 -----------------------------
3135 -- Can_Fold_Predicate_Call --
3136 -----------------------------
3138 function Can_Fold_Predicate_Call (P : Entity_Id) return Boolean is
3141 function May_Fold (N : Node_Id) return Traverse_Result;
3142 -- The predicate expression is foldable if it only contains operators
3143 -- and literals. During this check, we also replace occurrences of
3144 -- the formal of the constructed predicate function with the static
3145 -- value of the actual. This is done on a copy of the analyzed
3146 -- expression for the predicate.
3152 function May_Fold (N : Node_Id) return Traverse_Result is
3158 when N_Expanded_Name
3161 if Ekind (Entity (N)) = E_In_Parameter
3162 and then Entity (N) = First_Entity (P)
3164 Rewrite (N, New_Copy (Actual));
3165 Set_Is_Static_Expression (N);
3168 elsif Ekind (Entity (N)) = E_Enumeration_Literal then
3175 when N_Case_Expression
3180 when N_Integer_Literal =>
3188 function Try_Fold is new Traverse_Func (May_Fold);
3190 -- Other lLocal variables
3192 Subt : constant Entity_Id := Etype (First_Entity (P));
3196 -- Start of processing for Can_Fold_Predicate_Call
3199 -- Folding is only interesting if the actual is static and its type
3200 -- has a Dynamic_Predicate aspect. For CodePeer we preserve the
3203 Actual := First (Parameter_Associations (Call_Node));
3204 Aspect := Find_Aspect (Subt, Aspect_Dynamic_Predicate);
3206 -- If actual is a declared constant, retrieve its value
3208 if Is_Entity_Name (Actual)
3209 and then Ekind (Entity (Actual)) = E_Constant
3211 Actual := Constant_Value (Entity (Actual));
3215 or else Nkind (Actual) /= N_Integer_Literal
3216 or else not Has_Dynamic_Predicate_Aspect (Subt)
3218 or else CodePeer_Mode
3223 -- Retrieve the analyzed expression for the predicate
3225 Pred := New_Copy_Tree (Expression (Aspect));
3227 if Try_Fold (Pred) = OK then
3228 Rewrite (Call_Node, Pred);
3229 Analyze_And_Resolve (Call_Node, Standard_Boolean);
3232 -- Otherwise continue the expansion of the function call
3237 end Can_Fold_Predicate_Call;
3239 ------------------------------
3240 -- Check_Subprogram_Variant --
3241 ------------------------------
3243 procedure Check_Subprogram_Variant is
3244 Variant_Prag : constant Node_Id :=
3245 Get_Pragma (Current_Scope, Pragma_Subprogram_Variant);
3247 Variant_Proc : Entity_Id;
3250 if Present (Variant_Prag) and then Is_Checked (Variant_Prag) then
3252 -- Analysis of the pragma rewrites its argument with a reference
3253 -- to the internally generated procedure.
3259 (Pragma_Argument_Associations (Variant_Prag))));
3261 Insert_Action (Call_Node,
3262 Make_Procedure_Call_Statement (Loc,
3264 New_Occurrence_Of (Variant_Proc, Loc),
3265 Parameter_Associations =>
3266 New_Copy_List (Parameter_Associations (Call_Node))));
3268 end Check_Subprogram_Variant;
3270 ---------------------------
3271 -- Inherited_From_Formal --
3272 ---------------------------
3274 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
3276 Gen_Par : Entity_Id;
3277 Gen_Prim : Elist_Id;
3282 -- If the operation is inherited, it is attached to the corresponding
3283 -- type derivation. If the parent in the derivation is a generic
3284 -- actual, it is a subtype of the actual, and we have to recover the
3285 -- original derived type declaration to find the proper parent.
3287 if Nkind (Parent (S)) /= N_Full_Type_Declaration
3288 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
3289 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
3290 N_Derived_Type_Definition
3291 or else not In_Instance
3298 (Type_Definition (Original_Node (Parent (S))));
3300 if Nkind (Indic) = N_Subtype_Indication then
3301 Par := Entity (Subtype_Mark (Indic));
3303 Par := Entity (Indic);
3307 if not Is_Generic_Actual_Type (Par)
3308 or else Is_Tagged_Type (Par)
3309 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
3310 or else not In_Open_Scopes (Scope (Par))
3314 Gen_Par := Generic_Parent_Type (Parent (Par));
3317 -- If the actual has no generic parent type, the formal is not
3318 -- a formal derived type, so nothing to inherit.
3320 if No (Gen_Par) then
3324 -- If the generic parent type is still the generic type, this is a
3325 -- private formal, not a derived formal, and there are no operations
3326 -- inherited from the formal.
3328 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
3332 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
3334 Elmt := First_Elmt (Gen_Prim);
3335 while Present (Elmt) loop
3336 if Chars (Node (Elmt)) = Chars (S) then
3342 F1 := First_Formal (S);
3343 F2 := First_Formal (Node (Elmt));
3345 and then Present (F2)
3347 if Etype (F1) = Etype (F2)
3348 or else Etype (F2) = Gen_Par
3354 exit; -- not the right subprogram
3366 raise Program_Error;
3367 end Inherited_From_Formal;
3369 --------------------------
3370 -- In_Unfrozen_Instance --
3371 --------------------------
3373 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
3378 while Present (S) and then S /= Standard_Standard loop
3379 if Is_Generic_Instance (S)
3380 and then Present (Freeze_Node (S))
3381 and then not Analyzed (Freeze_Node (S))
3390 end In_Unfrozen_Instance;
3392 ----------------------------------
3393 -- Is_Class_Wide_Interface_Type --
3394 ----------------------------------
3396 function Is_Class_Wide_Interface_Type (E : Entity_Id) return Boolean is
3398 Typ : Entity_Id := E;
3401 if Has_Non_Limited_View (Typ) then
3402 Typ := Non_Limited_View (Typ);
3405 if Ekind (Typ) = E_Anonymous_Access_Type then
3406 DDT := Directly_Designated_Type (Typ);
3408 if Has_Non_Limited_View (DDT) then
3409 DDT := Non_Limited_View (DDT);
3412 return Is_Class_Wide_Type (DDT) and then Is_Interface (DDT);
3414 return Is_Class_Wide_Type (Typ) and then Is_Interface (Typ);
3416 end Is_Class_Wide_Interface_Type;
3418 -------------------------
3419 -- Is_Direct_Deep_Call --
3420 -------------------------
3422 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
3424 if Is_TSS (Subp, TSS_Deep_Adjust)
3425 or else Is_TSS (Subp, TSS_Deep_Finalize)
3426 or else Is_TSS (Subp, TSS_Deep_Initialize)
3433 Actual := First (Parameter_Associations (Call_Node));
3434 Formal := First_Formal (Subp);
3435 while Present (Actual)
3436 and then Present (Formal)
3438 if Nkind (Actual) = N_Identifier
3439 and then Is_Controlling_Actual (Actual)
3440 and then Etype (Actual) = Etype (Formal)
3446 Next_Formal (Formal);
3452 end Is_Direct_Deep_Call;
3458 function New_Value (From : Node_Id) return Node_Id is
3459 Res : constant Node_Id := Duplicate_Subexpr (From);
3461 if Is_Access_Type (Etype (From)) then
3462 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
3470 Remote : constant Boolean := Is_Remote_Call (Call_Node);
3473 Orig_Subp : Entity_Id := Empty;
3474 Param_Count : Positive;
3475 Parent_Formal : Entity_Id;
3476 Parent_Subp : Entity_Id;
3480 CW_Interface_Formals_Present : Boolean := False;
3482 -- Start of processing for Expand_Call_Helper
3485 Post_Call := New_List;
3487 -- Expand the function or procedure call if the first actual has a
3488 -- declared dimension aspect, and the subprogram is declared in one
3489 -- of the dimension I/O packages.
3491 if Ada_Version >= Ada_2012
3492 and then Nkind (Call_Node) in N_Subprogram_Call
3493 and then Present (Parameter_Associations (Call_Node))
3495 Expand_Put_Call_With_Symbol (Call_Node);
3498 -- Ignore if previous error
3500 if Nkind (Call_Node) in N_Has_Etype
3501 and then Etype (Call_Node) = Any_Type
3506 -- Call using access to subprogram with explicit dereference
3508 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3509 Subp := Etype (Name (Call_Node));
3510 Parent_Subp := Empty;
3512 -- Case of call to simple entry, where the Name is a selected component
3513 -- whose prefix is the task, and whose selector name is the entry name
3515 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
3516 Subp := Entity (Selector_Name (Name (Call_Node)));
3517 Parent_Subp := Empty;
3519 -- Case of call to member of entry family, where Name is an indexed
3520 -- component, with the prefix being a selected component giving the
3521 -- task and entry family name, and the index being the entry index.
3523 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
3524 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
3525 Parent_Subp := Empty;
3530 Subp := Entity (Name (Call_Node));
3531 Parent_Subp := Alias (Subp);
3533 -- Replace call to Raise_Exception by call to Raise_Exception_Always
3534 -- if we can tell that the first parameter cannot possibly be null.
3535 -- This improves efficiency by avoiding a run-time test.
3537 -- We do not do this if Raise_Exception_Always does not exist, which
3538 -- can happen in configurable run time profiles which provide only a
3541 if Is_RTE (Subp, RE_Raise_Exception)
3542 and then RTE_Available (RE_Raise_Exception_Always)
3545 FA : constant Node_Id :=
3546 Original_Node (First_Actual (Call_Node));
3549 -- The case we catch is where the first argument is obtained
3550 -- using the Identity attribute (which must always be
3553 if Nkind (FA) = N_Attribute_Reference
3554 and then Attribute_Name (FA) = Name_Identity
3556 Subp := RTE (RE_Raise_Exception_Always);
3557 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
3562 if Ekind (Subp) = E_Entry then
3563 Parent_Subp := Empty;
3567 -- Ada 2005 (AI-345): We have a procedure call as a triggering
3568 -- alternative in an asynchronous select or as an entry call in
3569 -- a conditional or timed select. Check whether the procedure call
3570 -- is a renaming of an entry and rewrite it as an entry call.
3572 if Ada_Version >= Ada_2005
3573 and then Nkind (Call_Node) = N_Procedure_Call_Statement
3575 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
3576 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
3578 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
3579 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
3583 Ren_Root : Entity_Id := Subp;
3586 -- This may be a chain of renamings, find the root
3588 if Present (Alias (Ren_Root)) then
3589 Ren_Root := Alias (Ren_Root);
3592 if Present (Parent (Ren_Root))
3593 and then Present (Original_Node (Parent (Parent (Ren_Root))))
3595 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
3597 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
3599 Make_Entry_Call_Statement (Loc,
3601 New_Copy_Tree (Name (Ren_Decl)),
3602 Parameter_Associations =>
3604 (Parameter_Associations (Call_Node))));
3612 -- If this is a call to a predicate function, try to constant fold it
3614 if Nkind (Call_Node) = N_Function_Call
3615 and then Is_Entity_Name (Name (Call_Node))
3616 and then Is_Predicate_Function (Subp)
3617 and then Can_Fold_Predicate_Call (Subp)
3622 if Transform_Function_Array
3623 and then Nkind (Call_Node) = N_Function_Call
3624 and then Is_Entity_Name (Name (Call_Node))
3627 Func_Id : constant Entity_Id :=
3628 Ultimate_Alias (Entity (Name (Call_Node)));
3630 -- When generating C code, transform a function call that returns
3631 -- a constrained array type into procedure form.
3633 if Rewritten_For_C (Func_Id) then
3635 -- For internally generated calls ensure that they reference
3636 -- the entity of the spec of the called function (needed since
3637 -- the expander may generate calls using the entity of their
3640 if not Comes_From_Source (Call_Node)
3641 and then Nkind (Unit_Declaration_Node (Func_Id)) =
3644 Set_Entity (Name (Call_Node),
3645 Corresponding_Function
3646 (Corresponding_Procedure (Func_Id)));
3649 Rewrite_Function_Call_For_C (Call_Node);
3652 -- Also introduce a temporary for functions that return a record
3653 -- called within another procedure or function call, since records
3654 -- are passed by pointer in the generated C code, and we cannot
3655 -- take a pointer from a subprogram call.
3657 elsif Modify_Tree_For_C
3658 and then Nkind (Parent (Call_Node)) in N_Subprogram_Call
3659 and then Is_Record_Type (Etype (Func_Id))
3662 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
3667 -- Temp : ... := Func_Call (...);
3670 Make_Object_Declaration (Loc,
3671 Defining_Identifier => Temp_Id,
3672 Object_Definition =>
3673 New_Occurrence_Of (Etype (Func_Id), Loc),
3675 Make_Function_Call (Loc,
3677 New_Occurrence_Of (Func_Id, Loc),
3678 Parameter_Associations =>
3679 Parameter_Associations (Call_Node)));
3681 Insert_Action (Parent (Call_Node), Decl);
3682 Rewrite (Call_Node, New_Occurrence_Of (Temp_Id, Loc));
3689 -- First step, compute extra actuals, corresponding to any Extra_Formals
3690 -- present. Note that we do not access Extra_Formals directly, instead
3691 -- we simply note the presence of the extra formals as we process the
3692 -- regular formals collecting corresponding actuals in Extra_Actuals.
3694 -- We also generate any required range checks for actuals for in formals
3695 -- as we go through the loop, since this is a convenient place to do it.
3696 -- (Though it seems that this would be better done in Expand_Actuals???)
3698 -- Special case: Thunks must not compute the extra actuals; they must
3699 -- just propagate to the target primitive their extra actuals.
3701 if Is_Thunk (Current_Scope)
3702 and then Thunk_Entity (Current_Scope) = Subp
3703 and then Present (Extra_Formals (Subp))
3705 pragma Assert (Present (Extra_Formals (Current_Scope)));
3708 Target_Formal : Entity_Id;
3709 Thunk_Formal : Entity_Id;
3712 Target_Formal := Extra_Formals (Subp);
3713 Thunk_Formal := Extra_Formals (Current_Scope);
3714 while Present (Target_Formal) loop
3716 (Expr => New_Occurrence_Of (Thunk_Formal, Loc),
3717 EF => Thunk_Formal);
3719 Target_Formal := Extra_Formal (Target_Formal);
3720 Thunk_Formal := Extra_Formal (Thunk_Formal);
3723 while Is_Non_Empty_List (Extra_Actuals) loop
3724 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3727 Expand_Actuals (Call_Node, Subp, Post_Call);
3728 pragma Assert (Is_Empty_List (Post_Call));
3729 pragma Assert (Check_Number_Of_Actuals (Call_Node, Subp));
3730 pragma Assert (Check_BIP_Actuals (Call_Node, Subp));
3735 Formal := First_Formal (Subp);
3736 Actual := First_Actual (Call_Node);
3738 while Present (Formal) loop
3739 -- Prepare to examine current entry
3743 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
3744 -- to expand it in a further round.
3746 CW_Interface_Formals_Present :=
3747 CW_Interface_Formals_Present
3748 or else Is_Class_Wide_Interface_Type (Etype (Formal));
3750 -- Create possible extra actual for constrained case. Usually, the
3751 -- extra actual is of the form actual'constrained, but since this
3752 -- attribute is only available for unconstrained records, TRUE is
3753 -- expanded if the type of the formal happens to be constrained (for
3754 -- instance when this procedure is inherited from an unconstrained
3755 -- record to a constrained one) or if the actual has no discriminant
3756 -- (its type is constrained). An exception to this is the case of a
3757 -- private type without discriminants. In this case we pass FALSE
3758 -- because the object has underlying discriminants with defaults.
3760 if Present (Extra_Constrained (Formal)) then
3761 if Ekind (Etype (Prev)) in Private_Kind
3762 and then not Has_Discriminants (Base_Type (Etype (Prev)))
3765 (Expr => New_Occurrence_Of (Standard_False, Loc),
3766 EF => Extra_Constrained (Formal));
3768 elsif Is_Constrained (Etype (Formal))
3769 or else not Has_Discriminants (Etype (Prev))
3772 (Expr => New_Occurrence_Of (Standard_True, Loc),
3773 EF => Extra_Constrained (Formal));
3775 -- Do not produce extra actuals for Unchecked_Union parameters.
3776 -- Jump directly to the end of the loop.
3778 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
3779 goto Skip_Extra_Actual_Generation;
3782 -- If the actual is a type conversion, then the constrained
3783 -- test applies to the actual, not the target type.
3789 -- Test for unchecked conversions as well, which can occur
3790 -- as out parameter actuals on calls to stream procedures.
3793 while Nkind (Act_Prev) in N_Type_Conversion
3794 | N_Unchecked_Type_Conversion
3796 Act_Prev := Expression (Act_Prev);
3799 -- If the expression is a conversion of a dereference, this
3800 -- is internally generated code that manipulates addresses,
3801 -- e.g. when building interface tables. No check should
3802 -- occur in this case, and the discriminated object is not
3803 -- directly at hand.
3805 if not Comes_From_Source (Actual)
3806 and then Nkind (Actual) = N_Unchecked_Type_Conversion
3807 and then Nkind (Act_Prev) = N_Explicit_Dereference
3810 (Expr => New_Occurrence_Of (Standard_False, Loc),
3811 EF => Extra_Constrained (Formal));
3816 Make_Attribute_Reference (Sloc (Prev),
3818 Duplicate_Subexpr_No_Checks
3819 (Act_Prev, Name_Req => True),
3820 Attribute_Name => Name_Constrained),
3821 EF => Extra_Constrained (Formal));
3827 -- Create possible extra actual for accessibility level
3829 if Present (Extra_Accessibility (Formal)) then
3830 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
3831 -- accessibility levels.
3833 if Is_Thunk (Current_Scope) then
3835 Parm_Ent : Entity_Id;
3838 if Is_Controlling_Actual (Actual) then
3840 -- Find the corresponding actual of the thunk
3842 Parm_Ent := First_Entity (Current_Scope);
3843 for J in 2 .. Param_Count loop
3844 Next_Entity (Parm_Ent);
3847 -- Handle unchecked conversion of access types generated
3848 -- in thunks (cf. Expand_Interface_Thunk).
3850 elsif Is_Access_Type (Etype (Actual))
3851 and then Nkind (Actual) = N_Unchecked_Type_Conversion
3853 Parm_Ent := Entity (Expression (Actual));
3855 else pragma Assert (Is_Entity_Name (Actual));
3856 Parm_Ent := Entity (Actual);
3862 (Get_Dynamic_Accessibility (Parm_Ent), Loc),
3863 EF => Extra_Accessibility (Formal));
3866 -- Conditional expressions
3868 elsif Nkind (Prev) = N_Expression_With_Actions
3869 and then Nkind (Original_Node (Prev)) in
3870 N_If_Expression | N_Case_Expression
3872 Add_Cond_Expression_Extra_Actual (Formal);
3874 -- Internal constant generated to remove side effects (normally
3875 -- from the expansion of dispatching calls).
3877 -- First verify the actual is internal
3879 elsif not Comes_From_Source (Prev)
3880 and then Original_Node (Prev) = Prev
3882 -- Next check that the actual is a constant
3884 and then Nkind (Prev) = N_Identifier
3885 and then Ekind (Entity (Prev)) = E_Constant
3886 and then Nkind (Parent (Entity (Prev))) = N_Object_Declaration
3888 -- Generate the accessibility level based on the expression in
3889 -- the constant's declaration.
3892 (Expr => Accessibility_Level
3893 (Expr => Expression (Parent (Entity (Prev))),
3894 Level => Dynamic_Level),
3895 EF => Extra_Accessibility (Formal));
3901 (Expr => Accessibility_Level (Prev, Dynamic_Level),
3902 EF => Extra_Accessibility (Formal));
3906 -- Perform the check of 4.6(49) that prevents a null value from being
3907 -- passed as an actual to an access parameter. Note that the check
3908 -- is elided in the common cases of passing an access attribute or
3909 -- access parameter as an actual. Also, we currently don't enforce
3910 -- this check for expander-generated actuals and when -gnatdj is set.
3912 if Ada_Version >= Ada_2005 then
3914 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
3915 -- the intent of 6.4.1(13) is that null-exclusion checks should
3916 -- not be done for 'out' parameters, even though it refers only
3917 -- to constraint checks, and a null_exclusion is not a constraint.
3918 -- Note that AI05-0196-1 corrects this mistake in the RM.
3920 if Is_Access_Type (Etype (Formal))
3921 and then Can_Never_Be_Null (Etype (Formal))
3922 and then Ekind (Formal) /= E_Out_Parameter
3923 and then Nkind (Prev) /= N_Raise_Constraint_Error
3924 and then (Known_Null (Prev)
3925 or else not Can_Never_Be_Null (Etype (Prev)))
3927 Install_Null_Excluding_Check (Prev);
3930 -- Ada_Version < Ada_2005
3933 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
3934 or else Access_Checks_Suppressed (Subp)
3938 elsif Debug_Flag_J then
3941 elsif not Comes_From_Source (Prev) then
3944 elsif Is_Entity_Name (Prev)
3945 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
3949 elsif Nkind (Prev) in N_Allocator | N_Attribute_Reference then
3953 Install_Null_Excluding_Check (Prev);
3957 -- Perform appropriate validity checks on parameters that
3960 if Validity_Checks_On then
3961 if (Ekind (Formal) = E_In_Parameter
3962 and then Validity_Check_In_Params)
3964 (Ekind (Formal) = E_In_Out_Parameter
3965 and then Validity_Check_In_Out_Params)
3967 -- If the actual is an indexed component of a packed type (or
3968 -- is an indexed or selected component whose prefix recursively
3969 -- meets this condition), it has not been expanded yet. It will
3970 -- be copied in the validity code that follows, and has to be
3971 -- expanded appropriately, so reanalyze it.
3973 -- What we do is just to unset analyzed bits on prefixes till
3974 -- we reach something that does not have a prefix.
3981 while Nkind (Nod) in
3982 N_Indexed_Component | N_Selected_Component
3984 Set_Analyzed (Nod, False);
3985 Nod := Prefix (Nod);
3989 Ensure_Valid (Actual);
3993 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3994 -- since this is a left side reference. We only do this for calls
3995 -- from the source program since we assume that compiler generated
3996 -- calls explicitly generate any required checks. We also need it
3997 -- only if we are doing standard validity checks, since clearly it is
3998 -- not needed if validity checks are off, and in subscript validity
3999 -- checking mode, all indexed components are checked with a call
4000 -- directly from Expand_N_Indexed_Component.
4002 if Comes_From_Source (Call_Node)
4003 and then Ekind (Formal) /= E_In_Parameter
4004 and then Validity_Checks_On
4005 and then Validity_Check_Default
4006 and then not Validity_Check_Subscripts
4008 Check_Valid_Lvalue_Subscripts (Actual);
4011 -- Mark any scalar OUT parameter that is a simple variable as no
4012 -- longer known to be valid (unless the type is always valid). This
4013 -- reflects the fact that if an OUT parameter is never set in a
4014 -- procedure, then it can become invalid on the procedure return.
4016 if Ekind (Formal) = E_Out_Parameter
4017 and then Is_Entity_Name (Actual)
4018 and then Ekind (Entity (Actual)) = E_Variable
4019 and then not Is_Known_Valid (Etype (Actual))
4021 Set_Is_Known_Valid (Entity (Actual), False);
4024 -- For an OUT or IN OUT parameter, if the actual is an entity, then
4025 -- clear current values, since they can be clobbered. We are probably
4026 -- doing this in more places than we need to, but better safe than
4027 -- sorry when it comes to retaining bad current values.
4029 if Ekind (Formal) /= E_In_Parameter
4030 and then Is_Entity_Name (Actual)
4031 and then Present (Entity (Actual))
4034 Ent : constant Entity_Id := Entity (Actual);
4038 -- For an OUT or IN OUT parameter that is an assignable entity,
4039 -- we do not want to clobber the Last_Assignment field, since
4040 -- if it is set, it was precisely because it is indeed an OUT
4041 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
4042 -- since the subprogram could have returned in invalid value.
4044 if Is_Assignable (Ent) then
4045 Sav := Last_Assignment (Ent);
4046 Kill_Current_Values (Ent);
4047 Set_Last_Assignment (Ent, Sav);
4048 Set_Is_Known_Valid (Ent, False);
4049 Set_Is_True_Constant (Ent, False);
4051 -- For all other cases, just kill the current values
4054 Kill_Current_Values (Ent);
4059 -- If the formal is class wide and the actual is an aggregate, force
4060 -- evaluation so that the back end who does not know about class-wide
4061 -- type, does not generate a temporary of the wrong size.
4063 if not Is_Class_Wide_Type (Etype (Formal)) then
4066 elsif Nkind (Actual) = N_Aggregate
4067 or else (Nkind (Actual) = N_Qualified_Expression
4068 and then Nkind (Expression (Actual)) = N_Aggregate)
4070 Force_Evaluation (Actual);
4073 -- In a remote call, if the formal is of a class-wide type, check
4074 -- that the actual meets the requirements described in E.4(18).
4076 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
4077 Insert_Action (Actual,
4078 Make_Transportable_Check (Loc,
4079 Duplicate_Subexpr_Move_Checks (Actual)));
4082 -- Perform invariant checks for all intermediate types in a view
4083 -- conversion after successful return from a call that passes the
4084 -- view conversion as an IN OUT or OUT parameter (RM 7.3.2 (12/3,
4085 -- 13/3, 14/3)). Consider only source conversion in order to avoid
4086 -- generating spurious checks on complex expansion such as object
4087 -- initialization through an extension aggregate.
4089 if Comes_From_Source (Call_Node)
4090 and then Ekind (Formal) /= E_In_Parameter
4091 and then Nkind (Actual) = N_Type_Conversion
4093 Add_View_Conversion_Invariants (Formal, Actual);
4096 -- Generating C the initialization of an allocator is performed by
4097 -- means of individual statements, and hence it must be done before
4100 if Modify_Tree_For_C
4101 and then Nkind (Actual) = N_Allocator
4102 and then Nkind (Expression (Actual)) = N_Qualified_Expression
4104 Remove_Side_Effects (Actual);
4107 -- This label is required when skipping extra actual generation for
4108 -- Unchecked_Union parameters.
4110 <<Skip_Extra_Actual_Generation>>
4112 Param_Count := Param_Count + 1;
4113 Next_Actual (Actual);
4114 Next_Formal (Formal);
4117 -- If we are calling an Ada 2012 function which needs to have the
4118 -- "accessibility level determined by the point of call" (AI05-0234)
4119 -- passed in to it, then pass it in.
4121 if Ekind (Subp) in E_Function | E_Operator | E_Subprogram_Type
4123 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
4126 Extra_Form : Node_Id := Empty;
4127 Level : Node_Id := Empty;
4130 -- Detect cases where the function call has been internally
4131 -- generated by examining the original node and return library
4132 -- level - taking care to avoid ignoring function calls expanded
4133 -- in prefix notation.
4135 if Nkind (Original_Node (Call_Node)) not in N_Function_Call
4136 | N_Selected_Component
4137 | N_Indexed_Component
4139 Level := Make_Integer_Literal
4140 (Loc, Scope_Depth (Standard_Standard));
4142 -- Otherwise get the level normally based on the call node
4145 Level := Accessibility_Level (Call_Node, Dynamic_Level);
4149 -- It may be possible that we are re-expanding an already
4150 -- expanded call when are are dealing with dispatching ???
4152 if not Present (Parameter_Associations (Call_Node))
4153 or else Nkind (Last (Parameter_Associations (Call_Node)))
4154 /= N_Parameter_Association
4155 or else not Is_Accessibility_Actual
4156 (Last (Parameter_Associations (Call_Node)))
4158 Extra_Form := Extra_Accessibility_Of_Result
4159 (Ultimate_Alias (Subp));
4168 -- If we are expanding the RHS of an assignment we need to check if tag
4169 -- propagation is needed. You might expect this processing to be in
4170 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
4171 -- assignment might be transformed to a declaration for an unconstrained
4172 -- value if the expression is classwide.
4174 if Nkind (Call_Node) = N_Function_Call
4175 and then Is_Tag_Indeterminate (Call_Node)
4176 and then Is_Entity_Name (Name (Call_Node))
4179 Ass : Node_Id := Empty;
4182 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
4183 Ass := Parent (Call_Node);
4185 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
4186 and then Nkind (Parent (Parent (Call_Node))) =
4187 N_Assignment_Statement
4189 Ass := Parent (Parent (Call_Node));
4191 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
4192 and then Nkind (Parent (Parent (Call_Node))) =
4193 N_Assignment_Statement
4195 Ass := Parent (Parent (Call_Node));
4199 and then Is_Class_Wide_Type (Etype (Name (Ass)))
4201 -- Move the error messages below to sem???
4203 if Is_Access_Type (Etype (Call_Node)) then
4204 if Designated_Type (Etype (Call_Node)) /=
4205 Root_Type (Etype (Name (Ass)))
4208 ("tag-indeterminate expression must have designated "
4209 & "type& (RM 5.2 (6))",
4210 Call_Node, Root_Type (Etype (Name (Ass))));
4212 Propagate_Tag (Name (Ass), Call_Node);
4215 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
4217 ("tag-indeterminate expression must have type & "
4219 Call_Node, Root_Type (Etype (Name (Ass))));
4222 Propagate_Tag (Name (Ass), Call_Node);
4225 -- The call will be rewritten as a dispatching call, and
4226 -- expanded as such.
4233 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
4234 -- it to point to the correct secondary virtual table.
4236 if Nkind (Call_Node) in N_Subprogram_Call
4237 and then CW_Interface_Formals_Present
4239 Expand_Interface_Actuals (Call_Node);
4242 -- Deals with Dispatch_Call if we still have a call, before expanding
4243 -- extra actuals since this will be done on the re-analysis of the
4244 -- dispatching call. Note that we do not try to shorten the actual list
4245 -- for a dispatching call, it would not make sense to do so. Expansion
4246 -- of dispatching calls is suppressed for VM targets, because the VM
4247 -- back-ends directly handle the generation of dispatching calls and
4248 -- would have to undo any expansion to an indirect call.
4250 if Nkind (Call_Node) in N_Subprogram_Call
4251 and then Present (Controlling_Argument (Call_Node))
4253 if Tagged_Type_Expansion then
4254 Expand_Dispatching_Call (Call_Node);
4256 -- Expand_Dispatching_Call takes care of all the needed processing
4264 Call_Typ : constant Entity_Id := Etype (Call_Node);
4265 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
4266 Eq_Prim_Op : Entity_Id := Empty;
4269 Prev_Call : Node_Id;
4272 Apply_Tag_Checks (Call_Node);
4274 if not Is_Limited_Type (Typ) then
4275 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
4278 -- If this is a dispatching "=", we must first compare the
4279 -- tags so we generate: x.tag = y.tag and then x = y
4281 if Subp = Eq_Prim_Op then
4283 -- Mark the node as analyzed to avoid reanalyzing this
4284 -- dispatching call (which would cause a never-ending loop)
4286 Prev_Call := Relocate_Node (Call_Node);
4287 Set_Analyzed (Prev_Call);
4289 Param := First_Actual (Call_Node);
4295 Make_Selected_Component (Loc,
4296 Prefix => New_Value (Param),
4299 (First_Tag_Component (Typ), Loc)),
4302 Make_Selected_Component (Loc,
4304 Unchecked_Convert_To (Typ,
4305 New_Value (Next_Actual (Param))),
4308 (First_Tag_Component (Typ), Loc))),
4309 Right_Opnd => Prev_Call);
4311 Rewrite (Call_Node, New_Call);
4313 (Call_Node, Call_Typ, Suppress => All_Checks);
4316 -- Expansion of a dispatching call results in an indirect call,
4317 -- which in turn causes current values to be killed (see
4318 -- Resolve_Call), so on VM targets we do the call here to
4319 -- ensure consistent warnings between VM and non-VM targets.
4321 Kill_Current_Values;
4323 -- If this is a dispatching "=" then we must update the reference
4324 -- to the call node because we generated:
4325 -- x.tag = y.tag and then x = y
4327 if Subp = Eq_Prim_Op then
4328 Call_Node := Right_Opnd (Call_Node);
4333 -- Similarly, expand calls to RCI subprograms on which pragma
4334 -- All_Calls_Remote applies. The rewriting will be reanalyzed
4335 -- later. Do this only when the call comes from source since we
4336 -- do not want such a rewriting to occur in expanded code.
4338 if Is_All_Remote_Call (Call_Node) then
4339 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
4341 -- Similarly, do not add extra actuals for an entry call whose entity
4342 -- is a protected procedure, or for an internal protected subprogram
4343 -- call, because it will be rewritten as a protected subprogram call
4344 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
4346 elsif Is_Protected_Type (Scope (Subp))
4347 and then Ekind (Subp) in E_Procedure | E_Function
4351 -- During that loop we gathered the extra actuals (the ones that
4352 -- correspond to Extra_Formals), so now they can be appended.
4355 while Is_Non_Empty_List (Extra_Actuals) loop
4356 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
4360 -- At this point we have all the actuals, so this is the point at which
4361 -- the various expansion activities for actuals is carried out.
4363 Expand_Actuals (Call_Node, Subp, Post_Call);
4365 -- If it is a recursive call then call the internal procedure that
4366 -- verifies Subprogram_Variant contract (if present and enabled).
4367 -- Detecting calls to subprogram aliases is necessary for recursive
4368 -- calls in instances of generic subprograms, where the renaming of
4369 -- the current subprogram is called.
4371 if Is_Subprogram (Subp)
4372 and then Same_Or_Aliased_Subprograms (Subp, Current_Scope)
4374 Check_Subprogram_Variant;
4377 -- Verify that the actuals do not share storage. This check must be done
4378 -- on the caller side rather that inside the subprogram to avoid issues
4379 -- of parameter passing.
4381 if Check_Aliasing_Of_Parameters then
4382 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
4385 -- If the subprogram is a renaming, or if it is inherited, replace it in
4386 -- the call with the name of the actual subprogram being called. If this
4387 -- is a dispatching call, the run-time decides what to call. The Alias
4388 -- attribute does not apply to entries.
4390 if Nkind (Call_Node) /= N_Entry_Call_Statement
4391 and then No (Controlling_Argument (Call_Node))
4392 and then Present (Parent_Subp)
4393 and then not Is_Direct_Deep_Call (Subp)
4395 if Present (Inherited_From_Formal (Subp)) then
4396 Parent_Subp := Inherited_From_Formal (Subp);
4398 Parent_Subp := Ultimate_Alias (Parent_Subp);
4401 -- The below setting of Entity is suspect, see F109-018 discussion???
4403 Set_Entity (Name (Call_Node), Parent_Subp);
4405 -- Move this check to sem???
4407 if Is_Abstract_Subprogram (Parent_Subp)
4408 and then not In_Instance
4411 ("cannot call abstract subprogram &!",
4412 Name (Call_Node), Parent_Subp);
4415 -- Inspect all formals of derived subprogram Subp. Compare parameter
4416 -- types with the parent subprogram and check whether an actual may
4417 -- need a type conversion to the corresponding formal of the parent
4420 -- Not clear whether intrinsic subprograms need such conversions. ???
4422 if not Is_Intrinsic_Subprogram (Parent_Subp)
4423 or else Is_Generic_Instance (Parent_Subp)
4426 procedure Convert (Act : Node_Id; Typ : Entity_Id);
4427 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
4428 -- and resolve the newly generated construct.
4434 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
4436 Rewrite (Act, OK_Convert_To (Typ, Act));
4437 Analyze_And_Resolve (Act, Typ);
4442 Actual_Typ : Entity_Id;
4443 Formal_Typ : Entity_Id;
4444 Parent_Typ : Entity_Id;
4447 Actual := First_Actual (Call_Node);
4448 Formal := First_Formal (Subp);
4449 Parent_Formal := First_Formal (Parent_Subp);
4450 while Present (Formal) loop
4451 Actual_Typ := Etype (Actual);
4452 Formal_Typ := Etype (Formal);
4453 Parent_Typ := Etype (Parent_Formal);
4455 -- For an IN parameter of a scalar type, the derived formal
4456 -- type and parent formal type differ, and the parent formal
4457 -- type and actual type do not match statically.
4459 if Is_Scalar_Type (Formal_Typ)
4460 and then Ekind (Formal) = E_In_Parameter
4461 and then Formal_Typ /= Parent_Typ
4463 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
4464 and then not Raises_Constraint_Error (Actual)
4466 Convert (Actual, Parent_Typ);
4468 -- For access types, the parent formal type and actual type
4471 elsif Is_Access_Type (Formal_Typ)
4472 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
4474 if Ekind (Formal) /= E_In_Parameter then
4475 Convert (Actual, Parent_Typ);
4477 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
4478 and then Designated_Type (Parent_Typ) /=
4479 Designated_Type (Actual_Typ)
4480 and then not Is_Controlling_Formal (Formal)
4482 -- This unchecked conversion is not necessary unless
4483 -- inlining is enabled, because in that case the type
4484 -- mismatch may become visible in the body about to be
4488 Unchecked_Convert_To (Parent_Typ, Actual));
4489 Analyze_And_Resolve (Actual, Parent_Typ);
4492 -- If there is a change of representation, then generate a
4493 -- warning, and do the change of representation.
4495 elsif not Has_Compatible_Representation
4496 (Target_Type => Formal_Typ,
4497 Operand_Type => Parent_Typ)
4500 ("??change of representation required", Actual);
4501 Convert (Actual, Parent_Typ);
4503 -- For array and record types, the parent formal type and
4504 -- derived formal type have different sizes or pragma Pack
4507 elsif ((Is_Array_Type (Formal_Typ)
4508 and then Is_Array_Type (Parent_Typ))
4510 (Is_Record_Type (Formal_Typ)
4511 and then Is_Record_Type (Parent_Typ)))
4513 (Esize (Formal_Typ) /= Esize (Parent_Typ)
4514 or else Has_Pragma_Pack (Formal_Typ) /=
4515 Has_Pragma_Pack (Parent_Typ))
4517 Convert (Actual, Parent_Typ);
4520 Next_Actual (Actual);
4521 Next_Formal (Formal);
4522 Next_Formal (Parent_Formal);
4528 Subp := Parent_Subp;
4531 -- Deal with case where call is an explicit dereference
4533 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
4535 -- Handle case of access to protected subprogram type
4537 if Is_Access_Protected_Subprogram_Type
4538 (Base_Type (Etype (Prefix (Name (Call_Node)))))
4540 -- If this is a call through an access to protected operation, the
4541 -- prefix has the form (object'address, operation'access). Rewrite
4542 -- as a for other protected calls: the object is the 1st parameter
4543 -- of the list of actuals.
4550 Ptr : constant Node_Id := Prefix (Name (Call_Node));
4552 T : constant Entity_Id :=
4553 Equivalent_Type (Base_Type (Etype (Ptr)));
4555 D_T : constant Entity_Id :=
4556 Designated_Type (Base_Type (Etype (Ptr)));
4560 Make_Selected_Component (Loc,
4561 Prefix => Unchecked_Convert_To (T, Ptr),
4563 New_Occurrence_Of (First_Entity (T), Loc));
4566 Make_Selected_Component (Loc,
4567 Prefix => Unchecked_Convert_To (T, Ptr),
4569 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
4572 Make_Explicit_Dereference (Loc,
4575 if Present (Parameter_Associations (Call_Node)) then
4576 Parm := Parameter_Associations (Call_Node);
4581 Prepend (Obj, Parm);
4583 if Etype (D_T) = Standard_Void_Type then
4585 Make_Procedure_Call_Statement (Loc,
4587 Parameter_Associations => Parm);
4590 Make_Function_Call (Loc,
4592 Parameter_Associations => Parm);
4595 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
4596 Set_Etype (Call, Etype (D_T));
4598 -- We do not re-analyze the call to avoid infinite recursion.
4599 -- We analyze separately the prefix and the object, and set
4600 -- the checks on the prefix that would otherwise be emitted
4601 -- when resolving a call.
4603 Rewrite (Call_Node, Call);
4605 Apply_Access_Check (Nam);
4612 -- If this is a call to an intrinsic subprogram, then perform the
4613 -- appropriate expansion to the corresponding tree node and we
4614 -- are all done (since after that the call is gone).
4616 -- In the case where the intrinsic is to be processed by the back end,
4617 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
4618 -- since the idea in this case is to pass the call unchanged. If the
4619 -- intrinsic is an inherited unchecked conversion, and the derived type
4620 -- is the target type of the conversion, we must retain it as the return
4621 -- type of the expression. Otherwise the expansion below, which uses the
4622 -- parent operation, will yield the wrong type.
4624 if Is_Intrinsic_Subprogram (Subp) then
4625 Expand_Intrinsic_Call (Call_Node, Subp);
4627 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
4628 and then Parent_Subp /= Orig_Subp
4629 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
4631 Set_Etype (Call_Node, Etype (Orig_Subp));
4637 if Ekind (Subp) in E_Function | E_Procedure then
4639 -- We perform a simple optimization on calls for To_Address by
4640 -- replacing them with an unchecked conversion. Not only is this
4641 -- efficient, but it also avoids order of elaboration problems when
4642 -- address clauses are inlined (address expression elaborated at the
4645 -- We perform this optimization regardless of whether we are in the
4646 -- main unit or in a unit in the context of the main unit, to ensure
4647 -- that the generated tree is the same in both cases, for CodePeer
4650 if Is_RTE (Subp, RE_To_Address) then
4652 Unchecked_Convert_To
4653 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
4656 -- A call to a null procedure is replaced by a null statement, but we
4657 -- are not allowed to ignore possible side effects of the call, so we
4658 -- make sure that actuals are evaluated.
4659 -- We also suppress this optimization for GNATcoverage.
4661 elsif Is_Null_Procedure (Subp)
4662 and then not Opt.Suppress_Control_Flow_Optimizations
4664 Actual := First_Actual (Call_Node);
4665 while Present (Actual) loop
4666 Remove_Side_Effects (Actual);
4667 Next_Actual (Actual);
4670 Rewrite (Call_Node, Make_Null_Statement (Loc));
4674 -- Handle inlining. No action needed if the subprogram is not inlined
4676 if not Is_Inlined (Subp) then
4679 -- Front-end inlining of expression functions (performed also when
4680 -- back-end inlining is enabled).
4682 elsif Is_Inlinable_Expression_Function (Subp) then
4684 (Call_Node, New_Copy (Expression_Of_Expression_Function (Subp)));
4685 Analyze (Call_Node);
4688 -- Handle front-end inlining
4690 elsif not Back_End_Inlining then
4691 Inlined_Subprogram : declare
4693 Must_Inline : Boolean := False;
4694 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
4697 -- Verify that the body to inline has already been seen, and
4698 -- that if the body is in the current unit the inlining does
4699 -- not occur earlier. This avoids order-of-elaboration problems
4702 -- This should be documented in sinfo/einfo ???
4705 or else Nkind (Spec) /= N_Subprogram_Declaration
4706 or else No (Body_To_Inline (Spec))
4708 Must_Inline := False;
4710 -- If this an inherited function that returns a private type,
4711 -- do not inline if the full view is an unconstrained array,
4712 -- because such calls cannot be inlined.
4714 elsif Present (Orig_Subp)
4715 and then Is_Array_Type (Etype (Orig_Subp))
4716 and then not Is_Constrained (Etype (Orig_Subp))
4718 Must_Inline := False;
4720 elsif In_Unfrozen_Instance (Scope (Subp)) then
4721 Must_Inline := False;
4724 Bod := Body_To_Inline (Spec);
4726 if (In_Extended_Main_Code_Unit (Call_Node)
4727 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
4728 or else Has_Pragma_Inline_Always (Subp))
4729 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
4731 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
4733 Must_Inline := True;
4735 -- If we are compiling a package body that is not the main
4736 -- unit, it must be for inlining/instantiation purposes,
4737 -- in which case we inline the call to insure that the same
4738 -- temporaries are generated when compiling the body by
4739 -- itself. Otherwise link errors can occur.
4741 -- If the function being called is itself in the main unit,
4742 -- we cannot inline, because there is a risk of double
4743 -- elaboration and/or circularity: the inlining can make
4744 -- visible a private entity in the body of the main unit,
4745 -- that gigi will see before its sees its proper definition.
4747 elsif not In_Extended_Main_Code_Unit (Call_Node)
4748 and then In_Package_Body
4750 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
4752 -- Inline calls to _postconditions when generating C code
4754 elsif Modify_Tree_For_C
4755 and then In_Same_Extended_Unit (Sloc (Bod), Loc)
4756 and then Chars (Name (Call_Node)) = Name_uPostconditions
4758 Must_Inline := True;
4763 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4766 -- Let the back end handle it
4768 Add_Inlined_Body (Subp, Call_Node);
4770 if Front_End_Inlining
4771 and then Nkind (Spec) = N_Subprogram_Declaration
4772 and then In_Extended_Main_Code_Unit (Call_Node)
4773 and then No (Body_To_Inline (Spec))
4774 and then not Has_Completion (Subp)
4775 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
4778 ("cannot inline& (body not seen yet)?",
4782 end Inlined_Subprogram;
4784 -- Front-end expansion of simple functions returning unconstrained
4785 -- types (see Check_And_Split_Unconstrained_Function). Note that the
4786 -- case of a simple renaming (Body_To_Inline in N_Entity below, see
4787 -- also Build_Renamed_Body) cannot be expanded here because this may
4788 -- give rise to order-of-elaboration issues for the types of the
4789 -- parameters of the subprogram, if any.
4791 elsif Present (Unit_Declaration_Node (Subp))
4792 and then Nkind (Unit_Declaration_Node (Subp)) =
4793 N_Subprogram_Declaration
4794 and then Present (Body_To_Inline (Unit_Declaration_Node (Subp)))
4796 Nkind (Body_To_Inline (Unit_Declaration_Node (Subp))) not in
4799 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4801 -- Back-end inlining either if optimization is enabled or the call is
4802 -- required to be inlined.
4804 elsif Optimization_Level > 0
4805 or else Has_Pragma_Inline_Always (Subp)
4807 Add_Inlined_Body (Subp, Call_Node);
4811 -- Check for protected subprogram. This is either an intra-object call,
4812 -- or a protected function call. Protected procedure calls are rewritten
4813 -- as entry calls and handled accordingly.
4815 -- In Ada 2005, this may be an indirect call to an access parameter that
4816 -- is an access_to_subprogram. In that case the anonymous type has a
4817 -- scope that is a protected operation, but the call is a regular one.
4818 -- In either case do not expand call if subprogram is eliminated.
4820 Scop := Scope (Subp);
4822 if Nkind (Call_Node) /= N_Entry_Call_Statement
4823 and then Is_Protected_Type (Scop)
4824 and then Ekind (Subp) /= E_Subprogram_Type
4825 and then not Is_Eliminated (Subp)
4827 -- If the call is an internal one, it is rewritten as a call to the
4828 -- corresponding unprotected subprogram.
4830 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
4833 -- Functions returning controlled objects need special attention. If
4834 -- the return type is limited, then the context is initialization and
4835 -- different processing applies. If the call is to a protected function,
4836 -- the expansion above will call Expand_Call recursively. Otherwise the
4837 -- function call is transformed into a temporary which obtains the
4838 -- result from the secondary stack.
4840 if Needs_Finalization (Etype (Subp)) then
4841 if not Is_Build_In_Place_Function_Call (Call_Node)
4843 (No (First_Formal (Subp))
4845 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
4847 Expand_Ctrl_Function_Call (Call_Node);
4849 -- Build-in-place function calls which appear in anonymous contexts
4850 -- need a transient scope to ensure the proper finalization of the
4851 -- intermediate result after its use.
4853 elsif Is_Build_In_Place_Function_Call (Call_Node)
4854 and then Nkind (Parent (Unqual_Conv (Call_Node))) in
4855 N_Attribute_Reference
4857 | N_Indexed_Component
4858 | N_Object_Renaming_Declaration
4859 | N_Procedure_Call_Statement
4860 | N_Selected_Component
4863 (Ekind (Current_Scope) /= E_Loop
4864 or else Nkind (Parent (Call_Node)) /= N_Function_Call
4865 or else not Is_Build_In_Place_Function_Call
4866 (Parent (Call_Node)))
4868 Establish_Transient_Scope (Call_Node, Manage_Sec_Stack => True);
4871 end Expand_Call_Helper;
4873 -------------------------------
4874 -- Expand_Ctrl_Function_Call --
4875 -------------------------------
4877 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
4878 function Is_Element_Reference (N : Node_Id) return Boolean;
4879 -- Determine whether node N denotes a reference to an Ada 2012 container
4882 --------------------------
4883 -- Is_Element_Reference --
4884 --------------------------
4886 function Is_Element_Reference (N : Node_Id) return Boolean is
4887 Ref : constant Node_Id := Original_Node (N);
4890 -- Analysis marks an element reference by setting the generalized
4891 -- indexing attribute of an indexed component before the component
4892 -- is rewritten into a function call.
4895 Nkind (Ref) = N_Indexed_Component
4896 and then Present (Generalized_Indexing (Ref));
4897 end Is_Element_Reference;
4899 -- Start of processing for Expand_Ctrl_Function_Call
4902 -- Optimization, if the returned value (which is on the sec-stack) is
4903 -- returned again, no need to copy/readjust/finalize, we can just pass
4904 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
4905 -- attachment is needed
4907 if Nkind (Parent (N)) = N_Simple_Return_Statement then
4911 -- Resolution is now finished, make sure we don't start analysis again
4912 -- because of the duplication.
4916 -- A function which returns a controlled object uses the secondary
4917 -- stack. Rewrite the call into a temporary which obtains the result of
4918 -- the function using 'reference.
4920 Remove_Side_Effects (N);
4922 -- The side effect removal of the function call produced a temporary.
4923 -- When the context is a case expression, if expression, or expression
4924 -- with actions, the lifetime of the temporary must be extended to match
4925 -- that of the context. Otherwise the function result will be finalized
4926 -- too early and affect the result of the expression. To prevent this
4927 -- unwanted effect, the temporary should not be considered for clean up
4928 -- actions by the general finalization machinery.
4930 -- Exception to this rule are references to Ada 2012 container elements.
4931 -- Such references must be finalized at the end of each iteration of the
4932 -- related quantified expression, otherwise the container will remain
4935 if Nkind (N) = N_Explicit_Dereference
4936 and then Within_Case_Or_If_Expression (N)
4937 and then not Is_Element_Reference (N)
4939 Set_Is_Ignored_Transient (Entity (Prefix (N)));
4941 end Expand_Ctrl_Function_Call;
4943 ----------------------------------------
4944 -- Expand_N_Extended_Return_Statement --
4945 ----------------------------------------
4947 -- If there is a Handled_Statement_Sequence, we rewrite this:
4949 -- return Result : T := <expression> do
4950 -- <handled_seq_of_stms>
4956 -- Result : T := <expression>;
4958 -- <handled_seq_of_stms>
4962 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
4964 -- return Result : T := <expression>;
4968 -- return <expression>;
4970 -- unless it's build-in-place or there's no <expression>, in which case
4974 -- Result : T := <expression>;
4979 -- Note that this case could have been written by the user as an extended
4980 -- return statement, or could have been transformed to this from a simple
4981 -- return statement.
4983 -- That is, we need to have a reified return object if there are statements
4984 -- (which might refer to it) or if we're doing build-in-place (so we can
4985 -- set its address to the final resting place or if there is no expression
4986 -- (in which case default initial values might need to be set)).
4988 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
4989 Loc : constant Source_Ptr := Sloc (N);
4991 function Build_Heap_Or_Pool_Allocator
4992 (Temp_Id : Entity_Id;
4993 Temp_Typ : Entity_Id;
4994 Func_Id : Entity_Id;
4995 Ret_Typ : Entity_Id;
4996 Alloc_Expr : Node_Id) return Node_Id;
4997 -- Create the statements necessary to allocate a return object on the
4998 -- heap or user-defined storage pool. The object may need finalization
4999 -- actions depending on the return type.
5001 -- * Controlled case
5003 -- if BIPfinalizationmaster = null then
5004 -- Temp_Id := <Alloc_Expr>;
5007 -- type Ptr_Typ is access Ret_Typ;
5008 -- for Ptr_Typ'Storage_Pool use
5009 -- Base_Pool (BIPfinalizationmaster.all).all;
5013 -- procedure Allocate (...) is
5015 -- System.Storage_Pools.Subpools.Allocate_Any (...);
5018 -- Local := <Alloc_Expr>;
5019 -- Temp_Id := Temp_Typ (Local);
5023 -- * Non-controlled case
5025 -- Temp_Id := <Alloc_Expr>;
5027 -- Temp_Id is the temporary which is used to reference the internally
5028 -- created object in all allocation forms. Temp_Typ is the type of the
5029 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
5030 -- type of Func_Id. Alloc_Expr is the actual allocator.
5032 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id;
5033 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
5035 -- From current activation chain
5036 -- To activation chain passed in by the caller
5037 -- New_Master master passed in by the caller
5039 -- Func_Id is the entity of the function where the extended return
5040 -- statement appears.
5042 ----------------------------------
5043 -- Build_Heap_Or_Pool_Allocator --
5044 ----------------------------------
5046 function Build_Heap_Or_Pool_Allocator
5047 (Temp_Id : Entity_Id;
5048 Temp_Typ : Entity_Id;
5049 Func_Id : Entity_Id;
5050 Ret_Typ : Entity_Id;
5051 Alloc_Expr : Node_Id) return Node_Id
5054 pragma Assert (Is_Build_In_Place_Function (Func_Id));
5056 -- Processing for objects that require finalization actions
5058 if Needs_Finalization (Ret_Typ) then
5060 Decls : constant List_Id := New_List;
5061 Fin_Mas_Id : constant Entity_Id :=
5062 Build_In_Place_Formal
5063 (Func_Id, BIP_Finalization_Master);
5064 Orig_Expr : constant Node_Id :=
5066 (Source => Alloc_Expr,
5067 Scopes_In_EWA_OK => True);
5068 Stmts : constant List_Id := New_List;
5069 Desig_Typ : Entity_Id;
5070 Local_Id : Entity_Id;
5071 Pool_Id : Entity_Id;
5072 Ptr_Typ : Entity_Id;
5076 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
5078 Pool_Id := Make_Temporary (Loc, 'P');
5081 Make_Object_Renaming_Declaration (Loc,
5082 Defining_Identifier => Pool_Id,
5084 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
5086 Make_Explicit_Dereference (Loc,
5088 Make_Function_Call (Loc,
5090 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
5091 Parameter_Associations => New_List (
5092 Make_Explicit_Dereference (Loc,
5094 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
5096 -- Create an access type which uses the storage pool of the
5097 -- caller's master. This additional type is necessary because
5098 -- the finalization master cannot be associated with the type
5099 -- of the temporary. Otherwise the secondary stack allocation
5102 Desig_Typ := Ret_Typ;
5104 -- Ensure that the build-in-place machinery uses a fat pointer
5105 -- when allocating an unconstrained array on the heap. In this
5106 -- case the result object type is a constrained array type even
5107 -- though the function type is unconstrained.
5109 if Ekind (Desig_Typ) = E_Array_Subtype then
5110 Desig_Typ := Base_Type (Desig_Typ);
5114 -- type Ptr_Typ is access Desig_Typ;
5116 Ptr_Typ := Make_Temporary (Loc, 'P');
5119 Make_Full_Type_Declaration (Loc,
5120 Defining_Identifier => Ptr_Typ,
5122 Make_Access_To_Object_Definition (Loc,
5123 Subtype_Indication =>
5124 New_Occurrence_Of (Desig_Typ, Loc))));
5126 -- Perform minor decoration in order to set the master and the
5127 -- storage pool attributes.
5129 Mutate_Ekind (Ptr_Typ, E_Access_Type);
5130 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
5131 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
5133 -- Create the temporary, generate:
5134 -- Local_Id : Ptr_Typ;
5136 Local_Id := Make_Temporary (Loc, 'T');
5139 Make_Object_Declaration (Loc,
5140 Defining_Identifier => Local_Id,
5141 Object_Definition =>
5142 New_Occurrence_Of (Ptr_Typ, Loc)));
5144 -- Allocate the object, generate:
5145 -- Local_Id := <Alloc_Expr>;
5148 Make_Assignment_Statement (Loc,
5149 Name => New_Occurrence_Of (Local_Id, Loc),
5150 Expression => Alloc_Expr));
5153 -- Temp_Id := Temp_Typ (Local_Id);
5156 Make_Assignment_Statement (Loc,
5157 Name => New_Occurrence_Of (Temp_Id, Loc),
5159 Unchecked_Convert_To (Temp_Typ,
5160 New_Occurrence_Of (Local_Id, Loc))));
5162 -- Wrap the allocation in a block. This is further conditioned
5163 -- by checking the caller finalization master at runtime. A
5164 -- null value indicates a non-existent master, most likely due
5165 -- to a Finalize_Storage_Only allocation.
5168 -- if BIPfinalizationmaster = null then
5169 -- Temp_Id := <Orig_Expr>;
5179 Make_If_Statement (Loc,
5182 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
5183 Right_Opnd => Make_Null (Loc)),
5185 Then_Statements => New_List (
5186 Make_Assignment_Statement (Loc,
5187 Name => New_Occurrence_Of (Temp_Id, Loc),
5188 Expression => Orig_Expr)),
5190 Else_Statements => New_List (
5191 Make_Block_Statement (Loc,
5192 Declarations => Decls,
5193 Handled_Statement_Sequence =>
5194 Make_Handled_Sequence_Of_Statements (Loc,
5195 Statements => Stmts))));
5198 -- For all other cases, generate:
5199 -- Temp_Id := <Alloc_Expr>;
5203 Make_Assignment_Statement (Loc,
5204 Name => New_Occurrence_Of (Temp_Id, Loc),
5205 Expression => Alloc_Expr);
5207 end Build_Heap_Or_Pool_Allocator;
5209 ---------------------------
5210 -- Move_Activation_Chain --
5211 ---------------------------
5213 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id is
5216 Make_Procedure_Call_Statement (Loc,
5218 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
5220 Parameter_Associations => New_List (
5224 Make_Attribute_Reference (Loc,
5225 Prefix => Make_Identifier (Loc, Name_uChain),
5226 Attribute_Name => Name_Unrestricted_Access),
5228 -- Destination chain
5231 (Build_In_Place_Formal (Func_Id, BIP_Activation_Chain), Loc),
5236 (Build_In_Place_Formal (Func_Id, BIP_Task_Master), Loc)));
5237 end Move_Activation_Chain;
5241 Func_Id : constant Entity_Id :=
5242 Return_Applies_To (Return_Statement_Entity (N));
5243 Is_BIP_Func : constant Boolean :=
5244 Is_Build_In_Place_Function (Func_Id);
5245 Ret_Obj_Id : constant Entity_Id :=
5246 First_Entity (Return_Statement_Entity (N));
5247 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
5248 Ret_Typ : constant Entity_Id := Etype (Func_Id);
5255 Return_Stmt : Node_Id := Empty;
5256 -- Force initialization to facilitate static analysis
5258 -- Start of processing for Expand_N_Extended_Return_Statement
5261 -- Given that functionality of interface thunks is simple (just displace
5262 -- the pointer to the object) they are always handled by means of
5263 -- simple return statements.
5265 pragma Assert (not Is_Thunk (Current_Subprogram));
5267 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
5268 Exp := Expression (Ret_Obj_Decl);
5270 -- Assert that if F says "return R : T := G(...) do..."
5271 -- then F and G are both b-i-p, or neither b-i-p.
5273 if Nkind (Exp) = N_Function_Call then
5274 pragma Assert (Ekind (Current_Subprogram) = E_Function);
5276 (Is_Build_In_Place_Function (Current_Subprogram) =
5277 Is_Build_In_Place_Function_Call (Exp));
5281 -- Ada 2005 (AI95-344): If the result type is class-wide, then insert
5282 -- a check that the level of the return expression's underlying type
5283 -- is not deeper than the level of the master enclosing the function.
5285 -- AI12-043: The check is made immediately after the return object
5288 if Present (Exp) and then Is_Class_Wide_Type (Ret_Typ) then
5289 Apply_CW_Accessibility_Check (Exp, Func_Id);
5295 HSS := Handled_Statement_Sequence (N);
5297 -- If the returned object needs finalization actions, the function must
5298 -- perform the appropriate cleanup should it fail to return. The state
5299 -- of the function itself is tracked through a flag which is coupled
5300 -- with the scope finalizer. There is one flag per each return object
5301 -- in case of multiple returns.
5303 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
5305 Flag_Decl : Node_Id;
5306 Flag_Id : Entity_Id;
5310 -- Recover the function body
5312 Func_Bod := Unit_Declaration_Node (Func_Id);
5314 if Nkind (Func_Bod) = N_Subprogram_Declaration then
5315 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
5318 if Nkind (Func_Bod) = N_Function_Specification then
5319 Func_Bod := Parent (Func_Bod); -- one more level for child units
5322 pragma Assert (Nkind (Func_Bod) = N_Subprogram_Body);
5324 -- Create a flag to track the function state
5326 Flag_Id := Make_Temporary (Loc, 'F');
5327 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
5329 -- Insert the flag at the beginning of the function declarations,
5331 -- Fnn : Boolean := False;
5334 Make_Object_Declaration (Loc,
5335 Defining_Identifier => Flag_Id,
5336 Object_Definition =>
5337 New_Occurrence_Of (Standard_Boolean, Loc),
5339 New_Occurrence_Of (Standard_False, Loc));
5341 Prepend_To (Declarations (Func_Bod), Flag_Decl);
5342 Analyze (Flag_Decl);
5346 -- Build a simple_return_statement that returns the return object when
5347 -- there is a statement sequence, or no expression, or the analysis of
5348 -- the return object declaration generated extra actions, or the result
5349 -- will be built in place. Note however that we currently do this for
5350 -- all composite cases, even though they are not built in place.
5354 or else List_Length (Return_Object_Declarations (N)) > 1
5355 or else Is_Composite_Type (Ret_Typ)
5360 -- If the extended return has a handled statement sequence, then wrap
5361 -- it in a block and use the block as the first statement.
5365 Make_Block_Statement (Loc,
5366 Declarations => New_List,
5367 Handled_Statement_Sequence => HSS));
5370 -- If the result type contains tasks, we call Move_Activation_Chain.
5371 -- Later, the cleanup code will call Complete_Master, which will
5372 -- terminate any unactivated tasks belonging to the return statement
5373 -- master. But Move_Activation_Chain updates their master to be that
5374 -- of the caller, so they will not be terminated unless the return
5375 -- statement completes unsuccessfully due to exception, abort, goto,
5376 -- or exit. As a formality, we test whether the function requires the
5377 -- result to be built in place, though that's necessarily true for
5378 -- the case of result types with task parts.
5380 if Is_BIP_Func and then Has_Task (Ret_Typ) then
5382 -- The return expression is an aggregate for a complex type which
5383 -- contains tasks. This particular case is left unexpanded since
5384 -- the regular expansion would insert all temporaries and
5385 -- initialization code in the wrong block.
5387 if Nkind (Exp) = N_Aggregate then
5388 Expand_N_Aggregate (Exp);
5391 -- Do not move the activation chain if the return object does not
5394 if Has_Task (Etype (Ret_Obj_Id)) then
5395 Append_To (Stmts, Move_Activation_Chain (Func_Id));
5399 -- Update the state of the function right before the object is
5402 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
5404 Flag_Id : constant Entity_Id :=
5405 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
5412 Make_Assignment_Statement (Loc,
5413 Name => New_Occurrence_Of (Flag_Id, Loc),
5414 Expression => New_Occurrence_Of (Standard_True, Loc)));
5418 -- Build a simple_return_statement that returns the return object
5421 Make_Simple_Return_Statement (Loc,
5422 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
5423 Append_To (Stmts, Return_Stmt);
5425 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
5428 -- Case where we build a return statement block
5430 if Present (HSS) then
5432 Make_Block_Statement (Loc,
5433 Declarations => Return_Object_Declarations (N),
5434 Handled_Statement_Sequence => HSS);
5436 -- We set the entity of the new block statement to be that of the
5437 -- return statement. This is necessary so that various fields, such
5438 -- as Finalization_Chain_Entity carry over from the return statement
5439 -- to the block. Note that this block is unusual, in that its entity
5440 -- is an E_Return_Statement rather than an E_Block.
5443 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
5445 -- If the object decl was already rewritten as a renaming, then we
5446 -- don't want to do the object allocation and transformation of
5447 -- the return object declaration to a renaming. This case occurs
5448 -- when the return object is initialized by a call to another
5449 -- build-in-place function, and that function is responsible for
5450 -- the allocation of the return object.
5453 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
5456 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
5459 -- It is a regular BIP object declaration
5461 (Is_Build_In_Place_Function_Call
5462 (Expression (Original_Node (Ret_Obj_Decl)))
5464 -- It is a BIP object declaration that displaces the pointer
5465 -- to the object to reference a converted interface type.
5468 Present (Unqual_BIP_Iface_Function_Call
5469 (Expression (Original_Node (Ret_Obj_Decl))))));
5471 -- Return the build-in-place result by reference
5473 Set_By_Ref (Return_Stmt);
5475 elsif Is_BIP_Func then
5477 -- Locate the implicit access parameter associated with the
5478 -- caller-supplied return object and convert the return
5479 -- statement's return object declaration to a renaming of a
5480 -- dereference of the access parameter. If the return object's
5481 -- declaration includes an expression that has not already been
5482 -- expanded as separate assignments, then add an assignment
5483 -- statement to ensure the return object gets initialized.
5486 -- Result : T [:= <expression>];
5493 -- Result : T renames FuncRA.all;
5494 -- [Result := <expression;]
5499 Ret_Obj_Expr : constant Node_Id := Expression (Ret_Obj_Decl);
5500 Ret_Obj_Typ : constant Entity_Id := Etype (Ret_Obj_Id);
5502 Init_Assignment : Node_Id := Empty;
5503 Obj_Acc_Formal : Entity_Id;
5504 Obj_Acc_Deref : Node_Id;
5505 Obj_Alloc_Formal : Entity_Id;
5508 -- Build-in-place results must be returned by reference
5510 Set_By_Ref (Return_Stmt);
5512 -- Retrieve the implicit access parameter passed by the caller
5515 Build_In_Place_Formal (Func_Id, BIP_Object_Access);
5517 -- If the return object's declaration includes an expression
5518 -- and the declaration isn't marked as No_Initialization, then
5519 -- we need to generate an assignment to the object and insert
5520 -- it after the declaration before rewriting it as a renaming
5521 -- (otherwise we'll lose the initialization). The case where
5522 -- the result type is an interface (or class-wide interface)
5523 -- is also excluded because the context of the function call
5524 -- must be unconstrained, so the initialization will always
5525 -- be done as part of an allocator evaluation (storage pool
5526 -- or secondary stack), never to a constrained target object
5527 -- passed in by the caller. Besides the assignment being
5528 -- unneeded in this case, it avoids problems with trying to
5529 -- generate a dispatching assignment when the return expression
5530 -- is a nonlimited descendant of a limited interface (the
5531 -- interface has no assignment operation).
5533 if Present (Ret_Obj_Expr)
5534 and then not No_Initialization (Ret_Obj_Decl)
5535 and then not Is_Interface (Ret_Obj_Typ)
5538 Make_Assignment_Statement (Loc,
5539 Name => New_Occurrence_Of (Ret_Obj_Id, Loc),
5542 (Source => Ret_Obj_Expr,
5543 Scopes_In_EWA_OK => True));
5545 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
5546 Set_Assignment_OK (Name (Init_Assignment));
5547 Set_No_Ctrl_Actions (Init_Assignment);
5549 Set_Parent (Name (Init_Assignment), Init_Assignment);
5550 Set_Parent (Expression (Init_Assignment), Init_Assignment);
5552 Set_Expression (Ret_Obj_Decl, Empty);
5554 if Is_Class_Wide_Type (Etype (Ret_Obj_Id))
5555 and then not Is_Class_Wide_Type
5556 (Etype (Expression (Init_Assignment)))
5558 Rewrite (Expression (Init_Assignment),
5559 Make_Type_Conversion (Loc,
5561 New_Occurrence_Of (Etype (Ret_Obj_Id), Loc),
5563 Relocate_Node (Expression (Init_Assignment))));
5566 -- In the case of functions where the calling context can
5567 -- determine the form of allocation needed, initialization
5568 -- is done with each part of the if statement that handles
5569 -- the different forms of allocation (this is true for
5570 -- unconstrained, tagged, and controlled result subtypes).
5572 if not Needs_BIP_Alloc_Form (Func_Id) then
5573 Insert_After (Ret_Obj_Decl, Init_Assignment);
5577 -- When the function's subtype is unconstrained, a run-time
5578 -- test may be needed to decide the form of allocation to use
5579 -- for the return object. The function has an implicit formal
5580 -- parameter indicating this. If the BIP_Alloc_Form formal has
5581 -- the value one, then the caller has passed access to an
5582 -- existing object for use as the return object. If the value
5583 -- is two, then the return object must be allocated on the
5584 -- secondary stack. Otherwise, the object must be allocated in
5585 -- a storage pool. We generate an if statement to test the
5586 -- implicit allocation formal and initialize a local access
5587 -- value appropriately, creating allocators in the secondary
5588 -- stack and global heap cases. The special formal also exists
5589 -- and must be tested when the function has a tagged result,
5590 -- even when the result subtype is constrained, because in
5591 -- general such functions can be called in dispatching contexts
5592 -- and must be handled similarly to functions with a class-wide
5595 if Needs_BIP_Alloc_Form (Func_Id) then
5597 Build_In_Place_Formal (Func_Id, BIP_Alloc_Form);
5600 Pool_Id : constant Entity_Id :=
5601 Make_Temporary (Loc, 'P');
5602 Alloc_Obj_Id : Entity_Id;
5603 Alloc_Obj_Decl : Node_Id;
5604 Alloc_If_Stmt : Node_Id;
5605 Guard_Except : Node_Id;
5606 Heap_Allocator : Node_Id;
5607 Pool_Decl : Node_Id;
5608 Pool_Allocator : Node_Id;
5609 Ptr_Type_Decl : Node_Id;
5610 Ref_Type : Entity_Id;
5611 SS_Allocator : Node_Id;
5614 -- Create an access type designating the function's
5617 Ref_Type := Make_Temporary (Loc, 'A');
5620 Make_Full_Type_Declaration (Loc,
5621 Defining_Identifier => Ref_Type,
5623 Make_Access_To_Object_Definition (Loc,
5624 All_Present => True,
5625 Subtype_Indication =>
5626 New_Occurrence_Of (Ret_Obj_Typ, Loc)));
5628 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
5630 -- Create an access object that will be initialized to an
5631 -- access value denoting the return object, either coming
5632 -- from an implicit access value passed in by the caller
5633 -- or from the result of an allocator.
5635 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
5636 Set_Etype (Alloc_Obj_Id, Ref_Type);
5639 Make_Object_Declaration (Loc,
5640 Defining_Identifier => Alloc_Obj_Id,
5641 Object_Definition =>
5642 New_Occurrence_Of (Ref_Type, Loc));
5644 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
5646 -- Create allocators for both the secondary stack and
5647 -- global heap. If there's an initialization expression,
5648 -- then create these as initialized allocators.
5650 if Present (Ret_Obj_Expr)
5651 and then not No_Initialization (Ret_Obj_Decl)
5653 -- Always use the type of the expression for the
5654 -- qualified expression, rather than the result type.
5655 -- In general we cannot always use the result type
5656 -- for the allocator, because the expression might be
5657 -- of a specific type, such as in the case of an
5658 -- aggregate or even a nonlimited object when the
5659 -- result type is a limited class-wide interface type.
5662 Make_Allocator (Loc,
5664 Make_Qualified_Expression (Loc,
5667 (Etype (Ret_Obj_Expr), Loc),
5670 (Source => Ret_Obj_Expr,
5671 Scopes_In_EWA_OK => True)));
5674 -- If the function returns a class-wide type we cannot
5675 -- use the return type for the allocator. Instead we
5676 -- use the type of the expression, which must be an
5677 -- aggregate of a definite type.
5679 if Is_Class_Wide_Type (Ret_Obj_Typ) then
5681 Make_Allocator (Loc,
5684 (Etype (Ret_Obj_Expr), Loc));
5687 Make_Allocator (Loc,
5689 New_Occurrence_Of (Ret_Obj_Typ, Loc));
5692 -- If the object requires default initialization then
5693 -- that will happen later following the elaboration of
5694 -- the object renaming. If we don't turn it off here
5695 -- then the object will be default initialized twice.
5697 Set_No_Initialization (Heap_Allocator);
5700 -- Set the flag indicating that the allocator came from
5701 -- a build-in-place return statement, so we can avoid
5702 -- adjusting the allocated object. Note that this flag
5703 -- will be inherited by the copies made below.
5705 Set_Alloc_For_BIP_Return (Heap_Allocator);
5707 -- The Pool_Allocator is just like the Heap_Allocator,
5708 -- except we set Storage_Pool and Procedure_To_Call so
5709 -- it will use the user-defined storage pool.
5713 (Source => Heap_Allocator,
5714 Scopes_In_EWA_OK => True);
5716 pragma Assert (Alloc_For_BIP_Return (Pool_Allocator));
5718 -- Do not generate the renaming of the build-in-place
5719 -- pool parameter on ZFP because the parameter is not
5720 -- created in the first place.
5722 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
5724 Make_Object_Renaming_Declaration (Loc,
5725 Defining_Identifier => Pool_Id,
5728 (RTE (RE_Root_Storage_Pool), Loc),
5730 Make_Explicit_Dereference (Loc,
5732 (Build_In_Place_Formal
5733 (Func_Id, BIP_Storage_Pool), Loc)));
5734 Set_Storage_Pool (Pool_Allocator, Pool_Id);
5735 Set_Procedure_To_Call
5736 (Pool_Allocator, RTE (RE_Allocate_Any));
5738 Pool_Decl := Make_Null_Statement (Loc);
5741 -- If the No_Allocators restriction is active, then only
5742 -- an allocator for secondary stack allocation is needed.
5743 -- It's OK for such allocators to have Comes_From_Source
5744 -- set to False, because gigi knows not to flag them as
5745 -- being a violation of No_Implicit_Heap_Allocations.
5747 if Restriction_Active (No_Allocators) then
5748 SS_Allocator := Heap_Allocator;
5749 Heap_Allocator := Make_Null (Loc);
5750 Pool_Allocator := Make_Null (Loc);
5752 -- Otherwise the heap and pool allocators may be needed,
5753 -- so we make another allocator for secondary stack
5759 (Source => Heap_Allocator,
5760 Scopes_In_EWA_OK => True);
5762 pragma Assert (Alloc_For_BIP_Return (SS_Allocator));
5764 -- The heap and pool allocators are marked as
5765 -- Comes_From_Source since they correspond to an
5766 -- explicit user-written allocator (that is, it will
5767 -- only be executed on behalf of callers that call the
5768 -- function as initialization for such an allocator).
5769 -- Prevents errors when No_Implicit_Heap_Allocations
5772 Set_Comes_From_Source (Heap_Allocator, True);
5773 Set_Comes_From_Source (Pool_Allocator, True);
5776 -- The allocator is returned on the secondary stack
5778 Check_Restriction (No_Secondary_Stack, N);
5779 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
5780 Set_Procedure_To_Call
5781 (SS_Allocator, RTE (RE_SS_Allocate));
5783 -- The allocator is returned on the secondary stack,
5784 -- so indicate that the function return, as well as
5785 -- all blocks that encloses the allocator, must not
5786 -- release it. The flags must be set now because
5787 -- the decision to use the secondary stack is done
5788 -- very late in the course of expanding the return
5789 -- statement, past the point where these flags are
5792 Set_Uses_Sec_Stack (Func_Id);
5793 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
5794 Set_Sec_Stack_Needed_For_Return
5795 (Return_Statement_Entity (N));
5796 Set_Enclosing_Sec_Stack_Return (N);
5798 -- Guard against poor expansion on the caller side by
5799 -- using a raise statement to catch out-of-range values
5800 -- of formal parameter BIP_Alloc_Form.
5802 if Exceptions_OK then
5804 Make_Raise_Program_Error (Loc,
5805 Reason => PE_Build_In_Place_Mismatch);
5807 Guard_Except := Make_Null_Statement (Loc);
5810 -- Create an if statement to test the BIP_Alloc_Form
5811 -- formal and initialize the access object to either the
5812 -- BIP_Object_Access formal (BIP_Alloc_Form =
5813 -- Caller_Allocation), the result of allocating the
5814 -- object in the secondary stack (BIP_Alloc_Form =
5815 -- Secondary_Stack), or else an allocator to create the
5816 -- return object in the heap or user-defined pool
5817 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
5819 -- ??? An unchecked type conversion must be made in the
5820 -- case of assigning the access object formal to the
5821 -- local access object, because a normal conversion would
5822 -- be illegal in some cases (such as converting access-
5823 -- to-unconstrained to access-to-constrained), but the
5824 -- the unchecked conversion will presumably fail to work
5825 -- right in just such cases. It's not clear at all how to
5829 Make_If_Statement (Loc,
5833 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5835 Make_Integer_Literal (Loc,
5836 UI_From_Int (BIP_Allocation_Form'Pos
5837 (Caller_Allocation)))),
5839 Then_Statements => New_List (
5840 Make_Assignment_Statement (Loc,
5842 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5844 Make_Unchecked_Type_Conversion (Loc,
5846 New_Occurrence_Of (Ref_Type, Loc),
5848 New_Occurrence_Of (Obj_Acc_Formal, Loc)))),
5850 Elsif_Parts => New_List (
5851 Make_Elsif_Part (Loc,
5855 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5857 Make_Integer_Literal (Loc,
5858 UI_From_Int (BIP_Allocation_Form'Pos
5859 (Secondary_Stack)))),
5861 Then_Statements => New_List (
5862 Make_Assignment_Statement (Loc,
5864 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5865 Expression => SS_Allocator))),
5867 Make_Elsif_Part (Loc,
5871 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5873 Make_Integer_Literal (Loc,
5874 UI_From_Int (BIP_Allocation_Form'Pos
5877 Then_Statements => New_List (
5878 Build_Heap_Or_Pool_Allocator
5879 (Temp_Id => Alloc_Obj_Id,
5880 Temp_Typ => Ref_Type,
5882 Ret_Typ => Ret_Obj_Typ,
5883 Alloc_Expr => Heap_Allocator))),
5885 -- ???If all is well, we can put the following
5886 -- 'elsif' in the 'else', but this is a useful
5887 -- self-check in case caller and callee don't agree
5888 -- on whether BIPAlloc and so on should be passed.
5890 Make_Elsif_Part (Loc,
5894 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5896 Make_Integer_Literal (Loc,
5897 UI_From_Int (BIP_Allocation_Form'Pos
5898 (User_Storage_Pool)))),
5900 Then_Statements => New_List (
5902 Build_Heap_Or_Pool_Allocator
5903 (Temp_Id => Alloc_Obj_Id,
5904 Temp_Typ => Ref_Type,
5906 Ret_Typ => Ret_Obj_Typ,
5907 Alloc_Expr => Pool_Allocator)))),
5909 -- Raise Program_Error if it's none of the above;
5910 -- this is a compiler bug.
5912 Else_Statements => New_List (Guard_Except));
5914 -- If a separate initialization assignment was created
5915 -- earlier, append that following the assignment of the
5916 -- implicit access formal to the access object, to ensure
5917 -- that the return object is initialized in that case. In
5918 -- this situation, the target of the assignment must be
5919 -- rewritten to denote a dereference of the access to the
5920 -- return object passed in by the caller.
5922 if Present (Init_Assignment) then
5923 Rewrite (Name (Init_Assignment),
5924 Make_Explicit_Dereference (Loc,
5925 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
5928 (Original_Node (Name (Init_Assignment))));
5929 Set_Assignment_OK (Name (Init_Assignment));
5931 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
5934 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
5937 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
5939 -- Remember the local access object for use in the
5940 -- dereference of the renaming created below.
5942 Obj_Acc_Formal := Alloc_Obj_Id;
5945 -- When the function's subtype is unconstrained and a run-time
5946 -- test is not needed, we nevertheless need to build the return
5947 -- using the function's result subtype.
5949 elsif not Is_Constrained (Underlying_Type (Etype (Func_Id)))
5952 Alloc_Obj_Id : Entity_Id;
5953 Alloc_Obj_Decl : Node_Id;
5954 Ptr_Type_Decl : Node_Id;
5955 Ref_Type : Entity_Id;
5958 -- Create an access type designating the function's
5961 Ref_Type := Make_Temporary (Loc, 'A');
5964 Make_Full_Type_Declaration (Loc,
5965 Defining_Identifier => Ref_Type,
5967 Make_Access_To_Object_Definition (Loc,
5968 All_Present => True,
5969 Subtype_Indication =>
5970 New_Occurrence_Of (Ret_Obj_Typ, Loc)));
5972 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
5974 -- Create an access object initialized to the conversion
5975 -- of the implicit access value passed in by the caller.
5977 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
5978 Set_Etype (Alloc_Obj_Id, Ref_Type);
5980 -- See the ??? comment a few lines above about the use of
5981 -- an unchecked conversion here.
5984 Make_Object_Declaration (Loc,
5985 Defining_Identifier => Alloc_Obj_Id,
5986 Object_Definition =>
5987 New_Occurrence_Of (Ref_Type, Loc),
5989 Make_Unchecked_Type_Conversion (Loc,
5991 New_Occurrence_Of (Ref_Type, Loc),
5993 New_Occurrence_Of (Obj_Acc_Formal, Loc)));
5995 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
5997 -- Remember the local access object for use in the
5998 -- dereference of the renaming created below.
6000 Obj_Acc_Formal := Alloc_Obj_Id;
6004 -- Replace the return object declaration with a renaming of a
6005 -- dereference of the access value designating the return
6009 Make_Explicit_Dereference (Loc,
6010 Prefix => New_Occurrence_Of (Obj_Acc_Formal, Loc));
6012 Rewrite (Ret_Obj_Decl,
6013 Make_Object_Renaming_Declaration (Loc,
6014 Defining_Identifier => Ret_Obj_Id,
6015 Access_Definition => Empty,
6016 Subtype_Mark => New_Occurrence_Of (Ret_Obj_Typ, Loc),
6017 Name => Obj_Acc_Deref));
6019 Set_Renamed_Object (Ret_Obj_Id, Obj_Acc_Deref);
6023 -- Case where we do not need to build a block. But we're about to drop
6024 -- Return_Object_Declarations on the floor, so assert that it contains
6025 -- only the return object declaration.
6027 else pragma Assert (List_Length (Return_Object_Declarations (N)) = 1);
6029 -- Build simple_return_statement that returns the expression directly
6031 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
6032 Result := Return_Stmt;
6035 -- Set the flag to prevent infinite recursion
6037 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
6038 Set_Return_Statement (Ret_Obj_Id, Return_Stmt);
6040 Rewrite (N, Result);
6042 -- AI12-043: The checks of 6.5(8.1/3) and 6.5(21/3) are made immediately
6043 -- before an object is returned. A predicate that applies to the return
6044 -- subtype is checked immediately before an object is returned.
6046 -- Suppress access checks to avoid generating extra checks for b-i-p.
6048 Analyze (N, Suppress => Access_Check);
6049 end Expand_N_Extended_Return_Statement;
6051 ----------------------------
6052 -- Expand_N_Function_Call --
6053 ----------------------------
6055 procedure Expand_N_Function_Call (N : Node_Id) is
6058 end Expand_N_Function_Call;
6060 ---------------------------------------
6061 -- Expand_N_Procedure_Call_Statement --
6062 ---------------------------------------
6064 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
6067 end Expand_N_Procedure_Call_Statement;
6069 ------------------------------------
6070 -- Expand_N_Return_When_Statement --
6071 ------------------------------------
6073 procedure Expand_N_Return_When_Statement (N : Node_Id) is
6074 Loc : constant Source_Ptr := Sloc (N);
6077 Make_If_Statement (Loc,
6078 Condition => Condition (N),
6079 Then_Statements => New_List (
6080 Make_Simple_Return_Statement (Loc,
6081 Expression => Expression (N)))));
6084 end Expand_N_Return_When_Statement;
6086 --------------------------------------
6087 -- Expand_N_Simple_Return_Statement --
6088 --------------------------------------
6090 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
6092 -- Defend against previous errors (i.e. the return statement calls a
6093 -- function that is not available in configurable runtime).
6095 if Present (Expression (N))
6096 and then Nkind (Expression (N)) = N_Empty
6098 Check_Error_Detected;
6102 -- Distinguish the function and non-function cases:
6104 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
6106 | E_Generic_Function
6108 Expand_Simple_Function_Return (N);
6112 | E_Generic_Procedure
6114 | E_Return_Statement
6116 Expand_Non_Function_Return (N);
6119 raise Program_Error;
6123 when RE_Not_Available =>
6125 end Expand_N_Simple_Return_Statement;
6127 ------------------------------
6128 -- Expand_N_Subprogram_Body --
6129 ------------------------------
6131 -- Add dummy push/pop label nodes at start and end to clear any local
6132 -- exception indications if local-exception-to-goto optimization is active.
6134 -- Add return statement if last statement in body is not a return statement
6135 -- (this makes things easier on Gigi which does not want to have to handle
6136 -- a missing return).
6138 -- Add call to Activate_Tasks if body is a task activator
6140 -- Deal with possible detection of infinite recursion
6142 -- Eliminate body completely if convention stubbed
6144 -- Encode entity names within body, since we will not need to reference
6145 -- these entities any longer in the front end.
6147 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
6149 -- Reset Pure indication if any parameter has root type System.Address
6150 -- or has any parameters of limited types, where limited means that the
6151 -- run-time view is limited (i.e. the full type is limited).
6155 procedure Expand_N_Subprogram_Body (N : Node_Id) is
6156 Body_Id : constant Entity_Id := Defining_Entity (N);
6157 HSS : constant Node_Id := Handled_Statement_Sequence (N);
6158 Loc : constant Source_Ptr := Sloc (N);
6160 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id);
6161 -- Append a return statement to the statement sequence Stmts if the last
6162 -- statement is not already a return or a goto statement. Note that the
6163 -- latter test is not critical, it does not matter if we add a few extra
6164 -- returns, since they get eliminated anyway later on. Spec_Id denotes
6165 -- the corresponding spec of the subprogram body.
6171 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id) is
6172 Last_Stmt : Node_Id;
6177 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
6178 -- not relevant in this context since they are not executable.
6180 Last_Stmt := Last (Stmts);
6181 while Nkind (Last_Stmt) in N_Pop_xxx_Label loop
6185 -- Now insert return unless last statement is a transfer
6187 if not Is_Transfer (Last_Stmt) then
6189 -- The source location for the return is the end label of the
6190 -- procedure if present. Otherwise use the sloc of the last
6191 -- statement in the list. If the list comes from a generated
6192 -- exception handler and we are not debugging generated code,
6193 -- all the statements within the handler are made invisible
6196 if Nkind (Parent (Stmts)) = N_Exception_Handler
6197 and then not Comes_From_Source (Parent (Stmts))
6199 Loc := Sloc (Last_Stmt);
6200 elsif Present (End_Label (HSS)) then
6201 Loc := Sloc (End_Label (HSS));
6203 Loc := Sloc (Last_Stmt);
6206 -- Append return statement, and set analyzed manually. We can't
6207 -- call Analyze on this return since the scope is wrong.
6209 -- Note: it almost works to push the scope and then do the Analyze
6210 -- call, but something goes wrong in some weird cases and it is
6211 -- not worth worrying about ???
6213 Stmt := Make_Simple_Return_Statement (Loc);
6215 -- The return statement is handled properly, and the call to the
6216 -- postcondition, inserted below, does not require information
6217 -- from the body either. However, that call is analyzed in the
6218 -- enclosing scope, and an elaboration check might improperly be
6219 -- added to it. A guard in Sem_Elab is needed to prevent that
6220 -- spurious check, see Check_Elab_Call.
6222 Append_To (Stmts, Stmt);
6223 Set_Analyzed (Stmt);
6225 -- Call the _Postconditions procedure if the related subprogram
6226 -- has contract assertions that need to be verified on exit.
6228 -- Also, mark the successful return to signal that postconditions
6229 -- need to be evaluated when finalization occurs by setting
6230 -- Return_Success_For_Postcond to be True.
6232 if Ekind (Spec_Id) = E_Procedure
6233 and then Present (Postconditions_Proc (Spec_Id))
6237 -- Return_Success_For_Postcond := True;
6238 -- if Postcond_Enabled then
6242 Insert_Action (Stmt,
6243 Make_Assignment_Statement (Loc,
6246 (Get_Return_Success_For_Postcond (Spec_Id), Loc),
6247 Expression => New_Occurrence_Of (Standard_True, Loc)));
6249 -- Wrap the call to _postconditions within a test of the
6250 -- Postcond_Enabled flag to delay postcondition evaluation
6251 -- until after finalization when required.
6253 Insert_Action (Stmt,
6254 Make_If_Statement (Loc,
6256 New_Occurrence_Of (Get_Postcond_Enabled (Spec_Id), Loc),
6257 Then_Statements => New_List (
6258 Make_Procedure_Call_Statement (Loc,
6261 (Postconditions_Proc (Spec_Id), Loc)))));
6264 -- Ada 2022 (AI12-0279): append the call to 'Yield unless this is
6265 -- a generic subprogram (since in such case it will be added to
6266 -- the instantiations).
6268 if Has_Yield_Aspect (Spec_Id)
6269 and then Ekind (Spec_Id) /= E_Generic_Procedure
6270 and then RTE_Available (RE_Yield)
6272 Insert_Action (Stmt,
6273 Make_Procedure_Call_Statement (Loc,
6274 New_Occurrence_Of (RTE (RE_Yield), Loc)));
6283 Spec_Id : Entity_Id;
6285 -- Start of processing for Expand_N_Subprogram_Body
6288 if Present (Corresponding_Spec (N)) then
6289 Spec_Id := Corresponding_Spec (N);
6294 -- If this is a Pure function which has any parameters whose root type
6295 -- is System.Address, reset the Pure indication.
6296 -- This check is also performed when the subprogram is frozen, but we
6297 -- repeat it on the body so that the indication is consistent, and so
6298 -- it applies as well to bodies without separate specifications.
6300 if Is_Pure (Spec_Id)
6301 and then Is_Subprogram (Spec_Id)
6302 and then not Has_Pragma_Pure_Function (Spec_Id)
6304 Check_Function_With_Address_Parameter (Spec_Id);
6306 if Spec_Id /= Body_Id then
6307 Set_Is_Pure (Body_Id, Is_Pure (Spec_Id));
6311 -- Set L to either the list of declarations if present, or to the list
6312 -- of statements if no declarations are present. This is used to insert
6313 -- new stuff at the start.
6315 if Is_Non_Empty_List (Declarations (N)) then
6316 L := Declarations (N);
6318 L := Statements (HSS);
6321 -- If local-exception-to-goto optimization active, insert dummy push
6322 -- statements at start, and dummy pop statements at end, but inhibit
6323 -- this if we have No_Exception_Handlers, since they are useless and
6324 -- interfere with analysis, e.g. by CodePeer. We also don't need these
6325 -- if we're unnesting subprograms because the only purpose of these
6326 -- nodes is to ensure we don't set a label in one subprogram and branch
6327 -- to it in another.
6329 if (Debug_Flag_Dot_G
6330 or else Restriction_Active (No_Exception_Propagation))
6331 and then not Restriction_Active (No_Exception_Handlers)
6332 and then not CodePeer_Mode
6333 and then not Unnest_Subprogram_Mode
6334 and then Is_Non_Empty_List (L)
6337 FS : constant Node_Id := First (L);
6338 FL : constant Source_Ptr := Sloc (FS);
6343 -- LS points to either last statement, if statements are present
6344 -- or to the last declaration if there are no statements present.
6345 -- It is the node after which the pop's are generated.
6347 if Is_Non_Empty_List (Statements (HSS)) then
6348 LS := Last (Statements (HSS));
6355 Insert_List_Before_And_Analyze (FS, New_List (
6356 Make_Push_Constraint_Error_Label (FL),
6357 Make_Push_Program_Error_Label (FL),
6358 Make_Push_Storage_Error_Label (FL)));
6360 Insert_List_After_And_Analyze (LS, New_List (
6361 Make_Pop_Constraint_Error_Label (LL),
6362 Make_Pop_Program_Error_Label (LL),
6363 Make_Pop_Storage_Error_Label (LL)));
6367 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
6369 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
6375 -- Loop through formals
6377 F := First_Formal (Spec_Id);
6378 while Present (F) loop
6379 if Is_Scalar_Type (Etype (F))
6380 and then Ekind (F) = E_Out_Parameter
6382 Check_Restriction (No_Default_Initialization, F);
6384 -- Insert the initialization. We turn off validity checks
6385 -- for this assignment, since we do not want any check on
6386 -- the initial value itself (which may well be invalid).
6387 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
6390 Make_Assignment_Statement (Loc,
6391 Name => New_Occurrence_Of (F, Loc),
6392 Expression => Get_Simple_Init_Val (Etype (F), N));
6393 Set_Suppress_Assignment_Checks (A);
6395 Insert_Before_And_Analyze (First (L),
6396 A, Suppress => Validity_Check);
6404 -- Clear out statement list for stubbed procedure
6406 if Present (Corresponding_Spec (N)) then
6407 Set_Elaboration_Flag (N, Spec_Id);
6409 if Convention (Spec_Id) = Convention_Stubbed
6410 or else Is_Eliminated (Spec_Id)
6412 Set_Declarations (N, Empty_List);
6413 Set_Handled_Statement_Sequence (N,
6414 Make_Handled_Sequence_Of_Statements (Loc,
6415 Statements => New_List (Make_Null_Statement (Loc))));
6421 -- Create a set of discriminals for the next protected subprogram body
6423 if Is_List_Member (N)
6424 and then Present (Parent (List_Containing (N)))
6425 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
6426 and then Present (Next_Protected_Operation (N))
6428 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
6431 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
6432 -- subprograms with no specs are not frozen.
6434 Compute_Returns_By_Ref (Spec_Id);
6436 -- For a procedure, we add a return for all possible syntactic ends of
6439 if Ekind (Spec_Id) in E_Procedure | E_Generic_Procedure then
6440 Add_Return (Spec_Id, Statements (HSS));
6442 if Present (Exception_Handlers (HSS)) then
6443 Except_H := First_Non_Pragma (Exception_Handlers (HSS));
6444 while Present (Except_H) loop
6445 Add_Return (Spec_Id, Statements (Except_H));
6446 Next_Non_Pragma (Except_H);
6450 -- For a function, we must deal with the case where there is at least
6451 -- one missing return. What we do is to wrap the entire body of the
6452 -- function in a block:
6465 -- raise Program_Error;
6468 -- This approach is necessary because the raise must be signalled to the
6469 -- caller, not handled by any local handler (RM 6.4(11)).
6471 -- Note: we do not need to analyze the constructed sequence here, since
6472 -- it has no handler, and an attempt to analyze the handled statement
6473 -- sequence twice is risky in various ways (e.g. the issue of expanding
6474 -- cleanup actions twice).
6476 elsif Has_Missing_Return (Spec_Id) then
6478 Hloc : constant Source_Ptr := Sloc (HSS);
6479 Blok : constant Node_Id :=
6480 Make_Block_Statement (Hloc,
6481 Handled_Statement_Sequence => HSS);
6482 Rais : constant Node_Id :=
6483 Make_Raise_Program_Error (Hloc,
6484 Reason => PE_Missing_Return);
6487 Set_Handled_Statement_Sequence (N,
6488 Make_Handled_Sequence_Of_Statements (Hloc,
6489 Statements => New_List (Blok, Rais)));
6491 Push_Scope (Spec_Id);
6498 -- If subprogram contains a parameterless recursive call, then we may
6499 -- have an infinite recursion, so see if we can generate code to check
6500 -- for this possibility if storage checks are not suppressed.
6502 if Ekind (Spec_Id) = E_Procedure
6503 and then Has_Recursive_Call (Spec_Id)
6504 and then not Storage_Checks_Suppressed (Spec_Id)
6506 Detect_Infinite_Recursion (N, Spec_Id);
6509 -- Set to encode entity names in package body before gigi is called
6511 Qualify_Entity_Names (N);
6513 -- If the body belongs to a nonabstract library-level source primitive
6514 -- of a tagged type, install an elaboration check which ensures that a
6515 -- dispatching call targeting the primitive will not execute the body
6516 -- without it being previously elaborated.
6518 Install_Primitive_Elaboration_Check (N);
6519 end Expand_N_Subprogram_Body;
6521 -----------------------------------
6522 -- Expand_N_Subprogram_Body_Stub --
6523 -----------------------------------
6525 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
6529 if Present (Corresponding_Body (N)) then
6530 Bod := Unit_Declaration_Node (Corresponding_Body (N));
6532 -- The body may have been expanded already when it is analyzed
6533 -- through the subunit node. Do no expand again: it interferes
6534 -- with the construction of unnesting tables when generating C.
6536 if not Analyzed (Bod) then
6537 Expand_N_Subprogram_Body (Bod);
6540 -- Add full qualification to entities that may be created late
6541 -- during unnesting.
6543 Qualify_Entity_Names (N);
6545 end Expand_N_Subprogram_Body_Stub;
6547 -------------------------------------
6548 -- Expand_N_Subprogram_Declaration --
6549 -------------------------------------
6551 -- If the declaration appears within a protected body, it is a private
6552 -- operation of the protected type. We must create the corresponding
6553 -- protected subprogram an associated formals. For a normal protected
6554 -- operation, this is done when expanding the protected type declaration.
6556 -- If the declaration is for a null procedure, emit null body
6558 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
6559 Loc : constant Source_Ptr := Sloc (N);
6560 Subp : constant Entity_Id := Defining_Entity (N);
6564 Scop : constant Entity_Id := Scope (Subp);
6566 Prot_Decl : Node_Id;
6567 Prot_Id : Entity_Id;
6571 -- Deal with case of protected subprogram. Do not generate protected
6572 -- operation if operation is flagged as eliminated.
6574 if Is_List_Member (N)
6575 and then Present (Parent (List_Containing (N)))
6576 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
6577 and then Is_Protected_Type (Scop)
6579 if No (Protected_Body_Subprogram (Subp))
6580 and then not Is_Eliminated (Subp)
6583 Make_Subprogram_Declaration (Loc,
6585 Build_Protected_Sub_Specification
6586 (N, Scop, Unprotected_Mode));
6588 -- The protected subprogram is declared outside of the protected
6589 -- body. Given that the body has frozen all entities so far, we
6590 -- analyze the subprogram and perform freezing actions explicitly.
6591 -- including the generation of an explicit freeze node, to ensure
6592 -- that gigi has the proper order of elaboration.
6593 -- If the body is a subunit, the insertion point is before the
6594 -- stub in the parent.
6596 Prot_Bod := Parent (List_Containing (N));
6598 if Nkind (Parent (Prot_Bod)) = N_Subunit then
6599 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
6602 Insert_Before (Prot_Bod, Prot_Decl);
6603 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
6604 Set_Has_Delayed_Freeze (Prot_Id);
6606 Push_Scope (Scope (Scop));
6607 Analyze (Prot_Decl);
6608 Freeze_Before (N, Prot_Id);
6609 Set_Protected_Body_Subprogram (Subp, Prot_Id);
6613 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
6614 -- cases this is superfluous because calls to it will be automatically
6615 -- inlined, but we definitely need the body if preconditions for the
6616 -- procedure are present, or if performing coverage analysis.
6618 elsif Nkind (Specification (N)) = N_Procedure_Specification
6619 and then Null_Present (Specification (N))
6622 Bod : constant Node_Id := Body_To_Inline (N);
6625 Set_Has_Completion (Subp, False);
6626 Append_Freeze_Action (Subp, Bod);
6628 -- The body now contains raise statements, so calls to it will
6631 Set_Is_Inlined (Subp, False);
6635 -- When generating C code, transform a function that returns a
6636 -- constrained array type into a procedure with an out parameter
6637 -- that carries the return value.
6639 -- We skip this transformation for unchecked conversions, since they
6640 -- are not needed by the C generator (and this also produces cleaner
6643 Typ := Get_Fullest_View (Etype (Subp));
6645 if Transform_Function_Array
6646 and then Nkind (Specification (N)) = N_Function_Specification
6647 and then Is_Array_Type (Typ)
6648 and then Is_Constrained (Typ)
6649 and then not Is_Unchecked_Conversion_Instance (Subp)
6651 Build_Procedure_Form (N);
6653 end Expand_N_Subprogram_Declaration;
6655 --------------------------------
6656 -- Expand_Non_Function_Return --
6657 --------------------------------
6659 procedure Expand_Non_Function_Return (N : Node_Id) is
6660 pragma Assert (No (Expression (N)));
6662 Loc : constant Source_Ptr := Sloc (N);
6663 Scope_Id : Entity_Id := Return_Applies_To (Return_Statement_Entity (N));
6664 Kind : constant Entity_Kind := Ekind (Scope_Id);
6667 Goto_Stat : Node_Id;
6671 -- Call the _Postconditions procedure if the related subprogram has
6672 -- contract assertions that need to be verified on exit.
6674 -- Also, mark the successful return to signal that postconditions need
6675 -- to be evaluated when finalization occurs.
6677 if Ekind (Scope_Id) in E_Entry | E_Entry_Family | E_Procedure
6678 and then Present (Postconditions_Proc (Scope_Id))
6682 -- Return_Success_For_Postcond := True;
6683 -- if Postcond_Enabled then
6688 Make_Assignment_Statement (Loc,
6691 (Get_Return_Success_For_Postcond (Scope_Id), Loc),
6692 Expression => New_Occurrence_Of (Standard_True, Loc)));
6694 -- Wrap the call to _postconditions within a test of the
6695 -- Postcond_Enabled flag to delay postcondition evaluation until
6696 -- after finalization when required.
6699 Make_If_Statement (Loc,
6701 New_Occurrence_Of (Get_Postcond_Enabled (Scope_Id), Loc),
6702 Then_Statements => New_List (
6703 Make_Procedure_Call_Statement (Loc,
6706 (Postconditions_Proc (Scope_Id), Loc)))));
6709 -- Ada 2022 (AI12-0279)
6711 if Has_Yield_Aspect (Scope_Id)
6712 and then RTE_Available (RE_Yield)
6715 Make_Procedure_Call_Statement (Loc,
6716 New_Occurrence_Of (RTE (RE_Yield), Loc)));
6719 -- If it is a return from a procedure do no extra steps
6721 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
6724 -- If it is a nested return within an extended one, replace it with a
6725 -- return of the previously declared return object.
6727 elsif Kind = E_Return_Statement then
6729 Make_Simple_Return_Statement (Loc,
6731 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
6732 Set_Comes_From_Extended_Return_Statement (N);
6733 Set_Return_Statement_Entity (N, Scope_Id);
6734 Expand_Simple_Function_Return (N);
6738 pragma Assert (Is_Entry (Scope_Id));
6740 -- Look at the enclosing block to see whether the return is from an
6741 -- accept statement or an entry body.
6743 for J in reverse 0 .. Scope_Stack.Last loop
6744 Scope_Id := Scope_Stack.Table (J).Entity;
6745 exit when Is_Concurrent_Type (Scope_Id);
6748 -- If it is a return from accept statement it is expanded as call to
6749 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6751 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6752 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6754 if Is_Task_Type (Scope_Id) then
6757 Make_Procedure_Call_Statement (Loc,
6758 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
6759 Insert_Before (N, Call);
6760 -- why not insert actions here???
6763 Acc_Stat := Parent (N);
6764 while Nkind (Acc_Stat) /= N_Accept_Statement loop
6765 Acc_Stat := Parent (Acc_Stat);
6768 Lab_Node := Last (Statements
6769 (Handled_Statement_Sequence (Acc_Stat)));
6771 Goto_Stat := Make_Goto_Statement (Loc,
6772 Name => New_Occurrence_Of
6773 (Entity (Identifier (Lab_Node)), Loc));
6775 Set_Analyzed (Goto_Stat);
6777 Rewrite (N, Goto_Stat);
6780 -- If it is a return from an entry body, put a Complete_Entry_Body call
6781 -- in front of the return.
6783 elsif Is_Protected_Type (Scope_Id) then
6785 Make_Procedure_Call_Statement (Loc,
6787 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
6788 Parameter_Associations => New_List (
6789 Make_Attribute_Reference (Loc,
6792 (Find_Protection_Object (Current_Scope), Loc),
6793 Attribute_Name => Name_Unchecked_Access)));
6795 Insert_Before (N, Call);
6798 end Expand_Non_Function_Return;
6800 ---------------------------------------
6801 -- Expand_Protected_Object_Reference --
6802 ---------------------------------------
6804 function Expand_Protected_Object_Reference
6806 Scop : Entity_Id) return Node_Id
6808 Loc : constant Source_Ptr := Sloc (N);
6815 Rec := Make_Identifier (Loc, Name_uObject);
6816 Set_Etype (Rec, Corresponding_Record_Type (Scop));
6818 -- Find enclosing protected operation, and retrieve its first parameter,
6819 -- which denotes the enclosing protected object. If the enclosing
6820 -- operation is an entry, we are immediately within the protected body,
6821 -- and we can retrieve the object from the service entries procedure. A
6822 -- barrier function has the same signature as an entry. A barrier
6823 -- function is compiled within the protected object, but unlike
6824 -- protected operations its never needs locks, so that its protected
6825 -- body subprogram points to itself.
6827 Proc := Current_Scope;
6828 while Present (Proc)
6829 and then Scope (Proc) /= Scop
6831 Proc := Scope (Proc);
6834 Corr := Protected_Body_Subprogram (Proc);
6838 -- Previous error left expansion incomplete.
6839 -- Nothing to do on this call.
6846 (First (Parameter_Specifications (Parent (Corr))));
6848 if Is_Subprogram (Proc) and then Proc /= Corr then
6850 -- Protected function or procedure
6852 Set_Entity (Rec, Param);
6854 -- Rec is a reference to an entity which will not be in scope when
6855 -- the call is reanalyzed, and needs no further analysis.
6860 -- Entry or barrier function for entry body. The first parameter of
6861 -- the entry body procedure is pointer to the object. We create a
6862 -- local variable of the proper type, duplicating what is done to
6863 -- define _object later on.
6867 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
6871 Make_Full_Type_Declaration (Loc,
6872 Defining_Identifier => Obj_Ptr,
6874 Make_Access_To_Object_Definition (Loc,
6875 Subtype_Indication =>
6877 (Corresponding_Record_Type (Scop), Loc))));
6879 Insert_Actions (N, Decls);
6880 Freeze_Before (N, Obj_Ptr);
6883 Make_Explicit_Dereference (Loc,
6885 Unchecked_Convert_To (Obj_Ptr,
6886 New_Occurrence_Of (Param, Loc)));
6888 -- Analyze new actual. Other actuals in calls are already analyzed
6889 -- and the list of actuals is not reanalyzed after rewriting.
6891 Set_Parent (Rec, N);
6897 end Expand_Protected_Object_Reference;
6899 --------------------------------------
6900 -- Expand_Protected_Subprogram_Call --
6901 --------------------------------------
6903 procedure Expand_Protected_Subprogram_Call
6910 procedure Expand_Internal_Init_Call;
6911 -- A call to an operation of the type may occur in the initialization
6912 -- of a private component. In that case the prefix of the call is an
6913 -- entity name and the call is treated as internal even though it
6914 -- appears in code outside of the protected type.
6916 procedure Freeze_Called_Function;
6917 -- If it is a function call it can appear in elaboration code and
6918 -- the called entity must be frozen before the call. This must be
6919 -- done before the call is expanded, as the expansion may rewrite it
6920 -- to something other than a call (e.g. a temporary initialized in a
6921 -- transient block).
6923 -------------------------------
6924 -- Expand_Internal_Init_Call --
6925 -------------------------------
6927 procedure Expand_Internal_Init_Call is
6929 -- If the context is a protected object (rather than a protected
6930 -- type) the call itself is bound to raise program_error because
6931 -- the protected body will not have been elaborated yet. This is
6932 -- diagnosed subsequently in Sem_Elab.
6934 Freeze_Called_Function;
6936 -- The target of the internal call is the first formal of the
6937 -- enclosing initialization procedure.
6939 Rec := New_Occurrence_Of (First_Formal (Current_Scope), Sloc (N));
6940 Build_Protected_Subprogram_Call (N,
6945 Resolve (N, Etype (Subp));
6946 end Expand_Internal_Init_Call;
6948 ----------------------------
6949 -- Freeze_Called_Function --
6950 ----------------------------
6952 procedure Freeze_Called_Function is
6954 if Ekind (Subp) = E_Function then
6955 Freeze_Expression (Name (N));
6957 end Freeze_Called_Function;
6959 -- Start of processing for Expand_Protected_Subprogram_Call
6962 -- If the protected object is not an enclosing scope, this is an inter-
6963 -- object function call. Inter-object procedure calls are expanded by
6964 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
6965 -- subprogram being called is in the protected body being compiled, and
6966 -- if the protected object in the call is statically the enclosing type.
6967 -- The object may be a component of some other data structure, in which
6968 -- case this must be handled as an inter-object call.
6970 if not In_Open_Scopes (Scop)
6971 or else Is_Entry_Wrapper (Current_Scope)
6972 or else not Is_Entity_Name (Name (N))
6974 if Nkind (Name (N)) = N_Selected_Component then
6975 Rec := Prefix (Name (N));
6977 elsif Nkind (Name (N)) = N_Indexed_Component then
6978 Rec := Prefix (Prefix (Name (N)));
6980 -- If this is a call within an entry wrapper, it appears within a
6981 -- precondition that calls another primitive of the synchronized
6982 -- type. The target object of the call is the first actual on the
6983 -- wrapper. Note that this is an external call, because the wrapper
6984 -- is called outside of the synchronized object. This means that
6985 -- an entry call to an entry with preconditions involves two
6986 -- synchronized operations.
6988 elsif Ekind (Current_Scope) = E_Procedure
6989 and then Is_Entry_Wrapper (Current_Scope)
6991 Rec := New_Occurrence_Of (First_Entity (Current_Scope), Sloc (N));
6993 -- A default parameter of a protected operation may be a call to
6994 -- a protected function of the type. This appears as an internal
6995 -- call in the profile of the operation, but if the context is an
6996 -- external call we must convert the call into an external one,
6997 -- using the protected object that is the target, so that:
7000 -- is transformed into
7003 elsif Nkind (Parent (N)) = N_Procedure_Call_Statement
7004 and then Nkind (Name (Parent (N))) = N_Selected_Component
7005 and then Is_Protected_Type (Etype (Prefix (Name (Parent (N)))))
7006 and then Is_Entity_Name (Name (N))
7007 and then Scope (Entity (Name (N))) =
7008 Etype (Prefix (Name (Parent (N))))
7011 Make_Selected_Component (Sloc (N),
7012 Prefix => New_Copy_Tree (Prefix (Name (Parent (N)))),
7013 Selector_Name => Relocate_Node (Name (N))));
7015 Analyze_And_Resolve (N);
7019 -- If the context is the initialization procedure for a protected
7020 -- type, the call is legal because the called entity must be a
7021 -- function of that enclosing type, and this is treated as an
7025 (Is_Entity_Name (Name (N)) and then Inside_Init_Proc);
7027 Expand_Internal_Init_Call;
7031 Freeze_Called_Function;
7032 Build_Protected_Subprogram_Call (N,
7033 Name => New_Occurrence_Of (Subp, Sloc (N)),
7034 Rec => Convert_Concurrent (Rec, Etype (Rec)),
7038 Rec := Expand_Protected_Object_Reference (N, Scop);
7044 Freeze_Called_Function;
7045 Build_Protected_Subprogram_Call (N,
7051 -- Analyze and resolve the new call. The actuals have already been
7052 -- resolved, but expansion of a function call will add extra actuals
7053 -- if needed. Analysis of a procedure call already includes resolution.
7057 if Ekind (Subp) = E_Function then
7058 Resolve (N, Etype (Subp));
7060 end Expand_Protected_Subprogram_Call;
7062 -----------------------------------
7063 -- Expand_Simple_Function_Return --
7064 -----------------------------------
7066 -- The "simple" comes from the syntax rule simple_return_statement. The
7067 -- semantics are not at all simple.
7069 procedure Expand_Simple_Function_Return (N : Node_Id) is
7070 Loc : constant Source_Ptr := Sloc (N);
7072 Scope_Id : constant Entity_Id :=
7073 Return_Applies_To (Return_Statement_Entity (N));
7074 -- The function we are returning from
7076 R_Type : constant Entity_Id := Etype (Scope_Id);
7077 -- The result type of the function
7079 Utyp : constant Entity_Id := Underlying_Type (R_Type);
7081 Exp : Node_Id := Expression (N);
7082 pragma Assert (Present (Exp));
7084 Exp_Is_Function_Call : constant Boolean :=
7085 Nkind (Exp) = N_Function_Call
7086 or else (Nkind (Exp) = N_Explicit_Dereference
7087 and then Is_Entity_Name (Prefix (Exp))
7088 and then Ekind (Entity (Prefix (Exp))) = E_Constant
7089 and then Is_Related_To_Func_Return (Entity (Prefix (Exp))));
7091 Exp_Typ : constant Entity_Id := Etype (Exp);
7092 -- The type of the expression (not necessarily the same as R_Type)
7094 Subtype_Ind : Node_Id;
7095 -- If the result type of the function is class-wide and the expression
7096 -- has a specific type, then we use the expression's type as the type of
7097 -- the return object. In cases where the expression is an aggregate that
7098 -- is built in place, this avoids the need for an expensive conversion
7099 -- of the return object to the specific type on assignments to the
7100 -- individual components.
7102 -- Start of processing for Expand_Simple_Function_Return
7105 if Is_Class_Wide_Type (R_Type)
7106 and then not Is_Class_Wide_Type (Exp_Typ)
7107 and then Nkind (Exp) /= N_Type_Conversion
7109 Subtype_Ind := New_Occurrence_Of (Exp_Typ, Loc);
7111 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
7113 -- If the result type is class-wide and the expression is a view
7114 -- conversion, the conversion plays no role in the expansion because
7115 -- it does not modify the tag of the object. Remove the conversion
7116 -- altogether to prevent tag overwriting.
7118 if Is_Class_Wide_Type (R_Type)
7119 and then not Is_Class_Wide_Type (Exp_Typ)
7120 and then Nkind (Exp) = N_Type_Conversion
7122 Exp := Expression (Exp);
7126 -- Assert that if F says "return G(...);"
7127 -- then F and G are both b-i-p, or neither b-i-p.
7129 if Nkind (Exp) = N_Function_Call then
7130 pragma Assert (Ekind (Scope_Id) = E_Function);
7132 (Is_Build_In_Place_Function (Scope_Id) =
7133 Is_Build_In_Place_Function_Call (Exp));
7137 -- For the case of a simple return that does not come from an
7138 -- extended return, in the case of build-in-place, we rewrite
7139 -- "return <expression>;" to be:
7141 -- return _anon_ : <return_subtype> := <expression>
7143 -- The expansion produced by Expand_N_Extended_Return_Statement will
7144 -- contain simple return statements (for example, a block containing
7145 -- simple return of the return object), which brings us back here with
7146 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
7147 -- checking for a simple return that does not come from an extended
7148 -- return is to avoid this infinite recursion.
7150 -- The reason for this design is that for Ada 2005 limited returns, we
7151 -- need to reify the return object, so we can build it "in place", and
7152 -- we need a block statement to hang finalization and tasking stuff.
7154 -- ??? In order to avoid disruption, we avoid translating to extended
7155 -- return except in the cases where we really need to (Ada 2005 for
7156 -- inherently limited). We might prefer to do this translation in all
7157 -- cases (except perhaps for the case of Ada 95 inherently limited),
7158 -- in order to fully exercise the Expand_N_Extended_Return_Statement
7159 -- code. This would also allow us to do the build-in-place optimization
7160 -- for efficiency even in cases where it is semantically not required.
7162 -- As before, we check the type of the return expression rather than the
7163 -- return type of the function, because the latter may be a limited
7164 -- class-wide interface type, which is not a limited type, even though
7165 -- the type of the expression may be.
7168 (Comes_From_Extended_Return_Statement (N)
7169 or else not Is_Build_In_Place_Function_Call (Exp)
7170 or else Is_Build_In_Place_Function (Scope_Id));
7172 if not Comes_From_Extended_Return_Statement (N)
7173 and then Is_Build_In_Place_Function (Scope_Id)
7174 and then not Debug_Flag_Dot_L
7176 -- The functionality of interface thunks is simple and it is always
7177 -- handled by means of simple return statements. This leaves their
7178 -- expansion simple and clean.
7180 and then not Is_Thunk (Scope_Id)
7183 Return_Object_Entity : constant Entity_Id :=
7184 Make_Temporary (Loc, 'R', Exp);
7186 Obj_Decl : constant Node_Id :=
7187 Make_Object_Declaration (Loc,
7188 Defining_Identifier => Return_Object_Entity,
7189 Object_Definition => Subtype_Ind,
7192 Ext : constant Node_Id :=
7193 Make_Extended_Return_Statement (Loc,
7194 Return_Object_Declarations => New_List (Obj_Decl));
7195 -- Do not perform this high-level optimization if the result type
7196 -- is an interface because the "this" pointer must be displaced.
7205 -- Here we have a simple return statement that is part of the expansion
7206 -- of an extended return statement (either written by the user, or
7207 -- generated by the above code).
7209 -- Always normalize C/Fortran boolean result. This is not always needed,
7210 -- but it seems a good idea to minimize the passing around of non-
7211 -- normalized values, and in any case this handles the processing of
7212 -- barrier functions for protected types, which turn the condition into
7213 -- a return statement.
7215 if Is_Boolean_Type (Exp_Typ) and then Nonzero_Is_True (Exp_Typ) then
7216 Adjust_Condition (Exp);
7217 Adjust_Result_Type (Exp, Exp_Typ);
7220 -- Do validity check if enabled for returns
7222 if Validity_Checks_On and then Validity_Check_Returns then
7226 -- Check the result expression of a scalar function against the subtype
7227 -- of the function by inserting a conversion. This conversion must
7228 -- eventually be performed for other classes of types, but for now it's
7229 -- only done for scalars ???
7231 if Is_Scalar_Type (Exp_Typ) and then Exp_Typ /= R_Type then
7232 Rewrite (Exp, Convert_To (R_Type, Exp));
7234 -- The expression is resolved to ensure that the conversion gets
7235 -- expanded to generate a possible constraint check.
7237 Analyze_And_Resolve (Exp, R_Type);
7240 -- Deal with returning variable length objects and controlled types
7242 -- Nothing to do if we are returning by reference, or this is not a
7243 -- type that requires special processing (indicated by the fact that
7244 -- it requires a cleanup scope for the secondary stack case).
7246 if Is_Build_In_Place_Function (Scope_Id)
7247 or else Is_Limited_Interface (Exp_Typ)
7251 -- No copy needed for thunks returning interface type objects since
7252 -- the object is returned by reference and the maximum functionality
7253 -- required is just to displace the pointer.
7255 elsif Is_Thunk (Scope_Id) and then Is_Interface (Exp_Typ) then
7258 -- If the call is within a thunk and the type is a limited view, the
7259 -- backend will eventually see the non-limited view of the type.
7261 elsif Is_Thunk (Scope_Id) and then Is_Incomplete_Type (Exp_Typ) then
7264 -- A return statement from an ignored Ghost function does not use the
7265 -- secondary stack (or any other one).
7267 elsif not Requires_Transient_Scope (R_Type)
7268 or else Is_Ignored_Ghost_Entity (Scope_Id)
7271 -- Mutable records with variable-length components are not returned
7272 -- on the sec-stack, so we need to make sure that the back end will
7273 -- only copy back the size of the actual value, and not the maximum
7274 -- size. We create an actual subtype for this purpose. However we
7275 -- need not do it if the expression is a function call since this
7276 -- will be done in the called function and doing it here too would
7277 -- cause a temporary with maximum size to be created.
7280 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exp_Typ));
7284 if not Exp_Is_Function_Call
7285 and then Has_Discriminants (Ubt)
7286 and then not Is_Constrained (Ubt)
7287 and then not Has_Unchecked_Union (Ubt)
7289 Decl := Build_Actual_Subtype (Ubt, Exp);
7290 Ent := Defining_Identifier (Decl);
7291 Insert_Action (Exp, Decl);
7292 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
7293 Analyze_And_Resolve (Exp);
7297 -- Here if secondary stack is used
7300 -- Prevent the reclamation of the secondary stack by all enclosing
7301 -- blocks and loops as well as the related function; otherwise the
7302 -- result would be reclaimed too early.
7304 Set_Enclosing_Sec_Stack_Return (N);
7306 -- Optimize the case where the result is a function call. In this
7307 -- case the result is already on the secondary stack and no further
7308 -- processing is required except to set the By_Ref flag to ensure
7309 -- that gigi does not attempt an extra unnecessary copy. (Actually
7310 -- not just unnecessary but wrong in the case of a controlled type,
7311 -- where gigi does not know how to do a copy.)
7313 if Requires_Transient_Scope (Exp_Typ)
7314 and then Exp_Is_Function_Call
7318 -- Remove side effects from the expression now so that other parts
7319 -- of the expander do not have to reanalyze this node without this
7322 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
7324 -- Ada 2005 (AI-251): If the type of the returned object is
7325 -- an interface then add an implicit type conversion to force
7326 -- displacement of the "this" pointer.
7328 if Is_Interface (R_Type) then
7329 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
7332 Analyze_And_Resolve (Exp, R_Type);
7334 -- For controlled types, do the allocation on the secondary stack
7335 -- manually in order to call adjust at the right time:
7337 -- type Anon1 is access R_Type;
7338 -- for Anon1'Storage_pool use ss_pool;
7339 -- Anon2 : anon1 := new R_Type'(expr);
7340 -- return Anon2.all;
7342 -- We do the same for classwide types that are not potentially
7343 -- controlled (by the virtue of restriction No_Finalization) because
7344 -- gigi is not able to properly allocate class-wide types.
7346 elsif CW_Or_Has_Controlled_Part (Utyp) then
7348 Loc : constant Source_Ptr := Sloc (N);
7349 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
7350 Alloc_Node : Node_Id;
7354 Mutate_Ekind (Acc_Typ, E_Access_Type);
7356 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
7358 -- This is an allocator for the secondary stack, and it's fine
7359 -- to have Comes_From_Source set False on it, as gigi knows not
7360 -- to flag it as a violation of No_Implicit_Heap_Allocations.
7363 Make_Allocator (Loc,
7365 Make_Qualified_Expression (Loc,
7366 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
7367 Expression => Relocate_Node (Exp)));
7369 -- We do not want discriminant checks on the declaration,
7370 -- given that it gets its value from the allocator.
7372 Set_No_Initialization (Alloc_Node);
7374 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
7376 Insert_List_Before_And_Analyze (N, New_List (
7377 Make_Full_Type_Declaration (Loc,
7378 Defining_Identifier => Acc_Typ,
7380 Make_Access_To_Object_Definition (Loc,
7381 Subtype_Indication => Subtype_Ind)),
7383 Make_Object_Declaration (Loc,
7384 Defining_Identifier => Temp,
7385 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
7386 Expression => Alloc_Node)));
7389 Make_Explicit_Dereference (Loc,
7390 Prefix => New_Occurrence_Of (Temp, Loc)));
7392 -- Ada 2005 (AI-251): If the type of the returned object is
7393 -- an interface then add an implicit type conversion to force
7394 -- displacement of the "this" pointer.
7396 if Is_Interface (R_Type) then
7397 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
7400 Analyze_And_Resolve (Exp, R_Type);
7403 -- Otherwise use the gigi mechanism to allocate result on the
7407 Check_Restriction (No_Secondary_Stack, N);
7408 Set_Storage_Pool (N, RTE (RE_SS_Pool));
7409 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
7413 -- Implement the rules of 6.5(8-10), which require a tag check in
7414 -- the case of a limited tagged return type, and tag reassignment for
7415 -- nonlimited tagged results. These actions are needed when the return
7416 -- type is a specific tagged type and the result expression is a
7417 -- conversion or a formal parameter, because in that case the tag of
7418 -- the expression might differ from the tag of the specific result type.
7420 -- We must also verify an underlying type exists for the return type in
7421 -- case it is incomplete - in which case is not necessary to generate a
7422 -- check anyway since an incomplete limited tagged return type would
7423 -- qualify as a premature usage.
7426 and then Is_Tagged_Type (Utyp)
7427 and then not Is_Class_Wide_Type (Utyp)
7428 and then (Nkind (Exp) in
7429 N_Type_Conversion | N_Unchecked_Type_Conversion
7430 or else (Is_Entity_Name (Exp)
7431 and then Is_Formal (Entity (Exp))))
7433 -- When the return type is limited, perform a check that the tag of
7434 -- the result is the same as the tag of the return type.
7436 if Is_Limited_Type (R_Type) then
7438 Make_Raise_Constraint_Error (Loc,
7442 Make_Selected_Component (Loc,
7443 Prefix => Duplicate_Subexpr (Exp),
7444 Selector_Name => Make_Identifier (Loc, Name_uTag)),
7446 Make_Attribute_Reference (Loc,
7448 New_Occurrence_Of (Base_Type (Utyp), Loc),
7449 Attribute_Name => Name_Tag)),
7450 Reason => CE_Tag_Check_Failed));
7452 -- If the result type is a specific nonlimited tagged type, then we
7453 -- have to ensure that the tag of the result is that of the result
7454 -- type. This is handled by making a copy of the expression in
7455 -- the case where it might have a different tag, namely when the
7456 -- expression is a conversion or a formal parameter. We create a new
7457 -- object of the result type and initialize it from the expression,
7458 -- which will implicitly force the tag to be set appropriately.
7462 ExpR : constant Node_Id := Relocate_Node (Exp);
7463 Result_Id : constant Entity_Id :=
7464 Make_Temporary (Loc, 'R', ExpR);
7465 Result_Exp : constant Node_Id :=
7466 New_Occurrence_Of (Result_Id, Loc);
7467 Result_Obj : constant Node_Id :=
7468 Make_Object_Declaration (Loc,
7469 Defining_Identifier => Result_Id,
7470 Object_Definition =>
7471 New_Occurrence_Of (R_Type, Loc),
7472 Constant_Present => True,
7473 Expression => ExpR);
7476 Set_Assignment_OK (Result_Obj);
7477 Insert_Action (Exp, Result_Obj);
7479 Rewrite (Exp, Result_Exp);
7480 Analyze_And_Resolve (Exp, R_Type);
7484 -- Ada 2005 (AI95-344): If the result type is class-wide, then insert
7485 -- a check that the level of the return expression's underlying type
7486 -- is not deeper than the level of the master enclosing the function.
7488 -- AI12-043: The check is made immediately after the return object is
7489 -- created. This means that we do not apply it to the simple return
7490 -- generated by the expansion of an extended return statement.
7492 -- No runtime check needed in interface thunks since it is performed
7493 -- by the target primitive associated with the thunk.
7495 elsif Is_Class_Wide_Type (R_Type)
7496 and then not Comes_From_Extended_Return_Statement (N)
7497 and then not Is_Thunk (Scope_Id)
7499 Apply_CW_Accessibility_Check (Exp, Scope_Id);
7501 -- Ada 2012 (AI05-0073): If the result subtype of the function is
7502 -- defined by an access_definition designating a specific tagged
7503 -- type T, a check is made that the result value is null or the tag
7504 -- of the object designated by the result value identifies T.
7506 -- The return expression is referenced twice in the code below, so it
7507 -- must be made free of side effects. Given that different compilers
7508 -- may evaluate these parameters in different order, both occurrences
7511 elsif Ekind (R_Type) = E_Anonymous_Access_Type
7512 and then Is_Tagged_Type (Designated_Type (R_Type))
7513 and then not Is_Class_Wide_Type (Designated_Type (R_Type))
7514 and then Nkind (Original_Node (Exp)) /= N_Null
7515 and then not Tag_Checks_Suppressed (Designated_Type (R_Type))
7518 -- [Constraint_Error
7520 -- and then Exp.all not in Designated_Type]
7523 Make_Raise_Constraint_Error (Loc,
7528 Left_Opnd => Duplicate_Subexpr (Exp),
7529 Right_Opnd => Make_Null (Loc)),
7534 Make_Explicit_Dereference (Loc,
7535 Prefix => Duplicate_Subexpr (Exp)),
7537 New_Occurrence_Of (Designated_Type (R_Type), Loc))),
7539 Reason => CE_Tag_Check_Failed),
7540 Suppress => All_Checks);
7543 -- If the result is of an unconstrained array subtype with fixed lower
7544 -- bound, then sliding to that bound may be needed.
7546 if Is_Fixed_Lower_Bound_Array_Subtype (R_Type) then
7547 Expand_Sliding_Conversion (Exp, R_Type);
7550 -- If we are returning a nonscalar object that is possibly unaligned,
7551 -- then copy the value into a temporary first. This copy may need to
7552 -- expand to a loop of component operations.
7554 if Is_Possibly_Unaligned_Slice (Exp)
7555 or else (Is_Possibly_Unaligned_Object (Exp)
7556 and then not Represented_As_Scalar (Etype (Exp)))
7559 ExpR : constant Node_Id := Relocate_Node (Exp);
7560 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
7563 Make_Object_Declaration (Loc,
7564 Defining_Identifier => Tnn,
7565 Constant_Present => True,
7566 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7567 Expression => ExpR),
7568 Suppress => All_Checks);
7569 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7573 -- Call the _Postconditions procedure if the related function has
7574 -- contract assertions that need to be verified on exit.
7576 if Ekind (Scope_Id) = E_Function
7577 and then Present (Postconditions_Proc (Scope_Id))
7579 -- In the case of discriminated objects, we have created a
7580 -- constrained subtype above, and used the underlying type. This
7581 -- transformation is post-analysis and harmless, except that now the
7582 -- call to the post-condition will be analyzed and the type kinds
7585 if Nkind (Exp) = N_Unchecked_Type_Conversion
7586 and then Is_Private_Type (R_Type) /= Is_Private_Type (Etype (Exp))
7588 Rewrite (Exp, Expression (Relocate_Node (Exp)));
7591 -- We are going to reference the returned value twice in this case,
7592 -- once in the call to _Postconditions, and once in the actual return
7593 -- statement, but we can't have side effects happening twice.
7595 Force_Evaluation (Exp, Mode => Strict);
7597 -- Save the return value or a pointer to the return value since we
7598 -- may need to call postconditions after finalization when cleanup
7599 -- actions are present.
7603 -- Result_Object_For_Postcond := [Exp]'Unrestricted_Access;
7606 Make_Assignment_Statement (Loc,
7609 (Get_Result_Object_For_Postcond (Scope_Id), Loc),
7611 (if Is_Elementary_Type (Etype (R_Type)) then
7614 Make_Attribute_Reference (Loc,
7615 Attribute_Name => Name_Unrestricted_Access,
7616 Prefix => New_Copy_Tree (Exp)))));
7618 -- Mark the successful return to signal that postconditions need to
7619 -- be evaluated when finalization occurs.
7623 -- Return_Success_For_Postcond := True;
7624 -- if Postcond_Enabled then
7625 -- _Postconditions ([exp]);
7629 Make_Assignment_Statement (Loc,
7632 (Get_Return_Success_For_Postcond (Scope_Id), Loc),
7633 Expression => New_Occurrence_Of (Standard_True, Loc)));
7635 -- Wrap the call to _postconditions within a test of the
7636 -- Postcond_Enabled flag to delay postcondition evaluation until
7637 -- after finalization when required.
7640 Make_If_Statement (Loc,
7642 New_Occurrence_Of (Get_Postcond_Enabled (Scope_Id), Loc),
7643 Then_Statements => New_List (
7644 Make_Procedure_Call_Statement (Loc,
7647 (Postconditions_Proc (Scope_Id), Loc),
7648 Parameter_Associations => New_List (New_Copy_Tree (Exp))))));
7651 -- Ada 2005 (AI-251): If this return statement corresponds with an
7652 -- simple return statement associated with an extended return statement
7653 -- and the type of the returned object is an interface then generate an
7654 -- implicit conversion to force displacement of the "this" pointer.
7656 if Ada_Version >= Ada_2005
7657 and then Comes_From_Extended_Return_Statement (N)
7658 and then Nkind (Expression (N)) = N_Identifier
7659 and then Is_Interface (Utyp)
7660 and then Utyp /= Underlying_Type (Exp_Typ)
7662 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
7663 Analyze_And_Resolve (Exp);
7666 -- Ada 2022 (AI12-0279)
7668 if Has_Yield_Aspect (Scope_Id)
7669 and then RTE_Available (RE_Yield)
7672 Make_Procedure_Call_Statement (Loc,
7673 New_Occurrence_Of (RTE (RE_Yield), Loc)));
7675 end Expand_Simple_Function_Return;
7677 -----------------------
7678 -- Freeze_Subprogram --
7679 -----------------------
7681 procedure Freeze_Subprogram (N : Node_Id) is
7682 Loc : constant Source_Ptr := Sloc (N);
7684 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
7685 -- (Ada 2005): Register a predefined primitive in all the secondary
7686 -- dispatch tables of its primitive type.
7688 ----------------------------------
7689 -- Register_Predefined_DT_Entry --
7690 ----------------------------------
7692 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
7693 Iface_DT_Ptr : Elmt_Id;
7694 Tagged_Typ : Entity_Id;
7695 Thunk_Id : Entity_Id;
7696 Thunk_Code : Node_Id;
7699 Tagged_Typ := Find_Dispatching_Type (Prim);
7701 if No (Access_Disp_Table (Tagged_Typ))
7702 or else not Has_Interfaces (Tagged_Typ)
7703 or else not RTE_Available (RE_Interface_Tag)
7704 or else Restriction_Active (No_Dispatching_Calls)
7709 -- Skip the first two access-to-dispatch-table pointers since they
7710 -- leads to the primary dispatch table (predefined DT and user
7711 -- defined DT). We are only concerned with the secondary dispatch
7712 -- table pointers. Note that the access-to- dispatch-table pointer
7713 -- corresponds to the first implemented interface retrieved below.
7716 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
7718 while Present (Iface_DT_Ptr)
7719 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
7721 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7722 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code,
7723 Iface => Related_Type (Node (Iface_DT_Ptr)));
7725 if Present (Thunk_Code) then
7726 Insert_Actions_After (N, New_List (
7729 Build_Set_Predefined_Prim_Op_Address (Loc,
7731 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
7732 Position => DT_Position (Prim),
7734 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7735 Make_Attribute_Reference (Loc,
7736 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7737 Attribute_Name => Name_Unrestricted_Access))),
7739 Build_Set_Predefined_Prim_Op_Address (Loc,
7742 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
7744 Position => DT_Position (Prim),
7746 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7747 Make_Attribute_Reference (Loc,
7748 Prefix => New_Occurrence_Of (Prim, Loc),
7749 Attribute_Name => Name_Unrestricted_Access)))));
7752 -- Skip the tag of the predefined primitives dispatch table
7754 Next_Elmt (Iface_DT_Ptr);
7755 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7757 -- Skip tag of the no-thunks dispatch table
7759 Next_Elmt (Iface_DT_Ptr);
7760 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7762 -- Skip tag of predefined primitives no-thunks dispatch table
7764 Next_Elmt (Iface_DT_Ptr);
7765 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7767 Next_Elmt (Iface_DT_Ptr);
7769 end Register_Predefined_DT_Entry;
7773 Subp : constant Entity_Id := Entity (N);
7775 -- Start of processing for Freeze_Subprogram
7778 -- We suppress the initialization of the dispatch table entry when
7779 -- not Tagged_Type_Expansion because the dispatching mechanism is
7780 -- handled internally by the target.
7782 if Is_Dispatching_Operation (Subp)
7783 and then not Is_Abstract_Subprogram (Subp)
7784 and then Present (DTC_Entity (Subp))
7785 and then Present (Scope (DTC_Entity (Subp)))
7786 and then Tagged_Type_Expansion
7787 and then not Restriction_Active (No_Dispatching_Calls)
7788 and then RTE_Available (RE_Tag)
7791 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
7794 -- Handle private overridden primitives
7796 if not Is_CPP_Class (Typ) then
7797 Check_Overriding_Operation (Subp);
7800 -- We assume that imported CPP primitives correspond with objects
7801 -- whose constructor is in the CPP side; therefore we don't need
7802 -- to generate code to register them in the dispatch table.
7804 if Is_CPP_Class (Typ) then
7807 -- Handle CPP primitives found in derivations of CPP_Class types.
7808 -- These primitives must have been inherited from some parent, and
7809 -- there is no need to register them in the dispatch table because
7810 -- Build_Inherit_Prims takes care of initializing these slots.
7812 elsif Is_Imported (Subp)
7813 and then Convention (Subp) in Convention_C_Family
7817 -- Generate code to register the primitive in non statically
7818 -- allocated dispatch tables
7820 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
7822 -- When a primitive is frozen, enter its name in its dispatch
7825 if not Is_Interface (Typ)
7826 or else Present (Interface_Alias (Subp))
7828 if Is_Predefined_Dispatching_Operation (Subp) then
7829 Register_Predefined_DT_Entry (Subp);
7832 Insert_Actions_After (N,
7833 Register_Primitive (Loc, Prim => Subp));
7839 -- Mark functions that return by reference. Note that it cannot be part
7840 -- of the normal semantic analysis of the spec since the underlying
7841 -- returned type may not be known yet (for private types).
7843 Compute_Returns_By_Ref (Subp);
7845 -- Wnen freezing a null procedure, analyze its delayed aspects now
7846 -- because we may not have reached the end of the declarative list when
7847 -- delayed aspects are normally analyzed. This ensures that dispatching
7848 -- calls are properly rewritten when the generated _Postcondition
7849 -- procedure is analyzed in the null procedure body.
7851 if Nkind (Parent (Subp)) = N_Procedure_Specification
7852 and then Null_Present (Parent (Subp))
7854 Analyze_Entry_Or_Subprogram_Contract (Subp);
7856 end Freeze_Subprogram;
7858 --------------------------
7859 -- Has_BIP_Extra_Formal --
7860 --------------------------
7862 function Has_BIP_Extra_Formal
7864 Kind : BIP_Formal_Kind) return Boolean
7866 Extra_Formal : Entity_Id := Extra_Formals (E);
7869 -- We can only rely on the availability of the extra formals in frozen
7870 -- entities or in subprogram types of dispatching calls (since their
7871 -- extra formals are added when the target subprogram is frozen; see
7872 -- Expand_Dispatching_Call).
7874 pragma Assert (Is_Frozen (E)
7875 or else (Ekind (E) = E_Subprogram_Type
7876 and then Is_Dispatch_Table_Entity (E))
7877 or else (Is_Dispatching_Operation (E)
7878 and then Is_Frozen (Find_Dispatching_Type (E))));
7880 while Present (Extra_Formal) loop
7881 if Is_Build_In_Place_Entity (Extra_Formal)
7882 and then BIP_Suffix_Kind (Extra_Formal) = Kind
7887 Next_Formal_With_Extras (Extra_Formal);
7891 end Has_BIP_Extra_Formal;
7893 ------------------------------
7894 -- Insert_Post_Call_Actions --
7895 ------------------------------
7897 procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id) is
7898 Context : constant Node_Id := Parent (N);
7901 if Is_Empty_List (Post_Call) then
7905 -- Cases where the call is not a member of a statement list. This also
7906 -- includes the cases where the call is an actual in another function
7907 -- call, or is an index, or is an operand of an if-expression, i.e. is
7908 -- in an expression context.
7910 if not Is_List_Member (N)
7911 or else Nkind (Context) in N_Function_Call
7913 | N_Indexed_Component
7915 -- In Ada 2012 the call may be a function call in an expression
7916 -- (since OUT and IN OUT parameters are now allowed for such calls).
7917 -- The write-back of (in)-out parameters is handled by the back-end,
7918 -- but the constraint checks generated when subtypes of formal and
7919 -- actual don't match must be inserted in the form of assignments.
7920 -- Also do this in the case of explicit dereferences, which can occur
7921 -- due to rewritings of function calls with controlled results.
7923 if Nkind (N) = N_Function_Call
7924 or else Nkind (Original_Node (N)) = N_Function_Call
7925 or else Nkind (N) = N_Explicit_Dereference
7927 pragma Assert (Ada_Version >= Ada_2012);
7928 -- Functions with '[in] out' parameters are only allowed in Ada
7931 -- We used to handle this by climbing up parents to a
7932 -- non-statement/declaration and then simply making a call to
7933 -- Insert_Actions_After (P, Post_Call), but that doesn't work
7934 -- for Ada 2012. If we are in the middle of an expression, e.g.
7935 -- the condition of an IF, this call would insert after the IF
7936 -- statement, which is much too late to be doing the write back.
7939 -- if Clobber (X) then
7940 -- Put_Line (X'Img);
7945 -- Now assume Clobber changes X, if we put the write back after
7946 -- the IF, the Put_Line gets the wrong value and the goto causes
7947 -- the write back to be skipped completely.
7949 -- To deal with this, we replace the call by
7952 -- Tnnn : constant function-result-type := function-call;
7953 -- Post_Call actions
7958 -- However, that doesn't work if function-result-type requires
7959 -- finalization (because function-call's result never gets
7960 -- finalized). So in that case, we instead replace the call by
7963 -- type Ref is access all function-result-type;
7964 -- Ptr : constant Ref := function-call'Reference;
7965 -- Tnnn : constant function-result-type := Ptr.all;
7966 -- Finalize (Ptr.all);
7967 -- Post_Call actions
7974 Loc : constant Source_Ptr := Sloc (N);
7975 Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T');
7976 FRTyp : constant Entity_Id := Etype (N);
7977 Name : constant Node_Id := Relocate_Node (N);
7980 if Needs_Finalization (FRTyp) then
7982 Ptr_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
7984 Ptr_Typ_Decl : constant Node_Id :=
7985 Make_Full_Type_Declaration (Loc,
7986 Defining_Identifier => Ptr_Typ,
7988 Make_Access_To_Object_Definition (Loc,
7989 All_Present => True,
7990 Subtype_Indication =>
7991 New_Occurrence_Of (FRTyp, Loc)));
7993 Ptr_Obj : constant Entity_Id :=
7994 Make_Temporary (Loc, 'P');
7996 Ptr_Obj_Decl : constant Node_Id :=
7997 Make_Object_Declaration (Loc,
7998 Defining_Identifier => Ptr_Obj,
7999 Object_Definition =>
8000 New_Occurrence_Of (Ptr_Typ, Loc),
8001 Constant_Present => True,
8003 Make_Attribute_Reference (Loc,
8005 Attribute_Name => Name_Unrestricted_Access));
8007 function Ptr_Dereference return Node_Id is
8008 (Make_Explicit_Dereference (Loc,
8009 Prefix => New_Occurrence_Of (Ptr_Obj, Loc)));
8011 Tnn_Decl : constant Node_Id :=
8012 Make_Object_Declaration (Loc,
8013 Defining_Identifier => Tnnn,
8014 Object_Definition => New_Occurrence_Of (FRTyp, Loc),
8015 Constant_Present => True,
8016 Expression => Ptr_Dereference);
8018 Finalize_Call : constant Node_Id :=
8020 (Obj_Ref => Ptr_Dereference, Typ => FRTyp);
8022 -- Prepend in reverse order
8024 Prepend_To (Post_Call, Finalize_Call);
8025 Prepend_To (Post_Call, Tnn_Decl);
8026 Prepend_To (Post_Call, Ptr_Obj_Decl);
8027 Prepend_To (Post_Call, Ptr_Typ_Decl);
8030 Prepend_To (Post_Call,
8031 Make_Object_Declaration (Loc,
8032 Defining_Identifier => Tnnn,
8033 Object_Definition => New_Occurrence_Of (FRTyp, Loc),
8034 Constant_Present => True,
8035 Expression => Name));
8039 Make_Expression_With_Actions (Loc,
8040 Actions => Post_Call,
8041 Expression => New_Occurrence_Of (Tnnn, Loc)));
8043 -- We don't want to just blindly call Analyze_And_Resolve
8044 -- because that would cause unwanted recursion on the call.
8045 -- So for a moment set the call as analyzed to prevent that
8046 -- recursion, and get the rest analyzed properly, then reset
8047 -- the analyzed flag, so our caller can continue.
8049 Set_Analyzed (Name, True);
8050 Analyze_And_Resolve (N, FRTyp);
8051 Set_Analyzed (Name, False);
8054 -- If not the special Ada 2012 case of a function call, then we must
8055 -- have the triggering statement of a triggering alternative or an
8056 -- entry call alternative, and we can add the post call stuff to the
8057 -- corresponding statement list.
8060 pragma Assert (Nkind (Context) in N_Entry_Call_Alternative
8061 | N_Triggering_Alternative);
8063 if Is_Non_Empty_List (Statements (Context)) then
8064 Insert_List_Before_And_Analyze
8065 (First (Statements (Context)), Post_Call);
8067 Set_Statements (Context, Post_Call);
8071 -- A procedure call is always part of a declarative or statement list,
8072 -- however a function call may appear nested within a construct. Most
8073 -- cases of function call nesting are handled in the special case above.
8074 -- The only exception is when the function call acts as an actual in a
8075 -- procedure call. In this case the function call is in a list, but the
8076 -- post-call actions must be inserted after the procedure call.
8077 -- What if the function call is an aggregate component ???
8079 elsif Nkind (Context) = N_Procedure_Call_Statement then
8080 Insert_Actions_After (Context, Post_Call);
8082 -- Otherwise, normal case where N is in a statement sequence, just put
8083 -- the post-call stuff after the call statement.
8086 Insert_Actions_After (N, Post_Call);
8088 end Insert_Post_Call_Actions;
8090 -----------------------------------
8091 -- Is_Build_In_Place_Result_Type --
8092 -----------------------------------
8094 function Is_Build_In_Place_Result_Type (Typ : Entity_Id) return Boolean is
8096 if not Expander_Active then
8100 -- In Ada 2005 all functions with an inherently limited return type
8101 -- must be handled using a build-in-place profile, including the case
8102 -- of a function with a limited interface result, where the function
8103 -- may return objects of nonlimited descendants.
8105 if Is_Limited_View (Typ) then
8106 return Ada_Version >= Ada_2005 and then not Debug_Flag_Dot_L;
8109 if Debug_Flag_Dot_9 then
8113 if Has_Interfaces (Typ) then
8118 T : Entity_Id := Typ;
8120 -- For T'Class, return True if it's True for T. This is necessary
8121 -- because a class-wide function might say "return F (...)", where
8122 -- F returns the corresponding specific type. We need a loop in
8123 -- case T is a subtype of a class-wide type.
8125 while Is_Class_Wide_Type (T) loop
8129 -- If this is a generic formal type in an instance, return True if
8130 -- it's True for the generic actual type.
8132 if Nkind (Parent (T)) = N_Subtype_Declaration
8133 and then Present (Generic_Parent_Type (Parent (T)))
8135 T := Entity (Subtype_Indication (Parent (T)));
8137 if Present (Full_View (T)) then
8142 if Present (Underlying_Type (T)) then
8143 T := Underlying_Type (T);
8148 -- So we can stop here in the debugger
8150 -- ???For now, enable build-in-place for a very narrow set of
8151 -- controlled types. Change "if True" to "if False" to
8152 -- experiment with more controlled types. Eventually, we might
8153 -- like to enable build-in-place for all tagged types, all
8154 -- types that need finalization, and all caller-unknown-size
8158 Result := Is_Controlled (T)
8159 and then not Is_Generic_Actual_Type (T)
8160 and then Present (Enclosing_Subprogram (T))
8161 and then not Is_Compilation_Unit (Enclosing_Subprogram (T))
8162 and then Ekind (Enclosing_Subprogram (T)) = E_Procedure;
8164 Result := Is_Controlled (T);
8171 end Is_Build_In_Place_Result_Type;
8173 ------------------------------
8174 -- Is_Build_In_Place_Entity --
8175 ------------------------------
8177 function Is_Build_In_Place_Entity (E : Entity_Id) return Boolean is
8178 Nam : constant String := Get_Name_String (Chars (E));
8180 function Has_Suffix (Suffix : String) return Boolean;
8181 -- Return True if Nam has suffix Suffix
8183 function Has_Suffix (Suffix : String) return Boolean is
8184 Len : constant Natural := Suffix'Length;
8186 return Nam'Length > Len
8187 and then Nam (Nam'Last - Len + 1 .. Nam'Last) = Suffix;
8190 -- Start of processing for Is_Build_In_Place_Entity
8193 return Has_Suffix (BIP_Alloc_Suffix)
8194 or else Has_Suffix (BIP_Storage_Pool_Suffix)
8195 or else Has_Suffix (BIP_Finalization_Master_Suffix)
8196 or else Has_Suffix (BIP_Task_Master_Suffix)
8197 or else Has_Suffix (BIP_Activation_Chain_Suffix)
8198 or else Has_Suffix (BIP_Object_Access_Suffix);
8199 end Is_Build_In_Place_Entity;
8201 --------------------------------
8202 -- Is_Build_In_Place_Function --
8203 --------------------------------
8205 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
8207 -- This function is called from Expand_Subtype_From_Expr during
8208 -- semantic analysis, even when expansion is off. In those cases
8209 -- the build_in_place expansion will not take place.
8211 if not Expander_Active then
8215 -- For now we test whether E denotes a function or access-to-function
8216 -- type whose result subtype is inherently limited. Later this test
8217 -- may be revised to allow composite nonlimited types.
8219 if Ekind (E) in E_Function | E_Generic_Function
8220 or else (Ekind (E) = E_Subprogram_Type
8221 and then Etype (E) /= Standard_Void_Type)
8223 -- If the function is imported from a foreign language, we don't do
8224 -- build-in-place. Note that Import (Ada) functions can do
8225 -- build-in-place. Note that it is OK for a build-in-place function
8226 -- to return a type with a foreign convention; the build-in-place
8227 -- machinery will ensure there is no copying.
8229 return Is_Build_In_Place_Result_Type (Etype (E))
8230 and then not (Has_Foreign_Convention (E) and then Is_Imported (E))
8231 and then not Debug_Flag_Dot_L;
8235 end Is_Build_In_Place_Function;
8237 -------------------------------------
8238 -- Is_Build_In_Place_Function_Call --
8239 -------------------------------------
8241 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
8242 Exp_Node : constant Node_Id := Unqual_Conv (N);
8243 Function_Id : Entity_Id;
8246 -- Return False if the expander is currently inactive, since awareness
8247 -- of build-in-place treatment is only relevant during expansion. Note
8248 -- that Is_Build_In_Place_Function, which is called as part of this
8249 -- function, is also conditioned this way, but we need to check here as
8250 -- well to avoid blowing up on processing protected calls when expansion
8251 -- is disabled (such as with -gnatc) since those would trip over the
8252 -- raise of Program_Error below.
8254 -- In SPARK mode, build-in-place calls are not expanded, so that we
8255 -- may end up with a call that is neither resolved to an entity, nor
8256 -- an indirect call.
8258 if not Expander_Active or else Nkind (Exp_Node) /= N_Function_Call then
8262 if Is_Entity_Name (Name (Exp_Node)) then
8263 Function_Id := Entity (Name (Exp_Node));
8265 -- In the case of an explicitly dereferenced call, use the subprogram
8266 -- type generated for the dereference.
8268 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
8269 Function_Id := Etype (Name (Exp_Node));
8271 -- This may be a call to a protected function.
8273 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
8274 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
8277 raise Program_Error;
8281 Result : constant Boolean := Is_Build_In_Place_Function (Function_Id);
8282 -- So we can stop here in the debugger
8286 end Is_Build_In_Place_Function_Call;
8288 -----------------------
8289 -- Is_Null_Procedure --
8290 -----------------------
8292 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
8293 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
8296 if Ekind (Subp) /= E_Procedure then
8299 -- Check if this is a declared null procedure
8301 elsif Nkind (Decl) = N_Subprogram_Declaration then
8302 if not Null_Present (Specification (Decl)) then
8305 elsif No (Body_To_Inline (Decl)) then
8308 -- Check if the body contains only a null statement, followed by
8309 -- the return statement added during expansion.
8313 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
8319 if Nkind (Orig_Bod) /= N_Subprogram_Body then
8322 -- We must skip SCIL nodes because they are currently
8323 -- implemented as special N_Null_Statement nodes.
8327 (Statements (Handled_Statement_Sequence (Orig_Bod)));
8328 Stat2 := Next_Non_SCIL_Node (Stat);
8331 Is_Empty_List (Declarations (Orig_Bod))
8332 and then Nkind (Stat) = N_Null_Statement
8336 (Nkind (Stat2) = N_Simple_Return_Statement
8337 and then No (Next (Stat2))));
8345 end Is_Null_Procedure;
8347 -------------------------------------------
8348 -- Make_Build_In_Place_Call_In_Allocator --
8349 -------------------------------------------
8351 procedure Make_Build_In_Place_Call_In_Allocator
8352 (Allocator : Node_Id;
8353 Function_Call : Node_Id)
8355 Acc_Type : constant Entity_Id := Etype (Allocator);
8356 Loc : constant Source_Ptr := Sloc (Function_Call);
8357 Func_Call : Node_Id := Function_Call;
8358 Ref_Func_Call : Node_Id;
8359 Function_Id : Entity_Id;
8360 Result_Subt : Entity_Id;
8361 New_Allocator : Node_Id;
8362 Return_Obj_Access : Entity_Id; -- temp for function result
8363 Temp_Init : Node_Id; -- initial value of Return_Obj_Access
8364 Alloc_Form : BIP_Allocation_Form;
8365 Pool : Node_Id; -- nonnull if Alloc_Form = User_Storage_Pool
8366 Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case
8367 Chain : Entity_Id; -- activation chain, in case of tasks
8370 -- Step past qualification or unchecked conversion (the latter can occur
8371 -- in cases of calls to 'Input).
8373 if Nkind (Func_Call) in N_Qualified_Expression
8375 | N_Unchecked_Type_Conversion
8377 Func_Call := Expression (Func_Call);
8380 -- Mark the call as processed as a build-in-place call
8382 pragma Assert (not Is_Expanded_Build_In_Place_Call (Func_Call));
8383 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8385 if Is_Entity_Name (Name (Func_Call)) then
8386 Function_Id := Entity (Name (Func_Call));
8388 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8389 Function_Id := Etype (Name (Func_Call));
8392 raise Program_Error;
8395 Warn_BIP (Func_Call);
8397 Result_Subt := Available_View (Etype (Function_Id));
8399 -- Create a temp for the function result. In the caller-allocates case,
8400 -- this will be initialized to the result of a new uninitialized
8401 -- allocator. Note: we do not use Allocator as the Related_Node of
8402 -- Return_Obj_Access in call to Make_Temporary below as this would
8403 -- create a sort of infinite "recursion".
8405 Return_Obj_Access := Make_Temporary (Loc, 'R');
8406 Set_Etype (Return_Obj_Access, Acc_Type);
8407 Set_Can_Never_Be_Null (Acc_Type, False);
8408 -- It gets initialized to null, so we can't have that
8410 -- When the result subtype is constrained, the return object is created
8411 -- on the caller side, and access to it is passed to the function. This
8412 -- optimization is disabled when the result subtype needs finalization
8413 -- actions because the caller side allocation may result in undesirable
8414 -- finalization. Consider the following example:
8416 -- function Make_Lim_Ctrl return Lim_Ctrl is
8418 -- return Result : Lim_Ctrl := raise Program_Error do
8421 -- end Make_Lim_Ctrl;
8423 -- Obj : Lim_Ctrl_Ptr := new Lim_Ctrl'(Make_Lim_Ctrl);
8425 -- Even though the size of limited controlled type Lim_Ctrl is known,
8426 -- allocating Obj at the caller side will chain Obj on Lim_Ctrl_Ptr's
8427 -- finalization master. The subsequent call to Make_Lim_Ctrl will fail
8428 -- during the initialization actions for Result, which implies that
8429 -- Result (and Obj by extension) should not be finalized. However Obj
8430 -- will be finalized when access type Lim_Ctrl_Ptr goes out of scope
8431 -- since it is already attached on the related finalization master.
8433 -- Here and in related routines, we must examine the full view of the
8434 -- type, because the view at the point of call may differ from the
8435 -- one in the function body, and the expansion mechanism depends on
8436 -- the characteristics of the full view.
8438 if Needs_BIP_Alloc_Form (Function_Id) then
8441 -- Case of a user-defined storage pool. Pass an allocation parameter
8442 -- indicating that the function should allocate its result in the
8443 -- pool, and pass the pool. Use 'Unrestricted_Access because the
8444 -- pool may not be aliased.
8446 if Present (Associated_Storage_Pool (Acc_Type)) then
8447 Alloc_Form := User_Storage_Pool;
8449 Make_Attribute_Reference (Loc,
8452 (Associated_Storage_Pool (Acc_Type), Loc),
8453 Attribute_Name => Name_Unrestricted_Access);
8455 -- No user-defined pool; pass an allocation parameter indicating that
8456 -- the function should allocate its result on the heap.
8459 Alloc_Form := Global_Heap;
8460 Pool := Make_Null (No_Location);
8463 -- The caller does not provide the return object in this case, so we
8464 -- have to pass null for the object access actual.
8466 Return_Obj_Actual := Empty;
8469 -- Replace the initialized allocator of form "new T'(Func (...))"
8470 -- with an uninitialized allocator of form "new T", where T is the
8471 -- result subtype of the called function. The call to the function
8472 -- is handled separately further below.
8475 Make_Allocator (Loc,
8476 Expression => New_Occurrence_Of (Result_Subt, Loc));
8477 Set_No_Initialization (New_Allocator);
8479 -- Copy attributes to new allocator. Note that the new allocator
8480 -- logically comes from source if the original one did, so copy the
8481 -- relevant flag. This ensures proper treatment of the restriction
8482 -- No_Implicit_Heap_Allocations in this case.
8484 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
8485 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
8486 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
8488 Rewrite (Allocator, New_Allocator);
8490 -- Initial value of the temp is the result of the uninitialized
8491 -- allocator. Unchecked_Convert is needed for T'Input where T is
8492 -- derived from a controlled type.
8494 Temp_Init := Relocate_Node (Allocator);
8496 if Nkind (Function_Call) in
8497 N_Type_Conversion | N_Unchecked_Type_Conversion
8499 Temp_Init := Unchecked_Convert_To (Acc_Type, Temp_Init);
8502 -- Indicate that caller allocates, and pass in the return object
8504 Alloc_Form := Caller_Allocation;
8505 Pool := Make_Null (No_Location);
8506 Return_Obj_Actual :=
8507 Make_Unchecked_Type_Conversion (Loc,
8508 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8510 Make_Explicit_Dereference (Loc,
8511 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)));
8513 -- When the result subtype is unconstrained, the function itself must
8514 -- perform the allocation of the return object, so we pass parameters
8519 -- Declare the temp object
8521 Insert_Action (Allocator,
8522 Make_Object_Declaration (Loc,
8523 Defining_Identifier => Return_Obj_Access,
8524 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
8525 Expression => Temp_Init));
8527 Ref_Func_Call := Make_Reference (Loc, Func_Call);
8529 -- Ada 2005 (AI-251): If the type of the allocator is an interface
8530 -- then generate an implicit conversion to force displacement of the
8533 if Is_Interface (Designated_Type (Acc_Type)) then
8536 OK_Convert_To (Acc_Type, Ref_Func_Call));
8538 -- If the types are incompatible, we need an unchecked conversion. Note
8539 -- that the full types will be compatible, but the types not visibly
8542 elsif Nkind (Function_Call)
8543 in N_Type_Conversion | N_Unchecked_Type_Conversion
8545 Ref_Func_Call := Unchecked_Convert_To (Acc_Type, Ref_Func_Call);
8549 Assign : constant Node_Id :=
8550 Make_Assignment_Statement (Loc,
8551 Name => New_Occurrence_Of (Return_Obj_Access, Loc),
8552 Expression => Ref_Func_Call);
8553 -- Assign the result of the function call into the temp. In the
8554 -- caller-allocates case, this is overwriting the temp with its
8555 -- initial value, which has no effect. In the callee-allocates case,
8556 -- this is setting the temp to point to the object allocated by the
8557 -- callee. Unchecked_Convert is needed for T'Input where T is derived
8558 -- from a controlled type.
8561 -- Actions to be inserted. If there are no tasks, this is just the
8562 -- assignment statement. If the allocated object has tasks, we need
8563 -- to wrap the assignment in a block that activates them. The
8564 -- activation chain of that block must be passed to the function,
8565 -- rather than some outer chain.
8568 if Might_Have_Tasks (Result_Subt) then
8569 Actions := New_List;
8570 Build_Task_Allocate_Block_With_Init_Stmts
8571 (Actions, Allocator, Init_Stmts => New_List (Assign));
8572 Chain := Activation_Chain_Entity (Last (Actions));
8574 Actions := New_List (Assign);
8578 Insert_Actions (Allocator, Actions);
8581 -- When the function has a controlling result, an allocation-form
8582 -- parameter must be passed indicating that the caller is allocating
8583 -- the result object. This is needed because such a function can be
8584 -- called as a dispatching operation and must be treated similarly
8585 -- to functions with unconstrained result subtypes.
8587 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8588 (Func_Call, Function_Id, Alloc_Form, Pool_Actual => Pool);
8590 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8591 (Func_Call, Function_Id, Acc_Type);
8593 Add_Task_Actuals_To_Build_In_Place_Call
8594 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type),
8597 -- Add an implicit actual to the function call that provides access
8598 -- to the allocated object. An unchecked conversion to the (specific)
8599 -- result subtype of the function is inserted to handle cases where
8600 -- the access type of the allocator has a class-wide designated type.
8602 Add_Access_Actual_To_Build_In_Place_Call
8603 (Func_Call, Function_Id, Return_Obj_Actual);
8605 -- Finally, replace the allocator node with a reference to the temp
8607 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8609 Analyze_And_Resolve (Allocator, Acc_Type);
8610 pragma Assert (Check_Number_Of_Actuals (Func_Call, Function_Id));
8611 pragma Assert (Check_BIP_Actuals (Func_Call, Function_Id));
8612 end Make_Build_In_Place_Call_In_Allocator;
8614 ---------------------------------------------------
8615 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8616 ---------------------------------------------------
8618 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8619 (Function_Call : Node_Id)
8621 Loc : constant Source_Ptr := Sloc (Function_Call);
8622 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8623 Function_Id : Entity_Id;
8624 Result_Subt : Entity_Id;
8625 Return_Obj_Id : Entity_Id;
8626 Return_Obj_Decl : Entity_Id;
8629 -- If the call has already been processed to add build-in-place actuals
8630 -- then return. One place this can occur is for calls to build-in-place
8631 -- functions that occur within a call to a protected operation, where
8632 -- due to rewriting and expansion of the protected call there can be
8633 -- more than one call to Expand_Actuals for the same set of actuals.
8635 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8639 -- Mark the call as processed as a build-in-place call
8641 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8643 if Is_Entity_Name (Name (Func_Call)) then
8644 Function_Id := Entity (Name (Func_Call));
8646 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8647 Function_Id := Etype (Name (Func_Call));
8650 raise Program_Error;
8653 Warn_BIP (Func_Call);
8655 Result_Subt := Etype (Function_Id);
8657 -- If the build-in-place function returns a controlled object, then the
8658 -- object needs to be finalized immediately after the context. Since
8659 -- this case produces a transient scope, the servicing finalizer needs
8660 -- to name the returned object. Create a temporary which is initialized
8661 -- with the function call:
8663 -- Temp_Id : Func_Type := BIP_Func_Call;
8665 -- The initialization expression of the temporary will be rewritten by
8666 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8667 -- Call_In_Object_Declaration.
8669 if Needs_Finalization (Result_Subt) then
8671 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
8672 Temp_Decl : Node_Id;
8675 -- Reset the guard on the function call since the following does
8676 -- not perform actual call expansion.
8678 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
8681 Make_Object_Declaration (Loc,
8682 Defining_Identifier => Temp_Id,
8683 Object_Definition =>
8684 New_Occurrence_Of (Result_Subt, Loc),
8686 New_Copy_Tree (Function_Call));
8688 Insert_Action (Function_Call, Temp_Decl);
8690 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
8691 Analyze (Function_Call);
8694 -- When the result subtype is definite, an object of the subtype is
8695 -- declared and an access value designating it is passed as an actual.
8697 elsif Caller_Known_Size (Func_Call, Result_Subt) then
8699 -- Create a temporary object to hold the function result
8701 Return_Obj_Id := Make_Temporary (Loc, 'R');
8702 Set_Etype (Return_Obj_Id, Result_Subt);
8705 Make_Object_Declaration (Loc,
8706 Defining_Identifier => Return_Obj_Id,
8707 Aliased_Present => True,
8708 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
8710 Set_No_Initialization (Return_Obj_Decl);
8712 Insert_Action (Func_Call, Return_Obj_Decl);
8714 -- When the function has a controlling result, an allocation-form
8715 -- parameter must be passed indicating that the caller is allocating
8716 -- the result object. This is needed because such a function can be
8717 -- called as a dispatching operation and must be treated similarly
8718 -- to functions with unconstrained result subtypes.
8720 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8721 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8723 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8724 (Func_Call, Function_Id);
8726 Add_Task_Actuals_To_Build_In_Place_Call
8727 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8729 -- Add an implicit actual to the function call that provides access
8730 -- to the caller's return object.
8732 Add_Access_Actual_To_Build_In_Place_Call
8733 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
8735 pragma Assert (Check_Number_Of_Actuals (Func_Call, Function_Id));
8736 pragma Assert (Check_BIP_Actuals (Func_Call, Function_Id));
8738 -- When the result subtype is unconstrained, the function must allocate
8739 -- the return object in the secondary stack, so appropriate implicit
8740 -- parameters are added to the call to indicate that. A transient
8741 -- scope is established to ensure eventual cleanup of the result.
8744 -- Pass an allocation parameter indicating that the function should
8745 -- allocate its result on the secondary stack.
8747 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8748 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8750 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8751 (Func_Call, Function_Id);
8753 Add_Task_Actuals_To_Build_In_Place_Call
8754 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8756 -- Pass a null value to the function since no return object is
8757 -- available on the caller side.
8759 Add_Access_Actual_To_Build_In_Place_Call
8760 (Func_Call, Function_Id, Empty);
8762 pragma Assert (Check_Number_Of_Actuals (Func_Call, Function_Id));
8763 pragma Assert (Check_BIP_Actuals (Func_Call, Function_Id));
8765 end Make_Build_In_Place_Call_In_Anonymous_Context;
8767 --------------------------------------------
8768 -- Make_Build_In_Place_Call_In_Assignment --
8769 --------------------------------------------
8771 procedure Make_Build_In_Place_Call_In_Assignment
8773 Function_Call : Node_Id)
8775 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8776 Lhs : constant Node_Id := Name (Assign);
8777 Loc : constant Source_Ptr := Sloc (Function_Call);
8778 Func_Id : Entity_Id;
8781 Ptr_Typ : Entity_Id;
8782 Ptr_Typ_Decl : Node_Id;
8784 Result_Subt : Entity_Id;
8787 -- Mark the call as processed as a build-in-place call
8789 pragma Assert (not Is_Expanded_Build_In_Place_Call (Func_Call));
8790 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8792 if Is_Entity_Name (Name (Func_Call)) then
8793 Func_Id := Entity (Name (Func_Call));
8795 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8796 Func_Id := Etype (Name (Func_Call));
8799 raise Program_Error;
8802 Warn_BIP (Func_Call);
8804 Result_Subt := Etype (Func_Id);
8806 -- When the result subtype is unconstrained, an additional actual must
8807 -- be passed to indicate that the caller is providing the return object.
8808 -- This parameter must also be passed when the called function has a
8809 -- controlling result, because dispatching calls to the function needs
8810 -- to be treated effectively the same as calls to class-wide functions.
8812 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8813 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
8815 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8816 (Func_Call, Func_Id);
8818 Add_Task_Actuals_To_Build_In_Place_Call
8819 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
8821 -- Add an implicit actual to the function call that provides access to
8822 -- the caller's return object.
8824 Add_Access_Actual_To_Build_In_Place_Call
8827 Make_Unchecked_Type_Conversion (Loc,
8828 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8829 Expression => Relocate_Node (Lhs)));
8831 -- Create an access type designating the function's result subtype
8833 Ptr_Typ := Make_Temporary (Loc, 'A');
8836 Make_Full_Type_Declaration (Loc,
8837 Defining_Identifier => Ptr_Typ,
8839 Make_Access_To_Object_Definition (Loc,
8840 All_Present => True,
8841 Subtype_Indication =>
8842 New_Occurrence_Of (Result_Subt, Loc)));
8843 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
8845 -- Finally, create an access object initialized to a reference to the
8846 -- function call. We know this access value is non-null, so mark the
8847 -- entity accordingly to suppress junk access checks.
8849 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
8851 -- Add a conversion if it's the wrong type
8853 if Etype (New_Expr) /= Ptr_Typ then
8855 Make_Unchecked_Type_Conversion (Loc,
8856 New_Occurrence_Of (Ptr_Typ, Loc), New_Expr);
8859 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
8860 Set_Etype (Obj_Id, Ptr_Typ);
8861 Set_Is_Known_Non_Null (Obj_Id);
8864 Make_Object_Declaration (Loc,
8865 Defining_Identifier => Obj_Id,
8866 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8867 Expression => New_Expr);
8868 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
8870 Rewrite (Assign, Make_Null_Statement (Loc));
8871 pragma Assert (Check_Number_Of_Actuals (Func_Call, Func_Id));
8872 pragma Assert (Check_BIP_Actuals (Func_Call, Func_Id));
8873 end Make_Build_In_Place_Call_In_Assignment;
8875 ----------------------------------------------------
8876 -- Make_Build_In_Place_Call_In_Object_Declaration --
8877 ----------------------------------------------------
8879 procedure Make_Build_In_Place_Call_In_Object_Declaration
8880 (Obj_Decl : Node_Id;
8881 Function_Call : Node_Id)
8883 function Get_Function_Id (Func_Call : Node_Id) return Entity_Id;
8884 -- Get the value of Function_Id, below
8886 ---------------------
8887 -- Get_Function_Id --
8888 ---------------------
8890 function Get_Function_Id (Func_Call : Node_Id) return Entity_Id is
8892 if Is_Entity_Name (Name (Func_Call)) then
8893 return Entity (Name (Func_Call));
8895 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8896 return Etype (Name (Func_Call));
8899 raise Program_Error;
8901 end Get_Function_Id;
8905 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8906 Function_Id : constant Entity_Id := Get_Function_Id (Func_Call);
8907 Loc : constant Source_Ptr := Sloc (Function_Call);
8908 Obj_Loc : constant Source_Ptr := Sloc (Obj_Decl);
8909 Obj_Def_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
8910 Obj_Typ : constant Entity_Id := Etype (Obj_Def_Id);
8911 Encl_Func : constant Entity_Id := Enclosing_Subprogram (Obj_Def_Id);
8912 Result_Subt : constant Entity_Id := Etype (Function_Id);
8914 Call_Deref : Node_Id;
8915 Caller_Object : Node_Id;
8917 Designated_Type : Entity_Id;
8918 Fmaster_Actual : Node_Id := Empty;
8919 Pool_Actual : Node_Id;
8920 Ptr_Typ : Entity_Id;
8921 Ptr_Typ_Decl : Node_Id;
8922 Pass_Caller_Acc : Boolean := False;
8925 Definite : constant Boolean :=
8926 Caller_Known_Size (Func_Call, Result_Subt)
8927 and then not Is_Class_Wide_Type (Obj_Typ);
8928 -- In the case of "X : T'Class := F(...);", where F returns a
8929 -- Caller_Known_Size (specific) tagged type, we treat it as
8930 -- indefinite, because the code for the Definite case below sets the
8931 -- initialization expression of the object to Empty, which would be
8932 -- illegal Ada, and would cause gigi to misallocate X.
8934 -- Start of processing for Make_Build_In_Place_Call_In_Object_Declaration
8937 -- If the call has already been processed to add build-in-place actuals
8940 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8944 -- Mark the call as processed as a build-in-place call
8946 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8948 Warn_BIP (Func_Call);
8950 -- Create an access type designating the function's result subtype.
8951 -- We use the type of the original call because it may be a call to an
8952 -- inherited operation, which the expansion has replaced with the parent
8953 -- operation that yields the parent type. Note that this access type
8954 -- must be declared before we establish a transient scope, so that it
8955 -- receives the proper accessibility level.
8957 if Is_Class_Wide_Type (Obj_Typ)
8958 and then not Is_Interface (Obj_Typ)
8959 and then not Is_Class_Wide_Type (Etype (Function_Call))
8961 Designated_Type := Obj_Typ;
8963 Designated_Type := Etype (Function_Call);
8966 Ptr_Typ := Make_Temporary (Loc, 'A');
8968 Make_Full_Type_Declaration (Loc,
8969 Defining_Identifier => Ptr_Typ,
8971 Make_Access_To_Object_Definition (Loc,
8972 All_Present => True,
8973 Subtype_Indication =>
8974 New_Occurrence_Of (Designated_Type, Loc)));
8976 -- The access type and its accompanying object must be inserted after
8977 -- the object declaration in the constrained case, so that the function
8978 -- call can be passed access to the object. In the indefinite case, or
8979 -- if the object declaration is for a return object, the access type and
8980 -- object must be inserted before the object, since the object
8981 -- declaration is rewritten to be a renaming of a dereference of the
8982 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8983 -- the result object is in a different (transient) scope, so won't cause
8986 if Definite and then not Is_Return_Object (Obj_Def_Id) then
8988 -- The presence of an address clause complicates the build-in-place
8989 -- expansion because the indicated address must be processed before
8990 -- the indirect call is generated (including the definition of a
8991 -- local pointer to the object). The address clause may come from
8992 -- an aspect specification or from an explicit attribute
8993 -- specification appearing after the object declaration. These two
8994 -- cases require different processing.
8996 if Has_Aspect (Obj_Def_Id, Aspect_Address) then
8998 -- Skip non-delayed pragmas that correspond to other aspects, if
8999 -- any, to find proper insertion point for freeze node of object.
9002 D : Node_Id := Obj_Decl;
9003 N : Node_Id := Next (D);
9007 and then Nkind (N) in N_Attribute_Reference | N_Pragma
9014 Insert_After (D, Ptr_Typ_Decl);
9016 -- Freeze object before pointer declaration, to ensure that
9017 -- generated attribute for address is inserted at the proper
9020 Freeze_Before (Ptr_Typ_Decl, Obj_Def_Id);
9023 Analyze (Ptr_Typ_Decl);
9025 elsif Present (Following_Address_Clause (Obj_Decl)) then
9027 -- Locate explicit address clause, which may also follow pragmas
9028 -- generated by other aspect specifications.
9031 Addr : constant Node_Id := Following_Address_Clause (Obj_Decl);
9032 D : Node_Id := Next (Obj_Decl);
9035 while Present (D) loop
9041 Insert_After_And_Analyze (Addr, Ptr_Typ_Decl);
9045 Insert_After_And_Analyze (Obj_Decl, Ptr_Typ_Decl);
9048 Insert_Action (Obj_Decl, Ptr_Typ_Decl);
9051 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
9052 -- elaborated in an inner (transient) scope and thus won't cause
9053 -- freezing by itself. It's not an itype, but it needs to be frozen
9054 -- inside the current subprogram (see Freeze_Outside in freeze.adb).
9056 Freeze_Itype (Ptr_Typ, Ptr_Typ_Decl);
9058 -- If the object is a return object of an enclosing build-in-place
9059 -- function, then the implicit build-in-place parameters of the
9060 -- enclosing function are simply passed along to the called function.
9061 -- (Unfortunately, this won't cover the case of extension aggregates
9062 -- where the ancestor part is a build-in-place indefinite function
9063 -- call that should be passed along the caller's parameters.
9064 -- Currently those get mishandled by reassigning the result of the
9065 -- call to the aggregate return object, when the call result should
9066 -- really be directly built in place in the aggregate and not in a
9069 if Is_Return_Object (Obj_Def_Id) then
9070 Pass_Caller_Acc := True;
9072 -- When the enclosing function has a BIP_Alloc_Form formal then we
9073 -- pass it along to the callee (such as when the enclosing function
9074 -- has an unconstrained or tagged result type).
9076 if Needs_BIP_Alloc_Form (Encl_Func) then
9077 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
9080 (Build_In_Place_Formal
9081 (Encl_Func, BIP_Storage_Pool), Loc);
9083 -- The build-in-place pool formal is not built on e.g. ZFP
9086 Pool_Actual := Empty;
9089 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9090 (Function_Call => Func_Call,
9091 Function_Id => Function_Id,
9094 (Build_In_Place_Formal (Encl_Func, BIP_Alloc_Form), Loc),
9095 Pool_Actual => Pool_Actual);
9097 -- Otherwise, if enclosing function has a definite result subtype,
9098 -- then caller allocation will be used.
9101 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9102 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
9105 if Needs_BIP_Finalization_Master (Encl_Func) then
9108 (Build_In_Place_Formal
9109 (Encl_Func, BIP_Finalization_Master), Loc);
9112 -- Retrieve the BIPacc formal from the enclosing function and convert
9113 -- it to the access type of the callee's BIP_Object_Access formal.
9116 Make_Unchecked_Type_Conversion (Loc,
9119 (Etype (Build_In_Place_Formal
9120 (Function_Id, BIP_Object_Access)),
9124 (Build_In_Place_Formal (Encl_Func, BIP_Object_Access),
9127 -- In the definite case, add an implicit actual to the function call
9128 -- that provides access to the declared object. An unchecked conversion
9129 -- to the (specific) result type of the function is inserted to handle
9130 -- the case where the object is declared with a class-wide type.
9134 Make_Unchecked_Type_Conversion (Loc,
9135 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
9136 Expression => New_Occurrence_Of (Obj_Def_Id, Loc));
9138 -- When the function has a controlling result, an allocation-form
9139 -- parameter must be passed indicating that the caller is allocating
9140 -- the result object. This is needed because such a function can be
9141 -- called as a dispatching operation and must be treated similarly to
9142 -- functions with indefinite result subtypes.
9144 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9145 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
9147 -- The allocation for indefinite library-level objects occurs on the
9148 -- heap as opposed to the secondary stack. This accommodates DLLs where
9149 -- the secondary stack is destroyed after each library unload. This is a
9150 -- hybrid mechanism where a stack-allocated object lives on the heap.
9152 elsif Is_Library_Level_Entity (Obj_Def_Id)
9153 and then not Restriction_Active (No_Implicit_Heap_Allocations)
9155 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9156 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
9157 Caller_Object := Empty;
9159 -- Create a finalization master for the access result type to ensure
9160 -- that the heap allocation can properly chain the object and later
9161 -- finalize it when the library unit goes out of scope.
9163 if Needs_Finalization (Etype (Func_Call)) then
9164 Build_Finalization_Master
9166 For_Lib_Level => True,
9167 Insertion_Node => Ptr_Typ_Decl);
9170 Make_Attribute_Reference (Loc,
9172 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
9173 Attribute_Name => Name_Unrestricted_Access);
9176 -- In other indefinite cases, pass an indication to do the allocation
9177 -- on the secondary stack and set Caller_Object to Empty so that a null
9178 -- value will be passed for the caller's object address. A transient
9179 -- scope is established to ensure eventual cleanup of the result.
9182 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9183 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
9184 Caller_Object := Empty;
9186 Establish_Transient_Scope (Obj_Decl, Manage_Sec_Stack => True);
9189 -- Pass along any finalization master actual, which is needed in the
9190 -- case where the called function initializes a return object of an
9191 -- enclosing build-in-place function.
9193 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9194 (Func_Call => Func_Call,
9195 Func_Id => Function_Id,
9196 Master_Exp => Fmaster_Actual);
9198 if Nkind (Parent (Obj_Decl)) = N_Extended_Return_Statement
9199 and then Needs_BIP_Task_Actuals (Function_Id)
9201 -- Here we're passing along the master that was passed in to this
9204 Add_Task_Actuals_To_Build_In_Place_Call
9205 (Func_Call, Function_Id,
9208 (Build_In_Place_Formal (Encl_Func, BIP_Task_Master), Loc));
9211 Add_Task_Actuals_To_Build_In_Place_Call
9212 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
9215 Add_Access_Actual_To_Build_In_Place_Call
9219 Is_Access => Pass_Caller_Acc);
9221 -- Finally, create an access object initialized to a reference to the
9222 -- function call. We know this access value cannot be null, so mark the
9223 -- entity accordingly to suppress the access check. We need to suppress
9224 -- warnings, because this can be part of the expansion of "for ... of"
9225 -- and similar constructs that generate finalization actions. Such
9226 -- finalization actions are safe, because they check a count that
9227 -- indicates which objects should be finalized, but the back end
9228 -- nonetheless warns about uninitialized objects.
9230 Def_Id := Make_Temporary (Loc, 'R', Func_Call);
9231 Set_Warnings_Off (Def_Id);
9232 Set_Etype (Def_Id, Ptr_Typ);
9233 Set_Is_Known_Non_Null (Def_Id);
9235 if Nkind (Function_Call) in N_Type_Conversion
9236 | N_Unchecked_Type_Conversion
9239 Make_Object_Declaration (Loc,
9240 Defining_Identifier => Def_Id,
9241 Constant_Present => True,
9242 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
9244 Make_Unchecked_Type_Conversion (Loc,
9245 New_Occurrence_Of (Ptr_Typ, Loc),
9246 Make_Reference (Loc, Relocate_Node (Func_Call))));
9249 Make_Object_Declaration (Loc,
9250 Defining_Identifier => Def_Id,
9251 Constant_Present => True,
9252 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
9254 Make_Reference (Loc, Relocate_Node (Func_Call)));
9257 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
9259 -- If the result subtype of the called function is definite and is not
9260 -- itself the return expression of an enclosing BIP function, then mark
9261 -- the object as having no initialization.
9263 if Definite and then not Is_Return_Object (Obj_Def_Id) then
9265 -- The related object declaration is encased in a transient block
9266 -- because the build-in-place function call contains at least one
9267 -- nested function call that produces a controlled transient
9270 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
9272 -- Since the build-in-place expansion decouples the call from the
9273 -- object declaration, the finalization machinery lacks the context
9274 -- which prompted the generation of the transient block. To resolve
9275 -- this scenario, store the build-in-place call.
9277 if Scope_Is_Transient then
9278 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
9281 Set_Expression (Obj_Decl, Empty);
9282 Set_No_Initialization (Obj_Decl);
9284 -- In case of an indefinite result subtype, or if the call is the
9285 -- return expression of an enclosing BIP function, rewrite the object
9286 -- declaration as an object renaming where the renamed object is a
9287 -- dereference of <function_Call>'reference:
9289 -- Obj : Subt renames <function_call>'Ref.all;
9293 Make_Explicit_Dereference (Obj_Loc,
9294 Prefix => New_Occurrence_Of (Def_Id, Obj_Loc));
9297 Make_Object_Renaming_Declaration (Obj_Loc,
9298 Defining_Identifier => Make_Temporary (Obj_Loc, 'D'),
9300 New_Occurrence_Of (Designated_Type, Obj_Loc),
9301 Name => Call_Deref));
9303 -- At this point, Defining_Identifier (Obj_Decl) is no longer equal
9306 Set_Renamed_Object (Defining_Identifier (Obj_Decl), Call_Deref);
9308 -- If the original entity comes from source, then mark the new
9309 -- entity as needing debug information, even though it's defined
9310 -- by a generated renaming that does not come from source, so that
9311 -- the Materialize_Entity flag will be set on the entity when
9312 -- Debug_Renaming_Declaration is called during analysis.
9314 if Comes_From_Source (Obj_Def_Id) then
9315 Set_Debug_Info_Needed (Defining_Identifier (Obj_Decl));
9319 Replace_Renaming_Declaration_Id
9320 (Obj_Decl, Original_Node (Obj_Decl));
9323 pragma Assert (Check_Number_Of_Actuals (Func_Call, Function_Id));
9324 pragma Assert (Check_BIP_Actuals (Func_Call, Function_Id));
9325 end Make_Build_In_Place_Call_In_Object_Declaration;
9327 -------------------------------------------------
9328 -- Make_Build_In_Place_Iface_Call_In_Allocator --
9329 -------------------------------------------------
9331 procedure Make_Build_In_Place_Iface_Call_In_Allocator
9332 (Allocator : Node_Id;
9333 Function_Call : Node_Id)
9335 BIP_Func_Call : constant Node_Id :=
9336 Unqual_BIP_Iface_Function_Call (Function_Call);
9337 Loc : constant Source_Ptr := Sloc (Function_Call);
9339 Anon_Type : Entity_Id;
9344 -- No action if the call has already been processed
9346 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
9350 Tmp_Id := Make_Temporary (Loc, 'D');
9352 -- Insert a temporary before N initialized with the BIP function call
9353 -- without its enclosing type conversions and analyze it without its
9354 -- expansion. This temporary facilitates us reusing the BIP machinery,
9355 -- which takes care of adding the extra build-in-place actuals and
9356 -- transforms this object declaration into an object renaming
9359 Anon_Type := Create_Itype (E_Anonymous_Access_Type, Function_Call);
9360 Set_Directly_Designated_Type (Anon_Type, Etype (BIP_Func_Call));
9361 Set_Etype (Anon_Type, Anon_Type);
9362 Build_Class_Wide_Master (Anon_Type);
9365 Make_Object_Declaration (Loc,
9366 Defining_Identifier => Tmp_Id,
9367 Object_Definition => New_Occurrence_Of (Anon_Type, Loc),
9369 Make_Allocator (Loc,
9371 Make_Qualified_Expression (Loc,
9373 New_Occurrence_Of (Etype (BIP_Func_Call), Loc),
9374 Expression => New_Copy_Tree (BIP_Func_Call))));
9376 -- Manually set the associated node for the anonymous access type to
9377 -- be its local declaration, to avoid confusing and complicating
9378 -- the accessibility machinery.
9380 Set_Associated_Node_For_Itype (Anon_Type, Tmp_Decl);
9382 Expander_Mode_Save_And_Set (False);
9383 Insert_Action (Allocator, Tmp_Decl);
9384 Expander_Mode_Restore;
9386 Make_Build_In_Place_Call_In_Allocator
9387 (Allocator => Expression (Tmp_Decl),
9388 Function_Call => Expression (Expression (Tmp_Decl)));
9390 -- Add a conversion to displace the pointer to the allocated object
9391 -- to reference the corresponding dispatch table.
9394 Convert_To (Etype (Allocator),
9395 New_Occurrence_Of (Tmp_Id, Loc)));
9396 end Make_Build_In_Place_Iface_Call_In_Allocator;
9398 ---------------------------------------------------------
9399 -- Make_Build_In_Place_Iface_Call_In_Anonymous_Context --
9400 ---------------------------------------------------------
9402 procedure Make_Build_In_Place_Iface_Call_In_Anonymous_Context
9403 (Function_Call : Node_Id)
9405 BIP_Func_Call : constant Node_Id :=
9406 Unqual_BIP_Iface_Function_Call (Function_Call);
9407 Loc : constant Source_Ptr := Sloc (Function_Call);
9413 -- No action of the call has already been processed
9415 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
9419 pragma Assert (Needs_Finalization (Etype (BIP_Func_Call)));
9421 -- Insert a temporary before the call initialized with function call to
9422 -- reuse the BIP machinery which takes care of adding the extra build-in
9423 -- place actuals and transforms this object declaration into an object
9424 -- renaming declaration.
9426 Tmp_Id := Make_Temporary (Loc, 'D');
9429 Make_Object_Declaration (Loc,
9430 Defining_Identifier => Tmp_Id,
9431 Object_Definition =>
9432 New_Occurrence_Of (Etype (Function_Call), Loc),
9433 Expression => Relocate_Node (Function_Call));
9435 Expander_Mode_Save_And_Set (False);
9436 Insert_Action (Function_Call, Tmp_Decl);
9437 Expander_Mode_Restore;
9439 Make_Build_In_Place_Iface_Call_In_Object_Declaration
9440 (Obj_Decl => Tmp_Decl,
9441 Function_Call => Expression (Tmp_Decl));
9442 end Make_Build_In_Place_Iface_Call_In_Anonymous_Context;
9444 ----------------------------------------------------------
9445 -- Make_Build_In_Place_Iface_Call_In_Object_Declaration --
9446 ----------------------------------------------------------
9448 procedure Make_Build_In_Place_Iface_Call_In_Object_Declaration
9449 (Obj_Decl : Node_Id;
9450 Function_Call : Node_Id)
9452 BIP_Func_Call : constant Node_Id :=
9453 Unqual_BIP_Iface_Function_Call (Function_Call);
9454 Loc : constant Source_Ptr := Sloc (Function_Call);
9455 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
9461 -- No action of the call has already been processed
9463 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
9467 Tmp_Id := Make_Temporary (Loc, 'D');
9469 -- Insert a temporary before N initialized with the BIP function call
9470 -- without its enclosing type conversions and analyze it without its
9471 -- expansion. This temporary facilitates us reusing the BIP machinery,
9472 -- which takes care of adding the extra build-in-place actuals and
9473 -- transforms this object declaration into an object renaming
9477 Make_Object_Declaration (Loc,
9478 Defining_Identifier => Tmp_Id,
9479 Object_Definition =>
9480 New_Occurrence_Of (Etype (BIP_Func_Call), Loc),
9481 Expression => New_Copy_Tree (BIP_Func_Call));
9483 Expander_Mode_Save_And_Set (False);
9484 Insert_Action (Obj_Decl, Tmp_Decl);
9485 Expander_Mode_Restore;
9487 Make_Build_In_Place_Call_In_Object_Declaration
9488 (Obj_Decl => Tmp_Decl,
9489 Function_Call => Expression (Tmp_Decl));
9491 pragma Assert (Nkind (Tmp_Decl) = N_Object_Renaming_Declaration);
9493 -- Replace the original build-in-place function call by a reference to
9494 -- the resulting temporary object renaming declaration. In this way,
9495 -- all the interface conversions performed in the original Function_Call
9496 -- on the build-in-place object are preserved.
9498 Rewrite (BIP_Func_Call, New_Occurrence_Of (Tmp_Id, Loc));
9500 -- Replace the original object declaration by an internal object
9501 -- renaming declaration. This leaves the generated code more clean (the
9502 -- build-in-place function call in an object renaming declaration and
9503 -- displacements of the pointer to the build-in-place object in another
9504 -- renaming declaration) and allows us to invoke the routine that takes
9505 -- care of replacing the identifier of the renaming declaration (routine
9506 -- originally developed for the regular build-in-place management).
9509 Make_Object_Renaming_Declaration (Loc,
9510 Defining_Identifier => Make_Temporary (Loc, 'D'),
9511 Subtype_Mark => New_Occurrence_Of (Etype (Obj_Id), Loc),
9512 Name => Function_Call));
9515 Replace_Renaming_Declaration_Id (Obj_Decl, Original_Node (Obj_Decl));
9516 end Make_Build_In_Place_Iface_Call_In_Object_Declaration;
9518 --------------------------------------------
9519 -- Make_CPP_Constructor_Call_In_Allocator --
9520 --------------------------------------------
9522 procedure Make_CPP_Constructor_Call_In_Allocator
9523 (Allocator : Node_Id;
9524 Function_Call : Node_Id)
9526 Loc : constant Source_Ptr := Sloc (Function_Call);
9527 Acc_Type : constant Entity_Id := Etype (Allocator);
9528 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
9529 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
9531 New_Allocator : Node_Id;
9532 Return_Obj_Access : Entity_Id;
9536 pragma Assert (Nkind (Allocator) = N_Allocator
9537 and then Nkind (Function_Call) = N_Function_Call);
9538 pragma Assert (Convention (Function_Id) = Convention_CPP
9539 and then Is_Constructor (Function_Id));
9540 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
9542 -- Replace the initialized allocator of form "new T'(Func (...))" with
9543 -- an uninitialized allocator of form "new T", where T is the result
9544 -- subtype of the called function. The call to the function is handled
9545 -- separately further below.
9548 Make_Allocator (Loc,
9549 Expression => New_Occurrence_Of (Result_Subt, Loc));
9550 Set_No_Initialization (New_Allocator);
9552 -- Copy attributes to new allocator. Note that the new allocator
9553 -- logically comes from source if the original one did, so copy the
9554 -- relevant flag. This ensures proper treatment of the restriction
9555 -- No_Implicit_Heap_Allocations in this case.
9557 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
9558 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
9559 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
9561 Rewrite (Allocator, New_Allocator);
9563 -- Create a new access object and initialize it to the result of the
9564 -- new uninitialized allocator. Note: we do not use Allocator as the
9565 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9566 -- as this would create a sort of infinite "recursion".
9568 Return_Obj_Access := Make_Temporary (Loc, 'R');
9569 Set_Etype (Return_Obj_Access, Acc_Type);
9572 -- Rnnn : constant ptr_T := new (T);
9573 -- Init (Rnn.all,...);
9576 Make_Object_Declaration (Loc,
9577 Defining_Identifier => Return_Obj_Access,
9578 Constant_Present => True,
9579 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
9580 Expression => Relocate_Node (Allocator));
9581 Insert_Action (Allocator, Tmp_Obj);
9583 Insert_List_After_And_Analyze (Tmp_Obj,
9584 Build_Initialization_Call (Loc,
9586 Make_Explicit_Dereference (Loc,
9587 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
9588 Typ => Etype (Function_Id),
9589 Constructor_Ref => Function_Call));
9591 -- Finally, replace the allocator node with a reference to the result of
9592 -- the function call itself (which will effectively be an access to the
9593 -- object created by the allocator).
9595 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
9597 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9598 -- generate an implicit conversion to force displacement of the "this"
9601 if Is_Interface (Designated_Type (Acc_Type)) then
9602 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
9605 Analyze_And_Resolve (Allocator, Acc_Type);
9606 end Make_CPP_Constructor_Call_In_Allocator;
9608 ----------------------
9609 -- Might_Have_Tasks --
9610 ----------------------
9612 function Might_Have_Tasks (Typ : Entity_Id) return Boolean is
9614 return not Global_No_Tasking
9615 and then not No_Run_Time_Mode
9616 and then (Has_Task (Typ)
9617 or else (Is_Class_Wide_Type (Typ)
9618 and then Is_Limited_Record (Typ)
9619 and then not Has_Aspect
9620 (Etype (Typ), Aspect_No_Task_Parts)));
9621 end Might_Have_Tasks;
9623 ----------------------------
9624 -- Needs_BIP_Task_Actuals --
9625 ----------------------------
9627 function Needs_BIP_Task_Actuals (Func_Id : Entity_Id) return Boolean is
9628 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9629 Subp_Id : Entity_Id;
9630 Func_Typ : Entity_Id;
9633 if Global_No_Tasking or else No_Run_Time_Mode then
9637 -- For thunks we must rely on their target entity; otherwise, given that
9638 -- the profile of thunks for functions returning a limited interface
9639 -- type returns a class-wide type, we would erroneously add these extra
9642 if Is_Thunk (Func_Id) then
9643 Subp_Id := Thunk_Entity (Func_Id);
9651 Func_Typ := Underlying_Type (Etype (Subp_Id));
9653 -- At first sight, for all the following cases, we could add assertions
9654 -- to ensure that if Func_Id is frozen then the computed result matches
9655 -- with the availability of the task master extra formal; unfortunately
9656 -- this is not feasible because we may be precisely freezing this entity
9657 -- (that is, Is_Frozen has been set by Freeze_Entity but it has not
9658 -- completed its work).
9660 if Has_Task (Func_Typ) then
9663 elsif Ekind (Func_Id) = E_Function then
9664 return Might_Have_Tasks (Func_Typ);
9666 -- Handle subprogram type internally generated for dispatching call. We
9667 -- cannot rely on the return type of the subprogram type of dispatching
9668 -- calls since it is always a class-wide type (cf. Expand_Dispatching_
9671 elsif Ekind (Func_Id) = E_Subprogram_Type then
9672 if Is_Dispatch_Table_Entity (Func_Id) then
9673 return Has_BIP_Extra_Formal (Func_Id, BIP_Task_Master);
9675 return Might_Have_Tasks (Func_Typ);
9679 raise Program_Error;
9681 end Needs_BIP_Task_Actuals;
9683 -----------------------------------
9684 -- Needs_BIP_Finalization_Master --
9685 -----------------------------------
9687 function Needs_BIP_Finalization_Master
9688 (Func_Id : Entity_Id) return Boolean
9690 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9691 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9693 -- A formal giving the finalization master is needed for build-in-place
9694 -- functions whose result type needs finalization or is a tagged type.
9695 -- Tagged primitive build-in-place functions need such a formal because
9696 -- they can be called by a dispatching call, and extensions may require
9697 -- finalization even if the root type doesn't. This means they're also
9698 -- needed for tagged nonprimitive build-in-place functions with tagged
9699 -- results, since such functions can be called via access-to-function
9700 -- types, and those can be used to call primitives, so masters have to
9701 -- be passed to all such build-in-place functions, primitive or not.
9704 not Restriction_Active (No_Finalization)
9705 and then (Needs_Finalization (Func_Typ)
9706 or else Is_Tagged_Type (Func_Typ));
9707 end Needs_BIP_Finalization_Master;
9709 --------------------------
9710 -- Needs_BIP_Alloc_Form --
9711 --------------------------
9713 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
9714 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9715 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9717 return Requires_Transient_Scope (Func_Typ);
9718 end Needs_BIP_Alloc_Form;
9720 -------------------------------------
9721 -- Replace_Renaming_Declaration_Id --
9722 -------------------------------------
9724 procedure Replace_Renaming_Declaration_Id
9725 (New_Decl : Node_Id;
9726 Orig_Decl : Node_Id)
9728 New_Id : constant Entity_Id := Defining_Entity (New_Decl);
9729 Orig_Id : constant Entity_Id := Defining_Entity (Orig_Decl);
9732 Set_Chars (New_Id, Chars (Orig_Id));
9734 -- Swap next entity links in preparation for exchanging entities
9737 Next_Id : constant Entity_Id := Next_Entity (New_Id);
9739 Link_Entities (New_Id, Next_Entity (Orig_Id));
9740 Link_Entities (Orig_Id, Next_Id);
9743 Set_Homonym (New_Id, Homonym (Orig_Id));
9744 Exchange_Entities (New_Id, Orig_Id);
9746 -- Preserve source indication of original declaration, so that xref
9747 -- information is properly generated for the right entity.
9749 Preserve_Comes_From_Source (New_Decl, Orig_Decl);
9750 Preserve_Comes_From_Source (Orig_Id, Orig_Decl);
9752 Set_Comes_From_Source (New_Id, False);
9753 end Replace_Renaming_Declaration_Id;
9755 ---------------------------------
9756 -- Rewrite_Function_Call_For_C --
9757 ---------------------------------
9759 procedure Rewrite_Function_Call_For_C (N : Node_Id) is
9760 Orig_Func : constant Entity_Id := Entity (Name (N));
9761 Func_Id : constant Entity_Id := Ultimate_Alias (Orig_Func);
9762 Par : constant Node_Id := Parent (N);
9763 Proc_Id : constant Entity_Id := Corresponding_Procedure (Func_Id);
9764 Loc : constant Source_Ptr := Sloc (Par);
9766 Last_Actual : Node_Id;
9767 Last_Formal : Entity_Id;
9769 -- Start of processing for Rewrite_Function_Call_For_C
9772 -- The actuals may be given by named associations, so the added actual
9773 -- that is the target of the return value of the call must be a named
9774 -- association as well, so we retrieve the name of the generated
9777 Last_Formal := First_Formal (Proc_Id);
9778 while Present (Next_Formal (Last_Formal)) loop
9779 Next_Formal (Last_Formal);
9782 Actuals := Parameter_Associations (N);
9784 -- The original function may lack parameters
9786 if No (Actuals) then
9787 Actuals := New_List;
9790 -- If the function call is the expression of an assignment statement,
9791 -- transform the assignment into a procedure call. Generate:
9793 -- LHS := Func_Call (...);
9795 -- Proc_Call (..., LHS);
9797 -- If function is inherited, a conversion may be necessary.
9799 if Nkind (Par) = N_Assignment_Statement then
9800 Last_Actual := Name (Par);
9802 if not Comes_From_Source (Orig_Func)
9803 and then Etype (Orig_Func) /= Etype (Func_Id)
9806 Make_Type_Conversion (Loc,
9807 New_Occurrence_Of (Etype (Func_Id), Loc),
9812 Make_Parameter_Association (Loc,
9814 Make_Identifier (Loc, Chars (Last_Formal)),
9815 Explicit_Actual_Parameter => Last_Actual));
9818 Make_Procedure_Call_Statement (Loc,
9819 Name => New_Occurrence_Of (Proc_Id, Loc),
9820 Parameter_Associations => Actuals));
9823 -- Otherwise the context is an expression. Generate a temporary and a
9824 -- procedure call to obtain the function result. Generate:
9826 -- ... Func_Call (...) ...
9829 -- Proc_Call (..., Temp);
9834 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
9843 Make_Object_Declaration (Loc,
9844 Defining_Identifier => Temp_Id,
9845 Object_Definition =>
9846 New_Occurrence_Of (Etype (Func_Id), Loc));
9849 -- Proc_Call (..., Temp);
9852 Make_Parameter_Association (Loc,
9854 Make_Identifier (Loc, Chars (Last_Formal)),
9855 Explicit_Actual_Parameter =>
9856 New_Occurrence_Of (Temp_Id, Loc)));
9859 Make_Procedure_Call_Statement (Loc,
9860 Name => New_Occurrence_Of (Proc_Id, Loc),
9861 Parameter_Associations => Actuals);
9863 Insert_Actions (Par, New_List (Decl, Call));
9864 Rewrite (N, New_Occurrence_Of (Temp_Id, Loc));
9867 end Rewrite_Function_Call_For_C;
9869 ------------------------------------
9870 -- Set_Enclosing_Sec_Stack_Return --
9871 ------------------------------------
9873 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id) is
9877 -- Due to a possible mix of internally generated blocks, source blocks
9878 -- and loops, the scope stack may not be contiguous as all labels are
9879 -- inserted at the top level within the related function. Instead,
9880 -- perform a parent-based traversal and mark all appropriate constructs.
9882 while Present (P) loop
9884 -- Mark the label of a source or internally generated block or
9887 if Nkind (P) in N_Block_Statement | N_Loop_Statement then
9888 Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P)));
9890 -- Mark the enclosing function
9892 elsif Nkind (P) = N_Subprogram_Body then
9893 if Present (Corresponding_Spec (P)) then
9894 Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P));
9896 Set_Sec_Stack_Needed_For_Return (Defining_Entity (P));
9899 -- Do not go beyond the enclosing function
9906 end Set_Enclosing_Sec_Stack_Return;
9908 ------------------------------------
9909 -- Unqual_BIP_Iface_Function_Call --
9910 ------------------------------------
9912 function Unqual_BIP_Iface_Function_Call (Expr : Node_Id) return Node_Id is
9913 Has_Pointer_Displacement : Boolean := False;
9914 On_Object_Declaration : Boolean := False;
9915 -- Remember if processing the renaming expressions on recursion we have
9916 -- traversed an object declaration, since we can traverse many object
9917 -- declaration renamings but just one regular object declaration.
9919 function Unqual_BIP_Function_Call (Expr : Node_Id) return Node_Id;
9920 -- Search for a build-in-place function call skipping any qualification
9921 -- including qualified expressions, type conversions, references, calls
9922 -- to displace the pointer to the object, and renamings. Return Empty if
9923 -- no build-in-place function call is found.
9925 ------------------------------
9926 -- Unqual_BIP_Function_Call --
9927 ------------------------------
9929 function Unqual_BIP_Function_Call (Expr : Node_Id) return Node_Id is
9931 -- Recurse to handle case of multiple levels of qualification and/or
9934 if Nkind (Expr) in N_Qualified_Expression
9936 | N_Unchecked_Type_Conversion
9938 return Unqual_BIP_Function_Call (Expression (Expr));
9940 -- Recurse to handle case of multiple levels of references and
9941 -- explicit dereferences.
9943 elsif Nkind (Expr) in N_Attribute_Reference
9944 | N_Explicit_Dereference
9947 return Unqual_BIP_Function_Call (Prefix (Expr));
9949 -- Recurse on object renamings
9951 elsif Nkind (Expr) = N_Identifier
9952 and then Present (Entity (Expr))
9953 and then Ekind (Entity (Expr)) in E_Constant | E_Variable
9954 and then Nkind (Parent (Entity (Expr))) =
9955 N_Object_Renaming_Declaration
9956 and then Present (Renamed_Object (Entity (Expr)))
9958 return Unqual_BIP_Function_Call (Renamed_Object (Entity (Expr)));
9960 -- Recurse on the initializing expression of the first reference of
9961 -- an object declaration.
9963 elsif not On_Object_Declaration
9964 and then Nkind (Expr) = N_Identifier
9965 and then Present (Entity (Expr))
9966 and then Ekind (Entity (Expr)) in E_Constant | E_Variable
9967 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
9968 and then Present (Expression (Parent (Entity (Expr))))
9970 On_Object_Declaration := True;
9972 Unqual_BIP_Function_Call (Expression (Parent (Entity (Expr))));
9974 -- Recurse to handle calls to displace the pointer to the object to
9975 -- reference a secondary dispatch table.
9977 elsif Nkind (Expr) = N_Function_Call
9978 and then Nkind (Name (Expr)) in N_Has_Entity
9979 and then Present (Entity (Name (Expr)))
9980 and then Is_RTE (Entity (Name (Expr)), RE_Displace)
9982 Has_Pointer_Displacement := True;
9984 Unqual_BIP_Function_Call (First (Parameter_Associations (Expr)));
9986 -- Normal case: check if the inner expression is a BIP function call
9987 -- and the pointer to the object is displaced.
9989 elsif Has_Pointer_Displacement
9990 and then Is_Build_In_Place_Function_Call (Expr)
9997 end Unqual_BIP_Function_Call;
9999 -- Start of processing for Unqual_BIP_Iface_Function_Call
10002 if Nkind (Expr) = N_Identifier and then No (Entity (Expr)) then
10004 -- Can happen for X'Elab_Spec in the binder-generated file
10009 return Unqual_BIP_Function_Call (Expr);
10010 end Unqual_BIP_Iface_Function_Call;
10016 procedure Warn_BIP (Func_Call : Node_Id) is
10018 if Debug_Flag_Underscore_BB then
10019 Error_Msg_N ("build-in-place function call??", Func_Call);