1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Accessibility
; use Accessibility
;
27 with Atree
; use Atree
;
28 with Aspects
; use Aspects
;
29 with Checks
; use Checks
;
30 with Debug
; use Debug
;
31 with Einfo
; use Einfo
;
32 with Einfo
.Entities
; use Einfo
.Entities
;
33 with Einfo
.Utils
; use Einfo
.Utils
;
34 with Errout
; use Errout
;
35 with Elists
; use Elists
;
36 with Expander
; use Expander
;
37 with Exp_Aggr
; use Exp_Aggr
;
38 with Exp_Atag
; use Exp_Atag
;
39 with Exp_Ch3
; use Exp_Ch3
;
40 with Exp_Ch4
; use Exp_Ch4
;
41 with Exp_Ch7
; use Exp_Ch7
;
42 with Exp_Ch9
; use Exp_Ch9
;
43 with Exp_Dbug
; use Exp_Dbug
;
44 with Exp_Disp
; use Exp_Disp
;
45 with Exp_Dist
; use Exp_Dist
;
46 with Exp_Intr
; use Exp_Intr
;
47 with Exp_Pakd
; use Exp_Pakd
;
48 with Exp_Tss
; use Exp_Tss
;
49 with Exp_Util
; use Exp_Util
;
50 with Freeze
; use Freeze
;
51 with Inline
; use Inline
;
52 with Itypes
; use Itypes
;
54 with Namet
; use Namet
;
55 with Nlists
; use Nlists
;
56 with Nmake
; use Nmake
;
58 with Restrict
; use Restrict
;
59 with Rident
; use Rident
;
60 with Rtsfind
; use Rtsfind
;
62 with Sem_Aux
; use Sem_Aux
;
63 with Sem_Ch6
; use Sem_Ch6
;
64 with Sem_Ch8
; use Sem_Ch8
;
65 with Sem_Ch13
; use Sem_Ch13
;
66 with Sem_Dim
; use Sem_Dim
;
67 with Sem_Disp
; use Sem_Disp
;
68 with Sem_Dist
; use Sem_Dist
;
69 with Sem_Eval
; use Sem_Eval
;
70 with Sem_Mech
; use Sem_Mech
;
71 with Sem_Res
; use Sem_Res
;
72 with Sem_SCIL
; use Sem_SCIL
;
73 with Sem_Util
; use Sem_Util
;
74 use Sem_Util
.Storage_Model_Support
;
75 with Sinfo
; use Sinfo
;
76 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
77 with Sinfo
.Utils
; use Sinfo
.Utils
;
78 with Sinput
; use Sinput
;
79 with Snames
; use Snames
;
80 with Stand
; use Stand
;
81 with Stringt
; use Stringt
;
82 with Tbuild
; use Tbuild
;
83 with Uintp
; use Uintp
;
84 with Validsw
; use Validsw
;
86 package body Exp_Ch6
is
88 --------------------------------
89 -- Function return mechanisms --
90 --------------------------------
92 -- This is a summary of the various function return mechanisms implemented
93 -- in GNAT for Ada 2005 and later versions of the language. In the below
94 -- table, the first column must be read as an if expression: if the result
95 -- type of the function is limited, then the return mechanism is and ...;
96 -- elsif the result type is indefinite or large definite, then ...; elsif
97 -- ...; else ... The different mechanisms are implemented either in the
98 -- front end, or in the back end, or partly in both ends, depending on the
101 -- Result type | Return mechanism | Front end | Back end
102 -- --------------------------------------------------------------------
104 -- Limited Build In Place All
106 -- Indefinite/ Secondary Stack Needs Fin. Others
109 -- Needs Fin. Secondary Stack All
112 -- Needs Fin. Invisible Parameter All All
113 -- (BERS True) (return) (call)
115 -- By Reference Invisible Parameter All
117 -- Others Primary stack/ All
120 -- Needs Fin.: type needs finalization [RM 7.6(9.1/2-9.6/2)]
121 -- BERS: Opt.Back_End_Return_Slot setting
123 -- The table is valid for all calls except for those dispatching on result;
124 -- the latter calls are considered as returning a class-wide type and thus
125 -- always return on the secondary stack, with the help of a small wrapper
126 -- function (thunk) if the original result type is not itself returned on
127 -- the secondary stack as per the above table.
129 -- Suffixes for Build-In-Place extra formals
131 BIP_Alloc_Suffix
: constant String := "BIPalloc";
132 BIP_Storage_Pool_Suffix
: constant String := "BIPstoragepool";
133 BIP_Finalization_Master_Suffix
: constant String := "BIPfinalizationmaster";
134 BIP_Task_Master_Suffix
: constant String := "BIPtaskmaster";
135 BIP_Activation_Chain_Suffix
: constant String := "BIPactivationchain";
136 BIP_Object_Access_Suffix
: constant String := "BIPaccess";
138 -----------------------
139 -- Local Subprograms --
140 -----------------------
142 procedure Add_Access_Actual_To_Build_In_Place_Call
143 (Function_Call
: Node_Id
;
144 Function_Id
: Entity_Id
;
145 Return_Object
: Node_Id
;
146 Is_Access
: Boolean := False);
147 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
148 -- object name given by Return_Object and add the attribute to the end of
149 -- the actual parameter list associated with the build-in-place function
150 -- call denoted by Function_Call. However, if Is_Access is True, then
151 -- Return_Object is already an access expression, in which case it's passed
152 -- along directly to the build-in-place function. Finally, if Return_Object
153 -- is empty, then pass a null literal as the actual.
155 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
156 (Function_Call
: Node_Id
;
157 Function_Id
: Entity_Id
;
158 Alloc_Form
: BIP_Allocation_Form
:= Unspecified
;
159 Alloc_Form_Exp
: Node_Id
:= Empty
;
160 Pool_Actual
: Node_Id
:= Make_Null
(No_Location
));
161 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
162 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
163 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
164 -- otherwise pass a literal corresponding to the Alloc_Form parameter
165 -- (which must not be Unspecified in that case). Pool_Actual is the
166 -- parameter to pass to BIP_Storage_Pool.
168 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
169 (Func_Call
: Node_Id
;
171 Ptr_Typ
: Entity_Id
:= Empty
;
172 Master_Exp
: Node_Id
:= Empty
);
173 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
174 -- finalization actions, add an actual parameter which is a pointer to the
175 -- finalization master of the caller. If Master_Exp is not Empty, then that
176 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
177 -- will result in an automatic "null" value for the actual.
179 procedure Add_Task_Actuals_To_Build_In_Place_Call
180 (Function_Call
: Node_Id
;
181 Function_Id
: Entity_Id
;
182 Master_Actual
: Node_Id
;
183 Chain
: Node_Id
:= Empty
);
184 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
185 -- contains tasks, add two actual parameters: the master, and a pointer to
186 -- the caller's activation chain. Master_Actual is the actual parameter
187 -- expression to pass for the master. In most cases, this is the current
188 -- master (_master). The two exceptions are: If the function call is the
189 -- initialization expression for an allocator, we pass the master of the
190 -- access type. If the function call is the initialization expression for a
191 -- return object, we pass along the master passed in by the caller. In most
192 -- contexts, the activation chain to pass is the local one, which is
193 -- indicated by No (Chain). However, in an allocator, the caller passes in
194 -- the activation Chain. Note: Master_Actual can be Empty, but only if
195 -- there are no tasks.
197 function Caller_Known_Size
198 (Func_Call
: Node_Id
;
199 Result_Subt
: Entity_Id
) return Boolean;
200 -- True if result subtype is definite or has a size that does not require
201 -- secondary stack usage (i.e. no variant part or components whose type
202 -- depends on discriminants). In particular, untagged types with only
203 -- access discriminants do not require secondary stack use. Note we must
204 -- always use the secondary stack for dispatching-on-result calls.
206 function Check_BIP_Actuals
207 (Subp_Call
: Node_Id
;
208 Subp_Id
: Entity_Id
) return Boolean;
209 -- Given a subprogram call to the given subprogram return True if the
210 -- names of BIP extra actual and formal parameters match, and the number
211 -- of actuals (including extra actuals) matches the number of formals.
213 function Check_Number_Of_Actuals
214 (Subp_Call
: Node_Id
;
215 Subp_Id
: Entity_Id
) return Boolean;
216 -- Given a subprogram call to the given subprogram return True if the
217 -- number of actual parameters (including extra actuals) is correct.
219 procedure Check_Overriding_Operation
(Subp
: Entity_Id
);
220 -- Subp is a dispatching operation. Check whether it may override an
221 -- inherited private operation, in which case its DT entry is that of
222 -- the hidden operation, not the one it may have received earlier.
223 -- This must be done before emitting the code to set the corresponding
224 -- DT to the address of the subprogram. The actual placement of Subp in
225 -- the proper place in the list of primitive operations is done in
226 -- Declare_Inherited_Private_Subprograms, which also has to deal with
227 -- implicit operations. This duplication is unavoidable for now???
229 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
);
230 -- This procedure is called only if the subprogram body N, whose spec
231 -- has the given entity Spec, contains a parameterless recursive call.
232 -- It attempts to generate runtime code to detect if this a case of
233 -- infinite recursion.
235 -- The body is scanned to determine dependencies. If the only external
236 -- dependencies are on a small set of scalar variables, then the values
237 -- of these variables are captured on entry to the subprogram, and if
238 -- the values are not changed for the call, we know immediately that
239 -- we have an infinite recursion.
241 procedure Expand_Actuals
244 Post_Call
: out List_Id
);
245 -- Return a list of actions to take place after the call in Post_Call. The
246 -- call will later be rewritten as an Expression_With_Actions, with the
247 -- Post_Call actions inserted, and the call inside.
249 -- For each actual of an in-out or out parameter which is a numeric (view)
250 -- conversion of the form T (A), where A denotes a variable, we insert the
253 -- Temp : T[ := T (A)];
255 -- prior to the call. Then we replace the actual with a reference to Temp,
256 -- and append the assignment:
258 -- A := TypeA (Temp);
260 -- after the call. Here TypeA is the actual type of variable A. For out
261 -- parameters, the initial declaration has no expression. If A is not an
262 -- entity name, we generate instead:
264 -- Var : TypeA renames A;
265 -- Temp : T := Var; -- omitting expression for out parameter.
267 -- Var := TypeA (Temp);
269 -- For other in-out parameters, we emit the required constraint checks
270 -- before and/or after the call.
272 -- For all parameter modes, actuals that denote components and slices of
273 -- packed arrays are expanded into suitable temporaries.
275 -- For nonscalar objects that are possibly unaligned, add call by copy code
276 -- (copy in for IN and IN OUT, copy out for OUT and IN OUT).
278 -- For OUT and IN OUT parameters, add predicate checks after the call
279 -- based on the predicates of the actual type.
281 procedure Expand_Call_Helper
(N
: Node_Id
; Post_Call
: out List_Id
);
282 -- Does the main work of Expand_Call. Post_Call is as for Expand_Actuals.
284 procedure Expand_Ctrl_Function_Call
(N
: Node_Id
; Use_Sec_Stack
: Boolean);
285 -- N is a function call which returns a controlled object. Transform the
286 -- call into a temporary which retrieves the returned object from the
287 -- primary or secondary stack (Use_Sec_Stack says which) using 'reference.
289 procedure Expand_Non_Function_Return
(N
: Node_Id
);
290 -- Expand a simple return statement found in a procedure body, entry body,
291 -- accept statement, or an extended return statement. Note that all non-
292 -- function returns are simple return statements.
294 function Expand_Protected_Object_Reference
296 Scop
: Entity_Id
) return Node_Id
;
298 procedure Expand_Protected_Subprogram_Call
302 -- A call to a protected subprogram within the protected object may appear
303 -- as a regular call. The list of actuals must be expanded to contain a
304 -- reference to the object itself, and the call becomes a call to the
305 -- corresponding protected subprogram.
307 procedure Expand_Simple_Function_Return
(N
: Node_Id
);
308 -- Expand simple return from function. In the case where we are returning
309 -- from a function body this is called by Expand_N_Simple_Return_Statement.
311 procedure Insert_Post_Call_Actions
(N
: Node_Id
; Post_Call
: List_Id
);
312 -- Insert the Post_Call list previously produced by routine Expand_Actuals
313 -- or Expand_Call_Helper into the tree.
315 function Is_True_Build_In_Place_Function_Call
(N
: Node_Id
) return Boolean;
316 -- Ada 2005 (AI-318-02): Returns True if N denotes a call to a function
317 -- that requires handling as a build-in-place call; returns False for
318 -- non-BIP function calls and also for calls to functions with inherited
319 -- BIP formals that do not require BIP formals. For example:
321 -- type Iface is limited interface;
322 -- function Get_Object return Iface;
323 -- -- This function has BIP extra formals
325 -- type Root1 is limited tagged record ...
326 -- type T1 is new Root1 and Iface with ...
327 -- function Get_Object return T1;
328 -- -- This primitive requires the BIP formals, and the evaluation of
329 -- -- Is_True_Build_In_Place_Function_Call returns True.
331 -- type Root2 is tagged record ...
332 -- type T2 is new Root2 and Iface with ...
333 -- function Get_Object return T2;
334 -- -- This primitive inherits the BIP formals of the interface primitive
335 -- -- but, given that T2 is not a limited type, it does not require such
336 -- -- formals; therefore Is_True_Build_In_Place_Function_Call returns
339 procedure Replace_Renaming_Declaration_Id
341 Orig_Decl
: Node_Id
);
342 -- Replace the internal identifier of the new renaming declaration New_Decl
343 -- with the identifier of its original declaration Orig_Decl exchanging the
344 -- entities containing their defining identifiers to ensure the correct
345 -- replacement of the object declaration by the object renaming declaration
346 -- to avoid homograph conflicts (since the object declaration's defining
347 -- identifier was already entered in the current scope). The Next_Entity
348 -- links of the two entities are also swapped since the entities are part
349 -- of the return scope's entity list and the list structure would otherwise
350 -- be corrupted. The homonym chain is preserved as well.
352 procedure Rewrite_Function_Call_For_C
(N
: Node_Id
);
353 -- When generating C code, replace a call to a function that returns an
354 -- array into the generated procedure with an additional out parameter.
356 procedure Set_Enclosing_Sec_Stack_Return
(N
: Node_Id
);
357 -- N is a return statement for a function that returns its result on the
358 -- secondary stack. This sets the Sec_Stack_Needed_For_Return flag on the
359 -- function and all blocks and loops that the return statement is jumping
360 -- out of. This ensures that the secondary stack is not released; otherwise
361 -- the function result would be reclaimed before returning to the caller.
363 procedure Warn_BIP
(Func_Call
: Node_Id
);
364 -- Give a warning on a build-in-place function call if the -gnatd_B switch
367 ----------------------------------------------
368 -- Add_Access_Actual_To_Build_In_Place_Call --
369 ----------------------------------------------
371 procedure Add_Access_Actual_To_Build_In_Place_Call
372 (Function_Call
: Node_Id
;
373 Function_Id
: Entity_Id
;
374 Return_Object
: Node_Id
;
375 Is_Access
: Boolean := False)
377 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
378 Obj_Address
: Node_Id
;
379 Obj_Acc_Formal
: Entity_Id
;
382 -- Locate the implicit access parameter in the called function
384 Obj_Acc_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Object_Access
);
386 -- If no return object is provided, then pass null
388 if No
(Return_Object
) then
389 Obj_Address
:= Make_Null
(Loc
);
390 Set_Parent
(Obj_Address
, Function_Call
);
392 -- If Return_Object is already an expression of an access type, then use
393 -- it directly, since it must be an access value denoting the return
394 -- object, and couldn't possibly be the return object itself.
397 Obj_Address
:= Return_Object
;
398 Set_Parent
(Obj_Address
, Function_Call
);
400 -- Apply Unrestricted_Access to caller's return object
404 Make_Attribute_Reference
(Loc
,
405 Prefix
=> Return_Object
,
406 Attribute_Name
=> Name_Unrestricted_Access
);
408 Set_Parent
(Return_Object
, Obj_Address
);
409 Set_Parent
(Obj_Address
, Function_Call
);
412 Analyze_And_Resolve
(Obj_Address
, Etype
(Obj_Acc_Formal
));
414 -- Build the parameter association for the new actual and add it to the
415 -- end of the function's actuals.
417 Add_Extra_Actual_To_Call
(Function_Call
, Obj_Acc_Formal
, Obj_Address
);
418 end Add_Access_Actual_To_Build_In_Place_Call
;
420 ------------------------------------------------------
421 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
422 ------------------------------------------------------
424 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
425 (Function_Call
: Node_Id
;
426 Function_Id
: Entity_Id
;
427 Alloc_Form
: BIP_Allocation_Form
:= Unspecified
;
428 Alloc_Form_Exp
: Node_Id
:= Empty
;
429 Pool_Actual
: Node_Id
:= Make_Null
(No_Location
))
431 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
433 Alloc_Form_Actual
: Node_Id
;
434 Alloc_Form_Formal
: Node_Id
;
435 Pool_Formal
: Node_Id
;
438 -- Nothing to do when the size of the object is known, and the caller is
439 -- in charge of allocating it, and the callee doesn't unconditionally
440 -- require an allocation form (such as due to having a tagged result).
442 if not Needs_BIP_Alloc_Form
(Function_Id
) then
446 -- Locate the implicit allocation form parameter in the called function.
447 -- Maybe it would be better for each implicit formal of a build-in-place
448 -- function to have a flag or a Uint attribute to identify it. ???
450 Alloc_Form_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Alloc_Form
);
452 if Present
(Alloc_Form_Exp
) then
453 pragma Assert
(Alloc_Form
= Unspecified
);
455 Alloc_Form_Actual
:= Alloc_Form_Exp
;
458 pragma Assert
(Alloc_Form
/= Unspecified
);
461 Make_Integer_Literal
(Loc
,
462 Intval
=> UI_From_Int
(BIP_Allocation_Form
'Pos (Alloc_Form
)));
465 Analyze_And_Resolve
(Alloc_Form_Actual
, Etype
(Alloc_Form_Formal
));
467 -- Build the parameter association for the new actual and add it to the
468 -- end of the function's actuals.
470 Add_Extra_Actual_To_Call
471 (Function_Call
, Alloc_Form_Formal
, Alloc_Form_Actual
);
473 -- Pass the Storage_Pool parameter. This parameter is omitted on ZFP as
474 -- those targets do not support pools.
476 if RTE_Available
(RE_Root_Storage_Pool_Ptr
) then
477 Pool_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Storage_Pool
);
478 Analyze_And_Resolve
(Pool_Actual
, Etype
(Pool_Formal
));
479 Add_Extra_Actual_To_Call
480 (Function_Call
, Pool_Formal
, Pool_Actual
);
482 end Add_Unconstrained_Actuals_To_Build_In_Place_Call
;
484 -----------------------------------------------------------
485 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
486 -----------------------------------------------------------
488 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
489 (Func_Call
: Node_Id
;
491 Ptr_Typ
: Entity_Id
:= Empty
;
492 Master_Exp
: Node_Id
:= Empty
)
495 if not Needs_BIP_Finalization_Master
(Func_Id
) then
500 Formal
: constant Entity_Id
:=
501 Build_In_Place_Formal
(Func_Id
, BIP_Finalization_Master
);
502 Loc
: constant Source_Ptr
:= Sloc
(Func_Call
);
505 Desig_Typ
: Entity_Id
;
508 pragma Assert
(Present
(Formal
));
510 -- If there is a finalization master actual, such as the implicit
511 -- finalization master of an enclosing build-in-place function,
512 -- then this must be added as an extra actual of the call.
514 if Present
(Master_Exp
) then
515 Actual
:= Master_Exp
;
517 -- Case where the context does not require an actual master
519 elsif No
(Ptr_Typ
) then
520 Actual
:= Make_Null
(Loc
);
523 Desig_Typ
:= Directly_Designated_Type
(Ptr_Typ
);
525 -- Check for a library-level access type whose designated type has
526 -- suppressed finalization or the access type is subject to pragma
527 -- No_Heap_Finalization. Such an access type lacks a master. Pass
528 -- a null actual to callee in order to signal a missing master.
530 if Is_Library_Level_Entity
(Ptr_Typ
)
531 and then (Finalize_Storage_Only
(Desig_Typ
)
532 or else No_Heap_Finalization
(Ptr_Typ
))
534 Actual
:= Make_Null
(Loc
);
536 -- Types in need of finalization actions
538 elsif Needs_Finalization
(Desig_Typ
) then
540 -- The general mechanism of creating finalization masters for
541 -- anonymous access types is disabled by default, otherwise
542 -- finalization masters will pop all over the place. Such types
543 -- use context-specific masters.
545 if Ekind
(Ptr_Typ
) = E_Anonymous_Access_Type
546 and then No
(Finalization_Master
(Ptr_Typ
))
548 Build_Anonymous_Master
(Ptr_Typ
);
551 -- Access-to-controlled types should always have a master
553 pragma Assert
(Present
(Finalization_Master
(Ptr_Typ
)));
556 Make_Attribute_Reference
(Loc
,
558 New_Occurrence_Of
(Finalization_Master
(Ptr_Typ
), Loc
),
559 Attribute_Name
=> Name_Unrestricted_Access
);
564 Actual
:= Make_Null
(Loc
);
568 Analyze_And_Resolve
(Actual
, Etype
(Formal
));
570 -- Build the parameter association for the new actual and add it to
571 -- the end of the function's actuals.
573 Add_Extra_Actual_To_Call
(Func_Call
, Formal
, Actual
);
575 end Add_Finalization_Master_Actual_To_Build_In_Place_Call
;
577 ------------------------------
578 -- Add_Extra_Actual_To_Call --
579 ------------------------------
581 procedure Add_Extra_Actual_To_Call
582 (Subprogram_Call
: Node_Id
;
583 Extra_Formal
: Entity_Id
;
584 Extra_Actual
: Node_Id
)
586 Loc
: constant Source_Ptr
:= Sloc
(Subprogram_Call
);
587 Param_Assoc
: Node_Id
;
591 Make_Parameter_Association
(Loc
,
592 Selector_Name
=> New_Occurrence_Of
(Extra_Formal
, Loc
),
593 Explicit_Actual_Parameter
=> Extra_Actual
);
595 Set_Parent
(Param_Assoc
, Subprogram_Call
);
596 Set_Parent
(Extra_Actual
, Param_Assoc
);
598 if Present
(Parameter_Associations
(Subprogram_Call
)) then
599 if Nkind
(Last
(Parameter_Associations
(Subprogram_Call
))) =
600 N_Parameter_Association
603 -- Find last named actual, and append
608 L
:= First_Actual
(Subprogram_Call
);
609 while Present
(L
) loop
610 if No
(Next_Actual
(L
)) then
611 Set_Next_Named_Actual
(Parent
(L
), Extra_Actual
);
619 Set_First_Named_Actual
(Subprogram_Call
, Extra_Actual
);
622 Append
(Param_Assoc
, To
=> Parameter_Associations
(Subprogram_Call
));
625 Set_Parameter_Associations
(Subprogram_Call
, New_List
(Param_Assoc
));
626 Set_First_Named_Actual
(Subprogram_Call
, Extra_Actual
);
628 end Add_Extra_Actual_To_Call
;
630 ---------------------------------------------
631 -- Add_Task_Actuals_To_Build_In_Place_Call --
632 ---------------------------------------------
634 procedure Add_Task_Actuals_To_Build_In_Place_Call
635 (Function_Call
: Node_Id
;
636 Function_Id
: Entity_Id
;
637 Master_Actual
: Node_Id
;
638 Chain
: Node_Id
:= Empty
)
640 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
642 Chain_Actual
: Node_Id
;
643 Chain_Formal
: Node_Id
;
644 Master_Formal
: Node_Id
;
647 -- No such extra parameters are needed if there are no tasks
649 if not Needs_BIP_Task_Actuals
(Function_Id
) then
651 -- However we must add dummy extra actuals if the function is
652 -- a dispatching operation that inherited these extra formals.
654 if Is_Dispatching_Operation
(Function_Id
)
655 and then Has_BIP_Extra_Formal
(Function_Id
, BIP_Task_Master
)
658 Build_In_Place_Formal
(Function_Id
, BIP_Task_Master
);
659 Actual
:= Make_Integer_Literal
(Loc
, Uint_0
);
660 Analyze_And_Resolve
(Actual
, Etype
(Master_Formal
));
661 Add_Extra_Actual_To_Call
(Function_Call
, Master_Formal
, Actual
);
664 Build_In_Place_Formal
(Function_Id
, BIP_Activation_Chain
);
665 Chain_Actual
:= Make_Null
(Loc
);
666 Analyze_And_Resolve
(Chain_Actual
, Etype
(Chain_Formal
));
667 Add_Extra_Actual_To_Call
668 (Function_Call
, Chain_Formal
, Chain_Actual
);
674 Actual
:= Master_Actual
;
676 -- Use a dummy _master actual in case of No_Task_Hierarchy
678 if Restriction_Active
(No_Task_Hierarchy
) then
679 Actual
:= Make_Integer_Literal
(Loc
, Library_Task_Level
);
681 -- In the case where we use the master associated with an access type,
682 -- the actual is an entity and requires an explicit reference.
684 elsif Nkind
(Actual
) = N_Defining_Identifier
then
685 Actual
:= New_Occurrence_Of
(Actual
, Loc
);
688 -- Locate the implicit master parameter in the called function
690 Master_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Task_Master
);
691 Analyze_And_Resolve
(Actual
, Etype
(Master_Formal
));
693 -- Build the parameter association for the new actual and add it to the
694 -- end of the function's actuals.
696 Add_Extra_Actual_To_Call
(Function_Call
, Master_Formal
, Actual
);
698 -- Locate the implicit activation chain parameter in the called function
701 Build_In_Place_Formal
(Function_Id
, BIP_Activation_Chain
);
703 -- Create the actual which is a pointer to the current activation chain
705 if Restriction_Active
(No_Task_Hierarchy
) then
706 Chain_Actual
:= Make_Null
(Loc
);
708 elsif No
(Chain
) then
710 Make_Attribute_Reference
(Loc
,
711 Prefix
=> Make_Identifier
(Loc
, Name_uChain
),
712 Attribute_Name
=> Name_Unrestricted_Access
);
714 -- Allocator case; make a reference to the Chain passed in by the caller
718 Make_Attribute_Reference
(Loc
,
719 Prefix
=> New_Occurrence_Of
(Chain
, Loc
),
720 Attribute_Name
=> Name_Unrestricted_Access
);
723 Analyze_And_Resolve
(Chain_Actual
, Etype
(Chain_Formal
));
725 -- Build the parameter association for the new actual and add it to the
726 -- end of the function's actuals.
728 Add_Extra_Actual_To_Call
(Function_Call
, Chain_Formal
, Chain_Actual
);
729 end Add_Task_Actuals_To_Build_In_Place_Call
;
731 ----------------------------------
732 -- Apply_CW_Accessibility_Check --
733 ----------------------------------
735 procedure Apply_CW_Accessibility_Check
(Exp
: Node_Id
; Func
: Entity_Id
) is
736 Loc
: constant Source_Ptr
:= Sloc
(Exp
);
739 -- CodePeer does not do anything useful on Ada.Tags.Type_Specific_Data
742 if Ada_Version
>= Ada_2005
743 and then not CodePeer_Mode
744 and then Tagged_Type_Expansion
745 and then not Scope_Suppress
.Suppress
(Accessibility_Check
)
747 (Is_Class_Wide_Type
(Etype
(Exp
))
748 or else Nkind
(Exp
) in
749 N_Type_Conversion | N_Unchecked_Type_Conversion
750 or else (Is_Entity_Name
(Exp
)
751 and then Is_Formal
(Entity
(Exp
)))
752 or else Scope_Depth
(Enclosing_Dynamic_Scope
(Etype
(Exp
))) >
753 Scope_Depth
(Enclosing_Dynamic_Scope
(Func
)))
759 -- Ada 2005 (AI-251): In class-wide interface objects we displace
760 -- "this" to reference the base of the object. This is required to
761 -- get access to the TSD of the object.
763 if Is_Class_Wide_Type
(Etype
(Exp
))
764 and then Is_Interface
(Etype
(Exp
))
766 -- If the expression is an explicit dereference then we can
767 -- directly displace the pointer to reference the base of
770 if Nkind
(Exp
) = N_Explicit_Dereference
then
772 Make_Explicit_Dereference
(Loc
,
774 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
775 Make_Function_Call
(Loc
,
777 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
778 Parameter_Associations
=> New_List
(
779 Unchecked_Convert_To
(RTE
(RE_Address
),
780 Duplicate_Subexpr
(Prefix
(Exp
)))))));
782 -- Similar case to the previous one but the expression is a
783 -- renaming of an explicit dereference.
785 elsif Nkind
(Exp
) = N_Identifier
786 and then Present
(Renamed_Object
(Entity
(Exp
)))
787 and then Nkind
(Renamed_Object
(Entity
(Exp
)))
788 = N_Explicit_Dereference
791 Make_Explicit_Dereference
(Loc
,
793 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
794 Make_Function_Call
(Loc
,
796 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
797 Parameter_Associations
=> New_List
(
798 Unchecked_Convert_To
(RTE
(RE_Address
),
801 (Renamed_Object
(Entity
(Exp
)))))))));
803 -- Common case: obtain the address of the actual object and
804 -- displace the pointer to reference the base of the object.
808 Make_Explicit_Dereference
(Loc
,
810 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
811 Make_Function_Call
(Loc
,
813 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
814 Parameter_Associations
=> New_List
(
815 Make_Attribute_Reference
(Loc
,
816 Prefix
=> Duplicate_Subexpr
(Exp
),
817 Attribute_Name
=> Name_Address
)))));
821 Make_Attribute_Reference
(Loc
,
822 Prefix
=> Duplicate_Subexpr
(Exp
),
823 Attribute_Name
=> Name_Tag
);
826 -- Suppress junk access chacks on RE_Tag_Ptr
829 Make_Raise_Program_Error
(Loc
,
832 Left_Opnd
=> Build_Get_Access_Level
(Loc
, Tag_Node
),
834 Make_Integer_Literal
(Loc
,
835 Scope_Depth
(Enclosing_Dynamic_Scope
(Func
)))),
836 Reason
=> PE_Accessibility_Check_Failed
),
837 Suppress
=> Access_Check
);
840 end Apply_CW_Accessibility_Check
;
842 -----------------------
843 -- BIP_Formal_Suffix --
844 -----------------------
846 function BIP_Formal_Suffix
(Kind
: BIP_Formal_Kind
) return String is
849 when BIP_Alloc_Form
=>
850 return BIP_Alloc_Suffix
;
852 when BIP_Storage_Pool
=>
853 return BIP_Storage_Pool_Suffix
;
855 when BIP_Finalization_Master
=>
856 return BIP_Finalization_Master_Suffix
;
858 when BIP_Task_Master
=>
859 return BIP_Task_Master_Suffix
;
861 when BIP_Activation_Chain
=>
862 return BIP_Activation_Chain_Suffix
;
864 when BIP_Object_Access
=>
865 return BIP_Object_Access_Suffix
;
867 end BIP_Formal_Suffix
;
869 ---------------------
870 -- BIP_Suffix_Kind --
871 ---------------------
873 function BIP_Suffix_Kind
(E
: Entity_Id
) return BIP_Formal_Kind
is
874 Nam
: constant String := Get_Name_String
(Chars
(E
));
876 function Has_Suffix
(Suffix
: String) return Boolean;
877 -- Return True if Nam has suffix Suffix
879 function Has_Suffix
(Suffix
: String) return Boolean is
880 Len
: constant Natural := Suffix
'Length;
882 return Nam
'Length > Len
883 and then Nam
(Nam
'Last - Len
+ 1 .. Nam
'Last) = Suffix
;
886 -- Start of processing for BIP_Suffix_Kind
889 if Has_Suffix
(BIP_Alloc_Suffix
) then
890 return BIP_Alloc_Form
;
892 elsif Has_Suffix
(BIP_Storage_Pool_Suffix
) then
893 return BIP_Storage_Pool
;
895 elsif Has_Suffix
(BIP_Finalization_Master_Suffix
) then
896 return BIP_Finalization_Master
;
898 elsif Has_Suffix
(BIP_Task_Master_Suffix
) then
899 return BIP_Task_Master
;
901 elsif Has_Suffix
(BIP_Activation_Chain_Suffix
) then
902 return BIP_Activation_Chain
;
904 elsif Has_Suffix
(BIP_Object_Access_Suffix
) then
905 return BIP_Object_Access
;
912 ---------------------------
913 -- Build_In_Place_Formal --
914 ---------------------------
916 function Build_In_Place_Formal
918 Kind
: BIP_Formal_Kind
) return Entity_Id
920 Extra_Formal
: Entity_Id
:= Extra_Formals
(Func
);
921 Formal_Suffix
: constant String := BIP_Formal_Suffix
(Kind
);
924 -- Maybe it would be better for each implicit formal of a build-in-place
925 -- function to have a flag or a Uint attribute to identify it. ???
927 -- The return type in the function declaration may have been a limited
928 -- view, and the extra formals for the function were not generated at
929 -- that point. At the point of call the full view must be available and
930 -- the extra formals can be created and Returns_By_Ref computed.
932 if No
(Extra_Formal
) then
933 Create_Extra_Formals
(Func
);
934 Extra_Formal
:= Extra_Formals
(Func
);
935 Compute_Returns_By_Ref
(Func
);
938 -- We search for a formal with a matching suffix. We can't search
939 -- for the full name, because of the code at the end of Sem_Ch6.-
940 -- Create_Extra_Formals, which copies the Extra_Formals over to
941 -- the Alias of an instance, which will cause the formals to have
942 -- "incorrect" names.
944 while Present
(Extra_Formal
) loop
946 Name
: constant String := Get_Name_String
(Chars
(Extra_Formal
));
948 exit when Name
'Length >= Formal_Suffix
'Length
949 and then Formal_Suffix
=
950 Name
(Name
'Last - Formal_Suffix
'Length + 1 .. Name
'Last);
953 Next_Formal_With_Extras
(Extra_Formal
);
956 if No
(Extra_Formal
) then
961 end Build_In_Place_Formal
;
963 -------------------------------
964 -- Build_Procedure_Body_Form --
965 -------------------------------
967 function Build_Procedure_Body_Form
968 (Func_Id
: Entity_Id
;
969 Func_Body
: Node_Id
) return Node_Id
971 Loc
: constant Source_Ptr
:= Sloc
(Func_Body
);
973 Proc_Decl
: constant Node_Id
:= Prev
(Unit_Declaration_Node
(Func_Id
));
974 -- It is assumed that the node before the declaration of the
975 -- corresponding subprogram spec is the declaration of the procedure
978 Proc_Id
: constant Entity_Id
:= Defining_Entity
(Proc_Decl
);
980 procedure Replace_Returns
(Param_Id
: Entity_Id
; Stmts
: List_Id
);
981 -- Replace each return statement found in the list Stmts with an
982 -- assignment of the return expression to parameter Param_Id.
984 ---------------------
985 -- Replace_Returns --
986 ---------------------
988 procedure Replace_Returns
(Param_Id
: Entity_Id
; Stmts
: List_Id
) is
992 Stmt
:= First
(Stmts
);
993 while Present
(Stmt
) loop
994 if Nkind
(Stmt
) = N_Block_Statement
then
995 Replace_Returns
(Param_Id
,
996 Statements
(Handled_Statement_Sequence
(Stmt
)));
998 elsif Nkind
(Stmt
) = N_Case_Statement
then
1002 Alt
:= First
(Alternatives
(Stmt
));
1003 while Present
(Alt
) loop
1004 Replace_Returns
(Param_Id
, Statements
(Alt
));
1009 elsif Nkind
(Stmt
) = N_Extended_Return_Statement
then
1011 Ret_Obj
: constant Entity_Id
:=
1013 (First
(Return_Object_Declarations
(Stmt
)));
1014 Assign
: constant Node_Id
:=
1015 Make_Assignment_Statement
(Sloc
(Stmt
),
1017 New_Occurrence_Of
(Param_Id
, Loc
),
1019 New_Occurrence_Of
(Ret_Obj
, Sloc
(Stmt
)));
1023 -- The extended return may just contain the declaration
1025 if Present
(Handled_Statement_Sequence
(Stmt
)) then
1026 Stmts
:= Statements
(Handled_Statement_Sequence
(Stmt
));
1031 Set_Assignment_OK
(Name
(Assign
));
1034 Make_Block_Statement
(Sloc
(Stmt
),
1036 Return_Object_Declarations
(Stmt
),
1037 Handled_Statement_Sequence
=>
1038 Make_Handled_Sequence_Of_Statements
(Loc
,
1039 Statements
=> Stmts
)));
1041 Replace_Returns
(Param_Id
, Stmts
);
1043 Append_To
(Stmts
, Assign
);
1044 Append_To
(Stmts
, Make_Simple_Return_Statement
(Loc
));
1047 elsif Nkind
(Stmt
) = N_If_Statement
then
1048 Replace_Returns
(Param_Id
, Then_Statements
(Stmt
));
1049 Replace_Returns
(Param_Id
, Else_Statements
(Stmt
));
1054 Part
:= First
(Elsif_Parts
(Stmt
));
1055 while Present
(Part
) loop
1056 Replace_Returns
(Param_Id
, Then_Statements
(Part
));
1061 elsif Nkind
(Stmt
) = N_Loop_Statement
then
1062 Replace_Returns
(Param_Id
, Statements
(Stmt
));
1064 elsif Nkind
(Stmt
) = N_Simple_Return_Statement
then
1071 Make_Assignment_Statement
(Sloc
(Stmt
),
1072 Name
=> New_Occurrence_Of
(Param_Id
, Loc
),
1073 Expression
=> Relocate_Node
(Expression
(Stmt
))));
1075 Insert_After
(Stmt
, Make_Simple_Return_Statement
(Loc
));
1077 -- Skip the added return
1084 end Replace_Returns
;
1091 -- Start of processing for Build_Procedure_Body_Form
1094 -- This routine replaces the original function body:
1096 -- function F (...) return Array_Typ is
1099 -- return Something;
1102 -- with the following:
1104 -- procedure P (..., Result : out Array_Typ) is
1107 -- Result := Something;
1111 Statements
(Handled_Statement_Sequence
(Func_Body
));
1112 Replace_Returns
(Last_Entity
(Proc_Id
), Stmts
);
1115 Make_Subprogram_Body
(Loc
,
1117 Copy_Subprogram_Spec
(Specification
(Proc_Decl
)),
1118 Declarations
=> Declarations
(Func_Body
),
1119 Handled_Statement_Sequence
=>
1120 Make_Handled_Sequence_Of_Statements
(Loc
,
1121 Statements
=> Stmts
));
1123 -- If the function is a generic instance, so is the new procedure.
1124 -- Set flag accordingly so that the proper renaming declarations are
1127 Set_Is_Generic_Instance
(Proc_Id
, Is_Generic_Instance
(Func_Id
));
1129 end Build_Procedure_Body_Form
;
1131 -----------------------
1132 -- Caller_Known_Size --
1133 -----------------------
1135 function Caller_Known_Size
1136 (Func_Call
: Node_Id
;
1137 Result_Subt
: Entity_Id
) return Boolean
1139 Utyp
: constant Entity_Id
:= Underlying_Type
(Result_Subt
);
1142 return not Needs_Secondary_Stack
(Utyp
)
1143 and then not (Is_Tagged_Type
(Utyp
)
1144 and then Present
(Controlling_Argument
(Func_Call
)));
1145 end Caller_Known_Size
;
1147 -----------------------
1148 -- Check_BIP_Actuals --
1149 -----------------------
1151 function Check_BIP_Actuals
1152 (Subp_Call
: Node_Id
;
1153 Subp_Id
: Entity_Id
) return Boolean
1159 pragma Assert
(Nkind
(Subp_Call
) in N_Entry_Call_Statement
1161 | N_Procedure_Call_Statement
);
1163 -- In CodePeer_Mode, the tree for `'Elab_Spec` procedures will be
1164 -- malformed because GNAT does not perform the usual expansion that
1165 -- results in the importation of external elaboration procedure symbols.
1166 -- This is expected: the CodePeer backend has special handling for this
1168 -- Thus, we do not need to check the tree (and in fact can't, because
1172 and then Nkind
(Name
(Subp_Call
)) = N_Attribute_Reference
1173 and then Attribute_Name
(Name
(Subp_Call
)) in Name_Elab_Spec
1175 | Name_Elab_Subp_Body
1180 Formal
:= First_Formal_With_Extras
(Subp_Id
);
1181 Actual
:= First_Actual
(Subp_Call
);
1183 while Present
(Formal
) and then Present
(Actual
) loop
1184 if Is_Build_In_Place_Entity
(Formal
)
1185 and then Nkind
(Actual
) = N_Identifier
1186 and then Is_Build_In_Place_Entity
(Entity
(Actual
))
1187 and then BIP_Suffix_Kind
(Formal
)
1188 /= BIP_Suffix_Kind
(Entity
(Actual
))
1193 Next_Formal_With_Extras
(Formal
);
1194 Next_Actual
(Actual
);
1197 return No
(Formal
) and then No
(Actual
);
1198 end Check_BIP_Actuals
;
1200 -----------------------------
1201 -- Check_Number_Of_Actuals --
1202 -----------------------------
1204 function Check_Number_Of_Actuals
1205 (Subp_Call
: Node_Id
;
1206 Subp_Id
: Entity_Id
) return Boolean
1212 pragma Assert
(Nkind
(Subp_Call
) in N_Entry_Call_Statement
1214 | N_Procedure_Call_Statement
);
1216 Formal
:= First_Formal_With_Extras
(Subp_Id
);
1217 Actual
:= First_Actual
(Subp_Call
);
1219 while Present
(Formal
) and then Present
(Actual
) loop
1220 Next_Formal_With_Extras
(Formal
);
1221 Next_Actual
(Actual
);
1224 return No
(Formal
) and then No
(Actual
);
1225 end Check_Number_Of_Actuals
;
1227 --------------------------------
1228 -- Check_Overriding_Operation --
1229 --------------------------------
1231 procedure Check_Overriding_Operation
(Subp
: Entity_Id
) is
1232 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
1233 Op_List
: constant Elist_Id
:= Primitive_Operations
(Typ
);
1235 Prim_Op
: Entity_Id
;
1239 if Is_Derived_Type
(Typ
)
1240 and then not Is_Private_Type
(Typ
)
1241 and then In_Open_Scopes
(Scope
(Etype
(Typ
)))
1242 and then Is_Base_Type
(Typ
)
1244 -- Subp overrides an inherited private operation if there is an
1245 -- inherited operation with a different name than Subp (see
1246 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
1247 -- same name as Subp.
1249 Op_Elmt
:= First_Elmt
(Op_List
);
1250 while Present
(Op_Elmt
) loop
1251 Prim_Op
:= Node
(Op_Elmt
);
1252 Par_Op
:= Alias
(Prim_Op
);
1255 and then not Comes_From_Source
(Prim_Op
)
1256 and then Chars
(Prim_Op
) /= Chars
(Par_Op
)
1257 and then Chars
(Par_Op
) = Chars
(Subp
)
1258 and then Is_Hidden
(Par_Op
)
1259 and then Type_Conformant
(Prim_Op
, Subp
)
1261 Set_DT_Position_Value
(Subp
, DT_Position
(Prim_Op
));
1264 Next_Elmt
(Op_Elmt
);
1267 end Check_Overriding_Operation
;
1269 -------------------------------
1270 -- Detect_Infinite_Recursion --
1271 -------------------------------
1273 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
) is
1274 Loc
: constant Source_Ptr
:= Sloc
(N
);
1276 Var_List
: constant Elist_Id
:= New_Elmt_List
;
1277 -- List of globals referenced by body of procedure
1279 Call_List
: constant Elist_Id
:= New_Elmt_List
;
1280 -- List of recursive calls in body of procedure
1282 Shad_List
: constant Elist_Id
:= New_Elmt_List
;
1283 -- List of entity id's for entities created to capture the value of
1284 -- referenced globals on entry to the procedure.
1286 Scop
: constant Uint
:= Scope_Depth
(Spec
);
1287 -- This is used to record the scope depth of the current procedure, so
1288 -- that we can identify global references.
1290 Max_Vars
: constant := 4;
1291 -- Do not test more than four global variables
1293 Count_Vars
: Natural := 0;
1294 -- Count variables found so far
1306 function Process
(Nod
: Node_Id
) return Traverse_Result
;
1307 -- Function to traverse the subprogram body (using Traverse_Func)
1313 function Process
(Nod
: Node_Id
) return Traverse_Result
is
1317 if Nkind
(Nod
) = N_Procedure_Call_Statement
then
1319 -- Case of one of the detected recursive calls
1321 if Is_Entity_Name
(Name
(Nod
))
1322 and then Has_Recursive_Call
(Entity
(Name
(Nod
)))
1323 and then Entity
(Name
(Nod
)) = Spec
1325 Append_Elmt
(Nod
, Call_List
);
1328 -- Any other procedure call may have side effects
1334 -- A call to a pure function can always be ignored
1336 elsif Nkind
(Nod
) = N_Function_Call
1337 and then Is_Entity_Name
(Name
(Nod
))
1338 and then Is_Pure
(Entity
(Name
(Nod
)))
1342 -- Case of an identifier reference
1344 elsif Nkind
(Nod
) = N_Identifier
then
1345 Ent
:= Entity
(Nod
);
1347 -- If no entity, then ignore the reference
1349 -- Not clear why this can happen. To investigate, remove this
1350 -- test and look at the crash that occurs here in 3401-004 ???
1355 -- Ignore entities with no Scope, again not clear how this
1356 -- can happen, to investigate, look at 4108-008 ???
1358 elsif No
(Scope
(Ent
)) then
1361 -- Ignore the reference if not to a more global object
1363 elsif Scope_Depth
(Scope
(Ent
)) >= Scop
then
1366 -- References to types, exceptions and constants are always OK
1369 or else Ekind
(Ent
) = E_Exception
1370 or else Ekind
(Ent
) = E_Constant
1374 -- If other than a non-volatile scalar variable, we have some
1375 -- kind of global reference (e.g. to a function) that we cannot
1376 -- deal with so we forget the attempt.
1378 elsif Ekind
(Ent
) /= E_Variable
1379 or else not Is_Scalar_Type
(Etype
(Ent
))
1380 or else Treat_As_Volatile
(Ent
)
1384 -- Otherwise we have a reference to a global scalar
1387 -- Loop through global entities already detected
1389 Elm
:= First_Elmt
(Var_List
);
1391 -- If not detected before, record this new global reference
1394 Count_Vars
:= Count_Vars
+ 1;
1396 if Count_Vars
<= Max_Vars
then
1397 Append_Elmt
(Entity
(Nod
), Var_List
);
1404 -- If recorded before, ignore
1406 elsif Node
(Elm
) = Entity
(Nod
) then
1409 -- Otherwise keep looking
1419 -- For all other node kinds, recursively visit syntactic children
1426 function Traverse_Body
is new Traverse_Func
(Process
);
1428 -- Start of processing for Detect_Infinite_Recursion
1431 -- Do not attempt detection in No_Implicit_Conditional mode, since we
1432 -- won't be able to generate the code to handle the recursion in any
1435 if Restriction_Active
(No_Implicit_Conditionals
) then
1439 -- Otherwise do traversal and quit if we get abandon signal
1441 if Traverse_Body
(N
) = Abandon
then
1444 -- We must have a call, since Has_Recursive_Call was set. If not just
1445 -- ignore (this is only an error check, so if we have a funny situation,
1446 -- due to bugs or errors, we do not want to bomb).
1448 elsif Is_Empty_Elmt_List
(Call_List
) then
1452 -- Here is the case where we detect recursion at compile time
1454 -- Push our current scope for analyzing the declarations and code that
1455 -- we will insert for the checking.
1459 -- This loop builds temporary variables for each of the referenced
1460 -- globals, so that at the end of the loop the list Shad_List contains
1461 -- these temporaries in one-to-one correspondence with the elements in
1465 Elm
:= First_Elmt
(Var_List
);
1466 while Present
(Elm
) loop
1468 Ent
:= Make_Temporary
(Loc
, 'S');
1469 Append_Elmt
(Ent
, Shad_List
);
1471 -- Insert a declaration for this temporary at the start of the
1472 -- declarations for the procedure. The temporaries are declared as
1473 -- constant objects initialized to the current values of the
1474 -- corresponding temporaries.
1477 Make_Object_Declaration
(Loc
,
1478 Defining_Identifier
=> Ent
,
1479 Object_Definition
=> New_Occurrence_Of
(Etype
(Var
), Loc
),
1480 Constant_Present
=> True,
1481 Expression
=> New_Occurrence_Of
(Var
, Loc
));
1484 Prepend
(Decl
, Declarations
(N
));
1486 Insert_After
(Last
, Decl
);
1494 -- Loop through calls
1496 Call
:= First_Elmt
(Call_List
);
1497 while Present
(Call
) loop
1499 -- Build a predicate expression of the form
1502 -- and then global1 = temp1
1503 -- and then global2 = temp2
1506 -- This predicate determines if any of the global values
1507 -- referenced by the procedure have changed since the
1508 -- current call, if not an infinite recursion is assured.
1510 Test
:= New_Occurrence_Of
(Standard_True
, Loc
);
1512 Elm1
:= First_Elmt
(Var_List
);
1513 Elm2
:= First_Elmt
(Shad_List
);
1514 while Present
(Elm1
) loop
1520 Left_Opnd
=> New_Occurrence_Of
(Node
(Elm1
), Loc
),
1521 Right_Opnd
=> New_Occurrence_Of
(Node
(Elm2
), Loc
)));
1527 -- Now we replace the call with the sequence
1529 -- if no-changes (see above) then
1530 -- raise Storage_Error;
1535 Rewrite
(Node
(Call
),
1536 Make_If_Statement
(Loc
,
1538 Then_Statements
=> New_List
(
1539 Make_Raise_Storage_Error
(Loc
,
1540 Reason
=> SE_Infinite_Recursion
)),
1542 Else_Statements
=> New_List
(
1543 Relocate_Node
(Node
(Call
)))));
1545 Analyze
(Node
(Call
));
1550 -- Remove temporary scope stack entry used for analysis
1553 end Detect_Infinite_Recursion
;
1555 --------------------
1556 -- Expand_Actuals --
1557 --------------------
1559 procedure Expand_Actuals
1562 Post_Call
: out List_Id
)
1564 Loc
: constant Source_Ptr
:= Sloc
(N
);
1568 E_Actual
: Entity_Id
;
1569 E_Formal
: Entity_Id
;
1571 procedure Add_Call_By_Copy_Code
;
1572 -- For cases where the parameter must be passed by copy, this routine
1573 -- generates a temporary variable into which the actual is copied and
1574 -- then passes this as the parameter. For an OUT or IN OUT parameter,
1575 -- an assignment is also generated to copy the result back. The call
1576 -- also takes care of any constraint checks required for the type
1577 -- conversion case (on both the way in and the way out).
1579 procedure Add_Simple_Call_By_Copy_Code
(Force
: Boolean);
1580 -- This is similar to the above, but is used in cases where we know
1581 -- that all that is needed is to simply create a temporary and copy
1582 -- the value in and out of the temporary. If Force is True, then the
1583 -- procedure may disregard legality considerations.
1585 -- ??? We need to do the copy for a bit-packed array because this is
1586 -- where the rewriting into a mask-and-shift sequence is done. But of
1587 -- course this may break the program if it expects bits to be really
1588 -- passed by reference. That's what we have done historically though.
1590 procedure Add_Validation_Call_By_Copy_Code
(Act
: Node_Id
);
1591 -- Perform copy-back for actual parameter Act which denotes a validation
1594 procedure Check_Fortran_Logical
;
1595 -- A value of type Logical that is passed through a formal parameter
1596 -- must be normalized because .TRUE. usually does not have the same
1597 -- representation as True. We assume that .FALSE. = False = 0.
1598 -- What about functions that return a logical type ???
1600 function Is_Legal_Copy
return Boolean;
1601 -- Check that an actual can be copied before generating the temporary
1602 -- to be used in the call. If the formal is of a by_reference type or
1603 -- is aliased, then the program is illegal (this can only happen in
1604 -- the presence of representation clauses that force a misalignment)
1605 -- If the formal is a by_reference parameter imposed by a DEC pragma,
1606 -- emit a warning that this might lead to unaligned arguments.
1608 function Make_Var
(Actual
: Node_Id
) return Entity_Id
;
1609 -- Returns an entity that refers to the given actual parameter, Actual
1610 -- (not including any type conversion). If Actual is an entity name,
1611 -- then this entity is returned unchanged, otherwise a renaming is
1612 -- created to provide an entity for the actual.
1614 procedure Reset_Packed_Prefix
;
1615 -- The expansion of a packed array component reference is delayed in
1616 -- the context of a call. Now we need to complete the expansion, so we
1617 -- unmark the analyzed bits in all prefixes.
1619 function Requires_Atomic_Or_Volatile_Copy
return Boolean;
1620 -- Returns whether a copy is required as per RM C.6(19) and gives a
1621 -- warning in this case.
1623 ---------------------------
1624 -- Add_Call_By_Copy_Code --
1625 ---------------------------
1627 procedure Add_Call_By_Copy_Code
is
1630 F_Typ
: Entity_Id
:= Etype
(Formal
);
1638 if not Is_Legal_Copy
then
1642 Temp
:= Make_Temporary
(Loc
, 'T', Actual
);
1644 -- Handle formals whose type comes from the limited view
1646 if From_Limited_With
(F_Typ
)
1647 and then Has_Non_Limited_View
(F_Typ
)
1649 F_Typ
:= Non_Limited_View
(F_Typ
);
1652 -- Use formal type for temp, unless formal type is an unconstrained
1653 -- array, in which case we don't have to worry about bounds checks,
1654 -- and we use the actual type, since that has appropriate bounds.
1656 if Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
1657 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1659 Indic
:= New_Occurrence_Of
(F_Typ
, Loc
);
1662 -- The new code will be properly analyzed below and the setting of
1663 -- the Do_Range_Check flag recomputed so remove the obsolete one.
1665 Set_Do_Range_Check
(Actual
, False);
1667 if Nkind
(Actual
) = N_Type_Conversion
then
1668 Set_Do_Range_Check
(Expression
(Actual
), False);
1670 V_Typ
:= Etype
(Expression
(Actual
));
1672 -- If the formal is an (in-)out parameter, capture the name
1673 -- of the variable in order to build the post-call assignment.
1675 Var
:= Make_Var
(Expression
(Actual
));
1677 Crep
:= not Has_Compatible_Representation
1678 (Target_Typ
=> F_Typ
,
1679 Operand_Typ
=> Etype
(Expression
(Actual
)));
1682 V_Typ
:= Etype
(Actual
);
1683 Var
:= Make_Var
(Actual
);
1687 -- If the actual denotes a variable which captures the value of an
1688 -- object for validation purposes, we propagate the link with this
1689 -- object to the new variable made from the actual just above.
1691 if Ekind
(Formal
) /= E_In_Parameter
1692 and then Is_Validation_Variable_Reference
(Actual
)
1695 Ref
: constant Node_Id
:= Unqual_Conv
(Actual
);
1698 if Is_Entity_Name
(Ref
) then
1699 Set_Validated_Object
(Var
, Validated_Object
(Entity
(Ref
)));
1702 pragma Assert
(False);
1708 -- Setup initialization for case of in out parameter, or an out
1709 -- parameter where the formal is an unconstrained array (in the
1710 -- latter case, we have to pass in an object with bounds).
1712 -- If this is an out parameter, the initial copy is wasteful, so as
1713 -- an optimization for the one-dimensional case we extract the
1714 -- bounds of the actual and build an uninitialized temporary of the
1717 -- If the formal is an out parameter with discriminants, the
1718 -- discriminants must be captured even if the rest of the object
1719 -- is in principle uninitialized, because the discriminants may
1720 -- be read by the called subprogram.
1722 if Ekind
(Formal
) = E_In_Out_Parameter
1723 or else (Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
))
1724 or else Has_Discriminants
(F_Typ
)
1726 if Nkind
(Actual
) = N_Type_Conversion
then
1727 if Conversion_OK
(Actual
) then
1728 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1730 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1733 elsif Ekind
(Formal
) = E_Out_Parameter
1734 and then Is_Array_Type
(F_Typ
)
1735 and then Number_Dimensions
(F_Typ
) = 1
1736 and then not Has_Non_Null_Base_Init_Proc
(F_Typ
)
1738 -- Actual is a one-dimensional array or slice, and the type
1739 -- requires no initialization. Create a temporary of the
1740 -- right size, but do not copy actual into it (optimization).
1744 Make_Subtype_Indication
(Loc
,
1745 Subtype_Mark
=> New_Occurrence_Of
(F_Typ
, Loc
),
1747 Make_Index_Or_Discriminant_Constraint
(Loc
,
1748 Constraints
=> New_List
(
1751 Make_Attribute_Reference
(Loc
,
1752 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
1753 Attribute_Name
=> Name_First
),
1755 Make_Attribute_Reference
(Loc
,
1756 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
1757 Attribute_Name
=> Name_Last
)))));
1760 Init
:= New_Occurrence_Of
(Var
, Loc
);
1763 -- An initialization is created for packed conversions as
1764 -- actuals for out parameters to enable Make_Object_Declaration
1765 -- to determine the proper subtype for N_Node. Note that this
1766 -- is wasteful because the extra copying on the call side is
1767 -- not required for such out parameters. ???
1769 elsif Ekind
(Formal
) = E_Out_Parameter
1770 and then Nkind
(Actual
) = N_Type_Conversion
1771 and then (Is_Bit_Packed_Array
(F_Typ
)
1773 Is_Bit_Packed_Array
(Etype
(Expression
(Actual
))))
1775 if Conversion_OK
(Actual
) then
1776 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1778 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1781 elsif Ekind
(Formal
) = E_In_Parameter
then
1783 -- Handle the case in which the actual is a type conversion
1785 if Nkind
(Actual
) = N_Type_Conversion
then
1786 if Conversion_OK
(Actual
) then
1787 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1789 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1792 Init
:= New_Occurrence_Of
(Var
, Loc
);
1795 -- Access types are passed in without checks, but if a copy-back is
1796 -- required for a null-excluding check on an in-out or out parameter,
1797 -- then the initial value is that of the actual.
1799 elsif Is_Access_Type
(E_Formal
)
1800 and then Can_Never_Be_Null
(Etype
(Actual
))
1801 and then not Can_Never_Be_Null
(E_Formal
)
1803 Init
:= New_Occurrence_Of
(Var
, Loc
);
1805 -- View conversions when the formal type has the Default_Value aspect
1806 -- require passing in the value of the conversion's operand. The type
1807 -- of that operand also has Default_Value, as required by AI12-0074
1808 -- (RM 6.4.1(5.3/4)). The subtype denoted by the subtype_indication
1809 -- is changed to the base type of the formal subtype, to ensure that
1810 -- the actual's value can be assigned without a constraint check
1811 -- (note that no check is done on passing to an out parameter). Also
1812 -- note that the two types necessarily share the same ancestor type,
1813 -- as required by 6.4.1(5.2/4), so underlying base types will match.
1815 elsif Ekind
(Formal
) = E_Out_Parameter
1816 and then Is_Scalar_Type
(Etype
(F_Typ
))
1817 and then Nkind
(Actual
) = N_Type_Conversion
1818 and then Present
(Default_Aspect_Value
(Etype
(F_Typ
)))
1820 Indic
:= New_Occurrence_Of
(Base_Type
(F_Typ
), Loc
);
1822 (Base_Type
(F_Typ
), New_Occurrence_Of
(Var
, Loc
));
1829 Make_Object_Declaration
(Loc
,
1830 Defining_Identifier
=> Temp
,
1831 Object_Definition
=> Indic
,
1832 Expression
=> Init
);
1833 Set_Assignment_OK
(N_Node
);
1834 Insert_Action
(N
, N_Node
);
1836 -- Now, normally the deal here is that we use the defining
1837 -- identifier created by that object declaration. There is
1838 -- one exception to this. In the change of representation case
1839 -- the above declaration will end up looking like:
1841 -- temp : type := identifier;
1843 -- And in this case we might as well use the identifier directly
1844 -- and eliminate the temporary. Note that the analysis of the
1845 -- declaration was not a waste of time in that case, since it is
1846 -- what generated the necessary change of representation code. If
1847 -- the change of representation introduced additional code, as in
1848 -- a fixed-integer conversion, the expression is not an identifier
1849 -- and must be kept.
1852 and then Present
(Expression
(N_Node
))
1853 and then Is_Entity_Name
(Expression
(N_Node
))
1855 Temp
:= Entity
(Expression
(N_Node
));
1856 Rewrite
(N_Node
, Make_Null_Statement
(Loc
));
1859 -- For IN parameter, all we do is to replace the actual
1861 if Ekind
(Formal
) = E_In_Parameter
then
1862 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
1865 -- Processing for OUT or IN OUT parameter
1868 -- Kill current value indications for the temporary variable we
1869 -- created, since we just passed it as an OUT parameter.
1871 Kill_Current_Values
(Temp
);
1872 Set_Is_Known_Valid
(Temp
, False);
1873 Set_Is_True_Constant
(Temp
, False);
1875 -- If type conversion, use reverse conversion on exit
1877 if Nkind
(Actual
) = N_Type_Conversion
then
1878 if Conversion_OK
(Actual
) then
1879 Expr
:= OK_Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
1881 Expr
:= Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
1884 Expr
:= New_Occurrence_Of
(Temp
, Loc
);
1887 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Sloc
(Actual
)));
1890 -- If the actual is a conversion of a packed reference, it may
1891 -- already have been expanded by Remove_Side_Effects, and the
1892 -- resulting variable is a temporary which does not designate
1893 -- the proper out-parameter, which may not be addressable. In
1894 -- that case, generate an assignment to the original expression
1895 -- (before expansion of the packed reference) so that the proper
1896 -- expansion of assignment to a packed component can take place.
1903 if Is_Renaming_Of_Object
(Var
)
1904 and then Nkind
(Renamed_Object
(Var
)) = N_Selected_Component
1905 and then Nkind
(Original_Node
(Prefix
(Renamed_Object
(Var
))))
1906 = N_Indexed_Component
1908 Has_Non_Standard_Rep
(Etype
(Prefix
(Renamed_Object
(Var
))))
1910 Obj
:= Renamed_Object
(Var
);
1912 Make_Selected_Component
(Loc
,
1914 New_Copy_Tree
(Original_Node
(Prefix
(Obj
))),
1915 Selector_Name
=> New_Copy
(Selector_Name
(Obj
)));
1916 Reset_Analyzed_Flags
(Lhs
);
1919 Lhs
:= New_Occurrence_Of
(Var
, Loc
);
1922 Set_Assignment_OK
(Lhs
);
1924 if Is_Access_Type
(E_Formal
)
1925 and then Is_Entity_Name
(Lhs
)
1927 Present
(Effective_Extra_Accessibility
(Entity
(Lhs
)))
1928 and then not No_Dynamic_Accessibility_Checks_Enabled
(Lhs
)
1930 -- Copyback target is an Ada 2012 stand-alone object of an
1931 -- anonymous access type.
1933 pragma Assert
(Ada_Version
>= Ada_2012
);
1935 Apply_Accessibility_Check
(Lhs
, E_Formal
, N
);
1937 Append_To
(Post_Call
,
1938 Make_Assignment_Statement
(Loc
,
1940 Expression
=> Expr
));
1942 -- We would like to somehow suppress generation of the
1943 -- extra_accessibility assignment generated by the expansion
1944 -- of the above assignment statement. It's not a correctness
1945 -- issue because the following assignment renders it dead,
1946 -- but generating back-to-back assignments to the same
1947 -- target is undesirable. ???
1949 Append_To
(Post_Call
,
1950 Make_Assignment_Statement
(Loc
,
1951 Name
=> New_Occurrence_Of
(
1952 Effective_Extra_Accessibility
(Entity
(Lhs
)), Loc
),
1953 Expression
=> Make_Integer_Literal
(Loc
,
1954 Type_Access_Level
(E_Formal
))));
1957 if Is_Access_Type
(E_Formal
)
1958 and then Can_Never_Be_Null
(Etype
(Actual
))
1959 and then not Can_Never_Be_Null
(E_Formal
)
1961 Append_To
(Post_Call
,
1962 Make_Raise_Constraint_Error
(Loc
,
1965 Left_Opnd
=> New_Occurrence_Of
(Temp
, Loc
),
1966 Right_Opnd
=> Make_Null
(Loc
)),
1967 Reason
=> CE_Access_Check_Failed
));
1970 Append_To
(Post_Call
,
1971 Make_Assignment_Statement
(Loc
,
1973 Expression
=> Expr
));
1976 -- Add a copy-back to reflect any potential changes in value
1977 -- back into the original object, if any.
1979 if Is_Validation_Variable_Reference
(Lhs
) then
1980 Add_Validation_Call_By_Copy_Code
(Lhs
);
1984 end Add_Call_By_Copy_Code
;
1986 ----------------------------------
1987 -- Add_Simple_Call_By_Copy_Code --
1988 ----------------------------------
1990 procedure Add_Simple_Call_By_Copy_Code
(Force
: Boolean) is
1991 With_Storage_Model
: constant Boolean :=
1992 Nkind
(Actual
) = N_Explicit_Dereference
1994 Has_Designated_Storage_Model_Aspect
(Etype
(Prefix
(Actual
)));
2007 -- Unless forced not to, check the legality of the copy operation
2009 if not Force
and then not Is_Legal_Copy
then
2013 F_Typ
:= Etype
(Formal
);
2015 -- Handle formals whose type comes from the limited view
2017 if From_Limited_With
(F_Typ
)
2018 and then Has_Non_Limited_View
(F_Typ
)
2020 F_Typ
:= Non_Limited_View
(F_Typ
);
2023 -- Use formal type for temp, unless formal type is an unconstrained
2024 -- composite, in which case we don't have to worry about checks and
2025 -- we can use the actual type, since that has appropriate bounds.
2027 if Is_Composite_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
2028 Indic
:= New_Occurrence_Of
(Get_Actual_Subtype
(Actual
), Loc
);
2030 Indic
:= New_Occurrence_Of
(F_Typ
, Loc
);
2033 -- Prepare to generate code
2035 Reset_Packed_Prefix
;
2037 Incod
:= Relocate_Node
(Actual
);
2038 Outcod
:= New_Copy_Tree
(Incod
);
2040 -- Generate declaration of temporary variable, initializing it
2041 -- with the input parameter unless we have an OUT formal or
2042 -- this is an initialization call.
2044 if Ekind
(Formal
) = E_Out_Parameter
then
2047 elsif Inside_Init_Proc
then
2049 -- Skip using the actual as the expression in Decl if we are in
2050 -- an init proc and it is not a component which depends on a
2051 -- discriminant, because, in this case, we need to use the actual
2052 -- type of the component instead.
2054 if Nkind
(Actual
) /= N_Selected_Component
2056 not Has_Discriminant_Dependent_Constraint
2057 (Entity
(Selector_Name
(Actual
)))
2061 -- Otherwise, keep the component so we can generate the proper
2062 -- actual subtype - since the subtype depends on enclosing
2072 if With_Storage_Model
then
2074 Build_Temporary_On_Secondary_Stack
(Loc
, Entity
(Indic
), Cpcod
);
2076 if Present
(Incod
) then
2078 Make_Assignment_Statement
(Loc
,
2080 Make_Explicit_Dereference
(Loc
,
2081 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)),
2082 Expression
=> Incod
));
2083 Set_Suppress_Assignment_Checks
(Last
(Cpcod
));
2087 Temp
:= Make_Temporary
(Loc
, 'T', Actual
);
2090 Make_Object_Declaration
(Loc
,
2091 Defining_Identifier
=> Temp
,
2092 Object_Definition
=> Indic
,
2093 Expression
=> Incod
);
2095 -- If the call is to initialize a component of a composite type,
2096 -- and the component does not depend on discriminants, use the
2097 -- actual type of the component. This is required in case the
2098 -- component is constrained, because in general the formal of the
2099 -- initialization procedure will be unconstrained. Note that if
2100 -- the component being initialized is constrained by an enclosing
2101 -- discriminant, the presence of the initialization in the
2102 -- declaration will generate an expression for the actual subtype.
2104 if Inside_Init_Proc
and then No
(Incod
) then
2105 Set_No_Initialization
(Decl
);
2106 Set_Object_Definition
(Decl
,
2107 New_Occurrence_Of
(Etype
(Actual
), Loc
));
2110 Append_To
(Cpcod
, Decl
);
2113 Insert_Actions
(N
, Cpcod
);
2115 -- The actual is simply a reference to the temporary
2117 if With_Storage_Model
then
2119 Make_Explicit_Dereference
(Loc
,
2120 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
2122 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
2127 -- Generate copy out if OUT or IN OUT parameter
2129 if Ekind
(Formal
) /= E_In_Parameter
then
2132 if With_Storage_Model
then
2134 Make_Explicit_Dereference
(Loc
,
2135 Prefix
=> New_Occurrence_Of
(Temp
, Loc
));
2137 Rhs
:= New_Occurrence_Of
(Temp
, Loc
);
2138 Set_Is_True_Constant
(Temp
, False);
2141 -- Deal with conversion
2143 if Nkind
(Lhs
) = N_Type_Conversion
then
2144 Lhs
:= Expression
(Lhs
);
2145 Rhs
:= Convert_To
(Etype
(Actual
), Rhs
);
2148 Append_To
(Post_Call
,
2149 Make_Assignment_Statement
(Loc
,
2151 Expression
=> Rhs
));
2152 Set_Suppress_Assignment_Checks
(Last
(Post_Call
));
2153 Set_Assignment_OK
(Name
(Last
(Post_Call
)));
2155 end Add_Simple_Call_By_Copy_Code
;
2157 --------------------------------------
2158 -- Add_Validation_Call_By_Copy_Code --
2159 --------------------------------------
2161 procedure Add_Validation_Call_By_Copy_Code
(Act
: Node_Id
) is
2162 Var
: constant Node_Id
:= Unqual_Conv
(Act
);
2166 Obj_Typ
: Entity_Id
;
2170 -- Generate range check if required
2172 if Do_Range_Check
(Actual
) then
2173 Generate_Range_Check
(Actual
, E_Formal
, CE_Range_Check_Failed
);
2176 -- If there is a type conversion in the actual, it will be reinstated
2177 -- below, the new instance will be properly analyzed and the setting
2178 -- of the Do_Range_Check flag recomputed so remove the obsolete one.
2180 if Nkind
(Actual
) = N_Type_Conversion
then
2181 Set_Do_Range_Check
(Expression
(Actual
), False);
2184 -- Copy the value of the validation variable back into the object
2187 if Is_Entity_Name
(Var
) then
2188 Var_Id
:= Entity
(Var
);
2189 Obj
:= Validated_Object
(Var_Id
);
2190 Obj_Typ
:= Etype
(Obj
);
2192 Expr
:= New_Occurrence_Of
(Var_Id
, Loc
);
2194 -- A type conversion is needed when the validation variable and
2195 -- the validated object carry different types. This case occurs
2196 -- when the actual is qualified in some fashion.
2199 -- subtype Int is Integer range ...;
2200 -- procedure Call (Val : in out Integer);
2204 -- Call (Integer (Object));
2208 -- Var : Integer := Object; -- conversion to base type
2209 -- if not Var'Valid then -- validity check
2210 -- Call (Var); -- modify Var
2211 -- Object := Int (Var); -- conversion to subtype
2213 if Etype
(Var_Id
) /= Obj_Typ
then
2215 Make_Type_Conversion
(Loc
,
2216 Subtype_Mark
=> New_Occurrence_Of
(Obj_Typ
, Loc
),
2217 Expression
=> Expr
);
2223 -- Object := Object_Type (Var);
2225 Append_To
(Post_Call
,
2226 Make_Assignment_Statement
(Loc
,
2228 Expression
=> Expr
));
2230 -- If the flow reaches this point, then this routine was invoked with
2231 -- an actual which does not denote a validation variable.
2234 pragma Assert
(False);
2237 end Add_Validation_Call_By_Copy_Code
;
2239 ---------------------------
2240 -- Check_Fortran_Logical --
2241 ---------------------------
2243 procedure Check_Fortran_Logical
is
2244 Logical
: constant Entity_Id
:= Etype
(Formal
);
2247 -- Note: this is very incomplete, e.g. it does not handle arrays
2248 -- of logical values. This is really not the right approach at all???)
2251 if Convention
(Subp
) = Convention_Fortran
2252 and then Root_Type
(Etype
(Formal
)) = Standard_Boolean
2253 and then Ekind
(Formal
) /= E_In_Parameter
2255 Var
:= Make_Var
(Actual
);
2256 Append_To
(Post_Call
,
2257 Make_Assignment_Statement
(Loc
,
2258 Name
=> New_Occurrence_Of
(Var
, Loc
),
2260 Unchecked_Convert_To
(
2263 Left_Opnd
=> New_Occurrence_Of
(Var
, Loc
),
2265 Unchecked_Convert_To
(
2267 New_Occurrence_Of
(Standard_False
, Loc
))))));
2269 end Check_Fortran_Logical
;
2275 function Is_Legal_Copy
return Boolean is
2277 -- An attempt to copy a value of such a type can only occur if
2278 -- representation clauses give the actual a misaligned address.
2280 if Is_By_Reference_Type
(Etype
(Formal
))
2281 or else Is_Aliased
(Formal
)
2282 or else (Mechanism
(Formal
) = By_Reference
2283 and then not Has_Foreign_Convention
(Subp
))
2286 -- The actual may in fact be properly aligned but there is not
2287 -- enough front-end information to determine this. In that case
2288 -- gigi will emit an error or a warning if a copy is not legal,
2289 -- or generate the proper code.
2293 -- For users of Starlet, we assume that the specification of by-
2294 -- reference mechanism is mandatory. This may lead to unaligned
2295 -- objects but at least for DEC legacy code it is known to work.
2296 -- The warning will alert users of this code that a problem may
2299 elsif Mechanism
(Formal
) = By_Reference
2300 and then Ekind
(Scope
(Formal
)) = E_Procedure
2301 and then Is_Valued_Procedure
(Scope
(Formal
))
2304 ("by_reference actual may be misaligned??", Actual
);
2316 function Make_Var
(Actual
: Node_Id
) return Entity_Id
is
2320 if Is_Entity_Name
(Actual
) then
2321 return Entity
(Actual
);
2324 Var
:= Make_Temporary
(Loc
, 'T', Actual
);
2327 Make_Object_Renaming_Declaration
(Loc
,
2328 Defining_Identifier
=> Var
,
2330 New_Occurrence_Of
(Etype
(Actual
), Loc
),
2331 Name
=> Relocate_Node
(Actual
));
2333 Insert_Action
(N
, N_Node
);
2338 -------------------------
2339 -- Reset_Packed_Prefix --
2340 -------------------------
2342 procedure Reset_Packed_Prefix
is
2343 Pfx
: Node_Id
:= Actual
;
2346 Set_Analyzed
(Pfx
, False);
2348 Nkind
(Pfx
) not in N_Selected_Component | N_Indexed_Component
;
2349 Pfx
:= Prefix
(Pfx
);
2351 end Reset_Packed_Prefix
;
2353 ----------------------------------------
2354 -- Requires_Atomic_Or_Volatile_Copy --
2355 ----------------------------------------
2357 function Requires_Atomic_Or_Volatile_Copy
return Boolean is
2359 -- If the formal is already passed by copy, no need to do anything
2361 if Is_By_Copy_Type
(E_Formal
) then
2365 -- There is no requirement inside initialization procedures and this
2366 -- would generate copies for atomic or volatile composite components.
2368 if Inside_Init_Proc
then
2372 -- Check for atomicity mismatch
2374 if Is_Atomic_Object
(Actual
) and then not Is_Atomic
(E_Formal
)
2376 if Comes_From_Source
(N
) then
2378 ("??atomic actual passed by copy (RM C.6(19))", Actual
);
2383 -- Check for volatility mismatch
2385 if Is_Volatile_Object_Ref
(Actual
) and then not Is_Volatile
(E_Formal
)
2387 if Comes_From_Source
(N
) then
2389 ("??volatile actual passed by copy (RM C.6(19))", Actual
);
2395 end Requires_Atomic_Or_Volatile_Copy
;
2397 -- Start of processing for Expand_Actuals
2400 Post_Call
:= New_List
;
2402 Formal
:= First_Formal
(Subp
);
2403 Actual
:= First_Actual
(N
);
2404 while Present
(Formal
) loop
2405 E_Formal
:= Etype
(Formal
);
2406 E_Actual
:= Etype
(Actual
);
2408 -- Handle formals whose type comes from the limited view
2410 if From_Limited_With
(E_Formal
)
2411 and then Has_Non_Limited_View
(E_Formal
)
2413 E_Formal
:= Non_Limited_View
(E_Formal
);
2416 if Is_Scalar_Type
(E_Formal
)
2417 or else Nkind
(Actual
) = N_Slice
2419 Check_Fortran_Logical
;
2423 elsif Ekind
(Formal
) /= E_Out_Parameter
then
2425 -- The unusual case of the current instance of a protected type
2426 -- requires special handling. This can only occur in the context
2427 -- of a call within the body of a protected operation.
2429 if Is_Entity_Name
(Actual
)
2430 and then Ekind
(Entity
(Actual
)) = E_Protected_Type
2431 and then In_Open_Scopes
(Entity
(Actual
))
2433 if Scope
(Subp
) /= Entity
(Actual
) then
2435 ("operation outside protected type may not "
2436 & "call back its protected operations??", Actual
);
2440 Expand_Protected_Object_Reference
(N
, Entity
(Actual
)));
2443 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
2444 -- build-in-place function, then a temporary return object needs
2445 -- to be created and access to it must be passed to the function
2446 -- (and ensure that we have an activation chain defined for tasks
2447 -- and a Master variable).
2449 -- Currently we limit such functions to those with inherently
2450 -- limited result subtypes, but eventually we plan to expand the
2451 -- functions that are treated as build-in-place to include other
2452 -- composite result types.
2454 -- But do not do it here for intrinsic subprograms since this will
2455 -- be done properly after the subprogram is expanded.
2457 if Is_Intrinsic_Subprogram
(Subp
) then
2460 elsif Is_Build_In_Place_Function_Call
(Actual
) then
2461 if Might_Have_Tasks
(Etype
(Actual
)) then
2462 Build_Activation_Chain_Entity
(N
);
2463 Build_Master_Entity
(Etype
(Actual
));
2466 Make_Build_In_Place_Call_In_Anonymous_Context
(Actual
);
2468 -- Ada 2005 (AI-318-02): Specialization of the previous case for
2469 -- actuals containing build-in-place function calls whose returned
2470 -- object covers interface types.
2472 elsif Present
(Unqual_BIP_Iface_Function_Call
(Actual
)) then
2473 Build_Activation_Chain_Entity
(N
);
2474 Build_Master_Entity
(Etype
(Actual
));
2475 Make_Build_In_Place_Iface_Call_In_Anonymous_Context
(Actual
);
2478 Apply_Constraint_Check
(Actual
, E_Formal
);
2480 -- Out parameter case. No constraint checks on access type
2481 -- RM 6.4.1 (13), but on return a null-excluding check may be
2482 -- required (see below).
2484 elsif Is_Access_Type
(E_Formal
) then
2489 elsif Has_Discriminants
(Base_Type
(E_Formal
))
2490 or else Has_Non_Null_Base_Init_Proc
(E_Formal
)
2492 Apply_Constraint_Check
(Actual
, E_Formal
);
2497 Apply_Constraint_Check
(Actual
, Base_Type
(E_Formal
));
2500 -- Processing for IN-OUT and OUT parameters
2502 if Ekind
(Formal
) /= E_In_Parameter
then
2504 -- For type conversions of arrays, apply length/range checks
2506 if Is_Array_Type
(E_Formal
)
2507 and then Nkind
(Actual
) = N_Type_Conversion
2509 if Is_Constrained
(E_Formal
) then
2510 Apply_Length_Check
(Expression
(Actual
), E_Formal
);
2512 Apply_Range_Check
(Expression
(Actual
), E_Formal
);
2516 -- If argument is a type conversion for a type that is passed by
2517 -- copy, then we must pass the parameter by copy.
2519 if Nkind
(Actual
) = N_Type_Conversion
2521 (Is_Elementary_Type
(E_Formal
)
2522 or else Is_Bit_Packed_Array
(Etype
(Formal
))
2523 or else Is_Bit_Packed_Array
(Etype
(Expression
(Actual
)))
2525 -- Also pass by copy if change of representation
2527 or else not Has_Compatible_Representation
2528 (Target_Typ
=> Etype
(Formal
),
2529 Operand_Typ
=> Etype
(Expression
(Actual
))))
2531 Add_Call_By_Copy_Code
;
2533 -- References to components of bit-packed arrays are expanded
2534 -- at this point, rather than at the point of analysis of the
2535 -- actuals, to handle the expansion of the assignment to
2536 -- [in] out parameters.
2538 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
2539 Add_Simple_Call_By_Copy_Code
(Force
=> True);
2541 -- If the actual has a nonnative storage model, we need a copy
2543 elsif Nkind
(Actual
) = N_Explicit_Dereference
2545 Has_Designated_Storage_Model_Aspect
(Etype
(Prefix
(Actual
)))
2547 (Present
(Storage_Model_Copy_To
2548 (Storage_Model_Object
(Etype
(Prefix
(Actual
)))))
2550 (Ekind
(Formal
) = E_In_Out_Parameter
2552 Present
(Storage_Model_Copy_From
2553 (Storage_Model_Object
(Etype
(Prefix
(Actual
)))))))
2555 Add_Simple_Call_By_Copy_Code
(Force
=> True);
2557 -- If a nonscalar actual is possibly bit-aligned, we need a copy
2558 -- because the back-end cannot cope with such objects. In other
2559 -- cases where alignment forces a copy, the back-end generates
2560 -- it properly. It should not be generated unconditionally in the
2561 -- front-end because it does not know precisely the alignment
2562 -- requirements of the target, and makes too conservative an
2563 -- estimate, leading to superfluous copies or spurious errors
2564 -- on by-reference parameters.
2566 elsif Nkind
(Actual
) = N_Selected_Component
2568 Component_May_Be_Bit_Aligned
(Entity
(Selector_Name
(Actual
)))
2569 and then not Represented_As_Scalar
(Etype
(Formal
))
2571 Add_Simple_Call_By_Copy_Code
(Force
=> False);
2573 -- References to slices of bit-packed arrays are expanded
2575 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
2576 Add_Call_By_Copy_Code
;
2578 -- References to possibly unaligned slices of arrays are expanded
2580 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
2581 Add_Call_By_Copy_Code
;
2583 -- Deal with access types where the actual subtype and the
2584 -- formal subtype are not the same, requiring a check.
2586 -- It is necessary to exclude tagged types because of "downward
2587 -- conversion" errors, but null-excluding checks on return may be
2590 elsif Is_Access_Type
(E_Formal
)
2591 and then not Is_Tagged_Type
(Designated_Type
(E_Formal
))
2592 and then (not Same_Type
(E_Formal
, E_Actual
)
2593 or else (Can_Never_Be_Null
(E_Actual
)
2594 and then not Can_Never_Be_Null
(E_Formal
)))
2596 Add_Call_By_Copy_Code
;
2598 -- We may need to force a copy because of atomicity or volatility
2601 elsif Requires_Atomic_Or_Volatile_Copy
then
2602 Add_Call_By_Copy_Code
;
2604 -- Add call-by-copy code for the case of scalar out parameters
2605 -- when it is not known at compile time that the subtype of the
2606 -- formal is a subrange of the subtype of the actual (or vice
2607 -- versa for in out parameters), in order to get range checks
2608 -- on such actuals. (Maybe this case should be handled earlier
2609 -- in the if statement???)
2611 elsif Is_Scalar_Type
(E_Formal
)
2613 (not In_Subrange_Of
(E_Formal
, E_Actual
)
2615 (Ekind
(Formal
) = E_In_Out_Parameter
2616 and then not In_Subrange_Of
(E_Actual
, E_Formal
)))
2618 Add_Call_By_Copy_Code
;
2620 -- The actual denotes a variable which captures the value of an
2621 -- object for validation purposes. Add a copy-back to reflect any
2622 -- potential changes in value back into the original object.
2624 -- Var : ... := Object;
2625 -- if not Var'Valid then -- validity check
2626 -- Call (Var); -- modify var
2627 -- Object := Var; -- update Object
2629 elsif Is_Validation_Variable_Reference
(Actual
) then
2630 Add_Validation_Call_By_Copy_Code
(Actual
);
2633 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
2634 -- by-reference parameters on exit from the call. If the actual
2635 -- is a derived type and the operation is inherited, the body
2636 -- of the operation will not contain a call to the predicate
2637 -- function, so it must be done explicitly after the call. Ditto
2638 -- if the actual is an entity of a predicated subtype.
2640 -- The rule refers to by-reference types, but a check is needed
2641 -- for by-copy types as well. That check is subsumed by the rule
2642 -- for subtype conversion on assignment, but we can generate the
2643 -- required check now.
2645 -- Note also that Subp may be either a subprogram entity for
2646 -- direct calls, or a type entity for indirect calls, which must
2647 -- be handled separately because the name does not denote an
2648 -- overloadable entity.
2650 By_Ref_Predicate_Check
: declare
2651 Aund
: constant Entity_Id
:= Underlying_Type
(E_Actual
);
2661 if Predicate_Enabled
(Atyp
)
2663 -- Skip predicate checks for special cases
2665 and then Predicate_Tests_On_Arguments
(Subp
)
2667 Append_To
(Post_Call
,
2668 Make_Predicate_Check
(Atyp
, Actual
));
2670 end By_Ref_Predicate_Check
;
2672 -- Processing for IN parameters
2675 -- Generate range check if required
2677 if Do_Range_Check
(Actual
) then
2678 Generate_Range_Check
(Actual
, E_Formal
, CE_Range_Check_Failed
);
2681 -- For IN parameters in the bit-packed array case, we expand an
2682 -- indexed component (the circuit in Exp_Ch4 deliberately left
2683 -- indexed components appearing as actuals untouched, so that
2684 -- the special processing above for the OUT and IN OUT cases
2685 -- could be performed. We could make the test in Exp_Ch4 more
2686 -- complex and have it detect the parameter mode, but it is
2687 -- easier simply to handle all cases here.)
2689 if Nkind
(Actual
) = N_Indexed_Component
2690 and then Is_Bit_Packed_Array
(Etype
(Prefix
(Actual
)))
2692 Reset_Packed_Prefix
;
2693 Expand_Packed_Element_Reference
(Actual
);
2695 -- If we have a reference to a bit-packed array, we copy it, since
2696 -- the actual must be byte aligned.
2698 -- Is this really necessary in all cases???
2700 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
2701 Add_Simple_Call_By_Copy_Code
(Force
=> True);
2703 -- If the actual has a nonnative storage model, we need a copy
2705 elsif Nkind
(Actual
) = N_Explicit_Dereference
2707 Has_Designated_Storage_Model_Aspect
(Etype
(Prefix
(Actual
)))
2709 Present
(Storage_Model_Copy_From
2710 (Storage_Model_Object
(Etype
(Prefix
(Actual
)))))
2712 Add_Simple_Call_By_Copy_Code
(Force
=> True);
2714 -- If we have a C++ constructor call, we need to create the object
2716 elsif Is_CPP_Constructor_Call
(Actual
) then
2717 Add_Simple_Call_By_Copy_Code
(Force
=> True);
2719 -- If a nonscalar actual is possibly unaligned, we need a copy
2721 elsif Is_Possibly_Unaligned_Object
(Actual
)
2722 and then not Represented_As_Scalar
(Etype
(Formal
))
2724 Add_Simple_Call_By_Copy_Code
(Force
=> False);
2726 -- Similarly, we have to expand slices of packed arrays here
2727 -- because the result must be byte aligned.
2729 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
2730 Add_Call_By_Copy_Code
;
2732 -- Only processing remaining is to pass by copy if this is a
2733 -- reference to a possibly unaligned slice, since the caller
2734 -- expects an appropriately aligned argument.
2736 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
2737 Add_Call_By_Copy_Code
;
2739 -- We may need to force a copy because of atomicity or volatility
2742 elsif Requires_Atomic_Or_Volatile_Copy
then
2743 Add_Call_By_Copy_Code
;
2745 -- An unusual case: a current instance of an enclosing task can be
2746 -- an actual, and must be replaced by a reference to self.
2748 elsif Is_Entity_Name
(Actual
)
2749 and then Is_Task_Type
(Entity
(Actual
))
2751 if In_Open_Scopes
(Entity
(Actual
)) then
2753 (Make_Function_Call
(Loc
,
2754 Name
=> New_Occurrence_Of
(RTE
(RE_Self
), Loc
))));
2757 -- A task type cannot otherwise appear as an actual
2760 raise Program_Error
;
2765 -- Type-invariant checks for in-out and out parameters, as well as
2766 -- for in parameters of procedures (AI05-0289 and AI12-0044).
2768 if Ekind
(Formal
) /= E_In_Parameter
2769 or else Ekind
(Subp
) = E_Procedure
2771 Caller_Side_Invariant_Checks
: declare
2773 function Is_Public_Subp
return Boolean;
2774 -- Check whether the subprogram being called is a visible
2775 -- operation of the type of the actual. Used to determine
2776 -- whether an invariant check must be generated on the
2779 ---------------------
2780 -- Is_Public_Subp --
2781 ---------------------
2783 function Is_Public_Subp
return Boolean is
2784 Pack
: constant Entity_Id
:= Scope
(Subp
);
2785 Subp_Decl
: Node_Id
;
2788 if not Is_Subprogram
(Subp
) then
2791 -- The operation may be inherited, or a primitive of the
2795 Nkind
(Parent
(Subp
)) in N_Private_Extension_Declaration
2796 | N_Full_Type_Declaration
2798 Subp_Decl
:= Parent
(Subp
);
2801 Subp_Decl
:= Unit_Declaration_Node
(Subp
);
2804 return Ekind
(Pack
) = E_Package
2806 List_Containing
(Subp_Decl
) =
2807 Visible_Declarations
2808 (Specification
(Unit_Declaration_Node
(Pack
)));
2811 -- Start of processing for Caller_Side_Invariant_Checks
2814 -- We generate caller-side invariant checks in two cases:
2816 -- a) when calling an inherited operation, where there is an
2817 -- implicit view conversion of the actual to the parent type.
2819 -- b) When the conversion is explicit
2821 -- We treat these cases separately because the required
2822 -- conversion for a) is added later when expanding the call.
2824 if Has_Invariants
(Etype
(Actual
))
2826 Nkind
(Parent
(Etype
(Actual
)))
2827 = N_Private_Extension_Declaration
2829 if Comes_From_Source
(N
) and then Is_Public_Subp
then
2830 Append_To
(Post_Call
, Make_Invariant_Call
(Actual
));
2833 elsif Nkind
(Actual
) = N_Type_Conversion
2834 and then Has_Invariants
(Etype
(Expression
(Actual
)))
2836 if Comes_From_Source
(N
) and then Is_Public_Subp
then
2838 (Post_Call
, Make_Invariant_Call
(Expression
(Actual
)));
2841 end Caller_Side_Invariant_Checks
;
2844 Next_Formal
(Formal
);
2845 Next_Actual
(Actual
);
2853 procedure Expand_Call
(N
: Node_Id
) is
2854 function Is_Unchecked_Union_Equality
(N
: Node_Id
) return Boolean;
2855 -- Return True if N is a call to the predefined equality operator of an
2856 -- unchecked union type, or a renaming thereof.
2858 ---------------------------------
2859 -- Is_Unchecked_Union_Equality --
2860 ---------------------------------
2862 function Is_Unchecked_Union_Equality
(N
: Node_Id
) return Boolean is
2864 if Is_Entity_Name
(Name
(N
))
2865 and then Ekind
(Entity
(Name
(N
))) = E_Function
2866 and then Present
(First_Formal
(Entity
(Name
(N
))))
2868 Is_Unchecked_Union
(Etype
(First_Formal
(Entity
(Name
(N
)))))
2871 Func
: constant Entity_Id
:= Entity
(Name
(N
));
2872 Typ
: constant Entity_Id
:= Etype
(First_Formal
(Func
));
2873 Decl
: constant Node_Id
:=
2874 Original_Node
(Parent
(Declaration_Node
(Func
)));
2877 return Func
= TSS
(Typ
, TSS_Composite_Equality
)
2878 or else (Nkind
(Decl
) = N_Subprogram_Renaming_Declaration
2879 and then Nkind
(Name
(Decl
)) = N_Operator_Symbol
2880 and then Chars
(Name
(Decl
)) = Name_Op_Eq
2881 and then Ekind
(Entity
(Name
(Decl
))) = E_Operator
);
2887 end Is_Unchecked_Union_Equality
;
2889 -- If this is an indirect call through an Access_To_Subprogram
2890 -- with contract specifications, it is rewritten as a call to
2891 -- the corresponding Access_Subprogram_Wrapper with the same
2892 -- actuals, whose body contains a naked indirect call (which
2893 -- itself must not be rewritten, to prevent infinite recursion).
2895 Must_Rewrite_Indirect_Call
: constant Boolean :=
2896 Ada_Version
>= Ada_2022
2897 and then Nkind
(Name
(N
)) = N_Explicit_Dereference
2898 and then Ekind
(Etype
(Name
(N
))) = E_Subprogram_Type
2900 (Access_Subprogram_Wrapper
(Etype
(Name
(N
))));
2902 Post_Call
: List_Id
;
2904 -- Start of processing for Expand_Call
2907 pragma Assert
(Nkind
(N
) in N_Entry_Call_Statement
2909 | N_Procedure_Call_Statement
);
2911 -- Check that this is not the call in the body of the access
2912 -- subprogram wrapper or the postconditions wrapper.
2914 if Must_Rewrite_Indirect_Call
2915 and then (not Is_Overloadable
(Current_Scope
)
2916 or else not (Is_Access_Subprogram_Wrapper
(Current_Scope
)
2918 (Chars
(Current_Scope
) = Name_uWrapped_Statements
2919 and then Is_Access_Subprogram_Wrapper
2920 (Scope
(Current_Scope
)))))
2923 Loc
: constant Source_Ptr
:= Sloc
(N
);
2924 Wrapper
: constant Entity_Id
:=
2925 Access_Subprogram_Wrapper
(Etype
(Name
(N
)));
2926 Ptr
: constant Node_Id
:= Prefix
(Name
(N
));
2927 Ptr_Type
: constant Entity_Id
:= Etype
(Ptr
);
2928 Typ
: constant Entity_Id
:= Etype
(N
);
2931 Parms
: List_Id
:= Parameter_Associations
(N
);
2935 -- The last actual in the call is the pointer itself.
2936 -- If the aspect is inherited, convert the pointer to the
2937 -- parent type that specifies the contract.
2938 -- If the original access_to_subprogram has defaults for
2939 -- in_parameters, the call may include named associations, so
2940 -- we create one for the pointer as well.
2942 if Is_Derived_Type
(Ptr_Type
)
2943 and then Ptr_Type
/= Etype
(Last_Formal
(Wrapper
))
2946 Make_Type_Conversion
(Loc
,
2948 (Etype
(Last_Formal
(Wrapper
)), Loc
), Ptr
);
2954 -- Handle parameterless subprogram.
2961 (Make_Parameter_Association
(Loc
,
2962 Selector_Name
=> Make_Identifier
(Loc
,
2963 Chars
(Last_Formal
(Wrapper
))),
2964 Explicit_Actual_Parameter
=> Ptr_Act
),
2967 if Nkind
(N
) = N_Procedure_Call_Statement
then
2968 New_N
:= Make_Procedure_Call_Statement
(Loc
,
2969 Name
=> New_Occurrence_Of
(Wrapper
, Loc
),
2970 Parameter_Associations
=> Parms
);
2972 New_N
:= Make_Function_Call
(Loc
,
2973 Name
=> New_Occurrence_Of
(Wrapper
, Loc
),
2974 Parameter_Associations
=> Parms
);
2978 Analyze_And_Resolve
(N
, Typ
);
2981 -- Case of a call to the predefined equality operator of an unchecked
2982 -- union type, which requires specific processing.
2984 elsif Is_Unchecked_Union_Equality
(N
) then
2986 Eq
: constant Entity_Id
:= Entity
(Name
(N
));
2989 Expand_Unchecked_Union_Equality
(N
);
2991 -- If the call was not rewritten as a raise, expand the actuals
2993 if Nkind
(N
) = N_Function_Call
then
2994 pragma Assert
(Check_Number_Of_Actuals
(N
, Eq
));
2995 Expand_Actuals
(N
, Eq
, Post_Call
);
2996 pragma Assert
(Is_Empty_List
(Post_Call
));
3003 Expand_Call_Helper
(N
, Post_Call
);
3004 Insert_Post_Call_Actions
(N
, Post_Call
);
3008 ------------------------
3009 -- Expand_Call_Helper --
3010 ------------------------
3012 -- This procedure handles expansion of function calls and procedure call
3013 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
3014 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
3016 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
3017 -- Provide values of actuals for all formals in Extra_Formals list
3018 -- Replace "call" to enumeration literal function by literal itself
3019 -- Rewrite call to predefined operator as operator
3020 -- Replace actuals to in-out parameters that are numeric conversions,
3021 -- with explicit assignment to temporaries before and after the call.
3023 -- Note that the list of actuals has been filled with default expressions
3024 -- during semantic analysis of the call. Only the extra actuals required
3025 -- for the 'Constrained attribute and for accessibility checks are added
3028 procedure Expand_Call_Helper
(N
: Node_Id
; Post_Call
: out List_Id
) is
3029 Loc
: constant Source_Ptr
:= Sloc
(N
);
3030 Call_Node
: Node_Id
:= N
;
3031 Extra_Actuals
: List_Id
:= No_List
;
3032 Prev
: Node_Id
:= Empty
;
3034 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
);
3035 -- Adds one entry to the end of the actual parameter list. Used for
3036 -- default parameters and for extra actuals (for Extra_Formals). The
3037 -- argument is an N_Parameter_Association node.
3039 procedure Add_Cond_Expression_Extra_Actual
(Formal
: Entity_Id
);
3040 -- Adds extra accessibility actuals in the case of a conditional
3041 -- expression corresponding to Formal.
3043 -- Note: Conditional expressions used as actuals for anonymous access
3044 -- formals complicate the process of propagating extra accessibility
3045 -- actuals and must be handled in a recursive fashion since they can
3046 -- be embedded within each other.
3048 procedure Add_Dummy_Build_In_Place_Actuals
3049 (Function_Id
: Entity_Id
;
3050 Num_Added_Extra_Actuals
: Nat
:= 0);
3051 -- Adds dummy actuals for the BIP extra formals of the called function.
3052 -- Num_Added_Extra_Actuals is the number of non-BIP extra actuals added
3053 -- to the actuals immediately before calling this subprogram.
3055 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
);
3056 -- Adds an extra actual to the list of extra actuals. Expr is the
3057 -- expression for the value of the actual, EF is the entity for the
3060 procedure Add_View_Conversion_Invariants
3061 (Formal
: Entity_Id
;
3063 -- Adds invariant checks for every intermediate type between the range
3064 -- of a view converted argument to its ancestor (from parent to child).
3066 function Can_Fold_Predicate_Call
(P
: Entity_Id
) return Boolean;
3067 -- Try to constant-fold a predicate check, which often enough is a
3068 -- simple arithmetic expression that can be computed statically if
3069 -- its argument is static. This cleans up the output of CCG, even
3070 -- though useless predicate checks will be generally removed by
3071 -- back-end optimizations.
3073 procedure Check_Subprogram_Variant
;
3074 -- Emit a call to the internally generated procedure with checks for
3075 -- aspect Subprogram_Variant, if present and enabled.
3077 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
;
3078 -- Within an instance, a type derived from an untagged formal derived
3079 -- type inherits from the original parent, not from the actual. The
3080 -- current derivation mechanism has the derived type inherit from the
3081 -- actual, which is only correct outside of the instance. If the
3082 -- subprogram is inherited, we test for this particular case through a
3083 -- convoluted tree traversal before setting the proper subprogram to be
3086 function In_Unfrozen_Instance
(E
: Entity_Id
) return Boolean;
3087 -- Return true if E comes from an instance that is not yet frozen
3089 function Is_Class_Wide_Interface_Type
(E
: Entity_Id
) return Boolean;
3090 -- Return True when E is a class-wide interface type or an access to
3091 -- a class-wide interface type.
3093 function Is_Direct_Deep_Call
(Subp
: Entity_Id
) return Boolean;
3094 -- Determine if Subp denotes a non-dispatching call to a Deep routine
3096 function New_Value
(From
: Node_Id
) return Node_Id
;
3097 -- From is the original Expression. New_Value is equivalent to a call
3098 -- to Duplicate_Subexpr with an explicit dereference when From is an
3099 -- access parameter.
3101 --------------------------
3102 -- Add_Actual_Parameter --
3103 --------------------------
3105 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
) is
3106 Actual_Expr
: constant Node_Id
:=
3107 Explicit_Actual_Parameter
(Insert_Param
);
3110 -- Case of insertion is first named actual
3112 if No
(Prev
) or else
3113 Nkind
(Parent
(Prev
)) /= N_Parameter_Association
3115 Set_Next_Named_Actual
3116 (Insert_Param
, First_Named_Actual
(Call_Node
));
3117 Set_First_Named_Actual
(Call_Node
, Actual_Expr
);
3120 if No
(Parameter_Associations
(Call_Node
)) then
3121 Set_Parameter_Associations
(Call_Node
, New_List
);
3124 Append
(Insert_Param
, Parameter_Associations
(Call_Node
));
3127 Insert_After
(Prev
, Insert_Param
);
3130 -- Case of insertion is not first named actual
3133 Set_Next_Named_Actual
3134 (Insert_Param
, Next_Named_Actual
(Parent
(Prev
)));
3135 Set_Next_Named_Actual
(Parent
(Prev
), Actual_Expr
);
3136 Append
(Insert_Param
, Parameter_Associations
(Call_Node
));
3139 Prev
:= Actual_Expr
;
3140 end Add_Actual_Parameter
;
3142 --------------------------------------
3143 -- Add_Cond_Expression_Extra_Actual --
3144 --------------------------------------
3146 procedure Add_Cond_Expression_Extra_Actual
3147 (Formal
: Entity_Id
)
3152 procedure Insert_Level_Assign
(Branch
: Node_Id
);
3153 -- Recursively add assignment of the level temporary on each branch
3154 -- while moving through nested conditional expressions.
3156 -------------------------
3157 -- Insert_Level_Assign --
3158 -------------------------
3160 procedure Insert_Level_Assign
(Branch
: Node_Id
) is
3162 procedure Expand_Branch
(Res_Assn
: Node_Id
);
3163 -- Perform expansion or iterate further within nested
3164 -- conditionals given the object declaration or assignment to
3165 -- result object created during expansion which represents a
3166 -- branch of the conditional expression.
3172 procedure Expand_Branch
(Res_Assn
: Node_Id
) is
3174 pragma Assert
(Nkind
(Res_Assn
) in
3175 N_Assignment_Statement |
3176 N_Object_Declaration
);
3178 -- There are more nested conditional expressions so we must go
3181 if Nkind
(Expression
(Res_Assn
)) = N_Expression_With_Actions
3183 Nkind
(Original_Node
(Expression
(Res_Assn
)))
3184 in N_Case_Expression | N_If_Expression
3187 (Expression
(Res_Assn
));
3189 -- Add the level assignment
3192 Insert_Before_And_Analyze
(Res_Assn
,
3193 Make_Assignment_Statement
(Loc
,
3194 Name
=> New_Occurrence_Of
(Lvl
, Loc
),
3197 (Expr
=> Expression
(Res_Assn
),
3198 Level
=> Dynamic_Level
,
3199 Allow_Alt_Model
=> False)));
3206 -- Start of processing for Insert_Level_Assign
3209 -- Examine further nested conditionals
3211 pragma Assert
(Nkind
(Branch
) =
3212 N_Expression_With_Actions
);
3214 -- Find the relevant statement in the actions
3216 Cond
:= First
(Actions
(Branch
));
3217 while Present
(Cond
) loop
3218 exit when Nkind
(Cond
) in N_Case_Statement | N_If_Statement
;
3222 -- The conditional expression may have been optimized away, so
3223 -- examine the actions in the branch.
3226 Expand_Branch
(Last
(Actions
(Branch
)));
3228 -- Iterate through if expression branches
3230 elsif Nkind
(Cond
) = N_If_Statement
then
3231 Expand_Branch
(Last
(Then_Statements
(Cond
)));
3232 Expand_Branch
(Last
(Else_Statements
(Cond
)));
3234 -- Iterate through case alternatives
3236 elsif Nkind
(Cond
) = N_Case_Statement
then
3238 Alt
:= First
(Alternatives
(Cond
));
3239 while Present
(Alt
) loop
3240 Expand_Branch
(Last
(Statements
(Alt
)));
3244 end Insert_Level_Assign
;
3246 -- Start of processing for cond expression case
3249 -- Create declaration of a temporary to store the accessibility
3250 -- level of each branch of the conditional expression.
3252 Lvl
:= Make_Temporary
(Loc
, 'L');
3253 Decl
:= Make_Object_Declaration
(Loc
,
3254 Defining_Identifier
=> Lvl
,
3255 Object_Definition
=>
3256 New_Occurrence_Of
(Standard_Natural
, Loc
));
3258 -- Install the declaration and perform necessary expansion if we
3259 -- are dealing with a procedure call.
3261 if Nkind
(Call_Node
) = N_Procedure_Call_Statement
then
3266 -- If_Exp_Res : Typ;
3268 -- Lvl := 0; -- Access level
3269 -- If_Exp_Res := Exp;
3271 -- in If_Exp_Res end;},
3276 Insert_Before_And_Analyze
(Call_Node
, Decl
);
3278 -- Ditto for a function call. Note that we do not wrap the function
3279 -- call into an expression with action to avoid bad interactions with
3280 -- Exp_Ch4.Process_Transient_In_Expression.
3284 -- Lvl : Natural; -- placed above the function call
3290 -- Lvl := 0; -- Access level
3291 -- If_Exp_Res := Exp;
3292 -- in If_Exp_Res end;},
3297 Insert_Action
(Call_Node
, Decl
);
3298 Analyze
(Call_Node
);
3301 -- Decorate the conditional expression with assignments to our level
3304 Insert_Level_Assign
(Prev
);
3306 -- Make our level temporary the passed actual
3309 (Expr
=> New_Occurrence_Of
(Lvl
, Loc
),
3310 EF
=> Extra_Accessibility
(Formal
));
3311 end Add_Cond_Expression_Extra_Actual
;
3313 --------------------------------------
3314 -- Add_Dummy_Build_In_Place_Actuals --
3315 --------------------------------------
3317 procedure Add_Dummy_Build_In_Place_Actuals
3318 (Function_Id
: Entity_Id
;
3319 Num_Added_Extra_Actuals
: Nat
:= 0)
3321 Loc
: constant Source_Ptr
:= Sloc
(Call_Node
);
3322 Formal
: Entity_Id
:= Extra_Formals
(Function_Id
);
3327 -- We never generate extra formals if expansion is not active because
3328 -- we don't need them unless we are generating code. No action needed
3329 -- for thunks since they propagate all their extra actuals.
3331 if not Expander_Active
3332 or else Is_Thunk
(Current_Scope
)
3337 -- Skip already-added non-BIP extra actuals
3339 Skip_Extra
:= Num_Added_Extra_Actuals
;
3340 while Skip_Extra
> 0 loop
3341 pragma Assert
(not Is_Build_In_Place_Entity
(Formal
));
3342 Formal
:= Extra_Formal
(Formal
);
3343 Skip_Extra
:= Skip_Extra
- 1;
3346 -- Append the dummy BIP extra actuals
3348 while Present
(Formal
) loop
3349 pragma Assert
(Is_Build_In_Place_Entity
(Formal
));
3353 if Etype
(Formal
) = Standard_Natural
then
3354 Actual
:= Make_Integer_Literal
(Loc
, Uint_0
);
3355 Analyze_And_Resolve
(Actual
, Standard_Natural
);
3356 Add_Extra_Actual_To_Call
(N
, Formal
, Actual
);
3360 elsif Etype
(Formal
) = Standard_Integer
then
3361 Actual
:= Make_Integer_Literal
(Loc
, Uint_0
);
3362 Analyze_And_Resolve
(Actual
, Standard_Integer
);
3363 Add_Extra_Actual_To_Call
(N
, Formal
, Actual
);
3365 -- BIPstoragepool, BIPfinalizationmaster, BIPactivationchain,
3368 elsif Is_Access_Type
(Etype
(Formal
)) then
3369 Actual
:= Make_Null
(Loc
);
3370 Analyze_And_Resolve
(Actual
, Etype
(Formal
));
3371 Add_Extra_Actual_To_Call
(N
, Formal
, Actual
);
3374 pragma Assert
(False);
3375 raise Program_Error
;
3378 Formal
:= Extra_Formal
(Formal
);
3381 -- Mark the call as processed build-in-place call; required
3382 -- to avoid adding the extra formals twice.
3384 Set_Is_Expanded_Build_In_Place_Call
(Call_Node
);
3386 pragma Assert
(Check_Number_Of_Actuals
(Call_Node
, Function_Id
));
3387 pragma Assert
(Check_BIP_Actuals
(Call_Node
, Function_Id
));
3388 end Add_Dummy_Build_In_Place_Actuals
;
3390 ----------------------
3391 -- Add_Extra_Actual --
3392 ----------------------
3394 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
) is
3395 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
3398 if Extra_Actuals
= No_List
then
3399 Extra_Actuals
:= New_List
;
3400 Set_Parent
(Extra_Actuals
, Call_Node
);
3403 Append_To
(Extra_Actuals
,
3404 Make_Parameter_Association
(Loc
,
3405 Selector_Name
=> New_Occurrence_Of
(EF
, Loc
),
3406 Explicit_Actual_Parameter
=> Expr
));
3408 Analyze_And_Resolve
(Expr
, Etype
(EF
));
3410 if Nkind
(Call_Node
) = N_Function_Call
then
3411 Set_Is_Accessibility_Actual
(Parent
(Expr
));
3413 end Add_Extra_Actual
;
3415 ------------------------------------
3416 -- Add_View_Conversion_Invariants --
3417 ------------------------------------
3419 procedure Add_View_Conversion_Invariants
3420 (Formal
: Entity_Id
;
3424 Curr_Typ
: Entity_Id
;
3425 Inv_Checks
: List_Id
;
3426 Par_Typ
: Entity_Id
;
3429 Inv_Checks
:= No_List
;
3431 -- Extract the argument from a potentially nested set of view
3435 while Nkind
(Arg
) = N_Type_Conversion
loop
3436 Arg
:= Expression
(Arg
);
3439 -- Move up the derivation chain starting with the type of the formal
3440 -- parameter down to the type of the actual object.
3443 Par_Typ
:= Etype
(Arg
);
3444 while Par_Typ
/= Etype
(Formal
) and Par_Typ
/= Curr_Typ
loop
3445 Curr_Typ
:= Par_Typ
;
3447 if Has_Invariants
(Curr_Typ
)
3448 and then Present
(Invariant_Procedure
(Curr_Typ
))
3450 -- Verify the invariant of the current type. Generate:
3452 -- <Curr_Typ>Invariant (Curr_Typ (Arg));
3454 Prepend_New_To
(Inv_Checks
,
3455 Make_Procedure_Call_Statement
(Loc
,
3458 (Invariant_Procedure
(Curr_Typ
), Loc
),
3459 Parameter_Associations
=> New_List
(
3460 Make_Type_Conversion
(Loc
,
3461 Subtype_Mark
=> New_Occurrence_Of
(Curr_Typ
, Loc
),
3462 Expression
=> New_Copy_Tree
(Arg
)))));
3465 Par_Typ
:= Base_Type
(Etype
(Curr_Typ
));
3468 -- If the node is a function call the generated tests have been
3469 -- already handled in Insert_Post_Call_Actions.
3471 if not Is_Empty_List
(Inv_Checks
)
3472 and then Nkind
(Call_Node
) = N_Procedure_Call_Statement
3474 Insert_Actions_After
(Call_Node
, Inv_Checks
);
3476 end Add_View_Conversion_Invariants
;
3478 -----------------------------
3479 -- Can_Fold_Predicate_Call --
3480 -----------------------------
3482 function Can_Fold_Predicate_Call
(P
: Entity_Id
) return Boolean is
3485 function Augments_Other_Dynamic_Predicate
(DP_Aspect_Spec
: Node_Id
)
3487 -- Given a Dynamic_Predicate aspect aspecification for a
3488 -- discrete type, returns True iff another DP specification
3489 -- applies (indirectly, via a subtype type or a derived type)
3490 -- to the same entity that this aspect spec applies to.
3492 function May_Fold
(N
: Node_Id
) return Traverse_Result
;
3493 -- The predicate expression is foldable if it only contains operators
3494 -- and literals. During this check, we also replace occurrences of
3495 -- the formal of the constructed predicate function with the static
3496 -- value of the actual. This is done on a copy of the analyzed
3497 -- expression for the predicate.
3499 --------------------------------------
3500 -- Augments_Other_Dynamic_Predicate --
3501 --------------------------------------
3503 function Augments_Other_Dynamic_Predicate
(DP_Aspect_Spec
: Node_Id
)
3506 Aspect_Bearer
: Entity_Id
:= Entity
(DP_Aspect_Spec
);
3509 Aspect_Bearer
:= Nearest_Ancestor
(Aspect_Bearer
);
3511 if No
(Aspect_Bearer
) then
3516 Aspect_Spec
: constant Node_Id
:=
3517 Find_Aspect
(Aspect_Bearer
, Aspect_Dynamic_Predicate
);
3519 if Present
(Aspect_Spec
)
3520 and then Aspect_Spec
/= DP_Aspect_Spec
3522 -- Found another Dynamic_Predicate aspect spec
3527 end Augments_Other_Dynamic_Predicate
;
3533 function May_Fold
(N
: Node_Id
) return Traverse_Result
is
3539 when N_Expanded_Name
3542 if Ekind
(Entity
(N
)) = E_In_Parameter
3543 and then Entity
(N
) = First_Entity
(P
)
3545 Rewrite
(N
, New_Copy
(Actual
));
3546 Set_Is_Static_Expression
(N
);
3549 elsif Ekind
(Entity
(N
)) = E_Enumeration_Literal
then
3556 when N_Case_Expression
3561 when N_Integer_Literal
=>
3569 function Try_Fold
is new Traverse_Func
(May_Fold
);
3571 -- Other Local variables
3573 Subt
: constant Entity_Id
:= Etype
(First_Entity
(P
));
3577 -- Start of processing for Can_Fold_Predicate_Call
3580 -- Folding is only interesting if the actual is static and its type
3581 -- has a Dynamic_Predicate aspect. For CodePeer we preserve the
3584 Actual
:= First
(Parameter_Associations
(Call_Node
));
3585 Aspect
:= Find_Aspect
(Subt
, Aspect_Dynamic_Predicate
);
3587 -- If actual is a declared constant, retrieve its value
3589 if Is_Entity_Name
(Actual
)
3590 and then Ekind
(Entity
(Actual
)) = E_Constant
3592 Actual
:= Constant_Value
(Entity
(Actual
));
3596 or else Nkind
(Actual
) /= N_Integer_Literal
3597 or else not Has_Dynamic_Predicate_Aspect
(Subt
)
3600 -- Do not fold if multiple applicable predicate aspects
3601 or else Has_Ghost_Predicate_Aspect
(Subt
)
3602 or else Has_Aspect
(Subt
, Aspect_Static_Predicate
)
3603 or else Has_Aspect
(Subt
, Aspect_Predicate
)
3604 or else Augments_Other_Dynamic_Predicate
(Aspect
)
3605 or else CodePeer_Mode
3610 -- Retrieve the analyzed expression for the predicate
3612 Pred
:= New_Copy_Tree
(Expression
(Aspect
));
3614 if Try_Fold
(Pred
) = OK
then
3615 Rewrite
(Call_Node
, Pred
);
3616 Analyze_And_Resolve
(Call_Node
, Standard_Boolean
);
3619 -- Otherwise continue the expansion of the function call
3624 end Can_Fold_Predicate_Call
;
3626 ------------------------------
3627 -- Check_Subprogram_Variant --
3628 ------------------------------
3630 procedure Check_Subprogram_Variant
is
3632 function Duplicate_Params_Without_Extra_Actuals
3633 (Call_Node
: Node_Id
) return List_Id
;
3634 -- Duplicate actual parameters of Call_Node into New_Call without
3637 --------------------------------------------
3638 -- Duplicate_Params_Without_Extra_Actuals --
3639 --------------------------------------------
3641 function Duplicate_Params_Without_Extra_Actuals
3642 (Call_Node
: Node_Id
) return List_Id
3644 Proc_Id
: constant Entity_Id
:= Entity
(Name
(Call_Node
));
3645 Actuals
: constant List_Id
:= Parameter_Associations
(Call_Node
);
3647 Actual
: Node_Or_Entity_Id
;
3651 if Actuals
= No_List
then
3656 Actual
:= First
(Actuals
);
3657 Formal
:= First_Formal
(Proc_Id
);
3659 while Present
(Formal
)
3660 and then Formal
/= Extra_Formals
(Proc_Id
)
3662 Append
(New_Copy
(Actual
), NL
);
3665 Next_Formal
(Formal
);
3670 end Duplicate_Params_Without_Extra_Actuals
;
3674 Variant_Prag
: constant Node_Id
:=
3675 Get_Pragma
(Current_Scope
, Pragma_Subprogram_Variant
);
3678 Pragma_Arg1
: Node_Id
;
3679 Variant_Proc
: Entity_Id
;
3682 if Present
(Variant_Prag
) and then Is_Checked
(Variant_Prag
) then
3685 Expression
(First
(Pragma_Argument_Associations
(Variant_Prag
)));
3687 -- If pragma parameter is still an aggregate, it comes from a
3688 -- structural variant, which is not expanded and ignored for
3689 -- run-time execution.
3691 if Nkind
(Pragma_Arg1
) = N_Aggregate
then
3696 (First
(Component_Associations
(Pragma_Arg1
))))) =
3701 -- Otherwise, analysis of the pragma rewrites its argument with a
3702 -- reference to the internally generated procedure.
3704 Variant_Proc
:= Entity
(Pragma_Arg1
);
3707 Make_Procedure_Call_Statement
(Loc
,
3709 New_Occurrence_Of
(Variant_Proc
, Loc
),
3710 Parameter_Associations
=>
3711 Duplicate_Params_Without_Extra_Actuals
(Call_Node
));
3713 Insert_Action
(Call_Node
, New_Call
);
3715 pragma Assert
(Etype
(New_Call
) /= Any_Type
3716 or else Serious_Errors_Detected
> 0);
3718 end Check_Subprogram_Variant
;
3720 ---------------------------
3721 -- Inherited_From_Formal --
3722 ---------------------------
3724 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
is
3726 Gen_Par
: Entity_Id
;
3727 Gen_Prim
: Elist_Id
;
3732 -- If the operation is inherited, it is attached to the corresponding
3733 -- type derivation. If the parent in the derivation is a generic
3734 -- actual, it is a subtype of the actual, and we have to recover the
3735 -- original derived type declaration to find the proper parent.
3737 if Nkind
(Parent
(S
)) /= N_Full_Type_Declaration
3738 or else not Is_Derived_Type
(Defining_Identifier
(Parent
(S
)))
3739 or else Nkind
(Type_Definition
(Original_Node
(Parent
(S
)))) /=
3740 N_Derived_Type_Definition
3741 or else not In_Instance
3748 (Type_Definition
(Original_Node
(Parent
(S
))));
3750 if Nkind
(Indic
) = N_Subtype_Indication
then
3751 Par
:= Entity
(Subtype_Mark
(Indic
));
3753 Par
:= Entity
(Indic
);
3757 if not Is_Generic_Actual_Type
(Par
)
3758 or else Is_Tagged_Type
(Par
)
3759 or else Nkind
(Parent
(Par
)) /= N_Subtype_Declaration
3760 or else not In_Open_Scopes
(Scope
(Par
))
3764 Gen_Par
:= Generic_Parent_Type
(Parent
(Par
));
3767 -- If the actual has no generic parent type, the formal is not
3768 -- a formal derived type, so nothing to inherit.
3770 if No
(Gen_Par
) then
3774 -- If the generic parent type is still the generic type, this is a
3775 -- private formal, not a derived formal, and there are no operations
3776 -- inherited from the formal.
3778 if Nkind
(Parent
(Gen_Par
)) = N_Formal_Type_Declaration
then
3782 Gen_Prim
:= Collect_Primitive_Operations
(Gen_Par
);
3784 Elmt
:= First_Elmt
(Gen_Prim
);
3785 while Present
(Elmt
) loop
3786 if Chars
(Node
(Elmt
)) = Chars
(S
) then
3792 F1
:= First_Formal
(S
);
3793 F2
:= First_Formal
(Node
(Elmt
));
3795 and then Present
(F2
)
3797 if Etype
(F1
) = Etype
(F2
)
3798 or else Etype
(F2
) = Gen_Par
3804 exit; -- not the right subprogram
3816 raise Program_Error
;
3817 end Inherited_From_Formal
;
3819 --------------------------
3820 -- In_Unfrozen_Instance --
3821 --------------------------
3823 function In_Unfrozen_Instance
(E
: Entity_Id
) return Boolean is
3828 while Present
(S
) and then S
/= Standard_Standard
loop
3829 if Is_Generic_Instance
(S
)
3830 and then Present
(Freeze_Node
(S
))
3831 and then not Analyzed
(Freeze_Node
(S
))
3840 end In_Unfrozen_Instance
;
3842 ----------------------------------
3843 -- Is_Class_Wide_Interface_Type --
3844 ----------------------------------
3846 function Is_Class_Wide_Interface_Type
(E
: Entity_Id
) return Boolean is
3848 Typ
: Entity_Id
:= E
;
3851 if Has_Non_Limited_View
(Typ
) then
3852 Typ
:= Non_Limited_View
(Typ
);
3855 if Ekind
(Typ
) = E_Anonymous_Access_Type
then
3856 DDT
:= Directly_Designated_Type
(Typ
);
3858 if Has_Non_Limited_View
(DDT
) then
3859 DDT
:= Non_Limited_View
(DDT
);
3862 return Is_Class_Wide_Type
(DDT
) and then Is_Interface
(DDT
);
3864 return Is_Class_Wide_Type
(Typ
) and then Is_Interface
(Typ
);
3866 end Is_Class_Wide_Interface_Type
;
3868 -------------------------
3869 -- Is_Direct_Deep_Call --
3870 -------------------------
3872 function Is_Direct_Deep_Call
(Subp
: Entity_Id
) return Boolean is
3874 if Is_TSS
(Subp
, TSS_Deep_Adjust
)
3875 or else Is_TSS
(Subp
, TSS_Deep_Finalize
)
3876 or else Is_TSS
(Subp
, TSS_Deep_Initialize
)
3883 Actual
:= First
(Parameter_Associations
(Call_Node
));
3884 Formal
:= First_Formal
(Subp
);
3885 while Present
(Actual
)
3886 and then Present
(Formal
)
3888 if Nkind
(Actual
) = N_Identifier
3889 and then Is_Controlling_Actual
(Actual
)
3890 and then Etype
(Actual
) = Etype
(Formal
)
3896 Next_Formal
(Formal
);
3902 end Is_Direct_Deep_Call
;
3908 function New_Value
(From
: Node_Id
) return Node_Id
is
3909 Res
: constant Node_Id
:= Duplicate_Subexpr
(From
);
3911 if Is_Access_Type
(Etype
(From
)) then
3912 return Make_Explicit_Dereference
(Sloc
(From
), Prefix
=> Res
);
3920 Remote
: constant Boolean := Is_Remote_Call
(Call_Node
);
3923 Orig_Subp
: Entity_Id
:= Empty
;
3924 Param_Count
: Positive;
3925 Parent_Formal
: Entity_Id
;
3926 Parent_Subp
: Entity_Id
;
3930 CW_Interface_Formals_Present
: Boolean := False;
3932 -- Start of processing for Expand_Call_Helper
3935 Post_Call
:= New_List
;
3937 -- Expand the function or procedure call if the first actual has a
3938 -- declared dimension aspect, and the subprogram is declared in one
3939 -- of the dimension I/O packages.
3941 if Ada_Version
>= Ada_2012
3942 and then Nkind
(Call_Node
) in N_Subprogram_Call
3943 and then Present
(Parameter_Associations
(Call_Node
))
3945 Expand_Put_Call_With_Symbol
(Call_Node
);
3948 -- Ignore if previous error
3950 if Nkind
(Call_Node
) in N_Has_Etype
3951 and then Etype
(Call_Node
) = Any_Type
3956 -- Call using access to subprogram with explicit dereference
3958 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
3959 Subp
:= Etype
(Name
(Call_Node
));
3960 Parent_Subp
:= Empty
;
3962 -- Case of call to simple entry, where the Name is a selected component
3963 -- whose prefix is the task, and whose selector name is the entry name
3965 elsif Nkind
(Name
(Call_Node
)) = N_Selected_Component
then
3966 Subp
:= Entity
(Selector_Name
(Name
(Call_Node
)));
3967 Parent_Subp
:= Empty
;
3969 -- Case of call to member of entry family, where Name is an indexed
3970 -- component, with the prefix being a selected component giving the
3971 -- task and entry family name, and the index being the entry index.
3973 elsif Nkind
(Name
(Call_Node
)) = N_Indexed_Component
then
3974 Subp
:= Entity
(Selector_Name
(Prefix
(Name
(Call_Node
))));
3975 Parent_Subp
:= Empty
;
3980 Subp
:= Entity
(Name
(Call_Node
));
3981 Parent_Subp
:= Alias
(Subp
);
3983 -- Replace call to Raise_Exception by call to Raise_Exception_Always
3984 -- if we can tell that the first parameter cannot possibly be null.
3985 -- This improves efficiency by avoiding a run-time test.
3987 -- We do not do this if Raise_Exception_Always does not exist, which
3988 -- can happen in configurable run time profiles which provide only a
3991 if Is_RTE
(Subp
, RE_Raise_Exception
)
3992 and then RTE_Available
(RE_Raise_Exception_Always
)
3995 FA
: constant Node_Id
:=
3996 Original_Node
(First_Actual
(Call_Node
));
3999 -- The case we catch is where the first argument is obtained
4000 -- using the Identity attribute (which must always be
4003 if Nkind
(FA
) = N_Attribute_Reference
4004 and then Attribute_Name
(FA
) = Name_Identity
4006 Subp
:= RTE
(RE_Raise_Exception_Always
);
4007 Set_Name
(Call_Node
, New_Occurrence_Of
(Subp
, Loc
));
4012 if Ekind
(Subp
) = E_Entry
then
4013 Parent_Subp
:= Empty
;
4017 -- Ensure that the called subprogram has all its formals
4019 if not Is_Frozen
(Subp
) then
4020 Create_Extra_Formals
(Subp
);
4023 -- Ada 2005 (AI-345): We have a procedure call as a triggering
4024 -- alternative in an asynchronous select or as an entry call in
4025 -- a conditional or timed select. Check whether the procedure call
4026 -- is a renaming of an entry and rewrite it as an entry call.
4028 if Ada_Version
>= Ada_2005
4029 and then Nkind
(Call_Node
) = N_Procedure_Call_Statement
4031 ((Nkind
(Parent
(Call_Node
)) = N_Triggering_Alternative
4032 and then Triggering_Statement
(Parent
(Call_Node
)) = Call_Node
)
4034 (Nkind
(Parent
(Call_Node
)) = N_Entry_Call_Alternative
4035 and then Entry_Call_Statement
(Parent
(Call_Node
)) = Call_Node
))
4039 Ren_Root
: Entity_Id
:= Subp
;
4042 -- This may be a chain of renamings, find the root
4044 if Present
(Alias
(Ren_Root
)) then
4045 Ren_Root
:= Alias
(Ren_Root
);
4048 if Present
(Parent
(Ren_Root
))
4049 and then Present
(Original_Node
(Parent
(Parent
(Ren_Root
))))
4051 Ren_Decl
:= Original_Node
(Parent
(Parent
(Ren_Root
)));
4053 if Nkind
(Ren_Decl
) = N_Subprogram_Renaming_Declaration
then
4055 Make_Entry_Call_Statement
(Loc
,
4057 New_Copy_Tree
(Name
(Ren_Decl
)),
4058 Parameter_Associations
=>
4060 (Parameter_Associations
(Call_Node
))));
4068 -- If this is a call to a predicate function, try to constant fold it
4070 if Nkind
(Call_Node
) = N_Function_Call
4071 and then Is_Entity_Name
(Name
(Call_Node
))
4072 and then Is_Predicate_Function
(Subp
)
4073 and then Can_Fold_Predicate_Call
(Subp
)
4078 if Transform_Function_Array
4079 and then Nkind
(Call_Node
) = N_Function_Call
4080 and then Is_Entity_Name
(Name
(Call_Node
))
4083 Func_Id
: constant Entity_Id
:=
4084 Ultimate_Alias
(Entity
(Name
(Call_Node
)));
4086 -- When generating C code, transform a function call that returns
4087 -- a constrained array type into procedure form.
4089 if Rewritten_For_C
(Func_Id
) then
4091 -- For internally generated calls ensure that they reference
4092 -- the entity of the spec of the called function (needed since
4093 -- the expander may generate calls using the entity of their
4096 if not Comes_From_Source
(Call_Node
)
4097 and then Nkind
(Unit_Declaration_Node
(Func_Id
)) =
4100 Set_Entity
(Name
(Call_Node
),
4101 Corresponding_Function
4102 (Corresponding_Procedure
(Func_Id
)));
4105 Rewrite_Function_Call_For_C
(Call_Node
);
4108 -- Also introduce a temporary for functions that return a record
4109 -- called within another procedure or function call, since records
4110 -- are passed by pointer in the generated C code, and we cannot
4111 -- take a pointer from a subprogram call.
4113 elsif Modify_Tree_For_C
4114 and then Nkind
(Parent
(Call_Node
)) in N_Subprogram_Call
4115 and then Is_Record_Type
(Etype
(Func_Id
))
4118 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
4123 -- Temp : ... := Func_Call (...);
4126 Make_Object_Declaration
(Loc
,
4127 Defining_Identifier
=> Temp_Id
,
4128 Object_Definition
=>
4129 New_Occurrence_Of
(Etype
(Func_Id
), Loc
),
4131 Make_Function_Call
(Loc
,
4133 New_Occurrence_Of
(Func_Id
, Loc
),
4134 Parameter_Associations
=>
4135 Parameter_Associations
(Call_Node
)));
4137 Insert_Action
(Parent
(Call_Node
), Decl
);
4138 Rewrite
(Call_Node
, New_Occurrence_Of
(Temp_Id
, Loc
));
4145 -- First step, compute extra actuals, corresponding to any Extra_Formals
4146 -- present. Note that we do not access Extra_Formals directly, instead
4147 -- we simply note the presence of the extra formals as we process the
4148 -- regular formals collecting corresponding actuals in Extra_Actuals.
4150 -- We also generate any required range checks for actuals for in formals
4151 -- as we go through the loop, since this is a convenient place to do it.
4152 -- (Though it seems that this would be better done in Expand_Actuals???)
4154 -- Special case: Thunks must not compute the extra actuals; they must
4155 -- just propagate to the target primitive their extra actuals.
4157 if Is_Thunk
(Current_Scope
)
4158 and then Thunk_Entity
(Current_Scope
) = Subp
4159 and then Present
(Extra_Formals
(Subp
))
4161 pragma Assert
(Extra_Formals_Match_OK
(Current_Scope
, Subp
));
4164 Target_Formal
: Entity_Id
;
4165 Thunk_Formal
: Entity_Id
;
4168 Target_Formal
:= Extra_Formals
(Subp
);
4169 Thunk_Formal
:= Extra_Formals
(Current_Scope
);
4170 while Present
(Target_Formal
) loop
4172 (Expr
=> New_Occurrence_Of
(Thunk_Formal
, Loc
),
4173 EF
=> Thunk_Formal
);
4175 Target_Formal
:= Extra_Formal
(Target_Formal
);
4176 Thunk_Formal
:= Extra_Formal
(Thunk_Formal
);
4179 while Is_Non_Empty_List
(Extra_Actuals
) loop
4180 Add_Actual_Parameter
(Remove_Head
(Extra_Actuals
));
4183 -- Mark the call as processed build-in-place call; required
4184 -- to avoid adding the extra formals twice.
4186 if Nkind
(Call_Node
) = N_Function_Call
then
4187 Set_Is_Expanded_Build_In_Place_Call
(Call_Node
);
4190 Expand_Actuals
(Call_Node
, Subp
, Post_Call
);
4191 pragma Assert
(Is_Empty_List
(Post_Call
));
4192 pragma Assert
(Check_Number_Of_Actuals
(Call_Node
, Subp
));
4193 pragma Assert
(Check_BIP_Actuals
(Call_Node
, Subp
));
4198 Formal
:= First_Formal
(Subp
);
4199 Actual
:= First_Actual
(Call_Node
);
4201 while Present
(Formal
) loop
4202 -- Prepare to examine current entry
4206 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
4207 -- to expand it in a further round.
4209 CW_Interface_Formals_Present
:=
4210 CW_Interface_Formals_Present
4211 or else Is_Class_Wide_Interface_Type
(Etype
(Formal
));
4213 -- Create possible extra actual for constrained case. Usually, the
4214 -- extra actual is of the form actual'constrained, but since this
4215 -- attribute is only available for unconstrained records, TRUE is
4216 -- expanded if the type of the formal happens to be constrained (for
4217 -- instance when this procedure is inherited from an unconstrained
4218 -- record to a constrained one) or if the actual has no discriminant
4219 -- (its type is constrained). An exception to this is the case of a
4220 -- private type without discriminants. In this case we pass FALSE
4221 -- because the object has underlying discriminants with defaults.
4223 if Present
(Extra_Constrained
(Formal
)) then
4224 if Is_Private_Type
(Etype
(Prev
))
4225 and then not Has_Discriminants
(Base_Type
(Etype
(Prev
)))
4228 (Expr
=> New_Occurrence_Of
(Standard_False
, Loc
),
4229 EF
=> Extra_Constrained
(Formal
));
4231 elsif Is_Constrained
(Etype
(Formal
))
4232 or else not Has_Discriminants
(Etype
(Prev
))
4235 (Expr
=> New_Occurrence_Of
(Standard_True
, Loc
),
4236 EF
=> Extra_Constrained
(Formal
));
4238 -- Do not produce extra actuals for Unchecked_Union parameters.
4239 -- Jump directly to the end of the loop.
4241 elsif Is_Unchecked_Union
(Base_Type
(Etype
(Actual
))) then
4242 goto Skip_Extra_Actual_Generation
;
4245 -- If the actual is a type conversion, then the constrained
4246 -- test applies to the actual, not the target type.
4252 -- Test for unchecked conversions as well, which can occur
4253 -- as out parameter actuals on calls to stream procedures.
4256 while Nkind
(Act_Prev
) in N_Type_Conversion
4257 | N_Unchecked_Type_Conversion
4259 Act_Prev
:= Expression
(Act_Prev
);
4262 -- If the expression is a conversion of a dereference, this
4263 -- is internally generated code that manipulates addresses,
4264 -- e.g. when building interface tables. No check should
4265 -- occur in this case, and the discriminated object is not
4266 -- directly at hand.
4268 if not Comes_From_Source
(Actual
)
4269 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
4270 and then Nkind
(Act_Prev
) = N_Explicit_Dereference
4273 (Expr
=> New_Occurrence_Of
(Standard_False
, Loc
),
4274 EF
=> Extra_Constrained
(Formal
));
4279 Make_Attribute_Reference
(Sloc
(Prev
),
4281 Duplicate_Subexpr_No_Checks
4282 (Act_Prev
, Name_Req
=> True),
4283 Attribute_Name
=> Name_Constrained
),
4284 EF
=> Extra_Constrained
(Formal
));
4290 -- Create possible extra actual for accessibility level
4292 if Present
(Extra_Accessibility
(Formal
)) then
4293 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
4294 -- accessibility levels.
4296 if Is_Thunk
(Current_Scope
) then
4298 Parm_Ent
: Entity_Id
;
4301 if Is_Controlling_Actual
(Actual
) then
4303 -- Find the corresponding actual of the thunk
4305 Parm_Ent
:= First_Entity
(Current_Scope
);
4306 for J
in 2 .. Param_Count
loop
4307 Next_Entity
(Parm_Ent
);
4310 -- Handle unchecked conversion of access types generated
4311 -- in thunks (cf. Expand_Interface_Thunk).
4313 elsif Is_Access_Type
(Etype
(Actual
))
4314 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
4316 Parm_Ent
:= Entity
(Expression
(Actual
));
4318 else pragma Assert
(Is_Entity_Name
(Actual
));
4319 Parm_Ent
:= Entity
(Actual
);
4323 (Expr
=> Accessibility_Level
4325 Level
=> Dynamic_Level
,
4326 Allow_Alt_Model
=> False),
4327 EF
=> Extra_Accessibility
(Formal
));
4330 -- Conditional expressions
4332 elsif Nkind
(Prev
) = N_Expression_With_Actions
4333 and then Nkind
(Original_Node
(Prev
)) in
4334 N_If_Expression | N_Case_Expression
4336 Add_Cond_Expression_Extra_Actual
(Formal
);
4338 -- Internal constant generated to remove side effects (normally
4339 -- from the expansion of dispatching calls).
4341 -- First verify the actual is internal
4343 elsif not Comes_From_Source
(Prev
)
4344 and then not Is_Rewrite_Substitution
(Prev
)
4346 -- Next check that the actual is a constant
4348 and then Nkind
(Prev
) = N_Identifier
4349 and then Ekind
(Entity
(Prev
)) = E_Constant
4350 and then Nkind
(Parent
(Entity
(Prev
))) = N_Object_Declaration
4352 -- Generate the accessibility level based on the expression in
4353 -- the constant's declaration.
4356 (Expr
=> Accessibility_Level
4358 (Parent
(Entity
(Prev
))),
4359 Level
=> Dynamic_Level
,
4360 Allow_Alt_Model
=> False),
4361 EF
=> Extra_Accessibility
(Formal
));
4367 (Expr
=> Accessibility_Level
4369 Level
=> Dynamic_Level
,
4370 Allow_Alt_Model
=> False),
4371 EF
=> Extra_Accessibility
(Formal
));
4375 -- Perform the check of 4.6(49) that prevents a null value from being
4376 -- passed as an actual to an access parameter. Note that the check
4377 -- is elided in the common cases of passing an access attribute or
4378 -- access parameter as an actual. Also, we currently don't enforce
4379 -- this check for expander-generated actuals and when -gnatdj is set.
4381 if Ada_Version
>= Ada_2005
then
4383 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
4384 -- the intent of 6.4.1(13) is that null-exclusion checks should
4385 -- not be done for 'out' parameters, even though it refers only
4386 -- to constraint checks, and a null_exclusion is not a constraint.
4387 -- Note that AI05-0196-1 corrects this mistake in the RM.
4389 if Is_Access_Type
(Etype
(Formal
))
4390 and then Can_Never_Be_Null
(Etype
(Formal
))
4391 and then Ekind
(Formal
) /= E_Out_Parameter
4392 and then Nkind
(Prev
) /= N_Raise_Constraint_Error
4393 and then (Known_Null
(Prev
)
4394 or else not Can_Never_Be_Null
(Etype
(Prev
)))
4396 Install_Null_Excluding_Check
(Prev
);
4399 -- Ada_Version < Ada_2005
4402 if Ekind
(Etype
(Formal
)) /= E_Anonymous_Access_Type
4403 or else Access_Checks_Suppressed
(Subp
)
4407 elsif Debug_Flag_J
then
4410 elsif not Comes_From_Source
(Prev
) then
4413 elsif Is_Entity_Name
(Prev
)
4414 and then Ekind
(Etype
(Prev
)) = E_Anonymous_Access_Type
4418 elsif Nkind
(Prev
) in N_Allocator | N_Attribute_Reference
then
4422 Install_Null_Excluding_Check
(Prev
);
4426 -- Perform appropriate validity checks on parameters that
4429 if Validity_Checks_On
then
4430 if (Ekind
(Formal
) = E_In_Parameter
4431 and then Validity_Check_In_Params
)
4433 (Ekind
(Formal
) = E_In_Out_Parameter
4434 and then Validity_Check_In_Out_Params
)
4436 -- If the actual is an indexed component of a packed type (or
4437 -- is an indexed or selected component whose prefix recursively
4438 -- meets this condition), it has not been expanded yet. It will
4439 -- be copied in the validity code that follows, and has to be
4440 -- expanded appropriately, so reanalyze it.
4442 -- What we do is just to unset analyzed bits on prefixes till
4443 -- we reach something that does not have a prefix.
4450 while Nkind
(Nod
) in
4451 N_Indexed_Component | N_Selected_Component
4453 Set_Analyzed
(Nod
, False);
4454 Nod
:= Prefix
(Nod
);
4458 Ensure_Valid
(Actual
);
4462 -- For IN OUT and OUT parameters, ensure that subscripts are valid
4463 -- since this is a left side reference. We only do this for calls
4464 -- from the source program since we assume that compiler generated
4465 -- calls explicitly generate any required checks. We also need it
4466 -- only if we are doing standard validity checks, since clearly it is
4467 -- not needed if validity checks are off, and in subscript validity
4468 -- checking mode, all indexed components are checked with a call
4469 -- directly from Expand_N_Indexed_Component.
4471 if Comes_From_Source
(Call_Node
)
4472 and then Ekind
(Formal
) /= E_In_Parameter
4473 and then Validity_Checks_On
4474 and then Validity_Check_Default
4475 and then not Validity_Check_Subscripts
4477 Check_Valid_Lvalue_Subscripts
(Actual
);
4480 -- Mark any scalar OUT parameter that is a simple variable as no
4481 -- longer known to be valid (unless the type is always valid). This
4482 -- reflects the fact that if an OUT parameter is never set in a
4483 -- procedure, then it can become invalid on the procedure return.
4485 if Ekind
(Formal
) = E_Out_Parameter
4486 and then Is_Entity_Name
(Actual
)
4487 and then Ekind
(Entity
(Actual
)) = E_Variable
4488 and then not Is_Known_Valid
(Etype
(Actual
))
4490 Set_Is_Known_Valid
(Entity
(Actual
), False);
4493 -- For an OUT or IN OUT parameter, if the actual is an entity, then
4494 -- clear current values, since they can be clobbered. We are probably
4495 -- doing this in more places than we need to, but better safe than
4496 -- sorry when it comes to retaining bad current values.
4498 if Ekind
(Formal
) /= E_In_Parameter
4499 and then Is_Entity_Name
(Actual
)
4500 and then Present
(Entity
(Actual
))
4503 Ent
: constant Entity_Id
:= Entity
(Actual
);
4507 -- For an OUT or IN OUT parameter that is an assignable entity,
4508 -- we do not want to clobber the Last_Assignment field, since
4509 -- if it is set, it was precisely because it is indeed an OUT
4510 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
4511 -- since the subprogram could have returned in invalid value.
4513 if Is_Assignable
(Ent
) then
4514 Sav
:= Last_Assignment
(Ent
);
4515 Kill_Current_Values
(Ent
);
4516 Set_Last_Assignment
(Ent
, Sav
);
4517 Set_Is_Known_Valid
(Ent
, False);
4518 Set_Is_True_Constant
(Ent
, False);
4520 -- For all other cases, just kill the current values
4523 Kill_Current_Values
(Ent
);
4528 -- If the formal is class-wide and the actual is an aggregate, force
4529 -- evaluation so that the back end who does not know about class-wide
4530 -- type, does not generate a temporary of the wrong size.
4532 if not Is_Class_Wide_Type
(Etype
(Formal
)) then
4535 elsif Nkind
(Actual
) = N_Aggregate
4536 or else (Nkind
(Actual
) = N_Qualified_Expression
4537 and then Nkind
(Expression
(Actual
)) = N_Aggregate
)
4539 Force_Evaluation
(Actual
);
4542 -- In a remote call, if the formal is of a class-wide type, check
4543 -- that the actual meets the requirements described in E.4(18).
4545 if Remote
and then Is_Class_Wide_Type
(Etype
(Formal
)) then
4546 Insert_Action
(Actual
,
4547 Make_Transportable_Check
(Loc
,
4548 Duplicate_Subexpr_Move_Checks
(Actual
)));
4551 -- Perform invariant checks for all intermediate types in a view
4552 -- conversion after successful return from a call that passes the
4553 -- view conversion as an IN OUT or OUT parameter (RM 7.3.2 (12/3,
4554 -- 13/3, 14/3)). Consider only source conversion in order to avoid
4555 -- generating spurious checks on complex expansion such as object
4556 -- initialization through an extension aggregate.
4558 if Comes_From_Source
(Call_Node
)
4559 and then Ekind
(Formal
) /= E_In_Parameter
4560 and then Nkind
(Actual
) = N_Type_Conversion
4562 Add_View_Conversion_Invariants
(Formal
, Actual
);
4565 -- Generating C the initialization of an allocator is performed by
4566 -- means of individual statements, and hence it must be done before
4569 if Modify_Tree_For_C
4570 and then Nkind
(Actual
) = N_Allocator
4571 and then Nkind
(Expression
(Actual
)) = N_Qualified_Expression
4573 Remove_Side_Effects
(Actual
);
4576 -- This label is required when skipping extra actual generation for
4577 -- Unchecked_Union parameters.
4579 <<Skip_Extra_Actual_Generation
>>
4581 Param_Count
:= Param_Count
+ 1;
4582 Next_Actual
(Actual
);
4583 Next_Formal
(Formal
);
4586 -- If we are calling an Ada 2012 function which needs to have the
4587 -- "accessibility level determined by the point of call" (AI05-0234)
4588 -- passed in to it, then pass it in.
4590 if Ekind
(Subp
) in E_Function | E_Operator | E_Subprogram_Type
4592 Present
(Extra_Accessibility_Of_Result
(Ultimate_Alias
(Subp
)))
4595 Extra_Form
: Node_Id
:= Empty
;
4596 Level
: Node_Id
:= Empty
;
4599 -- Detect cases where the function call has been internally
4600 -- generated by examining the original node and return library
4601 -- level - taking care to avoid ignoring function calls expanded
4602 -- in prefix notation.
4604 if Nkind
(Original_Node
(Call_Node
)) not in N_Function_Call
4605 | N_Selected_Component
4606 | N_Indexed_Component
4608 Level
:= Make_Integer_Literal
4609 (Loc
, Scope_Depth
(Standard_Standard
));
4611 -- Otherwise get the level normally based on the call node
4614 Level
:= Accessibility_Level
4616 Level
=> Dynamic_Level
,
4617 Allow_Alt_Model
=> False);
4620 -- It may be possible that we are re-expanding an already
4621 -- expanded call when are are dealing with dispatching ???
4623 if not Present
(Parameter_Associations
(Call_Node
))
4624 or else Nkind
(Last
(Parameter_Associations
(Call_Node
)))
4625 /= N_Parameter_Association
4626 or else not Is_Accessibility_Actual
4627 (Last
(Parameter_Associations
(Call_Node
)))
4629 Extra_Form
:= Extra_Accessibility_Of_Result
4630 (Ultimate_Alias
(Subp
));
4639 -- If we are expanding the RHS of an assignment we need to check if tag
4640 -- propagation is needed. You might expect this processing to be in
4641 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
4642 -- assignment might be transformed to a declaration for an unconstrained
4643 -- value if the expression is classwide.
4645 if Nkind
(Call_Node
) = N_Function_Call
4646 and then Is_Tag_Indeterminate
(Call_Node
)
4647 and then Is_Entity_Name
(Name
(Call_Node
))
4650 Ass
: Node_Id
:= Empty
;
4653 if Nkind
(Parent
(Call_Node
)) = N_Assignment_Statement
then
4654 Ass
:= Parent
(Call_Node
);
4656 elsif Nkind
(Parent
(Call_Node
)) = N_Qualified_Expression
4657 and then Nkind
(Parent
(Parent
(Call_Node
))) =
4658 N_Assignment_Statement
4660 Ass
:= Parent
(Parent
(Call_Node
));
4662 elsif Nkind
(Parent
(Call_Node
)) = N_Explicit_Dereference
4663 and then Nkind
(Parent
(Parent
(Call_Node
))) =
4664 N_Assignment_Statement
4666 Ass
:= Parent
(Parent
(Call_Node
));
4670 and then Is_Class_Wide_Type
(Etype
(Name
(Ass
)))
4672 -- Move the error messages below to sem???
4674 if Is_Access_Type
(Etype
(Call_Node
)) then
4675 if Designated_Type
(Etype
(Call_Node
)) /=
4676 Root_Type
(Etype
(Name
(Ass
)))
4679 ("tag-indeterminate expression must have designated "
4680 & "type& (RM 5.2 (6))",
4681 Call_Node
, Root_Type
(Etype
(Name
(Ass
))));
4683 Propagate_Tag
(Name
(Ass
), Call_Node
);
4686 elsif Etype
(Call_Node
) /= Root_Type
(Etype
(Name
(Ass
))) then
4688 ("tag-indeterminate expression must have type & "
4690 Call_Node
, Root_Type
(Etype
(Name
(Ass
))));
4693 Propagate_Tag
(Name
(Ass
), Call_Node
);
4696 -- The call will be rewritten as a dispatching call, and
4697 -- expanded as such.
4704 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
4705 -- it to point to the correct secondary virtual table.
4707 if Nkind
(Call_Node
) in N_Subprogram_Call
4708 and then CW_Interface_Formals_Present
4710 Expand_Interface_Actuals
(Call_Node
);
4713 -- Install class-wide preconditions runtime check when this is a
4714 -- dispatching primitive that has or inherits class-wide preconditions;
4715 -- otherwise no runtime check is installed.
4717 if Nkind
(Call_Node
) in N_Subprogram_Call
4718 and then Is_Dispatching_Operation
(Subp
)
4720 Install_Class_Preconditions_Check
(Call_Node
);
4723 -- Deals with Dispatch_Call if we still have a call, before expanding
4724 -- extra actuals since this will be done on the re-analysis of the
4725 -- dispatching call. Note that we do not try to shorten the actual list
4726 -- for a dispatching call, it would not make sense to do so. Expansion
4727 -- of dispatching calls is suppressed for VM targets, because the VM
4728 -- back-ends directly handle the generation of dispatching calls and
4729 -- would have to undo any expansion to an indirect call.
4731 if Nkind
(Call_Node
) in N_Subprogram_Call
4732 and then Present
(Controlling_Argument
(Call_Node
))
4734 if Tagged_Type_Expansion
then
4735 Expand_Dispatching_Call
(Call_Node
);
4737 -- Expand_Dispatching_Call takes care of all the needed processing
4745 Call_Typ
: constant Entity_Id
:= Etype
(Call_Node
);
4746 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
4747 Eq_Prim_Op
: Entity_Id
:= Empty
;
4750 Prev_Call
: Node_Id
;
4753 Apply_Tag_Checks
(Call_Node
);
4755 if not Is_Limited_Type
(Typ
) then
4756 Eq_Prim_Op
:= Find_Prim_Op
(Typ
, Name_Op_Eq
);
4759 -- If this is a dispatching "=", we must first compare the
4760 -- tags so we generate: x.tag = y.tag and then x = y
4762 if Subp
= Eq_Prim_Op
then
4764 -- Mark the node as analyzed to avoid reanalyzing this
4765 -- dispatching call (which would cause a never-ending loop)
4767 Prev_Call
:= Relocate_Node
(Call_Node
);
4768 Set_Analyzed
(Prev_Call
);
4770 Param
:= First_Actual
(Call_Node
);
4776 Make_Selected_Component
(Loc
,
4777 Prefix
=> New_Value
(Param
),
4780 (First_Tag_Component
(Typ
), Loc
)),
4783 Make_Selected_Component
(Loc
,
4785 Unchecked_Convert_To
(Typ
,
4786 New_Value
(Next_Actual
(Param
))),
4789 (First_Tag_Component
(Typ
), Loc
))),
4790 Right_Opnd
=> Prev_Call
);
4792 Rewrite
(Call_Node
, New_Call
);
4794 (Call_Node
, Call_Typ
, Suppress
=> All_Checks
);
4797 -- Expansion of a dispatching call results in an indirect call,
4798 -- which in turn causes current values to be killed (see
4799 -- Resolve_Call), so on VM targets we do the call here to
4800 -- ensure consistent warnings between VM and non-VM targets.
4802 Kill_Current_Values
;
4804 -- If this is a dispatching "=" then we must update the reference
4805 -- to the call node because we generated:
4806 -- x.tag = y.tag and then x = y
4808 if Subp
= Eq_Prim_Op
then
4809 Call_Node
:= Right_Opnd
(Call_Node
);
4814 -- Similarly, expand calls to RCI subprograms on which pragma
4815 -- All_Calls_Remote applies. The rewriting will be reanalyzed
4816 -- later. Do this only when the call comes from source since we
4817 -- do not want such a rewriting to occur in expanded code.
4819 if Is_All_Remote_Call
(Call_Node
) then
4820 Expand_All_Calls_Remote_Subprogram_Call
(Call_Node
);
4822 -- Similarly, do not add extra actuals for an entry call whose entity
4823 -- is a protected procedure, or for an internal protected subprogram
4824 -- call, because it will be rewritten as a protected subprogram call
4825 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
4827 elsif Is_Protected_Type
(Scope
(Subp
))
4828 and then Ekind
(Subp
) in E_Procedure | E_Function
4832 -- During that loop we gathered the extra actuals (the ones that
4833 -- correspond to Extra_Formals), so now they can be appended.
4835 elsif Is_Non_Empty_List
(Extra_Actuals
) then
4837 Num_Extra_Actuals
: constant Nat
:= List_Length
(Extra_Actuals
);
4840 while Is_Non_Empty_List
(Extra_Actuals
) loop
4841 Add_Actual_Parameter
(Remove_Head
(Extra_Actuals
));
4844 -- Add dummy extra BIP actuals if we are calling a function that
4845 -- inherited the BIP extra actuals but does not require them.
4847 if Nkind
(Call_Node
) = N_Function_Call
4848 and then Is_Build_In_Place_Function_Call
(Call_Node
)
4849 and then not Is_True_Build_In_Place_Function_Call
(Call_Node
)
4851 Add_Dummy_Build_In_Place_Actuals
(Subp
,
4852 Num_Added_Extra_Actuals
=> Num_Extra_Actuals
);
4856 -- Add dummy extra BIP actuals if we are calling a function that
4857 -- inherited the BIP extra actuals but does not require them.
4859 elsif Nkind
(Call_Node
) = N_Function_Call
4860 and then Is_Build_In_Place_Function_Call
(Call_Node
)
4861 and then not Is_True_Build_In_Place_Function_Call
(Call_Node
)
4863 Add_Dummy_Build_In_Place_Actuals
(Subp
);
4866 -- At this point we have all the actuals, so this is the point at which
4867 -- the various expansion activities for actuals is carried out.
4869 Expand_Actuals
(Call_Node
, Subp
, Post_Call
);
4871 -- If it is a recursive call then call the internal procedure that
4872 -- verifies Subprogram_Variant contract (if present and enabled).
4873 -- Detecting calls to subprogram aliases is necessary for recursive
4874 -- calls in instances of generic subprograms, where the renaming of
4875 -- the current subprogram is called.
4877 if Is_Subprogram
(Subp
)
4878 and then not Is_Ignored_Ghost_Entity
(Subp
)
4879 and then Same_Or_Aliased_Subprograms
(Subp
, Current_Scope
)
4881 Check_Subprogram_Variant
;
4884 -- Verify that the actuals do not share storage. This check must be done
4885 -- on the caller side rather that inside the subprogram to avoid issues
4886 -- of parameter passing.
4888 if Check_Aliasing_Of_Parameters
then
4889 Apply_Parameter_Aliasing_Checks
(Call_Node
, Subp
);
4892 -- If the subprogram is a renaming, or if it is inherited, replace it in
4893 -- the call with the name of the actual subprogram being called. If this
4894 -- is a dispatching call, the run-time decides what to call. The Alias
4895 -- attribute does not apply to entries.
4897 if Nkind
(Call_Node
) /= N_Entry_Call_Statement
4898 and then No
(Controlling_Argument
(Call_Node
))
4899 and then Present
(Parent_Subp
)
4900 and then not Is_Direct_Deep_Call
(Subp
)
4902 if Present
(Inherited_From_Formal
(Subp
)) then
4903 Parent_Subp
:= Inherited_From_Formal
(Subp
);
4905 Parent_Subp
:= Ultimate_Alias
(Parent_Subp
);
4908 -- The below setting of Entity is suspect, see F109-018 discussion???
4910 Set_Entity
(Name
(Call_Node
), Parent_Subp
);
4912 -- Inspect all formals of derived subprogram Subp. Compare parameter
4913 -- types with the parent subprogram and check whether an actual may
4914 -- need a type conversion to the corresponding formal of the parent
4917 -- Not clear whether intrinsic subprograms need such conversions. ???
4919 if not Is_Intrinsic_Subprogram
(Parent_Subp
)
4920 or else Is_Generic_Instance
(Parent_Subp
)
4923 procedure Convert
(Act
: Node_Id
; Typ
: Entity_Id
);
4924 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
4925 -- and resolve the newly generated construct.
4931 procedure Convert
(Act
: Node_Id
; Typ
: Entity_Id
) is
4933 Rewrite
(Act
, OK_Convert_To
(Typ
, Act
));
4934 Analyze_And_Resolve
(Act
, Typ
);
4939 Actual_Typ
: Entity_Id
;
4940 Formal_Typ
: Entity_Id
;
4941 Parent_Typ
: Entity_Id
;
4944 Actual
:= First_Actual
(Call_Node
);
4945 Formal
:= First_Formal
(Subp
);
4946 Parent_Formal
:= First_Formal
(Parent_Subp
);
4947 while Present
(Formal
) loop
4948 Actual_Typ
:= Etype
(Actual
);
4949 Formal_Typ
:= Etype
(Formal
);
4950 Parent_Typ
:= Etype
(Parent_Formal
);
4952 -- For an IN parameter of a scalar type, the derived formal
4953 -- type and parent formal type differ, and the parent formal
4954 -- type and actual type do not match statically.
4956 if Is_Scalar_Type
(Formal_Typ
)
4957 and then Ekind
(Formal
) = E_In_Parameter
4958 and then Formal_Typ
/= Parent_Typ
4960 not Subtypes_Statically_Match
(Parent_Typ
, Actual_Typ
)
4961 and then not Raises_Constraint_Error
(Actual
)
4963 Convert
(Actual
, Parent_Typ
);
4965 -- For access types, the parent formal type and actual type
4968 elsif Is_Access_Type
(Formal_Typ
)
4969 and then Base_Type
(Parent_Typ
) /= Base_Type
(Actual_Typ
)
4971 if Ekind
(Formal
) /= E_In_Parameter
then
4972 Convert
(Actual
, Parent_Typ
);
4974 elsif Ekind
(Parent_Typ
) = E_Anonymous_Access_Type
4975 and then Designated_Type
(Parent_Typ
) /=
4976 Designated_Type
(Actual_Typ
)
4977 and then not Is_Controlling_Formal
(Formal
)
4979 -- This unchecked conversion is not necessary unless
4980 -- inlining is enabled, because in that case the type
4981 -- mismatch may become visible in the body about to be
4985 Unchecked_Convert_To
(Parent_Typ
, Actual
));
4986 Analyze_And_Resolve
(Actual
, Parent_Typ
);
4989 -- If there is a change of representation, then generate a
4990 -- warning, and do the change of representation.
4992 elsif not Has_Compatible_Representation
4993 (Target_Typ
=> Formal_Typ
,
4994 Operand_Typ
=> Parent_Typ
)
4997 ("??change of representation required", Actual
);
4998 Convert
(Actual
, Parent_Typ
);
5000 -- For array and record types, the parent formal type and
5001 -- derived formal type have different sizes or pragma Pack
5004 elsif ((Is_Array_Type
(Formal_Typ
)
5005 and then Is_Array_Type
(Parent_Typ
))
5007 (Is_Record_Type
(Formal_Typ
)
5008 and then Is_Record_Type
(Parent_Typ
)))
5009 and then Known_Esize
(Formal_Typ
)
5010 and then Known_Esize
(Parent_Typ
)
5012 (Esize
(Formal_Typ
) /= Esize
(Parent_Typ
)
5013 or else Has_Pragma_Pack
(Formal_Typ
) /=
5014 Has_Pragma_Pack
(Parent_Typ
))
5016 Convert
(Actual
, Parent_Typ
);
5019 Next_Actual
(Actual
);
5020 Next_Formal
(Formal
);
5021 Next_Formal
(Parent_Formal
);
5027 Subp
:= Parent_Subp
;
5030 -- Deal with case where call is an explicit dereference
5032 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
5034 -- Handle case of access to protected subprogram type
5036 if Is_Access_Protected_Subprogram_Type
5037 (Base_Type
(Etype
(Prefix
(Name
(Call_Node
)))))
5039 -- If this is a call through an access to protected operation, the
5040 -- prefix has the form (object'address, operation'access). Rewrite
5041 -- as a for other protected calls: the object is the 1st parameter
5042 -- of the list of actuals.
5049 Ptr
: constant Node_Id
:= Prefix
(Name
(Call_Node
));
5051 T
: constant Entity_Id
:=
5052 Equivalent_Type
(Base_Type
(Etype
(Ptr
)));
5054 D_T
: constant Entity_Id
:=
5055 Designated_Type
(Base_Type
(Etype
(Ptr
)));
5059 Make_Selected_Component
(Loc
,
5060 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
5062 New_Occurrence_Of
(First_Entity
(T
), Loc
));
5065 Make_Selected_Component
(Loc
,
5066 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
5068 New_Occurrence_Of
(Next_Entity
(First_Entity
(T
)), Loc
));
5071 Make_Explicit_Dereference
(Loc
,
5074 if Present
(Parameter_Associations
(Call_Node
)) then
5075 Parm
:= Parameter_Associations
(Call_Node
);
5080 Prepend
(Obj
, Parm
);
5082 if Etype
(D_T
) = Standard_Void_Type
then
5084 Make_Procedure_Call_Statement
(Loc
,
5086 Parameter_Associations
=> Parm
);
5089 Make_Function_Call
(Loc
,
5091 Parameter_Associations
=> Parm
);
5094 Set_First_Named_Actual
(Call
, First_Named_Actual
(Call_Node
));
5095 Set_Etype
(Call
, Etype
(D_T
));
5097 -- We do not re-analyze the call to avoid infinite recursion.
5098 -- We analyze separately the prefix and the object, and set
5099 -- the checks on the prefix that would otherwise be emitted
5100 -- when resolving a call.
5102 Rewrite
(Call_Node
, Call
);
5104 Apply_Access_Check
(Nam
);
5111 -- If this is a call to an intrinsic subprogram, then perform the
5112 -- appropriate expansion to the corresponding tree node and we
5113 -- are all done (since after that the call is gone).
5115 -- In the case where the intrinsic is to be processed by the back end,
5116 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
5117 -- since the idea in this case is to pass the call unchanged. If the
5118 -- intrinsic is an inherited unchecked conversion, and the derived type
5119 -- is the target type of the conversion, we must retain it as the return
5120 -- type of the expression. Otherwise the expansion below, which uses the
5121 -- parent operation, will yield the wrong type.
5123 if Is_Intrinsic_Subprogram
(Subp
) then
5124 Expand_Intrinsic_Call
(Call_Node
, Subp
);
5126 if Nkind
(Call_Node
) = N_Unchecked_Type_Conversion
5127 and then Parent_Subp
/= Orig_Subp
5128 and then Etype
(Parent_Subp
) /= Etype
(Orig_Subp
)
5130 Set_Etype
(Call_Node
, Etype
(Orig_Subp
));
5136 if Ekind
(Subp
) in E_Function | E_Procedure
then
5138 -- We perform a simple optimization on calls for To_Address by
5139 -- replacing them with an unchecked conversion. Not only is this
5140 -- efficient, but it also avoids order of elaboration problems when
5141 -- address clauses are inlined (address expression elaborated at the
5144 -- We perform this optimization regardless of whether we are in the
5145 -- main unit or in a unit in the context of the main unit, to ensure
5146 -- that the generated tree is the same in both cases, for CodePeer
5149 if Is_RTE
(Subp
, RE_To_Address
) then
5151 Unchecked_Convert_To
5152 (RTE
(RE_Address
), Relocate_Node
(First_Actual
(Call_Node
))));
5155 -- A call to a null procedure is replaced by a null statement, but we
5156 -- are not allowed to ignore possible side effects of the call, so we
5157 -- make sure that actuals are evaluated.
5158 -- We also suppress this optimization for GNATcoverage.
5160 elsif Is_Null_Procedure
(Subp
)
5161 and then not Opt
.Suppress_Control_Flow_Optimizations
5163 Actual
:= First_Actual
(Call_Node
);
5164 while Present
(Actual
) loop
5165 Remove_Side_Effects
(Actual
);
5166 Next_Actual
(Actual
);
5169 Rewrite
(Call_Node
, Make_Null_Statement
(Loc
));
5173 -- Handle inlining. No action needed if the subprogram is not inlined
5175 if not Is_Inlined
(Subp
) then
5178 -- Front-end inlining of expression functions (performed also when
5179 -- back-end inlining is enabled).
5181 elsif Is_Inlinable_Expression_Function
(Subp
) then
5183 (Call_Node
, New_Copy
(Expression_Of_Expression_Function
(Subp
)));
5184 Analyze
(Call_Node
);
5187 -- Handle front-end inlining
5189 elsif not Back_End_Inlining
then
5190 Inlined_Subprogram
: declare
5192 Must_Inline
: Boolean := False;
5193 Spec
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
5196 -- Verify that the body to inline has already been seen, and
5197 -- that if the body is in the current unit the inlining does
5198 -- not occur earlier. This avoids order-of-elaboration problems
5201 -- This should be documented in sinfo/einfo ???
5204 or else Nkind
(Spec
) /= N_Subprogram_Declaration
5205 or else No
(Body_To_Inline
(Spec
))
5207 Must_Inline
:= False;
5209 -- If this an inherited function that returns a private type,
5210 -- do not inline if the full view is an unconstrained array,
5211 -- because such calls cannot be inlined.
5213 elsif Present
(Orig_Subp
)
5214 and then Is_Array_Type
(Etype
(Orig_Subp
))
5215 and then not Is_Constrained
(Etype
(Orig_Subp
))
5217 Must_Inline
:= False;
5219 elsif In_Unfrozen_Instance
(Scope
(Subp
)) then
5220 Must_Inline
:= False;
5223 Bod
:= Body_To_Inline
(Spec
);
5225 if (In_Extended_Main_Code_Unit
(Call_Node
)
5226 or else In_Extended_Main_Code_Unit
(Parent
(Call_Node
))
5227 or else Has_Pragma_Inline_Always
(Subp
))
5228 and then (not In_Same_Extended_Unit
(Sloc
(Bod
), Loc
)
5230 Earlier_In_Extended_Unit
(Sloc
(Bod
), Loc
))
5232 Must_Inline
:= True;
5234 -- If we are compiling a package body that is not the main
5235 -- unit, it must be for inlining/instantiation purposes,
5236 -- in which case we inline the call to insure that the same
5237 -- temporaries are generated when compiling the body by
5238 -- itself. Otherwise link errors can occur.
5240 -- If the function being called is itself in the main unit,
5241 -- we cannot inline, because there is a risk of double
5242 -- elaboration and/or circularity: the inlining can make
5243 -- visible a private entity in the body of the main unit,
5244 -- that gigi will see before its sees its proper definition.
5246 elsif not In_Extended_Main_Code_Unit
(Call_Node
)
5247 and then In_Package_Body
5249 Must_Inline
:= not In_Extended_Main_Source_Unit
(Subp
);
5251 -- Inline calls to _Wrapped_Statements when generating C
5253 elsif Modify_Tree_For_C
5254 and then In_Same_Extended_Unit
(Sloc
(Bod
), Loc
)
5255 and then Chars
(Name
(Call_Node
))
5256 = Name_uWrapped_Statements
5258 Must_Inline
:= True;
5263 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
5266 -- Let the back end handle it
5268 Add_Inlined_Body
(Subp
, Call_Node
);
5270 if Front_End_Inlining
5271 and then Nkind
(Spec
) = N_Subprogram_Declaration
5272 and then In_Extended_Main_Code_Unit
(Call_Node
)
5273 and then No
(Body_To_Inline
(Spec
))
5274 and then not Has_Completion
(Subp
)
5275 and then In_Same_Extended_Unit
(Sloc
(Spec
), Loc
)
5278 ("cannot inline& (body not seen yet)?",
5282 end Inlined_Subprogram
;
5284 -- Front-end expansion of simple functions returning unconstrained
5285 -- types (see Check_And_Split_Unconstrained_Function). Note that the
5286 -- case of a simple renaming (Body_To_Inline in N_Entity below, see
5287 -- also Build_Renamed_Body) cannot be expanded here because this may
5288 -- give rise to order-of-elaboration issues for the types of the
5289 -- parameters of the subprogram, if any.
5291 elsif Present
(Unit_Declaration_Node
(Subp
))
5292 and then Nkind
(Unit_Declaration_Node
(Subp
)) =
5293 N_Subprogram_Declaration
5294 and then Present
(Body_To_Inline
(Unit_Declaration_Node
(Subp
)))
5296 Nkind
(Body_To_Inline
(Unit_Declaration_Node
(Subp
))) not in
5299 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
5301 -- Back-end inlining either if optimization is enabled or the call is
5302 -- required to be inlined.
5304 elsif Optimization_Level
> 0
5305 or else Has_Pragma_Inline_Always
(Subp
)
5307 Add_Inlined_Body
(Subp
, Call_Node
);
5311 -- Check for protected subprogram. This is either an intra-object call,
5312 -- or a protected function call. Protected procedure calls are rewritten
5313 -- as entry calls and handled accordingly.
5315 -- In Ada 2005, this may be an indirect call to an access parameter that
5316 -- is an access_to_subprogram. In that case the anonymous type has a
5317 -- scope that is a protected operation, but the call is a regular one.
5318 -- In either case do not expand call if subprogram is eliminated.
5320 Scop
:= Scope
(Subp
);
5322 if Nkind
(Call_Node
) /= N_Entry_Call_Statement
5323 and then Is_Protected_Type
(Scop
)
5324 and then Ekind
(Subp
) /= E_Subprogram_Type
5325 and then not Is_Eliminated
(Subp
)
5327 -- If the call is an internal one, it is rewritten as a call to the
5328 -- corresponding unprotected subprogram.
5330 Expand_Protected_Subprogram_Call
(Call_Node
, Subp
, Scop
);
5333 -- Functions returning controlled objects need special attention. If
5334 -- the return type is limited, then the context is initialization and
5335 -- different processing applies. If the call is to a protected function,
5336 -- the expansion above will call Expand_Call recursively. Otherwise the
5337 -- function call is transformed into a reference to the result that has
5338 -- been built either on the primary or the secondary stack.
5340 if Needs_Finalization
(Etype
(Subp
)) then
5341 if not Is_Build_In_Place_Function_Call
(Call_Node
)
5343 (No
(First_Formal
(Subp
))
5345 not Is_Concurrent_Record_Type
(Etype
(First_Formal
(Subp
))))
5347 Expand_Ctrl_Function_Call
5348 (Call_Node
, Needs_Secondary_Stack
(Etype
(Subp
)));
5350 -- Build-in-place function calls which appear in anonymous contexts
5351 -- need a transient scope to ensure the proper finalization of the
5352 -- intermediate result after its use.
5354 elsif Is_Build_In_Place_Function_Call
(Call_Node
)
5355 and then Nkind
(Parent
(Unqual_Conv
(Call_Node
))) in
5356 N_Attribute_Reference
5358 | N_Indexed_Component
5359 | N_Object_Renaming_Declaration
5360 | N_Procedure_Call_Statement
5361 | N_Selected_Component
5364 (Ekind
(Current_Scope
) /= E_Loop
5365 or else Nkind
(Parent
(Call_Node
)) /= N_Function_Call
5367 Is_Build_In_Place_Function_Call
(Parent
(Call_Node
)))
5369 Establish_Transient_Scope
5370 (Call_Node
, Needs_Secondary_Stack
(Etype
(Subp
)));
5373 end Expand_Call_Helper
;
5375 -------------------------------
5376 -- Expand_Ctrl_Function_Call --
5377 -------------------------------
5379 procedure Expand_Ctrl_Function_Call
(N
: Node_Id
; Use_Sec_Stack
: Boolean)
5381 Par
: constant Node_Id
:= Parent
(N
);
5383 function Is_Element_Reference
(N
: Node_Id
) return Boolean;
5384 -- Determine whether node N denotes a reference to an Ada 2012 container
5387 --------------------------
5388 -- Is_Element_Reference --
5389 --------------------------
5391 function Is_Element_Reference
(N
: Node_Id
) return Boolean is
5392 Ref
: constant Node_Id
:= Original_Node
(N
);
5395 -- Analysis marks an element reference by setting the generalized
5396 -- indexing attribute of an indexed component before the component
5397 -- is rewritten into a function call.
5400 Nkind
(Ref
) = N_Indexed_Component
5401 and then Present
(Generalized_Indexing
(Ref
));
5402 end Is_Element_Reference
;
5404 -- Start of processing for Expand_Ctrl_Function_Call
5407 -- Optimization: if the returned value is returned again, then no need
5408 -- to copy/readjust/finalize, we can just pass the value through (see
5409 -- Expand_N_Simple_Return_Statement), and thus no attachment is needed.
5410 -- Note that simple return statements are distributed into conditional
5411 -- expressions but we may be invoked before this distribution is done.
5413 if Nkind
(Par
) = N_Simple_Return_Statement
5414 or else (Nkind
(Par
) = N_If_Expression
5415 and then Nkind
(Parent
(Par
)) = N_Simple_Return_Statement
)
5416 or else (Nkind
(Par
) = N_Case_Expression_Alternative
5418 Nkind
(Parent
(Parent
(Par
))) = N_Simple_Return_Statement
)
5423 -- Another optimization: if the returned value is used to initialize an
5424 -- object, then no need to copy/readjust/finalize, we can initialize it
5425 -- in place. However, if the call returns on the secondary stack, then
5426 -- we need the expansion because we'll be renaming the temporary as the
5427 -- (permanent) object.
5429 if Nkind
(Par
) = N_Object_Declaration
and then not Use_Sec_Stack
then
5433 -- Resolution is now finished, make sure we don't start analysis again
5434 -- because of the duplication.
5438 -- Apply the transformation, unless it was already applied manually
5440 if Nkind
(Par
) /= N_Reference
then
5441 Remove_Side_Effects
(N
);
5444 -- The side effect removal of the function call produced a temporary.
5445 -- When the context is a case expression, if expression, or expression
5446 -- with actions, the lifetime of the temporary must be extended to match
5447 -- that of the context. Otherwise the function result will be finalized
5448 -- too early and affect the result of the expression. To prevent this
5449 -- unwanted effect, the temporary should not be considered for clean up
5450 -- actions by the general finalization machinery.
5452 -- Exception to this rule are references to Ada 2012 container elements.
5453 -- Such references must be finalized at the end of each iteration of the
5454 -- related quantified expression, otherwise the container will remain
5457 if Nkind
(N
) = N_Explicit_Dereference
5458 and then Within_Case_Or_If_Expression
(N
)
5459 and then not Is_Element_Reference
(N
)
5461 Set_Is_Ignored_Transient
(Entity
(Prefix
(N
)));
5463 end Expand_Ctrl_Function_Call
;
5465 ----------------------------------------
5466 -- Expand_N_Extended_Return_Statement --
5467 ----------------------------------------
5469 -- If there is a Handled_Statement_Sequence, we rewrite this:
5471 -- return Result : T := <expression> do
5472 -- <handled_seq_of_stms>
5478 -- Result : T := <expression>;
5480 -- <handled_seq_of_stms>
5484 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
5486 -- return Result : T := <expression>;
5490 -- return <expression>;
5492 -- unless it's build-in-place or there's no <expression>, in which case
5496 -- Result : T := <expression>;
5501 -- Note that this case could have been written by the user as an extended
5502 -- return statement, or could have been transformed to this from a simple
5503 -- return statement.
5505 -- That is, we need to have a reified return object if there are statements
5506 -- (which might refer to it) or if we're doing build-in-place (so we can
5507 -- set its address to the final resting place or if there is no expression
5508 -- (in which case default initial values might need to be set)).
5510 procedure Expand_N_Extended_Return_Statement
(N
: Node_Id
) is
5511 Loc
: constant Source_Ptr
:= Sloc
(N
);
5512 Func_Id
: constant Entity_Id
:=
5513 Return_Applies_To
(Return_Statement_Entity
(N
));
5514 Is_BIP_Func
: constant Boolean :=
5515 Is_Build_In_Place_Function
(Func_Id
);
5516 Ret_Obj_Id
: constant Entity_Id
:=
5517 First_Entity
(Return_Statement_Entity
(N
));
5518 Ret_Obj_Decl
: constant Node_Id
:= Parent
(Ret_Obj_Id
);
5519 Ret_Typ
: constant Entity_Id
:= Etype
(Func_Id
);
5521 function Move_Activation_Chain
(Func_Id
: Entity_Id
) return Node_Id
;
5522 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
5524 -- From current activation chain
5525 -- To activation chain passed in by the caller
5526 -- New_Master master passed in by the caller
5528 -- Func_Id is the entity of the function where the extended return
5529 -- statement appears.
5531 ---------------------------
5532 -- Move_Activation_Chain --
5533 ---------------------------
5535 function Move_Activation_Chain
(Func_Id
: Entity_Id
) return Node_Id
is
5538 Make_Procedure_Call_Statement
(Loc
,
5540 New_Occurrence_Of
(RTE
(RE_Move_Activation_Chain
), Loc
),
5542 Parameter_Associations
=> New_List
(
5546 Make_Attribute_Reference
(Loc
,
5547 Prefix
=> Make_Identifier
(Loc
, Name_uChain
),
5548 Attribute_Name
=> Name_Unrestricted_Access
),
5550 -- Destination chain
5553 (Build_In_Place_Formal
(Func_Id
, BIP_Activation_Chain
), Loc
),
5558 (Build_In_Place_Formal
(Func_Id
, BIP_Task_Master
), Loc
)));
5559 end Move_Activation_Chain
;
5566 Stmts
: List_Id
:= No_List
;
5568 Return_Stmt
: Node_Id
:= Empty
;
5569 -- Force initialization to facilitate static analysis
5571 -- Start of processing for Expand_N_Extended_Return_Statement
5574 -- Given that functionality of interface thunks is simple (just displace
5575 -- the pointer to the object) they are always handled by means of
5576 -- simple return statements.
5578 pragma Assert
(not Is_Thunk
(Current_Subprogram
));
5580 if Nkind
(Ret_Obj_Decl
) = N_Object_Declaration
then
5581 Exp
:= Expression
(Ret_Obj_Decl
);
5583 -- Assert that if F says "return R : T := G(...) do..."
5584 -- then F and G are both b-i-p, or neither b-i-p.
5586 if Present
(Exp
) and then Nkind
(Exp
) = N_Function_Call
then
5587 pragma Assert
(Ekind
(Current_Subprogram
) = E_Function
);
5589 (Is_Build_In_Place_Function
(Current_Subprogram
) =
5590 Is_True_Build_In_Place_Function_Call
(Exp
));
5598 HSS
:= Handled_Statement_Sequence
(N
);
5600 -- If the returned object needs finalization actions, the function must
5601 -- perform the appropriate cleanup should it fail to return. The state
5602 -- of the function itself is tracked through a flag which is coupled
5603 -- with the scope finalizer. There is one flag per each return object
5604 -- in case of multiple returns.
5606 if Is_BIP_Func
and then Needs_Finalization
(Etype
(Ret_Obj_Id
)) then
5608 Flag_Decl
: Node_Id
;
5609 Flag_Id
: Entity_Id
;
5613 -- Recover the function body
5615 Func_Bod
:= Unit_Declaration_Node
(Func_Id
);
5617 if Nkind
(Func_Bod
) = N_Subprogram_Declaration
then
5618 Func_Bod
:= Parent
(Parent
(Corresponding_Body
(Func_Bod
)));
5621 if Nkind
(Func_Bod
) = N_Function_Specification
then
5622 Func_Bod
:= Parent
(Func_Bod
); -- one more level for child units
5625 pragma Assert
(Nkind
(Func_Bod
) = N_Subprogram_Body
);
5627 -- Create a flag to track the function state
5629 Flag_Id
:= Make_Temporary
(Loc
, 'F');
5630 Set_Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
, Flag_Id
);
5632 -- Insert the flag at the beginning of the function declarations,
5634 -- Fnn : Boolean := False;
5637 Make_Object_Declaration
(Loc
,
5638 Defining_Identifier
=> Flag_Id
,
5639 Object_Definition
=>
5640 New_Occurrence_Of
(Standard_Boolean
, Loc
),
5642 New_Occurrence_Of
(Standard_False
, Loc
));
5644 Prepend_To
(Declarations
(Func_Bod
), Flag_Decl
);
5645 Analyze
(Flag_Decl
);
5649 -- Build a simple_return_statement that returns the return object when
5650 -- there is a statement sequence, or no expression, or the analysis of
5651 -- the return object declaration generated extra actions, or the result
5652 -- will be built in place. Note however that we currently do this for
5653 -- all composite cases, even though they are not built in place.
5657 or else List_Length
(Return_Object_Declarations
(N
)) > 1
5658 or else Is_Composite_Type
(Ret_Typ
)
5663 -- If the extended return has a handled statement sequence, then wrap
5664 -- it in a block and use the block as the first statement.
5668 Make_Block_Statement
(Loc
,
5669 Declarations
=> New_List
,
5670 Handled_Statement_Sequence
=> HSS
));
5673 -- If the result type contains tasks, we call Move_Activation_Chain.
5674 -- Later, the cleanup code will call Complete_Master, which will
5675 -- terminate any unactivated tasks belonging to the return statement
5676 -- master. But Move_Activation_Chain updates their master to be that
5677 -- of the caller, so they will not be terminated unless the return
5678 -- statement completes unsuccessfully due to exception, abort, goto,
5679 -- or exit. As a formality, we test whether the function requires the
5680 -- result to be built in place, though that's necessarily true for
5681 -- the case of result types with task parts.
5683 if Is_BIP_Func
and then Has_Task
(Ret_Typ
) then
5685 -- The return expression is an aggregate for a complex type which
5686 -- contains tasks. This particular case is left unexpanded since
5687 -- the regular expansion would insert all temporaries and
5688 -- initialization code in the wrong block.
5690 if Nkind
(Exp
) = N_Aggregate
then
5691 Expand_N_Aggregate
(Exp
);
5694 -- Do not move the activation chain if the return object does not
5697 if Has_Task
(Etype
(Ret_Obj_Id
)) then
5698 Append_To
(Stmts
, Move_Activation_Chain
(Func_Id
));
5702 -- Update the state of the function right before the object is
5705 if Is_BIP_Func
and then Needs_Finalization
(Etype
(Ret_Obj_Id
)) then
5707 Flag_Id
: constant Entity_Id
:=
5708 Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
);
5715 Make_Assignment_Statement
(Loc
,
5716 Name
=> New_Occurrence_Of
(Flag_Id
, Loc
),
5717 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)));
5721 HSS
:= Make_Handled_Sequence_Of_Statements
(Loc
, Stmts
);
5724 -- Case where we build a return statement block
5726 if Present
(HSS
) then
5728 Make_Block_Statement
(Loc
,
5729 Declarations
=> Return_Object_Declarations
(N
),
5730 Handled_Statement_Sequence
=> HSS
);
5732 -- We set the entity of the new block statement to be that of the
5733 -- return statement. This is necessary so that various fields, such
5734 -- as Finalization_Chain_Entity carry over from the return statement
5735 -- to the block. Note that this block is unusual, in that its entity
5736 -- is an E_Return_Statement rather than an E_Block.
5739 (Result
, New_Occurrence_Of
(Return_Statement_Entity
(N
), Loc
));
5741 -- Build a simple_return_statement that returns the return object
5744 Make_Simple_Return_Statement
(Loc
,
5745 Expression
=> New_Occurrence_Of
(Ret_Obj_Id
, Loc
));
5746 Append_To
(Stmts
, Return_Stmt
);
5748 -- Case where we do not need to build a block. But we're about to drop
5749 -- Return_Object_Declarations on the floor, so assert that it contains
5750 -- only the return object declaration.
5752 else pragma Assert
(List_Length
(Return_Object_Declarations
(N
)) = 1);
5754 -- Build simple_return_statement that returns the expression directly
5756 Return_Stmt
:= Make_Simple_Return_Statement
(Loc
, Expression
=> Exp
);
5757 Result
:= Return_Stmt
;
5760 -- Set the flag to prevent infinite recursion
5762 Set_Comes_From_Extended_Return_Statement
(Return_Stmt
);
5763 Set_Return_Statement
(Ret_Obj_Id
, Return_Stmt
);
5765 Rewrite
(N
, Result
);
5767 -- AI12-043: The checks of 6.5(8.1/3) and 6.5(21/3) are made immediately
5768 -- before an object is returned. A predicate that applies to the return
5769 -- subtype is checked immediately before an object is returned.
5772 end Expand_N_Extended_Return_Statement
;
5774 ----------------------------
5775 -- Expand_N_Function_Call --
5776 ----------------------------
5778 procedure Expand_N_Function_Call
(N
: Node_Id
) is
5781 end Expand_N_Function_Call
;
5783 ---------------------------------------
5784 -- Expand_N_Procedure_Call_Statement --
5785 ---------------------------------------
5787 procedure Expand_N_Procedure_Call_Statement
(N
: Node_Id
) is
5790 end Expand_N_Procedure_Call_Statement
;
5792 ------------------------------------
5793 -- Expand_N_Return_When_Statement --
5794 ------------------------------------
5796 procedure Expand_N_Return_When_Statement
(N
: Node_Id
) is
5797 Loc
: constant Source_Ptr
:= Sloc
(N
);
5800 Make_If_Statement
(Loc
,
5801 Condition
=> Condition
(N
),
5802 Then_Statements
=> New_List
(
5803 Make_Simple_Return_Statement
(Loc
,
5804 Expression
=> Expression
(N
)))));
5807 end Expand_N_Return_When_Statement
;
5809 --------------------------------------
5810 -- Expand_N_Simple_Return_Statement --
5811 --------------------------------------
5813 procedure Expand_N_Simple_Return_Statement
(N
: Node_Id
) is
5815 -- Defend against previous errors (i.e. the return statement calls a
5816 -- function that is not available in configurable runtime).
5818 if Present
(Expression
(N
))
5819 and then Nkind
(Expression
(N
)) = N_Empty
5821 Check_Error_Detected
;
5825 -- Distinguish the function and non-function cases:
5827 case Ekind
(Return_Applies_To
(Return_Statement_Entity
(N
))) is
5829 | E_Generic_Function
5831 Expand_Simple_Function_Return
(N
);
5835 | E_Generic_Procedure
5837 | E_Return_Statement
5839 Expand_Non_Function_Return
(N
);
5842 raise Program_Error
;
5846 when RE_Not_Available
=>
5848 end Expand_N_Simple_Return_Statement
;
5850 ------------------------------
5851 -- Expand_N_Subprogram_Body --
5852 ------------------------------
5854 -- Add dummy push/pop label nodes at start and end to clear any local
5855 -- exception indications if local-exception-to-goto optimization is active.
5857 -- Add return statement if last statement in body is not a return statement
5858 -- (this makes things easier on Gigi which does not want to have to handle
5859 -- a missing return).
5861 -- Add call to Activate_Tasks if body is a task activator
5863 -- Deal with possible detection of infinite recursion
5865 -- Eliminate body completely if convention stubbed
5867 -- Encode entity names within body, since we will not need to reference
5868 -- these entities any longer in the front end.
5870 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5872 -- Reset Pure indication if any parameter has root type System.Address
5873 -- or has any parameters of limited types, where limited means that the
5874 -- run-time view is limited (i.e. the full type is limited).
5878 procedure Expand_N_Subprogram_Body
(N
: Node_Id
) is
5879 Body_Id
: constant Entity_Id
:= Defining_Entity
(N
);
5880 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
5881 Loc
: constant Source_Ptr
:= Sloc
(N
);
5883 procedure Add_Return
(Spec_Id
: Entity_Id
; Stmts
: List_Id
);
5884 -- Append a return statement to the statement sequence Stmts if the last
5885 -- statement is not already a return or a goto statement. Note that the
5886 -- latter test is not critical, it does not matter if we add a few extra
5887 -- returns, since they get eliminated anyway later on. Spec_Id denotes
5888 -- the corresponding spec of the subprogram body.
5894 procedure Add_Return
(Spec_Id
: Entity_Id
; Stmts
: List_Id
) is
5895 Last_Stmt
: Node_Id
;
5900 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5901 -- not relevant in this context since they are not executable.
5903 Last_Stmt
:= Last
(Stmts
);
5904 while Nkind
(Last_Stmt
) in N_Pop_xxx_Label
loop
5908 -- Now insert return unless last statement is a transfer
5910 if not Is_Transfer
(Last_Stmt
) then
5912 -- The source location for the return is the end label of the
5913 -- procedure if present. Otherwise use the sloc of the last
5914 -- statement in the list. If the list comes from a generated
5915 -- exception handler and we are not debugging generated code,
5916 -- all the statements within the handler are made invisible
5919 if Nkind
(Parent
(Stmts
)) = N_Exception_Handler
5920 and then not Comes_From_Source
(Parent
(Stmts
))
5922 Loc
:= Sloc
(Last_Stmt
);
5923 elsif Present
(End_Label
(HSS
)) then
5924 Loc
:= Sloc
(End_Label
(HSS
));
5926 Loc
:= Sloc
(Last_Stmt
);
5929 -- Append return statement, and set analyzed manually. We can't
5930 -- call Analyze on this return since the scope is wrong.
5932 -- Note: it almost works to push the scope and then do the Analyze
5933 -- call, but something goes wrong in some weird cases and it is
5934 -- not worth worrying about ???
5936 Stmt
:= Make_Simple_Return_Statement
(Loc
);
5938 -- The return statement is handled properly, and the call to the
5939 -- postcondition, inserted below, does not require information
5940 -- from the body either. However, that call is analyzed in the
5941 -- enclosing scope, and an elaboration check might improperly be
5942 -- added to it. A guard in Sem_Elab is needed to prevent that
5943 -- spurious check, see Check_Elab_Call.
5945 Append_To
(Stmts
, Stmt
);
5946 Set_Analyzed
(Stmt
);
5948 -- Ada 2022 (AI12-0279): append the call to 'Yield unless this is
5949 -- a generic subprogram (since in such case it will be added to
5950 -- the instantiations).
5952 if Has_Yield_Aspect
(Spec_Id
)
5953 and then Ekind
(Spec_Id
) /= E_Generic_Procedure
5954 and then RTE_Available
(RE_Yield
)
5956 Insert_Action
(Stmt
,
5957 Make_Procedure_Call_Statement
(Loc
,
5958 New_Occurrence_Of
(RTE
(RE_Yield
), Loc
)));
5967 Spec_Id
: Entity_Id
;
5969 -- Start of processing for Expand_N_Subprogram_Body
5972 if Present
(Corresponding_Spec
(N
)) then
5973 Spec_Id
:= Corresponding_Spec
(N
);
5978 -- If this is a Pure function which has any parameters whose root type
5979 -- is System.Address, reset the Pure indication.
5980 -- This check is also performed when the subprogram is frozen, but we
5981 -- repeat it on the body so that the indication is consistent, and so
5982 -- it applies as well to bodies without separate specifications.
5984 if Is_Pure
(Spec_Id
)
5985 and then Is_Subprogram
(Spec_Id
)
5986 and then not Has_Pragma_Pure_Function
(Spec_Id
)
5988 Check_Function_With_Address_Parameter
(Spec_Id
);
5990 if Spec_Id
/= Body_Id
then
5991 Set_Is_Pure
(Body_Id
, Is_Pure
(Spec_Id
));
5995 -- Set L to either the list of declarations if present, or to the list
5996 -- of statements if no declarations are present. This is used to insert
5997 -- new stuff at the start.
5999 if Is_Non_Empty_List
(Declarations
(N
)) then
6000 L
:= Declarations
(N
);
6002 L
:= Statements
(HSS
);
6005 -- If local-exception-to-goto optimization active, insert dummy push
6006 -- statements at start, and dummy pop statements at end, but inhibit
6007 -- this if we have No_Exception_Handlers, since they are useless and
6008 -- interfere with analysis, e.g. by CodePeer. We also don't need these
6009 -- if we're unnesting subprograms because the only purpose of these
6010 -- nodes is to ensure we don't set a label in one subprogram and branch
6011 -- to it in another.
6013 if (Debug_Flag_Dot_G
6014 or else Restriction_Active
(No_Exception_Propagation
))
6015 and then not Restriction_Active
(No_Exception_Handlers
)
6016 and then not CodePeer_Mode
6017 and then not Unnest_Subprogram_Mode
6018 and then Is_Non_Empty_List
(L
)
6021 FS
: constant Node_Id
:= First
(L
);
6022 FL
: constant Source_Ptr
:= Sloc
(FS
);
6027 -- LS points to either last statement, if statements are present
6028 -- or to the last declaration if there are no statements present.
6029 -- It is the node after which the pop's are generated.
6031 if Is_Non_Empty_List
(Statements
(HSS
)) then
6032 LS
:= Last
(Statements
(HSS
));
6039 Insert_List_Before_And_Analyze
(FS
, New_List
(
6040 Make_Push_Constraint_Error_Label
(FL
),
6041 Make_Push_Program_Error_Label
(FL
),
6042 Make_Push_Storage_Error_Label
(FL
)));
6044 Insert_List_After_And_Analyze
(LS
, New_List
(
6045 Make_Pop_Constraint_Error_Label
(LL
),
6046 Make_Pop_Program_Error_Label
(LL
),
6047 Make_Pop_Storage_Error_Label
(LL
)));
6051 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
6053 if Init_Or_Norm_Scalars
and then Is_Subprogram
(Spec_Id
) then
6059 -- Loop through formals
6061 F
:= First_Formal
(Spec_Id
);
6062 while Present
(F
) loop
6063 if Is_Scalar_Type
(Etype
(F
))
6064 and then Ekind
(F
) = E_Out_Parameter
6066 Check_Restriction
(No_Default_Initialization
, F
);
6068 -- Insert the initialization. We turn off validity checks
6069 -- for this assignment, since we do not want any check on
6070 -- the initial value itself (which may well be invalid).
6071 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
6074 Make_Assignment_Statement
(Loc
,
6075 Name
=> New_Occurrence_Of
(F
, Loc
),
6076 Expression
=> Get_Simple_Init_Val
(Etype
(F
), N
));
6077 Set_Suppress_Assignment_Checks
(A
);
6079 Insert_Before_And_Analyze
(First
(L
),
6080 A
, Suppress
=> Validity_Check
);
6088 -- Clear out statement list for stubbed procedure
6090 if Present
(Corresponding_Spec
(N
)) then
6091 Set_Elaboration_Flag
(N
, Spec_Id
);
6093 if Convention
(Spec_Id
) = Convention_Stubbed
6094 or else Is_Eliminated
(Spec_Id
)
6096 Set_Declarations
(N
, Empty_List
);
6097 Set_Handled_Statement_Sequence
(N
,
6098 Make_Handled_Sequence_Of_Statements
(Loc
,
6099 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
6105 -- Create a set of discriminals for the next protected subprogram body
6107 if Is_List_Member
(N
)
6108 and then Present
(Parent
(List_Containing
(N
)))
6109 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
6110 and then Present
(Next_Protected_Operation
(N
))
6112 Set_Discriminals
(Parent
(Base_Type
(Scope
(Spec_Id
))));
6115 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
6116 -- subprograms with no specs are not frozen.
6118 Compute_Returns_By_Ref
(Spec_Id
);
6120 -- For a procedure, we add a return for all possible syntactic ends of
6123 if Ekind
(Spec_Id
) in E_Procedure | E_Generic_Procedure
then
6124 Add_Return
(Spec_Id
, Statements
(HSS
));
6126 if Present
(Exception_Handlers
(HSS
)) then
6127 Except_H
:= First_Non_Pragma
(Exception_Handlers
(HSS
));
6128 while Present
(Except_H
) loop
6129 Add_Return
(Spec_Id
, Statements
(Except_H
));
6130 Next_Non_Pragma
(Except_H
);
6134 -- For a function, we must deal with the case where there is at least
6135 -- one missing return. What we do is to wrap the entire body of the
6136 -- function in a block:
6149 -- raise Program_Error;
6152 -- This approach is necessary because the raise must be signalled to the
6153 -- caller, not handled by any local handler (RM 6.4(11)).
6155 -- Note: we do not need to analyze the constructed sequence here, since
6156 -- it has no handler, and an attempt to analyze the handled statement
6157 -- sequence twice is risky in various ways (e.g. the issue of expanding
6158 -- cleanup actions twice).
6160 elsif Has_Missing_Return
(Spec_Id
) then
6162 Hloc
: constant Source_Ptr
:= Sloc
(HSS
);
6163 Blok
: constant Node_Id
:=
6164 Make_Block_Statement
(Hloc
,
6165 Handled_Statement_Sequence
=> HSS
);
6166 Rais
: constant Node_Id
:=
6167 Make_Raise_Program_Error
(Hloc
,
6168 Reason
=> PE_Missing_Return
);
6171 Set_Handled_Statement_Sequence
(N
,
6172 Make_Handled_Sequence_Of_Statements
(Hloc
,
6173 Statements
=> New_List
(Blok
, Rais
)));
6175 Push_Scope
(Spec_Id
);
6182 -- If subprogram contains a parameterless recursive call, then we may
6183 -- have an infinite recursion, so see if we can generate code to check
6184 -- for this possibility if storage checks are not suppressed.
6186 if Ekind
(Spec_Id
) = E_Procedure
6187 and then Has_Recursive_Call
(Spec_Id
)
6188 and then not Storage_Checks_Suppressed
(Spec_Id
)
6190 Detect_Infinite_Recursion
(N
, Spec_Id
);
6193 -- Set to encode entity names in package body before gigi is called
6195 Qualify_Entity_Names
(N
);
6197 -- If the body belongs to a nonabstract library-level source primitive
6198 -- of a tagged type, install an elaboration check which ensures that a
6199 -- dispatching call targeting the primitive will not execute the body
6200 -- without it being previously elaborated.
6202 Install_Primitive_Elaboration_Check
(N
);
6203 end Expand_N_Subprogram_Body
;
6205 -----------------------------------
6206 -- Expand_N_Subprogram_Body_Stub --
6207 -----------------------------------
6209 procedure Expand_N_Subprogram_Body_Stub
(N
: Node_Id
) is
6213 if Present
(Corresponding_Body
(N
)) then
6214 Bod
:= Unit_Declaration_Node
(Corresponding_Body
(N
));
6216 -- The body may have been expanded already when it is analyzed
6217 -- through the subunit node. Do no expand again: it interferes
6218 -- with the construction of unnesting tables when generating C.
6220 if not Analyzed
(Bod
) then
6221 Expand_N_Subprogram_Body
(Bod
);
6224 -- Add full qualification to entities that may be created late
6225 -- during unnesting.
6227 Qualify_Entity_Names
(N
);
6229 end Expand_N_Subprogram_Body_Stub
;
6231 -------------------------------------
6232 -- Expand_N_Subprogram_Declaration --
6233 -------------------------------------
6235 -- If the declaration appears within a protected body, it is a private
6236 -- operation of the protected type. We must create the corresponding
6237 -- protected subprogram an associated formals. For a normal protected
6238 -- operation, this is done when expanding the protected type declaration.
6240 -- If the declaration is for a null procedure, emit null body
6242 procedure Expand_N_Subprogram_Declaration
(N
: Node_Id
) is
6243 Loc
: constant Source_Ptr
:= Sloc
(N
);
6244 Subp
: constant Entity_Id
:= Defining_Entity
(N
);
6248 Scop
: constant Entity_Id
:= Scope
(Subp
);
6250 Prot_Decl
: Node_Id
;
6251 Prot_Id
: Entity_Id
;
6255 -- Deal with case of protected subprogram. Do not generate protected
6256 -- operation if operation is flagged as eliminated.
6258 if Is_List_Member
(N
)
6259 and then Present
(Parent
(List_Containing
(N
)))
6260 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
6261 and then Is_Protected_Type
(Scop
)
6263 if No
(Protected_Body_Subprogram
(Subp
))
6264 and then not Is_Eliminated
(Subp
)
6267 Make_Subprogram_Declaration
(Loc
,
6269 Build_Protected_Sub_Specification
6270 (N
, Scop
, Unprotected_Mode
));
6272 -- The protected subprogram is declared outside of the protected
6273 -- body. Given that the body has frozen all entities so far, we
6274 -- analyze the subprogram and perform freezing actions explicitly.
6275 -- including the generation of an explicit freeze node, to ensure
6276 -- that gigi has the proper order of elaboration.
6277 -- If the body is a subunit, the insertion point is before the
6278 -- stub in the parent.
6280 Prot_Bod
:= Parent
(List_Containing
(N
));
6282 if Nkind
(Parent
(Prot_Bod
)) = N_Subunit
then
6283 Prot_Bod
:= Corresponding_Stub
(Parent
(Prot_Bod
));
6286 Insert_Before
(Prot_Bod
, Prot_Decl
);
6287 Prot_Id
:= Defining_Unit_Name
(Specification
(Prot_Decl
));
6288 Set_Has_Delayed_Freeze
(Prot_Id
);
6290 Push_Scope
(Scope
(Scop
));
6291 Analyze
(Prot_Decl
);
6292 Freeze_Before
(N
, Prot_Id
);
6293 Set_Protected_Body_Subprogram
(Subp
, Prot_Id
);
6297 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
6298 -- cases this is superfluous because calls to it will be automatically
6299 -- inlined, but we definitely need the body if preconditions for the
6300 -- procedure are present, or if performing coverage analysis.
6302 elsif Nkind
(Specification
(N
)) = N_Procedure_Specification
6303 and then Null_Present
(Specification
(N
))
6306 Bod
: constant Node_Id
:= Body_To_Inline
(N
);
6309 Set_Has_Completion
(Subp
, False);
6310 Append_Freeze_Action
(Subp
, Bod
);
6312 -- The body now contains raise statements, so calls to it will
6315 Set_Is_Inlined
(Subp
, False);
6319 -- When generating C code, transform a function that returns a
6320 -- constrained array type into a procedure with an out parameter
6321 -- that carries the return value.
6323 -- We skip this transformation for unchecked conversions, since they
6324 -- are not needed by the C generator (and this also produces cleaner
6327 Typ
:= Get_Fullest_View
(Etype
(Subp
));
6329 if Transform_Function_Array
6330 and then Nkind
(Specification
(N
)) = N_Function_Specification
6331 and then Is_Array_Type
(Typ
)
6332 and then Is_Constrained
(Typ
)
6333 and then not Is_Unchecked_Conversion_Instance
(Subp
)
6335 Build_Procedure_Form
(N
);
6337 end Expand_N_Subprogram_Declaration
;
6339 --------------------------------
6340 -- Expand_Non_Function_Return --
6341 --------------------------------
6343 procedure Expand_Non_Function_Return
(N
: Node_Id
) is
6344 pragma Assert
(No
(Expression
(N
)));
6346 Loc
: constant Source_Ptr
:= Sloc
(N
);
6347 Scope_Id
: Entity_Id
:= Return_Applies_To
(Return_Statement_Entity
(N
));
6348 Kind
: constant Entity_Kind
:= Ekind
(Scope_Id
);
6351 Goto_Stat
: Node_Id
;
6355 -- Ada 2022 (AI12-0279)
6357 if Has_Yield_Aspect
(Scope_Id
)
6358 and then RTE_Available
(RE_Yield
)
6361 Make_Procedure_Call_Statement
(Loc
,
6362 New_Occurrence_Of
(RTE
(RE_Yield
), Loc
)));
6365 -- If it is a return from a procedure do no extra steps
6367 if Kind
= E_Procedure
or else Kind
= E_Generic_Procedure
then
6370 -- If it is a nested return within an extended one, replace it with a
6371 -- return of the previously declared return object.
6373 elsif Kind
= E_Return_Statement
then
6375 Make_Simple_Return_Statement
(Loc
,
6377 New_Occurrence_Of
(First_Entity
(Scope_Id
), Loc
)));
6378 Set_Comes_From_Extended_Return_Statement
(N
);
6379 Set_Return_Statement_Entity
(N
, Scope_Id
);
6380 Expand_Simple_Function_Return
(N
);
6384 pragma Assert
(Is_Entry
(Scope_Id
));
6386 -- Look at the enclosing block to see whether the return is from an
6387 -- accept statement or an entry body.
6389 for J
in reverse 0 .. Scope_Stack
.Last
loop
6390 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
6391 exit when Is_Concurrent_Type
(Scope_Id
);
6394 -- If it is a return from accept statement it is expanded as call to
6395 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6397 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6398 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6400 if Is_Task_Type
(Scope_Id
) then
6403 Make_Procedure_Call_Statement
(Loc
,
6404 Name
=> New_Occurrence_Of
(RTE
(RE_Complete_Rendezvous
), Loc
));
6405 Insert_Before
(N
, Call
);
6406 -- why not insert actions here???
6409 Acc_Stat
:= Parent
(N
);
6410 while Nkind
(Acc_Stat
) /= N_Accept_Statement
loop
6411 Acc_Stat
:= Parent
(Acc_Stat
);
6414 Lab_Node
:= Last
(Statements
6415 (Handled_Statement_Sequence
(Acc_Stat
)));
6417 Goto_Stat
:= Make_Goto_Statement
(Loc
,
6418 Name
=> New_Occurrence_Of
6419 (Entity
(Identifier
(Lab_Node
)), Loc
));
6421 Set_Analyzed
(Goto_Stat
);
6423 Rewrite
(N
, Goto_Stat
);
6426 -- If it is a return from an entry body, put a Complete_Entry_Body call
6427 -- in front of the return.
6429 elsif Is_Protected_Type
(Scope_Id
) then
6431 Make_Procedure_Call_Statement
(Loc
,
6433 New_Occurrence_Of
(RTE
(RE_Complete_Entry_Body
), Loc
),
6434 Parameter_Associations
=> New_List
(
6435 Make_Attribute_Reference
(Loc
,
6438 (Find_Protection_Object
(Current_Scope
), Loc
),
6439 Attribute_Name
=> Name_Unchecked_Access
)));
6441 Insert_Before
(N
, Call
);
6444 end Expand_Non_Function_Return
;
6446 ---------------------------------------
6447 -- Expand_Protected_Object_Reference --
6448 ---------------------------------------
6450 function Expand_Protected_Object_Reference
6452 Scop
: Entity_Id
) return Node_Id
6454 Loc
: constant Source_Ptr
:= Sloc
(N
);
6461 Rec
:= Make_Identifier
(Loc
, Name_uObject
);
6462 Set_Etype
(Rec
, Corresponding_Record_Type
(Scop
));
6464 -- Find enclosing protected operation, and retrieve its first parameter,
6465 -- which denotes the enclosing protected object. If the enclosing
6466 -- operation is an entry, we are immediately within the protected body,
6467 -- and we can retrieve the object from the service entries procedure. A
6468 -- barrier function has the same signature as an entry. A barrier
6469 -- function is compiled within the protected object, but unlike
6470 -- protected operations its never needs locks, so that its protected
6471 -- body subprogram points to itself.
6473 Proc
:= Current_Scope
;
6474 while Present
(Proc
) and then Scope
(Proc
) /= Scop
loop
6475 Proc
:= Scope
(Proc
);
6476 if Is_Subprogram
(Proc
)
6477 and then Present
(Protected_Subprogram
(Proc
))
6479 Proc
:= Protected_Subprogram
(Proc
);
6483 Corr
:= Protected_Body_Subprogram
(Proc
);
6487 -- Previous error left expansion incomplete.
6488 -- Nothing to do on this call.
6495 (First
(Parameter_Specifications
(Parent
(Corr
))));
6497 if Is_Subprogram
(Proc
) and then Proc
/= Corr
then
6499 -- Protected function or procedure
6501 Set_Entity
(Rec
, Param
);
6503 -- Rec is a reference to an entity which will not be in scope when
6504 -- the call is reanalyzed, and needs no further analysis.
6509 -- Entry or barrier function for entry body. The first parameter of
6510 -- the entry body procedure is pointer to the object. We create a
6511 -- local variable of the proper type, duplicating what is done to
6512 -- define _object later on.
6516 Obj_Ptr
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
6520 Make_Full_Type_Declaration
(Loc
,
6521 Defining_Identifier
=> Obj_Ptr
,
6523 Make_Access_To_Object_Definition
(Loc
,
6524 Subtype_Indication
=>
6526 (Corresponding_Record_Type
(Scop
), Loc
))));
6528 Insert_Actions
(N
, Decls
);
6529 Freeze_Before
(N
, Obj_Ptr
);
6532 Make_Explicit_Dereference
(Loc
,
6534 Unchecked_Convert_To
(Obj_Ptr
,
6535 New_Occurrence_Of
(Param
, Loc
)));
6537 -- Analyze new actual. Other actuals in calls are already analyzed
6538 -- and the list of actuals is not reanalyzed after rewriting.
6540 Set_Parent
(Rec
, N
);
6546 end Expand_Protected_Object_Reference
;
6548 --------------------------------------
6549 -- Expand_Protected_Subprogram_Call --
6550 --------------------------------------
6552 procedure Expand_Protected_Subprogram_Call
6559 procedure Expand_Internal_Init_Call
;
6560 -- A call to an operation of the type may occur in the initialization
6561 -- of a private component. In that case the prefix of the call is an
6562 -- entity name and the call is treated as internal even though it
6563 -- appears in code outside of the protected type.
6565 procedure Freeze_Called_Function
;
6566 -- If it is a function call it can appear in elaboration code and
6567 -- the called entity must be frozen before the call. This must be
6568 -- done before the call is expanded, as the expansion may rewrite it
6569 -- to something other than a call (e.g. a temporary initialized in a
6570 -- transient block).
6572 -------------------------------
6573 -- Expand_Internal_Init_Call --
6574 -------------------------------
6576 procedure Expand_Internal_Init_Call
is
6578 -- If the context is a protected object (rather than a protected
6579 -- type) the call itself is bound to raise program_error because
6580 -- the protected body will not have been elaborated yet. This is
6581 -- diagnosed subsequently in Sem_Elab.
6583 Freeze_Called_Function
;
6585 -- The target of the internal call is the first formal of the
6586 -- enclosing initialization procedure.
6588 Rec
:= New_Occurrence_Of
(First_Formal
(Current_Scope
), Sloc
(N
));
6589 Build_Protected_Subprogram_Call
(N
,
6594 Resolve
(N
, Etype
(Subp
));
6595 end Expand_Internal_Init_Call
;
6597 ----------------------------
6598 -- Freeze_Called_Function --
6599 ----------------------------
6601 procedure Freeze_Called_Function
is
6603 if Ekind
(Subp
) = E_Function
then
6604 Freeze_Expression
(Name
(N
));
6606 end Freeze_Called_Function
;
6608 -- Start of processing for Expand_Protected_Subprogram_Call
6611 -- If the protected object is not an enclosing scope, this is an inter-
6612 -- object function call. Inter-object procedure calls are expanded by
6613 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
6614 -- subprogram being called is in the protected body being compiled, and
6615 -- if the protected object in the call is statically the enclosing type.
6616 -- The object may be a component of some other data structure, in which
6617 -- case this must be handled as an inter-object call.
6619 if not Scope_Within_Or_Same
(Inner
=> Current_Scope
, Outer
=> Scop
)
6620 or else Is_Entry_Wrapper
(Current_Scope
)
6621 or else not Is_Entity_Name
(Name
(N
))
6623 if Nkind
(Name
(N
)) = N_Selected_Component
then
6624 Rec
:= Prefix
(Name
(N
));
6626 elsif Nkind
(Name
(N
)) = N_Indexed_Component
then
6627 Rec
:= Prefix
(Prefix
(Name
(N
)));
6629 -- If this is a call within an entry wrapper, it appears within a
6630 -- precondition that calls another primitive of the synchronized
6631 -- type. The target object of the call is the first actual on the
6632 -- wrapper. Note that this is an external call, because the wrapper
6633 -- is called outside of the synchronized object. This means that
6634 -- an entry call to an entry with preconditions involves two
6635 -- synchronized operations.
6637 elsif Ekind
(Current_Scope
) = E_Procedure
6638 and then Is_Entry_Wrapper
(Current_Scope
)
6640 Rec
:= New_Occurrence_Of
(First_Entity
(Current_Scope
), Sloc
(N
));
6642 -- A default parameter of a protected operation may be a call to
6643 -- a protected function of the type. This appears as an internal
6644 -- call in the profile of the operation, but if the context is an
6645 -- external call we must convert the call into an external one,
6646 -- using the protected object that is the target, so that:
6649 -- is transformed into
6652 elsif Nkind
(Parent
(N
)) = N_Procedure_Call_Statement
6653 and then Nkind
(Name
(Parent
(N
))) = N_Selected_Component
6654 and then Is_Protected_Type
(Etype
(Prefix
(Name
(Parent
(N
)))))
6655 and then Is_Entity_Name
(Name
(N
))
6656 and then Scope
(Entity
(Name
(N
))) =
6657 Etype
(Prefix
(Name
(Parent
(N
))))
6660 Make_Selected_Component
(Sloc
(N
),
6661 Prefix
=> New_Copy_Tree
(Prefix
(Name
(Parent
(N
)))),
6662 Selector_Name
=> Relocate_Node
(Name
(N
))));
6664 Analyze_And_Resolve
(N
);
6668 -- If the context is the initialization procedure for a protected
6669 -- type, the call is legal because the called entity must be a
6670 -- function of that enclosing type, and this is treated as an
6674 (Is_Entity_Name
(Name
(N
)) and then Inside_Init_Proc
);
6676 Expand_Internal_Init_Call
;
6680 Freeze_Called_Function
;
6681 Build_Protected_Subprogram_Call
(N
,
6682 Name
=> New_Occurrence_Of
(Subp
, Sloc
(N
)),
6683 Rec
=> Convert_Concurrent
(Rec
, Etype
(Rec
)),
6687 Rec
:= Expand_Protected_Object_Reference
(N
, Scop
);
6693 Freeze_Called_Function
;
6694 Build_Protected_Subprogram_Call
(N
,
6700 -- Analyze and resolve the new call. The actuals have already been
6701 -- resolved, but expansion of a function call will add extra actuals
6702 -- if needed. Analysis of a procedure call already includes resolution.
6706 if Ekind
(Subp
) = E_Function
then
6707 Resolve
(N
, Etype
(Subp
));
6709 end Expand_Protected_Subprogram_Call
;
6711 -----------------------------------
6712 -- Expand_Simple_Function_Return --
6713 -----------------------------------
6715 -- The "simple" comes from the syntax rule simple_return_statement. The
6716 -- semantics are not at all simple.
6718 procedure Expand_Simple_Function_Return
(N
: Node_Id
) is
6719 Loc
: constant Source_Ptr
:= Sloc
(N
);
6721 Scope_Id
: constant Entity_Id
:=
6722 Return_Applies_To
(Return_Statement_Entity
(N
));
6723 -- The function we are returning from
6725 R_Type
: constant Entity_Id
:= Etype
(Scope_Id
);
6726 -- The result type of the function
6728 Utyp
: constant Entity_Id
:= Underlying_Type
(R_Type
);
6729 -- The underlying result type of the function
6731 Exp
: Node_Id
:= Expression
(N
);
6732 pragma Assert
(Present
(Exp
));
6734 Exp_Is_Function_Call
: constant Boolean :=
6735 Nkind
(Exp
) = N_Function_Call
6737 (Is_Captured_Function_Call
(Exp
)
6738 and then Is_Related_To_Func_Return
(Entity
(Prefix
(Exp
))));
6739 -- If the expression is a captured function call, then we need to make
6740 -- sure that the object doing the capture is properly recognized by the
6741 -- Is_Related_To_Func_Return predicate; otherwise, if it is of a type
6742 -- that needs finalization, Requires_Cleanup_Actions would return true
6743 -- because of this and Build_Finalizer would finalize it prematurely.
6745 Exp_Typ
: constant Entity_Id
:= Etype
(Exp
);
6746 -- The type of the expression (not necessarily the same as R_Type)
6748 Subtype_Ind
: Node_Id
;
6749 -- If the result type of the function is class-wide and the expression
6750 -- has a specific type, then we use the expression's type as the type of
6751 -- the return object. In cases where the expression is an aggregate that
6752 -- is built in place, this avoids the need for an expensive conversion
6753 -- of the return object to the specific type on assignments to the
6754 -- individual components.
6756 -- Start of processing for Expand_Simple_Function_Return
6759 if Is_Class_Wide_Type
(R_Type
)
6760 and then not Is_Class_Wide_Type
(Exp_Typ
)
6761 and then Nkind
(Exp
) /= N_Type_Conversion
6763 Subtype_Ind
:= New_Occurrence_Of
(Exp_Typ
, Loc
);
6765 Subtype_Ind
:= New_Occurrence_Of
(R_Type
, Loc
);
6767 -- If the result type is class-wide and the expression is a view
6768 -- conversion, the conversion plays no role in the expansion because
6769 -- it does not modify the tag of the object. Remove the conversion
6770 -- altogether to prevent tag overwriting.
6772 if Is_Class_Wide_Type
(R_Type
)
6773 and then not Is_Class_Wide_Type
(Exp_Typ
)
6774 and then Nkind
(Exp
) = N_Type_Conversion
6776 Exp
:= Expression
(Exp
);
6780 -- Assert that if F says "return G(...);"
6781 -- then F and G are both b-i-p, or neither b-i-p.
6783 if Nkind
(Exp
) = N_Function_Call
then
6784 pragma Assert
(Ekind
(Scope_Id
) = E_Function
);
6786 (Is_Build_In_Place_Function
(Scope_Id
) =
6787 Is_True_Build_In_Place_Function_Call
(Exp
));
6791 -- For the case of a simple return that does not come from an
6792 -- extended return, in the case of build-in-place, we rewrite
6793 -- "return <expression>;" to be:
6795 -- return _anon_ : <return_subtype> := <expression>
6797 -- The expansion produced by Expand_N_Extended_Return_Statement will
6798 -- contain simple return statements (for example, a block containing
6799 -- simple return of the return object), which brings us back here with
6800 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
6801 -- checking for a simple return that does not come from an extended
6802 -- return is to avoid this infinite recursion.
6804 -- The reason for this design is that for Ada 2005 limited returns, we
6805 -- need to reify the return object, so we can build it "in place", and
6806 -- we need a block statement to hang finalization and tasking stuff.
6809 (Comes_From_Extended_Return_Statement
(N
)
6810 or else not Is_True_Build_In_Place_Function_Call
(Exp
)
6811 or else Has_BIP_Formals
(Scope_Id
));
6813 if not Comes_From_Extended_Return_Statement
(N
)
6814 and then Is_Build_In_Place_Function
(Scope_Id
)
6816 -- The functionality of interface thunks is simple and it is always
6817 -- handled by means of simple return statements. This leaves their
6818 -- expansion simple and clean.
6820 and then not Is_Thunk
(Scope_Id
)
6823 Return_Object_Entity
: constant Entity_Id
:=
6824 Make_Temporary
(Loc
, 'R', Exp
);
6826 Obj_Decl
: constant Node_Id
:=
6827 Make_Object_Declaration
(Loc
,
6828 Defining_Identifier
=> Return_Object_Entity
,
6829 Object_Definition
=> Subtype_Ind
,
6832 Ext
: constant Node_Id
:=
6833 Make_Extended_Return_Statement
(Loc
,
6834 Return_Object_Declarations
=> New_List
(Obj_Decl
));
6835 -- Do not perform this high-level optimization if the result type
6836 -- is an interface because the "this" pointer must be displaced.
6845 -- Here we have a simple return statement that is part of the expansion
6846 -- of an extended return statement (either written by the user, or
6847 -- generated by the above code).
6849 -- Always normalize C/Fortran boolean result. This is not always needed,
6850 -- but it seems a good idea to minimize the passing around of non-
6851 -- normalized values, and in any case this handles the processing of
6852 -- barrier functions for protected types, which turn the condition into
6853 -- a return statement.
6855 if Is_Boolean_Type
(Exp_Typ
) and then Nonzero_Is_True
(Exp_Typ
) then
6856 Adjust_Condition
(Exp
);
6857 Adjust_Result_Type
(Exp
, Exp_Typ
);
6859 -- The adjustment of the expression may have rewritten the return
6860 -- statement itself, e.g. when it is turned into an if expression.
6862 if Nkind
(N
) /= N_Simple_Return_Statement
then
6867 -- Do validity check if enabled for returns
6869 if Validity_Checks_On
and then Validity_Check_Returns
then
6873 -- Check the result expression of a scalar function against the subtype
6874 -- of the function by inserting a conversion. This conversion must
6875 -- eventually be performed for other classes of types, but for now it's
6876 -- only done for scalars ???
6878 if Is_Scalar_Type
(Exp_Typ
) and then Exp_Typ
/= R_Type
then
6879 Rewrite
(Exp
, Convert_To
(R_Type
, Exp
));
6881 -- The expression is resolved to ensure that the conversion gets
6882 -- expanded to generate a possible constraint check.
6884 Analyze_And_Resolve
(Exp
, R_Type
);
6887 -- Deal with returning variable length objects and controlled types
6889 -- Nothing to do if we are returning by reference
6891 if Is_Build_In_Place_Function
(Scope_Id
) then
6892 -- Prevent the reclamation of the secondary stack by all enclosing
6893 -- blocks and loops as well as the related function; otherwise the
6894 -- result would be reclaimed too early.
6896 if Needs_BIP_Alloc_Form
(Scope_Id
) then
6897 Set_Enclosing_Sec_Stack_Return
(N
);
6900 elsif Is_Limited_View
(R_Type
) then
6903 -- No copy needed for thunks returning interface type objects since
6904 -- the object is returned by reference and the maximum functionality
6905 -- required is just to displace the pointer.
6907 elsif Is_Thunk
(Scope_Id
) and then Is_Interface
(Exp_Typ
) then
6910 -- If the call is within a thunk and the type is a limited view, the
6911 -- back end will eventually see the non-limited view of the type.
6913 elsif Is_Thunk
(Scope_Id
) and then Is_Incomplete_Type
(Exp_Typ
) then
6916 -- A return statement from an ignored Ghost function does not use the
6917 -- secondary stack (or any other one).
6919 elsif (not Needs_Secondary_Stack
(R_Type
)
6920 and then not Is_Secondary_Stack_Thunk
(Scope_Id
))
6921 or else Is_Ignored_Ghost_Entity
(Scope_Id
)
6923 -- Mutable records with variable-length components are not returned
6924 -- on the sec-stack, so we need to make sure that the back end will
6925 -- only copy back the size of the actual value, and not the maximum
6926 -- size. We create an actual subtype for this purpose. However we
6927 -- need not do it if the expression is a function call since this
6928 -- will be done in the called function and doing it here too would
6929 -- cause a temporary with maximum size to be created. Likewise for
6930 -- a special return object, since there is no copy in this case.
6933 Ubt
: constant Entity_Id
:= Underlying_Type
(Base_Type
(Exp_Typ
));
6938 if not Exp_Is_Function_Call
6939 and then not (Is_Entity_Name
(Exp
)
6940 and then Is_Special_Return_Object
(Entity
(Exp
)))
6941 and then Has_Defaulted_Discriminants
(Ubt
)
6942 and then not Is_Constrained
(Ubt
)
6943 and then not Has_Unchecked_Union
(Ubt
)
6945 Decl
:= Build_Actual_Subtype
(Ubt
, Exp
);
6946 Ent
:= Defining_Identifier
(Decl
);
6947 Insert_Action
(Exp
, Decl
);
6948 Rewrite
(Exp
, Unchecked_Convert_To
(Ent
, Exp
));
6949 Analyze_And_Resolve
(Exp
);
6953 -- For types which need finalization, do the allocation on the return
6954 -- stack manually in order to call Adjust at the right time:
6956 -- type Ann is access R_Type;
6957 -- for Ann'Storage_pool use rs_pool;
6958 -- Rnn : constant Ann := new Exp_Typ'(Exp);
6961 -- but optimize the case where the result is a function call that
6962 -- also needs finalization. In this case the result can directly be
6963 -- allocated on the return stack of the caller and no further
6964 -- processing is required. Likewise if this is a return object.
6966 if Comes_From_Extended_Return_Statement
(N
) then
6969 elsif Present
(Utyp
)
6970 and then Needs_Finalization
(Utyp
)
6971 and then not (Exp_Is_Function_Call
6972 and then Needs_Finalization
(Exp_Typ
))
6975 Acc_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
6977 Alloc_Node
: Node_Id
;
6981 Mutate_Ekind
(Acc_Typ
, E_Access_Type
);
6983 Set_Associated_Storage_Pool
(Acc_Typ
, RTE
(RE_RS_Pool
));
6985 -- This is an allocator for the return stack, and it's fine
6986 -- to have Comes_From_Source set False on it, as gigi knows not
6987 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6990 Make_Allocator
(Loc
,
6992 Make_Qualified_Expression
(Loc
,
6993 Subtype_Mark
=> New_Occurrence_Of
(Exp_Typ
, Loc
),
6994 Expression
=> Relocate_Node
(Exp
)));
6996 -- We do not want discriminant checks on the declaration,
6997 -- given that it gets its value from the allocator.
6999 Set_No_Initialization
(Alloc_Node
);
7001 Temp
:= Make_Temporary
(Loc
, 'R', Alloc_Node
);
7003 Insert_Actions
(Exp
, New_List
(
7004 Make_Full_Type_Declaration
(Loc
,
7005 Defining_Identifier
=> Acc_Typ
,
7007 Make_Access_To_Object_Definition
(Loc
,
7008 Subtype_Indication
=> Subtype_Ind
)),
7010 Make_Object_Declaration
(Loc
,
7011 Defining_Identifier
=> Temp
,
7012 Constant_Present
=> True,
7013 Object_Definition
=> New_Occurrence_Of
(Acc_Typ
, Loc
),
7014 Expression
=> Alloc_Node
)));
7017 Make_Explicit_Dereference
(Loc
,
7018 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
7020 Analyze_And_Resolve
(Exp
, R_Type
);
7024 -- Here if secondary stack is used
7027 -- Prevent the reclamation of the secondary stack by all enclosing
7028 -- blocks and loops as well as the related function; otherwise the
7029 -- result would be reclaimed too early.
7031 Set_Enclosing_Sec_Stack_Return
(N
);
7033 -- Nothing else to do for a return object
7035 if Comes_From_Extended_Return_Statement
(N
) then
7038 -- Optimize the case where the result is a function call that also
7039 -- returns on the secondary stack; in this case the result is already
7040 -- on the secondary stack and no further processing is required.
7042 elsif Exp_Is_Function_Call
7043 and then Needs_Secondary_Stack
(Exp_Typ
)
7045 -- Remove side effects from the expression now so that other parts
7046 -- of the expander do not have to reanalyze this node without this
7049 Rewrite
(Exp
, Duplicate_Subexpr_No_Checks
(Exp
));
7051 -- Ada 2005 (AI-251): If the type of the returned object is
7052 -- an interface then add an implicit type conversion to force
7053 -- displacement of the "this" pointer.
7055 if Is_Interface
(R_Type
) then
7056 Rewrite
(Exp
, Convert_To
(R_Type
, Relocate_Node
(Exp
)));
7059 Analyze_And_Resolve
(Exp
, R_Type
);
7061 -- For types which both need finalization and are returned on the
7062 -- secondary stack, do the allocation on secondary stack manually
7063 -- in order to call Adjust at the right time:
7065 -- type Ann is access R_Type;
7066 -- for Ann'Storage_pool use ss_pool;
7067 -- Rnn : constant Ann := new Exp_Typ'(Exp);
7070 -- And we do the same for class-wide types that are not potentially
7071 -- controlled (by the virtue of restriction No_Finalization) because
7072 -- gigi is not able to properly allocate class-wide types.
7074 -- But optimize the case where the result is a function call that
7075 -- also needs finalization; in this case the result can directly be
7076 -- allocated on the secondary stack and no further processing is
7077 -- required, unless the returned object is an interface.
7079 elsif CW_Or_Needs_Finalization
(Utyp
)
7080 and then (Is_Interface
(R_Type
)
7081 or else not (Exp_Is_Function_Call
7082 and then Needs_Finalization
(Exp_Typ
)))
7085 Acc_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
7087 Alloc_Node
: Node_Id
;
7091 Mutate_Ekind
(Acc_Typ
, E_Access_Type
);
7092 Set_Associated_Storage_Pool
(Acc_Typ
, RTE
(RE_SS_Pool
));
7094 -- This is an allocator for the secondary stack, and it's fine
7095 -- to have Comes_From_Source set False on it, as gigi knows not
7096 -- to flag it as a violation of No_Implicit_Heap_Allocations.
7099 Make_Allocator
(Loc
,
7101 Make_Qualified_Expression
(Loc
,
7102 Subtype_Mark
=> New_Occurrence_Of
(Etype
(Exp
), Loc
),
7103 Expression
=> Relocate_Node
(Exp
)));
7105 -- We do not want discriminant checks on the declaration,
7106 -- given that it gets its value from the allocator.
7108 Set_No_Initialization
(Alloc_Node
);
7110 Temp
:= Make_Temporary
(Loc
, 'R', Alloc_Node
);
7112 Insert_Actions
(Exp
, New_List
(
7113 Make_Full_Type_Declaration
(Loc
,
7114 Defining_Identifier
=> Acc_Typ
,
7116 Make_Access_To_Object_Definition
(Loc
,
7117 Subtype_Indication
=> Subtype_Ind
)),
7119 Make_Object_Declaration
(Loc
,
7120 Defining_Identifier
=> Temp
,
7121 Constant_Present
=> True,
7122 Object_Definition
=> New_Occurrence_Of
(Acc_Typ
, Loc
),
7123 Expression
=> Alloc_Node
)));
7126 Make_Explicit_Dereference
(Loc
,
7127 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
7129 -- Ada 2005 (AI-251): If the type of the returned object is
7130 -- an interface then add an implicit type conversion to force
7131 -- displacement of the "this" pointer.
7133 if Is_Interface
(R_Type
) then
7134 Rewrite
(Exp
, Convert_To
(R_Type
, Relocate_Node
(Exp
)));
7137 Analyze_And_Resolve
(Exp
, R_Type
);
7140 -- Otherwise use the gigi mechanism to allocate result on the
7144 Check_Restriction
(No_Secondary_Stack
, N
);
7145 Set_Storage_Pool
(N
, RTE
(RE_SS_Pool
));
7146 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
7150 -- Implement the rules of 6.5(8-10), which require a tag check in
7151 -- the case of a limited tagged return type, and tag reassignment for
7152 -- nonlimited tagged results. These actions are needed when the return
7153 -- type is a specific tagged type and the result expression is a
7154 -- conversion or a formal parameter, because in that case the tag of
7155 -- the expression might differ from the tag of the specific result type.
7157 -- We must also verify an underlying type exists for the return type in
7158 -- case it is incomplete - in which case is not necessary to generate a
7159 -- check anyway since an incomplete limited tagged return type would
7160 -- qualify as a premature usage.
7163 and then Is_Tagged_Type
(Utyp
)
7164 and then not Is_Class_Wide_Type
(Utyp
)
7165 and then (Nkind
(Exp
) in
7166 N_Type_Conversion | N_Unchecked_Type_Conversion
7167 or else (Nkind
(Exp
) = N_Explicit_Dereference
7168 and then Nkind
(Prefix
(Exp
)) in
7170 N_Unchecked_Type_Conversion
)
7171 or else (Is_Entity_Name
(Exp
)
7172 and then Is_Formal
(Entity
(Exp
))))
7174 -- When the return type is limited, perform a check that the tag of
7175 -- the result is the same as the tag of the return type.
7177 if Is_Limited_Type
(R_Type
) then
7179 Make_Raise_Constraint_Error
(Loc
,
7183 Make_Selected_Component
(Loc
,
7184 Prefix
=> Duplicate_Subexpr
(Exp
),
7185 Selector_Name
=> Make_Identifier
(Loc
, Name_uTag
)),
7187 Make_Attribute_Reference
(Loc
,
7189 New_Occurrence_Of
(Base_Type
(Utyp
), Loc
),
7190 Attribute_Name
=> Name_Tag
)),
7191 Reason
=> CE_Tag_Check_Failed
));
7193 -- If the result type is a specific nonlimited tagged type, then we
7194 -- have to ensure that the tag of the result is that of the result
7195 -- type. This is handled by making a copy of the expression in
7196 -- the case where it might have a different tag, namely when the
7197 -- expression is a conversion or a formal parameter. We create a new
7198 -- object of the result type and initialize it from the expression,
7199 -- which will implicitly force the tag to be set appropriately.
7203 ExpR
: constant Node_Id
:= Relocate_Node
(Exp
);
7204 Result_Id
: constant Entity_Id
:=
7205 Make_Temporary
(Loc
, 'R', ExpR
);
7206 Result_Exp
: constant Node_Id
:=
7207 New_Occurrence_Of
(Result_Id
, Loc
);
7208 Result_Obj
: constant Node_Id
:=
7209 Make_Object_Declaration
(Loc
,
7210 Defining_Identifier
=> Result_Id
,
7211 Object_Definition
=>
7212 New_Occurrence_Of
(R_Type
, Loc
),
7213 Constant_Present
=> True,
7214 Expression
=> ExpR
);
7217 Set_Assignment_OK
(Result_Obj
);
7218 Insert_Action
(Exp
, Result_Obj
);
7220 Rewrite
(Exp
, Result_Exp
);
7221 Analyze_And_Resolve
(Exp
, R_Type
);
7225 -- Ada 2005 (AI95-344): If the result type is class-wide, then insert
7226 -- a check that the level of the return expression's underlying type
7227 -- is not deeper than the level of the master enclosing the function.
7229 -- AI12-043: The check is made immediately after the return object is
7230 -- created. This means that we do not apply it to the simple return
7231 -- generated by the expansion of an extended return statement.
7233 -- No runtime check needed in interface thunks since it is performed
7234 -- by the target primitive associated with the thunk.
7236 elsif Is_Class_Wide_Type
(R_Type
)
7237 and then not Comes_From_Extended_Return_Statement
(N
)
7238 and then not Is_Thunk
(Scope_Id
)
7240 Apply_CW_Accessibility_Check
(Exp
, Scope_Id
);
7242 -- Ada 2012 (AI05-0073): If the result subtype of the function is
7243 -- defined by an access_definition designating a specific tagged
7244 -- type T, a check is made that the result value is null or the tag
7245 -- of the object designated by the result value identifies T.
7247 -- The return expression is referenced twice in the code below, so it
7248 -- must be made free of side effects. Given that different compilers
7249 -- may evaluate these parameters in different order, both occurrences
7252 elsif Ekind
(R_Type
) = E_Anonymous_Access_Type
7253 and then Is_Tagged_Type
(Designated_Type
(R_Type
))
7254 and then not Is_Class_Wide_Type
(Designated_Type
(R_Type
))
7255 and then Nkind
(Original_Node
(Exp
)) /= N_Null
7256 and then not Tag_Checks_Suppressed
(Designated_Type
(R_Type
))
7259 -- [Constraint_Error
7261 -- and then Exp.all not in Designated_Type]
7264 Make_Raise_Constraint_Error
(Loc
,
7269 Left_Opnd
=> Duplicate_Subexpr
(Exp
),
7270 Right_Opnd
=> Make_Null
(Loc
)),
7275 Make_Explicit_Dereference
(Loc
,
7276 Prefix
=> Duplicate_Subexpr
(Exp
)),
7278 New_Occurrence_Of
(Designated_Type
(R_Type
), Loc
))),
7280 Reason
=> CE_Tag_Check_Failed
),
7281 Suppress
=> All_Checks
);
7284 -- If the result is of an unconstrained array subtype with fixed lower
7285 -- bound, then sliding to that bound may be needed.
7287 if Is_Fixed_Lower_Bound_Array_Subtype
(R_Type
) then
7288 Expand_Sliding_Conversion
(Exp
, R_Type
);
7291 -- If we are returning a nonscalar object that is possibly unaligned,
7292 -- then copy the value into a temporary first. This copy may need to
7293 -- expand to a loop of component operations.
7295 if Is_Possibly_Unaligned_Slice
(Exp
)
7296 or else (Is_Possibly_Unaligned_Object
(Exp
)
7297 and then not Represented_As_Scalar
(Etype
(Exp
)))
7300 ExpR
: constant Node_Id
:= Relocate_Node
(Exp
);
7301 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', ExpR
);
7304 Make_Object_Declaration
(Loc
,
7305 Defining_Identifier
=> Tnn
,
7306 Constant_Present
=> True,
7307 Object_Definition
=> New_Occurrence_Of
(R_Type
, Loc
),
7308 Expression
=> ExpR
),
7309 Suppress
=> All_Checks
);
7310 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
7314 -- Ada 2005 (AI-251): If this return statement corresponds with an
7315 -- simple return statement associated with an extended return statement
7316 -- and the type of the returned object is an interface then generate an
7317 -- implicit conversion to force displacement of the "this" pointer.
7319 if Ada_Version
>= Ada_2005
7320 and then Comes_From_Extended_Return_Statement
(N
)
7321 and then Nkind
(Expression
(N
)) = N_Identifier
7322 and then Is_Interface
(Utyp
)
7323 and then Utyp
/= Underlying_Type
(Exp_Typ
)
7325 Rewrite
(Exp
, Convert_To
(Utyp
, Relocate_Node
(Exp
)));
7326 Analyze_And_Resolve
(Exp
);
7329 -- Ada 2022 (AI12-0279)
7331 if Has_Yield_Aspect
(Scope_Id
)
7332 and then RTE_Available
(RE_Yield
)
7335 Make_Procedure_Call_Statement
(Loc
,
7336 New_Occurrence_Of
(RTE
(RE_Yield
), Loc
)));
7338 end Expand_Simple_Function_Return
;
7340 -----------------------
7341 -- Freeze_Subprogram --
7342 -----------------------
7344 procedure Freeze_Subprogram
(N
: Node_Id
) is
7345 Loc
: constant Source_Ptr
:= Sloc
(N
);
7346 Subp
: constant Entity_Id
:= Entity
(N
);
7349 -- We suppress the initialization of the dispatch table entry when
7350 -- not Tagged_Type_Expansion because the dispatching mechanism is
7351 -- handled internally by the target.
7353 if Is_Dispatching_Operation
(Subp
)
7354 and then not Is_Abstract_Subprogram
(Subp
)
7355 and then Present
(DTC_Entity
(Subp
))
7356 and then Present
(Scope
(DTC_Entity
(Subp
)))
7357 and then Tagged_Type_Expansion
7358 and then not Restriction_Active
(No_Dispatching_Calls
)
7359 and then RTE_Available
(RE_Tag
)
7362 Typ
: constant Entity_Id
:= Scope
(DTC_Entity
(Subp
));
7367 -- Handle private overridden primitives
7369 if not Is_CPP_Class
(Typ
) then
7370 Check_Overriding_Operation
(Subp
);
7373 -- We assume that imported CPP primitives correspond with objects
7374 -- whose constructor is in the CPP side; therefore we don't need
7375 -- to generate code to register them in the dispatch table.
7377 if Is_CPP_Class
(Typ
) then
7380 -- Handle CPP primitives found in derivations of CPP_Class types.
7381 -- These primitives must have been inherited from some parent, and
7382 -- there is no need to register them in the dispatch table because
7383 -- Build_Inherit_Prims takes care of initializing these slots.
7385 elsif Is_Imported
(Subp
)
7386 and then Convention
(Subp
) in Convention_C_Family
7390 -- Generate code to register the primitive in non statically
7391 -- allocated dispatch tables
7393 elsif not Building_Static_DT
(Scope
(DTC_Entity
(Subp
))) then
7395 -- When a primitive is frozen, enter its name in its dispatch
7398 if not Is_Interface
(Typ
)
7399 or else Present
(Interface_Alias
(Subp
))
7401 if Is_Predefined_Dispatching_Operation
(Subp
) then
7402 L
:= Register_Predefined_Primitive
(Loc
, Subp
);
7407 Append_List_To
(L
, Register_Primitive
(Loc
, Subp
));
7409 if Is_Empty_List
(L
) then
7412 elsif No
(Actions
(N
)) then
7416 Append_List
(L
, Actions
(N
));
7423 -- Mark functions that return by reference. Note that it cannot be part
7424 -- of the normal semantic analysis of the spec since the underlying
7425 -- returned type may not be known yet (for private types).
7427 Compute_Returns_By_Ref
(Subp
);
7428 end Freeze_Subprogram
;
7430 --------------------------
7431 -- Has_BIP_Extra_Formal --
7432 --------------------------
7434 function Has_BIP_Extra_Formal
7436 Kind
: BIP_Formal_Kind
;
7437 Must_Be_Frozen
: Boolean := True) return Boolean
7439 Extra_Formal
: Entity_Id
:= Extra_Formals
(E
);
7442 -- We can only rely on the availability of the extra formals in frozen
7443 -- entities or in subprogram types of dispatching calls (since their
7444 -- extra formals are added when the target subprogram is frozen; see
7445 -- Expand_Dispatching_Call).
7447 pragma Assert
((Is_Frozen
(E
) or else not Must_Be_Frozen
)
7448 or else (Ekind
(E
) = E_Subprogram_Type
7449 and then Is_Dispatch_Table_Entity
(E
))
7450 or else (Is_Dispatching_Operation
(E
)
7451 and then Is_Frozen
(Find_Dispatching_Type
(E
))));
7453 while Present
(Extra_Formal
) loop
7454 if Is_Build_In_Place_Entity
(Extra_Formal
)
7455 and then BIP_Suffix_Kind
(Extra_Formal
) = Kind
7460 Next_Formal_With_Extras
(Extra_Formal
);
7464 end Has_BIP_Extra_Formal
;
7466 ------------------------------
7467 -- Insert_Post_Call_Actions --
7468 ------------------------------
7470 procedure Insert_Post_Call_Actions
(N
: Node_Id
; Post_Call
: List_Id
) is
7471 Context
: constant Node_Id
:= Parent
(N
);
7474 if Is_Empty_List
(Post_Call
) then
7478 -- Cases where the call is not a member of a statement list. This also
7479 -- includes the cases where the call is an actual in another function
7480 -- call, or is an index, or is an operand of an if-expression, i.e. is
7481 -- in an expression context.
7483 if not Is_List_Member
(N
)
7484 or else Nkind
(Context
) in N_Function_Call
7486 | N_Indexed_Component
7488 -- In Ada 2012 the call may be a function call in an expression
7489 -- (since OUT and IN OUT parameters are now allowed for such calls).
7490 -- The write-back of (in)-out parameters is handled by the back-end,
7491 -- but the constraint checks generated when subtypes of formal and
7492 -- actual don't match must be inserted in the form of assignments.
7493 -- Also do this in the case of explicit dereferences, which can occur
7494 -- due to rewritings of function calls with controlled results.
7496 if Nkind
(N
) = N_Function_Call
7497 or else Nkind
(Original_Node
(N
)) = N_Function_Call
7498 or else Nkind
(N
) = N_Explicit_Dereference
7500 pragma Assert
(Ada_Version
>= Ada_2012
);
7501 -- Functions with '[in] out' parameters are only allowed in Ada
7504 -- We used to handle this by climbing up parents to a
7505 -- non-statement/declaration and then simply making a call to
7506 -- Insert_Actions_After (P, Post_Call), but that doesn't work
7507 -- for Ada 2012. If we are in the middle of an expression, e.g.
7508 -- the condition of an IF, this call would insert after the IF
7509 -- statement, which is much too late to be doing the write back.
7512 -- if Clobber (X) then
7513 -- Put_Line (X'Img);
7518 -- Now assume Clobber changes X, if we put the write back after
7519 -- the IF, the Put_Line gets the wrong value and the goto causes
7520 -- the write back to be skipped completely.
7522 -- To deal with this, we replace the call by
7525 -- Tnnn : constant function-result-type := function-call;
7526 -- Post_Call actions
7531 -- However, that doesn't work if function-result-type requires
7532 -- finalization (because function-call's result never gets
7533 -- finalized). So in that case, we instead replace the call by
7536 -- type Ref is access all function-result-type;
7537 -- Ptr : constant Ref := function-call'Reference;
7538 -- Tnnn : constant function-result-type := Ptr.all;
7539 -- Finalize (Ptr.all);
7540 -- Post_Call actions
7547 Loc
: constant Source_Ptr
:= Sloc
(N
);
7548 Tnnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
7549 FRTyp
: constant Entity_Id
:= Etype
(N
);
7550 Name
: constant Node_Id
:= Relocate_Node
(N
);
7553 if Needs_Finalization
(FRTyp
) then
7555 Ptr_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
7557 Ptr_Typ_Decl
: constant Node_Id
:=
7558 Make_Full_Type_Declaration
(Loc
,
7559 Defining_Identifier
=> Ptr_Typ
,
7561 Make_Access_To_Object_Definition
(Loc
,
7562 All_Present
=> True,
7563 Subtype_Indication
=>
7564 New_Occurrence_Of
(FRTyp
, Loc
)));
7566 Ptr_Obj
: constant Entity_Id
:=
7567 Make_Temporary
(Loc
, 'P');
7569 Ptr_Obj_Decl
: constant Node_Id
:=
7570 Make_Object_Declaration
(Loc
,
7571 Defining_Identifier
=> Ptr_Obj
,
7572 Object_Definition
=>
7573 New_Occurrence_Of
(Ptr_Typ
, Loc
),
7574 Constant_Present
=> True,
7576 Make_Attribute_Reference
(Loc
,
7578 Attribute_Name
=> Name_Unrestricted_Access
));
7580 function Ptr_Dereference
return Node_Id
is
7581 (Make_Explicit_Dereference
(Loc
,
7582 Prefix
=> New_Occurrence_Of
(Ptr_Obj
, Loc
)));
7584 Tnn_Decl
: constant Node_Id
:=
7585 Make_Object_Declaration
(Loc
,
7586 Defining_Identifier
=> Tnnn
,
7587 Object_Definition
=> New_Occurrence_Of
(FRTyp
, Loc
),
7588 Constant_Present
=> True,
7589 Expression
=> Ptr_Dereference
);
7591 Finalize_Call
: constant Node_Id
:=
7593 (Obj_Ref
=> Ptr_Dereference
, Typ
=> FRTyp
);
7595 -- Prepend in reverse order
7597 Prepend_To
(Post_Call
, Finalize_Call
);
7598 Prepend_To
(Post_Call
, Tnn_Decl
);
7599 Prepend_To
(Post_Call
, Ptr_Obj_Decl
);
7600 Prepend_To
(Post_Call
, Ptr_Typ_Decl
);
7603 Prepend_To
(Post_Call
,
7604 Make_Object_Declaration
(Loc
,
7605 Defining_Identifier
=> Tnnn
,
7606 Object_Definition
=> New_Occurrence_Of
(FRTyp
, Loc
),
7607 Constant_Present
=> True,
7608 Expression
=> Name
));
7612 Make_Expression_With_Actions
(Loc
,
7613 Actions
=> Post_Call
,
7614 Expression
=> New_Occurrence_Of
(Tnnn
, Loc
)));
7616 -- We don't want to just blindly call Analyze_And_Resolve
7617 -- because that would cause unwanted recursion on the call.
7618 -- So for a moment set the call as analyzed to prevent that
7619 -- recursion, and get the rest analyzed properly, then reset
7620 -- the analyzed flag, so our caller can continue.
7622 Set_Analyzed
(Name
, True);
7623 Analyze_And_Resolve
(N
, FRTyp
);
7624 Set_Analyzed
(Name
, False);
7627 -- If not the special Ada 2012 case of a function call, then we must
7628 -- have the triggering statement of a triggering alternative or an
7629 -- entry call alternative, and we can add the post call stuff to the
7630 -- corresponding statement list.
7633 pragma Assert
(Nkind
(Context
) in N_Entry_Call_Alternative
7634 | N_Triggering_Alternative
);
7636 if Is_Non_Empty_List
(Statements
(Context
)) then
7637 Insert_List_Before_And_Analyze
7638 (First
(Statements
(Context
)), Post_Call
);
7640 Set_Statements
(Context
, Post_Call
);
7644 -- A procedure call is always part of a declarative or statement list,
7645 -- however a function call may appear nested within a construct. Most
7646 -- cases of function call nesting are handled in the special case above.
7647 -- The only exception is when the function call acts as an actual in a
7648 -- procedure call. In this case the function call is in a list, but the
7649 -- post-call actions must be inserted after the procedure call.
7650 -- What if the function call is an aggregate component ???
7652 elsif Nkind
(Context
) = N_Procedure_Call_Statement
then
7653 Insert_Actions_After
(Context
, Post_Call
);
7655 -- Otherwise, normal case where N is in a statement sequence, just put
7656 -- the post-call stuff after the call statement.
7659 Insert_Actions_After
(N
, Post_Call
);
7661 end Insert_Post_Call_Actions
;
7663 ---------------------------------------
7664 -- Install_Class_Preconditions_Check --
7665 ---------------------------------------
7667 procedure Install_Class_Preconditions_Check
(Call_Node
: Node_Id
) is
7668 Loc
: constant Source_Ptr
:= Sloc
(Call_Node
);
7670 function Build_Dynamic_Check_Helper_Call
return Node_Id
;
7671 -- Build call to the helper runtime function of the nearest ancestor
7672 -- of the target subprogram that dynamically evaluates the merged
7673 -- or-else preconditions.
7675 function Build_Error_Message
(Subp_Id
: Entity_Id
) return Node_Id
;
7676 -- Build message associated with the class-wide precondition of Subp_Id
7677 -- indicating the call that caused it.
7679 function Build_Static_Check_Helper_Call
return Node_Id
;
7680 -- Build call to the helper runtime function of the nearest ancestor
7681 -- of the target subprogram that dynamically evaluates the merged
7682 -- or-else preconditions.
7684 function Class_Preconditions_Subprogram
7685 (Spec_Id
: Entity_Id
;
7686 Dynamic
: Boolean) return Node_Id
;
7687 -- Return the nearest ancestor of Spec_Id defining a helper function
7688 -- that evaluates a combined or-else expression containing all the
7689 -- inherited class-wide preconditions; Dynamic enables searching for
7690 -- the helper that dynamically evaluates preconditions using dispatching
7691 -- calls; if False it searches for the helper that statically evaluates
7692 -- preconditions; return Empty when not available (which means that no
7693 -- preconditions check is required).
7695 -------------------------------------
7696 -- Build_Dynamic_Check_Helper_Call --
7697 -------------------------------------
7699 function Build_Dynamic_Check_Helper_Call
return Node_Id
is
7700 Spec_Id
: constant Entity_Id
:= Entity
(Name
(Call_Node
));
7701 CW_Subp
: constant Entity_Id
:=
7702 Class_Preconditions_Subprogram
(Spec_Id
,
7704 Helper_Id
: constant Entity_Id
:=
7705 Dynamic_Call_Helper
(CW_Subp
);
7706 Actuals
: constant List_Id
:= New_List
;
7707 A
: Node_Id
:= First_Actual
(Call_Node
);
7708 F
: Entity_Id
:= First_Formal
(Helper_Id
);
7711 while Present
(A
) loop
7713 -- Ensure that the evaluation of the actuals will not produce
7716 Remove_Side_Effects
(A
);
7718 Append_To
(Actuals
, New_Copy_Tree
(A
));
7724 Make_Function_Call
(Loc
,
7725 Name
=> New_Occurrence_Of
(Helper_Id
, Loc
),
7726 Parameter_Associations
=> Actuals
);
7727 end Build_Dynamic_Check_Helper_Call
;
7729 -------------------------
7730 -- Build_Error_Message --
7731 -------------------------
7733 function Build_Error_Message
(Subp_Id
: Entity_Id
) return Node_Id
is
7735 procedure Append_Message
7737 Is_First
: in out Boolean);
7738 -- Build the fragment of the message associated with subprogram Id;
7739 -- Is_First facilitates identifying continuation messages.
7741 --------------------
7742 -- Append_Message --
7743 --------------------
7745 procedure Append_Message
7747 Is_First
: in out Boolean)
7749 Prag
: constant Node_Id
:= Get_Class_Wide_Pragma
(Id
,
7750 Pragma_Precondition
);
7755 if No
(Prag
) or else Is_Ignored
(Prag
) then
7759 Msg
:= Expression
(Last
(Pragma_Argument_Associations
(Prag
)));
7760 Str_Id
:= Strval
(Msg
);
7765 Append
(Global_Name_Buffer
, Strval
(Msg
));
7768 and then Name_Buffer
(1 .. 19) = "failed precondition"
7770 Insert_Str_In_Name_Buffer
("inherited ", 8);
7775 Str
: constant String := To_String
(Str_Id
);
7779 Append
(Global_Name_Buffer
, ASCII
.LF
);
7780 Append
(Global_Name_Buffer
, " or ");
7782 From_Idx
:= Name_Len
;
7783 Append
(Global_Name_Buffer
, Str_Id
);
7785 if Str
(1 .. 19) = "failed precondition" then
7786 Insert_Str_In_Name_Buffer
("inherited ", From_Idx
+ 8);
7794 Str_Loc
: constant String := Build_Location_String
(Loc
);
7795 Subps
: constant Subprogram_List
:=
7796 Inherited_Subprograms
(Subp_Id
);
7797 Is_First
: Boolean := True;
7799 -- Start of processing for Build_Error_Message
7803 Append_Message
(Subp_Id
, Is_First
);
7805 for Index
in Subps
'Range loop
7806 Append_Message
(Subps
(Index
), Is_First
);
7809 if Present
(Controlling_Argument
(Call_Node
)) then
7810 Append
(Global_Name_Buffer
, " in dispatching call at ");
7812 Append
(Global_Name_Buffer
, " in call at ");
7815 Append
(Global_Name_Buffer
, Str_Loc
);
7817 return Make_String_Literal
(Loc
, Name_Buffer
(1 .. Name_Len
));
7818 end Build_Error_Message
;
7820 ------------------------------------
7821 -- Build_Static_Check_Helper_Call --
7822 ------------------------------------
7824 function Build_Static_Check_Helper_Call
return Node_Id
is
7825 Actuals
: constant List_Id
:= New_List
;
7827 Helper_Id
: Entity_Id
;
7829 CW_Subp
: Entity_Id
;
7830 Spec_Id
: constant Entity_Id
:= Entity
(Name
(Call_Node
));
7833 -- The target is the wrapper built to support inheriting body but
7834 -- overriding pre/postconditions (AI12-0195).
7836 if Is_Dispatch_Table_Wrapper
(Spec_Id
) then
7842 CW_Subp
:= Class_Preconditions_Subprogram
(Spec_Id
,
7846 Helper_Id
:= Static_Call_Helper
(CW_Subp
);
7848 F
:= First_Formal
(Helper_Id
);
7849 A
:= First_Actual
(Call_Node
);
7850 while Present
(A
) loop
7852 -- Ensure that the evaluation of the actuals will not produce
7855 Remove_Side_Effects
(A
);
7857 -- Ensure matching types to avoid reporting spurious errors since
7858 -- the called helper may have been built for a parent type.
7860 if Etype
(F
) /= Etype
(A
) then
7862 Unchecked_Convert_To
(Etype
(F
), New_Copy_Tree
(A
)));
7864 Append_To
(Actuals
, New_Copy_Tree
(A
));
7872 Make_Function_Call
(Loc
,
7873 Name
=> New_Occurrence_Of
(Helper_Id
, Loc
),
7874 Parameter_Associations
=> Actuals
);
7875 end Build_Static_Check_Helper_Call
;
7877 ------------------------------------
7878 -- Class_Preconditions_Subprogram --
7879 ------------------------------------
7881 function Class_Preconditions_Subprogram
7882 (Spec_Id
: Entity_Id
;
7883 Dynamic
: Boolean) return Node_Id
7885 Subp_Id
: constant Entity_Id
:= Ultimate_Alias
(Spec_Id
);
7888 -- Prevent cascaded errors
7890 if not Is_Dispatching_Operation
(Subp_Id
) then
7893 -- No need to search if this subprogram has the helper we are
7897 if Present
(Dynamic_Call_Helper
(Subp_Id
)) then
7901 if Present
(Static_Call_Helper
(Subp_Id
)) then
7906 -- Process inherited subprograms looking for class-wide
7910 Subps
: constant Subprogram_List
:=
7911 Inherited_Subprograms
(Subp_Id
);
7912 Subp_Id
: Entity_Id
;
7915 for Index
in Subps
'Range loop
7916 Subp_Id
:= Subps
(Index
);
7918 if Present
(Alias
(Subp_Id
)) then
7919 Subp_Id
:= Ultimate_Alias
(Subp_Id
);
7922 -- Wrappers of class-wide pre/postconditions reference the
7923 -- parent primitive that has the inherited contract.
7925 if Is_Wrapper
(Subp_Id
)
7926 and then Present
(LSP_Subprogram
(Subp_Id
))
7928 Subp_Id
:= LSP_Subprogram
(Subp_Id
);
7932 if Present
(Dynamic_Call_Helper
(Subp_Id
)) then
7936 if Present
(Static_Call_Helper
(Subp_Id
)) then
7944 end Class_Preconditions_Subprogram
;
7948 Dynamic_Check
: constant Boolean :=
7949 Present
(Controlling_Argument
(Call_Node
));
7950 Class_Subp
: Entity_Id
;
7954 -- Start of processing for Install_Class_Preconditions_Check
7957 -- Do not expand the check if we are compiling under restriction
7958 -- No_Dispatching_Calls; the semantic analyzer has previously
7959 -- notified the violation of this restriction.
7962 and then Restriction_Active
(No_Dispatching_Calls
)
7966 -- Class-wide precondition check not needed in interface thunks since
7967 -- they are installed in the dispatching call that caused invoking the
7970 elsif Is_Thunk
(Current_Scope
) then
7974 Subp
:= Entity
(Name
(Call_Node
));
7976 -- No check needed for this subprogram call if no class-wide
7977 -- preconditions apply (or if the unique available preconditions
7978 -- are ignored preconditions).
7980 Class_Subp
:= Class_Preconditions_Subprogram
(Subp
, Dynamic_Check
);
7983 or else No
(Class_Preconditions
(Class_Subp
))
7988 -- Build and install the check
7990 if Dynamic_Check
then
7991 Cond
:= Build_Dynamic_Check_Helper_Call
;
7993 Cond
:= Build_Static_Check_Helper_Call
;
7996 if Exception_Locations_Suppressed
then
7997 Insert_Action
(Call_Node
,
7998 Make_If_Statement
(Loc
,
7999 Condition
=> Make_Op_Not
(Loc
, Cond
),
8000 Then_Statements
=> New_List
(
8001 Make_Raise_Statement
(Loc
,
8004 (RTE
(RE_Assert_Failure
), Loc
)))));
8006 -- Failed check with message indicating the failed precondition and the
8007 -- call that caused it.
8010 Insert_Action
(Call_Node
,
8011 Make_If_Statement
(Loc
,
8012 Condition
=> Make_Op_Not
(Loc
, Cond
),
8013 Then_Statements
=> New_List
(
8014 Make_Procedure_Call_Statement
(Loc
,
8017 (RTE
(RE_Raise_Assert_Failure
), Loc
),
8018 Parameter_Associations
=>
8019 New_List
(Build_Error_Message
(Subp
))))));
8021 end Install_Class_Preconditions_Check
;
8023 ------------------------------
8024 -- Is_Build_In_Place_Entity --
8025 ------------------------------
8027 function Is_Build_In_Place_Entity
(E
: Entity_Id
) return Boolean is
8028 Nam
: constant String := Get_Name_String
(Chars
(E
));
8030 function Has_Suffix
(Suffix
: String) return Boolean;
8031 -- Return True if Nam has suffix Suffix
8033 function Has_Suffix
(Suffix
: String) return Boolean is
8034 Len
: constant Natural := Suffix
'Length;
8036 return Nam
'Length > Len
8037 and then Nam
(Nam
'Last - Len
+ 1 .. Nam
'Last) = Suffix
;
8040 -- Start of processing for Is_Build_In_Place_Entity
8043 return Has_Suffix
(BIP_Alloc_Suffix
)
8044 or else Has_Suffix
(BIP_Storage_Pool_Suffix
)
8045 or else Has_Suffix
(BIP_Finalization_Master_Suffix
)
8046 or else Has_Suffix
(BIP_Task_Master_Suffix
)
8047 or else Has_Suffix
(BIP_Activation_Chain_Suffix
)
8048 or else Has_Suffix
(BIP_Object_Access_Suffix
);
8049 end Is_Build_In_Place_Entity
;
8051 --------------------------------
8052 -- Is_Build_In_Place_Function --
8053 --------------------------------
8055 function Is_Build_In_Place_Function
(E
: Entity_Id
) return Boolean is
8056 Kind
: constant Entity_Kind
:= Ekind
(E
);
8057 Typ
: constant Entity_Id
:= Etype
(E
);
8060 -- This function is called from Expand_Subtype_From_Expr during
8061 -- semantic analysis, even when expansion is off. In those cases
8062 -- the build_in_place expansion will not take place.
8064 if not Expander_Active
then
8068 -- We never use build-in-place if the convention is other than Ada,
8069 -- but note that it is OK for a build-in-place function to return a
8070 -- type with a foreign convention because the machinery ensures there
8073 return (Kind
in E_Function | E_Generic_Function
8075 (Kind
= E_Subprogram_Type
and then Typ
/= Standard_Void_Type
))
8076 and then Is_Build_In_Place_Result_Type
(Typ
)
8077 and then not Has_Foreign_Convention
(E
);
8078 end Is_Build_In_Place_Function
;
8080 -------------------------------------
8081 -- Is_Build_In_Place_Function_Call --
8082 -------------------------------------
8084 function Is_Build_In_Place_Function_Call
(N
: Node_Id
) return Boolean is
8085 Exp_Node
: constant Node_Id
:= Unqual_Conv
(N
);
8086 Function_Id
: Entity_Id
;
8089 -- Return False if the expander is currently inactive, since awareness
8090 -- of build-in-place treatment is only relevant during expansion. Note
8091 -- that Is_Build_In_Place_Function, which is called as part of this
8092 -- function, is also conditioned this way, but we need to check here as
8093 -- well to avoid blowing up on processing protected calls when expansion
8094 -- is disabled (such as with -gnatc) since those would trip over the
8095 -- raise of Program_Error below.
8097 -- In SPARK mode, build-in-place calls are not expanded, so that we
8098 -- may end up with a call that is neither resolved to an entity, nor
8099 -- an indirect call.
8101 if not Expander_Active
or else Nkind
(Exp_Node
) /= N_Function_Call
then
8105 if Is_Entity_Name
(Name
(Exp_Node
)) then
8106 Function_Id
:= Entity
(Name
(Exp_Node
));
8108 -- In the case of an explicitly dereferenced call, use the subprogram
8109 -- type generated for the dereference.
8111 elsif Nkind
(Name
(Exp_Node
)) = N_Explicit_Dereference
then
8112 Function_Id
:= Etype
(Name
(Exp_Node
));
8114 -- This may be a call to a protected function.
8116 elsif Nkind
(Name
(Exp_Node
)) = N_Selected_Component
then
8117 -- The selector in question might not have been analyzed due to a
8118 -- previous error, so analyze it here to output the appropriate
8119 -- error message instead of crashing when attempting to fetch its
8122 if not Analyzed
(Selector_Name
(Name
(Exp_Node
))) then
8123 Analyze
(Selector_Name
(Name
(Exp_Node
)));
8126 Function_Id
:= Etype
(Entity
(Selector_Name
(Name
(Exp_Node
))));
8129 raise Program_Error
;
8132 if Is_Build_In_Place_Function
(Function_Id
) then
8135 -- True also if the function has BIP Formals
8139 Kind
: constant Entity_Kind
:= Ekind
(Function_Id
);
8142 if (Kind
in E_Function | E_Generic_Function
8143 or else (Kind
= E_Subprogram_Type
8145 Etype
(Function_Id
) /= Standard_Void_Type
))
8146 and then Has_BIP_Formals
(Function_Id
)
8148 -- So we can stop here in the debugger
8155 end Is_Build_In_Place_Function_Call
;
8157 ------------------------------------------
8158 -- Is_True_Build_In_Place_Function_Call --
8159 ------------------------------------------
8161 function Is_True_Build_In_Place_Function_Call
(N
: Node_Id
) return Boolean
8164 Function_Id
: Entity_Id
;
8167 -- No action needed if we know that this is not a BIP function call
8169 if not Is_Build_In_Place_Function_Call
(N
) then
8173 Exp_Node
:= Unqual_Conv
(N
);
8175 if Is_Entity_Name
(Name
(Exp_Node
)) then
8176 Function_Id
:= Entity
(Name
(Exp_Node
));
8178 elsif Nkind
(Name
(Exp_Node
)) = N_Explicit_Dereference
then
8179 Function_Id
:= Etype
(Name
(Exp_Node
));
8181 elsif Nkind
(Name
(Exp_Node
)) = N_Selected_Component
then
8182 Function_Id
:= Etype
(Entity
(Selector_Name
(Name
(Exp_Node
))));
8185 raise Program_Error
;
8188 return Is_Build_In_Place_Function
(Function_Id
);
8189 end Is_True_Build_In_Place_Function_Call
;
8191 -----------------------------------
8192 -- Is_Build_In_Place_Result_Type --
8193 -----------------------------------
8195 function Is_Build_In_Place_Result_Type
(Typ
: Entity_Id
) return Boolean is
8197 if not Expander_Active
then
8201 -- In Ada 2005 all functions with an inherently limited return type
8202 -- must be handled using a build-in-place profile, including the case
8203 -- of a function with a limited interface result, where the function
8204 -- may return objects of nonlimited descendants.
8206 return Is_Limited_View
(Typ
)
8207 and then Ada_Version
>= Ada_2005
8208 and then not Debug_Flag_Dot_L
;
8209 end Is_Build_In_Place_Result_Type
;
8211 -------------------------------------
8212 -- Is_Build_In_Place_Return_Object --
8213 -------------------------------------
8215 function Is_Build_In_Place_Return_Object
(E
: Entity_Id
) return Boolean is
8217 return Is_Return_Object
(E
)
8218 and then Is_Build_In_Place_Function
(Return_Applies_To
(Scope
(E
)));
8219 end Is_Build_In_Place_Return_Object
;
8221 -----------------------------------
8222 -- Is_By_Reference_Return_Object --
8223 -----------------------------------
8225 function Is_By_Reference_Return_Object
(E
: Entity_Id
) return Boolean is
8227 return Is_Return_Object
(E
)
8228 and then Is_By_Reference_Type
(Etype
(Return_Applies_To
(Scope
(E
))));
8229 end Is_By_Reference_Return_Object
;
8231 -----------------------
8232 -- Is_Null_Procedure --
8233 -----------------------
8235 function Is_Null_Procedure
(Subp
: Entity_Id
) return Boolean is
8236 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
8239 if Ekind
(Subp
) /= E_Procedure
then
8242 -- Check if this is a declared null procedure
8244 elsif Nkind
(Decl
) = N_Subprogram_Declaration
then
8245 if not Null_Present
(Specification
(Decl
)) then
8248 elsif No
(Body_To_Inline
(Decl
)) then
8251 -- Check if the body contains only a null statement, followed by
8252 -- the return statement added during expansion.
8256 Orig_Bod
: constant Node_Id
:= Body_To_Inline
(Decl
);
8262 if Nkind
(Orig_Bod
) /= N_Subprogram_Body
then
8265 -- We must skip SCIL nodes because they are currently
8266 -- implemented as special N_Null_Statement nodes.
8270 (Statements
(Handled_Statement_Sequence
(Orig_Bod
)));
8271 Stat2
:= Next_Non_SCIL_Node
(Stat
);
8274 Is_Empty_List
(Declarations
(Orig_Bod
))
8275 and then Nkind
(Stat
) = N_Null_Statement
8279 (Nkind
(Stat2
) = N_Simple_Return_Statement
8280 and then No
(Next
(Stat2
))));
8288 end Is_Null_Procedure
;
8290 --------------------------------------
8291 -- Is_Secondary_Stack_Return_Object --
8292 --------------------------------------
8294 function Is_Secondary_Stack_Return_Object
(E
: Entity_Id
) return Boolean is
8296 return Is_Return_Object
(E
)
8297 and then Needs_Secondary_Stack
(Etype
(Return_Applies_To
(Scope
(E
))));
8298 end Is_Secondary_Stack_Return_Object
;
8300 ------------------------------
8301 -- Is_Special_Return_Object --
8302 ------------------------------
8304 function Is_Special_Return_Object
(E
: Entity_Id
) return Boolean is
8306 return Is_Build_In_Place_Return_Object
(E
)
8307 or else Is_Secondary_Stack_Return_Object
(E
)
8308 or else (Back_End_Return_Slot
8309 and then Is_By_Reference_Return_Object
(E
));
8310 end Is_Special_Return_Object
;
8312 -------------------------------------------
8313 -- Make_Build_In_Place_Call_In_Allocator --
8314 -------------------------------------------
8316 procedure Make_Build_In_Place_Call_In_Allocator
8317 (Allocator
: Node_Id
;
8318 Function_Call
: Node_Id
)
8320 Acc_Type
: constant Entity_Id
:= Etype
(Allocator
);
8321 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
8322 Func_Call
: Node_Id
:= Function_Call
;
8323 Ref_Func_Call
: Node_Id
;
8324 Function_Id
: Entity_Id
;
8325 Result_Subt
: Entity_Id
;
8326 New_Allocator
: Node_Id
;
8327 Return_Obj_Access
: Entity_Id
; -- temp for function result
8328 Temp_Init
: Node_Id
; -- initial value of Return_Obj_Access
8329 Alloc_Form
: BIP_Allocation_Form
;
8330 Pool
: Node_Id
; -- nonnull if Alloc_Form = User_Storage_Pool
8331 Return_Obj_Actual
: Node_Id
; -- the temp.all, in caller-allocates case
8332 Chain
: Entity_Id
; -- activation chain, in case of tasks
8335 -- Step past qualification or unchecked conversion (the latter can occur
8336 -- in cases of calls to 'Input).
8338 if Nkind
(Func_Call
) in N_Qualified_Expression
8340 | N_Unchecked_Type_Conversion
8342 Func_Call
:= Expression
(Func_Call
);
8345 -- No action needed if the called function inherited the BIP extra
8346 -- formals but it is not a true BIP function.
8348 if not Is_True_Build_In_Place_Function_Call
(Func_Call
) then
8349 pragma Assert
(Is_Expanded_Build_In_Place_Call
(Func_Call
));
8353 -- Mark the call as processed as a build-in-place call
8355 pragma Assert
(not Is_Expanded_Build_In_Place_Call
(Func_Call
));
8356 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8358 if Is_Entity_Name
(Name
(Func_Call
)) then
8359 Function_Id
:= Entity
(Name
(Func_Call
));
8361 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8362 Function_Id
:= Etype
(Name
(Func_Call
));
8365 raise Program_Error
;
8368 Warn_BIP
(Func_Call
);
8370 Result_Subt
:= Available_View
(Etype
(Function_Id
));
8372 -- Create a temp for the function result. In the caller-allocates case,
8373 -- this will be initialized to the result of a new uninitialized
8374 -- allocator. Note: we do not use Allocator as the Related_Node of
8375 -- Return_Obj_Access in call to Make_Temporary below as this would
8376 -- create a sort of infinite "recursion".
8378 Return_Obj_Access
:= Make_Temporary
(Loc
, 'R');
8379 Set_Etype
(Return_Obj_Access
, Acc_Type
);
8380 Set_Can_Never_Be_Null
(Acc_Type
, False);
8381 -- It gets initialized to null, so we can't have that
8383 -- When the result subtype is constrained, the return object is created
8384 -- on the caller side, and access to it is passed to the function. This
8385 -- optimization is disabled when the result subtype needs finalization
8386 -- actions because the caller side allocation may result in undesirable
8387 -- finalization. Consider the following example:
8389 -- function Make_Lim_Ctrl return Lim_Ctrl is
8391 -- return Result : Lim_Ctrl := raise Program_Error do
8394 -- end Make_Lim_Ctrl;
8396 -- Obj : Lim_Ctrl_Ptr := new Lim_Ctrl'(Make_Lim_Ctrl);
8398 -- Even though the size of limited controlled type Lim_Ctrl is known,
8399 -- allocating Obj at the caller side will chain Obj on Lim_Ctrl_Ptr's
8400 -- finalization master. The subsequent call to Make_Lim_Ctrl will fail
8401 -- during the initialization actions for Result, which implies that
8402 -- Result (and Obj by extension) should not be finalized. However Obj
8403 -- will be finalized when access type Lim_Ctrl_Ptr goes out of scope
8404 -- since it is already attached on the related finalization master.
8406 -- Here and in related routines, we must examine the full view of the
8407 -- type, because the view at the point of call may differ from the
8408 -- one in the function body, and the expansion mechanism depends on
8409 -- the characteristics of the full view.
8411 if Needs_BIP_Alloc_Form
(Function_Id
) then
8414 -- Case of a user-defined storage pool. Pass an allocation parameter
8415 -- indicating that the function should allocate its result in the
8416 -- pool, and pass the pool. Use 'Unrestricted_Access because the
8417 -- pool may not be aliased.
8419 if Present
(Associated_Storage_Pool
(Acc_Type
)) then
8420 Alloc_Form
:= User_Storage_Pool
;
8422 Make_Attribute_Reference
(Loc
,
8425 (Associated_Storage_Pool
(Acc_Type
), Loc
),
8426 Attribute_Name
=> Name_Unrestricted_Access
);
8428 -- No user-defined pool; pass an allocation parameter indicating that
8429 -- the function should allocate its result on the heap.
8432 Alloc_Form
:= Global_Heap
;
8433 Pool
:= Make_Null
(No_Location
);
8436 -- The caller does not provide the return object in this case, so we
8437 -- have to pass null for the object access actual.
8439 Return_Obj_Actual
:= Empty
;
8442 -- Replace the initialized allocator of form "new T'(Func (...))"
8443 -- with an uninitialized allocator of form "new T", where T is the
8444 -- result subtype of the called function. The call to the function
8445 -- is handled separately further below.
8448 Make_Allocator
(Loc
,
8449 Expression
=> New_Occurrence_Of
(Result_Subt
, Loc
));
8450 Set_No_Initialization
(New_Allocator
);
8452 -- Copy attributes to new allocator. Note that the new allocator
8453 -- logically comes from source if the original one did, so copy the
8454 -- relevant flag. This ensures proper treatment of the restriction
8455 -- No_Implicit_Heap_Allocations in this case.
8457 Set_Storage_Pool
(New_Allocator
, Storage_Pool
(Allocator
));
8458 Set_Procedure_To_Call
(New_Allocator
, Procedure_To_Call
(Allocator
));
8459 Set_Comes_From_Source
(New_Allocator
, Comes_From_Source
(Allocator
));
8461 Rewrite
(Allocator
, New_Allocator
);
8463 -- Initial value of the temp is the result of the uninitialized
8464 -- allocator. Unchecked_Convert is needed for T'Input where T is
8465 -- derived from a controlled type.
8467 Temp_Init
:= Relocate_Node
(Allocator
);
8469 if Nkind
(Function_Call
) in
8470 N_Type_Conversion | N_Unchecked_Type_Conversion
8472 Temp_Init
:= Unchecked_Convert_To
(Acc_Type
, Temp_Init
);
8475 -- Indicate that caller allocates, and pass in the return object
8477 Alloc_Form
:= Caller_Allocation
;
8478 Pool
:= Make_Null
(No_Location
);
8479 Return_Obj_Actual
:= Unchecked_Convert_To
8481 Make_Explicit_Dereference
(Loc
,
8482 Prefix
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
)));
8484 -- When the result subtype is unconstrained, the function itself must
8485 -- perform the allocation of the return object, so we pass parameters
8490 -- Declare the temp object
8492 Insert_Action
(Allocator
,
8493 Make_Object_Declaration
(Loc
,
8494 Defining_Identifier
=> Return_Obj_Access
,
8495 Object_Definition
=> New_Occurrence_Of
(Acc_Type
, Loc
),
8496 Expression
=> Temp_Init
));
8498 Ref_Func_Call
:= Make_Reference
(Loc
, Func_Call
);
8500 -- Ada 2005 (AI-251): If the type of the allocator is an interface
8501 -- then generate an implicit conversion to force displacement of the
8504 if Is_Interface
(Designated_Type
(Acc_Type
)) then
8507 OK_Convert_To
(Acc_Type
, Ref_Func_Call
));
8509 -- If the types are incompatible, we need an unchecked conversion. Note
8510 -- that the full types will be compatible, but the types not visibly
8513 elsif Nkind
(Function_Call
)
8514 in N_Type_Conversion | N_Unchecked_Type_Conversion
8516 Ref_Func_Call
:= Unchecked_Convert_To
(Acc_Type
, Ref_Func_Call
);
8520 Assign
: constant Node_Id
:=
8521 Make_Assignment_Statement
(Loc
,
8522 Name
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
),
8523 Expression
=> Ref_Func_Call
);
8524 -- Assign the result of the function call into the temp. In the
8525 -- caller-allocates case, this is overwriting the temp with its
8526 -- initial value, which has no effect. In the callee-allocates case,
8527 -- this is setting the temp to point to the object allocated by the
8528 -- callee. Unchecked_Convert is needed for T'Input where T is derived
8529 -- from a controlled type.
8532 -- Actions to be inserted. If there are no tasks, this is just the
8533 -- assignment statement. If the allocated object has tasks, we need
8534 -- to wrap the assignment in a block that activates them. The
8535 -- activation chain of that block must be passed to the function,
8536 -- rather than some outer chain.
8539 if Might_Have_Tasks
(Result_Subt
) then
8540 Actions
:= New_List
;
8541 Build_Task_Allocate_Block_With_Init_Stmts
8542 (Actions
, Allocator
, Init_Stmts
=> New_List
(Assign
));
8543 Chain
:= Activation_Chain_Entity
(Last
(Actions
));
8545 Actions
:= New_List
(Assign
);
8549 Insert_Actions
(Allocator
, Actions
);
8552 -- When the function has a controlling result, an allocation-form
8553 -- parameter must be passed indicating that the caller is allocating
8554 -- the result object. This is needed because such a function can be
8555 -- called as a dispatching operation and must be treated similarly
8556 -- to functions with unconstrained result subtypes.
8558 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8559 (Func_Call
, Function_Id
, Alloc_Form
, Pool_Actual
=> Pool
);
8561 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8562 (Func_Call
, Function_Id
, Acc_Type
);
8564 Add_Task_Actuals_To_Build_In_Place_Call
8565 (Func_Call
, Function_Id
, Master_Actual
=> Master_Id
(Acc_Type
),
8568 -- Add an implicit actual to the function call that provides access
8569 -- to the allocated object. An unchecked conversion to the (specific)
8570 -- result subtype of the function is inserted to handle cases where
8571 -- the access type of the allocator has a class-wide designated type.
8573 Add_Access_Actual_To_Build_In_Place_Call
8574 (Func_Call
, Function_Id
, Return_Obj_Actual
);
8576 -- Finally, replace the allocator node with a reference to the temp
8578 Rewrite
(Allocator
, New_Occurrence_Of
(Return_Obj_Access
, Loc
));
8580 Analyze_And_Resolve
(Allocator
, Acc_Type
);
8581 pragma Assert
(Check_Number_Of_Actuals
(Func_Call
, Function_Id
));
8582 pragma Assert
(Check_BIP_Actuals
(Func_Call
, Function_Id
));
8583 end Make_Build_In_Place_Call_In_Allocator
;
8585 ---------------------------------------------------
8586 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8587 ---------------------------------------------------
8589 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8590 (Function_Call
: Node_Id
)
8592 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
8593 Func_Call
: constant Node_Id
:= Unqual_Conv
(Function_Call
);
8594 Function_Id
: Entity_Id
;
8595 Result_Subt
: Entity_Id
;
8596 Return_Obj_Id
: Entity_Id
;
8597 Return_Obj_Decl
: Entity_Id
;
8600 -- If the call has already been processed to add build-in-place actuals
8601 -- then return. One place this can occur is for calls to build-in-place
8602 -- functions that occur within a call to a protected operation, where
8603 -- due to rewriting and expansion of the protected call there can be
8604 -- more than one call to Expand_Actuals for the same set of actuals.
8606 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
8610 -- Mark the call as processed as a build-in-place call
8612 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8614 if Is_Entity_Name
(Name
(Func_Call
)) then
8615 Function_Id
:= Entity
(Name
(Func_Call
));
8617 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8618 Function_Id
:= Etype
(Name
(Func_Call
));
8621 raise Program_Error
;
8624 Warn_BIP
(Func_Call
);
8626 Result_Subt
:= Etype
(Function_Id
);
8628 -- If the build-in-place function returns a controlled object, then the
8629 -- object needs to be finalized immediately after the context. Since
8630 -- this case produces a transient scope, the servicing finalizer needs
8631 -- to name the returned object. Create a temporary which is initialized
8632 -- with the function call:
8634 -- Temp_Id : Func_Type := BIP_Func_Call;
8636 -- The initialization expression of the temporary will be rewritten by
8637 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8638 -- Call_In_Object_Declaration.
8640 if Needs_Finalization
(Result_Subt
) then
8642 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'R');
8643 Temp_Decl
: Node_Id
;
8646 -- Reset the guard on the function call since the following does
8647 -- not perform actual call expansion.
8649 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
, False);
8652 Make_Object_Declaration
(Loc
,
8653 Defining_Identifier
=> Temp_Id
,
8654 Object_Definition
=>
8655 New_Occurrence_Of
(Result_Subt
, Loc
),
8657 New_Copy_Tree
(Function_Call
));
8659 Insert_Action
(Function_Call
, Temp_Decl
);
8661 Rewrite
(Function_Call
, New_Occurrence_Of
(Temp_Id
, Loc
));
8662 Analyze
(Function_Call
);
8665 -- When the result subtype is definite, an object of the subtype is
8666 -- declared and an access value designating it is passed as an actual.
8668 elsif Caller_Known_Size
(Func_Call
, Result_Subt
) then
8670 -- Create a temporary object to hold the function result
8672 Return_Obj_Id
:= Make_Temporary
(Loc
, 'R');
8673 Set_Etype
(Return_Obj_Id
, Result_Subt
);
8676 Make_Object_Declaration
(Loc
,
8677 Defining_Identifier
=> Return_Obj_Id
,
8678 Aliased_Present
=> True,
8679 Object_Definition
=> New_Occurrence_Of
(Result_Subt
, Loc
));
8681 Set_No_Initialization
(Return_Obj_Decl
);
8683 Insert_Action
(Func_Call
, Return_Obj_Decl
);
8685 -- When the function has a controlling result, an allocation-form
8686 -- parameter must be passed indicating that the caller is allocating
8687 -- the result object. This is needed because such a function can be
8688 -- called as a dispatching operation and must be treated similarly
8689 -- to functions with unconstrained result subtypes.
8691 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8692 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
8694 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8695 (Func_Call
, Function_Id
);
8697 Add_Task_Actuals_To_Build_In_Place_Call
8698 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
8700 -- Add an implicit actual to the function call that provides access
8701 -- to the caller's return object.
8703 Add_Access_Actual_To_Build_In_Place_Call
8704 (Func_Call
, Function_Id
, New_Occurrence_Of
(Return_Obj_Id
, Loc
));
8706 pragma Assert
(Check_Number_Of_Actuals
(Func_Call
, Function_Id
));
8707 pragma Assert
(Check_BIP_Actuals
(Func_Call
, Function_Id
));
8709 -- When the result subtype is unconstrained, the function must allocate
8710 -- the return object in the secondary stack, so appropriate implicit
8711 -- parameters are added to the call to indicate that. A transient
8712 -- scope is established to ensure eventual cleanup of the result.
8715 -- Pass an allocation parameter indicating that the function should
8716 -- allocate its result on the secondary stack.
8718 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8719 (Func_Call
, Function_Id
, Alloc_Form
=> Secondary_Stack
);
8721 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8722 (Func_Call
, Function_Id
);
8724 Add_Task_Actuals_To_Build_In_Place_Call
8725 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
8727 -- Pass a null value to the function since no return object is
8728 -- available on the caller side.
8730 Add_Access_Actual_To_Build_In_Place_Call
8731 (Func_Call
, Function_Id
, Empty
);
8733 pragma Assert
(Check_Number_Of_Actuals
(Func_Call
, Function_Id
));
8734 pragma Assert
(Check_BIP_Actuals
(Func_Call
, Function_Id
));
8736 end Make_Build_In_Place_Call_In_Anonymous_Context
;
8738 --------------------------------------------
8739 -- Make_Build_In_Place_Call_In_Assignment --
8740 --------------------------------------------
8742 procedure Make_Build_In_Place_Call_In_Assignment
8744 Function_Call
: Node_Id
)
8746 Func_Call
: constant Node_Id
:= Unqual_Conv
(Function_Call
);
8747 Lhs
: constant Node_Id
:= Name
(Assign
);
8748 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
8749 Func_Id
: Entity_Id
;
8752 Ptr_Typ
: Entity_Id
;
8753 Ptr_Typ_Decl
: Node_Id
;
8755 Result_Subt
: Entity_Id
;
8758 -- No action needed if the called function inherited the BIP extra
8759 -- formals but it is not a true BIP function.
8761 if not Is_True_Build_In_Place_Function_Call
(Func_Call
) then
8762 pragma Assert
(Is_Expanded_Build_In_Place_Call
(Func_Call
));
8766 -- Mark the call as processed as a build-in-place call
8768 pragma Assert
(not Is_Expanded_Build_In_Place_Call
(Func_Call
));
8769 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8771 if Is_Entity_Name
(Name
(Func_Call
)) then
8772 Func_Id
:= Entity
(Name
(Func_Call
));
8774 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8775 Func_Id
:= Etype
(Name
(Func_Call
));
8778 raise Program_Error
;
8781 Warn_BIP
(Func_Call
);
8783 Result_Subt
:= Etype
(Func_Id
);
8785 -- When the result subtype is unconstrained, an additional actual must
8786 -- be passed to indicate that the caller is providing the return object.
8787 -- This parameter must also be passed when the called function has a
8788 -- controlling result, because dispatching calls to the function needs
8789 -- to be treated effectively the same as calls to class-wide functions.
8791 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8792 (Func_Call
, Func_Id
, Alloc_Form
=> Caller_Allocation
);
8794 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8795 (Func_Call
, Func_Id
);
8797 Add_Task_Actuals_To_Build_In_Place_Call
8798 (Func_Call
, Func_Id
, Make_Identifier
(Loc
, Name_uMaster
));
8800 -- Add an implicit actual to the function call that provides access to
8801 -- the caller's return object.
8803 Add_Access_Actual_To_Build_In_Place_Call
8804 (Func_Call
, Func_Id
, Unchecked_Convert_To
(Result_Subt
, Lhs
));
8806 -- Create an access type designating the function's result subtype
8808 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
8811 Make_Full_Type_Declaration
(Loc
,
8812 Defining_Identifier
=> Ptr_Typ
,
8814 Make_Access_To_Object_Definition
(Loc
,
8815 All_Present
=> True,
8816 Subtype_Indication
=>
8817 New_Occurrence_Of
(Result_Subt
, Loc
)));
8818 Insert_After_And_Analyze
(Assign
, Ptr_Typ_Decl
);
8820 -- Finally, create an access object initialized to a reference to the
8821 -- function call. We know this access value is non-null, so mark the
8822 -- entity accordingly to suppress junk access checks.
8824 New_Expr
:= Make_Reference
(Loc
, Relocate_Node
(Func_Call
));
8826 -- Add a conversion if it's the wrong type
8828 New_Expr
:= Unchecked_Convert_To
(Ptr_Typ
, New_Expr
);
8830 Obj_Id
:= Make_Temporary
(Loc
, 'R', New_Expr
);
8831 Set_Etype
(Obj_Id
, Ptr_Typ
);
8832 Set_Is_Known_Non_Null
(Obj_Id
);
8835 Make_Object_Declaration
(Loc
,
8836 Defining_Identifier
=> Obj_Id
,
8837 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
8838 Expression
=> New_Expr
);
8839 Insert_After_And_Analyze
(Ptr_Typ_Decl
, Obj_Decl
);
8841 Rewrite
(Assign
, Make_Null_Statement
(Loc
));
8842 pragma Assert
(Check_Number_Of_Actuals
(Func_Call
, Func_Id
));
8843 pragma Assert
(Check_BIP_Actuals
(Func_Call
, Func_Id
));
8844 end Make_Build_In_Place_Call_In_Assignment
;
8846 ----------------------------------------------------
8847 -- Make_Build_In_Place_Call_In_Object_Declaration --
8848 ----------------------------------------------------
8850 procedure Make_Build_In_Place_Call_In_Object_Declaration
8851 (Obj_Decl
: Node_Id
;
8852 Function_Call
: Node_Id
)
8854 function Get_Function_Id
(Func_Call
: Node_Id
) return Entity_Id
;
8855 -- Get the value of Function_Id, below
8857 ---------------------
8858 -- Get_Function_Id --
8859 ---------------------
8861 function Get_Function_Id
(Func_Call
: Node_Id
) return Entity_Id
is
8863 if Is_Entity_Name
(Name
(Func_Call
)) then
8864 return Entity
(Name
(Func_Call
));
8866 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8867 return Etype
(Name
(Func_Call
));
8870 raise Program_Error
;
8872 end Get_Function_Id
;
8876 Func_Call
: constant Node_Id
:= Unqual_Conv
(Function_Call
);
8877 Function_Id
: constant Entity_Id
:= Get_Function_Id
(Func_Call
);
8878 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
8879 Obj_Loc
: constant Source_Ptr
:= Sloc
(Obj_Decl
);
8880 Obj_Def_Id
: constant Entity_Id
:= Defining_Identifier
(Obj_Decl
);
8881 Obj_Typ
: constant Entity_Id
:= Etype
(Obj_Def_Id
);
8882 Encl_Func
: constant Entity_Id
:= Enclosing_Subprogram
(Obj_Def_Id
);
8883 Result_Subt
: constant Entity_Id
:= Etype
(Function_Id
);
8885 Call_Deref
: Node_Id
;
8886 Caller_Object
: Node_Id
;
8888 Designated_Type
: Entity_Id
;
8889 Fmaster_Actual
: Node_Id
:= Empty
;
8890 Pool_Actual
: Node_Id
;
8891 Ptr_Typ
: Entity_Id
;
8892 Ptr_Typ_Decl
: Node_Id
;
8893 Pass_Caller_Acc
: Boolean := False;
8896 Definite
: constant Boolean :=
8897 Caller_Known_Size
(Func_Call
, Result_Subt
)
8898 and then not Is_Class_Wide_Type
(Obj_Typ
);
8899 -- In the case of "X : T'Class := F(...);", where F returns a
8900 -- Caller_Known_Size (specific) tagged type, we treat it as
8901 -- indefinite, because the code for the Definite case below sets the
8902 -- initialization expression of the object to Empty, which would be
8903 -- illegal Ada, and would cause gigi to misallocate X.
8905 Is_OK_Return_Object
: constant Boolean :=
8906 Is_Return_Object
(Obj_Def_Id
)
8908 not Has_Foreign_Convention
(Return_Applies_To
(Scope
(Obj_Def_Id
)));
8910 -- Start of processing for Make_Build_In_Place_Call_In_Object_Declaration
8913 -- If the call has already been processed to add build-in-place actuals
8916 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
8920 -- Mark the call as processed as a build-in-place call
8922 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8924 Warn_BIP
(Func_Call
);
8926 -- Create an access type designating the function's result subtype.
8927 -- We use the type of the original call because it may be a call to an
8928 -- inherited operation, which the expansion has replaced with the parent
8929 -- operation that yields the parent type. Note that this access type
8930 -- must be declared before we establish a transient scope, so that it
8931 -- receives the proper accessibility level.
8933 if Is_Class_Wide_Type
(Obj_Typ
)
8934 and then not Is_Interface
(Obj_Typ
)
8935 and then not Is_Class_Wide_Type
(Etype
(Function_Call
))
8937 Designated_Type
:= Obj_Typ
;
8939 Designated_Type
:= Etype
(Function_Call
);
8942 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
8944 Make_Full_Type_Declaration
(Loc
,
8945 Defining_Identifier
=> Ptr_Typ
,
8947 Make_Access_To_Object_Definition
(Loc
,
8948 All_Present
=> True,
8949 Subtype_Indication
=>
8950 New_Occurrence_Of
(Designated_Type
, Loc
)));
8952 -- The access type and its accompanying object must be inserted after
8953 -- the object declaration in the constrained case, so that the function
8954 -- call can be passed access to the object. In the indefinite case, or
8955 -- if the object declaration is for a return object, the access type and
8956 -- object must be inserted before the object, since the object
8957 -- declaration is rewritten to be a renaming of a dereference of the
8958 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8959 -- the result object is in a different (transient) scope, so won't cause
8962 if Definite
and then not Is_OK_Return_Object
then
8964 -- The presence of an address clause complicates the build-in-place
8965 -- expansion because the indicated address must be processed before
8966 -- the indirect call is generated (including the definition of a
8967 -- local pointer to the object). The address clause may come from
8968 -- an aspect specification or from an explicit attribute
8969 -- specification appearing after the object declaration. These two
8970 -- cases require different processing.
8972 if Has_Aspect
(Obj_Def_Id
, Aspect_Address
) then
8974 -- Skip non-delayed pragmas that correspond to other aspects, if
8975 -- any, to find proper insertion point for freeze node of object.
8978 D
: Node_Id
:= Obj_Decl
;
8979 N
: Node_Id
:= Next
(D
);
8983 and then Nkind
(N
) in N_Attribute_Reference | N_Pragma
8990 Insert_After
(D
, Ptr_Typ_Decl
);
8992 -- Freeze object before pointer declaration, to ensure that
8993 -- generated attribute for address is inserted at the proper
8996 Freeze_Before
(Ptr_Typ_Decl
, Obj_Def_Id
);
8999 Analyze
(Ptr_Typ_Decl
);
9001 elsif Present
(Following_Address_Clause
(Obj_Decl
)) then
9003 -- Locate explicit address clause, which may also follow pragmas
9004 -- generated by other aspect specifications.
9007 Addr
: constant Node_Id
:= Following_Address_Clause
(Obj_Decl
);
9008 D
: Node_Id
:= Next
(Obj_Decl
);
9011 while Present
(D
) loop
9017 Insert_After_And_Analyze
(Addr
, Ptr_Typ_Decl
);
9021 Insert_After_And_Analyze
(Obj_Decl
, Ptr_Typ_Decl
);
9024 Insert_Action
(Obj_Decl
, Ptr_Typ_Decl
);
9027 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
9028 -- elaborated in an inner (transient) scope and thus won't cause
9029 -- freezing by itself. It's not an itype, but it needs to be frozen
9030 -- inside the current subprogram (see Freeze_Outside in freeze.adb).
9032 Freeze_Itype
(Ptr_Typ
, Ptr_Typ_Decl
);
9034 -- If the object is a return object of an enclosing build-in-place
9035 -- function, then the implicit build-in-place parameters of the
9036 -- enclosing function are simply passed along to the called function.
9037 -- (Unfortunately, this won't cover the case of extension aggregates
9038 -- where the ancestor part is a build-in-place indefinite function
9039 -- call that should be passed along the caller's parameters.
9040 -- Currently those get mishandled by reassigning the result of the
9041 -- call to the aggregate return object, when the call result should
9042 -- really be directly built in place in the aggregate and not in a
9045 if Is_OK_Return_Object
then
9046 Pass_Caller_Acc
:= True;
9048 -- When the enclosing function has a BIP_Alloc_Form formal then we
9049 -- pass it along to the callee (such as when the enclosing function
9050 -- has an unconstrained or tagged result type).
9052 if Needs_BIP_Alloc_Form
(Encl_Func
) then
9053 if RTE_Available
(RE_Root_Storage_Pool_Ptr
) then
9056 (Build_In_Place_Formal
9057 (Encl_Func
, BIP_Storage_Pool
), Loc
);
9059 -- The build-in-place pool formal is not built on e.g. ZFP
9062 Pool_Actual
:= Empty
;
9065 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9066 (Function_Call
=> Func_Call
,
9067 Function_Id
=> Function_Id
,
9070 (Build_In_Place_Formal
(Encl_Func
, BIP_Alloc_Form
), Loc
),
9071 Pool_Actual
=> Pool_Actual
);
9073 -- Otherwise, if enclosing function has a definite result subtype,
9074 -- then caller allocation will be used.
9077 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9078 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
9081 if Needs_BIP_Finalization_Master
(Encl_Func
) then
9084 (Build_In_Place_Formal
9085 (Encl_Func
, BIP_Finalization_Master
), Loc
);
9088 -- Retrieve the BIPacc formal from the enclosing function and convert
9089 -- it to the access type of the callee's BIP_Object_Access formal.
9092 Unchecked_Convert_To
9093 (Etype
(Build_In_Place_Formal
(Function_Id
, BIP_Object_Access
)),
9095 (Build_In_Place_Formal
(Encl_Func
, BIP_Object_Access
), Loc
));
9097 -- In the definite case, add an implicit actual to the function call
9098 -- that provides access to the declared object. An unchecked conversion
9099 -- to the (specific) result type of the function is inserted to handle
9100 -- the case where the object is declared with a class-wide type.
9103 Caller_Object
:= Unchecked_Convert_To
9104 (Result_Subt
, New_Occurrence_Of
(Obj_Def_Id
, Loc
));
9106 -- When the function has a controlling result, an allocation-form
9107 -- parameter must be passed indicating that the caller is allocating
9108 -- the result object. This is needed because such a function can be
9109 -- called as a dispatching operation and must be treated similarly to
9110 -- functions with indefinite result subtypes.
9112 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9113 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
9115 -- The allocation for indefinite library-level objects occurs on the
9116 -- heap as opposed to the secondary stack. This accommodates DLLs where
9117 -- the secondary stack is destroyed after each library unload. This is a
9118 -- hybrid mechanism where a stack-allocated object lives on the heap.
9120 elsif Is_Library_Level_Entity
(Obj_Def_Id
)
9121 and then not Restriction_Active
(No_Implicit_Heap_Allocations
)
9123 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9124 (Func_Call
, Function_Id
, Alloc_Form
=> Global_Heap
);
9125 Caller_Object
:= Empty
;
9127 -- Create a finalization master for the access result type to ensure
9128 -- that the heap allocation can properly chain the object and later
9129 -- finalize it when the library unit goes out of scope.
9131 if Needs_Finalization
(Etype
(Func_Call
)) then
9132 Build_Finalization_Master
9134 For_Lib_Level
=> True,
9135 Insertion_Node
=> Ptr_Typ_Decl
);
9138 Make_Attribute_Reference
(Loc
,
9140 New_Occurrence_Of
(Finalization_Master
(Ptr_Typ
), Loc
),
9141 Attribute_Name
=> Name_Unrestricted_Access
);
9144 -- In other indefinite cases, pass an indication to do the allocation
9145 -- on the secondary stack and set Caller_Object to Empty so that a null
9146 -- value will be passed for the caller's object address. A transient
9147 -- scope is established to ensure eventual cleanup of the result.
9150 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9151 (Func_Call
, Function_Id
, Alloc_Form
=> Secondary_Stack
);
9152 Caller_Object
:= Empty
;
9154 Establish_Transient_Scope
(Obj_Decl
, Manage_Sec_Stack
=> True);
9157 -- Pass along any finalization master actual, which is needed in the
9158 -- case where the called function initializes a return object of an
9159 -- enclosing build-in-place function.
9161 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9162 (Func_Call
=> Func_Call
,
9163 Func_Id
=> Function_Id
,
9164 Master_Exp
=> Fmaster_Actual
);
9166 if Nkind
(Parent
(Obj_Decl
)) = N_Extended_Return_Statement
9167 and then Needs_BIP_Task_Actuals
(Function_Id
)
9169 -- Here we're passing along the master that was passed in to this
9172 Add_Task_Actuals_To_Build_In_Place_Call
9173 (Func_Call
, Function_Id
,
9176 (Build_In_Place_Formal
(Encl_Func
, BIP_Task_Master
), Loc
));
9179 Add_Task_Actuals_To_Build_In_Place_Call
9180 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
9183 Add_Access_Actual_To_Build_In_Place_Call
9187 Is_Access
=> Pass_Caller_Acc
);
9189 -- Finally, create an access object initialized to a reference to the
9190 -- function call. We know this access value cannot be null, so mark the
9191 -- entity accordingly to suppress the access check. We need to suppress
9192 -- warnings, because this can be part of the expansion of "for ... of"
9193 -- and similar constructs that generate finalization actions. Such
9194 -- finalization actions are safe, because they check a count that
9195 -- indicates which objects should be finalized, but the back end
9196 -- nonetheless warns about uninitialized objects.
9198 Def_Id
:= Make_Temporary
(Loc
, 'R', Func_Call
);
9199 Set_Warnings_Off
(Def_Id
);
9200 Set_Etype
(Def_Id
, Ptr_Typ
);
9201 Set_Is_Known_Non_Null
(Def_Id
);
9203 if Nkind
(Function_Call
) in N_Type_Conversion
9204 | N_Unchecked_Type_Conversion
9207 Make_Object_Declaration
(Loc
,
9208 Defining_Identifier
=> Def_Id
,
9209 Constant_Present
=> True,
9210 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
9212 Unchecked_Convert_To
9213 (Ptr_Typ
, Make_Reference
(Loc
, Relocate_Node
(Func_Call
))));
9216 Make_Object_Declaration
(Loc
,
9217 Defining_Identifier
=> Def_Id
,
9218 Constant_Present
=> True,
9219 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
9221 Make_Reference
(Loc
, Relocate_Node
(Func_Call
)));
9224 Insert_After_And_Analyze
(Ptr_Typ_Decl
, Res_Decl
);
9226 -- If the result subtype of the called function is definite and is not
9227 -- itself the return expression of an enclosing BIP function, then mark
9228 -- the object as having no initialization.
9230 if Definite
and then not Is_OK_Return_Object
then
9232 -- The related object declaration is encased in a transient block
9233 -- because the build-in-place function call contains at least one
9234 -- nested function call that produces a controlled transient
9237 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
9239 -- Since the build-in-place expansion decouples the call from the
9240 -- object declaration, the finalization machinery lacks the context
9241 -- which prompted the generation of the transient block. To resolve
9242 -- this scenario, store the build-in-place call.
9244 if Scope_Is_Transient
then
9245 Set_BIP_Initialization_Call
(Obj_Def_Id
, Res_Decl
);
9248 Set_Expression
(Obj_Decl
, Empty
);
9249 Set_No_Initialization
(Obj_Decl
);
9251 -- In case of an indefinite result subtype, or if the call is the
9252 -- return expression of an enclosing BIP function, rewrite the object
9253 -- declaration as an object renaming where the renamed object is a
9254 -- dereference of <function_Call>'reference:
9256 -- Obj : Subt renames <function_call>'Ref.all;
9260 Make_Explicit_Dereference
(Obj_Loc
,
9261 Prefix
=> New_Occurrence_Of
(Def_Id
, Obj_Loc
));
9264 Make_Object_Renaming_Declaration
(Obj_Loc
,
9265 Defining_Identifier
=> Make_Temporary
(Obj_Loc
, 'D'),
9267 New_Occurrence_Of
(Designated_Type
, Obj_Loc
),
9268 Name
=> Call_Deref
));
9270 -- At this point, Defining_Identifier (Obj_Decl) is no longer equal
9273 pragma Assert
(Ekind
(Defining_Identifier
(Obj_Decl
)) = E_Void
);
9274 Set_Renamed_Object_Of_Possibly_Void
9275 (Defining_Identifier
(Obj_Decl
), Call_Deref
);
9277 -- If the original entity comes from source, then mark the new
9278 -- entity as needing debug information, even though it's defined
9279 -- by a generated renaming that does not come from source, so that
9280 -- the Materialize_Entity flag will be set on the entity when
9281 -- Debug_Renaming_Declaration is called during analysis.
9283 if Comes_From_Source
(Obj_Def_Id
) then
9284 Set_Debug_Info_Needed
(Defining_Identifier
(Obj_Decl
));
9288 Replace_Renaming_Declaration_Id
9289 (Obj_Decl
, Original_Node
(Obj_Decl
));
9292 pragma Assert
(Check_Number_Of_Actuals
(Func_Call
, Function_Id
));
9293 pragma Assert
(Check_BIP_Actuals
(Func_Call
, Function_Id
));
9294 end Make_Build_In_Place_Call_In_Object_Declaration
;
9296 -------------------------------------------------
9297 -- Make_Build_In_Place_Iface_Call_In_Allocator --
9298 -------------------------------------------------
9300 procedure Make_Build_In_Place_Iface_Call_In_Allocator
9301 (Allocator
: Node_Id
;
9302 Function_Call
: Node_Id
)
9304 BIP_Func_Call
: constant Node_Id
:=
9305 Unqual_BIP_Iface_Function_Call
(Function_Call
);
9306 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
9308 Anon_Type
: Entity_Id
;
9313 -- No action if the call has already been processed
9315 if Is_Expanded_Build_In_Place_Call
(BIP_Func_Call
) then
9319 Tmp_Id
:= Make_Temporary
(Loc
, 'D');
9321 -- Insert a temporary before N initialized with the BIP function call
9322 -- without its enclosing type conversions and analyze it without its
9323 -- expansion. This temporary facilitates us reusing the BIP machinery,
9324 -- which takes care of adding the extra build-in-place actuals and
9325 -- transforms this object declaration into an object renaming
9328 Anon_Type
:= Create_Itype
(E_Anonymous_Access_Type
, Function_Call
);
9329 Set_Directly_Designated_Type
(Anon_Type
, Etype
(BIP_Func_Call
));
9330 Set_Etype
(Anon_Type
, Anon_Type
);
9331 Build_Class_Wide_Master
(Anon_Type
);
9334 Make_Object_Declaration
(Loc
,
9335 Defining_Identifier
=> Tmp_Id
,
9336 Object_Definition
=> New_Occurrence_Of
(Anon_Type
, Loc
),
9338 Make_Allocator
(Loc
,
9340 Make_Qualified_Expression
(Loc
,
9342 New_Occurrence_Of
(Etype
(BIP_Func_Call
), Loc
),
9343 Expression
=> New_Copy_Tree
(BIP_Func_Call
))));
9345 -- Manually set the associated node for the anonymous access type to
9346 -- be its local declaration, to avoid confusing and complicating
9347 -- the accessibility machinery.
9349 Set_Associated_Node_For_Itype
(Anon_Type
, Tmp_Decl
);
9351 Expander_Mode_Save_And_Set
(False);
9352 Insert_Action
(Allocator
, Tmp_Decl
);
9353 Expander_Mode_Restore
;
9355 Make_Build_In_Place_Call_In_Allocator
9356 (Allocator
=> Expression
(Tmp_Decl
),
9357 Function_Call
=> Expression
(Expression
(Tmp_Decl
)));
9359 -- Add a conversion to displace the pointer to the allocated object
9360 -- to reference the corresponding dispatch table.
9363 Convert_To
(Etype
(Allocator
),
9364 New_Occurrence_Of
(Tmp_Id
, Loc
)));
9365 end Make_Build_In_Place_Iface_Call_In_Allocator
;
9367 ---------------------------------------------------------
9368 -- Make_Build_In_Place_Iface_Call_In_Anonymous_Context --
9369 ---------------------------------------------------------
9371 procedure Make_Build_In_Place_Iface_Call_In_Anonymous_Context
9372 (Function_Call
: Node_Id
)
9374 BIP_Func_Call
: constant Node_Id
:=
9375 Unqual_BIP_Iface_Function_Call
(Function_Call
);
9376 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
9382 -- No action of the call has already been processed
9384 if Is_Expanded_Build_In_Place_Call
(BIP_Func_Call
) then
9388 pragma Assert
(Needs_Finalization
(Etype
(BIP_Func_Call
)));
9390 -- Insert a temporary before the call initialized with function call to
9391 -- reuse the BIP machinery which takes care of adding the extra build-in
9392 -- place actuals and transforms this object declaration into an object
9393 -- renaming declaration.
9395 Tmp_Id
:= Make_Temporary
(Loc
, 'D');
9398 Make_Object_Declaration
(Loc
,
9399 Defining_Identifier
=> Tmp_Id
,
9400 Object_Definition
=>
9401 New_Occurrence_Of
(Etype
(Function_Call
), Loc
),
9402 Expression
=> Relocate_Node
(Function_Call
));
9404 Expander_Mode_Save_And_Set
(False);
9405 Insert_Action
(Function_Call
, Tmp_Decl
);
9406 Expander_Mode_Restore
;
9408 Make_Build_In_Place_Iface_Call_In_Object_Declaration
9409 (Obj_Decl
=> Tmp_Decl
,
9410 Function_Call
=> Expression
(Tmp_Decl
));
9411 end Make_Build_In_Place_Iface_Call_In_Anonymous_Context
;
9413 ----------------------------------------------------------
9414 -- Make_Build_In_Place_Iface_Call_In_Object_Declaration --
9415 ----------------------------------------------------------
9417 procedure Make_Build_In_Place_Iface_Call_In_Object_Declaration
9418 (Obj_Decl
: Node_Id
;
9419 Function_Call
: Node_Id
)
9421 BIP_Func_Call
: constant Node_Id
:=
9422 Unqual_BIP_Iface_Function_Call
(Function_Call
);
9423 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
9424 Obj_Id
: constant Entity_Id
:= Defining_Entity
(Obj_Decl
);
9430 -- No action of the call has already been processed
9432 if Is_Expanded_Build_In_Place_Call
(BIP_Func_Call
) then
9436 Tmp_Id
:= Make_Temporary
(Loc
, 'D');
9438 -- Insert a temporary before N initialized with the BIP function call
9439 -- without its enclosing type conversions and analyze it without its
9440 -- expansion. This temporary facilitates us reusing the BIP machinery,
9441 -- which takes care of adding the extra build-in-place actuals and
9442 -- transforms this object declaration into an object renaming
9446 Make_Object_Declaration
(Loc
,
9447 Defining_Identifier
=> Tmp_Id
,
9448 Object_Definition
=>
9449 New_Occurrence_Of
(Etype
(BIP_Func_Call
), Loc
),
9450 Expression
=> New_Copy_Tree
(BIP_Func_Call
));
9452 Expander_Mode_Save_And_Set
(False);
9453 Insert_Action
(Obj_Decl
, Tmp_Decl
);
9454 Expander_Mode_Restore
;
9456 Make_Build_In_Place_Call_In_Object_Declaration
9457 (Obj_Decl
=> Tmp_Decl
,
9458 Function_Call
=> Expression
(Tmp_Decl
));
9460 pragma Assert
(Nkind
(Tmp_Decl
) = N_Object_Renaming_Declaration
);
9462 -- Replace the original build-in-place function call by a reference to
9463 -- the resulting temporary object renaming declaration. In this way,
9464 -- all the interface conversions performed in the original Function_Call
9465 -- on the build-in-place object are preserved.
9467 Rewrite
(BIP_Func_Call
, New_Occurrence_Of
(Tmp_Id
, Loc
));
9469 -- Replace the original object declaration by an internal object
9470 -- renaming declaration. This leaves the generated code more clean (the
9471 -- build-in-place function call in an object renaming declaration and
9472 -- displacements of the pointer to the build-in-place object in another
9473 -- renaming declaration) and allows us to invoke the routine that takes
9474 -- care of replacing the identifier of the renaming declaration (routine
9475 -- originally developed for the regular build-in-place management).
9478 Make_Object_Renaming_Declaration
(Loc
,
9479 Defining_Identifier
=> Make_Temporary
(Loc
, 'D'),
9480 Subtype_Mark
=> New_Occurrence_Of
(Etype
(Obj_Id
), Loc
),
9481 Name
=> Function_Call
));
9484 Replace_Renaming_Declaration_Id
(Obj_Decl
, Original_Node
(Obj_Decl
));
9485 end Make_Build_In_Place_Iface_Call_In_Object_Declaration
;
9487 --------------------------------------------
9488 -- Make_CPP_Constructor_Call_In_Allocator --
9489 --------------------------------------------
9491 procedure Make_CPP_Constructor_Call_In_Allocator
9492 (Allocator
: Node_Id
;
9493 Function_Call
: Node_Id
)
9495 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
9496 Acc_Type
: constant Entity_Id
:= Etype
(Allocator
);
9497 Function_Id
: constant Entity_Id
:= Entity
(Name
(Function_Call
));
9498 Result_Subt
: constant Entity_Id
:= Available_View
(Etype
(Function_Id
));
9500 New_Allocator
: Node_Id
;
9501 Return_Obj_Access
: Entity_Id
;
9505 pragma Assert
(Nkind
(Allocator
) = N_Allocator
9506 and then Nkind
(Function_Call
) = N_Function_Call
);
9507 pragma Assert
(Convention
(Function_Id
) = Convention_CPP
9508 and then Is_Constructor
(Function_Id
));
9509 pragma Assert
(Is_Constrained
(Underlying_Type
(Result_Subt
)));
9511 -- Replace the initialized allocator of form "new T'(Func (...))" with
9512 -- an uninitialized allocator of form "new T", where T is the result
9513 -- subtype of the called function. The call to the function is handled
9514 -- separately further below.
9517 Make_Allocator
(Loc
,
9518 Expression
=> New_Occurrence_Of
(Result_Subt
, Loc
));
9519 Set_No_Initialization
(New_Allocator
);
9521 -- Copy attributes to new allocator. Note that the new allocator
9522 -- logically comes from source if the original one did, so copy the
9523 -- relevant flag. This ensures proper treatment of the restriction
9524 -- No_Implicit_Heap_Allocations in this case.
9526 Set_Storage_Pool
(New_Allocator
, Storage_Pool
(Allocator
));
9527 Set_Procedure_To_Call
(New_Allocator
, Procedure_To_Call
(Allocator
));
9528 Set_Comes_From_Source
(New_Allocator
, Comes_From_Source
(Allocator
));
9530 Rewrite
(Allocator
, New_Allocator
);
9532 -- Create a new access object and initialize it to the result of the
9533 -- new uninitialized allocator. Note: we do not use Allocator as the
9534 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9535 -- as this would create a sort of infinite "recursion".
9537 Return_Obj_Access
:= Make_Temporary
(Loc
, 'R');
9538 Set_Etype
(Return_Obj_Access
, Acc_Type
);
9541 -- Rnnn : constant ptr_T := new (T);
9542 -- Init (Rnn.all,...);
9545 Make_Object_Declaration
(Loc
,
9546 Defining_Identifier
=> Return_Obj_Access
,
9547 Constant_Present
=> True,
9548 Object_Definition
=> New_Occurrence_Of
(Acc_Type
, Loc
),
9549 Expression
=> Relocate_Node
(Allocator
));
9550 Insert_Action
(Allocator
, Tmp_Obj
);
9552 Insert_List_After_And_Analyze
(Tmp_Obj
,
9553 Build_Initialization_Call
(Loc
,
9555 Make_Explicit_Dereference
(Loc
,
9556 Prefix
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
)),
9557 Typ
=> Etype
(Function_Id
),
9558 Constructor_Ref
=> Function_Call
));
9560 -- Finally, replace the allocator node with a reference to the result of
9561 -- the function call itself (which will effectively be an access to the
9562 -- object created by the allocator).
9564 Rewrite
(Allocator
, New_Occurrence_Of
(Return_Obj_Access
, Loc
));
9566 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9567 -- generate an implicit conversion to force displacement of the "this"
9570 if Is_Interface
(Designated_Type
(Acc_Type
)) then
9571 Rewrite
(Allocator
, Convert_To
(Acc_Type
, Relocate_Node
(Allocator
)));
9574 Analyze_And_Resolve
(Allocator
, Acc_Type
);
9575 end Make_CPP_Constructor_Call_In_Allocator
;
9577 ----------------------
9578 -- Might_Have_Tasks --
9579 ----------------------
9581 function Might_Have_Tasks
(Typ
: Entity_Id
) return Boolean is
9583 return not Global_No_Tasking
9584 and then not No_Run_Time_Mode
9585 and then (Has_Task
(Typ
)
9586 or else (Is_Class_Wide_Type
(Typ
)
9587 and then Is_Limited_Record
(Typ
)
9588 and then not Has_Aspect
9589 (Etype
(Typ
), Aspect_No_Task_Parts
)));
9590 end Might_Have_Tasks
;
9592 ----------------------------
9593 -- Needs_BIP_Task_Actuals --
9594 ----------------------------
9596 function Needs_BIP_Task_Actuals
(Func_Id
: Entity_Id
) return Boolean is
9597 Subp_Id
: Entity_Id
;
9598 Func_Typ
: Entity_Id
;
9601 if Global_No_Tasking
or else No_Run_Time_Mode
then
9605 -- For thunks we must rely on their target entity; otherwise, given that
9606 -- the profile of thunks for functions returning a limited interface
9607 -- type returns a class-wide type, we would erroneously add these extra
9610 if Is_Thunk
(Func_Id
) then
9611 Subp_Id
:= Thunk_Target
(Func_Id
);
9619 Func_Typ
:= Underlying_Type
(Etype
(Subp_Id
));
9621 -- Functions returning types with foreign convention don't have extra
9624 if Has_Foreign_Convention
(Func_Typ
) then
9627 -- At first sight, for all the following cases, we could add assertions
9628 -- to ensure that if Func_Id is frozen then the computed result matches
9629 -- with the availability of the task master extra formal; unfortunately
9630 -- this is not feasible because we may be precisely freezing this entity
9631 -- (that is, Is_Frozen has been set by Freeze_Entity but it has not
9632 -- completed its work).
9634 elsif Has_Task
(Func_Typ
) then
9637 elsif Ekind
(Func_Id
) = E_Function
then
9638 return Might_Have_Tasks
(Func_Typ
);
9640 -- Handle subprogram type internally generated for dispatching call. We
9641 -- cannot rely on the return type of the subprogram type of dispatching
9642 -- calls since it is always a class-wide type (cf. Expand_Dispatching_
9645 elsif Ekind
(Func_Id
) = E_Subprogram_Type
then
9646 if Is_Dispatch_Table_Entity
(Func_Id
) then
9647 return Has_BIP_Extra_Formal
(Func_Id
, BIP_Task_Master
);
9649 return Might_Have_Tasks
(Func_Typ
);
9653 raise Program_Error
;
9655 end Needs_BIP_Task_Actuals
;
9657 -----------------------------------
9658 -- Needs_BIP_Finalization_Master --
9659 -----------------------------------
9661 function Needs_BIP_Finalization_Master
(Func_Id
: Entity_Id
) return Boolean
9663 Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
9666 -- A formal giving the finalization master is needed for build-in-place
9667 -- functions whose result type needs finalization or is a tagged type.
9668 -- Tagged primitive build-in-place functions need such a formal because
9669 -- they can be called by a dispatching call, and extensions may require
9670 -- finalization even if the root type doesn't. This means nonprimitive
9671 -- build-in-place functions with tagged results also need it, since such
9672 -- functions can be called via access-to-function types, and those can
9673 -- be used to call primitives, so the formal needs to be passed to all
9674 -- such build-in-place functions, primitive or not.
9676 return not Restriction_Active
(No_Finalization
)
9677 and then (Needs_Finalization
(Typ
) or else Is_Tagged_Type
(Typ
))
9678 and then not Has_Foreign_Convention
(Typ
);
9679 end Needs_BIP_Finalization_Master
;
9681 --------------------------
9682 -- Needs_BIP_Alloc_Form --
9683 --------------------------
9685 function Needs_BIP_Alloc_Form
(Func_Id
: Entity_Id
) return Boolean is
9686 Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
9689 -- A formal giving the allocation method is needed for build-in-place
9690 -- functions whose result type is returned on the secondary stack or
9691 -- is a tagged type. Tagged primitive build-in-place functions need
9692 -- such a formal because they can be called by a dispatching call, and
9693 -- the secondary stack is always used for dispatching-on-result calls.
9694 -- This means nonprimitive build-in-place functions with tagged results
9695 -- also need it, as such functions can be called via access-to-function
9696 -- types, and those can be used to call primitives, so the formal needs
9697 -- to be passed to all such build-in-place functions, primitive or not.
9699 -- We never use build-in-place if the function has foreign convention,
9700 -- but note that it is OK for a build-in-place function to return a
9701 -- type with a foreign convention because the machinery ensures there
9704 return not Restriction_Active
(No_Secondary_Stack
)
9705 and then (Needs_Secondary_Stack
(Typ
) or else Is_Tagged_Type
(Typ
))
9706 and then not Has_Foreign_Convention
(Func_Id
);
9707 end Needs_BIP_Alloc_Form
;
9709 -------------------------------------
9710 -- Replace_Renaming_Declaration_Id --
9711 -------------------------------------
9713 procedure Replace_Renaming_Declaration_Id
9714 (New_Decl
: Node_Id
;
9715 Orig_Decl
: Node_Id
)
9717 New_Id
: constant Entity_Id
:= Defining_Entity
(New_Decl
);
9718 Orig_Id
: constant Entity_Id
:= Defining_Entity
(Orig_Decl
);
9721 Set_Chars
(New_Id
, Chars
(Orig_Id
));
9723 -- Swap next entity links in preparation for exchanging entities
9726 Next_Id
: constant Entity_Id
:= Next_Entity
(New_Id
);
9728 Link_Entities
(New_Id
, Next_Entity
(Orig_Id
));
9729 Link_Entities
(Orig_Id
, Next_Id
);
9732 Set_Homonym
(New_Id
, Homonym
(Orig_Id
));
9733 Exchange_Entities
(New_Id
, Orig_Id
);
9735 -- Preserve source indication of original declaration, so that xref
9736 -- information is properly generated for the right entity.
9738 Preserve_Comes_From_Source
(New_Decl
, Orig_Decl
);
9739 Preserve_Comes_From_Source
(Orig_Id
, Orig_Decl
);
9741 Set_Comes_From_Source
(New_Id
, False);
9743 -- Preserve aliased indication
9745 Set_Is_Aliased
(Orig_Id
, Is_Aliased
(New_Id
));
9746 end Replace_Renaming_Declaration_Id
;
9748 ---------------------------------
9749 -- Rewrite_Function_Call_For_C --
9750 ---------------------------------
9752 procedure Rewrite_Function_Call_For_C
(N
: Node_Id
) is
9753 Orig_Func
: constant Entity_Id
:= Entity
(Name
(N
));
9754 Func_Id
: constant Entity_Id
:= Ultimate_Alias
(Orig_Func
);
9755 Par
: constant Node_Id
:= Parent
(N
);
9756 Proc_Id
: constant Entity_Id
:= Corresponding_Procedure
(Func_Id
);
9757 Loc
: constant Source_Ptr
:= Sloc
(Par
);
9759 Last_Actual
: Node_Id
;
9760 Last_Formal
: Entity_Id
;
9762 -- Start of processing for Rewrite_Function_Call_For_C
9765 -- The actuals may be given by named associations, so the added actual
9766 -- that is the target of the return value of the call must be a named
9767 -- association as well, so we retrieve the name of the generated
9770 Last_Formal
:= First_Formal
(Proc_Id
);
9771 while Present
(Next_Formal
(Last_Formal
)) loop
9772 Next_Formal
(Last_Formal
);
9775 Actuals
:= Parameter_Associations
(N
);
9777 -- The original function may lack parameters
9779 if No
(Actuals
) then
9780 Actuals
:= New_List
;
9783 -- If the function call is the expression of an assignment statement,
9784 -- transform the assignment into a procedure call. Generate:
9786 -- LHS := Func_Call (...);
9788 -- Proc_Call (..., LHS);
9790 -- If function is inherited, a conversion may be necessary.
9792 if Nkind
(Par
) = N_Assignment_Statement
then
9793 Last_Actual
:= Name
(Par
);
9795 if not Comes_From_Source
(Orig_Func
)
9796 and then Etype
(Orig_Func
) /= Etype
(Func_Id
)
9799 Make_Type_Conversion
(Loc
,
9800 New_Occurrence_Of
(Etype
(Func_Id
), Loc
),
9805 Make_Parameter_Association
(Loc
,
9807 Make_Identifier
(Loc
, Chars
(Last_Formal
)),
9808 Explicit_Actual_Parameter
=> Last_Actual
));
9811 Make_Procedure_Call_Statement
(Loc
,
9812 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
9813 Parameter_Associations
=> Actuals
));
9816 -- Otherwise the context is an expression. Generate a temporary and a
9817 -- procedure call to obtain the function result. Generate:
9819 -- ... Func_Call (...) ...
9822 -- Proc_Call (..., Temp);
9827 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
9836 Make_Object_Declaration
(Loc
,
9837 Defining_Identifier
=> Temp_Id
,
9838 Object_Definition
=>
9839 New_Occurrence_Of
(Etype
(Func_Id
), Loc
));
9842 -- Proc_Call (..., Temp);
9845 Make_Parameter_Association
(Loc
,
9847 Make_Identifier
(Loc
, Chars
(Last_Formal
)),
9848 Explicit_Actual_Parameter
=>
9849 New_Occurrence_Of
(Temp_Id
, Loc
)));
9852 Make_Procedure_Call_Statement
(Loc
,
9853 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
9854 Parameter_Associations
=> Actuals
);
9856 Insert_Actions
(Par
, New_List
(Decl
, Call
));
9857 Rewrite
(N
, New_Occurrence_Of
(Temp_Id
, Loc
));
9860 end Rewrite_Function_Call_For_C
;
9862 ------------------------------------
9863 -- Set_Enclosing_Sec_Stack_Return --
9864 ------------------------------------
9866 procedure Set_Enclosing_Sec_Stack_Return
(N
: Node_Id
) is
9870 -- Due to a possible mix of internally generated blocks, source blocks
9871 -- and loops, the scope stack may not be contiguous as all labels are
9872 -- inserted at the top level within the related function. Instead,
9873 -- perform a parent-based traversal and mark all appropriate constructs.
9875 while Present
(P
) loop
9877 -- Mark the label of a source or internally generated block or
9880 if Nkind
(P
) in N_Block_Statement | N_Loop_Statement
then
9881 Set_Sec_Stack_Needed_For_Return
(Entity
(Identifier
(P
)));
9883 -- Mark the enclosing function
9885 elsif Nkind
(P
) = N_Subprogram_Body
then
9886 if Present
(Corresponding_Spec
(P
)) then
9887 Set_Sec_Stack_Needed_For_Return
(Corresponding_Spec
(P
));
9889 Set_Sec_Stack_Needed_For_Return
(Defining_Entity
(P
));
9892 -- Do not go beyond the enclosing function
9899 end Set_Enclosing_Sec_Stack_Return
;
9901 ------------------------------------
9902 -- Unqual_BIP_Iface_Function_Call --
9903 ------------------------------------
9905 function Unqual_BIP_Iface_Function_Call
(Expr
: Node_Id
) return Node_Id
is
9906 Has_Pointer_Displacement
: Boolean := False;
9907 On_Object_Declaration
: Boolean := False;
9908 -- Remember if processing the renaming expressions on recursion we have
9909 -- traversed an object declaration, since we can traverse many object
9910 -- declaration renamings but just one regular object declaration.
9912 function Unqual_BIP_Function_Call
(Expr
: Node_Id
) return Node_Id
;
9913 -- Search for a build-in-place function call skipping any qualification
9914 -- including qualified expressions, type conversions, references, calls
9915 -- to displace the pointer to the object, and renamings. Return Empty if
9916 -- no build-in-place function call is found.
9918 ------------------------------
9919 -- Unqual_BIP_Function_Call --
9920 ------------------------------
9922 function Unqual_BIP_Function_Call
(Expr
: Node_Id
) return Node_Id
is
9924 -- Recurse to handle case of multiple levels of qualification and/or
9927 if Nkind
(Expr
) in N_Qualified_Expression
9929 | N_Unchecked_Type_Conversion
9931 return Unqual_BIP_Function_Call
(Expression
(Expr
));
9933 -- Recurse to handle case of multiple levels of references and
9934 -- explicit dereferences.
9936 elsif Nkind
(Expr
) in N_Attribute_Reference
9937 | N_Explicit_Dereference
9940 return Unqual_BIP_Function_Call
(Prefix
(Expr
));
9942 -- Recurse on object renamings
9944 elsif Nkind
(Expr
) = N_Identifier
9945 and then Present
(Entity
(Expr
))
9946 and then Ekind
(Entity
(Expr
)) in E_Constant | E_Variable
9947 and then Nkind
(Parent
(Entity
(Expr
))) =
9948 N_Object_Renaming_Declaration
9949 and then Present
(Renamed_Object
(Entity
(Expr
)))
9951 return Unqual_BIP_Function_Call
(Renamed_Object
(Entity
(Expr
)));
9953 -- Recurse on the initializing expression of the first reference of
9954 -- an object declaration.
9956 elsif not On_Object_Declaration
9957 and then Nkind
(Expr
) = N_Identifier
9958 and then Present
(Entity
(Expr
))
9959 and then Ekind
(Entity
(Expr
)) in E_Constant | E_Variable
9960 and then Nkind
(Parent
(Entity
(Expr
))) = N_Object_Declaration
9961 and then Present
(Expression
(Parent
(Entity
(Expr
))))
9963 On_Object_Declaration
:= True;
9965 Unqual_BIP_Function_Call
(Expression
(Parent
(Entity
(Expr
))));
9967 -- Recurse to handle calls to displace the pointer to the object to
9968 -- reference a secondary dispatch table.
9970 elsif Nkind
(Expr
) = N_Function_Call
9971 and then Nkind
(Name
(Expr
)) in N_Has_Entity
9972 and then Present
(Entity
(Name
(Expr
)))
9973 and then Is_RTE
(Entity
(Name
(Expr
)), RE_Displace
)
9975 Has_Pointer_Displacement
:= True;
9977 Unqual_BIP_Function_Call
(First
(Parameter_Associations
(Expr
)));
9979 -- Normal case: check if the inner expression is a BIP function call
9980 -- and the pointer to the object is displaced.
9982 elsif Has_Pointer_Displacement
9983 and then Is_Build_In_Place_Function_Call
(Expr
)
9990 end Unqual_BIP_Function_Call
;
9992 -- Start of processing for Unqual_BIP_Iface_Function_Call
9995 if Nkind
(Expr
) = N_Identifier
and then No
(Entity
(Expr
)) then
9997 -- Can happen for X'Elab_Spec in the binder-generated file
10002 return Unqual_BIP_Function_Call
(Expr
);
10003 end Unqual_BIP_Iface_Function_Call
;
10005 -------------------------------
10006 -- Validate_Subprogram_Calls --
10007 -------------------------------
10009 procedure Validate_Subprogram_Calls
(N
: Node_Id
) is
10011 function Process_Node
(Nod
: Node_Id
) return Traverse_Result
;
10012 -- Function to traverse the subtree of N using Traverse_Proc.
10018 function Process_Node
(Nod
: Node_Id
) return Traverse_Result
is
10020 case Nkind
(Nod
) is
10021 when N_Entry_Call_Statement
10022 | N_Procedure_Call_Statement
10026 Call_Node
: Node_Id
renames Nod
;
10030 -- Call using access to subprogram with explicit dereference
10032 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
10033 Subp
:= Etype
(Name
(Call_Node
));
10035 -- Prefix notation calls
10037 elsif Nkind
(Name
(Call_Node
)) = N_Selected_Component
then
10038 Subp
:= Entity
(Selector_Name
(Name
(Call_Node
)));
10040 -- Call to member of entry family, where Name is an indexed
10041 -- component, with the prefix being a selected component
10042 -- giving the task and entry family name, and the index
10043 -- being the entry index.
10045 elsif Nkind
(Name
(Call_Node
)) = N_Indexed_Component
then
10047 Entity
(Selector_Name
(Prefix
(Name
(Call_Node
))));
10052 Subp
:= Entity
(Name
(Call_Node
));
10055 pragma Assert
(Check_BIP_Actuals
(Call_Node
, Subp
));
10058 -- Skip generic bodies
10060 when N_Package_Body
=>
10061 if Ekind
(Unique_Defining_Entity
(Nod
)) = E_Generic_Package
then
10065 when N_Subprogram_Body
=>
10066 if Ekind
(Unique_Defining_Entity
(Nod
)) in E_Generic_Function
10067 | E_Generic_Procedure
10072 -- Nodes we want to ignore
10074 -- Skip calls placed in the full declaration of record types since
10075 -- the call will be performed by their Init Proc; for example,
10076 -- calls initializing default values of discriminants or calls
10077 -- providing the initial value of record type components. Other
10078 -- full type declarations are processed because they may have
10079 -- calls that must be checked. For example:
10081 -- type T is array (1 .. Some_Function_Call (...)) of Some_Type;
10083 -- ??? More work needed here to handle the following case:
10085 -- type Rec is record
10086 -- F : String (1 .. <some complicated expression>);
10089 when N_Full_Type_Declaration
=>
10090 if Is_Record_Type
(Defining_Entity
(Nod
)) then
10094 -- Skip calls placed in subprogram specifications since function
10095 -- calls initializing default parameter values will be processed
10096 -- when the call to the subprogram is found (if the default actual
10097 -- parameter is required), and calls found in aspects will be
10098 -- processed when their corresponding pragma is found, or in the
10099 -- specific case of class-wide pre-/postconditions, when their
10100 -- helpers are found.
10102 when N_Procedure_Specification
10103 | N_Function_Specification
10107 when N_Abstract_Subprogram_Declaration
10111 | N_Enumeration_Representation_Clause
10112 | N_Enumeration_Type_Definition
10113 | N_Function_Instantiation
10114 | N_Freeze_Generic_Entity
10115 | N_Generic_Function_Renaming_Declaration
10116 | N_Generic_Package_Renaming_Declaration
10117 | N_Generic_Procedure_Renaming_Declaration
10118 | N_Generic_Package_Declaration
10119 | N_Generic_Subprogram_Declaration
10120 | N_Itype_Reference
10121 | N_Number_Declaration
10122 | N_Package_Instantiation
10123 | N_Package_Renaming_Declaration
10125 | N_Procedure_Instantiation
10126 | N_Protected_Type_Declaration
10127 | N_Record_Representation_Clause
10128 | N_Validate_Unchecked_Conversion
10129 | N_Variable_Reference_Marker
10130 | N_Use_Package_Clause
10131 | N_Use_Type_Clause
10143 procedure Check_Calls
is new Traverse_Proc
(Process_Node
);
10145 -- Start of processing for Validate_Subprogram_Calls
10148 -- No action required if we are not generating code or compiling sources
10149 -- that have errors.
10151 if Serious_Errors_Detected
> 0
10152 or else Operating_Mode
/= Generate_Code
10158 end Validate_Subprogram_Calls
;
10164 procedure Warn_BIP
(Func_Call
: Node_Id
) is
10166 if Debug_Flag_Underscore_BB
then
10167 Error_Msg_N
("build-in-place function call??", Func_Call
);