1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2024, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Accessibility
; use Accessibility
;
27 with Atree
; use Atree
;
28 with Aspects
; use Aspects
;
29 with Checks
; use Checks
;
30 with Debug
; use Debug
;
31 with Einfo
; use Einfo
;
32 with Einfo
.Entities
; use Einfo
.Entities
;
33 with Einfo
.Utils
; use Einfo
.Utils
;
34 with Errout
; use Errout
;
35 with Elists
; use Elists
;
36 with Expander
; use Expander
;
37 with Exp_Aggr
; use Exp_Aggr
;
38 with Exp_Atag
; use Exp_Atag
;
39 with Exp_Ch3
; use Exp_Ch3
;
40 with Exp_Ch4
; use Exp_Ch4
;
41 with Exp_Ch7
; use Exp_Ch7
;
42 with Exp_Ch9
; use Exp_Ch9
;
43 with Exp_Dbug
; use Exp_Dbug
;
44 with Exp_Disp
; use Exp_Disp
;
45 with Exp_Dist
; use Exp_Dist
;
46 with Exp_Intr
; use Exp_Intr
;
47 with Exp_Pakd
; use Exp_Pakd
;
48 with Exp_Tss
; use Exp_Tss
;
49 with Exp_Util
; use Exp_Util
;
50 with Freeze
; use Freeze
;
51 with Inline
; use Inline
;
52 with Itypes
; use Itypes
;
54 with Namet
; use Namet
;
55 with Nlists
; use Nlists
;
56 with Nmake
; use Nmake
;
58 with Restrict
; use Restrict
;
59 with Rident
; use Rident
;
60 with Rtsfind
; use Rtsfind
;
62 with Sem_Aux
; use Sem_Aux
;
63 with Sem_Ch6
; use Sem_Ch6
;
64 with Sem_Ch8
; use Sem_Ch8
;
65 with Sem_Ch13
; use Sem_Ch13
;
66 with Sem_Dim
; use Sem_Dim
;
67 with Sem_Disp
; use Sem_Disp
;
68 with Sem_Dist
; use Sem_Dist
;
69 with Sem_Eval
; use Sem_Eval
;
70 with Sem_Mech
; use Sem_Mech
;
71 with Sem_Res
; use Sem_Res
;
72 with Sem_SCIL
; use Sem_SCIL
;
73 with Sem_Util
; use Sem_Util
;
74 use Sem_Util
.Storage_Model_Support
;
75 with Sinfo
; use Sinfo
;
76 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
77 with Sinfo
.Utils
; use Sinfo
.Utils
;
78 with Sinput
; use Sinput
;
79 with Snames
; use Snames
;
80 with Stand
; use Stand
;
81 with 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 Build_Flag_For_Function
(Func_Id
: Entity_Id
) return Entity_Id
;
198 -- Generate code to declare a boolean flag initialized to False in the
199 -- function Func_Id and return the entity for the flag.
201 function Caller_Known_Size
202 (Func_Call
: Node_Id
;
203 Result_Subt
: Entity_Id
) return Boolean;
204 -- True if result subtype is definite or has a size that does not require
205 -- secondary stack usage (i.e. no variant part or components whose type
206 -- depends on discriminants). In particular, untagged types with only
207 -- access discriminants do not require secondary stack use. Note we must
208 -- always use the secondary stack for dispatching-on-result calls.
210 function Check_BIP_Actuals
211 (Subp_Call
: Node_Id
;
212 Subp_Id
: Entity_Id
) return Boolean;
213 -- Given a subprogram call to the given subprogram return True if the
214 -- names of BIP extra actual and formal parameters match, and the number
215 -- of actuals (including extra actuals) matches the number of formals.
217 function Check_Number_Of_Actuals
218 (Subp_Call
: Node_Id
;
219 Subp_Id
: Entity_Id
) return Boolean;
220 -- Given a subprogram call to the given subprogram return True if the
221 -- number of actual parameters (including extra actuals) is correct.
223 procedure Check_Overriding_Operation
(Subp
: Entity_Id
);
224 -- Subp is a dispatching operation. Check whether it may override an
225 -- inherited private operation, in which case its DT entry is that of
226 -- the hidden operation, not the one it may have received earlier.
227 -- This must be done before emitting the code to set the corresponding
228 -- DT to the address of the subprogram. The actual placement of Subp in
229 -- the proper place in the list of primitive operations is done in
230 -- Declare_Inherited_Private_Subprograms, which also has to deal with
231 -- implicit operations. This duplication is unavoidable for now???
233 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
);
234 -- This procedure is called only if the subprogram body N, whose spec
235 -- has the given entity Spec, contains a parameterless recursive call.
236 -- It attempts to generate runtime code to detect if this a case of
237 -- infinite recursion.
239 -- The body is scanned to determine dependencies. If the only external
240 -- dependencies are on a small set of scalar variables, then the values
241 -- of these variables are captured on entry to the subprogram, and if
242 -- the values are not changed for the call, we know immediately that
243 -- we have an infinite recursion.
245 procedure Expand_Actuals
248 Post_Call
: out List_Id
);
249 -- Return a list of actions to take place after the call in Post_Call. The
250 -- call will later be rewritten as an Expression_With_Actions, with the
251 -- Post_Call actions inserted, and the call inside.
253 -- For each actual of an in-out or out parameter which is a numeric (view)
254 -- conversion of the form T (A), where A denotes a variable, we insert the
257 -- Temp : T[ := T (A)];
259 -- prior to the call. Then we replace the actual with a reference to Temp,
260 -- and append the assignment:
262 -- A := TypeA (Temp);
264 -- after the call. Here TypeA is the actual type of variable A. For out
265 -- parameters, the initial declaration has no expression. If A is not an
266 -- entity name, we generate instead:
268 -- Var : TypeA renames A;
269 -- Temp : T := Var; -- omitting expression for out parameter.
271 -- Var := TypeA (Temp);
273 -- For other in-out parameters, we emit the required constraint checks
274 -- before and/or after the call.
276 -- For all parameter modes, actuals that denote components and slices of
277 -- packed arrays are expanded into suitable temporaries.
279 -- For nonscalar objects that are possibly unaligned, add call by copy code
280 -- (copy in for IN and IN OUT, copy out for OUT and IN OUT).
282 -- For OUT and IN OUT parameters, add predicate checks after the call
283 -- based on the predicates of the actual type.
285 procedure Expand_Call_Helper
(N
: Node_Id
; Post_Call
: out List_Id
);
286 -- Does the main work of Expand_Call. Post_Call is as for Expand_Actuals.
288 procedure Expand_Ctrl_Function_Call
(N
: Node_Id
; Use_Sec_Stack
: Boolean);
289 -- N is a function call which returns a controlled object. Transform the
290 -- call into a temporary which retrieves the returned object from the
291 -- primary or secondary stack (Use_Sec_Stack says which) using 'reference.
293 procedure Expand_Non_Function_Return
(N
: Node_Id
);
294 -- Expand a simple return statement found in a procedure body, entry body,
295 -- accept statement, or an extended return statement. Note that all non-
296 -- function returns are simple return statements.
298 function Expand_Protected_Object_Reference
300 Scop
: Entity_Id
) return Node_Id
;
302 procedure Expand_Protected_Subprogram_Call
306 -- A call to a protected subprogram within the protected object may appear
307 -- as a regular call. The list of actuals must be expanded to contain a
308 -- reference to the object itself, and the call becomes a call to the
309 -- corresponding protected subprogram.
311 procedure Expand_Simple_Function_Return
(N
: Node_Id
);
312 -- Expand simple return from function. In the case where we are returning
313 -- from a function body this is called by Expand_N_Simple_Return_Statement.
315 procedure Insert_Post_Call_Actions
(N
: Node_Id
; Post_Call
: List_Id
);
316 -- Insert the Post_Call list previously produced by routine Expand_Actuals
317 -- or Expand_Call_Helper into the tree.
319 function Is_Function_Call_With_BIP_Formals
(N
: Node_Id
) return Boolean;
320 -- Ada 2005 (AI-318-02): Returns True if N denotes a call to a function
321 -- that requires handling as a build-in-place call, that is, BIP function
322 -- calls and calls to functions with inherited BIP formals. For example:
324 -- type Iface is limited interface;
325 -- function Get_Object return Iface;
326 -- -- This function has BIP extra formals
328 -- type Root1 is limited tagged record ...
329 -- type T1 is new Root1 and Iface with ...
330 -- function Get_Object return T1;
331 -- -- This primitive requires the BIP formals, and the evaluation of
332 -- -- Is_Build_In_Place_Function_Call returns True.
334 -- type Root2 is tagged record ...
335 -- type T2 is new Root2 and Iface with ...
336 -- function Get_Object return T2;
337 -- -- This primitive inherits the BIP formals of the interface primitive
338 -- -- but, given that T2 is not a limited type, it does not require such
339 -- -- formals; therefore Is_Build_In_Place_Function_Call returns False.
341 procedure Replace_Renaming_Declaration_Id
343 Orig_Decl
: Node_Id
);
344 -- Replace the internal identifier of the new renaming declaration New_Decl
345 -- with the identifier of its original declaration Orig_Decl exchanging the
346 -- entities containing their defining identifiers to ensure the correct
347 -- replacement of the object declaration by the object renaming declaration
348 -- to avoid homograph conflicts (since the object declaration's defining
349 -- identifier was already entered in the current scope). The Next_Entity
350 -- links of the two entities are also swapped since the entities are part
351 -- of the return scope's entity list and the list structure would otherwise
352 -- be corrupted. The homonym chain is preserved as well.
354 procedure Rewrite_Function_Call_For_C
(N
: Node_Id
);
355 -- When generating C code, replace a call to a function that returns an
356 -- array into the generated procedure with an additional out parameter.
358 procedure Set_Enclosing_Sec_Stack_Return
(N
: Node_Id
);
359 -- N is a return statement for a function that returns its result on the
360 -- secondary stack. This sets the Sec_Stack_Needed_For_Return flag on the
361 -- function and all blocks and loops that the return statement is jumping
362 -- out of. This ensures that the secondary stack is not released; otherwise
363 -- the function result would be reclaimed before returning to the caller.
365 procedure Warn_BIP
(Func_Call
: Node_Id
);
366 -- Give a warning on a build-in-place function call if the -gnatd_B switch
369 ----------------------------------------------
370 -- Add_Access_Actual_To_Build_In_Place_Call --
371 ----------------------------------------------
373 procedure Add_Access_Actual_To_Build_In_Place_Call
374 (Function_Call
: Node_Id
;
375 Function_Id
: Entity_Id
;
376 Return_Object
: Node_Id
;
377 Is_Access
: Boolean := False)
379 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
380 Obj_Address
: Node_Id
;
381 Obj_Acc_Formal
: Entity_Id
;
384 -- Locate the implicit access parameter in the called function
386 Obj_Acc_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Object_Access
);
388 -- If no return object is provided, then pass null
390 if No
(Return_Object
) then
391 Obj_Address
:= Make_Null
(Loc
);
392 Set_Parent
(Obj_Address
, Function_Call
);
394 -- If Return_Object is already an expression of an access type, then use
395 -- it directly, since it must be an access value denoting the return
396 -- object, and couldn't possibly be the return object itself.
399 Obj_Address
:= Return_Object
;
400 Set_Parent
(Obj_Address
, Function_Call
);
402 -- Apply Unrestricted_Access to caller's return object
406 Make_Attribute_Reference
(Loc
,
407 Prefix
=> Return_Object
,
408 Attribute_Name
=> Name_Unrestricted_Access
);
410 Set_Parent
(Return_Object
, Obj_Address
);
411 Set_Parent
(Obj_Address
, Function_Call
);
414 Analyze_And_Resolve
(Obj_Address
, Etype
(Obj_Acc_Formal
));
416 -- Build the parameter association for the new actual and add it to the
417 -- end of the function's actuals.
419 Add_Extra_Actual_To_Call
(Function_Call
, Obj_Acc_Formal
, Obj_Address
);
420 end Add_Access_Actual_To_Build_In_Place_Call
;
422 ------------------------------------------------------
423 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
424 ------------------------------------------------------
426 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
427 (Function_Call
: Node_Id
;
428 Function_Id
: Entity_Id
;
429 Alloc_Form
: BIP_Allocation_Form
:= Unspecified
;
430 Alloc_Form_Exp
: Node_Id
:= Empty
;
431 Pool_Actual
: Node_Id
:= Make_Null
(No_Location
))
433 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
435 Alloc_Form_Actual
: Node_Id
;
436 Alloc_Form_Formal
: Node_Id
;
437 Pool_Formal
: Node_Id
;
440 -- Nothing to do when the size of the object is known, and the caller is
441 -- in charge of allocating it, and the callee doesn't unconditionally
442 -- require an allocation form (such as due to having a tagged result).
444 if not Needs_BIP_Alloc_Form
(Function_Id
) then
448 -- Locate the implicit allocation form parameter in the called function.
449 -- Maybe it would be better for each implicit formal of a build-in-place
450 -- function to have a flag or a Uint attribute to identify it. ???
452 Alloc_Form_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Alloc_Form
);
454 if Present
(Alloc_Form_Exp
) then
455 pragma Assert
(Alloc_Form
= Unspecified
);
457 Alloc_Form_Actual
:= Alloc_Form_Exp
;
460 pragma Assert
(Alloc_Form
/= Unspecified
);
463 Make_Integer_Literal
(Loc
,
464 Intval
=> UI_From_Int
(BIP_Allocation_Form
'Pos (Alloc_Form
)));
467 Analyze_And_Resolve
(Alloc_Form_Actual
, Etype
(Alloc_Form_Formal
));
469 -- Build the parameter association for the new actual and add it to the
470 -- end of the function's actuals.
472 Add_Extra_Actual_To_Call
473 (Function_Call
, Alloc_Form_Formal
, Alloc_Form_Actual
);
475 -- Pass the Storage_Pool parameter. This parameter is omitted on ZFP as
476 -- those targets do not support pools.
478 if RTE_Available
(RE_Root_Storage_Pool_Ptr
) then
479 Pool_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Storage_Pool
);
480 Analyze_And_Resolve
(Pool_Actual
, Etype
(Pool_Formal
));
481 Add_Extra_Actual_To_Call
482 (Function_Call
, Pool_Formal
, Pool_Actual
);
484 end Add_Unconstrained_Actuals_To_Build_In_Place_Call
;
486 -----------------------------------------------------------
487 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
488 -----------------------------------------------------------
490 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
491 (Func_Call
: Node_Id
;
493 Ptr_Typ
: Entity_Id
:= Empty
;
494 Master_Exp
: Node_Id
:= Empty
)
497 if not Needs_BIP_Finalization_Master
(Func_Id
) then
502 Formal
: constant Entity_Id
:=
503 Build_In_Place_Formal
(Func_Id
, BIP_Finalization_Master
);
504 Loc
: constant Source_Ptr
:= Sloc
(Func_Call
);
507 Desig_Typ
: Entity_Id
;
510 pragma Assert
(Present
(Formal
));
512 -- If there is a finalization master actual, such as the implicit
513 -- finalization master of an enclosing build-in-place function,
514 -- then this must be added as an extra actual of the call.
516 if Present
(Master_Exp
) then
517 Actual
:= Master_Exp
;
519 -- Case where the context does not require an actual master
521 elsif No
(Ptr_Typ
) then
522 Actual
:= Make_Null
(Loc
);
525 Desig_Typ
:= Directly_Designated_Type
(Ptr_Typ
);
527 -- Check for a library-level access type whose designated type has
528 -- suppressed finalization or the access type is subject to pragma
529 -- No_Heap_Finalization. Such an access type lacks a master. Pass
530 -- a null actual to callee in order to signal a missing master.
532 if Is_Library_Level_Entity
(Ptr_Typ
)
533 and then (Finalize_Storage_Only
(Desig_Typ
)
534 or else No_Heap_Finalization
(Ptr_Typ
))
536 Actual
:= Make_Null
(Loc
);
538 -- Types in need of finalization actions
540 elsif Needs_Finalization
(Desig_Typ
) then
542 -- The general mechanism of creating finalization masters for
543 -- anonymous access types is disabled by default, otherwise
544 -- finalization masters will pop all over the place. Such types
545 -- use context-specific masters.
547 if Ekind
(Ptr_Typ
) = E_Anonymous_Access_Type
548 and then No
(Finalization_Master
(Ptr_Typ
))
550 Build_Anonymous_Master
(Ptr_Typ
);
553 -- Access-to-controlled types should always have a master
555 pragma Assert
(Present
(Finalization_Master
(Ptr_Typ
)));
558 Make_Attribute_Reference
(Loc
,
560 New_Occurrence_Of
(Finalization_Master
(Ptr_Typ
), Loc
),
561 Attribute_Name
=> Name_Unrestricted_Access
);
566 Actual
:= Make_Null
(Loc
);
570 Analyze_And_Resolve
(Actual
, Etype
(Formal
));
572 -- Build the parameter association for the new actual and add it to
573 -- the end of the function's actuals.
575 Add_Extra_Actual_To_Call
(Func_Call
, Formal
, Actual
);
577 end Add_Finalization_Master_Actual_To_Build_In_Place_Call
;
579 ------------------------------
580 -- Add_Extra_Actual_To_Call --
581 ------------------------------
583 procedure Add_Extra_Actual_To_Call
584 (Subprogram_Call
: Node_Id
;
585 Extra_Formal
: Entity_Id
;
586 Extra_Actual
: Node_Id
)
588 Loc
: constant Source_Ptr
:= Sloc
(Subprogram_Call
);
589 Param_Assoc
: Node_Id
;
593 Make_Parameter_Association
(Loc
,
594 Selector_Name
=> New_Occurrence_Of
(Extra_Formal
, Loc
),
595 Explicit_Actual_Parameter
=> Extra_Actual
);
597 Set_Parent
(Param_Assoc
, Subprogram_Call
);
598 Set_Parent
(Extra_Actual
, Param_Assoc
);
600 if Present
(Parameter_Associations
(Subprogram_Call
)) then
601 if Nkind
(Last
(Parameter_Associations
(Subprogram_Call
))) =
602 N_Parameter_Association
605 -- Find last named actual, and append
610 L
:= First_Actual
(Subprogram_Call
);
611 while Present
(L
) loop
612 if No
(Next_Actual
(L
)) then
613 Set_Next_Named_Actual
(Parent
(L
), Extra_Actual
);
621 Set_First_Named_Actual
(Subprogram_Call
, Extra_Actual
);
624 Append
(Param_Assoc
, To
=> Parameter_Associations
(Subprogram_Call
));
627 Set_Parameter_Associations
(Subprogram_Call
, New_List
(Param_Assoc
));
628 Set_First_Named_Actual
(Subprogram_Call
, Extra_Actual
);
630 end Add_Extra_Actual_To_Call
;
632 ---------------------------------------------
633 -- Add_Task_Actuals_To_Build_In_Place_Call --
634 ---------------------------------------------
636 procedure Add_Task_Actuals_To_Build_In_Place_Call
637 (Function_Call
: Node_Id
;
638 Function_Id
: Entity_Id
;
639 Master_Actual
: Node_Id
;
640 Chain
: Node_Id
:= Empty
)
642 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
644 Chain_Actual
: Node_Id
;
645 Chain_Formal
: Node_Id
;
646 Master_Formal
: Node_Id
;
649 -- No such extra parameters are needed if there are no tasks
651 if not Needs_BIP_Task_Actuals
(Function_Id
) then
653 -- However we must add dummy extra actuals if the function is
654 -- a dispatching operation that inherited these extra formals.
656 if Is_Dispatching_Operation
(Function_Id
)
657 and then Has_BIP_Extra_Formal
(Function_Id
, BIP_Task_Master
)
660 Build_In_Place_Formal
(Function_Id
, BIP_Task_Master
);
661 Actual
:= Make_Integer_Literal
(Loc
, Uint_0
);
662 Analyze_And_Resolve
(Actual
, Etype
(Master_Formal
));
663 Add_Extra_Actual_To_Call
(Function_Call
, Master_Formal
, Actual
);
666 Build_In_Place_Formal
(Function_Id
, BIP_Activation_Chain
);
667 Chain_Actual
:= Make_Null
(Loc
);
668 Analyze_And_Resolve
(Chain_Actual
, Etype
(Chain_Formal
));
669 Add_Extra_Actual_To_Call
670 (Function_Call
, Chain_Formal
, Chain_Actual
);
676 Actual
:= Master_Actual
;
678 -- Use a dummy _master actual in case of No_Task_Hierarchy
680 if Restriction_Active
(No_Task_Hierarchy
) then
681 Actual
:= Make_Integer_Literal
(Loc
, Library_Task_Level
);
683 -- In the case where we use the master associated with an access type,
684 -- the actual is an entity and requires an explicit reference.
686 elsif Nkind
(Actual
) = N_Defining_Identifier
then
687 Actual
:= New_Occurrence_Of
(Actual
, Loc
);
690 -- Locate the implicit master parameter in the called function
692 Master_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Task_Master
);
693 Analyze_And_Resolve
(Actual
, Etype
(Master_Formal
));
695 -- Build the parameter association for the new actual and add it to the
696 -- end of the function's actuals.
698 Add_Extra_Actual_To_Call
(Function_Call
, Master_Formal
, Actual
);
700 -- Locate the implicit activation chain parameter in the called function
703 Build_In_Place_Formal
(Function_Id
, BIP_Activation_Chain
);
705 -- Create the actual which is a pointer to the current activation chain
707 if Restriction_Active
(No_Task_Hierarchy
) then
708 Chain_Actual
:= Make_Null
(Loc
);
710 elsif No
(Chain
) then
712 Make_Attribute_Reference
(Loc
,
713 Prefix
=> Make_Identifier
(Loc
, Name_uChain
),
714 Attribute_Name
=> Name_Unrestricted_Access
);
716 -- Allocator case; make a reference to the Chain passed in by the caller
720 Make_Attribute_Reference
(Loc
,
721 Prefix
=> New_Occurrence_Of
(Chain
, Loc
),
722 Attribute_Name
=> Name_Unrestricted_Access
);
725 Analyze_And_Resolve
(Chain_Actual
, Etype
(Chain_Formal
));
727 -- Build the parameter association for the new actual and add it to the
728 -- end of the function's actuals.
730 Add_Extra_Actual_To_Call
(Function_Call
, Chain_Formal
, Chain_Actual
);
731 end Add_Task_Actuals_To_Build_In_Place_Call
;
733 ----------------------------------
734 -- Apply_CW_Accessibility_Check --
735 ----------------------------------
737 procedure Apply_CW_Accessibility_Check
(Exp
: Node_Id
; Func
: Entity_Id
) is
738 Loc
: constant Source_Ptr
:= Sloc
(Exp
);
741 -- CodePeer does not do anything useful on Ada.Tags.Type_Specific_Data
744 if Ada_Version
>= Ada_2005
745 and then not CodePeer_Mode
746 and then Tagged_Type_Expansion
747 and then not Scope_Suppress
.Suppress
(Accessibility_Check
)
749 (Is_Class_Wide_Type
(Etype
(Exp
))
750 or else Nkind
(Exp
) in
751 N_Type_Conversion | N_Unchecked_Type_Conversion
752 or else (Is_Entity_Name
(Exp
)
753 and then Is_Formal
(Entity
(Exp
)))
754 or else Scope_Depth
(Enclosing_Dynamic_Scope
(Etype
(Exp
))) >
755 Scope_Depth
(Enclosing_Dynamic_Scope
(Func
)))
761 -- Ada 2005 (AI-251): In class-wide interface objects we displace
762 -- "this" to reference the base of the object. This is required to
763 -- get access to the TSD of the object.
765 if Is_Class_Wide_Type
(Etype
(Exp
))
766 and then Is_Interface
(Etype
(Exp
))
768 -- If the expression is an explicit dereference then we can
769 -- directly displace the pointer to reference the base of
772 if Nkind
(Exp
) = N_Explicit_Dereference
then
774 Make_Explicit_Dereference
(Loc
,
776 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
777 Make_Function_Call
(Loc
,
779 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
780 Parameter_Associations
=> New_List
(
781 Unchecked_Convert_To
(RTE
(RE_Address
),
782 Duplicate_Subexpr
(Prefix
(Exp
)))))));
784 -- Similar case to the previous one but the expression is a
785 -- renaming of an explicit dereference.
787 elsif Nkind
(Exp
) = N_Identifier
788 and then Present
(Renamed_Object
(Entity
(Exp
)))
789 and then Nkind
(Renamed_Object
(Entity
(Exp
)))
790 = N_Explicit_Dereference
793 Make_Explicit_Dereference
(Loc
,
795 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
796 Make_Function_Call
(Loc
,
798 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
799 Parameter_Associations
=> New_List
(
800 Unchecked_Convert_To
(RTE
(RE_Address
),
803 (Renamed_Object
(Entity
(Exp
)))))))));
805 -- Common case: obtain the address of the actual object and
806 -- displace the pointer to reference the base of the object.
810 Make_Explicit_Dereference
(Loc
,
812 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
813 Make_Function_Call
(Loc
,
815 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
816 Parameter_Associations
=> New_List
(
817 Make_Attribute_Reference
(Loc
,
818 Prefix
=> Duplicate_Subexpr
(Exp
),
819 Attribute_Name
=> Name_Address
)))));
823 Make_Attribute_Reference
(Loc
,
824 Prefix
=> Duplicate_Subexpr
(Exp
),
825 Attribute_Name
=> Name_Tag
);
828 -- Suppress junk access chacks on RE_Tag_Ptr
831 Make_Raise_Program_Error
(Loc
,
834 Left_Opnd
=> Build_Get_Access_Level
(Loc
, Tag_Node
),
836 Make_Integer_Literal
(Loc
,
837 Scope_Depth
(Enclosing_Dynamic_Scope
(Func
)))),
838 Reason
=> PE_Accessibility_Check_Failed
),
839 Suppress
=> Access_Check
);
842 end Apply_CW_Accessibility_Check
;
844 -----------------------
845 -- BIP_Formal_Suffix --
846 -----------------------
848 function BIP_Formal_Suffix
(Kind
: BIP_Formal_Kind
) return String is
851 when BIP_Alloc_Form
=>
852 return BIP_Alloc_Suffix
;
854 when BIP_Storage_Pool
=>
855 return BIP_Storage_Pool_Suffix
;
857 when BIP_Finalization_Master
=>
858 return BIP_Finalization_Master_Suffix
;
860 when BIP_Task_Master
=>
861 return BIP_Task_Master_Suffix
;
863 when BIP_Activation_Chain
=>
864 return BIP_Activation_Chain_Suffix
;
866 when BIP_Object_Access
=>
867 return BIP_Object_Access_Suffix
;
869 end BIP_Formal_Suffix
;
871 ---------------------
872 -- BIP_Suffix_Kind --
873 ---------------------
875 function BIP_Suffix_Kind
(E
: Entity_Id
) return BIP_Formal_Kind
is
876 Nam
: constant String := Get_Name_String
(Chars
(E
));
878 function Has_Suffix
(Suffix
: String) return Boolean;
879 -- Return True if Nam has suffix Suffix
881 function Has_Suffix
(Suffix
: String) return Boolean is
882 Len
: constant Natural := Suffix
'Length;
884 return Nam
'Length > Len
885 and then Nam
(Nam
'Last - Len
+ 1 .. Nam
'Last) = Suffix
;
888 -- Start of processing for BIP_Suffix_Kind
891 if Has_Suffix
(BIP_Alloc_Suffix
) then
892 return BIP_Alloc_Form
;
894 elsif Has_Suffix
(BIP_Storage_Pool_Suffix
) then
895 return BIP_Storage_Pool
;
897 elsif Has_Suffix
(BIP_Finalization_Master_Suffix
) then
898 return BIP_Finalization_Master
;
900 elsif Has_Suffix
(BIP_Task_Master_Suffix
) then
901 return BIP_Task_Master
;
903 elsif Has_Suffix
(BIP_Activation_Chain_Suffix
) then
904 return BIP_Activation_Chain
;
906 elsif Has_Suffix
(BIP_Object_Access_Suffix
) then
907 return BIP_Object_Access
;
914 -----------------------------
915 -- Build_Flag_For_Function --
916 -----------------------------
918 function Build_Flag_For_Function
(Func_Id
: Entity_Id
) return Entity_Id
is
925 -- Recover the function body
927 Func_Bod
:= Unit_Declaration_Node
(Func_Id
);
929 if Nkind
(Func_Bod
) = N_Subprogram_Declaration
then
930 Func_Bod
:= Parent
(Parent
(Corresponding_Body
(Func_Bod
)));
933 if Nkind
(Func_Bod
) = N_Function_Specification
then
934 Func_Bod
:= Parent
(Func_Bod
); -- one more level for child units
937 pragma Assert
(Nkind
(Func_Bod
) = N_Subprogram_Body
);
939 Loc
:= Sloc
(Func_Bod
);
941 -- Create a flag to track the function state
943 Flag_Id
:= Make_Temporary
(Loc
, 'F');
945 -- Insert the flag at the beginning of the function declarations,
947 -- Fnn : Boolean := False;
950 Make_Object_Declaration
(Loc
,
951 Defining_Identifier
=> Flag_Id
,
952 Object_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
),
953 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
));
955 Prepend_To
(Declarations
(Func_Bod
), Flag_Decl
);
959 end Build_Flag_For_Function
;
961 ---------------------------
962 -- Build_In_Place_Formal --
963 ---------------------------
965 function Build_In_Place_Formal
967 Kind
: BIP_Formal_Kind
) return Entity_Id
969 Extra_Formal
: Entity_Id
:= Extra_Formals
(Func
);
970 Formal_Suffix
: constant String := BIP_Formal_Suffix
(Kind
);
973 -- Maybe it would be better for each implicit formal of a build-in-place
974 -- function to have a flag or a Uint attribute to identify it. ???
976 -- The return type in the function declaration may have been a limited
977 -- view, and the extra formals for the function were not generated at
978 -- that point. At the point of call the full view must be available and
979 -- the extra formals can be created and Returns_By_Ref computed.
981 if No
(Extra_Formal
) then
982 Create_Extra_Formals
(Func
);
983 Extra_Formal
:= Extra_Formals
(Func
);
984 Compute_Returns_By_Ref
(Func
);
987 -- We search for a formal with a matching suffix. We can't search
988 -- for the full name, because of the code at the end of Sem_Ch6.-
989 -- Create_Extra_Formals, which copies the Extra_Formals over to
990 -- the Alias of an instance, which will cause the formals to have
991 -- "incorrect" names.
993 while Present
(Extra_Formal
) loop
995 Name
: constant String := Get_Name_String
(Chars
(Extra_Formal
));
997 exit when Name
'Length >= Formal_Suffix
'Length
998 and then Formal_Suffix
=
999 Name
(Name
'Last - Formal_Suffix
'Length + 1 .. Name
'Last);
1002 Next_Formal_With_Extras
(Extra_Formal
);
1005 if No
(Extra_Formal
) then
1006 raise Program_Error
;
1009 return Extra_Formal
;
1010 end Build_In_Place_Formal
;
1012 -------------------------------
1013 -- Build_Procedure_Body_Form --
1014 -------------------------------
1016 function Build_Procedure_Body_Form
1017 (Func_Id
: Entity_Id
;
1018 Func_Body
: Node_Id
) return Node_Id
1020 Loc
: constant Source_Ptr
:= Sloc
(Func_Body
);
1022 Proc_Decl
: constant Node_Id
:= Prev
(Unit_Declaration_Node
(Func_Id
));
1023 -- It is assumed that the node before the declaration of the
1024 -- corresponding subprogram spec is the declaration of the procedure
1027 Proc_Id
: constant Entity_Id
:= Defining_Entity
(Proc_Decl
);
1029 procedure Replace_Returns
(Param_Id
: Entity_Id
; Stmts
: List_Id
);
1030 -- Replace each return statement found in the list Stmts with an
1031 -- assignment of the return expression to parameter Param_Id.
1033 ---------------------
1034 -- Replace_Returns --
1035 ---------------------
1037 procedure Replace_Returns
(Param_Id
: Entity_Id
; Stmts
: List_Id
) is
1041 Stmt
:= First
(Stmts
);
1042 while Present
(Stmt
) loop
1043 if Nkind
(Stmt
) = N_Block_Statement
then
1044 Replace_Returns
(Param_Id
,
1045 Statements
(Handled_Statement_Sequence
(Stmt
)));
1047 elsif Nkind
(Stmt
) = N_Case_Statement
then
1051 Alt
:= First
(Alternatives
(Stmt
));
1052 while Present
(Alt
) loop
1053 Replace_Returns
(Param_Id
, Statements
(Alt
));
1058 elsif Nkind
(Stmt
) = N_Extended_Return_Statement
then
1060 Ret_Obj
: constant Entity_Id
:=
1062 (First
(Return_Object_Declarations
(Stmt
)));
1063 Assign
: constant Node_Id
:=
1064 Make_Assignment_Statement
(Sloc
(Stmt
),
1066 New_Occurrence_Of
(Param_Id
, Loc
),
1068 New_Occurrence_Of
(Ret_Obj
, Sloc
(Stmt
)));
1072 -- The extended return may just contain the declaration
1074 if Present
(Handled_Statement_Sequence
(Stmt
)) then
1075 Stmts
:= Statements
(Handled_Statement_Sequence
(Stmt
));
1080 Set_Assignment_OK
(Name
(Assign
));
1083 Make_Block_Statement
(Sloc
(Stmt
),
1085 Return_Object_Declarations
(Stmt
),
1086 Handled_Statement_Sequence
=>
1087 Make_Handled_Sequence_Of_Statements
(Loc
,
1088 Statements
=> Stmts
)));
1090 Replace_Returns
(Param_Id
, Stmts
);
1092 Append_To
(Stmts
, Assign
);
1093 Append_To
(Stmts
, Make_Simple_Return_Statement
(Loc
));
1096 elsif Nkind
(Stmt
) = N_If_Statement
then
1097 Replace_Returns
(Param_Id
, Then_Statements
(Stmt
));
1098 Replace_Returns
(Param_Id
, Else_Statements
(Stmt
));
1103 Part
:= First
(Elsif_Parts
(Stmt
));
1104 while Present
(Part
) loop
1105 Replace_Returns
(Param_Id
, Then_Statements
(Part
));
1110 elsif Nkind
(Stmt
) = N_Loop_Statement
then
1111 Replace_Returns
(Param_Id
, Statements
(Stmt
));
1113 elsif Nkind
(Stmt
) = N_Simple_Return_Statement
then
1120 Make_Assignment_Statement
(Sloc
(Stmt
),
1121 Name
=> New_Occurrence_Of
(Param_Id
, Loc
),
1122 Expression
=> Relocate_Node
(Expression
(Stmt
))));
1124 Insert_After
(Stmt
, Make_Simple_Return_Statement
(Loc
));
1126 -- Skip the added return
1133 end Replace_Returns
;
1140 -- Start of processing for Build_Procedure_Body_Form
1143 -- This routine replaces the original function body:
1145 -- function F (...) return Array_Typ is
1148 -- return Something;
1151 -- with the following:
1153 -- procedure P (..., Result : out Array_Typ) is
1156 -- Result := Something;
1160 Statements
(Handled_Statement_Sequence
(Func_Body
));
1161 Replace_Returns
(Last_Entity
(Proc_Id
), Stmts
);
1164 Make_Subprogram_Body
(Loc
,
1166 Copy_Subprogram_Spec
(Specification
(Proc_Decl
)),
1167 Declarations
=> Declarations
(Func_Body
),
1168 Handled_Statement_Sequence
=>
1169 Make_Handled_Sequence_Of_Statements
(Loc
,
1170 Statements
=> Stmts
));
1172 -- If the function is a generic instance, so is the new procedure.
1173 -- Set flag accordingly so that the proper renaming declarations are
1176 Set_Is_Generic_Instance
(Proc_Id
, Is_Generic_Instance
(Func_Id
));
1178 end Build_Procedure_Body_Form
;
1180 -----------------------
1181 -- Caller_Known_Size --
1182 -----------------------
1184 function Caller_Known_Size
1185 (Func_Call
: Node_Id
;
1186 Result_Subt
: Entity_Id
) return Boolean
1188 Utyp
: constant Entity_Id
:= Underlying_Type
(Result_Subt
);
1191 return not Needs_Secondary_Stack
(Utyp
)
1192 and then not (Is_Tagged_Type
(Utyp
)
1193 and then Present
(Controlling_Argument
(Func_Call
)));
1194 end Caller_Known_Size
;
1196 -----------------------
1197 -- Check_BIP_Actuals --
1198 -----------------------
1200 function Check_BIP_Actuals
1201 (Subp_Call
: Node_Id
;
1202 Subp_Id
: Entity_Id
) return Boolean
1208 pragma Assert
(Nkind
(Subp_Call
) in N_Entry_Call_Statement
1210 | N_Procedure_Call_Statement
);
1212 -- In CodePeer_Mode, the tree for `'Elab_Spec` procedures will be
1213 -- malformed because GNAT does not perform the usual expansion that
1214 -- results in the importation of external elaboration procedure symbols.
1215 -- This is expected: the CodePeer backend has special handling for this
1217 -- Thus, we do not need to check the tree (and in fact can't, because
1221 and then Nkind
(Name
(Subp_Call
)) = N_Attribute_Reference
1222 and then Attribute_Name
(Name
(Subp_Call
)) in Name_Elab_Spec
1224 | Name_Elab_Subp_Body
1229 Formal
:= First_Formal_With_Extras
(Subp_Id
);
1230 Actual
:= First_Actual
(Subp_Call
);
1232 while Present
(Formal
) and then Present
(Actual
) loop
1233 if Is_Build_In_Place_Entity
(Formal
)
1234 and then Nkind
(Actual
) = N_Identifier
1235 and then Is_Build_In_Place_Entity
(Entity
(Actual
))
1236 and then BIP_Suffix_Kind
(Formal
)
1237 /= BIP_Suffix_Kind
(Entity
(Actual
))
1242 Next_Formal_With_Extras
(Formal
);
1243 Next_Actual
(Actual
);
1246 return No
(Formal
) and then No
(Actual
);
1247 end Check_BIP_Actuals
;
1249 -----------------------------
1250 -- Check_Number_Of_Actuals --
1251 -----------------------------
1253 function Check_Number_Of_Actuals
1254 (Subp_Call
: Node_Id
;
1255 Subp_Id
: Entity_Id
) return Boolean
1261 pragma Assert
(Nkind
(Subp_Call
) in N_Entry_Call_Statement
1263 | N_Procedure_Call_Statement
);
1265 Formal
:= First_Formal_With_Extras
(Subp_Id
);
1266 Actual
:= First_Actual
(Subp_Call
);
1268 while Present
(Formal
) and then Present
(Actual
) loop
1269 Next_Formal_With_Extras
(Formal
);
1270 Next_Actual
(Actual
);
1273 return No
(Formal
) and then No
(Actual
);
1274 end Check_Number_Of_Actuals
;
1276 --------------------------------
1277 -- Check_Overriding_Operation --
1278 --------------------------------
1280 procedure Check_Overriding_Operation
(Subp
: Entity_Id
) is
1281 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
1282 Op_List
: constant Elist_Id
:= Primitive_Operations
(Typ
);
1284 Prim_Op
: Entity_Id
;
1288 if Is_Derived_Type
(Typ
)
1289 and then not Is_Private_Type
(Typ
)
1290 and then In_Open_Scopes
(Scope
(Etype
(Typ
)))
1291 and then Is_Base_Type
(Typ
)
1293 -- Subp overrides an inherited private operation if there is an
1294 -- inherited operation with a different name than Subp (see
1295 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
1296 -- same name as Subp.
1298 Op_Elmt
:= First_Elmt
(Op_List
);
1299 while Present
(Op_Elmt
) loop
1300 Prim_Op
:= Node
(Op_Elmt
);
1301 Par_Op
:= Alias
(Prim_Op
);
1304 and then not Comes_From_Source
(Prim_Op
)
1305 and then Chars
(Prim_Op
) /= Chars
(Par_Op
)
1306 and then Chars
(Par_Op
) = Chars
(Subp
)
1307 and then Is_Hidden
(Par_Op
)
1308 and then Type_Conformant
(Prim_Op
, Subp
)
1310 Set_DT_Position_Value
(Subp
, DT_Position
(Prim_Op
));
1313 Next_Elmt
(Op_Elmt
);
1316 end Check_Overriding_Operation
;
1318 -------------------------------
1319 -- Detect_Infinite_Recursion --
1320 -------------------------------
1322 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
) is
1323 Loc
: constant Source_Ptr
:= Sloc
(N
);
1325 Var_List
: constant Elist_Id
:= New_Elmt_List
;
1326 -- List of globals referenced by body of procedure
1328 Call_List
: constant Elist_Id
:= New_Elmt_List
;
1329 -- List of recursive calls in body of procedure
1331 Shad_List
: constant Elist_Id
:= New_Elmt_List
;
1332 -- List of entity id's for entities created to capture the value of
1333 -- referenced globals on entry to the procedure.
1335 Scop
: constant Uint
:= Scope_Depth
(Spec
);
1336 -- This is used to record the scope depth of the current procedure, so
1337 -- that we can identify global references.
1339 Max_Vars
: constant := 4;
1340 -- Do not test more than four global variables
1342 Count_Vars
: Natural := 0;
1343 -- Count variables found so far
1355 function Process
(Nod
: Node_Id
) return Traverse_Result
;
1356 -- Function to traverse the subprogram body (using Traverse_Func)
1362 function Process
(Nod
: Node_Id
) return Traverse_Result
is
1366 if Nkind
(Nod
) = N_Procedure_Call_Statement
then
1368 -- Case of one of the detected recursive calls
1370 if Is_Entity_Name
(Name
(Nod
))
1371 and then Has_Recursive_Call
(Entity
(Name
(Nod
)))
1372 and then Entity
(Name
(Nod
)) = Spec
1374 Append_Elmt
(Nod
, Call_List
);
1377 -- Any other procedure call may have side effects
1383 -- A call to a pure function can always be ignored
1385 elsif Nkind
(Nod
) = N_Function_Call
1386 and then Is_Entity_Name
(Name
(Nod
))
1387 and then Is_Pure
(Entity
(Name
(Nod
)))
1391 -- Case of an identifier reference
1393 elsif Nkind
(Nod
) = N_Identifier
then
1394 Ent
:= Entity
(Nod
);
1396 -- If no entity, then ignore the reference
1398 -- Not clear why this can happen. To investigate, remove this
1399 -- test and look at the crash that occurs here in 3401-004 ???
1404 -- Ignore entities with no Scope, again not clear how this
1405 -- can happen, to investigate, look at 4108-008 ???
1407 elsif No
(Scope
(Ent
)) then
1410 -- Ignore the reference if not to a more global object
1412 elsif Scope_Depth
(Scope
(Ent
)) >= Scop
then
1415 -- References to types, exceptions and constants are always OK
1418 or else Ekind
(Ent
) = E_Exception
1419 or else Ekind
(Ent
) = E_Constant
1423 -- If other than a non-volatile scalar variable, we have some
1424 -- kind of global reference (e.g. to a function) that we cannot
1425 -- deal with so we forget the attempt.
1427 elsif Ekind
(Ent
) /= E_Variable
1428 or else not Is_Scalar_Type
(Etype
(Ent
))
1429 or else Treat_As_Volatile
(Ent
)
1433 -- Otherwise we have a reference to a global scalar
1436 -- Loop through global entities already detected
1438 Elm
:= First_Elmt
(Var_List
);
1440 -- If not detected before, record this new global reference
1443 Count_Vars
:= Count_Vars
+ 1;
1445 if Count_Vars
<= Max_Vars
then
1446 Append_Elmt
(Entity
(Nod
), Var_List
);
1453 -- If recorded before, ignore
1455 elsif Node
(Elm
) = Entity
(Nod
) then
1458 -- Otherwise keep looking
1468 -- For all other node kinds, recursively visit syntactic children
1475 function Traverse_Body
is new Traverse_Func
(Process
);
1477 -- Start of processing for Detect_Infinite_Recursion
1480 -- Do not attempt detection in No_Implicit_Conditional mode, since we
1481 -- won't be able to generate the code to handle the recursion in any
1484 if Restriction_Active
(No_Implicit_Conditionals
) then
1488 -- Otherwise do traversal and quit if we get abandon signal
1490 if Traverse_Body
(N
) = Abandon
then
1493 -- We must have a call, since Has_Recursive_Call was set. If not just
1494 -- ignore (this is only an error check, so if we have a funny situation,
1495 -- due to bugs or errors, we do not want to bomb).
1497 elsif Is_Empty_Elmt_List
(Call_List
) then
1501 -- Here is the case where we detect recursion at compile time
1503 -- Push our current scope for analyzing the declarations and code that
1504 -- we will insert for the checking.
1508 -- This loop builds temporary variables for each of the referenced
1509 -- globals, so that at the end of the loop the list Shad_List contains
1510 -- these temporaries in one-to-one correspondence with the elements in
1514 Elm
:= First_Elmt
(Var_List
);
1515 while Present
(Elm
) loop
1517 Ent
:= Make_Temporary
(Loc
, 'S');
1518 Append_Elmt
(Ent
, Shad_List
);
1520 -- Insert a declaration for this temporary at the start of the
1521 -- declarations for the procedure. The temporaries are declared as
1522 -- constant objects initialized to the current values of the
1523 -- corresponding temporaries.
1526 Make_Object_Declaration
(Loc
,
1527 Defining_Identifier
=> Ent
,
1528 Object_Definition
=> New_Occurrence_Of
(Etype
(Var
), Loc
),
1529 Constant_Present
=> True,
1530 Expression
=> New_Occurrence_Of
(Var
, Loc
));
1533 Prepend
(Decl
, Declarations
(N
));
1535 Insert_After
(Last
, Decl
);
1543 -- Loop through calls
1545 Call
:= First_Elmt
(Call_List
);
1546 while Present
(Call
) loop
1548 -- Build a predicate expression of the form
1551 -- and then global1 = temp1
1552 -- and then global2 = temp2
1555 -- This predicate determines if any of the global values
1556 -- referenced by the procedure have changed since the
1557 -- current call, if not an infinite recursion is assured.
1559 Test
:= New_Occurrence_Of
(Standard_True
, Loc
);
1561 Elm1
:= First_Elmt
(Var_List
);
1562 Elm2
:= First_Elmt
(Shad_List
);
1563 while Present
(Elm1
) loop
1569 Left_Opnd
=> New_Occurrence_Of
(Node
(Elm1
), Loc
),
1570 Right_Opnd
=> New_Occurrence_Of
(Node
(Elm2
), Loc
)));
1576 -- Now we replace the call with the sequence
1578 -- if no-changes (see above) then
1579 -- raise Storage_Error;
1584 Rewrite
(Node
(Call
),
1585 Make_If_Statement
(Loc
,
1587 Then_Statements
=> New_List
(
1588 Make_Raise_Storage_Error
(Loc
,
1589 Reason
=> SE_Infinite_Recursion
)),
1591 Else_Statements
=> New_List
(
1592 Relocate_Node
(Node
(Call
)))));
1594 Analyze
(Node
(Call
));
1599 -- Remove temporary scope stack entry used for analysis
1602 end Detect_Infinite_Recursion
;
1604 --------------------
1605 -- Expand_Actuals --
1606 --------------------
1608 procedure Expand_Actuals
1611 Post_Call
: out List_Id
)
1613 Loc
: constant Source_Ptr
:= Sloc
(N
);
1617 E_Actual
: Entity_Id
;
1618 E_Formal
: Entity_Id
;
1620 procedure Add_Call_By_Copy_Code
;
1621 -- For cases where the parameter must be passed by copy, this routine
1622 -- generates a temporary variable into which the actual is copied and
1623 -- then passes this as the parameter. For an OUT or IN OUT parameter,
1624 -- an assignment is also generated to copy the result back. The call
1625 -- also takes care of any constraint checks required for the type
1626 -- conversion case (on both the way in and the way out).
1628 procedure Add_Simple_Call_By_Copy_Code
(Force
: Boolean);
1629 -- This is similar to the above, but is used in cases where we know
1630 -- that all that is needed is to simply create a temporary and copy
1631 -- the value in and out of the temporary. If Force is True, then the
1632 -- procedure may disregard legality considerations.
1634 -- ??? We need to do the copy for a bit-packed array because this is
1635 -- where the rewriting into a mask-and-shift sequence is done. But of
1636 -- course this may break the program if it expects bits to be really
1637 -- passed by reference. That's what we have done historically though.
1639 procedure Add_Validation_Call_By_Copy_Code
(Act
: Node_Id
);
1640 -- Perform copy-back for actual parameter Act which denotes a validation
1643 procedure Check_Fortran_Logical
;
1644 -- A value of type Logical that is passed through a formal parameter
1645 -- must be normalized because .TRUE. usually does not have the same
1646 -- representation as True. We assume that .FALSE. = False = 0.
1647 -- What about functions that return a logical type ???
1649 function Is_Legal_Copy
return Boolean;
1650 -- Check that an actual can be copied before generating the temporary
1651 -- to be used in the call. If the formal is of a by_reference type or
1652 -- is aliased, then the program is illegal (this can only happen in
1653 -- the presence of representation clauses that force a misalignment)
1654 -- If the formal is a by_reference parameter imposed by a DEC pragma,
1655 -- emit a warning that this might lead to unaligned arguments.
1657 function Make_Var
(Actual
: Node_Id
) return Entity_Id
;
1658 -- Returns an entity that refers to the given actual parameter, Actual
1659 -- (not including any type conversion). If Actual is an entity name,
1660 -- then this entity is returned unchanged, otherwise a renaming is
1661 -- created to provide an entity for the actual.
1663 procedure Reset_Packed_Prefix
;
1664 -- The expansion of a packed array component reference is delayed in
1665 -- the context of a call. Now we need to complete the expansion, so we
1666 -- unmark the analyzed bits in all prefixes.
1668 function Requires_Atomic_Or_Volatile_Copy
return Boolean;
1669 -- Returns whether a copy is required as per RM C.6(19) and gives a
1670 -- warning in this case.
1672 ---------------------------
1673 -- Add_Call_By_Copy_Code --
1674 ---------------------------
1676 procedure Add_Call_By_Copy_Code
is
1679 F_Typ
: Entity_Id
:= Etype
(Formal
);
1687 if not Is_Legal_Copy
then
1691 Temp
:= Make_Temporary
(Loc
, 'T', Actual
);
1693 -- Handle formals whose type comes from the limited view
1695 if From_Limited_With
(F_Typ
)
1696 and then Has_Non_Limited_View
(F_Typ
)
1698 F_Typ
:= Non_Limited_View
(F_Typ
);
1701 -- Use formal type for temp, unless formal type is an unconstrained
1702 -- array, in which case we don't have to worry about bounds checks,
1703 -- and we use the actual type, since that has appropriate bounds.
1705 if Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
1706 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1708 Indic
:= New_Occurrence_Of
(F_Typ
, Loc
);
1711 -- The new code will be properly analyzed below and the setting of
1712 -- the Do_Range_Check flag recomputed so remove the obsolete one.
1714 Set_Do_Range_Check
(Actual
, False);
1716 if Nkind
(Actual
) = N_Type_Conversion
then
1717 Set_Do_Range_Check
(Expression
(Actual
), False);
1719 V_Typ
:= Etype
(Expression
(Actual
));
1721 -- If the formal is an (in-)out parameter, capture the name
1722 -- of the variable in order to build the post-call assignment.
1724 Var
:= Make_Var
(Expression
(Actual
));
1726 Crep
:= not Has_Compatible_Representation
1727 (Target_Typ
=> F_Typ
,
1728 Operand_Typ
=> Etype
(Expression
(Actual
)));
1731 V_Typ
:= Etype
(Actual
);
1732 Var
:= Make_Var
(Actual
);
1736 -- If the actual denotes a variable which captures the value of an
1737 -- object for validation purposes, we propagate the link with this
1738 -- object to the new variable made from the actual just above.
1740 if Ekind
(Formal
) /= E_In_Parameter
1741 and then Is_Validation_Variable_Reference
(Actual
)
1744 Ref
: constant Node_Id
:= Unqual_Conv
(Actual
);
1747 if Is_Entity_Name
(Ref
) then
1748 Set_Validated_Object
(Var
, Validated_Object
(Entity
(Ref
)));
1751 pragma Assert
(False);
1757 -- Setup initialization for case of in out parameter, or an out
1758 -- parameter where the formal is an unconstrained array (in the
1759 -- latter case, we have to pass in an object with bounds).
1761 -- If this is an out parameter, the initial copy is wasteful, so as
1762 -- an optimization for the one-dimensional case we extract the
1763 -- bounds of the actual and build an uninitialized temporary of the
1766 -- If the formal is an out parameter with discriminants, the
1767 -- discriminants must be captured even if the rest of the object
1768 -- is in principle uninitialized, because the discriminants may
1769 -- be read by the called subprogram.
1771 if Ekind
(Formal
) = E_In_Out_Parameter
1772 or else (Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
))
1773 or else Has_Discriminants
(F_Typ
)
1775 if Nkind
(Actual
) = N_Type_Conversion
then
1776 if Conversion_OK
(Actual
) then
1777 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1779 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1782 elsif Ekind
(Formal
) = E_Out_Parameter
1783 and then Is_Array_Type
(F_Typ
)
1784 and then Number_Dimensions
(F_Typ
) = 1
1785 and then not Has_Non_Null_Base_Init_Proc
(F_Typ
)
1787 -- Actual is a one-dimensional array or slice, and the type
1788 -- requires no initialization. Create a temporary of the
1789 -- right size, but do not copy actual into it (optimization).
1793 Make_Subtype_Indication
(Loc
,
1794 Subtype_Mark
=> New_Occurrence_Of
(F_Typ
, Loc
),
1796 Make_Index_Or_Discriminant_Constraint
(Loc
,
1797 Constraints
=> New_List
(
1800 Make_Attribute_Reference
(Loc
,
1801 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
1802 Attribute_Name
=> Name_First
),
1804 Make_Attribute_Reference
(Loc
,
1805 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
1806 Attribute_Name
=> Name_Last
)))));
1809 Init
:= New_Occurrence_Of
(Var
, Loc
);
1812 -- An initialization is created for packed conversions as
1813 -- actuals for out parameters to enable Make_Object_Declaration
1814 -- to determine the proper subtype for N_Node. Note that this
1815 -- is wasteful because the extra copying on the call side is
1816 -- not required for such out parameters. ???
1818 elsif Ekind
(Formal
) = E_Out_Parameter
1819 and then Nkind
(Actual
) = N_Type_Conversion
1820 and then (Is_Bit_Packed_Array
(F_Typ
)
1822 Is_Bit_Packed_Array
(Etype
(Expression
(Actual
))))
1824 if Conversion_OK
(Actual
) then
1825 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1827 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1830 elsif Ekind
(Formal
) = E_In_Parameter
then
1832 -- Handle the case in which the actual is a type conversion
1834 if Nkind
(Actual
) = N_Type_Conversion
then
1835 if Conversion_OK
(Actual
) then
1836 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1838 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1841 Init
:= New_Occurrence_Of
(Var
, Loc
);
1844 -- Access types are passed in without checks, but if a copy-back is
1845 -- required for a null-excluding check on an in-out or out parameter,
1846 -- then the initial value is that of the actual.
1848 elsif Is_Access_Type
(E_Formal
)
1849 and then Can_Never_Be_Null
(Etype
(Actual
))
1850 and then not Can_Never_Be_Null
(E_Formal
)
1852 Init
:= New_Occurrence_Of
(Var
, Loc
);
1854 -- View conversions when the formal type has the Default_Value aspect
1855 -- require passing in the value of the conversion's operand. The type
1856 -- of that operand also has Default_Value, as required by AI12-0074
1857 -- (RM 6.4.1(5.3/4)). The subtype denoted by the subtype_indication
1858 -- is changed to the base type of the formal subtype, to ensure that
1859 -- the actual's value can be assigned without a constraint check
1860 -- (note that no check is done on passing to an out parameter). Also
1861 -- note that the two types necessarily share the same ancestor type,
1862 -- as required by 6.4.1(5.2/4), so underlying base types will match.
1864 elsif Ekind
(Formal
) = E_Out_Parameter
1865 and then Is_Scalar_Type
(Etype
(F_Typ
))
1866 and then Nkind
(Actual
) = N_Type_Conversion
1867 and then Present
(Default_Aspect_Value
(Etype
(F_Typ
)))
1869 Indic
:= New_Occurrence_Of
(Base_Type
(F_Typ
), Loc
);
1871 (Base_Type
(F_Typ
), New_Occurrence_Of
(Var
, Loc
));
1878 Make_Object_Declaration
(Loc
,
1879 Defining_Identifier
=> Temp
,
1880 Object_Definition
=> Indic
,
1881 Expression
=> Init
);
1882 Set_Assignment_OK
(N_Node
);
1883 Insert_Action
(N
, N_Node
);
1885 -- Now, normally the deal here is that we use the defining
1886 -- identifier created by that object declaration. There is
1887 -- one exception to this. In the change of representation case
1888 -- the above declaration will end up looking like:
1890 -- temp : type := identifier;
1892 -- And in this case we might as well use the identifier directly
1893 -- and eliminate the temporary. Note that the analysis of the
1894 -- declaration was not a waste of time in that case, since it is
1895 -- what generated the necessary change of representation code. If
1896 -- the change of representation introduced additional code, as in
1897 -- a fixed-integer conversion, the expression is not an identifier
1898 -- and must be kept.
1901 and then Present
(Expression
(N_Node
))
1902 and then Is_Entity_Name
(Expression
(N_Node
))
1904 Temp
:= Entity
(Expression
(N_Node
));
1905 Rewrite
(N_Node
, Make_Null_Statement
(Loc
));
1908 -- For IN parameter, all we do is to replace the actual
1910 if Ekind
(Formal
) = E_In_Parameter
then
1911 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
1914 -- Processing for OUT or IN OUT parameter
1917 -- Kill current value indications for the temporary variable we
1918 -- created, since we just passed it as an OUT parameter.
1920 Kill_Current_Values
(Temp
);
1921 Set_Is_Known_Valid
(Temp
, False);
1922 Set_Is_True_Constant
(Temp
, False);
1924 -- If type conversion, use reverse conversion on exit
1926 if Nkind
(Actual
) = N_Type_Conversion
then
1927 if Conversion_OK
(Actual
) then
1928 Expr
:= OK_Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
1930 Expr
:= Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
1933 Expr
:= New_Occurrence_Of
(Temp
, Loc
);
1936 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Sloc
(Actual
)));
1939 -- If the actual is a conversion of a packed reference, it may
1940 -- already have been expanded by Remove_Side_Effects, and the
1941 -- resulting variable is a temporary which does not designate
1942 -- the proper out-parameter, which may not be addressable. In
1943 -- that case, generate an assignment to the original expression
1944 -- (before expansion of the packed reference) so that the proper
1945 -- expansion of assignment to a packed component can take place.
1952 if Is_Renaming_Of_Object
(Var
)
1953 and then Nkind
(Renamed_Object
(Var
)) = N_Selected_Component
1954 and then Nkind
(Original_Node
(Prefix
(Renamed_Object
(Var
))))
1955 = N_Indexed_Component
1957 Has_Non_Standard_Rep
(Etype
(Prefix
(Renamed_Object
(Var
))))
1959 Obj
:= Renamed_Object
(Var
);
1961 Make_Selected_Component
(Loc
,
1963 New_Copy_Tree
(Original_Node
(Prefix
(Obj
))),
1964 Selector_Name
=> New_Copy
(Selector_Name
(Obj
)));
1965 Reset_Analyzed_Flags
(Lhs
);
1968 Lhs
:= New_Occurrence_Of
(Var
, Loc
);
1971 Set_Assignment_OK
(Lhs
);
1973 if Is_Access_Type
(E_Formal
)
1974 and then Is_Entity_Name
(Lhs
)
1976 Present
(Effective_Extra_Accessibility
(Entity
(Lhs
)))
1977 and then not No_Dynamic_Accessibility_Checks_Enabled
(Lhs
)
1979 -- Copyback target is an Ada 2012 stand-alone object of an
1980 -- anonymous access type.
1982 pragma Assert
(Ada_Version
>= Ada_2012
);
1984 Apply_Accessibility_Check
(Lhs
, E_Formal
, N
);
1986 Append_To
(Post_Call
,
1987 Make_Assignment_Statement
(Loc
,
1989 Expression
=> Expr
));
1991 -- We would like to somehow suppress generation of the
1992 -- extra_accessibility assignment generated by the expansion
1993 -- of the above assignment statement. It's not a correctness
1994 -- issue because the following assignment renders it dead,
1995 -- but generating back-to-back assignments to the same
1996 -- target is undesirable. ???
1998 Append_To
(Post_Call
,
1999 Make_Assignment_Statement
(Loc
,
2000 Name
=> New_Occurrence_Of
(
2001 Effective_Extra_Accessibility
(Entity
(Lhs
)), Loc
),
2002 Expression
=> Make_Integer_Literal
(Loc
,
2003 Type_Access_Level
(E_Formal
))));
2006 if Is_Access_Type
(E_Formal
)
2007 and then Can_Never_Be_Null
(Etype
(Actual
))
2008 and then not Can_Never_Be_Null
(E_Formal
)
2010 Append_To
(Post_Call
,
2011 Make_Raise_Constraint_Error
(Loc
,
2014 Left_Opnd
=> New_Occurrence_Of
(Temp
, Loc
),
2015 Right_Opnd
=> Make_Null
(Loc
)),
2016 Reason
=> CE_Access_Check_Failed
));
2019 Append_To
(Post_Call
,
2020 Make_Assignment_Statement
(Loc
,
2022 Expression
=> Expr
));
2025 -- Add a copy-back to reflect any potential changes in value
2026 -- back into the original object, if any.
2028 if Is_Validation_Variable_Reference
(Lhs
) then
2029 Add_Validation_Call_By_Copy_Code
(Lhs
);
2033 end Add_Call_By_Copy_Code
;
2035 ----------------------------------
2036 -- Add_Simple_Call_By_Copy_Code --
2037 ----------------------------------
2039 procedure Add_Simple_Call_By_Copy_Code
(Force
: Boolean) is
2040 With_Storage_Model
: constant Boolean :=
2041 Nkind
(Actual
) = N_Explicit_Dereference
2043 Has_Designated_Storage_Model_Aspect
(Etype
(Prefix
(Actual
)));
2056 -- Unless forced not to, check the legality of the copy operation
2058 if not Force
and then not Is_Legal_Copy
then
2062 F_Typ
:= Etype
(Formal
);
2064 -- Handle formals whose type comes from the limited view
2066 if From_Limited_With
(F_Typ
)
2067 and then Has_Non_Limited_View
(F_Typ
)
2069 F_Typ
:= Non_Limited_View
(F_Typ
);
2072 -- Use formal type for temp, unless formal type is an unconstrained
2073 -- composite, in which case we don't have to worry about checks and
2074 -- we can use the actual type, since that has appropriate bounds.
2076 if Is_Composite_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
2077 Indic
:= New_Occurrence_Of
(Get_Actual_Subtype
(Actual
), Loc
);
2079 Indic
:= New_Occurrence_Of
(F_Typ
, Loc
);
2082 -- Prepare to generate code
2084 Reset_Packed_Prefix
;
2086 Incod
:= Relocate_Node
(Actual
);
2087 Outcod
:= New_Copy_Tree
(Incod
);
2089 -- Generate declaration of temporary variable, initializing it
2090 -- with the input parameter unless we have an OUT formal or
2091 -- this is an initialization call.
2093 if Ekind
(Formal
) = E_Out_Parameter
then
2096 elsif Inside_Init_Proc
then
2098 -- Skip using the actual as the expression in Decl if we are in
2099 -- an init proc and it is not a component which depends on a
2100 -- discriminant, because, in this case, we need to use the actual
2101 -- type of the component instead.
2103 if Nkind
(Actual
) /= N_Selected_Component
2105 not Has_Discriminant_Dependent_Constraint
2106 (Entity
(Selector_Name
(Actual
)))
2110 -- Otherwise, keep the component so we can generate the proper
2111 -- actual subtype - since the subtype depends on enclosing
2121 if With_Storage_Model
then
2123 Build_Temporary_On_Secondary_Stack
(Loc
, Entity
(Indic
), Cpcod
);
2125 if Present
(Incod
) then
2127 Make_Assignment_Statement
(Loc
,
2129 Make_Explicit_Dereference
(Loc
,
2130 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)),
2131 Expression
=> Incod
));
2132 Set_Suppress_Assignment_Checks
(Last
(Cpcod
));
2136 Temp
:= Make_Temporary
(Loc
, 'T', Actual
);
2139 Make_Object_Declaration
(Loc
,
2140 Defining_Identifier
=> Temp
,
2141 Object_Definition
=> Indic
,
2142 Expression
=> Incod
);
2144 -- If the call is to initialize a component of a composite type,
2145 -- and the component does not depend on discriminants, use the
2146 -- actual type of the component. This is required in case the
2147 -- component is constrained, because in general the formal of the
2148 -- initialization procedure will be unconstrained. Note that if
2149 -- the component being initialized is constrained by an enclosing
2150 -- discriminant, the presence of the initialization in the
2151 -- declaration will generate an expression for the actual subtype.
2153 if Inside_Init_Proc
and then No
(Incod
) then
2154 Set_No_Initialization
(Decl
);
2155 Set_Object_Definition
(Decl
,
2156 New_Occurrence_Of
(Etype
(Actual
), Loc
));
2159 Append_To
(Cpcod
, Decl
);
2162 Insert_Actions
(N
, Cpcod
);
2164 -- The actual is simply a reference to the temporary
2166 if With_Storage_Model
then
2168 Make_Explicit_Dereference
(Loc
,
2169 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
2171 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
2176 -- Generate copy out if OUT or IN OUT parameter
2178 if Ekind
(Formal
) /= E_In_Parameter
then
2181 if With_Storage_Model
then
2183 Make_Explicit_Dereference
(Loc
,
2184 Prefix
=> New_Occurrence_Of
(Temp
, Loc
));
2186 Rhs
:= New_Occurrence_Of
(Temp
, Loc
);
2187 Set_Is_True_Constant
(Temp
, False);
2190 -- Deal with conversion
2192 if Nkind
(Lhs
) = N_Type_Conversion
then
2193 Lhs
:= Expression
(Lhs
);
2194 Rhs
:= Convert_To
(Etype
(Actual
), Rhs
);
2197 Append_To
(Post_Call
,
2198 Make_Assignment_Statement
(Loc
,
2200 Expression
=> Rhs
));
2201 Set_Suppress_Assignment_Checks
(Last
(Post_Call
));
2202 Set_Assignment_OK
(Name
(Last
(Post_Call
)));
2204 end Add_Simple_Call_By_Copy_Code
;
2206 --------------------------------------
2207 -- Add_Validation_Call_By_Copy_Code --
2208 --------------------------------------
2210 procedure Add_Validation_Call_By_Copy_Code
(Act
: Node_Id
) is
2211 Var
: constant Node_Id
:= Unqual_Conv
(Act
);
2215 Obj_Typ
: Entity_Id
;
2219 -- Generate range check if required
2221 if Do_Range_Check
(Actual
) then
2222 Generate_Range_Check
(Actual
, E_Formal
, CE_Range_Check_Failed
);
2225 -- If there is a type conversion in the actual, it will be reinstated
2226 -- below, the new instance will be properly analyzed and the setting
2227 -- of the Do_Range_Check flag recomputed so remove the obsolete one.
2229 if Nkind
(Actual
) = N_Type_Conversion
then
2230 Set_Do_Range_Check
(Expression
(Actual
), False);
2233 -- Copy the value of the validation variable back into the object
2236 if Is_Entity_Name
(Var
) then
2237 Var_Id
:= Entity
(Var
);
2238 Obj
:= Validated_Object
(Var_Id
);
2239 Obj_Typ
:= Etype
(Obj
);
2241 Expr
:= New_Occurrence_Of
(Var_Id
, Loc
);
2243 -- A type conversion is needed when the validation variable and
2244 -- the validated object carry different types. This case occurs
2245 -- when the actual is qualified in some fashion.
2248 -- subtype Int is Integer range ...;
2249 -- procedure Call (Val : in out Integer);
2253 -- Call (Integer (Object));
2257 -- Var : Integer := Object; -- conversion to base type
2258 -- if not Var'Valid then -- validity check
2259 -- Call (Var); -- modify Var
2260 -- Object := Int (Var); -- conversion to subtype
2262 if Etype
(Var_Id
) /= Obj_Typ
then
2264 Make_Type_Conversion
(Loc
,
2265 Subtype_Mark
=> New_Occurrence_Of
(Obj_Typ
, Loc
),
2266 Expression
=> Expr
);
2272 -- Object := Object_Type (Var);
2274 Append_To
(Post_Call
,
2275 Make_Assignment_Statement
(Loc
,
2277 Expression
=> Expr
));
2279 -- If the flow reaches this point, then this routine was invoked with
2280 -- an actual which does not denote a validation variable.
2283 pragma Assert
(False);
2286 end Add_Validation_Call_By_Copy_Code
;
2288 ---------------------------
2289 -- Check_Fortran_Logical --
2290 ---------------------------
2292 procedure Check_Fortran_Logical
is
2293 Logical
: constant Entity_Id
:= Etype
(Formal
);
2296 -- Note: this is very incomplete, e.g. it does not handle arrays
2297 -- of logical values. This is really not the right approach at all???)
2300 if Convention
(Subp
) = Convention_Fortran
2301 and then Root_Type
(Etype
(Formal
)) = Standard_Boolean
2302 and then Ekind
(Formal
) /= E_In_Parameter
2304 Var
:= Make_Var
(Actual
);
2305 Append_To
(Post_Call
,
2306 Make_Assignment_Statement
(Loc
,
2307 Name
=> New_Occurrence_Of
(Var
, Loc
),
2309 Unchecked_Convert_To
(
2312 Left_Opnd
=> New_Occurrence_Of
(Var
, Loc
),
2314 Unchecked_Convert_To
(
2316 New_Occurrence_Of
(Standard_False
, Loc
))))));
2318 end Check_Fortran_Logical
;
2324 function Is_Legal_Copy
return Boolean is
2326 -- An attempt to copy a value of such a type can only occur if
2327 -- representation clauses give the actual a misaligned address.
2329 if Is_By_Reference_Type
(Etype
(Formal
))
2330 or else Is_Aliased
(Formal
)
2331 or else (Mechanism
(Formal
) = By_Reference
2332 and then not Has_Foreign_Convention
(Subp
))
2335 -- The actual may in fact be properly aligned but there is not
2336 -- enough front-end information to determine this. In that case
2337 -- gigi will emit an error or a warning if a copy is not legal,
2338 -- or generate the proper code.
2342 -- For users of Starlet, we assume that the specification of by-
2343 -- reference mechanism is mandatory. This may lead to unaligned
2344 -- objects but at least for DEC legacy code it is known to work.
2345 -- The warning will alert users of this code that a problem may
2348 elsif Mechanism
(Formal
) = By_Reference
2349 and then Ekind
(Scope
(Formal
)) = E_Procedure
2350 and then Is_Valued_Procedure
(Scope
(Formal
))
2353 ("by_reference actual may be misaligned??", Actual
);
2365 function Make_Var
(Actual
: Node_Id
) return Entity_Id
is
2369 if Is_Entity_Name
(Actual
) then
2370 return Entity
(Actual
);
2373 Var
:= Make_Temporary
(Loc
, 'T', Actual
);
2376 Make_Object_Renaming_Declaration
(Loc
,
2377 Defining_Identifier
=> Var
,
2379 New_Occurrence_Of
(Etype
(Actual
), Loc
),
2380 Name
=> Relocate_Node
(Actual
));
2382 Insert_Action
(N
, N_Node
);
2387 -------------------------
2388 -- Reset_Packed_Prefix --
2389 -------------------------
2391 procedure Reset_Packed_Prefix
is
2392 Pfx
: Node_Id
:= Actual
;
2395 Set_Analyzed
(Pfx
, False);
2397 Nkind
(Pfx
) not in N_Selected_Component | N_Indexed_Component
;
2398 Pfx
:= Prefix
(Pfx
);
2400 end Reset_Packed_Prefix
;
2402 ----------------------------------------
2403 -- Requires_Atomic_Or_Volatile_Copy --
2404 ----------------------------------------
2406 function Requires_Atomic_Or_Volatile_Copy
return Boolean is
2408 -- If the formal is already passed by copy, no need to do anything
2410 if Is_By_Copy_Type
(E_Formal
) then
2414 -- There is no requirement inside initialization procedures and this
2415 -- would generate copies for atomic or volatile composite components.
2417 if Inside_Init_Proc
then
2421 -- Check for atomicity mismatch
2423 if Is_Atomic_Object
(Actual
) and then not Is_Atomic
(E_Formal
)
2425 if Comes_From_Source
(N
) then
2427 ("??atomic actual passed by copy (RM C.6(19))", Actual
);
2432 -- Check for volatility mismatch
2434 if Is_Volatile_Object_Ref
(Actual
) and then not Is_Volatile
(E_Formal
)
2436 if Comes_From_Source
(N
) then
2438 ("??volatile actual passed by copy (RM C.6(19))", Actual
);
2444 end Requires_Atomic_Or_Volatile_Copy
;
2446 -- Start of processing for Expand_Actuals
2449 Post_Call
:= New_List
;
2451 Formal
:= First_Formal
(Subp
);
2452 Actual
:= First_Actual
(N
);
2453 while Present
(Formal
) loop
2454 E_Formal
:= Etype
(Formal
);
2455 E_Actual
:= Etype
(Actual
);
2457 -- Handle formals whose type comes from the limited view
2459 if From_Limited_With
(E_Formal
)
2460 and then Has_Non_Limited_View
(E_Formal
)
2462 E_Formal
:= Non_Limited_View
(E_Formal
);
2465 if Is_Scalar_Type
(E_Formal
)
2466 or else Nkind
(Actual
) = N_Slice
2468 Check_Fortran_Logical
;
2472 elsif Ekind
(Formal
) /= E_Out_Parameter
then
2474 -- The unusual case of the current instance of a protected type
2475 -- requires special handling. This can only occur in the context
2476 -- of a call within the body of a protected operation.
2478 if Is_Entity_Name
(Actual
)
2479 and then Ekind
(Entity
(Actual
)) = E_Protected_Type
2480 and then In_Open_Scopes
(Entity
(Actual
))
2482 if Scope
(Subp
) /= Entity
(Actual
) then
2484 ("operation outside protected type may not "
2485 & "call back its protected operations??", Actual
);
2489 Expand_Protected_Object_Reference
(N
, Entity
(Actual
)));
2492 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
2493 -- build-in-place function, then a temporary return object needs
2494 -- to be created and access to it must be passed to the function
2495 -- (and ensure that we have an activation chain defined for tasks
2496 -- and a Master variable).
2498 -- Currently we limit such functions to those with inherently
2499 -- limited result subtypes, but eventually we plan to expand the
2500 -- functions that are treated as build-in-place to include other
2501 -- composite result types.
2503 -- But do not do it here for intrinsic subprograms since this will
2504 -- be done properly after the subprogram is expanded.
2506 if Is_Intrinsic_Subprogram
(Subp
) then
2509 elsif Is_Build_In_Place_Function_Call
(Actual
) then
2510 if Might_Have_Tasks
(Etype
(Actual
)) then
2511 Build_Activation_Chain_Entity
(N
);
2512 Build_Master_Entity
(Etype
(Actual
));
2515 Make_Build_In_Place_Call_In_Anonymous_Context
(Actual
);
2517 -- Ada 2005 (AI-318-02): Specialization of the previous case for
2518 -- actuals containing build-in-place function calls whose returned
2519 -- object covers interface types.
2521 elsif Present
(Unqual_BIP_Iface_Function_Call
(Actual
)) then
2522 Build_Activation_Chain_Entity
(N
);
2523 Build_Master_Entity
(Etype
(Actual
));
2524 Make_Build_In_Place_Iface_Call_In_Anonymous_Context
(Actual
);
2527 Apply_Constraint_Check
(Actual
, E_Formal
);
2529 -- Out parameter case. No constraint checks on access type
2530 -- RM 6.4.1 (13), but on return a null-excluding check may be
2531 -- required (see below).
2533 elsif Is_Access_Type
(E_Formal
) then
2538 elsif Has_Discriminants
(Base_Type
(E_Formal
))
2539 or else Has_Non_Null_Base_Init_Proc
(E_Formal
)
2541 Apply_Constraint_Check
(Actual
, E_Formal
);
2546 Apply_Constraint_Check
(Actual
, Base_Type
(E_Formal
));
2549 -- Processing for IN-OUT and OUT parameters
2551 if Ekind
(Formal
) /= E_In_Parameter
then
2553 -- For type conversions of arrays, apply length/range checks
2555 if Is_Array_Type
(E_Formal
)
2556 and then Nkind
(Actual
) = N_Type_Conversion
2558 if Is_Constrained
(E_Formal
) then
2559 Apply_Length_Check
(Expression
(Actual
), E_Formal
);
2561 Apply_Range_Check
(Expression
(Actual
), E_Formal
);
2565 -- If argument is a type conversion for a type that is passed by
2566 -- copy, then we must pass the parameter by copy.
2568 if Nkind
(Actual
) = N_Type_Conversion
2570 (Is_Elementary_Type
(E_Formal
)
2571 or else Is_Bit_Packed_Array
(Etype
(Formal
))
2572 or else Is_Bit_Packed_Array
(Etype
(Expression
(Actual
)))
2574 -- Also pass by copy if change of representation
2576 or else not Has_Compatible_Representation
2577 (Target_Typ
=> Etype
(Formal
),
2578 Operand_Typ
=> Etype
(Expression
(Actual
))))
2580 Add_Call_By_Copy_Code
;
2582 -- References to components of bit-packed arrays are expanded
2583 -- at this point, rather than at the point of analysis of the
2584 -- actuals, to handle the expansion of the assignment to
2585 -- [in] out parameters.
2587 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
2588 Add_Simple_Call_By_Copy_Code
(Force
=> True);
2590 -- If the actual has a nonnative storage model, we need a copy
2592 elsif Nkind
(Actual
) = N_Explicit_Dereference
2594 Has_Designated_Storage_Model_Aspect
(Etype
(Prefix
(Actual
)))
2596 (Present
(Storage_Model_Copy_To
2597 (Storage_Model_Object
(Etype
(Prefix
(Actual
)))))
2599 (Ekind
(Formal
) = E_In_Out_Parameter
2601 Present
(Storage_Model_Copy_From
2602 (Storage_Model_Object
(Etype
(Prefix
(Actual
)))))))
2604 Add_Simple_Call_By_Copy_Code
(Force
=> True);
2606 -- If a nonscalar actual is possibly bit-aligned, we need a copy
2607 -- because the back-end cannot cope with such objects. In other
2608 -- cases where alignment forces a copy, the back-end generates
2609 -- it properly. It should not be generated unconditionally in the
2610 -- front-end because it does not know precisely the alignment
2611 -- requirements of the target, and makes too conservative an
2612 -- estimate, leading to superfluous copies or spurious errors
2613 -- on by-reference parameters.
2615 elsif Nkind
(Actual
) = N_Selected_Component
2617 Component_May_Be_Bit_Aligned
(Entity
(Selector_Name
(Actual
)))
2618 and then not Represented_As_Scalar
(Etype
(Formal
))
2620 Add_Simple_Call_By_Copy_Code
(Force
=> False);
2622 -- References to slices of bit-packed arrays are expanded
2624 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
2625 Add_Call_By_Copy_Code
;
2627 -- References to possibly unaligned slices of arrays are expanded
2629 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
2630 Add_Call_By_Copy_Code
;
2632 -- Deal with access types where the actual subtype and the
2633 -- formal subtype are not the same, requiring a check.
2635 -- It is necessary to exclude tagged types because of "downward
2636 -- conversion" errors, but null-excluding checks on return may be
2639 elsif Is_Access_Type
(E_Formal
)
2640 and then not Is_Tagged_Type
(Designated_Type
(E_Formal
))
2641 and then (not Same_Type
(E_Formal
, E_Actual
)
2642 or else (Can_Never_Be_Null
(E_Actual
)
2643 and then not Can_Never_Be_Null
(E_Formal
)))
2645 Add_Call_By_Copy_Code
;
2647 -- We may need to force a copy because of atomicity or volatility
2650 elsif Requires_Atomic_Or_Volatile_Copy
then
2651 Add_Call_By_Copy_Code
;
2653 -- Add call-by-copy code for the case of scalar out parameters
2654 -- when it is not known at compile time that the subtype of the
2655 -- formal is a subrange of the subtype of the actual (or vice
2656 -- versa for in out parameters), in order to get range checks
2657 -- on such actuals. (Maybe this case should be handled earlier
2658 -- in the if statement???)
2660 elsif Is_Scalar_Type
(E_Formal
)
2662 (not In_Subrange_Of
(E_Formal
, E_Actual
)
2664 (Ekind
(Formal
) = E_In_Out_Parameter
2665 and then not In_Subrange_Of
(E_Actual
, E_Formal
)))
2667 Add_Call_By_Copy_Code
;
2669 -- The actual denotes a variable which captures the value of an
2670 -- object for validation purposes. Add a copy-back to reflect any
2671 -- potential changes in value back into the original object.
2673 -- Var : ... := Object;
2674 -- if not Var'Valid then -- validity check
2675 -- Call (Var); -- modify var
2676 -- Object := Var; -- update Object
2678 elsif Is_Validation_Variable_Reference
(Actual
) then
2679 Add_Validation_Call_By_Copy_Code
(Actual
);
2682 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
2683 -- by-reference parameters on exit from the call. If the actual
2684 -- is a derived type and the operation is inherited, the body
2685 -- of the operation will not contain a call to the predicate
2686 -- function, so it must be done explicitly after the call. Ditto
2687 -- if the actual is an entity of a predicated subtype.
2689 -- The rule refers to by-reference types, but a check is needed
2690 -- for by-copy types as well. That check is subsumed by the rule
2691 -- for subtype conversion on assignment, but we can generate the
2692 -- required check now.
2694 -- Note also that Subp may be either a subprogram entity for
2695 -- direct calls, or a type entity for indirect calls, which must
2696 -- be handled separately because the name does not denote an
2697 -- overloadable entity.
2699 By_Ref_Predicate_Check
: declare
2700 Aund
: constant Entity_Id
:= Underlying_Type
(E_Actual
);
2710 if Predicate_Enabled
(Atyp
)
2712 -- Skip predicate checks for special cases
2714 and then Predicate_Tests_On_Arguments
(Subp
)
2716 Append_To
(Post_Call
,
2717 Make_Predicate_Check
(Atyp
, Actual
));
2719 end By_Ref_Predicate_Check
;
2721 -- Processing for IN parameters
2724 -- Generate range check if required
2726 if Do_Range_Check
(Actual
) then
2727 Generate_Range_Check
(Actual
, E_Formal
, CE_Range_Check_Failed
);
2730 -- For IN parameters in the bit-packed array case, we expand an
2731 -- indexed component (the circuit in Exp_Ch4 deliberately left
2732 -- indexed components appearing as actuals untouched, so that
2733 -- the special processing above for the OUT and IN OUT cases
2734 -- could be performed. We could make the test in Exp_Ch4 more
2735 -- complex and have it detect the parameter mode, but it is
2736 -- easier simply to handle all cases here.)
2738 if Nkind
(Actual
) = N_Indexed_Component
2739 and then Is_Bit_Packed_Array
(Etype
(Prefix
(Actual
)))
2741 Reset_Packed_Prefix
;
2742 Expand_Packed_Element_Reference
(Actual
);
2744 -- If we have a reference to a bit-packed array, we copy it, since
2745 -- the actual must be byte aligned.
2747 -- Is this really necessary in all cases???
2749 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
2750 Add_Simple_Call_By_Copy_Code
(Force
=> True);
2752 -- If the actual has a nonnative storage model, we need a copy
2754 elsif Nkind
(Actual
) = N_Explicit_Dereference
2756 Has_Designated_Storage_Model_Aspect
(Etype
(Prefix
(Actual
)))
2758 Present
(Storage_Model_Copy_From
2759 (Storage_Model_Object
(Etype
(Prefix
(Actual
)))))
2761 Add_Simple_Call_By_Copy_Code
(Force
=> True);
2763 -- If we have a C++ constructor call, we need to create the object
2765 elsif Is_CPP_Constructor_Call
(Actual
) then
2766 Add_Simple_Call_By_Copy_Code
(Force
=> True);
2768 -- If a nonscalar actual is possibly unaligned, we need a copy
2770 elsif Is_Possibly_Unaligned_Object
(Actual
)
2771 and then not Represented_As_Scalar
(Etype
(Formal
))
2773 Add_Simple_Call_By_Copy_Code
(Force
=> False);
2775 -- Similarly, we have to expand slices of packed arrays here
2776 -- because the result must be byte aligned.
2778 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
2779 Add_Call_By_Copy_Code
;
2781 -- Only processing remaining is to pass by copy if this is a
2782 -- reference to a possibly unaligned slice, since the caller
2783 -- expects an appropriately aligned argument.
2785 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
2786 Add_Call_By_Copy_Code
;
2788 -- We may need to force a copy because of atomicity or volatility
2791 elsif Requires_Atomic_Or_Volatile_Copy
then
2792 Add_Call_By_Copy_Code
;
2794 -- An unusual case: a current instance of an enclosing task can be
2795 -- an actual, and must be replaced by a reference to self.
2797 elsif Is_Entity_Name
(Actual
)
2798 and then Is_Task_Type
(Entity
(Actual
))
2800 if In_Open_Scopes
(Entity
(Actual
)) then
2802 (Make_Function_Call
(Loc
,
2803 Name
=> New_Occurrence_Of
(RTE
(RE_Self
), Loc
))));
2806 -- A task type cannot otherwise appear as an actual
2809 raise Program_Error
;
2814 -- Type-invariant checks for in-out and out parameters, as well as
2815 -- for in parameters of procedures (AI05-0289 and AI12-0044).
2817 if Ekind
(Formal
) /= E_In_Parameter
2818 or else Ekind
(Subp
) = E_Procedure
2820 Caller_Side_Invariant_Checks
: declare
2822 function Is_Public_Subp
return Boolean;
2823 -- Check whether the subprogram being called is a visible
2824 -- operation of the type of the actual. Used to determine
2825 -- whether an invariant check must be generated on the
2828 ---------------------
2829 -- Is_Public_Subp --
2830 ---------------------
2832 function Is_Public_Subp
return Boolean is
2833 Pack
: constant Entity_Id
:= Scope
(Subp
);
2834 Subp_Decl
: Node_Id
;
2837 if not Is_Subprogram
(Subp
) then
2840 -- The operation may be inherited, or a primitive of the
2844 Nkind
(Parent
(Subp
)) in N_Private_Extension_Declaration
2845 | N_Full_Type_Declaration
2847 Subp_Decl
:= Parent
(Subp
);
2850 Subp_Decl
:= Unit_Declaration_Node
(Subp
);
2853 return Ekind
(Pack
) = E_Package
2855 List_Containing
(Subp_Decl
) =
2856 Visible_Declarations
2857 (Specification
(Unit_Declaration_Node
(Pack
)));
2860 -- Start of processing for Caller_Side_Invariant_Checks
2863 -- We generate caller-side invariant checks in two cases:
2865 -- a) when calling an inherited operation, where there is an
2866 -- implicit view conversion of the actual to the parent type.
2868 -- b) When the conversion is explicit
2870 -- We treat these cases separately because the required
2871 -- conversion for a) is added later when expanding the call.
2873 if Has_Invariants
(Etype
(Actual
))
2875 Nkind
(Parent
(Etype
(Actual
)))
2876 = N_Private_Extension_Declaration
2878 if Comes_From_Source
(N
) and then Is_Public_Subp
then
2879 Append_To
(Post_Call
, Make_Invariant_Call
(Actual
));
2882 elsif Nkind
(Actual
) = N_Type_Conversion
2883 and then Has_Invariants
(Etype
(Expression
(Actual
)))
2885 if Comes_From_Source
(N
) and then Is_Public_Subp
then
2887 (Post_Call
, Make_Invariant_Call
(Expression
(Actual
)));
2890 end Caller_Side_Invariant_Checks
;
2893 Next_Formal
(Formal
);
2894 Next_Actual
(Actual
);
2902 procedure Expand_Call
(N
: Node_Id
) is
2903 function Is_Unchecked_Union_Equality
(N
: Node_Id
) return Boolean;
2904 -- Return True if N is a call to the predefined equality operator of an
2905 -- unchecked union type, or a renaming thereof.
2907 ---------------------------------
2908 -- Is_Unchecked_Union_Equality --
2909 ---------------------------------
2911 function Is_Unchecked_Union_Equality
(N
: Node_Id
) return Boolean is
2913 if Is_Entity_Name
(Name
(N
))
2914 and then Ekind
(Entity
(Name
(N
))) = E_Function
2915 and then Present
(First_Formal
(Entity
(Name
(N
))))
2917 Is_Unchecked_Union
(Etype
(First_Formal
(Entity
(Name
(N
)))))
2920 Func
: constant Entity_Id
:= Entity
(Name
(N
));
2921 Typ
: constant Entity_Id
:= Etype
(First_Formal
(Func
));
2922 Decl
: constant Node_Id
:=
2923 Original_Node
(Parent
(Declaration_Node
(Func
)));
2926 return Func
= TSS
(Typ
, TSS_Composite_Equality
)
2927 or else (Nkind
(Decl
) = N_Subprogram_Renaming_Declaration
2928 and then Nkind
(Name
(Decl
)) = N_Operator_Symbol
2929 and then Chars
(Name
(Decl
)) = Name_Op_Eq
2930 and then Ekind
(Entity
(Name
(Decl
))) = E_Operator
);
2936 end Is_Unchecked_Union_Equality
;
2938 -- If this is an indirect call through an Access_To_Subprogram
2939 -- with contract specifications, it is rewritten as a call to
2940 -- the corresponding Access_Subprogram_Wrapper with the same
2941 -- actuals, whose body contains a naked indirect call (which
2942 -- itself must not be rewritten, to prevent infinite recursion).
2944 Must_Rewrite_Indirect_Call
: constant Boolean :=
2945 Ada_Version
>= Ada_2022
2946 and then Nkind
(Name
(N
)) = N_Explicit_Dereference
2947 and then Ekind
(Etype
(Name
(N
))) = E_Subprogram_Type
2949 (Access_Subprogram_Wrapper
(Etype
(Name
(N
))));
2951 Post_Call
: List_Id
;
2953 -- Start of processing for Expand_Call
2956 pragma Assert
(Nkind
(N
) in N_Entry_Call_Statement
2958 | N_Procedure_Call_Statement
);
2960 -- Check that this is not the call in the body of the access
2961 -- subprogram wrapper or the postconditions wrapper.
2963 if Must_Rewrite_Indirect_Call
2964 and then (not Is_Overloadable
(Current_Scope
)
2965 or else not (Is_Access_Subprogram_Wrapper
(Current_Scope
)
2967 (Chars
(Current_Scope
) = Name_uWrapped_Statements
2968 and then Is_Access_Subprogram_Wrapper
2969 (Scope
(Current_Scope
)))))
2972 Loc
: constant Source_Ptr
:= Sloc
(N
);
2973 Wrapper
: constant Entity_Id
:=
2974 Access_Subprogram_Wrapper
(Etype
(Name
(N
)));
2975 Ptr
: constant Node_Id
:= Prefix
(Name
(N
));
2976 Ptr_Type
: constant Entity_Id
:= Etype
(Ptr
);
2977 Typ
: constant Entity_Id
:= Etype
(N
);
2980 Parms
: List_Id
:= Parameter_Associations
(N
);
2984 -- The last actual in the call is the pointer itself.
2985 -- If the aspect is inherited, convert the pointer to the
2986 -- parent type that specifies the contract.
2987 -- If the original access_to_subprogram has defaults for
2988 -- in_parameters, the call may include named associations, so
2989 -- we create one for the pointer as well.
2991 if Is_Derived_Type
(Ptr_Type
)
2992 and then Ptr_Type
/= Etype
(Last_Formal
(Wrapper
))
2995 Make_Type_Conversion
(Loc
,
2997 (Etype
(Last_Formal
(Wrapper
)), Loc
), Ptr
);
3003 -- Handle parameterless subprogram.
3010 (Make_Parameter_Association
(Loc
,
3011 Selector_Name
=> Make_Identifier
(Loc
,
3012 Chars
(Last_Formal
(Wrapper
))),
3013 Explicit_Actual_Parameter
=> Ptr_Act
),
3016 if Nkind
(N
) = N_Procedure_Call_Statement
then
3017 New_N
:= Make_Procedure_Call_Statement
(Loc
,
3018 Name
=> New_Occurrence_Of
(Wrapper
, Loc
),
3019 Parameter_Associations
=> Parms
);
3021 New_N
:= Make_Function_Call
(Loc
,
3022 Name
=> New_Occurrence_Of
(Wrapper
, Loc
),
3023 Parameter_Associations
=> Parms
);
3027 Analyze_And_Resolve
(N
, Typ
);
3030 -- Case of a call to the predefined equality operator of an unchecked
3031 -- union type, which requires specific processing.
3033 elsif Is_Unchecked_Union_Equality
(N
) then
3035 Eq
: constant Entity_Id
:= Entity
(Name
(N
));
3038 Expand_Unchecked_Union_Equality
(N
);
3040 -- If the call was not rewritten as a raise, expand the actuals
3042 if Nkind
(N
) = N_Function_Call
then
3043 pragma Assert
(Check_Number_Of_Actuals
(N
, Eq
));
3044 Expand_Actuals
(N
, Eq
, Post_Call
);
3045 pragma Assert
(Is_Empty_List
(Post_Call
));
3052 Expand_Call_Helper
(N
, Post_Call
);
3053 Insert_Post_Call_Actions
(N
, Post_Call
);
3057 ------------------------
3058 -- Expand_Call_Helper --
3059 ------------------------
3061 -- This procedure handles expansion of function calls and procedure call
3062 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
3063 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
3065 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
3066 -- Provide values of actuals for all formals in Extra_Formals list
3067 -- Replace "call" to enumeration literal function by literal itself
3068 -- Rewrite call to predefined operator as operator
3069 -- Replace actuals to in-out parameters that are numeric conversions,
3070 -- with explicit assignment to temporaries before and after the call.
3072 -- Note that the list of actuals has been filled with default expressions
3073 -- during semantic analysis of the call. Only the extra actuals required
3074 -- for the 'Constrained attribute and for accessibility checks are added
3077 procedure Expand_Call_Helper
(N
: Node_Id
; Post_Call
: out List_Id
) is
3078 Loc
: constant Source_Ptr
:= Sloc
(N
);
3079 Call_Node
: Node_Id
:= N
;
3080 Extra_Actuals
: List_Id
:= No_List
;
3081 Prev
: Node_Id
:= Empty
;
3083 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
);
3084 -- Adds one entry to the end of the actual parameter list. Used for
3085 -- default parameters and for extra actuals (for Extra_Formals). The
3086 -- argument is an N_Parameter_Association node.
3088 procedure Add_Cond_Expression_Extra_Actual
(Formal
: Entity_Id
);
3089 -- Adds extra accessibility actuals in the case of a conditional
3090 -- expression corresponding to Formal.
3092 -- Note: Conditional expressions used as actuals for anonymous access
3093 -- formals complicate the process of propagating extra accessibility
3094 -- actuals and must be handled in a recursive fashion since they can
3095 -- be embedded within each other.
3097 procedure Add_Dummy_Build_In_Place_Actuals
3098 (Function_Id
: Entity_Id
;
3099 Num_Added_Extra_Actuals
: Nat
:= 0);
3100 -- Adds dummy actuals for the BIP extra formals of the called function.
3101 -- Num_Added_Extra_Actuals is the number of non-BIP extra actuals added
3102 -- to the actuals immediately before calling this subprogram.
3104 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
);
3105 -- Adds an extra actual to the list of extra actuals. Expr is the
3106 -- expression for the value of the actual, EF is the entity for the
3109 procedure Add_View_Conversion_Invariants
3110 (Formal
: Entity_Id
;
3112 -- Adds invariant checks for every intermediate type between the range
3113 -- of a view converted argument to its ancestor (from parent to child).
3115 function Can_Fold_Predicate_Call
(P
: Entity_Id
) return Boolean;
3116 -- Try to constant-fold a predicate check, which often enough is a
3117 -- simple arithmetic expression that can be computed statically if
3118 -- its argument is static. This cleans up the output of CCG, even
3119 -- though useless predicate checks will be generally removed by
3120 -- back-end optimizations.
3122 procedure Check_Subprogram_Variant
;
3123 -- Emit a call to the internally generated procedure with checks for
3124 -- aspect Subprogram_Variant, if present and enabled.
3126 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
;
3127 -- Within an instance, a type derived from an untagged formal derived
3128 -- type inherits from the original parent, not from the actual. The
3129 -- current derivation mechanism has the derived type inherit from the
3130 -- actual, which is only correct outside of the instance. If the
3131 -- subprogram is inherited, we test for this particular case through a
3132 -- convoluted tree traversal before setting the proper subprogram to be
3135 function In_Unfrozen_Instance
(E
: Entity_Id
) return Boolean;
3136 -- Return true if E comes from an instance that is not yet frozen
3138 function Is_Class_Wide_Interface_Type
(E
: Entity_Id
) return Boolean;
3139 -- Return True when E is a class-wide interface type or an access to
3140 -- a class-wide interface type.
3142 function Is_Direct_Deep_Call
(Subp
: Entity_Id
) return Boolean;
3143 -- Determine if Subp denotes a non-dispatching call to a Deep routine
3145 function New_Value
(From
: Node_Id
) return Node_Id
;
3146 -- From is the original Expression. New_Value is equivalent to a call
3147 -- to Duplicate_Subexpr with an explicit dereference when From is an
3148 -- access parameter.
3150 --------------------------
3151 -- Add_Actual_Parameter --
3152 --------------------------
3154 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
) is
3155 Actual_Expr
: constant Node_Id
:=
3156 Explicit_Actual_Parameter
(Insert_Param
);
3159 -- Case of insertion is first named actual
3161 if No
(Prev
) or else
3162 Nkind
(Parent
(Prev
)) /= N_Parameter_Association
3164 Set_Next_Named_Actual
3165 (Insert_Param
, First_Named_Actual
(Call_Node
));
3166 Set_First_Named_Actual
(Call_Node
, Actual_Expr
);
3169 if No
(Parameter_Associations
(Call_Node
)) then
3170 Set_Parameter_Associations
(Call_Node
, New_List
);
3173 Append
(Insert_Param
, Parameter_Associations
(Call_Node
));
3176 Insert_After
(Prev
, Insert_Param
);
3179 -- Case of insertion is not first named actual
3182 Set_Next_Named_Actual
3183 (Insert_Param
, Next_Named_Actual
(Parent
(Prev
)));
3184 Set_Next_Named_Actual
(Parent
(Prev
), Actual_Expr
);
3185 Append
(Insert_Param
, Parameter_Associations
(Call_Node
));
3188 Prev
:= Actual_Expr
;
3189 end Add_Actual_Parameter
;
3191 --------------------------------------
3192 -- Add_Cond_Expression_Extra_Actual --
3193 --------------------------------------
3195 procedure Add_Cond_Expression_Extra_Actual
3196 (Formal
: Entity_Id
)
3201 procedure Insert_Level_Assign
(Branch
: Node_Id
);
3202 -- Recursively add assignment of the level temporary on each branch
3203 -- while moving through nested conditional expressions.
3205 -------------------------
3206 -- Insert_Level_Assign --
3207 -------------------------
3209 procedure Insert_Level_Assign
(Branch
: Node_Id
) is
3211 procedure Expand_Branch
(Res_Assn
: Node_Id
);
3212 -- Perform expansion or iterate further within nested
3213 -- conditionals given the object declaration or assignment to
3214 -- result object created during expansion which represents a
3215 -- branch of the conditional expression.
3221 procedure Expand_Branch
(Res_Assn
: Node_Id
) is
3223 pragma Assert
(Nkind
(Res_Assn
) in
3224 N_Assignment_Statement |
3225 N_Object_Declaration
);
3227 -- There are more nested conditional expressions so we must go
3230 if Nkind
(Expression
(Res_Assn
)) = N_Expression_With_Actions
3232 Nkind
(Original_Node
(Expression
(Res_Assn
)))
3233 in N_Case_Expression | N_If_Expression
3236 (Expression
(Res_Assn
));
3238 -- Add the level assignment
3241 Insert_Before_And_Analyze
(Res_Assn
,
3242 Make_Assignment_Statement
(Loc
,
3243 Name
=> New_Occurrence_Of
(Lvl
, Loc
),
3246 (Expr
=> Expression
(Res_Assn
),
3247 Level
=> Dynamic_Level
,
3248 Allow_Alt_Model
=> False)));
3255 -- Start of processing for Insert_Level_Assign
3258 -- Examine further nested conditionals
3260 pragma Assert
(Nkind
(Branch
) =
3261 N_Expression_With_Actions
);
3263 -- Find the relevant statement in the actions
3265 Cond
:= First
(Actions
(Branch
));
3266 while Present
(Cond
) loop
3267 exit when Nkind
(Cond
) in N_Case_Statement | N_If_Statement
;
3271 -- The conditional expression may have been optimized away, so
3272 -- examine the actions in the branch.
3275 Expand_Branch
(Last
(Actions
(Branch
)));
3277 -- Iterate through if expression branches
3279 elsif Nkind
(Cond
) = N_If_Statement
then
3280 Expand_Branch
(Last
(Then_Statements
(Cond
)));
3281 Expand_Branch
(Last
(Else_Statements
(Cond
)));
3283 -- Iterate through case alternatives
3285 elsif Nkind
(Cond
) = N_Case_Statement
then
3287 Alt
:= First
(Alternatives
(Cond
));
3288 while Present
(Alt
) loop
3289 Expand_Branch
(Last
(Statements
(Alt
)));
3293 end Insert_Level_Assign
;
3295 -- Start of processing for cond expression case
3298 -- Create declaration of a temporary to store the accessibility
3299 -- level of each branch of the conditional expression.
3301 Lvl
:= Make_Temporary
(Loc
, 'L');
3302 Decl
:= Make_Object_Declaration
(Loc
,
3303 Defining_Identifier
=> Lvl
,
3304 Object_Definition
=>
3305 New_Occurrence_Of
(Standard_Natural
, Loc
));
3307 -- Install the declaration and perform necessary expansion if we
3308 -- are dealing with a procedure call.
3310 if Nkind
(Call_Node
) = N_Procedure_Call_Statement
then
3315 -- If_Exp_Res : Typ;
3317 -- Lvl := 0; -- Access level
3318 -- If_Exp_Res := Exp;
3320 -- in If_Exp_Res end;},
3325 Insert_Before_And_Analyze
(Call_Node
, Decl
);
3327 -- Ditto for a function call. Note that we do not wrap the function
3328 -- call into an expression with action to avoid bad interactions with
3329 -- Exp_Ch4.Process_Transient_In_Expression.
3333 -- Lvl : Natural; -- placed above the function call
3339 -- Lvl := 0; -- Access level
3340 -- If_Exp_Res := Exp;
3341 -- in If_Exp_Res end;},
3346 Insert_Action
(Call_Node
, Decl
);
3347 Analyze
(Call_Node
);
3350 -- Decorate the conditional expression with assignments to our level
3353 Insert_Level_Assign
(Prev
);
3355 -- Make our level temporary the passed actual
3358 (Expr
=> New_Occurrence_Of
(Lvl
, Loc
),
3359 EF
=> Extra_Accessibility
(Formal
));
3360 end Add_Cond_Expression_Extra_Actual
;
3362 --------------------------------------
3363 -- Add_Dummy_Build_In_Place_Actuals --
3364 --------------------------------------
3366 procedure Add_Dummy_Build_In_Place_Actuals
3367 (Function_Id
: Entity_Id
;
3368 Num_Added_Extra_Actuals
: Nat
:= 0)
3370 Loc
: constant Source_Ptr
:= Sloc
(Call_Node
);
3371 Formal
: Entity_Id
:= Extra_Formals
(Function_Id
);
3376 -- We never generate extra formals if expansion is not active because
3377 -- we don't need them unless we are generating code. No action needed
3378 -- for thunks since they propagate all their extra actuals.
3380 if not Expander_Active
3381 or else Is_Thunk
(Current_Scope
)
3386 -- Skip already-added non-BIP extra actuals
3388 Skip_Extra
:= Num_Added_Extra_Actuals
;
3389 while Skip_Extra
> 0 loop
3390 pragma Assert
(not Is_Build_In_Place_Entity
(Formal
));
3391 Formal
:= Extra_Formal
(Formal
);
3392 Skip_Extra
:= Skip_Extra
- 1;
3395 -- Append the dummy BIP extra actuals
3397 while Present
(Formal
) loop
3398 pragma Assert
(Is_Build_In_Place_Entity
(Formal
));
3402 if Etype
(Formal
) = Standard_Natural
then
3403 Actual
:= Make_Integer_Literal
(Loc
, Uint_0
);
3404 Analyze_And_Resolve
(Actual
, Standard_Natural
);
3405 Add_Extra_Actual_To_Call
(N
, Formal
, Actual
);
3409 elsif Etype
(Formal
) = Standard_Integer
then
3410 Actual
:= Make_Integer_Literal
(Loc
, Uint_0
);
3411 Analyze_And_Resolve
(Actual
, Standard_Integer
);
3412 Add_Extra_Actual_To_Call
(N
, Formal
, Actual
);
3414 -- BIPstoragepool, BIPfinalizationmaster, BIPactivationchain,
3417 elsif Is_Access_Type
(Etype
(Formal
)) then
3418 Actual
:= Make_Null
(Loc
);
3419 Analyze_And_Resolve
(Actual
, Etype
(Formal
));
3420 Add_Extra_Actual_To_Call
(N
, Formal
, Actual
);
3423 pragma Assert
(False);
3424 raise Program_Error
;
3427 Formal
:= Extra_Formal
(Formal
);
3430 -- Mark the call as processed build-in-place call; required
3431 -- to avoid adding the extra formals twice.
3433 Set_Is_Expanded_Build_In_Place_Call
(Call_Node
);
3435 pragma Assert
(Check_Number_Of_Actuals
(Call_Node
, Function_Id
));
3436 pragma Assert
(Check_BIP_Actuals
(Call_Node
, Function_Id
));
3437 end Add_Dummy_Build_In_Place_Actuals
;
3439 ----------------------
3440 -- Add_Extra_Actual --
3441 ----------------------
3443 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
) is
3444 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
3447 if Extra_Actuals
= No_List
then
3448 Extra_Actuals
:= New_List
;
3449 Set_Parent
(Extra_Actuals
, Call_Node
);
3452 Append_To
(Extra_Actuals
,
3453 Make_Parameter_Association
(Loc
,
3454 Selector_Name
=> New_Occurrence_Of
(EF
, Loc
),
3455 Explicit_Actual_Parameter
=> Expr
));
3457 Analyze_And_Resolve
(Expr
, Etype
(EF
));
3459 if Nkind
(Call_Node
) = N_Function_Call
then
3460 Set_Is_Accessibility_Actual
(Parent
(Expr
));
3462 end Add_Extra_Actual
;
3464 ------------------------------------
3465 -- Add_View_Conversion_Invariants --
3466 ------------------------------------
3468 procedure Add_View_Conversion_Invariants
3469 (Formal
: Entity_Id
;
3473 Curr_Typ
: Entity_Id
;
3474 Inv_Checks
: List_Id
;
3475 Par_Typ
: Entity_Id
;
3478 Inv_Checks
:= No_List
;
3480 -- Extract the argument from a potentially nested set of view
3484 while Nkind
(Arg
) = N_Type_Conversion
loop
3485 Arg
:= Expression
(Arg
);
3488 -- Move up the derivation chain starting with the type of the formal
3489 -- parameter down to the type of the actual object.
3492 Par_Typ
:= Etype
(Arg
);
3493 while Par_Typ
/= Etype
(Formal
) and Par_Typ
/= Curr_Typ
loop
3494 Curr_Typ
:= Par_Typ
;
3496 if Has_Invariants
(Curr_Typ
)
3497 and then Present
(Invariant_Procedure
(Curr_Typ
))
3499 -- Verify the invariant of the current type. Generate:
3501 -- <Curr_Typ>Invariant (Curr_Typ (Arg));
3503 Prepend_New_To
(Inv_Checks
,
3504 Make_Procedure_Call_Statement
(Loc
,
3507 (Invariant_Procedure
(Curr_Typ
), Loc
),
3508 Parameter_Associations
=> New_List
(
3509 Make_Type_Conversion
(Loc
,
3510 Subtype_Mark
=> New_Occurrence_Of
(Curr_Typ
, Loc
),
3511 Expression
=> New_Copy_Tree
(Arg
)))));
3514 Par_Typ
:= Base_Type
(Etype
(Curr_Typ
));
3517 -- If the node is a function call the generated tests have been
3518 -- already handled in Insert_Post_Call_Actions.
3520 if not Is_Empty_List
(Inv_Checks
)
3521 and then Nkind
(Call_Node
) = N_Procedure_Call_Statement
3523 Insert_Actions_After
(Call_Node
, Inv_Checks
);
3525 end Add_View_Conversion_Invariants
;
3527 -----------------------------
3528 -- Can_Fold_Predicate_Call --
3529 -----------------------------
3531 function Can_Fold_Predicate_Call
(P
: Entity_Id
) return Boolean is
3534 function Augments_Other_Dynamic_Predicate
(DP_Aspect_Spec
: Node_Id
)
3536 -- Given a Dynamic_Predicate aspect aspecification for a
3537 -- discrete type, returns True iff another DP specification
3538 -- applies (indirectly, via a subtype type or a derived type)
3539 -- to the same entity that this aspect spec applies to.
3541 function May_Fold
(N
: Node_Id
) return Traverse_Result
;
3542 -- The predicate expression is foldable if it only contains operators
3543 -- and literals. During this check, we also replace occurrences of
3544 -- the formal of the constructed predicate function with the static
3545 -- value of the actual. This is done on a copy of the analyzed
3546 -- expression for the predicate.
3548 --------------------------------------
3549 -- Augments_Other_Dynamic_Predicate --
3550 --------------------------------------
3552 function Augments_Other_Dynamic_Predicate
(DP_Aspect_Spec
: Node_Id
)
3555 Aspect_Bearer
: Entity_Id
:= Entity
(DP_Aspect_Spec
);
3558 Aspect_Bearer
:= Nearest_Ancestor
(Aspect_Bearer
);
3560 if No
(Aspect_Bearer
) then
3565 Aspect_Spec
: constant Node_Id
:=
3566 Find_Aspect
(Aspect_Bearer
, Aspect_Dynamic_Predicate
);
3568 if Present
(Aspect_Spec
)
3569 and then Aspect_Spec
/= DP_Aspect_Spec
3571 -- Found another Dynamic_Predicate aspect spec
3576 end Augments_Other_Dynamic_Predicate
;
3582 function May_Fold
(N
: Node_Id
) return Traverse_Result
is
3588 when N_Expanded_Name
3591 if Ekind
(Entity
(N
)) = E_In_Parameter
3592 and then Entity
(N
) = First_Entity
(P
)
3594 Rewrite
(N
, New_Copy
(Actual
));
3595 Set_Is_Static_Expression
(N
);
3598 elsif Ekind
(Entity
(N
)) = E_Enumeration_Literal
then
3605 when N_Case_Expression
3610 when N_Integer_Literal
=>
3618 function Try_Fold
is new Traverse_Func
(May_Fold
);
3620 -- Other Local variables
3622 Subt
: constant Entity_Id
:= Etype
(First_Entity
(P
));
3626 -- Start of processing for Can_Fold_Predicate_Call
3629 -- Folding is only interesting if the actual is static and its type
3630 -- has a Dynamic_Predicate aspect. For CodePeer we preserve the
3633 Actual
:= First
(Parameter_Associations
(Call_Node
));
3634 Aspect
:= Find_Aspect
(Subt
, Aspect_Dynamic_Predicate
);
3636 -- If actual is a declared constant, retrieve its value
3638 if Is_Entity_Name
(Actual
)
3639 and then Ekind
(Entity
(Actual
)) = E_Constant
3641 Actual
:= Constant_Value
(Entity
(Actual
));
3645 or else Nkind
(Actual
) /= N_Integer_Literal
3646 or else not Has_Dynamic_Predicate_Aspect
(Subt
)
3649 -- Do not fold if multiple applicable predicate aspects
3650 or else Has_Ghost_Predicate_Aspect
(Subt
)
3651 or else Has_Aspect
(Subt
, Aspect_Static_Predicate
)
3652 or else Has_Aspect
(Subt
, Aspect_Predicate
)
3653 or else Augments_Other_Dynamic_Predicate
(Aspect
)
3654 or else CodePeer_Mode
3659 -- Retrieve the analyzed expression for the predicate
3661 Pred
:= New_Copy_Tree
(Expression
(Aspect
));
3663 if Try_Fold
(Pred
) = OK
then
3664 Rewrite
(Call_Node
, Pred
);
3665 Analyze_And_Resolve
(Call_Node
, Standard_Boolean
);
3668 -- Otherwise continue the expansion of the function call
3673 end Can_Fold_Predicate_Call
;
3675 ------------------------------
3676 -- Check_Subprogram_Variant --
3677 ------------------------------
3679 procedure Check_Subprogram_Variant
is
3681 function Duplicate_Params_Without_Extra_Actuals
3682 (Call_Node
: Node_Id
) return List_Id
;
3683 -- Duplicate actual parameters of Call_Node into New_Call without
3686 --------------------------------------------
3687 -- Duplicate_Params_Without_Extra_Actuals --
3688 --------------------------------------------
3690 function Duplicate_Params_Without_Extra_Actuals
3691 (Call_Node
: Node_Id
) return List_Id
3693 Proc_Id
: constant Entity_Id
:= Entity
(Name
(Call_Node
));
3694 Actuals
: constant List_Id
:= Parameter_Associations
(Call_Node
);
3696 Actual
: Node_Or_Entity_Id
;
3700 if Actuals
= No_List
then
3705 Actual
:= First
(Actuals
);
3706 Formal
:= First_Formal
(Proc_Id
);
3708 while Present
(Formal
)
3709 and then Formal
/= Extra_Formals
(Proc_Id
)
3711 Append
(New_Copy
(Actual
), NL
);
3714 Next_Formal
(Formal
);
3719 end Duplicate_Params_Without_Extra_Actuals
;
3723 Variant_Prag
: constant Node_Id
:=
3724 Get_Pragma
(Current_Scope
, Pragma_Subprogram_Variant
);
3727 Pragma_Arg1
: Node_Id
;
3728 Variant_Proc
: Entity_Id
;
3731 if Present
(Variant_Prag
) and then Is_Checked
(Variant_Prag
) then
3734 Expression
(First
(Pragma_Argument_Associations
(Variant_Prag
)));
3736 -- If pragma parameter is still an aggregate, it comes from a
3737 -- structural variant, which is not expanded and ignored for
3738 -- run-time execution.
3740 if Nkind
(Pragma_Arg1
) = N_Aggregate
then
3745 (First
(Component_Associations
(Pragma_Arg1
))))) =
3750 -- Otherwise, analysis of the pragma rewrites its argument with a
3751 -- reference to the internally generated procedure.
3753 Variant_Proc
:= Entity
(Pragma_Arg1
);
3756 Make_Procedure_Call_Statement
(Loc
,
3758 New_Occurrence_Of
(Variant_Proc
, Loc
),
3759 Parameter_Associations
=>
3760 Duplicate_Params_Without_Extra_Actuals
(Call_Node
));
3762 Insert_Action
(Call_Node
, New_Call
);
3764 pragma Assert
(Etype
(New_Call
) /= Any_Type
3765 or else Serious_Errors_Detected
> 0);
3767 end Check_Subprogram_Variant
;
3769 ---------------------------
3770 -- Inherited_From_Formal --
3771 ---------------------------
3773 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
is
3775 Gen_Par
: Entity_Id
;
3776 Gen_Prim
: Elist_Id
;
3781 -- If the operation is inherited, it is attached to the corresponding
3782 -- type derivation. If the parent in the derivation is a generic
3783 -- actual, it is a subtype of the actual, and we have to recover the
3784 -- original derived type declaration to find the proper parent.
3786 if Nkind
(Parent
(S
)) /= N_Full_Type_Declaration
3787 or else not Is_Derived_Type
(Defining_Identifier
(Parent
(S
)))
3788 or else Nkind
(Type_Definition
(Original_Node
(Parent
(S
)))) /=
3789 N_Derived_Type_Definition
3790 or else not In_Instance
3797 (Type_Definition
(Original_Node
(Parent
(S
))));
3799 if Nkind
(Indic
) = N_Subtype_Indication
then
3800 Par
:= Entity
(Subtype_Mark
(Indic
));
3802 Par
:= Entity
(Indic
);
3806 if not Is_Generic_Actual_Type
(Par
)
3807 or else Is_Tagged_Type
(Par
)
3808 or else Nkind
(Parent
(Par
)) /= N_Subtype_Declaration
3809 or else not In_Open_Scopes
(Scope
(Par
))
3813 Gen_Par
:= Generic_Parent_Type
(Parent
(Par
));
3816 -- If the actual has no generic parent type, the formal is not
3817 -- a formal derived type, so nothing to inherit.
3819 if No
(Gen_Par
) then
3823 -- If the generic parent type is still the generic type, this is a
3824 -- private formal, not a derived formal, and there are no operations
3825 -- inherited from the formal.
3827 if Nkind
(Parent
(Gen_Par
)) = N_Formal_Type_Declaration
then
3831 Gen_Prim
:= Collect_Primitive_Operations
(Gen_Par
);
3833 Elmt
:= First_Elmt
(Gen_Prim
);
3834 while Present
(Elmt
) loop
3835 if Chars
(Node
(Elmt
)) = Chars
(S
) then
3841 F1
:= First_Formal
(S
);
3842 F2
:= First_Formal
(Node
(Elmt
));
3844 and then Present
(F2
)
3846 if Etype
(F1
) = Etype
(F2
)
3847 or else Etype
(F2
) = Gen_Par
3853 exit; -- not the right subprogram
3865 raise Program_Error
;
3866 end Inherited_From_Formal
;
3868 --------------------------
3869 -- In_Unfrozen_Instance --
3870 --------------------------
3872 function In_Unfrozen_Instance
(E
: Entity_Id
) return Boolean is
3877 while Present
(S
) and then S
/= Standard_Standard
loop
3878 if Is_Generic_Instance
(S
)
3879 and then Present
(Freeze_Node
(S
))
3880 and then not Analyzed
(Freeze_Node
(S
))
3889 end In_Unfrozen_Instance
;
3891 ----------------------------------
3892 -- Is_Class_Wide_Interface_Type --
3893 ----------------------------------
3895 function Is_Class_Wide_Interface_Type
(E
: Entity_Id
) return Boolean is
3897 Typ
: Entity_Id
:= E
;
3900 if Has_Non_Limited_View
(Typ
) then
3901 Typ
:= Non_Limited_View
(Typ
);
3904 if Ekind
(Typ
) = E_Anonymous_Access_Type
then
3905 DDT
:= Directly_Designated_Type
(Typ
);
3907 if Has_Non_Limited_View
(DDT
) then
3908 DDT
:= Non_Limited_View
(DDT
);
3911 return Is_Class_Wide_Type
(DDT
) and then Is_Interface
(DDT
);
3913 return Is_Class_Wide_Type
(Typ
) and then Is_Interface
(Typ
);
3915 end Is_Class_Wide_Interface_Type
;
3917 -------------------------
3918 -- Is_Direct_Deep_Call --
3919 -------------------------
3921 function Is_Direct_Deep_Call
(Subp
: Entity_Id
) return Boolean is
3923 if Is_TSS
(Subp
, TSS_Deep_Adjust
)
3924 or else Is_TSS
(Subp
, TSS_Deep_Finalize
)
3925 or else Is_TSS
(Subp
, TSS_Deep_Initialize
)
3932 Actual
:= First
(Parameter_Associations
(Call_Node
));
3933 Formal
:= First_Formal
(Subp
);
3934 while Present
(Actual
)
3935 and then Present
(Formal
)
3937 if Nkind
(Actual
) = N_Identifier
3938 and then Is_Controlling_Actual
(Actual
)
3939 and then Etype
(Actual
) = Etype
(Formal
)
3945 Next_Formal
(Formal
);
3951 end Is_Direct_Deep_Call
;
3957 function New_Value
(From
: Node_Id
) return Node_Id
is
3958 Res
: constant Node_Id
:= Duplicate_Subexpr
(From
);
3960 if Is_Access_Type
(Etype
(From
)) then
3961 return Make_Explicit_Dereference
(Sloc
(From
), Prefix
=> Res
);
3969 Remote
: constant Boolean := Is_Remote_Call
(Call_Node
);
3972 Orig_Subp
: Entity_Id
:= Empty
;
3973 Param_Count
: Positive;
3974 Parent_Formal
: Entity_Id
;
3975 Parent_Subp
: Entity_Id
;
3979 CW_Interface_Formals_Present
: Boolean := False;
3981 -- Start of processing for Expand_Call_Helper
3984 Post_Call
:= New_List
;
3986 -- Expand the function or procedure call if the first actual has a
3987 -- declared dimension aspect, and the subprogram is declared in one
3988 -- of the dimension I/O packages.
3990 if Ada_Version
>= Ada_2012
3991 and then Nkind
(Call_Node
) in N_Subprogram_Call
3992 and then Present
(Parameter_Associations
(Call_Node
))
3994 Expand_Put_Call_With_Symbol
(Call_Node
);
3997 -- Ignore if previous error
3999 if Nkind
(Call_Node
) in N_Has_Etype
4000 and then Etype
(Call_Node
) = Any_Type
4005 -- Call using access to subprogram with explicit dereference
4007 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
4008 Subp
:= Etype
(Name
(Call_Node
));
4009 Parent_Subp
:= Empty
;
4011 -- Case of call to simple entry, where the Name is a selected component
4012 -- whose prefix is the task, and whose selector name is the entry name
4014 elsif Nkind
(Name
(Call_Node
)) = N_Selected_Component
then
4015 Subp
:= Entity
(Selector_Name
(Name
(Call_Node
)));
4016 Parent_Subp
:= Empty
;
4018 -- Case of call to member of entry family, where Name is an indexed
4019 -- component, with the prefix being a selected component giving the
4020 -- task and entry family name, and the index being the entry index.
4022 elsif Nkind
(Name
(Call_Node
)) = N_Indexed_Component
then
4023 Subp
:= Entity
(Selector_Name
(Prefix
(Name
(Call_Node
))));
4024 Parent_Subp
:= Empty
;
4029 Subp
:= Entity
(Name
(Call_Node
));
4030 Parent_Subp
:= Alias
(Subp
);
4032 -- Replace call to Raise_Exception by call to Raise_Exception_Always
4033 -- if we can tell that the first parameter cannot possibly be null.
4034 -- This improves efficiency by avoiding a run-time test.
4036 -- We do not do this if Raise_Exception_Always does not exist, which
4037 -- can happen in configurable run time profiles which provide only a
4040 if Is_RTE
(Subp
, RE_Raise_Exception
)
4041 and then RTE_Available
(RE_Raise_Exception_Always
)
4044 FA
: constant Node_Id
:=
4045 Original_Node
(First_Actual
(Call_Node
));
4048 -- The case we catch is where the first argument is obtained
4049 -- using the Identity attribute (which must always be
4052 if Nkind
(FA
) = N_Attribute_Reference
4053 and then Attribute_Name
(FA
) = Name_Identity
4055 Subp
:= RTE
(RE_Raise_Exception_Always
);
4056 Set_Name
(Call_Node
, New_Occurrence_Of
(Subp
, Loc
));
4061 if Ekind
(Subp
) = E_Entry
then
4062 Parent_Subp
:= Empty
;
4066 -- Ensure that the called subprogram has all its formals
4068 if not Is_Frozen
(Subp
) then
4069 Create_Extra_Formals
(Subp
);
4072 -- Ada 2005 (AI-345): We have a procedure call as a triggering
4073 -- alternative in an asynchronous select or as an entry call in
4074 -- a conditional or timed select. Check whether the procedure call
4075 -- is a renaming of an entry and rewrite it as an entry call.
4077 if Ada_Version
>= Ada_2005
4078 and then Nkind
(Call_Node
) = N_Procedure_Call_Statement
4080 ((Nkind
(Parent
(Call_Node
)) = N_Triggering_Alternative
4081 and then Triggering_Statement
(Parent
(Call_Node
)) = Call_Node
)
4083 (Nkind
(Parent
(Call_Node
)) = N_Entry_Call_Alternative
4084 and then Entry_Call_Statement
(Parent
(Call_Node
)) = Call_Node
))
4088 Ren_Root
: Entity_Id
:= Subp
;
4091 -- This may be a chain of renamings, find the root
4093 if Present
(Alias
(Ren_Root
)) then
4094 Ren_Root
:= Alias
(Ren_Root
);
4097 if Present
(Parent
(Ren_Root
))
4098 and then Present
(Original_Node
(Parent
(Parent
(Ren_Root
))))
4100 Ren_Decl
:= Original_Node
(Parent
(Parent
(Ren_Root
)));
4102 if Nkind
(Ren_Decl
) = N_Subprogram_Renaming_Declaration
then
4104 Make_Entry_Call_Statement
(Loc
,
4106 New_Copy_Tree
(Name
(Ren_Decl
)),
4107 Parameter_Associations
=>
4109 (Parameter_Associations
(Call_Node
))));
4117 -- If this is a call to a predicate function, try to constant fold it
4119 if Nkind
(Call_Node
) = N_Function_Call
4120 and then Is_Entity_Name
(Name
(Call_Node
))
4121 and then Is_Predicate_Function
(Subp
)
4122 and then Can_Fold_Predicate_Call
(Subp
)
4127 if Transform_Function_Array
4128 and then Nkind
(Call_Node
) = N_Function_Call
4129 and then Is_Entity_Name
(Name
(Call_Node
))
4132 Func_Id
: constant Entity_Id
:=
4133 Ultimate_Alias
(Entity
(Name
(Call_Node
)));
4135 -- When generating C code, transform a function call that returns
4136 -- a constrained array type into procedure form.
4138 if Rewritten_For_C
(Func_Id
) then
4140 -- For internally generated calls ensure that they reference
4141 -- the entity of the spec of the called function (needed since
4142 -- the expander may generate calls using the entity of their
4145 if not Comes_From_Source
(Call_Node
)
4146 and then Nkind
(Unit_Declaration_Node
(Func_Id
)) =
4149 Set_Entity
(Name
(Call_Node
),
4150 Corresponding_Function
4151 (Corresponding_Procedure
(Func_Id
)));
4154 Rewrite_Function_Call_For_C
(Call_Node
);
4157 -- Also introduce a temporary for functions that return a record
4158 -- called within another procedure or function call, since records
4159 -- are passed by pointer in the generated C code, and we cannot
4160 -- take a pointer from a subprogram call.
4162 elsif Modify_Tree_For_C
4163 and then Nkind
(Parent
(Call_Node
)) in N_Subprogram_Call
4164 and then Is_Record_Type
(Etype
(Func_Id
))
4167 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
4172 -- Temp : ... := Func_Call (...);
4175 Make_Object_Declaration
(Loc
,
4176 Defining_Identifier
=> Temp_Id
,
4177 Object_Definition
=>
4178 New_Occurrence_Of
(Etype
(Func_Id
), Loc
),
4180 Make_Function_Call
(Loc
,
4182 New_Occurrence_Of
(Func_Id
, Loc
),
4183 Parameter_Associations
=>
4184 Parameter_Associations
(Call_Node
)));
4186 Insert_Action
(Parent
(Call_Node
), Decl
);
4187 Rewrite
(Call_Node
, New_Occurrence_Of
(Temp_Id
, Loc
));
4194 -- First step, compute extra actuals, corresponding to any Extra_Formals
4195 -- present. Note that we do not access Extra_Formals directly, instead
4196 -- we simply note the presence of the extra formals as we process the
4197 -- regular formals collecting corresponding actuals in Extra_Actuals.
4199 -- We also generate any required range checks for actuals for in formals
4200 -- as we go through the loop, since this is a convenient place to do it.
4201 -- (Though it seems that this would be better done in Expand_Actuals???)
4203 -- Special case: Thunks must not compute the extra actuals; they must
4204 -- just propagate to the target primitive their extra actuals.
4206 if Is_Thunk
(Current_Scope
)
4207 and then Thunk_Entity
(Current_Scope
) = Subp
4208 and then Present
(Extra_Formals
(Subp
))
4210 pragma Assert
(Extra_Formals_Match_OK
(Current_Scope
, Subp
));
4213 Target_Formal
: Entity_Id
;
4214 Thunk_Formal
: Entity_Id
;
4217 Target_Formal
:= Extra_Formals
(Subp
);
4218 Thunk_Formal
:= Extra_Formals
(Current_Scope
);
4219 while Present
(Target_Formal
) loop
4221 (Expr
=> New_Occurrence_Of
(Thunk_Formal
, Loc
),
4222 EF
=> Thunk_Formal
);
4224 Target_Formal
:= Extra_Formal
(Target_Formal
);
4225 Thunk_Formal
:= Extra_Formal
(Thunk_Formal
);
4228 while Is_Non_Empty_List
(Extra_Actuals
) loop
4229 Add_Actual_Parameter
(Remove_Head
(Extra_Actuals
));
4232 -- Mark the call as processed build-in-place call; required
4233 -- to avoid adding the extra formals twice.
4235 if Nkind
(Call_Node
) = N_Function_Call
then
4236 Set_Is_Expanded_Build_In_Place_Call
(Call_Node
);
4239 Expand_Actuals
(Call_Node
, Subp
, Post_Call
);
4240 pragma Assert
(Is_Empty_List
(Post_Call
));
4241 pragma Assert
(Check_Number_Of_Actuals
(Call_Node
, Subp
));
4242 pragma Assert
(Check_BIP_Actuals
(Call_Node
, Subp
));
4247 Formal
:= First_Formal
(Subp
);
4248 Actual
:= First_Actual
(Call_Node
);
4250 while Present
(Formal
) loop
4251 -- Prepare to examine current entry
4255 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
4256 -- to expand it in a further round.
4258 CW_Interface_Formals_Present
:=
4259 CW_Interface_Formals_Present
4260 or else Is_Class_Wide_Interface_Type
(Etype
(Formal
));
4262 -- Create possible extra actual for constrained case. Usually, the
4263 -- extra actual is of the form actual'constrained, but since this
4264 -- attribute is only available for unconstrained records, TRUE is
4265 -- expanded if the type of the formal happens to be constrained (for
4266 -- instance when this procedure is inherited from an unconstrained
4267 -- record to a constrained one) or if the actual has no discriminant
4268 -- (its type is constrained). An exception to this is the case of a
4269 -- private type without discriminants. In this case we pass FALSE
4270 -- because the object has underlying discriminants with defaults.
4272 if Present
(Extra_Constrained
(Formal
)) then
4273 if Is_Private_Type
(Etype
(Prev
))
4274 and then not Has_Discriminants
(Base_Type
(Etype
(Prev
)))
4277 (Expr
=> New_Occurrence_Of
(Standard_False
, Loc
),
4278 EF
=> Extra_Constrained
(Formal
));
4280 elsif Is_Constrained
(Etype
(Formal
))
4281 or else not Has_Discriminants
(Etype
(Prev
))
4284 (Expr
=> New_Occurrence_Of
(Standard_True
, Loc
),
4285 EF
=> Extra_Constrained
(Formal
));
4287 -- Do not produce extra actuals for Unchecked_Union parameters.
4288 -- Jump directly to the end of the loop.
4290 elsif Is_Unchecked_Union
(Base_Type
(Etype
(Actual
))) then
4291 goto Skip_Extra_Actual_Generation
;
4294 -- If the actual is a type conversion, then the constrained
4295 -- test applies to the actual, not the target type.
4301 -- Test for unchecked conversions as well, which can occur
4302 -- as out parameter actuals on calls to stream procedures.
4305 while Nkind
(Act_Prev
) in N_Type_Conversion
4306 | N_Unchecked_Type_Conversion
4308 Act_Prev
:= Expression
(Act_Prev
);
4311 -- If the expression is a conversion of a dereference, this
4312 -- is internally generated code that manipulates addresses,
4313 -- e.g. when building interface tables. No check should
4314 -- occur in this case, and the discriminated object is not
4315 -- directly at hand.
4317 if not Comes_From_Source
(Actual
)
4318 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
4319 and then Nkind
(Act_Prev
) = N_Explicit_Dereference
4322 (Expr
=> New_Occurrence_Of
(Standard_False
, Loc
),
4323 EF
=> Extra_Constrained
(Formal
));
4328 Make_Attribute_Reference
(Sloc
(Prev
),
4330 Duplicate_Subexpr_No_Checks
4331 (Act_Prev
, Name_Req
=> True),
4332 Attribute_Name
=> Name_Constrained
),
4333 EF
=> Extra_Constrained
(Formal
));
4339 -- Create possible extra actual for accessibility level
4341 if Present
(Extra_Accessibility
(Formal
)) then
4342 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
4343 -- accessibility levels.
4345 if Is_Thunk
(Current_Scope
) then
4347 Parm_Ent
: Entity_Id
;
4350 if Is_Controlling_Actual
(Actual
) then
4352 -- Find the corresponding actual of the thunk
4354 Parm_Ent
:= First_Entity
(Current_Scope
);
4355 for J
in 2 .. Param_Count
loop
4356 Next_Entity
(Parm_Ent
);
4359 -- Handle unchecked conversion of access types generated
4360 -- in thunks (cf. Expand_Interface_Thunk).
4362 elsif Is_Access_Type
(Etype
(Actual
))
4363 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
4365 Parm_Ent
:= Entity
(Expression
(Actual
));
4367 else pragma Assert
(Is_Entity_Name
(Actual
));
4368 Parm_Ent
:= Entity
(Actual
);
4372 (Expr
=> Accessibility_Level
4374 Level
=> Dynamic_Level
,
4375 Allow_Alt_Model
=> False),
4376 EF
=> Extra_Accessibility
(Formal
));
4379 -- Conditional expressions
4381 elsif Nkind
(Prev
) = N_Expression_With_Actions
4382 and then Nkind
(Original_Node
(Prev
)) in
4383 N_If_Expression | N_Case_Expression
4385 Add_Cond_Expression_Extra_Actual
(Formal
);
4387 -- Internal constant generated to remove side effects (normally
4388 -- from the expansion of dispatching calls).
4390 -- First verify the actual is internal
4392 elsif not Comes_From_Source
(Prev
)
4393 and then not Is_Rewrite_Substitution
(Prev
)
4395 -- Next check that the actual is a constant
4397 and then Nkind
(Prev
) = N_Identifier
4398 and then Ekind
(Entity
(Prev
)) = E_Constant
4399 and then Nkind
(Parent
(Entity
(Prev
))) = N_Object_Declaration
4401 -- Generate the accessibility level based on the expression in
4402 -- the constant's declaration.
4405 Ent
: Entity_Id
:= Entity
(Prev
);
4408 -- Handle deferred constants
4410 if Present
(Full_View
(Ent
)) then
4411 Ent
:= Full_View
(Ent
);
4415 (Expr
=> Accessibility_Level
4416 (Expr
=> Expression
(Parent
(Ent
)),
4417 Level
=> Dynamic_Level
,
4418 Allow_Alt_Model
=> False),
4419 EF
=> Extra_Accessibility
(Formal
));
4426 (Expr
=> Accessibility_Level
4428 Level
=> Dynamic_Level
,
4429 Allow_Alt_Model
=> False),
4430 EF
=> Extra_Accessibility
(Formal
));
4434 -- Perform the check of 4.6(49) that prevents a null value from being
4435 -- passed as an actual to an access parameter. Note that the check
4436 -- is elided in the common cases of passing an access attribute or
4437 -- access parameter as an actual. Also, we currently don't enforce
4438 -- this check for expander-generated actuals and when -gnatdj is set.
4440 if Ada_Version
>= Ada_2005
then
4442 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
4443 -- the intent of 6.4.1(13) is that null-exclusion checks should
4444 -- not be done for 'out' parameters, even though it refers only
4445 -- to constraint checks, and a null_exclusion is not a constraint.
4446 -- Note that AI05-0196-1 corrects this mistake in the RM.
4448 if Is_Access_Type
(Etype
(Formal
))
4449 and then Can_Never_Be_Null
(Etype
(Formal
))
4450 and then Ekind
(Formal
) /= E_Out_Parameter
4451 and then Nkind
(Prev
) /= N_Raise_Constraint_Error
4452 and then (Known_Null
(Prev
)
4453 or else not Can_Never_Be_Null
(Etype
(Prev
)))
4455 Install_Null_Excluding_Check
(Prev
);
4458 -- Ada_Version < Ada_2005
4461 if Ekind
(Etype
(Formal
)) /= E_Anonymous_Access_Type
4462 or else Access_Checks_Suppressed
(Subp
)
4466 elsif Debug_Flag_J
then
4469 elsif not Comes_From_Source
(Prev
) then
4472 elsif Is_Entity_Name
(Prev
)
4473 and then Ekind
(Etype
(Prev
)) = E_Anonymous_Access_Type
4477 elsif Nkind
(Prev
) in N_Allocator | N_Attribute_Reference
then
4481 Install_Null_Excluding_Check
(Prev
);
4485 -- Perform appropriate validity checks on parameters that
4488 if Validity_Checks_On
then
4489 if (Ekind
(Formal
) = E_In_Parameter
4490 and then Validity_Check_In_Params
)
4492 (Ekind
(Formal
) = E_In_Out_Parameter
4493 and then Validity_Check_In_Out_Params
)
4495 -- If the actual is an indexed component of a packed type (or
4496 -- is an indexed or selected component whose prefix recursively
4497 -- meets this condition), it has not been expanded yet. It will
4498 -- be copied in the validity code that follows, and has to be
4499 -- expanded appropriately, so reanalyze it.
4501 -- What we do is just to unset analyzed bits on prefixes till
4502 -- we reach something that does not have a prefix.
4509 while Nkind
(Nod
) in
4510 N_Indexed_Component | N_Selected_Component
4512 Set_Analyzed
(Nod
, False);
4513 Nod
:= Prefix
(Nod
);
4517 Ensure_Valid
(Actual
);
4521 -- For IN OUT and OUT parameters, ensure that subscripts are valid
4522 -- since this is a left side reference. We only do this for calls
4523 -- from the source program since we assume that compiler generated
4524 -- calls explicitly generate any required checks. We also need it
4525 -- only if we are doing standard validity checks, since clearly it is
4526 -- not needed if validity checks are off, and in subscript validity
4527 -- checking mode, all indexed components are checked with a call
4528 -- directly from Expand_N_Indexed_Component.
4530 if Comes_From_Source
(Call_Node
)
4531 and then Ekind
(Formal
) /= E_In_Parameter
4532 and then Validity_Checks_On
4533 and then Validity_Check_Default
4534 and then not Validity_Check_Subscripts
4536 Check_Valid_Lvalue_Subscripts
(Actual
);
4539 -- Mark any scalar OUT parameter that is a simple variable as no
4540 -- longer known to be valid (unless the type is always valid). This
4541 -- reflects the fact that if an OUT parameter is never set in a
4542 -- procedure, then it can become invalid on the procedure return.
4544 if Ekind
(Formal
) = E_Out_Parameter
4545 and then Is_Entity_Name
(Actual
)
4546 and then Ekind
(Entity
(Actual
)) = E_Variable
4547 and then not Is_Known_Valid
(Etype
(Actual
))
4549 Set_Is_Known_Valid
(Entity
(Actual
), False);
4552 -- For an OUT or IN OUT parameter, if the actual is an entity, then
4553 -- clear current values, since they can be clobbered. We are probably
4554 -- doing this in more places than we need to, but better safe than
4555 -- sorry when it comes to retaining bad current values.
4557 if Ekind
(Formal
) /= E_In_Parameter
4558 and then Is_Entity_Name
(Actual
)
4559 and then Present
(Entity
(Actual
))
4562 Ent
: constant Entity_Id
:= Entity
(Actual
);
4566 -- For an OUT or IN OUT parameter that is an assignable entity,
4567 -- we do not want to clobber the Last_Assignment field, since
4568 -- if it is set, it was precisely because it is indeed an OUT
4569 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
4570 -- since the subprogram could have returned in invalid value.
4572 if Is_Assignable
(Ent
) then
4573 Sav
:= Last_Assignment
(Ent
);
4574 Kill_Current_Values
(Ent
);
4575 Set_Last_Assignment
(Ent
, Sav
);
4576 Set_Is_Known_Valid
(Ent
, False);
4577 Set_Is_True_Constant
(Ent
, False);
4579 -- For all other cases, just kill the current values
4582 Kill_Current_Values
(Ent
);
4587 -- If the formal is class-wide and the actual is an aggregate, force
4588 -- evaluation so that the back end who does not know about class-wide
4589 -- type, does not generate a temporary of the wrong size.
4591 if not Is_Class_Wide_Type
(Etype
(Formal
)) then
4594 elsif Nkind
(Actual
) = N_Aggregate
4595 or else (Nkind
(Actual
) = N_Qualified_Expression
4596 and then Nkind
(Expression
(Actual
)) = N_Aggregate
)
4598 Force_Evaluation
(Actual
);
4601 -- In a remote call, if the formal is of a class-wide type, check
4602 -- that the actual meets the requirements described in E.4(18).
4604 if Remote
and then Is_Class_Wide_Type
(Etype
(Formal
)) then
4605 Insert_Action
(Actual
,
4606 Make_Transportable_Check
(Loc
,
4607 Duplicate_Subexpr_Move_Checks
(Actual
)));
4610 -- Perform invariant checks for all intermediate types in a view
4611 -- conversion after successful return from a call that passes the
4612 -- view conversion as an IN OUT or OUT parameter (RM 7.3.2 (12/3,
4613 -- 13/3, 14/3)). Consider only source conversion in order to avoid
4614 -- generating spurious checks on complex expansion such as object
4615 -- initialization through an extension aggregate.
4617 if Comes_From_Source
(Call_Node
)
4618 and then Ekind
(Formal
) /= E_In_Parameter
4619 and then Nkind
(Actual
) = N_Type_Conversion
4621 Add_View_Conversion_Invariants
(Formal
, Actual
);
4624 -- Generating C the initialization of an allocator is performed by
4625 -- means of individual statements, and hence it must be done before
4628 if Modify_Tree_For_C
4629 and then Nkind
(Actual
) = N_Allocator
4630 and then Nkind
(Expression
(Actual
)) = N_Qualified_Expression
4632 Remove_Side_Effects
(Actual
);
4635 -- This label is required when skipping extra actual generation for
4636 -- Unchecked_Union parameters.
4638 <<Skip_Extra_Actual_Generation
>>
4640 Param_Count
:= Param_Count
+ 1;
4641 Next_Actual
(Actual
);
4642 Next_Formal
(Formal
);
4645 -- If we are calling an Ada 2012 function which needs to have the
4646 -- "accessibility level determined by the point of call" (AI05-0234)
4647 -- passed in to it, then pass it in.
4649 if Ekind
(Subp
) in E_Function | E_Operator | E_Subprogram_Type
4651 Present
(Extra_Accessibility_Of_Result
(Ultimate_Alias
(Subp
)))
4654 Extra_Form
: Node_Id
:= Empty
;
4655 Level
: Node_Id
:= Empty
;
4658 -- Detect cases where the function call has been internally
4659 -- generated by examining the original node and return library
4660 -- level - taking care to avoid ignoring function calls expanded
4661 -- in prefix notation.
4663 if Nkind
(Original_Node
(Call_Node
)) not in N_Function_Call
4664 | N_Selected_Component
4665 | N_Indexed_Component
4667 Level
:= Make_Integer_Literal
4668 (Loc
, Scope_Depth
(Standard_Standard
));
4670 -- Otherwise get the level normally based on the call node
4673 Level
:= Accessibility_Level
4675 Level
=> Dynamic_Level
,
4676 Allow_Alt_Model
=> False);
4679 -- It may be possible that we are re-expanding an already
4680 -- expanded call when are are dealing with dispatching ???
4682 if No
(Parameter_Associations
(Call_Node
))
4683 or else Nkind
(Last
(Parameter_Associations
(Call_Node
)))
4684 /= N_Parameter_Association
4685 or else not Is_Accessibility_Actual
4686 (Last
(Parameter_Associations
(Call_Node
)))
4688 Extra_Form
:= Extra_Accessibility_Of_Result
4689 (Ultimate_Alias
(Subp
));
4698 -- If we are expanding the RHS of an assignment we need to check if tag
4699 -- propagation is needed. You might expect this processing to be in
4700 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
4701 -- assignment might be transformed to a declaration for an unconstrained
4702 -- value if the expression is classwide.
4704 if Nkind
(Call_Node
) = N_Function_Call
4705 and then Is_Tag_Indeterminate
(Call_Node
)
4706 and then Is_Entity_Name
(Name
(Call_Node
))
4709 Ass
: Node_Id
:= Empty
;
4712 if Nkind
(Parent
(Call_Node
)) = N_Assignment_Statement
then
4713 Ass
:= Parent
(Call_Node
);
4715 elsif Nkind
(Parent
(Call_Node
)) = N_Qualified_Expression
4716 and then Nkind
(Parent
(Parent
(Call_Node
))) =
4717 N_Assignment_Statement
4719 Ass
:= Parent
(Parent
(Call_Node
));
4721 elsif Nkind
(Parent
(Call_Node
)) = N_Explicit_Dereference
4722 and then Nkind
(Parent
(Parent
(Call_Node
))) =
4723 N_Assignment_Statement
4725 Ass
:= Parent
(Parent
(Call_Node
));
4729 and then Is_Class_Wide_Type
(Etype
(Name
(Ass
)))
4731 -- Move the error messages below to sem???
4733 if Is_Access_Type
(Etype
(Call_Node
)) then
4734 if Designated_Type
(Etype
(Call_Node
)) /=
4735 Root_Type
(Etype
(Name
(Ass
)))
4738 ("tag-indeterminate expression must have designated "
4739 & "type& (RM 5.2 (6))",
4740 Call_Node
, Root_Type
(Etype
(Name
(Ass
))));
4742 Propagate_Tag
(Name
(Ass
), Call_Node
);
4745 elsif Etype
(Call_Node
) /= Root_Type
(Etype
(Name
(Ass
))) then
4747 ("tag-indeterminate expression must have type & "
4749 Call_Node
, Root_Type
(Etype
(Name
(Ass
))));
4752 Propagate_Tag
(Name
(Ass
), Call_Node
);
4755 -- The call will be rewritten as a dispatching call, and
4756 -- expanded as such.
4763 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
4764 -- it to point to the correct secondary virtual table.
4766 if Nkind
(Call_Node
) in N_Subprogram_Call
4767 and then CW_Interface_Formals_Present
4769 Expand_Interface_Actuals
(Call_Node
);
4772 -- Install class-wide preconditions runtime check when this is a
4773 -- dispatching primitive that has or inherits class-wide preconditions;
4774 -- otherwise no runtime check is installed.
4776 if Nkind
(Call_Node
) in N_Subprogram_Call
4777 and then Is_Dispatching_Operation
(Subp
)
4779 Install_Class_Preconditions_Check
(Call_Node
);
4782 -- Deals with Dispatch_Call if we still have a call, before expanding
4783 -- extra actuals since this will be done on the re-analysis of the
4784 -- dispatching call. Note that we do not try to shorten the actual list
4785 -- for a dispatching call, it would not make sense to do so. Expansion
4786 -- of dispatching calls is suppressed for VM targets, because the VM
4787 -- back-ends directly handle the generation of dispatching calls and
4788 -- would have to undo any expansion to an indirect call.
4790 if Nkind
(Call_Node
) in N_Subprogram_Call
4791 and then Present
(Controlling_Argument
(Call_Node
))
4793 if Tagged_Type_Expansion
then
4794 Expand_Dispatching_Call
(Call_Node
);
4796 -- Expand_Dispatching_Call takes care of all the needed processing
4804 Call_Typ
: constant Entity_Id
:= Etype
(Call_Node
);
4805 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
4806 Eq_Prim_Op
: Entity_Id
:= Empty
;
4809 Prev_Call
: Node_Id
;
4812 Apply_Tag_Checks
(Call_Node
);
4814 if not Is_Limited_Type
(Typ
) then
4815 Eq_Prim_Op
:= Find_Prim_Op
(Typ
, Name_Op_Eq
);
4818 -- If this is a dispatching "=", we must first compare the
4819 -- tags so we generate: x.tag = y.tag and then x = y
4821 if Subp
= Eq_Prim_Op
then
4823 -- Mark the node as analyzed to avoid reanalyzing this
4824 -- dispatching call (which would cause a never-ending loop)
4826 Prev_Call
:= Relocate_Node
(Call_Node
);
4827 Set_Analyzed
(Prev_Call
);
4829 Param
:= First_Actual
(Call_Node
);
4835 Make_Selected_Component
(Loc
,
4836 Prefix
=> New_Value
(Param
),
4839 (First_Tag_Component
(Typ
), Loc
)),
4842 Make_Selected_Component
(Loc
,
4844 Unchecked_Convert_To
(Typ
,
4845 New_Value
(Next_Actual
(Param
))),
4848 (First_Tag_Component
(Typ
), Loc
))),
4849 Right_Opnd
=> Prev_Call
);
4851 Rewrite
(Call_Node
, New_Call
);
4853 (Call_Node
, Call_Typ
, Suppress
=> All_Checks
);
4856 -- Expansion of a dispatching call results in an indirect call,
4857 -- which in turn causes current values to be killed (see
4858 -- Resolve_Call), so on VM targets we do the call here to
4859 -- ensure consistent warnings between VM and non-VM targets.
4861 Kill_Current_Values
;
4863 -- If this is a dispatching "=" then we must update the reference
4864 -- to the call node because we generated:
4865 -- x.tag = y.tag and then x = y
4867 if Subp
= Eq_Prim_Op
then
4868 Call_Node
:= Right_Opnd
(Call_Node
);
4873 -- Similarly, expand calls to RCI subprograms on which pragma
4874 -- All_Calls_Remote applies. The rewriting will be reanalyzed
4875 -- later. Do this only when the call comes from source since we
4876 -- do not want such a rewriting to occur in expanded code.
4878 if Is_All_Remote_Call
(Call_Node
) then
4879 Expand_All_Calls_Remote_Subprogram_Call
(Call_Node
);
4881 -- Similarly, do not add extra actuals for an entry call whose entity
4882 -- is a protected procedure, or for an internal protected subprogram
4883 -- call, because it will be rewritten as a protected subprogram call
4884 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
4886 elsif Is_Protected_Type
(Scope
(Subp
))
4887 and then Ekind
(Subp
) in E_Procedure | E_Function
4891 -- During that loop we gathered the extra actuals (the ones that
4892 -- correspond to Extra_Formals), so now they can be appended.
4894 elsif Is_Non_Empty_List
(Extra_Actuals
) then
4896 Num_Extra_Actuals
: constant Nat
:= List_Length
(Extra_Actuals
);
4899 while Is_Non_Empty_List
(Extra_Actuals
) loop
4900 Add_Actual_Parameter
(Remove_Head
(Extra_Actuals
));
4903 -- Add dummy extra BIP actuals if we are calling a function that
4904 -- inherited the BIP extra actuals but does not require them.
4906 if Nkind
(Call_Node
) = N_Function_Call
4907 and then Is_Function_Call_With_BIP_Formals
(Call_Node
)
4908 and then not Is_Build_In_Place_Function_Call
(Call_Node
)
4910 Add_Dummy_Build_In_Place_Actuals
(Subp
,
4911 Num_Added_Extra_Actuals
=> Num_Extra_Actuals
);
4915 -- Add dummy extra BIP actuals if we are calling a function that
4916 -- inherited the BIP extra actuals but does not require them.
4918 elsif Nkind
(Call_Node
) = N_Function_Call
4919 and then Is_Function_Call_With_BIP_Formals
(Call_Node
)
4920 and then not Is_Build_In_Place_Function_Call
(Call_Node
)
4922 Add_Dummy_Build_In_Place_Actuals
(Subp
);
4925 -- At this point we have all the actuals, so this is the point at which
4926 -- the various expansion activities for actuals is carried out.
4928 Expand_Actuals
(Call_Node
, Subp
, Post_Call
);
4930 -- If it is a recursive call then call the internal procedure that
4931 -- verifies Subprogram_Variant contract (if present and enabled).
4932 -- Detecting calls to subprogram aliases is necessary for recursive
4933 -- calls in instances of generic subprograms, where the renaming of
4934 -- the current subprogram is called.
4936 if Is_Subprogram
(Subp
)
4937 and then not Is_Ignored_Ghost_Entity
(Subp
)
4938 and then Same_Or_Aliased_Subprograms
(Subp
, Current_Scope
)
4940 Check_Subprogram_Variant
;
4943 -- Verify that the actuals do not share storage. This check must be done
4944 -- on the caller side rather that inside the subprogram to avoid issues
4945 -- of parameter passing.
4947 if Check_Aliasing_Of_Parameters
then
4948 Apply_Parameter_Aliasing_Checks
(Call_Node
, Subp
);
4951 -- If the subprogram is a renaming, or if it is inherited, replace it in
4952 -- the call with the name of the actual subprogram being called. If this
4953 -- is a dispatching call, the run-time decides what to call. The Alias
4954 -- attribute does not apply to entries.
4956 if Nkind
(Call_Node
) /= N_Entry_Call_Statement
4957 and then No
(Controlling_Argument
(Call_Node
))
4958 and then Present
(Parent_Subp
)
4959 and then not Is_Direct_Deep_Call
(Subp
)
4961 if Present
(Inherited_From_Formal
(Subp
)) then
4962 Parent_Subp
:= Inherited_From_Formal
(Subp
);
4964 Parent_Subp
:= Ultimate_Alias
(Parent_Subp
);
4967 -- The below setting of Entity is suspect, see F109-018 discussion???
4969 Set_Entity
(Name
(Call_Node
), Parent_Subp
);
4971 -- Inspect all formals of derived subprogram Subp. Compare parameter
4972 -- types with the parent subprogram and check whether an actual may
4973 -- need a type conversion to the corresponding formal of the parent
4976 -- Not clear whether intrinsic subprograms need such conversions. ???
4978 if not Is_Intrinsic_Subprogram
(Parent_Subp
)
4979 or else Is_Generic_Instance
(Parent_Subp
)
4982 procedure Convert
(Act
: Node_Id
; Typ
: Entity_Id
);
4983 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
4984 -- and resolve the newly generated construct.
4990 procedure Convert
(Act
: Node_Id
; Typ
: Entity_Id
) is
4992 Rewrite
(Act
, OK_Convert_To
(Typ
, Act
));
4993 Analyze_And_Resolve
(Act
, Typ
);
4998 Actual_Typ
: Entity_Id
;
4999 Formal_Typ
: Entity_Id
;
5000 Parent_Typ
: Entity_Id
;
5003 Actual
:= First_Actual
(Call_Node
);
5004 Formal
:= First_Formal
(Subp
);
5005 Parent_Formal
:= First_Formal
(Parent_Subp
);
5006 while Present
(Formal
) loop
5007 Actual_Typ
:= Etype
(Actual
);
5008 Formal_Typ
:= Etype
(Formal
);
5009 Parent_Typ
:= Etype
(Parent_Formal
);
5011 -- For an IN parameter of a scalar type, the derived formal
5012 -- type and parent formal type differ, and the parent formal
5013 -- type and actual type do not match statically.
5015 if Is_Scalar_Type
(Formal_Typ
)
5016 and then Ekind
(Formal
) = E_In_Parameter
5017 and then Formal_Typ
/= Parent_Typ
5019 not Subtypes_Statically_Match
(Parent_Typ
, Actual_Typ
)
5020 and then not Raises_Constraint_Error
(Actual
)
5022 Convert
(Actual
, Parent_Typ
);
5024 -- For access types, the parent formal type and actual type
5027 elsif Is_Access_Type
(Formal_Typ
)
5028 and then Base_Type
(Parent_Typ
) /= Base_Type
(Actual_Typ
)
5030 if Ekind
(Formal
) /= E_In_Parameter
then
5031 Convert
(Actual
, Parent_Typ
);
5033 elsif Ekind
(Parent_Typ
) = E_Anonymous_Access_Type
5034 and then Designated_Type
(Parent_Typ
) /=
5035 Designated_Type
(Actual_Typ
)
5036 and then not Is_Controlling_Formal
(Formal
)
5038 -- This unchecked conversion is not necessary unless
5039 -- inlining is enabled, because in that case the type
5040 -- mismatch may become visible in the body about to be
5044 Unchecked_Convert_To
(Parent_Typ
, Actual
));
5045 Analyze_And_Resolve
(Actual
, Parent_Typ
);
5048 -- If there is a change of representation, then generate a
5049 -- warning, and do the change of representation.
5051 elsif not Has_Compatible_Representation
5052 (Target_Typ
=> Formal_Typ
,
5053 Operand_Typ
=> Parent_Typ
)
5056 ("??change of representation required", Actual
);
5057 Convert
(Actual
, Parent_Typ
);
5059 -- For array and record types, the parent formal type and
5060 -- derived formal type have different sizes or pragma Pack
5063 elsif ((Is_Array_Type
(Formal_Typ
)
5064 and then Is_Array_Type
(Parent_Typ
))
5066 (Is_Record_Type
(Formal_Typ
)
5067 and then Is_Record_Type
(Parent_Typ
)))
5068 and then Known_Esize
(Formal_Typ
)
5069 and then Known_Esize
(Parent_Typ
)
5071 (Esize
(Formal_Typ
) /= Esize
(Parent_Typ
)
5072 or else Has_Pragma_Pack
(Formal_Typ
) /=
5073 Has_Pragma_Pack
(Parent_Typ
))
5075 Convert
(Actual
, Parent_Typ
);
5078 Next_Actual
(Actual
);
5079 Next_Formal
(Formal
);
5080 Next_Formal
(Parent_Formal
);
5086 Subp
:= Parent_Subp
;
5089 -- Deal with case where call is an explicit dereference
5091 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
5093 -- Handle case of access to protected subprogram type
5095 if Is_Access_Protected_Subprogram_Type
5096 (Base_Type
(Etype
(Prefix
(Name
(Call_Node
)))))
5098 -- If this is a call through an access to protected operation, the
5099 -- prefix has the form (object'address, operation'access). Rewrite
5100 -- as a for other protected calls: the object is the 1st parameter
5101 -- of the list of actuals.
5108 Ptr
: constant Node_Id
:= Prefix
(Name
(Call_Node
));
5110 T
: constant Entity_Id
:=
5111 Equivalent_Type
(Base_Type
(Etype
(Ptr
)));
5113 D_T
: constant Entity_Id
:=
5114 Designated_Type
(Base_Type
(Etype
(Ptr
)));
5118 Make_Selected_Component
(Loc
,
5119 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
5121 New_Occurrence_Of
(First_Entity
(T
), Loc
));
5124 Make_Selected_Component
(Loc
,
5125 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
5127 New_Occurrence_Of
(Next_Entity
(First_Entity
(T
)), Loc
));
5130 Make_Explicit_Dereference
(Loc
,
5133 if Present
(Parameter_Associations
(Call_Node
)) then
5134 Parm
:= Parameter_Associations
(Call_Node
);
5139 Prepend
(Obj
, Parm
);
5141 if Etype
(D_T
) = Standard_Void_Type
then
5143 Make_Procedure_Call_Statement
(Loc
,
5145 Parameter_Associations
=> Parm
);
5148 Make_Function_Call
(Loc
,
5150 Parameter_Associations
=> Parm
);
5153 Set_First_Named_Actual
(Call
, First_Named_Actual
(Call_Node
));
5154 Set_Etype
(Call
, Etype
(D_T
));
5156 -- We do not re-analyze the call to avoid infinite recursion.
5157 -- We analyze separately the prefix and the object, and set
5158 -- the checks on the prefix that would otherwise be emitted
5159 -- when resolving a call.
5161 Rewrite
(Call_Node
, Call
);
5163 Apply_Access_Check
(Nam
);
5170 -- If this is a call to an intrinsic subprogram, then perform the
5171 -- appropriate expansion to the corresponding tree node and we
5172 -- are all done (since after that the call is gone).
5174 -- In the case where the intrinsic is to be processed by the back end,
5175 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
5176 -- since the idea in this case is to pass the call unchanged. If the
5177 -- intrinsic is an inherited unchecked conversion, and the derived type
5178 -- is the target type of the conversion, we must retain it as the return
5179 -- type of the expression. Otherwise the expansion below, which uses the
5180 -- parent operation, will yield the wrong type.
5182 if Is_Intrinsic_Subprogram
(Subp
) then
5183 Expand_Intrinsic_Call
(Call_Node
, Subp
);
5185 if Nkind
(Call_Node
) = N_Unchecked_Type_Conversion
5186 and then Parent_Subp
/= Orig_Subp
5187 and then Etype
(Parent_Subp
) /= Etype
(Orig_Subp
)
5189 Set_Etype
(Call_Node
, Etype
(Orig_Subp
));
5195 if Ekind
(Subp
) in E_Function | E_Procedure
then
5197 -- We perform a simple optimization on calls for To_Address by
5198 -- replacing them with an unchecked conversion. Not only is this
5199 -- efficient, but it also avoids order of elaboration problems when
5200 -- address clauses are inlined (address expression elaborated at the
5203 -- We perform this optimization regardless of whether we are in the
5204 -- main unit or in a unit in the context of the main unit, to ensure
5205 -- that the generated tree is the same in both cases, for CodePeer
5208 if Is_RTE
(Subp
, RE_To_Address
) then
5210 Unchecked_Convert_To
5211 (RTE
(RE_Address
), Relocate_Node
(First_Actual
(Call_Node
))));
5214 -- A call to a null procedure is replaced by a null statement, but we
5215 -- are not allowed to ignore possible side effects of the call, so we
5216 -- make sure that actuals are evaluated.
5217 -- We also suppress this optimization for GNATcoverage.
5219 elsif Is_Null_Procedure
(Subp
)
5220 and then not Opt
.Suppress_Control_Flow_Optimizations
5222 Actual
:= First_Actual
(Call_Node
);
5223 while Present
(Actual
) loop
5224 Remove_Side_Effects
(Actual
);
5225 Next_Actual
(Actual
);
5228 Rewrite
(Call_Node
, Make_Null_Statement
(Loc
));
5232 -- Handle inlining. No action needed if the subprogram is not inlined
5234 if not Is_Inlined
(Subp
) then
5237 -- Front-end inlining of expression functions (performed also when
5238 -- back-end inlining is enabled).
5240 elsif Is_Inlinable_Expression_Function
(Subp
) then
5242 (Call_Node
, New_Copy
(Expression_Of_Expression_Function
(Subp
)));
5243 Analyze
(Call_Node
);
5246 -- Handle front-end inlining
5248 elsif not Back_End_Inlining
then
5249 Inlined_Subprogram
: declare
5251 Must_Inline
: Boolean := False;
5252 Spec
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
5255 -- Verify that the body to inline has already been seen, and
5256 -- that if the body is in the current unit the inlining does
5257 -- not occur earlier. This avoids order-of-elaboration problems
5260 -- This should be documented in sinfo/einfo ???
5263 or else Nkind
(Spec
) /= N_Subprogram_Declaration
5264 or else No
(Body_To_Inline
(Spec
))
5266 Must_Inline
:= False;
5268 -- If this an inherited function that returns a private type,
5269 -- do not inline if the full view is an unconstrained array,
5270 -- because such calls cannot be inlined.
5272 elsif Present
(Orig_Subp
)
5273 and then Is_Array_Type
(Etype
(Orig_Subp
))
5274 and then not Is_Constrained
(Etype
(Orig_Subp
))
5276 Must_Inline
:= False;
5278 elsif In_Unfrozen_Instance
(Scope
(Subp
)) then
5279 Must_Inline
:= False;
5282 Bod
:= Body_To_Inline
(Spec
);
5284 if (In_Extended_Main_Code_Unit
(Call_Node
)
5285 or else In_Extended_Main_Code_Unit
(Parent
(Call_Node
))
5286 or else Has_Pragma_Inline_Always
(Subp
))
5287 and then (not In_Same_Extended_Unit
(Sloc
(Bod
), Loc
)
5289 Earlier_In_Extended_Unit
(Sloc
(Bod
), Loc
))
5291 Must_Inline
:= True;
5293 -- If we are compiling a package body that is not the main
5294 -- unit, it must be for inlining/instantiation purposes,
5295 -- in which case we inline the call to insure that the same
5296 -- temporaries are generated when compiling the body by
5297 -- itself. Otherwise link errors can occur.
5299 -- If the function being called is itself in the main unit,
5300 -- we cannot inline, because there is a risk of double
5301 -- elaboration and/or circularity: the inlining can make
5302 -- visible a private entity in the body of the main unit,
5303 -- that gigi will see before its sees its proper definition.
5305 elsif not In_Extended_Main_Code_Unit
(Call_Node
)
5306 and then In_Package_Body
5308 Must_Inline
:= not In_Extended_Main_Source_Unit
(Subp
);
5310 -- Inline calls to _Wrapped_Statements when generating C
5312 elsif Modify_Tree_For_C
5313 and then In_Same_Extended_Unit
(Sloc
(Bod
), Loc
)
5314 and then Chars
(Name
(Call_Node
))
5315 = Name_uWrapped_Statements
5317 Must_Inline
:= True;
5322 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
5325 -- Let the back end handle it
5327 Add_Inlined_Body
(Subp
, Call_Node
);
5329 if Front_End_Inlining
5330 and then Nkind
(Spec
) = N_Subprogram_Declaration
5331 and then In_Extended_Main_Code_Unit
(Call_Node
)
5332 and then No
(Body_To_Inline
(Spec
))
5333 and then not Has_Completion
(Subp
)
5334 and then In_Same_Extended_Unit
(Sloc
(Spec
), Loc
)
5337 ("cannot inline& (body not seen yet)?",
5341 end Inlined_Subprogram
;
5343 -- Front-end expansion of simple functions returning unconstrained
5344 -- types (see Check_And_Split_Unconstrained_Function). Note that the
5345 -- case of a simple renaming (Body_To_Inline in N_Entity below, see
5346 -- also Build_Renamed_Body) cannot be expanded here because this may
5347 -- give rise to order-of-elaboration issues for the types of the
5348 -- parameters of the subprogram, if any.
5350 elsif Present
(Unit_Declaration_Node
(Subp
))
5351 and then Nkind
(Unit_Declaration_Node
(Subp
)) =
5352 N_Subprogram_Declaration
5353 and then Present
(Body_To_Inline
(Unit_Declaration_Node
(Subp
)))
5355 Nkind
(Body_To_Inline
(Unit_Declaration_Node
(Subp
))) not in
5358 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
5360 -- Back-end inlining either if optimization is enabled or the call is
5361 -- required to be inlined.
5363 elsif Optimization_Level
> 0
5364 or else Has_Pragma_Inline_Always
(Subp
)
5366 Add_Inlined_Body
(Subp
, Call_Node
);
5370 -- Check for protected subprogram. This is either an intra-object call,
5371 -- or a protected function call. Protected procedure calls are rewritten
5372 -- as entry calls and handled accordingly.
5374 -- In Ada 2005, this may be an indirect call to an access parameter that
5375 -- is an access_to_subprogram. In that case the anonymous type has a
5376 -- scope that is a protected operation, but the call is a regular one.
5377 -- In either case do not expand call if subprogram is eliminated.
5379 Scop
:= Scope
(Subp
);
5381 if Nkind
(Call_Node
) /= N_Entry_Call_Statement
5382 and then Is_Protected_Type
(Scop
)
5383 and then Ekind
(Subp
) /= E_Subprogram_Type
5384 and then not Is_Eliminated
(Subp
)
5386 -- If the call is an internal one, it is rewritten as a call to the
5387 -- corresponding unprotected subprogram.
5389 Expand_Protected_Subprogram_Call
(Call_Node
, Subp
, Scop
);
5392 -- Functions returning controlled objects need special attention. If
5393 -- the return type is limited, then the context is initialization and
5394 -- different processing applies. If the call is to a protected function,
5395 -- the expansion above will call Expand_Call recursively. Otherwise the
5396 -- function call is transformed into a reference to the result that has
5397 -- been built either on the primary or the secondary stack.
5399 if Needs_Finalization
(Etype
(Subp
)) then
5400 if not Is_Build_In_Place_Function_Call
(Call_Node
)
5402 (No
(First_Formal
(Subp
))
5404 not Is_Concurrent_Record_Type
(Etype
(First_Formal
(Subp
))))
5406 Expand_Ctrl_Function_Call
5407 (Call_Node
, Needs_Secondary_Stack
(Etype
(Subp
)));
5409 -- Build-in-place function calls which appear in anonymous contexts
5410 -- need a transient scope to ensure the proper finalization of the
5411 -- intermediate result after its use.
5413 elsif Is_Build_In_Place_Function_Call
(Call_Node
)
5414 and then Nkind
(Parent
(Unqual_Conv
(Call_Node
))) in
5415 N_Attribute_Reference
5417 | N_Indexed_Component
5418 | N_Object_Renaming_Declaration
5419 | N_Procedure_Call_Statement
5420 | N_Selected_Component
5423 (Ekind
(Current_Scope
) /= E_Loop
5424 or else Nkind
(Parent
(Call_Node
)) /= N_Function_Call
5426 Is_Build_In_Place_Function_Call
(Parent
(Call_Node
)))
5428 Establish_Transient_Scope
5429 (Call_Node
, Needs_Secondary_Stack
(Etype
(Subp
)));
5432 end Expand_Call_Helper
;
5434 -------------------------------
5435 -- Expand_Ctrl_Function_Call --
5436 -------------------------------
5438 procedure Expand_Ctrl_Function_Call
(N
: Node_Id
; Use_Sec_Stack
: Boolean)
5440 Par
: constant Node_Id
:= Parent
(N
);
5443 -- Optimization: if the returned value is returned again, then no need
5444 -- to copy/readjust/finalize, we can just pass the value through (see
5445 -- Expand_N_Simple_Return_Statement), and thus no attachment is needed.
5446 -- Note that simple return statements are distributed into conditional
5447 -- expressions but we may be invoked before this distribution is done.
5449 if Nkind
(Par
) = N_Simple_Return_Statement
5450 or else (Nkind
(Par
) = N_If_Expression
5451 and then Nkind
(Parent
(Par
)) = N_Simple_Return_Statement
)
5452 or else (Nkind
(Par
) = N_Case_Expression_Alternative
5454 Nkind
(Parent
(Parent
(Par
))) = N_Simple_Return_Statement
)
5459 -- Another optimization: if the returned value is used to initialize an
5460 -- object, then no need to copy/readjust/finalize, we can initialize it
5461 -- in place. However, if the call returns on the secondary stack, then
5462 -- we need the expansion because we'll be renaming the temporary as the
5463 -- (permanent) object. We also apply it in the case of the expression of
5464 -- a delta aggregate, since it is used only to initialize a temporary.
5466 if Nkind
(Par
) in N_Object_Declaration | N_Delta_Aggregate
5467 and then Expression
(Par
) = N
5468 and then not Use_Sec_Stack
5473 -- Resolution is now finished, make sure we don't start analysis again
5474 -- because of the duplication.
5478 -- Apply the transformation unless it was already applied earlier. This
5479 -- may happen because Remove_Side_Effects can be called during semantic
5480 -- analysis, for example from Build_Actual_Subtype_Of_Component. It is
5481 -- crucial to avoid creating a reference of reference here, because it
5482 -- would not be subsequently recognized by the Is_Finalizable_Transient
5483 -- and Requires_Cleanup_Actions predicates.
5485 if Nkind
(Par
) /= N_Reference
then
5486 Remove_Side_Effects
(N
);
5488 end Expand_Ctrl_Function_Call
;
5490 ----------------------------------------
5491 -- Expand_N_Extended_Return_Statement --
5492 ----------------------------------------
5494 -- If there is a Handled_Statement_Sequence, we rewrite this:
5496 -- return Result : T := <expression> do
5497 -- <handled_seq_of_stms>
5503 -- Result : T := <expression>;
5505 -- <handled_seq_of_stms>
5509 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
5511 -- return Result : T := <expression>;
5515 -- return <expression>;
5517 -- unless it's build-in-place or there's no <expression>, in which case
5521 -- Result : T := <expression>;
5526 -- Note that this case could have been written by the user as an extended
5527 -- return statement, or could have been transformed to this from a simple
5528 -- return statement.
5530 -- That is, we need to have a reified return object if there are statements
5531 -- (which might refer to it) or if we're doing build-in-place (so we can
5532 -- set its address to the final resting place or if there is no expression
5533 -- (in which case default initial values might need to be set)).
5535 procedure Expand_N_Extended_Return_Statement
(N
: Node_Id
) is
5536 Loc
: constant Source_Ptr
:= Sloc
(N
);
5537 Func_Id
: constant Entity_Id
:=
5538 Return_Applies_To
(Return_Statement_Entity
(N
));
5539 Is_BIP_Func
: constant Boolean :=
5540 Is_Build_In_Place_Function
(Func_Id
);
5541 Ret_Obj_Id
: constant Entity_Id
:=
5542 First_Entity
(Return_Statement_Entity
(N
));
5543 Ret_Obj_Decl
: constant Node_Id
:= Parent
(Ret_Obj_Id
);
5544 Ret_Typ
: constant Entity_Id
:= Etype
(Func_Id
);
5546 function Move_Activation_Chain
(Func_Id
: Entity_Id
) return Node_Id
;
5547 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
5549 -- From current activation chain
5550 -- To activation chain passed in by the caller
5551 -- New_Master master passed in by the caller
5553 -- Func_Id is the entity of the function where the extended return
5554 -- statement appears.
5556 ---------------------------
5557 -- Move_Activation_Chain --
5558 ---------------------------
5560 function Move_Activation_Chain
(Func_Id
: Entity_Id
) return Node_Id
is
5563 Make_Procedure_Call_Statement
(Loc
,
5565 New_Occurrence_Of
(RTE
(RE_Move_Activation_Chain
), Loc
),
5567 Parameter_Associations
=> New_List
(
5571 Make_Attribute_Reference
(Loc
,
5572 Prefix
=> Make_Identifier
(Loc
, Name_uChain
),
5573 Attribute_Name
=> Name_Unrestricted_Access
),
5575 -- Destination chain
5578 (Build_In_Place_Formal
(Func_Id
, BIP_Activation_Chain
), Loc
),
5583 (Build_In_Place_Formal
(Func_Id
, BIP_Task_Master
), Loc
)));
5584 end Move_Activation_Chain
;
5591 Stmts
: List_Id
:= No_List
;
5593 Return_Stmt
: Node_Id
:= Empty
;
5594 -- Force initialization to facilitate static analysis
5596 -- Start of processing for Expand_N_Extended_Return_Statement
5599 -- Given that functionality of interface thunks is simple (just displace
5600 -- the pointer to the object) they are always handled by means of
5601 -- simple return statements.
5603 pragma Assert
(not Is_Thunk
(Current_Subprogram
));
5605 if Nkind
(Ret_Obj_Decl
) = N_Object_Declaration
then
5606 Exp
:= Expression
(Ret_Obj_Decl
);
5608 -- Assert that if F says "return R : T := G(...) do..."
5609 -- then F and G are both b-i-p, or neither b-i-p.
5611 if Present
(Exp
) and then Nkind
(Exp
) = N_Function_Call
then
5612 pragma Assert
(Ekind
(Current_Subprogram
) = E_Function
);
5614 (Is_Build_In_Place_Function
(Current_Subprogram
) =
5615 Is_Build_In_Place_Function_Call
(Exp
));
5623 HSS
:= Handled_Statement_Sequence
(N
);
5625 -- If the returned object needs finalization actions, the function must
5626 -- perform the appropriate cleanup should it fail to return. The state
5627 -- of the function itself is tracked through a flag which is coupled
5628 -- with the scope finalizer. There is one flag per each return object
5629 -- in case of multiple extended returns. Note that the flag has already
5630 -- been created if the extended return contains a nested return.
5632 if Needs_Finalization
(Etype
(Ret_Obj_Id
))
5633 and then No
(Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
))
5635 Set_Status_Flag_Or_Transient_Decl
5636 (Ret_Obj_Id
, Build_Flag_For_Function
(Func_Id
));
5639 -- Build a simple_return_statement that returns the return object when
5640 -- there is a statement sequence, or no expression, or the analysis of
5641 -- the return object declaration generated extra actions, or the result
5642 -- will be built in place. Note however that we currently do this for
5643 -- all composite cases, even though they are not built in place.
5647 or else List_Length
(Return_Object_Declarations
(N
)) > 1
5648 or else Is_Composite_Type
(Ret_Typ
)
5653 -- If the extended return has a handled statement sequence, then wrap
5654 -- it in a block and use the block as the first statement.
5658 Make_Block_Statement
(Loc
,
5659 Declarations
=> New_List
,
5660 Handled_Statement_Sequence
=> HSS
));
5663 -- If the result type contains tasks, we call Move_Activation_Chain.
5664 -- Later, the cleanup code will call Complete_Master, which will
5665 -- terminate any unactivated tasks belonging to the return statement
5666 -- master. But Move_Activation_Chain updates their master to be that
5667 -- of the caller, so they will not be terminated unless the return
5668 -- statement completes unsuccessfully due to exception, abort, goto,
5669 -- or exit. As a formality, we test whether the function requires the
5670 -- result to be built in place, though that's necessarily true for
5671 -- the case of result types with task parts.
5673 if Is_BIP_Func
and then Has_Task
(Ret_Typ
) then
5675 -- The return expression is an aggregate for a complex type which
5676 -- contains tasks. This particular case is left unexpanded since
5677 -- the regular expansion would insert all temporaries and
5678 -- initialization code in the wrong block.
5680 if Nkind
(Exp
) = N_Aggregate
then
5681 Expand_N_Aggregate
(Exp
);
5684 -- Do not move the activation chain if the return object does not
5687 if Has_Task
(Etype
(Ret_Obj_Id
)) then
5688 Append_To
(Stmts
, Move_Activation_Chain
(Func_Id
));
5692 -- Update the state of the function right before the object is
5695 if Needs_Finalization
(Etype
(Ret_Obj_Id
)) then
5697 Flag_Id
: constant Entity_Id
:=
5698 Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
);
5701 pragma Assert
(Present
(Flag_Id
));
5707 Make_Assignment_Statement
(Loc
,
5708 Name
=> New_Occurrence_Of
(Flag_Id
, Loc
),
5709 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)));
5713 HSS
:= Make_Handled_Sequence_Of_Statements
(Loc
, Stmts
);
5716 -- Case where we build a return statement block
5718 if Present
(HSS
) then
5720 Make_Block_Statement
(Loc
,
5721 Declarations
=> Return_Object_Declarations
(N
),
5722 Handled_Statement_Sequence
=> HSS
);
5724 -- We set the entity of the new block statement to be that of the
5725 -- return statement. This is necessary so that various fields, such
5726 -- as Finalization_Chain_Entity carry over from the return statement
5727 -- to the block. Note that this block is unusual, in that its entity
5728 -- is an E_Return_Statement rather than an E_Block.
5731 (Result
, New_Occurrence_Of
(Return_Statement_Entity
(N
), Loc
));
5733 -- Build a simple_return_statement that returns the return object
5736 Make_Simple_Return_Statement
(Loc
,
5737 Expression
=> New_Occurrence_Of
(Ret_Obj_Id
, Loc
));
5738 Append_To
(Stmts
, Return_Stmt
);
5740 -- Case where we do not need to build a block. But we're about to drop
5741 -- Return_Object_Declarations on the floor, so assert that it contains
5742 -- only the return object declaration.
5744 else pragma Assert
(List_Length
(Return_Object_Declarations
(N
)) = 1);
5746 -- Build simple_return_statement that returns the expression directly
5748 Return_Stmt
:= Make_Simple_Return_Statement
(Loc
, Expression
=> Exp
);
5749 Result
:= Return_Stmt
;
5752 -- Set the flag to prevent infinite recursion
5754 Set_Comes_From_Extended_Return_Statement
(Return_Stmt
);
5755 Set_Return_Statement
(Ret_Obj_Id
, Return_Stmt
);
5757 Rewrite
(N
, Result
);
5759 -- AI12-043: The checks of 6.5(8.1/3) and 6.5(21/3) are made immediately
5760 -- before an object is returned. A predicate that applies to the return
5761 -- subtype is checked immediately before an object is returned.
5764 end Expand_N_Extended_Return_Statement
;
5766 ----------------------------
5767 -- Expand_N_Function_Call --
5768 ----------------------------
5770 procedure Expand_N_Function_Call
(N
: Node_Id
) is
5773 end Expand_N_Function_Call
;
5775 ---------------------------------------
5776 -- Expand_N_Procedure_Call_Statement --
5777 ---------------------------------------
5779 procedure Expand_N_Procedure_Call_Statement
(N
: Node_Id
) is
5782 end Expand_N_Procedure_Call_Statement
;
5784 ------------------------------------
5785 -- Expand_N_Return_When_Statement --
5786 ------------------------------------
5788 procedure Expand_N_Return_When_Statement
(N
: Node_Id
) is
5789 Loc
: constant Source_Ptr
:= Sloc
(N
);
5792 Make_If_Statement
(Loc
,
5793 Condition
=> Condition
(N
),
5794 Then_Statements
=> New_List
(
5795 Make_Simple_Return_Statement
(Loc
,
5796 Expression
=> Expression
(N
)))));
5799 end Expand_N_Return_When_Statement
;
5801 --------------------------------------
5802 -- Expand_N_Simple_Return_Statement --
5803 --------------------------------------
5805 procedure Expand_N_Simple_Return_Statement
(N
: Node_Id
) is
5807 -- Defend against previous errors (i.e. the return statement calls a
5808 -- function that is not available in configurable runtime).
5810 if Present
(Expression
(N
))
5811 and then Nkind
(Expression
(N
)) = N_Empty
5813 Check_Error_Detected
;
5817 -- Distinguish the function and non-function cases:
5819 case Ekind
(Return_Applies_To
(Return_Statement_Entity
(N
))) is
5821 | E_Generic_Function
5823 Expand_Simple_Function_Return
(N
);
5827 | E_Generic_Procedure
5829 | E_Return_Statement
5831 Expand_Non_Function_Return
(N
);
5834 raise Program_Error
;
5838 when RE_Not_Available
=>
5840 end Expand_N_Simple_Return_Statement
;
5842 ------------------------------
5843 -- Expand_N_Subprogram_Body --
5844 ------------------------------
5846 -- Add dummy push/pop label nodes at start and end to clear any local
5847 -- exception indications if local-exception-to-goto optimization is active.
5849 -- Add return statement if last statement in body is not a return statement
5850 -- (this makes things easier on Gigi which does not want to have to handle
5851 -- a missing return).
5853 -- Add call to Activate_Tasks if body is a task activator
5855 -- Deal with possible detection of infinite recursion
5857 -- Eliminate body completely if convention stubbed
5859 -- Encode entity names within body, since we will not need to reference
5860 -- these entities any longer in the front end.
5862 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5864 -- Reset Pure indication if any parameter has root type System.Address
5865 -- or has any parameters of limited types, where limited means that the
5866 -- run-time view is limited (i.e. the full type is limited).
5870 procedure Expand_N_Subprogram_Body
(N
: Node_Id
) is
5871 Body_Id
: constant Entity_Id
:= Defining_Entity
(N
);
5872 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
5873 Loc
: constant Source_Ptr
:= Sloc
(N
);
5875 procedure Add_Return
(Spec_Id
: Entity_Id
; Stmts
: List_Id
);
5876 -- Append a return statement to the statement sequence Stmts if the last
5877 -- statement is not already a return or a goto statement. Note that the
5878 -- latter test is not critical, it does not matter if we add a few extra
5879 -- returns, since they get eliminated anyway later on. Spec_Id denotes
5880 -- the corresponding spec of the subprogram body.
5886 procedure Add_Return
(Spec_Id
: Entity_Id
; Stmts
: List_Id
) is
5887 Last_Stmt
: Node_Id
;
5892 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5893 -- not relevant in this context since they are not executable.
5895 Last_Stmt
:= Last
(Stmts
);
5896 while Nkind
(Last_Stmt
) in N_Pop_xxx_Label
loop
5900 -- Now insert return unless last statement is a transfer
5902 if not Is_Transfer
(Last_Stmt
) then
5904 -- The source location for the return is the end label of the
5905 -- procedure if present. Otherwise use the sloc of the last
5906 -- statement in the list. If the list comes from a generated
5907 -- exception handler and we are not debugging generated code,
5908 -- all the statements within the handler are made invisible
5911 if Nkind
(Parent
(Stmts
)) = N_Exception_Handler
5912 and then not Comes_From_Source
(Parent
(Stmts
))
5914 Loc
:= Sloc
(Last_Stmt
);
5915 elsif Present
(End_Label
(HSS
)) then
5916 Loc
:= Sloc
(End_Label
(HSS
));
5918 Loc
:= Sloc
(Last_Stmt
);
5921 -- Append return statement, and set analyzed manually. We can't
5922 -- call Analyze on this return since the scope is wrong.
5924 -- Note: it almost works to push the scope and then do the Analyze
5925 -- call, but something goes wrong in some weird cases and it is
5926 -- not worth worrying about ???
5928 Stmt
:= Make_Simple_Return_Statement
(Loc
);
5930 -- The return statement is handled properly, and the call to the
5931 -- postcondition, inserted below, does not require information
5932 -- from the body either. However, that call is analyzed in the
5933 -- enclosing scope, and an elaboration check might improperly be
5934 -- added to it. A guard in Sem_Elab is needed to prevent that
5935 -- spurious check, see Check_Elab_Call.
5937 Append_To
(Stmts
, Stmt
);
5938 Set_Analyzed
(Stmt
);
5940 -- Ada 2022 (AI12-0279): append the call to 'Yield unless this is
5941 -- a generic subprogram (since in such case it will be added to
5942 -- the instantiations).
5944 if Has_Yield_Aspect
(Spec_Id
)
5945 and then Ekind
(Spec_Id
) /= E_Generic_Procedure
5946 and then RTE_Available
(RE_Yield
)
5948 Insert_Action
(Stmt
,
5949 Make_Procedure_Call_Statement
(Loc
,
5950 New_Occurrence_Of
(RTE
(RE_Yield
), Loc
)));
5959 Spec_Id
: Entity_Id
;
5961 -- Start of processing for Expand_N_Subprogram_Body
5964 if Present
(Corresponding_Spec
(N
)) then
5965 Spec_Id
:= Corresponding_Spec
(N
);
5970 -- If this is a Pure function which has any parameters whose root type
5971 -- is System.Address, reset the Pure indication.
5972 -- This check is also performed when the subprogram is frozen, but we
5973 -- repeat it on the body so that the indication is consistent, and so
5974 -- it applies as well to bodies without separate specifications.
5976 if Is_Pure
(Spec_Id
)
5977 and then Is_Subprogram
(Spec_Id
)
5978 and then not Has_Pragma_Pure_Function
(Spec_Id
)
5980 Check_Function_With_Address_Parameter
(Spec_Id
);
5982 if Spec_Id
/= Body_Id
then
5983 Set_Is_Pure
(Body_Id
, Is_Pure
(Spec_Id
));
5987 -- Set L to either the list of declarations if present, or to the list
5988 -- of statements if no declarations are present. This is used to insert
5989 -- new stuff at the start.
5991 if Is_Non_Empty_List
(Declarations
(N
)) then
5992 L
:= Declarations
(N
);
5994 L
:= Statements
(HSS
);
5997 -- If local-exception-to-goto optimization active, insert dummy push
5998 -- statements at start, and dummy pop statements at end, but inhibit
5999 -- this if we have No_Exception_Handlers or expanding a entry barrier
6000 -- function, since they are useless and interfere with analysis (e.g. by
6001 -- CodePeer) and other optimizations. We also don't need these if we're
6002 -- unnesting subprograms because the only purpose of these nodes is to
6003 -- ensure we don't set a label in one subprogram and branch to it in
6006 if (Debug_Flag_Dot_G
6007 or else Restriction_Active
(No_Exception_Propagation
))
6008 and then not Restriction_Active
(No_Exception_Handlers
)
6009 and then not CodePeer_Mode
6010 and then not Is_Entry_Barrier_Function
(N
)
6011 and then not Unnest_Subprogram_Mode
6012 and then Is_Non_Empty_List
(L
)
6015 FS
: constant Node_Id
:= First
(L
);
6016 FL
: constant Source_Ptr
:= Sloc
(FS
);
6021 -- LS points to either last statement, if statements are present
6022 -- or to the last declaration if there are no statements present.
6023 -- It is the node after which the pop's are generated.
6025 if Is_Non_Empty_List
(Statements
(HSS
)) then
6026 LS
:= Last
(Statements
(HSS
));
6033 Insert_List_Before_And_Analyze
(FS
, New_List
(
6034 Make_Push_Constraint_Error_Label
(FL
),
6035 Make_Push_Program_Error_Label
(FL
),
6036 Make_Push_Storage_Error_Label
(FL
)));
6038 Insert_List_After_And_Analyze
(LS
, New_List
(
6039 Make_Pop_Constraint_Error_Label
(LL
),
6040 Make_Pop_Program_Error_Label
(LL
),
6041 Make_Pop_Storage_Error_Label
(LL
)));
6045 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
6047 if Init_Or_Norm_Scalars
and then Is_Subprogram
(Spec_Id
) then
6053 -- Loop through formals
6055 F
:= First_Formal
(Spec_Id
);
6056 while Present
(F
) loop
6057 if Is_Scalar_Type
(Etype
(F
))
6058 and then Ekind
(F
) = E_Out_Parameter
6060 Check_Restriction
(No_Default_Initialization
, F
);
6062 -- Insert the initialization. We turn off validity checks
6063 -- for this assignment, since we do not want any check on
6064 -- the initial value itself (which may well be invalid).
6065 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
6068 Make_Assignment_Statement
(Loc
,
6069 Name
=> New_Occurrence_Of
(F
, Loc
),
6070 Expression
=> Get_Simple_Init_Val
(Etype
(F
), N
));
6071 Set_Suppress_Assignment_Checks
(A
);
6073 Insert_Before_And_Analyze
(First
(L
),
6074 A
, Suppress
=> Validity_Check
);
6082 -- Clear out statement list for stubbed procedure
6084 if Present
(Corresponding_Spec
(N
)) then
6085 Set_Elaboration_Flag
(N
, Spec_Id
);
6087 if Convention
(Spec_Id
) = Convention_Stubbed
6088 or else Is_Eliminated
(Spec_Id
)
6090 Set_Declarations
(N
, Empty_List
);
6091 Set_Handled_Statement_Sequence
(N
,
6092 Make_Handled_Sequence_Of_Statements
(Loc
,
6093 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
6099 -- Create a set of discriminals for the next protected subprogram body
6101 if Is_List_Member
(N
)
6102 and then Present
(Parent
(List_Containing
(N
)))
6103 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
6104 and then Present
(Next_Protected_Operation
(N
))
6106 Set_Discriminals
(Parent
(Base_Type
(Scope
(Spec_Id
))));
6109 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
6110 -- subprograms with no specs are not frozen.
6112 Compute_Returns_By_Ref
(Spec_Id
);
6114 -- For a procedure, we add a return for all possible syntactic ends of
6117 if Ekind
(Spec_Id
) in E_Procedure | E_Generic_Procedure
then
6118 Add_Return
(Spec_Id
, Statements
(HSS
));
6120 if Present
(Exception_Handlers
(HSS
)) then
6121 Except_H
:= First_Non_Pragma
(Exception_Handlers
(HSS
));
6122 while Present
(Except_H
) loop
6123 Add_Return
(Spec_Id
, Statements
(Except_H
));
6124 Next_Non_Pragma
(Except_H
);
6128 -- For a function, we must deal with the case where there is at least
6129 -- one missing return. What we do is to wrap the entire body of the
6130 -- function in a block:
6143 -- raise Program_Error;
6146 -- This approach is necessary because the raise must be signalled to the
6147 -- caller, not handled by any local handler (RM 6.4(11)).
6149 -- Note: we do not need to analyze the constructed sequence here, since
6150 -- it has no handler, and an attempt to analyze the handled statement
6151 -- sequence twice is risky in various ways (e.g. the issue of expanding
6152 -- cleanup actions twice).
6154 elsif Has_Missing_Return
(Spec_Id
) then
6156 Hloc
: constant Source_Ptr
:= Sloc
(HSS
);
6157 Blok
: constant Node_Id
:=
6158 Make_Block_Statement
(Hloc
,
6159 Handled_Statement_Sequence
=> HSS
);
6160 Rais
: constant Node_Id
:=
6161 Make_Raise_Program_Error
(Hloc
,
6162 Reason
=> PE_Missing_Return
);
6165 Set_Handled_Statement_Sequence
(N
,
6166 Make_Handled_Sequence_Of_Statements
(Hloc
,
6167 Statements
=> New_List
(Blok
, Rais
)));
6169 Push_Scope
(Spec_Id
);
6176 -- If subprogram contains a parameterless recursive call, then we may
6177 -- have an infinite recursion, so see if we can generate code to check
6178 -- for this possibility if storage checks are not suppressed.
6180 if Ekind
(Spec_Id
) = E_Procedure
6181 and then Has_Recursive_Call
(Spec_Id
)
6182 and then not Storage_Checks_Suppressed
(Spec_Id
)
6184 Detect_Infinite_Recursion
(N
, Spec_Id
);
6187 -- Set to encode entity names in package body before gigi is called
6189 Qualify_Entity_Names
(N
);
6191 -- If the body belongs to a nonabstract library-level source primitive
6192 -- of a tagged type, install an elaboration check which ensures that a
6193 -- dispatching call targeting the primitive will not execute the body
6194 -- without it being previously elaborated.
6196 Install_Primitive_Elaboration_Check
(N
);
6197 end Expand_N_Subprogram_Body
;
6199 -----------------------------------
6200 -- Expand_N_Subprogram_Body_Stub --
6201 -----------------------------------
6203 procedure Expand_N_Subprogram_Body_Stub
(N
: Node_Id
) is
6207 if Present
(Corresponding_Body
(N
)) then
6208 Bod
:= Unit_Declaration_Node
(Corresponding_Body
(N
));
6210 -- The body may have been expanded already when it is analyzed
6211 -- through the subunit node. Do no expand again: it interferes
6212 -- with the construction of unnesting tables when generating C.
6214 if not Analyzed
(Bod
) then
6215 Expand_N_Subprogram_Body
(Bod
);
6218 -- Add full qualification to entities that may be created late
6219 -- during unnesting.
6221 Qualify_Entity_Names
(N
);
6223 end Expand_N_Subprogram_Body_Stub
;
6225 -------------------------------------
6226 -- Expand_N_Subprogram_Declaration --
6227 -------------------------------------
6229 -- If the declaration appears within a protected body, it is a private
6230 -- operation of the protected type. We must create the corresponding
6231 -- protected subprogram an associated formals. For a normal protected
6232 -- operation, this is done when expanding the protected type declaration.
6234 -- If the declaration is for a null procedure, emit null body
6236 procedure Expand_N_Subprogram_Declaration
(N
: Node_Id
) is
6237 Loc
: constant Source_Ptr
:= Sloc
(N
);
6238 Subp
: constant Entity_Id
:= Defining_Entity
(N
);
6242 Scop
: constant Entity_Id
:= Scope
(Subp
);
6244 Prot_Decl
: Node_Id
;
6245 Prot_Id
: Entity_Id
;
6249 -- Deal with case of protected subprogram. Do not generate protected
6250 -- operation if operation is flagged as eliminated.
6252 if Is_List_Member
(N
)
6253 and then Present
(Parent
(List_Containing
(N
)))
6254 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
6255 and then Is_Protected_Type
(Scop
)
6257 if No
(Protected_Body_Subprogram
(Subp
))
6258 and then not Is_Eliminated
(Subp
)
6261 Make_Subprogram_Declaration
(Loc
,
6263 Build_Protected_Sub_Specification
6264 (N
, Scop
, Unprotected_Mode
));
6266 -- The protected subprogram is declared outside of the protected
6267 -- body. Given that the body has frozen all entities so far, we
6268 -- analyze the subprogram and perform freezing actions explicitly.
6269 -- including the generation of an explicit freeze node, to ensure
6270 -- that gigi has the proper order of elaboration.
6271 -- If the body is a subunit, the insertion point is before the
6272 -- stub in the parent.
6274 Prot_Bod
:= Parent
(List_Containing
(N
));
6276 if Nkind
(Parent
(Prot_Bod
)) = N_Subunit
then
6277 Prot_Bod
:= Corresponding_Stub
(Parent
(Prot_Bod
));
6280 Insert_Before
(Prot_Bod
, Prot_Decl
);
6281 Prot_Id
:= Defining_Unit_Name
(Specification
(Prot_Decl
));
6282 Set_Has_Delayed_Freeze
(Prot_Id
);
6284 Push_Scope
(Scope
(Scop
));
6285 Analyze
(Prot_Decl
);
6286 Freeze_Before
(N
, Prot_Id
);
6287 Set_Protected_Body_Subprogram
(Subp
, Prot_Id
);
6291 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
6292 -- cases this is superfluous because calls to it will be automatically
6293 -- inlined, but we definitely need the body if preconditions for the
6294 -- procedure are present, or if performing coverage analysis.
6296 elsif Nkind
(Specification
(N
)) = N_Procedure_Specification
6297 and then Null_Present
(Specification
(N
))
6300 Bod
: constant Node_Id
:= Body_To_Inline
(N
);
6303 Set_Has_Completion
(Subp
, False);
6304 Append_Freeze_Action
(Subp
, Bod
);
6306 -- The body now contains raise statements, so calls to it will
6309 Set_Is_Inlined
(Subp
, False);
6313 -- When generating C code, transform a function that returns a
6314 -- constrained array type into a procedure with an out parameter
6315 -- that carries the return value.
6317 -- We skip this transformation for unchecked conversions, since they
6318 -- are not needed by the C generator (and this also produces cleaner
6321 Typ
:= Get_Fullest_View
(Etype
(Subp
));
6323 if Transform_Function_Array
6324 and then Nkind
(Specification
(N
)) = N_Function_Specification
6325 and then Is_Array_Type
(Typ
)
6326 and then Is_Constrained
(Typ
)
6327 and then not Is_Unchecked_Conversion_Instance
(Subp
)
6329 Build_Procedure_Form
(N
);
6331 end Expand_N_Subprogram_Declaration
;
6333 --------------------------------
6334 -- Expand_Non_Function_Return --
6335 --------------------------------
6337 procedure Expand_Non_Function_Return
(N
: Node_Id
) is
6338 pragma Assert
(No
(Expression
(N
)));
6340 Loc
: constant Source_Ptr
:= Sloc
(N
);
6341 Scope_Id
: Entity_Id
:= Return_Applies_To
(Return_Statement_Entity
(N
));
6342 Kind
: constant Entity_Kind
:= Ekind
(Scope_Id
);
6345 Goto_Stat
: Node_Id
;
6349 -- Ada 2022 (AI12-0279)
6351 if Has_Yield_Aspect
(Scope_Id
)
6352 and then RTE_Available
(RE_Yield
)
6355 Make_Procedure_Call_Statement
(Loc
,
6356 New_Occurrence_Of
(RTE
(RE_Yield
), Loc
)));
6359 -- If it is a return from a procedure do no extra steps
6361 if Kind
= E_Procedure
or else Kind
= E_Generic_Procedure
then
6364 -- If it is a nested return within an extended one, replace it with a
6365 -- return of the previously declared return object.
6367 elsif Kind
= E_Return_Statement
then
6369 Ret_Obj_Id
: constant Entity_Id
:= First_Entity
(Scope_Id
);
6371 Flag_Id
: Entity_Id
;
6374 -- Apply the same processing as Expand_N_Extended_Return_Statement
6375 -- if the returned object needs finalization actions. Note that we
6376 -- are invoked before Expand_N_Extended_Return_Statement but there
6377 -- may be multiple nested returns within the extended one.
6379 if Needs_Finalization
(Etype
(Ret_Obj_Id
)) then
6380 if Present
(Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
)) then
6381 Flag_Id
:= Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
);
6384 Build_Flag_For_Function
(Return_Applies_To
(Scope_Id
));
6385 Set_Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
, Flag_Id
);
6392 Make_Assignment_Statement
(Loc
,
6394 New_Occurrence_Of
(Flag_Id
, Loc
),
6395 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)));
6399 Make_Simple_Return_Statement
(Loc
,
6400 Expression
=> New_Occurrence_Of
(Ret_Obj_Id
, Loc
)));
6401 Set_Comes_From_Extended_Return_Statement
(N
);
6402 Set_Return_Statement_Entity
(N
, Scope_Id
);
6403 Expand_Simple_Function_Return
(N
);
6408 pragma Assert
(Is_Entry
(Scope_Id
));
6410 -- Look at the enclosing block to see whether the return is from an
6411 -- accept statement or an entry body.
6413 for J
in reverse 0 .. Scope_Stack
.Last
loop
6414 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
6415 exit when Is_Concurrent_Type
(Scope_Id
);
6418 -- If it is a return from accept statement it is expanded as call to
6419 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6421 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6422 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6424 if Is_Task_Type
(Scope_Id
) then
6427 Make_Procedure_Call_Statement
(Loc
,
6428 Name
=> New_Occurrence_Of
(RTE
(RE_Complete_Rendezvous
), Loc
));
6429 Insert_Before
(N
, Call
);
6430 -- why not insert actions here???
6433 Acc_Stat
:= Parent
(N
);
6434 while Nkind
(Acc_Stat
) /= N_Accept_Statement
loop
6435 Acc_Stat
:= Parent
(Acc_Stat
);
6438 Lab_Node
:= Last
(Statements
6439 (Handled_Statement_Sequence
(Acc_Stat
)));
6441 Goto_Stat
:= Make_Goto_Statement
(Loc
,
6442 Name
=> New_Occurrence_Of
6443 (Entity
(Identifier
(Lab_Node
)), Loc
));
6445 Set_Analyzed
(Goto_Stat
);
6447 Rewrite
(N
, Goto_Stat
);
6450 -- If it is a return from an entry body, put a Complete_Entry_Body call
6451 -- in front of the return.
6453 elsif Is_Protected_Type
(Scope_Id
) then
6455 Make_Procedure_Call_Statement
(Loc
,
6457 New_Occurrence_Of
(RTE
(RE_Complete_Entry_Body
), Loc
),
6458 Parameter_Associations
=> New_List
(
6459 Make_Attribute_Reference
(Loc
,
6462 (Find_Protection_Object
(Current_Scope
), Loc
),
6463 Attribute_Name
=> Name_Unchecked_Access
)));
6465 Insert_Before
(N
, Call
);
6468 end Expand_Non_Function_Return
;
6470 ---------------------------------------
6471 -- Expand_Protected_Object_Reference --
6472 ---------------------------------------
6474 function Expand_Protected_Object_Reference
6476 Scop
: Entity_Id
) return Node_Id
6478 Loc
: constant Source_Ptr
:= Sloc
(N
);
6485 Rec
:= Make_Identifier
(Loc
, Name_uObject
);
6486 Set_Etype
(Rec
, Corresponding_Record_Type
(Scop
));
6488 -- Find enclosing protected operation, and retrieve its first parameter,
6489 -- which denotes the enclosing protected object. If the enclosing
6490 -- operation is an entry, we are immediately within the protected body,
6491 -- and we can retrieve the object from the service entries procedure. A
6492 -- barrier function has the same signature as an entry. A barrier
6493 -- function is compiled within the protected object, but unlike
6494 -- protected operations its never needs locks, so that its protected
6495 -- body subprogram points to itself.
6497 Proc
:= Current_Scope
;
6498 while Present
(Proc
) and then Scope
(Proc
) /= Scop
loop
6499 Proc
:= Scope
(Proc
);
6500 if Is_Subprogram
(Proc
)
6501 and then Present
(Protected_Subprogram
(Proc
))
6503 Proc
:= Protected_Subprogram
(Proc
);
6507 Corr
:= Protected_Body_Subprogram
(Proc
);
6511 -- Previous error left expansion incomplete.
6512 -- Nothing to do on this call.
6519 (First
(Parameter_Specifications
(Parent
(Corr
))));
6521 if Is_Subprogram
(Proc
) and then Proc
/= Corr
then
6523 -- Protected function or procedure
6525 Set_Entity
(Rec
, Param
);
6527 -- Rec is a reference to an entity which will not be in scope when
6528 -- the call is reanalyzed, and needs no further analysis.
6533 -- Entry or barrier function for entry body. The first parameter of
6534 -- the entry body procedure is pointer to the object. We create a
6535 -- local variable of the proper type, duplicating what is done to
6536 -- define _object later on.
6540 Obj_Ptr
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
6544 Make_Full_Type_Declaration
(Loc
,
6545 Defining_Identifier
=> Obj_Ptr
,
6547 Make_Access_To_Object_Definition
(Loc
,
6548 Subtype_Indication
=>
6550 (Corresponding_Record_Type
(Scop
), Loc
))));
6552 Insert_Actions
(N
, Decls
);
6553 Freeze_Before
(N
, Obj_Ptr
);
6556 Make_Explicit_Dereference
(Loc
,
6558 Unchecked_Convert_To
(Obj_Ptr
,
6559 New_Occurrence_Of
(Param
, Loc
)));
6561 -- Analyze new actual. Other actuals in calls are already analyzed
6562 -- and the list of actuals is not reanalyzed after rewriting.
6564 Set_Parent
(Rec
, N
);
6570 end Expand_Protected_Object_Reference
;
6572 --------------------------------------
6573 -- Expand_Protected_Subprogram_Call --
6574 --------------------------------------
6576 procedure Expand_Protected_Subprogram_Call
6583 procedure Expand_Internal_Init_Call
;
6584 -- A call to an operation of the type may occur in the initialization
6585 -- of a private component. In that case the prefix of the call is an
6586 -- entity name and the call is treated as internal even though it
6587 -- appears in code outside of the protected type.
6589 procedure Freeze_Called_Function
;
6590 -- If it is a function call it can appear in elaboration code and
6591 -- the called entity must be frozen before the call. This must be
6592 -- done before the call is expanded, as the expansion may rewrite it
6593 -- to something other than a call (e.g. a temporary initialized in a
6594 -- transient block).
6596 -------------------------------
6597 -- Expand_Internal_Init_Call --
6598 -------------------------------
6600 procedure Expand_Internal_Init_Call
is
6602 -- If the context is a protected object (rather than a protected
6603 -- type) the call itself is bound to raise program_error because
6604 -- the protected body will not have been elaborated yet. This is
6605 -- diagnosed subsequently in Sem_Elab.
6607 Freeze_Called_Function
;
6609 -- The target of the internal call is the first formal of the
6610 -- enclosing initialization procedure.
6612 Rec
:= New_Occurrence_Of
(First_Formal
(Current_Scope
), Sloc
(N
));
6613 Build_Protected_Subprogram_Call
(N
,
6618 Resolve
(N
, Etype
(Subp
));
6619 end Expand_Internal_Init_Call
;
6621 ----------------------------
6622 -- Freeze_Called_Function --
6623 ----------------------------
6625 procedure Freeze_Called_Function
is
6627 if Ekind
(Subp
) = E_Function
then
6628 Freeze_Expression
(Name
(N
));
6630 end Freeze_Called_Function
;
6632 -- Start of processing for Expand_Protected_Subprogram_Call
6635 -- If the protected object is not an enclosing scope, this is an inter-
6636 -- object function call. Inter-object procedure calls are expanded by
6637 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
6638 -- subprogram being called is in the protected body being compiled, and
6639 -- if the protected object in the call is statically the enclosing type.
6640 -- The object may be a component of some other data structure, in which
6641 -- case this must be handled as an inter-object call.
6643 if not Scope_Within_Or_Same
(Inner
=> Current_Scope
, Outer
=> Scop
)
6644 or else Is_Entry_Wrapper
(Current_Scope
)
6645 or else not Is_Entity_Name
(Name
(N
))
6647 if Nkind
(Name
(N
)) = N_Selected_Component
then
6648 Rec
:= Prefix
(Name
(N
));
6650 elsif Nkind
(Name
(N
)) = N_Indexed_Component
then
6651 Rec
:= Prefix
(Prefix
(Name
(N
)));
6653 -- If this is a call within an entry wrapper, it appears within a
6654 -- precondition that calls another primitive of the synchronized
6655 -- type. The target object of the call is the first actual on the
6656 -- wrapper. Note that this is an external call, because the wrapper
6657 -- is called outside of the synchronized object. This means that
6658 -- an entry call to an entry with preconditions involves two
6659 -- synchronized operations.
6661 elsif Ekind
(Current_Scope
) = E_Procedure
6662 and then Is_Entry_Wrapper
(Current_Scope
)
6664 Rec
:= New_Occurrence_Of
(First_Entity
(Current_Scope
), Sloc
(N
));
6666 -- A default parameter of a protected operation may be a call to
6667 -- a protected function of the type. This appears as an internal
6668 -- call in the profile of the operation, but if the context is an
6669 -- external call we must convert the call into an external one,
6670 -- using the protected object that is the target, so that:
6673 -- is transformed into
6676 elsif Nkind
(Parent
(N
)) = N_Procedure_Call_Statement
6677 and then Nkind
(Name
(Parent
(N
))) = N_Selected_Component
6678 and then Is_Protected_Type
(Etype
(Prefix
(Name
(Parent
(N
)))))
6679 and then Is_Entity_Name
(Name
(N
))
6680 and then Scope
(Entity
(Name
(N
))) =
6681 Etype
(Prefix
(Name
(Parent
(N
))))
6684 Make_Selected_Component
(Sloc
(N
),
6685 Prefix
=> New_Copy_Tree
(Prefix
(Name
(Parent
(N
)))),
6686 Selector_Name
=> Relocate_Node
(Name
(N
))));
6688 Analyze_And_Resolve
(N
);
6692 -- If the context is the initialization procedure for a protected
6693 -- type, the call is legal because the called entity must be a
6694 -- function of that enclosing type, and this is treated as an
6698 (Is_Entity_Name
(Name
(N
)) and then Inside_Init_Proc
);
6700 Expand_Internal_Init_Call
;
6704 Freeze_Called_Function
;
6705 Build_Protected_Subprogram_Call
(N
,
6706 Name
=> New_Occurrence_Of
(Subp
, Sloc
(N
)),
6707 Rec
=> Convert_Concurrent
(Rec
, Etype
(Rec
)),
6711 Rec
:= Expand_Protected_Object_Reference
(N
, Scop
);
6717 Freeze_Called_Function
;
6718 Build_Protected_Subprogram_Call
(N
,
6724 -- Analyze and resolve the new call. The actuals have already been
6725 -- resolved, but expansion of a function call will add extra actuals
6726 -- if needed. Analysis of a procedure call already includes resolution.
6730 if Ekind
(Subp
) = E_Function
then
6731 Resolve
(N
, Etype
(Subp
));
6733 end Expand_Protected_Subprogram_Call
;
6735 -----------------------------------
6736 -- Expand_Simple_Function_Return --
6737 -----------------------------------
6739 -- The "simple" comes from the syntax rule simple_return_statement. The
6740 -- semantics are not at all simple.
6742 procedure Expand_Simple_Function_Return
(N
: Node_Id
) is
6743 Loc
: constant Source_Ptr
:= Sloc
(N
);
6745 Scope_Id
: constant Entity_Id
:=
6746 Return_Applies_To
(Return_Statement_Entity
(N
));
6747 -- The function we are returning from
6749 R_Type
: constant Entity_Id
:= Etype
(Scope_Id
);
6750 -- The result type of the function
6752 Utyp
: constant Entity_Id
:= Underlying_Type
(R_Type
);
6753 -- The underlying result type of the function
6755 Exp
: Node_Id
:= Expression
(N
);
6756 pragma Assert
(Present
(Exp
));
6758 Exp_Is_Function_Call
: constant Boolean :=
6759 Nkind
(Exp
) = N_Function_Call
6761 (Is_Captured_Function_Call
(Exp
)
6762 and then Is_Related_To_Func_Return
(Entity
(Prefix
(Exp
))));
6763 -- If the expression is a captured function call, then we need to make
6764 -- sure that the object doing the capture is properly recognized by the
6765 -- Is_Related_To_Func_Return predicate; otherwise, if it is of a type
6766 -- that needs finalization, Requires_Cleanup_Actions would return true
6767 -- because of this and Build_Finalizer would finalize it prematurely.
6769 Exp_Typ
: constant Entity_Id
:= Etype
(Exp
);
6770 -- The type of the expression (not necessarily the same as R_Type)
6772 Subtype_Ind
: Node_Id
;
6773 -- If the result type of the function is class-wide and the expression
6774 -- has a specific type, then we use the expression's type as the type of
6775 -- the return object. In cases where the expression is an aggregate that
6776 -- is built in place, this avoids the need for an expensive conversion
6777 -- of the return object to the specific type on assignments to the
6778 -- individual components.
6780 -- Start of processing for Expand_Simple_Function_Return
6783 if Is_Class_Wide_Type
(R_Type
)
6784 and then not Is_Class_Wide_Type
(Exp_Typ
)
6785 and then Nkind
(Exp
) /= N_Type_Conversion
6787 Subtype_Ind
:= New_Occurrence_Of
(Exp_Typ
, Loc
);
6789 Subtype_Ind
:= New_Occurrence_Of
(R_Type
, Loc
);
6791 -- If the result type is class-wide and the expression is a view
6792 -- conversion, the conversion plays no role in the expansion because
6793 -- it does not modify the tag of the object. Remove the conversion
6794 -- altogether to prevent tag overwriting.
6796 if Is_Class_Wide_Type
(R_Type
)
6797 and then not Is_Class_Wide_Type
(Exp_Typ
)
6798 and then Nkind
(Exp
) = N_Type_Conversion
6800 Exp
:= Expression
(Exp
);
6804 -- For the case of a simple return that does not come from an
6805 -- extended return, in the case of build-in-place, we rewrite
6806 -- "return <expression>;" to be:
6808 -- return _anon_ : <return_subtype> := <expression>
6810 -- The expansion produced by Expand_N_Extended_Return_Statement will
6811 -- contain simple return statements (for example, a block containing
6812 -- simple return of the return object), which brings us back here with
6813 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
6814 -- checking for a simple return that does not come from an extended
6815 -- return is to avoid this infinite recursion.
6817 -- The reason for this design is that for Ada 2005 limited returns, we
6818 -- need to reify the return object, so we can build it "in place", and
6819 -- we need a block statement to hang finalization and tasking stuff.
6822 (Comes_From_Extended_Return_Statement
(N
)
6823 or else not Is_Build_In_Place_Function_Call
(Exp
)
6824 or else Has_BIP_Formals
(Scope_Id
));
6826 if not Comes_From_Extended_Return_Statement
(N
)
6827 and then Is_Build_In_Place_Function
(Scope_Id
)
6829 -- The functionality of interface thunks is simple and it is always
6830 -- handled by means of simple return statements. This leaves their
6831 -- expansion simple and clean.
6833 and then not Is_Thunk
(Scope_Id
)
6836 Return_Object_Entity
: constant Entity_Id
:=
6837 Make_Temporary
(Loc
, 'R', Exp
);
6839 Obj_Decl
: constant Node_Id
:=
6840 Make_Object_Declaration
(Loc
,
6841 Defining_Identifier
=> Return_Object_Entity
,
6842 Object_Definition
=> Subtype_Ind
,
6845 Ext
: constant Node_Id
:=
6846 Make_Extended_Return_Statement
(Loc
,
6847 Return_Object_Declarations
=> New_List
(Obj_Decl
));
6848 -- Do not perform this high-level optimization if the result type
6849 -- is an interface because the "this" pointer must be displaced.
6858 -- Assert that if F says "return G(...);"
6859 -- then F and G are both b-i-p, or neither b-i-p.
6861 if Nkind
(Exp
) = N_Function_Call
then
6862 pragma Assert
(Ekind
(Scope_Id
) = E_Function
);
6864 (Is_Build_In_Place_Function
(Scope_Id
) =
6865 Is_Build_In_Place_Function_Call
(Exp
));
6869 -- Here we have a simple return statement that is part of the expansion
6870 -- of an extended return statement (either written by the user, or
6871 -- generated by the above code).
6873 -- Always normalize C/Fortran boolean result. This is not always needed,
6874 -- but it seems a good idea to minimize the passing around of non-
6875 -- normalized values, and in any case this handles the processing of
6876 -- barrier functions for protected types, which turn the condition into
6877 -- a return statement.
6879 if Is_Boolean_Type
(Exp_Typ
) and then Nonzero_Is_True
(Exp_Typ
) then
6880 Adjust_Condition
(Exp
);
6881 Adjust_Result_Type
(Exp
, Exp_Typ
);
6883 -- The adjustment of the expression may have rewritten the return
6884 -- statement itself, e.g. when it is turned into an if expression.
6886 if Nkind
(N
) /= N_Simple_Return_Statement
then
6891 -- Do validity check if enabled for returns
6893 if Validity_Checks_On
and then Validity_Check_Returns
then
6897 -- Check the result expression of a scalar function against the subtype
6898 -- of the function by inserting a conversion. This conversion must
6899 -- eventually be performed for other classes of types, but for now it's
6900 -- only done for scalars ???
6902 if Is_Scalar_Type
(Exp_Typ
) and then Exp_Typ
/= R_Type
then
6903 Rewrite
(Exp
, Convert_To
(R_Type
, Exp
));
6905 -- The expression is resolved to ensure that the conversion gets
6906 -- expanded to generate a possible constraint check.
6908 Analyze_And_Resolve
(Exp
, R_Type
);
6911 -- Deal with returning variable length objects and controlled types
6913 -- Nothing to do if we are returning by reference
6915 if Is_Build_In_Place_Function
(Scope_Id
) then
6916 -- Prevent the reclamation of the secondary stack by all enclosing
6917 -- blocks and loops as well as the related function; otherwise the
6918 -- result would be reclaimed too early.
6920 if Needs_BIP_Alloc_Form
(Scope_Id
) then
6921 Set_Enclosing_Sec_Stack_Return
(N
);
6924 elsif Is_Inherently_Limited_Type
(R_Type
) then
6927 -- No copy needed for thunks returning interface type objects since
6928 -- the object is returned by reference and the maximum functionality
6929 -- required is just to displace the pointer.
6931 elsif Is_Thunk
(Scope_Id
) and then Is_Interface
(Exp_Typ
) then
6934 -- If the call is within a thunk and the type is a limited view, the
6935 -- back end will eventually see the non-limited view of the type.
6937 elsif Is_Thunk
(Scope_Id
) and then Is_Incomplete_Type
(Exp_Typ
) then
6940 -- A return statement from an ignored Ghost function does not use the
6941 -- secondary stack (or any other one).
6943 elsif (not Needs_Secondary_Stack
(R_Type
)
6944 and then not Is_Secondary_Stack_Thunk
(Scope_Id
))
6945 or else Is_Ignored_Ghost_Entity
(Scope_Id
)
6947 -- Mutable records with variable-length components are not returned
6948 -- on the sec-stack, so we need to make sure that the back end will
6949 -- only copy back the size of the actual value, and not the maximum
6950 -- size. We create an actual subtype for this purpose. However we
6951 -- need not do it if the expression is a function call since this
6952 -- will be done in the called function and doing it here too would
6953 -- cause a temporary with maximum size to be created. Likewise for
6954 -- a special return object, since there is no copy in this case.
6957 Ubt
: constant Entity_Id
:= Underlying_Type
(Base_Type
(Exp_Typ
));
6962 if not Exp_Is_Function_Call
6963 and then not (Is_Entity_Name
(Exp
)
6964 and then Is_Special_Return_Object
(Entity
(Exp
)))
6965 and then Has_Defaulted_Discriminants
(Ubt
)
6966 and then not Is_Constrained
(Ubt
)
6967 and then not Has_Unchecked_Union
(Ubt
)
6969 Decl
:= Build_Actual_Subtype
(Ubt
, Exp
);
6970 Ent
:= Defining_Identifier
(Decl
);
6971 Insert_Action
(Exp
, Decl
);
6972 Rewrite
(Exp
, Unchecked_Convert_To
(Ent
, Exp
));
6973 Analyze_And_Resolve
(Exp
);
6977 -- For types which need finalization, do the allocation on the return
6978 -- stack manually in order to call Adjust at the right time:
6980 -- type Ann is access R_Type;
6981 -- for Ann'Storage_pool use rs_pool;
6982 -- Rnn : constant Ann := new Exp_Typ'(Exp);
6985 -- but optimize the case where the result is a function call that
6986 -- also needs finalization. In this case the result can directly be
6987 -- allocated on the return stack of the caller and no further
6988 -- processing is required. Likewise if this is a return object.
6990 if Comes_From_Extended_Return_Statement
(N
) then
6993 elsif Present
(Utyp
)
6994 and then Needs_Finalization
(Utyp
)
6995 and then not (Exp_Is_Function_Call
6996 and then Needs_Finalization
(Exp_Typ
))
6999 Acc_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
7001 Alloc_Node
: Node_Id
;
7005 Mutate_Ekind
(Acc_Typ
, E_Access_Type
);
7007 Set_Associated_Storage_Pool
(Acc_Typ
, RTE
(RE_RS_Pool
));
7009 -- This is an allocator for the return stack, and it's fine
7010 -- to have Comes_From_Source set False on it, as gigi knows not
7011 -- to flag it as a violation of No_Implicit_Heap_Allocations.
7014 Make_Allocator
(Loc
,
7016 Make_Qualified_Expression
(Loc
,
7017 Subtype_Mark
=> New_Occurrence_Of
(Exp_Typ
, Loc
),
7018 Expression
=> Relocate_Node
(Exp
)));
7020 -- We do not want discriminant checks on the declaration,
7021 -- given that it gets its value from the allocator.
7023 Set_No_Initialization
(Alloc_Node
);
7025 Temp
:= Make_Temporary
(Loc
, 'R', Alloc_Node
);
7027 Insert_Actions
(Exp
, New_List
(
7028 Make_Full_Type_Declaration
(Loc
,
7029 Defining_Identifier
=> Acc_Typ
,
7031 Make_Access_To_Object_Definition
(Loc
,
7032 Subtype_Indication
=> Subtype_Ind
)),
7034 Make_Object_Declaration
(Loc
,
7035 Defining_Identifier
=> Temp
,
7036 Constant_Present
=> True,
7037 Object_Definition
=> New_Occurrence_Of
(Acc_Typ
, Loc
),
7038 Expression
=> Alloc_Node
)));
7041 Make_Explicit_Dereference
(Loc
,
7042 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
7044 Analyze_And_Resolve
(Exp
, R_Type
);
7048 -- Here if secondary stack is used
7051 -- Prevent the reclamation of the secondary stack by all enclosing
7052 -- blocks and loops as well as the related function; otherwise the
7053 -- result would be reclaimed too early.
7055 Set_Enclosing_Sec_Stack_Return
(N
);
7057 -- Nothing else to do for a return object
7059 if Comes_From_Extended_Return_Statement
(N
) then
7062 -- Optimize the case where the result is a function call that also
7063 -- returns on the secondary stack; in this case the result is already
7064 -- on the secondary stack and no further processing is required.
7066 elsif Exp_Is_Function_Call
7067 and then Needs_Secondary_Stack
(Exp_Typ
)
7069 -- Remove side effects from the expression now so that other parts
7070 -- of the expander do not have to reanalyze this node without this
7073 Rewrite
(Exp
, Duplicate_Subexpr_No_Checks
(Exp
));
7075 -- Ada 2005 (AI-251): If the type of the returned object is
7076 -- an interface then add an implicit type conversion to force
7077 -- displacement of the "this" pointer.
7079 if Is_Interface
(R_Type
) then
7080 Rewrite
(Exp
, Convert_To
(R_Type
, Relocate_Node
(Exp
)));
7083 Analyze_And_Resolve
(Exp
, R_Type
);
7085 -- For types which both need finalization and are returned on the
7086 -- secondary stack, do the allocation on secondary stack manually
7087 -- in order to call Adjust at the right time:
7089 -- type Ann is access R_Type;
7090 -- for Ann'Storage_pool use ss_pool;
7091 -- Rnn : constant Ann := new Exp_Typ'(Exp);
7094 -- And we do the same for class-wide types that are not potentially
7095 -- controlled (by the virtue of restriction No_Finalization) because
7096 -- gigi is not able to properly allocate class-wide types.
7098 -- But optimize the case where the result is a function call that
7099 -- also needs finalization; in this case the result can directly be
7100 -- allocated on the secondary stack and no further processing is
7101 -- required, unless the returned object is an interface.
7103 elsif CW_Or_Needs_Finalization
(Utyp
)
7104 and then (Is_Interface
(R_Type
)
7105 or else not (Exp_Is_Function_Call
7106 and then Needs_Finalization
(Exp_Typ
)))
7109 Acc_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
7111 Alloc_Node
: Node_Id
;
7115 Mutate_Ekind
(Acc_Typ
, E_Access_Type
);
7116 Set_Associated_Storage_Pool
(Acc_Typ
, RTE
(RE_SS_Pool
));
7118 -- This is an allocator for the secondary stack, and it's fine
7119 -- to have Comes_From_Source set False on it, as gigi knows not
7120 -- to flag it as a violation of No_Implicit_Heap_Allocations.
7123 Make_Allocator
(Loc
,
7125 Make_Qualified_Expression
(Loc
,
7126 Subtype_Mark
=> New_Occurrence_Of
(Etype
(Exp
), Loc
),
7127 Expression
=> Relocate_Node
(Exp
)));
7129 -- We do not want discriminant checks on the declaration,
7130 -- given that it gets its value from the allocator.
7132 Set_No_Initialization
(Alloc_Node
);
7134 Temp
:= Make_Temporary
(Loc
, 'R', Alloc_Node
);
7136 Insert_Actions
(Exp
, New_List
(
7137 Make_Full_Type_Declaration
(Loc
,
7138 Defining_Identifier
=> Acc_Typ
,
7140 Make_Access_To_Object_Definition
(Loc
,
7141 Subtype_Indication
=> Subtype_Ind
)),
7143 Make_Object_Declaration
(Loc
,
7144 Defining_Identifier
=> Temp
,
7145 Constant_Present
=> True,
7146 Object_Definition
=> New_Occurrence_Of
(Acc_Typ
, Loc
),
7147 Expression
=> Alloc_Node
)));
7150 Make_Explicit_Dereference
(Loc
,
7151 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
7153 -- Ada 2005 (AI-251): If the type of the returned object is
7154 -- an interface then add an implicit type conversion to force
7155 -- displacement of the "this" pointer.
7157 if Is_Interface
(R_Type
) then
7158 Rewrite
(Exp
, Convert_To
(R_Type
, Relocate_Node
(Exp
)));
7161 Analyze_And_Resolve
(Exp
, R_Type
);
7164 -- Otherwise use the gigi mechanism to allocate result on the
7168 Check_Restriction
(No_Secondary_Stack
, N
);
7169 Set_Storage_Pool
(N
, RTE
(RE_SS_Pool
));
7170 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
7174 -- Implement the rules of 6.5(8-10), which require a tag check in
7175 -- the case of a limited tagged return type, and tag reassignment for
7176 -- nonlimited tagged results. These actions are needed when the return
7177 -- type is a specific tagged type and the result expression is a
7178 -- conversion or a formal parameter, because in that case the tag of
7179 -- the expression might differ from the tag of the specific result type.
7181 -- We must also verify an underlying type exists for the return type in
7182 -- case it is incomplete - in which case is not necessary to generate a
7183 -- check anyway since an incomplete limited tagged return type would
7184 -- qualify as a premature usage.
7187 and then Is_Tagged_Type
(Utyp
)
7188 and then not Is_Class_Wide_Type
(Utyp
)
7189 and then (Nkind
(Exp
) in
7190 N_Type_Conversion | N_Unchecked_Type_Conversion
7191 or else (Nkind
(Exp
) = N_Explicit_Dereference
7192 and then Nkind
(Prefix
(Exp
)) in
7194 N_Unchecked_Type_Conversion
)
7195 or else (Is_Entity_Name
(Exp
)
7196 and then Is_Formal
(Entity
(Exp
))))
7198 -- When the return type is limited, perform a check that the tag of
7199 -- the result is the same as the tag of the return type.
7201 if Is_Limited_Type
(R_Type
) then
7203 Make_Raise_Constraint_Error
(Loc
,
7207 Make_Selected_Component
(Loc
,
7208 Prefix
=> Duplicate_Subexpr
(Exp
),
7209 Selector_Name
=> Make_Identifier
(Loc
, Name_uTag
)),
7211 Make_Attribute_Reference
(Loc
,
7213 New_Occurrence_Of
(Base_Type
(Utyp
), Loc
),
7214 Attribute_Name
=> Name_Tag
)),
7215 Reason
=> CE_Tag_Check_Failed
));
7217 -- If the result type is a specific nonlimited tagged type, then we
7218 -- have to ensure that the tag of the result is that of the result
7219 -- type. This is handled by making a copy of the expression in
7220 -- the case where it might have a different tag, namely when the
7221 -- expression is a conversion or a formal parameter. We create a new
7222 -- object of the result type and initialize it from the expression,
7223 -- which will implicitly force the tag to be set appropriately.
7227 ExpR
: constant Node_Id
:= Relocate_Node
(Exp
);
7228 Result_Id
: constant Entity_Id
:=
7229 Make_Temporary
(Loc
, 'R', ExpR
);
7230 Result_Exp
: constant Node_Id
:=
7231 New_Occurrence_Of
(Result_Id
, Loc
);
7232 Result_Obj
: constant Node_Id
:=
7233 Make_Object_Declaration
(Loc
,
7234 Defining_Identifier
=> Result_Id
,
7235 Object_Definition
=>
7236 New_Occurrence_Of
(R_Type
, Loc
),
7237 Constant_Present
=> True,
7238 Expression
=> ExpR
);
7241 Set_Assignment_OK
(Result_Obj
);
7242 Insert_Action
(Exp
, Result_Obj
);
7244 Rewrite
(Exp
, Result_Exp
);
7245 Analyze_And_Resolve
(Exp
, R_Type
);
7249 -- Ada 2005 (AI95-344): If the result type is class-wide, then insert
7250 -- a check that the level of the return expression's underlying type
7251 -- is not deeper than the level of the master enclosing the function.
7253 -- AI12-043: The check is made immediately after the return object is
7254 -- created. This means that we do not apply it to the simple return
7255 -- generated by the expansion of an extended return statement.
7257 -- No runtime check needed in interface thunks since it is performed
7258 -- by the target primitive associated with the thunk.
7260 elsif Is_Class_Wide_Type
(R_Type
)
7261 and then not Comes_From_Extended_Return_Statement
(N
)
7262 and then not Is_Thunk
(Scope_Id
)
7264 Apply_CW_Accessibility_Check
(Exp
, Scope_Id
);
7266 -- Ada 2012 (AI05-0073): If the result subtype of the function is
7267 -- defined by an access_definition designating a specific tagged
7268 -- type T, a check is made that the result value is null or the tag
7269 -- of the object designated by the result value identifies T.
7271 -- The return expression is referenced twice in the code below, so it
7272 -- must be made free of side effects. Given that different compilers
7273 -- may evaluate these parameters in different order, both occurrences
7276 elsif Ekind
(R_Type
) = E_Anonymous_Access_Type
7277 and then Is_Tagged_Type
(Designated_Type
(R_Type
))
7278 and then not Is_Class_Wide_Type
(Designated_Type
(R_Type
))
7279 and then Nkind
(Original_Node
(Exp
)) /= N_Null
7280 and then not Tag_Checks_Suppressed
(Designated_Type
(R_Type
))
7283 -- [Constraint_Error
7285 -- and then Exp.all not in Designated_Type]
7288 Make_Raise_Constraint_Error
(Loc
,
7293 Left_Opnd
=> Duplicate_Subexpr
(Exp
),
7294 Right_Opnd
=> Make_Null
(Loc
)),
7299 Make_Explicit_Dereference
(Loc
,
7300 Prefix
=> Duplicate_Subexpr
(Exp
)),
7302 New_Occurrence_Of
(Designated_Type
(R_Type
), Loc
))),
7304 Reason
=> CE_Tag_Check_Failed
),
7305 Suppress
=> All_Checks
);
7308 -- If the result is of an unconstrained array subtype with fixed lower
7309 -- bound, then sliding to that bound may be needed.
7311 if Is_Fixed_Lower_Bound_Array_Subtype
(R_Type
) then
7312 Expand_Sliding_Conversion
(Exp
, R_Type
);
7315 -- If we are returning a nonscalar object that is possibly unaligned,
7316 -- then copy the value into a temporary first. This copy may need to
7317 -- expand to a loop of component operations.
7319 if Is_Possibly_Unaligned_Slice
(Exp
)
7320 or else (Is_Possibly_Unaligned_Object
(Exp
)
7321 and then not Represented_As_Scalar
(Etype
(Exp
)))
7324 ExpR
: constant Node_Id
:= Relocate_Node
(Exp
);
7325 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', ExpR
);
7328 Make_Object_Declaration
(Loc
,
7329 Defining_Identifier
=> Tnn
,
7330 Constant_Present
=> True,
7331 Object_Definition
=> New_Occurrence_Of
(R_Type
, Loc
),
7332 Expression
=> ExpR
),
7333 Suppress
=> All_Checks
);
7334 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
7338 -- Ada 2005 (AI-251): If this return statement corresponds with an
7339 -- simple return statement associated with an extended return statement
7340 -- and the type of the returned object is an interface then generate an
7341 -- implicit conversion to force displacement of the "this" pointer.
7343 if Ada_Version
>= Ada_2005
7344 and then Comes_From_Extended_Return_Statement
(N
)
7345 and then Nkind
(Expression
(N
)) = N_Identifier
7346 and then Is_Interface
(Utyp
)
7347 and then Utyp
/= Underlying_Type
(Exp_Typ
)
7349 Rewrite
(Exp
, Convert_To
(Utyp
, Relocate_Node
(Exp
)));
7350 Analyze_And_Resolve
(Exp
);
7353 -- Ada 2022 (AI12-0279)
7355 if Has_Yield_Aspect
(Scope_Id
)
7356 and then RTE_Available
(RE_Yield
)
7359 Make_Procedure_Call_Statement
(Loc
,
7360 New_Occurrence_Of
(RTE
(RE_Yield
), Loc
)));
7362 end Expand_Simple_Function_Return
;
7364 -----------------------
7365 -- Freeze_Subprogram --
7366 -----------------------
7368 procedure Freeze_Subprogram
(N
: Node_Id
) is
7369 Loc
: constant Source_Ptr
:= Sloc
(N
);
7370 Subp
: constant Entity_Id
:= Entity
(N
);
7373 -- We suppress the initialization of the dispatch table entry when
7374 -- not Tagged_Type_Expansion because the dispatching mechanism is
7375 -- handled internally by the target.
7377 if Is_Dispatching_Operation
(Subp
)
7378 and then not Is_Abstract_Subprogram
(Subp
)
7379 and then Present
(DTC_Entity
(Subp
))
7380 and then Present
(Scope
(DTC_Entity
(Subp
)))
7381 and then Tagged_Type_Expansion
7382 and then not Restriction_Active
(No_Dispatching_Calls
)
7383 and then RTE_Available
(RE_Tag
)
7386 Typ
: constant Entity_Id
:= Scope
(DTC_Entity
(Subp
));
7391 -- Handle private overridden primitives
7393 if not Is_CPP_Class
(Typ
) then
7394 Check_Overriding_Operation
(Subp
);
7397 -- We assume that imported CPP primitives correspond with objects
7398 -- whose constructor is in the CPP side; therefore we don't need
7399 -- to generate code to register them in the dispatch table.
7401 if Is_CPP_Class
(Typ
) then
7404 -- Handle CPP primitives found in derivations of CPP_Class types.
7405 -- These primitives must have been inherited from some parent, and
7406 -- there is no need to register them in the dispatch table because
7407 -- Build_Inherit_Prims takes care of initializing these slots.
7409 elsif Is_Imported
(Subp
)
7410 and then Convention
(Subp
) in Convention_C_Family
7414 -- Generate code to register the primitive in non statically
7415 -- allocated dispatch tables
7417 elsif not Building_Static_DT
(Scope
(DTC_Entity
(Subp
))) then
7419 -- When a primitive is frozen, enter its name in its dispatch
7422 if not Is_Interface
(Typ
)
7423 or else Present
(Interface_Alias
(Subp
))
7425 if Is_Predefined_Dispatching_Operation
(Subp
) then
7426 L
:= Register_Predefined_Primitive
(Loc
, Subp
);
7431 Append_List_To
(L
, Register_Primitive
(Loc
, Subp
));
7433 if Is_Empty_List
(L
) then
7436 elsif No
(Actions
(N
)) then
7440 Append_List
(L
, Actions
(N
));
7447 -- Mark functions that return by reference. Note that it cannot be part
7448 -- of the normal semantic analysis of the spec since the underlying
7449 -- returned type may not be known yet (for private types).
7451 Compute_Returns_By_Ref
(Subp
);
7452 end Freeze_Subprogram
;
7454 --------------------------
7455 -- Has_BIP_Extra_Formal --
7456 --------------------------
7458 function Has_BIP_Extra_Formal
7460 Kind
: BIP_Formal_Kind
;
7461 Must_Be_Frozen
: Boolean := True) return Boolean
7463 Extra_Formal
: Entity_Id
:= Extra_Formals
(E
);
7466 -- We can only rely on the availability of the extra formals in frozen
7467 -- entities or in subprogram types of dispatching calls (since their
7468 -- extra formals are added when the target subprogram is frozen; see
7469 -- Expand_Dispatching_Call).
7471 pragma Assert
((Is_Frozen
(E
) or else not Must_Be_Frozen
)
7472 or else (Ekind
(E
) = E_Subprogram_Type
7473 and then Is_Dispatch_Table_Entity
(E
))
7474 or else (Is_Dispatching_Operation
(E
)
7475 and then Is_Frozen
(Find_Dispatching_Type
(E
))));
7477 while Present
(Extra_Formal
) loop
7478 if Is_Build_In_Place_Entity
(Extra_Formal
)
7479 and then BIP_Suffix_Kind
(Extra_Formal
) = Kind
7484 Next_Formal_With_Extras
(Extra_Formal
);
7488 end Has_BIP_Extra_Formal
;
7490 ------------------------------
7491 -- Insert_Post_Call_Actions --
7492 ------------------------------
7494 procedure Insert_Post_Call_Actions
(N
: Node_Id
; Post_Call
: List_Id
) is
7495 Context
: constant Node_Id
:= Parent
(N
);
7498 if Is_Empty_List
(Post_Call
) then
7502 -- Cases where the call is not a member of a statement list. This also
7503 -- includes the cases where the call is an actual in another function
7504 -- call, or is an index, or is an operand of an if-expression, i.e. is
7505 -- in an expression context.
7507 if not Is_List_Member
(N
)
7508 or else Nkind
(Context
) in N_Function_Call
7510 | N_Indexed_Component
7512 -- In Ada 2012 the call may be a function call in an expression
7513 -- (since OUT and IN OUT parameters are now allowed for such calls).
7514 -- The write-back of (in)-out parameters is handled by the back-end,
7515 -- but the constraint checks generated when subtypes of formal and
7516 -- actual don't match must be inserted in the form of assignments.
7517 -- Also do this in the case of explicit dereferences, which can occur
7518 -- due to rewritings of function calls with controlled results.
7520 if Nkind
(N
) = N_Function_Call
7521 or else Nkind
(Original_Node
(N
)) = N_Function_Call
7522 or else Nkind
(N
) = N_Explicit_Dereference
7524 pragma Assert
(Ada_Version
>= Ada_2012
);
7525 -- Functions with '[in] out' parameters are only allowed in Ada
7528 -- We used to handle this by climbing up parents to a
7529 -- non-statement/declaration and then simply making a call to
7530 -- Insert_Actions_After (P, Post_Call), but that doesn't work
7531 -- for Ada 2012. If we are in the middle of an expression, e.g.
7532 -- the condition of an IF, this call would insert after the IF
7533 -- statement, which is much too late to be doing the write back.
7536 -- if Clobber (X) then
7537 -- Put_Line (X'Img);
7542 -- Now assume Clobber changes X, if we put the write back after
7543 -- the IF, the Put_Line gets the wrong value and the goto causes
7544 -- the write back to be skipped completely.
7546 -- To deal with this, we replace the call by
7549 -- Tnnn : constant function-result-type := function-call;
7550 -- Post_Call actions
7555 -- However, that doesn't work if function-result-type requires
7556 -- finalization (because function-call's result never gets
7557 -- finalized). So in that case, we instead replace the call by
7560 -- type Ref is access all function-result-type;
7561 -- Ptr : constant Ref := function-call'Reference;
7562 -- Tnnn : constant function-result-type := Ptr.all;
7563 -- Finalize (Ptr.all);
7564 -- Post_Call actions
7571 Loc
: constant Source_Ptr
:= Sloc
(N
);
7572 Tnnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
7573 FRTyp
: constant Entity_Id
:= Etype
(N
);
7574 Name
: constant Node_Id
:= Relocate_Node
(N
);
7577 if Needs_Finalization
(FRTyp
) then
7579 Ptr_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
7581 Ptr_Typ_Decl
: constant Node_Id
:=
7582 Make_Full_Type_Declaration
(Loc
,
7583 Defining_Identifier
=> Ptr_Typ
,
7585 Make_Access_To_Object_Definition
(Loc
,
7586 All_Present
=> True,
7587 Subtype_Indication
=>
7588 New_Occurrence_Of
(FRTyp
, Loc
)));
7590 Ptr_Obj
: constant Entity_Id
:=
7591 Make_Temporary
(Loc
, 'P');
7593 Ptr_Obj_Decl
: constant Node_Id
:=
7594 Make_Object_Declaration
(Loc
,
7595 Defining_Identifier
=> Ptr_Obj
,
7596 Object_Definition
=>
7597 New_Occurrence_Of
(Ptr_Typ
, Loc
),
7598 Constant_Present
=> True,
7600 Make_Attribute_Reference
(Loc
,
7602 Attribute_Name
=> Name_Unrestricted_Access
));
7604 function Ptr_Dereference
return Node_Id
is
7605 (Make_Explicit_Dereference
(Loc
,
7606 Prefix
=> New_Occurrence_Of
(Ptr_Obj
, Loc
)));
7608 Tnn_Decl
: constant Node_Id
:=
7609 Make_Object_Declaration
(Loc
,
7610 Defining_Identifier
=> Tnnn
,
7611 Object_Definition
=> New_Occurrence_Of
(FRTyp
, Loc
),
7612 Constant_Present
=> True,
7613 Expression
=> Ptr_Dereference
);
7615 Finalize_Call
: constant Node_Id
:=
7617 (Obj_Ref
=> Ptr_Dereference
, Typ
=> FRTyp
);
7619 -- Prepend in reverse order
7621 Prepend_To
(Post_Call
, Finalize_Call
);
7622 Prepend_To
(Post_Call
, Tnn_Decl
);
7623 Prepend_To
(Post_Call
, Ptr_Obj_Decl
);
7624 Prepend_To
(Post_Call
, Ptr_Typ_Decl
);
7627 Prepend_To
(Post_Call
,
7628 Make_Object_Declaration
(Loc
,
7629 Defining_Identifier
=> Tnnn
,
7630 Object_Definition
=> New_Occurrence_Of
(FRTyp
, Loc
),
7631 Constant_Present
=> True,
7632 Expression
=> Name
));
7636 Make_Expression_With_Actions
(Loc
,
7637 Actions
=> Post_Call
,
7638 Expression
=> New_Occurrence_Of
(Tnnn
, Loc
)));
7640 -- We don't want to just blindly call Analyze_And_Resolve
7641 -- because that would cause unwanted recursion on the call.
7642 -- So for a moment set the call as analyzed to prevent that
7643 -- recursion, and get the rest analyzed properly, then reset
7644 -- the analyzed flag, so our caller can continue.
7646 Set_Analyzed
(Name
, True);
7647 Analyze_And_Resolve
(N
, FRTyp
);
7648 Set_Analyzed
(Name
, False);
7651 -- If not the special Ada 2012 case of a function call, then we must
7652 -- have the triggering statement of a triggering alternative or an
7653 -- entry call alternative, and we can add the post call stuff to the
7654 -- corresponding statement list.
7657 pragma Assert
(Nkind
(Context
) in N_Entry_Call_Alternative
7658 | N_Triggering_Alternative
);
7660 if Is_Non_Empty_List
(Statements
(Context
)) then
7661 Insert_List_Before_And_Analyze
7662 (First
(Statements
(Context
)), Post_Call
);
7664 Set_Statements
(Context
, Post_Call
);
7668 -- A procedure call is always part of a declarative or statement list,
7669 -- however a function call may appear nested within a construct. Most
7670 -- cases of function call nesting are handled in the special case above.
7671 -- The only exception is when the function call acts as an actual in a
7672 -- procedure call. In this case the function call is in a list, but the
7673 -- post-call actions must be inserted after the procedure call.
7674 -- What if the function call is an aggregate component ???
7676 elsif Nkind
(Context
) = N_Procedure_Call_Statement
then
7677 Insert_Actions_After
(Context
, Post_Call
);
7679 -- Otherwise, normal case where N is in a statement sequence, just put
7680 -- the post-call stuff after the call statement.
7683 Insert_Actions_After
(N
, Post_Call
);
7685 end Insert_Post_Call_Actions
;
7687 ---------------------------------------
7688 -- Install_Class_Preconditions_Check --
7689 ---------------------------------------
7691 procedure Install_Class_Preconditions_Check
(Call_Node
: Node_Id
) is
7692 Loc
: constant Source_Ptr
:= Sloc
(Call_Node
);
7694 function Build_Dynamic_Check_Helper_Call
return Node_Id
;
7695 -- Build call to the helper runtime function of the nearest ancestor
7696 -- of the target subprogram that dynamically evaluates the merged
7697 -- or-else preconditions.
7699 function Build_Error_Message
(Subp_Id
: Entity_Id
) return Node_Id
;
7700 -- Build message associated with the class-wide precondition of Subp_Id
7701 -- indicating the call that caused it.
7703 function Build_Static_Check_Helper_Call
return Node_Id
;
7704 -- Build call to the helper runtime function of the nearest ancestor
7705 -- of the target subprogram that dynamically evaluates the merged
7706 -- or-else preconditions.
7708 function Class_Preconditions_Subprogram
7709 (Spec_Id
: Entity_Id
;
7710 Dynamic
: Boolean) return Node_Id
;
7711 -- Return the nearest ancestor of Spec_Id defining a helper function
7712 -- that evaluates a combined or-else expression containing all the
7713 -- inherited class-wide preconditions; Dynamic enables searching for
7714 -- the helper that dynamically evaluates preconditions using dispatching
7715 -- calls; if False it searches for the helper that statically evaluates
7716 -- preconditions; return Empty when not available (which means that no
7717 -- preconditions check is required).
7719 -------------------------------------
7720 -- Build_Dynamic_Check_Helper_Call --
7721 -------------------------------------
7723 function Build_Dynamic_Check_Helper_Call
return Node_Id
is
7724 Spec_Id
: constant Entity_Id
:= Entity
(Name
(Call_Node
));
7725 CW_Subp
: constant Entity_Id
:=
7726 Class_Preconditions_Subprogram
(Spec_Id
,
7728 Helper_Id
: constant Entity_Id
:=
7729 Dynamic_Call_Helper
(CW_Subp
);
7730 Actuals
: constant List_Id
:= New_List
;
7731 A
: Node_Id
:= First_Actual
(Call_Node
);
7732 F
: Entity_Id
:= First_Formal
(Helper_Id
);
7735 while Present
(A
) loop
7737 -- Ensure that the evaluation of the actuals will not produce
7740 Remove_Side_Effects
(A
);
7742 Append_To
(Actuals
, New_Copy_Tree
(A
));
7748 Make_Function_Call
(Loc
,
7749 Name
=> New_Occurrence_Of
(Helper_Id
, Loc
),
7750 Parameter_Associations
=> Actuals
);
7751 end Build_Dynamic_Check_Helper_Call
;
7753 -------------------------
7754 -- Build_Error_Message --
7755 -------------------------
7757 function Build_Error_Message
(Subp_Id
: Entity_Id
) return Node_Id
is
7759 procedure Append_Message
7761 Is_First
: in out Boolean);
7762 -- Build the fragment of the message associated with subprogram Id;
7763 -- Is_First facilitates identifying continuation messages.
7765 --------------------
7766 -- Append_Message --
7767 --------------------
7769 procedure Append_Message
7771 Is_First
: in out Boolean)
7773 Prag
: constant Node_Id
:= Get_Class_Wide_Pragma
(Id
,
7774 Pragma_Precondition
);
7779 if No
(Prag
) or else Is_Ignored
(Prag
) then
7783 Msg
:= Expression
(Last
(Pragma_Argument_Associations
(Prag
)));
7784 Str_Id
:= Strval
(Msg
);
7789 Append
(Global_Name_Buffer
, Strval
(Msg
));
7792 and then Name_Buffer
(1 .. 19) = "failed precondition"
7794 Insert_Str_In_Name_Buffer
("inherited ", 8);
7799 Str
: constant String := To_String
(Str_Id
);
7803 Append
(Global_Name_Buffer
, ASCII
.LF
);
7804 Append
(Global_Name_Buffer
, " or ");
7806 From_Idx
:= Name_Len
;
7807 Append
(Global_Name_Buffer
, Str_Id
);
7809 if Str
(1 .. 19) = "failed precondition" then
7810 Insert_Str_In_Name_Buffer
("inherited ", From_Idx
+ 8);
7818 Str_Loc
: constant String := Build_Location_String
(Loc
);
7819 Subps
: constant Subprogram_List
:=
7820 Inherited_Subprograms
(Subp_Id
);
7821 Is_First
: Boolean := True;
7823 -- Start of processing for Build_Error_Message
7827 Append_Message
(Subp_Id
, Is_First
);
7829 for Index
in Subps
'Range loop
7830 Append_Message
(Subps
(Index
), Is_First
);
7833 if Present
(Controlling_Argument
(Call_Node
)) then
7834 Append
(Global_Name_Buffer
, " in dispatching call at ");
7836 Append
(Global_Name_Buffer
, " in call at ");
7839 Append
(Global_Name_Buffer
, Str_Loc
);
7841 return Make_String_Literal
(Loc
, Name_Buffer
(1 .. Name_Len
));
7842 end Build_Error_Message
;
7844 ------------------------------------
7845 -- Build_Static_Check_Helper_Call --
7846 ------------------------------------
7848 function Build_Static_Check_Helper_Call
return Node_Id
is
7849 Actuals
: constant List_Id
:= New_List
;
7851 Helper_Id
: Entity_Id
;
7853 CW_Subp
: Entity_Id
;
7854 Spec_Id
: constant Entity_Id
:= Entity
(Name
(Call_Node
));
7857 -- The target is the wrapper built to support inheriting body but
7858 -- overriding pre/postconditions (AI12-0195).
7860 if Is_Dispatch_Table_Wrapper
(Spec_Id
) then
7866 CW_Subp
:= Class_Preconditions_Subprogram
(Spec_Id
,
7870 Helper_Id
:= Static_Call_Helper
(CW_Subp
);
7872 F
:= First_Formal
(Helper_Id
);
7873 A
:= First_Actual
(Call_Node
);
7874 while Present
(A
) loop
7876 -- Ensure that the evaluation of the actuals will not produce
7879 Remove_Side_Effects
(A
);
7881 -- Ensure matching types to avoid reporting spurious errors since
7882 -- the called helper may have been built for a parent type.
7884 if Etype
(F
) /= Etype
(A
) then
7886 Unchecked_Convert_To
(Etype
(F
), New_Copy_Tree
(A
)));
7888 Append_To
(Actuals
, New_Copy_Tree
(A
));
7896 Make_Function_Call
(Loc
,
7897 Name
=> New_Occurrence_Of
(Helper_Id
, Loc
),
7898 Parameter_Associations
=> Actuals
);
7899 end Build_Static_Check_Helper_Call
;
7901 ------------------------------------
7902 -- Class_Preconditions_Subprogram --
7903 ------------------------------------
7905 function Class_Preconditions_Subprogram
7906 (Spec_Id
: Entity_Id
;
7907 Dynamic
: Boolean) return Node_Id
7909 Subp_Id
: constant Entity_Id
:= Ultimate_Alias
(Spec_Id
);
7912 -- Prevent cascaded errors
7914 if not Is_Dispatching_Operation
(Subp_Id
) then
7917 -- No need to search if this subprogram has the helper we are
7921 if Present
(Dynamic_Call_Helper
(Subp_Id
)) then
7925 if Present
(Static_Call_Helper
(Subp_Id
)) then
7930 -- Process inherited subprograms looking for class-wide
7934 Subps
: constant Subprogram_List
:=
7935 Inherited_Subprograms
(Subp_Id
);
7936 Subp_Id
: Entity_Id
;
7939 for Index
in Subps
'Range loop
7940 Subp_Id
:= Subps
(Index
);
7942 if Present
(Alias
(Subp_Id
)) then
7943 Subp_Id
:= Ultimate_Alias
(Subp_Id
);
7946 -- Wrappers of class-wide pre/postconditions reference the
7947 -- parent primitive that has the inherited contract.
7949 if Is_Wrapper
(Subp_Id
)
7950 and then Present
(LSP_Subprogram
(Subp_Id
))
7952 Subp_Id
:= LSP_Subprogram
(Subp_Id
);
7956 if Present
(Dynamic_Call_Helper
(Subp_Id
)) then
7960 if Present
(Static_Call_Helper
(Subp_Id
)) then
7968 end Class_Preconditions_Subprogram
;
7972 Dynamic_Check
: constant Boolean :=
7973 Present
(Controlling_Argument
(Call_Node
));
7974 Class_Subp
: Entity_Id
;
7978 -- Start of processing for Install_Class_Preconditions_Check
7981 -- Do not expand the check if we are compiling under restriction
7982 -- No_Dispatching_Calls; the semantic analyzer has previously
7983 -- notified the violation of this restriction.
7986 and then Restriction_Active
(No_Dispatching_Calls
)
7990 -- Class-wide precondition check not needed in interface thunks since
7991 -- they are installed in the dispatching call that caused invoking the
7994 elsif Is_Thunk
(Current_Scope
) then
7998 Subp
:= Entity
(Name
(Call_Node
));
8000 -- No check needed for this subprogram call if no class-wide
8001 -- preconditions apply (or if the unique available preconditions
8002 -- are ignored preconditions).
8004 Class_Subp
:= Class_Preconditions_Subprogram
(Subp
, Dynamic_Check
);
8007 or else No
(Class_Preconditions
(Class_Subp
))
8012 -- Build and install the check
8014 if Dynamic_Check
then
8015 Cond
:= Build_Dynamic_Check_Helper_Call
;
8017 Cond
:= Build_Static_Check_Helper_Call
;
8020 if Exception_Locations_Suppressed
then
8021 Insert_Action
(Call_Node
,
8022 Make_If_Statement
(Loc
,
8023 Condition
=> Make_Op_Not
(Loc
, Cond
),
8024 Then_Statements
=> New_List
(
8025 Make_Raise_Statement
(Loc
,
8028 (RTE
(RE_Assert_Failure
), Loc
)))));
8030 -- Failed check with message indicating the failed precondition and the
8031 -- call that caused it.
8034 Insert_Action
(Call_Node
,
8035 Make_If_Statement
(Loc
,
8036 Condition
=> Make_Op_Not
(Loc
, Cond
),
8037 Then_Statements
=> New_List
(
8038 Make_Procedure_Call_Statement
(Loc
,
8041 (RTE
(RE_Raise_Assert_Failure
), Loc
),
8042 Parameter_Associations
=>
8043 New_List
(Build_Error_Message
(Subp
))))));
8045 end Install_Class_Preconditions_Check
;
8047 ------------------------------
8048 -- Is_Build_In_Place_Entity --
8049 ------------------------------
8051 function Is_Build_In_Place_Entity
(E
: Entity_Id
) return Boolean is
8052 Nam
: constant String := Get_Name_String
(Chars
(E
));
8054 function Has_Suffix
(Suffix
: String) return Boolean;
8055 -- Return True if Nam has suffix Suffix
8057 function Has_Suffix
(Suffix
: String) return Boolean is
8058 Len
: constant Natural := Suffix
'Length;
8060 return Nam
'Length > Len
8061 and then Nam
(Nam
'Last - Len
+ 1 .. Nam
'Last) = Suffix
;
8064 -- Start of processing for Is_Build_In_Place_Entity
8067 return Has_Suffix
(BIP_Alloc_Suffix
)
8068 or else Has_Suffix
(BIP_Storage_Pool_Suffix
)
8069 or else Has_Suffix
(BIP_Finalization_Master_Suffix
)
8070 or else Has_Suffix
(BIP_Task_Master_Suffix
)
8071 or else Has_Suffix
(BIP_Activation_Chain_Suffix
)
8072 or else Has_Suffix
(BIP_Object_Access_Suffix
);
8073 end Is_Build_In_Place_Entity
;
8075 --------------------------------
8076 -- Is_Build_In_Place_Function --
8077 --------------------------------
8079 function Is_Build_In_Place_Function
(E
: Entity_Id
) return Boolean is
8080 Kind
: constant Entity_Kind
:= Ekind
(E
);
8081 Typ
: constant Entity_Id
:= Etype
(E
);
8084 -- This function is called from Expand_Subtype_From_Expr during
8085 -- semantic analysis, even when expansion is off. In those cases
8086 -- the build_in_place expansion will not take place.
8088 if not Expander_Active
then
8092 -- We never use build-in-place if the convention is other than Ada,
8093 -- but note that it is OK for a build-in-place function to return a
8094 -- type with a foreign convention because the machinery ensures there
8097 return (Kind
in E_Function | E_Generic_Function
8099 (Kind
= E_Subprogram_Type
and then Typ
/= Standard_Void_Type
))
8100 and then Is_Build_In_Place_Result_Type
(Typ
)
8101 and then not Has_Foreign_Convention
(E
);
8102 end Is_Build_In_Place_Function
;
8104 -------------------------------------
8105 -- Is_Build_In_Place_Function_Call --
8106 -------------------------------------
8108 function Is_Build_In_Place_Function_Call
(N
: Node_Id
) return Boolean is
8109 Exp_Node
: constant Node_Id
:= Unqual_Conv
(N
);
8110 Function_Id
: Entity_Id
;
8113 -- Return False if the expander is currently inactive, since awareness
8114 -- of build-in-place treatment is only relevant during expansion. Note
8115 -- that Is_Build_In_Place_Function, which is called as part of this
8116 -- function, is also conditioned this way, but we need to check here as
8117 -- well to avoid blowing up on processing protected calls when expansion
8118 -- is disabled (such as with -gnatc) since those would trip over the
8119 -- raise of Program_Error below.
8121 -- In SPARK mode, build-in-place calls are not expanded, so that we
8122 -- may end up with a call that is neither resolved to an entity, nor
8123 -- an indirect call.
8125 if not Expander_Active
or else Nkind
(Exp_Node
) /= N_Function_Call
then
8129 if Is_Entity_Name
(Name
(Exp_Node
)) then
8130 Function_Id
:= Entity
(Name
(Exp_Node
));
8132 -- In the case of an explicitly dereferenced call, use the subprogram
8133 -- type generated for the dereference.
8135 elsif Nkind
(Name
(Exp_Node
)) = N_Explicit_Dereference
then
8136 Function_Id
:= Etype
(Name
(Exp_Node
));
8138 -- This may be a call to a protected function.
8140 elsif Nkind
(Name
(Exp_Node
)) = N_Selected_Component
then
8141 -- The selector in question might not have been analyzed due to a
8142 -- previous error, so analyze it here to output the appropriate
8143 -- error message instead of crashing when attempting to fetch its
8146 if not Analyzed
(Selector_Name
(Name
(Exp_Node
))) then
8147 Analyze
(Selector_Name
(Name
(Exp_Node
)));
8150 Function_Id
:= Etype
(Entity
(Selector_Name
(Name
(Exp_Node
))));
8153 raise Program_Error
;
8157 Result
: constant Boolean := Is_Build_In_Place_Function
(Function_Id
);
8158 -- So we can stop here in the debugger
8162 end Is_Build_In_Place_Function_Call
;
8164 ---------------------------------------
8165 -- Is_Function_Call_With_BIP_Formals --
8166 ---------------------------------------
8168 function Is_Function_Call_With_BIP_Formals
(N
: Node_Id
) return Boolean is
8169 Exp_Node
: constant Node_Id
:= Unqual_Conv
(N
);
8170 Function_Id
: Entity_Id
;
8173 -- Return False if the expander is currently inactive, since awareness
8174 -- of build-in-place treatment is only relevant during expansion. Note
8175 -- that Is_Build_In_Place_Function, which is called as part of this
8176 -- function, is also conditioned this way, but we need to check here as
8177 -- well to avoid blowing up on processing protected calls when expansion
8178 -- is disabled (such as with -gnatc) since those would trip over the
8179 -- raise of Program_Error below.
8181 -- In SPARK mode, build-in-place calls are not expanded, so that we
8182 -- may end up with a call that is neither resolved to an entity, nor
8183 -- an indirect call.
8185 if not Expander_Active
or else Nkind
(Exp_Node
) /= N_Function_Call
then
8189 if Is_Entity_Name
(Name
(Exp_Node
)) then
8190 Function_Id
:= Entity
(Name
(Exp_Node
));
8192 -- In the case of an explicitly dereferenced call, use the subprogram
8193 -- type generated for the dereference.
8195 elsif Nkind
(Name
(Exp_Node
)) = N_Explicit_Dereference
then
8196 Function_Id
:= Etype
(Name
(Exp_Node
));
8198 -- This may be a call to a protected function.
8200 elsif Nkind
(Name
(Exp_Node
)) = N_Selected_Component
then
8201 -- The selector in question might not have been analyzed due to a
8202 -- previous error, so analyze it here to output the appropriate
8203 -- error message instead of crashing when attempting to fetch its
8206 if not Analyzed
(Selector_Name
(Name
(Exp_Node
))) then
8207 Analyze
(Selector_Name
(Name
(Exp_Node
)));
8210 Function_Id
:= Etype
(Entity
(Selector_Name
(Name
(Exp_Node
))));
8213 raise Program_Error
;
8216 if Is_Build_In_Place_Function
(Function_Id
) then
8219 -- True also if the function has BIP Formals
8223 Kind
: constant Entity_Kind
:= Ekind
(Function_Id
);
8226 if (Kind
in E_Function | E_Generic_Function
8227 or else (Kind
= E_Subprogram_Type
8229 Etype
(Function_Id
) /= Standard_Void_Type
))
8230 and then Has_BIP_Formals
(Function_Id
)
8232 -- So we can stop here in the debugger
8239 end Is_Function_Call_With_BIP_Formals
;
8241 -----------------------------------
8242 -- Is_Build_In_Place_Result_Type --
8243 -----------------------------------
8245 function Is_Build_In_Place_Result_Type
(Typ
: Entity_Id
) return Boolean is
8247 if not Expander_Active
then
8251 -- In Ada 2005 all functions with an inherently limited return type
8252 -- must be handled using a build-in-place profile, including the case
8253 -- of a function with a limited interface result, where the function
8254 -- may return objects of nonlimited descendants.
8256 return Is_Inherently_Limited_Type
(Typ
)
8257 and then Ada_Version
>= Ada_2005
8258 and then not Debug_Flag_Dot_L
;
8259 end Is_Build_In_Place_Result_Type
;
8261 -------------------------------------
8262 -- Is_Build_In_Place_Return_Object --
8263 -------------------------------------
8265 function Is_Build_In_Place_Return_Object
(E
: Entity_Id
) return Boolean is
8267 return Is_Return_Object
(E
)
8268 and then Is_Build_In_Place_Function
(Return_Applies_To
(Scope
(E
)));
8269 end Is_Build_In_Place_Return_Object
;
8271 -----------------------------------
8272 -- Is_By_Reference_Return_Object --
8273 -----------------------------------
8275 function Is_By_Reference_Return_Object
(E
: Entity_Id
) return Boolean is
8277 return Is_Return_Object
(E
)
8278 and then Is_By_Reference_Type
(Etype
(Return_Applies_To
(Scope
(E
))));
8279 end Is_By_Reference_Return_Object
;
8281 -----------------------
8282 -- Is_Null_Procedure --
8283 -----------------------
8285 function Is_Null_Procedure
(Subp
: Entity_Id
) return Boolean is
8286 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
8289 if Ekind
(Subp
) /= E_Procedure
then
8292 -- Check if this is a declared null procedure
8294 elsif Nkind
(Decl
) = N_Subprogram_Declaration
then
8295 if not Null_Present
(Specification
(Decl
)) then
8298 elsif No
(Body_To_Inline
(Decl
)) then
8301 -- Check if the body contains only a null statement, followed by
8302 -- the return statement added during expansion.
8306 Orig_Bod
: constant Node_Id
:= Body_To_Inline
(Decl
);
8312 if Nkind
(Orig_Bod
) /= N_Subprogram_Body
then
8315 -- We must skip SCIL nodes because they are currently
8316 -- implemented as special N_Null_Statement nodes.
8320 (Statements
(Handled_Statement_Sequence
(Orig_Bod
)));
8321 Stat2
:= Next_Non_SCIL_Node
(Stat
);
8324 Is_Empty_List
(Declarations
(Orig_Bod
))
8325 and then Nkind
(Stat
) = N_Null_Statement
8329 (Nkind
(Stat2
) = N_Simple_Return_Statement
8330 and then No
(Next
(Stat2
))));
8338 end Is_Null_Procedure
;
8340 --------------------------------------
8341 -- Is_Secondary_Stack_Return_Object --
8342 --------------------------------------
8344 function Is_Secondary_Stack_Return_Object
(E
: Entity_Id
) return Boolean is
8346 return Is_Return_Object
(E
)
8347 and then Needs_Secondary_Stack
(Etype
(Return_Applies_To
(Scope
(E
))));
8348 end Is_Secondary_Stack_Return_Object
;
8350 ------------------------------
8351 -- Is_Special_Return_Object --
8352 ------------------------------
8354 function Is_Special_Return_Object
(E
: Entity_Id
) return Boolean is
8356 return Is_Build_In_Place_Return_Object
(E
)
8357 or else Is_Secondary_Stack_Return_Object
(E
)
8358 or else (Back_End_Return_Slot
8359 and then Is_By_Reference_Return_Object
(E
));
8360 end Is_Special_Return_Object
;
8362 -------------------------------------------
8363 -- Make_Build_In_Place_Call_In_Allocator --
8364 -------------------------------------------
8366 procedure Make_Build_In_Place_Call_In_Allocator
8367 (Allocator
: Node_Id
;
8368 Function_Call
: Node_Id
)
8370 Acc_Type
: constant Entity_Id
:= Etype
(Allocator
);
8371 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
8372 Func_Call
: Node_Id
:= Function_Call
;
8373 Ref_Func_Call
: Node_Id
;
8374 Function_Id
: Entity_Id
;
8375 Result_Subt
: Entity_Id
;
8376 New_Allocator
: Node_Id
;
8377 Return_Obj_Access
: Entity_Id
; -- temp for function result
8378 Temp_Init
: Node_Id
; -- initial value of Return_Obj_Access
8379 Alloc_Form
: BIP_Allocation_Form
;
8380 Pool
: Node_Id
; -- nonnull if Alloc_Form = User_Storage_Pool
8381 Return_Obj_Actual
: Node_Id
; -- the temp.all, in caller-allocates case
8382 Chain
: Entity_Id
; -- activation chain, in case of tasks
8385 -- Step past qualification or unchecked conversion (the latter can occur
8386 -- in cases of calls to 'Input).
8388 if Nkind
(Func_Call
) in N_Qualified_Expression
8390 | N_Unchecked_Type_Conversion
8392 Func_Call
:= Expression
(Func_Call
);
8395 -- Mark the call as processed as a build-in-place call
8397 pragma Assert
(not Is_Expanded_Build_In_Place_Call
(Func_Call
));
8398 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8400 if Is_Entity_Name
(Name
(Func_Call
)) then
8401 Function_Id
:= Entity
(Name
(Func_Call
));
8403 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8404 Function_Id
:= Etype
(Name
(Func_Call
));
8407 raise Program_Error
;
8410 Warn_BIP
(Func_Call
);
8412 Result_Subt
:= Available_View
(Etype
(Function_Id
));
8414 -- Create a temp for the function result. In the caller-allocates case,
8415 -- this will be initialized to the result of a new uninitialized
8416 -- allocator. Note: we do not use Allocator as the Related_Node of
8417 -- Return_Obj_Access in call to Make_Temporary below as this would
8418 -- create a sort of infinite "recursion".
8420 Return_Obj_Access
:= Make_Temporary
(Loc
, 'R');
8421 Set_Etype
(Return_Obj_Access
, Acc_Type
);
8422 Set_Can_Never_Be_Null
(Acc_Type
, False);
8423 -- It gets initialized to null, so we can't have that
8425 -- When the result subtype is constrained, the return object is created
8426 -- on the caller side, and access to it is passed to the function. This
8427 -- optimization is disabled when the result subtype needs finalization
8428 -- actions because the caller side allocation may result in undesirable
8429 -- finalization. Consider the following example:
8431 -- function Make_Lim_Ctrl return Lim_Ctrl is
8433 -- return Result : Lim_Ctrl := raise Program_Error do
8436 -- end Make_Lim_Ctrl;
8438 -- Obj : Lim_Ctrl_Ptr := new Lim_Ctrl'(Make_Lim_Ctrl);
8440 -- Even though the size of limited controlled type Lim_Ctrl is known,
8441 -- allocating Obj at the caller side will chain Obj on Lim_Ctrl_Ptr's
8442 -- finalization master. The subsequent call to Make_Lim_Ctrl will fail
8443 -- during the initialization actions for Result, which implies that
8444 -- Result (and Obj by extension) should not be finalized. However Obj
8445 -- will be finalized when access type Lim_Ctrl_Ptr goes out of scope
8446 -- since it is already attached on the related finalization master.
8448 -- Here and in related routines, we must examine the full view of the
8449 -- type, because the view at the point of call may differ from the
8450 -- one in the function body, and the expansion mechanism depends on
8451 -- the characteristics of the full view.
8453 if Needs_BIP_Alloc_Form
(Function_Id
) then
8456 -- Case of a user-defined storage pool. Pass an allocation parameter
8457 -- indicating that the function should allocate its result in the
8458 -- pool, and pass the pool. Use 'Unrestricted_Access because the
8459 -- pool may not be aliased.
8461 if Present
(Associated_Storage_Pool
(Acc_Type
)) then
8462 Alloc_Form
:= User_Storage_Pool
;
8464 Make_Attribute_Reference
(Loc
,
8467 (Associated_Storage_Pool
(Acc_Type
), Loc
),
8468 Attribute_Name
=> Name_Unrestricted_Access
);
8470 -- No user-defined pool; pass an allocation parameter indicating that
8471 -- the function should allocate its result on the heap.
8474 Alloc_Form
:= Global_Heap
;
8475 Pool
:= Make_Null
(No_Location
);
8478 -- The caller does not provide the return object in this case, so we
8479 -- have to pass null for the object access actual.
8481 Return_Obj_Actual
:= Empty
;
8484 -- Replace the initialized allocator of form "new T'(Func (...))"
8485 -- with an uninitialized allocator of form "new T", where T is the
8486 -- result subtype of the called function. The call to the function
8487 -- is handled separately further below.
8490 Make_Allocator
(Loc
,
8491 Expression
=> New_Occurrence_Of
(Result_Subt
, Loc
));
8492 Set_No_Initialization
(New_Allocator
);
8494 -- Copy attributes to new allocator. Note that the new allocator
8495 -- logically comes from source if the original one did, so copy the
8496 -- relevant flag. This ensures proper treatment of the restriction
8497 -- No_Implicit_Heap_Allocations in this case.
8499 Set_Storage_Pool
(New_Allocator
, Storage_Pool
(Allocator
));
8500 Set_Procedure_To_Call
(New_Allocator
, Procedure_To_Call
(Allocator
));
8501 Set_Comes_From_Source
(New_Allocator
, Comes_From_Source
(Allocator
));
8503 Rewrite
(Allocator
, New_Allocator
);
8505 -- Initial value of the temp is the result of the uninitialized
8506 -- allocator. Unchecked_Convert is needed for T'Input where T is
8507 -- derived from a controlled type.
8509 Temp_Init
:= Relocate_Node
(Allocator
);
8511 if Nkind
(Function_Call
) in
8512 N_Type_Conversion | N_Unchecked_Type_Conversion
8514 Temp_Init
:= Unchecked_Convert_To
(Acc_Type
, Temp_Init
);
8517 -- Indicate that caller allocates, and pass in the return object
8519 Alloc_Form
:= Caller_Allocation
;
8520 Pool
:= Make_Null
(No_Location
);
8521 Return_Obj_Actual
:= Unchecked_Convert_To
8523 Make_Explicit_Dereference
(Loc
,
8524 Prefix
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
)));
8526 -- When the result subtype is unconstrained, the function itself must
8527 -- perform the allocation of the return object, so we pass parameters
8532 -- Declare the temp object
8534 Insert_Action
(Allocator
,
8535 Make_Object_Declaration
(Loc
,
8536 Defining_Identifier
=> Return_Obj_Access
,
8537 Object_Definition
=> New_Occurrence_Of
(Acc_Type
, Loc
),
8538 Expression
=> Temp_Init
));
8540 Ref_Func_Call
:= Make_Reference
(Loc
, Func_Call
);
8542 -- Ada 2005 (AI-251): If the type of the allocator is an interface
8543 -- then generate an implicit conversion to force displacement of the
8546 if Is_Interface
(Designated_Type
(Acc_Type
)) then
8549 OK_Convert_To
(Acc_Type
, Ref_Func_Call
));
8551 -- If the types are incompatible, we need an unchecked conversion. Note
8552 -- that the full types will be compatible, but the types not visibly
8555 elsif Nkind
(Function_Call
)
8556 in N_Type_Conversion | N_Unchecked_Type_Conversion
8558 Ref_Func_Call
:= Unchecked_Convert_To
(Acc_Type
, Ref_Func_Call
);
8562 Assign
: constant Node_Id
:=
8563 Make_Assignment_Statement
(Loc
,
8564 Name
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
),
8565 Expression
=> Ref_Func_Call
);
8566 -- Assign the result of the function call into the temp. In the
8567 -- caller-allocates case, this is overwriting the temp with its
8568 -- initial value, which has no effect. In the callee-allocates case,
8569 -- this is setting the temp to point to the object allocated by the
8570 -- callee. Unchecked_Convert is needed for T'Input where T is derived
8571 -- from a controlled type.
8574 -- Actions to be inserted. If there are no tasks, this is just the
8575 -- assignment statement. If the allocated object has tasks, we need
8576 -- to wrap the assignment in a block that activates them. The
8577 -- activation chain of that block must be passed to the function,
8578 -- rather than some outer chain.
8581 if Might_Have_Tasks
(Result_Subt
) then
8582 Actions
:= New_List
;
8583 Build_Task_Allocate_Block_With_Init_Stmts
8584 (Actions
, Allocator
, Init_Stmts
=> New_List
(Assign
));
8585 Chain
:= Activation_Chain_Entity
(Last
(Actions
));
8587 Actions
:= New_List
(Assign
);
8591 Insert_Actions
(Allocator
, Actions
);
8594 -- When the function has a controlling result, an allocation-form
8595 -- parameter must be passed indicating that the caller is allocating
8596 -- the result object. This is needed because such a function can be
8597 -- called as a dispatching operation and must be treated similarly
8598 -- to functions with unconstrained result subtypes.
8600 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8601 (Func_Call
, Function_Id
, Alloc_Form
, Pool_Actual
=> Pool
);
8603 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8604 (Func_Call
, Function_Id
, Acc_Type
);
8606 Add_Task_Actuals_To_Build_In_Place_Call
8607 (Func_Call
, Function_Id
, Master_Actual
=> Master_Id
(Acc_Type
),
8610 -- Add an implicit actual to the function call that provides access
8611 -- to the allocated object. An unchecked conversion to the (specific)
8612 -- result subtype of the function is inserted to handle cases where
8613 -- the access type of the allocator has a class-wide designated type.
8615 Add_Access_Actual_To_Build_In_Place_Call
8616 (Func_Call
, Function_Id
, Return_Obj_Actual
);
8618 -- Finally, replace the allocator node with a reference to the temp
8620 Rewrite
(Allocator
, New_Occurrence_Of
(Return_Obj_Access
, Loc
));
8622 Analyze_And_Resolve
(Allocator
, Acc_Type
);
8623 pragma Assert
(Check_Number_Of_Actuals
(Func_Call
, Function_Id
));
8624 pragma Assert
(Check_BIP_Actuals
(Func_Call
, Function_Id
));
8625 end Make_Build_In_Place_Call_In_Allocator
;
8627 ---------------------------------------------------
8628 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8629 ---------------------------------------------------
8631 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8632 (Function_Call
: Node_Id
)
8634 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
8635 Func_Call
: constant Node_Id
:= Unqual_Conv
(Function_Call
);
8636 Function_Id
: Entity_Id
;
8637 Result_Subt
: Entity_Id
;
8638 Return_Obj_Id
: Entity_Id
;
8639 Return_Obj_Decl
: Entity_Id
;
8642 -- If the call has already been processed to add build-in-place actuals
8643 -- then return. One place this can occur is for calls to build-in-place
8644 -- functions that occur within a call to a protected operation, where
8645 -- due to rewriting and expansion of the protected call there can be
8646 -- more than one call to Expand_Actuals for the same set of actuals.
8648 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
8652 -- Mark the call as processed as a build-in-place call
8654 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8656 if Is_Entity_Name
(Name
(Func_Call
)) then
8657 Function_Id
:= Entity
(Name
(Func_Call
));
8659 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8660 Function_Id
:= Etype
(Name
(Func_Call
));
8663 raise Program_Error
;
8666 Warn_BIP
(Func_Call
);
8668 Result_Subt
:= Etype
(Function_Id
);
8670 -- If the build-in-place function returns a controlled object, then the
8671 -- object needs to be finalized immediately after the context. Since
8672 -- this case produces a transient scope, the servicing finalizer needs
8673 -- to name the returned object. Create a temporary which is initialized
8674 -- with the function call:
8676 -- Temp_Id : Func_Type := BIP_Func_Call;
8678 -- The initialization expression of the temporary will be rewritten by
8679 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8680 -- Call_In_Object_Declaration.
8682 if Needs_Finalization
(Result_Subt
) then
8684 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'R');
8685 Temp_Decl
: Node_Id
;
8688 -- Reset the guard on the function call since the following does
8689 -- not perform actual call expansion.
8691 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
, False);
8694 Make_Object_Declaration
(Loc
,
8695 Defining_Identifier
=> Temp_Id
,
8696 Object_Definition
=>
8697 New_Occurrence_Of
(Result_Subt
, Loc
),
8699 New_Copy_Tree
(Function_Call
));
8701 Insert_Action
(Function_Call
, Temp_Decl
);
8703 Rewrite
(Function_Call
, New_Occurrence_Of
(Temp_Id
, Loc
));
8704 Analyze
(Function_Call
);
8707 -- When the result subtype is definite, an object of the subtype is
8708 -- declared and an access value designating it is passed as an actual.
8710 elsif Caller_Known_Size
(Func_Call
, Result_Subt
) then
8712 -- Create a temporary object to hold the function result
8714 Return_Obj_Id
:= Make_Temporary
(Loc
, 'R');
8715 Set_Etype
(Return_Obj_Id
, Result_Subt
);
8718 Make_Object_Declaration
(Loc
,
8719 Defining_Identifier
=> Return_Obj_Id
,
8720 Aliased_Present
=> True,
8721 Object_Definition
=> New_Occurrence_Of
(Result_Subt
, Loc
));
8723 Set_No_Initialization
(Return_Obj_Decl
);
8725 Insert_Action
(Func_Call
, Return_Obj_Decl
);
8727 -- When the function has a controlling result, an allocation-form
8728 -- parameter must be passed indicating that the caller is allocating
8729 -- the result object. This is needed because such a function can be
8730 -- called as a dispatching operation and must be treated similarly
8731 -- to functions with unconstrained result subtypes.
8733 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8734 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
8736 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8737 (Func_Call
, Function_Id
);
8739 Add_Task_Actuals_To_Build_In_Place_Call
8740 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
8742 -- Add an implicit actual to the function call that provides access
8743 -- to the caller's return object.
8745 Add_Access_Actual_To_Build_In_Place_Call
8746 (Func_Call
, Function_Id
, New_Occurrence_Of
(Return_Obj_Id
, Loc
));
8748 pragma Assert
(Check_Number_Of_Actuals
(Func_Call
, Function_Id
));
8749 pragma Assert
(Check_BIP_Actuals
(Func_Call
, Function_Id
));
8751 -- When the result subtype is unconstrained, the function must allocate
8752 -- the return object in the secondary stack, so appropriate implicit
8753 -- parameters are added to the call to indicate that. A transient
8754 -- scope is established to ensure eventual cleanup of the result.
8757 -- Pass an allocation parameter indicating that the function should
8758 -- allocate its result on the secondary stack.
8760 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8761 (Func_Call
, Function_Id
, Alloc_Form
=> Secondary_Stack
);
8763 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8764 (Func_Call
, Function_Id
);
8766 Add_Task_Actuals_To_Build_In_Place_Call
8767 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
8769 -- Pass a null value to the function since no return object is
8770 -- available on the caller side.
8772 Add_Access_Actual_To_Build_In_Place_Call
8773 (Func_Call
, Function_Id
, Empty
);
8775 pragma Assert
(Check_Number_Of_Actuals
(Func_Call
, Function_Id
));
8776 pragma Assert
(Check_BIP_Actuals
(Func_Call
, Function_Id
));
8778 end Make_Build_In_Place_Call_In_Anonymous_Context
;
8780 --------------------------------------------
8781 -- Make_Build_In_Place_Call_In_Assignment --
8782 --------------------------------------------
8784 procedure Make_Build_In_Place_Call_In_Assignment
8786 Function_Call
: Node_Id
)
8788 Func_Call
: constant Node_Id
:= Unqual_Conv
(Function_Call
);
8789 Lhs
: constant Node_Id
:= Name
(Assign
);
8790 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
8791 Func_Id
: Entity_Id
;
8794 Ptr_Typ
: Entity_Id
;
8795 Ptr_Typ_Decl
: Node_Id
;
8797 Result_Subt
: Entity_Id
;
8800 -- Mark the call as processed as a build-in-place call
8802 pragma Assert
(not Is_Expanded_Build_In_Place_Call
(Func_Call
));
8803 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8805 if Is_Entity_Name
(Name
(Func_Call
)) then
8806 Func_Id
:= Entity
(Name
(Func_Call
));
8808 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8809 Func_Id
:= Etype
(Name
(Func_Call
));
8812 raise Program_Error
;
8815 Warn_BIP
(Func_Call
);
8817 Result_Subt
:= Etype
(Func_Id
);
8819 -- When the result subtype is unconstrained, an additional actual must
8820 -- be passed to indicate that the caller is providing the return object.
8821 -- This parameter must also be passed when the called function has a
8822 -- controlling result, because dispatching calls to the function needs
8823 -- to be treated effectively the same as calls to class-wide functions.
8825 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8826 (Func_Call
, Func_Id
, Alloc_Form
=> Caller_Allocation
);
8828 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8829 (Func_Call
, Func_Id
);
8831 Add_Task_Actuals_To_Build_In_Place_Call
8832 (Func_Call
, Func_Id
, Make_Identifier
(Loc
, Name_uMaster
));
8834 -- Add an implicit actual to the function call that provides access to
8835 -- the caller's return object.
8837 Add_Access_Actual_To_Build_In_Place_Call
8838 (Func_Call
, Func_Id
, Unchecked_Convert_To
(Result_Subt
, Lhs
));
8840 -- Create an access type designating the function's result subtype
8842 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
8845 Make_Full_Type_Declaration
(Loc
,
8846 Defining_Identifier
=> Ptr_Typ
,
8848 Make_Access_To_Object_Definition
(Loc
,
8849 All_Present
=> True,
8850 Subtype_Indication
=>
8851 New_Occurrence_Of
(Result_Subt
, Loc
)));
8852 Insert_After_And_Analyze
(Assign
, Ptr_Typ_Decl
);
8854 -- Finally, create an access object initialized to a reference to the
8855 -- function call. We know this access value is non-null, so mark the
8856 -- entity accordingly to suppress junk access checks.
8858 New_Expr
:= Make_Reference
(Loc
, Relocate_Node
(Func_Call
));
8860 -- Add a conversion if it's the wrong type
8862 New_Expr
:= Unchecked_Convert_To
(Ptr_Typ
, New_Expr
);
8864 Obj_Id
:= Make_Temporary
(Loc
, 'R', New_Expr
);
8865 Set_Etype
(Obj_Id
, Ptr_Typ
);
8866 Set_Is_Known_Non_Null
(Obj_Id
);
8869 Make_Object_Declaration
(Loc
,
8870 Defining_Identifier
=> Obj_Id
,
8871 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
8872 Expression
=> New_Expr
);
8873 Insert_After_And_Analyze
(Ptr_Typ_Decl
, Obj_Decl
);
8875 Rewrite
(Assign
, Make_Null_Statement
(Loc
));
8876 pragma Assert
(Check_Number_Of_Actuals
(Func_Call
, Func_Id
));
8877 pragma Assert
(Check_BIP_Actuals
(Func_Call
, Func_Id
));
8878 end Make_Build_In_Place_Call_In_Assignment
;
8880 ----------------------------------------------------
8881 -- Make_Build_In_Place_Call_In_Object_Declaration --
8882 ----------------------------------------------------
8884 procedure Make_Build_In_Place_Call_In_Object_Declaration
8885 (Obj_Decl
: Node_Id
;
8886 Function_Call
: Node_Id
)
8888 function Get_Function_Id
(Func_Call
: Node_Id
) return Entity_Id
;
8889 -- Get the value of Function_Id, below
8891 ---------------------
8892 -- Get_Function_Id --
8893 ---------------------
8895 function Get_Function_Id
(Func_Call
: Node_Id
) return Entity_Id
is
8897 if Is_Entity_Name
(Name
(Func_Call
)) then
8898 return Entity
(Name
(Func_Call
));
8900 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8901 return Etype
(Name
(Func_Call
));
8904 raise Program_Error
;
8906 end Get_Function_Id
;
8910 Func_Call
: constant Node_Id
:= Unqual_Conv
(Function_Call
);
8911 Function_Id
: constant Entity_Id
:= Get_Function_Id
(Func_Call
);
8912 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
8913 Obj_Loc
: constant Source_Ptr
:= Sloc
(Obj_Decl
);
8914 Obj_Def_Id
: constant Entity_Id
:= Defining_Identifier
(Obj_Decl
);
8915 Obj_Typ
: constant Entity_Id
:= Etype
(Obj_Def_Id
);
8916 Encl_Func
: constant Entity_Id
:= Enclosing_Subprogram
(Obj_Def_Id
);
8917 Result_Subt
: constant Entity_Id
:= Etype
(Function_Id
);
8919 Call_Deref
: Node_Id
;
8920 Caller_Object
: Node_Id
;
8922 Designated_Type
: Entity_Id
;
8923 Fmaster_Actual
: Node_Id
:= Empty
;
8924 Pool_Actual
: Node_Id
;
8925 Ptr_Typ
: Entity_Id
;
8926 Ptr_Typ_Decl
: Node_Id
;
8927 Pass_Caller_Acc
: Boolean := False;
8930 Definite
: constant Boolean :=
8931 Caller_Known_Size
(Func_Call
, Result_Subt
)
8932 and then not Is_Class_Wide_Type
(Obj_Typ
);
8933 -- In the case of "X : T'Class := F(...);", where F returns a
8934 -- Caller_Known_Size (specific) tagged type, we treat it as
8935 -- indefinite, because the code for the Definite case below sets the
8936 -- initialization expression of the object to Empty, which would be
8937 -- illegal Ada, and would cause gigi to misallocate X.
8939 Is_OK_Return_Object
: constant Boolean :=
8940 Is_Return_Object
(Obj_Def_Id
)
8942 not Has_Foreign_Convention
(Return_Applies_To
(Scope
(Obj_Def_Id
)));
8944 -- Start of processing for Make_Build_In_Place_Call_In_Object_Declaration
8947 -- If the call has already been processed to add build-in-place actuals
8950 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
8954 -- Mark the call as processed as a build-in-place call
8956 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8958 Warn_BIP
(Func_Call
);
8960 -- Create an access type designating the function's result subtype.
8961 -- We use the type of the original call because it may be a call to an
8962 -- inherited operation, which the expansion has replaced with the parent
8963 -- operation that yields the parent type. Note that this access type
8964 -- must be declared before we establish a transient scope, so that it
8965 -- receives the proper accessibility level.
8967 if Is_Class_Wide_Type
(Obj_Typ
)
8968 and then not Is_Interface
(Obj_Typ
)
8969 and then not Is_Class_Wide_Type
(Etype
(Function_Call
))
8971 Designated_Type
:= Obj_Typ
;
8973 Designated_Type
:= Etype
(Function_Call
);
8976 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
8978 Make_Full_Type_Declaration
(Loc
,
8979 Defining_Identifier
=> Ptr_Typ
,
8981 Make_Access_To_Object_Definition
(Loc
,
8982 All_Present
=> True,
8983 Subtype_Indication
=>
8984 New_Occurrence_Of
(Designated_Type
, Loc
)));
8986 -- The access type and its accompanying object must be inserted after
8987 -- the object declaration in the constrained case, so that the function
8988 -- call can be passed access to the object. In the indefinite case, or
8989 -- if the object declaration is for a return object, the access type and
8990 -- object must be inserted before the object, since the object
8991 -- declaration is rewritten to be a renaming of a dereference of the
8992 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8993 -- the result object is in a different (transient) scope, so won't cause
8996 if Definite
and then not Is_OK_Return_Object
then
8998 -- The presence of an address clause complicates the build-in-place
8999 -- expansion because the indicated address must be processed before
9000 -- the indirect call is generated (including the definition of a
9001 -- local pointer to the object). The address clause may come from
9002 -- an aspect specification or from an explicit attribute
9003 -- specification appearing after the object declaration. These two
9004 -- cases require different processing.
9006 if Has_Aspect
(Obj_Def_Id
, Aspect_Address
) then
9008 -- Skip non-delayed pragmas that correspond to other aspects, if
9009 -- any, to find proper insertion point for freeze node of object.
9012 D
: Node_Id
:= Obj_Decl
;
9013 N
: Node_Id
:= Next
(D
);
9017 and then Nkind
(N
) in N_Attribute_Reference | N_Pragma
9024 Insert_After
(D
, Ptr_Typ_Decl
);
9026 -- Freeze object before pointer declaration, to ensure that
9027 -- generated attribute for address is inserted at the proper
9030 Freeze_Before
(Ptr_Typ_Decl
, Obj_Def_Id
);
9033 Analyze
(Ptr_Typ_Decl
);
9035 elsif Present
(Following_Address_Clause
(Obj_Decl
)) then
9037 -- Locate explicit address clause, which may also follow pragmas
9038 -- generated by other aspect specifications.
9041 Addr
: constant Node_Id
:= Following_Address_Clause
(Obj_Decl
);
9042 D
: Node_Id
:= Next
(Obj_Decl
);
9045 while Present
(D
) loop
9051 Insert_After_And_Analyze
(Addr
, Ptr_Typ_Decl
);
9055 Insert_After_And_Analyze
(Obj_Decl
, Ptr_Typ_Decl
);
9058 Insert_Action
(Obj_Decl
, Ptr_Typ_Decl
);
9061 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
9062 -- elaborated in an inner (transient) scope and thus won't cause
9063 -- freezing by itself. It's not an itype, but it needs to be frozen
9064 -- inside the current subprogram (see Freeze_Outside in freeze.adb).
9066 Freeze_Itype
(Ptr_Typ
, Ptr_Typ_Decl
);
9068 -- If the object is a return object of an enclosing build-in-place
9069 -- function, then the implicit build-in-place parameters of the
9070 -- enclosing function are simply passed along to the called function.
9071 -- (Unfortunately, this won't cover the case of extension aggregates
9072 -- where the ancestor part is a build-in-place indefinite function
9073 -- call that should be passed along the caller's parameters.
9074 -- Currently those get mishandled by reassigning the result of the
9075 -- call to the aggregate return object, when the call result should
9076 -- really be directly built in place in the aggregate and not in a
9079 if Is_OK_Return_Object
then
9080 Pass_Caller_Acc
:= True;
9082 -- When the enclosing function has a BIP_Alloc_Form formal then we
9083 -- pass it along to the callee (such as when the enclosing function
9084 -- has an unconstrained or tagged result type).
9086 if Needs_BIP_Alloc_Form
(Encl_Func
) then
9087 if RTE_Available
(RE_Root_Storage_Pool_Ptr
) then
9090 (Build_In_Place_Formal
9091 (Encl_Func
, BIP_Storage_Pool
), Loc
);
9093 -- The build-in-place pool formal is not built on e.g. ZFP
9096 Pool_Actual
:= Empty
;
9099 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9100 (Function_Call
=> Func_Call
,
9101 Function_Id
=> Function_Id
,
9104 (Build_In_Place_Formal
(Encl_Func
, BIP_Alloc_Form
), Loc
),
9105 Pool_Actual
=> Pool_Actual
);
9107 -- Otherwise, if enclosing function has a definite result subtype,
9108 -- then caller allocation will be used.
9111 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9112 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
9115 if Needs_BIP_Finalization_Master
(Encl_Func
) then
9118 (Build_In_Place_Formal
9119 (Encl_Func
, BIP_Finalization_Master
), Loc
);
9122 -- Retrieve the BIPacc formal from the enclosing function and convert
9123 -- it to the access type of the callee's BIP_Object_Access formal.
9126 Unchecked_Convert_To
9127 (Etype
(Build_In_Place_Formal
(Function_Id
, BIP_Object_Access
)),
9129 (Build_In_Place_Formal
(Encl_Func
, BIP_Object_Access
), Loc
));
9131 -- In the definite case, add an implicit actual to the function call
9132 -- that provides access to the declared object. An unchecked conversion
9133 -- to the (specific) result type of the function is inserted to handle
9134 -- the case where the object is declared with a class-wide type.
9137 Caller_Object
:= Unchecked_Convert_To
9138 (Result_Subt
, New_Occurrence_Of
(Obj_Def_Id
, Loc
));
9140 -- When the function has a controlling result, an allocation-form
9141 -- parameter must be passed indicating that the caller is allocating
9142 -- the result object. This is needed because such a function can be
9143 -- called as a dispatching operation and must be treated similarly to
9144 -- functions with indefinite result subtypes.
9146 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9147 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
9149 -- The allocation for indefinite library-level objects occurs on the
9150 -- heap as opposed to the secondary stack. This accommodates DLLs where
9151 -- the secondary stack is destroyed after each library unload. This is a
9152 -- hybrid mechanism where a stack-allocated object lives on the heap.
9154 elsif Is_Library_Level_Entity
(Obj_Def_Id
)
9155 and then not Restriction_Active
(No_Implicit_Heap_Allocations
)
9157 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9158 (Func_Call
, Function_Id
, Alloc_Form
=> Global_Heap
);
9159 Caller_Object
:= Empty
;
9161 -- Create a finalization master for the access result type to ensure
9162 -- that the heap allocation can properly chain the object and later
9163 -- finalize it when the library unit goes out of scope.
9165 if Needs_Finalization
(Etype
(Func_Call
)) then
9166 Build_Finalization_Master
9168 For_Lib_Level
=> True,
9169 Insertion_Node
=> Ptr_Typ_Decl
);
9172 Make_Attribute_Reference
(Loc
,
9174 New_Occurrence_Of
(Finalization_Master
(Ptr_Typ
), Loc
),
9175 Attribute_Name
=> Name_Unrestricted_Access
);
9178 -- In other indefinite cases, pass an indication to do the allocation
9179 -- on the secondary stack and set Caller_Object to Empty so that a null
9180 -- value will be passed for the caller's object address. A transient
9181 -- scope is established to ensure eventual cleanup of the result.
9184 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9185 (Func_Call
, Function_Id
, Alloc_Form
=> Secondary_Stack
);
9186 Caller_Object
:= Empty
;
9188 Establish_Transient_Scope
(Obj_Decl
, Manage_Sec_Stack
=> True);
9191 -- Pass along any finalization master actual, which is needed in the
9192 -- case where the called function initializes a return object of an
9193 -- enclosing build-in-place function.
9195 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9196 (Func_Call
=> Func_Call
,
9197 Func_Id
=> Function_Id
,
9198 Master_Exp
=> Fmaster_Actual
);
9200 if Nkind
(Parent
(Obj_Decl
)) = N_Extended_Return_Statement
9201 and then Needs_BIP_Task_Actuals
(Function_Id
)
9203 -- Here we're passing along the master that was passed in to this
9206 Add_Task_Actuals_To_Build_In_Place_Call
9207 (Func_Call
, Function_Id
,
9210 (Build_In_Place_Formal
(Encl_Func
, BIP_Task_Master
), Loc
));
9213 Add_Task_Actuals_To_Build_In_Place_Call
9214 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
9217 Add_Access_Actual_To_Build_In_Place_Call
9221 Is_Access
=> Pass_Caller_Acc
);
9223 -- Finally, create an access object initialized to a reference to the
9224 -- function call. We know this access value cannot be null, so mark the
9225 -- entity accordingly to suppress the access check. We need to suppress
9226 -- warnings, because this can be part of the expansion of "for ... of"
9227 -- and similar constructs that generate finalization actions. Such
9228 -- finalization actions are safe, because they check a count that
9229 -- indicates which objects should be finalized, but the back end
9230 -- nonetheless warns about uninitialized objects.
9232 Def_Id
:= Make_Temporary
(Loc
, 'R', Func_Call
);
9233 Set_Warnings_Off
(Def_Id
);
9234 Set_Etype
(Def_Id
, Ptr_Typ
);
9235 Set_Is_Known_Non_Null
(Def_Id
);
9237 if Nkind
(Function_Call
) in N_Type_Conversion
9238 | N_Unchecked_Type_Conversion
9241 Make_Object_Declaration
(Loc
,
9242 Defining_Identifier
=> Def_Id
,
9243 Constant_Present
=> True,
9244 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
9246 Unchecked_Convert_To
9247 (Ptr_Typ
, Make_Reference
(Loc
, Relocate_Node
(Func_Call
))));
9250 Make_Object_Declaration
(Loc
,
9251 Defining_Identifier
=> Def_Id
,
9252 Constant_Present
=> True,
9253 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
9255 Make_Reference
(Loc
, Relocate_Node
(Func_Call
)));
9258 Insert_After_And_Analyze
(Ptr_Typ_Decl
, Res_Decl
);
9260 -- If the result subtype of the called function is definite and is not
9261 -- itself the return expression of an enclosing BIP function, then mark
9262 -- the object as having no initialization.
9264 if Definite
and then not Is_OK_Return_Object
then
9266 -- The related object declaration is encased in a transient block
9267 -- because the build-in-place function call contains at least one
9268 -- nested function call that produces a controlled transient
9271 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
9273 -- Since the build-in-place expansion decouples the call from the
9274 -- object declaration, the finalization machinery lacks the context
9275 -- which prompted the generation of the transient block. To resolve
9276 -- this scenario, store the build-in-place call.
9278 if Scope_Is_Transient
then
9279 Set_BIP_Initialization_Call
(Obj_Def_Id
, Res_Decl
);
9282 Set_Expression
(Obj_Decl
, Empty
);
9283 Set_No_Initialization
(Obj_Decl
);
9285 -- In case of an indefinite result subtype, or if the call is the
9286 -- return expression of an enclosing BIP function, rewrite the object
9287 -- declaration as an object renaming where the renamed object is a
9288 -- dereference of <function_Call>'reference:
9290 -- Obj : Subt renames <function_call>'Ref.all;
9294 Make_Explicit_Dereference
(Obj_Loc
,
9295 Prefix
=> New_Occurrence_Of
(Def_Id
, Obj_Loc
));
9298 Make_Object_Renaming_Declaration
(Obj_Loc
,
9299 Defining_Identifier
=> Make_Temporary
(Obj_Loc
, 'D'),
9301 New_Occurrence_Of
(Designated_Type
, Obj_Loc
),
9302 Name
=> Call_Deref
));
9304 -- At this point, Defining_Identifier (Obj_Decl) is no longer equal
9307 pragma Assert
(Ekind
(Defining_Identifier
(Obj_Decl
)) = E_Void
);
9308 Set_Renamed_Object_Of_Possibly_Void
9309 (Defining_Identifier
(Obj_Decl
), Call_Deref
);
9311 -- If the original entity comes from source, then mark the new
9312 -- entity as needing debug information, even though it's defined
9313 -- by a generated renaming that does not come from source, so that
9314 -- the Materialize_Entity flag will be set on the entity when
9315 -- Debug_Renaming_Declaration is called during analysis.
9317 if Comes_From_Source
(Obj_Def_Id
) then
9318 Set_Debug_Info_Needed
(Defining_Identifier
(Obj_Decl
));
9322 Replace_Renaming_Declaration_Id
9323 (Obj_Decl
, Original_Node
(Obj_Decl
));
9326 pragma Assert
(Check_Number_Of_Actuals
(Func_Call
, Function_Id
));
9327 pragma Assert
(Check_BIP_Actuals
(Func_Call
, Function_Id
));
9328 end Make_Build_In_Place_Call_In_Object_Declaration
;
9330 -------------------------------------------------
9331 -- Make_Build_In_Place_Iface_Call_In_Allocator --
9332 -------------------------------------------------
9334 procedure Make_Build_In_Place_Iface_Call_In_Allocator
9335 (Allocator
: Node_Id
;
9336 Function_Call
: Node_Id
)
9338 BIP_Func_Call
: constant Node_Id
:=
9339 Unqual_BIP_Iface_Function_Call
(Function_Call
);
9340 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
9342 Anon_Type
: Entity_Id
;
9347 -- No action if the call has already been processed
9349 if Is_Expanded_Build_In_Place_Call
(BIP_Func_Call
) then
9353 Tmp_Id
:= Make_Temporary
(Loc
, 'D');
9355 -- Insert a temporary before N initialized with the BIP function call
9356 -- without its enclosing type conversions and analyze it without its
9357 -- expansion. This temporary facilitates us reusing the BIP machinery,
9358 -- which takes care of adding the extra build-in-place actuals and
9359 -- transforms this object declaration into an object renaming
9362 Anon_Type
:= Create_Itype
(E_Anonymous_Access_Type
, Function_Call
);
9363 Set_Directly_Designated_Type
(Anon_Type
, Etype
(BIP_Func_Call
));
9364 Set_Etype
(Anon_Type
, Anon_Type
);
9365 Build_Class_Wide_Master
(Anon_Type
);
9368 Make_Object_Declaration
(Loc
,
9369 Defining_Identifier
=> Tmp_Id
,
9370 Object_Definition
=> New_Occurrence_Of
(Anon_Type
, Loc
),
9372 Make_Allocator
(Loc
,
9374 Make_Qualified_Expression
(Loc
,
9376 New_Occurrence_Of
(Etype
(BIP_Func_Call
), Loc
),
9377 Expression
=> New_Copy_Tree
(BIP_Func_Call
))));
9379 -- Manually set the associated node for the anonymous access type to
9380 -- be its local declaration, to avoid confusing and complicating
9381 -- the accessibility machinery.
9383 Set_Associated_Node_For_Itype
(Anon_Type
, Tmp_Decl
);
9385 Expander_Mode_Save_And_Set
(False);
9386 Insert_Action
(Allocator
, Tmp_Decl
);
9387 Expander_Mode_Restore
;
9389 Make_Build_In_Place_Call_In_Allocator
9390 (Allocator
=> Expression
(Tmp_Decl
),
9391 Function_Call
=> Expression
(Expression
(Tmp_Decl
)));
9393 -- Add a conversion to displace the pointer to the allocated object
9394 -- to reference the corresponding dispatch table.
9397 Convert_To
(Etype
(Allocator
),
9398 New_Occurrence_Of
(Tmp_Id
, Loc
)));
9399 end Make_Build_In_Place_Iface_Call_In_Allocator
;
9401 ---------------------------------------------------------
9402 -- Make_Build_In_Place_Iface_Call_In_Anonymous_Context --
9403 ---------------------------------------------------------
9405 procedure Make_Build_In_Place_Iface_Call_In_Anonymous_Context
9406 (Function_Call
: Node_Id
)
9408 BIP_Func_Call
: constant Node_Id
:=
9409 Unqual_BIP_Iface_Function_Call
(Function_Call
);
9410 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
9416 -- No action of the call has already been processed
9418 if Is_Expanded_Build_In_Place_Call
(BIP_Func_Call
) then
9422 pragma Assert
(Needs_Finalization
(Etype
(BIP_Func_Call
)));
9424 -- Insert a temporary before the call initialized with function call to
9425 -- reuse the BIP machinery which takes care of adding the extra build-in
9426 -- place actuals and transforms this object declaration into an object
9427 -- renaming declaration.
9429 Tmp_Id
:= Make_Temporary
(Loc
, 'D');
9432 Make_Object_Declaration
(Loc
,
9433 Defining_Identifier
=> Tmp_Id
,
9434 Object_Definition
=>
9435 New_Occurrence_Of
(Etype
(Function_Call
), Loc
),
9436 Expression
=> Relocate_Node
(Function_Call
));
9438 Expander_Mode_Save_And_Set
(False);
9439 Insert_Action
(Function_Call
, Tmp_Decl
);
9440 Expander_Mode_Restore
;
9442 Make_Build_In_Place_Iface_Call_In_Object_Declaration
9443 (Obj_Decl
=> Tmp_Decl
,
9444 Function_Call
=> Expression
(Tmp_Decl
));
9445 end Make_Build_In_Place_Iface_Call_In_Anonymous_Context
;
9447 ----------------------------------------------------------
9448 -- Make_Build_In_Place_Iface_Call_In_Object_Declaration --
9449 ----------------------------------------------------------
9451 procedure Make_Build_In_Place_Iface_Call_In_Object_Declaration
9452 (Obj_Decl
: Node_Id
;
9453 Function_Call
: Node_Id
)
9455 BIP_Func_Call
: constant Node_Id
:=
9456 Unqual_BIP_Iface_Function_Call
(Function_Call
);
9457 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
9458 Obj_Id
: constant Entity_Id
:= Defining_Entity
(Obj_Decl
);
9464 -- No action of the call has already been processed
9466 if Is_Expanded_Build_In_Place_Call
(BIP_Func_Call
) then
9470 Tmp_Id
:= Make_Temporary
(Loc
, 'D');
9472 -- Insert a temporary before N initialized with the BIP function call
9473 -- without its enclosing type conversions and analyze it without its
9474 -- expansion. This temporary facilitates us reusing the BIP machinery,
9475 -- which takes care of adding the extra build-in-place actuals and
9476 -- transforms this object declaration into an object renaming
9480 Make_Object_Declaration
(Loc
,
9481 Defining_Identifier
=> Tmp_Id
,
9482 Object_Definition
=>
9483 New_Occurrence_Of
(Etype
(BIP_Func_Call
), Loc
),
9484 Expression
=> New_Copy_Tree
(BIP_Func_Call
));
9486 Expander_Mode_Save_And_Set
(False);
9487 Insert_Action
(Obj_Decl
, Tmp_Decl
);
9488 Expander_Mode_Restore
;
9490 Make_Build_In_Place_Call_In_Object_Declaration
9491 (Obj_Decl
=> Tmp_Decl
,
9492 Function_Call
=> Expression
(Tmp_Decl
));
9494 pragma Assert
(Nkind
(Tmp_Decl
) = N_Object_Renaming_Declaration
);
9496 -- Replace the original build-in-place function call by a reference to
9497 -- the resulting temporary object renaming declaration. In this way,
9498 -- all the interface conversions performed in the original Function_Call
9499 -- on the build-in-place object are preserved.
9501 Rewrite
(BIP_Func_Call
, New_Occurrence_Of
(Tmp_Id
, Loc
));
9503 -- Replace the original object declaration by an internal object
9504 -- renaming declaration. This leaves the generated code more clean (the
9505 -- build-in-place function call in an object renaming declaration and
9506 -- displacements of the pointer to the build-in-place object in another
9507 -- renaming declaration) and allows us to invoke the routine that takes
9508 -- care of replacing the identifier of the renaming declaration (routine
9509 -- originally developed for the regular build-in-place management).
9512 Make_Object_Renaming_Declaration
(Loc
,
9513 Defining_Identifier
=> Make_Temporary
(Loc
, 'D'),
9514 Subtype_Mark
=> New_Occurrence_Of
(Etype
(Obj_Id
), Loc
),
9515 Name
=> Function_Call
));
9518 Replace_Renaming_Declaration_Id
(Obj_Decl
, Original_Node
(Obj_Decl
));
9519 end Make_Build_In_Place_Iface_Call_In_Object_Declaration
;
9521 --------------------------------------------
9522 -- Make_CPP_Constructor_Call_In_Allocator --
9523 --------------------------------------------
9525 procedure Make_CPP_Constructor_Call_In_Allocator
9526 (Allocator
: Node_Id
;
9527 Function_Call
: Node_Id
)
9529 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
9530 Acc_Type
: constant Entity_Id
:= Etype
(Allocator
);
9531 Function_Id
: constant Entity_Id
:= Entity
(Name
(Function_Call
));
9532 Result_Subt
: constant Entity_Id
:= Available_View
(Etype
(Function_Id
));
9534 New_Allocator
: Node_Id
;
9535 Return_Obj_Access
: Entity_Id
;
9539 pragma Assert
(Nkind
(Allocator
) = N_Allocator
9540 and then Nkind
(Function_Call
) = N_Function_Call
);
9541 pragma Assert
(Convention
(Function_Id
) = Convention_CPP
9542 and then Is_Constructor
(Function_Id
));
9543 pragma Assert
(Is_Constrained
(Underlying_Type
(Result_Subt
)));
9545 -- Replace the initialized allocator of form "new T'(Func (...))" with
9546 -- an uninitialized allocator of form "new T", where T is the result
9547 -- subtype of the called function. The call to the function is handled
9548 -- separately further below.
9551 Make_Allocator
(Loc
,
9552 Expression
=> New_Occurrence_Of
(Result_Subt
, Loc
));
9553 Set_No_Initialization
(New_Allocator
);
9555 -- Copy attributes to new allocator. Note that the new allocator
9556 -- logically comes from source if the original one did, so copy the
9557 -- relevant flag. This ensures proper treatment of the restriction
9558 -- No_Implicit_Heap_Allocations in this case.
9560 Set_Storage_Pool
(New_Allocator
, Storage_Pool
(Allocator
));
9561 Set_Procedure_To_Call
(New_Allocator
, Procedure_To_Call
(Allocator
));
9562 Set_Comes_From_Source
(New_Allocator
, Comes_From_Source
(Allocator
));
9564 Rewrite
(Allocator
, New_Allocator
);
9566 -- Create a new access object and initialize it to the result of the
9567 -- new uninitialized allocator. Note: we do not use Allocator as the
9568 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9569 -- as this would create a sort of infinite "recursion".
9571 Return_Obj_Access
:= Make_Temporary
(Loc
, 'R');
9572 Set_Etype
(Return_Obj_Access
, Acc_Type
);
9575 -- Rnnn : constant ptr_T := new (T);
9576 -- Init (Rnn.all,...);
9579 Make_Object_Declaration
(Loc
,
9580 Defining_Identifier
=> Return_Obj_Access
,
9581 Constant_Present
=> True,
9582 Object_Definition
=> New_Occurrence_Of
(Acc_Type
, Loc
),
9583 Expression
=> Relocate_Node
(Allocator
));
9584 Insert_Action
(Allocator
, Tmp_Obj
);
9586 Insert_List_After_And_Analyze
(Tmp_Obj
,
9587 Build_Initialization_Call
(Loc
,
9589 Make_Explicit_Dereference
(Loc
,
9590 Prefix
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
)),
9591 Typ
=> Etype
(Function_Id
),
9592 Constructor_Ref
=> Function_Call
));
9594 -- Finally, replace the allocator node with a reference to the result of
9595 -- the function call itself (which will effectively be an access to the
9596 -- object created by the allocator).
9598 Rewrite
(Allocator
, New_Occurrence_Of
(Return_Obj_Access
, Loc
));
9600 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9601 -- generate an implicit conversion to force displacement of the "this"
9604 if Is_Interface
(Designated_Type
(Acc_Type
)) then
9605 Rewrite
(Allocator
, Convert_To
(Acc_Type
, Relocate_Node
(Allocator
)));
9608 Analyze_And_Resolve
(Allocator
, Acc_Type
);
9609 end Make_CPP_Constructor_Call_In_Allocator
;
9611 ----------------------
9612 -- Might_Have_Tasks --
9613 ----------------------
9615 function Might_Have_Tasks
(Typ
: Entity_Id
) return Boolean is
9617 return not Global_No_Tasking
9618 and then not No_Run_Time_Mode
9619 and then (Has_Task
(Typ
)
9620 or else (Is_Class_Wide_Type
(Typ
)
9621 and then Is_Limited_Record
(Typ
)
9622 and then not Has_Aspect
9623 (Etype
(Typ
), Aspect_No_Task_Parts
)));
9624 end Might_Have_Tasks
;
9626 ----------------------------
9627 -- Needs_BIP_Task_Actuals --
9628 ----------------------------
9630 function Needs_BIP_Task_Actuals
(Func_Id
: Entity_Id
) return Boolean is
9631 Subp_Id
: Entity_Id
;
9632 Func_Typ
: Entity_Id
;
9635 if Global_No_Tasking
or else No_Run_Time_Mode
then
9639 -- For thunks we must rely on their target entity; otherwise, given that
9640 -- the profile of thunks for functions returning a limited interface
9641 -- type returns a class-wide type, we would erroneously add these extra
9644 if Is_Thunk
(Func_Id
) then
9645 Subp_Id
:= Thunk_Target
(Func_Id
);
9653 Func_Typ
:= Underlying_Type
(Etype
(Subp_Id
));
9655 -- Functions returning types with foreign convention don't have extra
9658 if Has_Foreign_Convention
(Func_Typ
) then
9661 -- At first sight, for all the following cases, we could add assertions
9662 -- to ensure that if Func_Id is frozen then the computed result matches
9663 -- with the availability of the task master extra formal; unfortunately
9664 -- this is not feasible because we may be precisely freezing this entity
9665 -- (that is, Is_Frozen has been set by Freeze_Entity but it has not
9666 -- completed its work).
9668 elsif Has_Task
(Func_Typ
) then
9671 elsif Ekind
(Func_Id
) = E_Function
then
9672 return Might_Have_Tasks
(Func_Typ
);
9674 -- Handle subprogram type internally generated for dispatching call. We
9675 -- cannot rely on the return type of the subprogram type of dispatching
9676 -- calls since it is always a class-wide type (cf. Expand_Dispatching_
9679 elsif Ekind
(Func_Id
) = E_Subprogram_Type
then
9680 if Is_Dispatch_Table_Entity
(Func_Id
) then
9681 return Has_BIP_Extra_Formal
(Func_Id
, BIP_Task_Master
);
9683 return Might_Have_Tasks
(Func_Typ
);
9687 raise Program_Error
;
9689 end Needs_BIP_Task_Actuals
;
9691 -----------------------------------
9692 -- Needs_BIP_Finalization_Master --
9693 -----------------------------------
9695 function Needs_BIP_Finalization_Master
(Func_Id
: Entity_Id
) return Boolean
9697 Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
9700 -- A formal giving the finalization master is needed for build-in-place
9701 -- functions whose result type needs finalization or is a tagged type.
9702 -- Tagged primitive build-in-place functions need such a formal because
9703 -- they can be called by a dispatching call, and extensions may require
9704 -- finalization even if the root type doesn't. This means nonprimitive
9705 -- build-in-place functions with tagged results also need it, since such
9706 -- functions can be called via access-to-function types, and those can
9707 -- be used to call primitives, so the formal needs to be passed to all
9708 -- such build-in-place functions, primitive or not.
9710 return not Restriction_Active
(No_Finalization
)
9711 and then (Needs_Finalization
(Typ
) or else Is_Tagged_Type
(Typ
))
9712 and then not Has_Foreign_Convention
(Typ
);
9713 end Needs_BIP_Finalization_Master
;
9715 --------------------------
9716 -- Needs_BIP_Alloc_Form --
9717 --------------------------
9719 function Needs_BIP_Alloc_Form
(Func_Id
: Entity_Id
) return Boolean is
9720 Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
9723 -- A formal giving the allocation method is needed for build-in-place
9724 -- functions whose result type is returned on the secondary stack or
9725 -- is a tagged type. Tagged primitive build-in-place functions need
9726 -- such a formal because they can be called by a dispatching call, and
9727 -- the secondary stack is always used for dispatching-on-result calls.
9728 -- This means nonprimitive build-in-place functions with tagged results
9729 -- also need it, as such functions can be called via access-to-function
9730 -- types, and those can be used to call primitives, so the formal needs
9731 -- to be passed to all such build-in-place functions, primitive or not.
9733 -- We never use build-in-place if the function has foreign convention,
9734 -- but note that it is OK for a build-in-place function to return a
9735 -- type with a foreign convention because the machinery ensures there
9738 return not Restriction_Active
(No_Secondary_Stack
)
9739 and then (Needs_Secondary_Stack
(Typ
) or else Is_Tagged_Type
(Typ
))
9740 and then not Has_Foreign_Convention
(Func_Id
);
9741 end Needs_BIP_Alloc_Form
;
9743 -------------------------------------
9744 -- Replace_Renaming_Declaration_Id --
9745 -------------------------------------
9747 procedure Replace_Renaming_Declaration_Id
9748 (New_Decl
: Node_Id
;
9749 Orig_Decl
: Node_Id
)
9751 New_Id
: constant Entity_Id
:= Defining_Entity
(New_Decl
);
9752 Orig_Id
: constant Entity_Id
:= Defining_Entity
(Orig_Decl
);
9755 Set_Chars
(New_Id
, Chars
(Orig_Id
));
9757 -- Swap next entity links in preparation for exchanging entities
9760 Next_Id
: constant Entity_Id
:= Next_Entity
(New_Id
);
9762 Link_Entities
(New_Id
, Next_Entity
(Orig_Id
));
9763 Link_Entities
(Orig_Id
, Next_Id
);
9766 Set_Homonym
(New_Id
, Homonym
(Orig_Id
));
9767 Exchange_Entities
(New_Id
, Orig_Id
);
9769 -- Preserve source indication of original declaration, so that xref
9770 -- information is properly generated for the right entity.
9772 Preserve_Comes_From_Source
(New_Decl
, Orig_Decl
);
9773 Preserve_Comes_From_Source
(Orig_Id
, Orig_Decl
);
9775 Set_Comes_From_Source
(New_Id
, False);
9777 -- Preserve aliased indication
9779 Set_Is_Aliased
(Orig_Id
, Is_Aliased
(New_Id
));
9780 end Replace_Renaming_Declaration_Id
;
9782 ---------------------------------
9783 -- Rewrite_Function_Call_For_C --
9784 ---------------------------------
9786 procedure Rewrite_Function_Call_For_C
(N
: Node_Id
) is
9787 Orig_Func
: constant Entity_Id
:= Entity
(Name
(N
));
9788 Func_Id
: constant Entity_Id
:= Ultimate_Alias
(Orig_Func
);
9789 Par
: constant Node_Id
:= Parent
(N
);
9790 Proc_Id
: constant Entity_Id
:= Corresponding_Procedure
(Func_Id
);
9791 Loc
: constant Source_Ptr
:= Sloc
(Par
);
9793 Last_Actual
: Node_Id
;
9794 Last_Formal
: Entity_Id
;
9796 -- Start of processing for Rewrite_Function_Call_For_C
9799 -- The actuals may be given by named associations, so the added actual
9800 -- that is the target of the return value of the call must be a named
9801 -- association as well, so we retrieve the name of the generated
9804 Last_Formal
:= First_Formal
(Proc_Id
);
9805 while Present
(Next_Formal
(Last_Formal
)) loop
9806 Next_Formal
(Last_Formal
);
9809 Actuals
:= Parameter_Associations
(N
);
9811 -- The original function may lack parameters
9813 if No
(Actuals
) then
9814 Actuals
:= New_List
;
9817 -- If the function call is the expression of an assignment statement,
9818 -- transform the assignment into a procedure call. Generate:
9820 -- LHS := Func_Call (...);
9822 -- Proc_Call (..., LHS);
9824 -- If function is inherited, a conversion may be necessary.
9826 if Nkind
(Par
) = N_Assignment_Statement
then
9827 Last_Actual
:= Name
(Par
);
9829 if not Comes_From_Source
(Orig_Func
)
9830 and then Etype
(Orig_Func
) /= Etype
(Func_Id
)
9833 Make_Type_Conversion
(Loc
,
9834 New_Occurrence_Of
(Etype
(Func_Id
), Loc
),
9839 Make_Parameter_Association
(Loc
,
9841 Make_Identifier
(Loc
, Chars
(Last_Formal
)),
9842 Explicit_Actual_Parameter
=> Last_Actual
));
9845 Make_Procedure_Call_Statement
(Loc
,
9846 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
9847 Parameter_Associations
=> Actuals
));
9850 -- Otherwise the context is an expression. Generate a temporary and a
9851 -- procedure call to obtain the function result. Generate:
9853 -- ... Func_Call (...) ...
9856 -- Proc_Call (..., Temp);
9861 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
9870 Make_Object_Declaration
(Loc
,
9871 Defining_Identifier
=> Temp_Id
,
9872 Object_Definition
=>
9873 New_Occurrence_Of
(Etype
(Func_Id
), Loc
));
9876 -- Proc_Call (..., Temp);
9879 Make_Parameter_Association
(Loc
,
9881 Make_Identifier
(Loc
, Chars
(Last_Formal
)),
9882 Explicit_Actual_Parameter
=>
9883 New_Occurrence_Of
(Temp_Id
, Loc
)));
9886 Make_Procedure_Call_Statement
(Loc
,
9887 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
9888 Parameter_Associations
=> Actuals
);
9890 Insert_Actions
(Par
, New_List
(Decl
, Call
));
9891 Rewrite
(N
, New_Occurrence_Of
(Temp_Id
, Loc
));
9894 end Rewrite_Function_Call_For_C
;
9896 ------------------------------------
9897 -- Set_Enclosing_Sec_Stack_Return --
9898 ------------------------------------
9900 procedure Set_Enclosing_Sec_Stack_Return
(N
: Node_Id
) is
9904 -- Due to a possible mix of internally generated blocks, source blocks
9905 -- and loops, the scope stack may not be contiguous as all labels are
9906 -- inserted at the top level within the related function. Instead,
9907 -- perform a parent-based traversal and mark all appropriate constructs.
9909 while Present
(P
) loop
9911 -- Mark the label of a source or internally generated block or
9914 if Nkind
(P
) in N_Block_Statement | N_Loop_Statement
then
9915 Set_Sec_Stack_Needed_For_Return
(Entity
(Identifier
(P
)));
9917 -- Mark the enclosing function
9919 elsif Nkind
(P
) = N_Subprogram_Body
then
9920 if Present
(Corresponding_Spec
(P
)) then
9921 Set_Sec_Stack_Needed_For_Return
(Corresponding_Spec
(P
));
9923 Set_Sec_Stack_Needed_For_Return
(Defining_Entity
(P
));
9926 -- Do not go beyond the enclosing function
9933 end Set_Enclosing_Sec_Stack_Return
;
9935 ------------------------------------
9936 -- Unqual_BIP_Iface_Function_Call --
9937 ------------------------------------
9939 function Unqual_BIP_Iface_Function_Call
(Expr
: Node_Id
) return Node_Id
is
9940 Has_Pointer_Displacement
: Boolean := False;
9941 On_Object_Declaration
: Boolean := False;
9942 -- Remember if processing the renaming expressions on recursion we have
9943 -- traversed an object declaration, since we can traverse many object
9944 -- declaration renamings but just one regular object declaration.
9946 function Unqual_BIP_Function_Call
(Expr
: Node_Id
) return Node_Id
;
9947 -- Search for a build-in-place function call skipping any qualification
9948 -- including qualified expressions, type conversions, references, calls
9949 -- to displace the pointer to the object, and renamings. Return Empty if
9950 -- no build-in-place function call is found.
9952 ------------------------------
9953 -- Unqual_BIP_Function_Call --
9954 ------------------------------
9956 function Unqual_BIP_Function_Call
(Expr
: Node_Id
) return Node_Id
is
9958 -- Recurse to handle case of multiple levels of qualification and/or
9961 if Nkind
(Expr
) in N_Qualified_Expression
9963 | N_Unchecked_Type_Conversion
9965 return Unqual_BIP_Function_Call
(Expression
(Expr
));
9967 -- Recurse to handle case of multiple levels of references and
9968 -- explicit dereferences.
9970 elsif Nkind
(Expr
) in N_Attribute_Reference
9971 | N_Explicit_Dereference
9974 return Unqual_BIP_Function_Call
(Prefix
(Expr
));
9976 -- Recurse on object renamings
9978 elsif Nkind
(Expr
) = N_Identifier
9979 and then Present
(Entity
(Expr
))
9980 and then Ekind
(Entity
(Expr
)) in E_Constant | E_Variable
9981 and then Nkind
(Parent
(Entity
(Expr
))) =
9982 N_Object_Renaming_Declaration
9983 and then Present
(Renamed_Object
(Entity
(Expr
)))
9985 return Unqual_BIP_Function_Call
(Renamed_Object
(Entity
(Expr
)));
9987 -- Recurse on the initializing expression of the first reference of
9988 -- an object declaration.
9990 elsif not On_Object_Declaration
9991 and then Nkind
(Expr
) = N_Identifier
9992 and then Present
(Entity
(Expr
))
9993 and then Ekind
(Entity
(Expr
)) in E_Constant | E_Variable
9994 and then Nkind
(Parent
(Entity
(Expr
))) = N_Object_Declaration
9995 and then Present
(Expression
(Parent
(Entity
(Expr
))))
9997 On_Object_Declaration
:= True;
9999 Unqual_BIP_Function_Call
(Expression
(Parent
(Entity
(Expr
))));
10001 -- Recurse to handle calls to displace the pointer to the object to
10002 -- reference a secondary dispatch table.
10004 elsif Nkind
(Expr
) = N_Function_Call
10005 and then Nkind
(Name
(Expr
)) in N_Has_Entity
10006 and then Present
(Entity
(Name
(Expr
)))
10007 and then Is_RTE
(Entity
(Name
(Expr
)), RE_Displace
)
10009 Has_Pointer_Displacement
:= True;
10011 Unqual_BIP_Function_Call
(First
(Parameter_Associations
(Expr
)));
10013 -- Normal case: check if the inner expression is a BIP function call
10014 -- and the pointer to the object is displaced.
10016 elsif Has_Pointer_Displacement
10017 and then Is_Build_In_Place_Function_Call
(Expr
)
10024 end Unqual_BIP_Function_Call
;
10026 -- Start of processing for Unqual_BIP_Iface_Function_Call
10029 if Nkind
(Expr
) = N_Identifier
and then No
(Entity
(Expr
)) then
10031 -- Can happen for X'Elab_Spec in the binder-generated file
10036 return Unqual_BIP_Function_Call
(Expr
);
10037 end Unqual_BIP_Iface_Function_Call
;
10039 -------------------------------
10040 -- Validate_Subprogram_Calls --
10041 -------------------------------
10043 procedure Validate_Subprogram_Calls
(N
: Node_Id
) is
10045 function Process_Node
(Nod
: Node_Id
) return Traverse_Result
;
10046 -- Function to traverse the subtree of N using Traverse_Proc.
10052 function Process_Node
(Nod
: Node_Id
) return Traverse_Result
is
10054 case Nkind
(Nod
) is
10055 when N_Entry_Call_Statement
10056 | N_Procedure_Call_Statement
10060 Call_Node
: Node_Id
renames Nod
;
10064 -- Call using access to subprogram with explicit dereference
10066 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
10067 Subp
:= Etype
(Name
(Call_Node
));
10069 -- Prefix notation calls
10071 elsif Nkind
(Name
(Call_Node
)) = N_Selected_Component
then
10072 Subp
:= Entity
(Selector_Name
(Name
(Call_Node
)));
10074 -- Call to member of entry family, where Name is an indexed
10075 -- component, with the prefix being a selected component
10076 -- giving the task and entry family name, and the index
10077 -- being the entry index.
10079 elsif Nkind
(Name
(Call_Node
)) = N_Indexed_Component
then
10081 Entity
(Selector_Name
(Prefix
(Name
(Call_Node
))));
10086 Subp
:= Entity
(Name
(Call_Node
));
10089 pragma Assert
(Check_BIP_Actuals
(Call_Node
, Subp
));
10092 -- Skip generic bodies
10094 when N_Package_Body
=>
10095 if Ekind
(Unique_Defining_Entity
(Nod
)) = E_Generic_Package
then
10099 when N_Subprogram_Body
=>
10100 if Ekind
(Unique_Defining_Entity
(Nod
)) in E_Generic_Function
10101 | E_Generic_Procedure
10106 -- Nodes we want to ignore
10108 -- Skip calls placed in the full declaration of record types since
10109 -- the call will be performed by their Init Proc; for example,
10110 -- calls initializing default values of discriminants or calls
10111 -- providing the initial value of record type components. Other
10112 -- full type declarations are processed because they may have
10113 -- calls that must be checked. For example:
10115 -- type T is array (1 .. Some_Function_Call (...)) of Some_Type;
10117 -- ??? More work needed here to handle the following case:
10119 -- type Rec is record
10120 -- F : String (1 .. <some complicated expression>);
10123 when N_Full_Type_Declaration
=>
10124 if Is_Record_Type
(Defining_Entity
(Nod
)) then
10128 -- Skip calls placed in subprogram specifications since function
10129 -- calls initializing default parameter values will be processed
10130 -- when the call to the subprogram is found (if the default actual
10131 -- parameter is required), and calls found in aspects will be
10132 -- processed when their corresponding pragma is found, or in the
10133 -- specific case of class-wide pre-/postconditions, when their
10134 -- helpers are found.
10136 when N_Procedure_Specification
10137 | N_Function_Specification
10141 when N_Abstract_Subprogram_Declaration
10142 | N_Aspect_Specification
10146 | N_Enumeration_Representation_Clause
10147 | N_Enumeration_Type_Definition
10148 | N_Function_Instantiation
10149 | N_Freeze_Generic_Entity
10150 | N_Generic_Function_Renaming_Declaration
10151 | N_Generic_Package_Renaming_Declaration
10152 | N_Generic_Procedure_Renaming_Declaration
10153 | N_Generic_Package_Declaration
10154 | N_Generic_Subprogram_Declaration
10155 | N_Itype_Reference
10156 | N_Number_Declaration
10157 | N_Package_Instantiation
10158 | N_Package_Renaming_Declaration
10160 | N_Procedure_Instantiation
10161 | N_Protected_Type_Declaration
10162 | N_Record_Representation_Clause
10163 | N_Validate_Unchecked_Conversion
10164 | N_Variable_Reference_Marker
10165 | N_Use_Package_Clause
10166 | N_Use_Type_Clause
10178 procedure Check_Calls
is new Traverse_Proc
(Process_Node
);
10180 -- Start of processing for Validate_Subprogram_Calls
10183 -- No action required if we are not generating code or compiling sources
10184 -- that have errors.
10186 if Serious_Errors_Detected
> 0
10187 or else Operating_Mode
/= Generate_Code
10193 end Validate_Subprogram_Calls
;
10199 procedure Warn_BIP
(Func_Call
: Node_Id
) is
10201 if Debug_Flag_Underscore_BB
then
10202 Error_Msg_N
("build-in-place function call??", Func_Call
);