1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Checks
; use Checks
;
28 with Contracts
; use Contracts
;
29 with Debug
; use Debug
;
30 with Einfo
; use Einfo
;
31 with Errout
; use Errout
;
32 with Elists
; use Elists
;
33 with Exp_Aggr
; use Exp_Aggr
;
34 with Exp_Atag
; use Exp_Atag
;
35 with Exp_Ch2
; use Exp_Ch2
;
36 with Exp_Ch3
; use Exp_Ch3
;
37 with Exp_Ch7
; use Exp_Ch7
;
38 with Exp_Ch9
; use Exp_Ch9
;
39 with Exp_Dbug
; use Exp_Dbug
;
40 with Exp_Disp
; use Exp_Disp
;
41 with Exp_Dist
; use Exp_Dist
;
42 with Exp_Intr
; use Exp_Intr
;
43 with Exp_Pakd
; use Exp_Pakd
;
44 with Exp_Tss
; use Exp_Tss
;
45 with Exp_Unst
; use Exp_Unst
;
46 with Exp_Util
; use Exp_Util
;
47 with Freeze
; use Freeze
;
48 with Ghost
; use Ghost
;
49 with Inline
; use Inline
;
51 with Namet
; use Namet
;
52 with Nlists
; use Nlists
;
53 with Nmake
; use Nmake
;
55 with Restrict
; use Restrict
;
56 with Rident
; use Rident
;
57 with Rtsfind
; use Rtsfind
;
59 with Sem_Aux
; use Sem_Aux
;
60 with Sem_Ch6
; use Sem_Ch6
;
61 with Sem_Ch8
; use Sem_Ch8
;
62 with Sem_Ch13
; use Sem_Ch13
;
63 with Sem_Dim
; use Sem_Dim
;
64 with Sem_Disp
; use Sem_Disp
;
65 with Sem_Dist
; use Sem_Dist
;
66 with Sem_Eval
; use Sem_Eval
;
67 with Sem_Mech
; use Sem_Mech
;
68 with Sem_Res
; use Sem_Res
;
69 with Sem_SCIL
; use Sem_SCIL
;
70 with Sem_Util
; use Sem_Util
;
71 with Sinfo
; use Sinfo
;
72 with Snames
; use Snames
;
73 with Stand
; use Stand
;
75 with Targparm
; use Targparm
;
76 with Tbuild
; use Tbuild
;
77 with Uintp
; use Uintp
;
78 with Validsw
; use Validsw
;
80 package body Exp_Ch6
is
82 -------------------------------------
83 -- Table for Unnesting Subprograms --
84 -------------------------------------
86 -- When we expand a subprogram body, if it has nested subprograms and if
87 -- we are in Unnest_Subprogram_Mode, then we record the subprogram entity
88 -- and the body in this table, to later be passed to Unnest_Subprogram.
90 -- We need this delaying mechanism, because we have to wait until all
91 -- instantiated bodies have been inserted before doing the unnesting.
93 type Unest_Entry
is record
95 -- Entity for subprogram to be unnested
98 -- Subprogram body to be unnested
101 package Unest_Bodies
is new Table
.Table
(
102 Table_Component_Type
=> Unest_Entry
,
103 Table_Index_Type
=> Nat
,
104 Table_Low_Bound
=> 1,
105 Table_Initial
=> 100,
106 Table_Increment
=> 200,
107 Table_Name
=> "Unest_Bodies");
109 -----------------------
110 -- Local Subprograms --
111 -----------------------
113 procedure Add_Access_Actual_To_Build_In_Place_Call
114 (Function_Call
: Node_Id
;
115 Function_Id
: Entity_Id
;
116 Return_Object
: Node_Id
;
117 Is_Access
: Boolean := False);
118 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
119 -- object name given by Return_Object and add the attribute to the end of
120 -- the actual parameter list associated with the build-in-place function
121 -- call denoted by Function_Call. However, if Is_Access is True, then
122 -- Return_Object is already an access expression, in which case it's passed
123 -- along directly to the build-in-place function. Finally, if Return_Object
124 -- is empty, then pass a null literal as the actual.
126 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
127 (Function_Call
: Node_Id
;
128 Function_Id
: Entity_Id
;
129 Alloc_Form
: BIP_Allocation_Form
:= Unspecified
;
130 Alloc_Form_Exp
: Node_Id
:= Empty
;
131 Pool_Actual
: Node_Id
:= Make_Null
(No_Location
));
132 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
133 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
134 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
135 -- otherwise pass a literal corresponding to the Alloc_Form parameter
136 -- (which must not be Unspecified in that case). Pool_Actual is the
137 -- parameter to pass to BIP_Storage_Pool.
139 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
140 (Func_Call
: Node_Id
;
142 Ptr_Typ
: Entity_Id
:= Empty
;
143 Master_Exp
: Node_Id
:= Empty
);
144 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
145 -- finalization actions, add an actual parameter which is a pointer to the
146 -- finalization master of the caller. If Master_Exp is not Empty, then that
147 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
148 -- will result in an automatic "null" value for the actual.
150 procedure Add_Task_Actuals_To_Build_In_Place_Call
151 (Function_Call
: Node_Id
;
152 Function_Id
: Entity_Id
;
153 Master_Actual
: Node_Id
;
154 Chain
: Node_Id
:= Empty
);
155 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
156 -- contains tasks, add two actual parameters: the master, and a pointer to
157 -- the caller's activation chain. Master_Actual is the actual parameter
158 -- expression to pass for the master. In most cases, this is the current
159 -- master (_master). The two exceptions are: If the function call is the
160 -- initialization expression for an allocator, we pass the master of the
161 -- access type. If the function call is the initialization expression for a
162 -- return object, we pass along the master passed in by the caller. In most
163 -- contexts, the activation chain to pass is the local one, which is
164 -- indicated by No (Chain). However, in an allocator, the caller passes in
165 -- the activation Chain. Note: Master_Actual can be Empty, but only if
166 -- there are no tasks.
168 procedure Check_Overriding_Operation
(Subp
: Entity_Id
);
169 -- Subp is a dispatching operation. Check whether it may override an
170 -- inherited private operation, in which case its DT entry is that of
171 -- the hidden operation, not the one it may have received earlier.
172 -- This must be done before emitting the code to set the corresponding
173 -- DT to the address of the subprogram. The actual placement of Subp in
174 -- the proper place in the list of primitive operations is done in
175 -- Declare_Inherited_Private_Subprograms, which also has to deal with
176 -- implicit operations. This duplication is unavoidable for now???
178 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
);
179 -- This procedure is called only if the subprogram body N, whose spec
180 -- has the given entity Spec, contains a parameterless recursive call.
181 -- It attempts to generate runtime code to detect if this a case of
182 -- infinite recursion.
184 -- The body is scanned to determine dependencies. If the only external
185 -- dependencies are on a small set of scalar variables, then the values
186 -- of these variables are captured on entry to the subprogram, and if
187 -- the values are not changed for the call, we know immediately that
188 -- we have an infinite recursion.
190 procedure Expand_Actuals
(N
: in out Node_Id
; Subp
: Entity_Id
);
191 -- For each actual of an in-out or out parameter which is a numeric
192 -- (view) conversion of the form T (A), where A denotes a variable,
193 -- we insert the declaration:
195 -- Temp : T[ := T (A)];
197 -- prior to the call. Then we replace the actual with a reference to Temp,
198 -- and append the assignment:
200 -- A := TypeA (Temp);
202 -- after the call. Here TypeA is the actual type of variable A. For out
203 -- parameters, the initial declaration has no expression. If A is not an
204 -- entity name, we generate instead:
206 -- Var : TypeA renames A;
207 -- Temp : T := Var; -- omitting expression for out parameter.
209 -- Var := TypeA (Temp);
211 -- For other in-out parameters, we emit the required constraint checks
212 -- before and/or after the call.
214 -- For all parameter modes, actuals that denote components and slices of
215 -- packed arrays are expanded into suitable temporaries.
217 -- For non-scalar objects that are possibly unaligned, add call by copy
218 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
220 -- For OUT and IN OUT parameters, add predicate checks after the call
221 -- based on the predicates of the actual type.
223 -- The parameter N is IN OUT because in some cases, the expansion code
224 -- rewrites the call as an expression actions with the call inside. In
225 -- this case N is reset to point to the inside call so that the caller
226 -- can continue processing of this call.
228 procedure Expand_Ctrl_Function_Call
(N
: Node_Id
);
229 -- N is a function call which returns a controlled object. Transform the
230 -- call into a temporary which retrieves the returned object from the
231 -- secondary stack using 'reference.
233 procedure Expand_Non_Function_Return
(N
: Node_Id
);
234 -- Expand a simple return statement found in a procedure body, entry body,
235 -- accept statement, or an extended return statement. Note that all non-
236 -- function returns are simple return statements.
238 function Expand_Protected_Object_Reference
240 Scop
: Entity_Id
) return Node_Id
;
242 procedure Expand_Protected_Subprogram_Call
246 -- A call to a protected subprogram within the protected object may appear
247 -- as a regular call. The list of actuals must be expanded to contain a
248 -- reference to the object itself, and the call becomes a call to the
249 -- corresponding protected subprogram.
251 function Has_Unconstrained_Access_Discriminants
252 (Subtyp
: Entity_Id
) return Boolean;
253 -- Returns True if the given subtype is unconstrained and has one
254 -- or more access discriminants.
256 procedure Expand_Simple_Function_Return
(N
: Node_Id
);
257 -- Expand simple return from function. In the case where we are returning
258 -- from a function body this is called by Expand_N_Simple_Return_Statement.
260 procedure Rewrite_Function_Call_For_C
(N
: Node_Id
);
261 -- When generating C code, replace a call to a function that returns an
262 -- array into the generated procedure with an additional out parameter.
264 procedure Set_Enclosing_Sec_Stack_Return
(N
: Node_Id
);
265 -- N is a return statement for a function that returns its result on the
266 -- secondary stack. This sets the Sec_Stack_Needed_For_Return flag on the
267 -- function and all blocks and loops that the return statement is jumping
268 -- out of. This ensures that the secondary stack is not released; otherwise
269 -- the function result would be reclaimed before returning to the caller.
271 ----------------------------------------------
272 -- Add_Access_Actual_To_Build_In_Place_Call --
273 ----------------------------------------------
275 procedure Add_Access_Actual_To_Build_In_Place_Call
276 (Function_Call
: Node_Id
;
277 Function_Id
: Entity_Id
;
278 Return_Object
: Node_Id
;
279 Is_Access
: Boolean := False)
281 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
282 Obj_Address
: Node_Id
;
283 Obj_Acc_Formal
: Entity_Id
;
286 -- Locate the implicit access parameter in the called function
288 Obj_Acc_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Object_Access
);
290 -- If no return object is provided, then pass null
292 if not Present
(Return_Object
) then
293 Obj_Address
:= Make_Null
(Loc
);
294 Set_Parent
(Obj_Address
, Function_Call
);
296 -- If Return_Object is already an expression of an access type, then use
297 -- it directly, since it must be an access value denoting the return
298 -- object, and couldn't possibly be the return object itself.
301 Obj_Address
:= Return_Object
;
302 Set_Parent
(Obj_Address
, Function_Call
);
304 -- Apply Unrestricted_Access to caller's return object
308 Make_Attribute_Reference
(Loc
,
309 Prefix
=> Return_Object
,
310 Attribute_Name
=> Name_Unrestricted_Access
);
312 Set_Parent
(Return_Object
, Obj_Address
);
313 Set_Parent
(Obj_Address
, Function_Call
);
316 Analyze_And_Resolve
(Obj_Address
, Etype
(Obj_Acc_Formal
));
318 -- Build the parameter association for the new actual and add it to the
319 -- end of the function's actuals.
321 Add_Extra_Actual_To_Call
(Function_Call
, Obj_Acc_Formal
, Obj_Address
);
322 end Add_Access_Actual_To_Build_In_Place_Call
;
324 ------------------------------------------------------
325 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
326 ------------------------------------------------------
328 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
329 (Function_Call
: Node_Id
;
330 Function_Id
: Entity_Id
;
331 Alloc_Form
: BIP_Allocation_Form
:= Unspecified
;
332 Alloc_Form_Exp
: Node_Id
:= Empty
;
333 Pool_Actual
: Node_Id
:= Make_Null
(No_Location
))
335 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
336 Alloc_Form_Actual
: Node_Id
;
337 Alloc_Form_Formal
: Node_Id
;
338 Pool_Formal
: Node_Id
;
341 -- The allocation form generally doesn't need to be passed in the case
342 -- of a constrained result subtype, since normally the caller performs
343 -- the allocation in that case. However this formal is still needed in
344 -- the case where the function has a tagged result, because generally
345 -- such functions can be called in a dispatching context and such calls
346 -- must be handled like calls to class-wide functions.
348 if Is_Constrained
(Underlying_Type
(Etype
(Function_Id
)))
349 and then not Is_Tagged_Type
(Underlying_Type
(Etype
(Function_Id
)))
354 -- Locate the implicit allocation form parameter in the called function.
355 -- Maybe it would be better for each implicit formal of a build-in-place
356 -- function to have a flag or a Uint attribute to identify it. ???
358 Alloc_Form_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Alloc_Form
);
360 if Present
(Alloc_Form_Exp
) then
361 pragma Assert
(Alloc_Form
= Unspecified
);
363 Alloc_Form_Actual
:= Alloc_Form_Exp
;
366 pragma Assert
(Alloc_Form
/= Unspecified
);
369 Make_Integer_Literal
(Loc
,
370 Intval
=> UI_From_Int
(BIP_Allocation_Form
'Pos (Alloc_Form
)));
373 Analyze_And_Resolve
(Alloc_Form_Actual
, Etype
(Alloc_Form_Formal
));
375 -- Build the parameter association for the new actual and add it to the
376 -- end of the function's actuals.
378 Add_Extra_Actual_To_Call
379 (Function_Call
, Alloc_Form_Formal
, Alloc_Form_Actual
);
381 -- Pass the Storage_Pool parameter. This parameter is omitted on
382 -- ZFP as those targets do not support pools.
384 if RTE_Available
(RE_Root_Storage_Pool_Ptr
) then
385 Pool_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Storage_Pool
);
386 Analyze_And_Resolve
(Pool_Actual
, Etype
(Pool_Formal
));
387 Add_Extra_Actual_To_Call
388 (Function_Call
, Pool_Formal
, Pool_Actual
);
390 end Add_Unconstrained_Actuals_To_Build_In_Place_Call
;
392 -----------------------------------------------------------
393 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
394 -----------------------------------------------------------
396 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
397 (Func_Call
: Node_Id
;
399 Ptr_Typ
: Entity_Id
:= Empty
;
400 Master_Exp
: Node_Id
:= Empty
)
403 if not Needs_BIP_Finalization_Master
(Func_Id
) then
408 Formal
: constant Entity_Id
:=
409 Build_In_Place_Formal
(Func_Id
, BIP_Finalization_Master
);
410 Loc
: constant Source_Ptr
:= Sloc
(Func_Call
);
413 Desig_Typ
: Entity_Id
;
416 -- If there is a finalization master actual, such as the implicit
417 -- finalization master of an enclosing build-in-place function,
418 -- then this must be added as an extra actual of the call.
420 if Present
(Master_Exp
) then
421 Actual
:= Master_Exp
;
423 -- Case where the context does not require an actual master
425 elsif No
(Ptr_Typ
) then
426 Actual
:= Make_Null
(Loc
);
429 Desig_Typ
:= Directly_Designated_Type
(Ptr_Typ
);
431 -- Check for a library-level access type whose designated type has
432 -- supressed finalization. Such an access types lack a master.
433 -- Pass a null actual to the callee in order to signal a missing
436 if Is_Library_Level_Entity
(Ptr_Typ
)
437 and then Finalize_Storage_Only
(Desig_Typ
)
439 Actual
:= Make_Null
(Loc
);
441 -- Types in need of finalization actions
443 elsif Needs_Finalization
(Desig_Typ
) then
445 -- The general mechanism of creating finalization masters for
446 -- anonymous access types is disabled by default, otherwise
447 -- finalization masters will pop all over the place. Such types
448 -- use context-specific masters.
450 if Ekind
(Ptr_Typ
) = E_Anonymous_Access_Type
451 and then No
(Finalization_Master
(Ptr_Typ
))
453 Build_Finalization_Master
455 For_Anonymous
=> True,
456 Context_Scope
=> Scope
(Ptr_Typ
),
457 Insertion_Node
=> Associated_Node_For_Itype
(Ptr_Typ
));
460 -- Access-to-controlled types should always have a master
462 pragma Assert
(Present
(Finalization_Master
(Ptr_Typ
)));
465 Make_Attribute_Reference
(Loc
,
467 New_Occurrence_Of
(Finalization_Master
(Ptr_Typ
), Loc
),
468 Attribute_Name
=> Name_Unrestricted_Access
);
473 Actual
:= Make_Null
(Loc
);
477 Analyze_And_Resolve
(Actual
, Etype
(Formal
));
479 -- Build the parameter association for the new actual and add it to
480 -- the end of the function's actuals.
482 Add_Extra_Actual_To_Call
(Func_Call
, Formal
, Actual
);
484 end Add_Finalization_Master_Actual_To_Build_In_Place_Call
;
486 ------------------------------
487 -- Add_Extra_Actual_To_Call --
488 ------------------------------
490 procedure Add_Extra_Actual_To_Call
491 (Subprogram_Call
: Node_Id
;
492 Extra_Formal
: Entity_Id
;
493 Extra_Actual
: Node_Id
)
495 Loc
: constant Source_Ptr
:= Sloc
(Subprogram_Call
);
496 Param_Assoc
: Node_Id
;
500 Make_Parameter_Association
(Loc
,
501 Selector_Name
=> New_Occurrence_Of
(Extra_Formal
, Loc
),
502 Explicit_Actual_Parameter
=> Extra_Actual
);
504 Set_Parent
(Param_Assoc
, Subprogram_Call
);
505 Set_Parent
(Extra_Actual
, Param_Assoc
);
507 if Present
(Parameter_Associations
(Subprogram_Call
)) then
508 if Nkind
(Last
(Parameter_Associations
(Subprogram_Call
))) =
509 N_Parameter_Association
512 -- Find last named actual, and append
517 L
:= First_Actual
(Subprogram_Call
);
518 while Present
(L
) loop
519 if No
(Next_Actual
(L
)) then
520 Set_Next_Named_Actual
(Parent
(L
), Extra_Actual
);
528 Set_First_Named_Actual
(Subprogram_Call
, Extra_Actual
);
531 Append
(Param_Assoc
, To
=> Parameter_Associations
(Subprogram_Call
));
534 Set_Parameter_Associations
(Subprogram_Call
, New_List
(Param_Assoc
));
535 Set_First_Named_Actual
(Subprogram_Call
, Extra_Actual
);
537 end Add_Extra_Actual_To_Call
;
539 ---------------------------------------------
540 -- Add_Task_Actuals_To_Build_In_Place_Call --
541 ---------------------------------------------
543 procedure Add_Task_Actuals_To_Build_In_Place_Call
544 (Function_Call
: Node_Id
;
545 Function_Id
: Entity_Id
;
546 Master_Actual
: Node_Id
;
547 Chain
: Node_Id
:= Empty
)
549 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
550 Result_Subt
: constant Entity_Id
:=
551 Available_View
(Etype
(Function_Id
));
553 Chain_Actual
: Node_Id
;
554 Chain_Formal
: Node_Id
;
555 Master_Formal
: Node_Id
;
558 -- No such extra parameters are needed if there are no tasks
560 if not Has_Task
(Result_Subt
) then
564 Actual
:= Master_Actual
;
566 -- Use a dummy _master actual in case of No_Task_Hierarchy
568 if Restriction_Active
(No_Task_Hierarchy
) then
569 Actual
:= New_Occurrence_Of
(RTE
(RE_Library_Task_Level
), Loc
);
571 -- In the case where we use the master associated with an access type,
572 -- the actual is an entity and requires an explicit reference.
574 elsif Nkind
(Actual
) = N_Defining_Identifier
then
575 Actual
:= New_Occurrence_Of
(Actual
, Loc
);
578 -- Locate the implicit master parameter in the called function
580 Master_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Task_Master
);
581 Analyze_And_Resolve
(Actual
, Etype
(Master_Formal
));
583 -- Build the parameter association for the new actual and add it to the
584 -- end of the function's actuals.
586 Add_Extra_Actual_To_Call
(Function_Call
, Master_Formal
, Actual
);
588 -- Locate the implicit activation chain parameter in the called function
591 Build_In_Place_Formal
(Function_Id
, BIP_Activation_Chain
);
593 -- Create the actual which is a pointer to the current activation chain
597 Make_Attribute_Reference
(Loc
,
598 Prefix
=> Make_Identifier
(Loc
, Name_uChain
),
599 Attribute_Name
=> Name_Unrestricted_Access
);
601 -- Allocator case; make a reference to the Chain passed in by the caller
605 Make_Attribute_Reference
(Loc
,
606 Prefix
=> New_Occurrence_Of
(Chain
, Loc
),
607 Attribute_Name
=> Name_Unrestricted_Access
);
610 Analyze_And_Resolve
(Chain_Actual
, Etype
(Chain_Formal
));
612 -- Build the parameter association for the new actual and add it to the
613 -- end of the function's actuals.
615 Add_Extra_Actual_To_Call
(Function_Call
, Chain_Formal
, Chain_Actual
);
616 end Add_Task_Actuals_To_Build_In_Place_Call
;
618 -----------------------
619 -- BIP_Formal_Suffix --
620 -----------------------
622 function BIP_Formal_Suffix
(Kind
: BIP_Formal_Kind
) return String is
625 when BIP_Alloc_Form
=>
627 when BIP_Storage_Pool
=>
628 return "BIPstoragepool";
629 when BIP_Finalization_Master
=>
630 return "BIPfinalizationmaster";
631 when BIP_Task_Master
=>
632 return "BIPtaskmaster";
633 when BIP_Activation_Chain
=>
634 return "BIPactivationchain";
635 when BIP_Object_Access
=>
638 end BIP_Formal_Suffix
;
640 ---------------------------
641 -- Build_In_Place_Formal --
642 ---------------------------
644 function Build_In_Place_Formal
646 Kind
: BIP_Formal_Kind
) return Entity_Id
648 Formal_Name
: constant Name_Id
:=
650 (Chars
(Func
), BIP_Formal_Suffix
(Kind
));
651 Extra_Formal
: Entity_Id
:= Extra_Formals
(Func
);
654 -- Maybe it would be better for each implicit formal of a build-in-place
655 -- function to have a flag or a Uint attribute to identify it. ???
657 -- The return type in the function declaration may have been a limited
658 -- view, and the extra formals for the function were not generated at
659 -- that point. At the point of call the full view must be available and
660 -- the extra formals can be created.
662 if No
(Extra_Formal
) then
663 Create_Extra_Formals
(Func
);
664 Extra_Formal
:= Extra_Formals
(Func
);
668 pragma Assert
(Present
(Extra_Formal
));
669 exit when Chars
(Extra_Formal
) = Formal_Name
;
671 Next_Formal_With_Extras
(Extra_Formal
);
675 end Build_In_Place_Formal
;
677 -------------------------------
678 -- Build_Procedure_Body_Form --
679 -------------------------------
681 function Build_Procedure_Body_Form
682 (Func_Id
: Entity_Id
;
683 Func_Body
: Node_Id
) return Node_Id
685 Loc
: constant Source_Ptr
:= Sloc
(Func_Body
);
687 Proc_Decl
: constant Node_Id
:=
688 Next
(Unit_Declaration_Node
(Func_Id
));
689 -- It is assumed that the next node following the declaration of the
690 -- corresponding subprogram spec is the declaration of the procedure
693 Proc_Id
: constant Entity_Id
:= Defining_Entity
(Proc_Decl
);
695 procedure Replace_Returns
(Param_Id
: Entity_Id
; Stmts
: List_Id
);
696 -- Replace each return statement found in the list Stmts with an
697 -- assignment of the return expression to parameter Param_Id.
699 ---------------------
700 -- Replace_Returns --
701 ---------------------
703 procedure Replace_Returns
(Param_Id
: Entity_Id
; Stmts
: List_Id
) is
707 Stmt
:= First
(Stmts
);
708 while Present
(Stmt
) loop
709 if Nkind
(Stmt
) = N_Block_Statement
then
710 Replace_Returns
(Param_Id
, Statements
(Stmt
));
712 elsif Nkind
(Stmt
) = N_Case_Statement
then
716 Alt
:= First
(Alternatives
(Stmt
));
717 while Present
(Alt
) loop
718 Replace_Returns
(Param_Id
, Statements
(Alt
));
723 elsif Nkind
(Stmt
) = N_If_Statement
then
724 Replace_Returns
(Param_Id
, Then_Statements
(Stmt
));
725 Replace_Returns
(Param_Id
, Else_Statements
(Stmt
));
730 Part
:= First
(Elsif_Parts
(Stmt
));
731 while Present
(Part
) loop
732 Replace_Returns
(Part
, Then_Statements
(Part
));
737 elsif Nkind
(Stmt
) = N_Loop_Statement
then
738 Replace_Returns
(Param_Id
, Statements
(Stmt
));
740 elsif Nkind
(Stmt
) = N_Simple_Return_Statement
then
747 Make_Assignment_Statement
(Sloc
(Stmt
),
748 Name
=> New_Occurrence_Of
(Param_Id
, Loc
),
749 Expression
=> Relocate_Node
(Expression
(Stmt
))));
751 Insert_After
(Stmt
, Make_Simple_Return_Statement
(Loc
));
753 -- Skip the added return
767 -- Start of processing for Build_Procedure_Body_Form
770 -- This routine replaces the original function body:
772 -- function F (...) return Array_Typ is
778 -- with the following:
780 -- procedure P (..., Result : out Array_Typ) is
783 -- Result := Something;
787 Statements
(Handled_Statement_Sequence
(Func_Body
));
788 Replace_Returns
(Last_Entity
(Proc_Id
), Stmts
);
791 Make_Subprogram_Body
(Loc
,
793 Copy_Subprogram_Spec
(Specification
(Proc_Decl
)),
794 Declarations
=> Declarations
(Func_Body
),
795 Handled_Statement_Sequence
=>
796 Make_Handled_Sequence_Of_Statements
(Loc
,
797 Statements
=> Stmts
));
800 end Build_Procedure_Body_Form
;
802 --------------------------------
803 -- Check_Overriding_Operation --
804 --------------------------------
806 procedure Check_Overriding_Operation
(Subp
: Entity_Id
) is
807 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
808 Op_List
: constant Elist_Id
:= Primitive_Operations
(Typ
);
814 if Is_Derived_Type
(Typ
)
815 and then not Is_Private_Type
(Typ
)
816 and then In_Open_Scopes
(Scope
(Etype
(Typ
)))
817 and then Is_Base_Type
(Typ
)
819 -- Subp overrides an inherited private operation if there is an
820 -- inherited operation with a different name than Subp (see
821 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
822 -- same name as Subp.
824 Op_Elmt
:= First_Elmt
(Op_List
);
825 while Present
(Op_Elmt
) loop
826 Prim_Op
:= Node
(Op_Elmt
);
827 Par_Op
:= Alias
(Prim_Op
);
830 and then not Comes_From_Source
(Prim_Op
)
831 and then Chars
(Prim_Op
) /= Chars
(Par_Op
)
832 and then Chars
(Par_Op
) = Chars
(Subp
)
833 and then Is_Hidden
(Par_Op
)
834 and then Type_Conformant
(Prim_Op
, Subp
)
836 Set_DT_Position_Value
(Subp
, DT_Position
(Prim_Op
));
842 end Check_Overriding_Operation
;
844 -------------------------------
845 -- Detect_Infinite_Recursion --
846 -------------------------------
848 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
) is
849 Loc
: constant Source_Ptr
:= Sloc
(N
);
851 Var_List
: constant Elist_Id
:= New_Elmt_List
;
852 -- List of globals referenced by body of procedure
854 Call_List
: constant Elist_Id
:= New_Elmt_List
;
855 -- List of recursive calls in body of procedure
857 Shad_List
: constant Elist_Id
:= New_Elmt_List
;
858 -- List of entity id's for entities created to capture the value of
859 -- referenced globals on entry to the procedure.
861 Scop
: constant Uint
:= Scope_Depth
(Spec
);
862 -- This is used to record the scope depth of the current procedure, so
863 -- that we can identify global references.
865 Max_Vars
: constant := 4;
866 -- Do not test more than four global variables
868 Count_Vars
: Natural := 0;
869 -- Count variables found so far
881 function Process
(Nod
: Node_Id
) return Traverse_Result
;
882 -- Function to traverse the subprogram body (using Traverse_Func)
888 function Process
(Nod
: Node_Id
) return Traverse_Result
is
892 if Nkind
(Nod
) = N_Procedure_Call_Statement
then
894 -- Case of one of the detected recursive calls
896 if Is_Entity_Name
(Name
(Nod
))
897 and then Has_Recursive_Call
(Entity
(Name
(Nod
)))
898 and then Entity
(Name
(Nod
)) = Spec
900 Append_Elmt
(Nod
, Call_List
);
903 -- Any other procedure call may have side effects
909 -- A call to a pure function can always be ignored
911 elsif Nkind
(Nod
) = N_Function_Call
912 and then Is_Entity_Name
(Name
(Nod
))
913 and then Is_Pure
(Entity
(Name
(Nod
)))
917 -- Case of an identifier reference
919 elsif Nkind
(Nod
) = N_Identifier
then
922 -- If no entity, then ignore the reference
924 -- Not clear why this can happen. To investigate, remove this
925 -- test and look at the crash that occurs here in 3401-004 ???
930 -- Ignore entities with no Scope, again not clear how this
931 -- can happen, to investigate, look at 4108-008 ???
933 elsif No
(Scope
(Ent
)) then
936 -- Ignore the reference if not to a more global object
938 elsif Scope_Depth
(Scope
(Ent
)) >= Scop
then
941 -- References to types, exceptions and constants are always OK
944 or else Ekind
(Ent
) = E_Exception
945 or else Ekind
(Ent
) = E_Constant
949 -- If other than a non-volatile scalar variable, we have some
950 -- kind of global reference (e.g. to a function) that we cannot
951 -- deal with so we forget the attempt.
953 elsif Ekind
(Ent
) /= E_Variable
954 or else not Is_Scalar_Type
(Etype
(Ent
))
955 or else Treat_As_Volatile
(Ent
)
959 -- Otherwise we have a reference to a global scalar
962 -- Loop through global entities already detected
964 Elm
:= First_Elmt
(Var_List
);
966 -- If not detected before, record this new global reference
969 Count_Vars
:= Count_Vars
+ 1;
971 if Count_Vars
<= Max_Vars
then
972 Append_Elmt
(Entity
(Nod
), Var_List
);
979 -- If recorded before, ignore
981 elsif Node
(Elm
) = Entity
(Nod
) then
984 -- Otherwise keep looking
994 -- For all other node kinds, recursively visit syntactic children
1001 function Traverse_Body
is new Traverse_Func
(Process
);
1003 -- Start of processing for Detect_Infinite_Recursion
1006 -- Do not attempt detection in No_Implicit_Conditional mode, since we
1007 -- won't be able to generate the code to handle the recursion in any
1010 if Restriction_Active
(No_Implicit_Conditionals
) then
1014 -- Otherwise do traversal and quit if we get abandon signal
1016 if Traverse_Body
(N
) = Abandon
then
1019 -- We must have a call, since Has_Recursive_Call was set. If not just
1020 -- ignore (this is only an error check, so if we have a funny situation,
1021 -- due to bugs or errors, we do not want to bomb).
1023 elsif Is_Empty_Elmt_List
(Call_List
) then
1027 -- Here is the case where we detect recursion at compile time
1029 -- Push our current scope for analyzing the declarations and code that
1030 -- we will insert for the checking.
1034 -- This loop builds temporary variables for each of the referenced
1035 -- globals, so that at the end of the loop the list Shad_List contains
1036 -- these temporaries in one-to-one correspondence with the elements in
1040 Elm
:= First_Elmt
(Var_List
);
1041 while Present
(Elm
) loop
1043 Ent
:= Make_Temporary
(Loc
, 'S');
1044 Append_Elmt
(Ent
, Shad_List
);
1046 -- Insert a declaration for this temporary at the start of the
1047 -- declarations for the procedure. The temporaries are declared as
1048 -- constant objects initialized to the current values of the
1049 -- corresponding temporaries.
1052 Make_Object_Declaration
(Loc
,
1053 Defining_Identifier
=> Ent
,
1054 Object_Definition
=> New_Occurrence_Of
(Etype
(Var
), Loc
),
1055 Constant_Present
=> True,
1056 Expression
=> New_Occurrence_Of
(Var
, Loc
));
1059 Prepend
(Decl
, Declarations
(N
));
1061 Insert_After
(Last
, Decl
);
1069 -- Loop through calls
1071 Call
:= First_Elmt
(Call_List
);
1072 while Present
(Call
) loop
1074 -- Build a predicate expression of the form
1077 -- and then global1 = temp1
1078 -- and then global2 = temp2
1081 -- This predicate determines if any of the global values
1082 -- referenced by the procedure have changed since the
1083 -- current call, if not an infinite recursion is assured.
1085 Test
:= New_Occurrence_Of
(Standard_True
, Loc
);
1087 Elm1
:= First_Elmt
(Var_List
);
1088 Elm2
:= First_Elmt
(Shad_List
);
1089 while Present
(Elm1
) loop
1095 Left_Opnd
=> New_Occurrence_Of
(Node
(Elm1
), Loc
),
1096 Right_Opnd
=> New_Occurrence_Of
(Node
(Elm2
), Loc
)));
1102 -- Now we replace the call with the sequence
1104 -- if no-changes (see above) then
1105 -- raise Storage_Error;
1110 Rewrite
(Node
(Call
),
1111 Make_If_Statement
(Loc
,
1113 Then_Statements
=> New_List
(
1114 Make_Raise_Storage_Error
(Loc
,
1115 Reason
=> SE_Infinite_Recursion
)),
1117 Else_Statements
=> New_List
(
1118 Relocate_Node
(Node
(Call
)))));
1120 Analyze
(Node
(Call
));
1125 -- Remove temporary scope stack entry used for analysis
1128 end Detect_Infinite_Recursion
;
1130 --------------------
1131 -- Expand_Actuals --
1132 --------------------
1134 procedure Expand_Actuals
(N
: in out Node_Id
; Subp
: Entity_Id
) is
1135 Loc
: constant Source_Ptr
:= Sloc
(N
);
1139 Post_Call
: List_Id
;
1140 E_Actual
: Entity_Id
;
1141 E_Formal
: Entity_Id
;
1143 procedure Add_Call_By_Copy_Code
;
1144 -- For cases where the parameter must be passed by copy, this routine
1145 -- generates a temporary variable into which the actual is copied and
1146 -- then passes this as the parameter. For an OUT or IN OUT parameter,
1147 -- an assignment is also generated to copy the result back. The call
1148 -- also takes care of any constraint checks required for the type
1149 -- conversion case (on both the way in and the way out).
1151 procedure Add_Simple_Call_By_Copy_Code
;
1152 -- This is similar to the above, but is used in cases where we know
1153 -- that all that is needed is to simply create a temporary and copy
1154 -- the value in and out of the temporary.
1156 procedure Check_Fortran_Logical
;
1157 -- A value of type Logical that is passed through a formal parameter
1158 -- must be normalized because .TRUE. usually does not have the same
1159 -- representation as True. We assume that .FALSE. = False = 0.
1160 -- What about functions that return a logical type ???
1162 function Is_Legal_Copy
return Boolean;
1163 -- Check that an actual can be copied before generating the temporary
1164 -- to be used in the call. If the actual is of a by_reference type then
1165 -- the program is illegal (this can only happen in the presence of
1166 -- rep. clauses that force an incorrect alignment). If the formal is
1167 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
1168 -- the effect that this might lead to unaligned arguments.
1170 function Make_Var
(Actual
: Node_Id
) return Entity_Id
;
1171 -- Returns an entity that refers to the given actual parameter, Actual
1172 -- (not including any type conversion). If Actual is an entity name,
1173 -- then this entity is returned unchanged, otherwise a renaming is
1174 -- created to provide an entity for the actual.
1176 procedure Reset_Packed_Prefix
;
1177 -- The expansion of a packed array component reference is delayed in
1178 -- the context of a call. Now we need to complete the expansion, so we
1179 -- unmark the analyzed bits in all prefixes.
1181 ---------------------------
1182 -- Add_Call_By_Copy_Code --
1183 ---------------------------
1185 procedure Add_Call_By_Copy_Code
is
1191 F_Typ
: constant Entity_Id
:= Etype
(Formal
);
1196 if not Is_Legal_Copy
then
1200 Temp
:= Make_Temporary
(Loc
, 'T', Actual
);
1202 -- Use formal type for temp, unless formal type is an unconstrained
1203 -- array, in which case we don't have to worry about bounds checks,
1204 -- and we use the actual type, since that has appropriate bounds.
1206 if Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
1207 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1209 Indic
:= New_Occurrence_Of
(Etype
(Formal
), Loc
);
1212 if Nkind
(Actual
) = N_Type_Conversion
then
1213 V_Typ
:= Etype
(Expression
(Actual
));
1215 -- If the formal is an (in-)out parameter, capture the name
1216 -- of the variable in order to build the post-call assignment.
1218 Var
:= Make_Var
(Expression
(Actual
));
1220 Crep
:= not Same_Representation
1221 (F_Typ
, Etype
(Expression
(Actual
)));
1224 V_Typ
:= Etype
(Actual
);
1225 Var
:= Make_Var
(Actual
);
1229 -- Setup initialization for case of in out parameter, or an out
1230 -- parameter where the formal is an unconstrained array (in the
1231 -- latter case, we have to pass in an object with bounds).
1233 -- If this is an out parameter, the initial copy is wasteful, so as
1234 -- an optimization for the one-dimensional case we extract the
1235 -- bounds of the actual and build an uninitialized temporary of the
1238 if Ekind
(Formal
) = E_In_Out_Parameter
1239 or else (Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
))
1241 if Nkind
(Actual
) = N_Type_Conversion
then
1242 if Conversion_OK
(Actual
) then
1243 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1245 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1248 elsif Ekind
(Formal
) = E_Out_Parameter
1249 and then Is_Array_Type
(F_Typ
)
1250 and then Number_Dimensions
(F_Typ
) = 1
1251 and then not Has_Non_Null_Base_Init_Proc
(F_Typ
)
1253 -- Actual is a one-dimensional array or slice, and the type
1254 -- requires no initialization. Create a temporary of the
1255 -- right size, but do not copy actual into it (optimization).
1259 Make_Subtype_Indication
(Loc
,
1260 Subtype_Mark
=> New_Occurrence_Of
(F_Typ
, Loc
),
1262 Make_Index_Or_Discriminant_Constraint
(Loc
,
1263 Constraints
=> New_List
(
1266 Make_Attribute_Reference
(Loc
,
1267 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
1268 Attribute_Name
=> Name_First
),
1270 Make_Attribute_Reference
(Loc
,
1271 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
1272 Attribute_Name
=> Name_Last
)))));
1275 Init
:= New_Occurrence_Of
(Var
, Loc
);
1278 -- An initialization is created for packed conversions as
1279 -- actuals for out parameters to enable Make_Object_Declaration
1280 -- to determine the proper subtype for N_Node. Note that this
1281 -- is wasteful because the extra copying on the call side is
1282 -- not required for such out parameters. ???
1284 elsif Ekind
(Formal
) = E_Out_Parameter
1285 and then Nkind
(Actual
) = N_Type_Conversion
1286 and then (Is_Bit_Packed_Array
(F_Typ
)
1288 Is_Bit_Packed_Array
(Etype
(Expression
(Actual
))))
1290 if Conversion_OK
(Actual
) then
1291 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1293 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1296 elsif Ekind
(Formal
) = E_In_Parameter
then
1298 -- Handle the case in which the actual is a type conversion
1300 if Nkind
(Actual
) = N_Type_Conversion
then
1301 if Conversion_OK
(Actual
) then
1302 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1304 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1307 Init
:= New_Occurrence_Of
(Var
, Loc
);
1315 Make_Object_Declaration
(Loc
,
1316 Defining_Identifier
=> Temp
,
1317 Object_Definition
=> Indic
,
1318 Expression
=> Init
);
1319 Set_Assignment_OK
(N_Node
);
1320 Insert_Action
(N
, N_Node
);
1322 -- Now, normally the deal here is that we use the defining
1323 -- identifier created by that object declaration. There is
1324 -- one exception to this. In the change of representation case
1325 -- the above declaration will end up looking like:
1327 -- temp : type := identifier;
1329 -- And in this case we might as well use the identifier directly
1330 -- and eliminate the temporary. Note that the analysis of the
1331 -- declaration was not a waste of time in that case, since it is
1332 -- what generated the necessary change of representation code. If
1333 -- the change of representation introduced additional code, as in
1334 -- a fixed-integer conversion, the expression is not an identifier
1335 -- and must be kept.
1338 and then Present
(Expression
(N_Node
))
1339 and then Is_Entity_Name
(Expression
(N_Node
))
1341 Temp
:= Entity
(Expression
(N_Node
));
1342 Rewrite
(N_Node
, Make_Null_Statement
(Loc
));
1345 -- For IN parameter, all we do is to replace the actual
1347 if Ekind
(Formal
) = E_In_Parameter
then
1348 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
1351 -- Processing for OUT or IN OUT parameter
1354 -- Kill current value indications for the temporary variable we
1355 -- created, since we just passed it as an OUT parameter.
1357 Kill_Current_Values
(Temp
);
1358 Set_Is_Known_Valid
(Temp
, False);
1360 -- If type conversion, use reverse conversion on exit
1362 if Nkind
(Actual
) = N_Type_Conversion
then
1363 if Conversion_OK
(Actual
) then
1364 Expr
:= OK_Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
1366 Expr
:= Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
1369 Expr
:= New_Occurrence_Of
(Temp
, Loc
);
1372 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
1375 -- If the actual is a conversion of a packed reference, it may
1376 -- already have been expanded by Remove_Side_Effects, and the
1377 -- resulting variable is a temporary which does not designate
1378 -- the proper out-parameter, which may not be addressable. In
1379 -- that case, generate an assignment to the original expression
1380 -- (before expansion of the packed reference) so that the proper
1381 -- expansion of assignment to a packed component can take place.
1388 if Is_Renaming_Of_Object
(Var
)
1389 and then Nkind
(Renamed_Object
(Var
)) = N_Selected_Component
1390 and then Nkind
(Original_Node
(Prefix
(Renamed_Object
(Var
))))
1391 = N_Indexed_Component
1393 Has_Non_Standard_Rep
(Etype
(Prefix
(Renamed_Object
(Var
))))
1395 Obj
:= Renamed_Object
(Var
);
1397 Make_Selected_Component
(Loc
,
1399 New_Copy_Tree
(Original_Node
(Prefix
(Obj
))),
1400 Selector_Name
=> New_Copy
(Selector_Name
(Obj
)));
1401 Reset_Analyzed_Flags
(Lhs
);
1404 Lhs
:= New_Occurrence_Of
(Var
, Loc
);
1407 Set_Assignment_OK
(Lhs
);
1409 if Is_Access_Type
(E_Formal
)
1410 and then Is_Entity_Name
(Lhs
)
1412 Present
(Effective_Extra_Accessibility
(Entity
(Lhs
)))
1414 -- Copyback target is an Ada 2012 stand-alone object of an
1415 -- anonymous access type.
1417 pragma Assert
(Ada_Version
>= Ada_2012
);
1419 if Type_Access_Level
(E_Formal
) >
1420 Object_Access_Level
(Lhs
)
1422 Append_To
(Post_Call
,
1423 Make_Raise_Program_Error
(Loc
,
1424 Reason
=> PE_Accessibility_Check_Failed
));
1427 Append_To
(Post_Call
,
1428 Make_Assignment_Statement
(Loc
,
1430 Expression
=> Expr
));
1432 -- We would like to somehow suppress generation of the
1433 -- extra_accessibility assignment generated by the expansion
1434 -- of the above assignment statement. It's not a correctness
1435 -- issue because the following assignment renders it dead,
1436 -- but generating back-to-back assignments to the same
1437 -- target is undesirable. ???
1439 Append_To
(Post_Call
,
1440 Make_Assignment_Statement
(Loc
,
1441 Name
=> New_Occurrence_Of
(
1442 Effective_Extra_Accessibility
(Entity
(Lhs
)), Loc
),
1443 Expression
=> Make_Integer_Literal
(Loc
,
1444 Type_Access_Level
(E_Formal
))));
1447 Append_To
(Post_Call
,
1448 Make_Assignment_Statement
(Loc
,
1450 Expression
=> Expr
));
1454 end Add_Call_By_Copy_Code
;
1456 ----------------------------------
1457 -- Add_Simple_Call_By_Copy_Code --
1458 ----------------------------------
1460 procedure Add_Simple_Call_By_Copy_Code
is
1468 F_Typ
: constant Entity_Id
:= Etype
(Formal
);
1471 if not Is_Legal_Copy
then
1475 -- Use formal type for temp, unless formal type is an unconstrained
1476 -- array, in which case we don't have to worry about bounds checks,
1477 -- and we use the actual type, since that has appropriate bounds.
1479 if Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
1480 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1482 Indic
:= New_Occurrence_Of
(Etype
(Formal
), Loc
);
1485 -- Prepare to generate code
1487 Reset_Packed_Prefix
;
1489 Temp
:= Make_Temporary
(Loc
, 'T', Actual
);
1490 Incod
:= Relocate_Node
(Actual
);
1491 Outcod
:= New_Copy_Tree
(Incod
);
1493 -- Generate declaration of temporary variable, initializing it
1494 -- with the input parameter unless we have an OUT formal or
1495 -- this is an initialization call.
1497 -- If the formal is an out parameter with discriminants, the
1498 -- discriminants must be captured even if the rest of the object
1499 -- is in principle uninitialized, because the discriminants may
1500 -- be read by the called subprogram.
1502 if Ekind
(Formal
) = E_Out_Parameter
then
1505 if Has_Discriminants
(Etype
(Formal
)) then
1506 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1509 elsif Inside_Init_Proc
then
1511 -- Could use a comment here to match comment below ???
1513 if Nkind
(Actual
) /= N_Selected_Component
1515 not Has_Discriminant_Dependent_Constraint
1516 (Entity
(Selector_Name
(Actual
)))
1520 -- Otherwise, keep the component in order to generate the proper
1521 -- actual subtype, that depends on enclosing discriminants.
1529 Make_Object_Declaration
(Loc
,
1530 Defining_Identifier
=> Temp
,
1531 Object_Definition
=> Indic
,
1532 Expression
=> Incod
);
1537 -- If the call is to initialize a component of a composite type,
1538 -- and the component does not depend on discriminants, use the
1539 -- actual type of the component. This is required in case the
1540 -- component is constrained, because in general the formal of the
1541 -- initialization procedure will be unconstrained. Note that if
1542 -- the component being initialized is constrained by an enclosing
1543 -- discriminant, the presence of the initialization in the
1544 -- declaration will generate an expression for the actual subtype.
1546 Set_No_Initialization
(Decl
);
1547 Set_Object_Definition
(Decl
,
1548 New_Occurrence_Of
(Etype
(Actual
), Loc
));
1551 Insert_Action
(N
, Decl
);
1553 -- The actual is simply a reference to the temporary
1555 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
1557 -- Generate copy out if OUT or IN OUT parameter
1559 if Ekind
(Formal
) /= E_In_Parameter
then
1561 Rhs
:= New_Occurrence_Of
(Temp
, Loc
);
1563 -- Deal with conversion
1565 if Nkind
(Lhs
) = N_Type_Conversion
then
1566 Lhs
:= Expression
(Lhs
);
1567 Rhs
:= Convert_To
(Etype
(Actual
), Rhs
);
1570 Append_To
(Post_Call
,
1571 Make_Assignment_Statement
(Loc
,
1573 Expression
=> Rhs
));
1574 Set_Assignment_OK
(Name
(Last
(Post_Call
)));
1576 end Add_Simple_Call_By_Copy_Code
;
1578 ---------------------------
1579 -- Check_Fortran_Logical --
1580 ---------------------------
1582 procedure Check_Fortran_Logical
is
1583 Logical
: constant Entity_Id
:= Etype
(Formal
);
1586 -- Note: this is very incomplete, e.g. it does not handle arrays
1587 -- of logical values. This is really not the right approach at all???)
1590 if Convention
(Subp
) = Convention_Fortran
1591 and then Root_Type
(Etype
(Formal
)) = Standard_Boolean
1592 and then Ekind
(Formal
) /= E_In_Parameter
1594 Var
:= Make_Var
(Actual
);
1595 Append_To
(Post_Call
,
1596 Make_Assignment_Statement
(Loc
,
1597 Name
=> New_Occurrence_Of
(Var
, Loc
),
1599 Unchecked_Convert_To
(
1602 Left_Opnd
=> New_Occurrence_Of
(Var
, Loc
),
1604 Unchecked_Convert_To
(
1606 New_Occurrence_Of
(Standard_False
, Loc
))))));
1608 end Check_Fortran_Logical
;
1614 function Is_Legal_Copy
return Boolean is
1616 -- An attempt to copy a value of such a type can only occur if
1617 -- representation clauses give the actual a misaligned address.
1619 if Is_By_Reference_Type
(Etype
(Formal
)) then
1621 -- If the front-end does not perform full type layout, the actual
1622 -- may in fact be properly aligned but there is not enough front-
1623 -- end information to determine this. In that case gigi will emit
1624 -- an error if a copy is not legal, or generate the proper code.
1625 -- For other backends we report the error now.
1627 -- Seems wrong to be issuing an error in the expander, since it
1628 -- will be missed in -gnatc mode ???
1630 if Frontend_Layout_On_Target
then
1632 ("misaligned actual cannot be passed by reference", Actual
);
1637 -- For users of Starlet, we assume that the specification of by-
1638 -- reference mechanism is mandatory. This may lead to unaligned
1639 -- objects but at least for DEC legacy code it is known to work.
1640 -- The warning will alert users of this code that a problem may
1643 elsif Mechanism
(Formal
) = By_Reference
1644 and then Is_Valued_Procedure
(Scope
(Formal
))
1647 ("by_reference actual may be misaligned??", Actual
);
1659 function Make_Var
(Actual
: Node_Id
) return Entity_Id
is
1663 if Is_Entity_Name
(Actual
) then
1664 return Entity
(Actual
);
1667 Var
:= Make_Temporary
(Loc
, 'T', Actual
);
1670 Make_Object_Renaming_Declaration
(Loc
,
1671 Defining_Identifier
=> Var
,
1673 New_Occurrence_Of
(Etype
(Actual
), Loc
),
1674 Name
=> Relocate_Node
(Actual
));
1676 Insert_Action
(N
, N_Node
);
1681 -------------------------
1682 -- Reset_Packed_Prefix --
1683 -------------------------
1685 procedure Reset_Packed_Prefix
is
1686 Pfx
: Node_Id
:= Actual
;
1689 Set_Analyzed
(Pfx
, False);
1691 not Nkind_In
(Pfx
, N_Selected_Component
, N_Indexed_Component
);
1692 Pfx
:= Prefix
(Pfx
);
1694 end Reset_Packed_Prefix
;
1696 -- Start of processing for Expand_Actuals
1699 Post_Call
:= New_List
;
1701 Formal
:= First_Formal
(Subp
);
1702 Actual
:= First_Actual
(N
);
1703 while Present
(Formal
) loop
1704 E_Formal
:= Etype
(Formal
);
1705 E_Actual
:= Etype
(Actual
);
1707 if Is_Scalar_Type
(E_Formal
)
1708 or else Nkind
(Actual
) = N_Slice
1710 Check_Fortran_Logical
;
1714 elsif Ekind
(Formal
) /= E_Out_Parameter
then
1716 -- The unusual case of the current instance of a protected type
1717 -- requires special handling. This can only occur in the context
1718 -- of a call within the body of a protected operation.
1720 if Is_Entity_Name
(Actual
)
1721 and then Ekind
(Entity
(Actual
)) = E_Protected_Type
1722 and then In_Open_Scopes
(Entity
(Actual
))
1724 if Scope
(Subp
) /= Entity
(Actual
) then
1726 ("operation outside protected type may not "
1727 & "call back its protected operations??", Actual
);
1731 Expand_Protected_Object_Reference
(N
, Entity
(Actual
)));
1734 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1735 -- build-in-place function, then a temporary return object needs
1736 -- to be created and access to it must be passed to the function.
1737 -- Currently we limit such functions to those with inherently
1738 -- limited result subtypes, but eventually we plan to expand the
1739 -- functions that are treated as build-in-place to include other
1740 -- composite result types.
1742 if Is_Build_In_Place_Function_Call
(Actual
) then
1743 Make_Build_In_Place_Call_In_Anonymous_Context
(Actual
);
1746 Apply_Constraint_Check
(Actual
, E_Formal
);
1748 -- Out parameter case. No constraint checks on access type
1751 elsif Is_Access_Type
(E_Formal
) then
1756 elsif Has_Discriminants
(Base_Type
(E_Formal
))
1757 or else Has_Non_Null_Base_Init_Proc
(E_Formal
)
1759 Apply_Constraint_Check
(Actual
, E_Formal
);
1764 Apply_Constraint_Check
(Actual
, Base_Type
(E_Formal
));
1767 -- Processing for IN-OUT and OUT parameters
1769 if Ekind
(Formal
) /= E_In_Parameter
then
1771 -- For type conversions of arrays, apply length/range checks
1773 if Is_Array_Type
(E_Formal
)
1774 and then Nkind
(Actual
) = N_Type_Conversion
1776 if Is_Constrained
(E_Formal
) then
1777 Apply_Length_Check
(Expression
(Actual
), E_Formal
);
1779 Apply_Range_Check
(Expression
(Actual
), E_Formal
);
1783 -- If argument is a type conversion for a type that is passed
1784 -- by copy, then we must pass the parameter by copy.
1786 if Nkind
(Actual
) = N_Type_Conversion
1788 (Is_Numeric_Type
(E_Formal
)
1789 or else Is_Access_Type
(E_Formal
)
1790 or else Is_Enumeration_Type
(E_Formal
)
1791 or else Is_Bit_Packed_Array
(Etype
(Formal
))
1792 or else Is_Bit_Packed_Array
(Etype
(Expression
(Actual
)))
1794 -- Also pass by copy if change of representation
1796 or else not Same_Representation
1798 Etype
(Expression
(Actual
))))
1800 Add_Call_By_Copy_Code
;
1802 -- References to components of bit packed arrays are expanded
1803 -- at this point, rather than at the point of analysis of the
1804 -- actuals, to handle the expansion of the assignment to
1805 -- [in] out parameters.
1807 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
1808 Add_Simple_Call_By_Copy_Code
;
1810 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1811 -- because the back-end cannot cope with such objects. In other
1812 -- cases where alignment forces a copy, the back-end generates
1813 -- it properly. It should not be generated unconditionally in the
1814 -- front-end because it does not know precisely the alignment
1815 -- requirements of the target, and makes too conservative an
1816 -- estimate, leading to superfluous copies or spurious errors
1817 -- on by-reference parameters.
1819 elsif Nkind
(Actual
) = N_Selected_Component
1821 Component_May_Be_Bit_Aligned
(Entity
(Selector_Name
(Actual
)))
1822 and then not Represented_As_Scalar
(Etype
(Formal
))
1824 Add_Simple_Call_By_Copy_Code
;
1826 -- References to slices of bit packed arrays are expanded
1828 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
1829 Add_Call_By_Copy_Code
;
1831 -- References to possibly unaligned slices of arrays are expanded
1833 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
1834 Add_Call_By_Copy_Code
;
1836 -- Deal with access types where the actual subtype and the
1837 -- formal subtype are not the same, requiring a check.
1839 -- It is necessary to exclude tagged types because of "downward
1840 -- conversion" errors.
1842 elsif Is_Access_Type
(E_Formal
)
1843 and then not Same_Type
(E_Formal
, E_Actual
)
1844 and then not Is_Tagged_Type
(Designated_Type
(E_Formal
))
1846 Add_Call_By_Copy_Code
;
1848 -- If the actual is not a scalar and is marked for volatile
1849 -- treatment, whereas the formal is not volatile, then pass
1850 -- by copy unless it is a by-reference type.
1852 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1853 -- because this is the enforcement of a language rule that applies
1854 -- only to "real" volatile variables, not e.g. to the address
1855 -- clause overlay case.
1857 elsif Is_Entity_Name
(Actual
)
1858 and then Is_Volatile
(Entity
(Actual
))
1859 and then not Is_By_Reference_Type
(E_Actual
)
1860 and then not Is_Scalar_Type
(Etype
(Entity
(Actual
)))
1861 and then not Is_Volatile
(E_Formal
)
1863 Add_Call_By_Copy_Code
;
1865 elsif Nkind
(Actual
) = N_Indexed_Component
1866 and then Is_Entity_Name
(Prefix
(Actual
))
1867 and then Has_Volatile_Components
(Entity
(Prefix
(Actual
)))
1869 Add_Call_By_Copy_Code
;
1871 -- Add call-by-copy code for the case of scalar out parameters
1872 -- when it is not known at compile time that the subtype of the
1873 -- formal is a subrange of the subtype of the actual (or vice
1874 -- versa for in out parameters), in order to get range checks
1875 -- on such actuals. (Maybe this case should be handled earlier
1876 -- in the if statement???)
1878 elsif Is_Scalar_Type
(E_Formal
)
1880 (not In_Subrange_Of
(E_Formal
, E_Actual
)
1882 (Ekind
(Formal
) = E_In_Out_Parameter
1883 and then not In_Subrange_Of
(E_Actual
, E_Formal
)))
1885 -- Perhaps the setting back to False should be done within
1886 -- Add_Call_By_Copy_Code, since it could get set on other
1887 -- cases occurring above???
1889 if Do_Range_Check
(Actual
) then
1890 Set_Do_Range_Check
(Actual
, False);
1893 Add_Call_By_Copy_Code
;
1896 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
1897 -- by-reference parameters on exit from the call. If the actual
1898 -- is a derived type and the operation is inherited, the body
1899 -- of the operation will not contain a call to the predicate
1900 -- function, so it must be done explicitly after the call. Ditto
1901 -- if the actual is an entity of a predicated subtype.
1903 -- The rule refers to by-reference types, but a check is needed
1904 -- for by-copy types as well. That check is subsumed by the rule
1905 -- for subtype conversion on assignment, but we can generate the
1906 -- required check now.
1908 -- Note also that Subp may be either a subprogram entity for
1909 -- direct calls, or a type entity for indirect calls, which must
1910 -- be handled separately because the name does not denote an
1911 -- overloadable entity.
1913 By_Ref_Predicate_Check
: declare
1914 Aund
: constant Entity_Id
:= Underlying_Type
(E_Actual
);
1917 function Is_Public_Subp
return Boolean;
1918 -- Check whether the subprogram being called is a visible
1919 -- operation of the type of the actual. Used to determine
1920 -- whether an invariant check must be generated on the
1923 ---------------------
1924 -- Is_Public_Subp --
1925 ---------------------
1927 function Is_Public_Subp
return Boolean is
1928 Pack
: constant Entity_Id
:= Scope
(Subp
);
1929 Subp_Decl
: Node_Id
;
1932 if not Is_Subprogram
(Subp
) then
1935 -- The operation may be inherited, or a primitive of the
1939 Nkind_In
(Parent
(Subp
), N_Private_Extension_Declaration
,
1940 N_Full_Type_Declaration
)
1942 Subp_Decl
:= Parent
(Subp
);
1945 Subp_Decl
:= Unit_Declaration_Node
(Subp
);
1948 return Ekind
(Pack
) = E_Package
1950 List_Containing
(Subp_Decl
) =
1951 Visible_Declarations
1952 (Specification
(Unit_Declaration_Node
(Pack
)));
1955 -- Start of processing for By_Ref_Predicate_Check
1964 if Has_Predicates
(Atyp
)
1965 and then Present
(Predicate_Function
(Atyp
))
1967 -- Skip predicate checks for special cases
1969 and then Predicate_Tests_On_Arguments
(Subp
)
1971 Append_To
(Post_Call
,
1972 Make_Predicate_Check
(Atyp
, Actual
));
1975 -- We generated caller-side invariant checks in two cases:
1977 -- a) when calling an inherited operation, where there is an
1978 -- implicit view conversion of the actual to the parent type.
1980 -- b) When the conversion is explicit
1982 -- We treat these cases separately because the required
1983 -- conversion for a) is added later when expanding the call.
1985 if Has_Invariants
(Etype
(Actual
))
1987 Nkind
(Parent
(Subp
)) = N_Private_Extension_Declaration
1989 if Comes_From_Source
(N
) and then Is_Public_Subp
then
1990 Append_To
(Post_Call
, Make_Invariant_Call
(Actual
));
1993 elsif Nkind
(Actual
) = N_Type_Conversion
1994 and then Has_Invariants
(Etype
(Expression
(Actual
)))
1996 if Comes_From_Source
(N
) and then Is_Public_Subp
then
1997 Append_To
(Post_Call
,
1998 Make_Invariant_Call
(Expression
(Actual
)));
2001 end By_Ref_Predicate_Check
;
2003 -- Processing for IN parameters
2006 -- For IN parameters is in the packed array case, we expand an
2007 -- indexed component (the circuit in Exp_Ch4 deliberately left
2008 -- indexed components appearing as actuals untouched, so that
2009 -- the special processing above for the OUT and IN OUT cases
2010 -- could be performed. We could make the test in Exp_Ch4 more
2011 -- complex and have it detect the parameter mode, but it is
2012 -- easier simply to handle all cases here.)
2014 if Nkind
(Actual
) = N_Indexed_Component
2015 and then Is_Packed
(Etype
(Prefix
(Actual
)))
2017 Reset_Packed_Prefix
;
2018 Expand_Packed_Element_Reference
(Actual
);
2020 -- If we have a reference to a bit packed array, we copy it, since
2021 -- the actual must be byte aligned.
2023 -- Is this really necessary in all cases???
2025 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
2026 Add_Simple_Call_By_Copy_Code
;
2028 -- If a non-scalar actual is possibly unaligned, we need a copy
2030 elsif Is_Possibly_Unaligned_Object
(Actual
)
2031 and then not Represented_As_Scalar
(Etype
(Formal
))
2033 Add_Simple_Call_By_Copy_Code
;
2035 -- Similarly, we have to expand slices of packed arrays here
2036 -- because the result must be byte aligned.
2038 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
2039 Add_Call_By_Copy_Code
;
2041 -- Only processing remaining is to pass by copy if this is a
2042 -- reference to a possibly unaligned slice, since the caller
2043 -- expects an appropriately aligned argument.
2045 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
2046 Add_Call_By_Copy_Code
;
2048 -- An unusual case: a current instance of an enclosing task can be
2049 -- an actual, and must be replaced by a reference to self.
2051 elsif Is_Entity_Name
(Actual
)
2052 and then Is_Task_Type
(Entity
(Actual
))
2054 if In_Open_Scopes
(Entity
(Actual
)) then
2056 (Make_Function_Call
(Loc
,
2057 Name
=> New_Occurrence_Of
(RTE
(RE_Self
), Loc
))));
2060 -- A task type cannot otherwise appear as an actual
2063 raise Program_Error
;
2068 Next_Formal
(Formal
);
2069 Next_Actual
(Actual
);
2072 -- Find right place to put post call stuff if it is present
2074 if not Is_Empty_List
(Post_Call
) then
2076 -- Cases where the call is not a member of a statement list
2078 if not Is_List_Member
(N
) then
2080 -- In Ada 2012 the call may be a function call in an expression
2081 -- (since OUT and IN OUT parameters are now allowed for such
2082 -- calls). The write-back of (in)-out parameters is handled
2083 -- by the back-end, but the constraint checks generated when
2084 -- subtypes of formal and actual don't match must be inserted
2085 -- in the form of assignments.
2087 if Ada_Version
>= Ada_2012
2088 and then Nkind
(N
) = N_Function_Call
2090 -- We used to just do handle this by climbing up parents to
2091 -- a non-statement/declaration and then simply making a call
2092 -- to Insert_Actions_After (P, Post_Call), but that doesn't
2093 -- work. If we are in the middle of an expression, e.g. the
2094 -- condition of an IF, this call would insert after the IF
2095 -- statement, which is much too late to be doing the write
2096 -- back. For example:
2098 -- if Clobber (X) then
2099 -- Put_Line (X'Img);
2104 -- Now assume Clobber changes X, if we put the write back
2105 -- after the IF, the Put_Line gets the wrong value and the
2106 -- goto causes the write back to be skipped completely.
2108 -- To deal with this, we replace the call by
2111 -- Tnnn : constant function-result-type := function-call;
2112 -- Post_Call actions
2118 Tnnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
2119 FRTyp
: constant Entity_Id
:= Etype
(N
);
2120 Name
: constant Node_Id
:= Relocate_Node
(N
);
2123 Prepend_To
(Post_Call
,
2124 Make_Object_Declaration
(Loc
,
2125 Defining_Identifier
=> Tnnn
,
2126 Object_Definition
=> New_Occurrence_Of
(FRTyp
, Loc
),
2127 Constant_Present
=> True,
2128 Expression
=> Name
));
2131 Make_Expression_With_Actions
(Loc
,
2132 Actions
=> Post_Call
,
2133 Expression
=> New_Occurrence_Of
(Tnnn
, Loc
)));
2135 -- We don't want to just blindly call Analyze_And_Resolve
2136 -- because that would cause unwanted recursion on the call.
2137 -- So for a moment set the call as analyzed to prevent that
2138 -- recursion, and get the rest analyzed properly, then reset
2139 -- the analyzed flag, so our caller can continue.
2141 Set_Analyzed
(Name
, True);
2142 Analyze_And_Resolve
(N
, FRTyp
);
2143 Set_Analyzed
(Name
, False);
2145 -- Reset calling argument to point to function call inside
2146 -- the expression with actions so the caller can continue
2147 -- to process the call. In spite of the fact that it is
2148 -- marked Analyzed above, it may be rewritten by Remove_
2149 -- Side_Effects if validity checks are present, so go back
2150 -- to original call.
2152 N
:= Original_Node
(Name
);
2155 -- If not the special Ada 2012 case of a function call, then
2156 -- we must have the triggering statement of a triggering
2157 -- alternative or an entry call alternative, and we can add
2158 -- the post call stuff to the corresponding statement list.
2166 pragma Assert
(Nkind_In
(P
, N_Triggering_Alternative
,
2167 N_Entry_Call_Alternative
));
2169 if Is_Non_Empty_List
(Statements
(P
)) then
2170 Insert_List_Before_And_Analyze
2171 (First
(Statements
(P
)), Post_Call
);
2173 Set_Statements
(P
, Post_Call
);
2180 -- Otherwise, normal case where N is in a statement sequence,
2181 -- just put the post-call stuff after the call statement.
2184 Insert_Actions_After
(N
, Post_Call
);
2189 -- The call node itself is re-analyzed in Expand_Call
2197 -- This procedure handles expansion of function calls and procedure call
2198 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
2199 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
2201 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
2202 -- Provide values of actuals for all formals in Extra_Formals list
2203 -- Replace "call" to enumeration literal function by literal itself
2204 -- Rewrite call to predefined operator as operator
2205 -- Replace actuals to in-out parameters that are numeric conversions,
2206 -- with explicit assignment to temporaries before and after the call.
2208 -- Note that the list of actuals has been filled with default expressions
2209 -- during semantic analysis of the call. Only the extra actuals required
2210 -- for the 'Constrained attribute and for accessibility checks are added
2213 procedure Expand_Call
(N
: Node_Id
) is
2214 Loc
: constant Source_Ptr
:= Sloc
(N
);
2215 Call_Node
: Node_Id
:= N
;
2216 Extra_Actuals
: List_Id
:= No_List
;
2217 Prev
: Node_Id
:= Empty
;
2219 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
);
2220 -- Adds one entry to the end of the actual parameter list. Used for
2221 -- default parameters and for extra actuals (for Extra_Formals). The
2222 -- argument is an N_Parameter_Association node.
2224 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
);
2225 -- Adds an extra actual to the list of extra actuals. Expr is the
2226 -- expression for the value of the actual, EF is the entity for the
2229 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
;
2230 -- Within an instance, a type derived from an untagged formal derived
2231 -- type inherits from the original parent, not from the actual. The
2232 -- current derivation mechanism has the derived type inherit from the
2233 -- actual, which is only correct outside of the instance. If the
2234 -- subprogram is inherited, we test for this particular case through a
2235 -- convoluted tree traversal before setting the proper subprogram to be
2238 function In_Unfrozen_Instance
(E
: Entity_Id
) return Boolean;
2239 -- Return true if E comes from an instance that is not yet frozen
2241 function Is_Direct_Deep_Call
(Subp
: Entity_Id
) return Boolean;
2242 -- Determine if Subp denotes a non-dispatching call to a Deep routine
2244 function New_Value
(From
: Node_Id
) return Node_Id
;
2245 -- From is the original Expression. New_Value is equivalent to a call
2246 -- to Duplicate_Subexpr with an explicit dereference when From is an
2247 -- access parameter.
2249 --------------------------
2250 -- Add_Actual_Parameter --
2251 --------------------------
2253 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
) is
2254 Actual_Expr
: constant Node_Id
:=
2255 Explicit_Actual_Parameter
(Insert_Param
);
2258 -- Case of insertion is first named actual
2260 if No
(Prev
) or else
2261 Nkind
(Parent
(Prev
)) /= N_Parameter_Association
2263 Set_Next_Named_Actual
2264 (Insert_Param
, First_Named_Actual
(Call_Node
));
2265 Set_First_Named_Actual
(Call_Node
, Actual_Expr
);
2268 if No
(Parameter_Associations
(Call_Node
)) then
2269 Set_Parameter_Associations
(Call_Node
, New_List
);
2272 Append
(Insert_Param
, Parameter_Associations
(Call_Node
));
2275 Insert_After
(Prev
, Insert_Param
);
2278 -- Case of insertion is not first named actual
2281 Set_Next_Named_Actual
2282 (Insert_Param
, Next_Named_Actual
(Parent
(Prev
)));
2283 Set_Next_Named_Actual
(Parent
(Prev
), Actual_Expr
);
2284 Append
(Insert_Param
, Parameter_Associations
(Call_Node
));
2287 Prev
:= Actual_Expr
;
2288 end Add_Actual_Parameter
;
2290 ----------------------
2291 -- Add_Extra_Actual --
2292 ----------------------
2294 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
) is
2295 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
2298 if Extra_Actuals
= No_List
then
2299 Extra_Actuals
:= New_List
;
2300 Set_Parent
(Extra_Actuals
, Call_Node
);
2303 Append_To
(Extra_Actuals
,
2304 Make_Parameter_Association
(Loc
,
2305 Selector_Name
=> New_Occurrence_Of
(EF
, Loc
),
2306 Explicit_Actual_Parameter
=> Expr
));
2308 Analyze_And_Resolve
(Expr
, Etype
(EF
));
2310 if Nkind
(Call_Node
) = N_Function_Call
then
2311 Set_Is_Accessibility_Actual
(Parent
(Expr
));
2313 end Add_Extra_Actual
;
2315 ---------------------------
2316 -- Inherited_From_Formal --
2317 ---------------------------
2319 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
is
2321 Gen_Par
: Entity_Id
;
2322 Gen_Prim
: Elist_Id
;
2327 -- If the operation is inherited, it is attached to the corresponding
2328 -- type derivation. If the parent in the derivation is a generic
2329 -- actual, it is a subtype of the actual, and we have to recover the
2330 -- original derived type declaration to find the proper parent.
2332 if Nkind
(Parent
(S
)) /= N_Full_Type_Declaration
2333 or else not Is_Derived_Type
(Defining_Identifier
(Parent
(S
)))
2334 or else Nkind
(Type_Definition
(Original_Node
(Parent
(S
)))) /=
2335 N_Derived_Type_Definition
2336 or else not In_Instance
2343 (Type_Definition
(Original_Node
(Parent
(S
))));
2345 if Nkind
(Indic
) = N_Subtype_Indication
then
2346 Par
:= Entity
(Subtype_Mark
(Indic
));
2348 Par
:= Entity
(Indic
);
2352 if not Is_Generic_Actual_Type
(Par
)
2353 or else Is_Tagged_Type
(Par
)
2354 or else Nkind
(Parent
(Par
)) /= N_Subtype_Declaration
2355 or else not In_Open_Scopes
(Scope
(Par
))
2359 Gen_Par
:= Generic_Parent_Type
(Parent
(Par
));
2362 -- If the actual has no generic parent type, the formal is not
2363 -- a formal derived type, so nothing to inherit.
2365 if No
(Gen_Par
) then
2369 -- If the generic parent type is still the generic type, this is a
2370 -- private formal, not a derived formal, and there are no operations
2371 -- inherited from the formal.
2373 if Nkind
(Parent
(Gen_Par
)) = N_Formal_Type_Declaration
then
2377 Gen_Prim
:= Collect_Primitive_Operations
(Gen_Par
);
2379 Elmt
:= First_Elmt
(Gen_Prim
);
2380 while Present
(Elmt
) loop
2381 if Chars
(Node
(Elmt
)) = Chars
(S
) then
2387 F1
:= First_Formal
(S
);
2388 F2
:= First_Formal
(Node
(Elmt
));
2390 and then Present
(F2
)
2392 if Etype
(F1
) = Etype
(F2
)
2393 or else Etype
(F2
) = Gen_Par
2399 exit; -- not the right subprogram
2411 raise Program_Error
;
2412 end Inherited_From_Formal
;
2414 --------------------------
2415 -- In_Unfrozen_Instance --
2416 --------------------------
2418 function In_Unfrozen_Instance
(E
: Entity_Id
) return Boolean is
2423 while Present
(S
) and then S
/= Standard_Standard
loop
2424 if Is_Generic_Instance
(S
)
2425 and then Present
(Freeze_Node
(S
))
2426 and then not Analyzed
(Freeze_Node
(S
))
2435 end In_Unfrozen_Instance
;
2437 -------------------------
2438 -- Is_Direct_Deep_Call --
2439 -------------------------
2441 function Is_Direct_Deep_Call
(Subp
: Entity_Id
) return Boolean is
2443 if Is_TSS
(Subp
, TSS_Deep_Adjust
)
2444 or else Is_TSS
(Subp
, TSS_Deep_Finalize
)
2445 or else Is_TSS
(Subp
, TSS_Deep_Initialize
)
2452 Actual
:= First
(Parameter_Associations
(N
));
2453 Formal
:= First_Formal
(Subp
);
2454 while Present
(Actual
)
2455 and then Present
(Formal
)
2457 if Nkind
(Actual
) = N_Identifier
2458 and then Is_Controlling_Actual
(Actual
)
2459 and then Etype
(Actual
) = Etype
(Formal
)
2465 Next_Formal
(Formal
);
2471 end Is_Direct_Deep_Call
;
2477 function New_Value
(From
: Node_Id
) return Node_Id
is
2478 Res
: constant Node_Id
:= Duplicate_Subexpr
(From
);
2480 if Is_Access_Type
(Etype
(From
)) then
2481 return Make_Explicit_Dereference
(Sloc
(From
), Prefix
=> Res
);
2489 Remote
: constant Boolean := Is_Remote_Call
(Call_Node
);
2492 Orig_Subp
: Entity_Id
:= Empty
;
2493 Param_Count
: Natural := 0;
2494 Parent_Formal
: Entity_Id
;
2495 Parent_Subp
: Entity_Id
;
2499 Prev_Orig
: Node_Id
;
2500 -- Original node for an actual, which may have been rewritten. If the
2501 -- actual is a function call that has been transformed from a selected
2502 -- component, the original node is unanalyzed. Otherwise, it carries
2503 -- semantic information used to generate additional actuals.
2505 CW_Interface_Formals_Present
: Boolean := False;
2507 -- Start of processing for Expand_Call
2510 -- Expand the function or procedure call if the first actual has a
2511 -- declared dimension aspect, and the subprogram is declared in one
2512 -- of the dimension I/O packages.
2514 if Ada_Version
>= Ada_2012
2516 Nkind_In
(Call_Node
, N_Procedure_Call_Statement
, N_Function_Call
)
2517 and then Present
(Parameter_Associations
(Call_Node
))
2519 Expand_Put_Call_With_Symbol
(Call_Node
);
2522 -- Ignore if previous error
2524 if Nkind
(Call_Node
) in N_Has_Etype
2525 and then Etype
(Call_Node
) = Any_Type
2530 -- Call using access to subprogram with explicit dereference
2532 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
2533 Subp
:= Etype
(Name
(Call_Node
));
2534 Parent_Subp
:= Empty
;
2536 -- Case of call to simple entry, where the Name is a selected component
2537 -- whose prefix is the task, and whose selector name is the entry name
2539 elsif Nkind
(Name
(Call_Node
)) = N_Selected_Component
then
2540 Subp
:= Entity
(Selector_Name
(Name
(Call_Node
)));
2541 Parent_Subp
:= Empty
;
2543 -- Case of call to member of entry family, where Name is an indexed
2544 -- component, with the prefix being a selected component giving the
2545 -- task and entry family name, and the index being the entry index.
2547 elsif Nkind
(Name
(Call_Node
)) = N_Indexed_Component
then
2548 Subp
:= Entity
(Selector_Name
(Prefix
(Name
(Call_Node
))));
2549 Parent_Subp
:= Empty
;
2554 Subp
:= Entity
(Name
(Call_Node
));
2555 Parent_Subp
:= Alias
(Subp
);
2557 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2558 -- if we can tell that the first parameter cannot possibly be null.
2559 -- This improves efficiency by avoiding a run-time test.
2561 -- We do not do this if Raise_Exception_Always does not exist, which
2562 -- can happen in configurable run time profiles which provide only a
2565 if Is_RTE
(Subp
, RE_Raise_Exception
)
2566 and then RTE_Available
(RE_Raise_Exception_Always
)
2569 FA
: constant Node_Id
:=
2570 Original_Node
(First_Actual
(Call_Node
));
2573 -- The case we catch is where the first argument is obtained
2574 -- using the Identity attribute (which must always be
2577 if Nkind
(FA
) = N_Attribute_Reference
2578 and then Attribute_Name
(FA
) = Name_Identity
2580 Subp
:= RTE
(RE_Raise_Exception_Always
);
2581 Set_Name
(Call_Node
, New_Occurrence_Of
(Subp
, Loc
));
2586 if Ekind
(Subp
) = E_Entry
then
2587 Parent_Subp
:= Empty
;
2591 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2592 -- alternative in an asynchronous select or as an entry call in
2593 -- a conditional or timed select. Check whether the procedure call
2594 -- is a renaming of an entry and rewrite it as an entry call.
2596 if Ada_Version
>= Ada_2005
2597 and then Nkind
(Call_Node
) = N_Procedure_Call_Statement
2599 ((Nkind
(Parent
(Call_Node
)) = N_Triggering_Alternative
2600 and then Triggering_Statement
(Parent
(Call_Node
)) = Call_Node
)
2602 (Nkind
(Parent
(Call_Node
)) = N_Entry_Call_Alternative
2603 and then Entry_Call_Statement
(Parent
(Call_Node
)) = Call_Node
))
2607 Ren_Root
: Entity_Id
:= Subp
;
2610 -- This may be a chain of renamings, find the root
2612 if Present
(Alias
(Ren_Root
)) then
2613 Ren_Root
:= Alias
(Ren_Root
);
2616 if Present
(Original_Node
(Parent
(Parent
(Ren_Root
)))) then
2617 Ren_Decl
:= Original_Node
(Parent
(Parent
(Ren_Root
)));
2619 if Nkind
(Ren_Decl
) = N_Subprogram_Renaming_Declaration
then
2621 Make_Entry_Call_Statement
(Loc
,
2623 New_Copy_Tree
(Name
(Ren_Decl
)),
2624 Parameter_Associations
=>
2626 (Parameter_Associations
(Call_Node
))));
2634 -- When generating C code, transform a function call that returns a
2635 -- constrained array type into procedure form.
2637 if Modify_Tree_For_C
2638 and then Nkind
(Call_Node
) = N_Function_Call
2639 and then Is_Entity_Name
(Name
(Call_Node
))
2640 and then Rewritten_For_C
(Entity
(Name
(Call_Node
)))
2642 Rewrite_Function_Call_For_C
(Call_Node
);
2646 -- First step, compute extra actuals, corresponding to any Extra_Formals
2647 -- present. Note that we do not access Extra_Formals directly, instead
2648 -- we simply note the presence of the extra formals as we process the
2649 -- regular formals collecting corresponding actuals in Extra_Actuals.
2651 -- We also generate any required range checks for actuals for in formals
2652 -- as we go through the loop, since this is a convenient place to do it.
2653 -- (Though it seems that this would be better done in Expand_Actuals???)
2655 -- Special case: Thunks must not compute the extra actuals; they must
2656 -- just propagate to the target primitive their extra actuals.
2658 if Is_Thunk
(Current_Scope
)
2659 and then Thunk_Entity
(Current_Scope
) = Subp
2660 and then Present
(Extra_Formals
(Subp
))
2662 pragma Assert
(Present
(Extra_Formals
(Current_Scope
)));
2665 Target_Formal
: Entity_Id
;
2666 Thunk_Formal
: Entity_Id
;
2669 Target_Formal
:= Extra_Formals
(Subp
);
2670 Thunk_Formal
:= Extra_Formals
(Current_Scope
);
2671 while Present
(Target_Formal
) loop
2673 (New_Occurrence_Of
(Thunk_Formal
, Loc
), Thunk_Formal
);
2675 Target_Formal
:= Extra_Formal
(Target_Formal
);
2676 Thunk_Formal
:= Extra_Formal
(Thunk_Formal
);
2679 while Is_Non_Empty_List
(Extra_Actuals
) loop
2680 Add_Actual_Parameter
(Remove_Head
(Extra_Actuals
));
2683 Expand_Actuals
(Call_Node
, Subp
);
2688 Formal
:= First_Formal
(Subp
);
2689 Actual
:= First_Actual
(Call_Node
);
2691 while Present
(Formal
) loop
2693 -- Generate range check if required
2695 if Do_Range_Check
(Actual
)
2696 and then Ekind
(Formal
) = E_In_Parameter
2698 Generate_Range_Check
2699 (Actual
, Etype
(Formal
), CE_Range_Check_Failed
);
2702 -- Prepare to examine current entry
2705 Prev_Orig
:= Original_Node
(Prev
);
2707 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2708 -- to expand it in a further round.
2710 CW_Interface_Formals_Present
:=
2711 CW_Interface_Formals_Present
2713 (Ekind
(Etype
(Formal
)) = E_Class_Wide_Type
2714 and then Is_Interface
(Etype
(Etype
(Formal
))))
2716 (Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
2717 and then Is_Interface
(Directly_Designated_Type
2718 (Etype
(Etype
(Formal
)))));
2720 -- Create possible extra actual for constrained case. Usually, the
2721 -- extra actual is of the form actual'constrained, but since this
2722 -- attribute is only available for unconstrained records, TRUE is
2723 -- expanded if the type of the formal happens to be constrained (for
2724 -- instance when this procedure is inherited from an unconstrained
2725 -- record to a constrained one) or if the actual has no discriminant
2726 -- (its type is constrained). An exception to this is the case of a
2727 -- private type without discriminants. In this case we pass FALSE
2728 -- because the object has underlying discriminants with defaults.
2730 if Present
(Extra_Constrained
(Formal
)) then
2731 if Ekind
(Etype
(Prev
)) in Private_Kind
2732 and then not Has_Discriminants
(Base_Type
(Etype
(Prev
)))
2735 (New_Occurrence_Of
(Standard_False
, Loc
),
2736 Extra_Constrained
(Formal
));
2738 elsif Is_Constrained
(Etype
(Formal
))
2739 or else not Has_Discriminants
(Etype
(Prev
))
2742 (New_Occurrence_Of
(Standard_True
, Loc
),
2743 Extra_Constrained
(Formal
));
2745 -- Do not produce extra actuals for Unchecked_Union parameters.
2746 -- Jump directly to the end of the loop.
2748 elsif Is_Unchecked_Union
(Base_Type
(Etype
(Actual
))) then
2749 goto Skip_Extra_Actual_Generation
;
2752 -- If the actual is a type conversion, then the constrained
2753 -- test applies to the actual, not the target type.
2759 -- Test for unchecked conversions as well, which can occur
2760 -- as out parameter actuals on calls to stream procedures.
2763 while Nkind_In
(Act_Prev
, N_Type_Conversion
,
2764 N_Unchecked_Type_Conversion
)
2766 Act_Prev
:= Expression
(Act_Prev
);
2769 -- If the expression is a conversion of a dereference, this
2770 -- is internally generated code that manipulates addresses,
2771 -- e.g. when building interface tables. No check should
2772 -- occur in this case, and the discriminated object is not
2775 if not Comes_From_Source
(Actual
)
2776 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
2777 and then Nkind
(Act_Prev
) = N_Explicit_Dereference
2780 (New_Occurrence_Of
(Standard_False
, Loc
),
2781 Extra_Constrained
(Formal
));
2785 (Make_Attribute_Reference
(Sloc
(Prev
),
2787 Duplicate_Subexpr_No_Checks
2788 (Act_Prev
, Name_Req
=> True),
2789 Attribute_Name
=> Name_Constrained
),
2790 Extra_Constrained
(Formal
));
2796 -- Create possible extra actual for accessibility level
2798 if Present
(Extra_Accessibility
(Formal
)) then
2800 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2801 -- attribute, then the original actual may be an aliased object
2802 -- occurring as the prefix in a call using "Object.Operation"
2803 -- notation. In that case we must pass the level of the object,
2804 -- so Prev_Orig is reset to Prev and the attribute will be
2805 -- processed by the code for Access attributes further below.
2807 if Prev_Orig
/= Prev
2808 and then Nkind
(Prev
) = N_Attribute_Reference
2810 Get_Attribute_Id
(Attribute_Name
(Prev
)) = Attribute_Access
2811 and then Is_Aliased_View
(Prev_Orig
)
2816 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2817 -- accessibility levels.
2819 if Is_Thunk
(Current_Scope
) then
2821 Parm_Ent
: Entity_Id
;
2824 if Is_Controlling_Actual
(Actual
) then
2826 -- Find the corresponding actual of the thunk
2828 Parm_Ent
:= First_Entity
(Current_Scope
);
2829 for J
in 2 .. Param_Count
loop
2830 Next_Entity
(Parm_Ent
);
2833 -- Handle unchecked conversion of access types generated
2834 -- in thunks (cf. Expand_Interface_Thunk).
2836 elsif Is_Access_Type
(Etype
(Actual
))
2837 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
2839 Parm_Ent
:= Entity
(Expression
(Actual
));
2841 else pragma Assert
(Is_Entity_Name
(Actual
));
2842 Parm_Ent
:= Entity
(Actual
);
2846 (New_Occurrence_Of
(Extra_Accessibility
(Parm_Ent
), Loc
),
2847 Extra_Accessibility
(Formal
));
2850 elsif Is_Entity_Name
(Prev_Orig
) then
2852 -- When passing an access parameter, or a renaming of an access
2853 -- parameter, as the actual to another access parameter we need
2854 -- to pass along the actual's own access level parameter. This
2855 -- is done if we are within the scope of the formal access
2856 -- parameter (if this is an inlined body the extra formal is
2859 if (Is_Formal
(Entity
(Prev_Orig
))
2861 (Present
(Renamed_Object
(Entity
(Prev_Orig
)))
2863 Is_Entity_Name
(Renamed_Object
(Entity
(Prev_Orig
)))
2866 (Entity
(Renamed_Object
(Entity
(Prev_Orig
))))))
2867 and then Ekind
(Etype
(Prev_Orig
)) = E_Anonymous_Access_Type
2868 and then In_Open_Scopes
(Scope
(Entity
(Prev_Orig
)))
2871 Parm_Ent
: constant Entity_Id
:= Param_Entity
(Prev_Orig
);
2874 pragma Assert
(Present
(Parm_Ent
));
2876 if Present
(Extra_Accessibility
(Parm_Ent
)) then
2879 (Extra_Accessibility
(Parm_Ent
), Loc
),
2880 Extra_Accessibility
(Formal
));
2882 -- If the actual access parameter does not have an
2883 -- associated extra formal providing its scope level,
2884 -- then treat the actual as having library-level
2889 (Make_Integer_Literal
(Loc
,
2890 Intval
=> Scope_Depth
(Standard_Standard
)),
2891 Extra_Accessibility
(Formal
));
2895 -- The actual is a normal access value, so just pass the level
2896 -- of the actual's access type.
2900 (Dynamic_Accessibility_Level
(Prev_Orig
),
2901 Extra_Accessibility
(Formal
));
2904 -- If the actual is an access discriminant, then pass the level
2905 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2907 elsif Nkind
(Prev_Orig
) = N_Selected_Component
2908 and then Ekind
(Entity
(Selector_Name
(Prev_Orig
))) =
2910 and then Ekind
(Etype
(Entity
(Selector_Name
(Prev_Orig
)))) =
2911 E_Anonymous_Access_Type
2914 (Make_Integer_Literal
(Loc
,
2915 Intval
=> Object_Access_Level
(Prefix
(Prev_Orig
))),
2916 Extra_Accessibility
(Formal
));
2921 case Nkind
(Prev_Orig
) is
2923 when N_Attribute_Reference
=>
2924 case Get_Attribute_Id
(Attribute_Name
(Prev_Orig
)) is
2926 -- For X'Access, pass on the level of the prefix X
2928 when Attribute_Access
=>
2930 -- If this is an Access attribute applied to the
2931 -- the current instance object passed to a type
2932 -- initialization procedure, then use the level
2933 -- of the type itself. This is not really correct,
2934 -- as there should be an extra level parameter
2935 -- passed in with _init formals (only in the case
2936 -- where the type is immutably limited), but we
2937 -- don't have an easy way currently to create such
2938 -- an extra formal (init procs aren't ever frozen).
2939 -- For now we just use the level of the type,
2940 -- which may be too shallow, but that works better
2941 -- than passing Object_Access_Level of the type,
2942 -- which can be one level too deep in some cases.
2945 if Is_Entity_Name
(Prefix
(Prev_Orig
))
2946 and then Is_Type
(Entity
(Prefix
(Prev_Orig
)))
2949 (Make_Integer_Literal
(Loc
,
2952 (Entity
(Prefix
(Prev_Orig
)))),
2953 Extra_Accessibility
(Formal
));
2957 (Make_Integer_Literal
(Loc
,
2960 (Prefix
(Prev_Orig
))),
2961 Extra_Accessibility
(Formal
));
2964 -- Treat the unchecked attributes as library-level
2966 when Attribute_Unchecked_Access |
2967 Attribute_Unrestricted_Access
=>
2969 (Make_Integer_Literal
(Loc
,
2970 Intval
=> Scope_Depth
(Standard_Standard
)),
2971 Extra_Accessibility
(Formal
));
2973 -- No other cases of attributes returning access
2974 -- values that can be passed to access parameters.
2977 raise Program_Error
;
2981 -- For allocators we pass the level of the execution of the
2982 -- called subprogram, which is one greater than the current
2987 (Make_Integer_Literal
(Loc
,
2988 Intval
=> Scope_Depth
(Current_Scope
) + 1),
2989 Extra_Accessibility
(Formal
));
2991 -- For most other cases we simply pass the level of the
2992 -- actual's access type. The type is retrieved from
2993 -- Prev rather than Prev_Orig, because in some cases
2994 -- Prev_Orig denotes an original expression that has
2995 -- not been analyzed.
2999 (Dynamic_Accessibility_Level
(Prev
),
3000 Extra_Accessibility
(Formal
));
3005 -- Perform the check of 4.6(49) that prevents a null value from being
3006 -- passed as an actual to an access parameter. Note that the check
3007 -- is elided in the common cases of passing an access attribute or
3008 -- access parameter as an actual. Also, we currently don't enforce
3009 -- this check for expander-generated actuals and when -gnatdj is set.
3011 if Ada_Version
>= Ada_2005
then
3013 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
3014 -- the intent of 6.4.1(13) is that null-exclusion checks should
3015 -- not be done for 'out' parameters, even though it refers only
3016 -- to constraint checks, and a null_exclusion is not a constraint.
3017 -- Note that AI05-0196-1 corrects this mistake in the RM.
3019 if Is_Access_Type
(Etype
(Formal
))
3020 and then Can_Never_Be_Null
(Etype
(Formal
))
3021 and then Ekind
(Formal
) /= E_Out_Parameter
3022 and then Nkind
(Prev
) /= N_Raise_Constraint_Error
3023 and then (Known_Null
(Prev
)
3024 or else not Can_Never_Be_Null
(Etype
(Prev
)))
3026 Install_Null_Excluding_Check
(Prev
);
3029 -- Ada_Version < Ada_2005
3032 if Ekind
(Etype
(Formal
)) /= E_Anonymous_Access_Type
3033 or else Access_Checks_Suppressed
(Subp
)
3037 elsif Debug_Flag_J
then
3040 elsif not Comes_From_Source
(Prev
) then
3043 elsif Is_Entity_Name
(Prev
)
3044 and then Ekind
(Etype
(Prev
)) = E_Anonymous_Access_Type
3048 elsif Nkind_In
(Prev
, N_Allocator
, N_Attribute_Reference
) then
3052 Install_Null_Excluding_Check
(Prev
);
3056 -- Perform appropriate validity checks on parameters that
3059 if Validity_Checks_On
then
3060 if (Ekind
(Formal
) = E_In_Parameter
3061 and then Validity_Check_In_Params
)
3063 (Ekind
(Formal
) = E_In_Out_Parameter
3064 and then Validity_Check_In_Out_Params
)
3066 -- If the actual is an indexed component of a packed type (or
3067 -- is an indexed or selected component whose prefix recursively
3068 -- meets this condition), it has not been expanded yet. It will
3069 -- be copied in the validity code that follows, and has to be
3070 -- expanded appropriately, so reanalyze it.
3072 -- What we do is just to unset analyzed bits on prefixes till
3073 -- we reach something that does not have a prefix.
3080 while Nkind_In
(Nod
, N_Indexed_Component
,
3081 N_Selected_Component
)
3083 Set_Analyzed
(Nod
, False);
3084 Nod
:= Prefix
(Nod
);
3088 Ensure_Valid
(Actual
);
3092 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3093 -- since this is a left side reference. We only do this for calls
3094 -- from the source program since we assume that compiler generated
3095 -- calls explicitly generate any required checks. We also need it
3096 -- only if we are doing standard validity checks, since clearly it is
3097 -- not needed if validity checks are off, and in subscript validity
3098 -- checking mode, all indexed components are checked with a call
3099 -- directly from Expand_N_Indexed_Component.
3101 if Comes_From_Source
(Call_Node
)
3102 and then Ekind
(Formal
) /= E_In_Parameter
3103 and then Validity_Checks_On
3104 and then Validity_Check_Default
3105 and then not Validity_Check_Subscripts
3107 Check_Valid_Lvalue_Subscripts
(Actual
);
3110 -- Mark any scalar OUT parameter that is a simple variable as no
3111 -- longer known to be valid (unless the type is always valid). This
3112 -- reflects the fact that if an OUT parameter is never set in a
3113 -- procedure, then it can become invalid on the procedure return.
3115 if Ekind
(Formal
) = E_Out_Parameter
3116 and then Is_Entity_Name
(Actual
)
3117 and then Ekind
(Entity
(Actual
)) = E_Variable
3118 and then not Is_Known_Valid
(Etype
(Actual
))
3120 Set_Is_Known_Valid
(Entity
(Actual
), False);
3123 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3124 -- clear current values, since they can be clobbered. We are probably
3125 -- doing this in more places than we need to, but better safe than
3126 -- sorry when it comes to retaining bad current values.
3128 if Ekind
(Formal
) /= E_In_Parameter
3129 and then Is_Entity_Name
(Actual
)
3130 and then Present
(Entity
(Actual
))
3133 Ent
: constant Entity_Id
:= Entity
(Actual
);
3137 -- For an OUT or IN OUT parameter that is an assignable entity,
3138 -- we do not want to clobber the Last_Assignment field, since
3139 -- if it is set, it was precisely because it is indeed an OUT
3140 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
3141 -- since the subprogram could have returned in invalid value.
3143 if Ekind_In
(Formal
, E_Out_Parameter
, E_In_Out_Parameter
)
3144 and then Is_Assignable
(Ent
)
3146 Sav
:= Last_Assignment
(Ent
);
3147 Kill_Current_Values
(Ent
);
3148 Set_Last_Assignment
(Ent
, Sav
);
3149 Set_Is_Known_Valid
(Ent
, False);
3151 -- For all other cases, just kill the current values
3154 Kill_Current_Values
(Ent
);
3159 -- If the formal is class wide and the actual is an aggregate, force
3160 -- evaluation so that the back end who does not know about class-wide
3161 -- type, does not generate a temporary of the wrong size.
3163 if not Is_Class_Wide_Type
(Etype
(Formal
)) then
3166 elsif Nkind
(Actual
) = N_Aggregate
3167 or else (Nkind
(Actual
) = N_Qualified_Expression
3168 and then Nkind
(Expression
(Actual
)) = N_Aggregate
)
3170 Force_Evaluation
(Actual
);
3173 -- In a remote call, if the formal is of a class-wide type, check
3174 -- that the actual meets the requirements described in E.4(18).
3176 if Remote
and then Is_Class_Wide_Type
(Etype
(Formal
)) then
3177 Insert_Action
(Actual
,
3178 Make_Transportable_Check
(Loc
,
3179 Duplicate_Subexpr_Move_Checks
(Actual
)));
3182 -- This label is required when skipping extra actual generation for
3183 -- Unchecked_Union parameters.
3185 <<Skip_Extra_Actual_Generation
>>
3187 Param_Count
:= Param_Count
+ 1;
3188 Next_Actual
(Actual
);
3189 Next_Formal
(Formal
);
3192 -- If we are calling an Ada 2012 function which needs to have the
3193 -- "accessibility level determined by the point of call" (AI05-0234)
3194 -- passed in to it, then pass it in.
3196 if Ekind_In
(Subp
, E_Function
, E_Operator
, E_Subprogram_Type
)
3198 Present
(Extra_Accessibility_Of_Result
(Ultimate_Alias
(Subp
)))
3201 Ancestor
: Node_Id
:= Parent
(Call_Node
);
3202 Level
: Node_Id
:= Empty
;
3203 Defer
: Boolean := False;
3206 -- Unimplemented: if Subp returns an anonymous access type, then
3208 -- a) if the call is the operand of an explict conversion, then
3209 -- the target type of the conversion (a named access type)
3210 -- determines the accessibility level pass in;
3212 -- b) if the call defines an access discriminant of an object
3213 -- (e.g., the discriminant of an object being created by an
3214 -- allocator, or the discriminant of a function result),
3215 -- then the accessibility level to pass in is that of the
3216 -- discriminated object being initialized).
3220 while Nkind
(Ancestor
) = N_Qualified_Expression
3222 Ancestor
:= Parent
(Ancestor
);
3225 case Nkind
(Ancestor
) is
3228 -- At this point, we'd like to assign
3230 -- Level := Dynamic_Accessibility_Level (Ancestor);
3232 -- but Etype of Ancestor may not have been set yet,
3233 -- so that doesn't work.
3235 -- Handle this later in Expand_Allocator_Expression.
3239 when N_Object_Declaration | N_Object_Renaming_Declaration
=>
3241 Def_Id
: constant Entity_Id
:=
3242 Defining_Identifier
(Ancestor
);
3245 if Is_Return_Object
(Def_Id
) then
3246 if Present
(Extra_Accessibility_Of_Result
3247 (Return_Applies_To
(Scope
(Def_Id
))))
3249 -- Pass along value that was passed in if the
3250 -- routine we are returning from also has an
3251 -- Accessibility_Of_Result formal.
3255 (Extra_Accessibility_Of_Result
3256 (Return_Applies_To
(Scope
(Def_Id
))), Loc
);
3260 Make_Integer_Literal
(Loc
,
3261 Intval
=> Object_Access_Level
(Def_Id
));
3265 when N_Simple_Return_Statement
=>
3266 if Present
(Extra_Accessibility_Of_Result
3268 (Return_Statement_Entity
(Ancestor
))))
3270 -- Pass along value that was passed in if the returned
3271 -- routine also has an Accessibility_Of_Result formal.
3275 (Extra_Accessibility_Of_Result
3277 (Return_Statement_Entity
(Ancestor
))), Loc
);
3285 if not Present
(Level
) then
3287 -- The "innermost master that evaluates the function call".
3289 -- ??? - Should we use Integer'Last here instead in order
3290 -- to deal with (some of) the problems associated with
3291 -- calls to subps whose enclosing scope is unknown (e.g.,
3292 -- Anon_Access_To_Subp_Param.all)?
3294 Level
:= Make_Integer_Literal
(Loc
,
3295 Scope_Depth
(Current_Scope
) + 1);
3300 Extra_Accessibility_Of_Result
(Ultimate_Alias
(Subp
)));
3305 -- If we are expanding the RHS of an assignment we need to check if tag
3306 -- propagation is needed. You might expect this processing to be in
3307 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3308 -- assignment might be transformed to a declaration for an unconstrained
3309 -- value if the expression is classwide.
3311 if Nkind
(Call_Node
) = N_Function_Call
3312 and then Is_Tag_Indeterminate
(Call_Node
)
3313 and then Is_Entity_Name
(Name
(Call_Node
))
3316 Ass
: Node_Id
:= Empty
;
3319 if Nkind
(Parent
(Call_Node
)) = N_Assignment_Statement
then
3320 Ass
:= Parent
(Call_Node
);
3322 elsif Nkind
(Parent
(Call_Node
)) = N_Qualified_Expression
3323 and then Nkind
(Parent
(Parent
(Call_Node
))) =
3324 N_Assignment_Statement
3326 Ass
:= Parent
(Parent
(Call_Node
));
3328 elsif Nkind
(Parent
(Call_Node
)) = N_Explicit_Dereference
3329 and then Nkind
(Parent
(Parent
(Call_Node
))) =
3330 N_Assignment_Statement
3332 Ass
:= Parent
(Parent
(Call_Node
));
3336 and then Is_Class_Wide_Type
(Etype
(Name
(Ass
)))
3338 if Is_Access_Type
(Etype
(Call_Node
)) then
3339 if Designated_Type
(Etype
(Call_Node
)) /=
3340 Root_Type
(Etype
(Name
(Ass
)))
3343 ("tag-indeterminate expression "
3344 & " must have designated type& (RM 5.2 (6))",
3345 Call_Node
, Root_Type
(Etype
(Name
(Ass
))));
3347 Propagate_Tag
(Name
(Ass
), Call_Node
);
3350 elsif Etype
(Call_Node
) /= Root_Type
(Etype
(Name
(Ass
))) then
3352 ("tag-indeterminate expression must have type&"
3354 Call_Node
, Root_Type
(Etype
(Name
(Ass
))));
3357 Propagate_Tag
(Name
(Ass
), Call_Node
);
3360 -- The call will be rewritten as a dispatching call, and
3361 -- expanded as such.
3368 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3369 -- it to point to the correct secondary virtual table
3371 if Nkind
(Call_Node
) in N_Subprogram_Call
3372 and then CW_Interface_Formals_Present
3374 Expand_Interface_Actuals
(Call_Node
);
3377 -- Deals with Dispatch_Call if we still have a call, before expanding
3378 -- extra actuals since this will be done on the re-analysis of the
3379 -- dispatching call. Note that we do not try to shorten the actual list
3380 -- for a dispatching call, it would not make sense to do so. Expansion
3381 -- of dispatching calls is suppressed for VM targets, because the VM
3382 -- back-ends directly handle the generation of dispatching calls and
3383 -- would have to undo any expansion to an indirect call.
3385 if Nkind
(Call_Node
) in N_Subprogram_Call
3386 and then Present
(Controlling_Argument
(Call_Node
))
3389 Call_Typ
: constant Entity_Id
:= Etype
(Call_Node
);
3390 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
3391 Eq_Prim_Op
: Entity_Id
:= Empty
;
3394 Prev_Call
: Node_Id
;
3397 if not Is_Limited_Type
(Typ
) then
3398 Eq_Prim_Op
:= Find_Prim_Op
(Typ
, Name_Op_Eq
);
3401 if Tagged_Type_Expansion
then
3402 Expand_Dispatching_Call
(Call_Node
);
3404 -- The following return is worrisome. Is it really OK to skip
3405 -- all remaining processing in this procedure ???
3412 Apply_Tag_Checks
(Call_Node
);
3414 -- If this is a dispatching "=", we must first compare the
3415 -- tags so we generate: x.tag = y.tag and then x = y
3417 if Subp
= Eq_Prim_Op
then
3419 -- Mark the node as analyzed to avoid reanalyzing this
3420 -- dispatching call (which would cause a never-ending loop)
3422 Prev_Call
:= Relocate_Node
(Call_Node
);
3423 Set_Analyzed
(Prev_Call
);
3425 Param
:= First_Actual
(Call_Node
);
3431 Make_Selected_Component
(Loc
,
3432 Prefix
=> New_Value
(Param
),
3435 (First_Tag_Component
(Typ
), Loc
)),
3438 Make_Selected_Component
(Loc
,
3440 Unchecked_Convert_To
(Typ
,
3441 New_Value
(Next_Actual
(Param
))),
3444 (First_Tag_Component
(Typ
), Loc
))),
3445 Right_Opnd
=> Prev_Call
);
3447 Rewrite
(Call_Node
, New_Call
);
3450 (Call_Node
, Call_Typ
, Suppress
=> All_Checks
);
3453 -- Expansion of a dispatching call results in an indirect call,
3454 -- which in turn causes current values to be killed (see
3455 -- Resolve_Call), so on VM targets we do the call here to
3456 -- ensure consistent warnings between VM and non-VM targets.
3458 Kill_Current_Values
;
3461 -- If this is a dispatching "=" then we must update the reference
3462 -- to the call node because we generated:
3463 -- x.tag = y.tag and then x = y
3465 if Subp
= Eq_Prim_Op
then
3466 Call_Node
:= Right_Opnd
(Call_Node
);
3471 -- Similarly, expand calls to RCI subprograms on which pragma
3472 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3473 -- later. Do this only when the call comes from source since we
3474 -- do not want such a rewriting to occur in expanded code.
3476 if Is_All_Remote_Call
(Call_Node
) then
3477 Expand_All_Calls_Remote_Subprogram_Call
(Call_Node
);
3479 -- Similarly, do not add extra actuals for an entry call whose entity
3480 -- is a protected procedure, or for an internal protected subprogram
3481 -- call, because it will be rewritten as a protected subprogram call
3482 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3484 elsif Is_Protected_Type
(Scope
(Subp
))
3485 and then (Ekind
(Subp
) = E_Procedure
3486 or else Ekind
(Subp
) = E_Function
)
3490 -- During that loop we gathered the extra actuals (the ones that
3491 -- correspond to Extra_Formals), so now they can be appended.
3494 while Is_Non_Empty_List
(Extra_Actuals
) loop
3495 Add_Actual_Parameter
(Remove_Head
(Extra_Actuals
));
3499 -- At this point we have all the actuals, so this is the point at which
3500 -- the various expansion activities for actuals is carried out.
3502 Expand_Actuals
(Call_Node
, Subp
);
3504 -- Verify that the actuals do not share storage. This check must be done
3505 -- on the caller side rather that inside the subprogram to avoid issues
3506 -- of parameter passing.
3508 if Check_Aliasing_Of_Parameters
then
3509 Apply_Parameter_Aliasing_Checks
(Call_Node
, Subp
);
3512 -- If the subprogram is a renaming, or if it is inherited, replace it in
3513 -- the call with the name of the actual subprogram being called. If this
3514 -- is a dispatching call, the run-time decides what to call. The Alias
3515 -- attribute does not apply to entries.
3517 if Nkind
(Call_Node
) /= N_Entry_Call_Statement
3518 and then No
(Controlling_Argument
(Call_Node
))
3519 and then Present
(Parent_Subp
)
3520 and then not Is_Direct_Deep_Call
(Subp
)
3522 if Present
(Inherited_From_Formal
(Subp
)) then
3523 Parent_Subp
:= Inherited_From_Formal
(Subp
);
3525 Parent_Subp
:= Ultimate_Alias
(Parent_Subp
);
3528 -- The below setting of Entity is suspect, see F109-018 discussion???
3530 Set_Entity
(Name
(Call_Node
), Parent_Subp
);
3532 if Is_Abstract_Subprogram
(Parent_Subp
)
3533 and then not In_Instance
3536 ("cannot call abstract subprogram &!",
3537 Name
(Call_Node
), Parent_Subp
);
3540 -- Inspect all formals of derived subprogram Subp. Compare parameter
3541 -- types with the parent subprogram and check whether an actual may
3542 -- need a type conversion to the corresponding formal of the parent
3545 -- Not clear whether intrinsic subprograms need such conversions. ???
3547 if not Is_Intrinsic_Subprogram
(Parent_Subp
)
3548 or else Is_Generic_Instance
(Parent_Subp
)
3551 procedure Convert
(Act
: Node_Id
; Typ
: Entity_Id
);
3552 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3553 -- and resolve the newly generated construct.
3559 procedure Convert
(Act
: Node_Id
; Typ
: Entity_Id
) is
3561 Rewrite
(Act
, OK_Convert_To
(Typ
, Relocate_Node
(Act
)));
3568 Actual_Typ
: Entity_Id
;
3569 Formal_Typ
: Entity_Id
;
3570 Parent_Typ
: Entity_Id
;
3573 Actual
:= First_Actual
(Call_Node
);
3574 Formal
:= First_Formal
(Subp
);
3575 Parent_Formal
:= First_Formal
(Parent_Subp
);
3576 while Present
(Formal
) loop
3577 Actual_Typ
:= Etype
(Actual
);
3578 Formal_Typ
:= Etype
(Formal
);
3579 Parent_Typ
:= Etype
(Parent_Formal
);
3581 -- For an IN parameter of a scalar type, the parent formal
3582 -- type and derived formal type differ or the parent formal
3583 -- type and actual type do not match statically.
3585 if Is_Scalar_Type
(Formal_Typ
)
3586 and then Ekind
(Formal
) = E_In_Parameter
3587 and then Formal_Typ
/= Parent_Typ
3589 not Subtypes_Statically_Match
(Parent_Typ
, Actual_Typ
)
3590 and then not Raises_Constraint_Error
(Actual
)
3592 Convert
(Actual
, Parent_Typ
);
3593 Enable_Range_Check
(Actual
);
3595 -- If the actual has been marked as requiring a range
3596 -- check, then generate it here.
3598 if Do_Range_Check
(Actual
) then
3599 Generate_Range_Check
3600 (Actual
, Etype
(Formal
), CE_Range_Check_Failed
);
3603 -- For access types, the parent formal type and actual type
3606 elsif Is_Access_Type
(Formal_Typ
)
3607 and then Base_Type
(Parent_Typ
) /= Base_Type
(Actual_Typ
)
3609 if Ekind
(Formal
) /= E_In_Parameter
then
3610 Convert
(Actual
, Parent_Typ
);
3612 elsif Ekind
(Parent_Typ
) = E_Anonymous_Access_Type
3613 and then Designated_Type
(Parent_Typ
) /=
3614 Designated_Type
(Actual_Typ
)
3615 and then not Is_Controlling_Formal
(Formal
)
3617 -- This unchecked conversion is not necessary unless
3618 -- inlining is enabled, because in that case the type
3619 -- mismatch may become visible in the body about to be
3623 Unchecked_Convert_To
(Parent_Typ
,
3624 Relocate_Node
(Actual
)));
3626 Resolve
(Actual
, Parent_Typ
);
3629 -- If there is a change of representation, then generate a
3630 -- warning, and do the change of representation.
3632 elsif not Same_Representation
(Formal_Typ
, Parent_Typ
) then
3634 ("??change of representation required", Actual
);
3635 Convert
(Actual
, Parent_Typ
);
3637 -- For array and record types, the parent formal type and
3638 -- derived formal type have different sizes or pragma Pack
3641 elsif ((Is_Array_Type
(Formal_Typ
)
3642 and then Is_Array_Type
(Parent_Typ
))
3644 (Is_Record_Type
(Formal_Typ
)
3645 and then Is_Record_Type
(Parent_Typ
)))
3647 (Esize
(Formal_Typ
) /= Esize
(Parent_Typ
)
3648 or else Has_Pragma_Pack
(Formal_Typ
) /=
3649 Has_Pragma_Pack
(Parent_Typ
))
3651 Convert
(Actual
, Parent_Typ
);
3654 Next_Actual
(Actual
);
3655 Next_Formal
(Formal
);
3656 Next_Formal
(Parent_Formal
);
3662 Subp
:= Parent_Subp
;
3665 -- Deal with case where call is an explicit dereference
3667 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
3669 -- Handle case of access to protected subprogram type
3671 if Is_Access_Protected_Subprogram_Type
3672 (Base_Type
(Etype
(Prefix
(Name
(Call_Node
)))))
3674 -- If this is a call through an access to protected operation, the
3675 -- prefix has the form (object'address, operation'access). Rewrite
3676 -- as a for other protected calls: the object is the 1st parameter
3677 -- of the list of actuals.
3684 Ptr
: constant Node_Id
:= Prefix
(Name
(Call_Node
));
3686 T
: constant Entity_Id
:=
3687 Equivalent_Type
(Base_Type
(Etype
(Ptr
)));
3689 D_T
: constant Entity_Id
:=
3690 Designated_Type
(Base_Type
(Etype
(Ptr
)));
3694 Make_Selected_Component
(Loc
,
3695 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
3697 New_Occurrence_Of
(First_Entity
(T
), Loc
));
3700 Make_Selected_Component
(Loc
,
3701 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
3703 New_Occurrence_Of
(Next_Entity
(First_Entity
(T
)), Loc
));
3706 Make_Explicit_Dereference
(Loc
,
3709 if Present
(Parameter_Associations
(Call_Node
)) then
3710 Parm
:= Parameter_Associations
(Call_Node
);
3715 Prepend
(Obj
, Parm
);
3717 if Etype
(D_T
) = Standard_Void_Type
then
3719 Make_Procedure_Call_Statement
(Loc
,
3721 Parameter_Associations
=> Parm
);
3724 Make_Function_Call
(Loc
,
3726 Parameter_Associations
=> Parm
);
3729 Set_First_Named_Actual
(Call
, First_Named_Actual
(Call_Node
));
3730 Set_Etype
(Call
, Etype
(D_T
));
3732 -- We do not re-analyze the call to avoid infinite recursion.
3733 -- We analyze separately the prefix and the object, and set
3734 -- the checks on the prefix that would otherwise be emitted
3735 -- when resolving a call.
3737 Rewrite
(Call_Node
, Call
);
3739 Apply_Access_Check
(Nam
);
3746 -- If this is a call to an intrinsic subprogram, then perform the
3747 -- appropriate expansion to the corresponding tree node and we
3748 -- are all done (since after that the call is gone).
3750 -- In the case where the intrinsic is to be processed by the back end,
3751 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3752 -- since the idea in this case is to pass the call unchanged. If the
3753 -- intrinsic is an inherited unchecked conversion, and the derived type
3754 -- is the target type of the conversion, we must retain it as the return
3755 -- type of the expression. Otherwise the expansion below, which uses the
3756 -- parent operation, will yield the wrong type.
3758 if Is_Intrinsic_Subprogram
(Subp
) then
3759 Expand_Intrinsic_Call
(Call_Node
, Subp
);
3761 if Nkind
(Call_Node
) = N_Unchecked_Type_Conversion
3762 and then Parent_Subp
/= Orig_Subp
3763 and then Etype
(Parent_Subp
) /= Etype
(Orig_Subp
)
3765 Set_Etype
(Call_Node
, Etype
(Orig_Subp
));
3771 if Ekind_In
(Subp
, E_Function
, E_Procedure
) then
3773 -- We perform two simple optimization on calls:
3775 -- a) replace calls to null procedures unconditionally;
3777 -- b) for To_Address, just do an unchecked conversion. Not only is
3778 -- this efficient, but it also avoids order of elaboration problems
3779 -- when address clauses are inlined (address expression elaborated
3780 -- at the wrong point).
3782 -- We perform these optimization regardless of whether we are in the
3783 -- main unit or in a unit in the context of the main unit, to ensure
3784 -- that tree generated is the same in both cases, for CodePeer use.
3786 if Is_RTE
(Subp
, RE_To_Address
) then
3788 Unchecked_Convert_To
3789 (RTE
(RE_Address
), Relocate_Node
(First_Actual
(Call_Node
))));
3792 elsif Is_Null_Procedure
(Subp
) then
3793 Rewrite
(Call_Node
, Make_Null_Statement
(Loc
));
3797 -- Handle inlining. No action needed if the subprogram is not inlined
3799 if not Is_Inlined
(Subp
) then
3802 -- Handle frontend inlining
3804 elsif not Back_End_Inlining
then
3805 Inlined_Subprogram
: declare
3807 Must_Inline
: Boolean := False;
3808 Spec
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
3811 -- Verify that the body to inline has already been seen, and
3812 -- that if the body is in the current unit the inlining does
3813 -- not occur earlier. This avoids order-of-elaboration problems
3816 -- This should be documented in sinfo/einfo ???
3819 or else Nkind
(Spec
) /= N_Subprogram_Declaration
3820 or else No
(Body_To_Inline
(Spec
))
3822 Must_Inline
:= False;
3824 -- If this an inherited function that returns a private type,
3825 -- do not inline if the full view is an unconstrained array,
3826 -- because such calls cannot be inlined.
3828 elsif Present
(Orig_Subp
)
3829 and then Is_Array_Type
(Etype
(Orig_Subp
))
3830 and then not Is_Constrained
(Etype
(Orig_Subp
))
3832 Must_Inline
:= False;
3834 elsif In_Unfrozen_Instance
(Scope
(Subp
)) then
3835 Must_Inline
:= False;
3838 Bod
:= Body_To_Inline
(Spec
);
3840 if (In_Extended_Main_Code_Unit
(Call_Node
)
3841 or else In_Extended_Main_Code_Unit
(Parent
(Call_Node
))
3842 or else Has_Pragma_Inline_Always
(Subp
))
3843 and then (not In_Same_Extended_Unit
(Sloc
(Bod
), Loc
)
3845 Earlier_In_Extended_Unit
(Sloc
(Bod
), Loc
))
3847 Must_Inline
:= True;
3849 -- If we are compiling a package body that is not the main
3850 -- unit, it must be for inlining/instantiation purposes,
3851 -- in which case we inline the call to insure that the same
3852 -- temporaries are generated when compiling the body by
3853 -- itself. Otherwise link errors can occur.
3855 -- If the function being called is itself in the main unit,
3856 -- we cannot inline, because there is a risk of double
3857 -- elaboration and/or circularity: the inlining can make
3858 -- visible a private entity in the body of the main unit,
3859 -- that gigi will see before its sees its proper definition.
3861 elsif not (In_Extended_Main_Code_Unit
(Call_Node
))
3862 and then In_Package_Body
3864 Must_Inline
:= not In_Extended_Main_Source_Unit
(Subp
);
3869 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
3872 -- Let the back end handle it
3874 Add_Inlined_Body
(Subp
, Call_Node
);
3876 if Front_End_Inlining
3877 and then Nkind
(Spec
) = N_Subprogram_Declaration
3878 and then (In_Extended_Main_Code_Unit
(Call_Node
))
3879 and then No
(Body_To_Inline
(Spec
))
3880 and then not Has_Completion
(Subp
)
3881 and then In_Same_Extended_Unit
(Sloc
(Spec
), Loc
)
3884 ("cannot inline& (body not seen yet)?",
3888 end Inlined_Subprogram
;
3890 -- Back end inlining: let the back end handle it
3892 elsif No
(Unit_Declaration_Node
(Subp
))
3893 or else Nkind
(Unit_Declaration_Node
(Subp
)) /=
3894 N_Subprogram_Declaration
3895 or else No
(Body_To_Inline
(Unit_Declaration_Node
(Subp
)))
3896 or else Nkind
(Body_To_Inline
(Unit_Declaration_Node
(Subp
))) in
3899 Add_Inlined_Body
(Subp
, Call_Node
);
3901 -- Front end expansion of simple functions returning unconstrained
3902 -- types (see Check_And_Split_Unconstrained_Function). Note that the
3903 -- case of a simple renaming (Body_To_Inline in N_Entity above, see
3904 -- also Build_Renamed_Body) cannot be expanded here because this may
3905 -- give rise to order-of-elaboration issues for the types of the
3906 -- parameters of the subprogram, if any.
3909 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
3913 -- Check for protected subprogram. This is either an intra-object call,
3914 -- or a protected function call. Protected procedure calls are rewritten
3915 -- as entry calls and handled accordingly.
3917 -- In Ada 2005, this may be an indirect call to an access parameter that
3918 -- is an access_to_subprogram. In that case the anonymous type has a
3919 -- scope that is a protected operation, but the call is a regular one.
3920 -- In either case do not expand call if subprogram is eliminated.
3922 Scop
:= Scope
(Subp
);
3924 if Nkind
(Call_Node
) /= N_Entry_Call_Statement
3925 and then Is_Protected_Type
(Scop
)
3926 and then Ekind
(Subp
) /= E_Subprogram_Type
3927 and then not Is_Eliminated
(Subp
)
3929 -- If the call is an internal one, it is rewritten as a call to the
3930 -- corresponding unprotected subprogram.
3932 Expand_Protected_Subprogram_Call
(Call_Node
, Subp
, Scop
);
3935 -- Functions returning controlled objects need special attention. If
3936 -- the return type is limited, then the context is initialization and
3937 -- different processing applies. If the call is to a protected function,
3938 -- the expansion above will call Expand_Call recursively. Otherwise the
3939 -- function call is transformed into a temporary which obtains the
3940 -- result from the secondary stack.
3942 if Needs_Finalization
(Etype
(Subp
)) then
3943 if not Is_Limited_View
(Etype
(Subp
))
3945 (No
(First_Formal
(Subp
))
3947 not Is_Concurrent_Record_Type
(Etype
(First_Formal
(Subp
))))
3949 Expand_Ctrl_Function_Call
(Call_Node
);
3951 -- Build-in-place function calls which appear in anonymous contexts
3952 -- need a transient scope to ensure the proper finalization of the
3953 -- intermediate result after its use.
3955 elsif Is_Build_In_Place_Function_Call
(Call_Node
)
3957 Nkind_In
(Parent
(Call_Node
), N_Attribute_Reference
,
3959 N_Indexed_Component
,
3960 N_Object_Renaming_Declaration
,
3961 N_Procedure_Call_Statement
,
3962 N_Selected_Component
,
3965 Establish_Transient_Scope
(Call_Node
, Sec_Stack
=> True);
3970 -------------------------------
3971 -- Expand_Ctrl_Function_Call --
3972 -------------------------------
3974 procedure Expand_Ctrl_Function_Call
(N
: Node_Id
) is
3975 function Is_Element_Reference
(N
: Node_Id
) return Boolean;
3976 -- Determine whether node N denotes a reference to an Ada 2012 container
3979 --------------------------
3980 -- Is_Element_Reference --
3981 --------------------------
3983 function Is_Element_Reference
(N
: Node_Id
) return Boolean is
3984 Ref
: constant Node_Id
:= Original_Node
(N
);
3987 -- Analysis marks an element reference by setting the generalized
3988 -- indexing attribute of an indexed component before the component
3989 -- is rewritten into a function call.
3992 Nkind
(Ref
) = N_Indexed_Component
3993 and then Present
(Generalized_Indexing
(Ref
));
3994 end Is_Element_Reference
;
3998 Is_Elem_Ref
: constant Boolean := Is_Element_Reference
(N
);
4000 -- Start of processing for Expand_Ctrl_Function_Call
4003 -- Optimization, if the returned value (which is on the sec-stack) is
4004 -- returned again, no need to copy/readjust/finalize, we can just pass
4005 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
4006 -- attachment is needed
4008 if Nkind
(Parent
(N
)) = N_Simple_Return_Statement
then
4012 -- Resolution is now finished, make sure we don't start analysis again
4013 -- because of the duplication.
4017 -- A function which returns a controlled object uses the secondary
4018 -- stack. Rewrite the call into a temporary which obtains the result of
4019 -- the function using 'reference.
4021 Remove_Side_Effects
(N
);
4023 -- When the temporary function result appears inside a case expression
4024 -- or an if expression, its lifetime must be extended to match that of
4025 -- the context. If not, the function result will be finalized too early
4026 -- and the evaluation of the expression could yield incorrect result. An
4027 -- exception to this rule are references to Ada 2012 container elements.
4028 -- Such references must be finalized at the end of each iteration of the
4029 -- related quantified expression, otherwise the container will remain
4033 and then Within_Case_Or_If_Expression
(N
)
4034 and then Nkind
(N
) = N_Explicit_Dereference
4036 Set_Is_Processed_Transient
(Entity
(Prefix
(N
)));
4038 end Expand_Ctrl_Function_Call
;
4040 ----------------------------------------
4041 -- Expand_N_Extended_Return_Statement --
4042 ----------------------------------------
4044 -- If there is a Handled_Statement_Sequence, we rewrite this:
4046 -- return Result : T := <expression> do
4047 -- <handled_seq_of_stms>
4053 -- Result : T := <expression>;
4055 -- <handled_seq_of_stms>
4059 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
4061 -- return Result : T := <expression>;
4065 -- return <expression>;
4067 -- unless it's build-in-place or there's no <expression>, in which case
4071 -- Result : T := <expression>;
4076 -- Note that this case could have been written by the user as an extended
4077 -- return statement, or could have been transformed to this from a simple
4078 -- return statement.
4080 -- That is, we need to have a reified return object if there are statements
4081 -- (which might refer to it) or if we're doing build-in-place (so we can
4082 -- set its address to the final resting place or if there is no expression
4083 -- (in which case default initial values might need to be set).
4085 procedure Expand_N_Extended_Return_Statement
(N
: Node_Id
) is
4086 Loc
: constant Source_Ptr
:= Sloc
(N
);
4088 function Build_Heap_Allocator
4089 (Temp_Id
: Entity_Id
;
4090 Temp_Typ
: Entity_Id
;
4091 Func_Id
: Entity_Id
;
4092 Ret_Typ
: Entity_Id
;
4093 Alloc_Expr
: Node_Id
) return Node_Id
;
4094 -- Create the statements necessary to allocate a return object on the
4095 -- caller's master. The master is available through implicit parameter
4096 -- BIPfinalizationmaster.
4098 -- if BIPfinalizationmaster /= null then
4100 -- type Ptr_Typ is access Ret_Typ;
4101 -- for Ptr_Typ'Storage_Pool use
4102 -- Base_Pool (BIPfinalizationmaster.all).all;
4106 -- procedure Allocate (...) is
4108 -- System.Storage_Pools.Subpools.Allocate_Any (...);
4111 -- Local := <Alloc_Expr>;
4112 -- Temp_Id := Temp_Typ (Local);
4116 -- Temp_Id is the temporary which is used to reference the internally
4117 -- created object in all allocation forms. Temp_Typ is the type of the
4118 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
4119 -- type of Func_Id. Alloc_Expr is the actual allocator.
4121 function Move_Activation_Chain
(Func_Id
: Entity_Id
) return Node_Id
;
4122 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
4124 -- From current activation chain
4125 -- To activation chain passed in by the caller
4126 -- New_Master master passed in by the caller
4128 -- Func_Id is the entity of the function where the extended return
4129 -- statement appears.
4131 --------------------------
4132 -- Build_Heap_Allocator --
4133 --------------------------
4135 function Build_Heap_Allocator
4136 (Temp_Id
: Entity_Id
;
4137 Temp_Typ
: Entity_Id
;
4138 Func_Id
: Entity_Id
;
4139 Ret_Typ
: Entity_Id
;
4140 Alloc_Expr
: Node_Id
) return Node_Id
4143 pragma Assert
(Is_Build_In_Place_Function
(Func_Id
));
4145 -- Processing for build-in-place object allocation.
4147 if Needs_Finalization
(Ret_Typ
) then
4149 Decls
: constant List_Id
:= New_List
;
4150 Fin_Mas_Id
: constant Entity_Id
:=
4151 Build_In_Place_Formal
4152 (Func_Id
, BIP_Finalization_Master
);
4153 Stmts
: constant List_Id
:= New_List
;
4154 Desig_Typ
: Entity_Id
;
4155 Local_Id
: Entity_Id
;
4156 Pool_Id
: Entity_Id
;
4157 Ptr_Typ
: Entity_Id
;
4161 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
4163 Pool_Id
:= Make_Temporary
(Loc
, 'P');
4166 Make_Object_Renaming_Declaration
(Loc
,
4167 Defining_Identifier
=> Pool_Id
,
4169 New_Occurrence_Of
(RTE
(RE_Root_Storage_Pool
), Loc
),
4171 Make_Explicit_Dereference
(Loc
,
4173 Make_Function_Call
(Loc
,
4175 New_Occurrence_Of
(RTE
(RE_Base_Pool
), Loc
),
4176 Parameter_Associations
=> New_List
(
4177 Make_Explicit_Dereference
(Loc
,
4179 New_Occurrence_Of
(Fin_Mas_Id
, Loc
)))))));
4181 -- Create an access type which uses the storage pool of the
4182 -- caller's master. This additional type is necessary because
4183 -- the finalization master cannot be associated with the type
4184 -- of the temporary. Otherwise the secondary stack allocation
4187 Desig_Typ
:= Ret_Typ
;
4189 -- Ensure that the build-in-place machinery uses a fat pointer
4190 -- when allocating an unconstrained array on the heap. In this
4191 -- case the result object type is a constrained array type even
4192 -- though the function type is unconstrained.
4194 if Ekind
(Desig_Typ
) = E_Array_Subtype
then
4195 Desig_Typ
:= Base_Type
(Desig_Typ
);
4199 -- type Ptr_Typ is access Desig_Typ;
4201 Ptr_Typ
:= Make_Temporary
(Loc
, 'P');
4204 Make_Full_Type_Declaration
(Loc
,
4205 Defining_Identifier
=> Ptr_Typ
,
4207 Make_Access_To_Object_Definition
(Loc
,
4208 Subtype_Indication
=>
4209 New_Occurrence_Of
(Desig_Typ
, Loc
))));
4211 -- Perform minor decoration in order to set the master and the
4212 -- storage pool attributes.
4214 Set_Ekind
(Ptr_Typ
, E_Access_Type
);
4215 Set_Finalization_Master
(Ptr_Typ
, Fin_Mas_Id
);
4216 Set_Associated_Storage_Pool
(Ptr_Typ
, Pool_Id
);
4218 -- Create the temporary, generate:
4219 -- Local_Id : Ptr_Typ;
4221 Local_Id
:= Make_Temporary
(Loc
, 'T');
4224 Make_Object_Declaration
(Loc
,
4225 Defining_Identifier
=> Local_Id
,
4226 Object_Definition
=>
4227 New_Occurrence_Of
(Ptr_Typ
, Loc
)));
4229 -- Allocate the object, generate:
4230 -- Local_Id := <Alloc_Expr>;
4233 Make_Assignment_Statement
(Loc
,
4234 Name
=> New_Occurrence_Of
(Local_Id
, Loc
),
4235 Expression
=> Alloc_Expr
));
4238 -- Temp_Id := Temp_Typ (Local_Id);
4241 Make_Assignment_Statement
(Loc
,
4242 Name
=> New_Occurrence_Of
(Temp_Id
, Loc
),
4244 Unchecked_Convert_To
(Temp_Typ
,
4245 New_Occurrence_Of
(Local_Id
, Loc
))));
4247 -- Wrap the allocation in a block. This is further conditioned
4248 -- by checking the caller finalization master at runtime. A
4249 -- null value indicates a non-existent master, most likely due
4250 -- to a Finalize_Storage_Only allocation.
4253 -- if BIPfinalizationmaster /= null then
4262 Make_If_Statement
(Loc
,
4265 Left_Opnd
=> New_Occurrence_Of
(Fin_Mas_Id
, Loc
),
4266 Right_Opnd
=> Make_Null
(Loc
)),
4268 Then_Statements
=> New_List
(
4269 Make_Block_Statement
(Loc
,
4270 Declarations
=> Decls
,
4271 Handled_Statement_Sequence
=>
4272 Make_Handled_Sequence_Of_Statements
(Loc
,
4273 Statements
=> Stmts
))));
4276 -- For all other cases, generate:
4277 -- Temp_Id := <Alloc_Expr>;
4281 Make_Assignment_Statement
(Loc
,
4282 Name
=> New_Occurrence_Of
(Temp_Id
, Loc
),
4283 Expression
=> Alloc_Expr
);
4285 end Build_Heap_Allocator
;
4287 ---------------------------
4288 -- Move_Activation_Chain --
4289 ---------------------------
4291 function Move_Activation_Chain
(Func_Id
: Entity_Id
) return Node_Id
is
4294 Make_Procedure_Call_Statement
(Loc
,
4296 New_Occurrence_Of
(RTE
(RE_Move_Activation_Chain
), Loc
),
4298 Parameter_Associations
=> New_List
(
4302 Make_Attribute_Reference
(Loc
,
4303 Prefix
=> Make_Identifier
(Loc
, Name_uChain
),
4304 Attribute_Name
=> Name_Unrestricted_Access
),
4306 -- Destination chain
4309 (Build_In_Place_Formal
(Func_Id
, BIP_Activation_Chain
), Loc
),
4314 (Build_In_Place_Formal
(Func_Id
, BIP_Task_Master
), Loc
)));
4315 end Move_Activation_Chain
;
4319 Func_Id
: constant Entity_Id
:=
4320 Return_Applies_To
(Return_Statement_Entity
(N
));
4321 Is_BIP_Func
: constant Boolean :=
4322 Is_Build_In_Place_Function
(Func_Id
);
4323 Ret_Obj_Id
: constant Entity_Id
:=
4324 First_Entity
(Return_Statement_Entity
(N
));
4325 Ret_Obj_Decl
: constant Node_Id
:= Parent
(Ret_Obj_Id
);
4326 Ret_Typ
: constant Entity_Id
:= Etype
(Func_Id
);
4331 Return_Stmt
: Node_Id
;
4334 -- Start of processing for Expand_N_Extended_Return_Statement
4337 -- Given that functionality of interface thunks is simple (just displace
4338 -- the pointer to the object) they are always handled by means of
4339 -- simple return statements.
4341 pragma Assert
(not Is_Thunk
(Current_Scope
));
4343 if Nkind
(Ret_Obj_Decl
) = N_Object_Declaration
then
4344 Exp
:= Expression
(Ret_Obj_Decl
);
4349 HSS
:= Handled_Statement_Sequence
(N
);
4351 -- If the returned object needs finalization actions, the function must
4352 -- perform the appropriate cleanup should it fail to return. The state
4353 -- of the function itself is tracked through a flag which is coupled
4354 -- with the scope finalizer. There is one flag per each return object
4355 -- in case of multiple returns.
4357 if Is_BIP_Func
and then Needs_Finalization
(Etype
(Ret_Obj_Id
)) then
4359 Flag_Decl
: Node_Id
;
4360 Flag_Id
: Entity_Id
;
4364 -- Recover the function body
4366 Func_Bod
:= Unit_Declaration_Node
(Func_Id
);
4368 if Nkind
(Func_Bod
) = N_Subprogram_Declaration
then
4369 Func_Bod
:= Parent
(Parent
(Corresponding_Body
(Func_Bod
)));
4372 -- Create a flag to track the function state
4374 Flag_Id
:= Make_Temporary
(Loc
, 'F');
4375 Set_Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
, Flag_Id
);
4377 -- Insert the flag at the beginning of the function declarations,
4379 -- Fnn : Boolean := False;
4382 Make_Object_Declaration
(Loc
,
4383 Defining_Identifier
=> Flag_Id
,
4384 Object_Definition
=>
4385 New_Occurrence_Of
(Standard_Boolean
, Loc
),
4387 New_Occurrence_Of
(Standard_False
, Loc
));
4389 Prepend_To
(Declarations
(Func_Bod
), Flag_Decl
);
4390 Analyze
(Flag_Decl
);
4394 -- Build a simple_return_statement that returns the return object when
4395 -- there is a statement sequence, or no expression, or the result will
4396 -- be built in place. Note however that we currently do this for all
4397 -- composite cases, even though nonlimited composite results are not yet
4398 -- built in place (though we plan to do so eventually).
4401 or else Is_Composite_Type
(Ret_Typ
)
4407 -- If the extended return has a handled statement sequence, then wrap
4408 -- it in a block and use the block as the first statement.
4412 Make_Block_Statement
(Loc
,
4413 Declarations
=> New_List
,
4414 Handled_Statement_Sequence
=> HSS
));
4417 -- If the result type contains tasks, we call Move_Activation_Chain.
4418 -- Later, the cleanup code will call Complete_Master, which will
4419 -- terminate any unactivated tasks belonging to the return statement
4420 -- master. But Move_Activation_Chain updates their master to be that
4421 -- of the caller, so they will not be terminated unless the return
4422 -- statement completes unsuccessfully due to exception, abort, goto,
4423 -- or exit. As a formality, we test whether the function requires the
4424 -- result to be built in place, though that's necessarily true for
4425 -- the case of result types with task parts.
4427 if Is_BIP_Func
and then Has_Task
(Ret_Typ
) then
4429 -- The return expression is an aggregate for a complex type which
4430 -- contains tasks. This particular case is left unexpanded since
4431 -- the regular expansion would insert all temporaries and
4432 -- initialization code in the wrong block.
4434 if Nkind
(Exp
) = N_Aggregate
then
4435 Expand_N_Aggregate
(Exp
);
4438 -- Do not move the activation chain if the return object does not
4441 if Has_Task
(Etype
(Ret_Obj_Id
)) then
4442 Append_To
(Stmts
, Move_Activation_Chain
(Func_Id
));
4446 -- Update the state of the function right before the object is
4449 if Is_BIP_Func
and then Needs_Finalization
(Etype
(Ret_Obj_Id
)) then
4451 Flag_Id
: constant Entity_Id
:=
4452 Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
);
4459 Make_Assignment_Statement
(Loc
,
4460 Name
=> New_Occurrence_Of
(Flag_Id
, Loc
),
4461 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)));
4465 -- Build a simple_return_statement that returns the return object
4468 Make_Simple_Return_Statement
(Loc
,
4469 Expression
=> New_Occurrence_Of
(Ret_Obj_Id
, Loc
));
4470 Append_To
(Stmts
, Return_Stmt
);
4472 HSS
:= Make_Handled_Sequence_Of_Statements
(Loc
, Stmts
);
4475 -- Case where we build a return statement block
4477 if Present
(HSS
) then
4479 Make_Block_Statement
(Loc
,
4480 Declarations
=> Return_Object_Declarations
(N
),
4481 Handled_Statement_Sequence
=> HSS
);
4483 -- We set the entity of the new block statement to be that of the
4484 -- return statement. This is necessary so that various fields, such
4485 -- as Finalization_Chain_Entity carry over from the return statement
4486 -- to the block. Note that this block is unusual, in that its entity
4487 -- is an E_Return_Statement rather than an E_Block.
4490 (Result
, New_Occurrence_Of
(Return_Statement_Entity
(N
), Loc
));
4492 -- If the object decl was already rewritten as a renaming, then we
4493 -- don't want to do the object allocation and transformation of
4494 -- the return object declaration to a renaming. This case occurs
4495 -- when the return object is initialized by a call to another
4496 -- build-in-place function, and that function is responsible for
4497 -- the allocation of the return object.
4500 and then Nkind
(Ret_Obj_Decl
) = N_Object_Renaming_Declaration
4503 (Nkind
(Original_Node
(Ret_Obj_Decl
)) = N_Object_Declaration
4504 and then Is_Build_In_Place_Function_Call
4505 (Expression
(Original_Node
(Ret_Obj_Decl
))));
4507 -- Return the build-in-place result by reference
4509 Set_By_Ref
(Return_Stmt
);
4511 elsif Is_BIP_Func
then
4513 -- Locate the implicit access parameter associated with the
4514 -- caller-supplied return object and convert the return
4515 -- statement's return object declaration to a renaming of a
4516 -- dereference of the access parameter. If the return object's
4517 -- declaration includes an expression that has not already been
4518 -- expanded as separate assignments, then add an assignment
4519 -- statement to ensure the return object gets initialized.
4522 -- Result : T [:= <expression>];
4529 -- Result : T renames FuncRA.all;
4530 -- [Result := <expression;]
4535 Ret_Obj_Expr
: constant Node_Id
:= Expression
(Ret_Obj_Decl
);
4536 Ret_Obj_Typ
: constant Entity_Id
:= Etype
(Ret_Obj_Id
);
4538 Init_Assignment
: Node_Id
:= Empty
;
4539 Obj_Acc_Formal
: Entity_Id
;
4540 Obj_Acc_Deref
: Node_Id
;
4541 Obj_Alloc_Formal
: Entity_Id
;
4544 -- Build-in-place results must be returned by reference
4546 Set_By_Ref
(Return_Stmt
);
4548 -- Retrieve the implicit access parameter passed by the caller
4551 Build_In_Place_Formal
(Func_Id
, BIP_Object_Access
);
4553 -- If the return object's declaration includes an expression
4554 -- and the declaration isn't marked as No_Initialization, then
4555 -- we need to generate an assignment to the object and insert
4556 -- it after the declaration before rewriting it as a renaming
4557 -- (otherwise we'll lose the initialization). The case where
4558 -- the result type is an interface (or class-wide interface)
4559 -- is also excluded because the context of the function call
4560 -- must be unconstrained, so the initialization will always
4561 -- be done as part of an allocator evaluation (storage pool
4562 -- or secondary stack), never to a constrained target object
4563 -- passed in by the caller. Besides the assignment being
4564 -- unneeded in this case, it avoids problems with trying to
4565 -- generate a dispatching assignment when the return expression
4566 -- is a nonlimited descendant of a limited interface (the
4567 -- interface has no assignment operation).
4569 if Present
(Ret_Obj_Expr
)
4570 and then not No_Initialization
(Ret_Obj_Decl
)
4571 and then not Is_Interface
(Ret_Obj_Typ
)
4574 Make_Assignment_Statement
(Loc
,
4575 Name
=> New_Occurrence_Of
(Ret_Obj_Id
, Loc
),
4576 Expression
=> Relocate_Node
(Ret_Obj_Expr
));
4578 Set_Etype
(Name
(Init_Assignment
), Etype
(Ret_Obj_Id
));
4579 Set_Assignment_OK
(Name
(Init_Assignment
));
4580 Set_No_Ctrl_Actions
(Init_Assignment
);
4582 Set_Parent
(Name
(Init_Assignment
), Init_Assignment
);
4583 Set_Parent
(Expression
(Init_Assignment
), Init_Assignment
);
4585 Set_Expression
(Ret_Obj_Decl
, Empty
);
4587 if Is_Class_Wide_Type
(Etype
(Ret_Obj_Id
))
4588 and then not Is_Class_Wide_Type
4589 (Etype
(Expression
(Init_Assignment
)))
4591 Rewrite
(Expression
(Init_Assignment
),
4592 Make_Type_Conversion
(Loc
,
4594 New_Occurrence_Of
(Etype
(Ret_Obj_Id
), Loc
),
4596 Relocate_Node
(Expression
(Init_Assignment
))));
4599 -- In the case of functions where the calling context can
4600 -- determine the form of allocation needed, initialization
4601 -- is done with each part of the if statement that handles
4602 -- the different forms of allocation (this is true for
4603 -- unconstrained and tagged result subtypes).
4605 if Is_Constrained
(Ret_Typ
)
4606 and then not Is_Tagged_Type
(Underlying_Type
(Ret_Typ
))
4608 Insert_After
(Ret_Obj_Decl
, Init_Assignment
);
4612 -- When the function's subtype is unconstrained, a run-time
4613 -- test is needed to determine the form of allocation to use
4614 -- for the return object. The function has an implicit formal
4615 -- parameter indicating this. If the BIP_Alloc_Form formal has
4616 -- the value one, then the caller has passed access to an
4617 -- existing object for use as the return object. If the value
4618 -- is two, then the return object must be allocated on the
4619 -- secondary stack. Otherwise, the object must be allocated in
4620 -- a storage pool (currently only supported for the global
4621 -- heap, user-defined storage pools TBD ???). We generate an
4622 -- if statement to test the implicit allocation formal and
4623 -- initialize a local access value appropriately, creating
4624 -- allocators in the secondary stack and global heap cases.
4625 -- The special formal also exists and must be tested when the
4626 -- function has a tagged result, even when the result subtype
4627 -- is constrained, because in general such functions can be
4628 -- called in dispatching contexts and must be handled similarly
4629 -- to functions with a class-wide result.
4631 if not Is_Constrained
(Ret_Typ
)
4632 or else Is_Tagged_Type
(Underlying_Type
(Ret_Typ
))
4635 Build_In_Place_Formal
(Func_Id
, BIP_Alloc_Form
);
4638 Pool_Id
: constant Entity_Id
:=
4639 Make_Temporary
(Loc
, 'P');
4640 Alloc_Obj_Id
: Entity_Id
;
4641 Alloc_Obj_Decl
: Node_Id
;
4642 Alloc_If_Stmt
: Node_Id
;
4643 Heap_Allocator
: Node_Id
;
4644 Pool_Decl
: Node_Id
;
4645 Pool_Allocator
: Node_Id
;
4646 Ptr_Type_Decl
: Node_Id
;
4647 Ref_Type
: Entity_Id
;
4648 SS_Allocator
: Node_Id
;
4651 -- Reuse the itype created for the function's implicit
4652 -- access formal. This avoids the need to create a new
4653 -- access type here, plus it allows assigning the access
4654 -- formal directly without applying a conversion.
4656 -- Ref_Type := Etype (Object_Access);
4658 -- Create an access type designating the function's
4661 Ref_Type
:= Make_Temporary
(Loc
, 'A');
4664 Make_Full_Type_Declaration
(Loc
,
4665 Defining_Identifier
=> Ref_Type
,
4667 Make_Access_To_Object_Definition
(Loc
,
4668 All_Present
=> True,
4669 Subtype_Indication
=>
4670 New_Occurrence_Of
(Ret_Obj_Typ
, Loc
)));
4672 Insert_Before
(Ret_Obj_Decl
, Ptr_Type_Decl
);
4674 -- Create an access object that will be initialized to an
4675 -- access value denoting the return object, either coming
4676 -- from an implicit access value passed in by the caller
4677 -- or from the result of an allocator.
4679 Alloc_Obj_Id
:= Make_Temporary
(Loc
, 'R');
4680 Set_Etype
(Alloc_Obj_Id
, Ref_Type
);
4683 Make_Object_Declaration
(Loc
,
4684 Defining_Identifier
=> Alloc_Obj_Id
,
4685 Object_Definition
=>
4686 New_Occurrence_Of
(Ref_Type
, Loc
));
4688 Insert_Before
(Ret_Obj_Decl
, Alloc_Obj_Decl
);
4690 -- Create allocators for both the secondary stack and
4691 -- global heap. If there's an initialization expression,
4692 -- then create these as initialized allocators.
4694 if Present
(Ret_Obj_Expr
)
4695 and then not No_Initialization
(Ret_Obj_Decl
)
4697 -- Always use the type of the expression for the
4698 -- qualified expression, rather than the result type.
4699 -- In general we cannot always use the result type
4700 -- for the allocator, because the expression might be
4701 -- of a specific type, such as in the case of an
4702 -- aggregate or even a nonlimited object when the
4703 -- result type is a limited class-wide interface type.
4706 Make_Allocator
(Loc
,
4708 Make_Qualified_Expression
(Loc
,
4711 (Etype
(Ret_Obj_Expr
), Loc
),
4712 Expression
=> New_Copy_Tree
(Ret_Obj_Expr
)));
4715 -- If the function returns a class-wide type we cannot
4716 -- use the return type for the allocator. Instead we
4717 -- use the type of the expression, which must be an
4718 -- aggregate of a definite type.
4720 if Is_Class_Wide_Type
(Ret_Obj_Typ
) then
4722 Make_Allocator
(Loc
,
4725 (Etype
(Ret_Obj_Expr
), Loc
));
4728 Make_Allocator
(Loc
,
4730 New_Occurrence_Of
(Ret_Obj_Typ
, Loc
));
4733 -- If the object requires default initialization then
4734 -- that will happen later following the elaboration of
4735 -- the object renaming. If we don't turn it off here
4736 -- then the object will be default initialized twice.
4738 Set_No_Initialization
(Heap_Allocator
);
4741 -- The Pool_Allocator is just like the Heap_Allocator,
4742 -- except we set Storage_Pool and Procedure_To_Call so
4743 -- it will use the user-defined storage pool.
4745 Pool_Allocator
:= New_Copy_Tree
(Heap_Allocator
);
4747 -- Do not generate the renaming of the build-in-place
4748 -- pool parameter on ZFP because the parameter is not
4749 -- created in the first place.
4751 if RTE_Available
(RE_Root_Storage_Pool_Ptr
) then
4753 Make_Object_Renaming_Declaration
(Loc
,
4754 Defining_Identifier
=> Pool_Id
,
4757 (RTE
(RE_Root_Storage_Pool
), Loc
),
4759 Make_Explicit_Dereference
(Loc
,
4761 (Build_In_Place_Formal
4762 (Func_Id
, BIP_Storage_Pool
), Loc
)));
4763 Set_Storage_Pool
(Pool_Allocator
, Pool_Id
);
4764 Set_Procedure_To_Call
4765 (Pool_Allocator
, RTE
(RE_Allocate_Any
));
4767 Pool_Decl
:= Make_Null_Statement
(Loc
);
4770 -- If the No_Allocators restriction is active, then only
4771 -- an allocator for secondary stack allocation is needed.
4772 -- It's OK for such allocators to have Comes_From_Source
4773 -- set to False, because gigi knows not to flag them as
4774 -- being a violation of No_Implicit_Heap_Allocations.
4776 if Restriction_Active
(No_Allocators
) then
4777 SS_Allocator
:= Heap_Allocator
;
4778 Heap_Allocator
:= Make_Null
(Loc
);
4779 Pool_Allocator
:= Make_Null
(Loc
);
4781 -- Otherwise the heap and pool allocators may be needed,
4782 -- so we make another allocator for secondary stack
4786 SS_Allocator
:= New_Copy_Tree
(Heap_Allocator
);
4788 -- The heap and pool allocators are marked as
4789 -- Comes_From_Source since they correspond to an
4790 -- explicit user-written allocator (that is, it will
4791 -- only be executed on behalf of callers that call the
4792 -- function as initialization for such an allocator).
4793 -- Prevents errors when No_Implicit_Heap_Allocations
4796 Set_Comes_From_Source
(Heap_Allocator
, True);
4797 Set_Comes_From_Source
(Pool_Allocator
, True);
4800 -- The allocator is returned on the secondary stack.
4802 Set_Storage_Pool
(SS_Allocator
, RTE
(RE_SS_Pool
));
4803 Set_Procedure_To_Call
4804 (SS_Allocator
, RTE
(RE_SS_Allocate
));
4806 -- The allocator is returned on the secondary stack,
4807 -- so indicate that the function return, as well as
4808 -- all blocks that encloses the allocator, must not
4809 -- release it. The flags must be set now because
4810 -- the decision to use the secondary stack is done
4811 -- very late in the course of expanding the return
4812 -- statement, past the point where these flags are
4815 Set_Uses_Sec_Stack
(Func_Id
);
4816 Set_Uses_Sec_Stack
(Return_Statement_Entity
(N
));
4817 Set_Sec_Stack_Needed_For_Return
4818 (Return_Statement_Entity
(N
));
4819 Set_Enclosing_Sec_Stack_Return
(N
);
4821 -- Create an if statement to test the BIP_Alloc_Form
4822 -- formal and initialize the access object to either the
4823 -- BIP_Object_Access formal (BIP_Alloc_Form =
4824 -- Caller_Allocation), the result of allocating the
4825 -- object in the secondary stack (BIP_Alloc_Form =
4826 -- Secondary_Stack), or else an allocator to create the
4827 -- return object in the heap or user-defined pool
4828 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
4830 -- ??? An unchecked type conversion must be made in the
4831 -- case of assigning the access object formal to the
4832 -- local access object, because a normal conversion would
4833 -- be illegal in some cases (such as converting access-
4834 -- to-unconstrained to access-to-constrained), but the
4835 -- the unchecked conversion will presumably fail to work
4836 -- right in just such cases. It's not clear at all how to
4840 Make_If_Statement
(Loc
,
4844 New_Occurrence_Of
(Obj_Alloc_Formal
, Loc
),
4846 Make_Integer_Literal
(Loc
,
4847 UI_From_Int
(BIP_Allocation_Form
'Pos
4848 (Caller_Allocation
)))),
4850 Then_Statements
=> New_List
(
4851 Make_Assignment_Statement
(Loc
,
4853 New_Occurrence_Of
(Alloc_Obj_Id
, Loc
),
4855 Make_Unchecked_Type_Conversion
(Loc
,
4857 New_Occurrence_Of
(Ref_Type
, Loc
),
4859 New_Occurrence_Of
(Obj_Acc_Formal
, Loc
)))),
4861 Elsif_Parts
=> New_List
(
4862 Make_Elsif_Part
(Loc
,
4866 New_Occurrence_Of
(Obj_Alloc_Formal
, Loc
),
4868 Make_Integer_Literal
(Loc
,
4869 UI_From_Int
(BIP_Allocation_Form
'Pos
4870 (Secondary_Stack
)))),
4872 Then_Statements
=> New_List
(
4873 Make_Assignment_Statement
(Loc
,
4875 New_Occurrence_Of
(Alloc_Obj_Id
, Loc
),
4876 Expression
=> SS_Allocator
))),
4878 Make_Elsif_Part
(Loc
,
4882 New_Occurrence_Of
(Obj_Alloc_Formal
, Loc
),
4884 Make_Integer_Literal
(Loc
,
4885 UI_From_Int
(BIP_Allocation_Form
'Pos
4888 Then_Statements
=> New_List
(
4889 Build_Heap_Allocator
4890 (Temp_Id
=> Alloc_Obj_Id
,
4891 Temp_Typ
=> Ref_Type
,
4893 Ret_Typ
=> Ret_Obj_Typ
,
4894 Alloc_Expr
=> Heap_Allocator
)))),
4896 Else_Statements
=> New_List
(
4898 Build_Heap_Allocator
4899 (Temp_Id
=> Alloc_Obj_Id
,
4900 Temp_Typ
=> Ref_Type
,
4902 Ret_Typ
=> Ret_Obj_Typ
,
4903 Alloc_Expr
=> Pool_Allocator
)));
4905 -- If a separate initialization assignment was created
4906 -- earlier, append that following the assignment of the
4907 -- implicit access formal to the access object, to ensure
4908 -- that the return object is initialized in that case. In
4909 -- this situation, the target of the assignment must be
4910 -- rewritten to denote a dereference of the access to the
4911 -- return object passed in by the caller.
4913 if Present
(Init_Assignment
) then
4914 Rewrite
(Name
(Init_Assignment
),
4915 Make_Explicit_Dereference
(Loc
,
4916 Prefix
=> New_Occurrence_Of
(Alloc_Obj_Id
, Loc
)));
4918 Set_Etype
(Name
(Init_Assignment
), Etype
(Ret_Obj_Id
));
4921 (Then_Statements
(Alloc_If_Stmt
), Init_Assignment
);
4924 Insert_Before
(Ret_Obj_Decl
, Alloc_If_Stmt
);
4926 -- Remember the local access object for use in the
4927 -- dereference of the renaming created below.
4929 Obj_Acc_Formal
:= Alloc_Obj_Id
;
4933 -- Replace the return object declaration with a renaming of a
4934 -- dereference of the access value designating the return
4938 Make_Explicit_Dereference
(Loc
,
4939 Prefix
=> New_Occurrence_Of
(Obj_Acc_Formal
, Loc
));
4941 Rewrite
(Ret_Obj_Decl
,
4942 Make_Object_Renaming_Declaration
(Loc
,
4943 Defining_Identifier
=> Ret_Obj_Id
,
4944 Access_Definition
=> Empty
,
4945 Subtype_Mark
=> New_Occurrence_Of
(Ret_Obj_Typ
, Loc
),
4946 Name
=> Obj_Acc_Deref
));
4948 Set_Renamed_Object
(Ret_Obj_Id
, Obj_Acc_Deref
);
4952 -- Case where we do not build a block
4955 -- We're about to drop Return_Object_Declarations on the floor, so
4956 -- we need to insert it, in case it got expanded into useful code.
4957 -- Remove side effects from expression, which may be duplicated in
4958 -- subsequent checks (see Expand_Simple_Function_Return).
4960 Insert_List_Before
(N
, Return_Object_Declarations
(N
));
4961 Remove_Side_Effects
(Exp
);
4963 -- Build simple_return_statement that returns the expression directly
4965 Return_Stmt
:= Make_Simple_Return_Statement
(Loc
, Expression
=> Exp
);
4966 Result
:= Return_Stmt
;
4969 -- Set the flag to prevent infinite recursion
4971 Set_Comes_From_Extended_Return_Statement
(Return_Stmt
);
4973 Rewrite
(N
, Result
);
4975 end Expand_N_Extended_Return_Statement
;
4977 ----------------------------
4978 -- Expand_N_Function_Call --
4979 ----------------------------
4981 procedure Expand_N_Function_Call
(N
: Node_Id
) is
4984 end Expand_N_Function_Call
;
4986 ---------------------------------------
4987 -- Expand_N_Procedure_Call_Statement --
4988 ---------------------------------------
4990 procedure Expand_N_Procedure_Call_Statement
(N
: Node_Id
) is
4991 Save_Ghost_Mode
: constant Ghost_Mode_Type
:= Ghost_Mode
;
4994 -- The procedure call is Ghost when the name is Ghost. Set the mode now
4995 -- to ensure that any nodes generated during expansion are properly set
5001 Ghost_Mode
:= Save_Ghost_Mode
;
5002 end Expand_N_Procedure_Call_Statement
;
5004 --------------------------------------
5005 -- Expand_N_Simple_Return_Statement --
5006 --------------------------------------
5008 procedure Expand_N_Simple_Return_Statement
(N
: Node_Id
) is
5010 -- Defend against previous errors (i.e. the return statement calls a
5011 -- function that is not available in configurable runtime).
5013 if Present
(Expression
(N
))
5014 and then Nkind
(Expression
(N
)) = N_Empty
5016 Check_Error_Detected
;
5020 -- Distinguish the function and non-function cases:
5022 case Ekind
(Return_Applies_To
(Return_Statement_Entity
(N
))) is
5025 E_Generic_Function
=>
5026 Expand_Simple_Function_Return
(N
);
5029 E_Generic_Procedure |
5032 E_Return_Statement
=>
5033 Expand_Non_Function_Return
(N
);
5036 raise Program_Error
;
5040 when RE_Not_Available
=>
5042 end Expand_N_Simple_Return_Statement
;
5044 ------------------------------
5045 -- Expand_N_Subprogram_Body --
5046 ------------------------------
5048 -- Add poll call if ATC polling is enabled, unless the body will be inlined
5051 -- Add dummy push/pop label nodes at start and end to clear any local
5052 -- exception indications if local-exception-to-goto optimization is active.
5054 -- Add return statement if last statement in body is not a return statement
5055 -- (this makes things easier on Gigi which does not want to have to handle
5056 -- a missing return).
5058 -- Add call to Activate_Tasks if body is a task activator
5060 -- Deal with possible detection of infinite recursion
5062 -- Eliminate body completely if convention stubbed
5064 -- Encode entity names within body, since we will not need to reference
5065 -- these entities any longer in the front end.
5067 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5069 -- Reset Pure indication if any parameter has root type System.Address
5070 -- or has any parameters of limited types, where limited means that the
5071 -- run-time view is limited (i.e. the full type is limited).
5075 procedure Expand_N_Subprogram_Body
(N
: Node_Id
) is
5076 Body_Id
: constant Entity_Id
:= Defining_Entity
(N
);
5077 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
5078 Loc
: constant Source_Ptr
:= Sloc
(N
);
5080 procedure Add_Return
(Spec_Id
: Entity_Id
; Stmts
: List_Id
);
5081 -- Append a return statement to the statement sequence Stmts if the last
5082 -- statement is not already a return or a goto statement. Note that the
5083 -- latter test is not critical, it does not matter if we add a few extra
5084 -- returns, since they get eliminated anyway later on. Spec_Id denotes
5085 -- the corresponding spec of the subprogram body.
5091 procedure Add_Return
(Spec_Id
: Entity_Id
; Stmts
: List_Id
) is
5092 Last_Stmt
: Node_Id
;
5097 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5098 -- not relevant in this context since they are not executable.
5100 Last_Stmt
:= Last
(Stmts
);
5101 while Nkind
(Last_Stmt
) in N_Pop_xxx_Label
loop
5105 -- Now insert return unless last statement is a transfer
5107 if not Is_Transfer
(Last_Stmt
) then
5109 -- The source location for the return is the end label of the
5110 -- procedure if present. Otherwise use the sloc of the last
5111 -- statement in the list. If the list comes from a generated
5112 -- exception handler and we are not debugging generated code,
5113 -- all the statements within the handler are made invisible
5116 if Nkind
(Parent
(Stmts
)) = N_Exception_Handler
5117 and then not Comes_From_Source
(Parent
(Stmts
))
5119 Loc
:= Sloc
(Last_Stmt
);
5120 elsif Present
(End_Label
(HSS
)) then
5121 Loc
:= Sloc
(End_Label
(HSS
));
5123 Loc
:= Sloc
(Last_Stmt
);
5126 -- Append return statement, and set analyzed manually. We can't
5127 -- call Analyze on this return since the scope is wrong.
5129 -- Note: it almost works to push the scope and then do the Analyze
5130 -- call, but something goes wrong in some weird cases and it is
5131 -- not worth worrying about ???
5133 Stmt
:= Make_Simple_Return_Statement
(Loc
);
5135 -- The return statement is handled properly, and the call to the
5136 -- postcondition, inserted below, does not require information
5137 -- from the body either. However, that call is analyzed in the
5138 -- enclosing scope, and an elaboration check might improperly be
5139 -- added to it. A guard in Sem_Elab is needed to prevent that
5140 -- spurious check, see Check_Elab_Call.
5142 Append_To
(Stmts
, Stmt
);
5143 Set_Analyzed
(Stmt
);
5145 -- Call the _Postconditions procedure if the related subprogram
5146 -- has contract assertions that need to be verified on exit.
5148 if Ekind
(Spec_Id
) = E_Procedure
5149 and then Present
(Postconditions_Proc
(Spec_Id
))
5151 Insert_Action
(Stmt
,
5152 Make_Procedure_Call_Statement
(Loc
,
5154 New_Occurrence_Of
(Postconditions_Proc
(Spec_Id
), Loc
)));
5161 Save_Ghost_Mode
: constant Ghost_Mode_Type
:= Ghost_Mode
;
5165 Spec_Id
: Entity_Id
;
5167 -- Start of processing for Expand_N_Subprogram_Body
5170 if Present
(Corresponding_Spec
(N
)) then
5171 Spec_Id
:= Corresponding_Spec
(N
);
5176 -- If this is a Pure function which has any parameters whose root type
5177 -- is System.Address, reset the Pure indication.
5178 -- This check is also performed when the subprogram is frozen, but we
5179 -- repeat it on the body so that the indication is consistent, and so
5180 -- it applies as well to bodies without separate specifications.
5182 if Is_Pure
(Spec_Id
)
5183 and then Is_Subprogram
(Spec_Id
)
5184 and then not Has_Pragma_Pure_Function
(Spec_Id
)
5186 Check_Function_With_Address_Parameter
(Spec_Id
);
5188 if Spec_Id
/= Body_Id
then
5189 Set_Is_Pure
(Body_Id
, Is_Pure
(Spec_Id
));
5193 -- The subprogram body is Ghost when it is stand alone and subject to
5194 -- pragma Ghost or the corresponding spec is Ghost. To accomodate both
5195 -- cases, set the mode now to ensure that any nodes generated during
5196 -- expansion are marked as Ghost.
5198 Set_Ghost_Mode
(N
, Spec_Id
);
5200 -- Set L to either the list of declarations if present, or to the list
5201 -- of statements if no declarations are present. This is used to insert
5202 -- new stuff at the start.
5204 if Is_Non_Empty_List
(Declarations
(N
)) then
5205 L
:= Declarations
(N
);
5207 L
:= Statements
(HSS
);
5210 -- If local-exception-to-goto optimization active, insert dummy push
5211 -- statements at start, and dummy pop statements at end, but inhibit
5212 -- this if we have No_Exception_Handlers, since they are useless and
5213 -- intefere with analysis, e.g. by codepeer.
5215 if (Debug_Flag_Dot_G
5216 or else Restriction_Active
(No_Exception_Propagation
))
5217 and then not Restriction_Active
(No_Exception_Handlers
)
5218 and then not CodePeer_Mode
5219 and then Is_Non_Empty_List
(L
)
5222 FS
: constant Node_Id
:= First
(L
);
5223 FL
: constant Source_Ptr
:= Sloc
(FS
);
5228 -- LS points to either last statement, if statements are present
5229 -- or to the last declaration if there are no statements present.
5230 -- It is the node after which the pop's are generated.
5232 if Is_Non_Empty_List
(Statements
(HSS
)) then
5233 LS
:= Last
(Statements
(HSS
));
5240 Insert_List_Before_And_Analyze
(FS
, New_List
(
5241 Make_Push_Constraint_Error_Label
(FL
),
5242 Make_Push_Program_Error_Label
(FL
),
5243 Make_Push_Storage_Error_Label
(FL
)));
5245 Insert_List_After_And_Analyze
(LS
, New_List
(
5246 Make_Pop_Constraint_Error_Label
(LL
),
5247 Make_Pop_Program_Error_Label
(LL
),
5248 Make_Pop_Storage_Error_Label
(LL
)));
5252 -- Need poll on entry to subprogram if polling enabled. We only do this
5253 -- for non-empty subprograms, since it does not seem necessary to poll
5254 -- for a dummy null subprogram.
5256 if Is_Non_Empty_List
(L
) then
5258 -- Do not add a polling call if the subprogram is to be inlined by
5259 -- the back-end, to avoid repeated calls with multiple inlinings.
5261 if Is_Inlined
(Spec_Id
)
5262 and then Front_End_Inlining
5263 and then Optimization_Level
> 1
5267 Generate_Poll_Call
(First
(L
));
5271 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5273 if Init_Or_Norm_Scalars
and then Is_Subprogram
(Spec_Id
) then
5279 -- Loop through formals
5281 F
:= First_Formal
(Spec_Id
);
5282 while Present
(F
) loop
5283 if Is_Scalar_Type
(Etype
(F
))
5284 and then Ekind
(F
) = E_Out_Parameter
5286 Check_Restriction
(No_Default_Initialization
, F
);
5288 -- Insert the initialization. We turn off validity checks
5289 -- for this assignment, since we do not want any check on
5290 -- the initial value itself (which may well be invalid).
5291 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5294 Make_Assignment_Statement
(Loc
,
5295 Name
=> New_Occurrence_Of
(F
, Loc
),
5296 Expression
=> Get_Simple_Init_Val
(Etype
(F
), N
));
5297 Set_Suppress_Assignment_Checks
(A
);
5299 Insert_Before_And_Analyze
(First
(L
),
5300 A
, Suppress
=> Validity_Check
);
5308 -- Clear out statement list for stubbed procedure
5310 if Present
(Corresponding_Spec
(N
)) then
5311 Set_Elaboration_Flag
(N
, Spec_Id
);
5313 if Convention
(Spec_Id
) = Convention_Stubbed
5314 or else Is_Eliminated
(Spec_Id
)
5316 Set_Declarations
(N
, Empty_List
);
5317 Set_Handled_Statement_Sequence
(N
,
5318 Make_Handled_Sequence_Of_Statements
(Loc
,
5319 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
5321 Ghost_Mode
:= Save_Ghost_Mode
;
5326 -- Create a set of discriminals for the next protected subprogram body
5328 if Is_List_Member
(N
)
5329 and then Present
(Parent
(List_Containing
(N
)))
5330 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
5331 and then Present
(Next_Protected_Operation
(N
))
5333 Set_Discriminals
(Parent
(Base_Type
(Scope
(Spec_Id
))));
5336 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5337 -- subprograms with no specs are not frozen.
5340 Typ
: constant Entity_Id
:= Etype
(Spec_Id
);
5341 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
5344 if not Acts_As_Spec
(N
)
5345 and then Nkind
(Parent
(Parent
(Spec_Id
))) /=
5346 N_Subprogram_Body_Stub
5350 elsif Is_Limited_View
(Typ
) then
5351 Set_Returns_By_Ref
(Spec_Id
);
5353 elsif Present
(Utyp
) and then CW_Or_Has_Controlled_Part
(Utyp
) then
5354 Set_Returns_By_Ref
(Spec_Id
);
5358 -- For a procedure, we add a return for all possible syntactic ends of
5361 if Ekind_In
(Spec_Id
, E_Procedure
, E_Generic_Procedure
) then
5362 Add_Return
(Spec_Id
, Statements
(HSS
));
5364 if Present
(Exception_Handlers
(HSS
)) then
5365 Except_H
:= First_Non_Pragma
(Exception_Handlers
(HSS
));
5366 while Present
(Except_H
) loop
5367 Add_Return
(Spec_Id
, Statements
(Except_H
));
5368 Next_Non_Pragma
(Except_H
);
5372 -- For a function, we must deal with the case where there is at least
5373 -- one missing return. What we do is to wrap the entire body of the
5374 -- function in a block:
5387 -- raise Program_Error;
5390 -- This approach is necessary because the raise must be signalled to the
5391 -- caller, not handled by any local handler (RM 6.4(11)).
5393 -- Note: we do not need to analyze the constructed sequence here, since
5394 -- it has no handler, and an attempt to analyze the handled statement
5395 -- sequence twice is risky in various ways (e.g. the issue of expanding
5396 -- cleanup actions twice).
5398 elsif Has_Missing_Return
(Spec_Id
) then
5400 Hloc
: constant Source_Ptr
:= Sloc
(HSS
);
5401 Blok
: constant Node_Id
:=
5402 Make_Block_Statement
(Hloc
,
5403 Handled_Statement_Sequence
=> HSS
);
5404 Rais
: constant Node_Id
:=
5405 Make_Raise_Program_Error
(Hloc
,
5406 Reason
=> PE_Missing_Return
);
5409 Set_Handled_Statement_Sequence
(N
,
5410 Make_Handled_Sequence_Of_Statements
(Hloc
,
5411 Statements
=> New_List
(Blok
, Rais
)));
5413 Push_Scope
(Spec_Id
);
5420 -- If subprogram contains a parameterless recursive call, then we may
5421 -- have an infinite recursion, so see if we can generate code to check
5422 -- for this possibility if storage checks are not suppressed.
5424 if Ekind
(Spec_Id
) = E_Procedure
5425 and then Has_Recursive_Call
(Spec_Id
)
5426 and then not Storage_Checks_Suppressed
(Spec_Id
)
5428 Detect_Infinite_Recursion
(N
, Spec_Id
);
5431 -- Set to encode entity names in package body before gigi is called
5433 Qualify_Entity_Names
(N
);
5435 -- If we are unnesting procedures, and this is an outer level procedure
5436 -- with nested subprograms, do the unnesting operation now.
5438 if Opt
.Unnest_Subprogram_Mode
5440 -- We are only interested in subprograms (not generic subprograms)
5442 and then Is_Subprogram
(Spec_Id
)
5444 -- Only deal with outer level subprograms. Nested subprograms are
5445 -- handled as part of dealing with the outer level subprogram in
5446 -- which they are nested.
5448 and then Enclosing_Subprogram
(Spec_Id
) = Empty
5450 -- We are only interested in subprograms that have nested subprograms
5452 and then Has_Nested_Subprogram
(Spec_Id
)
5454 Unest_Bodies
.Append
((Spec_Id
, N
));
5457 Ghost_Mode
:= Save_Ghost_Mode
;
5458 end Expand_N_Subprogram_Body
;
5460 -----------------------------------
5461 -- Expand_N_Subprogram_Body_Stub --
5462 -----------------------------------
5464 procedure Expand_N_Subprogram_Body_Stub
(N
: Node_Id
) is
5466 if Present
(Corresponding_Body
(N
)) then
5467 Expand_N_Subprogram_Body
(
5468 Unit_Declaration_Node
(Corresponding_Body
(N
)));
5470 end Expand_N_Subprogram_Body_Stub
;
5472 -------------------------------------
5473 -- Expand_N_Subprogram_Declaration --
5474 -------------------------------------
5476 -- If the declaration appears within a protected body, it is a private
5477 -- operation of the protected type. We must create the corresponding
5478 -- protected subprogram an associated formals. For a normal protected
5479 -- operation, this is done when expanding the protected type declaration.
5481 -- If the declaration is for a null procedure, emit null body
5483 procedure Expand_N_Subprogram_Declaration
(N
: Node_Id
) is
5484 Loc
: constant Source_Ptr
:= Sloc
(N
);
5485 Subp
: constant Entity_Id
:= Defining_Entity
(N
);
5487 procedure Build_Procedure_Form
;
5488 -- Create a procedure declaration which emulates the behavior of
5489 -- function Subp, for C-compatible generation.
5491 --------------------------
5492 -- Build_Procedure_Form --
5493 --------------------------
5495 procedure Build_Procedure_Form
is
5496 Func_Formal
: Entity_Id
;
5497 Proc_Formals
: List_Id
;
5500 Proc_Formals
:= New_List
;
5502 -- Create a list of formal parameters with the same types as the
5505 Func_Formal
:= First_Formal
(Subp
);
5506 while Present
(Func_Formal
) loop
5507 Append_To
(Proc_Formals
,
5508 Make_Parameter_Specification
(Loc
,
5509 Defining_Identifier
=>
5510 Make_Defining_Identifier
(Loc
, Chars
(Func_Formal
)),
5512 New_Occurrence_Of
(Etype
(Func_Formal
), Loc
)));
5514 Next_Formal
(Func_Formal
);
5517 -- Add an extra out parameter to carry the function result
5520 Name_Buffer
(1 .. Name_Len
) := "RESULT";
5521 Append_To
(Proc_Formals
,
5522 Make_Parameter_Specification
(Loc
,
5523 Defining_Identifier
=>
5524 Make_Defining_Identifier
(Loc
, Chars
=> Name_Find
),
5525 Out_Present
=> True,
5526 Parameter_Type
=> New_Occurrence_Of
(Etype
(Subp
), Loc
)));
5528 -- The new procedure declaration is inserted immediately after the
5529 -- function declaration. The processing in Build_Procedure_Body_Form
5530 -- relies on this order.
5532 Insert_After_And_Analyze
(N
,
5533 Make_Subprogram_Declaration
(Loc
,
5535 Make_Procedure_Specification
(Loc
,
5536 Defining_Unit_Name
=>
5537 Make_Defining_Identifier
(Loc
, Chars
(Subp
)),
5538 Parameter_Specifications
=> Proc_Formals
)));
5540 -- Mark the function as having a procedure form
5542 Set_Rewritten_For_C
(Subp
);
5543 end Build_Procedure_Form
;
5547 Scop
: constant Entity_Id
:= Scope
(Subp
);
5549 Prot_Decl
: Node_Id
;
5550 Prot_Id
: Entity_Id
;
5552 -- Start of processing for Expand_N_Subprogram_Declaration
5555 -- In SPARK, subprogram declarations are only allowed in package
5558 if Nkind
(Parent
(N
)) /= N_Package_Specification
then
5559 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5560 Check_SPARK_05_Restriction
5561 ("subprogram declaration is not a library item", N
);
5563 elsif Present
(Next
(N
))
5564 and then Nkind
(Next
(N
)) = N_Pragma
5565 and then Get_Pragma_Id
(Pragma_Name
(Next
(N
))) = Pragma_Import
5567 -- In SPARK, subprogram declarations are also permitted in
5568 -- declarative parts when immediately followed by a corresponding
5569 -- pragma Import. We only check here that there is some pragma
5574 Check_SPARK_05_Restriction
5575 ("subprogram declaration is not allowed here", N
);
5579 -- Deal with case of protected subprogram. Do not generate protected
5580 -- operation if operation is flagged as eliminated.
5582 if Is_List_Member
(N
)
5583 and then Present
(Parent
(List_Containing
(N
)))
5584 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
5585 and then Is_Protected_Type
(Scop
)
5587 if No
(Protected_Body_Subprogram
(Subp
))
5588 and then not Is_Eliminated
(Subp
)
5591 Make_Subprogram_Declaration
(Loc
,
5593 Build_Protected_Sub_Specification
5594 (N
, Scop
, Unprotected_Mode
));
5596 -- The protected subprogram is declared outside of the protected
5597 -- body. Given that the body has frozen all entities so far, we
5598 -- analyze the subprogram and perform freezing actions explicitly.
5599 -- including the generation of an explicit freeze node, to ensure
5600 -- that gigi has the proper order of elaboration.
5601 -- If the body is a subunit, the insertion point is before the
5602 -- stub in the parent.
5604 Prot_Bod
:= Parent
(List_Containing
(N
));
5606 if Nkind
(Parent
(Prot_Bod
)) = N_Subunit
then
5607 Prot_Bod
:= Corresponding_Stub
(Parent
(Prot_Bod
));
5610 Insert_Before
(Prot_Bod
, Prot_Decl
);
5611 Prot_Id
:= Defining_Unit_Name
(Specification
(Prot_Decl
));
5612 Set_Has_Delayed_Freeze
(Prot_Id
);
5614 Push_Scope
(Scope
(Scop
));
5615 Analyze
(Prot_Decl
);
5616 Freeze_Before
(N
, Prot_Id
);
5617 Set_Protected_Body_Subprogram
(Subp
, Prot_Id
);
5619 -- Create protected operation as well. Even though the operation
5620 -- is only accessible within the body, it is possible to make it
5621 -- available outside of the protected object by using 'Access to
5622 -- provide a callback, so build protected version in all cases.
5625 Make_Subprogram_Declaration
(Loc
,
5627 Build_Protected_Sub_Specification
(N
, Scop
, Protected_Mode
));
5628 Insert_Before
(Prot_Bod
, Prot_Decl
);
5629 Analyze
(Prot_Decl
);
5634 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
5635 -- cases this is superfluous because calls to it will be automatically
5636 -- inlined, but we definitely need the body if preconditions for the
5637 -- procedure are present.
5639 elsif Nkind
(Specification
(N
)) = N_Procedure_Specification
5640 and then Null_Present
(Specification
(N
))
5643 Bod
: constant Node_Id
:= Body_To_Inline
(N
);
5646 Set_Has_Completion
(Subp
, False);
5647 Append_Freeze_Action
(Subp
, Bod
);
5649 -- The body now contains raise statements, so calls to it will
5652 Set_Is_Inlined
(Subp
, False);
5656 -- When generating C code, transform a function that returns a
5657 -- constrained array type into a procedure with an out parameter
5658 -- that carries the return value.
5660 -- We skip this transformation for unchecked conversions, since they
5661 -- are not needed by the C generator (and this also produces cleaner
5664 if Modify_Tree_For_C
5665 and then Nkind
(Specification
(N
)) = N_Function_Specification
5666 and then Is_Array_Type
(Etype
(Subp
))
5667 and then Is_Constrained
(Etype
(Subp
))
5668 and then not Is_Unchecked_Conversion_Instance
(Subp
)
5670 Build_Procedure_Form
;
5672 end Expand_N_Subprogram_Declaration
;
5674 --------------------------------
5675 -- Expand_Non_Function_Return --
5676 --------------------------------
5678 procedure Expand_Non_Function_Return
(N
: Node_Id
) is
5679 pragma Assert
(No
(Expression
(N
)));
5681 Loc
: constant Source_Ptr
:= Sloc
(N
);
5682 Scope_Id
: Entity_Id
:= Return_Applies_To
(Return_Statement_Entity
(N
));
5683 Kind
: constant Entity_Kind
:= Ekind
(Scope_Id
);
5686 Goto_Stat
: Node_Id
;
5690 -- Call the _Postconditions procedure if the related subprogram has
5691 -- contract assertions that need to be verified on exit.
5693 if Ekind_In
(Scope_Id
, E_Entry
, E_Entry_Family
, E_Procedure
)
5694 and then Present
(Postconditions_Proc
(Scope_Id
))
5697 Make_Procedure_Call_Statement
(Loc
,
5698 Name
=> New_Occurrence_Of
(Postconditions_Proc
(Scope_Id
), Loc
)));
5701 -- If it is a return from a procedure do no extra steps
5703 if Kind
= E_Procedure
or else Kind
= E_Generic_Procedure
then
5706 -- If it is a nested return within an extended one, replace it with a
5707 -- return of the previously declared return object.
5709 elsif Kind
= E_Return_Statement
then
5711 Make_Simple_Return_Statement
(Loc
,
5713 New_Occurrence_Of
(First_Entity
(Scope_Id
), Loc
)));
5714 Set_Comes_From_Extended_Return_Statement
(N
);
5715 Set_Return_Statement_Entity
(N
, Scope_Id
);
5716 Expand_Simple_Function_Return
(N
);
5720 pragma Assert
(Is_Entry
(Scope_Id
));
5722 -- Look at the enclosing block to see whether the return is from an
5723 -- accept statement or an entry body.
5725 for J
in reverse 0 .. Scope_Stack
.Last
loop
5726 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
5727 exit when Is_Concurrent_Type
(Scope_Id
);
5730 -- If it is a return from accept statement it is expanded as call to
5731 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
5733 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
5734 -- Expand_N_Accept_Alternative in exp_ch9.adb)
5736 if Is_Task_Type
(Scope_Id
) then
5739 Make_Procedure_Call_Statement
(Loc
,
5740 Name
=> New_Occurrence_Of
(RTE
(RE_Complete_Rendezvous
), Loc
));
5741 Insert_Before
(N
, Call
);
5742 -- why not insert actions here???
5745 Acc_Stat
:= Parent
(N
);
5746 while Nkind
(Acc_Stat
) /= N_Accept_Statement
loop
5747 Acc_Stat
:= Parent
(Acc_Stat
);
5750 Lab_Node
:= Last
(Statements
5751 (Handled_Statement_Sequence
(Acc_Stat
)));
5753 Goto_Stat
:= Make_Goto_Statement
(Loc
,
5754 Name
=> New_Occurrence_Of
5755 (Entity
(Identifier
(Lab_Node
)), Loc
));
5757 Set_Analyzed
(Goto_Stat
);
5759 Rewrite
(N
, Goto_Stat
);
5762 -- If it is a return from an entry body, put a Complete_Entry_Body call
5763 -- in front of the return.
5765 elsif Is_Protected_Type
(Scope_Id
) then
5767 Make_Procedure_Call_Statement
(Loc
,
5769 New_Occurrence_Of
(RTE
(RE_Complete_Entry_Body
), Loc
),
5770 Parameter_Associations
=> New_List
(
5771 Make_Attribute_Reference
(Loc
,
5774 (Find_Protection_Object
(Current_Scope
), Loc
),
5775 Attribute_Name
=> Name_Unchecked_Access
)));
5777 Insert_Before
(N
, Call
);
5780 end Expand_Non_Function_Return
;
5782 ---------------------------------------
5783 -- Expand_Protected_Object_Reference --
5784 ---------------------------------------
5786 function Expand_Protected_Object_Reference
5788 Scop
: Entity_Id
) return Node_Id
5790 Loc
: constant Source_Ptr
:= Sloc
(N
);
5797 Rec
:= Make_Identifier
(Loc
, Name_uObject
);
5798 Set_Etype
(Rec
, Corresponding_Record_Type
(Scop
));
5800 -- Find enclosing protected operation, and retrieve its first parameter,
5801 -- which denotes the enclosing protected object. If the enclosing
5802 -- operation is an entry, we are immediately within the protected body,
5803 -- and we can retrieve the object from the service entries procedure. A
5804 -- barrier function has the same signature as an entry. A barrier
5805 -- function is compiled within the protected object, but unlike
5806 -- protected operations its never needs locks, so that its protected
5807 -- body subprogram points to itself.
5809 Proc
:= Current_Scope
;
5810 while Present
(Proc
)
5811 and then Scope
(Proc
) /= Scop
5813 Proc
:= Scope
(Proc
);
5816 Corr
:= Protected_Body_Subprogram
(Proc
);
5820 -- Previous error left expansion incomplete.
5821 -- Nothing to do on this call.
5828 (First
(Parameter_Specifications
(Parent
(Corr
))));
5830 if Is_Subprogram
(Proc
) and then Proc
/= Corr
then
5832 -- Protected function or procedure
5834 Set_Entity
(Rec
, Param
);
5836 -- Rec is a reference to an entity which will not be in scope when
5837 -- the call is reanalyzed, and needs no further analysis.
5842 -- Entry or barrier function for entry body. The first parameter of
5843 -- the entry body procedure is pointer to the object. We create a
5844 -- local variable of the proper type, duplicating what is done to
5845 -- define _object later on.
5849 Obj_Ptr
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
5853 Make_Full_Type_Declaration
(Loc
,
5854 Defining_Identifier
=> Obj_Ptr
,
5856 Make_Access_To_Object_Definition
(Loc
,
5857 Subtype_Indication
=>
5859 (Corresponding_Record_Type
(Scop
), Loc
))));
5861 Insert_Actions
(N
, Decls
);
5862 Freeze_Before
(N
, Obj_Ptr
);
5865 Make_Explicit_Dereference
(Loc
,
5867 Unchecked_Convert_To
(Obj_Ptr
,
5868 New_Occurrence_Of
(Param
, Loc
)));
5870 -- Analyze new actual. Other actuals in calls are already analyzed
5871 -- and the list of actuals is not reanalyzed after rewriting.
5873 Set_Parent
(Rec
, N
);
5879 end Expand_Protected_Object_Reference
;
5881 --------------------------------------
5882 -- Expand_Protected_Subprogram_Call --
5883 --------------------------------------
5885 procedure Expand_Protected_Subprogram_Call
5892 procedure Freeze_Called_Function
;
5893 -- If it is a function call it can appear in elaboration code and
5894 -- the called entity must be frozen before the call. This must be
5895 -- done before the call is expanded, as the expansion may rewrite it
5896 -- to something other than a call (e.g. a temporary initialized in a
5897 -- transient block).
5899 ----------------------------
5900 -- Freeze_Called_Function --
5901 ----------------------------
5903 procedure Freeze_Called_Function
is
5905 if Ekind
(Subp
) = E_Function
then
5906 Freeze_Expression
(Name
(N
));
5908 end Freeze_Called_Function
;
5910 -- Start of processing for Expand_Protected_Subprogram_Call
5913 -- If the protected object is not an enclosing scope, this is an inter-
5914 -- object function call. Inter-object procedure calls are expanded by
5915 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
5916 -- subprogram being called is in the protected body being compiled, and
5917 -- if the protected object in the call is statically the enclosing type.
5918 -- The object may be an component of some other data structure, in which
5919 -- case this must be handled as an inter-object call.
5921 if not In_Open_Scopes
(Scop
)
5922 or else not Is_Entity_Name
(Name
(N
))
5924 if Nkind
(Name
(N
)) = N_Selected_Component
then
5925 Rec
:= Prefix
(Name
(N
));
5928 pragma Assert
(Nkind
(Name
(N
)) = N_Indexed_Component
);
5929 Rec
:= Prefix
(Prefix
(Name
(N
)));
5932 Freeze_Called_Function
;
5933 Build_Protected_Subprogram_Call
(N
,
5934 Name
=> New_Occurrence_Of
(Subp
, Sloc
(N
)),
5935 Rec
=> Convert_Concurrent
(Rec
, Etype
(Rec
)),
5939 Rec
:= Expand_Protected_Object_Reference
(N
, Scop
);
5945 Freeze_Called_Function
;
5946 Build_Protected_Subprogram_Call
(N
,
5953 -- Analyze and resolve the new call. The actuals have already been
5954 -- resolved, but expansion of a function call will add extra actuals
5955 -- if needed. Analysis of a procedure call already includes resolution.
5959 if Ekind
(Subp
) = E_Function
then
5960 Resolve
(N
, Etype
(Subp
));
5962 end Expand_Protected_Subprogram_Call
;
5964 -----------------------------------
5965 -- Expand_Simple_Function_Return --
5966 -----------------------------------
5968 -- The "simple" comes from the syntax rule simple_return_statement. The
5969 -- semantics are not at all simple.
5971 procedure Expand_Simple_Function_Return
(N
: Node_Id
) is
5972 Loc
: constant Source_Ptr
:= Sloc
(N
);
5974 Scope_Id
: constant Entity_Id
:=
5975 Return_Applies_To
(Return_Statement_Entity
(N
));
5976 -- The function we are returning from
5978 R_Type
: constant Entity_Id
:= Etype
(Scope_Id
);
5979 -- The result type of the function
5981 Utyp
: constant Entity_Id
:= Underlying_Type
(R_Type
);
5983 Exp
: Node_Id
:= Expression
(N
);
5984 pragma Assert
(Present
(Exp
));
5986 Exptyp
: constant Entity_Id
:= Etype
(Exp
);
5987 -- The type of the expression (not necessarily the same as R_Type)
5989 Subtype_Ind
: Node_Id
;
5990 -- If the result type of the function is class-wide and the expression
5991 -- has a specific type, then we use the expression's type as the type of
5992 -- the return object. In cases where the expression is an aggregate that
5993 -- is built in place, this avoids the need for an expensive conversion
5994 -- of the return object to the specific type on assignments to the
5995 -- individual components.
5998 if Is_Class_Wide_Type
(R_Type
)
5999 and then not Is_Class_Wide_Type
(Exptyp
)
6000 and then Nkind
(Exp
) /= N_Type_Conversion
6002 Subtype_Ind
:= New_Occurrence_Of
(Exptyp
, Loc
);
6004 Subtype_Ind
:= New_Occurrence_Of
(R_Type
, Loc
);
6006 -- If the result type is class-wide and the expression is a view
6007 -- conversion, the conversion plays no role in the expansion because
6008 -- it does not modify the tag of the object. Remove the conversion
6009 -- altogether to prevent tag overwriting.
6011 if Is_Class_Wide_Type
(R_Type
)
6012 and then not Is_Class_Wide_Type
(Exptyp
)
6013 and then Nkind
(Exp
) = N_Type_Conversion
6015 Exp
:= Expression
(Exp
);
6019 -- For the case of a simple return that does not come from an extended
6020 -- return, in the case of Ada 2005 where we are returning a limited
6021 -- type, we rewrite "return <expression>;" to be:
6023 -- return _anon_ : <return_subtype> := <expression>
6025 -- The expansion produced by Expand_N_Extended_Return_Statement will
6026 -- contain simple return statements (for example, a block containing
6027 -- simple return of the return object), which brings us back here with
6028 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
6029 -- checking for a simple return that does not come from an extended
6030 -- return is to avoid this infinite recursion.
6032 -- The reason for this design is that for Ada 2005 limited returns, we
6033 -- need to reify the return object, so we can build it "in place", and
6034 -- we need a block statement to hang finalization and tasking stuff.
6036 -- ??? In order to avoid disruption, we avoid translating to extended
6037 -- return except in the cases where we really need to (Ada 2005 for
6038 -- inherently limited). We might prefer to do this translation in all
6039 -- cases (except perhaps for the case of Ada 95 inherently limited),
6040 -- in order to fully exercise the Expand_N_Extended_Return_Statement
6041 -- code. This would also allow us to do the build-in-place optimization
6042 -- for efficiency even in cases where it is semantically not required.
6044 -- As before, we check the type of the return expression rather than the
6045 -- return type of the function, because the latter may be a limited
6046 -- class-wide interface type, which is not a limited type, even though
6047 -- the type of the expression may be.
6049 if not Comes_From_Extended_Return_Statement
(N
)
6050 and then Is_Limited_View
(Etype
(Expression
(N
)))
6051 and then Ada_Version
>= Ada_2005
6052 and then not Debug_Flag_Dot_L
6054 -- The functionality of interface thunks is simple and it is always
6055 -- handled by means of simple return statements. This leaves their
6056 -- expansion simple and clean.
6058 and then not Is_Thunk
(Current_Scope
)
6061 Return_Object_Entity
: constant Entity_Id
:=
6062 Make_Temporary
(Loc
, 'R', Exp
);
6064 Obj_Decl
: constant Node_Id
:=
6065 Make_Object_Declaration
(Loc
,
6066 Defining_Identifier
=> Return_Object_Entity
,
6067 Object_Definition
=> Subtype_Ind
,
6070 Ext
: constant Node_Id
:=
6071 Make_Extended_Return_Statement
(Loc
,
6072 Return_Object_Declarations
=> New_List
(Obj_Decl
));
6073 -- Do not perform this high-level optimization if the result type
6074 -- is an interface because the "this" pointer must be displaced.
6083 -- Here we have a simple return statement that is part of the expansion
6084 -- of an extended return statement (either written by the user, or
6085 -- generated by the above code).
6087 -- Always normalize C/Fortran boolean result. This is not always needed,
6088 -- but it seems a good idea to minimize the passing around of non-
6089 -- normalized values, and in any case this handles the processing of
6090 -- barrier functions for protected types, which turn the condition into
6091 -- a return statement.
6093 if Is_Boolean_Type
(Exptyp
)
6094 and then Nonzero_Is_True
(Exptyp
)
6096 Adjust_Condition
(Exp
);
6097 Adjust_Result_Type
(Exp
, Exptyp
);
6100 -- Do validity check if enabled for returns
6102 if Validity_Checks_On
6103 and then Validity_Check_Returns
6108 -- Check the result expression of a scalar function against the subtype
6109 -- of the function by inserting a conversion. This conversion must
6110 -- eventually be performed for other classes of types, but for now it's
6111 -- only done for scalars.
6114 if Is_Scalar_Type
(Exptyp
) then
6115 Rewrite
(Exp
, Convert_To
(R_Type
, Exp
));
6117 -- The expression is resolved to ensure that the conversion gets
6118 -- expanded to generate a possible constraint check.
6120 Analyze_And_Resolve
(Exp
, R_Type
);
6123 -- Deal with returning variable length objects and controlled types
6125 -- Nothing to do if we are returning by reference, or this is not a
6126 -- type that requires special processing (indicated by the fact that
6127 -- it requires a cleanup scope for the secondary stack case).
6129 if Is_Limited_View
(Exptyp
)
6130 or else Is_Limited_Interface
(Exptyp
)
6134 -- No copy needed for thunks returning interface type objects since
6135 -- the object is returned by reference and the maximum functionality
6136 -- required is just to displace the pointer.
6138 elsif Is_Thunk
(Current_Scope
) and then Is_Interface
(Exptyp
) then
6141 -- If the call is within a thunk and the type is a limited view, the
6142 -- backend will eventually see the non-limited view of the type.
6144 elsif Is_Thunk
(Current_Scope
) and then Is_Incomplete_Type
(Exptyp
) then
6147 elsif not Requires_Transient_Scope
(R_Type
) then
6149 -- Mutable records with variable-length components are not returned
6150 -- on the sec-stack, so we need to make sure that the back end will
6151 -- only copy back the size of the actual value, and not the maximum
6152 -- size. We create an actual subtype for this purpose. However we
6153 -- need not do it if the expression is a function call since this
6154 -- will be done in the called function and doing it here too would
6155 -- cause a temporary with maximum size to be created.
6158 Ubt
: constant Entity_Id
:= Underlying_Type
(Base_Type
(Exptyp
));
6162 if Nkind
(Exp
) /= N_Function_Call
6163 and then Has_Discriminants
(Ubt
)
6164 and then not Is_Constrained
(Ubt
)
6165 and then not Has_Unchecked_Union
(Ubt
)
6167 Decl
:= Build_Actual_Subtype
(Ubt
, Exp
);
6168 Ent
:= Defining_Identifier
(Decl
);
6169 Insert_Action
(Exp
, Decl
);
6170 Rewrite
(Exp
, Unchecked_Convert_To
(Ent
, Exp
));
6171 Analyze_And_Resolve
(Exp
);
6175 -- Here if secondary stack is used
6178 -- Prevent the reclamation of the secondary stack by all enclosing
6179 -- blocks and loops as well as the related function; otherwise the
6180 -- result would be reclaimed too early.
6182 Set_Enclosing_Sec_Stack_Return
(N
);
6184 -- Optimize the case where the result is a function call. In this
6185 -- case either the result is already on the secondary stack, or is
6186 -- already being returned with the stack pointer depressed and no
6187 -- further processing is required except to set the By_Ref flag
6188 -- to ensure that gigi does not attempt an extra unnecessary copy.
6189 -- (actually not just unnecessary but harmfully wrong in the case
6190 -- of a controlled type, where gigi does not know how to do a copy).
6191 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
6192 -- for array types if the constrained status of the target type is
6193 -- different from that of the expression.
6195 if Requires_Transient_Scope
(Exptyp
)
6197 (not Is_Array_Type
(Exptyp
)
6198 or else Is_Constrained
(Exptyp
) = Is_Constrained
(R_Type
)
6199 or else CW_Or_Has_Controlled_Part
(Utyp
))
6200 and then Nkind
(Exp
) = N_Function_Call
6204 -- Remove side effects from the expression now so that other parts
6205 -- of the expander do not have to reanalyze this node without this
6208 Rewrite
(Exp
, Duplicate_Subexpr_No_Checks
(Exp
));
6210 -- For controlled types, do the allocation on the secondary stack
6211 -- manually in order to call adjust at the right time:
6213 -- type Anon1 is access R_Type;
6214 -- for Anon1'Storage_pool use ss_pool;
6215 -- Anon2 : anon1 := new R_Type'(expr);
6216 -- return Anon2.all;
6218 -- We do the same for classwide types that are not potentially
6219 -- controlled (by the virtue of restriction No_Finalization) because
6220 -- gigi is not able to properly allocate class-wide types.
6222 elsif CW_Or_Has_Controlled_Part
(Utyp
) then
6224 Loc
: constant Source_Ptr
:= Sloc
(N
);
6225 Acc_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
6226 Alloc_Node
: Node_Id
;
6230 Set_Ekind
(Acc_Typ
, E_Access_Type
);
6232 Set_Associated_Storage_Pool
(Acc_Typ
, RTE
(RE_SS_Pool
));
6234 -- This is an allocator for the secondary stack, and it's fine
6235 -- to have Comes_From_Source set False on it, as gigi knows not
6236 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6239 Make_Allocator
(Loc
,
6241 Make_Qualified_Expression
(Loc
,
6242 Subtype_Mark
=> New_Occurrence_Of
(Etype
(Exp
), Loc
),
6243 Expression
=> Relocate_Node
(Exp
)));
6245 -- We do not want discriminant checks on the declaration,
6246 -- given that it gets its value from the allocator.
6248 Set_No_Initialization
(Alloc_Node
);
6250 Temp
:= Make_Temporary
(Loc
, 'R', Alloc_Node
);
6252 Insert_List_Before_And_Analyze
(N
, New_List
(
6253 Make_Full_Type_Declaration
(Loc
,
6254 Defining_Identifier
=> Acc_Typ
,
6256 Make_Access_To_Object_Definition
(Loc
,
6257 Subtype_Indication
=> Subtype_Ind
)),
6259 Make_Object_Declaration
(Loc
,
6260 Defining_Identifier
=> Temp
,
6261 Object_Definition
=> New_Occurrence_Of
(Acc_Typ
, Loc
),
6262 Expression
=> Alloc_Node
)));
6265 Make_Explicit_Dereference
(Loc
,
6266 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
6268 -- Ada 2005 (AI-251): If the type of the returned object is
6269 -- an interface then add an implicit type conversion to force
6270 -- displacement of the "this" pointer.
6272 if Is_Interface
(R_Type
) then
6273 Rewrite
(Exp
, Convert_To
(R_Type
, Relocate_Node
(Exp
)));
6276 Analyze_And_Resolve
(Exp
, R_Type
);
6279 -- Otherwise use the gigi mechanism to allocate result on the
6283 Check_Restriction
(No_Secondary_Stack
, N
);
6284 Set_Storage_Pool
(N
, RTE
(RE_SS_Pool
));
6285 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
6289 -- Implement the rules of 6.5(8-10), which require a tag check in
6290 -- the case of a limited tagged return type, and tag reassignment for
6291 -- nonlimited tagged results. These actions are needed when the return
6292 -- type is a specific tagged type and the result expression is a
6293 -- conversion or a formal parameter, because in that case the tag of
6294 -- the expression might differ from the tag of the specific result type.
6296 if Is_Tagged_Type
(Utyp
)
6297 and then not Is_Class_Wide_Type
(Utyp
)
6298 and then (Nkind_In
(Exp
, N_Type_Conversion
,
6299 N_Unchecked_Type_Conversion
)
6300 or else (Is_Entity_Name
(Exp
)
6301 and then Ekind
(Entity
(Exp
)) in Formal_Kind
))
6303 -- When the return type is limited, perform a check that the tag of
6304 -- the result is the same as the tag of the return type.
6306 if Is_Limited_Type
(R_Type
) then
6308 Make_Raise_Constraint_Error
(Loc
,
6312 Make_Selected_Component
(Loc
,
6313 Prefix
=> Duplicate_Subexpr
(Exp
),
6314 Selector_Name
=> Make_Identifier
(Loc
, Name_uTag
)),
6316 Make_Attribute_Reference
(Loc
,
6318 New_Occurrence_Of
(Base_Type
(Utyp
), Loc
),
6319 Attribute_Name
=> Name_Tag
)),
6320 Reason
=> CE_Tag_Check_Failed
));
6322 -- If the result type is a specific nonlimited tagged type, then we
6323 -- have to ensure that the tag of the result is that of the result
6324 -- type. This is handled by making a copy of the expression in
6325 -- the case where it might have a different tag, namely when the
6326 -- expression is a conversion or a formal parameter. We create a new
6327 -- object of the result type and initialize it from the expression,
6328 -- which will implicitly force the tag to be set appropriately.
6332 ExpR
: constant Node_Id
:= Relocate_Node
(Exp
);
6333 Result_Id
: constant Entity_Id
:=
6334 Make_Temporary
(Loc
, 'R', ExpR
);
6335 Result_Exp
: constant Node_Id
:=
6336 New_Occurrence_Of
(Result_Id
, Loc
);
6337 Result_Obj
: constant Node_Id
:=
6338 Make_Object_Declaration
(Loc
,
6339 Defining_Identifier
=> Result_Id
,
6340 Object_Definition
=>
6341 New_Occurrence_Of
(R_Type
, Loc
),
6342 Constant_Present
=> True,
6343 Expression
=> ExpR
);
6346 Set_Assignment_OK
(Result_Obj
);
6347 Insert_Action
(Exp
, Result_Obj
);
6349 Rewrite
(Exp
, Result_Exp
);
6350 Analyze_And_Resolve
(Exp
, R_Type
);
6354 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6355 -- a check that the level of the return expression's underlying type
6356 -- is not deeper than the level of the master enclosing the function.
6357 -- Always generate the check when the type of the return expression
6358 -- is class-wide, when it's a type conversion, or when it's a formal
6359 -- parameter. Otherwise, suppress the check in the case where the
6360 -- return expression has a specific type whose level is known not to
6361 -- be statically deeper than the function's result type.
6363 -- No runtime check needed in interface thunks since it is performed
6364 -- by the target primitive associated with the thunk.
6366 -- Note: accessibility check is skipped in the VM case, since there
6367 -- does not seem to be any practical way to implement this check.
6369 elsif Ada_Version
>= Ada_2005
6370 and then Tagged_Type_Expansion
6371 and then Is_Class_Wide_Type
(R_Type
)
6372 and then not Is_Thunk
(Current_Scope
)
6373 and then not Scope_Suppress
.Suppress
(Accessibility_Check
)
6375 (Is_Class_Wide_Type
(Etype
(Exp
))
6376 or else Nkind_In
(Exp
, N_Type_Conversion
,
6377 N_Unchecked_Type_Conversion
)
6378 or else (Is_Entity_Name
(Exp
)
6379 and then Ekind
(Entity
(Exp
)) in Formal_Kind
)
6380 or else Scope_Depth
(Enclosing_Dynamic_Scope
(Etype
(Exp
))) >
6381 Scope_Depth
(Enclosing_Dynamic_Scope
(Scope_Id
)))
6387 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6388 -- "this" to reference the base of the object. This is required to
6389 -- get access to the TSD of the object.
6391 if Is_Class_Wide_Type
(Etype
(Exp
))
6392 and then Is_Interface
(Etype
(Exp
))
6394 -- If the expression is an explicit dereference then we can
6395 -- directly displace the pointer to reference the base of
6398 if Nkind
(Exp
) = N_Explicit_Dereference
then
6400 Make_Explicit_Dereference
(Loc
,
6402 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
6403 Make_Function_Call
(Loc
,
6405 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
6406 Parameter_Associations
=> New_List
(
6407 Unchecked_Convert_To
(RTE
(RE_Address
),
6408 Duplicate_Subexpr
(Prefix
(Exp
)))))));
6410 -- Similar case to the previous one but the expression is a
6411 -- renaming of an explicit dereference.
6413 elsif Nkind
(Exp
) = N_Identifier
6414 and then Present
(Renamed_Object
(Entity
(Exp
)))
6415 and then Nkind
(Renamed_Object
(Entity
(Exp
)))
6416 = N_Explicit_Dereference
6419 Make_Explicit_Dereference
(Loc
,
6421 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
6422 Make_Function_Call
(Loc
,
6424 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
6425 Parameter_Associations
=> New_List
(
6426 Unchecked_Convert_To
(RTE
(RE_Address
),
6429 (Renamed_Object
(Entity
(Exp
)))))))));
6431 -- Common case: obtain the address of the actual object and
6432 -- displace the pointer to reference the base of the object.
6436 Make_Explicit_Dereference
(Loc
,
6438 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
6439 Make_Function_Call
(Loc
,
6441 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
6442 Parameter_Associations
=> New_List
(
6443 Make_Attribute_Reference
(Loc
,
6444 Prefix
=> Duplicate_Subexpr
(Exp
),
6445 Attribute_Name
=> Name_Address
)))));
6449 Make_Attribute_Reference
(Loc
,
6450 Prefix
=> Duplicate_Subexpr
(Exp
),
6451 Attribute_Name
=> Name_Tag
);
6455 Make_Raise_Program_Error
(Loc
,
6458 Left_Opnd
=> Build_Get_Access_Level
(Loc
, Tag_Node
),
6460 Make_Integer_Literal
(Loc
,
6461 Scope_Depth
(Enclosing_Dynamic_Scope
(Scope_Id
)))),
6462 Reason
=> PE_Accessibility_Check_Failed
));
6465 -- AI05-0073: If function has a controlling access result, check that
6466 -- the tag of the return value, if it is not null, matches designated
6467 -- type of return type.
6469 -- The return expression is referenced twice in the code below, so it
6470 -- must be made free of side effects. Given that different compilers
6471 -- may evaluate these parameters in different order, both occurrences
6474 elsif Ekind
(R_Type
) = E_Anonymous_Access_Type
6475 and then Has_Controlling_Result
(Scope_Id
)
6478 Make_Raise_Constraint_Error
(Loc
,
6483 Left_Opnd
=> Duplicate_Subexpr
(Exp
),
6484 Right_Opnd
=> Make_Null
(Loc
)),
6486 Right_Opnd
=> Make_Op_Ne
(Loc
,
6488 Make_Selected_Component
(Loc
,
6489 Prefix
=> Duplicate_Subexpr
(Exp
),
6490 Selector_Name
=> Make_Identifier
(Loc
, Name_uTag
)),
6493 Make_Attribute_Reference
(Loc
,
6495 New_Occurrence_Of
(Designated_Type
(R_Type
), Loc
),
6496 Attribute_Name
=> Name_Tag
))),
6498 Reason
=> CE_Tag_Check_Failed
),
6499 Suppress
=> All_Checks
);
6502 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
6503 -- ensure that the function result does not outlive an
6504 -- object designated by one of it discriminants.
6506 if Present
(Extra_Accessibility_Of_Result
(Scope_Id
))
6507 and then Has_Unconstrained_Access_Discriminants
(R_Type
)
6510 Discrim_Source
: Node_Id
;
6512 procedure Check_Against_Result_Level
(Level
: Node_Id
);
6513 -- Check the given accessibility level against the level
6514 -- determined by the point of call. (AI05-0234).
6516 --------------------------------
6517 -- Check_Against_Result_Level --
6518 --------------------------------
6520 procedure Check_Against_Result_Level
(Level
: Node_Id
) is
6523 Make_Raise_Program_Error
(Loc
,
6529 (Extra_Accessibility_Of_Result
(Scope_Id
), Loc
)),
6530 Reason
=> PE_Accessibility_Check_Failed
));
6531 end Check_Against_Result_Level
;
6534 Discrim_Source
:= Exp
;
6535 while Nkind
(Discrim_Source
) = N_Qualified_Expression
loop
6536 Discrim_Source
:= Expression
(Discrim_Source
);
6539 if Nkind
(Discrim_Source
) = N_Identifier
6540 and then Is_Return_Object
(Entity
(Discrim_Source
))
6542 Discrim_Source
:= Entity
(Discrim_Source
);
6544 if Is_Constrained
(Etype
(Discrim_Source
)) then
6545 Discrim_Source
:= Etype
(Discrim_Source
);
6547 Discrim_Source
:= Expression
(Parent
(Discrim_Source
));
6550 elsif Nkind
(Discrim_Source
) = N_Identifier
6551 and then Nkind_In
(Original_Node
(Discrim_Source
),
6552 N_Aggregate
, N_Extension_Aggregate
)
6554 Discrim_Source
:= Original_Node
(Discrim_Source
);
6556 elsif Nkind
(Discrim_Source
) = N_Explicit_Dereference
and then
6557 Nkind
(Original_Node
(Discrim_Source
)) = N_Function_Call
6559 Discrim_Source
:= Original_Node
(Discrim_Source
);
6562 while Nkind_In
(Discrim_Source
, N_Qualified_Expression
,
6564 N_Unchecked_Type_Conversion
)
6566 Discrim_Source
:= Expression
(Discrim_Source
);
6569 case Nkind
(Discrim_Source
) is
6570 when N_Defining_Identifier
=>
6572 pragma Assert
(Is_Composite_Type
(Discrim_Source
)
6573 and then Has_Discriminants
(Discrim_Source
)
6574 and then Is_Constrained
(Discrim_Source
));
6577 Discrim
: Entity_Id
:=
6578 First_Discriminant
(Base_Type
(R_Type
));
6579 Disc_Elmt
: Elmt_Id
:=
6580 First_Elmt
(Discriminant_Constraint
6584 if Ekind
(Etype
(Discrim
)) =
6585 E_Anonymous_Access_Type
6587 Check_Against_Result_Level
6588 (Dynamic_Accessibility_Level
(Node
(Disc_Elmt
)));
6591 Next_Elmt
(Disc_Elmt
);
6592 Next_Discriminant
(Discrim
);
6593 exit when not Present
(Discrim
);
6597 when N_Aggregate | N_Extension_Aggregate
=>
6599 -- Unimplemented: extension aggregate case where discrims
6600 -- come from ancestor part, not extension part.
6603 Discrim
: Entity_Id
:=
6604 First_Discriminant
(Base_Type
(R_Type
));
6606 Disc_Exp
: Node_Id
:= Empty
;
6608 Positionals_Exhausted
6609 : Boolean := not Present
(Expressions
6612 function Associated_Expr
6613 (Comp_Id
: Entity_Id
;
6614 Associations
: List_Id
) return Node_Id
;
6616 -- Given a component and a component associations list,
6617 -- locate the expression for that component; returns
6618 -- Empty if no such expression is found.
6620 ---------------------
6621 -- Associated_Expr --
6622 ---------------------
6624 function Associated_Expr
6625 (Comp_Id
: Entity_Id
;
6626 Associations
: List_Id
) return Node_Id
6632 -- Simple linear search seems ok here
6634 Assoc
:= First
(Associations
);
6635 while Present
(Assoc
) loop
6636 Choice
:= First
(Choices
(Assoc
));
6637 while Present
(Choice
) loop
6638 if (Nkind
(Choice
) = N_Identifier
6639 and then Chars
(Choice
) = Chars
(Comp_Id
))
6640 or else (Nkind
(Choice
) = N_Others_Choice
)
6642 return Expression
(Assoc
);
6652 end Associated_Expr
;
6654 -- Start of processing for Expand_Simple_Function_Return
6657 if not Positionals_Exhausted
then
6658 Disc_Exp
:= First
(Expressions
(Discrim_Source
));
6662 if Positionals_Exhausted
then
6666 Component_Associations
(Discrim_Source
));
6669 if Ekind
(Etype
(Discrim
)) =
6670 E_Anonymous_Access_Type
6672 Check_Against_Result_Level
6673 (Dynamic_Accessibility_Level
(Disc_Exp
));
6676 Next_Discriminant
(Discrim
);
6677 exit when not Present
(Discrim
);
6679 if not Positionals_Exhausted
then
6681 Positionals_Exhausted
:= not Present
(Disc_Exp
);
6686 when N_Function_Call
=>
6688 -- No check needed (check performed by callee)
6695 Level
: constant Node_Id
:=
6696 Make_Integer_Literal
(Loc
,
6697 Object_Access_Level
(Discrim_Source
));
6700 -- Unimplemented: check for name prefix that includes
6701 -- a dereference of an access value with a dynamic
6702 -- accessibility level (e.g., an access param or a
6703 -- saooaaat) and use dynamic level in that case. For
6705 -- return Access_Param.all(Some_Index).Some_Component;
6708 Set_Etype
(Level
, Standard_Natural
);
6709 Check_Against_Result_Level
(Level
);
6716 -- If we are returning an object that may not be bit-aligned, then copy
6717 -- the value into a temporary first. This copy may need to expand to a
6718 -- loop of component operations.
6720 if Is_Possibly_Unaligned_Slice
(Exp
)
6721 or else Is_Possibly_Unaligned_Object
(Exp
)
6724 ExpR
: constant Node_Id
:= Relocate_Node
(Exp
);
6725 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', ExpR
);
6728 Make_Object_Declaration
(Loc
,
6729 Defining_Identifier
=> Tnn
,
6730 Constant_Present
=> True,
6731 Object_Definition
=> New_Occurrence_Of
(R_Type
, Loc
),
6732 Expression
=> ExpR
),
6733 Suppress
=> All_Checks
);
6734 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
6738 -- Call the _Postconditions procedure if the related function has
6739 -- contract assertions that need to be verified on exit.
6741 if Ekind
(Scope_Id
) = E_Function
6742 and then Present
(Postconditions_Proc
(Scope_Id
))
6744 -- In the case of discriminated objects, we have created a
6745 -- constrained subtype above, and used the underlying type. This
6746 -- transformation is post-analysis and harmless, except that now the
6747 -- call to the post-condition will be analyzed and the type kinds
6750 if Nkind
(Exp
) = N_Unchecked_Type_Conversion
6751 and then Is_Private_Type
(R_Type
) /= Is_Private_Type
(Etype
(Exp
))
6753 Rewrite
(Exp
, Expression
(Relocate_Node
(Exp
)));
6756 -- We are going to reference the returned value twice in this case,
6757 -- once in the call to _Postconditions, and once in the actual return
6758 -- statement, but we can't have side effects happening twice.
6760 Remove_Side_Effects
(Exp
);
6762 -- Generate call to _Postconditions
6765 Make_Procedure_Call_Statement
(Loc
,
6767 New_Occurrence_Of
(Postconditions_Proc
(Scope_Id
), Loc
),
6768 Parameter_Associations
=> New_List
(New_Copy_Tree
(Exp
))));
6771 -- Ada 2005 (AI-251): If this return statement corresponds with an
6772 -- simple return statement associated with an extended return statement
6773 -- and the type of the returned object is an interface then generate an
6774 -- implicit conversion to force displacement of the "this" pointer.
6776 if Ada_Version
>= Ada_2005
6777 and then Comes_From_Extended_Return_Statement
(N
)
6778 and then Nkind
(Expression
(N
)) = N_Identifier
6779 and then Is_Interface
(Utyp
)
6780 and then Utyp
/= Underlying_Type
(Exptyp
)
6782 Rewrite
(Exp
, Convert_To
(Utyp
, Relocate_Node
(Exp
)));
6783 Analyze_And_Resolve
(Exp
);
6785 end Expand_Simple_Function_Return
;
6787 --------------------------------------------
6788 -- Has_Unconstrained_Access_Discriminants --
6789 --------------------------------------------
6791 function Has_Unconstrained_Access_Discriminants
6792 (Subtyp
: Entity_Id
) return Boolean
6797 if Has_Discriminants
(Subtyp
)
6798 and then not Is_Constrained
(Subtyp
)
6800 Discr
:= First_Discriminant
(Subtyp
);
6801 while Present
(Discr
) loop
6802 if Ekind
(Etype
(Discr
)) = E_Anonymous_Access_Type
then
6806 Next_Discriminant
(Discr
);
6811 end Has_Unconstrained_Access_Discriminants
;
6817 procedure Initialize
is
6822 --------------------------------
6823 -- Is_Build_In_Place_Function --
6824 --------------------------------
6826 function Is_Build_In_Place_Function
(E
: Entity_Id
) return Boolean is
6828 -- This function is called from Expand_Subtype_From_Expr during
6829 -- semantic analysis, even when expansion is off. In those cases
6830 -- the build_in_place expansion will not take place.
6832 if not Expander_Active
then
6836 -- For now we test whether E denotes a function or access-to-function
6837 -- type whose result subtype is inherently limited. Later this test
6838 -- may be revised to allow composite nonlimited types. Functions with
6839 -- a foreign convention or whose result type has a foreign convention
6842 if Ekind_In
(E
, E_Function
, E_Generic_Function
)
6843 or else (Ekind
(E
) = E_Subprogram_Type
6844 and then Etype
(E
) /= Standard_Void_Type
)
6846 -- Note: If the function has a foreign convention, it cannot build
6847 -- its result in place, so you're on your own. On the other hand,
6848 -- if only the return type has a foreign convention, its layout is
6849 -- intended to be compatible with the other language, but the build-
6850 -- in place machinery can ensure that the object is not copied.
6852 if Has_Foreign_Convention
(E
) then
6855 -- In Ada 2005 all functions with an inherently limited return type
6856 -- must be handled using a build-in-place profile, including the case
6857 -- of a function with a limited interface result, where the function
6858 -- may return objects of nonlimited descendants.
6861 return Is_Limited_View
(Etype
(E
))
6862 and then Ada_Version
>= Ada_2005
6863 and then not Debug_Flag_Dot_L
;
6869 end Is_Build_In_Place_Function
;
6871 -------------------------------------
6872 -- Is_Build_In_Place_Function_Call --
6873 -------------------------------------
6875 function Is_Build_In_Place_Function_Call
(N
: Node_Id
) return Boolean is
6876 Exp_Node
: Node_Id
:= N
;
6877 Function_Id
: Entity_Id
;
6880 -- Return False if the expander is currently inactive, since awareness
6881 -- of build-in-place treatment is only relevant during expansion. Note
6882 -- that Is_Build_In_Place_Function, which is called as part of this
6883 -- function, is also conditioned this way, but we need to check here as
6884 -- well to avoid blowing up on processing protected calls when expansion
6885 -- is disabled (such as with -gnatc) since those would trip over the
6886 -- raise of Program_Error below.
6888 -- In SPARK mode, build-in-place calls are not expanded, so that we
6889 -- may end up with a call that is neither resolved to an entity, nor
6890 -- an indirect call.
6892 if not Expander_Active
then
6896 -- Step past qualification, type conversion (which can occur in actual
6897 -- parameter contexts), and unchecked conversion (which can occur in
6898 -- cases of calls to 'Input).
6900 if Nkind_In
(Exp_Node
, N_Qualified_Expression
,
6902 N_Unchecked_Type_Conversion
)
6904 Exp_Node
:= Expression
(N
);
6907 if Nkind
(Exp_Node
) /= N_Function_Call
then
6911 if Is_Entity_Name
(Name
(Exp_Node
)) then
6912 Function_Id
:= Entity
(Name
(Exp_Node
));
6914 -- In the case of an explicitly dereferenced call, use the subprogram
6915 -- type generated for the dereference.
6917 elsif Nkind
(Name
(Exp_Node
)) = N_Explicit_Dereference
then
6918 Function_Id
:= Etype
(Name
(Exp_Node
));
6920 -- This may be a call to a protected function.
6922 elsif Nkind
(Name
(Exp_Node
)) = N_Selected_Component
then
6923 Function_Id
:= Etype
(Entity
(Selector_Name
(Name
(Exp_Node
))));
6926 raise Program_Error
;
6929 return Is_Build_In_Place_Function
(Function_Id
);
6931 end Is_Build_In_Place_Function_Call
;
6933 -----------------------
6934 -- Freeze_Subprogram --
6935 -----------------------
6937 procedure Freeze_Subprogram
(N
: Node_Id
) is
6938 Loc
: constant Source_Ptr
:= Sloc
(N
);
6940 procedure Register_Predefined_DT_Entry
(Prim
: Entity_Id
);
6941 -- (Ada 2005): Register a predefined primitive in all the secondary
6942 -- dispatch tables of its primitive type.
6944 ----------------------------------
6945 -- Register_Predefined_DT_Entry --
6946 ----------------------------------
6948 procedure Register_Predefined_DT_Entry
(Prim
: Entity_Id
) is
6949 Iface_DT_Ptr
: Elmt_Id
;
6950 Tagged_Typ
: Entity_Id
;
6951 Thunk_Id
: Entity_Id
;
6952 Thunk_Code
: Node_Id
;
6955 Tagged_Typ
:= Find_Dispatching_Type
(Prim
);
6957 if No
(Access_Disp_Table
(Tagged_Typ
))
6958 or else not Has_Interfaces
(Tagged_Typ
)
6959 or else not RTE_Available
(RE_Interface_Tag
)
6960 or else Restriction_Active
(No_Dispatching_Calls
)
6965 -- Skip the first two access-to-dispatch-table pointers since they
6966 -- leads to the primary dispatch table (predefined DT and user
6967 -- defined DT). We are only concerned with the secondary dispatch
6968 -- table pointers. Note that the access-to- dispatch-table pointer
6969 -- corresponds to the first implemented interface retrieved below.
6972 Next_Elmt
(Next_Elmt
(First_Elmt
(Access_Disp_Table
(Tagged_Typ
))));
6974 while Present
(Iface_DT_Ptr
)
6975 and then Ekind
(Node
(Iface_DT_Ptr
)) = E_Constant
6977 pragma Assert
(Has_Thunks
(Node
(Iface_DT_Ptr
)));
6978 Expand_Interface_Thunk
(Prim
, Thunk_Id
, Thunk_Code
);
6980 if Present
(Thunk_Code
) then
6981 Insert_Actions_After
(N
, New_List
(
6984 Build_Set_Predefined_Prim_Op_Address
(Loc
,
6986 New_Occurrence_Of
(Node
(Next_Elmt
(Iface_DT_Ptr
)), Loc
),
6987 Position
=> DT_Position
(Prim
),
6989 Unchecked_Convert_To
(RTE
(RE_Prim_Ptr
),
6990 Make_Attribute_Reference
(Loc
,
6991 Prefix
=> New_Occurrence_Of
(Thunk_Id
, Loc
),
6992 Attribute_Name
=> Name_Unrestricted_Access
))),
6994 Build_Set_Predefined_Prim_Op_Address
(Loc
,
6997 (Node
(Next_Elmt
(Next_Elmt
(Next_Elmt
(Iface_DT_Ptr
)))),
6999 Position
=> DT_Position
(Prim
),
7001 Unchecked_Convert_To
(RTE
(RE_Prim_Ptr
),
7002 Make_Attribute_Reference
(Loc
,
7003 Prefix
=> New_Occurrence_Of
(Prim
, Loc
),
7004 Attribute_Name
=> Name_Unrestricted_Access
)))));
7007 -- Skip the tag of the predefined primitives dispatch table
7009 Next_Elmt
(Iface_DT_Ptr
);
7010 pragma Assert
(Has_Thunks
(Node
(Iface_DT_Ptr
)));
7012 -- Skip tag of the no-thunks dispatch table
7014 Next_Elmt
(Iface_DT_Ptr
);
7015 pragma Assert
(not Has_Thunks
(Node
(Iface_DT_Ptr
)));
7017 -- Skip tag of predefined primitives no-thunks dispatch table
7019 Next_Elmt
(Iface_DT_Ptr
);
7020 pragma Assert
(not Has_Thunks
(Node
(Iface_DT_Ptr
)));
7022 Next_Elmt
(Iface_DT_Ptr
);
7024 end Register_Predefined_DT_Entry
;
7028 Subp
: constant Entity_Id
:= Entity
(N
);
7030 -- Start of processing for Freeze_Subprogram
7033 -- We suppress the initialization of the dispatch table entry when
7034 -- not Tagged_Type_Expansion because the dispatching mechanism is
7035 -- handled internally by the target.
7037 if Is_Dispatching_Operation
(Subp
)
7038 and then not Is_Abstract_Subprogram
(Subp
)
7039 and then Present
(DTC_Entity
(Subp
))
7040 and then Present
(Scope
(DTC_Entity
(Subp
)))
7041 and then Tagged_Type_Expansion
7042 and then not Restriction_Active
(No_Dispatching_Calls
)
7043 and then RTE_Available
(RE_Tag
)
7046 Typ
: constant Entity_Id
:= Scope
(DTC_Entity
(Subp
));
7049 -- Handle private overridden primitives
7051 if not Is_CPP_Class
(Typ
) then
7052 Check_Overriding_Operation
(Subp
);
7055 -- We assume that imported CPP primitives correspond with objects
7056 -- whose constructor is in the CPP side; therefore we don't need
7057 -- to generate code to register them in the dispatch table.
7059 if Is_CPP_Class
(Typ
) then
7062 -- Handle CPP primitives found in derivations of CPP_Class types.
7063 -- These primitives must have been inherited from some parent, and
7064 -- there is no need to register them in the dispatch table because
7065 -- Build_Inherit_Prims takes care of initializing these slots.
7067 elsif Is_Imported
(Subp
)
7068 and then (Convention
(Subp
) = Convention_CPP
7069 or else Convention
(Subp
) = Convention_C
)
7073 -- Generate code to register the primitive in non statically
7074 -- allocated dispatch tables
7076 elsif not Building_Static_DT
(Scope
(DTC_Entity
(Subp
))) then
7078 -- When a primitive is frozen, enter its name in its dispatch
7081 if not Is_Interface
(Typ
)
7082 or else Present
(Interface_Alias
(Subp
))
7084 if Is_Predefined_Dispatching_Operation
(Subp
) then
7085 Register_Predefined_DT_Entry
(Subp
);
7088 Insert_Actions_After
(N
,
7089 Register_Primitive
(Loc
, Prim
=> Subp
));
7095 -- Mark functions that return by reference. Note that it cannot be part
7096 -- of the normal semantic analysis of the spec since the underlying
7097 -- returned type may not be known yet (for private types).
7100 Typ
: constant Entity_Id
:= Etype
(Subp
);
7101 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
7103 if Is_Limited_View
(Typ
) then
7104 Set_Returns_By_Ref
(Subp
);
7105 elsif Present
(Utyp
) and then CW_Or_Has_Controlled_Part
(Utyp
) then
7106 Set_Returns_By_Ref
(Subp
);
7110 -- Wnen freezing a null procedure, analyze its delayed aspects now
7111 -- because we may not have reached the end of the declarative list when
7112 -- delayed aspects are normally analyzed. This ensures that dispatching
7113 -- calls are properly rewritten when the generated _Postcondition
7114 -- procedure is analyzed in the null procedure body.
7116 if Nkind
(Parent
(Subp
)) = N_Procedure_Specification
7117 and then Null_Present
(Parent
(Subp
))
7119 Analyze_Entry_Or_Subprogram_Contract
(Subp
);
7121 end Freeze_Subprogram
;
7123 -----------------------
7124 -- Is_Null_Procedure --
7125 -----------------------
7127 function Is_Null_Procedure
(Subp
: Entity_Id
) return Boolean is
7128 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
7131 if Ekind
(Subp
) /= E_Procedure
then
7134 -- Check if this is a declared null procedure
7136 elsif Nkind
(Decl
) = N_Subprogram_Declaration
then
7137 if not Null_Present
(Specification
(Decl
)) then
7140 elsif No
(Body_To_Inline
(Decl
)) then
7143 -- Check if the body contains only a null statement, followed by
7144 -- the return statement added during expansion.
7148 Orig_Bod
: constant Node_Id
:= Body_To_Inline
(Decl
);
7154 if Nkind
(Orig_Bod
) /= N_Subprogram_Body
then
7157 -- We must skip SCIL nodes because they are currently
7158 -- implemented as special N_Null_Statement nodes.
7162 (Statements
(Handled_Statement_Sequence
(Orig_Bod
)));
7163 Stat2
:= Next_Non_SCIL_Node
(Stat
);
7166 Is_Empty_List
(Declarations
(Orig_Bod
))
7167 and then Nkind
(Stat
) = N_Null_Statement
7171 (Nkind
(Stat2
) = N_Simple_Return_Statement
7172 and then No
(Next
(Stat2
))));
7180 end Is_Null_Procedure
;
7182 -------------------------------------------
7183 -- Make_Build_In_Place_Call_In_Allocator --
7184 -------------------------------------------
7186 procedure Make_Build_In_Place_Call_In_Allocator
7187 (Allocator
: Node_Id
;
7188 Function_Call
: Node_Id
)
7190 Acc_Type
: constant Entity_Id
:= Etype
(Allocator
);
7192 Func_Call
: Node_Id
:= Function_Call
;
7193 Ref_Func_Call
: Node_Id
;
7194 Function_Id
: Entity_Id
;
7195 Result_Subt
: Entity_Id
;
7196 New_Allocator
: Node_Id
;
7197 Return_Obj_Access
: Entity_Id
; -- temp for function result
7198 Temp_Init
: Node_Id
; -- initial value of Return_Obj_Access
7199 Alloc_Form
: BIP_Allocation_Form
;
7200 Pool
: Node_Id
; -- nonnull if Alloc_Form = User_Storage_Pool
7201 Return_Obj_Actual
: Node_Id
; -- the temp.all, in caller-allocates case
7202 Chain
: Entity_Id
; -- activation chain, in case of tasks
7205 -- Step past qualification or unchecked conversion (the latter can occur
7206 -- in cases of calls to 'Input).
7208 if Nkind_In
(Func_Call
,
7209 N_Qualified_Expression
,
7210 N_Unchecked_Type_Conversion
)
7212 Func_Call
:= Expression
(Func_Call
);
7215 -- If the call has already been processed to add build-in-place actuals
7216 -- then return. This should not normally occur in an allocator context,
7217 -- but we add the protection as a defensive measure.
7219 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
7223 -- Mark the call as processed as a build-in-place call
7225 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
7227 Loc
:= Sloc
(Function_Call
);
7229 if Is_Entity_Name
(Name
(Func_Call
)) then
7230 Function_Id
:= Entity
(Name
(Func_Call
));
7232 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
7233 Function_Id
:= Etype
(Name
(Func_Call
));
7236 raise Program_Error
;
7239 Result_Subt
:= Available_View
(Etype
(Function_Id
));
7241 -- Create a temp for the function result. In the caller-allocates case,
7242 -- this will be initialized to the result of a new uninitialized
7243 -- allocator. Note: we do not use Allocator as the Related_Node of
7244 -- Return_Obj_Access in call to Make_Temporary below as this would
7245 -- create a sort of infinite "recursion".
7247 Return_Obj_Access
:= Make_Temporary
(Loc
, 'R');
7248 Set_Etype
(Return_Obj_Access
, Acc_Type
);
7250 -- When the result subtype is constrained, the return object is
7251 -- allocated on the caller side, and access to it is passed to the
7254 -- Here and in related routines, we must examine the full view of the
7255 -- type, because the view at the point of call may differ from that
7256 -- that in the function body, and the expansion mechanism depends on
7257 -- the characteristics of the full view.
7259 if Is_Constrained
(Underlying_Type
(Result_Subt
)) then
7261 -- Replace the initialized allocator of form "new T'(Func (...))"
7262 -- with an uninitialized allocator of form "new T", where T is the
7263 -- result subtype of the called function. The call to the function
7264 -- is handled separately further below.
7267 Make_Allocator
(Loc
,
7268 Expression
=> New_Occurrence_Of
(Result_Subt
, Loc
));
7269 Set_No_Initialization
(New_Allocator
);
7271 -- Copy attributes to new allocator. Note that the new allocator
7272 -- logically comes from source if the original one did, so copy the
7273 -- relevant flag. This ensures proper treatment of the restriction
7274 -- No_Implicit_Heap_Allocations in this case.
7276 Set_Storage_Pool
(New_Allocator
, Storage_Pool
(Allocator
));
7277 Set_Procedure_To_Call
(New_Allocator
, Procedure_To_Call
(Allocator
));
7278 Set_Comes_From_Source
(New_Allocator
, Comes_From_Source
(Allocator
));
7280 Rewrite
(Allocator
, New_Allocator
);
7282 -- Initial value of the temp is the result of the uninitialized
7285 Temp_Init
:= Relocate_Node
(Allocator
);
7287 -- Indicate that caller allocates, and pass in the return object
7289 Alloc_Form
:= Caller_Allocation
;
7290 Pool
:= Make_Null
(No_Location
);
7291 Return_Obj_Actual
:=
7292 Make_Unchecked_Type_Conversion
(Loc
,
7293 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Loc
),
7295 Make_Explicit_Dereference
(Loc
,
7296 Prefix
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
)));
7298 -- When the result subtype is unconstrained, the function itself must
7299 -- perform the allocation of the return object, so we pass parameters
7305 -- Case of a user-defined storage pool. Pass an allocation parameter
7306 -- indicating that the function should allocate its result in the
7307 -- pool, and pass the pool. Use 'Unrestricted_Access because the
7308 -- pool may not be aliased.
7310 if Present
(Associated_Storage_Pool
(Acc_Type
)) then
7311 Alloc_Form
:= User_Storage_Pool
;
7313 Make_Attribute_Reference
(Loc
,
7316 (Associated_Storage_Pool
(Acc_Type
), Loc
),
7317 Attribute_Name
=> Name_Unrestricted_Access
);
7319 -- No user-defined pool; pass an allocation parameter indicating that
7320 -- the function should allocate its result on the heap.
7323 Alloc_Form
:= Global_Heap
;
7324 Pool
:= Make_Null
(No_Location
);
7327 -- The caller does not provide the return object in this case, so we
7328 -- have to pass null for the object access actual.
7330 Return_Obj_Actual
:= Empty
;
7333 -- Declare the temp object
7335 Insert_Action
(Allocator
,
7336 Make_Object_Declaration
(Loc
,
7337 Defining_Identifier
=> Return_Obj_Access
,
7338 Object_Definition
=> New_Occurrence_Of
(Acc_Type
, Loc
),
7339 Expression
=> Temp_Init
));
7341 Ref_Func_Call
:= Make_Reference
(Loc
, Func_Call
);
7343 -- Ada 2005 (AI-251): If the type of the allocator is an interface
7344 -- then generate an implicit conversion to force displacement of the
7347 if Is_Interface
(Designated_Type
(Acc_Type
)) then
7350 OK_Convert_To
(Acc_Type
, Ref_Func_Call
));
7354 Assign
: constant Node_Id
:=
7355 Make_Assignment_Statement
(Loc
,
7356 Name
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
),
7357 Expression
=> Ref_Func_Call
);
7358 -- Assign the result of the function call into the temp. In the
7359 -- caller-allocates case, this is overwriting the temp with its
7360 -- initial value, which has no effect. In the callee-allocates case,
7361 -- this is setting the temp to point to the object allocated by the
7365 -- Actions to be inserted. If there are no tasks, this is just the
7366 -- assignment statement. If the allocated object has tasks, we need
7367 -- to wrap the assignment in a block that activates them. The
7368 -- activation chain of that block must be passed to the function,
7369 -- rather than some outer chain.
7371 if Has_Task
(Result_Subt
) then
7372 Actions
:= New_List
;
7373 Build_Task_Allocate_Block_With_Init_Stmts
7374 (Actions
, Allocator
, Init_Stmts
=> New_List
(Assign
));
7375 Chain
:= Activation_Chain_Entity
(Last
(Actions
));
7377 Actions
:= New_List
(Assign
);
7381 Insert_Actions
(Allocator
, Actions
);
7384 -- When the function has a controlling result, an allocation-form
7385 -- parameter must be passed indicating that the caller is allocating
7386 -- the result object. This is needed because such a function can be
7387 -- called as a dispatching operation and must be treated similarly
7388 -- to functions with unconstrained result subtypes.
7390 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7391 (Func_Call
, Function_Id
, Alloc_Form
, Pool_Actual
=> Pool
);
7393 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7394 (Func_Call
, Function_Id
, Acc_Type
);
7396 Add_Task_Actuals_To_Build_In_Place_Call
7397 (Func_Call
, Function_Id
, Master_Actual
=> Master_Id
(Acc_Type
),
7400 -- Add an implicit actual to the function call that provides access
7401 -- to the allocated object. An unchecked conversion to the (specific)
7402 -- result subtype of the function is inserted to handle cases where
7403 -- the access type of the allocator has a class-wide designated type.
7405 Add_Access_Actual_To_Build_In_Place_Call
7406 (Func_Call
, Function_Id
, Return_Obj_Actual
);
7408 -- Finally, replace the allocator node with a reference to the temp
7410 Rewrite
(Allocator
, New_Occurrence_Of
(Return_Obj_Access
, Loc
));
7412 Analyze_And_Resolve
(Allocator
, Acc_Type
);
7413 end Make_Build_In_Place_Call_In_Allocator
;
7415 ---------------------------------------------------
7416 -- Make_Build_In_Place_Call_In_Anonymous_Context --
7417 ---------------------------------------------------
7419 procedure Make_Build_In_Place_Call_In_Anonymous_Context
7420 (Function_Call
: Node_Id
)
7423 Func_Call
: Node_Id
:= Function_Call
;
7424 Function_Id
: Entity_Id
;
7425 Result_Subt
: Entity_Id
;
7426 Return_Obj_Id
: Entity_Id
;
7427 Return_Obj_Decl
: Entity_Id
;
7430 -- Step past qualification, type conversion (which can occur in actual
7431 -- parameter contexts), and unchecked conversion (which can occur in
7432 -- cases of calls to 'Input).
7434 if Nkind_In
(Func_Call
, N_Qualified_Expression
,
7436 N_Unchecked_Type_Conversion
)
7438 Func_Call
:= Expression
(Func_Call
);
7441 -- If the call has already been processed to add build-in-place actuals
7442 -- then return. One place this can occur is for calls to build-in-place
7443 -- functions that occur within a call to a protected operation, where
7444 -- due to rewriting and expansion of the protected call there can be
7445 -- more than one call to Expand_Actuals for the same set of actuals.
7447 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
7451 -- Mark the call as processed as a build-in-place call
7453 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
7455 Loc
:= Sloc
(Function_Call
);
7457 if Is_Entity_Name
(Name
(Func_Call
)) then
7458 Function_Id
:= Entity
(Name
(Func_Call
));
7460 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
7461 Function_Id
:= Etype
(Name
(Func_Call
));
7464 raise Program_Error
;
7467 Result_Subt
:= Etype
(Function_Id
);
7469 -- If the build-in-place function returns a controlled object, then the
7470 -- object needs to be finalized immediately after the context. Since
7471 -- this case produces a transient scope, the servicing finalizer needs
7472 -- to name the returned object. Create a temporary which is initialized
7473 -- with the function call:
7475 -- Temp_Id : Func_Type := BIP_Func_Call;
7477 -- The initialization expression of the temporary will be rewritten by
7478 -- the expander using the appropriate mechanism in Make_Build_In_Place_
7479 -- Call_In_Object_Declaration.
7481 if Needs_Finalization
(Result_Subt
) then
7483 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'R');
7484 Temp_Decl
: Node_Id
;
7487 -- Reset the guard on the function call since the following does
7488 -- not perform actual call expansion.
7490 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
, False);
7493 Make_Object_Declaration
(Loc
,
7494 Defining_Identifier
=> Temp_Id
,
7495 Object_Definition
=>
7496 New_Occurrence_Of
(Result_Subt
, Loc
),
7498 New_Copy_Tree
(Function_Call
));
7500 Insert_Action
(Function_Call
, Temp_Decl
);
7502 Rewrite
(Function_Call
, New_Occurrence_Of
(Temp_Id
, Loc
));
7503 Analyze
(Function_Call
);
7506 -- When the result subtype is constrained, an object of the subtype is
7507 -- declared and an access value designating it is passed as an actual.
7509 elsif Is_Constrained
(Underlying_Type
(Result_Subt
)) then
7511 -- Create a temporary object to hold the function result
7513 Return_Obj_Id
:= Make_Temporary
(Loc
, 'R');
7514 Set_Etype
(Return_Obj_Id
, Result_Subt
);
7517 Make_Object_Declaration
(Loc
,
7518 Defining_Identifier
=> Return_Obj_Id
,
7519 Aliased_Present
=> True,
7520 Object_Definition
=> New_Occurrence_Of
(Result_Subt
, Loc
));
7522 Set_No_Initialization
(Return_Obj_Decl
);
7524 Insert_Action
(Func_Call
, Return_Obj_Decl
);
7526 -- When the function has a controlling result, an allocation-form
7527 -- parameter must be passed indicating that the caller is allocating
7528 -- the result object. This is needed because such a function can be
7529 -- called as a dispatching operation and must be treated similarly
7530 -- to functions with unconstrained result subtypes.
7532 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7533 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
7535 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7536 (Func_Call
, Function_Id
);
7538 Add_Task_Actuals_To_Build_In_Place_Call
7539 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
7541 -- Add an implicit actual to the function call that provides access
7542 -- to the caller's return object.
7544 Add_Access_Actual_To_Build_In_Place_Call
7545 (Func_Call
, Function_Id
, New_Occurrence_Of
(Return_Obj_Id
, Loc
));
7547 -- When the result subtype is unconstrained, the function must allocate
7548 -- the return object in the secondary stack, so appropriate implicit
7549 -- parameters are added to the call to indicate that. A transient
7550 -- scope is established to ensure eventual cleanup of the result.
7553 -- Pass an allocation parameter indicating that the function should
7554 -- allocate its result on the secondary stack.
7556 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7557 (Func_Call
, Function_Id
, Alloc_Form
=> Secondary_Stack
);
7559 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7560 (Func_Call
, Function_Id
);
7562 Add_Task_Actuals_To_Build_In_Place_Call
7563 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
7565 -- Pass a null value to the function since no return object is
7566 -- available on the caller side.
7568 Add_Access_Actual_To_Build_In_Place_Call
7569 (Func_Call
, Function_Id
, Empty
);
7571 end Make_Build_In_Place_Call_In_Anonymous_Context
;
7573 --------------------------------------------
7574 -- Make_Build_In_Place_Call_In_Assignment --
7575 --------------------------------------------
7577 procedure Make_Build_In_Place_Call_In_Assignment
7579 Function_Call
: Node_Id
)
7581 Lhs
: constant Node_Id
:= Name
(Assign
);
7582 Func_Call
: Node_Id
:= Function_Call
;
7583 Func_Id
: Entity_Id
;
7587 Ptr_Typ
: Entity_Id
;
7588 Ptr_Typ_Decl
: Node_Id
;
7590 Result_Subt
: Entity_Id
;
7594 -- Step past qualification or unchecked conversion (the latter can occur
7595 -- in cases of calls to 'Input).
7597 if Nkind_In
(Func_Call
, N_Qualified_Expression
,
7598 N_Unchecked_Type_Conversion
)
7600 Func_Call
:= Expression
(Func_Call
);
7603 -- If the call has already been processed to add build-in-place actuals
7604 -- then return. This should not normally occur in an assignment context,
7605 -- but we add the protection as a defensive measure.
7607 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
7611 -- Mark the call as processed as a build-in-place call
7613 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
7615 Loc
:= Sloc
(Function_Call
);
7617 if Is_Entity_Name
(Name
(Func_Call
)) then
7618 Func_Id
:= Entity
(Name
(Func_Call
));
7620 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
7621 Func_Id
:= Etype
(Name
(Func_Call
));
7624 raise Program_Error
;
7627 Result_Subt
:= Etype
(Func_Id
);
7629 -- When the result subtype is unconstrained, an additional actual must
7630 -- be passed to indicate that the caller is providing the return object.
7631 -- This parameter must also be passed when the called function has a
7632 -- controlling result, because dispatching calls to the function needs
7633 -- to be treated effectively the same as calls to class-wide functions.
7635 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7636 (Func_Call
, Func_Id
, Alloc_Form
=> Caller_Allocation
);
7638 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7639 (Func_Call
, Func_Id
);
7641 Add_Task_Actuals_To_Build_In_Place_Call
7642 (Func_Call
, Func_Id
, Make_Identifier
(Loc
, Name_uMaster
));
7644 -- Add an implicit actual to the function call that provides access to
7645 -- the caller's return object.
7647 Add_Access_Actual_To_Build_In_Place_Call
7650 Make_Unchecked_Type_Conversion
(Loc
,
7651 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Loc
),
7652 Expression
=> Relocate_Node
(Lhs
)));
7654 -- Create an access type designating the function's result subtype
7656 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
7659 Make_Full_Type_Declaration
(Loc
,
7660 Defining_Identifier
=> Ptr_Typ
,
7662 Make_Access_To_Object_Definition
(Loc
,
7663 All_Present
=> True,
7664 Subtype_Indication
=>
7665 New_Occurrence_Of
(Result_Subt
, Loc
)));
7666 Insert_After_And_Analyze
(Assign
, Ptr_Typ_Decl
);
7668 -- Finally, create an access object initialized to a reference to the
7669 -- function call. We know this access value is non-null, so mark the
7670 -- entity accordingly to suppress junk access checks.
7672 New_Expr
:= Make_Reference
(Loc
, Relocate_Node
(Func_Call
));
7674 Obj_Id
:= Make_Temporary
(Loc
, 'R', New_Expr
);
7675 Set_Etype
(Obj_Id
, Ptr_Typ
);
7676 Set_Is_Known_Non_Null
(Obj_Id
);
7679 Make_Object_Declaration
(Loc
,
7680 Defining_Identifier
=> Obj_Id
,
7681 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
7682 Expression
=> New_Expr
);
7683 Insert_After_And_Analyze
(Ptr_Typ_Decl
, Obj_Decl
);
7685 Rewrite
(Assign
, Make_Null_Statement
(Loc
));
7687 -- Retrieve the target of the assignment
7689 if Nkind
(Lhs
) = N_Selected_Component
then
7690 Target
:= Selector_Name
(Lhs
);
7691 elsif Nkind
(Lhs
) = N_Type_Conversion
then
7692 Target
:= Expression
(Lhs
);
7697 -- If we are assigning to a return object or this is an expression of
7698 -- an extension aggregate, the target should either be an identifier
7699 -- or a simple expression. All other cases imply a different scenario.
7701 if Nkind
(Target
) in N_Has_Entity
then
7702 Target
:= Entity
(Target
);
7706 end Make_Build_In_Place_Call_In_Assignment
;
7708 ----------------------------------------------------
7709 -- Make_Build_In_Place_Call_In_Object_Declaration --
7710 ----------------------------------------------------
7712 procedure Make_Build_In_Place_Call_In_Object_Declaration
7713 (Obj_Decl
: Node_Id
;
7714 Function_Call
: Node_Id
)
7716 Obj_Def_Id
: constant Entity_Id
:= Defining_Identifier
(Obj_Decl
);
7717 Encl_Func
: constant Entity_Id
:= Enclosing_Subprogram
(Obj_Def_Id
);
7718 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
7719 Obj_Loc
: constant Source_Ptr
:= Sloc
(Obj_Decl
);
7721 Call_Deref
: Node_Id
;
7722 Caller_Object
: Node_Id
;
7724 Fmaster_Actual
: Node_Id
:= Empty
;
7725 Func_Call
: Node_Id
:= Function_Call
;
7726 Function_Id
: Entity_Id
;
7727 Pool_Actual
: Node_Id
;
7728 Ptr_Typ
: Entity_Id
;
7729 Ptr_Typ_Decl
: Node_Id
;
7730 Pass_Caller_Acc
: Boolean := False;
7732 Result_Subt
: Entity_Id
;
7735 -- True for definite function result subtype
7738 -- Step past qualification or unchecked conversion (the latter can occur
7739 -- in cases of calls to 'Input).
7741 if Nkind_In
(Func_Call
, N_Qualified_Expression
,
7742 N_Unchecked_Type_Conversion
)
7744 Func_Call
:= Expression
(Func_Call
);
7747 -- If the call has already been processed to add build-in-place actuals
7748 -- then return. This should not normally occur in an object declaration,
7749 -- but we add the protection as a defensive measure.
7751 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
7755 -- Mark the call as processed as a build-in-place call
7757 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
7759 if Is_Entity_Name
(Name
(Func_Call
)) then
7760 Function_Id
:= Entity
(Name
(Func_Call
));
7762 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
7763 Function_Id
:= Etype
(Name
(Func_Call
));
7766 raise Program_Error
;
7769 Result_Subt
:= Etype
(Function_Id
);
7770 Definite
:= Is_Definite_Subtype
(Underlying_Type
(Result_Subt
));
7772 -- Create an access type designating the function's result subtype. We
7773 -- use the type of the original call because it may be a call to an
7774 -- inherited operation, which the expansion has replaced with the parent
7775 -- operation that yields the parent type. Note that this access type
7776 -- must be declared before we establish a transient scope, so that it
7777 -- receives the proper accessibility level.
7779 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
7781 Make_Full_Type_Declaration
(Loc
,
7782 Defining_Identifier
=> Ptr_Typ
,
7784 Make_Access_To_Object_Definition
(Loc
,
7785 All_Present
=> True,
7786 Subtype_Indication
=>
7787 New_Occurrence_Of
(Etype
(Function_Call
), Loc
)));
7789 -- The access type and its accompanying object must be inserted after
7790 -- the object declaration in the constrained case, so that the function
7791 -- call can be passed access to the object. In the indefinite case,
7792 -- or if the object declaration is for a return object, the access type
7793 -- and object must be inserted before the object, since the object
7794 -- declaration is rewritten to be a renaming of a dereference of the
7795 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
7796 -- the result object is in a different (transient) scope, so won't
7800 and then not Is_Return_Object
(Defining_Identifier
(Obj_Decl
))
7802 Insert_After_And_Analyze
(Obj_Decl
, Ptr_Typ_Decl
);
7804 Insert_Action
(Obj_Decl
, Ptr_Typ_Decl
);
7807 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
7808 -- elaborated in an inner (transient) scope and thus won't cause
7809 -- freezing by itself.
7812 Ptr_Typ_Freeze_Ref
: constant Node_Id
:=
7813 New_Occurrence_Of
(Ptr_Typ
, Loc
);
7815 Set_Parent
(Ptr_Typ_Freeze_Ref
, Ptr_Typ_Decl
);
7816 Freeze_Expression
(Ptr_Typ_Freeze_Ref
);
7819 -- If the object is a return object of an enclosing build-in-place
7820 -- function, then the implicit build-in-place parameters of the
7821 -- enclosing function are simply passed along to the called function.
7822 -- (Unfortunately, this won't cover the case of extension aggregates
7823 -- where the ancestor part is a build-in-place indefinite function
7824 -- call that should be passed along the caller's parameters. Currently
7825 -- those get mishandled by reassigning the result of the call to the
7826 -- aggregate return object, when the call result should really be
7827 -- directly built in place in the aggregate and not in a temporary. ???)
7829 if Is_Return_Object
(Defining_Identifier
(Obj_Decl
)) then
7830 Pass_Caller_Acc
:= True;
7832 -- When the enclosing function has a BIP_Alloc_Form formal then we
7833 -- pass it along to the callee (such as when the enclosing function
7834 -- has an unconstrained or tagged result type).
7836 if Needs_BIP_Alloc_Form
(Encl_Func
) then
7837 if RTE_Available
(RE_Root_Storage_Pool_Ptr
) then
7840 (Build_In_Place_Formal
(Encl_Func
, BIP_Storage_Pool
), Loc
);
7842 -- The build-in-place pool formal is not built on e.g. ZFP
7845 Pool_Actual
:= Empty
;
7848 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7849 (Function_Call
=> Func_Call
,
7850 Function_Id
=> Function_Id
,
7853 (Build_In_Place_Formal
(Encl_Func
, BIP_Alloc_Form
), Loc
),
7854 Pool_Actual
=> Pool_Actual
);
7856 -- Otherwise, if enclosing function has a definite result subtype,
7857 -- then caller allocation will be used.
7860 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7861 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
7864 if Needs_BIP_Finalization_Master
(Encl_Func
) then
7867 (Build_In_Place_Formal
7868 (Encl_Func
, BIP_Finalization_Master
), Loc
);
7871 -- Retrieve the BIPacc formal from the enclosing function and convert
7872 -- it to the access type of the callee's BIP_Object_Access formal.
7875 Make_Unchecked_Type_Conversion
(Loc
,
7879 (Build_In_Place_Formal
(Function_Id
, BIP_Object_Access
)),
7883 (Build_In_Place_Formal
(Encl_Func
, BIP_Object_Access
),
7886 -- In the definite case, add an implicit actual to the function call
7887 -- that provides access to the declared object. An unchecked conversion
7888 -- to the (specific) result type of the function is inserted to handle
7889 -- the case where the object is declared with a class-wide type.
7893 Make_Unchecked_Type_Conversion
(Loc
,
7894 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Loc
),
7895 Expression
=> New_Occurrence_Of
(Obj_Def_Id
, Loc
));
7897 -- When the function has a controlling result, an allocation-form
7898 -- parameter must be passed indicating that the caller is allocating
7899 -- the result object. This is needed because such a function can be
7900 -- called as a dispatching operation and must be treated similarly
7901 -- to functions with indefinite result subtypes.
7903 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7904 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
7906 -- The allocation for indefinite library-level objects occurs on the
7907 -- heap as opposed to the secondary stack. This accommodates DLLs where
7908 -- the secondary stack is destroyed after each library unload. This is
7909 -- a hybrid mechanism where a stack-allocated object lives on the heap.
7911 elsif Is_Library_Level_Entity
(Defining_Identifier
(Obj_Decl
))
7912 and then not Restriction_Active
(No_Implicit_Heap_Allocations
)
7914 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7915 (Func_Call
, Function_Id
, Alloc_Form
=> Global_Heap
);
7916 Caller_Object
:= Empty
;
7918 -- Create a finalization master for the access result type to ensure
7919 -- that the heap allocation can properly chain the object and later
7920 -- finalize it when the library unit goes out of scope.
7922 if Needs_Finalization
(Etype
(Func_Call
)) then
7923 Build_Finalization_Master
7925 For_Lib_Level
=> True,
7926 Insertion_Node
=> Ptr_Typ_Decl
);
7929 Make_Attribute_Reference
(Loc
,
7931 New_Occurrence_Of
(Finalization_Master
(Ptr_Typ
), Loc
),
7932 Attribute_Name
=> Name_Unrestricted_Access
);
7935 -- In other indefinite cases, pass an indication to do the allocation
7936 -- on the secondary stack and set Caller_Object to Empty so that a null
7937 -- value will be passed for the caller's object address. A transient
7938 -- scope is established to ensure eventual cleanup of the result.
7941 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7942 (Func_Call
, Function_Id
, Alloc_Form
=> Secondary_Stack
);
7943 Caller_Object
:= Empty
;
7945 Establish_Transient_Scope
(Obj_Decl
, Sec_Stack
=> True);
7948 -- Pass along any finalization master actual, which is needed in the
7949 -- case where the called function initializes a return object of an
7950 -- enclosing build-in-place function.
7952 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7953 (Func_Call
=> Func_Call
,
7954 Func_Id
=> Function_Id
,
7955 Master_Exp
=> Fmaster_Actual
);
7957 if Nkind
(Parent
(Obj_Decl
)) = N_Extended_Return_Statement
7958 and then Has_Task
(Result_Subt
)
7960 -- Here we're passing along the master that was passed in to this
7963 Add_Task_Actuals_To_Build_In_Place_Call
7964 (Func_Call
, Function_Id
,
7967 (Build_In_Place_Formal
(Encl_Func
, BIP_Task_Master
), Loc
));
7970 Add_Task_Actuals_To_Build_In_Place_Call
7971 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
7974 Add_Access_Actual_To_Build_In_Place_Call
7975 (Func_Call
, Function_Id
, Caller_Object
, Is_Access
=> Pass_Caller_Acc
);
7977 -- Finally, create an access object initialized to a reference to the
7978 -- function call. We know this access value cannot be null, so mark the
7979 -- entity accordingly to suppress the access check.
7981 Def_Id
:= Make_Temporary
(Loc
, 'R', Func_Call
);
7982 Set_Etype
(Def_Id
, Ptr_Typ
);
7983 Set_Is_Known_Non_Null
(Def_Id
);
7986 Make_Object_Declaration
(Loc
,
7987 Defining_Identifier
=> Def_Id
,
7988 Constant_Present
=> True,
7989 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
7991 Make_Reference
(Loc
, Relocate_Node
(Func_Call
)));
7993 Insert_After_And_Analyze
(Ptr_Typ_Decl
, Res_Decl
);
7995 -- If the result subtype of the called function is definite and is not
7996 -- itself the return expression of an enclosing BIP function, then mark
7997 -- the object as having no initialization.
8000 and then not Is_Return_Object
(Defining_Identifier
(Obj_Decl
))
8002 -- The related object declaration is encased in a transient block
8003 -- because the build-in-place function call contains at least one
8004 -- nested function call that produces a controlled transient
8007 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
8009 -- Since the build-in-place expansion decouples the call from the
8010 -- object declaration, the finalization machinery lacks the context
8011 -- which prompted the generation of the transient block. To resolve
8012 -- this scenario, store the build-in-place call.
8014 if Scope_Is_Transient
and then Node_To_Be_Wrapped
= Obj_Decl
then
8015 Set_BIP_Initialization_Call
(Obj_Def_Id
, Res_Decl
);
8018 Set_Expression
(Obj_Decl
, Empty
);
8019 Set_No_Initialization
(Obj_Decl
);
8021 -- In case of an indefinite result subtype, or if the call is the
8022 -- return expression of an enclosing BIP function, rewrite the object
8023 -- declaration as an object renaming where the renamed object is a
8024 -- dereference of <function_Call>'reference:
8026 -- Obj : Subt renames <function_call>'Ref.all;
8030 Make_Explicit_Dereference
(Obj_Loc
,
8031 Prefix
=> New_Occurrence_Of
(Def_Id
, Obj_Loc
));
8034 Make_Object_Renaming_Declaration
(Obj_Loc
,
8035 Defining_Identifier
=> Make_Temporary
(Obj_Loc
, 'D'),
8036 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Obj_Loc
),
8037 Name
=> Call_Deref
));
8039 Set_Renamed_Object
(Defining_Identifier
(Obj_Decl
), Call_Deref
);
8041 -- If the original entity comes from source, then mark the new
8042 -- entity as needing debug information, even though it's defined
8043 -- by a generated renaming that does not come from source, so that
8044 -- the Materialize_Entity flag will be set on the entity when
8045 -- Debug_Renaming_Declaration is called during analysis.
8047 if Comes_From_Source
(Obj_Def_Id
) then
8048 Set_Debug_Info_Needed
(Defining_Identifier
(Obj_Decl
));
8053 -- Replace the internal identifier of the renaming declaration's
8054 -- entity with identifier of the original object entity. We also have
8055 -- to exchange the entities containing their defining identifiers to
8056 -- ensure the correct replacement of the object declaration by the
8057 -- object renaming declaration to avoid homograph conflicts (since
8058 -- the object declaration's defining identifier was already entered
8059 -- in current scope). The Next_Entity links of the two entities also
8060 -- have to be swapped since the entities are part of the return
8061 -- scope's entity list and the list structure would otherwise be
8062 -- corrupted. Finally, the homonym chain must be preserved as well.
8065 Ren_Id
: constant Entity_Id
:= Defining_Entity
(Obj_Decl
);
8066 Next_Id
: constant Entity_Id
:= Next_Entity
(Ren_Id
);
8069 Set_Chars
(Ren_Id
, Chars
(Obj_Def_Id
));
8071 -- Swap next entity links in preparation for exchanging entities
8073 Set_Next_Entity
(Ren_Id
, Next_Entity
(Obj_Def_Id
));
8074 Set_Next_Entity
(Obj_Def_Id
, Next_Id
);
8075 Set_Homonym
(Ren_Id
, Homonym
(Obj_Def_Id
));
8077 Exchange_Entities
(Ren_Id
, Obj_Def_Id
);
8079 -- Preserve source indication of original declaration, so that
8080 -- xref information is properly generated for the right entity.
8082 Preserve_Comes_From_Source
(Obj_Decl
, Original_Node
(Obj_Decl
));
8083 Preserve_Comes_From_Source
(Obj_Def_Id
, Original_Node
(Obj_Decl
));
8085 Set_Comes_From_Source
(Ren_Id
, False);
8089 -- If the object entity has a class-wide Etype, then we need to change
8090 -- it to the result subtype of the function call, because otherwise the
8091 -- object will be class-wide without an explicit initialization and
8092 -- won't be allocated properly by the back end. It seems unclean to make
8093 -- such a revision to the type at this point, and we should try to
8094 -- improve this treatment when build-in-place functions with class-wide
8095 -- results are implemented. ???
8097 if Is_Class_Wide_Type
(Etype
(Defining_Identifier
(Obj_Decl
))) then
8098 Set_Etype
(Defining_Identifier
(Obj_Decl
), Result_Subt
);
8100 end Make_Build_In_Place_Call_In_Object_Declaration
;
8102 --------------------------------------------
8103 -- Make_CPP_Constructor_Call_In_Allocator --
8104 --------------------------------------------
8106 procedure Make_CPP_Constructor_Call_In_Allocator
8107 (Allocator
: Node_Id
;
8108 Function_Call
: Node_Id
)
8110 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
8111 Acc_Type
: constant Entity_Id
:= Etype
(Allocator
);
8112 Function_Id
: constant Entity_Id
:= Entity
(Name
(Function_Call
));
8113 Result_Subt
: constant Entity_Id
:= Available_View
(Etype
(Function_Id
));
8115 New_Allocator
: Node_Id
;
8116 Return_Obj_Access
: Entity_Id
;
8120 pragma Assert
(Nkind
(Allocator
) = N_Allocator
8121 and then Nkind
(Function_Call
) = N_Function_Call
);
8122 pragma Assert
(Convention
(Function_Id
) = Convention_CPP
8123 and then Is_Constructor
(Function_Id
));
8124 pragma Assert
(Is_Constrained
(Underlying_Type
(Result_Subt
)));
8126 -- Replace the initialized allocator of form "new T'(Func (...))" with
8127 -- an uninitialized allocator of form "new T", where T is the result
8128 -- subtype of the called function. The call to the function is handled
8129 -- separately further below.
8132 Make_Allocator
(Loc
,
8133 Expression
=> New_Occurrence_Of
(Result_Subt
, Loc
));
8134 Set_No_Initialization
(New_Allocator
);
8136 -- Copy attributes to new allocator. Note that the new allocator
8137 -- logically comes from source if the original one did, so copy the
8138 -- relevant flag. This ensures proper treatment of the restriction
8139 -- No_Implicit_Heap_Allocations in this case.
8141 Set_Storage_Pool
(New_Allocator
, Storage_Pool
(Allocator
));
8142 Set_Procedure_To_Call
(New_Allocator
, Procedure_To_Call
(Allocator
));
8143 Set_Comes_From_Source
(New_Allocator
, Comes_From_Source
(Allocator
));
8145 Rewrite
(Allocator
, New_Allocator
);
8147 -- Create a new access object and initialize it to the result of the
8148 -- new uninitialized allocator. Note: we do not use Allocator as the
8149 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
8150 -- as this would create a sort of infinite "recursion".
8152 Return_Obj_Access
:= Make_Temporary
(Loc
, 'R');
8153 Set_Etype
(Return_Obj_Access
, Acc_Type
);
8156 -- Rnnn : constant ptr_T := new (T);
8157 -- Init (Rnn.all,...);
8160 Make_Object_Declaration
(Loc
,
8161 Defining_Identifier
=> Return_Obj_Access
,
8162 Constant_Present
=> True,
8163 Object_Definition
=> New_Occurrence_Of
(Acc_Type
, Loc
),
8164 Expression
=> Relocate_Node
(Allocator
));
8165 Insert_Action
(Allocator
, Tmp_Obj
);
8167 Insert_List_After_And_Analyze
(Tmp_Obj
,
8168 Build_Initialization_Call
(Loc
,
8170 Make_Explicit_Dereference
(Loc
,
8171 Prefix
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
)),
8172 Typ
=> Etype
(Function_Id
),
8173 Constructor_Ref
=> Function_Call
));
8175 -- Finally, replace the allocator node with a reference to the result of
8176 -- the function call itself (which will effectively be an access to the
8177 -- object created by the allocator).
8179 Rewrite
(Allocator
, New_Occurrence_Of
(Return_Obj_Access
, Loc
));
8181 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
8182 -- generate an implicit conversion to force displacement of the "this"
8185 if Is_Interface
(Designated_Type
(Acc_Type
)) then
8186 Rewrite
(Allocator
, Convert_To
(Acc_Type
, Relocate_Node
(Allocator
)));
8189 Analyze_And_Resolve
(Allocator
, Acc_Type
);
8190 end Make_CPP_Constructor_Call_In_Allocator
;
8192 -----------------------------------
8193 -- Needs_BIP_Finalization_Master --
8194 -----------------------------------
8196 function Needs_BIP_Finalization_Master
8197 (Func_Id
: Entity_Id
) return Boolean
8199 pragma Assert
(Is_Build_In_Place_Function
(Func_Id
));
8200 Func_Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
8203 not Restriction_Active
(No_Finalization
)
8204 and then Needs_Finalization
(Func_Typ
);
8205 end Needs_BIP_Finalization_Master
;
8207 --------------------------
8208 -- Needs_BIP_Alloc_Form --
8209 --------------------------
8211 function Needs_BIP_Alloc_Form
(Func_Id
: Entity_Id
) return Boolean is
8212 pragma Assert
(Is_Build_In_Place_Function
(Func_Id
));
8213 Func_Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
8215 return not Is_Constrained
(Func_Typ
) or else Is_Tagged_Type
(Func_Typ
);
8216 end Needs_BIP_Alloc_Form
;
8218 --------------------------------------
8219 -- Needs_Result_Accessibility_Level --
8220 --------------------------------------
8222 function Needs_Result_Accessibility_Level
8223 (Func_Id
: Entity_Id
) return Boolean
8225 Func_Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
8227 function Has_Unconstrained_Access_Discriminant_Component
8228 (Comp_Typ
: Entity_Id
) return Boolean;
8229 -- Returns True if any component of the type has an unconstrained access
8232 -----------------------------------------------------
8233 -- Has_Unconstrained_Access_Discriminant_Component --
8234 -----------------------------------------------------
8236 function Has_Unconstrained_Access_Discriminant_Component
8237 (Comp_Typ
: Entity_Id
) return Boolean
8240 if not Is_Limited_Type
(Comp_Typ
) then
8243 -- Only limited types can have access discriminants with
8246 elsif Has_Unconstrained_Access_Discriminants
(Comp_Typ
) then
8249 elsif Is_Array_Type
(Comp_Typ
) then
8250 return Has_Unconstrained_Access_Discriminant_Component
8251 (Underlying_Type
(Component_Type
(Comp_Typ
)));
8253 elsif Is_Record_Type
(Comp_Typ
) then
8258 Comp
:= First_Component
(Comp_Typ
);
8259 while Present
(Comp
) loop
8260 if Has_Unconstrained_Access_Discriminant_Component
8261 (Underlying_Type
(Etype
(Comp
)))
8266 Next_Component
(Comp
);
8272 end Has_Unconstrained_Access_Discriminant_Component
;
8274 Feature_Disabled
: constant Boolean := True;
8277 -- Start of processing for Needs_Result_Accessibility_Level
8280 -- False if completion unavailable (how does this happen???)
8282 if not Present
(Func_Typ
) then
8285 elsif Feature_Disabled
then
8288 -- False if not a function, also handle enum-lit renames case
8290 elsif Func_Typ
= Standard_Void_Type
8291 or else Is_Scalar_Type
(Func_Typ
)
8295 -- Handle a corner case, a cross-dialect subp renaming. For example,
8296 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
8297 -- an Ada 2005 (or earlier) unit references predefined run-time units.
8299 elsif Present
(Alias
(Func_Id
)) then
8301 -- Unimplemented: a cross-dialect subp renaming which does not set
8302 -- the Alias attribute (e.g., a rename of a dereference of an access
8303 -- to subprogram value). ???
8305 return Present
(Extra_Accessibility_Of_Result
(Alias
(Func_Id
)));
8307 -- Remaining cases require Ada 2012 mode
8309 elsif Ada_Version
< Ada_2012
then
8312 elsif Ekind
(Func_Typ
) = E_Anonymous_Access_Type
8313 or else Is_Tagged_Type
(Func_Typ
)
8315 -- In the case of, say, a null tagged record result type, the need
8316 -- for this extra parameter might not be obvious. This function
8317 -- returns True for all tagged types for compatibility reasons.
8318 -- A function with, say, a tagged null controlling result type might
8319 -- be overridden by a primitive of an extension having an access
8320 -- discriminant and the overrider and overridden must have compatible
8321 -- calling conventions (including implicitly declared parameters).
8322 -- Similarly, values of one access-to-subprogram type might designate
8323 -- both a primitive subprogram of a given type and a function
8324 -- which is, for example, not a primitive subprogram of any type.
8325 -- Again, this requires calling convention compatibility.
8326 -- It might be possible to solve these issues by introducing
8327 -- wrappers, but that is not the approach that was chosen.
8331 elsif Has_Unconstrained_Access_Discriminants
(Func_Typ
) then
8334 elsif Has_Unconstrained_Access_Discriminant_Component
(Func_Typ
) then
8337 -- False for all other cases
8342 end Needs_Result_Accessibility_Level
;
8344 ---------------------------------
8345 -- Rewrite_Function_Call_For_C --
8346 ---------------------------------
8348 procedure Rewrite_Function_Call_For_C
(N
: Node_Id
) is
8349 Func_Id
: constant Entity_Id
:= Entity
(Name
(N
));
8350 Func_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Func_Id
);
8351 Par
: constant Node_Id
:= Parent
(N
);
8352 Proc_Id
: constant Entity_Id
:= Defining_Entity
(Next
(Func_Decl
));
8353 Loc
: constant Source_Ptr
:= Sloc
(Par
);
8355 Last_Formal
: Entity_Id
;
8358 -- The actuals may be given by named associations, so the added actual
8359 -- that is the target of the return value of the call must be a named
8360 -- association as well, so we retrieve the name of the generated
8363 Last_Formal
:= First_Formal
(Proc_Id
);
8364 while Present
(Next_Formal
(Last_Formal
)) loop
8365 Last_Formal
:= Next_Formal
(Last_Formal
);
8368 Actuals
:= Parameter_Associations
(N
);
8370 -- The original function may lack parameters
8372 if No
(Actuals
) then
8373 Actuals
:= New_List
;
8376 -- If the function call is the expression of an assignment statement,
8377 -- transform the assignment into a procedure call. Generate:
8379 -- LHS := Func_Call (...);
8381 -- Proc_Call (..., LHS);
8383 if Nkind
(Par
) = N_Assignment_Statement
then
8385 Make_Parameter_Association
(Loc
,
8387 Make_Identifier
(Loc
, Chars
(Last_Formal
)),
8388 Explicit_Actual_Parameter
=> Name
(Par
)));
8391 Make_Procedure_Call_Statement
(Loc
,
8392 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
8393 Parameter_Associations
=> Actuals
));
8396 -- Otherwise the context is an expression. Generate a temporary and a
8397 -- procedure call to obtain the function result. Generate:
8399 -- ... Func_Call (...) ...
8402 -- Proc_Call (..., Temp);
8407 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
8416 Make_Object_Declaration
(Loc
,
8417 Defining_Identifier
=> Temp_Id
,
8418 Object_Definition
=>
8419 New_Occurrence_Of
(Etype
(Func_Id
), Loc
));
8422 -- Proc_Call (..., Temp);
8425 Make_Parameter_Association
(Loc
,
8427 Make_Identifier
(Loc
, Chars
(Last_Formal
)),
8428 Explicit_Actual_Parameter
=>
8429 New_Occurrence_Of
(Temp_Id
, Loc
)));
8432 Make_Procedure_Call_Statement
(Loc
,
8433 Name
=> New_Occurrence_Of
(Proc_Id
, Loc
),
8434 Parameter_Associations
=> Actuals
);
8436 Insert_Actions
(Par
, New_List
(Decl
, Call
));
8437 Rewrite
(N
, New_Occurrence_Of
(Temp_Id
, Loc
));
8440 end Rewrite_Function_Call_For_C
;
8442 ------------------------------------
8443 -- Set_Enclosing_Sec_Stack_Return --
8444 ------------------------------------
8446 procedure Set_Enclosing_Sec_Stack_Return
(N
: Node_Id
) is
8450 -- Due to a possible mix of internally generated blocks, source blocks
8451 -- and loops, the scope stack may not be contiguous as all labels are
8452 -- inserted at the top level within the related function. Instead,
8453 -- perform a parent-based traversal and mark all appropriate constructs.
8455 while Present
(P
) loop
8457 -- Mark the label of a source or internally generated block or
8460 if Nkind_In
(P
, N_Block_Statement
, N_Loop_Statement
) then
8461 Set_Sec_Stack_Needed_For_Return
(Entity
(Identifier
(P
)));
8463 -- Mark the enclosing function
8465 elsif Nkind
(P
) = N_Subprogram_Body
then
8466 if Present
(Corresponding_Spec
(P
)) then
8467 Set_Sec_Stack_Needed_For_Return
(Corresponding_Spec
(P
));
8469 Set_Sec_Stack_Needed_For_Return
(Defining_Entity
(P
));
8472 -- Do not go beyond the enclosing function
8479 end Set_Enclosing_Sec_Stack_Return
;
8481 ------------------------
8482 -- Unnest_Subprograms --
8483 ------------------------
8485 procedure Unnest_Subprograms
is
8487 for J
in Unest_Bodies
.First
.. Unest_Bodies
.Last
loop
8489 UBJ
: Unest_Entry
renames Unest_Bodies
.Table
(J
);
8491 Unnest_Subprogram
(UBJ
.Ent
, UBJ
.Bod
);
8494 end Unnest_Subprograms
;