1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2024, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Accessibility
; use Accessibility
;
27 with Atree
; use Atree
;
28 with Aspects
; use Aspects
;
29 with Checks
; use Checks
;
30 with Debug
; use Debug
;
31 with Einfo
; use Einfo
;
32 with Einfo
.Entities
; use Einfo
.Entities
;
33 with Einfo
.Utils
; use Einfo
.Utils
;
34 with Errout
; use Errout
;
35 with Elists
; use Elists
;
36 with Expander
; use Expander
;
37 with Exp_Aggr
; use Exp_Aggr
;
38 with Exp_Atag
; use Exp_Atag
;
39 with Exp_Ch3
; use Exp_Ch3
;
40 with Exp_Ch4
; use Exp_Ch4
;
41 with Exp_Ch7
; use Exp_Ch7
;
42 with Exp_Ch9
; use Exp_Ch9
;
43 with Exp_Dbug
; use Exp_Dbug
;
44 with Exp_Disp
; use Exp_Disp
;
45 with Exp_Dist
; use Exp_Dist
;
46 with Exp_Intr
; use Exp_Intr
;
47 with Exp_Pakd
; use Exp_Pakd
;
48 with Exp_Tss
; use Exp_Tss
;
49 with Exp_Util
; use Exp_Util
;
50 with Freeze
; use Freeze
;
51 with Inline
; use Inline
;
52 with Itypes
; use Itypes
;
54 with Namet
; use Namet
;
55 with Nlists
; use Nlists
;
56 with Nmake
; use Nmake
;
58 with Restrict
; use Restrict
;
59 with Rident
; use Rident
;
60 with Rtsfind
; use Rtsfind
;
62 with Sem_Aux
; use Sem_Aux
;
63 with Sem_Ch6
; use Sem_Ch6
;
64 with Sem_Ch8
; use Sem_Ch8
;
65 with Sem_Ch13
; use Sem_Ch13
;
66 with Sem_Dim
; use Sem_Dim
;
67 with Sem_Disp
; use Sem_Disp
;
68 with Sem_Dist
; use Sem_Dist
;
69 with Sem_Eval
; use Sem_Eval
;
70 with Sem_Mech
; use Sem_Mech
;
71 with Sem_Res
; use Sem_Res
;
72 with Sem_SCIL
; use Sem_SCIL
;
73 with Sem_Util
; use Sem_Util
;
74 use Sem_Util
.Storage_Model_Support
;
75 with Sinfo
; use Sinfo
;
76 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
77 with Sinfo
.Utils
; use Sinfo
.Utils
;
78 with Sinput
; use Sinput
;
79 with Snames
; use Snames
;
80 with Stand
; use Stand
;
81 with Tbuild
; use Tbuild
;
82 with Uintp
; use Uintp
;
83 with Validsw
; use Validsw
;
85 package body Exp_Ch6
is
87 --------------------------------
88 -- Function return mechanisms --
89 --------------------------------
91 -- This is a summary of the various function return mechanisms implemented
92 -- in GNAT for Ada 2005 and later versions of the language. In the below
93 -- table, the first column must be read as an if expression: if the result
94 -- type of the function is limited, then the return mechanism is and ...;
95 -- elsif the result type is indefinite or large definite, then ...; elsif
96 -- ...; else ... The different mechanisms are implemented either in the
97 -- front end, or in the back end, or partly in both ends, depending on the
100 -- Result type | Return mechanism | Front end | Back end
101 -- --------------------------------------------------------------------
103 -- Limited Build In Place All
105 -- Indefinite/ Secondary Stack Needs Fin. Others
108 -- Needs Fin. Secondary Stack All
111 -- Needs Fin. Invisible Parameter All All
112 -- (BERS True) (return) (call)
114 -- By Reference Invisible Parameter All
116 -- Others Primary stack/ All
119 -- Needs Fin.: type needs finalization [RM 7.6(9.1/2-9.6/2)]
120 -- BERS: Opt.Back_End_Return_Slot setting
122 -- The table is valid for all calls except for those dispatching on result;
123 -- the latter calls are considered as returning a class-wide type and thus
124 -- always return on the secondary stack, with the help of a small wrapper
125 -- function (thunk) if the original result type is not itself returned on
126 -- the secondary stack as per the above table.
128 -- Suffixes for Build-In-Place extra formals
130 BIP_Alloc_Suffix
: constant String := "BIPalloc";
131 BIP_Storage_Pool_Suffix
: constant String := "BIPstoragepool";
132 BIP_Collection_Suffix
: constant String := "BIPcollection";
133 BIP_Task_Master_Suffix
: constant String := "BIPtaskmaster";
134 BIP_Activation_Chain_Suffix
: constant String := "BIPactivationchain";
135 BIP_Object_Access_Suffix
: constant String := "BIPaccess";
137 -----------------------
138 -- Local Subprograms --
139 -----------------------
141 procedure Add_Access_Actual_To_Build_In_Place_Call
142 (Function_Call
: Node_Id
;
143 Function_Id
: Entity_Id
;
144 Return_Object
: Node_Id
;
145 Is_Access
: Boolean := False);
146 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
147 -- object name given by Return_Object and add the attribute to the end of
148 -- the actual parameter list associated with the build-in-place function
149 -- call denoted by Function_Call. However, if Is_Access is True, then
150 -- Return_Object is already an access expression, in which case it's passed
151 -- along directly to the build-in-place function. Finally, if Return_Object
152 -- is empty, then pass a null literal as the actual.
154 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
155 (Function_Call
: Node_Id
;
156 Function_Id
: Entity_Id
;
157 Alloc_Form
: BIP_Allocation_Form
:= Unspecified
;
158 Alloc_Form_Exp
: Node_Id
:= Empty
;
159 Pool_Exp
: Node_Id
:= Empty
);
160 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
161 -- them, add the actuals parameters BIP_Alloc_Form and BIP_Storage_Pool.
162 -- If Alloc_Form_Exp is present, then pass it for the first parameter,
163 -- otherwise pass a literal corresponding to the Alloc_Form parameter
164 -- (which must not be Unspecified in that case). If Pool_Exp is present,
165 -- then use it for BIP_Storage_Pool, otherwise pass "null".
167 procedure Add_Collection_Actual_To_Build_In_Place_Call
168 (Function_Call
: Node_Id
;
169 Function_Id
: Entity_Id
;
170 Ptr_Typ
: Entity_Id
:= Empty
;
171 Collection_Exp
: Node_Id
:= Empty
);
172 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
173 -- finalization actions, add an actual parameter which is a pointer to the
174 -- collection of the access type used by the caller. If Collection_Exp is
175 -- present, then that will be passed as the actual. Otherwise, if Ptr_Typ
176 -- is Empty, this will result in an automatic "null" value for the actual.
178 procedure Add_Task_Actuals_To_Build_In_Place_Call
179 (Function_Call
: Node_Id
;
180 Function_Id
: Entity_Id
;
181 Master_Actual
: Node_Id
;
182 Chain
: Node_Id
:= Empty
);
183 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
184 -- contains tasks, add two actual parameters: the master, and a pointer to
185 -- the caller's activation chain. Master_Actual is the actual parameter
186 -- expression to pass for the master. In most cases, this is the current
187 -- master (_master). The two exceptions are: If the function call is the
188 -- initialization expression for an allocator, we pass the master of the
189 -- access type. If the function call is the initialization expression for a
190 -- return object, we pass along the master passed in by the caller. In most
191 -- contexts, the activation chain to pass is the local one, which is
192 -- indicated by No (Chain). However, in an allocator, the caller passes in
193 -- the activation Chain. Note: Master_Actual can be Empty, but only if
194 -- there are no tasks.
196 function Caller_Known_Size
197 (Func_Call
: Node_Id
;
198 Result_Subt
: Entity_Id
) return Boolean;
199 -- True if result subtype is definite or has a size that does not require
200 -- secondary stack usage (i.e. no variant part or components whose type
201 -- depends on discriminants). In particular, untagged types with only
202 -- access discriminants do not require secondary stack use. Note we must
203 -- always use the secondary stack for dispatching-on-result calls.
205 function Check_BIP_Actuals
206 (Subp_Call
: Node_Id
;
207 Subp_Id
: Entity_Id
) return Boolean;
208 -- Given a subprogram call to the given subprogram return True if the
209 -- names of BIP extra actual and formal parameters match, and the number
210 -- of actuals (including extra actuals) matches the number of formals.
212 function Check_Number_Of_Actuals
213 (Subp_Call
: Node_Id
;
214 Subp_Id
: Entity_Id
) return Boolean;
215 -- Given a subprogram call to the given subprogram return True if the
216 -- number of actual parameters (including extra actuals) is correct.
218 procedure Check_Overriding_Operation
(Subp
: Entity_Id
);
219 -- Subp is a dispatching operation. Check whether it may override an
220 -- inherited private operation, in which case its DT entry is that of
221 -- the hidden operation, not the one it may have received earlier.
222 -- This must be done before emitting the code to set the corresponding
223 -- DT to the address of the subprogram. The actual placement of Subp in
224 -- the proper place in the list of primitive operations is done in
225 -- Declare_Inherited_Private_Subprograms, which also has to deal with
226 -- implicit operations. This duplication is unavoidable for now???
228 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
);
229 -- This procedure is called only if the subprogram body N, whose spec
230 -- has the given entity Spec, contains a parameterless recursive call.
231 -- It attempts to generate runtime code to detect if this a case of
232 -- infinite recursion.
234 -- The body is scanned to determine dependencies. If the only external
235 -- dependencies are on a small set of scalar variables, then the values
236 -- of these variables are captured on entry to the subprogram, and if
237 -- the values are not changed for the call, we know immediately that
238 -- we have an infinite recursion.
240 procedure Expand_Actuals
243 Post_Call
: out List_Id
);
244 -- Return a list of actions to take place after the call in Post_Call. The
245 -- call will later be rewritten as an Expression_With_Actions, with the
246 -- Post_Call actions inserted, and the call inside.
248 -- For each actual of an in-out or out parameter which is a numeric (view)
249 -- conversion of the form T (A), where A denotes a variable, we insert the
252 -- Temp : T[ := T (A)];
254 -- prior to the call. Then we replace the actual with a reference to Temp,
255 -- and append the assignment:
257 -- A := TypeA (Temp);
259 -- after the call. Here TypeA is the actual type of variable A. For out
260 -- parameters, the initial declaration has no expression. If A is not an
261 -- entity name, we generate instead:
263 -- Var : TypeA renames A;
264 -- Temp : T := Var; -- omitting expression for out parameter.
266 -- Var := TypeA (Temp);
268 -- For other in-out parameters, we emit the required constraint checks
269 -- before and/or after the call.
271 -- For all parameter modes, actuals that denote components and slices of
272 -- packed arrays are expanded into suitable temporaries.
274 -- For nonscalar objects that are possibly unaligned, add call by copy code
275 -- (copy in for IN and IN OUT, copy out for OUT and IN OUT).
277 -- For OUT and IN OUT parameters, add predicate checks after the call
278 -- based on the predicates of the actual type.
280 procedure Expand_Call_Helper
(N
: Node_Id
; Post_Call
: out List_Id
);
281 -- Does the main work of Expand_Call. Post_Call is as for Expand_Actuals.
283 procedure Expand_Ctrl_Function_Call
(N
: Node_Id
; Use_Sec_Stack
: Boolean);
284 -- N is a function call which returns a controlled object. Transform the
285 -- call into a temporary which retrieves the returned object from the
286 -- primary or secondary stack (Use_Sec_Stack says which) using 'reference.
288 procedure Expand_Non_Function_Return
(N
: Node_Id
);
289 -- Expand a simple return statement found in a procedure body, entry body,
290 -- accept statement, or an extended return statement. Note that all non-
291 -- function returns are simple return statements.
293 function Expand_Protected_Object_Reference
295 Scop
: Entity_Id
) return Node_Id
;
297 procedure Expand_Protected_Subprogram_Call
301 -- A call to a protected subprogram within the protected object may appear
302 -- as a regular call. The list of actuals must be expanded to contain a
303 -- reference to the object itself, and the call becomes a call to the
304 -- corresponding protected subprogram.
306 procedure Expand_Simple_Function_Return
(N
: Node_Id
);
307 -- Expand simple return from function. In the case where we are returning
308 -- from a function body this is called by Expand_N_Simple_Return_Statement.
310 procedure Insert_Post_Call_Actions
(N
: Node_Id
; Post_Call
: List_Id
);
311 -- Insert the Post_Call list previously produced by routine Expand_Actuals
312 -- or Expand_Call_Helper into the tree.
314 function Is_Function_Call_With_BIP_Formals
(N
: Node_Id
) return Boolean;
315 -- Ada 2005 (AI-318-02): Returns True if N denotes a call to a function
316 -- that requires handling as a build-in-place call, that is, BIP function
317 -- calls and calls to functions with inherited BIP formals. For example:
319 -- type Iface is limited interface;
320 -- function Get_Object return Iface;
321 -- -- This function has BIP extra formals
323 -- type Root1 is limited tagged record ...
324 -- type T1 is new Root1 and Iface with ...
325 -- function Get_Object return T1;
326 -- -- This primitive requires the BIP formals, and the evaluation of
327 -- -- Is_Build_In_Place_Function_Call returns True.
329 -- type Root2 is tagged record ...
330 -- type T2 is new Root2 and Iface with ...
331 -- function Get_Object return T2;
332 -- -- This primitive inherits the BIP formals of the interface primitive
333 -- -- but, given that T2 is not a limited type, it does not require such
334 -- -- formals; therefore Is_Build_In_Place_Function_Call returns False.
336 procedure Replace_Renaming_Declaration_Id
338 Orig_Decl
: Node_Id
);
339 -- Replace the internal identifier of the new renaming declaration New_Decl
340 -- with the identifier of its original declaration Orig_Decl exchanging the
341 -- entities containing their defining identifiers to ensure the correct
342 -- replacement of the object declaration by the object renaming declaration
343 -- to avoid homograph conflicts (since the object declaration's defining
344 -- identifier was already entered in the current scope). The Next_Entity
345 -- links of the two entities are also swapped since the entities are part
346 -- of the return scope's entity list and the list structure would otherwise
347 -- be corrupted. The homonym chain is preserved as well.
349 procedure Rewrite_Function_Call_For_C
(N
: Node_Id
);
350 -- When generating C code, replace a call to a function that returns an
351 -- array into the generated procedure with an additional out parameter.
353 procedure Set_Enclosing_Sec_Stack_Return
(N
: Node_Id
);
354 -- N is a return statement for a function that returns its result on the
355 -- secondary stack. This sets the Sec_Stack_Needed_For_Return flag on the
356 -- function and all blocks and loops that the return statement is jumping
357 -- out of. This ensures that the secondary stack is not released; otherwise
358 -- the function result would be reclaimed before returning to the caller.
360 procedure Warn_BIP
(Func_Call
: Node_Id
);
361 -- Give a warning on a build-in-place function call if the -gnatd_B switch
364 ----------------------------------------------
365 -- Add_Access_Actual_To_Build_In_Place_Call --
366 ----------------------------------------------
368 procedure Add_Access_Actual_To_Build_In_Place_Call
369 (Function_Call
: Node_Id
;
370 Function_Id
: Entity_Id
;
371 Return_Object
: Node_Id
;
372 Is_Access
: Boolean := False)
374 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
375 Obj_Address
: Node_Id
;
376 Obj_Acc_Formal
: Entity_Id
;
379 -- Locate the implicit access parameter in the called function
381 Obj_Acc_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Object_Access
);
383 -- If no return object is provided, then pass null
385 if No
(Return_Object
) then
386 Obj_Address
:= Make_Null
(Loc
);
387 Set_Parent
(Obj_Address
, Function_Call
);
389 -- If Return_Object is already an expression of an access type, then use
390 -- it directly, since it must be an access value denoting the return
391 -- object, and couldn't possibly be the return object itself.
394 Obj_Address
:= Return_Object
;
395 Set_Parent
(Obj_Address
, Function_Call
);
397 -- Apply Unrestricted_Access to caller's return object
401 Make_Attribute_Reference
(Loc
,
402 Prefix
=> Return_Object
,
403 Attribute_Name
=> Name_Unrestricted_Access
);
405 Set_Parent
(Return_Object
, Obj_Address
);
406 Set_Parent
(Obj_Address
, Function_Call
);
409 Analyze_And_Resolve
(Obj_Address
, Etype
(Obj_Acc_Formal
));
411 -- Build the parameter association for the new actual and add it to the
412 -- end of the function's actuals.
414 Add_Extra_Actual_To_Call
(Function_Call
, Obj_Acc_Formal
, Obj_Address
);
415 end Add_Access_Actual_To_Build_In_Place_Call
;
417 ------------------------------------------------------
418 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
419 ------------------------------------------------------
421 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
422 (Function_Call
: Node_Id
;
423 Function_Id
: Entity_Id
;
424 Alloc_Form
: BIP_Allocation_Form
:= Unspecified
;
425 Alloc_Form_Exp
: Node_Id
:= Empty
;
426 Pool_Exp
: Node_Id
:= Empty
)
428 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
430 Alloc_Form_Actual
: Node_Id
;
431 Alloc_Form_Formal
: Node_Id
;
434 -- Nothing to do when the size of the object is known, and the caller is
435 -- in charge of allocating it, and the callee doesn't unconditionally
436 -- require an allocation form (such as due to having a tagged result).
438 if not Needs_BIP_Alloc_Form
(Function_Id
) then
442 -- Locate the implicit allocation form parameter in the called function.
443 -- Maybe it would be better for each implicit formal of a build-in-place
444 -- function to have a flag or a Uint attribute to identify it. ???
446 Alloc_Form_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Alloc_Form
);
448 if Present
(Alloc_Form_Exp
) then
449 pragma Assert
(Alloc_Form
= Unspecified
);
451 Alloc_Form_Actual
:= Alloc_Form_Exp
;
454 pragma Assert
(Alloc_Form
/= Unspecified
);
457 Make_Integer_Literal
(Loc
,
458 Intval
=> UI_From_Int
(BIP_Allocation_Form
'Pos (Alloc_Form
)));
461 Analyze_And_Resolve
(Alloc_Form_Actual
, Etype
(Alloc_Form_Formal
));
463 -- Build the parameter association for the new actual and add it to the
464 -- end of the function's actuals.
466 Add_Extra_Actual_To_Call
467 (Function_Call
, Alloc_Form_Formal
, Alloc_Form_Actual
);
469 -- Pass the Storage_Pool parameter. This parameter is omitted on ZFP as
470 -- those targets do not support pools.
472 if RTE_Available
(RE_Root_Storage_Pool_Ptr
) then
474 Pool_Actual
: constant Node_Id
:=
475 (if Present
(Pool_Exp
) then Pool_Exp
else Make_Null
(Loc
));
476 Pool_Formal
: constant Node_Id
:=
477 Build_In_Place_Formal
(Function_Id
, BIP_Storage_Pool
);
480 Analyze_And_Resolve
(Pool_Actual
, Etype
(Pool_Formal
));
481 Add_Extra_Actual_To_Call
(Function_Call
, Pool_Formal
, Pool_Actual
);
484 end Add_Unconstrained_Actuals_To_Build_In_Place_Call
;
486 --------------------------------------------------
487 -- Add_Collection_Actual_To_Build_In_Place_Call --
488 --------------------------------------------------
490 procedure Add_Collection_Actual_To_Build_In_Place_Call
491 (Function_Call
: Node_Id
;
492 Function_Id
: Entity_Id
;
493 Ptr_Typ
: Entity_Id
:= Empty
;
494 Collection_Exp
: Node_Id
:= Empty
)
496 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
500 Desig_Typ
: Entity_Id
;
503 if not Needs_BIP_Collection
(Function_Id
) then
507 Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Collection
);
509 -- If there is a finalization collection actual, such as the implicit
510 -- finalization collection of an enclosing build-in-place function,
511 -- then this must be added as an extra actual of the call.
513 if Present
(Collection_Exp
) then
514 Actual
:= Collection_Exp
;
516 -- Case where the context does not require an actual collection
518 elsif No
(Ptr_Typ
) then
519 Actual
:= Make_Null
(Loc
);
522 Desig_Typ
:= Directly_Designated_Type
(Ptr_Typ
);
524 -- Check for a library-level access type whose designated type has
525 -- suppressed finalization or the access type is subject to pragma
526 -- No_Heap_Finalization. Such an access type lacks a collection. Pass
527 -- a null actual to callee in order to signal a missing collection.
529 if Is_Library_Level_Entity
(Ptr_Typ
)
530 and then (Finalize_Storage_Only
(Desig_Typ
)
531 or else No_Heap_Finalization
(Ptr_Typ
))
533 Actual
:= Make_Null
(Loc
);
535 -- Types in need of finalization actions
537 elsif Needs_Finalization
(Desig_Typ
) then
539 -- The general mechanism of creating finalization collections
540 -- for anonymous access types is disabled by default, otherwise
541 -- finalization collections will pop all over the place. Instead
542 -- such types use context-specific collections.
544 if Ekind
(Ptr_Typ
) = E_Anonymous_Access_Type
545 and then No
(Finalization_Collection
(Ptr_Typ
))
547 Build_Anonymous_Collection
(Ptr_Typ
);
550 -- Access-to-controlled types should always have a collection
552 pragma Assert
(Present
(Finalization_Collection
(Ptr_Typ
)));
555 Make_Attribute_Reference
(Loc
,
557 New_Occurrence_Of
(Finalization_Collection
(Ptr_Typ
), Loc
),
558 Attribute_Name
=> Name_Unrestricted_Access
);
563 Actual
:= Make_Null
(Loc
);
567 Analyze_And_Resolve
(Actual
, Etype
(Formal
));
569 -- Build the parameter association for the new actual and add it to
570 -- the end of the function's actuals.
572 Add_Extra_Actual_To_Call
(Function_Call
, Formal
, Actual
);
573 end Add_Collection_Actual_To_Build_In_Place_Call
;
575 ------------------------------
576 -- Add_Extra_Actual_To_Call --
577 ------------------------------
579 procedure Add_Extra_Actual_To_Call
580 (Subprogram_Call
: Node_Id
;
581 Extra_Formal
: Entity_Id
;
582 Extra_Actual
: Node_Id
)
584 Loc
: constant Source_Ptr
:= Sloc
(Subprogram_Call
);
585 Param_Assoc
: Node_Id
;
589 Make_Parameter_Association
(Loc
,
590 Selector_Name
=> New_Occurrence_Of
(Extra_Formal
, Loc
),
591 Explicit_Actual_Parameter
=> Extra_Actual
);
593 Set_Parent
(Param_Assoc
, Subprogram_Call
);
594 Set_Parent
(Extra_Actual
, Param_Assoc
);
596 if Present
(Parameter_Associations
(Subprogram_Call
)) then
597 if Nkind
(Last
(Parameter_Associations
(Subprogram_Call
))) =
598 N_Parameter_Association
601 -- Find last named actual, and append
606 L
:= First_Actual
(Subprogram_Call
);
607 while Present
(L
) loop
608 if No
(Next_Actual
(L
)) then
609 Set_Next_Named_Actual
(Parent
(L
), Extra_Actual
);
617 Set_First_Named_Actual
(Subprogram_Call
, Extra_Actual
);
620 Append
(Param_Assoc
, To
=> Parameter_Associations
(Subprogram_Call
));
623 Set_Parameter_Associations
(Subprogram_Call
, New_List
(Param_Assoc
));
624 Set_First_Named_Actual
(Subprogram_Call
, Extra_Actual
);
626 end Add_Extra_Actual_To_Call
;
628 ---------------------------------------------
629 -- Add_Task_Actuals_To_Build_In_Place_Call --
630 ---------------------------------------------
632 procedure Add_Task_Actuals_To_Build_In_Place_Call
633 (Function_Call
: Node_Id
;
634 Function_Id
: Entity_Id
;
635 Master_Actual
: Node_Id
;
636 Chain
: Node_Id
:= Empty
)
638 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
640 Chain_Actual
: Node_Id
;
641 Chain_Formal
: Node_Id
;
642 Master_Formal
: Node_Id
;
645 pragma Assert
(Ekind
(Function_Id
) in E_Function
646 | E_Subprogram_Type
);
648 -- No such extra parameters are needed if there are no tasks
650 if not Needs_BIP_Task_Actuals
(Function_Id
) then
652 -- However we must add dummy extra actuals if the function is
653 -- a dispatching operation that inherited these extra formals
654 -- or an access-to-subprogram type that requires these extra
657 if Has_BIP_Extra_Formal
(Function_Id
, BIP_Task_Master
,
658 Must_Be_Frozen
=> False)
661 Build_In_Place_Formal
(Function_Id
, BIP_Task_Master
);
662 Actual
:= Make_Integer_Literal
(Loc
, Uint_0
);
663 Analyze_And_Resolve
(Actual
, Etype
(Master_Formal
));
664 Add_Extra_Actual_To_Call
(Function_Call
, Master_Formal
, Actual
);
667 Build_In_Place_Formal
(Function_Id
, BIP_Activation_Chain
);
668 Chain_Actual
:= Make_Null
(Loc
);
669 Analyze_And_Resolve
(Chain_Actual
, Etype
(Chain_Formal
));
670 Add_Extra_Actual_To_Call
671 (Function_Call
, Chain_Formal
, Chain_Actual
);
677 Actual
:= Master_Actual
;
679 -- Use a dummy _master actual in case of No_Task_Hierarchy
681 if Restriction_Active
(No_Task_Hierarchy
) then
682 Actual
:= Make_Integer_Literal
(Loc
, Library_Task_Level
);
684 -- In the case where we use the master associated with an access type,
685 -- the actual is an entity and requires an explicit reference.
687 elsif Nkind
(Actual
) = N_Defining_Identifier
then
688 Actual
:= New_Occurrence_Of
(Actual
, Loc
);
691 -- Locate the implicit master parameter in the called function
693 Master_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Task_Master
);
694 Analyze_And_Resolve
(Actual
, Etype
(Master_Formal
));
696 -- Build the parameter association for the new actual and add it to the
697 -- end of the function's actuals.
699 Add_Extra_Actual_To_Call
(Function_Call
, Master_Formal
, Actual
);
701 -- Locate the implicit activation chain parameter in the called function
704 Build_In_Place_Formal
(Function_Id
, BIP_Activation_Chain
);
706 -- Create the actual which is a pointer to the current activation chain
708 if Restriction_Active
(No_Task_Hierarchy
) then
709 Chain_Actual
:= Make_Null
(Loc
);
711 elsif No
(Chain
) then
713 Make_Attribute_Reference
(Loc
,
714 Prefix
=> Make_Identifier
(Loc
, Name_uChain
),
715 Attribute_Name
=> Name_Unrestricted_Access
);
717 -- Allocator case; make a reference to the Chain passed in by the caller
721 Make_Attribute_Reference
(Loc
,
722 Prefix
=> New_Occurrence_Of
(Chain
, Loc
),
723 Attribute_Name
=> Name_Unrestricted_Access
);
726 Analyze_And_Resolve
(Chain_Actual
, Etype
(Chain_Formal
));
728 -- Build the parameter association for the new actual and add it to the
729 -- end of the function's actuals.
731 Add_Extra_Actual_To_Call
(Function_Call
, Chain_Formal
, Chain_Actual
);
732 end Add_Task_Actuals_To_Build_In_Place_Call
;
734 ----------------------------------
735 -- Apply_CW_Accessibility_Check --
736 ----------------------------------
738 procedure Apply_CW_Accessibility_Check
(Exp
: Node_Id
; Func
: Entity_Id
) is
739 Loc
: constant Source_Ptr
:= Sloc
(Exp
);
742 -- CodePeer does not do anything useful on Ada.Tags.Type_Specific_Data
745 if Ada_Version
>= Ada_2005
746 and then not CodePeer_Mode
747 and then Tagged_Type_Expansion
748 and then not Scope_Suppress
.Suppress
(Accessibility_Check
)
750 (Is_Class_Wide_Type
(Etype
(Exp
))
751 or else Nkind
(Exp
) in
752 N_Type_Conversion | N_Unchecked_Type_Conversion
753 or else (Is_Entity_Name
(Exp
)
754 and then Is_Formal
(Entity
(Exp
)))
755 or else Scope_Depth
(Enclosing_Dynamic_Scope
(Etype
(Exp
))) >
756 Scope_Depth
(Enclosing_Dynamic_Scope
(Func
)))
762 -- Ada 2005 (AI-251): In class-wide interface objects we displace
763 -- "this" to reference the base of the object. This is required to
764 -- get access to the TSD of the object.
766 if Is_Class_Wide_Type
(Etype
(Exp
))
767 and then Is_Interface
(Etype
(Exp
))
769 -- If the expression is an explicit dereference then we can
770 -- directly displace the pointer to reference the base of
773 if Nkind
(Exp
) = N_Explicit_Dereference
then
775 Make_Explicit_Dereference
(Loc
,
777 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
778 Make_Function_Call
(Loc
,
780 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
781 Parameter_Associations
=> New_List
(
782 Unchecked_Convert_To
(RTE
(RE_Address
),
783 Duplicate_Subexpr
(Prefix
(Exp
)))))));
785 -- Similar case to the previous one but the expression is a
786 -- renaming of an explicit dereference.
788 elsif Nkind
(Exp
) = N_Identifier
789 and then Present
(Renamed_Object
(Entity
(Exp
)))
790 and then Nkind
(Renamed_Object
(Entity
(Exp
)))
791 = N_Explicit_Dereference
794 Make_Explicit_Dereference
(Loc
,
796 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
797 Make_Function_Call
(Loc
,
799 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
800 Parameter_Associations
=> New_List
(
801 Unchecked_Convert_To
(RTE
(RE_Address
),
804 (Renamed_Object
(Entity
(Exp
)))))))));
806 -- Common case: obtain the address of the actual object and
807 -- displace the pointer to reference the base of the object.
811 Make_Explicit_Dereference
(Loc
,
813 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
814 Make_Function_Call
(Loc
,
816 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
817 Parameter_Associations
=> New_List
(
818 Make_Attribute_Reference
(Loc
,
819 Prefix
=> Duplicate_Subexpr
(Exp
),
820 Attribute_Name
=> Name_Address
)))));
824 Make_Attribute_Reference
(Loc
,
825 Prefix
=> Duplicate_Subexpr
(Exp
),
826 Attribute_Name
=> Name_Tag
);
829 -- Suppress junk access chacks on RE_Tag_Ptr
832 Make_Raise_Program_Error
(Loc
,
835 Left_Opnd
=> Build_Get_Access_Level
(Loc
, Tag_Node
),
837 Make_Integer_Literal
(Loc
,
838 Scope_Depth
(Enclosing_Dynamic_Scope
(Func
)))),
839 Reason
=> PE_Accessibility_Check_Failed
),
840 Suppress
=> Access_Check
);
843 end Apply_CW_Accessibility_Check
;
845 -----------------------
846 -- BIP_Formal_Suffix --
847 -----------------------
849 function BIP_Formal_Suffix
(Kind
: BIP_Formal_Kind
) return String is
852 when BIP_Alloc_Form
=>
853 return BIP_Alloc_Suffix
;
855 when BIP_Storage_Pool
=>
856 return BIP_Storage_Pool_Suffix
;
858 when BIP_Collection
=>
859 return BIP_Collection_Suffix
;
861 when BIP_Task_Master
=>
862 return BIP_Task_Master_Suffix
;
864 when BIP_Activation_Chain
=>
865 return BIP_Activation_Chain_Suffix
;
867 when BIP_Object_Access
=>
868 return BIP_Object_Access_Suffix
;
870 end BIP_Formal_Suffix
;
872 ---------------------
873 -- BIP_Suffix_Kind --
874 ---------------------
876 function BIP_Suffix_Kind
(E
: Entity_Id
) return BIP_Formal_Kind
is
877 Nam
: constant String := Get_Name_String
(Chars
(E
));
879 function Has_Suffix
(Suffix
: String) return Boolean;
880 -- Return True if Nam has suffix Suffix
882 function Has_Suffix
(Suffix
: String) return Boolean is
883 Len
: constant Natural := Suffix
'Length;
885 return Nam
'Length > Len
886 and then Nam
(Nam
'Last - Len
+ 1 .. Nam
'Last) = Suffix
;
889 -- Start of processing for BIP_Suffix_Kind
892 if Has_Suffix
(BIP_Alloc_Suffix
) then
893 return BIP_Alloc_Form
;
895 elsif Has_Suffix
(BIP_Storage_Pool_Suffix
) then
896 return BIP_Storage_Pool
;
898 elsif Has_Suffix
(BIP_Collection_Suffix
) then
899 return BIP_Collection
;
901 elsif Has_Suffix
(BIP_Task_Master_Suffix
) then
902 return BIP_Task_Master
;
904 elsif Has_Suffix
(BIP_Activation_Chain_Suffix
) then
905 return BIP_Activation_Chain
;
907 elsif Has_Suffix
(BIP_Object_Access_Suffix
) then
908 return BIP_Object_Access
;
915 ---------------------------
916 -- Build_In_Place_Formal --
917 ---------------------------
919 function Build_In_Place_Formal
921 Kind
: BIP_Formal_Kind
) return Entity_Id
923 Extra_Formal
: Entity_Id
:= Extra_Formals
(Func
);
924 Formal_Suffix
: constant String := BIP_Formal_Suffix
(Kind
);
927 -- Maybe it would be better for each implicit formal of a build-in-place
928 -- function to have a flag or a Uint attribute to identify it. ???
930 -- The return type in the function declaration may have been a limited
931 -- view, and the extra formals for the function were not generated at
932 -- that point. At the point of call the full view must be available and
933 -- the extra formals can be created and Returns_By_Ref computed.
935 if No
(Extra_Formal
) then
936 Create_Extra_Formals
(Func
);
937 Extra_Formal
:= Extra_Formals
(Func
);
938 Compute_Returns_By_Ref
(Func
);
941 -- We search for a formal with a matching suffix. We can't search
942 -- for the full name, because of the code at the end of Sem_Ch6.-
943 -- Create_Extra_Formals, which copies the Extra_Formals over to
944 -- the Alias of an instance, which will cause the formals to have
945 -- "incorrect" names.
947 while Present
(Extra_Formal
) loop
949 Name
: constant String := Get_Name_String
(Chars
(Extra_Formal
));
951 exit when Name
'Length >= Formal_Suffix
'Length
952 and then Formal_Suffix
=
953 Name
(Name
'Last - Formal_Suffix
'Length + 1 .. Name
'Last);
956 Next_Formal_With_Extras
(Extra_Formal
);
959 if No
(Extra_Formal
) then
964 end Build_In_Place_Formal
;
966 -------------------------------
967 -- Build_Procedure_Body_Form --
968 -------------------------------
970 function Build_Procedure_Body_Form
971 (Func_Id
: Entity_Id
;
972 Func_Body
: Node_Id
) return Node_Id
974 Loc
: constant Source_Ptr
:= Sloc
(Func_Body
);
976 Proc_Decl
: constant Node_Id
:= Prev
(Unit_Declaration_Node
(Func_Id
));
977 -- It is assumed that the node before the declaration of the
978 -- corresponding subprogram spec is the declaration of the procedure
981 Proc_Id
: constant Entity_Id
:= Defining_Entity
(Proc_Decl
);
983 procedure Replace_Returns
(Param_Id
: Entity_Id
; Stmts
: List_Id
);
984 -- Replace each return statement found in the list Stmts with an
985 -- assignment of the return expression to parameter Param_Id.
987 ---------------------
988 -- Replace_Returns --
989 ---------------------
991 procedure Replace_Returns
(Param_Id
: Entity_Id
; Stmts
: List_Id
) is
995 Stmt
:= First
(Stmts
);
996 while Present
(Stmt
) loop
997 if Nkind
(Stmt
) = N_Block_Statement
then
998 Replace_Returns
(Param_Id
,
999 Statements
(Handled_Statement_Sequence
(Stmt
)));
1001 elsif Nkind
(Stmt
) = N_Case_Statement
then
1005 Alt
:= First
(Alternatives
(Stmt
));
1006 while Present
(Alt
) loop
1007 Replace_Returns
(Param_Id
, Statements
(Alt
));
1012 elsif Nkind
(Stmt
) = N_Extended_Return_Statement
then
1014 Ret_Obj
: constant Entity_Id
:=
1016 (First
(Return_Object_Declarations
(Stmt
)));
1017 Assign
: constant Node_Id
:=
1018 Make_Assignment_Statement
(Sloc
(Stmt
),
1020 New_Occurrence_Of
(Param_Id
, Loc
),
1022 New_Occurrence_Of
(Ret_Obj
, Sloc
(Stmt
)));
1026 -- The extended return may just contain the declaration
1028 if Present
(Handled_Statement_Sequence
(Stmt
)) then
1029 Stmts
:= Statements
(Handled_Statement_Sequence
(Stmt
));
1034 Set_Assignment_OK
(Name
(Assign
));
1037 Make_Block_Statement
(Sloc
(Stmt
),
1039 Return_Object_Declarations
(Stmt
),
1040 Handled_Statement_Sequence
=>
1041 Make_Handled_Sequence_Of_Statements
(Loc
,
1042 Statements
=> Stmts
)));
1044 Replace_Returns
(Param_Id
, Stmts
);
1046 Append_To
(Stmts
, Assign
);
1047 Append_To
(Stmts
, Make_Simple_Return_Statement
(Loc
));
1050 elsif Nkind
(Stmt
) = N_If_Statement
then
1051 Replace_Returns
(Param_Id
, Then_Statements
(Stmt
));
1052 Replace_Returns
(Param_Id
, Else_Statements
(Stmt
));
1057 Part
:= First
(Elsif_Parts
(Stmt
));
1058 while Present
(Part
) loop
1059 Replace_Returns
(Param_Id
, Then_Statements
(Part
));
1064 elsif Nkind
(Stmt
) = N_Loop_Statement
then
1065 Replace_Returns
(Param_Id
, Statements
(Stmt
));
1067 elsif Nkind
(Stmt
) = N_Simple_Return_Statement
then
1074 Make_Assignment_Statement
(Sloc
(Stmt
),
1075 Name
=> New_Occurrence_Of
(Param_Id
, Loc
),
1076 Expression
=> Relocate_Node
(Expression
(Stmt
))));
1078 Insert_After
(Stmt
, Make_Simple_Return_Statement
(Loc
));
1080 -- Skip the added return
1087 end Replace_Returns
;
1094 -- Start of processing for Build_Procedure_Body_Form
1097 -- This routine replaces the original function body:
1099 -- function F (...) return Array_Typ is
1102 -- return Something;
1105 -- with the following:
1107 -- procedure P (..., Result : out Array_Typ) is
1110 -- Result := Something;
1114 Statements
(Handled_Statement_Sequence
(Func_Body
));
1115 Replace_Returns
(Last_Entity
(Proc_Id
), Stmts
);
1118 Make_Subprogram_Body
(Loc
,
1120 Copy_Subprogram_Spec
(Specification
(Proc_Decl
)),
1121 Declarations
=> Declarations
(Func_Body
),
1122 Handled_Statement_Sequence
=>
1123 Make_Handled_Sequence_Of_Statements
(Loc
,
1124 Statements
=> Stmts
));
1126 -- If the function is a generic instance, so is the new procedure.
1127 -- Set flag accordingly so that the proper renaming declarations are
1130 Set_Is_Generic_Instance
(Proc_Id
, Is_Generic_Instance
(Func_Id
));
1132 end Build_Procedure_Body_Form
;
1134 -----------------------
1135 -- Caller_Known_Size --
1136 -----------------------
1138 function Caller_Known_Size
1139 (Func_Call
: Node_Id
;
1140 Result_Subt
: Entity_Id
) return Boolean
1142 Utyp
: constant Entity_Id
:= Underlying_Type
(Result_Subt
);
1145 return not Needs_Secondary_Stack
(Utyp
)
1146 and then not (Is_Tagged_Type
(Utyp
)
1147 and then Present
(Controlling_Argument
(Func_Call
)));
1148 end Caller_Known_Size
;
1150 -----------------------
1151 -- Check_BIP_Actuals --
1152 -----------------------
1154 function Check_BIP_Actuals
1155 (Subp_Call
: Node_Id
;
1156 Subp_Id
: Entity_Id
) return Boolean
1162 pragma Assert
(Nkind
(Subp_Call
) in N_Entry_Call_Statement
1164 | N_Procedure_Call_Statement
);
1166 -- In CodePeer_Mode, the tree for `'Elab_Spec` procedures will be
1167 -- malformed because GNAT does not perform the usual expansion that
1168 -- results in the importation of external elaboration procedure symbols.
1169 -- This is expected: the CodePeer backend has special handling for this
1171 -- Thus, we do not need to check the tree (and in fact can't, because
1175 and then Nkind
(Name
(Subp_Call
)) = N_Attribute_Reference
1176 and then Attribute_Name
(Name
(Subp_Call
)) in Name_Elab_Spec
1178 | Name_Elab_Subp_Body
1183 Formal
:= First_Formal_With_Extras
(Subp_Id
);
1184 Actual
:= First_Actual
(Subp_Call
);
1186 while Present
(Formal
) and then Present
(Actual
) loop
1187 if Is_Build_In_Place_Entity
(Formal
)
1188 and then Nkind
(Actual
) = N_Identifier
1189 and then Is_Build_In_Place_Entity
(Entity
(Actual
))
1190 and then BIP_Suffix_Kind
(Formal
)
1191 /= BIP_Suffix_Kind
(Entity
(Actual
))
1196 Next_Formal_With_Extras
(Formal
);
1197 Next_Actual
(Actual
);
1200 return No
(Formal
) and then No
(Actual
);
1201 end Check_BIP_Actuals
;
1203 -----------------------------
1204 -- Check_Number_Of_Actuals --
1205 -----------------------------
1207 function Check_Number_Of_Actuals
1208 (Subp_Call
: Node_Id
;
1209 Subp_Id
: Entity_Id
) return Boolean
1215 pragma Assert
(Nkind
(Subp_Call
) in N_Entry_Call_Statement
1217 | N_Procedure_Call_Statement
);
1219 Formal
:= First_Formal_With_Extras
(Subp_Id
);
1220 Actual
:= First_Actual
(Subp_Call
);
1222 while Present
(Formal
) and then Present
(Actual
) loop
1223 Next_Formal_With_Extras
(Formal
);
1224 Next_Actual
(Actual
);
1227 return No
(Formal
) and then No
(Actual
);
1228 end Check_Number_Of_Actuals
;
1230 --------------------------------
1231 -- Check_Overriding_Operation --
1232 --------------------------------
1234 procedure Check_Overriding_Operation
(Subp
: Entity_Id
) is
1235 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
1236 Op_List
: constant Elist_Id
:= Primitive_Operations
(Typ
);
1238 Prim_Op
: Entity_Id
;
1242 if Is_Derived_Type
(Typ
)
1243 and then not Is_Private_Type
(Typ
)
1244 and then In_Open_Scopes
(Scope
(Etype
(Typ
)))
1245 and then Is_Base_Type
(Typ
)
1247 -- Subp overrides an inherited private operation if there is an
1248 -- inherited operation with a different name than Subp (see
1249 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
1250 -- same name as Subp.
1252 Op_Elmt
:= First_Elmt
(Op_List
);
1253 while Present
(Op_Elmt
) loop
1254 Prim_Op
:= Node
(Op_Elmt
);
1255 Par_Op
:= Alias
(Prim_Op
);
1258 and then not Comes_From_Source
(Prim_Op
)
1259 and then Chars
(Prim_Op
) /= Chars
(Par_Op
)
1260 and then Chars
(Par_Op
) = Chars
(Subp
)
1261 and then Is_Hidden
(Par_Op
)
1262 and then Type_Conformant
(Prim_Op
, Subp
)
1264 Set_DT_Position_Value
(Subp
, DT_Position
(Prim_Op
));
1267 Next_Elmt
(Op_Elmt
);
1270 end Check_Overriding_Operation
;
1272 -------------------------------
1273 -- Detect_Infinite_Recursion --
1274 -------------------------------
1276 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
) is
1277 Loc
: constant Source_Ptr
:= Sloc
(N
);
1279 Var_List
: constant Elist_Id
:= New_Elmt_List
;
1280 -- List of globals referenced by body of procedure
1282 Call_List
: constant Elist_Id
:= New_Elmt_List
;
1283 -- List of recursive calls in body of procedure
1285 Shad_List
: constant Elist_Id
:= New_Elmt_List
;
1286 -- List of entity id's for entities created to capture the value of
1287 -- referenced globals on entry to the procedure.
1289 Scop
: constant Uint
:= Scope_Depth
(Spec
);
1290 -- This is used to record the scope depth of the current procedure, so
1291 -- that we can identify global references.
1293 Max_Vars
: constant := 4;
1294 -- Do not test more than four global variables
1296 Count_Vars
: Natural := 0;
1297 -- Count variables found so far
1309 function Process
(Nod
: Node_Id
) return Traverse_Result
;
1310 -- Function to traverse the subprogram body (using Traverse_Func)
1316 function Process
(Nod
: Node_Id
) return Traverse_Result
is
1320 if Nkind
(Nod
) = N_Procedure_Call_Statement
then
1322 -- Case of one of the detected recursive calls
1324 if Is_Entity_Name
(Name
(Nod
))
1325 and then Has_Recursive_Call
(Entity
(Name
(Nod
)))
1326 and then Entity
(Name
(Nod
)) = Spec
1328 Append_Elmt
(Nod
, Call_List
);
1331 -- Any other procedure call may have side effects
1337 -- A call to a pure function can always be ignored
1339 elsif Nkind
(Nod
) = N_Function_Call
1340 and then Is_Entity_Name
(Name
(Nod
))
1341 and then Is_Pure
(Entity
(Name
(Nod
)))
1345 -- Case of an identifier reference
1347 elsif Nkind
(Nod
) = N_Identifier
then
1348 Ent
:= Entity
(Nod
);
1350 -- If no entity, then ignore the reference
1352 -- Not clear why this can happen. To investigate, remove this
1353 -- test and look at the crash that occurs here in 3401-004 ???
1358 -- Ignore entities with no Scope, again not clear how this
1359 -- can happen, to investigate, look at 4108-008 ???
1361 elsif No
(Scope
(Ent
)) then
1364 -- Ignore the reference if not to a more global object
1366 elsif Scope_Depth
(Scope
(Ent
)) >= Scop
then
1369 -- References to types, exceptions and constants are always OK
1372 or else Ekind
(Ent
) = E_Exception
1373 or else Ekind
(Ent
) = E_Constant
1377 -- If other than a non-volatile scalar variable, we have some
1378 -- kind of global reference (e.g. to a function) that we cannot
1379 -- deal with so we forget the attempt.
1381 elsif Ekind
(Ent
) /= E_Variable
1382 or else not Is_Scalar_Type
(Etype
(Ent
))
1383 or else Treat_As_Volatile
(Ent
)
1387 -- Otherwise we have a reference to a global scalar
1390 -- Loop through global entities already detected
1392 Elm
:= First_Elmt
(Var_List
);
1394 -- If not detected before, record this new global reference
1397 Count_Vars
:= Count_Vars
+ 1;
1399 if Count_Vars
<= Max_Vars
then
1400 Append_Elmt
(Entity
(Nod
), Var_List
);
1407 -- If recorded before, ignore
1409 elsif Node
(Elm
) = Entity
(Nod
) then
1412 -- Otherwise keep looking
1422 -- For all other node kinds, recursively visit syntactic children
1429 function Traverse_Body
is new Traverse_Func
(Process
);
1431 -- Start of processing for Detect_Infinite_Recursion
1434 -- Do not attempt detection in No_Implicit_Conditional mode, since we
1435 -- won't be able to generate the code to handle the recursion in any
1438 if Restriction_Active
(No_Implicit_Conditionals
) then
1442 -- Otherwise do traversal and quit if we get abandon signal
1444 if Traverse_Body
(N
) = Abandon
then
1447 -- We must have a call, since Has_Recursive_Call was set. If not just
1448 -- ignore (this is only an error check, so if we have a funny situation,
1449 -- due to bugs or errors, we do not want to bomb).
1451 elsif Is_Empty_Elmt_List
(Call_List
) then
1455 -- Here is the case where we detect recursion at compile time
1457 -- Push our current scope for analyzing the declarations and code that
1458 -- we will insert for the checking.
1462 -- This loop builds temporary variables for each of the referenced
1463 -- globals, so that at the end of the loop the list Shad_List contains
1464 -- these temporaries in one-to-one correspondence with the elements in
1468 Elm
:= First_Elmt
(Var_List
);
1469 while Present
(Elm
) loop
1471 Ent
:= Make_Temporary
(Loc
, 'S');
1472 Append_Elmt
(Ent
, Shad_List
);
1474 -- Insert a declaration for this temporary at the start of the
1475 -- declarations for the procedure. The temporaries are declared as
1476 -- constant objects initialized to the current values of the
1477 -- corresponding temporaries.
1480 Make_Object_Declaration
(Loc
,
1481 Defining_Identifier
=> Ent
,
1482 Object_Definition
=> New_Occurrence_Of
(Etype
(Var
), Loc
),
1483 Constant_Present
=> True,
1484 Expression
=> New_Occurrence_Of
(Var
, Loc
));
1487 Prepend
(Decl
, Declarations
(N
));
1489 Insert_After
(Last
, Decl
);
1497 -- Loop through calls
1499 Call
:= First_Elmt
(Call_List
);
1500 while Present
(Call
) loop
1502 -- Build a predicate expression of the form
1505 -- and then global1 = temp1
1506 -- and then global2 = temp2
1509 -- This predicate determines if any of the global values
1510 -- referenced by the procedure have changed since the
1511 -- current call, if not an infinite recursion is assured.
1513 Test
:= New_Occurrence_Of
(Standard_True
, Loc
);
1515 Elm1
:= First_Elmt
(Var_List
);
1516 Elm2
:= First_Elmt
(Shad_List
);
1517 while Present
(Elm1
) loop
1523 Left_Opnd
=> New_Occurrence_Of
(Node
(Elm1
), Loc
),
1524 Right_Opnd
=> New_Occurrence_Of
(Node
(Elm2
), Loc
)));
1530 -- Now we replace the call with the sequence
1532 -- if no-changes (see above) then
1533 -- raise Storage_Error;
1538 Rewrite
(Node
(Call
),
1539 Make_If_Statement
(Loc
,
1541 Then_Statements
=> New_List
(
1542 Make_Raise_Storage_Error
(Loc
,
1543 Reason
=> SE_Infinite_Recursion
)),
1545 Else_Statements
=> New_List
(
1546 Relocate_Node
(Node
(Call
)))));
1548 Analyze
(Node
(Call
));
1553 -- Remove temporary scope stack entry used for analysis
1556 end Detect_Infinite_Recursion
;
1558 --------------------
1559 -- Expand_Actuals --
1560 --------------------
1562 procedure Expand_Actuals
1565 Post_Call
: out List_Id
)
1567 Loc
: constant Source_Ptr
:= Sloc
(N
);
1571 E_Actual
: Entity_Id
;
1572 E_Formal
: Entity_Id
;
1574 procedure Add_Call_By_Copy_Code
;
1575 -- For cases where the parameter must be passed by copy, this routine
1576 -- generates a temporary variable into which the actual is copied and
1577 -- then passes this as the parameter. For an OUT or IN OUT parameter,
1578 -- an assignment is also generated to copy the result back. The call
1579 -- also takes care of any constraint checks required for the type
1580 -- conversion case (on both the way in and the way out).
1582 procedure Add_Simple_Call_By_Copy_Code
(Force
: Boolean);
1583 -- This is similar to the above, but is used in cases where we know
1584 -- that all that is needed is to simply create a temporary and copy
1585 -- the value in and out of the temporary. If Force is True, then the
1586 -- procedure may disregard legality considerations.
1588 -- ??? We need to do the copy for a bit-packed array because this is
1589 -- where the rewriting into a mask-and-shift sequence is done. But of
1590 -- course this may break the program if it expects bits to be really
1591 -- passed by reference. That's what we have done historically though.
1593 procedure Add_Validation_Call_By_Copy_Code
(Act
: Node_Id
);
1594 -- Perform copy-back for actual parameter Act which denotes a validation
1597 procedure Check_Fortran_Logical
;
1598 -- A value of type Logical that is passed through a formal parameter
1599 -- must be normalized because .TRUE. usually does not have the same
1600 -- representation as True. We assume that .FALSE. = False = 0.
1601 -- What about functions that return a logical type ???
1603 function Is_Legal_Copy
return Boolean;
1604 -- Check that an actual can be copied before generating the temporary
1605 -- to be used in the call. If the formal is of a by_reference type or
1606 -- is aliased, then the program is illegal (this can only happen in
1607 -- the presence of representation clauses that force a misalignment)
1608 -- If the formal is a by_reference parameter imposed by a DEC pragma,
1609 -- emit a warning that this might lead to unaligned arguments.
1611 function Make_Var
(Actual
: Node_Id
) return Entity_Id
;
1612 -- Returns an entity that refers to the given actual parameter, Actual
1613 -- (not including any type conversion). If Actual is an entity name,
1614 -- then this entity is returned unchanged, otherwise a renaming is
1615 -- created to provide an entity for the actual.
1617 procedure Reset_Packed_Prefix
;
1618 -- The expansion of a packed array component reference is delayed in
1619 -- the context of a call. Now we need to complete the expansion, so we
1620 -- unmark the analyzed bits in all prefixes.
1622 function Requires_Atomic_Or_Volatile_Copy
return Boolean;
1623 -- Returns whether a copy is required as per RM C.6(19) and gives a
1624 -- warning in this case.
1626 ---------------------------
1627 -- Add_Call_By_Copy_Code --
1628 ---------------------------
1630 procedure Add_Call_By_Copy_Code
is
1633 F_Typ
: Entity_Id
:= Etype
(Formal
);
1641 if not Is_Legal_Copy
then
1645 Temp
:= Make_Temporary
(Loc
, 'T', Actual
);
1647 -- Handle formals whose type comes from the limited view
1649 if From_Limited_With
(F_Typ
)
1650 and then Has_Non_Limited_View
(F_Typ
)
1652 F_Typ
:= Non_Limited_View
(F_Typ
);
1655 -- Use formal type for temp, unless formal type is an unconstrained
1656 -- array, in which case we don't have to worry about bounds checks,
1657 -- and we use the actual type, since that has appropriate bounds.
1659 if Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
1660 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1662 Indic
:= New_Occurrence_Of
(F_Typ
, Loc
);
1665 -- The new code will be properly analyzed below and the setting of
1666 -- the Do_Range_Check flag recomputed so remove the obsolete one.
1668 Set_Do_Range_Check
(Actual
, False);
1670 if Nkind
(Actual
) = N_Type_Conversion
then
1671 Set_Do_Range_Check
(Expression
(Actual
), False);
1673 V_Typ
:= Etype
(Expression
(Actual
));
1675 -- If the formal is an (in-)out parameter, capture the name
1676 -- of the variable in order to build the post-call assignment.
1678 Var
:= Make_Var
(Expression
(Actual
));
1680 Crep
:= not Has_Compatible_Representation
1681 (Target_Typ
=> F_Typ
,
1682 Operand_Typ
=> Etype
(Expression
(Actual
)));
1685 V_Typ
:= Etype
(Actual
);
1686 Var
:= Make_Var
(Actual
);
1690 -- If the actual denotes a variable which captures the value of an
1691 -- object for validation purposes, we propagate the link with this
1692 -- object to the new variable made from the actual just above.
1694 if Ekind
(Formal
) /= E_In_Parameter
1695 and then Is_Validation_Variable_Reference
(Actual
)
1698 Ref
: constant Node_Id
:= Unqual_Conv
(Actual
);
1701 if Is_Entity_Name
(Ref
) then
1702 Set_Validated_Object
(Var
, Validated_Object
(Entity
(Ref
)));
1705 pragma Assert
(False);
1711 -- Setup initialization for case of in out parameter, or an out
1712 -- parameter where the formal is an unconstrained array (in the
1713 -- latter case, we have to pass in an object with bounds).
1715 -- If this is an out parameter, the initial copy is wasteful, so as
1716 -- an optimization for the one-dimensional case we extract the
1717 -- bounds of the actual and build an uninitialized temporary of the
1720 -- If the formal is an out parameter with discriminants, the
1721 -- discriminants must be captured even if the rest of the object
1722 -- is in principle uninitialized, because the discriminants may
1723 -- be read by the called subprogram.
1725 if Ekind
(Formal
) = E_In_Out_Parameter
1726 or else (Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
))
1727 or else Has_Discriminants
(F_Typ
)
1729 if Nkind
(Actual
) = N_Type_Conversion
then
1730 if Conversion_OK
(Actual
) then
1731 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1733 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1736 elsif Ekind
(Formal
) = E_Out_Parameter
1737 and then Is_Array_Type
(F_Typ
)
1738 and then Number_Dimensions
(F_Typ
) = 1
1739 and then not Has_Non_Null_Base_Init_Proc
(F_Typ
)
1741 -- Actual is a one-dimensional array or slice, and the type
1742 -- requires no initialization. Create a temporary of the
1743 -- right size, but do not copy actual into it (optimization).
1747 Make_Subtype_Indication
(Loc
,
1748 Subtype_Mark
=> New_Occurrence_Of
(F_Typ
, Loc
),
1750 Make_Index_Or_Discriminant_Constraint
(Loc
,
1751 Constraints
=> New_List
(
1754 Make_Attribute_Reference
(Loc
,
1755 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
1756 Attribute_Name
=> Name_First
),
1758 Make_Attribute_Reference
(Loc
,
1759 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
1760 Attribute_Name
=> Name_Last
)))));
1763 Init
:= New_Occurrence_Of
(Var
, Loc
);
1766 -- An initialization is created for packed conversions as
1767 -- actuals for out parameters to enable Make_Object_Declaration
1768 -- to determine the proper subtype for N_Node. Note that this
1769 -- is wasteful because the extra copying on the call side is
1770 -- not required for such out parameters. ???
1772 elsif Ekind
(Formal
) = E_Out_Parameter
1773 and then Nkind
(Actual
) = N_Type_Conversion
1774 and then (Is_Bit_Packed_Array
(F_Typ
)
1776 Is_Bit_Packed_Array
(Etype
(Expression
(Actual
))))
1778 if Conversion_OK
(Actual
) then
1779 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1781 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1784 elsif Ekind
(Formal
) = E_In_Parameter
then
1786 -- Handle the case in which the actual is a type conversion
1788 if Nkind
(Actual
) = N_Type_Conversion
then
1789 if Conversion_OK
(Actual
) then
1790 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1792 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1795 Init
:= New_Occurrence_Of
(Var
, Loc
);
1798 -- Access types are passed in without checks, but if a copy-back is
1799 -- required for a null-excluding check on an in-out or out parameter,
1800 -- then the initial value is that of the actual.
1802 elsif Is_Access_Type
(E_Formal
)
1803 and then Can_Never_Be_Null
(Etype
(Actual
))
1804 and then not Can_Never_Be_Null
(E_Formal
)
1806 Init
:= New_Occurrence_Of
(Var
, Loc
);
1808 -- View conversions when the formal type has the Default_Value aspect
1809 -- require passing in the value of the conversion's operand. The type
1810 -- of that operand also has Default_Value, as required by AI12-0074
1811 -- (RM 6.4.1(5.3/4)). The subtype denoted by the subtype_indication
1812 -- is changed to the base type of the formal subtype, to ensure that
1813 -- the actual's value can be assigned without a constraint check
1814 -- (note that no check is done on passing to an out parameter). Also
1815 -- note that the two types necessarily share the same ancestor type,
1816 -- as required by 6.4.1(5.2/4), so underlying base types will match.
1818 elsif Ekind
(Formal
) = E_Out_Parameter
1819 and then Is_Scalar_Type
(Etype
(F_Typ
))
1820 and then Nkind
(Actual
) = N_Type_Conversion
1821 and then Present
(Default_Aspect_Value
(Etype
(F_Typ
)))
1823 Indic
:= New_Occurrence_Of
(Base_Type
(F_Typ
), Loc
);
1825 (Base_Type
(F_Typ
), New_Occurrence_Of
(Var
, Loc
));
1832 Make_Object_Declaration
(Loc
,
1833 Defining_Identifier
=> Temp
,
1834 Object_Definition
=> Indic
,
1835 Expression
=> Init
);
1836 Set_Assignment_OK
(N_Node
);
1837 Insert_Action
(N
, N_Node
);
1839 -- Now, normally the deal here is that we use the defining
1840 -- identifier created by that object declaration. There is
1841 -- one exception to this. In the change of representation case
1842 -- the above declaration will end up looking like:
1844 -- temp : type := identifier;
1846 -- And in this case we might as well use the identifier directly
1847 -- and eliminate the temporary. Note that the analysis of the
1848 -- declaration was not a waste of time in that case, since it is
1849 -- what generated the necessary change of representation code. If
1850 -- the change of representation introduced additional code, as in
1851 -- a fixed-integer conversion, the expression is not an identifier
1852 -- and must be kept.
1855 and then Present
(Expression
(N_Node
))
1856 and then Is_Entity_Name
(Expression
(N_Node
))
1858 Temp
:= Entity
(Expression
(N_Node
));
1859 Rewrite
(N_Node
, Make_Null_Statement
(Loc
));
1862 -- For IN parameter, all we do is to replace the actual
1864 if Ekind
(Formal
) = E_In_Parameter
then
1865 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
1868 -- Processing for OUT or IN OUT parameter
1871 -- Kill current value indications for the temporary variable we
1872 -- created, since we just passed it as an OUT parameter.
1874 Kill_Current_Values
(Temp
);
1875 Set_Is_Known_Valid
(Temp
, False);
1876 Set_Is_True_Constant
(Temp
, False);
1878 -- If type conversion, use reverse conversion on exit
1880 if Nkind
(Actual
) = N_Type_Conversion
then
1881 if Conversion_OK
(Actual
) then
1882 Expr
:= OK_Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
1884 Expr
:= Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
1887 Expr
:= New_Occurrence_Of
(Temp
, Loc
);
1890 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Sloc
(Actual
)));
1893 -- If the actual is a conversion of a packed reference, it may
1894 -- already have been expanded by Remove_Side_Effects, and the
1895 -- resulting variable is a temporary which does not designate
1896 -- the proper out-parameter, which may not be addressable. In
1897 -- that case, generate an assignment to the original expression
1898 -- (before expansion of the packed reference) so that the proper
1899 -- expansion of assignment to a packed component can take place.
1906 if Is_Renaming_Of_Object
(Var
)
1907 and then Nkind
(Renamed_Object
(Var
)) = N_Selected_Component
1908 and then Nkind
(Original_Node
(Prefix
(Renamed_Object
(Var
))))
1909 = N_Indexed_Component
1911 Has_Non_Standard_Rep
(Etype
(Prefix
(Renamed_Object
(Var
))))
1913 Obj
:= Renamed_Object
(Var
);
1915 Make_Selected_Component
(Loc
,
1917 New_Copy_Tree
(Original_Node
(Prefix
(Obj
))),
1918 Selector_Name
=> New_Copy
(Selector_Name
(Obj
)));
1919 Reset_Analyzed_Flags
(Lhs
);
1922 Lhs
:= New_Occurrence_Of
(Var
, Loc
);
1925 Set_Assignment_OK
(Lhs
);
1927 if Is_Access_Type
(E_Formal
)
1928 and then Is_Entity_Name
(Lhs
)
1930 Present
(Effective_Extra_Accessibility
(Entity
(Lhs
)))
1931 and then not No_Dynamic_Accessibility_Checks_Enabled
(Lhs
)
1933 -- Copyback target is an Ada 2012 stand-alone object of an
1934 -- anonymous access type.
1936 pragma Assert
(Ada_Version
>= Ada_2012
);
1938 Apply_Accessibility_Check
(Lhs
, E_Formal
, N
);
1940 Append_To
(Post_Call
,
1941 Make_Assignment_Statement
(Loc
,
1943 Expression
=> Expr
));
1945 -- We would like to somehow suppress generation of the
1946 -- extra_accessibility assignment generated by the expansion
1947 -- of the above assignment statement. It's not a correctness
1948 -- issue because the following assignment renders it dead,
1949 -- but generating back-to-back assignments to the same
1950 -- target is undesirable. ???
1952 Append_To
(Post_Call
,
1953 Make_Assignment_Statement
(Loc
,
1954 Name
=> New_Occurrence_Of
(
1955 Effective_Extra_Accessibility
(Entity
(Lhs
)), Loc
),
1956 Expression
=> Make_Integer_Literal
(Loc
,
1957 Type_Access_Level
(E_Formal
))));
1960 if Is_Access_Type
(E_Formal
)
1961 and then Can_Never_Be_Null
(Etype
(Actual
))
1962 and then not Can_Never_Be_Null
(E_Formal
)
1964 Append_To
(Post_Call
,
1965 Make_Raise_Constraint_Error
(Loc
,
1968 Left_Opnd
=> New_Occurrence_Of
(Temp
, Loc
),
1969 Right_Opnd
=> Make_Null
(Loc
)),
1970 Reason
=> CE_Access_Check_Failed
));
1973 Append_To
(Post_Call
,
1974 Make_Assignment_Statement
(Loc
,
1976 Expression
=> Expr
));
1979 -- Add a copy-back to reflect any potential changes in value
1980 -- back into the original object, if any.
1982 if Is_Validation_Variable_Reference
(Lhs
) then
1983 Add_Validation_Call_By_Copy_Code
(Lhs
);
1987 end Add_Call_By_Copy_Code
;
1989 ----------------------------------
1990 -- Add_Simple_Call_By_Copy_Code --
1991 ----------------------------------
1993 procedure Add_Simple_Call_By_Copy_Code
(Force
: Boolean) is
1994 With_Storage_Model
: constant Boolean :=
1995 Nkind
(Actual
) = N_Explicit_Dereference
1997 Has_Designated_Storage_Model_Aspect
(Etype
(Prefix
(Actual
)));
2010 -- Unless forced not to, check the legality of the copy operation
2012 if not Force
and then not Is_Legal_Copy
then
2016 F_Typ
:= Etype
(Formal
);
2018 -- Handle formals whose type comes from the limited view
2020 if From_Limited_With
(F_Typ
)
2021 and then Has_Non_Limited_View
(F_Typ
)
2023 F_Typ
:= Non_Limited_View
(F_Typ
);
2026 -- Use formal type for temp, unless formal type is an unconstrained
2027 -- composite, in which case we don't have to worry about checks and
2028 -- we can use the actual type, since that has appropriate bounds.
2030 if Is_Composite_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
2031 Indic
:= New_Occurrence_Of
(Get_Actual_Subtype
(Actual
), Loc
);
2033 Indic
:= New_Occurrence_Of
(F_Typ
, Loc
);
2036 -- Prepare to generate code
2038 Reset_Packed_Prefix
;
2040 Incod
:= Relocate_Node
(Actual
);
2041 Outcod
:= New_Copy_Tree
(Incod
);
2043 -- Generate declaration of temporary variable, initializing it
2044 -- with the input parameter unless we have an OUT formal or
2045 -- this is an initialization call.
2047 if Ekind
(Formal
) = E_Out_Parameter
then
2050 elsif Inside_Init_Proc
then
2052 -- Skip using the actual as the expression in Decl if we are in
2053 -- an init proc and it is not a component which depends on a
2054 -- discriminant, because, in this case, we need to use the actual
2055 -- type of the component instead.
2057 if Nkind
(Actual
) /= N_Selected_Component
2059 not Has_Discriminant_Dependent_Constraint
2060 (Entity
(Selector_Name
(Actual
)))
2064 -- Otherwise, keep the component so we can generate the proper
2065 -- actual subtype - since the subtype depends on enclosing
2075 if With_Storage_Model
then
2077 Build_Temporary_On_Secondary_Stack
(Loc
, Entity
(Indic
), Cpcod
);
2079 if Present
(Incod
) then
2081 Make_Assignment_Statement
(Loc
,
2083 Make_Explicit_Dereference
(Loc
,
2084 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)),
2085 Expression
=> Incod
));
2086 Set_Suppress_Assignment_Checks
(Last
(Cpcod
));
2090 Temp
:= Make_Temporary
(Loc
, 'T', Actual
);
2093 Make_Object_Declaration
(Loc
,
2094 Defining_Identifier
=> Temp
,
2095 Object_Definition
=> Indic
,
2096 Expression
=> Incod
);
2098 -- If the call is to initialize a component of a composite type,
2099 -- and the component does not depend on discriminants, use the
2100 -- actual type of the component. This is required in case the
2101 -- component is constrained, because in general the formal of the
2102 -- initialization procedure will be unconstrained. Note that if
2103 -- the component being initialized is constrained by an enclosing
2104 -- discriminant, the presence of the initialization in the
2105 -- declaration will generate an expression for the actual subtype.
2107 if Inside_Init_Proc
and then No
(Incod
) then
2108 Set_No_Initialization
(Decl
);
2109 Set_Object_Definition
(Decl
,
2110 New_Occurrence_Of
(Etype
(Actual
), Loc
));
2113 Append_To
(Cpcod
, Decl
);
2116 Insert_Actions
(N
, Cpcod
);
2118 -- The actual is simply a reference to the temporary
2120 if With_Storage_Model
then
2122 Make_Explicit_Dereference
(Loc
,
2123 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
2125 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
2130 -- Generate copy out if OUT or IN OUT parameter
2132 if Ekind
(Formal
) /= E_In_Parameter
then
2135 if With_Storage_Model
then
2137 Make_Explicit_Dereference
(Loc
,
2138 Prefix
=> New_Occurrence_Of
(Temp
, Loc
));
2140 Rhs
:= New_Occurrence_Of
(Temp
, Loc
);
2141 Set_Is_True_Constant
(Temp
, False);
2144 -- Deal with conversion
2146 if Nkind
(Lhs
) = N_Type_Conversion
then
2147 Lhs
:= Expression
(Lhs
);
2148 Rhs
:= Convert_To
(Etype
(Actual
), Rhs
);
2151 Append_To
(Post_Call
,
2152 Make_Assignment_Statement
(Loc
,
2154 Expression
=> Rhs
));
2155 Set_Suppress_Assignment_Checks
(Last
(Post_Call
));
2156 Set_Assignment_OK
(Name
(Last
(Post_Call
)));
2158 end Add_Simple_Call_By_Copy_Code
;
2160 --------------------------------------
2161 -- Add_Validation_Call_By_Copy_Code --
2162 --------------------------------------
2164 procedure Add_Validation_Call_By_Copy_Code
(Act
: Node_Id
) is
2165 Var
: constant Node_Id
:= Unqual_Conv
(Act
);
2169 Obj_Typ
: Entity_Id
;
2173 -- Generate range check if required
2175 if Do_Range_Check
(Actual
) then
2176 Generate_Range_Check
(Actual
, E_Formal
, CE_Range_Check_Failed
);
2179 -- If there is a type conversion in the actual, it will be reinstated
2180 -- below, the new instance will be properly analyzed and the setting
2181 -- of the Do_Range_Check flag recomputed so remove the obsolete one.
2183 if Nkind
(Actual
) = N_Type_Conversion
then
2184 Set_Do_Range_Check
(Expression
(Actual
), False);
2187 -- Copy the value of the validation variable back into the object
2190 if Is_Entity_Name
(Var
) then
2191 Var_Id
:= Entity
(Var
);
2192 Obj
:= Validated_Object
(Var_Id
);
2193 Obj_Typ
:= Etype
(Obj
);
2195 Expr
:= New_Occurrence_Of
(Var_Id
, Loc
);
2197 -- A type conversion is needed when the validation variable and
2198 -- the validated object carry different types. This case occurs
2199 -- when the actual is qualified in some fashion.
2202 -- subtype Int is Integer range ...;
2203 -- procedure Call (Val : in out Integer);
2207 -- Call (Integer (Object));
2211 -- Var : Integer := Object; -- conversion to base type
2212 -- if not Var'Valid then -- validity check
2213 -- Call (Var); -- modify Var
2214 -- Object := Int (Var); -- conversion to subtype
2216 if Etype
(Var_Id
) /= Obj_Typ
then
2218 Make_Type_Conversion
(Loc
,
2219 Subtype_Mark
=> New_Occurrence_Of
(Obj_Typ
, Loc
),
2220 Expression
=> Expr
);
2226 -- Object := Object_Type (Var);
2228 Append_To
(Post_Call
,
2229 Make_Assignment_Statement
(Loc
,
2231 Expression
=> Expr
));
2233 -- If the flow reaches this point, then this routine was invoked with
2234 -- an actual which does not denote a validation variable.
2237 pragma Assert
(False);
2240 end Add_Validation_Call_By_Copy_Code
;
2242 ---------------------------
2243 -- Check_Fortran_Logical --
2244 ---------------------------
2246 procedure Check_Fortran_Logical
is
2247 Logical
: constant Entity_Id
:= Etype
(Formal
);
2250 -- Note: this is very incomplete, e.g. it does not handle arrays
2251 -- of logical values. This is really not the right approach at all???)
2254 if Convention
(Subp
) = Convention_Fortran
2255 and then Root_Type
(Etype
(Formal
)) = Standard_Boolean
2256 and then Ekind
(Formal
) /= E_In_Parameter
2258 Var
:= Make_Var
(Actual
);
2259 Append_To
(Post_Call
,
2260 Make_Assignment_Statement
(Loc
,
2261 Name
=> New_Occurrence_Of
(Var
, Loc
),
2263 Unchecked_Convert_To
(
2266 Left_Opnd
=> New_Occurrence_Of
(Var
, Loc
),
2268 Unchecked_Convert_To
(
2270 New_Occurrence_Of
(Standard_False
, Loc
))))));
2272 end Check_Fortran_Logical
;
2278 function Is_Legal_Copy
return Boolean is
2280 -- An attempt to copy a value of such a type can only occur if
2281 -- representation clauses give the actual a misaligned address.
2283 if Is_By_Reference_Type
(Etype
(Formal
))
2284 or else Is_Aliased
(Formal
)
2285 or else (Mechanism
(Formal
) = By_Reference
2286 and then not Has_Foreign_Convention
(Subp
))
2289 -- The actual may in fact be properly aligned but there is not
2290 -- enough front-end information to determine this. In that case
2291 -- gigi will emit an error or a warning if a copy is not legal,
2292 -- or generate the proper code.
2296 -- For users of Starlet, we assume that the specification of by-
2297 -- reference mechanism is mandatory. This may lead to unaligned
2298 -- objects but at least for DEC legacy code it is known to work.
2299 -- The warning will alert users of this code that a problem may
2302 elsif Mechanism
(Formal
) = By_Reference
2303 and then Ekind
(Scope
(Formal
)) = E_Procedure
2304 and then Is_Valued_Procedure
(Scope
(Formal
))
2307 ("by_reference actual may be misaligned??", Actual
);
2319 function Make_Var
(Actual
: Node_Id
) return Entity_Id
is
2323 if Is_Entity_Name
(Actual
) then
2324 return Entity
(Actual
);
2327 Var
:= Make_Temporary
(Loc
, 'T', Actual
);
2330 Make_Object_Renaming_Declaration
(Loc
,
2331 Defining_Identifier
=> Var
,
2333 New_Occurrence_Of
(Etype
(Actual
), Loc
),
2334 Name
=> Relocate_Node
(Actual
));
2336 Insert_Action
(N
, N_Node
);
2341 -------------------------
2342 -- Reset_Packed_Prefix --
2343 -------------------------
2345 procedure Reset_Packed_Prefix
is
2346 Pfx
: Node_Id
:= Actual
;
2349 Set_Analyzed
(Pfx
, False);
2351 Nkind
(Pfx
) not in N_Selected_Component | N_Indexed_Component
;
2352 Pfx
:= Prefix
(Pfx
);
2354 end Reset_Packed_Prefix
;
2356 ----------------------------------------
2357 -- Requires_Atomic_Or_Volatile_Copy --
2358 ----------------------------------------
2360 function Requires_Atomic_Or_Volatile_Copy
return Boolean is
2362 -- If the formal is already passed by copy, no need to do anything
2364 if Is_By_Copy_Type
(E_Formal
) then
2368 -- There is no requirement inside initialization procedures and this
2369 -- would generate copies for atomic or volatile composite components.
2371 if Inside_Init_Proc
then
2375 -- Check for atomicity mismatch
2377 if Is_Atomic_Object
(Actual
) and then not Is_Atomic
(E_Formal
)
2379 if Comes_From_Source
(N
) then
2381 ("??atomic actual passed by copy (RM C.6(19))", Actual
);
2386 -- Check for volatility mismatch
2388 if Is_Volatile_Object_Ref
(Actual
) and then not Is_Volatile
(E_Formal
)
2390 if Comes_From_Source
(N
) then
2392 ("??volatile actual passed by copy (RM C.6(19))", Actual
);
2398 end Requires_Atomic_Or_Volatile_Copy
;
2400 -- Start of processing for Expand_Actuals
2403 Post_Call
:= New_List
;
2405 Formal
:= First_Formal
(Subp
);
2406 Actual
:= First_Actual
(N
);
2407 while Present
(Formal
) loop
2408 E_Formal
:= Etype
(Formal
);
2409 E_Actual
:= Etype
(Actual
);
2411 -- Handle formals whose type comes from the limited view
2413 if From_Limited_With
(E_Formal
)
2414 and then Has_Non_Limited_View
(E_Formal
)
2416 E_Formal
:= Non_Limited_View
(E_Formal
);
2419 if Is_Scalar_Type
(E_Formal
)
2420 or else Nkind
(Actual
) = N_Slice
2422 Check_Fortran_Logical
;
2426 elsif Ekind
(Formal
) /= E_Out_Parameter
then
2428 -- The unusual case of the current instance of a protected type
2429 -- requires special handling. This can only occur in the context
2430 -- of a call within the body of a protected operation.
2432 if Is_Entity_Name
(Actual
)
2433 and then Ekind
(Entity
(Actual
)) = E_Protected_Type
2434 and then In_Open_Scopes
(Entity
(Actual
))
2436 if Scope
(Subp
) /= Entity
(Actual
) then
2438 ("operation outside protected type may not "
2439 & "call back its protected operations??", Actual
);
2443 Expand_Protected_Object_Reference
(N
, Entity
(Actual
)));
2446 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
2447 -- build-in-place function, then a temporary return object needs
2448 -- to be created and access to it must be passed to the function
2449 -- (and ensure that we have an activation chain defined for tasks
2450 -- and a Master variable).
2452 -- Currently we limit such functions to those with inherently
2453 -- limited result subtypes, but eventually we plan to expand the
2454 -- functions that are treated as build-in-place to include other
2455 -- composite result types.
2457 -- But do not do it here for intrinsic subprograms since this will
2458 -- be done properly after the subprogram is expanded.
2460 if Is_Intrinsic_Subprogram
(Subp
) then
2463 elsif Is_Build_In_Place_Function_Call
(Actual
) then
2464 if Might_Have_Tasks
(Etype
(Actual
)) then
2465 Build_Activation_Chain_Entity
(N
);
2466 Build_Master_Entity
(Etype
(Actual
));
2469 Make_Build_In_Place_Call_In_Anonymous_Context
(Actual
);
2471 -- Ada 2005 (AI-318-02): Specialization of the previous case for
2472 -- actuals containing build-in-place function calls whose returned
2473 -- object covers interface types.
2475 elsif Present
(Unqual_BIP_Iface_Function_Call
(Actual
)) then
2476 Build_Activation_Chain_Entity
(N
);
2477 Build_Master_Entity
(Etype
(Actual
));
2478 Make_Build_In_Place_Iface_Call_In_Anonymous_Context
(Actual
);
2481 Apply_Constraint_Check
(Actual
, E_Formal
);
2483 -- Out parameter case. No constraint checks on access type
2484 -- RM 6.4.1 (13), but on return a null-excluding check may be
2485 -- required (see below).
2487 elsif Is_Access_Type
(E_Formal
) then
2492 elsif Has_Discriminants
(Base_Type
(E_Formal
))
2493 or else Has_Non_Null_Base_Init_Proc
(E_Formal
)
2495 Apply_Constraint_Check
(Actual
, E_Formal
);
2500 Apply_Constraint_Check
(Actual
, Base_Type
(E_Formal
));
2503 -- Processing for IN-OUT and OUT parameters
2505 if Ekind
(Formal
) /= E_In_Parameter
then
2507 -- For type conversions of arrays, apply length/range checks
2509 if Is_Array_Type
(E_Formal
)
2510 and then Nkind
(Actual
) = N_Type_Conversion
2512 if Is_Constrained
(E_Formal
) then
2513 Apply_Length_Check
(Expression
(Actual
), E_Formal
);
2515 Apply_Range_Check
(Expression
(Actual
), E_Formal
);
2519 -- If argument is a type conversion for a type that is passed by
2520 -- copy, then we must pass the parameter by copy.
2522 if Nkind
(Actual
) = N_Type_Conversion
2524 (Is_Elementary_Type
(E_Formal
)
2525 or else Is_Bit_Packed_Array
(Etype
(Formal
))
2526 or else Is_Bit_Packed_Array
(Etype
(Expression
(Actual
)))
2528 -- Also pass by copy if change of representation
2530 or else not Has_Compatible_Representation
2531 (Target_Typ
=> Etype
(Formal
),
2532 Operand_Typ
=> Etype
(Expression
(Actual
))))
2534 Add_Call_By_Copy_Code
;
2536 -- References to components of bit-packed arrays are expanded
2537 -- at this point, rather than at the point of analysis of the
2538 -- actuals, to handle the expansion of the assignment to
2539 -- [in] out parameters.
2541 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
2542 Add_Simple_Call_By_Copy_Code
(Force
=> True);
2544 -- If the actual has a nonnative storage model, we need a copy
2546 elsif Nkind
(Actual
) = N_Explicit_Dereference
2548 Has_Designated_Storage_Model_Aspect
(Etype
(Prefix
(Actual
)))
2550 (Present
(Storage_Model_Copy_To
2551 (Storage_Model_Object
(Etype
(Prefix
(Actual
)))))
2553 (Ekind
(Formal
) = E_In_Out_Parameter
2555 Present
(Storage_Model_Copy_From
2556 (Storage_Model_Object
(Etype
(Prefix
(Actual
)))))))
2558 Add_Simple_Call_By_Copy_Code
(Force
=> True);
2560 -- If a nonscalar actual is possibly bit-aligned, we need a copy
2561 -- because the back-end cannot cope with such objects. In other
2562 -- cases where alignment forces a copy, the back-end generates
2563 -- it properly. It should not be generated unconditionally in the
2564 -- front-end because it does not know precisely the alignment
2565 -- requirements of the target, and makes too conservative an
2566 -- estimate, leading to superfluous copies or spurious errors
2567 -- on by-reference parameters.
2569 elsif Nkind
(Actual
) = N_Selected_Component
2571 Component_May_Be_Bit_Aligned
(Entity
(Selector_Name
(Actual
)))
2572 and then not Represented_As_Scalar
(Etype
(Formal
))
2574 Add_Simple_Call_By_Copy_Code
(Force
=> False);
2576 -- References to slices of bit-packed arrays are expanded
2578 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
2579 Add_Call_By_Copy_Code
;
2581 -- References to possibly unaligned slices of arrays are expanded
2583 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
2584 Add_Call_By_Copy_Code
;
2586 -- Deal with access types where the actual subtype and the
2587 -- formal subtype are not the same, requiring a check.
2589 -- It is necessary to exclude tagged types because of "downward
2590 -- conversion" errors, but null-excluding checks on return may be
2593 elsif Is_Access_Type
(E_Formal
)
2594 and then not Is_Tagged_Type
(Designated_Type
(E_Formal
))
2595 and then (not Same_Type
(E_Formal
, E_Actual
)
2596 or else (Can_Never_Be_Null
(E_Actual
)
2597 and then not Can_Never_Be_Null
(E_Formal
)))
2599 Add_Call_By_Copy_Code
;
2601 -- We may need to force a copy because of atomicity or volatility
2604 elsif Requires_Atomic_Or_Volatile_Copy
then
2605 Add_Call_By_Copy_Code
;
2607 -- Add call-by-copy code for the case of scalar out parameters
2608 -- when it is not known at compile time that the subtype of the
2609 -- formal is a subrange of the subtype of the actual (or vice
2610 -- versa for in out parameters), in order to get range checks
2611 -- on such actuals. (Maybe this case should be handled earlier
2612 -- in the if statement???)
2614 elsif Is_Scalar_Type
(E_Formal
)
2616 (not In_Subrange_Of
(E_Formal
, E_Actual
)
2618 (Ekind
(Formal
) = E_In_Out_Parameter
2619 and then not In_Subrange_Of
(E_Actual
, E_Formal
)))
2621 Add_Call_By_Copy_Code
;
2623 -- The actual denotes a variable which captures the value of an
2624 -- object for validation purposes. Add a copy-back to reflect any
2625 -- potential changes in value back into the original object.
2627 -- Var : ... := Object;
2628 -- if not Var'Valid then -- validity check
2629 -- Call (Var); -- modify var
2630 -- Object := Var; -- update Object
2632 elsif Is_Validation_Variable_Reference
(Actual
) then
2633 Add_Validation_Call_By_Copy_Code
(Actual
);
2636 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
2637 -- by-reference parameters on exit from the call. If the actual
2638 -- is a derived type and the operation is inherited, the body
2639 -- of the operation will not contain a call to the predicate
2640 -- function, so it must be done explicitly after the call. Ditto
2641 -- if the actual is an entity of a predicated subtype.
2643 -- The rule refers to by-reference types, but a check is needed
2644 -- for by-copy types as well. That check is subsumed by the rule
2645 -- for subtype conversion on assignment, but we can generate the
2646 -- required check now.
2648 -- Note also that Subp may be either a subprogram entity for
2649 -- direct calls, or a type entity for indirect calls, which must
2650 -- be handled separately because the name does not denote an
2651 -- overloadable entity.
2653 By_Ref_Predicate_Check
: declare
2654 Aund
: constant Entity_Id
:= Underlying_Type
(E_Actual
);
2664 if Predicate_Enabled
(Atyp
)
2666 -- Skip predicate checks for special cases
2668 and then Predicate_Tests_On_Arguments
(Subp
)
2670 Append_To
(Post_Call
,
2671 Make_Predicate_Check
(Atyp
, Actual
));
2673 end By_Ref_Predicate_Check
;
2675 -- Processing for IN parameters
2678 -- Generate range check if required
2680 if Do_Range_Check
(Actual
) then
2681 Generate_Range_Check
(Actual
, E_Formal
, CE_Range_Check_Failed
);
2684 -- For IN parameters in the bit-packed array case, we expand an
2685 -- indexed component (the circuit in Exp_Ch4 deliberately left
2686 -- indexed components appearing as actuals untouched, so that
2687 -- the special processing above for the OUT and IN OUT cases
2688 -- could be performed. We could make the test in Exp_Ch4 more
2689 -- complex and have it detect the parameter mode, but it is
2690 -- easier simply to handle all cases here.)
2692 if Nkind
(Actual
) = N_Indexed_Component
2693 and then Is_Bit_Packed_Array
(Etype
(Prefix
(Actual
)))
2695 Reset_Packed_Prefix
;
2696 Expand_Packed_Element_Reference
(Actual
);
2698 -- If we have a reference to a bit-packed array, we copy it, since
2699 -- the actual must be byte aligned.
2701 -- Is this really necessary in all cases???
2703 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
2704 Add_Simple_Call_By_Copy_Code
(Force
=> True);
2706 -- If the actual has a nonnative storage model, we need a copy
2708 elsif Nkind
(Actual
) = N_Explicit_Dereference
2710 Has_Designated_Storage_Model_Aspect
(Etype
(Prefix
(Actual
)))
2712 Present
(Storage_Model_Copy_From
2713 (Storage_Model_Object
(Etype
(Prefix
(Actual
)))))
2715 Add_Simple_Call_By_Copy_Code
(Force
=> True);
2717 -- If we have a C++ constructor call, we need to create the object
2719 elsif Is_CPP_Constructor_Call
(Actual
) then
2720 Add_Simple_Call_By_Copy_Code
(Force
=> True);
2722 -- If a nonscalar actual is possibly unaligned, we need a copy
2724 elsif Is_Possibly_Unaligned_Object
(Actual
)
2725 and then not Represented_As_Scalar
(Etype
(Formal
))
2727 Add_Simple_Call_By_Copy_Code
(Force
=> False);
2729 -- Similarly, we have to expand slices of packed arrays here
2730 -- because the result must be byte aligned.
2732 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
2733 Add_Call_By_Copy_Code
;
2735 -- Only processing remaining is to pass by copy if this is a
2736 -- reference to a possibly unaligned slice, since the caller
2737 -- expects an appropriately aligned argument.
2739 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
2740 Add_Call_By_Copy_Code
;
2742 -- We may need to force a copy because of atomicity or volatility
2745 elsif Requires_Atomic_Or_Volatile_Copy
then
2746 Add_Call_By_Copy_Code
;
2748 -- An unusual case: a current instance of an enclosing task can be
2749 -- an actual, and must be replaced by a reference to self.
2751 elsif Is_Entity_Name
(Actual
)
2752 and then Is_Task_Type
(Entity
(Actual
))
2754 if In_Open_Scopes
(Entity
(Actual
)) then
2756 (Make_Function_Call
(Loc
,
2757 Name
=> New_Occurrence_Of
(RTE
(RE_Self
), Loc
))));
2760 -- A task type cannot otherwise appear as an actual
2763 raise Program_Error
;
2768 -- Type-invariant checks for in-out and out parameters, as well as
2769 -- for in parameters of procedures (AI05-0289 and AI12-0044).
2771 if Ekind
(Formal
) /= E_In_Parameter
2772 or else Ekind
(Subp
) = E_Procedure
2774 Caller_Side_Invariant_Checks
: declare
2776 function Is_Public_Subp
return Boolean;
2777 -- Check whether the subprogram being called is a visible
2778 -- operation of the type of the actual. Used to determine
2779 -- whether an invariant check must be generated on the
2782 ---------------------
2783 -- Is_Public_Subp --
2784 ---------------------
2786 function Is_Public_Subp
return Boolean is
2787 Pack
: constant Entity_Id
:= Scope
(Subp
);
2788 Subp_Decl
: Node_Id
;
2791 if not Is_Subprogram
(Subp
) then
2794 -- The operation may be inherited, or a primitive of the
2798 Nkind
(Parent
(Subp
)) in N_Private_Extension_Declaration
2799 | N_Full_Type_Declaration
2801 Subp_Decl
:= Parent
(Subp
);
2804 Subp_Decl
:= Unit_Declaration_Node
(Subp
);
2807 return Ekind
(Pack
) = E_Package
2809 List_Containing
(Subp_Decl
) =
2810 Visible_Declarations
2811 (Specification
(Unit_Declaration_Node
(Pack
)));
2814 -- Start of processing for Caller_Side_Invariant_Checks
2817 -- We generate caller-side invariant checks in two cases:
2819 -- a) when calling an inherited operation, where there is an
2820 -- implicit view conversion of the actual to the parent type.
2822 -- b) When the conversion is explicit
2824 -- We treat these cases separately because the required
2825 -- conversion for a) is added later when expanding the call.
2827 if Has_Invariants
(Etype
(Actual
))
2829 Nkind
(Parent
(Etype
(Actual
)))
2830 = N_Private_Extension_Declaration
2832 if Comes_From_Source
(N
) and then Is_Public_Subp
then
2833 Append_To
(Post_Call
, Make_Invariant_Call
(Actual
));
2836 elsif Nkind
(Actual
) = N_Type_Conversion
2837 and then Has_Invariants
(Etype
(Expression
(Actual
)))
2839 if Comes_From_Source
(N
) and then Is_Public_Subp
then
2841 (Post_Call
, Make_Invariant_Call
(Expression
(Actual
)));
2844 end Caller_Side_Invariant_Checks
;
2847 Next_Formal
(Formal
);
2848 Next_Actual
(Actual
);
2856 procedure Expand_Call
(N
: Node_Id
) is
2857 function Is_Unchecked_Union_Equality
(N
: Node_Id
) return Boolean;
2858 -- Return True if N is a call to the predefined equality operator of an
2859 -- unchecked union type, or a renaming thereof.
2861 ---------------------------------
2862 -- Is_Unchecked_Union_Equality --
2863 ---------------------------------
2865 function Is_Unchecked_Union_Equality
(N
: Node_Id
) return Boolean is
2867 if Is_Entity_Name
(Name
(N
))
2868 and then Ekind
(Entity
(Name
(N
))) = E_Function
2869 and then Present
(First_Formal
(Entity
(Name
(N
))))
2871 Is_Unchecked_Union
(Etype
(First_Formal
(Entity
(Name
(N
)))))
2874 Func
: constant Entity_Id
:= Entity
(Name
(N
));
2875 Typ
: constant Entity_Id
:= Etype
(First_Formal
(Func
));
2876 Decl
: constant Node_Id
:=
2877 Original_Node
(Parent
(Declaration_Node
(Func
)));
2880 return Func
= TSS
(Typ
, TSS_Composite_Equality
)
2881 or else (Nkind
(Decl
) = N_Subprogram_Renaming_Declaration
2882 and then Nkind
(Name
(Decl
)) = N_Operator_Symbol
2883 and then Chars
(Name
(Decl
)) = Name_Op_Eq
2884 and then Ekind
(Entity
(Name
(Decl
))) = E_Operator
);
2890 end Is_Unchecked_Union_Equality
;
2892 -- If this is an indirect call through an Access_To_Subprogram
2893 -- with contract specifications, it is rewritten as a call to
2894 -- the corresponding Access_Subprogram_Wrapper with the same
2895 -- actuals, whose body contains a naked indirect call (which
2896 -- itself must not be rewritten, to prevent infinite recursion).
2898 Must_Rewrite_Indirect_Call
: constant Boolean :=
2899 Ada_Version
>= Ada_2022
2900 and then Nkind
(Name
(N
)) = N_Explicit_Dereference
2901 and then Ekind
(Etype
(Name
(N
))) = E_Subprogram_Type
2903 (Access_Subprogram_Wrapper
(Etype
(Name
(N
))));
2905 Post_Call
: List_Id
;
2907 -- Start of processing for Expand_Call
2910 pragma Assert
(Nkind
(N
) in N_Entry_Call_Statement
2912 | N_Procedure_Call_Statement
);
2914 -- Check that this is not the call in the body of the access
2915 -- subprogram wrapper or the postconditions wrapper.
2917 if Must_Rewrite_Indirect_Call
2918 and then (not Is_Overloadable
(Current_Scope
)
2919 or else not (Is_Access_Subprogram_Wrapper
(Current_Scope
)
2921 (Chars
(Current_Scope
) = Name_uWrapped_Statements
2922 and then Is_Access_Subprogram_Wrapper
2923 (Scope
(Current_Scope
)))))
2926 Loc
: constant Source_Ptr
:= Sloc
(N
);
2927 Wrapper
: constant Entity_Id
:=
2928 Access_Subprogram_Wrapper
(Etype
(Name
(N
)));
2929 Ptr
: constant Node_Id
:= Prefix
(Name
(N
));
2930 Ptr_Type
: constant Entity_Id
:= Etype
(Ptr
);
2931 Typ
: constant Entity_Id
:= Etype
(N
);
2934 Parms
: List_Id
:= Parameter_Associations
(N
);
2938 -- The last actual in the call is the pointer itself.
2939 -- If the aspect is inherited, convert the pointer to the
2940 -- parent type that specifies the contract.
2941 -- If the original access_to_subprogram has defaults for
2942 -- in-mode parameters, the call may include named associations,
2943 -- so we create one for the pointer as well.
2945 if Is_Derived_Type
(Ptr_Type
)
2946 and then Ptr_Type
/= Etype
(Last_Formal
(Wrapper
))
2949 Make_Type_Conversion
(Loc
,
2951 (Etype
(Last_Formal
(Wrapper
)), Loc
), Ptr
);
2957 -- Handle parameterless subprogram.
2964 (Make_Parameter_Association
(Loc
,
2965 Selector_Name
=> Make_Identifier
(Loc
,
2966 Chars
(Last_Formal
(Wrapper
))),
2967 Explicit_Actual_Parameter
=> Ptr_Act
),
2970 if Nkind
(N
) = N_Procedure_Call_Statement
then
2971 New_N
:= Make_Procedure_Call_Statement
(Loc
,
2972 Name
=> New_Occurrence_Of
(Wrapper
, Loc
),
2973 Parameter_Associations
=> Parms
);
2975 New_N
:= Make_Function_Call
(Loc
,
2976 Name
=> New_Occurrence_Of
(Wrapper
, Loc
),
2977 Parameter_Associations
=> Parms
);
2981 Analyze_And_Resolve
(N
, Typ
);
2984 -- Case of a call to the predefined equality operator of an unchecked
2985 -- union type, which requires specific processing.
2987 elsif Is_Unchecked_Union_Equality
(N
) then
2989 Eq
: constant Entity_Id
:= Entity
(Name
(N
));
2992 Expand_Unchecked_Union_Equality
(N
);
2994 -- If the call was not rewritten as a raise, expand the actuals
2996 if Nkind
(N
) = N_Function_Call
then
2997 pragma Assert
(Check_Number_Of_Actuals
(N
, Eq
));
2998 Expand_Actuals
(N
, Eq
, Post_Call
);
2999 pragma Assert
(Is_Empty_List
(Post_Call
));
3006 Expand_Call_Helper
(N
, Post_Call
);
3007 Insert_Post_Call_Actions
(N
, Post_Call
);
3011 ------------------------
3012 -- Expand_Call_Helper --
3013 ------------------------
3015 -- This procedure handles expansion of function calls and procedure call
3016 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
3017 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
3019 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
3020 -- Provide values of actuals for all formals in Extra_Formals list
3021 -- Replace "call" to enumeration literal function by literal itself
3022 -- Rewrite call to predefined operator as operator
3023 -- Replace actuals to in-out parameters that are numeric conversions,
3024 -- with explicit assignment to temporaries before and after the call.
3026 -- Note that the list of actuals has been filled with default expressions
3027 -- during semantic analysis of the call. Only the extra actuals required
3028 -- for the 'Constrained attribute and for accessibility checks are added
3031 procedure Expand_Call_Helper
(N
: Node_Id
; Post_Call
: out List_Id
) is
3032 Loc
: constant Source_Ptr
:= Sloc
(N
);
3033 Call_Node
: Node_Id
:= N
;
3034 Extra_Actuals
: List_Id
:= No_List
;
3035 Prev
: Node_Id
:= Empty
;
3037 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
);
3038 -- Adds one entry to the end of the actual parameter list. Used for
3039 -- default parameters and for extra actuals (for Extra_Formals). The
3040 -- argument is an N_Parameter_Association node.
3042 procedure Add_Cond_Expression_Extra_Actual
(Formal
: Entity_Id
);
3043 -- Adds extra accessibility actuals in the case of a conditional
3044 -- expression corresponding to Formal.
3046 -- Note: Conditional expressions used as actuals for anonymous access
3047 -- formals complicate the process of propagating extra accessibility
3048 -- actuals and must be handled in a recursive fashion since they can
3049 -- be embedded within each other.
3051 procedure Add_Dummy_Build_In_Place_Actuals
3052 (Function_Id
: Entity_Id
;
3053 Num_Added_Extra_Actuals
: Nat
:= 0);
3054 -- Adds dummy actuals for the BIP extra formals of the called function.
3055 -- Num_Added_Extra_Actuals is the number of non-BIP extra actuals added
3056 -- to the actuals immediately before calling this subprogram.
3058 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
);
3059 -- Adds an extra actual to the list of extra actuals. Expr is the
3060 -- expression for the value of the actual, EF is the entity for the
3063 procedure Add_View_Conversion_Invariants
3064 (Formal
: Entity_Id
;
3066 -- Adds invariant checks for every intermediate type between the range
3067 -- of a view converted argument to its ancestor (from parent to child).
3069 function Can_Fold_Predicate_Call
(P
: Entity_Id
) return Boolean;
3070 -- Try to constant-fold a predicate check, which often enough is a
3071 -- simple arithmetic expression that can be computed statically if
3072 -- its argument is static. This cleans up the output of CCG, even
3073 -- though useless predicate checks will be generally removed by
3074 -- back-end optimizations.
3076 procedure Check_Subprogram_Variant
;
3077 -- Emit a call to the internally generated procedure with checks for
3078 -- aspect Subprogram_Variant, if present and enabled.
3080 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
;
3081 -- Within an instance, a type derived from an untagged formal derived
3082 -- type inherits from the original parent, not from the actual. The
3083 -- current derivation mechanism has the derived type inherit from the
3084 -- actual, which is only correct outside of the instance. If the
3085 -- subprogram is inherited, we test for this particular case through a
3086 -- convoluted tree traversal before setting the proper subprogram to be
3089 function In_Unfrozen_Instance
(E
: Entity_Id
) return Boolean;
3090 -- Return true if E comes from an instance that is not yet frozen
3092 function Is_Class_Wide_Interface_Type
(E
: Entity_Id
) return Boolean;
3093 -- Return True when E is a class-wide interface type or an access to
3094 -- a class-wide interface type.
3096 function Is_Direct_Deep_Call
(Subp
: Entity_Id
) return Boolean;
3097 -- Determine if Subp denotes a non-dispatching call to a Deep routine
3099 function New_Value
(From
: Node_Id
) return Node_Id
;
3100 -- From is the original Expression. New_Value is equivalent to a call
3101 -- to Duplicate_Subexpr with an explicit dereference when From is an
3102 -- access parameter.
3104 --------------------------
3105 -- Add_Actual_Parameter --
3106 --------------------------
3108 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
) is
3109 Actual_Expr
: constant Node_Id
:=
3110 Explicit_Actual_Parameter
(Insert_Param
);
3113 -- Case of insertion is first named actual
3115 if No
(Prev
) or else
3116 Nkind
(Parent
(Prev
)) /= N_Parameter_Association
3118 Set_Next_Named_Actual
3119 (Insert_Param
, First_Named_Actual
(Call_Node
));
3120 Set_First_Named_Actual
(Call_Node
, Actual_Expr
);
3123 if No
(Parameter_Associations
(Call_Node
)) then
3124 Set_Parameter_Associations
(Call_Node
, New_List
);
3127 Append
(Insert_Param
, Parameter_Associations
(Call_Node
));
3130 Insert_After
(Prev
, Insert_Param
);
3133 -- Case of insertion is not first named actual
3136 Set_Next_Named_Actual
3137 (Insert_Param
, Next_Named_Actual
(Parent
(Prev
)));
3138 Set_Next_Named_Actual
(Parent
(Prev
), Actual_Expr
);
3139 Append
(Insert_Param
, Parameter_Associations
(Call_Node
));
3142 Prev
:= Actual_Expr
;
3143 end Add_Actual_Parameter
;
3145 --------------------------------------
3146 -- Add_Cond_Expression_Extra_Actual --
3147 --------------------------------------
3149 procedure Add_Cond_Expression_Extra_Actual
3150 (Formal
: Entity_Id
)
3155 procedure Insert_Level_Assign
(Branch
: Node_Id
);
3156 -- Recursively add assignment of the level temporary on each branch
3157 -- while moving through nested conditional expressions.
3159 -------------------------
3160 -- Insert_Level_Assign --
3161 -------------------------
3163 procedure Insert_Level_Assign
(Branch
: Node_Id
) is
3165 procedure Expand_Branch
(Res_Assn
: Node_Id
);
3166 -- Perform expansion or iterate further within nested
3167 -- conditionals given the object declaration or assignment to
3168 -- result object created during expansion which represents a
3169 -- branch of the conditional expression.
3175 procedure Expand_Branch
(Res_Assn
: Node_Id
) is
3177 pragma Assert
(Nkind
(Res_Assn
) in
3178 N_Assignment_Statement |
3179 N_Object_Declaration
);
3181 -- There are more nested conditional expressions so we must go
3184 if Nkind
(Expression
(Res_Assn
)) = N_Expression_With_Actions
3186 Nkind
(Original_Node
(Expression
(Res_Assn
)))
3187 in N_Case_Expression | N_If_Expression
3190 (Expression
(Res_Assn
));
3192 -- Add the level assignment
3195 Insert_Before_And_Analyze
(Res_Assn
,
3196 Make_Assignment_Statement
(Loc
,
3197 Name
=> New_Occurrence_Of
(Lvl
, Loc
),
3200 (Expr
=> Expression
(Res_Assn
),
3201 Level
=> Dynamic_Level
,
3202 Allow_Alt_Model
=> False)));
3209 -- Start of processing for Insert_Level_Assign
3212 -- Examine further nested conditionals
3214 pragma Assert
(Nkind
(Branch
) =
3215 N_Expression_With_Actions
);
3217 -- Find the relevant statement in the actions
3219 Cond
:= First
(Actions
(Branch
));
3220 while Present
(Cond
) loop
3221 exit when Nkind
(Cond
) in N_Case_Statement | N_If_Statement
;
3225 -- The conditional expression may have been optimized away, so
3226 -- examine the actions in the branch.
3229 Expand_Branch
(Last
(Actions
(Branch
)));
3231 -- Iterate through if expression branches
3233 elsif Nkind
(Cond
) = N_If_Statement
then
3234 Expand_Branch
(Last
(Then_Statements
(Cond
)));
3235 Expand_Branch
(Last
(Else_Statements
(Cond
)));
3237 -- Iterate through case alternatives
3239 elsif Nkind
(Cond
) = N_Case_Statement
then
3241 Alt
:= First
(Alternatives
(Cond
));
3242 while Present
(Alt
) loop
3243 Expand_Branch
(Last
(Statements
(Alt
)));
3247 end Insert_Level_Assign
;
3249 -- Start of processing for cond expression case
3252 -- Create declaration of a temporary to store the accessibility
3253 -- level of each branch of the conditional expression.
3255 Lvl
:= Make_Temporary
(Loc
, 'L');
3256 Decl
:= Make_Object_Declaration
(Loc
,
3257 Defining_Identifier
=> Lvl
,
3258 Object_Definition
=>
3259 New_Occurrence_Of
(Standard_Natural
, Loc
));
3261 -- Install the declaration and perform necessary expansion if we
3262 -- are dealing with a procedure call.
3264 if Nkind
(Call_Node
) = N_Procedure_Call_Statement
then
3269 -- If_Exp_Res : Typ;
3271 -- Lvl := 0; -- Access level
3272 -- If_Exp_Res := Exp;
3274 -- in If_Exp_Res end;},
3279 Insert_Before_And_Analyze
(Call_Node
, Decl
);
3281 -- Ditto for a function call. Note that we do not wrap the function
3282 -- call into an expression with action to avoid bad interactions with
3283 -- Exp_Ch4.Process_Transient_In_Expression.
3287 -- Lvl : Natural; -- placed above the function call
3293 -- Lvl := 0; -- Access level
3294 -- If_Exp_Res := Exp;
3295 -- in If_Exp_Res end;},
3300 Insert_Action
(Call_Node
, Decl
);
3301 Analyze
(Call_Node
);
3304 -- Decorate the conditional expression with assignments to our level
3307 Insert_Level_Assign
(Prev
);
3309 -- Make our level temporary the passed actual
3312 (Expr
=> New_Occurrence_Of
(Lvl
, Loc
),
3313 EF
=> Extra_Accessibility
(Formal
));
3314 end Add_Cond_Expression_Extra_Actual
;
3316 --------------------------------------
3317 -- Add_Dummy_Build_In_Place_Actuals --
3318 --------------------------------------
3320 procedure Add_Dummy_Build_In_Place_Actuals
3321 (Function_Id
: Entity_Id
;
3322 Num_Added_Extra_Actuals
: Nat
:= 0)
3324 Loc
: constant Source_Ptr
:= Sloc
(Call_Node
);
3325 Formal
: Entity_Id
:= Extra_Formals
(Function_Id
);
3330 -- We never generate extra formals if expansion is not active because
3331 -- we don't need them unless we are generating code. No action needed
3332 -- for thunks since they propagate all their extra actuals.
3334 if not Expander_Active
3335 or else Is_Thunk
(Current_Scope
)
3340 -- Skip already-added non-BIP extra actuals
3342 Skip_Extra
:= Num_Added_Extra_Actuals
;
3343 while Skip_Extra
> 0 loop
3344 pragma Assert
(not Is_Build_In_Place_Entity
(Formal
));
3345 Formal
:= Extra_Formal
(Formal
);
3346 Skip_Extra
:= Skip_Extra
- 1;
3349 -- Append the dummy BIP extra actuals
3351 while Present
(Formal
) loop
3352 pragma Assert
(Is_Build_In_Place_Entity
(Formal
));
3356 if Etype
(Formal
) = Standard_Natural
then
3357 Actual
:= Make_Integer_Literal
(Loc
, Uint_0
);
3358 Analyze_And_Resolve
(Actual
, Standard_Natural
);
3359 Add_Extra_Actual_To_Call
(N
, Formal
, Actual
);
3363 elsif Etype
(Formal
) = Standard_Integer
then
3364 Actual
:= Make_Integer_Literal
(Loc
, Uint_0
);
3365 Analyze_And_Resolve
(Actual
, Standard_Integer
);
3366 Add_Extra_Actual_To_Call
(N
, Formal
, Actual
);
3368 -- BIPstoragepool, BIPcollection, BIPactivationchain,
3371 elsif Is_Access_Type
(Etype
(Formal
)) then
3372 Actual
:= Make_Null
(Loc
);
3373 Analyze_And_Resolve
(Actual
, Etype
(Formal
));
3374 Add_Extra_Actual_To_Call
(N
, Formal
, Actual
);
3377 pragma Assert
(False);
3378 raise Program_Error
;
3381 Formal
:= Extra_Formal
(Formal
);
3384 -- Mark the call as processed build-in-place call; required
3385 -- to avoid adding the extra formals twice.
3387 Set_Is_Expanded_Build_In_Place_Call
(Call_Node
);
3389 pragma Assert
(Check_Number_Of_Actuals
(Call_Node
, Function_Id
));
3390 pragma Assert
(Check_BIP_Actuals
(Call_Node
, Function_Id
));
3391 end Add_Dummy_Build_In_Place_Actuals
;
3393 ----------------------
3394 -- Add_Extra_Actual --
3395 ----------------------
3397 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
) is
3398 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
3401 if Extra_Actuals
= No_List
then
3402 Extra_Actuals
:= New_List
;
3403 Set_Parent
(Extra_Actuals
, Call_Node
);
3406 Append_To
(Extra_Actuals
,
3407 Make_Parameter_Association
(Loc
,
3408 Selector_Name
=> New_Occurrence_Of
(EF
, Loc
),
3409 Explicit_Actual_Parameter
=> Expr
));
3411 Analyze_And_Resolve
(Expr
, Etype
(EF
));
3413 if Nkind
(Call_Node
) = N_Function_Call
then
3414 Set_Is_Accessibility_Actual
(Parent
(Expr
));
3416 end Add_Extra_Actual
;
3418 ------------------------------------
3419 -- Add_View_Conversion_Invariants --
3420 ------------------------------------
3422 procedure Add_View_Conversion_Invariants
3423 (Formal
: Entity_Id
;
3427 Curr_Typ
: Entity_Id
;
3428 Inv_Checks
: List_Id
;
3429 Par_Typ
: Entity_Id
;
3432 Inv_Checks
:= No_List
;
3434 -- Extract the argument from a potentially nested set of view
3438 while Nkind
(Arg
) = N_Type_Conversion
loop
3439 Arg
:= Expression
(Arg
);
3442 -- Move up the derivation chain starting with the type of the formal
3443 -- parameter down to the type of the actual object.
3446 Par_Typ
:= Etype
(Arg
);
3447 while Par_Typ
/= Etype
(Formal
) and Par_Typ
/= Curr_Typ
loop
3448 Curr_Typ
:= Par_Typ
;
3450 if Has_Invariants
(Curr_Typ
)
3451 and then Present
(Invariant_Procedure
(Curr_Typ
))
3453 -- Verify the invariant of the current type. Generate:
3455 -- <Curr_Typ>Invariant (Curr_Typ (Arg));
3457 Prepend_New_To
(Inv_Checks
,
3458 Make_Procedure_Call_Statement
(Loc
,
3461 (Invariant_Procedure
(Curr_Typ
), Loc
),
3462 Parameter_Associations
=> New_List
(
3463 Make_Type_Conversion
(Loc
,
3464 Subtype_Mark
=> New_Occurrence_Of
(Curr_Typ
, Loc
),
3465 Expression
=> New_Copy_Tree
(Arg
)))));
3468 Par_Typ
:= Base_Type
(Etype
(Curr_Typ
));
3471 -- If the node is a function call the generated tests have been
3472 -- already handled in Insert_Post_Call_Actions.
3474 if not Is_Empty_List
(Inv_Checks
)
3475 and then Nkind
(Call_Node
) = N_Procedure_Call_Statement
3477 Insert_Actions_After
(Call_Node
, Inv_Checks
);
3479 end Add_View_Conversion_Invariants
;
3481 -----------------------------
3482 -- Can_Fold_Predicate_Call --
3483 -----------------------------
3485 function Can_Fold_Predicate_Call
(P
: Entity_Id
) return Boolean is
3488 function Augments_Other_Dynamic_Predicate
(DP_Aspect_Spec
: Node_Id
)
3490 -- Given a Dynamic_Predicate aspect aspecification for a
3491 -- discrete type, returns True iff another DP specification
3492 -- applies (indirectly, via a subtype type or a derived type)
3493 -- to the same entity that this aspect spec applies to.
3495 function May_Fold
(N
: Node_Id
) return Traverse_Result
;
3496 -- The predicate expression is foldable if it only contains operators
3497 -- and literals. During this check, we also replace occurrences of
3498 -- the formal of the constructed predicate function with the static
3499 -- value of the actual. This is done on a copy of the analyzed
3500 -- expression for the predicate.
3502 --------------------------------------
3503 -- Augments_Other_Dynamic_Predicate --
3504 --------------------------------------
3506 function Augments_Other_Dynamic_Predicate
(DP_Aspect_Spec
: Node_Id
)
3509 Aspect_Bearer
: Entity_Id
:= Entity
(DP_Aspect_Spec
);
3512 Aspect_Bearer
:= Nearest_Ancestor
(Aspect_Bearer
);
3514 if No
(Aspect_Bearer
) then
3519 Aspect_Spec
: constant Node_Id
:=
3520 Find_Aspect
(Aspect_Bearer
, Aspect_Dynamic_Predicate
);
3522 if Present
(Aspect_Spec
)
3523 and then Aspect_Spec
/= DP_Aspect_Spec
3525 -- Found another Dynamic_Predicate aspect spec
3530 end Augments_Other_Dynamic_Predicate
;
3536 function May_Fold
(N
: Node_Id
) return Traverse_Result
is
3542 when N_Expanded_Name
3545 if Ekind
(Entity
(N
)) = E_In_Parameter
3546 and then Entity
(N
) = First_Entity
(P
)
3548 Rewrite
(N
, New_Copy
(Actual
));
3549 Set_Is_Static_Expression
(N
);
3552 elsif Ekind
(Entity
(N
)) = E_Enumeration_Literal
then
3559 when N_Case_Expression
3564 when N_Integer_Literal
=>
3572 function Try_Fold
is new Traverse_Func
(May_Fold
);
3574 -- Other Local variables
3576 Subt
: constant Entity_Id
:= Etype
(First_Entity
(P
));
3580 -- Start of processing for Can_Fold_Predicate_Call
3583 -- Folding is only interesting if the actual is static and its type
3584 -- has a Dynamic_Predicate aspect. For CodePeer we preserve the
3587 Actual
:= First
(Parameter_Associations
(Call_Node
));
3588 Aspect
:= Find_Aspect
(Subt
, Aspect_Dynamic_Predicate
);
3590 -- If actual is a declared constant, retrieve its value
3592 if Is_Entity_Name
(Actual
)
3593 and then Ekind
(Entity
(Actual
)) = E_Constant
3595 Actual
:= Constant_Value
(Entity
(Actual
));
3599 or else Nkind
(Actual
) /= N_Integer_Literal
3600 or else not Has_Dynamic_Predicate_Aspect
(Subt
)
3603 -- Do not fold if multiple applicable predicate aspects
3604 or else Has_Ghost_Predicate_Aspect
(Subt
)
3605 or else Has_Aspect
(Subt
, Aspect_Static_Predicate
)
3606 or else Has_Aspect
(Subt
, Aspect_Predicate
)
3607 or else Augments_Other_Dynamic_Predicate
(Aspect
)
3608 or else CodePeer_Mode
3613 -- Retrieve the analyzed expression for the predicate
3615 Pred
:= New_Copy_Tree
(Expression
(Aspect
));
3617 if Try_Fold
(Pred
) = OK
then
3618 Rewrite
(Call_Node
, Pred
);
3619 Analyze_And_Resolve
(Call_Node
, Standard_Boolean
);
3622 -- Otherwise continue the expansion of the function call
3627 end Can_Fold_Predicate_Call
;
3629 ------------------------------
3630 -- Check_Subprogram_Variant --
3631 ------------------------------
3633 procedure Check_Subprogram_Variant
is
3635 function Duplicate_Params_Without_Extra_Actuals
3636 (Call_Node
: Node_Id
) return List_Id
;
3637 -- Duplicate actual parameters of Call_Node into New_Call without
3640 --------------------------------------------
3641 -- Duplicate_Params_Without_Extra_Actuals --
3642 --------------------------------------------
3644 function Duplicate_Params_Without_Extra_Actuals
3645 (Call_Node
: Node_Id
) return List_Id
3647 Proc_Id
: constant Entity_Id
:= Entity
(Name
(Call_Node
));
3648 Actuals
: constant List_Id
:= Parameter_Associations
(Call_Node
);
3650 Actual
: Node_Or_Entity_Id
;
3654 if Actuals
= No_List
then
3659 Actual
:= First
(Actuals
);
3660 Formal
:= First_Formal
(Proc_Id
);
3662 while Present
(Formal
)
3663 and then Formal
/= Extra_Formals
(Proc_Id
)
3665 Append
(New_Copy
(Actual
), NL
);
3668 Next_Formal
(Formal
);
3673 end Duplicate_Params_Without_Extra_Actuals
;
3677 Variant_Prag
: constant Node_Id
:=
3678 Get_Pragma
(Current_Scope
, Pragma_Subprogram_Variant
);
3681 Pragma_Arg1
: Node_Id
;
3682 Variant_Proc
: Entity_Id
;
3685 if Present
(Variant_Prag
) and then Is_Checked
(Variant_Prag
) then
3688 Expression
(First
(Pragma_Argument_Associations
(Variant_Prag
)));
3690 -- If pragma parameter is still an aggregate, it comes from a
3691 -- structural variant, which is not expanded and ignored for
3692 -- run-time execution.
3694 if Nkind
(Pragma_Arg1
) = N_Aggregate
then
3699 (First
(Component_Associations
(Pragma_Arg1
))))) =
3704 -- Otherwise, analysis of the pragma rewrites its argument with a
3705 -- reference to the internally generated procedure.
3707 Variant_Proc
:= Entity
(Pragma_Arg1
);
3710 Make_Procedure_Call_Statement
(Loc
,
3712 New_Occurrence_Of
(Variant_Proc
, Loc
),
3713 Parameter_Associations
=>
3714 Duplicate_Params_Without_Extra_Actuals
(Call_Node
));
3716 Insert_Action
(Call_Node
, New_Call
);
3718 pragma Assert
(Etype
(New_Call
) /= Any_Type
3719 or else Serious_Errors_Detected
> 0);
3721 end Check_Subprogram_Variant
;
3723 ---------------------------
3724 -- Inherited_From_Formal --
3725 ---------------------------
3727 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
is
3729 Gen_Par
: Entity_Id
;
3730 Gen_Prim
: Elist_Id
;
3735 -- If the operation is inherited, it is attached to the corresponding
3736 -- type derivation. If the parent in the derivation is a generic
3737 -- actual, it is a subtype of the actual, and we have to recover the
3738 -- original derived type declaration to find the proper parent.
3740 if Nkind
(Parent
(S
)) /= N_Full_Type_Declaration
3741 or else not Is_Derived_Type
(Defining_Identifier
(Parent
(S
)))
3742 or else Nkind
(Type_Definition
(Original_Node
(Parent
(S
)))) /=
3743 N_Derived_Type_Definition
3744 or else not In_Instance
3751 (Type_Definition
(Original_Node
(Parent
(S
))));
3753 if Nkind
(Indic
) = N_Subtype_Indication
then
3754 Par
:= Entity
(Subtype_Mark
(Indic
));
3756 Par
:= Entity
(Indic
);
3760 if not Is_Generic_Actual_Type
(Par
)
3761 or else Is_Tagged_Type
(Par
)
3762 or else Nkind
(Parent
(Par
)) /= N_Subtype_Declaration
3763 or else not In_Open_Scopes
(Scope
(Par
))
3767 Gen_Par
:= Generic_Parent_Type
(Parent
(Par
));
3770 -- If the actual has no generic parent type, the formal is not
3771 -- a formal derived type, so nothing to inherit.
3773 if No
(Gen_Par
) then
3777 -- If the generic parent type is still the generic type, this is a
3778 -- private formal, not a derived formal, and there are no operations
3779 -- inherited from the formal.
3781 if Nkind
(Parent
(Gen_Par
)) = N_Formal_Type_Declaration
then
3785 Gen_Prim
:= Collect_Primitive_Operations
(Gen_Par
);
3787 Elmt
:= First_Elmt
(Gen_Prim
);
3788 while Present
(Elmt
) loop
3789 if Chars
(Node
(Elmt
)) = Chars
(S
) then
3795 F1
:= First_Formal
(S
);
3796 F2
:= First_Formal
(Node
(Elmt
));
3798 and then Present
(F2
)
3800 if Etype
(F1
) = Etype
(F2
)
3801 or else Etype
(F2
) = Gen_Par
3807 exit; -- not the right subprogram
3819 raise Program_Error
;
3820 end Inherited_From_Formal
;
3822 --------------------------
3823 -- In_Unfrozen_Instance --
3824 --------------------------
3826 function In_Unfrozen_Instance
(E
: Entity_Id
) return Boolean is
3831 while Present
(S
) and then S
/= Standard_Standard
loop
3832 if Is_Generic_Instance
(S
)
3833 and then Present
(Freeze_Node
(S
))
3834 and then not Analyzed
(Freeze_Node
(S
))
3843 end In_Unfrozen_Instance
;
3845 ----------------------------------
3846 -- Is_Class_Wide_Interface_Type --
3847 ----------------------------------
3849 function Is_Class_Wide_Interface_Type
(E
: Entity_Id
) return Boolean is
3851 Typ
: Entity_Id
:= E
;
3854 if Has_Non_Limited_View
(Typ
) then
3855 Typ
:= Non_Limited_View
(Typ
);
3858 if Ekind
(Typ
) = E_Anonymous_Access_Type
then
3859 DDT
:= Directly_Designated_Type
(Typ
);
3861 if Has_Non_Limited_View
(DDT
) then
3862 DDT
:= Non_Limited_View
(DDT
);
3865 return Is_Class_Wide_Type
(DDT
) and then Is_Interface
(DDT
);
3867 return Is_Class_Wide_Type
(Typ
) and then Is_Interface
(Typ
);
3869 end Is_Class_Wide_Interface_Type
;
3871 -------------------------
3872 -- Is_Direct_Deep_Call --
3873 -------------------------
3875 function Is_Direct_Deep_Call
(Subp
: Entity_Id
) return Boolean is
3877 if Is_TSS
(Subp
, TSS_Deep_Adjust
)
3878 or else Is_TSS
(Subp
, TSS_Deep_Finalize
)
3879 or else Is_TSS
(Subp
, TSS_Deep_Initialize
)
3886 Actual
:= First
(Parameter_Associations
(Call_Node
));
3887 Formal
:= First_Formal
(Subp
);
3888 while Present
(Actual
)
3889 and then Present
(Formal
)
3891 if Nkind
(Actual
) = N_Identifier
3892 and then Is_Controlling_Actual
(Actual
)
3893 and then Etype
(Actual
) = Etype
(Formal
)
3899 Next_Formal
(Formal
);
3905 end Is_Direct_Deep_Call
;
3911 function New_Value
(From
: Node_Id
) return Node_Id
is
3912 Res
: constant Node_Id
:= Duplicate_Subexpr
(From
);
3914 if Is_Access_Type
(Etype
(From
)) then
3915 return Make_Explicit_Dereference
(Sloc
(From
), Prefix
=> Res
);
3923 Remote
: constant Boolean := Is_Remote_Call
(Call_Node
);
3926 Orig_Subp
: Entity_Id
:= Empty
;
3927 Param_Count
: Positive;
3928 Parent_Formal
: Entity_Id
;
3929 Parent_Subp
: Entity_Id
;
3933 CW_Interface_Formals_Present
: Boolean := False;
3935 -- Start of processing for Expand_Call_Helper
3938 Post_Call
:= New_List
;
3940 -- Expand the function or procedure call if the first actual has a
3941 -- declared dimension aspect, and the subprogram is declared in one
3942 -- of the dimension I/O packages.
3944 if Ada_Version
>= Ada_2012
3945 and then Nkind
(Call_Node
) in N_Subprogram_Call
3946 and then Present
(Parameter_Associations
(Call_Node
))
3948 Expand_Put_Call_With_Symbol
(Call_Node
);
3951 -- Ignore if previous error
3953 if Nkind
(Call_Node
) in N_Has_Etype
3954 and then Etype
(Call_Node
) = Any_Type
3959 -- Call using access to subprogram with explicit dereference
3961 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
3962 Subp
:= Etype
(Name
(Call_Node
));
3963 Parent_Subp
:= Empty
;
3965 -- Case of call to simple entry, where the Name is a selected component
3966 -- whose prefix is the task, and whose selector name is the entry name
3968 elsif Nkind
(Name
(Call_Node
)) = N_Selected_Component
then
3969 Subp
:= Entity
(Selector_Name
(Name
(Call_Node
)));
3970 Parent_Subp
:= Empty
;
3972 -- Case of call to member of entry family, where Name is an indexed
3973 -- component, with the prefix being a selected component giving the
3974 -- task and entry family name, and the index being the entry index.
3976 elsif Nkind
(Name
(Call_Node
)) = N_Indexed_Component
then
3977 Subp
:= Entity
(Selector_Name
(Prefix
(Name
(Call_Node
))));
3978 Parent_Subp
:= Empty
;
3983 Subp
:= Entity
(Name
(Call_Node
));
3984 Parent_Subp
:= Alias
(Subp
);
3986 -- Replace call to Raise_Exception by call to Raise_Exception_Always
3987 -- if we can tell that the first parameter cannot possibly be null.
3988 -- This improves efficiency by avoiding a run-time test.
3990 -- We do not do this if Raise_Exception_Always does not exist, which
3991 -- can happen in configurable run time profiles which provide only a
3994 if Is_RTE
(Subp
, RE_Raise_Exception
)
3995 and then RTE_Available
(RE_Raise_Exception_Always
)
3998 FA
: constant Node_Id
:=
3999 Original_Node
(First_Actual
(Call_Node
));
4002 -- The case we catch is where the first argument is obtained
4003 -- using the Identity attribute (which must always be
4006 if Nkind
(FA
) = N_Attribute_Reference
4007 and then Attribute_Name
(FA
) = Name_Identity
4009 Subp
:= RTE
(RE_Raise_Exception_Always
);
4010 Set_Name
(Call_Node
, New_Occurrence_Of
(Subp
, Loc
));
4015 if Ekind
(Subp
) = E_Entry
then
4016 Parent_Subp
:= Empty
;
4020 -- Ensure that the called subprogram has all its formals
4022 if not Is_Frozen
(Subp
) then
4023 Create_Extra_Formals
(Subp
);
4026 -- Ada 2005 (AI-345): We have a procedure call as a triggering
4027 -- alternative in an asynchronous select or as an entry call in
4028 -- a conditional or timed select. Check whether the procedure call
4029 -- is a renaming of an entry and rewrite it as an entry call.
4031 if Ada_Version
>= Ada_2005
4032 and then Nkind
(Call_Node
) = N_Procedure_Call_Statement
4034 ((Nkind
(Parent
(Call_Node
)) = N_Triggering_Alternative
4035 and then Triggering_Statement
(Parent
(Call_Node
)) = Call_Node
)
4037 (Nkind
(Parent
(Call_Node
)) = N_Entry_Call_Alternative
4038 and then Entry_Call_Statement
(Parent
(Call_Node
)) = Call_Node
))
4042 Ren_Root
: Entity_Id
:= Subp
;
4045 -- This may be a chain of renamings, find the root
4047 if Present
(Alias
(Ren_Root
)) then
4048 Ren_Root
:= Alias
(Ren_Root
);
4051 if Present
(Parent
(Ren_Root
))
4052 and then Present
(Original_Node
(Parent
(Parent
(Ren_Root
))))
4054 Ren_Decl
:= Original_Node
(Parent
(Parent
(Ren_Root
)));
4056 if Nkind
(Ren_Decl
) = N_Subprogram_Renaming_Declaration
then
4058 Make_Entry_Call_Statement
(Loc
,
4060 New_Copy_Tree
(Name
(Ren_Decl
)),
4061 Parameter_Associations
=>
4063 (Parameter_Associations
(Call_Node
))));
4071 -- If this is a call to a predicate function, try to constant fold it
4073 if Nkind
(Call_Node
) = N_Function_Call
4074 and then Is_Entity_Name
(Name
(Call_Node
))
4075 and then Is_Predicate_Function
(Subp
)
4076 and then Can_Fold_Predicate_Call
(Subp
)
4081 if Transform_Function_Array
4082 and then Nkind
(Call_Node
) = N_Function_Call
4083 and then Is_Entity_Name
(Name
(Call_Node
))
4086 Func_Id
: constant Entity_Id
:=
4087 Ultimate_Alias
(Entity
(Name
(Call_Node
)));
4089 -- When generating C code, transform a function call that returns
4090 -- a constrained array type into procedure form.
4092 if Rewritten_For_C
(Func_Id
) then
4094 -- For internally generated calls ensure that they reference
4095 -- the entity of the spec of the called function (needed since
4096 -- the expander may generate calls using the entity of their
4099 if not Comes_From_Source
(Call_Node
)
4100 and then Nkind
(Unit_Declaration_Node
(Func_Id
)) =
4103 Set_Entity
(Name
(Call_Node
),
4104 Corresponding_Function
4105 (Corresponding_Procedure
(Func_Id
)));
4108 Rewrite_Function_Call_For_C
(Call_Node
);
4111 -- Also introduce a temporary for functions that return a record
4112 -- called within another procedure or function call, since records
4113 -- are passed by pointer in the generated C code, and we cannot
4114 -- take a pointer from a subprogram call.
4116 elsif Modify_Tree_For_C
4117 and then Nkind
(Parent
(Call_Node
)) in N_Subprogram_Call
4118 and then Is_Record_Type
(Etype
(Func_Id
))
4121 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
4126 -- Temp : ... := Func_Call (...);
4129 Make_Object_Declaration
(Loc
,
4130 Defining_Identifier
=> Temp_Id
,
4131 Object_Definition
=>
4132 New_Occurrence_Of
(Etype
(Func_Id
), Loc
),
4134 Make_Function_Call
(Loc
,
4136 New_Occurrence_Of
(Func_Id
, Loc
),
4137 Parameter_Associations
=>
4138 Parameter_Associations
(Call_Node
)));
4140 Insert_Action
(Parent
(Call_Node
), Decl
);
4141 Rewrite
(Call_Node
, New_Occurrence_Of
(Temp_Id
, Loc
));
4148 -- First step, compute extra actuals, corresponding to any Extra_Formals
4149 -- present. Note that we do not access Extra_Formals directly, instead
4150 -- we simply note the presence of the extra formals as we process the
4151 -- regular formals collecting corresponding actuals in Extra_Actuals.
4153 -- We also generate any required range checks for actuals for in formals
4154 -- as we go through the loop, since this is a convenient place to do it.
4155 -- (Though it seems that this would be better done in Expand_Actuals???)
4157 -- Special case: Thunks must not compute the extra actuals; they must
4158 -- just propagate to the target primitive their extra actuals.
4160 if Is_Thunk
(Current_Scope
)
4161 and then Thunk_Entity
(Current_Scope
) = Subp
4162 and then Present
(Extra_Formals
(Subp
))
4164 pragma Assert
(Extra_Formals_Match_OK
(Current_Scope
, Subp
));
4167 Target_Formal
: Entity_Id
;
4168 Thunk_Formal
: Entity_Id
;
4171 Target_Formal
:= Extra_Formals
(Subp
);
4172 Thunk_Formal
:= Extra_Formals
(Current_Scope
);
4173 while Present
(Target_Formal
) loop
4175 (Expr
=> New_Occurrence_Of
(Thunk_Formal
, Loc
),
4176 EF
=> Thunk_Formal
);
4178 Target_Formal
:= Extra_Formal
(Target_Formal
);
4179 Thunk_Formal
:= Extra_Formal
(Thunk_Formal
);
4182 while Is_Non_Empty_List
(Extra_Actuals
) loop
4183 Add_Actual_Parameter
(Remove_Head
(Extra_Actuals
));
4186 -- Mark the call as processed build-in-place call; required
4187 -- to avoid adding the extra formals twice.
4189 if Nkind
(Call_Node
) = N_Function_Call
then
4190 Set_Is_Expanded_Build_In_Place_Call
(Call_Node
);
4193 Expand_Actuals
(Call_Node
, Subp
, Post_Call
);
4194 pragma Assert
(Is_Empty_List
(Post_Call
));
4195 pragma Assert
(Check_Number_Of_Actuals
(Call_Node
, Subp
));
4196 pragma Assert
(Check_BIP_Actuals
(Call_Node
, Subp
));
4201 Formal
:= First_Formal
(Subp
);
4202 Actual
:= First_Actual
(Call_Node
);
4204 while Present
(Formal
) loop
4205 -- Prepare to examine current entry
4209 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
4210 -- to expand it in a further round.
4212 CW_Interface_Formals_Present
:=
4213 CW_Interface_Formals_Present
4214 or else Is_Class_Wide_Interface_Type
(Etype
(Formal
));
4216 -- Create possible extra actual for constrained case. Usually, the
4217 -- extra actual is of the form actual'constrained, but since this
4218 -- attribute is only available for unconstrained records, TRUE is
4219 -- expanded if the type of the formal happens to be constrained (for
4220 -- instance when this procedure is inherited from an unconstrained
4221 -- record to a constrained one) or if the actual has no discriminant
4222 -- (its type is constrained). An exception to this is the case of a
4223 -- private type without discriminants. In this case we pass FALSE
4224 -- because the object has underlying discriminants with defaults.
4226 if Present
(Extra_Constrained
(Formal
)) then
4227 if Is_Mutably_Tagged_Type
(Etype
(Actual
))
4228 or else (Is_Private_Type
(Etype
(Prev
))
4229 and then not Has_Discriminants
4230 (Base_Type
(Etype
(Prev
))))
4233 (Expr
=> New_Occurrence_Of
(Standard_False
, Loc
),
4234 EF
=> Extra_Constrained
(Formal
));
4236 elsif Is_Constrained
(Etype
(Formal
))
4237 or else not Has_Discriminants
(Etype
(Prev
))
4240 (Expr
=> New_Occurrence_Of
(Standard_True
, Loc
),
4241 EF
=> Extra_Constrained
(Formal
));
4243 -- Do not produce extra actuals for Unchecked_Union parameters.
4244 -- Jump directly to the end of the loop.
4246 elsif Is_Unchecked_Union
(Base_Type
(Etype
(Actual
))) then
4247 goto Skip_Extra_Actual_Generation
;
4250 -- If the actual is a type conversion, then the constrained
4251 -- test applies to the actual, not the target type.
4257 -- Test for unchecked conversions as well, which can occur
4258 -- as out parameter actuals on calls to stream procedures.
4261 while Nkind
(Act_Prev
) in N_Type_Conversion
4262 | N_Unchecked_Type_Conversion
4264 Act_Prev
:= Expression
(Act_Prev
);
4267 -- If the expression is a conversion of a dereference, this
4268 -- is internally generated code that manipulates addresses,
4269 -- e.g. when building interface tables. No check should
4270 -- occur in this case, and the discriminated object is not
4271 -- directly at hand.
4273 if not Comes_From_Source
(Actual
)
4274 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
4275 and then Nkind
(Act_Prev
) = N_Explicit_Dereference
4278 (Expr
=> New_Occurrence_Of
(Standard_False
, Loc
),
4279 EF
=> Extra_Constrained
(Formal
));
4284 Make_Attribute_Reference
(Sloc
(Prev
),
4286 Duplicate_Subexpr_No_Checks
4287 (Act_Prev
, Name_Req
=> True),
4288 Attribute_Name
=> Name_Constrained
),
4289 EF
=> Extra_Constrained
(Formal
));
4295 -- Create possible extra actual for accessibility level
4297 if Present
(Extra_Accessibility
(Formal
)) then
4298 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
4299 -- accessibility levels.
4301 if Is_Thunk
(Current_Scope
) then
4303 Parm_Ent
: Entity_Id
;
4306 if Is_Controlling_Actual
(Actual
) then
4308 -- Find the corresponding actual of the thunk
4310 Parm_Ent
:= First_Entity
(Current_Scope
);
4311 for J
in 2 .. Param_Count
loop
4312 Next_Entity
(Parm_Ent
);
4315 -- Handle unchecked conversion of access types generated
4316 -- in thunks (cf. Expand_Interface_Thunk).
4318 elsif Is_Access_Type
(Etype
(Actual
))
4319 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
4321 Parm_Ent
:= Entity
(Expression
(Actual
));
4323 else pragma Assert
(Is_Entity_Name
(Actual
));
4324 Parm_Ent
:= Entity
(Actual
);
4328 (Expr
=> Accessibility_Level
4330 Level
=> Dynamic_Level
,
4331 Allow_Alt_Model
=> False),
4332 EF
=> Extra_Accessibility
(Formal
));
4335 -- Conditional expressions
4337 elsif Nkind
(Prev
) = N_Expression_With_Actions
4338 and then Nkind
(Original_Node
(Prev
)) in
4339 N_If_Expression | N_Case_Expression
4341 Add_Cond_Expression_Extra_Actual
(Formal
);
4343 -- Internal constant generated to remove side effects (normally
4344 -- from the expansion of dispatching calls).
4346 -- First verify the actual is internal
4348 elsif not Comes_From_Source
(Prev
)
4349 and then not Is_Rewrite_Substitution
(Prev
)
4351 -- Next check that the actual is a constant
4353 and then Nkind
(Prev
) = N_Identifier
4354 and then Ekind
(Entity
(Prev
)) = E_Constant
4355 and then Nkind
(Parent
(Entity
(Prev
))) = N_Object_Declaration
4357 -- Generate the accessibility level based on the expression in
4358 -- the constant's declaration.
4361 Ent
: Entity_Id
:= Entity
(Prev
);
4364 -- Handle deferred constants
4366 if Present
(Full_View
(Ent
)) then
4367 Ent
:= Full_View
(Ent
);
4371 (Expr
=> Accessibility_Level
4372 (Expr
=> Expression
(Parent
(Ent
)),
4373 Level
=> Dynamic_Level
,
4374 Allow_Alt_Model
=> False),
4375 EF
=> Extra_Accessibility
(Formal
));
4382 (Expr
=> Accessibility_Level
4384 Level
=> Dynamic_Level
,
4385 Allow_Alt_Model
=> False),
4386 EF
=> Extra_Accessibility
(Formal
));
4390 -- Perform the check of 4.6(49) that prevents a null value from being
4391 -- passed as an actual to an access parameter. Note that the check
4392 -- is elided in the common cases of passing an access attribute or
4393 -- access parameter as an actual. Also, we currently don't enforce
4394 -- this check for expander-generated actuals and when -gnatdj is set.
4396 if Ada_Version
>= Ada_2005
then
4398 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
4399 -- the intent of 6.4.1(13) is that null-exclusion checks should
4400 -- not be done for 'out' parameters, even though it refers only
4401 -- to constraint checks, and a null_exclusion is not a constraint.
4402 -- Note that AI05-0196-1 corrects this mistake in the RM.
4404 if Is_Access_Type
(Etype
(Formal
))
4405 and then Can_Never_Be_Null
(Etype
(Formal
))
4406 and then Ekind
(Formal
) /= E_Out_Parameter
4407 and then Nkind
(Prev
) /= N_Raise_Constraint_Error
4408 and then (Known_Null
(Prev
)
4409 or else not Can_Never_Be_Null
(Etype
(Prev
)))
4411 Install_Null_Excluding_Check
(Prev
);
4414 -- Ada_Version < Ada_2005
4417 if Ekind
(Etype
(Formal
)) /= E_Anonymous_Access_Type
4418 or else Access_Checks_Suppressed
(Subp
)
4422 elsif Debug_Flag_J
then
4425 elsif not Comes_From_Source
(Prev
) then
4428 elsif Is_Entity_Name
(Prev
)
4429 and then Ekind
(Etype
(Prev
)) = E_Anonymous_Access_Type
4433 elsif Nkind
(Prev
) in N_Allocator | N_Attribute_Reference
then
4437 Install_Null_Excluding_Check
(Prev
);
4441 -- Perform appropriate validity checks on parameters that
4444 if Validity_Checks_On
then
4445 if (Ekind
(Formal
) = E_In_Parameter
4446 and then Validity_Check_In_Params
)
4448 (Ekind
(Formal
) = E_In_Out_Parameter
4449 and then Validity_Check_In_Out_Params
)
4451 -- If the actual is an indexed component of a packed type (or
4452 -- is an indexed or selected component whose prefix recursively
4453 -- meets this condition), it has not been expanded yet. It will
4454 -- be copied in the validity code that follows, and has to be
4455 -- expanded appropriately, so reanalyze it.
4457 -- What we do is just to unset analyzed bits on prefixes till
4458 -- we reach something that does not have a prefix.
4465 while Nkind
(Nod
) in
4466 N_Indexed_Component | N_Selected_Component
4468 Set_Analyzed
(Nod
, False);
4469 Nod
:= Prefix
(Nod
);
4473 Ensure_Valid
(Actual
);
4477 -- For IN OUT and OUT parameters, ensure that subscripts are valid
4478 -- since this is a left side reference. We only do this for calls
4479 -- from the source program since we assume that compiler generated
4480 -- calls explicitly generate any required checks. We also need it
4481 -- only if we are doing standard validity checks, since clearly it is
4482 -- not needed if validity checks are off, and in subscript validity
4483 -- checking mode, all indexed components are checked with a call
4484 -- directly from Expand_N_Indexed_Component.
4486 if Comes_From_Source
(Call_Node
)
4487 and then Ekind
(Formal
) /= E_In_Parameter
4488 and then Validity_Checks_On
4489 and then Validity_Check_Default
4490 and then not Validity_Check_Subscripts
4492 Check_Valid_Lvalue_Subscripts
(Actual
);
4495 -- Mark any scalar OUT parameter that is a simple variable as no
4496 -- longer known to be valid (unless the type is always valid). This
4497 -- reflects the fact that if an OUT parameter is never set in a
4498 -- procedure, then it can become invalid on the procedure return.
4500 if Ekind
(Formal
) = E_Out_Parameter
4501 and then Is_Entity_Name
(Actual
)
4502 and then Ekind
(Entity
(Actual
)) = E_Variable
4503 and then not Is_Known_Valid
(Etype
(Actual
))
4505 Set_Is_Known_Valid
(Entity
(Actual
), False);
4508 -- For an OUT or IN OUT parameter, if the actual is an entity, then
4509 -- clear current values, since they can be clobbered. We are probably
4510 -- doing this in more places than we need to, but better safe than
4511 -- sorry when it comes to retaining bad current values.
4513 if Ekind
(Formal
) /= E_In_Parameter
4514 and then Is_Entity_Name
(Actual
)
4515 and then Present
(Entity
(Actual
))
4518 Ent
: constant Entity_Id
:= Entity
(Actual
);
4522 -- For an OUT or IN OUT parameter that is an assignable entity,
4523 -- we do not want to clobber the Last_Assignment field, since
4524 -- if it is set, it was precisely because it is indeed an OUT
4525 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
4526 -- since the subprogram could have returned in invalid value.
4528 if Is_Assignable
(Ent
) then
4529 Sav
:= Last_Assignment
(Ent
);
4530 Kill_Current_Values
(Ent
);
4531 Set_Last_Assignment
(Ent
, Sav
);
4532 Set_Is_Known_Valid
(Ent
, False);
4533 Set_Is_True_Constant
(Ent
, False);
4535 -- For all other cases, just kill the current values
4538 Kill_Current_Values
(Ent
);
4543 -- If the formal is class-wide and the actual is an aggregate, force
4544 -- evaluation so that the back end who does not know about class-wide
4545 -- type, does not generate a temporary of the wrong size.
4547 if not Is_Class_Wide_Type
(Etype
(Formal
)) then
4550 elsif Nkind
(Actual
) = N_Aggregate
4551 or else (Nkind
(Actual
) = N_Qualified_Expression
4552 and then Nkind
(Expression
(Actual
)) = N_Aggregate
)
4554 Force_Evaluation
(Actual
);
4557 -- In a remote call, if the formal is of a class-wide type, check
4558 -- that the actual meets the requirements described in E.4(18).
4560 if Remote
and then Is_Class_Wide_Type
(Etype
(Formal
)) then
4561 Insert_Action
(Actual
,
4562 Make_Transportable_Check
(Loc
,
4563 Duplicate_Subexpr_Move_Checks
(Actual
)));
4566 -- Perform invariant checks for all intermediate types in a view
4567 -- conversion after successful return from a call that passes the
4568 -- view conversion as an IN OUT or OUT parameter (RM 7.3.2 (12/3,
4569 -- 13/3, 14/3)). Consider only source conversion in order to avoid
4570 -- generating spurious checks on complex expansion such as object
4571 -- initialization through an extension aggregate.
4573 if Comes_From_Source
(Call_Node
)
4574 and then Ekind
(Formal
) /= E_In_Parameter
4575 and then Nkind
(Actual
) = N_Type_Conversion
4577 Add_View_Conversion_Invariants
(Formal
, Actual
);
4580 -- Generating C the initialization of an allocator is performed by
4581 -- means of individual statements, and hence it must be done before
4584 if Modify_Tree_For_C
4585 and then Nkind
(Actual
) = N_Allocator
4586 and then Nkind
(Expression
(Actual
)) = N_Qualified_Expression
4588 Remove_Side_Effects
(Actual
);
4591 -- This label is required when skipping extra actual generation for
4592 -- Unchecked_Union parameters.
4594 <<Skip_Extra_Actual_Generation
>>
4596 Param_Count
:= Param_Count
+ 1;
4597 Next_Actual
(Actual
);
4598 Next_Formal
(Formal
);
4601 -- If we are calling an Ada 2012 function which needs to have the
4602 -- "accessibility level determined by the point of call" (AI05-0234)
4603 -- passed in to it, then pass it in.
4605 if Ekind
(Subp
) in E_Function | E_Operator | E_Subprogram_Type
4607 Present
(Extra_Accessibility_Of_Result
(Ultimate_Alias
(Subp
)))
4610 Extra_Form
: Node_Id
:= Empty
;
4611 Level
: Node_Id
:= Empty
;
4614 -- Detect cases where the function call has been internally
4615 -- generated by examining the original node and return library
4616 -- level - taking care to avoid ignoring function calls expanded
4617 -- in prefix notation.
4619 if Nkind
(Original_Node
(Call_Node
)) not in N_Function_Call
4620 | N_Selected_Component
4621 | N_Indexed_Component
4623 Level
:= Make_Integer_Literal
4624 (Loc
, Scope_Depth
(Standard_Standard
));
4626 -- Otherwise get the level normally based on the call node
4629 Level
:= Accessibility_Level
4631 Level
=> Dynamic_Level
,
4632 Allow_Alt_Model
=> False);
4635 -- It may be possible that we are re-expanding an already
4636 -- expanded call when are are dealing with dispatching ???
4638 if No
(Parameter_Associations
(Call_Node
))
4639 or else Nkind
(Last
(Parameter_Associations
(Call_Node
)))
4640 /= N_Parameter_Association
4641 or else not Is_Accessibility_Actual
4642 (Last
(Parameter_Associations
(Call_Node
)))
4644 Extra_Form
:= Extra_Accessibility_Of_Result
4645 (Ultimate_Alias
(Subp
));
4654 -- If we are expanding the RHS of an assignment we need to check if tag
4655 -- propagation is needed. You might expect this processing to be in
4656 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
4657 -- assignment might be transformed to a declaration for an unconstrained
4658 -- value if the expression is classwide.
4660 if Nkind
(Call_Node
) = N_Function_Call
4661 and then Is_Tag_Indeterminate
(Call_Node
)
4662 and then Is_Entity_Name
(Name
(Call_Node
))
4665 Ass
: Node_Id
:= Empty
;
4668 if Nkind
(Parent
(Call_Node
)) = N_Assignment_Statement
then
4669 Ass
:= Parent
(Call_Node
);
4671 elsif Nkind
(Parent
(Call_Node
)) = N_Qualified_Expression
4672 and then Nkind
(Parent
(Parent
(Call_Node
))) =
4673 N_Assignment_Statement
4675 Ass
:= Parent
(Parent
(Call_Node
));
4677 elsif Nkind
(Parent
(Call_Node
)) = N_Explicit_Dereference
4678 and then Nkind
(Parent
(Parent
(Call_Node
))) =
4679 N_Assignment_Statement
4681 Ass
:= Parent
(Parent
(Call_Node
));
4685 and then Is_Class_Wide_Type
(Etype
(Name
(Ass
)))
4687 -- Move the error messages below to sem???
4689 if Is_Access_Type
(Etype
(Call_Node
)) then
4690 if Designated_Type
(Etype
(Call_Node
)) /=
4691 Root_Type
(Etype
(Name
(Ass
)))
4694 ("tag-indeterminate expression must have designated "
4695 & "type& (RM 5.2 (6))",
4696 Call_Node
, Root_Type
(Etype
(Name
(Ass
))));
4698 Propagate_Tag
(Name
(Ass
), Call_Node
);
4701 elsif Etype
(Call_Node
) /= Root_Type
(Etype
(Name
(Ass
))) then
4703 ("tag-indeterminate expression must have type & "
4705 Call_Node
, Root_Type
(Etype
(Name
(Ass
))));
4708 Propagate_Tag
(Name
(Ass
), Call_Node
);
4711 -- The call will be rewritten as a dispatching call, and
4712 -- expanded as such.
4719 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
4720 -- it to point to the correct secondary virtual table.
4722 if Nkind
(Call_Node
) in N_Subprogram_Call
4723 and then CW_Interface_Formals_Present
4725 Expand_Interface_Actuals
(Call_Node
);
4728 -- Install class-wide preconditions runtime check when this is a
4729 -- dispatching primitive that has or inherits class-wide preconditions;
4730 -- otherwise no runtime check is installed.
4732 if Nkind
(Call_Node
) in N_Subprogram_Call
4733 and then Is_Dispatching_Operation
(Subp
)
4735 Install_Class_Preconditions_Check
(Call_Node
);
4738 -- Deals with Dispatch_Call if we still have a call, before expanding
4739 -- extra actuals since this will be done on the re-analysis of the
4740 -- dispatching call. Note that we do not try to shorten the actual list
4741 -- for a dispatching call, it would not make sense to do so. Expansion
4742 -- of dispatching calls is suppressed for VM targets, because the VM
4743 -- back-ends directly handle the generation of dispatching calls and
4744 -- would have to undo any expansion to an indirect call.
4746 if Nkind
(Call_Node
) in N_Subprogram_Call
4747 and then Present
(Controlling_Argument
(Call_Node
))
4749 if Tagged_Type_Expansion
then
4750 Expand_Dispatching_Call
(Call_Node
);
4752 -- Expand_Dispatching_Call takes care of all the needed processing
4760 Call_Typ
: constant Entity_Id
:= Etype
(Call_Node
);
4761 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
4762 Eq_Prim_Op
: Entity_Id
:= Empty
;
4765 Prev_Call
: Node_Id
;
4768 Apply_Tag_Checks
(Call_Node
);
4770 if not Is_Limited_Type
(Typ
) then
4771 Eq_Prim_Op
:= Find_Prim_Op
(Typ
, Name_Op_Eq
);
4774 -- If this is a dispatching "=", we must first compare the
4775 -- tags so we generate: x.tag = y.tag and then x = y
4777 if Subp
= Eq_Prim_Op
then
4779 -- Mark the node as analyzed to avoid reanalyzing this
4780 -- dispatching call (which would cause a never-ending loop)
4782 Prev_Call
:= Relocate_Node
(Call_Node
);
4783 Set_Analyzed
(Prev_Call
);
4785 Param
:= First_Actual
(Call_Node
);
4791 Make_Selected_Component
(Loc
,
4792 Prefix
=> New_Value
(Param
),
4795 (First_Tag_Component
(Typ
), Loc
)),
4798 Make_Selected_Component
(Loc
,
4800 Unchecked_Convert_To
(Typ
,
4801 New_Value
(Next_Actual
(Param
))),
4804 (First_Tag_Component
(Typ
), Loc
))),
4805 Right_Opnd
=> Prev_Call
);
4807 Rewrite
(Call_Node
, New_Call
);
4809 (Call_Node
, Call_Typ
, Suppress
=> All_Checks
);
4812 -- Expansion of a dispatching call results in an indirect call,
4813 -- which in turn causes current values to be killed (see
4814 -- Resolve_Call), so on VM targets we do the call here to
4815 -- ensure consistent warnings between VM and non-VM targets.
4817 Kill_Current_Values
;
4819 -- If this is a dispatching "=" then we must update the reference
4820 -- to the call node because we generated:
4821 -- x.tag = y.tag and then x = y
4823 if Subp
= Eq_Prim_Op
then
4824 Call_Node
:= Right_Opnd
(Call_Node
);
4829 -- Similarly, expand calls to RCI subprograms on which pragma
4830 -- All_Calls_Remote applies. The rewriting will be reanalyzed
4831 -- later. Do this only when the call comes from source since we
4832 -- do not want such a rewriting to occur in expanded code.
4834 if Is_All_Remote_Call
(Call_Node
) then
4835 Expand_All_Calls_Remote_Subprogram_Call
(Call_Node
);
4837 -- Similarly, do not add extra actuals for an entry call whose entity
4838 -- is a protected procedure, or for an internal protected subprogram
4839 -- call, because it will be rewritten as a protected subprogram call
4840 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
4842 elsif Is_Protected_Type
(Scope
(Subp
))
4843 and then Ekind
(Subp
) in E_Procedure | E_Function
4847 -- During that loop we gathered the extra actuals (the ones that
4848 -- correspond to Extra_Formals), so now they can be appended.
4850 elsif Is_Non_Empty_List
(Extra_Actuals
) then
4852 Num_Extra_Actuals
: constant Nat
:= List_Length
(Extra_Actuals
);
4855 while Is_Non_Empty_List
(Extra_Actuals
) loop
4856 Add_Actual_Parameter
(Remove_Head
(Extra_Actuals
));
4859 -- Add dummy extra BIP actuals if we are calling a function that
4860 -- inherited the BIP extra actuals but does not require them.
4862 if Nkind
(Call_Node
) = N_Function_Call
4863 and then Is_Function_Call_With_BIP_Formals
(Call_Node
)
4864 and then not Is_Build_In_Place_Function_Call
(Call_Node
)
4866 Add_Dummy_Build_In_Place_Actuals
(Subp
,
4867 Num_Added_Extra_Actuals
=> Num_Extra_Actuals
);
4871 -- Add dummy extra BIP actuals if we are calling a function that
4872 -- inherited the BIP extra actuals but does not require them.
4874 elsif Nkind
(Call_Node
) = N_Function_Call
4875 and then Is_Function_Call_With_BIP_Formals
(Call_Node
)
4876 and then not Is_Build_In_Place_Function_Call
(Call_Node
)
4878 Add_Dummy_Build_In_Place_Actuals
(Subp
);
4881 -- At this point we have all the actuals, so this is the point at which
4882 -- the various expansion activities for actuals is carried out.
4884 Expand_Actuals
(Call_Node
, Subp
, Post_Call
);
4886 -- If it is a recursive call then call the internal procedure that
4887 -- verifies Subprogram_Variant contract (if present and enabled).
4888 -- Detecting calls to subprogram aliases is necessary for recursive
4889 -- calls in instances of generic subprograms, where the renaming of
4890 -- the current subprogram is called.
4892 if Is_Subprogram
(Subp
)
4893 and then not Is_Ignored_Ghost_Entity
(Subp
)
4894 and then Same_Or_Aliased_Subprograms
(Subp
, Current_Scope
)
4896 Check_Subprogram_Variant
;
4899 -- Verify that the actuals do not share storage. This check must be done
4900 -- on the caller side rather that inside the subprogram to avoid issues
4901 -- of parameter passing.
4903 if Check_Aliasing_Of_Parameters
then
4904 Apply_Parameter_Aliasing_Checks
(Call_Node
, Subp
);
4907 -- If the subprogram is a renaming, or if it is inherited, replace it in
4908 -- the call with the name of the actual subprogram being called. If this
4909 -- is a dispatching call, the run-time decides what to call. The Alias
4910 -- attribute does not apply to entries.
4912 if Nkind
(Call_Node
) /= N_Entry_Call_Statement
4913 and then No
(Controlling_Argument
(Call_Node
))
4914 and then Present
(Parent_Subp
)
4915 and then not Is_Direct_Deep_Call
(Subp
)
4917 if Present
(Inherited_From_Formal
(Subp
)) then
4918 Parent_Subp
:= Inherited_From_Formal
(Subp
);
4920 Parent_Subp
:= Ultimate_Alias
(Parent_Subp
);
4923 -- The below setting of Entity is suspect, see F109-018 discussion???
4925 Set_Entity
(Name
(Call_Node
), Parent_Subp
);
4927 -- Inspect all formals of derived subprogram Subp. Compare parameter
4928 -- types with the parent subprogram and check whether an actual may
4929 -- need a type conversion to the corresponding formal of the parent
4932 -- Not clear whether intrinsic subprograms need such conversions. ???
4934 if not Is_Intrinsic_Subprogram
(Parent_Subp
)
4935 or else Is_Generic_Instance
(Parent_Subp
)
4938 procedure Convert
(Act
: Node_Id
; Typ
: Entity_Id
);
4939 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
4940 -- and resolve the newly generated construct.
4946 procedure Convert
(Act
: Node_Id
; Typ
: Entity_Id
) is
4948 Rewrite
(Act
, OK_Convert_To
(Typ
, Act
));
4949 Analyze_And_Resolve
(Act
, Typ
);
4954 Actual_Typ
: Entity_Id
;
4955 Formal_Typ
: Entity_Id
;
4956 Parent_Typ
: Entity_Id
;
4959 Actual
:= First_Actual
(Call_Node
);
4960 Formal
:= First_Formal
(Subp
);
4961 Parent_Formal
:= First_Formal
(Parent_Subp
);
4962 while Present
(Formal
) loop
4963 Actual_Typ
:= Etype
(Actual
);
4964 Formal_Typ
:= Etype
(Formal
);
4965 Parent_Typ
:= Etype
(Parent_Formal
);
4967 -- For an IN parameter of a scalar type, the derived formal
4968 -- type and parent formal type differ, and the parent formal
4969 -- type and actual type do not match statically.
4971 if Is_Scalar_Type
(Formal_Typ
)
4972 and then Ekind
(Formal
) = E_In_Parameter
4973 and then Formal_Typ
/= Parent_Typ
4975 not Subtypes_Statically_Match
(Parent_Typ
, Actual_Typ
)
4976 and then not Raises_Constraint_Error
(Actual
)
4978 Convert
(Actual
, Parent_Typ
);
4980 -- For access types, the parent formal type and actual type
4983 elsif Is_Access_Type
(Formal_Typ
)
4984 and then Base_Type
(Parent_Typ
) /= Base_Type
(Actual_Typ
)
4986 if Ekind
(Formal
) /= E_In_Parameter
then
4987 Convert
(Actual
, Parent_Typ
);
4989 elsif Ekind
(Parent_Typ
) = E_Anonymous_Access_Type
4990 and then Designated_Type
(Parent_Typ
) /=
4991 Designated_Type
(Actual_Typ
)
4992 and then not Is_Controlling_Formal
(Formal
)
4994 -- This unchecked conversion is not necessary unless
4995 -- inlining is enabled, because in that case the type
4996 -- mismatch may become visible in the body about to be
5000 Unchecked_Convert_To
(Parent_Typ
, Actual
));
5001 Analyze_And_Resolve
(Actual
, Parent_Typ
);
5004 -- If there is a change of representation, then generate a
5005 -- warning, and do the change of representation.
5007 elsif not Has_Compatible_Representation
5008 (Target_Typ
=> Formal_Typ
,
5009 Operand_Typ
=> Parent_Typ
)
5012 ("??change of representation required", Actual
);
5013 Convert
(Actual
, Parent_Typ
);
5015 -- For array and record types, the parent formal type and
5016 -- derived formal type have different sizes or pragma Pack
5019 elsif ((Is_Array_Type
(Formal_Typ
)
5020 and then Is_Array_Type
(Parent_Typ
))
5022 (Is_Record_Type
(Formal_Typ
)
5023 and then Is_Record_Type
(Parent_Typ
)))
5024 and then Known_Esize
(Formal_Typ
)
5025 and then Known_Esize
(Parent_Typ
)
5027 (Esize
(Formal_Typ
) /= Esize
(Parent_Typ
)
5028 or else Has_Pragma_Pack
(Formal_Typ
) /=
5029 Has_Pragma_Pack
(Parent_Typ
))
5031 Convert
(Actual
, Parent_Typ
);
5034 Next_Actual
(Actual
);
5035 Next_Formal
(Formal
);
5036 Next_Formal
(Parent_Formal
);
5042 Subp
:= Parent_Subp
;
5045 -- Deal with case where call is an explicit dereference
5047 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
5049 -- Handle case of access to protected subprogram type
5051 if Is_Access_Protected_Subprogram_Type
5052 (Base_Type
(Etype
(Prefix
(Name
(Call_Node
)))))
5054 -- If this is a call through an access to protected operation, the
5055 -- prefix has the form (object'address, operation'access). Rewrite
5056 -- as a for other protected calls: the object is the 1st parameter
5057 -- of the list of actuals.
5064 Ptr
: constant Node_Id
:= Prefix
(Name
(Call_Node
));
5066 T
: constant Entity_Id
:=
5067 Equivalent_Type
(Base_Type
(Etype
(Ptr
)));
5069 D_T
: constant Entity_Id
:=
5070 Designated_Type
(Base_Type
(Etype
(Ptr
)));
5074 Make_Selected_Component
(Loc
,
5075 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
5077 New_Occurrence_Of
(First_Entity
(T
), Loc
));
5080 Make_Selected_Component
(Loc
,
5081 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
5083 New_Occurrence_Of
(Next_Entity
(First_Entity
(T
)), Loc
));
5086 Make_Explicit_Dereference
(Loc
,
5089 if Present
(Parameter_Associations
(Call_Node
)) then
5090 Parm
:= Parameter_Associations
(Call_Node
);
5095 Prepend
(Obj
, Parm
);
5097 if Etype
(D_T
) = Standard_Void_Type
then
5099 Make_Procedure_Call_Statement
(Loc
,
5101 Parameter_Associations
=> Parm
);
5104 Make_Function_Call
(Loc
,
5106 Parameter_Associations
=> Parm
);
5109 Set_First_Named_Actual
(Call
, First_Named_Actual
(Call_Node
));
5110 Set_Etype
(Call
, Etype
(D_T
));
5112 -- We do not re-analyze the call to avoid infinite recursion.
5113 -- We analyze separately the prefix and the object, and set
5114 -- the checks on the prefix that would otherwise be emitted
5115 -- when resolving a call.
5117 Rewrite
(Call_Node
, Call
);
5119 Apply_Access_Check
(Nam
);
5126 -- If this is a call to an intrinsic subprogram, then perform the
5127 -- appropriate expansion to the corresponding tree node and we
5128 -- are all done (since after that the call is gone).
5130 -- In the case where the intrinsic is to be processed by the back end,
5131 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
5132 -- since the idea in this case is to pass the call unchanged. If the
5133 -- intrinsic is an inherited unchecked conversion, and the derived type
5134 -- is the target type of the conversion, we must retain it as the return
5135 -- type of the expression. Otherwise the expansion below, which uses the
5136 -- parent operation, will yield the wrong type.
5138 if Is_Intrinsic_Subprogram
(Subp
) then
5139 Expand_Intrinsic_Call
(Call_Node
, Subp
);
5141 if Nkind
(Call_Node
) = N_Unchecked_Type_Conversion
5142 and then Parent_Subp
/= Orig_Subp
5143 and then Etype
(Parent_Subp
) /= Etype
(Orig_Subp
)
5145 Set_Etype
(Call_Node
, Etype
(Orig_Subp
));
5151 if Ekind
(Subp
) in E_Function | E_Procedure
then
5153 -- We perform a simple optimization on calls for To_Address by
5154 -- replacing them with an unchecked conversion. Not only is this
5155 -- efficient, but it also avoids order of elaboration problems when
5156 -- address clauses are inlined (address expression elaborated at the
5159 -- We perform this optimization regardless of whether we are in the
5160 -- main unit or in a unit in the context of the main unit, to ensure
5161 -- that the generated tree is the same in both cases, for CodePeer
5164 if Is_RTE
(Subp
, RE_To_Address
) then
5166 Unchecked_Convert_To
5167 (RTE
(RE_Address
), Relocate_Node
(First_Actual
(Call_Node
))));
5170 -- A call to a null procedure is replaced by a null statement, but we
5171 -- are not allowed to ignore possible side effects of the call, so we
5172 -- make sure that actuals are evaluated.
5173 -- We also suppress this optimization for GNATcoverage.
5175 elsif Is_Null_Procedure
(Subp
)
5176 and then not Opt
.Suppress_Control_Flow_Optimizations
5178 Actual
:= First_Actual
(Call_Node
);
5179 while Present
(Actual
) loop
5180 Remove_Side_Effects
(Actual
);
5181 Next_Actual
(Actual
);
5184 Rewrite
(Call_Node
, Make_Null_Statement
(Loc
));
5188 -- Handle inlining. No action needed if the subprogram is not inlined
5190 if not Is_Inlined
(Subp
) then
5193 -- Front-end inlining of expression functions (performed also when
5194 -- back-end inlining is enabled).
5196 elsif Is_Inlinable_Expression_Function
(Subp
) then
5198 (Call_Node
, New_Copy
(Expression_Of_Expression_Function
(Subp
)));
5199 Analyze
(Call_Node
);
5202 -- Handle front-end inlining
5204 elsif not Back_End_Inlining
then
5205 Inlined_Subprogram
: declare
5207 Must_Inline
: Boolean := False;
5208 Spec
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
5211 -- Verify that the body to inline has already been seen, and
5212 -- that if the body is in the current unit the inlining does
5213 -- not occur earlier. This avoids order-of-elaboration problems
5216 -- This should be documented in sinfo/einfo ???
5219 or else Nkind
(Spec
) /= N_Subprogram_Declaration
5220 or else No
(Body_To_Inline
(Spec
))
5222 Must_Inline
:= False;
5224 -- If this an inherited function that returns a private type,
5225 -- do not inline if the full view is an unconstrained array,
5226 -- because such calls cannot be inlined.
5228 elsif Present
(Orig_Subp
)
5229 and then Is_Array_Type
(Etype
(Orig_Subp
))
5230 and then not Is_Constrained
(Etype
(Orig_Subp
))
5232 Must_Inline
:= False;
5234 elsif In_Unfrozen_Instance
(Scope
(Subp
)) then
5235 Must_Inline
:= False;
5238 Bod
:= Body_To_Inline
(Spec
);
5240 if (In_Extended_Main_Code_Unit
(Call_Node
)
5241 or else In_Extended_Main_Code_Unit
(Parent
(Call_Node
))
5242 or else Has_Pragma_Inline_Always
(Subp
))
5243 and then (not In_Same_Extended_Unit
(Sloc
(Bod
), Loc
)
5245 Earlier_In_Extended_Unit
(Sloc
(Bod
), Loc
))
5247 Must_Inline
:= True;
5249 -- If we are compiling a package body that is not the main
5250 -- unit, it must be for inlining/instantiation purposes,
5251 -- in which case we inline the call to insure that the same
5252 -- temporaries are generated when compiling the body by
5253 -- itself. Otherwise link errors can occur.
5255 -- If the function being called is itself in the main unit,
5256 -- we cannot inline, because there is a risk of double
5257 -- elaboration and/or circularity: the inlining can make
5258 -- visible a private entity in the body of the main unit,
5259 -- that gigi will see before its sees its proper definition.
5261 elsif not In_Extended_Main_Code_Unit
(Call_Node
)
5262 and then In_Package_Body
5264 Must_Inline
:= not In_Extended_Main_Source_Unit
(Subp
);
5266 -- Inline calls to _Wrapped_Statements when generating C
5268 elsif Modify_Tree_For_C
5269 and then In_Same_Extended_Unit
(Sloc
(Bod
), Loc
)
5270 and then Chars
(Name
(Call_Node
))
5271 = Name_uWrapped_Statements
5273 Must_Inline
:= True;
5278 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
5281 -- Let the back end handle it
5283 Add_Inlined_Body
(Subp
, Call_Node
);
5285 if Front_End_Inlining
5286 and then Nkind
(Spec
) = N_Subprogram_Declaration
5287 and then In_Extended_Main_Code_Unit
(Call_Node
)
5288 and then No
(Body_To_Inline
(Spec
))
5289 and then not Has_Completion
(Subp
)
5290 and then In_Same_Extended_Unit
(Sloc
(Spec
), Loc
)
5293 ("cannot inline& (body not seen yet)?",
5297 end Inlined_Subprogram
;
5299 -- Front-end expansion of simple functions returning unconstrained
5300 -- types (see Check_And_Split_Unconstrained_Function). Note that the
5301 -- case of a simple renaming (Body_To_Inline in N_Entity below, see
5302 -- also Build_Renamed_Body) cannot be expanded here because this may
5303 -- give rise to order-of-elaboration issues for the types of the
5304 -- parameters of the subprogram, if any.
5306 elsif Present
(Unit_Declaration_Node
(Subp
))
5307 and then Nkind
(Unit_Declaration_Node
(Subp
)) =
5308 N_Subprogram_Declaration
5309 and then Present
(Body_To_Inline
(Unit_Declaration_Node
(Subp
)))
5311 Nkind
(Body_To_Inline
(Unit_Declaration_Node
(Subp
))) not in
5314 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
5316 -- Back-end inlining either if optimization is enabled, we're
5317 -- generating C, or the call is required to be inlined.
5319 elsif Optimization_Level
> 0
5321 or else Has_Pragma_Inline_Always
(Subp
)
5323 Add_Inlined_Body
(Subp
, Call_Node
);
5327 -- Check for protected subprogram. This is either an intra-object call,
5328 -- or a protected function call. Protected procedure calls are rewritten
5329 -- as entry calls and handled accordingly.
5331 -- In Ada 2005, this may be an indirect call to an access parameter that
5332 -- is an access_to_subprogram. In that case the anonymous type has a
5333 -- scope that is a protected operation, but the call is a regular one.
5334 -- In either case do not expand call if subprogram is eliminated.
5336 Scop
:= Scope
(Subp
);
5338 if Nkind
(Call_Node
) /= N_Entry_Call_Statement
5339 and then Is_Protected_Type
(Scop
)
5340 and then Ekind
(Subp
) /= E_Subprogram_Type
5341 and then not Is_Eliminated
(Subp
)
5343 -- If the call is an internal one, it is rewritten as a call to the
5344 -- corresponding unprotected subprogram.
5346 Expand_Protected_Subprogram_Call
(Call_Node
, Subp
, Scop
);
5349 -- Functions returning controlled objects need special attention. If
5350 -- the return type is limited, then the context is initialization and
5351 -- different processing applies. If the call is to a protected function,
5352 -- the expansion above will call Expand_Call recursively. Otherwise the
5353 -- function call is transformed into a reference to the result that has
5354 -- been built either on the primary or the secondary stack.
5356 if Needs_Finalization
(Etype
(Subp
)) then
5357 if not Is_Build_In_Place_Function_Call
(Call_Node
)
5359 (No
(First_Formal
(Subp
))
5361 not Is_Concurrent_Record_Type
(Etype
(First_Formal
(Subp
))))
5363 Expand_Ctrl_Function_Call
5364 (Call_Node
, Needs_Secondary_Stack
(Etype
(Subp
)));
5366 -- Build-in-place function calls which appear in anonymous contexts
5367 -- need a transient scope to ensure the proper finalization of the
5368 -- intermediate result after its use.
5370 elsif Is_Build_In_Place_Function_Call
(Call_Node
)
5371 and then Nkind
(Parent
(Unqual_Conv
(Call_Node
))) in
5372 N_Attribute_Reference
5374 | N_Indexed_Component
5375 | N_Object_Renaming_Declaration
5376 | N_Procedure_Call_Statement
5377 | N_Selected_Component
5380 (Ekind
(Current_Scope
) /= E_Loop
5381 or else Nkind
(Parent
(Call_Node
)) /= N_Function_Call
5383 Is_Build_In_Place_Function_Call
(Parent
(Call_Node
)))
5385 Establish_Transient_Scope
5386 (Call_Node
, Needs_Secondary_Stack
(Etype
(Subp
)));
5389 end Expand_Call_Helper
;
5391 -------------------------------
5392 -- Expand_Ctrl_Function_Call --
5393 -------------------------------
5395 procedure Expand_Ctrl_Function_Call
(N
: Node_Id
; Use_Sec_Stack
: Boolean)
5397 Par
: constant Node_Id
:= Parent
(N
);
5400 -- Optimization: if the returned value is returned again, then no need
5401 -- to copy/readjust/finalize, we can just pass the value through (see
5402 -- Expand_N_Simple_Return_Statement), and thus no attachment is needed.
5403 -- Note that simple return statements are distributed into conditional
5404 -- expressions but we may be invoked before this distribution is done.
5406 if Nkind
(Par
) = N_Simple_Return_Statement
5407 or else (Nkind
(Par
) = N_If_Expression
5408 and then Nkind
(Parent
(Par
)) = N_Simple_Return_Statement
)
5409 or else (Nkind
(Par
) = N_Case_Expression_Alternative
5411 Nkind
(Parent
(Parent
(Par
))) = N_Simple_Return_Statement
)
5416 -- Another optimization: if the returned value is used to initialize an
5417 -- object, then no need to copy/readjust/finalize, we can initialize it
5418 -- in place. However, if the call returns on the secondary stack, then
5419 -- we need the expansion because we'll be renaming the temporary as the
5420 -- (permanent) object. We also apply it in the case of the expression of
5421 -- a delta aggregate, since it is used only to initialize a temporary.
5423 if Nkind
(Par
) in N_Object_Declaration | N_Delta_Aggregate
5424 and then Expression
(Par
) = N
5425 and then not Use_Sec_Stack
5430 -- Resolution is now finished, make sure we don't start analysis again
5431 -- because of the duplication.
5435 -- Apply the transformation unless it was already applied earlier. This
5436 -- may happen because Remove_Side_Effects can be called during semantic
5437 -- analysis, for example from Build_Actual_Subtype_Of_Component. It is
5438 -- crucial to avoid creating a reference of reference here, because it
5439 -- would not be subsequently recognized by the Is_Finalizable_Transient
5440 -- and Requires_Cleanup_Actions predicates.
5442 if Nkind
(Par
) /= N_Reference
then
5443 Remove_Side_Effects
(N
);
5445 end Expand_Ctrl_Function_Call
;
5447 ----------------------------------------
5448 -- Expand_N_Extended_Return_Statement --
5449 ----------------------------------------
5451 -- If there is a Handled_Statement_Sequence, we rewrite this:
5453 -- return Result : T := <expression> do
5454 -- <handled_seq_of_stms>
5460 -- Result : T := <expression>;
5462 -- <handled_seq_of_stms>
5466 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
5468 -- return Result : T := <expression>;
5472 -- return <expression>;
5474 -- unless it's build-in-place or there's no <expression>, in which case
5478 -- Result : T := <expression>;
5483 -- Note that this case could have been written by the user as an extended
5484 -- return statement, or could have been transformed to this from a simple
5485 -- return statement.
5487 -- That is, we need to have a reified return object if there are statements
5488 -- (which might refer to it) or if we're doing build-in-place (so we can
5489 -- set its address to the final resting place or if there is no expression
5490 -- (in which case default initial values might need to be set)).
5492 procedure Expand_N_Extended_Return_Statement
(N
: Node_Id
) is
5493 Loc
: constant Source_Ptr
:= Sloc
(N
);
5494 Func_Id
: constant Entity_Id
:=
5495 Return_Applies_To
(Return_Statement_Entity
(N
));
5496 Is_BIP_Func
: constant Boolean :=
5497 Is_Build_In_Place_Function
(Func_Id
);
5498 Ret_Obj_Id
: constant Entity_Id
:=
5499 First_Entity
(Return_Statement_Entity
(N
));
5500 Ret_Obj_Decl
: constant Node_Id
:= Parent
(Ret_Obj_Id
);
5501 Ret_Typ
: constant Entity_Id
:= Etype
(Func_Id
);
5503 function Move_Activation_Chain
(Func_Id
: Entity_Id
) return Node_Id
;
5504 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
5506 -- From current activation chain
5507 -- To activation chain passed in by the caller
5508 -- New_Master master passed in by the caller
5510 -- Func_Id is the entity of the function where the extended return
5511 -- statement appears.
5513 ---------------------------
5514 -- Move_Activation_Chain --
5515 ---------------------------
5517 function Move_Activation_Chain
(Func_Id
: Entity_Id
) return Node_Id
is
5520 Make_Procedure_Call_Statement
(Loc
,
5522 New_Occurrence_Of
(RTE
(RE_Move_Activation_Chain
), Loc
),
5524 Parameter_Associations
=> New_List
(
5528 Make_Attribute_Reference
(Loc
,
5529 Prefix
=> Make_Identifier
(Loc
, Name_uChain
),
5530 Attribute_Name
=> Name_Unrestricted_Access
),
5532 -- Destination chain
5535 (Build_In_Place_Formal
(Func_Id
, BIP_Activation_Chain
), Loc
),
5540 (Build_In_Place_Formal
(Func_Id
, BIP_Task_Master
), Loc
)));
5541 end Move_Activation_Chain
;
5548 Stmts
: List_Id
:= No_List
;
5550 Return_Stmt
: Node_Id
:= Empty
;
5551 -- Force initialization to facilitate static analysis
5553 -- Start of processing for Expand_N_Extended_Return_Statement
5556 -- Given that functionality of interface thunks is simple (just displace
5557 -- the pointer to the object) they are always handled by means of
5558 -- simple return statements.
5560 pragma Assert
(not Is_Thunk
(Current_Subprogram
));
5562 if Nkind
(Ret_Obj_Decl
) = N_Object_Declaration
then
5563 Exp
:= Expression
(Ret_Obj_Decl
);
5565 -- Assert that if F says "return R : T := G(...) do..."
5566 -- then F and G are both b-i-p, or neither b-i-p.
5568 if Present
(Exp
) and then Nkind
(Exp
) = N_Function_Call
then
5569 pragma Assert
(Ekind
(Current_Subprogram
) = E_Function
);
5571 (Is_Build_In_Place_Function
(Current_Subprogram
) =
5572 Is_Build_In_Place_Function_Call
(Exp
));
5580 HSS
:= Handled_Statement_Sequence
(N
);
5582 -- Build a simple_return_statement that returns the return object when
5583 -- there is a statement sequence, or no expression, or the analysis of
5584 -- the return object declaration generated extra actions, or the result
5585 -- will be built in place. Note however that we currently do this for
5586 -- all composite cases, even though they are not built in place.
5590 or else List_Length
(Return_Object_Declarations
(N
)) > 1
5591 or else Is_Composite_Type
(Ret_Typ
)
5596 -- If the extended return has a handled statement sequence, then wrap
5597 -- it in a block and use the block as the first statement.
5601 Make_Block_Statement
(Loc
,
5602 Declarations
=> New_List
,
5603 Handled_Statement_Sequence
=> HSS
));
5606 -- If the result type contains tasks, we call Move_Activation_Chain.
5607 -- Later, the cleanup code will call Complete_Master, which will
5608 -- terminate any unactivated tasks belonging to the return statement
5609 -- master. But Move_Activation_Chain updates their master to be that
5610 -- of the caller, so they will not be terminated unless the return
5611 -- statement completes unsuccessfully due to exception, abort, goto,
5612 -- or exit. As a formality, we test whether the function requires the
5613 -- result to be built in place, though that's necessarily true for
5614 -- the case of result types with task parts.
5616 if Is_BIP_Func
and then Has_Task
(Ret_Typ
) then
5618 -- The return expression is an aggregate for a complex type which
5619 -- contains tasks. This particular case is left unexpanded since
5620 -- the regular expansion would insert all temporaries and
5621 -- initialization code in the wrong block.
5623 if Nkind
(Exp
) = N_Aggregate
then
5624 Expand_N_Aggregate
(Exp
);
5627 -- Do not move the activation chain if the return object does not
5630 if Has_Task
(Etype
(Ret_Obj_Id
)) then
5631 Append_To
(Stmts
, Move_Activation_Chain
(Func_Id
));
5635 -- If the returned object needs finalization actions, the function
5636 -- must perform the appropriate cleanup should it fail to return.
5638 if Needs_Finalization
(Etype
(Ret_Obj_Id
)) then
5640 (Stmts
, Make_Suppress_Object_Finalize_Call
(Loc
, Ret_Obj_Id
));
5643 HSS
:= Make_Handled_Sequence_Of_Statements
(Loc
, Stmts
);
5646 -- Case where we build a return statement block
5648 if Present
(HSS
) then
5650 Make_Block_Statement
(Loc
,
5651 Declarations
=> Return_Object_Declarations
(N
),
5652 Handled_Statement_Sequence
=> HSS
);
5654 -- We set the entity of the new block statement to be that of the
5655 -- return statement. This is necessary so that various fields, such
5656 -- as Finalization_Chain_Entity carry over from the return statement
5657 -- to the block. Note that this block is unusual, in that its entity
5658 -- is an E_Return_Statement rather than an E_Block.
5661 (Result
, New_Occurrence_Of
(Return_Statement_Entity
(N
), Loc
));
5663 -- Build a simple_return_statement that returns the return object
5666 Make_Simple_Return_Statement
(Loc
,
5667 Expression
=> New_Occurrence_Of
(Ret_Obj_Id
, Loc
));
5668 Append_To
(Stmts
, Return_Stmt
);
5670 -- Case where we do not need to build a block. But we're about to drop
5671 -- Return_Object_Declarations on the floor, so assert that it contains
5672 -- only the return object declaration.
5674 else pragma Assert
(List_Length
(Return_Object_Declarations
(N
)) = 1);
5676 -- Build simple_return_statement that returns the expression directly
5678 Return_Stmt
:= Make_Simple_Return_Statement
(Loc
, Expression
=> Exp
);
5679 Result
:= Return_Stmt
;
5682 -- Set the flag to prevent infinite recursion
5684 Set_Comes_From_Extended_Return_Statement
(Return_Stmt
);
5685 Set_Return_Statement
(Ret_Obj_Id
, Return_Stmt
);
5687 Rewrite
(N
, Result
);
5689 -- AI12-043: The checks of 6.5(8.1/3) and 6.5(21/3) are made immediately
5690 -- before an object is returned. A predicate that applies to the return
5691 -- subtype is checked immediately before an object is returned.
5694 end Expand_N_Extended_Return_Statement
;
5696 ----------------------------
5697 -- Expand_N_Function_Call --
5698 ----------------------------
5700 procedure Expand_N_Function_Call
(N
: Node_Id
) is
5703 end Expand_N_Function_Call
;
5705 ---------------------------------------
5706 -- Expand_N_Procedure_Call_Statement --
5707 ---------------------------------------
5709 procedure Expand_N_Procedure_Call_Statement
(N
: Node_Id
) is
5712 end Expand_N_Procedure_Call_Statement
;
5714 ------------------------------------
5715 -- Expand_N_Return_When_Statement --
5716 ------------------------------------
5718 procedure Expand_N_Return_When_Statement
(N
: Node_Id
) is
5719 Loc
: constant Source_Ptr
:= Sloc
(N
);
5722 Make_If_Statement
(Loc
,
5723 Condition
=> Condition
(N
),
5724 Then_Statements
=> New_List
(
5725 Make_Simple_Return_Statement
(Loc
,
5726 Expression
=> Expression
(N
)))));
5729 end Expand_N_Return_When_Statement
;
5731 --------------------------------------
5732 -- Expand_N_Simple_Return_Statement --
5733 --------------------------------------
5735 procedure Expand_N_Simple_Return_Statement
(N
: Node_Id
) is
5737 -- Defend against previous errors (i.e. the return statement calls a
5738 -- function that is not available in configurable runtime).
5740 if Present
(Expression
(N
))
5741 and then Nkind
(Expression
(N
)) = N_Empty
5743 Check_Error_Detected
;
5747 -- Distinguish the function and non-function cases:
5749 case Ekind
(Return_Applies_To
(Return_Statement_Entity
(N
))) is
5751 | E_Generic_Function
5753 Expand_Simple_Function_Return
(N
);
5757 | E_Generic_Procedure
5759 | E_Return_Statement
5761 Expand_Non_Function_Return
(N
);
5764 raise Program_Error
;
5768 when RE_Not_Available
=>
5770 end Expand_N_Simple_Return_Statement
;
5772 ------------------------------
5773 -- Expand_N_Subprogram_Body --
5774 ------------------------------
5776 -- Add dummy push/pop label nodes at start and end to clear any local
5777 -- exception indications if local-exception-to-goto optimization is active.
5779 -- Add return statement if last statement in body is not a return statement
5780 -- (this makes things easier on Gigi which does not want to have to handle
5781 -- a missing return).
5783 -- Add call to Activate_Tasks if body is a task activator
5785 -- Deal with possible detection of infinite recursion
5787 -- Eliminate body completely if convention stubbed
5789 -- Encode entity names within body, since we will not need to reference
5790 -- these entities any longer in the front end.
5792 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5794 -- Reset Pure indication if any parameter has root type System.Address
5795 -- or has any parameters of limited types, where limited means that the
5796 -- run-time view is limited (i.e. the full type is limited).
5800 procedure Expand_N_Subprogram_Body
(N
: Node_Id
) is
5801 Body_Id
: constant Entity_Id
:= Defining_Entity
(N
);
5802 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
5803 Loc
: constant Source_Ptr
:= Sloc
(N
);
5805 procedure Add_Return
(Spec_Id
: Entity_Id
; Stmts
: List_Id
);
5806 -- Append a return statement to the statement sequence Stmts if the last
5807 -- statement is not already a return or a goto statement. Note that the
5808 -- latter test is not critical, it does not matter if we add a few extra
5809 -- returns, since they get eliminated anyway later on. Spec_Id denotes
5810 -- the corresponding spec of the subprogram body.
5816 procedure Add_Return
(Spec_Id
: Entity_Id
; Stmts
: List_Id
) is
5817 Last_Stmt
: Node_Id
;
5822 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5823 -- not relevant in this context since they are not executable.
5825 Last_Stmt
:= Last
(Stmts
);
5826 while Nkind
(Last_Stmt
) in N_Pop_xxx_Label
loop
5830 -- Now insert return unless last statement is a transfer
5832 if not Is_Transfer
(Last_Stmt
) then
5834 -- The source location for the return is the end label of the
5835 -- procedure if present. Otherwise use the sloc of the last
5836 -- statement in the list. If the list comes from a generated
5837 -- exception handler and we are not debugging generated code,
5838 -- all the statements within the handler are made invisible
5841 if Nkind
(Parent
(Stmts
)) = N_Exception_Handler
5842 and then not Comes_From_Source
(Parent
(Stmts
))
5844 Loc
:= Sloc
(Last_Stmt
);
5845 elsif Present
(End_Label
(HSS
)) then
5846 Loc
:= Sloc
(End_Label
(HSS
));
5848 Loc
:= Sloc
(Last_Stmt
);
5851 -- Append return statement, and set analyzed manually. We can't
5852 -- call Analyze on this return since the scope is wrong.
5854 -- Note: it almost works to push the scope and then do the Analyze
5855 -- call, but something goes wrong in some weird cases and it is
5856 -- not worth worrying about ???
5858 Stmt
:= Make_Simple_Return_Statement
(Loc
);
5860 -- The return statement is handled properly, and the call to the
5861 -- postcondition, inserted below, does not require information
5862 -- from the body either. However, that call is analyzed in the
5863 -- enclosing scope, and an elaboration check might improperly be
5864 -- added to it. A guard in Sem_Elab is needed to prevent that
5865 -- spurious check, see Check_Elab_Call.
5867 Append_To
(Stmts
, Stmt
);
5868 Set_Analyzed
(Stmt
);
5870 -- Ada 2022 (AI12-0279): append the call to 'Yield unless this is
5871 -- a generic subprogram (since in such case it will be added to
5872 -- the instantiations).
5874 if Has_Yield_Aspect
(Spec_Id
)
5875 and then Ekind
(Spec_Id
) /= E_Generic_Procedure
5876 and then RTE_Available
(RE_Yield
)
5878 Insert_Action
(Stmt
,
5879 Make_Procedure_Call_Statement
(Loc
,
5880 New_Occurrence_Of
(RTE
(RE_Yield
), Loc
)));
5889 Spec_Id
: Entity_Id
;
5891 -- Start of processing for Expand_N_Subprogram_Body
5894 if Present
(Corresponding_Spec
(N
)) then
5895 Spec_Id
:= Corresponding_Spec
(N
);
5900 -- If this is a Pure function which has any parameters whose root type
5901 -- is System.Address, reset the Pure indication.
5902 -- This check is also performed when the subprogram is frozen, but we
5903 -- repeat it on the body so that the indication is consistent, and so
5904 -- it applies as well to bodies without separate specifications.
5906 if Is_Pure
(Spec_Id
)
5907 and then Is_Subprogram
(Spec_Id
)
5908 and then not Has_Pragma_Pure_Function
(Spec_Id
)
5910 Check_Function_With_Address_Parameter
(Spec_Id
);
5912 if Spec_Id
/= Body_Id
then
5913 Set_Is_Pure
(Body_Id
, Is_Pure
(Spec_Id
));
5917 -- Set L to either the list of declarations if present, or to the list
5918 -- of statements if no declarations are present. This is used to insert
5919 -- new stuff at the start.
5921 if Is_Non_Empty_List
(Declarations
(N
)) then
5922 L
:= Declarations
(N
);
5924 L
:= Statements
(HSS
);
5927 -- If local-exception-to-goto optimization active, insert dummy push
5928 -- statements at start, and dummy pop statements at end, but inhibit
5929 -- this if we have No_Exception_Handlers or expanding a entry barrier
5930 -- function, since they are useless and interfere with analysis (e.g. by
5931 -- CodePeer) and other optimizations. We also don't need these if we're
5932 -- unnesting subprograms because the only purpose of these nodes is to
5933 -- ensure we don't set a label in one subprogram and branch to it in
5936 if (Debug_Flag_Dot_G
5937 or else Restriction_Active
(No_Exception_Propagation
))
5938 and then not Restriction_Active
(No_Exception_Handlers
)
5939 and then not CodePeer_Mode
5940 and then not Is_Entry_Barrier_Function
(N
)
5941 and then not Unnest_Subprogram_Mode
5942 and then Is_Non_Empty_List
(L
)
5945 FS
: constant Node_Id
:= First
(L
);
5946 FL
: constant Source_Ptr
:= Sloc
(FS
);
5951 -- LS points to either last statement, if statements are present
5952 -- or to the last declaration if there are no statements present.
5953 -- It is the node after which the pop's are generated.
5955 if Is_Non_Empty_List
(Statements
(HSS
)) then
5956 LS
:= Last
(Statements
(HSS
));
5963 Insert_List_Before_And_Analyze
(FS
, New_List
(
5964 Make_Push_Constraint_Error_Label
(FL
),
5965 Make_Push_Program_Error_Label
(FL
),
5966 Make_Push_Storage_Error_Label
(FL
)));
5968 Insert_List_After_And_Analyze
(LS
, New_List
(
5969 Make_Pop_Constraint_Error_Label
(LL
),
5970 Make_Pop_Program_Error_Label
(LL
),
5971 Make_Pop_Storage_Error_Label
(LL
)));
5975 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5977 if Init_Or_Norm_Scalars
and then Is_Subprogram
(Spec_Id
) then
5983 -- Loop through formals
5985 F
:= First_Formal
(Spec_Id
);
5986 while Present
(F
) loop
5987 if Is_Scalar_Type
(Etype
(F
))
5988 and then Ekind
(F
) = E_Out_Parameter
5990 Check_Restriction
(No_Default_Initialization
, F
);
5992 -- Insert the initialization. We turn off validity checks
5993 -- for this assignment, since we do not want any check on
5994 -- the initial value itself (which may well be invalid).
5995 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5998 Make_Assignment_Statement
(Loc
,
5999 Name
=> New_Occurrence_Of
(F
, Loc
),
6000 Expression
=> Get_Simple_Init_Val
(Etype
(F
), N
));
6001 Set_Suppress_Assignment_Checks
(A
);
6003 Insert_Before_And_Analyze
(First
(L
),
6004 A
, Suppress
=> Validity_Check
);
6012 -- Clear out statement list for stubbed procedure
6014 if Present
(Corresponding_Spec
(N
)) then
6015 Set_Elaboration_Flag
(N
, Spec_Id
);
6017 if Convention
(Spec_Id
) = Convention_Stubbed
6018 or else Is_Eliminated
(Spec_Id
)
6020 Set_Declarations
(N
, Empty_List
);
6021 Set_Handled_Statement_Sequence
(N
,
6022 Make_Handled_Sequence_Of_Statements
(Loc
,
6023 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
6029 -- Create a set of discriminals for the next protected subprogram body
6031 if Is_List_Member
(N
)
6032 and then Present
(Parent
(List_Containing
(N
)))
6033 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
6034 and then Present
(Next_Protected_Operation
(N
))
6036 Set_Discriminals
(Parent
(Base_Type
(Scope
(Spec_Id
))));
6039 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
6040 -- subprograms with no specs are not frozen.
6042 Compute_Returns_By_Ref
(Spec_Id
);
6044 -- For a procedure, we add a return for all possible syntactic ends of
6047 if Ekind
(Spec_Id
) in E_Procedure | E_Generic_Procedure
then
6048 Add_Return
(Spec_Id
, Statements
(HSS
));
6050 if Present
(Exception_Handlers
(HSS
)) then
6051 Except_H
:= First_Non_Pragma
(Exception_Handlers
(HSS
));
6052 while Present
(Except_H
) loop
6053 Add_Return
(Spec_Id
, Statements
(Except_H
));
6054 Next_Non_Pragma
(Except_H
);
6058 -- For a function, we must deal with the case where there is at least
6059 -- one missing return. What we do is to wrap the entire body of the
6060 -- function in a block:
6073 -- raise Program_Error;
6076 -- This approach is necessary because the raise must be signalled to the
6077 -- caller, not handled by any local handler (RM 6.4(11)).
6079 -- Note: we do not need to analyze the constructed sequence here, since
6080 -- it has no handler, and an attempt to analyze the handled statement
6081 -- sequence twice is risky in various ways (e.g. the issue of expanding
6082 -- cleanup actions twice).
6084 elsif Has_Missing_Return
(Spec_Id
) then
6086 Hloc
: constant Source_Ptr
:= Sloc
(HSS
);
6087 Blok
: constant Node_Id
:=
6088 Make_Block_Statement
(Hloc
,
6089 Handled_Statement_Sequence
=> HSS
);
6090 Rais
: constant Node_Id
:=
6091 Make_Raise_Program_Error
(Hloc
,
6092 Reason
=> PE_Missing_Return
);
6095 Set_Handled_Statement_Sequence
(N
,
6096 Make_Handled_Sequence_Of_Statements
(Hloc
,
6097 Statements
=> New_List
(Blok
, Rais
)));
6099 Push_Scope
(Spec_Id
);
6106 -- If subprogram contains a parameterless recursive call, then we may
6107 -- have an infinite recursion, so see if we can generate code to check
6108 -- for this possibility if storage checks are not suppressed.
6110 if Ekind
(Spec_Id
) = E_Procedure
6111 and then Has_Recursive_Call
(Spec_Id
)
6112 and then not Storage_Checks_Suppressed
(Spec_Id
)
6114 Detect_Infinite_Recursion
(N
, Spec_Id
);
6117 -- Set to encode entity names in package body before gigi is called
6119 Qualify_Entity_Names
(N
);
6121 -- If the body belongs to a nonabstract library-level source primitive
6122 -- of a tagged type, install an elaboration check which ensures that a
6123 -- dispatching call targeting the primitive will not execute the body
6124 -- without it being previously elaborated.
6126 Install_Primitive_Elaboration_Check
(N
);
6127 end Expand_N_Subprogram_Body
;
6129 -----------------------------------
6130 -- Expand_N_Subprogram_Body_Stub --
6131 -----------------------------------
6133 procedure Expand_N_Subprogram_Body_Stub
(N
: Node_Id
) is
6137 if Present
(Corresponding_Body
(N
)) then
6138 Bod
:= Unit_Declaration_Node
(Corresponding_Body
(N
));
6140 -- The body may have been expanded already when it is analyzed
6141 -- through the subunit node. Do no expand again: it interferes
6142 -- with the construction of unnesting tables when generating C.
6144 if not Analyzed
(Bod
) then
6145 Expand_N_Subprogram_Body
(Bod
);
6148 -- Add full qualification to entities that may be created late
6149 -- during unnesting.
6151 Qualify_Entity_Names
(N
);
6153 end Expand_N_Subprogram_Body_Stub
;
6155 -------------------------------------
6156 -- Expand_N_Subprogram_Declaration --
6157 -------------------------------------
6159 -- If the declaration appears within a protected body, it is a private
6160 -- operation of the protected type. We must create the corresponding
6161 -- protected subprogram an associated formals. For a normal protected
6162 -- operation, this is done when expanding the protected type declaration.
6164 -- If the declaration is for a null procedure, emit null body
6166 procedure Expand_N_Subprogram_Declaration
(N
: Node_Id
) is
6167 Loc
: constant Source_Ptr
:= Sloc
(N
);
6168 Subp
: constant Entity_Id
:= Defining_Entity
(N
);
6172 Scop
: constant Entity_Id
:= Scope
(Subp
);
6174 Prot_Decl
: Node_Id
;
6175 Prot_Id
: Entity_Id
;
6179 -- Deal with case of protected subprogram. Do not generate protected
6180 -- operation if operation is flagged as eliminated.
6182 if Is_List_Member
(N
)
6183 and then Present
(Parent
(List_Containing
(N
)))
6184 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
6185 and then Is_Protected_Type
(Scop
)
6187 if No
(Protected_Body_Subprogram
(Subp
))
6188 and then not Is_Eliminated
(Subp
)
6191 Make_Subprogram_Declaration
(Loc
,
6193 Build_Protected_Sub_Specification
6194 (N
, Scop
, Unprotected_Mode
));
6196 -- The protected subprogram is declared outside of the protected
6197 -- body. Given that the body has frozen all entities so far, we
6198 -- analyze the subprogram and perform freezing actions explicitly.
6199 -- including the generation of an explicit freeze node, to ensure
6200 -- that gigi has the proper order of elaboration.
6201 -- If the body is a subunit, the insertion point is before the
6202 -- stub in the parent.
6204 Prot_Bod
:= Parent
(List_Containing
(N
));
6206 if Nkind
(Parent
(Prot_Bod
)) = N_Subunit
then
6207 Prot_Bod
:= Corresponding_Stub
(Parent
(Prot_Bod
));
6210 Insert_Before
(Prot_Bod
, Prot_Decl
);
6211 Prot_Id
:= Defining_Unit_Name
(Specification
(Prot_Decl
));
6212 Set_Has_Delayed_Freeze
(Prot_Id
);
6214 Push_Scope
(Scope
(Scop
));
6215 Analyze
(Prot_Decl
);
6216 Freeze_Before
(N
, Prot_Id
);
6217 Set_Protected_Body_Subprogram
(Subp
, Prot_Id
);
6221 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
6222 -- cases this is superfluous because calls to it will be automatically
6223 -- inlined, but we definitely need the body if preconditions for the
6224 -- procedure are present, or if performing coverage analysis.
6226 elsif Nkind
(Specification
(N
)) = N_Procedure_Specification
6227 and then Null_Present
(Specification
(N
))
6230 Bod
: constant Node_Id
:= Body_To_Inline
(N
);
6233 Set_Has_Completion
(Subp
, False);
6234 Append_Freeze_Action
(Subp
, Bod
);
6236 -- The body now contains raise statements, so calls to it will
6239 Set_Is_Inlined
(Subp
, False);
6243 -- When generating C code, transform a function that returns a
6244 -- constrained array type into a procedure with an out parameter
6245 -- that carries the return value.
6247 -- We skip this transformation for unchecked conversions, since they
6248 -- are not needed by the C generator (and this also produces cleaner
6251 Typ
:= Get_Fullest_View
(Etype
(Subp
));
6253 if Transform_Function_Array
6254 and then Nkind
(Specification
(N
)) = N_Function_Specification
6255 and then Is_Array_Type
(Typ
)
6256 and then Is_Constrained
(Typ
)
6257 and then not Is_Unchecked_Conversion_Instance
(Subp
)
6259 Build_Procedure_Form
(N
);
6261 end Expand_N_Subprogram_Declaration
;
6263 --------------------------------
6264 -- Expand_Non_Function_Return --
6265 --------------------------------
6267 procedure Expand_Non_Function_Return
(N
: Node_Id
) is
6268 pragma Assert
(No
(Expression
(N
)));
6270 Loc
: constant Source_Ptr
:= Sloc
(N
);
6271 Scope_Id
: Entity_Id
:= Return_Applies_To
(Return_Statement_Entity
(N
));
6272 Kind
: constant Entity_Kind
:= Ekind
(Scope_Id
);
6275 Goto_Stat
: Node_Id
;
6279 -- Ada 2022 (AI12-0279)
6281 if Has_Yield_Aspect
(Scope_Id
)
6282 and then RTE_Available
(RE_Yield
)
6285 Make_Procedure_Call_Statement
(Loc
,
6286 New_Occurrence_Of
(RTE
(RE_Yield
), Loc
)));
6289 -- If it is a return from a procedure do no extra steps
6291 if Kind
= E_Procedure
or else Kind
= E_Generic_Procedure
then
6294 -- If it is a nested return within an extended one, replace it with a
6295 -- return of the previously declared return object.
6297 elsif Kind
= E_Return_Statement
then
6299 Ret_Obj_Id
: constant Entity_Id
:= First_Entity
(Scope_Id
);
6302 -- Apply the same processing as Expand_N_Extended_Return_Statement
6303 -- if the returned object needs finalization actions. Note that we
6304 -- are invoked before Expand_N_Extended_Return_Statement but there
6305 -- may be multiple nested returns within the extended one.
6307 if Needs_Finalization
(Etype
(Ret_Obj_Id
)) then
6309 (N
, Make_Suppress_Object_Finalize_Call
(Loc
, Ret_Obj_Id
));
6313 Make_Simple_Return_Statement
(Loc
,
6314 Expression
=> New_Occurrence_Of
(Ret_Obj_Id
, Loc
)));
6315 Set_Comes_From_Extended_Return_Statement
(N
);
6316 Set_Return_Statement_Entity
(N
, Scope_Id
);
6317 Expand_Simple_Function_Return
(N
);
6322 pragma Assert
(Is_Entry
(Scope_Id
));
6324 -- Look at the enclosing block to see whether the return is from an
6325 -- accept statement or an entry body.
6327 for J
in reverse 0 .. Scope_Stack
.Last
loop
6328 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
6329 exit when Is_Concurrent_Type
(Scope_Id
);
6332 -- If it is a return from accept statement it is expanded as call to
6333 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6335 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6336 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6338 if Is_Task_Type
(Scope_Id
) then
6341 Make_Procedure_Call_Statement
(Loc
,
6342 Name
=> New_Occurrence_Of
(RTE
(RE_Complete_Rendezvous
), Loc
));
6343 Insert_Before
(N
, Call
);
6344 -- why not insert actions here???
6347 Acc_Stat
:= Parent
(N
);
6348 while Nkind
(Acc_Stat
) /= N_Accept_Statement
loop
6349 Acc_Stat
:= Parent
(Acc_Stat
);
6352 Lab_Node
:= Last
(Statements
6353 (Handled_Statement_Sequence
(Acc_Stat
)));
6355 Goto_Stat
:= Make_Goto_Statement
(Loc
,
6356 Name
=> New_Occurrence_Of
6357 (Entity
(Identifier
(Lab_Node
)), Loc
));
6359 Set_Analyzed
(Goto_Stat
);
6361 Rewrite
(N
, Goto_Stat
);
6364 -- If it is a return from an entry body, put a Complete_Entry_Body call
6365 -- in front of the return.
6367 elsif Is_Protected_Type
(Scope_Id
) then
6369 Make_Procedure_Call_Statement
(Loc
,
6371 New_Occurrence_Of
(RTE
(RE_Complete_Entry_Body
), Loc
),
6372 Parameter_Associations
=> New_List
(
6373 Make_Attribute_Reference
(Loc
,
6376 (Find_Protection_Object
(Current_Scope
), Loc
),
6377 Attribute_Name
=> Name_Unchecked_Access
)));
6379 Insert_Before
(N
, Call
);
6382 end Expand_Non_Function_Return
;
6384 ---------------------------------------
6385 -- Expand_Protected_Object_Reference --
6386 ---------------------------------------
6388 function Expand_Protected_Object_Reference
6390 Scop
: Entity_Id
) return Node_Id
6392 Loc
: constant Source_Ptr
:= Sloc
(N
);
6399 Rec
:= Make_Identifier
(Loc
, Name_uObject
);
6400 Set_Etype
(Rec
, Corresponding_Record_Type
(Scop
));
6402 -- Find enclosing protected operation, and retrieve its first parameter,
6403 -- which denotes the enclosing protected object. If the enclosing
6404 -- operation is an entry, we are immediately within the protected body,
6405 -- and we can retrieve the object from the service entries procedure. A
6406 -- barrier function has the same signature as an entry. A barrier
6407 -- function is compiled within the protected object, but unlike
6408 -- protected operations its never needs locks, so that its protected
6409 -- body subprogram points to itself.
6411 Proc
:= Current_Scope
;
6412 while Present
(Proc
) and then Scope
(Proc
) /= Scop
loop
6413 Proc
:= Scope
(Proc
);
6414 if Is_Subprogram
(Proc
)
6415 and then Present
(Protected_Subprogram
(Proc
))
6417 Proc
:= Protected_Subprogram
(Proc
);
6421 Corr
:= Protected_Body_Subprogram
(Proc
);
6425 -- Previous error left expansion incomplete.
6426 -- Nothing to do on this call.
6433 (First
(Parameter_Specifications
(Parent
(Corr
))));
6435 if Is_Subprogram
(Proc
) and then Proc
/= Corr
then
6437 -- Protected function or procedure
6439 Set_Entity
(Rec
, Param
);
6441 -- Rec is a reference to an entity which will not be in scope when
6442 -- the call is reanalyzed, and needs no further analysis.
6447 -- Entry or barrier function for entry body. The first parameter of
6448 -- the entry body procedure is pointer to the object. We create a
6449 -- local variable of the proper type, duplicating what is done to
6450 -- define _object later on.
6454 Obj_Ptr
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
6458 Make_Full_Type_Declaration
(Loc
,
6459 Defining_Identifier
=> Obj_Ptr
,
6461 Make_Access_To_Object_Definition
(Loc
,
6462 Subtype_Indication
=>
6464 (Corresponding_Record_Type
(Scop
), Loc
))));
6466 Insert_Actions
(N
, Decls
);
6467 Freeze_Before
(N
, Obj_Ptr
);
6470 Make_Explicit_Dereference
(Loc
,
6472 Unchecked_Convert_To
(Obj_Ptr
,
6473 New_Occurrence_Of
(Param
, Loc
)));
6475 -- Analyze new actual. Other actuals in calls are already analyzed
6476 -- and the list of actuals is not reanalyzed after rewriting.
6478 Set_Parent
(Rec
, N
);
6484 end Expand_Protected_Object_Reference
;
6486 --------------------------------------
6487 -- Expand_Protected_Subprogram_Call --
6488 --------------------------------------
6490 procedure Expand_Protected_Subprogram_Call
6497 procedure Expand_Internal_Init_Call
;
6498 -- A call to an operation of the type may occur in the initialization
6499 -- of a private component. In that case the prefix of the call is an
6500 -- entity name and the call is treated as internal even though it
6501 -- appears in code outside of the protected type.
6503 procedure Freeze_Called_Function
;
6504 -- If it is a function call it can appear in elaboration code and
6505 -- the called entity must be frozen before the call. This must be
6506 -- done before the call is expanded, as the expansion may rewrite it
6507 -- to something other than a call (e.g. a temporary initialized in a
6508 -- transient block).
6510 -------------------------------
6511 -- Expand_Internal_Init_Call --
6512 -------------------------------
6514 procedure Expand_Internal_Init_Call
is
6516 -- If the context is a protected object (rather than a protected
6517 -- type) the call itself is bound to raise program_error because
6518 -- the protected body will not have been elaborated yet. This is
6519 -- diagnosed subsequently in Sem_Elab.
6521 Freeze_Called_Function
;
6523 -- The target of the internal call is the first formal of the
6524 -- enclosing initialization procedure.
6526 Rec
:= New_Occurrence_Of
(First_Formal
(Current_Scope
), Sloc
(N
));
6527 Build_Protected_Subprogram_Call
(N
,
6532 Resolve
(N
, Etype
(Subp
));
6533 end Expand_Internal_Init_Call
;
6535 ----------------------------
6536 -- Freeze_Called_Function --
6537 ----------------------------
6539 procedure Freeze_Called_Function
is
6541 if Ekind
(Subp
) = E_Function
then
6542 Freeze_Expression
(Name
(N
));
6544 end Freeze_Called_Function
;
6546 -- Start of processing for Expand_Protected_Subprogram_Call
6549 -- If the protected object is not an enclosing scope, this is an inter-
6550 -- object function call. Inter-object procedure calls are expanded by
6551 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
6552 -- subprogram being called is in the protected body being compiled, and
6553 -- if the protected object in the call is statically the enclosing type.
6554 -- The object may be a component of some other data structure, in which
6555 -- case this must be handled as an inter-object call.
6557 if not Scope_Within_Or_Same
(Inner
=> Current_Scope
, Outer
=> Scop
)
6558 or else Is_Entry_Wrapper
(Current_Scope
)
6559 or else not Is_Entity_Name
(Name
(N
))
6561 if Nkind
(Name
(N
)) = N_Selected_Component
then
6562 Rec
:= Prefix
(Name
(N
));
6564 elsif Nkind
(Name
(N
)) = N_Indexed_Component
then
6565 Rec
:= Prefix
(Prefix
(Name
(N
)));
6567 -- If this is a call within an entry wrapper, it appears within a
6568 -- precondition that calls another primitive of the synchronized
6569 -- type. The target object of the call is the first actual on the
6570 -- wrapper. Note that this is an external call, because the wrapper
6571 -- is called outside of the synchronized object. This means that
6572 -- an entry call to an entry with preconditions involves two
6573 -- synchronized operations.
6575 elsif Ekind
(Current_Scope
) = E_Procedure
6576 and then Is_Entry_Wrapper
(Current_Scope
)
6578 Rec
:= New_Occurrence_Of
(First_Entity
(Current_Scope
), Sloc
(N
));
6580 -- A default parameter of a protected operation may be a call to
6581 -- a protected function of the type. This appears as an internal
6582 -- call in the profile of the operation, but if the context is an
6583 -- external call we must convert the call into an external one,
6584 -- using the protected object that is the target, so that:
6587 -- is transformed into
6590 elsif Nkind
(Parent
(N
)) = N_Procedure_Call_Statement
6591 and then Nkind
(Name
(Parent
(N
))) = N_Selected_Component
6592 and then Is_Protected_Type
(Etype
(Prefix
(Name
(Parent
(N
)))))
6593 and then Is_Entity_Name
(Name
(N
))
6594 and then Scope
(Entity
(Name
(N
))) =
6595 Etype
(Prefix
(Name
(Parent
(N
))))
6598 Make_Selected_Component
(Sloc
(N
),
6599 Prefix
=> New_Copy_Tree
(Prefix
(Name
(Parent
(N
)))),
6600 Selector_Name
=> Relocate_Node
(Name
(N
))));
6602 Analyze_And_Resolve
(N
);
6606 -- If the context is the initialization procedure for a protected
6607 -- type, the call is legal because the called entity must be a
6608 -- function of that enclosing type, and this is treated as an
6612 (Is_Entity_Name
(Name
(N
)) and then Inside_Init_Proc
);
6614 Expand_Internal_Init_Call
;
6618 Freeze_Called_Function
;
6619 Build_Protected_Subprogram_Call
(N
,
6620 Name
=> New_Occurrence_Of
(Subp
, Sloc
(N
)),
6621 Rec
=> Convert_Concurrent
(Rec
, Etype
(Rec
)),
6625 Rec
:= Expand_Protected_Object_Reference
(N
, Scop
);
6631 Freeze_Called_Function
;
6632 Build_Protected_Subprogram_Call
(N
,
6638 -- Analyze and resolve the new call. The actuals have already been
6639 -- resolved, but expansion of a function call will add extra actuals
6640 -- if needed. Analysis of a procedure call already includes resolution.
6644 if Ekind
(Subp
) = E_Function
then
6645 Resolve
(N
, Etype
(Subp
));
6647 end Expand_Protected_Subprogram_Call
;
6649 -----------------------------------
6650 -- Expand_Simple_Function_Return --
6651 -----------------------------------
6653 -- The "simple" comes from the syntax rule simple_return_statement. The
6654 -- semantics are not at all simple.
6656 procedure Expand_Simple_Function_Return
(N
: Node_Id
) is
6657 Loc
: constant Source_Ptr
:= Sloc
(N
);
6659 Scope_Id
: constant Entity_Id
:=
6660 Return_Applies_To
(Return_Statement_Entity
(N
));
6661 -- The function we are returning from
6663 R_Type
: constant Entity_Id
:= Etype
(Scope_Id
);
6664 -- The result type of the function
6666 Utyp
: constant Entity_Id
:= Underlying_Type
(R_Type
);
6667 -- The underlying result type of the function
6669 Exp
: Node_Id
:= Expression
(N
);
6670 pragma Assert
(Present
(Exp
));
6672 Exp_Is_Function_Call
: constant Boolean :=
6673 Nkind
(Exp
) = N_Function_Call
6675 (Is_Captured_Function_Call
(Exp
)
6676 and then Is_Related_To_Func_Return
(Entity
(Prefix
(Exp
))));
6677 -- If the expression is a captured function call, then we need to make
6678 -- sure that the object doing the capture is properly recognized by the
6679 -- Is_Related_To_Func_Return predicate; otherwise, if it is of a type
6680 -- that needs finalization, Requires_Cleanup_Actions would return true
6681 -- because of this and Build_Finalizer would finalize it prematurely.
6683 Exp_Typ
: constant Entity_Id
:= Etype
(Exp
);
6684 -- The type of the expression (not necessarily the same as R_Type)
6686 Subtype_Ind
: Node_Id
;
6687 -- If the result type of the function is class-wide and the expression
6688 -- has a specific type, then we use the expression's type as the type of
6689 -- the return object. In cases where the expression is an aggregate that
6690 -- is built in place, this avoids the need for an expensive conversion
6691 -- of the return object to the specific type on assignments to the
6692 -- individual components.
6694 -- Start of processing for Expand_Simple_Function_Return
6697 if Is_Class_Wide_Type
(R_Type
)
6698 and then not Is_Class_Wide_Type
(Exp_Typ
)
6699 and then Nkind
(Exp
) /= N_Type_Conversion
6701 Subtype_Ind
:= New_Occurrence_Of
(Exp_Typ
, Loc
);
6703 Subtype_Ind
:= New_Occurrence_Of
(R_Type
, Loc
);
6705 -- If the result type is class-wide and the expression is a view
6706 -- conversion, the conversion plays no role in the expansion because
6707 -- it does not modify the tag of the object. Remove the conversion
6708 -- altogether to prevent tag overwriting.
6710 if Is_Class_Wide_Type
(R_Type
)
6711 and then not Is_Class_Wide_Type
(Exp_Typ
)
6712 and then Nkind
(Exp
) = N_Type_Conversion
6714 Exp
:= Expression
(Exp
);
6718 -- For the case of a simple return that does not come from an
6719 -- extended return, in the case of build-in-place, we rewrite
6720 -- "return <expression>;" to be:
6722 -- return _anon_ : <return_subtype> := <expression>
6724 -- The expansion produced by Expand_N_Extended_Return_Statement will
6725 -- contain simple return statements (for example, a block containing
6726 -- simple return of the return object), which brings us back here with
6727 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
6728 -- checking for a simple return that does not come from an extended
6729 -- return is to avoid this infinite recursion.
6731 -- The reason for this design is that for Ada 2005 limited returns, we
6732 -- need to reify the return object, so we can build it "in place", and
6733 -- we need a block statement to hang finalization and tasking stuff.
6736 (Comes_From_Extended_Return_Statement
(N
)
6737 or else not Is_Build_In_Place_Function_Call
(Exp
)
6738 or else Has_BIP_Formals
(Scope_Id
));
6740 if not Comes_From_Extended_Return_Statement
(N
)
6741 and then Is_Build_In_Place_Function
(Scope_Id
)
6743 -- The functionality of interface thunks is simple and it is always
6744 -- handled by means of simple return statements. This leaves their
6745 -- expansion simple and clean.
6747 and then not Is_Thunk
(Scope_Id
)
6750 Return_Object_Entity
: constant Entity_Id
:=
6751 Make_Temporary
(Loc
, 'R', Exp
);
6753 Obj_Decl
: constant Node_Id
:=
6754 Make_Object_Declaration
(Loc
,
6755 Defining_Identifier
=> Return_Object_Entity
,
6756 Object_Definition
=> Subtype_Ind
,
6759 Ext
: constant Node_Id
:=
6760 Make_Extended_Return_Statement
(Loc
,
6761 Return_Object_Declarations
=> New_List
(Obj_Decl
));
6762 -- Do not perform this high-level optimization if the result type
6763 -- is an interface because the "this" pointer must be displaced.
6772 -- Assert that if F says "return G(...);"
6773 -- then F and G are both b-i-p, or neither b-i-p.
6775 if Nkind
(Exp
) = N_Function_Call
then
6776 pragma Assert
(Ekind
(Scope_Id
) = E_Function
);
6778 (Is_Build_In_Place_Function
(Scope_Id
) =
6779 Is_Build_In_Place_Function_Call
(Exp
));
6783 -- Here we have a simple return statement that is part of the expansion
6784 -- of an extended return statement (either written by the user, or
6785 -- generated by the above code).
6787 -- Always normalize C/Fortran boolean result. This is not always needed,
6788 -- but it seems a good idea to minimize the passing around of non-
6789 -- normalized values, and in any case this handles the processing of
6790 -- barrier functions for protected types, which turn the condition into
6791 -- a return statement.
6793 if Is_Boolean_Type
(Exp_Typ
) and then Nonzero_Is_True
(Exp_Typ
) then
6794 Adjust_Condition
(Exp
);
6795 Adjust_Result_Type
(Exp
, Exp_Typ
);
6797 -- The adjustment of the expression may have rewritten the return
6798 -- statement itself, e.g. when it is turned into an if expression.
6800 if Nkind
(N
) /= N_Simple_Return_Statement
then
6805 -- Do validity check if enabled for returns
6807 if Validity_Checks_On
and then Validity_Check_Returns
then
6811 -- Check the result expression of a scalar function against the subtype
6812 -- of the function by inserting a conversion. This conversion must
6813 -- eventually be performed for other classes of types, but for now it's
6814 -- only done for scalars ???
6816 if Is_Scalar_Type
(Exp_Typ
) and then Exp_Typ
/= R_Type
then
6817 Rewrite
(Exp
, Convert_To
(R_Type
, Exp
));
6819 -- The expression is resolved to ensure that the conversion gets
6820 -- expanded to generate a possible constraint check.
6822 Analyze_And_Resolve
(Exp
, R_Type
);
6825 -- Deal with returning variable length objects and controlled types
6827 -- Nothing to do if we are returning by reference
6829 if Is_Build_In_Place_Function
(Scope_Id
) then
6830 -- Prevent the reclamation of the secondary stack by all enclosing
6831 -- blocks and loops as well as the related function; otherwise the
6832 -- result would be reclaimed too early.
6834 if Needs_BIP_Alloc_Form
(Scope_Id
) then
6835 Set_Enclosing_Sec_Stack_Return
(N
);
6838 elsif Is_Inherently_Limited_Type
(R_Type
) then
6841 -- No copy needed for thunks returning interface type objects since
6842 -- the object is returned by reference and the maximum functionality
6843 -- required is just to displace the pointer.
6845 elsif Is_Thunk
(Scope_Id
) and then Is_Interface
(Exp_Typ
) then
6848 -- If the call is within a thunk and the type is a limited view, the
6849 -- back end will eventually see the non-limited view of the type.
6851 elsif Is_Thunk
(Scope_Id
) and then Is_Incomplete_Type
(Exp_Typ
) then
6854 -- A return statement from an ignored Ghost function does not use the
6855 -- secondary stack (or any other one).
6857 elsif (not Needs_Secondary_Stack
(R_Type
)
6858 and then not Is_Secondary_Stack_Thunk
(Scope_Id
))
6859 or else Is_Ignored_Ghost_Entity
(Scope_Id
)
6861 -- Mutable records with variable-length components are not returned
6862 -- on the sec-stack, so we need to make sure that the back end will
6863 -- only copy back the size of the actual value, and not the maximum
6864 -- size. We create an actual subtype for this purpose. However we
6865 -- need not do it if the expression is a function call since this
6866 -- will be done in the called function and doing it here too would
6867 -- cause a temporary with maximum size to be created. Likewise for
6868 -- a special return object, since there is no copy in this case.
6871 Ubt
: constant Entity_Id
:= Underlying_Type
(Base_Type
(Exp_Typ
));
6876 if not Exp_Is_Function_Call
6877 and then not (Is_Entity_Name
(Exp
)
6878 and then Is_Special_Return_Object
(Entity
(Exp
)))
6879 and then Has_Defaulted_Discriminants
(Ubt
)
6880 and then not Is_Constrained
(Ubt
)
6881 and then not Has_Unchecked_Union
(Ubt
)
6883 Decl
:= Build_Actual_Subtype
(Ubt
, Exp
);
6884 Ent
:= Defining_Identifier
(Decl
);
6885 Insert_Action
(Exp
, Decl
);
6886 Rewrite
(Exp
, Unchecked_Convert_To
(Ent
, Exp
));
6887 Analyze_And_Resolve
(Exp
);
6891 -- For types which need finalization, do the allocation on the return
6892 -- stack manually in order to call Adjust at the right time:
6894 -- type Ann is access R_Type;
6895 -- for Ann'Storage_pool use rs_pool;
6896 -- Rnn : constant Ann := new Exp_Typ'(Exp);
6899 -- but optimize the case where the result is a function call that
6900 -- also needs finalization. In this case the result can directly be
6901 -- allocated on the return stack of the caller and no further
6902 -- processing is required. Likewise if this is a return object.
6904 if Comes_From_Extended_Return_Statement
(N
) then
6907 elsif Present
(Utyp
)
6908 and then Needs_Finalization
(Utyp
)
6909 and then not (Exp_Is_Function_Call
6910 and then Needs_Finalization
(Exp_Typ
))
6913 Acc_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
6915 Alloc_Node
: Node_Id
;
6919 Mutate_Ekind
(Acc_Typ
, E_Access_Type
);
6921 Set_Associated_Storage_Pool
(Acc_Typ
, RTE
(RE_RS_Pool
));
6923 -- This is an allocator for the return stack, and it's fine
6924 -- to have Comes_From_Source set False on it, as gigi knows not
6925 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6928 Make_Allocator
(Loc
,
6930 Make_Qualified_Expression
(Loc
,
6931 Subtype_Mark
=> New_Occurrence_Of
(Exp_Typ
, Loc
),
6932 Expression
=> Relocate_Node
(Exp
)));
6934 -- We do not want discriminant checks on the declaration,
6935 -- given that it gets its value from the allocator.
6937 Set_No_Initialization
(Alloc_Node
);
6939 Temp
:= Make_Temporary
(Loc
, 'R', Alloc_Node
);
6941 Insert_Actions
(Exp
, New_List
(
6942 Make_Full_Type_Declaration
(Loc
,
6943 Defining_Identifier
=> Acc_Typ
,
6945 Make_Access_To_Object_Definition
(Loc
,
6946 Subtype_Indication
=> Subtype_Ind
)),
6948 Make_Object_Declaration
(Loc
,
6949 Defining_Identifier
=> Temp
,
6950 Constant_Present
=> True,
6951 Object_Definition
=> New_Occurrence_Of
(Acc_Typ
, Loc
),
6952 Expression
=> Alloc_Node
)));
6955 Make_Explicit_Dereference
(Loc
,
6956 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
6958 Analyze_And_Resolve
(Exp
, R_Type
);
6962 -- Here if secondary stack is used
6965 -- Prevent the reclamation of the secondary stack by all enclosing
6966 -- blocks and loops as well as the related function; otherwise the
6967 -- result would be reclaimed too early.
6969 Set_Enclosing_Sec_Stack_Return
(N
);
6971 -- Nothing else to do for a return object
6973 if Comes_From_Extended_Return_Statement
(N
) then
6976 -- Optimize the case where the result is a function call that also
6977 -- returns on the secondary stack; in this case the result is already
6978 -- on the secondary stack and no further processing is required.
6980 elsif Exp_Is_Function_Call
6981 and then Needs_Secondary_Stack
(Exp_Typ
)
6983 -- Remove side effects from the expression now so that other parts
6984 -- of the expander do not have to reanalyze this node without this
6987 Rewrite
(Exp
, Duplicate_Subexpr_No_Checks
(Exp
));
6989 -- Ada 2005 (AI-251): If the type of the returned object is
6990 -- an interface then add an implicit type conversion to force
6991 -- displacement of the "this" pointer.
6993 if Is_Interface
(R_Type
) then
6994 Rewrite
(Exp
, Convert_To
(R_Type
, Relocate_Node
(Exp
)));
6997 Analyze_And_Resolve
(Exp
, R_Type
);
6999 -- For types which both need finalization and are returned on the
7000 -- secondary stack, do the allocation on secondary stack manually
7001 -- in order to call Adjust at the right time:
7003 -- type Ann is access R_Type;
7004 -- for Ann'Storage_pool use ss_pool;
7005 -- Rnn : constant Ann := new Exp_Typ'(Exp);
7008 -- And we do the same for class-wide types that are not potentially
7009 -- controlled (by the virtue of restriction No_Finalization) because
7010 -- gigi is not able to properly allocate class-wide types.
7012 -- But optimize the case where the result is a function call that
7013 -- also needs finalization; in this case the result can directly be
7014 -- allocated on the secondary stack and no further processing is
7015 -- required, unless the returned object is an interface.
7017 elsif CW_Or_Needs_Finalization
(Utyp
)
7018 and then (Is_Interface
(R_Type
)
7019 or else not (Exp_Is_Function_Call
7020 and then Needs_Finalization
(Exp_Typ
)))
7023 Acc_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
7025 Alloc_Node
: Node_Id
;
7029 Mutate_Ekind
(Acc_Typ
, E_Access_Type
);
7030 Set_Associated_Storage_Pool
(Acc_Typ
, RTE
(RE_SS_Pool
));
7032 -- This is an allocator for the secondary stack, and it's fine
7033 -- to have Comes_From_Source set False on it, as gigi knows not
7034 -- to flag it as a violation of No_Implicit_Heap_Allocations.
7037 Make_Allocator
(Loc
,
7039 Make_Qualified_Expression
(Loc
,
7040 Subtype_Mark
=> New_Occurrence_Of
(Etype
(Exp
), Loc
),
7041 Expression
=> Relocate_Node
(Exp
)));
7043 -- We do not want discriminant checks on the declaration,
7044 -- given that it gets its value from the allocator.
7046 Set_No_Initialization
(Alloc_Node
);
7048 Temp
:= Make_Temporary
(Loc
, 'R', Alloc_Node
);
7050 Insert_Actions
(Exp
, New_List
(
7051 Make_Full_Type_Declaration
(Loc
,
7052 Defining_Identifier
=> Acc_Typ
,
7054 Make_Access_To_Object_Definition
(Loc
,
7055 Subtype_Indication
=> Subtype_Ind
)),
7057 Make_Object_Declaration
(Loc
,
7058 Defining_Identifier
=> Temp
,
7059 Constant_Present
=> True,
7060 Object_Definition
=> New_Occurrence_Of
(Acc_Typ
, Loc
),
7061 Expression
=> Alloc_Node
)));
7064 Make_Explicit_Dereference
(Loc
,
7065 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
7067 -- Ada 2005 (AI-251): If the type of the returned object is
7068 -- an interface then add an implicit type conversion to force
7069 -- displacement of the "this" pointer.
7071 if Is_Interface
(R_Type
) then
7072 Rewrite
(Exp
, Convert_To
(R_Type
, Relocate_Node
(Exp
)));
7075 Analyze_And_Resolve
(Exp
, R_Type
);
7078 -- Otherwise use the gigi mechanism to allocate result on the
7082 Check_Restriction
(No_Secondary_Stack
, N
);
7083 Set_Storage_Pool
(N
, RTE
(RE_SS_Pool
));
7084 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
7088 -- Implement the rules of 6.5(8-10), which require a tag check in
7089 -- the case of a limited tagged return type, and tag reassignment for
7090 -- nonlimited tagged results. These actions are needed when the return
7091 -- type is a specific tagged type and the result expression is a
7092 -- conversion or a formal parameter, because in that case the tag of
7093 -- the expression might differ from the tag of the specific result type.
7095 -- We must also verify an underlying type exists for the return type in
7096 -- case it is incomplete - in which case is not necessary to generate a
7097 -- check anyway since an incomplete limited tagged return type would
7098 -- qualify as a premature usage.
7101 and then Is_Tagged_Type
(Utyp
)
7102 and then not Is_Class_Wide_Type
(Utyp
)
7103 and then (Nkind
(Exp
) in
7104 N_Type_Conversion | N_Unchecked_Type_Conversion
7105 or else (Nkind
(Exp
) = N_Explicit_Dereference
7106 and then Nkind
(Prefix
(Exp
)) in
7108 N_Unchecked_Type_Conversion
)
7109 or else (Is_Entity_Name
(Exp
)
7110 and then Is_Formal
(Entity
(Exp
))))
7112 -- When the return type is limited, perform a check that the tag of
7113 -- the result is the same as the tag of the return type.
7115 if Is_Limited_Type
(R_Type
) then
7117 Make_Raise_Constraint_Error
(Loc
,
7121 Make_Selected_Component
(Loc
,
7122 Prefix
=> Duplicate_Subexpr
(Exp
),
7123 Selector_Name
=> Make_Identifier
(Loc
, Name_uTag
)),
7125 Make_Attribute_Reference
(Loc
,
7127 New_Occurrence_Of
(Base_Type
(Utyp
), Loc
),
7128 Attribute_Name
=> Name_Tag
)),
7129 Reason
=> CE_Tag_Check_Failed
));
7131 -- If the result type is a specific nonlimited tagged type, then we
7132 -- have to ensure that the tag of the result is that of the result
7133 -- type. This is handled by making a copy of the expression in
7134 -- the case where it might have a different tag, namely when the
7135 -- expression is a conversion or a formal parameter. We create a new
7136 -- object of the result type and initialize it from the expression,
7137 -- which will implicitly force the tag to be set appropriately.
7141 ExpR
: constant Node_Id
:= Relocate_Node
(Exp
);
7142 Result_Id
: constant Entity_Id
:=
7143 Make_Temporary
(Loc
, 'R', ExpR
);
7144 Result_Exp
: constant Node_Id
:=
7145 New_Occurrence_Of
(Result_Id
, Loc
);
7146 Result_Obj
: constant Node_Id
:=
7147 Make_Object_Declaration
(Loc
,
7148 Defining_Identifier
=> Result_Id
,
7149 Object_Definition
=>
7150 New_Occurrence_Of
(R_Type
, Loc
),
7151 Constant_Present
=> True,
7152 Expression
=> ExpR
);
7155 Set_Assignment_OK
(Result_Obj
);
7156 Insert_Action
(Exp
, Result_Obj
);
7158 Rewrite
(Exp
, Result_Exp
);
7159 Analyze_And_Resolve
(Exp
, R_Type
);
7163 -- Ada 2005 (AI95-344): If the result type is class-wide, then insert
7164 -- a check that the level of the return expression's underlying type
7165 -- is not deeper than the level of the master enclosing the function.
7167 -- AI12-043: The check is made immediately after the return object is
7168 -- created. This means that we do not apply it to the simple return
7169 -- generated by the expansion of an extended return statement.
7171 -- No runtime check needed in interface thunks since it is performed
7172 -- by the target primitive associated with the thunk.
7174 elsif Is_Class_Wide_Type
(R_Type
)
7175 and then not Comes_From_Extended_Return_Statement
(N
)
7176 and then not Is_Thunk
(Scope_Id
)
7178 Apply_CW_Accessibility_Check
(Exp
, Scope_Id
);
7180 -- Ada 2012 (AI05-0073): If the result subtype of the function is
7181 -- defined by an access_definition designating a specific tagged
7182 -- type T, a check is made that the result value is null or the tag
7183 -- of the object designated by the result value identifies T.
7185 -- The return expression is referenced twice in the code below, so it
7186 -- must be made free of side effects. Given that different compilers
7187 -- may evaluate these parameters in different order, both occurrences
7190 elsif Ekind
(R_Type
) = E_Anonymous_Access_Type
7191 and then Is_Tagged_Type
(Designated_Type
(R_Type
))
7192 and then not Is_Class_Wide_Type
(Designated_Type
(R_Type
))
7193 and then Nkind
(Original_Node
(Exp
)) /= N_Null
7194 and then not Tag_Checks_Suppressed
(Designated_Type
(R_Type
))
7197 -- [Constraint_Error
7199 -- and then Exp.all not in Designated_Type]
7202 Make_Raise_Constraint_Error
(Loc
,
7207 Left_Opnd
=> Duplicate_Subexpr
(Exp
),
7208 Right_Opnd
=> Make_Null
(Loc
)),
7213 Make_Explicit_Dereference
(Loc
,
7214 Prefix
=> Duplicate_Subexpr
(Exp
)),
7216 New_Occurrence_Of
(Designated_Type
(R_Type
), Loc
))),
7218 Reason
=> CE_Tag_Check_Failed
),
7219 Suppress
=> All_Checks
);
7222 -- If the result is of an unconstrained array subtype with fixed lower
7223 -- bound, then sliding to that bound may be needed.
7225 if Is_Fixed_Lower_Bound_Array_Subtype
(R_Type
) then
7226 Expand_Sliding_Conversion
(Exp
, R_Type
);
7229 -- If we are returning a nonscalar object that is possibly unaligned,
7230 -- then copy the value into a temporary first. This copy may need to
7231 -- expand to a loop of component operations.
7233 if Is_Possibly_Unaligned_Slice
(Exp
)
7234 or else (Is_Possibly_Unaligned_Object
(Exp
)
7235 and then not Represented_As_Scalar
(Etype
(Exp
)))
7238 ExpR
: constant Node_Id
:= Relocate_Node
(Exp
);
7239 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', ExpR
);
7242 Make_Object_Declaration
(Loc
,
7243 Defining_Identifier
=> Tnn
,
7244 Constant_Present
=> True,
7245 Object_Definition
=> New_Occurrence_Of
(R_Type
, Loc
),
7246 Expression
=> ExpR
),
7247 Suppress
=> All_Checks
);
7248 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
7252 -- Ada 2005 (AI-251): If this return statement corresponds with an
7253 -- simple return statement associated with an extended return statement
7254 -- and the type of the returned object is an interface then generate an
7255 -- implicit conversion to force displacement of the "this" pointer.
7257 if Ada_Version
>= Ada_2005
7258 and then Comes_From_Extended_Return_Statement
(N
)
7259 and then Nkind
(Expression
(N
)) = N_Identifier
7260 and then Is_Interface
(Utyp
)
7261 and then Utyp
/= Underlying_Type
(Exp_Typ
)
7263 Rewrite
(Exp
, Convert_To
(Utyp
, Relocate_Node
(Exp
)));
7264 Analyze_And_Resolve
(Exp
);
7267 -- Ada 2022 (AI12-0279)
7269 if Has_Yield_Aspect
(Scope_Id
)
7270 and then RTE_Available
(RE_Yield
)
7273 Make_Procedure_Call_Statement
(Loc
,
7274 New_Occurrence_Of
(RTE
(RE_Yield
), Loc
)));
7276 end Expand_Simple_Function_Return
;
7278 -----------------------
7279 -- Freeze_Subprogram --
7280 -----------------------
7282 procedure Freeze_Subprogram
(N
: Node_Id
) is
7283 Loc
: constant Source_Ptr
:= Sloc
(N
);
7284 Subp
: constant Entity_Id
:= Entity
(N
);
7287 -- We suppress the initialization of the dispatch table entry when
7288 -- not Tagged_Type_Expansion because the dispatching mechanism is
7289 -- handled internally by the target.
7291 if Is_Dispatching_Operation
(Subp
)
7292 and then not Is_Abstract_Subprogram
(Subp
)
7293 and then Present
(DTC_Entity
(Subp
))
7294 and then Present
(Scope
(DTC_Entity
(Subp
)))
7295 and then Tagged_Type_Expansion
7296 and then not Restriction_Active
(No_Dispatching_Calls
)
7297 and then RTE_Available
(RE_Tag
)
7300 Typ
: constant Entity_Id
:= Scope
(DTC_Entity
(Subp
));
7305 -- Handle private overridden primitives
7307 if not Is_CPP_Class
(Typ
) then
7308 Check_Overriding_Operation
(Subp
);
7311 -- We assume that imported CPP primitives correspond with objects
7312 -- whose constructor is in the CPP side; therefore we don't need
7313 -- to generate code to register them in the dispatch table.
7315 if Is_CPP_Class
(Typ
) then
7318 -- Handle CPP primitives found in derivations of CPP_Class types.
7319 -- These primitives must have been inherited from some parent, and
7320 -- there is no need to register them in the dispatch table because
7321 -- Build_Inherit_Prims takes care of initializing these slots.
7323 elsif Is_Imported
(Subp
)
7324 and then Convention
(Subp
) in Convention_C_Family
7328 -- Generate code to register the primitive in non statically
7329 -- allocated dispatch tables
7331 elsif not Building_Static_DT
(Scope
(DTC_Entity
(Subp
))) then
7333 -- When a primitive is frozen, enter its name in its dispatch
7336 if not Is_Interface
(Typ
)
7337 or else Present
(Interface_Alias
(Subp
))
7339 if Is_Predefined_Dispatching_Operation
(Subp
) then
7340 L
:= Register_Predefined_Primitive
(Loc
, Subp
);
7345 Append_List_To
(L
, Register_Primitive
(Loc
, Subp
));
7347 if Is_Empty_List
(L
) then
7350 elsif No
(Actions
(N
)) then
7354 Append_List
(L
, Actions
(N
));
7361 -- Mark functions that return by reference. Note that it cannot be part
7362 -- of the normal semantic analysis of the spec since the underlying
7363 -- returned type may not be known yet (for private types).
7365 Compute_Returns_By_Ref
(Subp
);
7366 end Freeze_Subprogram
;
7368 --------------------------
7369 -- Has_BIP_Extra_Formal --
7370 --------------------------
7372 function Has_BIP_Extra_Formal
7374 Kind
: BIP_Formal_Kind
;
7375 Must_Be_Frozen
: Boolean := True) return Boolean
7377 Extra_Formal
: Entity_Id
:= Extra_Formals
(E
);
7380 -- We can only rely on the availability of the extra formals in frozen
7381 -- entities or in subprogram types of dispatching calls (since their
7382 -- extra formals are added when the target subprogram is frozen; see
7383 -- Expand_Dispatching_Call).
7385 pragma Assert
((Is_Frozen
(E
) or else not Must_Be_Frozen
)
7386 or else (Ekind
(E
) = E_Subprogram_Type
7387 and then Is_Dispatch_Table_Entity
(E
))
7388 or else (Is_Dispatching_Operation
(E
)
7389 and then Is_Frozen
(Find_Dispatching_Type
(E
))));
7391 while Present
(Extra_Formal
) loop
7392 if Is_Build_In_Place_Entity
(Extra_Formal
)
7393 and then BIP_Suffix_Kind
(Extra_Formal
) = Kind
7398 Next_Formal_With_Extras
(Extra_Formal
);
7402 end Has_BIP_Extra_Formal
;
7404 ------------------------------
7405 -- Insert_Post_Call_Actions --
7406 ------------------------------
7408 procedure Insert_Post_Call_Actions
(N
: Node_Id
; Post_Call
: List_Id
) is
7409 Context
: constant Node_Id
:= Parent
(N
);
7412 if Is_Empty_List
(Post_Call
) then
7416 -- Cases where the call is not a member of a statement list. This also
7417 -- includes the cases where the call is an actual in another function
7418 -- call, or is an index, or is an operand of an if-expression, i.e. is
7419 -- in an expression context.
7421 if not Is_List_Member
(N
)
7422 or else Nkind
(Context
) in N_Function_Call
7424 | N_Indexed_Component
7426 -- In Ada 2012 the call may be a function call in an expression
7427 -- (since OUT and IN OUT parameters are now allowed for such calls).
7428 -- The write-back of (in)-out parameters is handled by the back-end,
7429 -- but the constraint checks generated when subtypes of formal and
7430 -- actual don't match must be inserted in the form of assignments.
7431 -- Also do this in the case of explicit dereferences, which can occur
7432 -- due to rewritings of function calls with controlled results.
7434 if Nkind
(N
) = N_Function_Call
7435 or else Nkind
(Original_Node
(N
)) = N_Function_Call
7436 or else Nkind
(N
) = N_Explicit_Dereference
7438 pragma Assert
(Ada_Version
>= Ada_2012
);
7439 -- Functions with '[in] out' parameters are only allowed in Ada
7442 -- We used to handle this by climbing up parents to a
7443 -- non-statement/declaration and then simply making a call to
7444 -- Insert_Actions_After (P, Post_Call), but that doesn't work
7445 -- for Ada 2012. If we are in the middle of an expression, e.g.
7446 -- the condition of an IF, this call would insert after the IF
7447 -- statement, which is much too late to be doing the write back.
7450 -- if Clobber (X) then
7451 -- Put_Line (X'Img);
7456 -- Now assume Clobber changes X, if we put the write back after
7457 -- the IF, the Put_Line gets the wrong value and the goto causes
7458 -- the write back to be skipped completely.
7460 -- To deal with this, we replace the call by
7463 -- Tnnn : constant function-result-type := function-call;
7464 -- Post_Call actions
7469 -- However, that doesn't work if function-result-type requires
7470 -- finalization (because function-call's result never gets
7471 -- finalized). So in that case, we instead replace the call by
7474 -- type Ref is access all function-result-type;
7475 -- Ptr : constant Ref := function-call'Reference;
7476 -- Tnnn : constant function-result-type := Ptr.all;
7477 -- Finalize (Ptr.all);
7478 -- Post_Call actions
7485 Loc
: constant Source_Ptr
:= Sloc
(N
);
7486 Tnnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
7487 FRTyp
: constant Entity_Id
:= Etype
(N
);
7488 Name
: constant Node_Id
:= Relocate_Node
(N
);
7491 if Needs_Finalization
(FRTyp
) then
7493 Ptr_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
7495 Ptr_Typ_Decl
: constant Node_Id
:=
7496 Make_Full_Type_Declaration
(Loc
,
7497 Defining_Identifier
=> Ptr_Typ
,
7499 Make_Access_To_Object_Definition
(Loc
,
7500 All_Present
=> True,
7501 Subtype_Indication
=>
7502 New_Occurrence_Of
(FRTyp
, Loc
)));
7504 Ptr_Obj
: constant Entity_Id
:=
7505 Make_Temporary
(Loc
, 'P');
7507 Ptr_Obj_Decl
: constant Node_Id
:=
7508 Make_Object_Declaration
(Loc
,
7509 Defining_Identifier
=> Ptr_Obj
,
7510 Object_Definition
=>
7511 New_Occurrence_Of
(Ptr_Typ
, Loc
),
7512 Constant_Present
=> True,
7514 Make_Attribute_Reference
(Loc
,
7516 Attribute_Name
=> Name_Unrestricted_Access
));
7518 function Ptr_Dereference
return Node_Id
is
7519 (Make_Explicit_Dereference
(Loc
,
7520 Prefix
=> New_Occurrence_Of
(Ptr_Obj
, Loc
)));
7522 Tnn_Decl
: constant Node_Id
:=
7523 Make_Object_Declaration
(Loc
,
7524 Defining_Identifier
=> Tnnn
,
7525 Object_Definition
=> New_Occurrence_Of
(FRTyp
, Loc
),
7526 Constant_Present
=> True,
7527 Expression
=> Ptr_Dereference
);
7529 Finalize_Call
: constant Node_Id
:=
7531 (Obj_Ref
=> Ptr_Dereference
, Typ
=> FRTyp
);
7533 -- Prepend in reverse order
7535 Prepend_To
(Post_Call
, Finalize_Call
);
7536 Prepend_To
(Post_Call
, Tnn_Decl
);
7537 Prepend_To
(Post_Call
, Ptr_Obj_Decl
);
7538 Prepend_To
(Post_Call
, Ptr_Typ_Decl
);
7541 Prepend_To
(Post_Call
,
7542 Make_Object_Declaration
(Loc
,
7543 Defining_Identifier
=> Tnnn
,
7544 Object_Definition
=> New_Occurrence_Of
(FRTyp
, Loc
),
7545 Constant_Present
=> True,
7546 Expression
=> Name
));
7550 Make_Expression_With_Actions
(Loc
,
7551 Actions
=> Post_Call
,
7552 Expression
=> New_Occurrence_Of
(Tnnn
, Loc
)));
7554 -- We don't want to just blindly call Analyze_And_Resolve
7555 -- because that would cause unwanted recursion on the call.
7556 -- So for a moment set the call as analyzed to prevent that
7557 -- recursion, and get the rest analyzed properly, then reset
7558 -- the analyzed flag, so our caller can continue.
7560 Set_Analyzed
(Name
, True);
7561 Analyze_And_Resolve
(N
, FRTyp
);
7562 Set_Analyzed
(Name
, False);
7565 -- If not the special Ada 2012 case of a function call, then we must
7566 -- have the triggering statement of a triggering alternative or an
7567 -- entry call alternative, and we can add the post call stuff to the
7568 -- corresponding statement list.
7571 pragma Assert
(Nkind
(Context
) in N_Entry_Call_Alternative
7572 | N_Triggering_Alternative
);
7574 if Is_Non_Empty_List
(Statements
(Context
)) then
7575 Insert_List_Before_And_Analyze
7576 (First
(Statements
(Context
)), Post_Call
);
7578 Set_Statements
(Context
, Post_Call
);
7582 -- A procedure call is always part of a declarative or statement list,
7583 -- however a function call may appear nested within a construct. Most
7584 -- cases of function call nesting are handled in the special case above.
7585 -- The only exception is when the function call acts as an actual in a
7586 -- procedure call. In this case the function call is in a list, but the
7587 -- post-call actions must be inserted after the procedure call.
7588 -- What if the function call is an aggregate component ???
7590 elsif Nkind
(Context
) = N_Procedure_Call_Statement
then
7591 Insert_Actions_After
(Context
, Post_Call
);
7593 -- Otherwise, normal case where N is in a statement sequence, just put
7594 -- the post-call stuff after the call statement.
7597 Insert_Actions_After
(N
, Post_Call
);
7599 end Insert_Post_Call_Actions
;
7601 ---------------------------------------
7602 -- Install_Class_Preconditions_Check --
7603 ---------------------------------------
7605 procedure Install_Class_Preconditions_Check
(Call_Node
: Node_Id
) is
7606 Loc
: constant Source_Ptr
:= Sloc
(Call_Node
);
7608 function Build_Dynamic_Check_Helper_Call
return Node_Id
;
7609 -- Build call to the helper runtime function of the nearest ancestor
7610 -- of the target subprogram that dynamically evaluates the merged
7611 -- or-else preconditions.
7613 function Build_Error_Message
(Subp_Id
: Entity_Id
) return Node_Id
;
7614 -- Build message associated with the class-wide precondition of Subp_Id
7615 -- indicating the call that caused it.
7617 function Build_Static_Check_Helper_Call
return Node_Id
;
7618 -- Build call to the helper runtime function of the nearest ancestor
7619 -- of the target subprogram that dynamically evaluates the merged
7620 -- or-else preconditions.
7622 function Class_Preconditions_Subprogram
7623 (Spec_Id
: Entity_Id
;
7624 Dynamic
: Boolean) return Node_Id
;
7625 -- Return the nearest ancestor of Spec_Id defining a helper function
7626 -- that evaluates a combined or-else expression containing all the
7627 -- inherited class-wide preconditions; Dynamic enables searching for
7628 -- the helper that dynamically evaluates preconditions using dispatching
7629 -- calls; if False it searches for the helper that statically evaluates
7630 -- preconditions; return Empty when not available (which means that no
7631 -- preconditions check is required).
7633 -------------------------------------
7634 -- Build_Dynamic_Check_Helper_Call --
7635 -------------------------------------
7637 function Build_Dynamic_Check_Helper_Call
return Node_Id
is
7638 Spec_Id
: constant Entity_Id
:= Entity
(Name
(Call_Node
));
7639 CW_Subp
: constant Entity_Id
:=
7640 Class_Preconditions_Subprogram
(Spec_Id
,
7642 Helper_Id
: constant Entity_Id
:=
7643 Dynamic_Call_Helper
(CW_Subp
);
7644 Actuals
: constant List_Id
:= New_List
;
7645 A
: Node_Id
:= First_Actual
(Call_Node
);
7648 while Present
(A
) loop
7650 -- Ensure that the evaluation of the actuals will not produce
7653 Remove_Side_Effects
(A
);
7655 Append_To
(Actuals
, New_Copy_Tree
(A
));
7661 Make_Function_Call
(Loc
,
7662 Name
=> New_Occurrence_Of
(Helper_Id
, Loc
),
7663 Parameter_Associations
=> Actuals
);
7664 end Build_Dynamic_Check_Helper_Call
;
7666 -------------------------
7667 -- Build_Error_Message --
7668 -------------------------
7670 function Build_Error_Message
(Subp_Id
: Entity_Id
) return Node_Id
is
7672 procedure Append_Message
7674 Is_First
: in out Boolean);
7675 -- Build the fragment of the message associated with subprogram Id;
7676 -- Is_First facilitates identifying continuation messages.
7678 --------------------
7679 -- Append_Message --
7680 --------------------
7682 procedure Append_Message
7684 Is_First
: in out Boolean)
7686 Prag
: constant Node_Id
:=
7687 Get_Class_Wide_Pragma
(Id
, Pragma_Precondition
);
7690 if No
(Prag
) or else Is_Ignored
(Prag
) then
7697 if Id
/= Subp_Id
then
7699 (Global_Name_Buffer
, "failed inherited precondition ");
7701 Append
(Global_Name_Buffer
, "failed precondition ");
7705 Append
(Global_Name_Buffer
, ASCII
.LF
);
7706 Append
(Global_Name_Buffer
, " or ");
7708 Append
(Global_Name_Buffer
, "failed inherited precondition ");
7711 Append
(Global_Name_Buffer
, "from " &
7712 Build_Location_String
7716 (First
(Pragma_Argument_Associations
(Prag
)))))));
7721 Str_Loc
: constant String := Build_Location_String
(Loc
);
7722 Subps
: constant Subprogram_List
:=
7723 Inherited_Subprograms
(Subp_Id
);
7724 Is_First
: Boolean := True;
7726 -- Start of processing for Build_Error_Message
7730 Append_Message
(Subp_Id
, Is_First
);
7732 for Index
in Subps
'Range loop
7733 Append_Message
(Subps
(Index
), Is_First
);
7736 if Present
(Controlling_Argument
(Call_Node
)) then
7737 Append
(Global_Name_Buffer
, " in dispatching call at ");
7739 Append
(Global_Name_Buffer
, " in call at ");
7742 Append
(Global_Name_Buffer
, Str_Loc
);
7744 return Make_String_Literal
(Loc
, Name_Buffer
(1 .. Name_Len
));
7745 end Build_Error_Message
;
7747 ------------------------------------
7748 -- Build_Static_Check_Helper_Call --
7749 ------------------------------------
7751 function Build_Static_Check_Helper_Call
return Node_Id
is
7752 Actuals
: constant List_Id
:= New_List
;
7754 Helper_Id
: Entity_Id
;
7756 CW_Subp
: Entity_Id
;
7757 Spec_Id
: constant Entity_Id
:= Entity
(Name
(Call_Node
));
7760 -- The target is the wrapper built to support inheriting body but
7761 -- overriding pre/postconditions (AI12-0195).
7763 if Is_Dispatch_Table_Wrapper
(Spec_Id
) then
7769 CW_Subp
:= Class_Preconditions_Subprogram
(Spec_Id
,
7773 Helper_Id
:= Static_Call_Helper
(CW_Subp
);
7775 F
:= First_Formal
(Helper_Id
);
7776 A
:= First_Actual
(Call_Node
);
7777 while Present
(A
) loop
7779 -- Ensure that the evaluation of the actuals will not produce
7782 Remove_Side_Effects
(A
);
7784 -- Ensure matching types to avoid reporting spurious errors since
7785 -- the called helper may have been built for a parent type.
7787 if Etype
(F
) /= Etype
(A
) then
7789 Unchecked_Convert_To
(Etype
(F
), New_Copy_Tree
(A
)));
7791 Append_To
(Actuals
, New_Copy_Tree
(A
));
7799 Make_Function_Call
(Loc
,
7800 Name
=> New_Occurrence_Of
(Helper_Id
, Loc
),
7801 Parameter_Associations
=> Actuals
);
7802 end Build_Static_Check_Helper_Call
;
7804 ------------------------------------
7805 -- Class_Preconditions_Subprogram --
7806 ------------------------------------
7808 function Class_Preconditions_Subprogram
7809 (Spec_Id
: Entity_Id
;
7810 Dynamic
: Boolean) return Node_Id
7812 Subp_Id
: constant Entity_Id
:= Ultimate_Alias
(Spec_Id
);
7815 -- Prevent cascaded errors
7817 if not Is_Dispatching_Operation
(Subp_Id
) then
7820 -- No need to search if this subprogram has the helper we are
7824 if Present
(Dynamic_Call_Helper
(Subp_Id
)) then
7828 if Present
(Static_Call_Helper
(Subp_Id
)) then
7833 -- Process inherited subprograms looking for class-wide
7837 Subps
: constant Subprogram_List
:=
7838 Inherited_Subprograms
(Subp_Id
);
7839 Subp_Id
: Entity_Id
;
7842 for Index
in Subps
'Range loop
7843 Subp_Id
:= Subps
(Index
);
7845 if Present
(Alias
(Subp_Id
)) then
7846 Subp_Id
:= Ultimate_Alias
(Subp_Id
);
7849 -- Wrappers of class-wide pre/postconditions reference the
7850 -- parent primitive that has the inherited contract.
7852 if Is_Wrapper
(Subp_Id
)
7853 and then Present
(LSP_Subprogram
(Subp_Id
))
7855 Subp_Id
:= LSP_Subprogram
(Subp_Id
);
7859 if Present
(Dynamic_Call_Helper
(Subp_Id
)) then
7863 if Present
(Static_Call_Helper
(Subp_Id
)) then
7871 end Class_Preconditions_Subprogram
;
7875 Dynamic_Check
: constant Boolean :=
7876 Present
(Controlling_Argument
(Call_Node
));
7877 Class_Subp
: Entity_Id
;
7882 -- Start of processing for Install_Class_Preconditions_Check
7885 -- Do not expand the check if we are compiling under restriction
7886 -- No_Dispatching_Calls; the semantic analyzer has previously
7887 -- notified the violation of this restriction.
7890 and then Restriction_Active
(No_Dispatching_Calls
)
7894 -- Class-wide precondition check not needed in interface thunks since
7895 -- they are installed in the dispatching call that caused invoking the
7898 elsif Is_Thunk
(Current_Scope
) then
7902 Subp
:= Entity
(Name
(Call_Node
));
7904 -- No check needed for this subprogram call if no class-wide
7905 -- preconditions apply (or if the unique available preconditions
7906 -- are ignored preconditions).
7908 Class_Subp
:= Class_Preconditions_Subprogram
(Subp
, Dynamic_Check
);
7911 or else No
(Class_Preconditions
(Class_Subp
))
7916 -- Build and install the check
7918 if Dynamic_Check
then
7919 Cond
:= Build_Dynamic_Check_Helper_Call
;
7921 Cond
:= Build_Static_Check_Helper_Call
;
7924 if Exception_Locations_Suppressed
then
7926 Make_Raise_Statement
(Loc
,
7929 (RTE
(RE_Assert_Failure
), Loc
));
7931 -- Failed check with message indicating the failed precondition and the
7932 -- call that caused it.
7936 Make_Procedure_Call_Statement
(Loc
,
7939 (RTE
(RE_Raise_Assert_Failure
), Loc
),
7940 Parameter_Associations
=>
7941 New_List
(Build_Error_Message
(Subp
)));
7944 Insert_Action
(Call_Node
,
7945 Make_If_Statement
(Loc
,
7946 Condition
=> Make_Op_Not
(Loc
, Cond
),
7947 Then_Statements
=> New_List
(Fail
)));
7948 end Install_Class_Preconditions_Check
;
7950 ------------------------------
7951 -- Is_Build_In_Place_Entity --
7952 ------------------------------
7954 function Is_Build_In_Place_Entity
(E
: Entity_Id
) return Boolean is
7955 Nam
: constant String := Get_Name_String
(Chars
(E
));
7957 function Has_Suffix
(Suffix
: String) return Boolean;
7958 -- Return True if Nam has suffix Suffix
7960 function Has_Suffix
(Suffix
: String) return Boolean is
7961 Len
: constant Natural := Suffix
'Length;
7963 return Nam
'Length > Len
7964 and then Nam
(Nam
'Last - Len
+ 1 .. Nam
'Last) = Suffix
;
7967 -- Start of processing for Is_Build_In_Place_Entity
7970 return Has_Suffix
(BIP_Alloc_Suffix
)
7971 or else Has_Suffix
(BIP_Storage_Pool_Suffix
)
7972 or else Has_Suffix
(BIP_Collection_Suffix
)
7973 or else Has_Suffix
(BIP_Task_Master_Suffix
)
7974 or else Has_Suffix
(BIP_Activation_Chain_Suffix
)
7975 or else Has_Suffix
(BIP_Object_Access_Suffix
);
7976 end Is_Build_In_Place_Entity
;
7978 --------------------------------
7979 -- Is_Build_In_Place_Function --
7980 --------------------------------
7982 function Is_Build_In_Place_Function
(E
: Entity_Id
) return Boolean is
7983 Kind
: constant Entity_Kind
:= Ekind
(E
);
7984 Typ
: constant Entity_Id
:= Etype
(E
);
7987 -- This function is called from Expand_Subtype_From_Expr during
7988 -- semantic analysis, even when expansion is off. In those cases
7989 -- the build_in_place expansion will not take place.
7991 if not Expander_Active
then
7995 -- We never use build-in-place if the convention is other than Ada,
7996 -- but note that it is OK for a build-in-place function to return a
7997 -- type with a foreign convention because the machinery ensures there
8000 return (Kind
in E_Function | E_Generic_Function
8002 (Kind
= E_Subprogram_Type
and then Typ
/= Standard_Void_Type
))
8003 and then Is_Build_In_Place_Result_Type
(Typ
)
8004 and then not Has_Foreign_Convention
(E
);
8005 end Is_Build_In_Place_Function
;
8007 -------------------------------------
8008 -- Is_Build_In_Place_Function_Call --
8009 -------------------------------------
8011 function Is_Build_In_Place_Function_Call
(N
: Node_Id
) return Boolean is
8012 Exp_Node
: constant Node_Id
:= Unqual_Conv
(N
);
8013 Function_Id
: Entity_Id
;
8016 -- Return False if the expander is currently inactive, since awareness
8017 -- of build-in-place treatment is only relevant during expansion. Note
8018 -- that Is_Build_In_Place_Function, which is called as part of this
8019 -- function, is also conditioned this way, but we need to check here as
8020 -- well to avoid blowing up on processing protected calls when expansion
8021 -- is disabled (such as with -gnatc) since those would trip over the
8022 -- raise of Program_Error below.
8024 -- In SPARK mode, build-in-place calls are not expanded, so that we
8025 -- may end up with a call that is neither resolved to an entity, nor
8026 -- an indirect call.
8028 if not Expander_Active
or else Nkind
(Exp_Node
) /= N_Function_Call
then
8032 if Is_Entity_Name
(Name
(Exp_Node
)) then
8033 Function_Id
:= Entity
(Name
(Exp_Node
));
8035 -- In the case of an explicitly dereferenced call, use the subprogram
8036 -- type generated for the dereference.
8038 elsif Nkind
(Name
(Exp_Node
)) = N_Explicit_Dereference
then
8039 Function_Id
:= Etype
(Name
(Exp_Node
));
8041 -- This may be a call to a protected function.
8043 elsif Nkind
(Name
(Exp_Node
)) = N_Selected_Component
then
8044 -- The selector in question might not have been analyzed due to a
8045 -- previous error, so analyze it here to output the appropriate
8046 -- error message instead of crashing when attempting to fetch its
8049 if not Analyzed
(Selector_Name
(Name
(Exp_Node
))) then
8050 Analyze
(Selector_Name
(Name
(Exp_Node
)));
8053 Function_Id
:= Etype
(Entity
(Selector_Name
(Name
(Exp_Node
))));
8056 raise Program_Error
;
8060 Result
: constant Boolean := Is_Build_In_Place_Function
(Function_Id
);
8061 -- So we can stop here in the debugger
8065 end Is_Build_In_Place_Function_Call
;
8067 ---------------------------------------
8068 -- Is_Function_Call_With_BIP_Formals --
8069 ---------------------------------------
8071 function Is_Function_Call_With_BIP_Formals
(N
: Node_Id
) return Boolean is
8072 Exp_Node
: constant Node_Id
:= Unqual_Conv
(N
);
8073 Function_Id
: Entity_Id
;
8076 -- Return False if the expander is currently inactive, since awareness
8077 -- of build-in-place treatment is only relevant during expansion. Note
8078 -- that Is_Build_In_Place_Function, which is called as part of this
8079 -- function, is also conditioned this way, but we need to check here as
8080 -- well to avoid blowing up on processing protected calls when expansion
8081 -- is disabled (such as with -gnatc) since those would trip over the
8082 -- raise of Program_Error below.
8084 -- In SPARK mode, build-in-place calls are not expanded, so that we
8085 -- may end up with a call that is neither resolved to an entity, nor
8086 -- an indirect call.
8088 if not Expander_Active
or else Nkind
(Exp_Node
) /= N_Function_Call
then
8092 if Is_Entity_Name
(Name
(Exp_Node
)) then
8093 Function_Id
:= Entity
(Name
(Exp_Node
));
8095 -- In the case of an explicitly dereferenced call, use the subprogram
8096 -- type generated for the dereference.
8098 elsif Nkind
(Name
(Exp_Node
)) = N_Explicit_Dereference
then
8099 Function_Id
:= Etype
(Name
(Exp_Node
));
8101 -- This may be a call to a protected function.
8103 elsif Nkind
(Name
(Exp_Node
)) = N_Selected_Component
then
8104 -- The selector in question might not have been analyzed due to a
8105 -- previous error, so analyze it here to output the appropriate
8106 -- error message instead of crashing when attempting to fetch its
8109 if not Analyzed
(Selector_Name
(Name
(Exp_Node
))) then
8110 Analyze
(Selector_Name
(Name
(Exp_Node
)));
8113 Function_Id
:= Etype
(Entity
(Selector_Name
(Name
(Exp_Node
))));
8116 raise Program_Error
;
8119 if Is_Build_In_Place_Function
(Function_Id
) then
8122 -- True also if the function has BIP Formals
8126 Kind
: constant Entity_Kind
:= Ekind
(Function_Id
);
8129 if (Kind
in E_Function | E_Generic_Function
8130 or else (Kind
= E_Subprogram_Type
8132 Etype
(Function_Id
) /= Standard_Void_Type
))
8133 and then Has_BIP_Formals
(Function_Id
)
8135 -- So we can stop here in the debugger
8142 end Is_Function_Call_With_BIP_Formals
;
8144 -----------------------------------
8145 -- Is_Build_In_Place_Result_Type --
8146 -----------------------------------
8148 function Is_Build_In_Place_Result_Type
(Typ
: Entity_Id
) return Boolean is
8150 if not Expander_Active
then
8154 -- In Ada 2005 all functions with an inherently limited return type
8155 -- must be handled using a build-in-place profile, including the case
8156 -- of a function with a limited interface result, where the function
8157 -- may return objects of nonlimited descendants.
8159 return Is_Inherently_Limited_Type
(Typ
)
8160 and then Ada_Version
>= Ada_2005
8161 and then not Debug_Flag_Dot_L
;
8162 end Is_Build_In_Place_Result_Type
;
8164 -------------------------------------
8165 -- Is_Build_In_Place_Return_Object --
8166 -------------------------------------
8168 function Is_Build_In_Place_Return_Object
(E
: Entity_Id
) return Boolean is
8170 return Is_Return_Object
(E
)
8171 and then Is_Build_In_Place_Function
(Return_Applies_To
(Scope
(E
)));
8172 end Is_Build_In_Place_Return_Object
;
8174 -----------------------------------
8175 -- Is_By_Reference_Return_Object --
8176 -----------------------------------
8178 function Is_By_Reference_Return_Object
(E
: Entity_Id
) return Boolean is
8180 return Is_Return_Object
(E
)
8181 and then Is_By_Reference_Type
(Etype
(Return_Applies_To
(Scope
(E
))));
8182 end Is_By_Reference_Return_Object
;
8184 -----------------------
8185 -- Is_Null_Procedure --
8186 -----------------------
8188 function Is_Null_Procedure
(Subp
: Entity_Id
) return Boolean is
8189 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
8192 if Ekind
(Subp
) /= E_Procedure
then
8195 -- Check if this is a declared null procedure
8197 elsif Nkind
(Decl
) = N_Subprogram_Declaration
then
8198 if not Null_Present
(Specification
(Decl
)) then
8201 elsif No
(Body_To_Inline
(Decl
)) then
8204 -- Check if the body contains only a null statement, followed by
8205 -- the return statement added during expansion.
8209 Orig_Bod
: constant Node_Id
:= Body_To_Inline
(Decl
);
8215 if Nkind
(Orig_Bod
) /= N_Subprogram_Body
then
8218 -- We must skip SCIL nodes because they are currently
8219 -- implemented as special N_Null_Statement nodes.
8223 (Statements
(Handled_Statement_Sequence
(Orig_Bod
)));
8224 Stat2
:= Next_Non_SCIL_Node
(Stat
);
8227 Is_Empty_List
(Declarations
(Orig_Bod
))
8228 and then Nkind
(Stat
) = N_Null_Statement
8232 (Nkind
(Stat2
) = N_Simple_Return_Statement
8233 and then No
(Next
(Stat2
))));
8241 end Is_Null_Procedure
;
8243 --------------------------------------
8244 -- Is_Secondary_Stack_Return_Object --
8245 --------------------------------------
8247 function Is_Secondary_Stack_Return_Object
(E
: Entity_Id
) return Boolean is
8249 return Is_Return_Object
(E
)
8250 and then Needs_Secondary_Stack
(Etype
(Return_Applies_To
(Scope
(E
))));
8251 end Is_Secondary_Stack_Return_Object
;
8253 ------------------------------
8254 -- Is_Special_Return_Object --
8255 ------------------------------
8257 function Is_Special_Return_Object
(E
: Entity_Id
) return Boolean is
8259 return Is_Build_In_Place_Return_Object
(E
)
8260 or else Is_Secondary_Stack_Return_Object
(E
)
8261 or else (Back_End_Return_Slot
8262 and then Is_By_Reference_Return_Object
(E
));
8263 end Is_Special_Return_Object
;
8265 -------------------------------------------
8266 -- Make_Build_In_Place_Call_In_Allocator --
8267 -------------------------------------------
8269 procedure Make_Build_In_Place_Call_In_Allocator
8270 (Allocator
: Node_Id
;
8271 Function_Call
: Node_Id
)
8273 Acc_Type
: constant Entity_Id
:= Etype
(Allocator
);
8274 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
8275 Func_Call
: Node_Id
:= Function_Call
;
8276 Ref_Func_Call
: Node_Id
;
8277 Function_Id
: Entity_Id
;
8278 Result_Subt
: Entity_Id
;
8279 New_Allocator
: Node_Id
;
8280 Return_Obj_Access
: Entity_Id
; -- temp for function result
8281 Temp_Init
: Node_Id
; -- initial value of Return_Obj_Access
8282 Alloc_Form
: BIP_Allocation_Form
;
8283 Pool_Actual
: Node_Id
; -- Present if Alloc_Form = User_Storage_Pool
8284 Return_Obj_Actual
: Node_Id
; -- the temp.all, in caller-allocates case
8285 Chain
: Entity_Id
; -- activation chain, in case of tasks
8288 -- Step past qualification or unchecked conversion (the latter can occur
8289 -- in cases of calls to 'Input).
8291 if Nkind
(Func_Call
) in N_Qualified_Expression
8293 | N_Unchecked_Type_Conversion
8295 Func_Call
:= Expression
(Func_Call
);
8298 -- Mark the call as processed as a build-in-place call
8300 pragma Assert
(not Is_Expanded_Build_In_Place_Call
(Func_Call
));
8301 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8303 if Is_Entity_Name
(Name
(Func_Call
)) then
8304 Function_Id
:= Entity
(Name
(Func_Call
));
8306 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8307 Function_Id
:= Etype
(Name
(Func_Call
));
8310 raise Program_Error
;
8313 Warn_BIP
(Func_Call
);
8315 Result_Subt
:= Available_View
(Etype
(Function_Id
));
8317 -- Create a temp for the function result. In the caller-allocates case,
8318 -- this will be initialized to the result of a new uninitialized
8319 -- allocator. Note: we do not use Allocator as the Related_Node of
8320 -- Return_Obj_Access in call to Make_Temporary below as this would
8321 -- create a sort of infinite "recursion".
8323 Return_Obj_Access
:= Make_Temporary
(Loc
, 'R');
8324 Set_Etype
(Return_Obj_Access
, Acc_Type
);
8325 Set_Can_Never_Be_Null
(Acc_Type
, False);
8326 -- It gets initialized to null, so we can't have that
8328 -- When the result subtype is returned on the secondary stack or is
8329 -- tagged, the called function itself must perform the allocation of
8330 -- the return object, so we pass parameters indicating that.
8332 -- But that's also the case when the result subtype needs finalization
8333 -- actions because the caller side allocation may result in undesirable
8334 -- finalization. Consider the following example:
8336 -- function Make_Lim_Ctrl return Lim_Ctrl is
8338 -- return Result : Lim_Ctrl := raise Program_Error do
8341 -- end Make_Lim_Ctrl;
8343 -- Obj : Lim_Ctrl_Ptr := new Lim_Ctrl'(Make_Lim_Ctrl);
8345 -- Even though the size of limited controlled type Lim_Ctrl is known,
8346 -- allocating Obj at the caller side will chain Obj on Lim_Ctrl_Ptr's
8347 -- finalization collection. The subsequent call to Make_Lim_Ctrl will
8348 -- fail during the initialization actions for Result, which means that
8349 -- Result (and Obj by extension) should not be finalized. However Obj
8350 -- will be finalized when access type Lim_Ctrl_Ptr goes out of scope
8351 -- since it is already attached on the its finalization collection.
8353 if Needs_BIP_Alloc_Form
(Function_Id
) then
8356 -- Case of a user-defined storage pool. Pass an allocation parameter
8357 -- indicating that the function should allocate its result in the
8358 -- pool, and pass an access to the pool. Use 'Unrestricted_Access
8359 -- because the pool may not be aliased.
8361 if Present
(Associated_Storage_Pool
(Acc_Type
)) then
8362 Alloc_Form
:= User_Storage_Pool
;
8364 Make_Attribute_Reference
(Loc
,
8367 (Associated_Storage_Pool
(Acc_Type
), Loc
),
8368 Attribute_Name
=> Name_Unrestricted_Access
);
8370 -- No user-defined pool; pass an allocation parameter indicating that
8371 -- the function should allocate its result on the heap. When there is
8372 -- a finalization collection, a pool reference is required.
8374 elsif Needs_BIP_Collection
(Function_Id
) then
8375 Alloc_Form
:= Global_Heap
;
8377 Make_Attribute_Reference
(Loc
,
8379 New_Occurrence_Of
(RTE
(RE_Global_Pool_Object
), Loc
),
8380 Attribute_Name
=> Name_Unrestricted_Access
);
8383 Alloc_Form
:= Global_Heap
;
8384 Pool_Actual
:= Empty
;
8387 -- The caller does not provide the return object in this case, so we
8388 -- have to pass null for the object access actual.
8390 Return_Obj_Actual
:= Empty
;
8392 -- When the result subtype neither is returned on the secondary stack
8393 -- nor is tagged, the return object is created on the caller side, and
8394 -- access to it is passed to the function.
8397 -- Replace the initialized allocator of form "new T'(Func (...))"
8398 -- with an uninitialized allocator of form "new T", where T is the
8399 -- result subtype of the called function. The call to the function
8400 -- is handled separately further below.
8403 Make_Allocator
(Loc
,
8404 Expression
=> New_Occurrence_Of
(Result_Subt
, Loc
));
8405 Set_No_Initialization
(New_Allocator
);
8407 -- Copy attributes to new allocator. Note that the new allocator
8408 -- logically comes from source if the original one did, so copy the
8409 -- relevant flag. This ensures proper treatment of the restriction
8410 -- No_Implicit_Heap_Allocations in this case.
8412 Set_Storage_Pool
(New_Allocator
, Storage_Pool
(Allocator
));
8413 Set_Procedure_To_Call
(New_Allocator
, Procedure_To_Call
(Allocator
));
8414 Set_Comes_From_Source
(New_Allocator
, Comes_From_Source
(Allocator
));
8416 Rewrite
(Allocator
, New_Allocator
);
8418 -- Initial value of the temp is the result of the uninitialized
8419 -- allocator. Unchecked_Convert is needed for T'Input where T is
8420 -- derived from a controlled type.
8422 Temp_Init
:= Relocate_Node
(Allocator
);
8424 if Nkind
(Function_Call
) in
8425 N_Type_Conversion | N_Unchecked_Type_Conversion
8427 Temp_Init
:= Unchecked_Convert_To
(Acc_Type
, Temp_Init
);
8430 -- Indicate that caller allocates, and pass in the return object
8432 Alloc_Form
:= Caller_Allocation
;
8433 Pool_Actual
:= Empty
;
8434 Return_Obj_Actual
:= Unchecked_Convert_To
8436 Make_Explicit_Dereference
(Loc
,
8437 Prefix
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
)));
8440 -- Declare the temp object
8442 Insert_Action
(Allocator
,
8443 Make_Object_Declaration
(Loc
,
8444 Defining_Identifier
=> Return_Obj_Access
,
8445 Object_Definition
=> New_Occurrence_Of
(Acc_Type
, Loc
),
8446 Expression
=> Temp_Init
));
8448 Ref_Func_Call
:= Make_Reference
(Loc
, Func_Call
);
8450 -- Ada 2005 (AI-251): If the type of the allocator is an interface
8451 -- then generate an implicit conversion to force displacement of the
8454 if Is_Interface
(Designated_Type
(Acc_Type
)) then
8457 OK_Convert_To
(Acc_Type
, Ref_Func_Call
));
8459 -- If the types are incompatible, we need an unchecked conversion. Note
8460 -- that the full types will be compatible, but the types not visibly
8463 elsif Nkind
(Function_Call
)
8464 in N_Type_Conversion | N_Unchecked_Type_Conversion
8466 Ref_Func_Call
:= Unchecked_Convert_To
(Acc_Type
, Ref_Func_Call
);
8470 Assign
: constant Node_Id
:=
8471 Make_Assignment_Statement
(Loc
,
8472 Name
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
),
8473 Expression
=> Ref_Func_Call
);
8474 -- Assign the result of the function call into the temp. In the
8475 -- caller-allocates case, this is overwriting the temp with its
8476 -- initial value, which has no effect. In the callee-allocates case,
8477 -- this is setting the temp to point to the object allocated by the
8478 -- callee. Unchecked_Convert is needed for T'Input where T is derived
8479 -- from a controlled type.
8482 -- Actions to be inserted. If there are no tasks, this is just the
8483 -- assignment statement. If the allocated object has tasks, we need
8484 -- to wrap the assignment in a block that activates them. The
8485 -- activation chain of that block must be passed to the function,
8486 -- rather than some outer chain.
8489 if Might_Have_Tasks
(Result_Subt
) then
8490 Actions
:= New_List
;
8491 Build_Task_Allocate_Block
8492 (Actions
, Allocator
, Init_Stmts
=> New_List
(Assign
));
8493 Chain
:= Activation_Chain_Entity
(Last
(Actions
));
8495 Actions
:= New_List
(Assign
);
8499 Insert_Actions
(Allocator
, Actions
);
8502 -- When the function has a controlling result, an allocation-form
8503 -- parameter must be passed indicating that the caller is allocating
8504 -- the result object. This is needed because such a function can be
8505 -- called as a dispatching operation and must be treated similarly
8506 -- to functions with unconstrained result subtypes.
8508 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8511 Alloc_Form
=> Alloc_Form
,
8512 Pool_Exp
=> Pool_Actual
);
8514 Add_Collection_Actual_To_Build_In_Place_Call
8515 (Func_Call
, Function_Id
, Ptr_Typ
=> Acc_Type
);
8517 Add_Task_Actuals_To_Build_In_Place_Call
8520 Master_Actual
=> Master_Id
(Acc_Type
),
8523 -- Add an implicit actual to the function call that provides access
8524 -- to the allocated object. An unchecked conversion to the (specific)
8525 -- result subtype of the function is inserted to handle cases where
8526 -- the access type of the allocator has a class-wide designated type.
8528 Add_Access_Actual_To_Build_In_Place_Call
8529 (Func_Call
, Function_Id
, Return_Obj_Actual
);
8531 -- Finally, replace the allocator node with a reference to the temp
8533 Rewrite
(Allocator
, New_Occurrence_Of
(Return_Obj_Access
, Loc
));
8535 Analyze_And_Resolve
(Allocator
, Acc_Type
);
8536 pragma Assert
(Check_Number_Of_Actuals
(Func_Call
, Function_Id
));
8537 pragma Assert
(Check_BIP_Actuals
(Func_Call
, Function_Id
));
8538 end Make_Build_In_Place_Call_In_Allocator
;
8540 ---------------------------------------------------
8541 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8542 ---------------------------------------------------
8544 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8545 (Function_Call
: Node_Id
)
8547 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
8548 Func_Call
: constant Node_Id
:= Unqual_Conv
(Function_Call
);
8549 Function_Id
: Entity_Id
;
8550 Result_Subt
: Entity_Id
;
8551 Return_Obj_Id
: Entity_Id
;
8552 Return_Obj_Decl
: Entity_Id
;
8555 -- If the call has already been processed to add build-in-place actuals
8556 -- then return. One place this can occur is for calls to build-in-place
8557 -- functions that occur within a call to a protected operation, where
8558 -- due to rewriting and expansion of the protected call there can be
8559 -- more than one call to Expand_Actuals for the same set of actuals.
8561 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
8565 -- Mark the call as processed as a build-in-place call
8567 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8569 if Is_Entity_Name
(Name
(Func_Call
)) then
8570 Function_Id
:= Entity
(Name
(Func_Call
));
8572 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8573 Function_Id
:= Etype
(Name
(Func_Call
));
8576 raise Program_Error
;
8579 Warn_BIP
(Func_Call
);
8581 Result_Subt
:= Etype
(Function_Id
);
8583 -- If the build-in-place function returns a controlled object, then the
8584 -- object needs to be finalized immediately after the context. Since
8585 -- this case produces a transient scope, the servicing finalizer needs
8586 -- to name the returned object. Create a temporary which is initialized
8587 -- with the function call:
8589 -- Temp_Id : Func_Type := BIP_Func_Call;
8591 -- The initialization expression of the temporary will be rewritten by
8592 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8593 -- Call_In_Object_Declaration.
8595 if Needs_Finalization
(Result_Subt
) then
8597 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'R');
8598 Temp_Decl
: Node_Id
;
8601 -- Reset the guard on the function call since the following does
8602 -- not perform actual call expansion.
8604 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
, False);
8607 Make_Object_Declaration
(Loc
,
8608 Defining_Identifier
=> Temp_Id
,
8609 Object_Definition
=>
8610 New_Occurrence_Of
(Result_Subt
, Loc
),
8612 New_Copy_Tree
(Function_Call
));
8614 Insert_Action
(Function_Call
, Temp_Decl
);
8616 Rewrite
(Function_Call
, New_Occurrence_Of
(Temp_Id
, Loc
));
8617 Analyze
(Function_Call
);
8620 -- When the result subtype is definite, an object of the subtype is
8621 -- declared and an access value designating it is passed as an actual.
8623 elsif Caller_Known_Size
(Func_Call
, Result_Subt
) then
8625 -- Create a temporary object to hold the function result
8627 Return_Obj_Id
:= Make_Temporary
(Loc
, 'R');
8628 Set_Etype
(Return_Obj_Id
, Result_Subt
);
8631 Make_Object_Declaration
(Loc
,
8632 Defining_Identifier
=> Return_Obj_Id
,
8633 Aliased_Present
=> True,
8634 Object_Definition
=> New_Occurrence_Of
(Result_Subt
, Loc
));
8636 Set_No_Initialization
(Return_Obj_Decl
);
8638 Insert_Action
(Func_Call
, Return_Obj_Decl
);
8640 -- When the function has a controlling result, an allocation-form
8641 -- parameter must be passed indicating that the caller is allocating
8642 -- the result object. This is needed because such a function can be
8643 -- called as a dispatching operation and must be treated similarly
8644 -- to functions with unconstrained result subtypes.
8646 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8647 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
8649 Add_Collection_Actual_To_Build_In_Place_Call
8650 (Func_Call
, Function_Id
);
8652 Add_Task_Actuals_To_Build_In_Place_Call
8653 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
8655 -- Add an implicit actual to the function call that provides access
8656 -- to the caller's return object.
8658 Add_Access_Actual_To_Build_In_Place_Call
8659 (Func_Call
, Function_Id
, New_Occurrence_Of
(Return_Obj_Id
, Loc
));
8661 pragma Assert
(Check_Number_Of_Actuals
(Func_Call
, Function_Id
));
8662 pragma Assert
(Check_BIP_Actuals
(Func_Call
, Function_Id
));
8664 -- When the result subtype is unconstrained, the function must allocate
8665 -- the return object in the secondary stack, so appropriate implicit
8666 -- parameters are added to the call to indicate that. A transient
8667 -- scope is established to ensure eventual cleanup of the result.
8670 -- Pass an allocation parameter indicating that the function should
8671 -- allocate its result on the secondary stack.
8673 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8674 (Func_Call
, Function_Id
, Alloc_Form
=> Secondary_Stack
);
8676 Add_Collection_Actual_To_Build_In_Place_Call
8677 (Func_Call
, Function_Id
);
8679 Add_Task_Actuals_To_Build_In_Place_Call
8680 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
8682 -- Pass a null value to the function since no return object is
8683 -- available on the caller side.
8685 Add_Access_Actual_To_Build_In_Place_Call
8686 (Func_Call
, Function_Id
, Empty
);
8688 pragma Assert
(Check_Number_Of_Actuals
(Func_Call
, Function_Id
));
8689 pragma Assert
(Check_BIP_Actuals
(Func_Call
, Function_Id
));
8691 end Make_Build_In_Place_Call_In_Anonymous_Context
;
8693 --------------------------------------------
8694 -- Make_Build_In_Place_Call_In_Assignment --
8695 --------------------------------------------
8697 procedure Make_Build_In_Place_Call_In_Assignment
8699 Function_Call
: Node_Id
)
8701 Func_Call
: constant Node_Id
:= Unqual_Conv
(Function_Call
);
8702 Lhs
: constant Node_Id
:= Name
(Assign
);
8703 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
8704 Func_Id
: Entity_Id
;
8707 Ptr_Typ
: Entity_Id
;
8708 Ptr_Typ_Decl
: Node_Id
;
8710 Result_Subt
: Entity_Id
;
8713 -- Mark the call as processed as a build-in-place call
8715 pragma Assert
(not Is_Expanded_Build_In_Place_Call
(Func_Call
));
8716 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8718 if Is_Entity_Name
(Name
(Func_Call
)) then
8719 Func_Id
:= Entity
(Name
(Func_Call
));
8721 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8722 Func_Id
:= Etype
(Name
(Func_Call
));
8725 raise Program_Error
;
8728 Warn_BIP
(Func_Call
);
8730 Result_Subt
:= Etype
(Func_Id
);
8732 -- When the result subtype is unconstrained, an additional actual must
8733 -- be passed to indicate that the caller is providing the return object.
8734 -- This parameter must also be passed when the called function has a
8735 -- controlling result, because dispatching calls to the function needs
8736 -- to be treated effectively the same as calls to class-wide functions.
8738 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8739 (Func_Call
, Func_Id
, Alloc_Form
=> Caller_Allocation
);
8741 Add_Collection_Actual_To_Build_In_Place_Call
8742 (Func_Call
, Func_Id
);
8744 Add_Task_Actuals_To_Build_In_Place_Call
8745 (Func_Call
, Func_Id
, Make_Identifier
(Loc
, Name_uMaster
));
8747 -- Add an implicit actual to the function call that provides access to
8748 -- the caller's return object.
8750 Add_Access_Actual_To_Build_In_Place_Call
8751 (Func_Call
, Func_Id
, Unchecked_Convert_To
(Result_Subt
, Lhs
));
8753 -- Create an access type designating the function's result subtype
8755 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
8758 Make_Full_Type_Declaration
(Loc
,
8759 Defining_Identifier
=> Ptr_Typ
,
8761 Make_Access_To_Object_Definition
(Loc
,
8762 All_Present
=> True,
8763 Subtype_Indication
=>
8764 New_Occurrence_Of
(Result_Subt
, Loc
)));
8765 Insert_After_And_Analyze
(Assign
, Ptr_Typ_Decl
);
8767 -- Finally, create an access object initialized to a reference to the
8768 -- function call. We know this access value is non-null, so mark the
8769 -- entity accordingly to suppress junk access checks.
8771 New_Expr
:= Make_Reference
(Loc
, Relocate_Node
(Func_Call
));
8773 -- Add a conversion if it's the wrong type
8775 New_Expr
:= Unchecked_Convert_To
(Ptr_Typ
, New_Expr
);
8777 Obj_Id
:= Make_Temporary
(Loc
, 'R', New_Expr
);
8778 Set_Etype
(Obj_Id
, Ptr_Typ
);
8779 Set_Is_Known_Non_Null
(Obj_Id
);
8782 Make_Object_Declaration
(Loc
,
8783 Defining_Identifier
=> Obj_Id
,
8784 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
8785 Expression
=> New_Expr
);
8786 Insert_After_And_Analyze
(Ptr_Typ_Decl
, Obj_Decl
);
8788 Rewrite
(Assign
, Make_Null_Statement
(Loc
));
8789 pragma Assert
(Check_Number_Of_Actuals
(Func_Call
, Func_Id
));
8790 pragma Assert
(Check_BIP_Actuals
(Func_Call
, Func_Id
));
8791 end Make_Build_In_Place_Call_In_Assignment
;
8793 ----------------------------------------------------
8794 -- Make_Build_In_Place_Call_In_Object_Declaration --
8795 ----------------------------------------------------
8797 procedure Make_Build_In_Place_Call_In_Object_Declaration
8798 (Obj_Decl
: Node_Id
;
8799 Function_Call
: Node_Id
)
8801 function Get_Function_Id
(Func_Call
: Node_Id
) return Entity_Id
;
8802 -- Get the value of Function_Id, below
8804 ---------------------
8805 -- Get_Function_Id --
8806 ---------------------
8808 function Get_Function_Id
(Func_Call
: Node_Id
) return Entity_Id
is
8810 if Is_Entity_Name
(Name
(Func_Call
)) then
8811 return Entity
(Name
(Func_Call
));
8813 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8814 return Etype
(Name
(Func_Call
));
8817 raise Program_Error
;
8819 end Get_Function_Id
;
8823 Func_Call
: constant Node_Id
:= Unqual_Conv
(Function_Call
);
8824 Function_Id
: constant Entity_Id
:= Get_Function_Id
(Func_Call
);
8825 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
8826 Obj_Loc
: constant Source_Ptr
:= Sloc
(Obj_Decl
);
8827 Obj_Def_Id
: constant Entity_Id
:= Defining_Identifier
(Obj_Decl
);
8828 Obj_Typ
: constant Entity_Id
:= Etype
(Obj_Def_Id
);
8829 Encl_Func
: constant Entity_Id
:= Enclosing_Subprogram
(Obj_Def_Id
);
8830 Result_Subt
: constant Entity_Id
:= Etype
(Function_Id
);
8832 Call_Deref
: Node_Id
;
8833 Caller_Object
: Node_Id
;
8834 Collection_Actual
: Node_Id
:= Empty
;
8836 Designated_Type
: Entity_Id
;
8837 Pool_Actual
: Node_Id
;
8838 Ptr_Typ
: Entity_Id
;
8839 Ptr_Typ_Decl
: Node_Id
;
8840 Pass_Caller_Acc
: Boolean := False;
8843 Definite
: constant Boolean :=
8844 Caller_Known_Size
(Func_Call
, Result_Subt
)
8845 and then not Is_Class_Wide_Type
(Obj_Typ
);
8846 -- In the case of "X : T'Class := F(...);", where F returns a
8847 -- Caller_Known_Size (specific) tagged type, we treat it as
8848 -- indefinite, because the code for the Definite case below sets the
8849 -- initialization expression of the object to Empty, which would be
8850 -- illegal Ada, and would cause gigi to misallocate X.
8852 Is_OK_Return_Object
: constant Boolean :=
8853 Is_Return_Object
(Obj_Def_Id
)
8855 not Has_Foreign_Convention
(Return_Applies_To
(Scope
(Obj_Def_Id
)));
8857 -- Start of processing for Make_Build_In_Place_Call_In_Object_Declaration
8860 -- If the call has already been processed to add build-in-place actuals
8863 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
8867 -- Mark the call as processed as a build-in-place call
8869 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8871 Warn_BIP
(Func_Call
);
8873 -- Create an access type designating the function's result subtype.
8874 -- We use the type of the original call because it may be a call to an
8875 -- inherited operation, which the expansion has replaced with the parent
8876 -- operation that yields the parent type. Note that this access type
8877 -- must be declared before we establish a transient scope, so that it
8878 -- receives the proper accessibility level.
8880 if Is_Class_Wide_Type
(Obj_Typ
)
8881 and then not Is_Interface
(Obj_Typ
)
8882 and then not Is_Class_Wide_Type
(Etype
(Function_Call
))
8884 Designated_Type
:= Obj_Typ
;
8886 Designated_Type
:= Etype
(Function_Call
);
8889 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
8891 Make_Full_Type_Declaration
(Loc
,
8892 Defining_Identifier
=> Ptr_Typ
,
8894 Make_Access_To_Object_Definition
(Loc
,
8895 All_Present
=> True,
8896 Subtype_Indication
=>
8897 New_Occurrence_Of
(Designated_Type
, Loc
)));
8899 -- The access type and its accompanying object must be inserted after
8900 -- the object declaration in the constrained case, so that the function
8901 -- call can be passed access to the object. In the indefinite case, or
8902 -- if the object declaration is for a return object, the access type and
8903 -- object must be inserted before the object, since the object
8904 -- declaration is rewritten to be a renaming of a dereference of the
8905 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8906 -- the result object is in a different (transient) scope, so won't cause
8909 if Definite
and then not Is_OK_Return_Object
then
8911 -- The presence of an address clause complicates the build-in-place
8912 -- expansion because the indicated address must be processed before
8913 -- the indirect call is generated (including the definition of a
8914 -- local pointer to the object). The address clause may come from
8915 -- an aspect specification or from an explicit attribute
8916 -- specification appearing after the object declaration. These two
8917 -- cases require different processing.
8919 if Has_Aspect
(Obj_Def_Id
, Aspect_Address
) then
8921 -- Skip non-delayed pragmas that correspond to other aspects, if
8922 -- any, to find proper insertion point for freeze node of object.
8925 D
: Node_Id
:= Obj_Decl
;
8926 N
: Node_Id
:= Next
(D
);
8930 and then Nkind
(N
) in N_Attribute_Reference | N_Pragma
8937 Insert_After
(D
, Ptr_Typ_Decl
);
8939 -- Freeze object before pointer declaration, to ensure that
8940 -- generated attribute for address is inserted at the proper
8943 Freeze_Before
(Ptr_Typ_Decl
, Obj_Def_Id
);
8946 Analyze
(Ptr_Typ_Decl
);
8948 elsif Present
(Following_Address_Clause
(Obj_Decl
)) then
8950 -- Locate explicit address clause, which may also follow pragmas
8951 -- generated by other aspect specifications.
8954 Addr
: constant Node_Id
:= Following_Address_Clause
(Obj_Decl
);
8955 D
: Node_Id
:= Next
(Obj_Decl
);
8958 while Present
(D
) loop
8964 Insert_After_And_Analyze
(Addr
, Ptr_Typ_Decl
);
8968 Insert_After_And_Analyze
(Obj_Decl
, Ptr_Typ_Decl
);
8971 Insert_Action
(Obj_Decl
, Ptr_Typ_Decl
);
8974 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
8975 -- elaborated in an inner (transient) scope and thus won't cause
8976 -- freezing by itself. It's not an itype, but it needs to be frozen
8977 -- inside the current subprogram (see Freeze_Outside in freeze.adb).
8979 Freeze_Itype
(Ptr_Typ
, Ptr_Typ_Decl
);
8981 -- If the object is a return object of an enclosing build-in-place
8982 -- function, then the implicit build-in-place parameters of the
8983 -- enclosing function are simply passed along to the called function.
8984 -- (Unfortunately, this won't cover the case of extension aggregates
8985 -- where the ancestor part is a build-in-place indefinite function
8986 -- call that should be passed along the caller's parameters.
8987 -- Currently those get mishandled by reassigning the result of the
8988 -- call to the aggregate return object, when the call result should
8989 -- really be directly built in place in the aggregate and not in a
8992 if Is_OK_Return_Object
then
8993 Pass_Caller_Acc
:= True;
8995 -- When the enclosing function has a BIP_Alloc_Form formal then we
8996 -- pass it along to the callee (such as when the enclosing function
8997 -- has an unconstrained or tagged result type).
8999 if Needs_BIP_Alloc_Form
(Encl_Func
) then
9000 if RTE_Available
(RE_Root_Storage_Pool_Ptr
) then
9003 (Build_In_Place_Formal
9004 (Encl_Func
, BIP_Storage_Pool
), Loc
);
9006 -- The build-in-place pool formal is not built on e.g. ZFP
9009 Pool_Actual
:= Empty
;
9012 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9013 (Function_Call
=> Func_Call
,
9014 Function_Id
=> Function_Id
,
9017 (Build_In_Place_Formal
(Encl_Func
, BIP_Alloc_Form
), Loc
),
9018 Pool_Exp
=> Pool_Actual
);
9020 -- Otherwise, if enclosing function has a definite result subtype,
9021 -- then caller allocation will be used.
9024 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9025 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
9028 if Needs_BIP_Collection
(Encl_Func
) then
9029 Collection_Actual
:=
9031 (Build_In_Place_Formal
9032 (Encl_Func
, BIP_Collection
), Loc
);
9035 -- Retrieve the BIPacc formal from the enclosing function and convert
9036 -- it to the access type of the callee's BIP_Object_Access formal.
9039 Unchecked_Convert_To
9040 (Etype
(Build_In_Place_Formal
(Function_Id
, BIP_Object_Access
)),
9042 (Build_In_Place_Formal
(Encl_Func
, BIP_Object_Access
), Loc
));
9044 -- In the definite case, add an implicit actual to the function call
9045 -- that provides access to the declared object. An unchecked conversion
9046 -- to the (specific) result type of the function is inserted to handle
9047 -- the case where the object is declared with a class-wide type.
9050 Caller_Object
:= Unchecked_Convert_To
9051 (Result_Subt
, New_Occurrence_Of
(Obj_Def_Id
, Loc
));
9053 -- When the function has a controlling result, an allocation-form
9054 -- parameter must be passed indicating that the caller is allocating
9055 -- the result object. This is needed because such a function can be
9056 -- called as a dispatching operation and must be treated similarly to
9057 -- functions with indefinite result subtypes.
9059 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9060 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
9062 -- The allocation for indefinite library-level objects occurs on the
9063 -- heap as opposed to the secondary stack. This accommodates DLLs where
9064 -- the secondary stack is destroyed after each library unload. This is a
9065 -- hybrid mechanism where a stack-allocated object lives on the heap.
9067 elsif Is_Library_Level_Entity
(Obj_Def_Id
)
9068 and then not Restriction_Active
(No_Implicit_Heap_Allocations
)
9070 -- Create a finalization collection for the access result type to
9071 -- ensure that the heap allocation can properly chain the object
9072 -- and later finalize it when the library unit goes out of scope.
9074 if Needs_BIP_Collection
(Func_Call
) then
9075 Build_Finalization_Collection
9077 For_Lib_Level
=> True,
9078 Insertion_Node
=> Ptr_Typ_Decl
);
9080 Collection_Actual
:=
9081 Make_Attribute_Reference
(Loc
,
9083 New_Occurrence_Of
(Finalization_Collection
(Ptr_Typ
), Loc
),
9084 Attribute_Name
=> Name_Unrestricted_Access
);
9087 Make_Attribute_Reference
(Loc
,
9089 New_Occurrence_Of
(RTE
(RE_Global_Pool_Object
), Loc
),
9090 Attribute_Name
=> Name_Unrestricted_Access
);
9093 Pool_Actual
:= Empty
;
9096 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9099 Alloc_Form
=> Global_Heap
,
9100 Pool_Exp
=> Pool_Actual
);
9101 Caller_Object
:= Empty
;
9103 -- In other indefinite cases, pass an indication to do the allocation
9104 -- on the secondary stack and set Caller_Object to Empty so that a null
9105 -- value will be passed for the caller's object address. A transient
9106 -- scope is established to ensure eventual cleanup of the result.
9109 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9110 (Func_Call
, Function_Id
, Alloc_Form
=> Secondary_Stack
);
9111 Caller_Object
:= Empty
;
9113 Establish_Transient_Scope
(Obj_Decl
, Manage_Sec_Stack
=> True);
9116 -- Pass along any finalization collection actual, which is needed in
9117 -- the case where the called function initializes a return object of
9118 -- an enclosing build-in-place function.
9120 Add_Collection_Actual_To_Build_In_Place_Call
9121 (Func_Call
, Function_Id
, Collection_Exp
=> Collection_Actual
);
9123 if Nkind
(Parent
(Obj_Decl
)) = N_Extended_Return_Statement
9124 and then Needs_BIP_Task_Actuals
(Function_Id
)
9126 -- Here we're passing along the master that was passed in to this
9129 Add_Task_Actuals_To_Build_In_Place_Call
9130 (Func_Call
, Function_Id
,
9133 (Build_In_Place_Formal
(Encl_Func
, BIP_Task_Master
), Loc
));
9136 Add_Task_Actuals_To_Build_In_Place_Call
9137 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
9140 Add_Access_Actual_To_Build_In_Place_Call
9144 Is_Access
=> Pass_Caller_Acc
);
9146 -- Finally, create an access object initialized to a reference to the
9147 -- function call. We know this access value cannot be null, so mark the
9148 -- entity accordingly to suppress the access check. We need to suppress
9149 -- warnings, because this can be part of the expansion of "for ... of"
9150 -- and similar constructs that generate finalization actions. Such
9151 -- finalization actions are safe, because they check a count that
9152 -- indicates which objects should be finalized, but the back end
9153 -- nonetheless warns about uninitialized objects.
9155 Def_Id
:= Make_Temporary
(Loc
, 'R', Func_Call
);
9156 Set_Warnings_Off
(Def_Id
);
9157 Set_Etype
(Def_Id
, Ptr_Typ
);
9158 Set_Is_Known_Non_Null
(Def_Id
);
9160 if Nkind
(Function_Call
) in N_Type_Conversion
9161 | N_Unchecked_Type_Conversion
9164 Make_Object_Declaration
(Loc
,
9165 Defining_Identifier
=> Def_Id
,
9166 Constant_Present
=> True,
9167 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
9169 Unchecked_Convert_To
9170 (Ptr_Typ
, Make_Reference
(Loc
, Relocate_Node
(Func_Call
))));
9173 Make_Object_Declaration
(Loc
,
9174 Defining_Identifier
=> Def_Id
,
9175 Constant_Present
=> True,
9176 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
9178 Make_Reference
(Loc
, Relocate_Node
(Func_Call
)));
9181 Insert_After_And_Analyze
(Ptr_Typ_Decl
, Res_Decl
);
9183 -- If the result subtype of the called function is definite and is not
9184 -- itself the return expression of an enclosing BIP function, then mark
9185 -- the object as having no initialization.
9187 if Definite
and then not Is_OK_Return_Object
then
9189 -- The related object declaration is encased in a transient block
9190 -- because the build-in-place function call contains at least one
9191 -- nested function call that produces a controlled transient
9194 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
9196 -- Since the build-in-place expansion decouples the call from the
9197 -- object declaration, the finalization machinery lacks the context
9198 -- which prompted the generation of the transient block. To resolve
9199 -- this scenario, store the build-in-place call.
9201 if Scope_Is_Transient
then
9202 Set_BIP_Initialization_Call
(Obj_Def_Id
, Res_Decl
);
9205 Set_Expression
(Obj_Decl
, Empty
);
9206 Set_No_Initialization
(Obj_Decl
);
9208 -- In case of an indefinite result subtype, or if the call is the
9209 -- return expression of an enclosing BIP function, rewrite the object
9210 -- declaration as an object renaming where the renamed object is a
9211 -- dereference of <function_Call>'reference:
9213 -- Obj : Subt renames <function_call>'Ref.all;
9217 Make_Explicit_Dereference
(Obj_Loc
,
9218 Prefix
=> New_Occurrence_Of
(Def_Id
, Obj_Loc
));
9221 Make_Object_Renaming_Declaration
(Obj_Loc
,
9222 Defining_Identifier
=> Make_Temporary
(Obj_Loc
, 'D'),
9224 New_Occurrence_Of
(Designated_Type
, Obj_Loc
),
9225 Name
=> Call_Deref
));
9227 -- At this point, Defining_Identifier (Obj_Decl) is no longer equal
9230 pragma Assert
(Ekind
(Defining_Identifier
(Obj_Decl
)) = E_Void
);
9231 Set_Renamed_Object_Of_Possibly_Void
9232 (Defining_Identifier
(Obj_Decl
), Call_Deref
);
9234 -- If the original entity comes from source, then mark the new
9235 -- entity as needing debug information, even though it's defined
9236 -- by a generated renaming that does not come from source, so that
9237 -- the Materialize_Entity flag will be set on the entity when
9238 -- Debug_Renaming_Declaration is called during analysis.
9240 if Comes_From_Source
(Obj_Def_Id
) then
9241 Set_Debug_Info_Needed
(Defining_Identifier
(Obj_Decl
));
9245 Replace_Renaming_Declaration_Id
9246 (Obj_Decl
, Original_Node
(Obj_Decl
));
9249 pragma Assert
(Check_Number_Of_Actuals
(Func_Call
, Function_Id
));
9250 pragma Assert
(Check_BIP_Actuals
(Func_Call
, Function_Id
));
9251 end Make_Build_In_Place_Call_In_Object_Declaration
;
9253 -------------------------------------------------
9254 -- Make_Build_In_Place_Iface_Call_In_Allocator --
9255 -------------------------------------------------
9257 procedure Make_Build_In_Place_Iface_Call_In_Allocator
9258 (Allocator
: Node_Id
;
9259 Function_Call
: Node_Id
)
9261 BIP_Func_Call
: constant Node_Id
:=
9262 Unqual_BIP_Iface_Function_Call
(Function_Call
);
9263 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
9265 Anon_Type
: Entity_Id
;
9270 -- No action if the call has already been processed
9272 if Is_Expanded_Build_In_Place_Call
(BIP_Func_Call
) then
9276 Tmp_Id
:= Make_Temporary
(Loc
, 'D');
9278 -- Insert a temporary before N initialized with the BIP function call
9279 -- without its enclosing type conversions and analyze it without its
9280 -- expansion. This temporary facilitates us reusing the BIP machinery,
9281 -- which takes care of adding the extra build-in-place actuals and
9282 -- transforms this object declaration into an object renaming
9285 Anon_Type
:= Create_Itype
(E_Anonymous_Access_Type
, Function_Call
);
9286 Set_Directly_Designated_Type
(Anon_Type
, Etype
(BIP_Func_Call
));
9287 Set_Etype
(Anon_Type
, Anon_Type
);
9288 Build_Class_Wide_Master
(Anon_Type
);
9291 Make_Object_Declaration
(Loc
,
9292 Defining_Identifier
=> Tmp_Id
,
9293 Object_Definition
=> New_Occurrence_Of
(Anon_Type
, Loc
),
9295 Make_Allocator
(Loc
,
9297 Make_Qualified_Expression
(Loc
,
9299 New_Occurrence_Of
(Etype
(BIP_Func_Call
), Loc
),
9300 Expression
=> New_Copy_Tree
(BIP_Func_Call
))));
9302 -- Manually set the associated node for the anonymous access type to
9303 -- be its local declaration, to avoid confusing and complicating
9304 -- the accessibility machinery.
9306 Set_Associated_Node_For_Itype
(Anon_Type
, Tmp_Decl
);
9308 Expander_Mode_Save_And_Set
(False);
9309 Insert_Action
(Allocator
, Tmp_Decl
);
9310 Expander_Mode_Restore
;
9312 Make_Build_In_Place_Call_In_Allocator
9313 (Allocator
=> Expression
(Tmp_Decl
),
9314 Function_Call
=> Expression
(Expression
(Tmp_Decl
)));
9316 -- Add a conversion to displace the pointer to the allocated object
9317 -- to reference the corresponding dispatch table.
9320 Convert_To
(Etype
(Allocator
),
9321 New_Occurrence_Of
(Tmp_Id
, Loc
)));
9322 end Make_Build_In_Place_Iface_Call_In_Allocator
;
9324 ---------------------------------------------------------
9325 -- Make_Build_In_Place_Iface_Call_In_Anonymous_Context --
9326 ---------------------------------------------------------
9328 procedure Make_Build_In_Place_Iface_Call_In_Anonymous_Context
9329 (Function_Call
: Node_Id
)
9331 BIP_Func_Call
: constant Node_Id
:=
9332 Unqual_BIP_Iface_Function_Call
(Function_Call
);
9333 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
9339 -- No action of the call has already been processed
9341 if Is_Expanded_Build_In_Place_Call
(BIP_Func_Call
) then
9345 pragma Assert
(Needs_Finalization
(Etype
(BIP_Func_Call
)));
9347 -- Insert a temporary before the call initialized with function call to
9348 -- reuse the BIP machinery which takes care of adding the extra build-in
9349 -- place actuals and transforms this object declaration into an object
9350 -- renaming declaration.
9352 Tmp_Id
:= Make_Temporary
(Loc
, 'D');
9355 Make_Object_Declaration
(Loc
,
9356 Defining_Identifier
=> Tmp_Id
,
9357 Object_Definition
=>
9358 New_Occurrence_Of
(Etype
(Function_Call
), Loc
),
9359 Expression
=> Relocate_Node
(Function_Call
));
9361 Expander_Mode_Save_And_Set
(False);
9362 Insert_Action
(Function_Call
, Tmp_Decl
);
9363 Expander_Mode_Restore
;
9365 Make_Build_In_Place_Iface_Call_In_Object_Declaration
9366 (Obj_Decl
=> Tmp_Decl
,
9367 Function_Call
=> Expression
(Tmp_Decl
));
9368 end Make_Build_In_Place_Iface_Call_In_Anonymous_Context
;
9370 ----------------------------------------------------------
9371 -- Make_Build_In_Place_Iface_Call_In_Object_Declaration --
9372 ----------------------------------------------------------
9374 procedure Make_Build_In_Place_Iface_Call_In_Object_Declaration
9375 (Obj_Decl
: Node_Id
;
9376 Function_Call
: Node_Id
)
9378 BIP_Func_Call
: constant Node_Id
:=
9379 Unqual_BIP_Iface_Function_Call
(Function_Call
);
9380 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
9381 Obj_Id
: constant Entity_Id
:= Defining_Entity
(Obj_Decl
);
9387 -- No action of the call has already been processed
9389 if Is_Expanded_Build_In_Place_Call
(BIP_Func_Call
) then
9393 Tmp_Id
:= Make_Temporary
(Loc
, 'D');
9395 -- Insert a temporary before N initialized with the BIP function call
9396 -- without its enclosing type conversions and analyze it without its
9397 -- expansion. This temporary facilitates us reusing the BIP machinery,
9398 -- which takes care of adding the extra build-in-place actuals and
9399 -- transforms this object declaration into an object renaming
9403 Make_Object_Declaration
(Loc
,
9404 Defining_Identifier
=> Tmp_Id
,
9405 Object_Definition
=>
9406 New_Occurrence_Of
(Etype
(BIP_Func_Call
), Loc
),
9407 Expression
=> New_Copy_Tree
(BIP_Func_Call
));
9409 Expander_Mode_Save_And_Set
(False);
9410 Insert_Action
(Obj_Decl
, Tmp_Decl
);
9411 Expander_Mode_Restore
;
9413 -- Inherit Is_Return_Object from the parent object to the temp object,
9414 -- so that Make_In_Build_Place_Call_In_Object_Declaration will handle
9415 -- the temp properly in cases where there's a BIP_Alloc_Form formal of
9416 -- an enclosing function that should be passed along (and which also
9417 -- ensures that if the BIP call is used as a function result and it
9418 -- requires finalization, then it will not be finalized prematurely
9421 Set_Is_Return_Object
(Tmp_Id
, Is_Return_Object
(Obj_Id
));
9423 Make_Build_In_Place_Call_In_Object_Declaration
9424 (Obj_Decl
=> Tmp_Decl
,
9425 Function_Call
=> Expression
(Tmp_Decl
));
9427 pragma Assert
(Nkind
(Tmp_Decl
) = N_Object_Renaming_Declaration
);
9429 -- Replace the original build-in-place function call by a reference to
9430 -- the resulting temporary object renaming declaration. In this way,
9431 -- all the interface conversions performed in the original Function_Call
9432 -- on the build-in-place object are preserved.
9434 Rewrite
(BIP_Func_Call
, New_Occurrence_Of
(Tmp_Id
, Loc
));
9436 -- Replace the original object declaration by an internal object
9437 -- renaming declaration. This leaves the generated code more clean (the
9438 -- build-in-place function call in an object renaming declaration and
9439 -- displacements of the pointer to the build-in-place object in another
9440 -- renaming declaration) and allows us to invoke the routine that takes
9441 -- care of replacing the identifier of the renaming declaration (routine
9442 -- originally developed for the regular build-in-place management).
9445 Make_Object_Renaming_Declaration
(Loc
,
9446 Defining_Identifier
=> Make_Temporary
(Loc
, 'D'),
9447 Subtype_Mark
=> New_Occurrence_Of
(Etype
(Obj_Id
), Loc
),
9448 Name
=> Function_Call
));
9451 Replace_Renaming_Declaration_Id
(Obj_Decl
, Original_Node
(Obj_Decl
));
9452 end Make_Build_In_Place_Iface_Call_In_Object_Declaration
;
9454 --------------------------------------------
9455 -- Make_CPP_Constructor_Call_In_Allocator --
9456 --------------------------------------------
9458 procedure Make_CPP_Constructor_Call_In_Allocator
9459 (Allocator
: Node_Id
;
9460 Function_Call
: Node_Id
)
9462 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
9463 Acc_Type
: constant Entity_Id
:= Etype
(Allocator
);
9464 Function_Id
: constant Entity_Id
:= Entity
(Name
(Function_Call
));
9465 Result_Subt
: constant Entity_Id
:= Available_View
(Etype
(Function_Id
));
9467 New_Allocator
: Node_Id
;
9468 Return_Obj_Access
: Entity_Id
;
9472 pragma Assert
(Nkind
(Allocator
) = N_Allocator
9473 and then Nkind
(Function_Call
) = N_Function_Call
);
9474 pragma Assert
(Convention
(Function_Id
) = Convention_CPP
9475 and then Is_Constructor
(Function_Id
));
9476 pragma Assert
(Is_Constrained
(Underlying_Type
(Result_Subt
)));
9478 -- Replace the initialized allocator of form "new T'(Func (...))" with
9479 -- an uninitialized allocator of form "new T", where T is the result
9480 -- subtype of the called function. The call to the function is handled
9481 -- separately further below.
9484 Make_Allocator
(Loc
,
9485 Expression
=> New_Occurrence_Of
(Result_Subt
, Loc
));
9486 Set_No_Initialization
(New_Allocator
);
9488 -- Copy attributes to new allocator. Note that the new allocator
9489 -- logically comes from source if the original one did, so copy the
9490 -- relevant flag. This ensures proper treatment of the restriction
9491 -- No_Implicit_Heap_Allocations in this case.
9493 Set_Storage_Pool
(New_Allocator
, Storage_Pool
(Allocator
));
9494 Set_Procedure_To_Call
(New_Allocator
, Procedure_To_Call
(Allocator
));
9495 Set_Comes_From_Source
(New_Allocator
, Comes_From_Source
(Allocator
));
9497 Rewrite
(Allocator
, New_Allocator
);
9499 -- Create a new access object and initialize it to the result of the
9500 -- new uninitialized allocator. Note: we do not use Allocator as the
9501 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9502 -- as this would create a sort of infinite "recursion".
9504 Return_Obj_Access
:= Make_Temporary
(Loc
, 'R');
9505 Set_Etype
(Return_Obj_Access
, Acc_Type
);
9508 -- Rnnn : constant ptr_T := new (T);
9509 -- Init (Rnn.all,...);
9512 Make_Object_Declaration
(Loc
,
9513 Defining_Identifier
=> Return_Obj_Access
,
9514 Constant_Present
=> True,
9515 Object_Definition
=> New_Occurrence_Of
(Acc_Type
, Loc
),
9516 Expression
=> Relocate_Node
(Allocator
));
9517 Insert_Action
(Allocator
, Tmp_Obj
);
9519 Insert_List_After_And_Analyze
(Tmp_Obj
,
9520 Build_Initialization_Call
(Allocator
,
9522 Make_Explicit_Dereference
(Loc
,
9523 Prefix
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
)),
9524 Typ
=> Etype
(Function_Id
),
9525 Constructor_Ref
=> Function_Call
));
9527 -- Finally, replace the allocator node with a reference to the result of
9528 -- the function call itself (which will effectively be an access to the
9529 -- object created by the allocator).
9531 Rewrite
(Allocator
, New_Occurrence_Of
(Return_Obj_Access
, Loc
));
9533 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9534 -- generate an implicit conversion to force displacement of the "this"
9537 if Is_Interface
(Designated_Type
(Acc_Type
)) then
9538 Rewrite
(Allocator
, Convert_To
(Acc_Type
, Relocate_Node
(Allocator
)));
9541 Analyze_And_Resolve
(Allocator
, Acc_Type
);
9542 end Make_CPP_Constructor_Call_In_Allocator
;
9544 ----------------------
9545 -- Might_Have_Tasks --
9546 ----------------------
9548 function Might_Have_Tasks
(Typ
: Entity_Id
) return Boolean is
9550 return not Global_No_Tasking
9551 and then not No_Run_Time_Mode
9552 and then (Has_Task
(Typ
)
9553 or else (Is_Class_Wide_Type
(Typ
)
9554 and then Is_Limited_Record
(Typ
)
9555 and then not Has_Aspect
9556 (Etype
(Typ
), Aspect_No_Task_Parts
)));
9557 end Might_Have_Tasks
;
9559 ----------------------------
9560 -- Needs_BIP_Task_Actuals --
9561 ----------------------------
9563 function Needs_BIP_Task_Actuals
(Func_Id
: Entity_Id
) return Boolean is
9564 Subp_Id
: Entity_Id
;
9565 Func_Typ
: Entity_Id
;
9568 if Global_No_Tasking
or else No_Run_Time_Mode
then
9572 -- For thunks we must rely on their target entity; otherwise, given that
9573 -- the profile of thunks for functions returning a limited interface
9574 -- type returns a class-wide type, we would erroneously add these extra
9577 if Is_Thunk
(Func_Id
) then
9578 Subp_Id
:= Thunk_Target
(Func_Id
);
9586 Func_Typ
:= Underlying_Type
(Etype
(Subp_Id
));
9588 -- Functions returning types with foreign convention don't have extra
9591 if Has_Foreign_Convention
(Func_Typ
) then
9594 -- At first sight, for all the following cases, we could add assertions
9595 -- to ensure that if Func_Id is frozen then the computed result matches
9596 -- with the availability of the task master extra formal; unfortunately
9597 -- this is not feasible because we may be precisely freezing this entity
9598 -- (that is, Is_Frozen has been set by Freeze_Entity but it has not
9599 -- completed its work).
9601 elsif Has_Task
(Func_Typ
) then
9604 elsif Ekind
(Func_Id
) = E_Function
then
9605 return Might_Have_Tasks
(Func_Typ
);
9607 -- Handle subprogram type internally generated for dispatching call. We
9608 -- cannot rely on the return type of the subprogram type of dispatching
9609 -- calls since it is always a class-wide type (cf. Expand_Dispatching_
9612 elsif Ekind
(Func_Id
) = E_Subprogram_Type
then
9613 if Is_Dispatch_Table_Entity
(Func_Id
) then
9614 return Has_BIP_Extra_Formal
(Func_Id
, BIP_Task_Master
);
9616 return Might_Have_Tasks
(Func_Typ
);
9620 raise Program_Error
;
9622 end Needs_BIP_Task_Actuals
;
9624 --------------------------
9625 -- Needs_BIP_Collection --
9626 --------------------------
9628 function Needs_BIP_Collection
(Func_Id
: Entity_Id
) return Boolean is
9629 Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
9632 -- A formal for the finalization collection is needed for build-in-place
9633 -- functions whose result type needs finalization or is a tagged type.
9634 -- Tagged primitive build-in-place functions need such a formal because
9635 -- they can be called by a dispatching call, and extensions may require
9636 -- finalization even if the root type doesn't. This means nonprimitive
9637 -- build-in-place functions with tagged results also need it, since such
9638 -- functions can be called via access-to-function types, and those can
9639 -- be used to call primitives, so the formal needs to be passed to all
9640 -- such build-in-place functions, primitive or not.
9642 return not Restriction_Active
(No_Finalization
)
9643 and then (Needs_Finalization
(Typ
) or else Is_Tagged_Type
(Typ
))
9644 and then not Has_Foreign_Convention
(Typ
);
9645 end Needs_BIP_Collection
;
9647 --------------------------
9648 -- Needs_BIP_Alloc_Form --
9649 --------------------------
9651 function Needs_BIP_Alloc_Form
(Func_Id
: Entity_Id
) return Boolean is
9652 Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
9655 -- See Make_Build_In_Place_Call_In_Allocator for the rationale
9657 if Needs_BIP_Collection
(Func_Id
) then
9661 -- A formal giving the allocation method is needed for build-in-place
9662 -- functions whose result type is returned on the secondary stack or
9663 -- is a tagged type. Tagged primitive build-in-place functions need
9664 -- such a formal because they can be called by a dispatching call, and
9665 -- the secondary stack is always used for dispatching-on-result calls.
9666 -- This means nonprimitive build-in-place functions with tagged results
9667 -- also need it, as such functions can be called via access-to-function
9668 -- types, and those can be used to call primitives, so the formal needs
9669 -- to be passed to all such build-in-place functions, primitive or not.
9671 -- We never use build-in-place if the function has foreign convention,
9672 -- but note that it is OK for a build-in-place function to return a
9673 -- type with a foreign convention because the machinery ensures there
9676 return not Restriction_Active
(No_Secondary_Stack
)
9677 and then (Needs_Secondary_Stack
(Typ
) or else Is_Tagged_Type
(Typ
))
9678 and then not Has_Foreign_Convention
(Func_Id
);
9679 end Needs_BIP_Alloc_Form
;
9681 -------------------------------------
9682 -- Replace_Renaming_Declaration_Id --
9683 -------------------------------------
9685 procedure Replace_Renaming_Declaration_Id
9686 (New_Decl
: Node_Id
;
9687 Orig_Decl
: Node_Id
)
9689 New_Id
: constant Entity_Id
:= Defining_Entity
(New_Decl
);
9690 Orig_Id
: constant Entity_Id
:= Defining_Entity
(Orig_Decl
);
9693 Set_Chars
(New_Id
, Chars
(Orig_Id
));
9695 -- Swap next entity links in preparation for exchanging entities
9698 Next_Id
: constant Entity_Id
:= Next_Entity
(New_Id
);
9700 Link_Entities
(New_Id
, Next_Entity
(Orig_Id
));
9701 Link_Entities
(Orig_Id
, Next_Id
);
9704 Set_Homonym
(New_Id
, Homonym
(Orig_Id
));
9705 Exchange_Entities
(New_Id
, Orig_Id
);
9707 -- Preserve source indication of original declaration, so that xref
9708 -- information is properly generated for the right entity.
9710 Preserve_Comes_From_Source
(New_Decl
, Orig_Decl
);
9711 Preserve_Comes_From_Source
(Orig_Id
, Orig_Decl
);
9713 Set_Comes_From_Source
(New_Id
, False);
9715 -- Preserve aliased indication
9717 Set_Is_Aliased
(Orig_Id
, Is_Aliased
(New_Id
));
9718 end Replace_Renaming_Declaration_Id
;
9720 ---------------------------------
9721 -- Rewrite_Function_Call_For_C --
9722 ---------------------------------
9724 procedure Rewrite_Function_Call_For_C
(N
: Node_Id
) is
9725 Orig_Func
: constant Entity_Id
:= Entity
(Name
(N
));
9726 Func_Id
: constant Entity_Id
:= Ultimate_Alias
(Orig_Func
);
9727 Par
: constant Node_Id
:= Parent
(N
);
9728 Proc_Id
: constant Entity_Id
:= Corresponding_Procedure
(Func_Id
);
9729 Loc
: constant Source_Ptr
:= Sloc
(Par
);
9731 Last_Actual
: Node_Id
;
9732 Last_Formal
: Entity_Id
;
9734 -- Start of processing for Rewrite_Function_Call_For_C
9737 -- The actuals may be given by named associations, so the added actual
9738 -- that is the target of the return value of the call must be a named
9739 -- association as well, so we retrieve the name of the generated
9742 Last_Formal
:= First_Formal
(Proc_Id
);
9743 while Present
(Next_Formal
(Last_Formal
)) loop
9744 Next_Formal
(Last_Formal
);
9747 Actuals
:= Parameter_Associations
(N
);
9749 -- The original function may lack parameters
9751 if No
(Actuals
) then
9752 Actuals
:= New_List
;
9755 -- If the function call is the expression of an assignment statement,
9756 -- transform the assignment into a procedure call. Generate:
9758 -- LHS := Func_Call (...);
9760 -- Proc_Call (..., LHS);
9762 -- If function is inherited, a conversion may be necessary.
9764 if Nkind
(Par
) = N_Assignment_Statement
then
9765 Last_Actual
:= Name
(Par
);
9767 if not Comes_From_Source
(Orig_Func
)
9768 and then Etype
(Orig_Func
) /= Etype
(Func_Id
)
9771 Make_Type_Conversion
(Loc
,
9772 New_Occurrence_Of
(Etype
(Func_Id
), Loc
),
9777 Make_Parameter_Association
(Loc
,
9779 Make_Identifier
(Loc
, Chars
(Last_Formal
)),
9780 Explicit_Actual_Parameter
=> Last_Actual
));
9783 Make_Procedure_Call_Statement
(Loc
,
9784 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
9785 Parameter_Associations
=> Actuals
));
9788 -- Otherwise the context is an expression. Generate a temporary and a
9789 -- procedure call to obtain the function result. Generate:
9791 -- ... Func_Call (...) ...
9794 -- Proc_Call (..., Temp);
9799 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
9808 Make_Object_Declaration
(Loc
,
9809 Defining_Identifier
=> Temp_Id
,
9810 Object_Definition
=>
9811 New_Occurrence_Of
(Etype
(Func_Id
), Loc
));
9814 -- Proc_Call (..., Temp);
9817 Make_Parameter_Association
(Loc
,
9819 Make_Identifier
(Loc
, Chars
(Last_Formal
)),
9820 Explicit_Actual_Parameter
=>
9821 New_Occurrence_Of
(Temp_Id
, Loc
)));
9824 Make_Procedure_Call_Statement
(Loc
,
9825 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
9826 Parameter_Associations
=> Actuals
);
9828 Insert_Actions
(Par
, New_List
(Decl
, Call
));
9829 Rewrite
(N
, New_Occurrence_Of
(Temp_Id
, Loc
));
9832 end Rewrite_Function_Call_For_C
;
9834 ------------------------------------
9835 -- Set_Enclosing_Sec_Stack_Return --
9836 ------------------------------------
9838 procedure Set_Enclosing_Sec_Stack_Return
(N
: Node_Id
) is
9842 -- Due to a possible mix of internally generated blocks, source blocks
9843 -- and loops, the scope stack may not be contiguous as all labels are
9844 -- inserted at the top level within the related function. Instead,
9845 -- perform a parent-based traversal and mark all appropriate constructs.
9847 while Present
(P
) loop
9849 -- Mark the label of a source or internally generated block or
9852 if Nkind
(P
) in N_Block_Statement | N_Loop_Statement
then
9853 Set_Sec_Stack_Needed_For_Return
(Entity
(Identifier
(P
)));
9855 -- Mark the enclosing function
9857 elsif Nkind
(P
) = N_Subprogram_Body
then
9858 if Present
(Corresponding_Spec
(P
)) then
9859 Set_Sec_Stack_Needed_For_Return
(Corresponding_Spec
(P
));
9861 Set_Sec_Stack_Needed_For_Return
(Defining_Entity
(P
));
9864 -- Do not go beyond the enclosing function
9871 end Set_Enclosing_Sec_Stack_Return
;
9873 ------------------------------------
9874 -- Unqual_BIP_Iface_Function_Call --
9875 ------------------------------------
9877 function Unqual_BIP_Iface_Function_Call
(Expr
: Node_Id
) return Node_Id
is
9878 Has_Pointer_Displacement
: Boolean := False;
9879 On_Object_Declaration
: Boolean := False;
9880 -- Remember if processing the renaming expressions on recursion we have
9881 -- traversed an object declaration, since we can traverse many object
9882 -- declaration renamings but just one regular object declaration.
9884 function Unqual_BIP_Function_Call
(Expr
: Node_Id
) return Node_Id
;
9885 -- Search for a build-in-place function call skipping any qualification
9886 -- including qualified expressions, type conversions, references, calls
9887 -- to displace the pointer to the object, and renamings. Return Empty if
9888 -- no build-in-place function call is found.
9890 ------------------------------
9891 -- Unqual_BIP_Function_Call --
9892 ------------------------------
9894 function Unqual_BIP_Function_Call
(Expr
: Node_Id
) return Node_Id
is
9896 -- Recurse to handle case of multiple levels of qualification and/or
9899 if Nkind
(Expr
) in N_Qualified_Expression
9901 | N_Unchecked_Type_Conversion
9903 return Unqual_BIP_Function_Call
(Expression
(Expr
));
9905 -- Recurse to handle case of multiple levels of references and
9906 -- explicit dereferences.
9908 elsif Nkind
(Expr
) in N_Attribute_Reference
9909 | N_Explicit_Dereference
9912 return Unqual_BIP_Function_Call
(Prefix
(Expr
));
9914 -- Recurse on object renamings
9916 elsif Nkind
(Expr
) = N_Identifier
9917 and then Present
(Entity
(Expr
))
9918 and then Ekind
(Entity
(Expr
)) in E_Constant | E_Variable
9919 and then Nkind
(Parent
(Entity
(Expr
))) =
9920 N_Object_Renaming_Declaration
9921 and then Present
(Renamed_Object
(Entity
(Expr
)))
9923 return Unqual_BIP_Function_Call
(Renamed_Object
(Entity
(Expr
)));
9925 -- Recurse on the initializing expression of the first reference of
9926 -- an object declaration.
9928 elsif not On_Object_Declaration
9929 and then Nkind
(Expr
) = N_Identifier
9930 and then Present
(Entity
(Expr
))
9931 and then Ekind
(Entity
(Expr
)) in E_Constant | E_Variable
9932 and then Nkind
(Parent
(Entity
(Expr
))) = N_Object_Declaration
9933 and then Present
(Expression
(Parent
(Entity
(Expr
))))
9935 On_Object_Declaration
:= True;
9937 Unqual_BIP_Function_Call
(Expression
(Parent
(Entity
(Expr
))));
9939 -- Recurse to handle calls to displace the pointer to the object to
9940 -- reference a secondary dispatch table.
9942 elsif Nkind
(Expr
) = N_Function_Call
9943 and then Nkind
(Name
(Expr
)) in N_Has_Entity
9944 and then Present
(Entity
(Name
(Expr
)))
9945 and then Is_RTE
(Entity
(Name
(Expr
)), RE_Displace
)
9947 Has_Pointer_Displacement
:= True;
9949 Unqual_BIP_Function_Call
(First
(Parameter_Associations
(Expr
)));
9951 -- Normal case: check if the inner expression is a BIP function call
9952 -- and the pointer to the object is displaced.
9954 elsif Has_Pointer_Displacement
9955 and then Is_Build_In_Place_Function_Call
(Expr
)
9962 end Unqual_BIP_Function_Call
;
9964 -- Start of processing for Unqual_BIP_Iface_Function_Call
9967 if Nkind
(Expr
) = N_Identifier
and then No
(Entity
(Expr
)) then
9969 -- Can happen for X'Elab_Spec in the binder-generated file
9974 return Unqual_BIP_Function_Call
(Expr
);
9975 end Unqual_BIP_Iface_Function_Call
;
9977 -------------------------------
9978 -- Validate_Subprogram_Calls --
9979 -------------------------------
9981 procedure Validate_Subprogram_Calls
(N
: Node_Id
) is
9983 function Process_Node
(Nod
: Node_Id
) return Traverse_Result
;
9984 -- Function to traverse the subtree of N using Traverse_Proc.
9990 function Process_Node
(Nod
: Node_Id
) return Traverse_Result
is
9993 when N_Entry_Call_Statement
9994 | N_Procedure_Call_Statement
9998 Call_Node
: Node_Id
renames Nod
;
10002 -- Call using access to subprogram with explicit dereference
10004 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
10005 Subp
:= Etype
(Name
(Call_Node
));
10007 -- Prefix notation calls
10009 elsif Nkind
(Name
(Call_Node
)) = N_Selected_Component
then
10010 Subp
:= Entity
(Selector_Name
(Name
(Call_Node
)));
10012 -- Call to member of entry family, where Name is an indexed
10013 -- component, with the prefix being a selected component
10014 -- giving the task and entry family name, and the index
10015 -- being the entry index.
10017 elsif Nkind
(Name
(Call_Node
)) = N_Indexed_Component
then
10019 Entity
(Selector_Name
(Prefix
(Name
(Call_Node
))));
10024 Subp
:= Entity
(Name
(Call_Node
));
10027 pragma Assert
(Check_BIP_Actuals
(Call_Node
, Subp
));
10030 -- Skip generic bodies
10032 when N_Package_Body
=>
10033 if Ekind
(Unique_Defining_Entity
(Nod
)) = E_Generic_Package
then
10037 when N_Subprogram_Body
=>
10038 if Ekind
(Unique_Defining_Entity
(Nod
)) in E_Generic_Function
10039 | E_Generic_Procedure
10044 -- Nodes we want to ignore
10046 -- Skip calls placed in the full declaration of record types since
10047 -- the call will be performed by their Init Proc; for example,
10048 -- calls initializing default values of discriminants or calls
10049 -- providing the initial value of record type components. Other
10050 -- full type declarations are processed because they may have
10051 -- calls that must be checked. For example:
10053 -- type T is array (1 .. Some_Function_Call (...)) of Some_Type;
10055 -- ??? More work needed here to handle the following case:
10057 -- type Rec is record
10058 -- F : String (1 .. <some complicated expression>);
10061 when N_Full_Type_Declaration
=>
10062 if Is_Record_Type
(Defining_Entity
(Nod
)) then
10066 -- Skip calls placed in subprogram specifications since function
10067 -- calls initializing default parameter values will be processed
10068 -- when the call to the subprogram is found (if the default actual
10069 -- parameter is required), and calls found in aspects will be
10070 -- processed when their corresponding pragma is found, or in the
10071 -- specific case of class-wide pre-/postconditions, when their
10072 -- helpers are found.
10074 when N_Procedure_Specification
10075 | N_Function_Specification
10079 when N_Abstract_Subprogram_Declaration
10080 | N_Aspect_Specification
10084 | N_Enumeration_Representation_Clause
10085 | N_Enumeration_Type_Definition
10086 | N_Function_Instantiation
10087 | N_Freeze_Generic_Entity
10088 | N_Generic_Function_Renaming_Declaration
10089 | N_Generic_Package_Renaming_Declaration
10090 | N_Generic_Procedure_Renaming_Declaration
10091 | N_Generic_Package_Declaration
10092 | N_Generic_Subprogram_Declaration
10093 | N_Itype_Reference
10094 | N_Number_Declaration
10095 | N_Package_Instantiation
10096 | N_Package_Renaming_Declaration
10098 | N_Procedure_Instantiation
10099 | N_Protected_Type_Declaration
10100 | N_Record_Representation_Clause
10101 | N_Validate_Unchecked_Conversion
10102 | N_Variable_Reference_Marker
10103 | N_Use_Package_Clause
10104 | N_Use_Type_Clause
10116 procedure Check_Calls
is new Traverse_Proc
(Process_Node
);
10118 -- Start of processing for Validate_Subprogram_Calls
10121 -- No action required if we are not generating code or compiling sources
10122 -- that have errors.
10124 if Serious_Errors_Detected
> 0
10125 or else Operating_Mode
/= Generate_Code
10131 end Validate_Subprogram_Calls
;
10137 procedure Warn_BIP
(Func_Call
: Node_Id
) is
10139 if Debug_Flag_Underscore_BB
then
10140 Error_Msg_N
("build-in-place function call??", Func_Call
);