1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, 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 Debug
; use Debug
;
29 with Einfo
; use Einfo
;
30 with Errout
; use Errout
;
31 with Elists
; use Elists
;
32 with Exp_Aggr
; use Exp_Aggr
;
33 with Exp_Atag
; use Exp_Atag
;
34 with Exp_Ch2
; use Exp_Ch2
;
35 with Exp_Ch3
; use Exp_Ch3
;
36 with Exp_Ch7
; use Exp_Ch7
;
37 with Exp_Ch9
; use Exp_Ch9
;
38 with Exp_Dbug
; use Exp_Dbug
;
39 with Exp_Disp
; use Exp_Disp
;
40 with Exp_Dist
; use Exp_Dist
;
41 with Exp_Intr
; use Exp_Intr
;
42 with Exp_Pakd
; use Exp_Pakd
;
43 with Exp_Prag
; use Exp_Prag
;
44 with Exp_Tss
; use Exp_Tss
;
45 with Exp_Util
; use Exp_Util
;
46 with Fname
; use Fname
;
47 with Freeze
; use Freeze
;
48 with Inline
; use Inline
;
50 with Namet
; use Namet
;
51 with Nlists
; use Nlists
;
52 with Nmake
; use Nmake
;
54 with Restrict
; use Restrict
;
55 with Rident
; use Rident
;
56 with Rtsfind
; use Rtsfind
;
58 with Sem_Aux
; use Sem_Aux
;
59 with Sem_Ch6
; use Sem_Ch6
;
60 with Sem_Ch8
; use Sem_Ch8
;
61 with Sem_Ch13
; use Sem_Ch13
;
62 with Sem_Dim
; use Sem_Dim
;
63 with Sem_Disp
; use Sem_Disp
;
64 with Sem_Dist
; use Sem_Dist
;
65 with Sem_Eval
; use Sem_Eval
;
66 with Sem_Mech
; use Sem_Mech
;
67 with Sem_Res
; use Sem_Res
;
68 with Sem_SCIL
; use Sem_SCIL
;
69 with Sem_Util
; use Sem_Util
;
70 with Sinfo
; use Sinfo
;
71 with Snames
; use Snames
;
72 with Stand
; use Stand
;
73 with Stringt
; use Stringt
;
74 with Targparm
; use Targparm
;
75 with Tbuild
; use Tbuild
;
76 with Uintp
; use Uintp
;
77 with Validsw
; use Validsw
;
79 package body Exp_Ch6
is
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 procedure Add_Access_Actual_To_Build_In_Place_Call
86 (Function_Call
: Node_Id
;
87 Function_Id
: Entity_Id
;
88 Return_Object
: Node_Id
;
89 Is_Access
: Boolean := False);
90 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
91 -- object name given by Return_Object and add the attribute to the end of
92 -- the actual parameter list associated with the build-in-place function
93 -- call denoted by Function_Call. However, if Is_Access is True, then
94 -- Return_Object is already an access expression, in which case it's passed
95 -- along directly to the build-in-place function. Finally, if Return_Object
96 -- is empty, then pass a null literal as the actual.
98 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
99 (Function_Call
: Node_Id
;
100 Function_Id
: Entity_Id
;
101 Alloc_Form
: BIP_Allocation_Form
:= Unspecified
;
102 Alloc_Form_Exp
: Node_Id
:= Empty
;
103 Pool_Actual
: Node_Id
:= Make_Null
(No_Location
));
104 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
105 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
106 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
107 -- otherwise pass a literal corresponding to the Alloc_Form parameter
108 -- (which must not be Unspecified in that case). Pool_Actual is the
109 -- parameter to pass to BIP_Storage_Pool.
111 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
112 (Func_Call
: Node_Id
;
114 Ptr_Typ
: Entity_Id
:= Empty
;
115 Master_Exp
: Node_Id
:= Empty
);
116 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
117 -- finalization actions, add an actual parameter which is a pointer to the
118 -- finalization master of the caller. If Master_Exp is not Empty, then that
119 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
120 -- will result in an automatic "null" value for the actual.
122 procedure Add_Task_Actuals_To_Build_In_Place_Call
123 (Function_Call
: Node_Id
;
124 Function_Id
: Entity_Id
;
125 Master_Actual
: Node_Id
;
126 Chain
: Node_Id
:= Empty
);
127 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
128 -- contains tasks, add two actual parameters: the master, and a pointer to
129 -- the caller's activation chain. Master_Actual is the actual parameter
130 -- expression to pass for the master. In most cases, this is the current
131 -- master (_master). The two exceptions are: If the function call is the
132 -- initialization expression for an allocator, we pass the master of the
133 -- access type. If the function call is the initialization expression for a
134 -- return object, we pass along the master passed in by the caller. In most
135 -- contexts, the activation chain to pass is the local one, which is
136 -- indicated by No (Chain). However, in an allocator, the caller passes in
137 -- the activation Chain. Note: Master_Actual can be Empty, but only if
138 -- there are no tasks.
140 procedure Check_Overriding_Operation
(Subp
: Entity_Id
);
141 -- Subp is a dispatching operation. Check whether it may override an
142 -- inherited private operation, in which case its DT entry is that of
143 -- the hidden operation, not the one it may have received earlier.
144 -- This must be done before emitting the code to set the corresponding
145 -- DT to the address of the subprogram. The actual placement of Subp in
146 -- the proper place in the list of primitive operations is done in
147 -- Declare_Inherited_Private_Subprograms, which also has to deal with
148 -- implicit operations. This duplication is unavoidable for now???
150 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
);
151 -- This procedure is called only if the subprogram body N, whose spec
152 -- has the given entity Spec, contains a parameterless recursive call.
153 -- It attempts to generate runtime code to detect if this a case of
154 -- infinite recursion.
156 -- The body is scanned to determine dependencies. If the only external
157 -- dependencies are on a small set of scalar variables, then the values
158 -- of these variables are captured on entry to the subprogram, and if
159 -- the values are not changed for the call, we know immediately that
160 -- we have an infinite recursion.
162 procedure Expand_Actuals
(N
: in out Node_Id
; Subp
: Entity_Id
);
163 -- For each actual of an in-out or out parameter which is a numeric
164 -- (view) conversion of the form T (A), where A denotes a variable,
165 -- we insert the declaration:
167 -- Temp : T[ := T (A)];
169 -- prior to the call. Then we replace the actual with a reference to Temp,
170 -- and append the assignment:
172 -- A := TypeA (Temp);
174 -- after the call. Here TypeA is the actual type of variable A. For out
175 -- parameters, the initial declaration has no expression. If A is not an
176 -- entity name, we generate instead:
178 -- Var : TypeA renames A;
179 -- Temp : T := Var; -- omitting expression for out parameter.
181 -- Var := TypeA (Temp);
183 -- For other in-out parameters, we emit the required constraint checks
184 -- before and/or after the call.
186 -- For all parameter modes, actuals that denote components and slices of
187 -- packed arrays are expanded into suitable temporaries.
189 -- For non-scalar objects that are possibly unaligned, add call by copy
190 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
192 -- For OUT and IN OUT parameters, add predicate checks after the call
193 -- based on the predicates of the actual type.
195 -- The parameter N is IN OUT because in some cases, the expansion code
196 -- rewrites the call as an expression actions with the call inside. In
197 -- this case N is reset to point to the inside call so that the caller
198 -- can continue processing of this call.
200 procedure Expand_Ctrl_Function_Call
(N
: Node_Id
);
201 -- N is a function call which returns a controlled object. Transform the
202 -- call into a temporary which retrieves the returned object from the
203 -- secondary stack using 'reference.
205 procedure Expand_Non_Function_Return
(N
: Node_Id
);
206 -- Called by Expand_N_Simple_Return_Statement in case we're returning from
207 -- a procedure body, entry body, accept statement, or extended return
208 -- statement. Note that all non-function returns are simple return
211 function Expand_Protected_Object_Reference
213 Scop
: Entity_Id
) return Node_Id
;
215 procedure Expand_Protected_Subprogram_Call
219 -- A call to a protected subprogram within the protected object may appear
220 -- as a regular call. The list of actuals must be expanded to contain a
221 -- reference to the object itself, and the call becomes a call to the
222 -- corresponding protected subprogram.
224 function Has_Unconstrained_Access_Discriminants
225 (Subtyp
: Entity_Id
) return Boolean;
226 -- Returns True if the given subtype is unconstrained and has one
227 -- or more access discriminants.
229 procedure Expand_Simple_Function_Return
(N
: Node_Id
);
230 -- Expand simple return from function. In the case where we are returning
231 -- from a function body this is called by Expand_N_Simple_Return_Statement.
233 ----------------------------------------------
234 -- Add_Access_Actual_To_Build_In_Place_Call --
235 ----------------------------------------------
237 procedure Add_Access_Actual_To_Build_In_Place_Call
238 (Function_Call
: Node_Id
;
239 Function_Id
: Entity_Id
;
240 Return_Object
: Node_Id
;
241 Is_Access
: Boolean := False)
243 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
244 Obj_Address
: Node_Id
;
245 Obj_Acc_Formal
: Entity_Id
;
248 -- Locate the implicit access parameter in the called function
250 Obj_Acc_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Object_Access
);
252 -- If no return object is provided, then pass null
254 if not Present
(Return_Object
) then
255 Obj_Address
:= Make_Null
(Loc
);
256 Set_Parent
(Obj_Address
, Function_Call
);
258 -- If Return_Object is already an expression of an access type, then use
259 -- it directly, since it must be an access value denoting the return
260 -- object, and couldn't possibly be the return object itself.
263 Obj_Address
:= Return_Object
;
264 Set_Parent
(Obj_Address
, Function_Call
);
266 -- Apply Unrestricted_Access to caller's return object
270 Make_Attribute_Reference
(Loc
,
271 Prefix
=> Return_Object
,
272 Attribute_Name
=> Name_Unrestricted_Access
);
274 Set_Parent
(Return_Object
, Obj_Address
);
275 Set_Parent
(Obj_Address
, Function_Call
);
278 Analyze_And_Resolve
(Obj_Address
, Etype
(Obj_Acc_Formal
));
280 -- Build the parameter association for the new actual and add it to the
281 -- end of the function's actuals.
283 Add_Extra_Actual_To_Call
(Function_Call
, Obj_Acc_Formal
, Obj_Address
);
284 end Add_Access_Actual_To_Build_In_Place_Call
;
286 ------------------------------------------------------
287 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
288 ------------------------------------------------------
290 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
291 (Function_Call
: Node_Id
;
292 Function_Id
: Entity_Id
;
293 Alloc_Form
: BIP_Allocation_Form
:= Unspecified
;
294 Alloc_Form_Exp
: Node_Id
:= Empty
;
295 Pool_Actual
: Node_Id
:= Make_Null
(No_Location
))
297 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
298 Alloc_Form_Actual
: Node_Id
;
299 Alloc_Form_Formal
: Node_Id
;
300 Pool_Formal
: Node_Id
;
303 -- The allocation form generally doesn't need to be passed in the case
304 -- of a constrained result subtype, since normally the caller performs
305 -- the allocation in that case. However this formal is still needed in
306 -- the case where the function has a tagged result, because generally
307 -- such functions can be called in a dispatching context and such calls
308 -- must be handled like calls to class-wide functions.
310 if Is_Constrained
(Underlying_Type
(Etype
(Function_Id
)))
311 and then not Is_Tagged_Type
(Underlying_Type
(Etype
(Function_Id
)))
316 -- Locate the implicit allocation form parameter in the called function.
317 -- Maybe it would be better for each implicit formal of a build-in-place
318 -- function to have a flag or a Uint attribute to identify it. ???
320 Alloc_Form_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Alloc_Form
);
322 if Present
(Alloc_Form_Exp
) then
323 pragma Assert
(Alloc_Form
= Unspecified
);
325 Alloc_Form_Actual
:= Alloc_Form_Exp
;
328 pragma Assert
(Alloc_Form
/= Unspecified
);
331 Make_Integer_Literal
(Loc
,
332 Intval
=> UI_From_Int
(BIP_Allocation_Form
'Pos (Alloc_Form
)));
335 Analyze_And_Resolve
(Alloc_Form_Actual
, Etype
(Alloc_Form_Formal
));
337 -- Build the parameter association for the new actual and add it to the
338 -- end of the function's actuals.
340 Add_Extra_Actual_To_Call
341 (Function_Call
, Alloc_Form_Formal
, Alloc_Form_Actual
);
343 -- Pass the Storage_Pool parameter. This parameter is omitted on
344 -- .NET/JVM/ZFP as those targets do not support pools.
347 and then RTE_Available
(RE_Root_Storage_Pool_Ptr
)
349 Pool_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Storage_Pool
);
350 Analyze_And_Resolve
(Pool_Actual
, Etype
(Pool_Formal
));
351 Add_Extra_Actual_To_Call
352 (Function_Call
, Pool_Formal
, Pool_Actual
);
354 end Add_Unconstrained_Actuals_To_Build_In_Place_Call
;
356 -----------------------------------------------------------
357 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
358 -----------------------------------------------------------
360 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
361 (Func_Call
: Node_Id
;
363 Ptr_Typ
: Entity_Id
:= Empty
;
364 Master_Exp
: Node_Id
:= Empty
)
367 if not Needs_BIP_Finalization_Master
(Func_Id
) then
372 Formal
: constant Entity_Id
:=
373 Build_In_Place_Formal
(Func_Id
, BIP_Finalization_Master
);
374 Loc
: constant Source_Ptr
:= Sloc
(Func_Call
);
377 Desig_Typ
: Entity_Id
;
380 -- If there is a finalization master actual, such as the implicit
381 -- finalization master of an enclosing build-in-place function,
382 -- then this must be added as an extra actual of the call.
384 if Present
(Master_Exp
) then
385 Actual
:= Master_Exp
;
387 -- Case where the context does not require an actual master
389 elsif No
(Ptr_Typ
) then
390 Actual
:= Make_Null
(Loc
);
393 Desig_Typ
:= Directly_Designated_Type
(Ptr_Typ
);
395 -- Check for a library-level access type whose designated type has
396 -- supressed finalization. Such an access types lack a master.
397 -- Pass a null actual to the callee in order to signal a missing
400 if Is_Library_Level_Entity
(Ptr_Typ
)
401 and then Finalize_Storage_Only
(Desig_Typ
)
403 Actual
:= Make_Null
(Loc
);
405 -- Types in need of finalization actions
407 elsif Needs_Finalization
(Desig_Typ
) then
409 -- The general mechanism of creating finalization masters for
410 -- anonymous access types is disabled by default, otherwise
411 -- finalization masters will pop all over the place. Such types
412 -- use context-specific masters.
414 if Ekind
(Ptr_Typ
) = E_Anonymous_Access_Type
415 and then No
(Finalization_Master
(Ptr_Typ
))
417 Build_Finalization_Master
419 Ins_Node
=> Associated_Node_For_Itype
(Ptr_Typ
),
420 Encl_Scope
=> Scope
(Ptr_Typ
));
423 -- Access-to-controlled types should always have a master
425 pragma Assert
(Present
(Finalization_Master
(Ptr_Typ
)));
428 Make_Attribute_Reference
(Loc
,
430 New_Occurrence_Of
(Finalization_Master
(Ptr_Typ
), Loc
),
431 Attribute_Name
=> Name_Unrestricted_Access
);
436 Actual
:= Make_Null
(Loc
);
440 Analyze_And_Resolve
(Actual
, Etype
(Formal
));
442 -- Build the parameter association for the new actual and add it to
443 -- the end of the function's actuals.
445 Add_Extra_Actual_To_Call
(Func_Call
, Formal
, Actual
);
447 end Add_Finalization_Master_Actual_To_Build_In_Place_Call
;
449 ------------------------------
450 -- Add_Extra_Actual_To_Call --
451 ------------------------------
453 procedure Add_Extra_Actual_To_Call
454 (Subprogram_Call
: Node_Id
;
455 Extra_Formal
: Entity_Id
;
456 Extra_Actual
: Node_Id
)
458 Loc
: constant Source_Ptr
:= Sloc
(Subprogram_Call
);
459 Param_Assoc
: Node_Id
;
463 Make_Parameter_Association
(Loc
,
464 Selector_Name
=> New_Occurrence_Of
(Extra_Formal
, Loc
),
465 Explicit_Actual_Parameter
=> Extra_Actual
);
467 Set_Parent
(Param_Assoc
, Subprogram_Call
);
468 Set_Parent
(Extra_Actual
, Param_Assoc
);
470 if Present
(Parameter_Associations
(Subprogram_Call
)) then
471 if Nkind
(Last
(Parameter_Associations
(Subprogram_Call
))) =
472 N_Parameter_Association
475 -- Find last named actual, and append
480 L
:= First_Actual
(Subprogram_Call
);
481 while Present
(L
) loop
482 if No
(Next_Actual
(L
)) then
483 Set_Next_Named_Actual
(Parent
(L
), Extra_Actual
);
491 Set_First_Named_Actual
(Subprogram_Call
, Extra_Actual
);
494 Append
(Param_Assoc
, To
=> Parameter_Associations
(Subprogram_Call
));
497 Set_Parameter_Associations
(Subprogram_Call
, New_List
(Param_Assoc
));
498 Set_First_Named_Actual
(Subprogram_Call
, Extra_Actual
);
500 end Add_Extra_Actual_To_Call
;
502 ---------------------------------------------
503 -- Add_Task_Actuals_To_Build_In_Place_Call --
504 ---------------------------------------------
506 procedure Add_Task_Actuals_To_Build_In_Place_Call
507 (Function_Call
: Node_Id
;
508 Function_Id
: Entity_Id
;
509 Master_Actual
: Node_Id
;
510 Chain
: Node_Id
:= Empty
)
512 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
513 Result_Subt
: constant Entity_Id
:=
514 Available_View
(Etype
(Function_Id
));
516 Chain_Actual
: Node_Id
;
517 Chain_Formal
: Node_Id
;
518 Master_Formal
: Node_Id
;
521 -- No such extra parameters are needed if there are no tasks
523 if not Has_Task
(Result_Subt
) then
527 Actual
:= Master_Actual
;
529 -- Use a dummy _master actual in case of No_Task_Hierarchy
531 if Restriction_Active
(No_Task_Hierarchy
) then
532 Actual
:= New_Occurrence_Of
(RTE
(RE_Library_Task_Level
), Loc
);
534 -- In the case where we use the master associated with an access type,
535 -- the actual is an entity and requires an explicit reference.
537 elsif Nkind
(Actual
) = N_Defining_Identifier
then
538 Actual
:= New_Occurrence_Of
(Actual
, Loc
);
541 -- Locate the implicit master parameter in the called function
543 Master_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Task_Master
);
544 Analyze_And_Resolve
(Actual
, Etype
(Master_Formal
));
546 -- Build the parameter association for the new actual and add it to the
547 -- end of the function's actuals.
549 Add_Extra_Actual_To_Call
(Function_Call
, Master_Formal
, Actual
);
551 -- Locate the implicit activation chain parameter in the called function
554 Build_In_Place_Formal
(Function_Id
, BIP_Activation_Chain
);
556 -- Create the actual which is a pointer to the current activation chain
560 Make_Attribute_Reference
(Loc
,
561 Prefix
=> Make_Identifier
(Loc
, Name_uChain
),
562 Attribute_Name
=> Name_Unrestricted_Access
);
564 -- Allocator case; make a reference to the Chain passed in by the caller
568 Make_Attribute_Reference
(Loc
,
569 Prefix
=> New_Occurrence_Of
(Chain
, Loc
),
570 Attribute_Name
=> Name_Unrestricted_Access
);
573 Analyze_And_Resolve
(Chain_Actual
, Etype
(Chain_Formal
));
575 -- Build the parameter association for the new actual and add it to the
576 -- end of the function's actuals.
578 Add_Extra_Actual_To_Call
(Function_Call
, Chain_Formal
, Chain_Actual
);
579 end Add_Task_Actuals_To_Build_In_Place_Call
;
581 -----------------------
582 -- BIP_Formal_Suffix --
583 -----------------------
585 function BIP_Formal_Suffix
(Kind
: BIP_Formal_Kind
) return String is
588 when BIP_Alloc_Form
=>
590 when BIP_Storage_Pool
=>
591 return "BIPstoragepool";
592 when BIP_Finalization_Master
=>
593 return "BIPfinalizationmaster";
594 when BIP_Task_Master
=>
595 return "BIPtaskmaster";
596 when BIP_Activation_Chain
=>
597 return "BIPactivationchain";
598 when BIP_Object_Access
=>
601 end BIP_Formal_Suffix
;
603 ---------------------------
604 -- Build_In_Place_Formal --
605 ---------------------------
607 function Build_In_Place_Formal
609 Kind
: BIP_Formal_Kind
) return Entity_Id
611 Formal_Name
: constant Name_Id
:=
613 (Chars
(Func
), BIP_Formal_Suffix
(Kind
));
614 Extra_Formal
: Entity_Id
:= Extra_Formals
(Func
);
617 -- Maybe it would be better for each implicit formal of a build-in-place
618 -- function to have a flag or a Uint attribute to identify it. ???
620 -- The return type in the function declaration may have been a limited
621 -- view, and the extra formals for the function were not generated at
622 -- that point. At the point of call the full view must be available and
623 -- the extra formals can be created.
625 if No
(Extra_Formal
) then
626 Create_Extra_Formals
(Func
);
627 Extra_Formal
:= Extra_Formals
(Func
);
631 pragma Assert
(Present
(Extra_Formal
));
632 exit when Chars
(Extra_Formal
) = Formal_Name
;
634 Next_Formal_With_Extras
(Extra_Formal
);
638 end Build_In_Place_Formal
;
640 --------------------------------
641 -- Check_Overriding_Operation --
642 --------------------------------
644 procedure Check_Overriding_Operation
(Subp
: Entity_Id
) is
645 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
646 Op_List
: constant Elist_Id
:= Primitive_Operations
(Typ
);
652 if Is_Derived_Type
(Typ
)
653 and then not Is_Private_Type
(Typ
)
654 and then In_Open_Scopes
(Scope
(Etype
(Typ
)))
655 and then Is_Base_Type
(Typ
)
657 -- Subp overrides an inherited private operation if there is an
658 -- inherited operation with a different name than Subp (see
659 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
660 -- same name as Subp.
662 Op_Elmt
:= First_Elmt
(Op_List
);
663 while Present
(Op_Elmt
) loop
664 Prim_Op
:= Node
(Op_Elmt
);
665 Par_Op
:= Alias
(Prim_Op
);
668 and then not Comes_From_Source
(Prim_Op
)
669 and then Chars
(Prim_Op
) /= Chars
(Par_Op
)
670 and then Chars
(Par_Op
) = Chars
(Subp
)
671 and then Is_Hidden
(Par_Op
)
672 and then Type_Conformant
(Prim_Op
, Subp
)
674 Set_DT_Position
(Subp
, DT_Position
(Prim_Op
));
680 end Check_Overriding_Operation
;
682 -------------------------------
683 -- Detect_Infinite_Recursion --
684 -------------------------------
686 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
) is
687 Loc
: constant Source_Ptr
:= Sloc
(N
);
689 Var_List
: constant Elist_Id
:= New_Elmt_List
;
690 -- List of globals referenced by body of procedure
692 Call_List
: constant Elist_Id
:= New_Elmt_List
;
693 -- List of recursive calls in body of procedure
695 Shad_List
: constant Elist_Id
:= New_Elmt_List
;
696 -- List of entity id's for entities created to capture the value of
697 -- referenced globals on entry to the procedure.
699 Scop
: constant Uint
:= Scope_Depth
(Spec
);
700 -- This is used to record the scope depth of the current procedure, so
701 -- that we can identify global references.
703 Max_Vars
: constant := 4;
704 -- Do not test more than four global variables
706 Count_Vars
: Natural := 0;
707 -- Count variables found so far
719 function Process
(Nod
: Node_Id
) return Traverse_Result
;
720 -- Function to traverse the subprogram body (using Traverse_Func)
726 function Process
(Nod
: Node_Id
) return Traverse_Result
is
730 if Nkind
(Nod
) = N_Procedure_Call_Statement
then
732 -- Case of one of the detected recursive calls
734 if Is_Entity_Name
(Name
(Nod
))
735 and then Has_Recursive_Call
(Entity
(Name
(Nod
)))
736 and then Entity
(Name
(Nod
)) = Spec
738 Append_Elmt
(Nod
, Call_List
);
741 -- Any other procedure call may have side effects
747 -- A call to a pure function can always be ignored
749 elsif Nkind
(Nod
) = N_Function_Call
750 and then Is_Entity_Name
(Name
(Nod
))
751 and then Is_Pure
(Entity
(Name
(Nod
)))
755 -- Case of an identifier reference
757 elsif Nkind
(Nod
) = N_Identifier
then
760 -- If no entity, then ignore the reference
762 -- Not clear why this can happen. To investigate, remove this
763 -- test and look at the crash that occurs here in 3401-004 ???
768 -- Ignore entities with no Scope, again not clear how this
769 -- can happen, to investigate, look at 4108-008 ???
771 elsif No
(Scope
(Ent
)) then
774 -- Ignore the reference if not to a more global object
776 elsif Scope_Depth
(Scope
(Ent
)) >= Scop
then
779 -- References to types, exceptions and constants are always OK
782 or else Ekind
(Ent
) = E_Exception
783 or else Ekind
(Ent
) = E_Constant
787 -- If other than a non-volatile scalar variable, we have some
788 -- kind of global reference (e.g. to a function) that we cannot
789 -- deal with so we forget the attempt.
791 elsif Ekind
(Ent
) /= E_Variable
792 or else not Is_Scalar_Type
(Etype
(Ent
))
793 or else Treat_As_Volatile
(Ent
)
797 -- Otherwise we have a reference to a global scalar
800 -- Loop through global entities already detected
802 Elm
:= First_Elmt
(Var_List
);
804 -- If not detected before, record this new global reference
807 Count_Vars
:= Count_Vars
+ 1;
809 if Count_Vars
<= Max_Vars
then
810 Append_Elmt
(Entity
(Nod
), Var_List
);
817 -- If recorded before, ignore
819 elsif Node
(Elm
) = Entity
(Nod
) then
822 -- Otherwise keep looking
832 -- For all other node kinds, recursively visit syntactic children
839 function Traverse_Body
is new Traverse_Func
(Process
);
841 -- Start of processing for Detect_Infinite_Recursion
844 -- Do not attempt detection in No_Implicit_Conditional mode, since we
845 -- won't be able to generate the code to handle the recursion in any
848 if Restriction_Active
(No_Implicit_Conditionals
) then
852 -- Otherwise do traversal and quit if we get abandon signal
854 if Traverse_Body
(N
) = Abandon
then
857 -- We must have a call, since Has_Recursive_Call was set. If not just
858 -- ignore (this is only an error check, so if we have a funny situation,
859 -- due to bugs or errors, we do not want to bomb).
861 elsif Is_Empty_Elmt_List
(Call_List
) then
865 -- Here is the case where we detect recursion at compile time
867 -- Push our current scope for analyzing the declarations and code that
868 -- we will insert for the checking.
872 -- This loop builds temporary variables for each of the referenced
873 -- globals, so that at the end of the loop the list Shad_List contains
874 -- these temporaries in one-to-one correspondence with the elements in
878 Elm
:= First_Elmt
(Var_List
);
879 while Present
(Elm
) loop
881 Ent
:= Make_Temporary
(Loc
, 'S');
882 Append_Elmt
(Ent
, Shad_List
);
884 -- Insert a declaration for this temporary at the start of the
885 -- declarations for the procedure. The temporaries are declared as
886 -- constant objects initialized to the current values of the
887 -- corresponding temporaries.
890 Make_Object_Declaration
(Loc
,
891 Defining_Identifier
=> Ent
,
892 Object_Definition
=> New_Occurrence_Of
(Etype
(Var
), Loc
),
893 Constant_Present
=> True,
894 Expression
=> New_Occurrence_Of
(Var
, Loc
));
897 Prepend
(Decl
, Declarations
(N
));
899 Insert_After
(Last
, Decl
);
907 -- Loop through calls
909 Call
:= First_Elmt
(Call_List
);
910 while Present
(Call
) loop
912 -- Build a predicate expression of the form
915 -- and then global1 = temp1
916 -- and then global2 = temp2
919 -- This predicate determines if any of the global values
920 -- referenced by the procedure have changed since the
921 -- current call, if not an infinite recursion is assured.
923 Test
:= New_Occurrence_Of
(Standard_True
, Loc
);
925 Elm1
:= First_Elmt
(Var_List
);
926 Elm2
:= First_Elmt
(Shad_List
);
927 while Present
(Elm1
) loop
933 Left_Opnd
=> New_Occurrence_Of
(Node
(Elm1
), Loc
),
934 Right_Opnd
=> New_Occurrence_Of
(Node
(Elm2
), Loc
)));
940 -- Now we replace the call with the sequence
942 -- if no-changes (see above) then
943 -- raise Storage_Error;
948 Rewrite
(Node
(Call
),
949 Make_If_Statement
(Loc
,
951 Then_Statements
=> New_List
(
952 Make_Raise_Storage_Error
(Loc
,
953 Reason
=> SE_Infinite_Recursion
)),
955 Else_Statements
=> New_List
(
956 Relocate_Node
(Node
(Call
)))));
958 Analyze
(Node
(Call
));
963 -- Remove temporary scope stack entry used for analysis
966 end Detect_Infinite_Recursion
;
972 procedure Expand_Actuals
(N
: in out Node_Id
; Subp
: Entity_Id
) is
973 Loc
: constant Source_Ptr
:= Sloc
(N
);
978 E_Actual
: Entity_Id
;
979 E_Formal
: Entity_Id
;
981 procedure Add_Call_By_Copy_Code
;
982 -- For cases where the parameter must be passed by copy, this routine
983 -- generates a temporary variable into which the actual is copied and
984 -- then passes this as the parameter. For an OUT or IN OUT parameter,
985 -- an assignment is also generated to copy the result back. The call
986 -- also takes care of any constraint checks required for the type
987 -- conversion case (on both the way in and the way out).
989 procedure Add_Simple_Call_By_Copy_Code
;
990 -- This is similar to the above, but is used in cases where we know
991 -- that all that is needed is to simply create a temporary and copy
992 -- the value in and out of the temporary.
994 procedure Check_Fortran_Logical
;
995 -- A value of type Logical that is passed through a formal parameter
996 -- must be normalized because .TRUE. usually does not have the same
997 -- representation as True. We assume that .FALSE. = False = 0.
998 -- What about functions that return a logical type ???
1000 function Is_Legal_Copy
return Boolean;
1001 -- Check that an actual can be copied before generating the temporary
1002 -- to be used in the call. If the actual is of a by_reference type then
1003 -- the program is illegal (this can only happen in the presence of
1004 -- rep. clauses that force an incorrect alignment). If the formal is
1005 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
1006 -- the effect that this might lead to unaligned arguments.
1008 function Make_Var
(Actual
: Node_Id
) return Entity_Id
;
1009 -- Returns an entity that refers to the given actual parameter, Actual
1010 -- (not including any type conversion). If Actual is an entity name,
1011 -- then this entity is returned unchanged, otherwise a renaming is
1012 -- created to provide an entity for the actual.
1014 procedure Reset_Packed_Prefix
;
1015 -- The expansion of a packed array component reference is delayed in
1016 -- the context of a call. Now we need to complete the expansion, so we
1017 -- unmark the analyzed bits in all prefixes.
1019 ---------------------------
1020 -- Add_Call_By_Copy_Code --
1021 ---------------------------
1023 procedure Add_Call_By_Copy_Code
is
1029 F_Typ
: constant Entity_Id
:= Etype
(Formal
);
1034 if not Is_Legal_Copy
then
1038 Temp
:= Make_Temporary
(Loc
, 'T', Actual
);
1040 -- Use formal type for temp, unless formal type is an unconstrained
1041 -- array, in which case we don't have to worry about bounds checks,
1042 -- and we use the actual type, since that has appropriate bounds.
1044 if Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
1045 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1047 Indic
:= New_Occurrence_Of
(Etype
(Formal
), Loc
);
1050 if Nkind
(Actual
) = N_Type_Conversion
then
1051 V_Typ
:= Etype
(Expression
(Actual
));
1053 -- If the formal is an (in-)out parameter, capture the name
1054 -- of the variable in order to build the post-call assignment.
1056 Var
:= Make_Var
(Expression
(Actual
));
1058 Crep
:= not Same_Representation
1059 (F_Typ
, Etype
(Expression
(Actual
)));
1062 V_Typ
:= Etype
(Actual
);
1063 Var
:= Make_Var
(Actual
);
1067 -- Setup initialization for case of in out parameter, or an out
1068 -- parameter where the formal is an unconstrained array (in the
1069 -- latter case, we have to pass in an object with bounds).
1071 -- If this is an out parameter, the initial copy is wasteful, so as
1072 -- an optimization for the one-dimensional case we extract the
1073 -- bounds of the actual and build an uninitialized temporary of the
1076 if Ekind
(Formal
) = E_In_Out_Parameter
1077 or else (Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
))
1079 if Nkind
(Actual
) = N_Type_Conversion
then
1080 if Conversion_OK
(Actual
) then
1081 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1083 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1086 elsif Ekind
(Formal
) = E_Out_Parameter
1087 and then Is_Array_Type
(F_Typ
)
1088 and then Number_Dimensions
(F_Typ
) = 1
1089 and then not Has_Non_Null_Base_Init_Proc
(F_Typ
)
1091 -- Actual is a one-dimensional array or slice, and the type
1092 -- requires no initialization. Create a temporary of the
1093 -- right size, but do not copy actual into it (optimization).
1097 Make_Subtype_Indication
(Loc
,
1098 Subtype_Mark
=> New_Occurrence_Of
(F_Typ
, Loc
),
1100 Make_Index_Or_Discriminant_Constraint
(Loc
,
1101 Constraints
=> New_List
(
1104 Make_Attribute_Reference
(Loc
,
1105 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
1106 Attribute_Name
=> Name_First
),
1108 Make_Attribute_Reference
(Loc
,
1109 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
1110 Attribute_Name
=> Name_Last
)))));
1113 Init
:= New_Occurrence_Of
(Var
, Loc
);
1116 -- An initialization is created for packed conversions as
1117 -- actuals for out parameters to enable Make_Object_Declaration
1118 -- to determine the proper subtype for N_Node. Note that this
1119 -- is wasteful because the extra copying on the call side is
1120 -- not required for such out parameters. ???
1122 elsif Ekind
(Formal
) = E_Out_Parameter
1123 and then Nkind
(Actual
) = N_Type_Conversion
1124 and then (Is_Bit_Packed_Array
(F_Typ
)
1126 Is_Bit_Packed_Array
(Etype
(Expression
(Actual
))))
1128 if Conversion_OK
(Actual
) then
1129 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1131 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1134 elsif Ekind
(Formal
) = E_In_Parameter
then
1136 -- Handle the case in which the actual is a type conversion
1138 if Nkind
(Actual
) = N_Type_Conversion
then
1139 if Conversion_OK
(Actual
) then
1140 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1142 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1145 Init
:= New_Occurrence_Of
(Var
, Loc
);
1153 Make_Object_Declaration
(Loc
,
1154 Defining_Identifier
=> Temp
,
1155 Object_Definition
=> Indic
,
1156 Expression
=> Init
);
1157 Set_Assignment_OK
(N_Node
);
1158 Insert_Action
(N
, N_Node
);
1160 -- Now, normally the deal here is that we use the defining
1161 -- identifier created by that object declaration. There is
1162 -- one exception to this. In the change of representation case
1163 -- the above declaration will end up looking like:
1165 -- temp : type := identifier;
1167 -- And in this case we might as well use the identifier directly
1168 -- and eliminate the temporary. Note that the analysis of the
1169 -- declaration was not a waste of time in that case, since it is
1170 -- what generated the necessary change of representation code. If
1171 -- the change of representation introduced additional code, as in
1172 -- a fixed-integer conversion, the expression is not an identifier
1173 -- and must be kept.
1176 and then Present
(Expression
(N_Node
))
1177 and then Is_Entity_Name
(Expression
(N_Node
))
1179 Temp
:= Entity
(Expression
(N_Node
));
1180 Rewrite
(N_Node
, Make_Null_Statement
(Loc
));
1183 -- For IN parameter, all we do is to replace the actual
1185 if Ekind
(Formal
) = E_In_Parameter
then
1186 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
1189 -- Processing for OUT or IN OUT parameter
1192 -- Kill current value indications for the temporary variable we
1193 -- created, since we just passed it as an OUT parameter.
1195 Kill_Current_Values
(Temp
);
1196 Set_Is_Known_Valid
(Temp
, False);
1198 -- If type conversion, use reverse conversion on exit
1200 if Nkind
(Actual
) = N_Type_Conversion
then
1201 if Conversion_OK
(Actual
) then
1202 Expr
:= OK_Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
1204 Expr
:= Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
1207 Expr
:= New_Occurrence_Of
(Temp
, Loc
);
1210 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
1213 -- If the actual is a conversion of a packed reference, it may
1214 -- already have been expanded by Remove_Side_Effects, and the
1215 -- resulting variable is a temporary which does not designate
1216 -- the proper out-parameter, which may not be addressable. In
1217 -- that case, generate an assignment to the original expression
1218 -- (before expansion of the packed reference) so that the proper
1219 -- expansion of assignment to a packed component can take place.
1226 if Is_Renaming_Of_Object
(Var
)
1227 and then Nkind
(Renamed_Object
(Var
)) = N_Selected_Component
1228 and then Is_Entity_Name
(Prefix
(Renamed_Object
(Var
)))
1229 and then Nkind
(Original_Node
(Prefix
(Renamed_Object
(Var
))))
1230 = N_Indexed_Component
1232 Has_Non_Standard_Rep
(Etype
(Prefix
(Renamed_Object
(Var
))))
1234 Obj
:= Renamed_Object
(Var
);
1236 Make_Selected_Component
(Loc
,
1238 New_Copy_Tree
(Original_Node
(Prefix
(Obj
))),
1239 Selector_Name
=> New_Copy
(Selector_Name
(Obj
)));
1240 Reset_Analyzed_Flags
(Lhs
);
1243 Lhs
:= New_Occurrence_Of
(Var
, Loc
);
1246 Set_Assignment_OK
(Lhs
);
1248 if Is_Access_Type
(E_Formal
)
1249 and then Is_Entity_Name
(Lhs
)
1251 Present
(Effective_Extra_Accessibility
(Entity
(Lhs
)))
1253 -- Copyback target is an Ada 2012 stand-alone object of an
1254 -- anonymous access type.
1256 pragma Assert
(Ada_Version
>= Ada_2012
);
1258 if Type_Access_Level
(E_Formal
) >
1259 Object_Access_Level
(Lhs
)
1261 Append_To
(Post_Call
,
1262 Make_Raise_Program_Error
(Loc
,
1263 Reason
=> PE_Accessibility_Check_Failed
));
1266 Append_To
(Post_Call
,
1267 Make_Assignment_Statement
(Loc
,
1269 Expression
=> Expr
));
1271 -- We would like to somehow suppress generation of the
1272 -- extra_accessibility assignment generated by the expansion
1273 -- of the above assignment statement. It's not a correctness
1274 -- issue because the following assignment renders it dead,
1275 -- but generating back-to-back assignments to the same
1276 -- target is undesirable. ???
1278 Append_To
(Post_Call
,
1279 Make_Assignment_Statement
(Loc
,
1280 Name
=> New_Occurrence_Of
(
1281 Effective_Extra_Accessibility
(Entity
(Lhs
)), Loc
),
1282 Expression
=> Make_Integer_Literal
(Loc
,
1283 Type_Access_Level
(E_Formal
))));
1286 Append_To
(Post_Call
,
1287 Make_Assignment_Statement
(Loc
,
1289 Expression
=> Expr
));
1293 end Add_Call_By_Copy_Code
;
1295 ----------------------------------
1296 -- Add_Simple_Call_By_Copy_Code --
1297 ----------------------------------
1299 procedure Add_Simple_Call_By_Copy_Code
is
1307 F_Typ
: constant Entity_Id
:= Etype
(Formal
);
1310 if not Is_Legal_Copy
then
1314 -- Use formal type for temp, unless formal type is an unconstrained
1315 -- array, in which case we don't have to worry about bounds checks,
1316 -- and we use the actual type, since that has appropriate bounds.
1318 if Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
1319 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1321 Indic
:= New_Occurrence_Of
(Etype
(Formal
), Loc
);
1324 -- Prepare to generate code
1326 Reset_Packed_Prefix
;
1328 Temp
:= Make_Temporary
(Loc
, 'T', Actual
);
1329 Incod
:= Relocate_Node
(Actual
);
1330 Outcod
:= New_Copy_Tree
(Incod
);
1332 -- Generate declaration of temporary variable, initializing it
1333 -- with the input parameter unless we have an OUT formal or
1334 -- this is an initialization call.
1336 -- If the formal is an out parameter with discriminants, the
1337 -- discriminants must be captured even if the rest of the object
1338 -- is in principle uninitialized, because the discriminants may
1339 -- be read by the called subprogram.
1341 if Ekind
(Formal
) = E_Out_Parameter
then
1344 if Has_Discriminants
(Etype
(Formal
)) then
1345 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1348 elsif Inside_Init_Proc
then
1350 -- Could use a comment here to match comment below ???
1352 if Nkind
(Actual
) /= N_Selected_Component
1354 not Has_Discriminant_Dependent_Constraint
1355 (Entity
(Selector_Name
(Actual
)))
1359 -- Otherwise, keep the component in order to generate the proper
1360 -- actual subtype, that depends on enclosing discriminants.
1368 Make_Object_Declaration
(Loc
,
1369 Defining_Identifier
=> Temp
,
1370 Object_Definition
=> Indic
,
1371 Expression
=> Incod
);
1376 -- If the call is to initialize a component of a composite type,
1377 -- and the component does not depend on discriminants, use the
1378 -- actual type of the component. This is required in case the
1379 -- component is constrained, because in general the formal of the
1380 -- initialization procedure will be unconstrained. Note that if
1381 -- the component being initialized is constrained by an enclosing
1382 -- discriminant, the presence of the initialization in the
1383 -- declaration will generate an expression for the actual subtype.
1385 Set_No_Initialization
(Decl
);
1386 Set_Object_Definition
(Decl
,
1387 New_Occurrence_Of
(Etype
(Actual
), Loc
));
1390 Insert_Action
(N
, Decl
);
1392 -- The actual is simply a reference to the temporary
1394 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
1396 -- Generate copy out if OUT or IN OUT parameter
1398 if Ekind
(Formal
) /= E_In_Parameter
then
1400 Rhs
:= New_Occurrence_Of
(Temp
, Loc
);
1402 -- Deal with conversion
1404 if Nkind
(Lhs
) = N_Type_Conversion
then
1405 Lhs
:= Expression
(Lhs
);
1406 Rhs
:= Convert_To
(Etype
(Actual
), Rhs
);
1409 Append_To
(Post_Call
,
1410 Make_Assignment_Statement
(Loc
,
1412 Expression
=> Rhs
));
1413 Set_Assignment_OK
(Name
(Last
(Post_Call
)));
1415 end Add_Simple_Call_By_Copy_Code
;
1417 ---------------------------
1418 -- Check_Fortran_Logical --
1419 ---------------------------
1421 procedure Check_Fortran_Logical
is
1422 Logical
: constant Entity_Id
:= Etype
(Formal
);
1425 -- Note: this is very incomplete, e.g. it does not handle arrays
1426 -- of logical values. This is really not the right approach at all???)
1429 if Convention
(Subp
) = Convention_Fortran
1430 and then Root_Type
(Etype
(Formal
)) = Standard_Boolean
1431 and then Ekind
(Formal
) /= E_In_Parameter
1433 Var
:= Make_Var
(Actual
);
1434 Append_To
(Post_Call
,
1435 Make_Assignment_Statement
(Loc
,
1436 Name
=> New_Occurrence_Of
(Var
, Loc
),
1438 Unchecked_Convert_To
(
1441 Left_Opnd
=> New_Occurrence_Of
(Var
, Loc
),
1443 Unchecked_Convert_To
(
1445 New_Occurrence_Of
(Standard_False
, Loc
))))));
1447 end Check_Fortran_Logical
;
1453 function Is_Legal_Copy
return Boolean is
1455 -- An attempt to copy a value of such a type can only occur if
1456 -- representation clauses give the actual a misaligned address.
1458 if Is_By_Reference_Type
(Etype
(Formal
)) then
1460 -- If the front-end does not perform full type layout, the actual
1461 -- may in fact be properly aligned but there is not enough front-
1462 -- end information to determine this. In that case gigi will emit
1463 -- an error if a copy is not legal, or generate the proper code.
1464 -- For other backends we report the error now.
1466 -- Seems wrong to be issuing an error in the expander, since it
1467 -- will be missed in -gnatc mode ???
1469 if Frontend_Layout_On_Target
then
1471 ("misaligned actual cannot be passed by reference", Actual
);
1476 -- For users of Starlet, we assume that the specification of by-
1477 -- reference mechanism is mandatory. This may lead to unaligned
1478 -- objects but at least for DEC legacy code it is known to work.
1479 -- The warning will alert users of this code that a problem may
1482 elsif Mechanism
(Formal
) = By_Reference
1483 and then Is_Valued_Procedure
(Scope
(Formal
))
1486 ("by_reference actual may be misaligned??", Actual
);
1498 function Make_Var
(Actual
: Node_Id
) return Entity_Id
is
1502 if Is_Entity_Name
(Actual
) then
1503 return Entity
(Actual
);
1506 Var
:= Make_Temporary
(Loc
, 'T', Actual
);
1509 Make_Object_Renaming_Declaration
(Loc
,
1510 Defining_Identifier
=> Var
,
1512 New_Occurrence_Of
(Etype
(Actual
), Loc
),
1513 Name
=> Relocate_Node
(Actual
));
1515 Insert_Action
(N
, N_Node
);
1520 -------------------------
1521 -- Reset_Packed_Prefix --
1522 -------------------------
1524 procedure Reset_Packed_Prefix
is
1525 Pfx
: Node_Id
:= Actual
;
1528 Set_Analyzed
(Pfx
, False);
1530 not Nkind_In
(Pfx
, N_Selected_Component
, N_Indexed_Component
);
1531 Pfx
:= Prefix
(Pfx
);
1533 end Reset_Packed_Prefix
;
1535 -- Start of processing for Expand_Actuals
1538 Post_Call
:= New_List
;
1540 Formal
:= First_Formal
(Subp
);
1541 Actual
:= First_Actual
(N
);
1542 while Present
(Formal
) loop
1543 E_Formal
:= Etype
(Formal
);
1544 E_Actual
:= Etype
(Actual
);
1546 if Is_Scalar_Type
(E_Formal
)
1547 or else Nkind
(Actual
) = N_Slice
1549 Check_Fortran_Logical
;
1553 elsif Ekind
(Formal
) /= E_Out_Parameter
then
1555 -- The unusual case of the current instance of a protected type
1556 -- requires special handling. This can only occur in the context
1557 -- of a call within the body of a protected operation.
1559 if Is_Entity_Name
(Actual
)
1560 and then Ekind
(Entity
(Actual
)) = E_Protected_Type
1561 and then In_Open_Scopes
(Entity
(Actual
))
1563 if Scope
(Subp
) /= Entity
(Actual
) then
1565 ("operation outside protected type may not "
1566 & "call back its protected operations??", Actual
);
1570 Expand_Protected_Object_Reference
(N
, Entity
(Actual
)));
1573 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1574 -- build-in-place function, then a temporary return object needs
1575 -- to be created and access to it must be passed to the function.
1576 -- Currently we limit such functions to those with inherently
1577 -- limited result subtypes, but eventually we plan to expand the
1578 -- functions that are treated as build-in-place to include other
1579 -- composite result types.
1581 if Is_Build_In_Place_Function_Call
(Actual
) then
1582 Make_Build_In_Place_Call_In_Anonymous_Context
(Actual
);
1585 Apply_Constraint_Check
(Actual
, E_Formal
);
1587 -- Out parameter case. No constraint checks on access type
1590 elsif Is_Access_Type
(E_Formal
) then
1595 elsif Has_Discriminants
(Base_Type
(E_Formal
))
1596 or else Has_Non_Null_Base_Init_Proc
(E_Formal
)
1598 Apply_Constraint_Check
(Actual
, E_Formal
);
1603 Apply_Constraint_Check
(Actual
, Base_Type
(E_Formal
));
1606 -- Processing for IN-OUT and OUT parameters
1608 if Ekind
(Formal
) /= E_In_Parameter
then
1610 -- For type conversions of arrays, apply length/range checks
1612 if Is_Array_Type
(E_Formal
)
1613 and then Nkind
(Actual
) = N_Type_Conversion
1615 if Is_Constrained
(E_Formal
) then
1616 Apply_Length_Check
(Expression
(Actual
), E_Formal
);
1618 Apply_Range_Check
(Expression
(Actual
), E_Formal
);
1622 -- If argument is a type conversion for a type that is passed
1623 -- by copy, then we must pass the parameter by copy.
1625 if Nkind
(Actual
) = N_Type_Conversion
1627 (Is_Numeric_Type
(E_Formal
)
1628 or else Is_Access_Type
(E_Formal
)
1629 or else Is_Enumeration_Type
(E_Formal
)
1630 or else Is_Bit_Packed_Array
(Etype
(Formal
))
1631 or else Is_Bit_Packed_Array
(Etype
(Expression
(Actual
)))
1633 -- Also pass by copy if change of representation
1635 or else not Same_Representation
1637 Etype
(Expression
(Actual
))))
1639 Add_Call_By_Copy_Code
;
1641 -- References to components of bit packed arrays are expanded
1642 -- at this point, rather than at the point of analysis of the
1643 -- actuals, to handle the expansion of the assignment to
1644 -- [in] out parameters.
1646 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
1647 Add_Simple_Call_By_Copy_Code
;
1649 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1650 -- because the back-end cannot cope with such objects. In other
1651 -- cases where alignment forces a copy, the back-end generates
1652 -- it properly. It should not be generated unconditionally in the
1653 -- front-end because it does not know precisely the alignment
1654 -- requirements of the target, and makes too conservative an
1655 -- estimate, leading to superfluous copies or spurious errors
1656 -- on by-reference parameters.
1658 elsif Nkind
(Actual
) = N_Selected_Component
1660 Component_May_Be_Bit_Aligned
(Entity
(Selector_Name
(Actual
)))
1661 and then not Represented_As_Scalar
(Etype
(Formal
))
1663 Add_Simple_Call_By_Copy_Code
;
1665 -- References to slices of bit packed arrays are expanded
1667 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
1668 Add_Call_By_Copy_Code
;
1670 -- References to possibly unaligned slices of arrays are expanded
1672 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
1673 Add_Call_By_Copy_Code
;
1675 -- Deal with access types where the actual subtype and the
1676 -- formal subtype are not the same, requiring a check.
1678 -- It is necessary to exclude tagged types because of "downward
1679 -- conversion" errors.
1681 elsif Is_Access_Type
(E_Formal
)
1682 and then not Same_Type
(E_Formal
, E_Actual
)
1683 and then not Is_Tagged_Type
(Designated_Type
(E_Formal
))
1685 Add_Call_By_Copy_Code
;
1687 -- If the actual is not a scalar and is marked for volatile
1688 -- treatment, whereas the formal is not volatile, then pass
1689 -- by copy unless it is a by-reference type.
1691 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1692 -- because this is the enforcement of a language rule that applies
1693 -- only to "real" volatile variables, not e.g. to the address
1694 -- clause overlay case.
1696 elsif Is_Entity_Name
(Actual
)
1697 and then Is_Volatile
(Entity
(Actual
))
1698 and then not Is_By_Reference_Type
(E_Actual
)
1699 and then not Is_Scalar_Type
(Etype
(Entity
(Actual
)))
1700 and then not Is_Volatile
(E_Formal
)
1702 Add_Call_By_Copy_Code
;
1704 elsif Nkind
(Actual
) = N_Indexed_Component
1705 and then Is_Entity_Name
(Prefix
(Actual
))
1706 and then Has_Volatile_Components
(Entity
(Prefix
(Actual
)))
1708 Add_Call_By_Copy_Code
;
1710 -- Add call-by-copy code for the case of scalar out parameters
1711 -- when it is not known at compile time that the subtype of the
1712 -- formal is a subrange of the subtype of the actual (or vice
1713 -- versa for in out parameters), in order to get range checks
1714 -- on such actuals. (Maybe this case should be handled earlier
1715 -- in the if statement???)
1717 elsif Is_Scalar_Type
(E_Formal
)
1719 (not In_Subrange_Of
(E_Formal
, E_Actual
)
1721 (Ekind
(Formal
) = E_In_Out_Parameter
1722 and then not In_Subrange_Of
(E_Actual
, E_Formal
)))
1724 -- Perhaps the setting back to False should be done within
1725 -- Add_Call_By_Copy_Code, since it could get set on other
1726 -- cases occurring above???
1728 if Do_Range_Check
(Actual
) then
1729 Set_Do_Range_Check
(Actual
, False);
1732 Add_Call_By_Copy_Code
;
1735 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
1736 -- by-reference parameters on exit from the call. If the actual
1737 -- is a derived type and the operation is inherited, the body
1738 -- of the operation will not contain a call to the predicate
1739 -- function, so it must be done explicitly after the call. Ditto
1740 -- if the actual is an entity of a predicated subtype.
1742 -- The rule refers to by-reference types, but a check is needed
1743 -- for by-copy types as well. That check is subsumed by the rule
1744 -- for subtype conversion on assignment, but we can generate the
1745 -- required check now.
1747 -- Note also that Subp may be either a subprogram entity for
1748 -- direct calls, or a type entity for indirect calls, which must
1749 -- be handled separately because the name does not denote an
1750 -- overloadable entity.
1753 Aund
: constant Entity_Id
:= Underlying_Type
(E_Actual
);
1763 if Has_Predicates
(Atyp
)
1764 and then Present
(Predicate_Function
(Atyp
))
1766 -- Skip predicate checks for special cases
1768 and then Predicate_Tests_On_Arguments
(Subp
)
1770 Append_To
(Post_Call
,
1771 Make_Predicate_Check
(Atyp
, Actual
));
1775 -- Processing for IN parameters
1778 -- For IN parameters is in the packed array case, we expand an
1779 -- indexed component (the circuit in Exp_Ch4 deliberately left
1780 -- indexed components appearing as actuals untouched, so that
1781 -- the special processing above for the OUT and IN OUT cases
1782 -- could be performed. We could make the test in Exp_Ch4 more
1783 -- complex and have it detect the parameter mode, but it is
1784 -- easier simply to handle all cases here.)
1786 if Nkind
(Actual
) = N_Indexed_Component
1787 and then Is_Packed
(Etype
(Prefix
(Actual
)))
1789 Reset_Packed_Prefix
;
1790 Expand_Packed_Element_Reference
(Actual
);
1792 -- If we have a reference to a bit packed array, we copy it, since
1793 -- the actual must be byte aligned.
1795 -- Is this really necessary in all cases???
1797 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
1798 Add_Simple_Call_By_Copy_Code
;
1800 -- If a non-scalar actual is possibly unaligned, we need a copy
1802 elsif Is_Possibly_Unaligned_Object
(Actual
)
1803 and then not Represented_As_Scalar
(Etype
(Formal
))
1805 Add_Simple_Call_By_Copy_Code
;
1807 -- Similarly, we have to expand slices of packed arrays here
1808 -- because the result must be byte aligned.
1810 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
1811 Add_Call_By_Copy_Code
;
1813 -- Only processing remaining is to pass by copy if this is a
1814 -- reference to a possibly unaligned slice, since the caller
1815 -- expects an appropriately aligned argument.
1817 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
1818 Add_Call_By_Copy_Code
;
1820 -- An unusual case: a current instance of an enclosing task can be
1821 -- an actual, and must be replaced by a reference to self.
1823 elsif Is_Entity_Name
(Actual
)
1824 and then Is_Task_Type
(Entity
(Actual
))
1826 if In_Open_Scopes
(Entity
(Actual
)) then
1828 (Make_Function_Call
(Loc
,
1829 Name
=> New_Occurrence_Of
(RTE
(RE_Self
), Loc
))));
1832 -- A task type cannot otherwise appear as an actual
1835 raise Program_Error
;
1840 Next_Formal
(Formal
);
1841 Next_Actual
(Actual
);
1844 -- Find right place to put post call stuff if it is present
1846 if not Is_Empty_List
(Post_Call
) then
1848 -- Cases where the call is not a member of a statement list
1850 if not Is_List_Member
(N
) then
1852 -- In Ada 2012 the call may be a function call in an expression
1853 -- (since OUT and IN OUT parameters are now allowed for such
1854 -- calls). The write-back of (in)-out parameters is handled
1855 -- by the back-end, but the constraint checks generated when
1856 -- subtypes of formal and actual don't match must be inserted
1857 -- in the form of assignments.
1859 if Ada_Version
>= Ada_2012
1860 and then Nkind
(N
) = N_Function_Call
1862 -- We used to just do handle this by climbing up parents to
1863 -- a non-statement/declaration and then simply making a call
1864 -- to Insert_Actions_After (P, Post_Call), but that doesn't
1865 -- work. If we are in the middle of an expression, e.g. the
1866 -- condition of an IF, this call would insert after the IF
1867 -- statement, which is much too late to be doing the write
1868 -- back. For example:
1870 -- if Clobber (X) then
1871 -- Put_Line (X'Img);
1876 -- Now assume Clobber changes X, if we put the write back
1877 -- after the IF, the Put_Line gets the wrong value and the
1878 -- goto causes the write back to be skipped completely.
1880 -- To deal with this, we replace the call by
1883 -- Tnnn : function-result-type renames function-call;
1884 -- Post_Call actions
1889 -- Note: this won't do in Modify_Tree_For_C mode, but we
1890 -- will deal with that later (it will require creating a
1891 -- declaration for Temp, using Insert_Declaration) ???
1894 Tnnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
1895 FRTyp
: constant Entity_Id
:= Etype
(N
);
1896 Name
: constant Node_Id
:= Relocate_Node
(N
);
1899 Prepend_To
(Post_Call
,
1900 Make_Object_Renaming_Declaration
(Loc
,
1901 Defining_Identifier
=> Tnnn
,
1902 Subtype_Mark
=> New_Occurrence_Of
(FRTyp
, Loc
),
1906 Make_Expression_With_Actions
(Loc
,
1907 Actions
=> Post_Call
,
1908 Expression
=> New_Occurrence_Of
(Tnnn
, Loc
)));
1910 -- We don't want to just blindly call Analyze_And_Resolve
1911 -- because that would cause unwanted recursion on the call.
1912 -- So for a moment set the call as analyzed to prevent that
1913 -- recursion, and get the rest analyzed properly, then reset
1914 -- the analyzed flag, so our caller can continue.
1916 Set_Analyzed
(Name
, True);
1917 Analyze_And_Resolve
(N
, FRTyp
);
1918 Set_Analyzed
(Name
, False);
1920 -- Reset calling argument to point to function call inside
1921 -- the expression with actions so the caller can continue
1922 -- to process the call.
1927 -- If not the special Ada 2012 case of a function call, then
1928 -- we must have the triggering statement of a triggering
1929 -- alternative or an entry call alternative, and we can add
1930 -- the post call stuff to the corresponding statement list.
1938 pragma Assert
(Nkind_In
(P
, N_Triggering_Alternative
,
1939 N_Entry_Call_Alternative
));
1941 if Is_Non_Empty_List
(Statements
(P
)) then
1942 Insert_List_Before_And_Analyze
1943 (First
(Statements
(P
)), Post_Call
);
1945 Set_Statements
(P
, Post_Call
);
1952 -- Otherwise, normal case where N is in a statement sequence,
1953 -- just put the post-call stuff after the call statement.
1956 Insert_Actions_After
(N
, Post_Call
);
1961 -- The call node itself is re-analyzed in Expand_Call
1969 -- This procedure handles expansion of function calls and procedure call
1970 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1971 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
1973 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
1974 -- Provide values of actuals for all formals in Extra_Formals list
1975 -- Replace "call" to enumeration literal function by literal itself
1976 -- Rewrite call to predefined operator as operator
1977 -- Replace actuals to in-out parameters that are numeric conversions,
1978 -- with explicit assignment to temporaries before and after the call.
1980 -- Note that the list of actuals has been filled with default expressions
1981 -- during semantic analysis of the call. Only the extra actuals required
1982 -- for the 'Constrained attribute and for accessibility checks are added
1985 procedure Expand_Call
(N
: Node_Id
) is
1986 Loc
: constant Source_Ptr
:= Sloc
(N
);
1987 Call_Node
: Node_Id
:= N
;
1988 Extra_Actuals
: List_Id
:= No_List
;
1989 Prev
: Node_Id
:= Empty
;
1991 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
);
1992 -- Adds one entry to the end of the actual parameter list. Used for
1993 -- default parameters and for extra actuals (for Extra_Formals). The
1994 -- argument is an N_Parameter_Association node.
1996 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
);
1997 -- Adds an extra actual to the list of extra actuals. Expr is the
1998 -- expression for the value of the actual, EF is the entity for the
2001 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
;
2002 -- Within an instance, a type derived from an untagged formal derived
2003 -- type inherits from the original parent, not from the actual. The
2004 -- current derivation mechanism has the derived type inherit from the
2005 -- actual, which is only correct outside of the instance. If the
2006 -- subprogram is inherited, we test for this particular case through a
2007 -- convoluted tree traversal before setting the proper subprogram to be
2010 function In_Unfrozen_Instance
(E
: Entity_Id
) return Boolean;
2011 -- Return true if E comes from an instance that is not yet frozen
2013 function Is_Direct_Deep_Call
(Subp
: Entity_Id
) return Boolean;
2014 -- Determine if Subp denotes a non-dispatching call to a Deep routine
2016 function New_Value
(From
: Node_Id
) return Node_Id
;
2017 -- From is the original Expression. New_Value is equivalent to a call
2018 -- to Duplicate_Subexpr with an explicit dereference when From is an
2019 -- access parameter.
2021 --------------------------
2022 -- Add_Actual_Parameter --
2023 --------------------------
2025 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
) is
2026 Actual_Expr
: constant Node_Id
:=
2027 Explicit_Actual_Parameter
(Insert_Param
);
2030 -- Case of insertion is first named actual
2032 if No
(Prev
) or else
2033 Nkind
(Parent
(Prev
)) /= N_Parameter_Association
2035 Set_Next_Named_Actual
2036 (Insert_Param
, First_Named_Actual
(Call_Node
));
2037 Set_First_Named_Actual
(Call_Node
, Actual_Expr
);
2040 if No
(Parameter_Associations
(Call_Node
)) then
2041 Set_Parameter_Associations
(Call_Node
, New_List
);
2044 Append
(Insert_Param
, Parameter_Associations
(Call_Node
));
2047 Insert_After
(Prev
, Insert_Param
);
2050 -- Case of insertion is not first named actual
2053 Set_Next_Named_Actual
2054 (Insert_Param
, Next_Named_Actual
(Parent
(Prev
)));
2055 Set_Next_Named_Actual
(Parent
(Prev
), Actual_Expr
);
2056 Append
(Insert_Param
, Parameter_Associations
(Call_Node
));
2059 Prev
:= Actual_Expr
;
2060 end Add_Actual_Parameter
;
2062 ----------------------
2063 -- Add_Extra_Actual --
2064 ----------------------
2066 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
) is
2067 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
2070 if Extra_Actuals
= No_List
then
2071 Extra_Actuals
:= New_List
;
2072 Set_Parent
(Extra_Actuals
, Call_Node
);
2075 Append_To
(Extra_Actuals
,
2076 Make_Parameter_Association
(Loc
,
2077 Selector_Name
=> New_Occurrence_Of
(EF
, Loc
),
2078 Explicit_Actual_Parameter
=> Expr
));
2080 Analyze_And_Resolve
(Expr
, Etype
(EF
));
2082 if Nkind
(Call_Node
) = N_Function_Call
then
2083 Set_Is_Accessibility_Actual
(Parent
(Expr
));
2085 end Add_Extra_Actual
;
2087 ---------------------------
2088 -- Inherited_From_Formal --
2089 ---------------------------
2091 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
is
2093 Gen_Par
: Entity_Id
;
2094 Gen_Prim
: Elist_Id
;
2099 -- If the operation is inherited, it is attached to the corresponding
2100 -- type derivation. If the parent in the derivation is a generic
2101 -- actual, it is a subtype of the actual, and we have to recover the
2102 -- original derived type declaration to find the proper parent.
2104 if Nkind
(Parent
(S
)) /= N_Full_Type_Declaration
2105 or else not Is_Derived_Type
(Defining_Identifier
(Parent
(S
)))
2106 or else Nkind
(Type_Definition
(Original_Node
(Parent
(S
)))) /=
2107 N_Derived_Type_Definition
2108 or else not In_Instance
2115 (Type_Definition
(Original_Node
(Parent
(S
))));
2117 if Nkind
(Indic
) = N_Subtype_Indication
then
2118 Par
:= Entity
(Subtype_Mark
(Indic
));
2120 Par
:= Entity
(Indic
);
2124 if not Is_Generic_Actual_Type
(Par
)
2125 or else Is_Tagged_Type
(Par
)
2126 or else Nkind
(Parent
(Par
)) /= N_Subtype_Declaration
2127 or else not In_Open_Scopes
(Scope
(Par
))
2131 Gen_Par
:= Generic_Parent_Type
(Parent
(Par
));
2134 -- If the actual has no generic parent type, the formal is not
2135 -- a formal derived type, so nothing to inherit.
2137 if No
(Gen_Par
) then
2141 -- If the generic parent type is still the generic type, this is a
2142 -- private formal, not a derived formal, and there are no operations
2143 -- inherited from the formal.
2145 if Nkind
(Parent
(Gen_Par
)) = N_Formal_Type_Declaration
then
2149 Gen_Prim
:= Collect_Primitive_Operations
(Gen_Par
);
2151 Elmt
:= First_Elmt
(Gen_Prim
);
2152 while Present
(Elmt
) loop
2153 if Chars
(Node
(Elmt
)) = Chars
(S
) then
2159 F1
:= First_Formal
(S
);
2160 F2
:= First_Formal
(Node
(Elmt
));
2162 and then Present
(F2
)
2164 if Etype
(F1
) = Etype
(F2
)
2165 or else Etype
(F2
) = Gen_Par
2171 exit; -- not the right subprogram
2183 raise Program_Error
;
2184 end Inherited_From_Formal
;
2186 --------------------------
2187 -- In_Unfrozen_Instance --
2188 --------------------------
2190 function In_Unfrozen_Instance
(E
: Entity_Id
) return Boolean is
2195 while Present
(S
) and then S
/= Standard_Standard
loop
2196 if Is_Generic_Instance
(S
)
2197 and then Present
(Freeze_Node
(S
))
2198 and then not Analyzed
(Freeze_Node
(S
))
2207 end In_Unfrozen_Instance
;
2209 -------------------------
2210 -- Is_Direct_Deep_Call --
2211 -------------------------
2213 function Is_Direct_Deep_Call
(Subp
: Entity_Id
) return Boolean is
2215 if Is_TSS
(Subp
, TSS_Deep_Adjust
)
2216 or else Is_TSS
(Subp
, TSS_Deep_Finalize
)
2217 or else Is_TSS
(Subp
, TSS_Deep_Initialize
)
2224 Actual
:= First
(Parameter_Associations
(N
));
2225 Formal
:= First_Formal
(Subp
);
2226 while Present
(Actual
)
2227 and then Present
(Formal
)
2229 if Nkind
(Actual
) = N_Identifier
2230 and then Is_Controlling_Actual
(Actual
)
2231 and then Etype
(Actual
) = Etype
(Formal
)
2237 Next_Formal
(Formal
);
2243 end Is_Direct_Deep_Call
;
2249 function New_Value
(From
: Node_Id
) return Node_Id
is
2250 Res
: constant Node_Id
:= Duplicate_Subexpr
(From
);
2252 if Is_Access_Type
(Etype
(From
)) then
2253 return Make_Explicit_Dereference
(Sloc
(From
), Prefix
=> Res
);
2261 Curr_S
: constant Entity_Id
:= Current_Scope
;
2262 Remote
: constant Boolean := Is_Remote_Call
(Call_Node
);
2265 Orig_Subp
: Entity_Id
:= Empty
;
2266 Param_Count
: Natural := 0;
2267 Parent_Formal
: Entity_Id
;
2268 Parent_Subp
: Entity_Id
;
2272 Prev_Orig
: Node_Id
;
2273 -- Original node for an actual, which may have been rewritten. If the
2274 -- actual is a function call that has been transformed from a selected
2275 -- component, the original node is unanalyzed. Otherwise, it carries
2276 -- semantic information used to generate additional actuals.
2278 CW_Interface_Formals_Present
: Boolean := False;
2280 -- Start of processing for Expand_Call
2283 -- Expand the procedure call if the first actual has a dimension and if
2284 -- the procedure is Put (Ada 2012).
2286 if Ada_Version
>= Ada_2012
2287 and then Nkind
(Call_Node
) = N_Procedure_Call_Statement
2288 and then Present
(Parameter_Associations
(Call_Node
))
2290 Expand_Put_Call_With_Symbol
(Call_Node
);
2293 -- Ignore if previous error
2295 if Nkind
(Call_Node
) in N_Has_Etype
2296 and then Etype
(Call_Node
) = Any_Type
2301 -- Call using access to subprogram with explicit dereference
2303 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
2304 Subp
:= Etype
(Name
(Call_Node
));
2305 Parent_Subp
:= Empty
;
2307 -- Case of call to simple entry, where the Name is a selected component
2308 -- whose prefix is the task, and whose selector name is the entry name
2310 elsif Nkind
(Name
(Call_Node
)) = N_Selected_Component
then
2311 Subp
:= Entity
(Selector_Name
(Name
(Call_Node
)));
2312 Parent_Subp
:= Empty
;
2314 -- Case of call to member of entry family, where Name is an indexed
2315 -- component, with the prefix being a selected component giving the
2316 -- task and entry family name, and the index being the entry index.
2318 elsif Nkind
(Name
(Call_Node
)) = N_Indexed_Component
then
2319 Subp
:= Entity
(Selector_Name
(Prefix
(Name
(Call_Node
))));
2320 Parent_Subp
:= Empty
;
2325 Subp
:= Entity
(Name
(Call_Node
));
2326 Parent_Subp
:= Alias
(Subp
);
2328 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2329 -- if we can tell that the first parameter cannot possibly be null.
2330 -- This improves efficiency by avoiding a run-time test.
2332 -- We do not do this if Raise_Exception_Always does not exist, which
2333 -- can happen in configurable run time profiles which provide only a
2336 if Is_RTE
(Subp
, RE_Raise_Exception
)
2337 and then RTE_Available
(RE_Raise_Exception_Always
)
2340 FA
: constant Node_Id
:=
2341 Original_Node
(First_Actual
(Call_Node
));
2344 -- The case we catch is where the first argument is obtained
2345 -- using the Identity attribute (which must always be
2348 if Nkind
(FA
) = N_Attribute_Reference
2349 and then Attribute_Name
(FA
) = Name_Identity
2351 Subp
:= RTE
(RE_Raise_Exception_Always
);
2352 Set_Name
(Call_Node
, New_Occurrence_Of
(Subp
, Loc
));
2357 if Ekind
(Subp
) = E_Entry
then
2358 Parent_Subp
:= Empty
;
2362 -- Detect the following code in System.Finalization_Masters only on
2363 -- .NET/JVM targets:
2365 -- procedure Finalize (Master : in out Finalization_Master) is
2369 -- Finalize (Curr_Ptr.all);
2371 -- Since .NET/JVM compilers lack address arithmetic and Deep_Finalize
2372 -- cannot be named in library or user code, the compiler has to deal
2373 -- with this by transforming the call to Finalize into Deep_Finalize.
2375 if VM_Target
/= No_VM
2376 and then Chars
(Subp
) = Name_Finalize
2377 and then Ekind
(Curr_S
) = E_Block
2378 and then Ekind
(Scope
(Curr_S
)) = E_Procedure
2379 and then Chars
(Scope
(Curr_S
)) = Name_Finalize
2380 and then Etype
(First_Formal
(Scope
(Curr_S
))) =
2381 RTE
(RE_Finalization_Master
)
2384 Deep_Fin
: constant Entity_Id
:=
2385 Find_Prim_Op
(RTE
(RE_Root_Controlled
),
2388 -- Since Root_Controlled is a tagged type, the compiler should
2389 -- always generate Deep_Finalize for it.
2391 pragma Assert
(Present
(Deep_Fin
));
2394 -- Deep_Finalize (Curr_Ptr.all);
2397 Make_Procedure_Call_Statement
(Loc
,
2399 New_Occurrence_Of
(Deep_Fin
, Loc
),
2400 Parameter_Associations
=>
2401 New_Copy_List_Tree
(Parameter_Associations
(N
))));
2408 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2409 -- alternative in an asynchronous select or as an entry call in
2410 -- a conditional or timed select. Check whether the procedure call
2411 -- is a renaming of an entry and rewrite it as an entry call.
2413 if Ada_Version
>= Ada_2005
2414 and then Nkind
(Call_Node
) = N_Procedure_Call_Statement
2416 ((Nkind
(Parent
(Call_Node
)) = N_Triggering_Alternative
2417 and then Triggering_Statement
(Parent
(Call_Node
)) = Call_Node
)
2419 (Nkind
(Parent
(Call_Node
)) = N_Entry_Call_Alternative
2420 and then Entry_Call_Statement
(Parent
(Call_Node
)) = Call_Node
))
2424 Ren_Root
: Entity_Id
:= Subp
;
2427 -- This may be a chain of renamings, find the root
2429 if Present
(Alias
(Ren_Root
)) then
2430 Ren_Root
:= Alias
(Ren_Root
);
2433 if Present
(Original_Node
(Parent
(Parent
(Ren_Root
)))) then
2434 Ren_Decl
:= Original_Node
(Parent
(Parent
(Ren_Root
)));
2436 if Nkind
(Ren_Decl
) = N_Subprogram_Renaming_Declaration
then
2438 Make_Entry_Call_Statement
(Loc
,
2440 New_Copy_Tree
(Name
(Ren_Decl
)),
2441 Parameter_Associations
=>
2443 (Parameter_Associations
(Call_Node
))));
2451 -- First step, compute extra actuals, corresponding to any Extra_Formals
2452 -- present. Note that we do not access Extra_Formals directly, instead
2453 -- we simply note the presence of the extra formals as we process the
2454 -- regular formals collecting corresponding actuals in Extra_Actuals.
2456 -- We also generate any required range checks for actuals for in formals
2457 -- as we go through the loop, since this is a convenient place to do it.
2458 -- (Though it seems that this would be better done in Expand_Actuals???)
2460 -- Special case: Thunks must not compute the extra actuals; they must
2461 -- just propagate to the target primitive their extra actuals.
2463 if Is_Thunk
(Current_Scope
)
2464 and then Thunk_Entity
(Current_Scope
) = Subp
2465 and then Present
(Extra_Formals
(Subp
))
2467 pragma Assert
(Present
(Extra_Formals
(Current_Scope
)));
2470 Target_Formal
: Entity_Id
;
2471 Thunk_Formal
: Entity_Id
;
2474 Target_Formal
:= Extra_Formals
(Subp
);
2475 Thunk_Formal
:= Extra_Formals
(Current_Scope
);
2476 while Present
(Target_Formal
) loop
2478 (New_Occurrence_Of
(Thunk_Formal
, Loc
), Thunk_Formal
);
2480 Target_Formal
:= Extra_Formal
(Target_Formal
);
2481 Thunk_Formal
:= Extra_Formal
(Thunk_Formal
);
2484 while Is_Non_Empty_List
(Extra_Actuals
) loop
2485 Add_Actual_Parameter
(Remove_Head
(Extra_Actuals
));
2488 Expand_Actuals
(Call_Node
, Subp
);
2493 Formal
:= First_Formal
(Subp
);
2494 Actual
:= First_Actual
(Call_Node
);
2496 while Present
(Formal
) loop
2498 -- Generate range check if required
2500 if Do_Range_Check
(Actual
)
2501 and then Ekind
(Formal
) = E_In_Parameter
2503 Generate_Range_Check
2504 (Actual
, Etype
(Formal
), CE_Range_Check_Failed
);
2507 -- Prepare to examine current entry
2510 Prev_Orig
:= Original_Node
(Prev
);
2512 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2513 -- to expand it in a further round.
2515 CW_Interface_Formals_Present
:=
2516 CW_Interface_Formals_Present
2518 (Ekind
(Etype
(Formal
)) = E_Class_Wide_Type
2519 and then Is_Interface
(Etype
(Etype
(Formal
))))
2521 (Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
2522 and then Is_Interface
(Directly_Designated_Type
2523 (Etype
(Etype
(Formal
)))));
2525 -- Create possible extra actual for constrained case. Usually, the
2526 -- extra actual is of the form actual'constrained, but since this
2527 -- attribute is only available for unconstrained records, TRUE is
2528 -- expanded if the type of the formal happens to be constrained (for
2529 -- instance when this procedure is inherited from an unconstrained
2530 -- record to a constrained one) or if the actual has no discriminant
2531 -- (its type is constrained). An exception to this is the case of a
2532 -- private type without discriminants. In this case we pass FALSE
2533 -- because the object has underlying discriminants with defaults.
2535 if Present
(Extra_Constrained
(Formal
)) then
2536 if Ekind
(Etype
(Prev
)) in Private_Kind
2537 and then not Has_Discriminants
(Base_Type
(Etype
(Prev
)))
2540 (New_Occurrence_Of
(Standard_False
, Loc
),
2541 Extra_Constrained
(Formal
));
2543 elsif Is_Constrained
(Etype
(Formal
))
2544 or else not Has_Discriminants
(Etype
(Prev
))
2547 (New_Occurrence_Of
(Standard_True
, Loc
),
2548 Extra_Constrained
(Formal
));
2550 -- Do not produce extra actuals for Unchecked_Union parameters.
2551 -- Jump directly to the end of the loop.
2553 elsif Is_Unchecked_Union
(Base_Type
(Etype
(Actual
))) then
2554 goto Skip_Extra_Actual_Generation
;
2557 -- If the actual is a type conversion, then the constrained
2558 -- test applies to the actual, not the target type.
2564 -- Test for unchecked conversions as well, which can occur
2565 -- as out parameter actuals on calls to stream procedures.
2568 while Nkind_In
(Act_Prev
, N_Type_Conversion
,
2569 N_Unchecked_Type_Conversion
)
2571 Act_Prev
:= Expression
(Act_Prev
);
2574 -- If the expression is a conversion of a dereference, this
2575 -- is internally generated code that manipulates addresses,
2576 -- e.g. when building interface tables. No check should
2577 -- occur in this case, and the discriminated object is not
2580 if not Comes_From_Source
(Actual
)
2581 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
2582 and then Nkind
(Act_Prev
) = N_Explicit_Dereference
2585 (New_Occurrence_Of
(Standard_False
, Loc
),
2586 Extra_Constrained
(Formal
));
2590 (Make_Attribute_Reference
(Sloc
(Prev
),
2592 Duplicate_Subexpr_No_Checks
2593 (Act_Prev
, Name_Req
=> True),
2594 Attribute_Name
=> Name_Constrained
),
2595 Extra_Constrained
(Formal
));
2601 -- Create possible extra actual for accessibility level
2603 if Present
(Extra_Accessibility
(Formal
)) then
2605 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2606 -- attribute, then the original actual may be an aliased object
2607 -- occurring as the prefix in a call using "Object.Operation"
2608 -- notation. In that case we must pass the level of the object,
2609 -- so Prev_Orig is reset to Prev and the attribute will be
2610 -- processed by the code for Access attributes further below.
2612 if Prev_Orig
/= Prev
2613 and then Nkind
(Prev
) = N_Attribute_Reference
2615 Get_Attribute_Id
(Attribute_Name
(Prev
)) = Attribute_Access
2616 and then Is_Aliased_View
(Prev_Orig
)
2621 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2622 -- accessibility levels.
2624 if Is_Thunk
(Current_Scope
) then
2626 Parm_Ent
: Entity_Id
;
2629 if Is_Controlling_Actual
(Actual
) then
2631 -- Find the corresponding actual of the thunk
2633 Parm_Ent
:= First_Entity
(Current_Scope
);
2634 for J
in 2 .. Param_Count
loop
2635 Next_Entity
(Parm_Ent
);
2638 -- Handle unchecked conversion of access types generated
2639 -- in thunks (cf. Expand_Interface_Thunk).
2641 elsif Is_Access_Type
(Etype
(Actual
))
2642 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
2644 Parm_Ent
:= Entity
(Expression
(Actual
));
2646 else pragma Assert
(Is_Entity_Name
(Actual
));
2647 Parm_Ent
:= Entity
(Actual
);
2651 (New_Occurrence_Of
(Extra_Accessibility
(Parm_Ent
), Loc
),
2652 Extra_Accessibility
(Formal
));
2655 elsif Is_Entity_Name
(Prev_Orig
) then
2657 -- When passing an access parameter, or a renaming of an access
2658 -- parameter, as the actual to another access parameter we need
2659 -- to pass along the actual's own access level parameter. This
2660 -- is done if we are within the scope of the formal access
2661 -- parameter (if this is an inlined body the extra formal is
2664 if (Is_Formal
(Entity
(Prev_Orig
))
2666 (Present
(Renamed_Object
(Entity
(Prev_Orig
)))
2668 Is_Entity_Name
(Renamed_Object
(Entity
(Prev_Orig
)))
2671 (Entity
(Renamed_Object
(Entity
(Prev_Orig
))))))
2672 and then Ekind
(Etype
(Prev_Orig
)) = E_Anonymous_Access_Type
2673 and then In_Open_Scopes
(Scope
(Entity
(Prev_Orig
)))
2676 Parm_Ent
: constant Entity_Id
:= Param_Entity
(Prev_Orig
);
2679 pragma Assert
(Present
(Parm_Ent
));
2681 if Present
(Extra_Accessibility
(Parm_Ent
)) then
2684 (Extra_Accessibility
(Parm_Ent
), Loc
),
2685 Extra_Accessibility
(Formal
));
2687 -- If the actual access parameter does not have an
2688 -- associated extra formal providing its scope level,
2689 -- then treat the actual as having library-level
2694 (Make_Integer_Literal
(Loc
,
2695 Intval
=> Scope_Depth
(Standard_Standard
)),
2696 Extra_Accessibility
(Formal
));
2700 -- The actual is a normal access value, so just pass the level
2701 -- of the actual's access type.
2705 (Dynamic_Accessibility_Level
(Prev_Orig
),
2706 Extra_Accessibility
(Formal
));
2709 -- If the actual is an access discriminant, then pass the level
2710 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2712 elsif Nkind
(Prev_Orig
) = N_Selected_Component
2713 and then Ekind
(Entity
(Selector_Name
(Prev_Orig
))) =
2715 and then Ekind
(Etype
(Entity
(Selector_Name
(Prev_Orig
)))) =
2716 E_Anonymous_Access_Type
2719 (Make_Integer_Literal
(Loc
,
2720 Intval
=> Object_Access_Level
(Prefix
(Prev_Orig
))),
2721 Extra_Accessibility
(Formal
));
2726 case Nkind
(Prev_Orig
) is
2728 when N_Attribute_Reference
=>
2729 case Get_Attribute_Id
(Attribute_Name
(Prev_Orig
)) is
2731 -- For X'Access, pass on the level of the prefix X
2733 when Attribute_Access
=>
2735 -- If this is an Access attribute applied to the
2736 -- the current instance object passed to a type
2737 -- initialization procedure, then use the level
2738 -- of the type itself. This is not really correct,
2739 -- as there should be an extra level parameter
2740 -- passed in with _init formals (only in the case
2741 -- where the type is immutably limited), but we
2742 -- don't have an easy way currently to create such
2743 -- an extra formal (init procs aren't ever frozen).
2744 -- For now we just use the level of the type,
2745 -- which may be too shallow, but that works better
2746 -- than passing Object_Access_Level of the type,
2747 -- which can be one level too deep in some cases.
2750 if Is_Entity_Name
(Prefix
(Prev_Orig
))
2751 and then Is_Type
(Entity
(Prefix
(Prev_Orig
)))
2754 (Make_Integer_Literal
(Loc
,
2757 (Entity
(Prefix
(Prev_Orig
)))),
2758 Extra_Accessibility
(Formal
));
2762 (Make_Integer_Literal
(Loc
,
2765 (Prefix
(Prev_Orig
))),
2766 Extra_Accessibility
(Formal
));
2769 -- Treat the unchecked attributes as library-level
2771 when Attribute_Unchecked_Access |
2772 Attribute_Unrestricted_Access
=>
2774 (Make_Integer_Literal
(Loc
,
2775 Intval
=> Scope_Depth
(Standard_Standard
)),
2776 Extra_Accessibility
(Formal
));
2778 -- No other cases of attributes returning access
2779 -- values that can be passed to access parameters.
2782 raise Program_Error
;
2786 -- For allocators we pass the level of the execution of the
2787 -- called subprogram, which is one greater than the current
2792 (Make_Integer_Literal
(Loc
,
2793 Intval
=> Scope_Depth
(Current_Scope
) + 1),
2794 Extra_Accessibility
(Formal
));
2796 -- For most other cases we simply pass the level of the
2797 -- actual's access type. The type is retrieved from
2798 -- Prev rather than Prev_Orig, because in some cases
2799 -- Prev_Orig denotes an original expression that has
2800 -- not been analyzed.
2804 (Dynamic_Accessibility_Level
(Prev
),
2805 Extra_Accessibility
(Formal
));
2810 -- Perform the check of 4.6(49) that prevents a null value from being
2811 -- passed as an actual to an access parameter. Note that the check
2812 -- is elided in the common cases of passing an access attribute or
2813 -- access parameter as an actual. Also, we currently don't enforce
2814 -- this check for expander-generated actuals and when -gnatdj is set.
2816 if Ada_Version
>= Ada_2005
then
2818 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
2819 -- the intent of 6.4.1(13) is that null-exclusion checks should
2820 -- not be done for 'out' parameters, even though it refers only
2821 -- to constraint checks, and a null_exclusion is not a constraint.
2822 -- Note that AI05-0196-1 corrects this mistake in the RM.
2824 if Is_Access_Type
(Etype
(Formal
))
2825 and then Can_Never_Be_Null
(Etype
(Formal
))
2826 and then Ekind
(Formal
) /= E_Out_Parameter
2827 and then Nkind
(Prev
) /= N_Raise_Constraint_Error
2828 and then (Known_Null
(Prev
)
2829 or else not Can_Never_Be_Null
(Etype
(Prev
)))
2831 Install_Null_Excluding_Check
(Prev
);
2834 -- Ada_Version < Ada_2005
2837 if Ekind
(Etype
(Formal
)) /= E_Anonymous_Access_Type
2838 or else Access_Checks_Suppressed
(Subp
)
2842 elsif Debug_Flag_J
then
2845 elsif not Comes_From_Source
(Prev
) then
2848 elsif Is_Entity_Name
(Prev
)
2849 and then Ekind
(Etype
(Prev
)) = E_Anonymous_Access_Type
2853 elsif Nkind_In
(Prev
, N_Allocator
, N_Attribute_Reference
) then
2856 -- Suppress null checks when passing to access parameters of Java
2857 -- and CIL subprograms. (Should this be done for other foreign
2858 -- conventions as well ???)
2860 elsif Convention
(Subp
) = Convention_Java
2861 or else Convention
(Subp
) = Convention_CIL
2866 Install_Null_Excluding_Check
(Prev
);
2870 -- Perform appropriate validity checks on parameters that
2873 if Validity_Checks_On
then
2874 if (Ekind
(Formal
) = E_In_Parameter
2875 and then Validity_Check_In_Params
)
2877 (Ekind
(Formal
) = E_In_Out_Parameter
2878 and then Validity_Check_In_Out_Params
)
2880 -- If the actual is an indexed component of a packed type (or
2881 -- is an indexed or selected component whose prefix recursively
2882 -- meets this condition), it has not been expanded yet. It will
2883 -- be copied in the validity code that follows, and has to be
2884 -- expanded appropriately, so reanalyze it.
2886 -- What we do is just to unset analyzed bits on prefixes till
2887 -- we reach something that does not have a prefix.
2894 while Nkind_In
(Nod
, N_Indexed_Component
,
2895 N_Selected_Component
)
2897 Set_Analyzed
(Nod
, False);
2898 Nod
:= Prefix
(Nod
);
2902 Ensure_Valid
(Actual
);
2906 -- For IN OUT and OUT parameters, ensure that subscripts are valid
2907 -- since this is a left side reference. We only do this for calls
2908 -- from the source program since we assume that compiler generated
2909 -- calls explicitly generate any required checks. We also need it
2910 -- only if we are doing standard validity checks, since clearly it is
2911 -- not needed if validity checks are off, and in subscript validity
2912 -- checking mode, all indexed components are checked with a call
2913 -- directly from Expand_N_Indexed_Component.
2915 if Comes_From_Source
(Call_Node
)
2916 and then Ekind
(Formal
) /= E_In_Parameter
2917 and then Validity_Checks_On
2918 and then Validity_Check_Default
2919 and then not Validity_Check_Subscripts
2921 Check_Valid_Lvalue_Subscripts
(Actual
);
2924 -- Mark any scalar OUT parameter that is a simple variable as no
2925 -- longer known to be valid (unless the type is always valid). This
2926 -- reflects the fact that if an OUT parameter is never set in a
2927 -- procedure, then it can become invalid on the procedure return.
2929 if Ekind
(Formal
) = E_Out_Parameter
2930 and then Is_Entity_Name
(Actual
)
2931 and then Ekind
(Entity
(Actual
)) = E_Variable
2932 and then not Is_Known_Valid
(Etype
(Actual
))
2934 Set_Is_Known_Valid
(Entity
(Actual
), False);
2937 -- For an OUT or IN OUT parameter, if the actual is an entity, then
2938 -- clear current values, since they can be clobbered. We are probably
2939 -- doing this in more places than we need to, but better safe than
2940 -- sorry when it comes to retaining bad current values.
2942 if Ekind
(Formal
) /= E_In_Parameter
2943 and then Is_Entity_Name
(Actual
)
2944 and then Present
(Entity
(Actual
))
2947 Ent
: constant Entity_Id
:= Entity
(Actual
);
2951 -- For an OUT or IN OUT parameter that is an assignable entity,
2952 -- we do not want to clobber the Last_Assignment field, since
2953 -- if it is set, it was precisely because it is indeed an OUT
2954 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
2955 -- since the subprogram could have returned in invalid value.
2957 if Ekind_In
(Formal
, E_Out_Parameter
, E_In_Out_Parameter
)
2958 and then Is_Assignable
(Ent
)
2960 Sav
:= Last_Assignment
(Ent
);
2961 Kill_Current_Values
(Ent
);
2962 Set_Last_Assignment
(Ent
, Sav
);
2963 Set_Is_Known_Valid
(Ent
, False);
2965 -- For all other cases, just kill the current values
2968 Kill_Current_Values
(Ent
);
2973 -- If the formal is class wide and the actual is an aggregate, force
2974 -- evaluation so that the back end who does not know about class-wide
2975 -- type, does not generate a temporary of the wrong size.
2977 if not Is_Class_Wide_Type
(Etype
(Formal
)) then
2980 elsif Nkind
(Actual
) = N_Aggregate
2981 or else (Nkind
(Actual
) = N_Qualified_Expression
2982 and then Nkind
(Expression
(Actual
)) = N_Aggregate
)
2984 Force_Evaluation
(Actual
);
2987 -- In a remote call, if the formal is of a class-wide type, check
2988 -- that the actual meets the requirements described in E.4(18).
2990 if Remote
and then Is_Class_Wide_Type
(Etype
(Formal
)) then
2991 Insert_Action
(Actual
,
2992 Make_Transportable_Check
(Loc
,
2993 Duplicate_Subexpr_Move_Checks
(Actual
)));
2996 -- This label is required when skipping extra actual generation for
2997 -- Unchecked_Union parameters.
2999 <<Skip_Extra_Actual_Generation
>>
3001 Param_Count
:= Param_Count
+ 1;
3002 Next_Actual
(Actual
);
3003 Next_Formal
(Formal
);
3006 -- If we are calling an Ada 2012 function which needs to have the
3007 -- "accessibility level determined by the point of call" (AI05-0234)
3008 -- passed in to it, then pass it in.
3010 if Ekind_In
(Subp
, E_Function
, E_Operator
, E_Subprogram_Type
)
3012 Present
(Extra_Accessibility_Of_Result
(Ultimate_Alias
(Subp
)))
3015 Ancestor
: Node_Id
:= Parent
(Call_Node
);
3016 Level
: Node_Id
:= Empty
;
3017 Defer
: Boolean := False;
3020 -- Unimplemented: if Subp returns an anonymous access type, then
3022 -- a) if the call is the operand of an explict conversion, then
3023 -- the target type of the conversion (a named access type)
3024 -- determines the accessibility level pass in;
3026 -- b) if the call defines an access discriminant of an object
3027 -- (e.g., the discriminant of an object being created by an
3028 -- allocator, or the discriminant of a function result),
3029 -- then the accessibility level to pass in is that of the
3030 -- discriminated object being initialized).
3034 while Nkind
(Ancestor
) = N_Qualified_Expression
3036 Ancestor
:= Parent
(Ancestor
);
3039 case Nkind
(Ancestor
) is
3042 -- At this point, we'd like to assign
3044 -- Level := Dynamic_Accessibility_Level (Ancestor);
3046 -- but Etype of Ancestor may not have been set yet,
3047 -- so that doesn't work.
3049 -- Handle this later in Expand_Allocator_Expression.
3053 when N_Object_Declaration | N_Object_Renaming_Declaration
=>
3055 Def_Id
: constant Entity_Id
:=
3056 Defining_Identifier
(Ancestor
);
3059 if Is_Return_Object
(Def_Id
) then
3060 if Present
(Extra_Accessibility_Of_Result
3061 (Return_Applies_To
(Scope
(Def_Id
))))
3063 -- Pass along value that was passed in if the
3064 -- routine we are returning from also has an
3065 -- Accessibility_Of_Result formal.
3069 (Extra_Accessibility_Of_Result
3070 (Return_Applies_To
(Scope
(Def_Id
))), Loc
);
3074 Make_Integer_Literal
(Loc
,
3075 Intval
=> Object_Access_Level
(Def_Id
));
3079 when N_Simple_Return_Statement
=>
3080 if Present
(Extra_Accessibility_Of_Result
3082 (Return_Statement_Entity
(Ancestor
))))
3084 -- Pass along value that was passed in if the returned
3085 -- routine also has an Accessibility_Of_Result formal.
3089 (Extra_Accessibility_Of_Result
3091 (Return_Statement_Entity
(Ancestor
))), Loc
);
3099 if not Present
(Level
) then
3101 -- The "innermost master that evaluates the function call".
3103 -- ??? - Should we use Integer'Last here instead in order
3104 -- to deal with (some of) the problems associated with
3105 -- calls to subps whose enclosing scope is unknown (e.g.,
3106 -- Anon_Access_To_Subp_Param.all)?
3108 Level
:= Make_Integer_Literal
(Loc
,
3109 Scope_Depth
(Current_Scope
) + 1);
3114 Extra_Accessibility_Of_Result
(Ultimate_Alias
(Subp
)));
3119 -- If we are expanding the RHS of an assignment we need to check if tag
3120 -- propagation is needed. You might expect this processing to be in
3121 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3122 -- assignment might be transformed to a declaration for an unconstrained
3123 -- value if the expression is classwide.
3125 if Nkind
(Call_Node
) = N_Function_Call
3126 and then Is_Tag_Indeterminate
(Call_Node
)
3127 and then Is_Entity_Name
(Name
(Call_Node
))
3130 Ass
: Node_Id
:= Empty
;
3133 if Nkind
(Parent
(Call_Node
)) = N_Assignment_Statement
then
3134 Ass
:= Parent
(Call_Node
);
3136 elsif Nkind
(Parent
(Call_Node
)) = N_Qualified_Expression
3137 and then Nkind
(Parent
(Parent
(Call_Node
))) =
3138 N_Assignment_Statement
3140 Ass
:= Parent
(Parent
(Call_Node
));
3142 elsif Nkind
(Parent
(Call_Node
)) = N_Explicit_Dereference
3143 and then Nkind
(Parent
(Parent
(Call_Node
))) =
3144 N_Assignment_Statement
3146 Ass
:= Parent
(Parent
(Call_Node
));
3150 and then Is_Class_Wide_Type
(Etype
(Name
(Ass
)))
3152 if Is_Access_Type
(Etype
(Call_Node
)) then
3153 if Designated_Type
(Etype
(Call_Node
)) /=
3154 Root_Type
(Etype
(Name
(Ass
)))
3157 ("tag-indeterminate expression "
3158 & " must have designated type& (RM 5.2 (6))",
3159 Call_Node
, Root_Type
(Etype
(Name
(Ass
))));
3161 Propagate_Tag
(Name
(Ass
), Call_Node
);
3164 elsif Etype
(Call_Node
) /= Root_Type
(Etype
(Name
(Ass
))) then
3166 ("tag-indeterminate expression must have type&"
3168 Call_Node
, Root_Type
(Etype
(Name
(Ass
))));
3171 Propagate_Tag
(Name
(Ass
), Call_Node
);
3174 -- The call will be rewritten as a dispatching call, and
3175 -- expanded as such.
3182 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3183 -- it to point to the correct secondary virtual table
3185 if Nkind
(Call_Node
) in N_Subprogram_Call
3186 and then CW_Interface_Formals_Present
3188 Expand_Interface_Actuals
(Call_Node
);
3191 -- Deals with Dispatch_Call if we still have a call, before expanding
3192 -- extra actuals since this will be done on the re-analysis of the
3193 -- dispatching call. Note that we do not try to shorten the actual list
3194 -- for a dispatching call, it would not make sense to do so. Expansion
3195 -- of dispatching calls is suppressed when VM_Target, because the VM
3196 -- back-ends directly handle the generation of dispatching calls and
3197 -- would have to undo any expansion to an indirect call.
3199 if Nkind
(Call_Node
) in N_Subprogram_Call
3200 and then Present
(Controlling_Argument
(Call_Node
))
3203 Call_Typ
: constant Entity_Id
:= Etype
(Call_Node
);
3204 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
3205 Eq_Prim_Op
: Entity_Id
:= Empty
;
3208 Prev_Call
: Node_Id
;
3211 if not Is_Limited_Type
(Typ
) then
3212 Eq_Prim_Op
:= Find_Prim_Op
(Typ
, Name_Op_Eq
);
3215 if Tagged_Type_Expansion
then
3216 Expand_Dispatching_Call
(Call_Node
);
3218 -- The following return is worrisome. Is it really OK to skip
3219 -- all remaining processing in this procedure ???
3226 Apply_Tag_Checks
(Call_Node
);
3228 -- If this is a dispatching "=", we must first compare the
3229 -- tags so we generate: x.tag = y.tag and then x = y
3231 if Subp
= Eq_Prim_Op
then
3233 -- Mark the node as analyzed to avoid reanalizing this
3234 -- dispatching call (which would cause a never-ending loop)
3236 Prev_Call
:= Relocate_Node
(Call_Node
);
3237 Set_Analyzed
(Prev_Call
);
3239 Param
:= First_Actual
(Call_Node
);
3245 Make_Selected_Component
(Loc
,
3246 Prefix
=> New_Value
(Param
),
3249 (First_Tag_Component
(Typ
), Loc
)),
3252 Make_Selected_Component
(Loc
,
3254 Unchecked_Convert_To
(Typ
,
3255 New_Value
(Next_Actual
(Param
))),
3258 (First_Tag_Component
(Typ
), Loc
))),
3259 Right_Opnd
=> Prev_Call
);
3261 Rewrite
(Call_Node
, New_Call
);
3264 (Call_Node
, Call_Typ
, Suppress
=> All_Checks
);
3267 -- Expansion of a dispatching call results in an indirect call,
3268 -- which in turn causes current values to be killed (see
3269 -- Resolve_Call), so on VM targets we do the call here to
3270 -- ensure consistent warnings between VM and non-VM targets.
3272 Kill_Current_Values
;
3275 -- If this is a dispatching "=" then we must update the reference
3276 -- to the call node because we generated:
3277 -- x.tag = y.tag and then x = y
3279 if Subp
= Eq_Prim_Op
then
3280 Call_Node
:= Right_Opnd
(Call_Node
);
3285 -- Similarly, expand calls to RCI subprograms on which pragma
3286 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3287 -- later. Do this only when the call comes from source since we
3288 -- do not want such a rewriting to occur in expanded code.
3290 if Is_All_Remote_Call
(Call_Node
) then
3291 Expand_All_Calls_Remote_Subprogram_Call
(Call_Node
);
3293 -- Similarly, do not add extra actuals for an entry call whose entity
3294 -- is a protected procedure, or for an internal protected subprogram
3295 -- call, because it will be rewritten as a protected subprogram call
3296 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3298 elsif Is_Protected_Type
(Scope
(Subp
))
3299 and then (Ekind
(Subp
) = E_Procedure
3300 or else Ekind
(Subp
) = E_Function
)
3304 -- During that loop we gathered the extra actuals (the ones that
3305 -- correspond to Extra_Formals), so now they can be appended.
3308 while Is_Non_Empty_List
(Extra_Actuals
) loop
3309 Add_Actual_Parameter
(Remove_Head
(Extra_Actuals
));
3313 -- At this point we have all the actuals, so this is the point at which
3314 -- the various expansion activities for actuals is carried out.
3316 Expand_Actuals
(Call_Node
, Subp
);
3318 -- Verify that the actuals do not share storage. This check must be done
3319 -- on the caller side rather that inside the subprogram to avoid issues
3320 -- of parameter passing.
3322 if Check_Aliasing_Of_Parameters
then
3323 Apply_Parameter_Aliasing_Checks
(Call_Node
, Subp
);
3326 -- If the subprogram is a renaming, or if it is inherited, replace it in
3327 -- the call with the name of the actual subprogram being called. If this
3328 -- is a dispatching call, the run-time decides what to call. The Alias
3329 -- attribute does not apply to entries.
3331 if Nkind
(Call_Node
) /= N_Entry_Call_Statement
3332 and then No
(Controlling_Argument
(Call_Node
))
3333 and then Present
(Parent_Subp
)
3334 and then not Is_Direct_Deep_Call
(Subp
)
3336 if Present
(Inherited_From_Formal
(Subp
)) then
3337 Parent_Subp
:= Inherited_From_Formal
(Subp
);
3339 Parent_Subp
:= Ultimate_Alias
(Parent_Subp
);
3342 -- The below setting of Entity is suspect, see F109-018 discussion???
3344 Set_Entity
(Name
(Call_Node
), Parent_Subp
);
3346 if Is_Abstract_Subprogram
(Parent_Subp
)
3347 and then not In_Instance
3350 ("cannot call abstract subprogram &!",
3351 Name
(Call_Node
), Parent_Subp
);
3354 -- Inspect all formals of derived subprogram Subp. Compare parameter
3355 -- types with the parent subprogram and check whether an actual may
3356 -- need a type conversion to the corresponding formal of the parent
3359 -- Not clear whether intrinsic subprograms need such conversions. ???
3361 if not Is_Intrinsic_Subprogram
(Parent_Subp
)
3362 or else Is_Generic_Instance
(Parent_Subp
)
3365 procedure Convert
(Act
: Node_Id
; Typ
: Entity_Id
);
3366 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3367 -- and resolve the newly generated construct.
3373 procedure Convert
(Act
: Node_Id
; Typ
: Entity_Id
) is
3375 Rewrite
(Act
, OK_Convert_To
(Typ
, Relocate_Node
(Act
)));
3382 Actual_Typ
: Entity_Id
;
3383 Formal_Typ
: Entity_Id
;
3384 Parent_Typ
: Entity_Id
;
3387 Actual
:= First_Actual
(Call_Node
);
3388 Formal
:= First_Formal
(Subp
);
3389 Parent_Formal
:= First_Formal
(Parent_Subp
);
3390 while Present
(Formal
) loop
3391 Actual_Typ
:= Etype
(Actual
);
3392 Formal_Typ
:= Etype
(Formal
);
3393 Parent_Typ
:= Etype
(Parent_Formal
);
3395 -- For an IN parameter of a scalar type, the parent formal
3396 -- type and derived formal type differ or the parent formal
3397 -- type and actual type do not match statically.
3399 if Is_Scalar_Type
(Formal_Typ
)
3400 and then Ekind
(Formal
) = E_In_Parameter
3401 and then Formal_Typ
/= Parent_Typ
3403 not Subtypes_Statically_Match
(Parent_Typ
, Actual_Typ
)
3404 and then not Raises_Constraint_Error
(Actual
)
3406 Convert
(Actual
, Parent_Typ
);
3407 Enable_Range_Check
(Actual
);
3409 -- If the actual has been marked as requiring a range
3410 -- check, then generate it here.
3412 if Do_Range_Check
(Actual
) then
3413 Generate_Range_Check
3414 (Actual
, Etype
(Formal
), CE_Range_Check_Failed
);
3417 -- For access types, the parent formal type and actual type
3420 elsif Is_Access_Type
(Formal_Typ
)
3421 and then Base_Type
(Parent_Typ
) /= Base_Type
(Actual_Typ
)
3423 if Ekind
(Formal
) /= E_In_Parameter
then
3424 Convert
(Actual
, Parent_Typ
);
3426 elsif Ekind
(Parent_Typ
) = E_Anonymous_Access_Type
3427 and then Designated_Type
(Parent_Typ
) /=
3428 Designated_Type
(Actual_Typ
)
3429 and then not Is_Controlling_Formal
(Formal
)
3431 -- This unchecked conversion is not necessary unless
3432 -- inlining is enabled, because in that case the type
3433 -- mismatch may become visible in the body about to be
3437 Unchecked_Convert_To
(Parent_Typ
,
3438 Relocate_Node
(Actual
)));
3440 Resolve
(Actual
, Parent_Typ
);
3443 -- If there is a change of representation, then generate a
3444 -- warning, and do the change of representation.
3446 elsif not Same_Representation
(Formal_Typ
, Parent_Typ
) then
3448 ("??change of representation required", Actual
);
3449 Convert
(Actual
, Parent_Typ
);
3451 -- For array and record types, the parent formal type and
3452 -- derived formal type have different sizes or pragma Pack
3455 elsif ((Is_Array_Type
(Formal_Typ
)
3456 and then Is_Array_Type
(Parent_Typ
))
3458 (Is_Record_Type
(Formal_Typ
)
3459 and then Is_Record_Type
(Parent_Typ
)))
3461 (Esize
(Formal_Typ
) /= Esize
(Parent_Typ
)
3462 or else Has_Pragma_Pack
(Formal_Typ
) /=
3463 Has_Pragma_Pack
(Parent_Typ
))
3465 Convert
(Actual
, Parent_Typ
);
3468 Next_Actual
(Actual
);
3469 Next_Formal
(Formal
);
3470 Next_Formal
(Parent_Formal
);
3476 Subp
:= Parent_Subp
;
3479 -- Deal with case where call is an explicit dereference
3481 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
3483 -- Handle case of access to protected subprogram type
3485 if Is_Access_Protected_Subprogram_Type
3486 (Base_Type
(Etype
(Prefix
(Name
(Call_Node
)))))
3488 -- If this is a call through an access to protected operation, the
3489 -- prefix has the form (object'address, operation'access). Rewrite
3490 -- as a for other protected calls: the object is the 1st parameter
3491 -- of the list of actuals.
3498 Ptr
: constant Node_Id
:= Prefix
(Name
(Call_Node
));
3500 T
: constant Entity_Id
:=
3501 Equivalent_Type
(Base_Type
(Etype
(Ptr
)));
3503 D_T
: constant Entity_Id
:=
3504 Designated_Type
(Base_Type
(Etype
(Ptr
)));
3508 Make_Selected_Component
(Loc
,
3509 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
3511 New_Occurrence_Of
(First_Entity
(T
), Loc
));
3514 Make_Selected_Component
(Loc
,
3515 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
3517 New_Occurrence_Of
(Next_Entity
(First_Entity
(T
)), Loc
));
3520 Make_Explicit_Dereference
(Loc
,
3523 if Present
(Parameter_Associations
(Call_Node
)) then
3524 Parm
:= Parameter_Associations
(Call_Node
);
3529 Prepend
(Obj
, Parm
);
3531 if Etype
(D_T
) = Standard_Void_Type
then
3533 Make_Procedure_Call_Statement
(Loc
,
3535 Parameter_Associations
=> Parm
);
3538 Make_Function_Call
(Loc
,
3540 Parameter_Associations
=> Parm
);
3543 Set_First_Named_Actual
(Call
, First_Named_Actual
(Call_Node
));
3544 Set_Etype
(Call
, Etype
(D_T
));
3546 -- We do not re-analyze the call to avoid infinite recursion.
3547 -- We analyze separately the prefix and the object, and set
3548 -- the checks on the prefix that would otherwise be emitted
3549 -- when resolving a call.
3551 Rewrite
(Call_Node
, Call
);
3553 Apply_Access_Check
(Nam
);
3560 -- If this is a call to an intrinsic subprogram, then perform the
3561 -- appropriate expansion to the corresponding tree node and we
3562 -- are all done (since after that the call is gone).
3564 -- In the case where the intrinsic is to be processed by the back end,
3565 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3566 -- since the idea in this case is to pass the call unchanged. If the
3567 -- intrinsic is an inherited unchecked conversion, and the derived type
3568 -- is the target type of the conversion, we must retain it as the return
3569 -- type of the expression. Otherwise the expansion below, which uses the
3570 -- parent operation, will yield the wrong type.
3572 if Is_Intrinsic_Subprogram
(Subp
) then
3573 Expand_Intrinsic_Call
(Call_Node
, Subp
);
3575 if Nkind
(Call_Node
) = N_Unchecked_Type_Conversion
3576 and then Parent_Subp
/= Orig_Subp
3577 and then Etype
(Parent_Subp
) /= Etype
(Orig_Subp
)
3579 Set_Etype
(Call_Node
, Etype
(Orig_Subp
));
3585 if Ekind_In
(Subp
, E_Function
, E_Procedure
) then
3587 -- We perform two simple optimization on calls:
3589 -- a) replace calls to null procedures unconditionally;
3591 -- b) for To_Address, just do an unchecked conversion. Not only is
3592 -- this efficient, but it also avoids order of elaboration problems
3593 -- when address clauses are inlined (address expression elaborated
3594 -- at the wrong point).
3596 -- We perform these optimization regardless of whether we are in the
3597 -- main unit or in a unit in the context of the main unit, to ensure
3598 -- that tree generated is the same in both cases, for CodePeer use.
3600 if Is_RTE
(Subp
, RE_To_Address
) then
3602 Unchecked_Convert_To
3603 (RTE
(RE_Address
), Relocate_Node
(First_Actual
(Call_Node
))));
3606 elsif Is_Null_Procedure
(Subp
) then
3607 Rewrite
(Call_Node
, Make_Null_Statement
(Loc
));
3611 -- Handle inlining. No action needed if the subprogram is not inlined
3613 if not Is_Inlined
(Subp
) then
3616 -- Handle frontend inlining
3618 elsif not Back_End_Inlining
then
3619 Inlined_Subprogram
: declare
3621 Must_Inline
: Boolean := False;
3622 Spec
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
3625 -- Verify that the body to inline has already been seen, and
3626 -- that if the body is in the current unit the inlining does
3627 -- not occur earlier. This avoids order-of-elaboration problems
3630 -- This should be documented in sinfo/einfo ???
3633 or else Nkind
(Spec
) /= N_Subprogram_Declaration
3634 or else No
(Body_To_Inline
(Spec
))
3636 Must_Inline
:= False;
3638 -- If this an inherited function that returns a private type,
3639 -- do not inline if the full view is an unconstrained array,
3640 -- because such calls cannot be inlined.
3642 elsif Present
(Orig_Subp
)
3643 and then Is_Array_Type
(Etype
(Orig_Subp
))
3644 and then not Is_Constrained
(Etype
(Orig_Subp
))
3646 Must_Inline
:= False;
3648 elsif In_Unfrozen_Instance
(Scope
(Subp
)) then
3649 Must_Inline
:= False;
3652 Bod
:= Body_To_Inline
(Spec
);
3654 if (In_Extended_Main_Code_Unit
(Call_Node
)
3655 or else In_Extended_Main_Code_Unit
(Parent
(Call_Node
))
3656 or else Has_Pragma_Inline_Always
(Subp
))
3657 and then (not In_Same_Extended_Unit
(Sloc
(Bod
), Loc
)
3659 Earlier_In_Extended_Unit
(Sloc
(Bod
), Loc
))
3661 Must_Inline
:= True;
3663 -- If we are compiling a package body that is not the main
3664 -- unit, it must be for inlining/instantiation purposes,
3665 -- in which case we inline the call to insure that the same
3666 -- temporaries are generated when compiling the body by
3667 -- itself. Otherwise link errors can occur.
3669 -- If the function being called is itself in the main unit,
3670 -- we cannot inline, because there is a risk of double
3671 -- elaboration and/or circularity: the inlining can make
3672 -- visible a private entity in the body of the main unit,
3673 -- that gigi will see before its sees its proper definition.
3675 elsif not (In_Extended_Main_Code_Unit
(Call_Node
))
3676 and then In_Package_Body
3678 Must_Inline
:= not In_Extended_Main_Source_Unit
(Subp
);
3683 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
3686 -- Let the back end handle it
3688 Add_Inlined_Body
(Subp
);
3690 if Front_End_Inlining
3691 and then Nkind
(Spec
) = N_Subprogram_Declaration
3692 and then (In_Extended_Main_Code_Unit
(Call_Node
))
3693 and then No
(Body_To_Inline
(Spec
))
3694 and then not Has_Completion
(Subp
)
3695 and then In_Same_Extended_Unit
(Sloc
(Spec
), Loc
)
3698 ("cannot inline& (body not seen yet)?",
3702 end Inlined_Subprogram
;
3704 -- Back end inlining: let the back end handle it
3706 elsif No
(Unit_Declaration_Node
(Subp
))
3707 or else Nkind
(Unit_Declaration_Node
(Subp
)) /=
3708 N_Subprogram_Declaration
3709 or else No
(Body_To_Inline
(Unit_Declaration_Node
(Subp
)))
3711 Add_Inlined_Body
(Subp
);
3712 Register_Backend_Call
(Call_Node
);
3714 -- If the call is to a function in a run-time unit that is marked
3715 -- Inline_Always, we must suppress debugging information on it,
3716 -- so that the code that is eventually inlined will not affect
3717 -- debugging of the user program.
3719 if Is_Predefined_File_Name
3720 (Unit_File_Name
(Get_Source_Unit
(Sloc
(Subp
))))
3721 and then In_Extended_Main_Source_Unit
(N
)
3723 -- We make an exception for calls to the Ada hierarchy if call
3724 -- comes from source, because some user applications need the
3725 -- debugging information for such calls.
3727 if Comes_From_Source
(Call_Node
)
3728 and then Name_Buffer
(1 .. 2) = "a-"
3732 Set_Needs_Debug_Info
(Subp
, False);
3736 -- Front end expansion of simple functions returning unconstrained
3737 -- types (see Check_And_Split_Unconstrained_Function) and simple
3738 -- renamings inlined by the front end (see Build_Renamed_Entity).
3741 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
3745 -- Check for protected subprogram. This is either an intra-object call,
3746 -- or a protected function call. Protected procedure calls are rewritten
3747 -- as entry calls and handled accordingly.
3749 -- In Ada 2005, this may be an indirect call to an access parameter that
3750 -- is an access_to_subprogram. In that case the anonymous type has a
3751 -- scope that is a protected operation, but the call is a regular one.
3752 -- In either case do not expand call if subprogram is eliminated.
3754 Scop
:= Scope
(Subp
);
3756 if Nkind
(Call_Node
) /= N_Entry_Call_Statement
3757 and then Is_Protected_Type
(Scop
)
3758 and then Ekind
(Subp
) /= E_Subprogram_Type
3759 and then not Is_Eliminated
(Subp
)
3761 -- If the call is an internal one, it is rewritten as a call to the
3762 -- corresponding unprotected subprogram.
3764 Expand_Protected_Subprogram_Call
(Call_Node
, Subp
, Scop
);
3767 -- Functions returning controlled objects need special attention. If
3768 -- the return type is limited, then the context is initialization and
3769 -- different processing applies. If the call is to a protected function,
3770 -- the expansion above will call Expand_Call recursively. Otherwise the
3771 -- function call is transformed into a temporary which obtains the
3772 -- result from the secondary stack.
3774 if Needs_Finalization
(Etype
(Subp
)) then
3775 if not Is_Limited_View
(Etype
(Subp
))
3777 (No
(First_Formal
(Subp
))
3779 not Is_Concurrent_Record_Type
(Etype
(First_Formal
(Subp
))))
3781 Expand_Ctrl_Function_Call
(Call_Node
);
3783 -- Build-in-place function calls which appear in anonymous contexts
3784 -- need a transient scope to ensure the proper finalization of the
3785 -- intermediate result after its use.
3787 elsif Is_Build_In_Place_Function_Call
(Call_Node
)
3789 Nkind_In
(Parent
(Call_Node
), N_Attribute_Reference
,
3791 N_Indexed_Component
,
3792 N_Object_Renaming_Declaration
,
3793 N_Procedure_Call_Statement
,
3794 N_Selected_Component
,
3797 Establish_Transient_Scope
(Call_Node
, Sec_Stack
=> True);
3802 -------------------------------
3803 -- Expand_Ctrl_Function_Call --
3804 -------------------------------
3806 procedure Expand_Ctrl_Function_Call
(N
: Node_Id
) is
3807 function Is_Element_Reference
(N
: Node_Id
) return Boolean;
3808 -- Determine whether node N denotes a reference to an Ada 2012 container
3811 --------------------------
3812 -- Is_Element_Reference --
3813 --------------------------
3815 function Is_Element_Reference
(N
: Node_Id
) return Boolean is
3816 Ref
: constant Node_Id
:= Original_Node
(N
);
3819 -- Analysis marks an element reference by setting the generalized
3820 -- indexing attribute of an indexed component before the component
3821 -- is rewritten into a function call.
3824 Nkind
(Ref
) = N_Indexed_Component
3825 and then Present
(Generalized_Indexing
(Ref
));
3826 end Is_Element_Reference
;
3830 Is_Elem_Ref
: constant Boolean := Is_Element_Reference
(N
);
3832 -- Start of processing for Expand_Ctrl_Function_Call
3835 -- Optimization, if the returned value (which is on the sec-stack) is
3836 -- returned again, no need to copy/readjust/finalize, we can just pass
3837 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
3838 -- attachment is needed
3840 if Nkind
(Parent
(N
)) = N_Simple_Return_Statement
then
3844 -- Resolution is now finished, make sure we don't start analysis again
3845 -- because of the duplication.
3849 -- A function which returns a controlled object uses the secondary
3850 -- stack. Rewrite the call into a temporary which obtains the result of
3851 -- the function using 'reference.
3853 Remove_Side_Effects
(N
);
3855 -- When the temporary function result appears inside a case expression
3856 -- or an if expression, its lifetime must be extended to match that of
3857 -- the context. If not, the function result will be finalized too early
3858 -- and the evaluation of the expression could yield incorrect result. An
3859 -- exception to this rule are references to Ada 2012 container elements.
3860 -- Such references must be finalized at the end of each iteration of the
3861 -- related quantified expression, otherwise the container will remain
3865 and then Within_Case_Or_If_Expression
(N
)
3866 and then Nkind
(N
) = N_Explicit_Dereference
3868 Set_Is_Processed_Transient
(Entity
(Prefix
(N
)));
3870 end Expand_Ctrl_Function_Call
;
3872 ----------------------------------------
3873 -- Expand_N_Extended_Return_Statement --
3874 ----------------------------------------
3876 -- If there is a Handled_Statement_Sequence, we rewrite this:
3878 -- return Result : T := <expression> do
3879 -- <handled_seq_of_stms>
3885 -- Result : T := <expression>;
3887 -- <handled_seq_of_stms>
3891 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
3893 -- return Result : T := <expression>;
3897 -- return <expression>;
3899 -- unless it's build-in-place or there's no <expression>, in which case
3903 -- Result : T := <expression>;
3908 -- Note that this case could have been written by the user as an extended
3909 -- return statement, or could have been transformed to this from a simple
3910 -- return statement.
3912 -- That is, we need to have a reified return object if there are statements
3913 -- (which might refer to it) or if we're doing build-in-place (so we can
3914 -- set its address to the final resting place or if there is no expression
3915 -- (in which case default initial values might need to be set).
3917 procedure Expand_N_Extended_Return_Statement
(N
: Node_Id
) is
3918 Loc
: constant Source_Ptr
:= Sloc
(N
);
3920 Par_Func
: constant Entity_Id
:=
3921 Return_Applies_To
(Return_Statement_Entity
(N
));
3922 Result_Subt
: constant Entity_Id
:= Etype
(Par_Func
);
3923 Ret_Obj_Id
: constant Entity_Id
:=
3924 First_Entity
(Return_Statement_Entity
(N
));
3925 Ret_Obj_Decl
: constant Node_Id
:= Parent
(Ret_Obj_Id
);
3927 Is_Build_In_Place
: constant Boolean :=
3928 Is_Build_In_Place_Function
(Par_Func
);
3933 Return_Stmt
: Node_Id
;
3936 function Build_Heap_Allocator
3937 (Temp_Id
: Entity_Id
;
3938 Temp_Typ
: Entity_Id
;
3939 Func_Id
: Entity_Id
;
3940 Ret_Typ
: Entity_Id
;
3941 Alloc_Expr
: Node_Id
) return Node_Id
;
3942 -- Create the statements necessary to allocate a return object on the
3943 -- caller's master. The master is available through implicit parameter
3944 -- BIPfinalizationmaster.
3946 -- if BIPfinalizationmaster /= null then
3948 -- type Ptr_Typ is access Ret_Typ;
3949 -- for Ptr_Typ'Storage_Pool use
3950 -- Base_Pool (BIPfinalizationmaster.all).all;
3954 -- procedure Allocate (...) is
3956 -- System.Storage_Pools.Subpools.Allocate_Any (...);
3959 -- Local := <Alloc_Expr>;
3960 -- Temp_Id := Temp_Typ (Local);
3964 -- Temp_Id is the temporary which is used to reference the internally
3965 -- created object in all allocation forms. Temp_Typ is the type of the
3966 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
3967 -- type of Func_Id. Alloc_Expr is the actual allocator.
3969 function Move_Activation_Chain
return Node_Id
;
3970 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
3972 -- From current activation chain
3973 -- To activation chain passed in by the caller
3974 -- New_Master master passed in by the caller
3976 --------------------------
3977 -- Build_Heap_Allocator --
3978 --------------------------
3980 function Build_Heap_Allocator
3981 (Temp_Id
: Entity_Id
;
3982 Temp_Typ
: Entity_Id
;
3983 Func_Id
: Entity_Id
;
3984 Ret_Typ
: Entity_Id
;
3985 Alloc_Expr
: Node_Id
) return Node_Id
3988 pragma Assert
(Is_Build_In_Place_Function
(Func_Id
));
3990 -- Processing for build-in-place object allocation. This is disabled
3991 -- on .NET/JVM because the targets do not support pools.
3993 if VM_Target
= No_VM
3994 and then Needs_Finalization
(Ret_Typ
)
3997 Decls
: constant List_Id
:= New_List
;
3998 Fin_Mas_Id
: constant Entity_Id
:=
3999 Build_In_Place_Formal
4000 (Func_Id
, BIP_Finalization_Master
);
4001 Stmts
: constant List_Id
:= New_List
;
4002 Desig_Typ
: Entity_Id
;
4003 Local_Id
: Entity_Id
;
4004 Pool_Id
: Entity_Id
;
4005 Ptr_Typ
: Entity_Id
;
4009 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
4011 Pool_Id
:= Make_Temporary
(Loc
, 'P');
4014 Make_Object_Renaming_Declaration
(Loc
,
4015 Defining_Identifier
=> Pool_Id
,
4017 New_Occurrence_Of
(RTE
(RE_Root_Storage_Pool
), Loc
),
4019 Make_Explicit_Dereference
(Loc
,
4021 Make_Function_Call
(Loc
,
4023 New_Occurrence_Of
(RTE
(RE_Base_Pool
), Loc
),
4024 Parameter_Associations
=> New_List
(
4025 Make_Explicit_Dereference
(Loc
,
4027 New_Occurrence_Of
(Fin_Mas_Id
, Loc
)))))));
4029 -- Create an access type which uses the storage pool of the
4030 -- caller's master. This additional type is necessary because
4031 -- the finalization master cannot be associated with the type
4032 -- of the temporary. Otherwise the secondary stack allocation
4035 Desig_Typ
:= Ret_Typ
;
4037 -- Ensure that the build-in-place machinery uses a fat pointer
4038 -- when allocating an unconstrained array on the heap. In this
4039 -- case the result object type is a constrained array type even
4040 -- though the function type is unconstrained.
4042 if Ekind
(Desig_Typ
) = E_Array_Subtype
then
4043 Desig_Typ
:= Base_Type
(Desig_Typ
);
4047 -- type Ptr_Typ is access Desig_Typ;
4049 Ptr_Typ
:= Make_Temporary
(Loc
, 'P');
4052 Make_Full_Type_Declaration
(Loc
,
4053 Defining_Identifier
=> Ptr_Typ
,
4055 Make_Access_To_Object_Definition
(Loc
,
4056 Subtype_Indication
=>
4057 New_Occurrence_Of
(Desig_Typ
, Loc
))));
4059 -- Perform minor decoration in order to set the master and the
4060 -- storage pool attributes.
4062 Set_Ekind
(Ptr_Typ
, E_Access_Type
);
4063 Set_Finalization_Master
(Ptr_Typ
, Fin_Mas_Id
);
4064 Set_Associated_Storage_Pool
(Ptr_Typ
, Pool_Id
);
4066 -- Create the temporary, generate:
4067 -- Local_Id : Ptr_Typ;
4069 Local_Id
:= Make_Temporary
(Loc
, 'T');
4072 Make_Object_Declaration
(Loc
,
4073 Defining_Identifier
=> Local_Id
,
4074 Object_Definition
=>
4075 New_Occurrence_Of
(Ptr_Typ
, Loc
)));
4077 -- Allocate the object, generate:
4078 -- Local_Id := <Alloc_Expr>;
4081 Make_Assignment_Statement
(Loc
,
4082 Name
=> New_Occurrence_Of
(Local_Id
, Loc
),
4083 Expression
=> Alloc_Expr
));
4086 -- Temp_Id := Temp_Typ (Local_Id);
4089 Make_Assignment_Statement
(Loc
,
4090 Name
=> New_Occurrence_Of
(Temp_Id
, Loc
),
4092 Unchecked_Convert_To
(Temp_Typ
,
4093 New_Occurrence_Of
(Local_Id
, Loc
))));
4095 -- Wrap the allocation in a block. This is further conditioned
4096 -- by checking the caller finalization master at runtime. A
4097 -- null value indicates a non-existent master, most likely due
4098 -- to a Finalize_Storage_Only allocation.
4101 -- if BIPfinalizationmaster /= null then
4110 Make_If_Statement
(Loc
,
4113 Left_Opnd
=> New_Occurrence_Of
(Fin_Mas_Id
, Loc
),
4114 Right_Opnd
=> Make_Null
(Loc
)),
4116 Then_Statements
=> New_List
(
4117 Make_Block_Statement
(Loc
,
4118 Declarations
=> Decls
,
4119 Handled_Statement_Sequence
=>
4120 Make_Handled_Sequence_Of_Statements
(Loc
,
4121 Statements
=> Stmts
))));
4124 -- For all other cases, generate:
4125 -- Temp_Id := <Alloc_Expr>;
4129 Make_Assignment_Statement
(Loc
,
4130 Name
=> New_Occurrence_Of
(Temp_Id
, Loc
),
4131 Expression
=> Alloc_Expr
);
4133 end Build_Heap_Allocator
;
4135 ---------------------------
4136 -- Move_Activation_Chain --
4137 ---------------------------
4139 function Move_Activation_Chain
return Node_Id
is
4142 Make_Procedure_Call_Statement
(Loc
,
4144 New_Occurrence_Of
(RTE
(RE_Move_Activation_Chain
), Loc
),
4146 Parameter_Associations
=> New_List
(
4150 Make_Attribute_Reference
(Loc
,
4151 Prefix
=> Make_Identifier
(Loc
, Name_uChain
),
4152 Attribute_Name
=> Name_Unrestricted_Access
),
4154 -- Destination chain
4157 (Build_In_Place_Formal
(Par_Func
, BIP_Activation_Chain
), Loc
),
4162 (Build_In_Place_Formal
(Par_Func
, BIP_Task_Master
), Loc
)));
4163 end Move_Activation_Chain
;
4165 -- Start of processing for Expand_N_Extended_Return_Statement
4168 -- Given that functionality of interface thunks is simple (just displace
4169 -- the pointer to the object) they are always handled by means of
4170 -- simple return statements.
4172 pragma Assert
(not Is_Thunk
(Current_Scope
));
4174 if Nkind
(Ret_Obj_Decl
) = N_Object_Declaration
then
4175 Exp
:= Expression
(Ret_Obj_Decl
);
4180 HSS
:= Handled_Statement_Sequence
(N
);
4182 -- If the returned object needs finalization actions, the function must
4183 -- perform the appropriate cleanup should it fail to return. The state
4184 -- of the function itself is tracked through a flag which is coupled
4185 -- with the scope finalizer. There is one flag per each return object
4186 -- in case of multiple returns.
4188 if Is_Build_In_Place
4189 and then Needs_Finalization
(Etype
(Ret_Obj_Id
))
4192 Flag_Decl
: Node_Id
;
4193 Flag_Id
: Entity_Id
;
4197 -- Recover the function body
4199 Func_Bod
:= Unit_Declaration_Node
(Par_Func
);
4201 if Nkind
(Func_Bod
) = N_Subprogram_Declaration
then
4202 Func_Bod
:= Parent
(Parent
(Corresponding_Body
(Func_Bod
)));
4205 -- Create a flag to track the function state
4207 Flag_Id
:= Make_Temporary
(Loc
, 'F');
4208 Set_Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
, Flag_Id
);
4210 -- Insert the flag at the beginning of the function declarations,
4212 -- Fnn : Boolean := False;
4215 Make_Object_Declaration
(Loc
,
4216 Defining_Identifier
=> Flag_Id
,
4217 Object_Definition
=>
4218 New_Occurrence_Of
(Standard_Boolean
, Loc
),
4220 New_Occurrence_Of
(Standard_False
, Loc
));
4222 Prepend_To
(Declarations
(Func_Bod
), Flag_Decl
);
4223 Analyze
(Flag_Decl
);
4227 -- Build a simple_return_statement that returns the return object when
4228 -- there is a statement sequence, or no expression, or the result will
4229 -- be built in place. Note however that we currently do this for all
4230 -- composite cases, even though nonlimited composite results are not yet
4231 -- built in place (though we plan to do so eventually).
4234 or else Is_Composite_Type
(Result_Subt
)
4240 -- If the extended return has a handled statement sequence, then wrap
4241 -- it in a block and use the block as the first statement.
4245 Make_Block_Statement
(Loc
,
4246 Declarations
=> New_List
,
4247 Handled_Statement_Sequence
=> HSS
));
4250 -- If the result type contains tasks, we call Move_Activation_Chain.
4251 -- Later, the cleanup code will call Complete_Master, which will
4252 -- terminate any unactivated tasks belonging to the return statement
4253 -- master. But Move_Activation_Chain updates their master to be that
4254 -- of the caller, so they will not be terminated unless the return
4255 -- statement completes unsuccessfully due to exception, abort, goto,
4256 -- or exit. As a formality, we test whether the function requires the
4257 -- result to be built in place, though that's necessarily true for
4258 -- the case of result types with task parts.
4260 if Is_Build_In_Place
4261 and then Has_Task
(Result_Subt
)
4263 -- The return expression is an aggregate for a complex type which
4264 -- contains tasks. This particular case is left unexpanded since
4265 -- the regular expansion would insert all temporaries and
4266 -- initialization code in the wrong block.
4268 if Nkind
(Exp
) = N_Aggregate
then
4269 Expand_N_Aggregate
(Exp
);
4272 -- Do not move the activation chain if the return object does not
4275 if Has_Task
(Etype
(Ret_Obj_Id
)) then
4276 Append_To
(Stmts
, Move_Activation_Chain
);
4280 -- Update the state of the function right before the object is
4283 if Is_Build_In_Place
4284 and then Needs_Finalization
(Etype
(Ret_Obj_Id
))
4287 Flag_Id
: constant Entity_Id
:=
4288 Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
);
4295 Make_Assignment_Statement
(Loc
,
4296 Name
=> New_Occurrence_Of
(Flag_Id
, Loc
),
4297 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)));
4301 -- Build a simple_return_statement that returns the return object
4304 Make_Simple_Return_Statement
(Loc
,
4305 Expression
=> New_Occurrence_Of
(Ret_Obj_Id
, Loc
));
4306 Append_To
(Stmts
, Return_Stmt
);
4308 HSS
:= Make_Handled_Sequence_Of_Statements
(Loc
, Stmts
);
4311 -- Case where we build a return statement block
4313 if Present
(HSS
) then
4315 Make_Block_Statement
(Loc
,
4316 Declarations
=> Return_Object_Declarations
(N
),
4317 Handled_Statement_Sequence
=> HSS
);
4319 -- We set the entity of the new block statement to be that of the
4320 -- return statement. This is necessary so that various fields, such
4321 -- as Finalization_Chain_Entity carry over from the return statement
4322 -- to the block. Note that this block is unusual, in that its entity
4323 -- is an E_Return_Statement rather than an E_Block.
4326 (Result
, New_Occurrence_Of
(Return_Statement_Entity
(N
), Loc
));
4328 -- If the object decl was already rewritten as a renaming, then we
4329 -- don't want to do the object allocation and transformation of of
4330 -- the return object declaration to a renaming. This case occurs
4331 -- when the return object is initialized by a call to another
4332 -- build-in-place function, and that function is responsible for
4333 -- the allocation of the return object.
4335 if Is_Build_In_Place
4336 and then Nkind
(Ret_Obj_Decl
) = N_Object_Renaming_Declaration
4339 (Nkind
(Original_Node
(Ret_Obj_Decl
)) = N_Object_Declaration
4340 and then Is_Build_In_Place_Function_Call
4341 (Expression
(Original_Node
(Ret_Obj_Decl
))));
4343 -- Return the build-in-place result by reference
4345 Set_By_Ref
(Return_Stmt
);
4347 elsif Is_Build_In_Place
then
4349 -- Locate the implicit access parameter associated with the
4350 -- caller-supplied return object and convert the return
4351 -- statement's return object declaration to a renaming of a
4352 -- dereference of the access parameter. If the return object's
4353 -- declaration includes an expression that has not already been
4354 -- expanded as separate assignments, then add an assignment
4355 -- statement to ensure the return object gets initialized.
4358 -- Result : T [:= <expression>];
4365 -- Result : T renames FuncRA.all;
4366 -- [Result := <expression;]
4371 Return_Obj_Id
: constant Entity_Id
:=
4372 Defining_Identifier
(Ret_Obj_Decl
);
4373 Return_Obj_Typ
: constant Entity_Id
:= Etype
(Return_Obj_Id
);
4374 Return_Obj_Expr
: constant Node_Id
:=
4375 Expression
(Ret_Obj_Decl
);
4376 Constr_Result
: constant Boolean :=
4377 Is_Constrained
(Result_Subt
);
4378 Obj_Alloc_Formal
: Entity_Id
;
4379 Object_Access
: Entity_Id
;
4380 Obj_Acc_Deref
: Node_Id
;
4381 Init_Assignment
: Node_Id
:= Empty
;
4384 -- Build-in-place results must be returned by reference
4386 Set_By_Ref
(Return_Stmt
);
4388 -- Retrieve the implicit access parameter passed by the caller
4391 Build_In_Place_Formal
(Par_Func
, BIP_Object_Access
);
4393 -- If the return object's declaration includes an expression
4394 -- and the declaration isn't marked as No_Initialization, then
4395 -- we need to generate an assignment to the object and insert
4396 -- it after the declaration before rewriting it as a renaming
4397 -- (otherwise we'll lose the initialization). The case where
4398 -- the result type is an interface (or class-wide interface)
4399 -- is also excluded because the context of the function call
4400 -- must be unconstrained, so the initialization will always
4401 -- be done as part of an allocator evaluation (storage pool
4402 -- or secondary stack), never to a constrained target object
4403 -- passed in by the caller. Besides the assignment being
4404 -- unneeded in this case, it avoids problems with trying to
4405 -- generate a dispatching assignment when the return expression
4406 -- is a nonlimited descendant of a limited interface (the
4407 -- interface has no assignment operation).
4409 if Present
(Return_Obj_Expr
)
4410 and then not No_Initialization
(Ret_Obj_Decl
)
4411 and then not Is_Interface
(Return_Obj_Typ
)
4414 Make_Assignment_Statement
(Loc
,
4415 Name
=> New_Occurrence_Of
(Return_Obj_Id
, Loc
),
4416 Expression
=> Relocate_Node
(Return_Obj_Expr
));
4418 Set_Etype
(Name
(Init_Assignment
), Etype
(Return_Obj_Id
));
4419 Set_Assignment_OK
(Name
(Init_Assignment
));
4420 Set_No_Ctrl_Actions
(Init_Assignment
);
4422 Set_Parent
(Name
(Init_Assignment
), Init_Assignment
);
4423 Set_Parent
(Expression
(Init_Assignment
), Init_Assignment
);
4425 Set_Expression
(Ret_Obj_Decl
, Empty
);
4427 if Is_Class_Wide_Type
(Etype
(Return_Obj_Id
))
4428 and then not Is_Class_Wide_Type
4429 (Etype
(Expression
(Init_Assignment
)))
4431 Rewrite
(Expression
(Init_Assignment
),
4432 Make_Type_Conversion
(Loc
,
4434 New_Occurrence_Of
(Etype
(Return_Obj_Id
), Loc
),
4436 Relocate_Node
(Expression
(Init_Assignment
))));
4439 -- In the case of functions where the calling context can
4440 -- determine the form of allocation needed, initialization
4441 -- is done with each part of the if statement that handles
4442 -- the different forms of allocation (this is true for
4443 -- unconstrained and tagged result subtypes).
4446 and then not Is_Tagged_Type
(Underlying_Type
(Result_Subt
))
4448 Insert_After
(Ret_Obj_Decl
, Init_Assignment
);
4452 -- When the function's subtype is unconstrained, a run-time
4453 -- test is needed to determine the form of allocation to use
4454 -- for the return object. The function has an implicit formal
4455 -- parameter indicating this. If the BIP_Alloc_Form formal has
4456 -- the value one, then the caller has passed access to an
4457 -- existing object for use as the return object. If the value
4458 -- is two, then the return object must be allocated on the
4459 -- secondary stack. Otherwise, the object must be allocated in
4460 -- a storage pool (currently only supported for the global
4461 -- heap, user-defined storage pools TBD ???). We generate an
4462 -- if statement to test the implicit allocation formal and
4463 -- initialize a local access value appropriately, creating
4464 -- allocators in the secondary stack and global heap cases.
4465 -- The special formal also exists and must be tested when the
4466 -- function has a tagged result, even when the result subtype
4467 -- is constrained, because in general such functions can be
4468 -- called in dispatching contexts and must be handled similarly
4469 -- to functions with a class-wide result.
4471 if not Constr_Result
4472 or else Is_Tagged_Type
(Underlying_Type
(Result_Subt
))
4475 Build_In_Place_Formal
(Par_Func
, BIP_Alloc_Form
);
4478 Pool_Id
: constant Entity_Id
:=
4479 Make_Temporary
(Loc
, 'P');
4480 Alloc_Obj_Id
: Entity_Id
;
4481 Alloc_Obj_Decl
: Node_Id
;
4482 Alloc_If_Stmt
: Node_Id
;
4483 Heap_Allocator
: Node_Id
;
4484 Pool_Decl
: Node_Id
;
4485 Pool_Allocator
: Node_Id
;
4486 Ptr_Type_Decl
: Node_Id
;
4487 Ref_Type
: Entity_Id
;
4488 SS_Allocator
: Node_Id
;
4491 -- Reuse the itype created for the function's implicit
4492 -- access formal. This avoids the need to create a new
4493 -- access type here, plus it allows assigning the access
4494 -- formal directly without applying a conversion.
4496 -- Ref_Type := Etype (Object_Access);
4498 -- Create an access type designating the function's
4501 Ref_Type
:= Make_Temporary
(Loc
, 'A');
4504 Make_Full_Type_Declaration
(Loc
,
4505 Defining_Identifier
=> Ref_Type
,
4507 Make_Access_To_Object_Definition
(Loc
,
4508 All_Present
=> True,
4509 Subtype_Indication
=>
4510 New_Occurrence_Of
(Return_Obj_Typ
, Loc
)));
4512 Insert_Before
(Ret_Obj_Decl
, Ptr_Type_Decl
);
4514 -- Create an access object that will be initialized to an
4515 -- access value denoting the return object, either coming
4516 -- from an implicit access value passed in by the caller
4517 -- or from the result of an allocator.
4519 Alloc_Obj_Id
:= Make_Temporary
(Loc
, 'R');
4520 Set_Etype
(Alloc_Obj_Id
, Ref_Type
);
4523 Make_Object_Declaration
(Loc
,
4524 Defining_Identifier
=> Alloc_Obj_Id
,
4525 Object_Definition
=>
4526 New_Occurrence_Of
(Ref_Type
, Loc
));
4528 Insert_Before
(Ret_Obj_Decl
, Alloc_Obj_Decl
);
4530 -- Create allocators for both the secondary stack and
4531 -- global heap. If there's an initialization expression,
4532 -- then create these as initialized allocators.
4534 if Present
(Return_Obj_Expr
)
4535 and then not No_Initialization
(Ret_Obj_Decl
)
4537 -- Always use the type of the expression for the
4538 -- qualified expression, rather than the result type.
4539 -- In general we cannot always use the result type
4540 -- for the allocator, because the expression might be
4541 -- of a specific type, such as in the case of an
4542 -- aggregate or even a nonlimited object when the
4543 -- result type is a limited class-wide interface type.
4546 Make_Allocator
(Loc
,
4548 Make_Qualified_Expression
(Loc
,
4551 (Etype
(Return_Obj_Expr
), Loc
),
4553 New_Copy_Tree
(Return_Obj_Expr
)));
4556 -- If the function returns a class-wide type we cannot
4557 -- use the return type for the allocator. Instead we
4558 -- use the type of the expression, which must be an
4559 -- aggregate of a definite type.
4561 if Is_Class_Wide_Type
(Return_Obj_Typ
) then
4563 Make_Allocator
(Loc
,
4566 (Etype
(Return_Obj_Expr
), Loc
));
4569 Make_Allocator
(Loc
,
4571 New_Occurrence_Of
(Return_Obj_Typ
, Loc
));
4574 -- If the object requires default initialization then
4575 -- that will happen later following the elaboration of
4576 -- the object renaming. If we don't turn it off here
4577 -- then the object will be default initialized twice.
4579 Set_No_Initialization
(Heap_Allocator
);
4582 -- The Pool_Allocator is just like the Heap_Allocator,
4583 -- except we set Storage_Pool and Procedure_To_Call so
4584 -- it will use the user-defined storage pool.
4586 Pool_Allocator
:= New_Copy_Tree
(Heap_Allocator
);
4588 -- Do not generate the renaming of the build-in-place
4589 -- pool parameter on .NET/JVM/ZFP because the parameter
4590 -- is not created in the first place.
4592 if VM_Target
= No_VM
4593 and then RTE_Available
(RE_Root_Storage_Pool_Ptr
)
4596 Make_Object_Renaming_Declaration
(Loc
,
4597 Defining_Identifier
=> Pool_Id
,
4600 (RTE
(RE_Root_Storage_Pool
), Loc
),
4602 Make_Explicit_Dereference
(Loc
,
4604 (Build_In_Place_Formal
4605 (Par_Func
, BIP_Storage_Pool
), Loc
)));
4606 Set_Storage_Pool
(Pool_Allocator
, Pool_Id
);
4607 Set_Procedure_To_Call
4608 (Pool_Allocator
, RTE
(RE_Allocate_Any
));
4610 Pool_Decl
:= Make_Null_Statement
(Loc
);
4613 -- If the No_Allocators restriction is active, then only
4614 -- an allocator for secondary stack allocation is needed.
4615 -- It's OK for such allocators to have Comes_From_Source
4616 -- set to False, because gigi knows not to flag them as
4617 -- being a violation of No_Implicit_Heap_Allocations.
4619 if Restriction_Active
(No_Allocators
) then
4620 SS_Allocator
:= Heap_Allocator
;
4621 Heap_Allocator
:= Make_Null
(Loc
);
4622 Pool_Allocator
:= Make_Null
(Loc
);
4624 -- Otherwise the heap and pool allocators may be needed,
4625 -- so we make another allocator for secondary stack
4629 SS_Allocator
:= New_Copy_Tree
(Heap_Allocator
);
4631 -- The heap and pool allocators are marked as
4632 -- Comes_From_Source since they correspond to an
4633 -- explicit user-written allocator (that is, it will
4634 -- only be executed on behalf of callers that call the
4635 -- function as initialization for such an allocator).
4636 -- Prevents errors when No_Implicit_Heap_Allocations
4639 Set_Comes_From_Source
(Heap_Allocator
, True);
4640 Set_Comes_From_Source
(Pool_Allocator
, True);
4643 -- The allocator is returned on the secondary stack. We
4644 -- don't do this on VM targets, since the SS is not used.
4646 if VM_Target
= No_VM
then
4647 Set_Storage_Pool
(SS_Allocator
, RTE
(RE_SS_Pool
));
4648 Set_Procedure_To_Call
4649 (SS_Allocator
, RTE
(RE_SS_Allocate
));
4651 -- The allocator is returned on the secondary stack,
4652 -- so indicate that the function return, as well as
4653 -- the block that encloses the allocator, must not
4654 -- release it. The flags must be set now because
4655 -- the decision to use the secondary stack is done
4656 -- very late in the course of expanding the return
4657 -- statement, past the point where these flags are
4660 Set_Sec_Stack_Needed_For_Return
(Par_Func
);
4661 Set_Sec_Stack_Needed_For_Return
4662 (Return_Statement_Entity
(N
));
4663 Set_Uses_Sec_Stack
(Par_Func
);
4664 Set_Uses_Sec_Stack
(Return_Statement_Entity
(N
));
4667 -- Create an if statement to test the BIP_Alloc_Form
4668 -- formal and initialize the access object to either the
4669 -- BIP_Object_Access formal (BIP_Alloc_Form =
4670 -- Caller_Allocation), the result of allocating the
4671 -- object in the secondary stack (BIP_Alloc_Form =
4672 -- Secondary_Stack), or else an allocator to create the
4673 -- return object in the heap or user-defined pool
4674 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
4676 -- ??? An unchecked type conversion must be made in the
4677 -- case of assigning the access object formal to the
4678 -- local access object, because a normal conversion would
4679 -- be illegal in some cases (such as converting access-
4680 -- to-unconstrained to access-to-constrained), but the
4681 -- the unchecked conversion will presumably fail to work
4682 -- right in just such cases. It's not clear at all how to
4686 Make_If_Statement
(Loc
,
4690 New_Occurrence_Of
(Obj_Alloc_Formal
, Loc
),
4692 Make_Integer_Literal
(Loc
,
4693 UI_From_Int
(BIP_Allocation_Form
'Pos
4694 (Caller_Allocation
)))),
4696 Then_Statements
=> New_List
(
4697 Make_Assignment_Statement
(Loc
,
4699 New_Occurrence_Of
(Alloc_Obj_Id
, Loc
),
4701 Make_Unchecked_Type_Conversion
(Loc
,
4703 New_Occurrence_Of
(Ref_Type
, Loc
),
4705 New_Occurrence_Of
(Object_Access
, Loc
)))),
4707 Elsif_Parts
=> New_List
(
4708 Make_Elsif_Part
(Loc
,
4712 New_Occurrence_Of
(Obj_Alloc_Formal
, Loc
),
4714 Make_Integer_Literal
(Loc
,
4715 UI_From_Int
(BIP_Allocation_Form
'Pos
4716 (Secondary_Stack
)))),
4718 Then_Statements
=> New_List
(
4719 Make_Assignment_Statement
(Loc
,
4721 New_Occurrence_Of
(Alloc_Obj_Id
, Loc
),
4722 Expression
=> SS_Allocator
))),
4724 Make_Elsif_Part
(Loc
,
4728 New_Occurrence_Of
(Obj_Alloc_Formal
, Loc
),
4730 Make_Integer_Literal
(Loc
,
4731 UI_From_Int
(BIP_Allocation_Form
'Pos
4734 Then_Statements
=> New_List
(
4735 Build_Heap_Allocator
4736 (Temp_Id
=> Alloc_Obj_Id
,
4737 Temp_Typ
=> Ref_Type
,
4738 Func_Id
=> Par_Func
,
4739 Ret_Typ
=> Return_Obj_Typ
,
4740 Alloc_Expr
=> Heap_Allocator
)))),
4742 Else_Statements
=> New_List
(
4744 Build_Heap_Allocator
4745 (Temp_Id
=> Alloc_Obj_Id
,
4746 Temp_Typ
=> Ref_Type
,
4747 Func_Id
=> Par_Func
,
4748 Ret_Typ
=> Return_Obj_Typ
,
4749 Alloc_Expr
=> Pool_Allocator
)));
4751 -- If a separate initialization assignment was created
4752 -- earlier, append that following the assignment of the
4753 -- implicit access formal to the access object, to ensure
4754 -- that the return object is initialized in that case. In
4755 -- this situation, the target of the assignment must be
4756 -- rewritten to denote a dereference of the access to the
4757 -- return object passed in by the caller.
4759 if Present
(Init_Assignment
) then
4760 Rewrite
(Name
(Init_Assignment
),
4761 Make_Explicit_Dereference
(Loc
,
4762 Prefix
=> New_Occurrence_Of
(Alloc_Obj_Id
, Loc
)));
4765 (Name
(Init_Assignment
), Etype
(Return_Obj_Id
));
4768 (Then_Statements
(Alloc_If_Stmt
), Init_Assignment
);
4771 Insert_Before
(Ret_Obj_Decl
, Alloc_If_Stmt
);
4773 -- Remember the local access object for use in the
4774 -- dereference of the renaming created below.
4776 Object_Access
:= Alloc_Obj_Id
;
4780 -- Replace the return object declaration with a renaming of a
4781 -- dereference of the access value designating the return
4785 Make_Explicit_Dereference
(Loc
,
4786 Prefix
=> New_Occurrence_Of
(Object_Access
, Loc
));
4788 Rewrite
(Ret_Obj_Decl
,
4789 Make_Object_Renaming_Declaration
(Loc
,
4790 Defining_Identifier
=> Return_Obj_Id
,
4791 Access_Definition
=> Empty
,
4793 New_Occurrence_Of
(Return_Obj_Typ
, Loc
),
4794 Name
=> Obj_Acc_Deref
));
4796 Set_Renamed_Object
(Return_Obj_Id
, Obj_Acc_Deref
);
4800 -- Case where we do not build a block
4803 -- We're about to drop Return_Object_Declarations on the floor, so
4804 -- we need to insert it, in case it got expanded into useful code.
4805 -- Remove side effects from expression, which may be duplicated in
4806 -- subsequent checks (see Expand_Simple_Function_Return).
4808 Insert_List_Before
(N
, Return_Object_Declarations
(N
));
4809 Remove_Side_Effects
(Exp
);
4811 -- Build simple_return_statement that returns the expression directly
4813 Return_Stmt
:= Make_Simple_Return_Statement
(Loc
, Expression
=> Exp
);
4814 Result
:= Return_Stmt
;
4817 -- Set the flag to prevent infinite recursion
4819 Set_Comes_From_Extended_Return_Statement
(Return_Stmt
);
4821 Rewrite
(N
, Result
);
4823 end Expand_N_Extended_Return_Statement
;
4825 ----------------------------
4826 -- Expand_N_Function_Call --
4827 ----------------------------
4829 procedure Expand_N_Function_Call
(N
: Node_Id
) is
4832 end Expand_N_Function_Call
;
4834 ---------------------------------------
4835 -- Expand_N_Procedure_Call_Statement --
4836 ---------------------------------------
4838 procedure Expand_N_Procedure_Call_Statement
(N
: Node_Id
) is
4841 end Expand_N_Procedure_Call_Statement
;
4843 --------------------------------------
4844 -- Expand_N_Simple_Return_Statement --
4845 --------------------------------------
4847 procedure Expand_N_Simple_Return_Statement
(N
: Node_Id
) is
4849 -- Defend against previous errors (i.e. the return statement calls a
4850 -- function that is not available in configurable runtime).
4852 if Present
(Expression
(N
))
4853 and then Nkind
(Expression
(N
)) = N_Empty
4855 Check_Error_Detected
;
4859 -- Distinguish the function and non-function cases:
4861 case Ekind
(Return_Applies_To
(Return_Statement_Entity
(N
))) is
4864 E_Generic_Function
=>
4865 Expand_Simple_Function_Return
(N
);
4868 E_Generic_Procedure |
4871 E_Return_Statement
=>
4872 Expand_Non_Function_Return
(N
);
4875 raise Program_Error
;
4879 when RE_Not_Available
=>
4881 end Expand_N_Simple_Return_Statement
;
4883 ------------------------------
4884 -- Expand_N_Subprogram_Body --
4885 ------------------------------
4887 -- Add poll call if ATC polling is enabled, unless the body will be inlined
4890 -- Add dummy push/pop label nodes at start and end to clear any local
4891 -- exception indications if local-exception-to-goto optimization is active.
4893 -- Add return statement if last statement in body is not a return statement
4894 -- (this makes things easier on Gigi which does not want to have to handle
4895 -- a missing return).
4897 -- Add call to Activate_Tasks if body is a task activator
4899 -- Deal with possible detection of infinite recursion
4901 -- Eliminate body completely if convention stubbed
4903 -- Encode entity names within body, since we will not need to reference
4904 -- these entities any longer in the front end.
4906 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
4908 -- Reset Pure indication if any parameter has root type System.Address
4909 -- or has any parameters of limited types, where limited means that the
4910 -- run-time view is limited (i.e. the full type is limited).
4914 procedure Expand_N_Subprogram_Body
(N
: Node_Id
) is
4915 Loc
: constant Source_Ptr
:= Sloc
(N
);
4916 H
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
4917 Body_Id
: Entity_Id
;
4920 Spec_Id
: Entity_Id
;
4922 procedure Add_Return
(S
: List_Id
);
4923 -- Append a return statement to the statement sequence S if the last
4924 -- statement is not already a return or a goto statement. Note that
4925 -- the latter test is not critical, it does not matter if we add a few
4926 -- extra returns, since they get eliminated anyway later on.
4932 procedure Add_Return
(S
: List_Id
) is
4937 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
4938 -- not relevant in this context since they are not executable.
4940 Last_Stm
:= Last
(S
);
4941 while Nkind
(Last_Stm
) in N_Pop_xxx_Label
loop
4945 -- Now insert return unless last statement is a transfer
4947 if not Is_Transfer
(Last_Stm
) then
4949 -- The source location for the return is the end label of the
4950 -- procedure if present. Otherwise use the sloc of the last
4951 -- statement in the list. If the list comes from a generated
4952 -- exception handler and we are not debugging generated code,
4953 -- all the statements within the handler are made invisible
4956 if Nkind
(Parent
(S
)) = N_Exception_Handler
4957 and then not Comes_From_Source
(Parent
(S
))
4959 Loc
:= Sloc
(Last_Stm
);
4960 elsif Present
(End_Label
(H
)) then
4961 Loc
:= Sloc
(End_Label
(H
));
4963 Loc
:= Sloc
(Last_Stm
);
4967 Rtn
: constant Node_Id
:= Make_Simple_Return_Statement
(Loc
);
4970 -- Append return statement, and set analyzed manually. We can't
4971 -- call Analyze on this return since the scope is wrong.
4973 -- Note: it almost works to push the scope and then do the
4974 -- Analyze call, but something goes wrong in some weird cases
4975 -- and it is not worth worrying about ???
4977 -- The return statement is handled properly, and the call
4978 -- to the postcondition, inserted below, does not require
4979 -- information from the body either. However, that call is
4980 -- analyzed in the enclosing scope, and an elaboration check
4981 -- might improperly be added to it. A guard in Sem_Elab is
4982 -- needed to prevent that spurious check, see Check_Elab_Call.
4987 -- Call _Postconditions procedure if appropriate. We need to
4988 -- do this explicitly because we did not analyze the generated
4989 -- return statement above, so the call did not get inserted.
4991 if Ekind
(Spec_Id
) = E_Procedure
4992 and then Has_Postconditions
(Spec_Id
)
4994 pragma Assert
(Present
(Postcondition_Proc
(Spec_Id
)));
4996 Make_Procedure_Call_Statement
(Loc
,
4999 (Postcondition_Proc
(Spec_Id
), Loc
)));
5005 -- Start of processing for Expand_N_Subprogram_Body
5008 -- Set L to either the list of declarations if present, or to the list
5009 -- of statements if no declarations are present. This is used to insert
5010 -- new stuff at the start.
5012 if Is_Non_Empty_List
(Declarations
(N
)) then
5013 L
:= Declarations
(N
);
5015 L
:= Statements
(H
);
5018 -- If local-exception-to-goto optimization active, insert dummy push
5019 -- statements at start, and dummy pop statements at end, but inhibit
5020 -- this if we have No_Exception_Handlers, since they are useless and
5021 -- intefere with analysis, e.g. by codepeer.
5023 if (Debug_Flag_Dot_G
5024 or else Restriction_Active
(No_Exception_Propagation
))
5025 and then not Restriction_Active
(No_Exception_Handlers
)
5026 and then not CodePeer_Mode
5027 and then Is_Non_Empty_List
(L
)
5030 FS
: constant Node_Id
:= First
(L
);
5031 FL
: constant Source_Ptr
:= Sloc
(FS
);
5036 -- LS points to either last statement, if statements are present
5037 -- or to the last declaration if there are no statements present.
5038 -- It is the node after which the pop's are generated.
5040 if Is_Non_Empty_List
(Statements
(H
)) then
5041 LS
:= Last
(Statements
(H
));
5048 Insert_List_Before_And_Analyze
(FS
, New_List
(
5049 Make_Push_Constraint_Error_Label
(FL
),
5050 Make_Push_Program_Error_Label
(FL
),
5051 Make_Push_Storage_Error_Label
(FL
)));
5053 Insert_List_After_And_Analyze
(LS
, New_List
(
5054 Make_Pop_Constraint_Error_Label
(LL
),
5055 Make_Pop_Program_Error_Label
(LL
),
5056 Make_Pop_Storage_Error_Label
(LL
)));
5060 -- Find entity for subprogram
5062 Body_Id
:= Defining_Entity
(N
);
5064 if Present
(Corresponding_Spec
(N
)) then
5065 Spec_Id
:= Corresponding_Spec
(N
);
5070 -- Need poll on entry to subprogram if polling enabled. We only do this
5071 -- for non-empty subprograms, since it does not seem necessary to poll
5072 -- for a dummy null subprogram.
5074 if Is_Non_Empty_List
(L
) then
5076 -- Do not add a polling call if the subprogram is to be inlined by
5077 -- the back-end, to avoid repeated calls with multiple inlinings.
5079 if Is_Inlined
(Spec_Id
)
5080 and then Front_End_Inlining
5081 and then Optimization_Level
> 1
5085 Generate_Poll_Call
(First
(L
));
5089 -- If this is a Pure function which has any parameters whose root type
5090 -- is System.Address, reset the Pure indication, since it will likely
5091 -- cause incorrect code to be generated as the parameter is probably
5092 -- a pointer, and the fact that the same pointer is passed does not mean
5093 -- that the same value is being referenced.
5095 -- Note that if the programmer gave an explicit Pure_Function pragma,
5096 -- then we believe the programmer, and leave the subprogram Pure.
5098 -- This code should probably be at the freeze point, so that it happens
5099 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
5100 -- semantic tree has Is_Pure set properly ???
5102 if Is_Pure
(Spec_Id
)
5103 and then Is_Subprogram
(Spec_Id
)
5104 and then not Has_Pragma_Pure_Function
(Spec_Id
)
5110 F
:= First_Formal
(Spec_Id
);
5111 while Present
(F
) loop
5112 if Is_Descendent_Of_Address
(Etype
(F
))
5114 -- Note that this test is being made in the body of the
5115 -- subprogram, not the spec, so we are testing the full
5116 -- type for being limited here, as required.
5118 or else Is_Limited_Type
(Etype
(F
))
5120 Set_Is_Pure
(Spec_Id
, False);
5122 if Spec_Id
/= Body_Id
then
5123 Set_Is_Pure
(Body_Id
, False);
5134 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5136 if Init_Or_Norm_Scalars
and then Is_Subprogram
(Spec_Id
) then
5142 -- Loop through formals
5144 F
:= First_Formal
(Spec_Id
);
5145 while Present
(F
) loop
5146 if Is_Scalar_Type
(Etype
(F
))
5147 and then Ekind
(F
) = E_Out_Parameter
5149 Check_Restriction
(No_Default_Initialization
, F
);
5151 -- Insert the initialization. We turn off validity checks
5152 -- for this assignment, since we do not want any check on
5153 -- the initial value itself (which may well be invalid).
5154 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5156 A
:= Make_Assignment_Statement
(Loc
,
5157 Name
=> New_Occurrence_Of
(F
, Loc
),
5158 Expression
=> Get_Simple_Init_Val
(Etype
(F
), N
));
5159 Set_Suppress_Assignment_Checks
(A
);
5161 Insert_Before_And_Analyze
(First
(L
),
5162 A
, Suppress
=> Validity_Check
);
5170 -- Clear out statement list for stubbed procedure
5172 if Present
(Corresponding_Spec
(N
)) then
5173 Set_Elaboration_Flag
(N
, Spec_Id
);
5175 if Convention
(Spec_Id
) = Convention_Stubbed
5176 or else Is_Eliminated
(Spec_Id
)
5178 Set_Declarations
(N
, Empty_List
);
5179 Set_Handled_Statement_Sequence
(N
,
5180 Make_Handled_Sequence_Of_Statements
(Loc
,
5181 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
5186 -- Create a set of discriminals for the next protected subprogram body
5188 if Is_List_Member
(N
)
5189 and then Present
(Parent
(List_Containing
(N
)))
5190 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
5191 and then Present
(Next_Protected_Operation
(N
))
5193 Set_Discriminals
(Parent
(Base_Type
(Scope
(Spec_Id
))));
5196 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5197 -- subprograms with no specs are not frozen.
5200 Typ
: constant Entity_Id
:= Etype
(Spec_Id
);
5201 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
5204 if not Acts_As_Spec
(N
)
5205 and then Nkind
(Parent
(Parent
(Spec_Id
))) /=
5206 N_Subprogram_Body_Stub
5210 elsif Is_Limited_View
(Typ
) then
5211 Set_Returns_By_Ref
(Spec_Id
);
5213 elsif Present
(Utyp
) and then CW_Or_Has_Controlled_Part
(Utyp
) then
5214 Set_Returns_By_Ref
(Spec_Id
);
5218 -- For a procedure, we add a return for all possible syntactic ends of
5221 if Ekind_In
(Spec_Id
, E_Procedure
, E_Generic_Procedure
) then
5222 Add_Return
(Statements
(H
));
5224 if Present
(Exception_Handlers
(H
)) then
5225 Except_H
:= First_Non_Pragma
(Exception_Handlers
(H
));
5226 while Present
(Except_H
) loop
5227 Add_Return
(Statements
(Except_H
));
5228 Next_Non_Pragma
(Except_H
);
5232 -- For a function, we must deal with the case where there is at least
5233 -- one missing return. What we do is to wrap the entire body of the
5234 -- function in a block:
5247 -- raise Program_Error;
5250 -- This approach is necessary because the raise must be signalled to the
5251 -- caller, not handled by any local handler (RM 6.4(11)).
5253 -- Note: we do not need to analyze the constructed sequence here, since
5254 -- it has no handler, and an attempt to analyze the handled statement
5255 -- sequence twice is risky in various ways (e.g. the issue of expanding
5256 -- cleanup actions twice).
5258 elsif Has_Missing_Return
(Spec_Id
) then
5260 Hloc
: constant Source_Ptr
:= Sloc
(H
);
5261 Blok
: constant Node_Id
:=
5262 Make_Block_Statement
(Hloc
,
5263 Handled_Statement_Sequence
=> H
);
5264 Rais
: constant Node_Id
:=
5265 Make_Raise_Program_Error
(Hloc
,
5266 Reason
=> PE_Missing_Return
);
5269 Set_Handled_Statement_Sequence
(N
,
5270 Make_Handled_Sequence_Of_Statements
(Hloc
,
5271 Statements
=> New_List
(Blok
, Rais
)));
5273 Push_Scope
(Spec_Id
);
5280 -- If subprogram contains a parameterless recursive call, then we may
5281 -- have an infinite recursion, so see if we can generate code to check
5282 -- for this possibility if storage checks are not suppressed.
5284 if Ekind
(Spec_Id
) = E_Procedure
5285 and then Has_Recursive_Call
(Spec_Id
)
5286 and then not Storage_Checks_Suppressed
(Spec_Id
)
5288 Detect_Infinite_Recursion
(N
, Spec_Id
);
5291 -- Set to encode entity names in package body before gigi is called
5293 Qualify_Entity_Names
(N
);
5294 end Expand_N_Subprogram_Body
;
5296 -----------------------------------
5297 -- Expand_N_Subprogram_Body_Stub --
5298 -----------------------------------
5300 procedure Expand_N_Subprogram_Body_Stub
(N
: Node_Id
) is
5302 if Present
(Corresponding_Body
(N
)) then
5303 Expand_N_Subprogram_Body
(
5304 Unit_Declaration_Node
(Corresponding_Body
(N
)));
5306 end Expand_N_Subprogram_Body_Stub
;
5308 -------------------------------------
5309 -- Expand_N_Subprogram_Declaration --
5310 -------------------------------------
5312 -- If the declaration appears within a protected body, it is a private
5313 -- operation of the protected type. We must create the corresponding
5314 -- protected subprogram an associated formals. For a normal protected
5315 -- operation, this is done when expanding the protected type declaration.
5317 -- If the declaration is for a null procedure, emit null body
5319 procedure Expand_N_Subprogram_Declaration
(N
: Node_Id
) is
5320 Loc
: constant Source_Ptr
:= Sloc
(N
);
5321 Subp
: constant Entity_Id
:= Defining_Entity
(N
);
5322 Scop
: constant Entity_Id
:= Scope
(Subp
);
5323 Prot_Decl
: Node_Id
;
5325 Prot_Id
: Entity_Id
;
5328 -- In SPARK, subprogram declarations are only allowed in package
5331 if Nkind
(Parent
(N
)) /= N_Package_Specification
then
5332 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5333 Check_SPARK_05_Restriction
5334 ("subprogram declaration is not a library item", N
);
5336 elsif Present
(Next
(N
))
5337 and then Nkind
(Next
(N
)) = N_Pragma
5338 and then Get_Pragma_Id
(Pragma_Name
(Next
(N
))) = Pragma_Import
5340 -- In SPARK, subprogram declarations are also permitted in
5341 -- declarative parts when immediately followed by a corresponding
5342 -- pragma Import. We only check here that there is some pragma
5347 Check_SPARK_05_Restriction
5348 ("subprogram declaration is not allowed here", N
);
5352 -- Deal with case of protected subprogram. Do not generate protected
5353 -- operation if operation is flagged as eliminated.
5355 if Is_List_Member
(N
)
5356 and then Present
(Parent
(List_Containing
(N
)))
5357 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
5358 and then Is_Protected_Type
(Scop
)
5360 if No
(Protected_Body_Subprogram
(Subp
))
5361 and then not Is_Eliminated
(Subp
)
5364 Make_Subprogram_Declaration
(Loc
,
5366 Build_Protected_Sub_Specification
5367 (N
, Scop
, Unprotected_Mode
));
5369 -- The protected subprogram is declared outside of the protected
5370 -- body. Given that the body has frozen all entities so far, we
5371 -- analyze the subprogram and perform freezing actions explicitly.
5372 -- including the generation of an explicit freeze node, to ensure
5373 -- that gigi has the proper order of elaboration.
5374 -- If the body is a subunit, the insertion point is before the
5375 -- stub in the parent.
5377 Prot_Bod
:= Parent
(List_Containing
(N
));
5379 if Nkind
(Parent
(Prot_Bod
)) = N_Subunit
then
5380 Prot_Bod
:= Corresponding_Stub
(Parent
(Prot_Bod
));
5383 Insert_Before
(Prot_Bod
, Prot_Decl
);
5384 Prot_Id
:= Defining_Unit_Name
(Specification
(Prot_Decl
));
5385 Set_Has_Delayed_Freeze
(Prot_Id
);
5387 Push_Scope
(Scope
(Scop
));
5388 Analyze
(Prot_Decl
);
5389 Freeze_Before
(N
, Prot_Id
);
5390 Set_Protected_Body_Subprogram
(Subp
, Prot_Id
);
5392 -- Create protected operation as well. Even though the operation
5393 -- is only accessible within the body, it is possible to make it
5394 -- available outside of the protected object by using 'Access to
5395 -- provide a callback, so build protected version in all cases.
5398 Make_Subprogram_Declaration
(Loc
,
5400 Build_Protected_Sub_Specification
(N
, Scop
, Protected_Mode
));
5401 Insert_Before
(Prot_Bod
, Prot_Decl
);
5402 Analyze
(Prot_Decl
);
5407 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
5408 -- cases this is superfluous because calls to it will be automatically
5409 -- inlined, but we definitely need the body if preconditions for the
5410 -- procedure are present.
5412 elsif Nkind
(Specification
(N
)) = N_Procedure_Specification
5413 and then Null_Present
(Specification
(N
))
5416 Bod
: constant Node_Id
:= Body_To_Inline
(N
);
5419 Set_Has_Completion
(Subp
, False);
5420 Append_Freeze_Action
(Subp
, Bod
);
5422 -- The body now contains raise statements, so calls to it will
5425 Set_Is_Inlined
(Subp
, False);
5428 end Expand_N_Subprogram_Declaration
;
5430 --------------------------------
5431 -- Expand_Non_Function_Return --
5432 --------------------------------
5434 procedure Expand_Non_Function_Return
(N
: Node_Id
) is
5435 pragma Assert
(No
(Expression
(N
)));
5437 Loc
: constant Source_Ptr
:= Sloc
(N
);
5438 Scope_Id
: Entity_Id
:=
5439 Return_Applies_To
(Return_Statement_Entity
(N
));
5440 Kind
: constant Entity_Kind
:= Ekind
(Scope_Id
);
5443 Goto_Stat
: Node_Id
;
5447 -- Call _Postconditions procedure if procedure with active
5448 -- postconditions. Here, we use the Postcondition_Proc attribute,
5449 -- which is needed for implicitly-generated returns. Functions
5450 -- never have implicitly-generated returns, and there's no
5451 -- room for Postcondition_Proc in E_Function, so we look up the
5452 -- identifier Name_uPostconditions for function returns (see
5453 -- Expand_Simple_Function_Return).
5455 if Ekind
(Scope_Id
) = E_Procedure
5456 and then Has_Postconditions
(Scope_Id
)
5458 pragma Assert
(Present
(Postcondition_Proc
(Scope_Id
)));
5460 Make_Procedure_Call_Statement
(Loc
,
5461 Name
=> New_Occurrence_Of
(Postcondition_Proc
(Scope_Id
), Loc
)));
5464 -- If it is a return from a procedure do no extra steps
5466 if Kind
= E_Procedure
or else Kind
= E_Generic_Procedure
then
5469 -- If it is a nested return within an extended one, replace it with a
5470 -- return of the previously declared return object.
5472 elsif Kind
= E_Return_Statement
then
5474 Make_Simple_Return_Statement
(Loc
,
5476 New_Occurrence_Of
(First_Entity
(Scope_Id
), Loc
)));
5477 Set_Comes_From_Extended_Return_Statement
(N
);
5478 Set_Return_Statement_Entity
(N
, Scope_Id
);
5479 Expand_Simple_Function_Return
(N
);
5483 pragma Assert
(Is_Entry
(Scope_Id
));
5485 -- Look at the enclosing block to see whether the return is from an
5486 -- accept statement or an entry body.
5488 for J
in reverse 0 .. Scope_Stack
.Last
loop
5489 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
5490 exit when Is_Concurrent_Type
(Scope_Id
);
5493 -- If it is a return from accept statement it is expanded as call to
5494 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
5496 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
5497 -- Expand_N_Accept_Alternative in exp_ch9.adb)
5499 if Is_Task_Type
(Scope_Id
) then
5502 Make_Procedure_Call_Statement
(Loc
,
5503 Name
=> New_Occurrence_Of
(RTE
(RE_Complete_Rendezvous
), Loc
));
5504 Insert_Before
(N
, Call
);
5505 -- why not insert actions here???
5508 Acc_Stat
:= Parent
(N
);
5509 while Nkind
(Acc_Stat
) /= N_Accept_Statement
loop
5510 Acc_Stat
:= Parent
(Acc_Stat
);
5513 Lab_Node
:= Last
(Statements
5514 (Handled_Statement_Sequence
(Acc_Stat
)));
5516 Goto_Stat
:= Make_Goto_Statement
(Loc
,
5517 Name
=> New_Occurrence_Of
5518 (Entity
(Identifier
(Lab_Node
)), Loc
));
5520 Set_Analyzed
(Goto_Stat
);
5522 Rewrite
(N
, Goto_Stat
);
5525 -- If it is a return from an entry body, put a Complete_Entry_Body call
5526 -- in front of the return.
5528 elsif Is_Protected_Type
(Scope_Id
) then
5530 Make_Procedure_Call_Statement
(Loc
,
5532 New_Occurrence_Of
(RTE
(RE_Complete_Entry_Body
), Loc
),
5533 Parameter_Associations
=> New_List
(
5534 Make_Attribute_Reference
(Loc
,
5537 (Find_Protection_Object
(Current_Scope
), Loc
),
5538 Attribute_Name
=> Name_Unchecked_Access
)));
5540 Insert_Before
(N
, Call
);
5543 end Expand_Non_Function_Return
;
5545 ---------------------------------------
5546 -- Expand_Protected_Object_Reference --
5547 ---------------------------------------
5549 function Expand_Protected_Object_Reference
5551 Scop
: Entity_Id
) return Node_Id
5553 Loc
: constant Source_Ptr
:= Sloc
(N
);
5560 Rec
:= Make_Identifier
(Loc
, Name_uObject
);
5561 Set_Etype
(Rec
, Corresponding_Record_Type
(Scop
));
5563 -- Find enclosing protected operation, and retrieve its first parameter,
5564 -- which denotes the enclosing protected object. If the enclosing
5565 -- operation is an entry, we are immediately within the protected body,
5566 -- and we can retrieve the object from the service entries procedure. A
5567 -- barrier function has the same signature as an entry. A barrier
5568 -- function is compiled within the protected object, but unlike
5569 -- protected operations its never needs locks, so that its protected
5570 -- body subprogram points to itself.
5572 Proc
:= Current_Scope
;
5573 while Present
(Proc
)
5574 and then Scope
(Proc
) /= Scop
5576 Proc
:= Scope
(Proc
);
5579 Corr
:= Protected_Body_Subprogram
(Proc
);
5583 -- Previous error left expansion incomplete.
5584 -- Nothing to do on this call.
5591 (First
(Parameter_Specifications
(Parent
(Corr
))));
5593 if Is_Subprogram
(Proc
) and then Proc
/= Corr
then
5595 -- Protected function or procedure
5597 Set_Entity
(Rec
, Param
);
5599 -- Rec is a reference to an entity which will not be in scope when
5600 -- the call is reanalyzed, and needs no further analysis.
5605 -- Entry or barrier function for entry body. The first parameter of
5606 -- the entry body procedure is pointer to the object. We create a
5607 -- local variable of the proper type, duplicating what is done to
5608 -- define _object later on.
5612 Obj_Ptr
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
5616 Make_Full_Type_Declaration
(Loc
,
5617 Defining_Identifier
=> Obj_Ptr
,
5619 Make_Access_To_Object_Definition
(Loc
,
5620 Subtype_Indication
=>
5622 (Corresponding_Record_Type
(Scop
), Loc
))));
5624 Insert_Actions
(N
, Decls
);
5625 Freeze_Before
(N
, Obj_Ptr
);
5628 Make_Explicit_Dereference
(Loc
,
5630 Unchecked_Convert_To
(Obj_Ptr
,
5631 New_Occurrence_Of
(Param
, Loc
)));
5633 -- Analyze new actual. Other actuals in calls are already analyzed
5634 -- and the list of actuals is not reanalyzed after rewriting.
5636 Set_Parent
(Rec
, N
);
5642 end Expand_Protected_Object_Reference
;
5644 --------------------------------------
5645 -- Expand_Protected_Subprogram_Call --
5646 --------------------------------------
5648 procedure Expand_Protected_Subprogram_Call
5655 procedure Freeze_Called_Function
;
5656 -- If it is a function call it can appear in elaboration code and
5657 -- the called entity must be frozen before the call. This must be
5658 -- done before the call is expanded, as the expansion may rewrite it
5659 -- to something other than a call (e.g. a temporary initialized in a
5660 -- transient block).
5662 ----------------------------
5663 -- Freeze_Called_Function --
5664 ----------------------------
5666 procedure Freeze_Called_Function
is
5668 if Ekind
(Subp
) = E_Function
then
5669 Freeze_Expression
(Name
(N
));
5671 end Freeze_Called_Function
;
5673 -- Start of processing for Expand_Protected_Subprogram_Call
5676 -- If the protected object is not an enclosing scope, this is an inter-
5677 -- object function call. Inter-object procedure calls are expanded by
5678 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
5679 -- subprogram being called is in the protected body being compiled, and
5680 -- if the protected object in the call is statically the enclosing type.
5681 -- The object may be an component of some other data structure, in which
5682 -- case this must be handled as an inter-object call.
5684 if not In_Open_Scopes
(Scop
)
5685 or else not Is_Entity_Name
(Name
(N
))
5687 if Nkind
(Name
(N
)) = N_Selected_Component
then
5688 Rec
:= Prefix
(Name
(N
));
5691 pragma Assert
(Nkind
(Name
(N
)) = N_Indexed_Component
);
5692 Rec
:= Prefix
(Prefix
(Name
(N
)));
5695 Freeze_Called_Function
;
5696 Build_Protected_Subprogram_Call
(N
,
5697 Name
=> New_Occurrence_Of
(Subp
, Sloc
(N
)),
5698 Rec
=> Convert_Concurrent
(Rec
, Etype
(Rec
)),
5702 Rec
:= Expand_Protected_Object_Reference
(N
, Scop
);
5708 Freeze_Called_Function
;
5709 Build_Protected_Subprogram_Call
(N
,
5716 -- Analyze and resolve the new call. The actuals have already been
5717 -- resolved, but expansion of a function call will add extra actuals
5718 -- if needed. Analysis of a procedure call already includes resolution.
5722 if Ekind
(Subp
) = E_Function
then
5723 Resolve
(N
, Etype
(Subp
));
5725 end Expand_Protected_Subprogram_Call
;
5727 --------------------------------------------
5728 -- Has_Unconstrained_Access_Discriminants --
5729 --------------------------------------------
5731 function Has_Unconstrained_Access_Discriminants
5732 (Subtyp
: Entity_Id
) return Boolean
5737 if Has_Discriminants
(Subtyp
)
5738 and then not Is_Constrained
(Subtyp
)
5740 Discr
:= First_Discriminant
(Subtyp
);
5741 while Present
(Discr
) loop
5742 if Ekind
(Etype
(Discr
)) = E_Anonymous_Access_Type
then
5746 Next_Discriminant
(Discr
);
5751 end Has_Unconstrained_Access_Discriminants
;
5753 -----------------------------------
5754 -- Expand_Simple_Function_Return --
5755 -----------------------------------
5757 -- The "simple" comes from the syntax rule simple_return_statement. The
5758 -- semantics are not at all simple.
5760 procedure Expand_Simple_Function_Return
(N
: Node_Id
) is
5761 Loc
: constant Source_Ptr
:= Sloc
(N
);
5763 Scope_Id
: constant Entity_Id
:=
5764 Return_Applies_To
(Return_Statement_Entity
(N
));
5765 -- The function we are returning from
5767 R_Type
: constant Entity_Id
:= Etype
(Scope_Id
);
5768 -- The result type of the function
5770 Utyp
: constant Entity_Id
:= Underlying_Type
(R_Type
);
5772 Exp
: constant Node_Id
:= Expression
(N
);
5773 pragma Assert
(Present
(Exp
));
5775 Exptyp
: constant Entity_Id
:= Etype
(Exp
);
5776 -- The type of the expression (not necessarily the same as R_Type)
5778 Subtype_Ind
: Node_Id
;
5779 -- If the result type of the function is class-wide and the expression
5780 -- has a specific type, then we use the expression's type as the type of
5781 -- the return object. In cases where the expression is an aggregate that
5782 -- is built in place, this avoids the need for an expensive conversion
5783 -- of the return object to the specific type on assignments to the
5784 -- individual components.
5787 if Is_Class_Wide_Type
(R_Type
)
5788 and then not Is_Class_Wide_Type
(Etype
(Exp
))
5790 Subtype_Ind
:= New_Occurrence_Of
(Etype
(Exp
), Loc
);
5792 Subtype_Ind
:= New_Occurrence_Of
(R_Type
, Loc
);
5795 -- For the case of a simple return that does not come from an extended
5796 -- return, in the case of Ada 2005 where we are returning a limited
5797 -- type, we rewrite "return <expression>;" to be:
5799 -- return _anon_ : <return_subtype> := <expression>
5801 -- The expansion produced by Expand_N_Extended_Return_Statement will
5802 -- contain simple return statements (for example, a block containing
5803 -- simple return of the return object), which brings us back here with
5804 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
5805 -- checking for a simple return that does not come from an extended
5806 -- return is to avoid this infinite recursion.
5808 -- The reason for this design is that for Ada 2005 limited returns, we
5809 -- need to reify the return object, so we can build it "in place", and
5810 -- we need a block statement to hang finalization and tasking stuff.
5812 -- ??? In order to avoid disruption, we avoid translating to extended
5813 -- return except in the cases where we really need to (Ada 2005 for
5814 -- inherently limited). We might prefer to do this translation in all
5815 -- cases (except perhaps for the case of Ada 95 inherently limited),
5816 -- in order to fully exercise the Expand_N_Extended_Return_Statement
5817 -- code. This would also allow us to do the build-in-place optimization
5818 -- for efficiency even in cases where it is semantically not required.
5820 -- As before, we check the type of the return expression rather than the
5821 -- return type of the function, because the latter may be a limited
5822 -- class-wide interface type, which is not a limited type, even though
5823 -- the type of the expression may be.
5825 if not Comes_From_Extended_Return_Statement
(N
)
5826 and then Is_Limited_View
(Etype
(Expression
(N
)))
5827 and then Ada_Version
>= Ada_2005
5828 and then not Debug_Flag_Dot_L
5830 -- The functionality of interface thunks is simple and it is always
5831 -- handled by means of simple return statements. This leaves their
5832 -- expansion simple and clean.
5834 and then not Is_Thunk
(Current_Scope
)
5837 Return_Object_Entity
: constant Entity_Id
:=
5838 Make_Temporary
(Loc
, 'R', Exp
);
5840 Obj_Decl
: constant Node_Id
:=
5841 Make_Object_Declaration
(Loc
,
5842 Defining_Identifier
=> Return_Object_Entity
,
5843 Object_Definition
=> Subtype_Ind
,
5846 Ext
: constant Node_Id
:=
5847 Make_Extended_Return_Statement
(Loc
,
5848 Return_Object_Declarations
=> New_List
(Obj_Decl
));
5849 -- Do not perform this high-level optimization if the result type
5850 -- is an interface because the "this" pointer must be displaced.
5859 -- Here we have a simple return statement that is part of the expansion
5860 -- of an extended return statement (either written by the user, or
5861 -- generated by the above code).
5863 -- Always normalize C/Fortran boolean result. This is not always needed,
5864 -- but it seems a good idea to minimize the passing around of non-
5865 -- normalized values, and in any case this handles the processing of
5866 -- barrier functions for protected types, which turn the condition into
5867 -- a return statement.
5869 if Is_Boolean_Type
(Exptyp
)
5870 and then Nonzero_Is_True
(Exptyp
)
5872 Adjust_Condition
(Exp
);
5873 Adjust_Result_Type
(Exp
, Exptyp
);
5876 -- Do validity check if enabled for returns
5878 if Validity_Checks_On
5879 and then Validity_Check_Returns
5884 -- Check the result expression of a scalar function against the subtype
5885 -- of the function by inserting a conversion. This conversion must
5886 -- eventually be performed for other classes of types, but for now it's
5887 -- only done for scalars.
5890 if Is_Scalar_Type
(Exptyp
) then
5891 Rewrite
(Exp
, Convert_To
(R_Type
, Exp
));
5893 -- The expression is resolved to ensure that the conversion gets
5894 -- expanded to generate a possible constraint check.
5896 Analyze_And_Resolve
(Exp
, R_Type
);
5899 -- Deal with returning variable length objects and controlled types
5901 -- Nothing to do if we are returning by reference, or this is not a
5902 -- type that requires special processing (indicated by the fact that
5903 -- it requires a cleanup scope for the secondary stack case).
5905 if Is_Limited_View
(Exptyp
)
5906 or else Is_Limited_Interface
(Exptyp
)
5910 -- No copy needed for thunks returning interface type objects since
5911 -- the object is returned by reference and the maximum functionality
5912 -- required is just to displace the pointer.
5914 elsif Is_Thunk
(Current_Scope
) and then Is_Interface
(Exptyp
) then
5917 elsif not Requires_Transient_Scope
(R_Type
) then
5919 -- Mutable records with no variable length components are not
5920 -- returned on the sec-stack, so we need to make sure that the
5921 -- backend will only copy back the size of the actual value, and not
5922 -- the maximum size. We create an actual subtype for this purpose.
5925 Ubt
: constant Entity_Id
:= Underlying_Type
(Base_Type
(Exptyp
));
5929 if Has_Discriminants
(Ubt
)
5930 and then not Is_Constrained
(Ubt
)
5931 and then not Has_Unchecked_Union
(Ubt
)
5933 Decl
:= Build_Actual_Subtype
(Ubt
, Exp
);
5934 Ent
:= Defining_Identifier
(Decl
);
5935 Insert_Action
(Exp
, Decl
);
5936 Rewrite
(Exp
, Unchecked_Convert_To
(Ent
, Exp
));
5937 Analyze_And_Resolve
(Exp
);
5941 -- Here if secondary stack is used
5944 -- Prevent the reclamation of the secondary stack by all enclosing
5945 -- blocks and loops as well as the related function, otherwise the
5946 -- result will be reclaimed too early or even clobbered. Due to a
5947 -- possible mix of internally generated blocks, source blocks and
5948 -- loops, the scope stack may not be contiguous as all labels are
5949 -- inserted at the top level within the related function. Instead,
5950 -- perform a parent-based traversal and mark all appropriate
5958 while Present
(P
) loop
5960 -- Mark the label of a source or internally generated block or
5963 if Nkind_In
(P
, N_Block_Statement
, N_Loop_Statement
) then
5964 Set_Sec_Stack_Needed_For_Return
(Entity
(Identifier
(P
)));
5966 -- Mark the enclosing function
5968 elsif Nkind
(P
) = N_Subprogram_Body
then
5969 if Present
(Corresponding_Spec
(P
)) then
5970 Set_Sec_Stack_Needed_For_Return
(Corresponding_Spec
(P
));
5972 Set_Sec_Stack_Needed_For_Return
(Defining_Entity
(P
));
5975 -- Do not go beyond the enclosing function
5984 -- Optimize the case where the result is a function call. In this
5985 -- case either the result is already on the secondary stack, or is
5986 -- already being returned with the stack pointer depressed and no
5987 -- further processing is required except to set the By_Ref flag
5988 -- to ensure that gigi does not attempt an extra unnecessary copy.
5989 -- (actually not just unnecessary but harmfully wrong in the case
5990 -- of a controlled type, where gigi does not know how to do a copy).
5991 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
5992 -- for array types if the constrained status of the target type is
5993 -- different from that of the expression.
5995 if Requires_Transient_Scope
(Exptyp
)
5997 (not Is_Array_Type
(Exptyp
)
5998 or else Is_Constrained
(Exptyp
) = Is_Constrained
(R_Type
)
5999 or else CW_Or_Has_Controlled_Part
(Utyp
))
6000 and then Nkind
(Exp
) = N_Function_Call
6004 -- Remove side effects from the expression now so that other parts
6005 -- of the expander do not have to reanalyze this node without this
6008 Rewrite
(Exp
, Duplicate_Subexpr_No_Checks
(Exp
));
6010 -- For controlled types, do the allocation on the secondary stack
6011 -- manually in order to call adjust at the right time:
6013 -- type Anon1 is access R_Type;
6014 -- for Anon1'Storage_pool use ss_pool;
6015 -- Anon2 : anon1 := new R_Type'(expr);
6016 -- return Anon2.all;
6018 -- We do the same for classwide types that are not potentially
6019 -- controlled (by the virtue of restriction No_Finalization) because
6020 -- gigi is not able to properly allocate class-wide types.
6022 elsif CW_Or_Has_Controlled_Part
(Utyp
) then
6024 Loc
: constant Source_Ptr
:= Sloc
(N
);
6025 Acc_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
6026 Alloc_Node
: Node_Id
;
6030 Set_Ekind
(Acc_Typ
, E_Access_Type
);
6032 Set_Associated_Storage_Pool
(Acc_Typ
, RTE
(RE_SS_Pool
));
6034 -- This is an allocator for the secondary stack, and it's fine
6035 -- to have Comes_From_Source set False on it, as gigi knows not
6036 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6039 Make_Allocator
(Loc
,
6041 Make_Qualified_Expression
(Loc
,
6042 Subtype_Mark
=> New_Occurrence_Of
(Etype
(Exp
), Loc
),
6043 Expression
=> Relocate_Node
(Exp
)));
6045 -- We do not want discriminant checks on the declaration,
6046 -- given that it gets its value from the allocator.
6048 Set_No_Initialization
(Alloc_Node
);
6050 Temp
:= Make_Temporary
(Loc
, 'R', Alloc_Node
);
6052 Insert_List_Before_And_Analyze
(N
, New_List
(
6053 Make_Full_Type_Declaration
(Loc
,
6054 Defining_Identifier
=> Acc_Typ
,
6056 Make_Access_To_Object_Definition
(Loc
,
6057 Subtype_Indication
=> Subtype_Ind
)),
6059 Make_Object_Declaration
(Loc
,
6060 Defining_Identifier
=> Temp
,
6061 Object_Definition
=> New_Occurrence_Of
(Acc_Typ
, Loc
),
6062 Expression
=> Alloc_Node
)));
6065 Make_Explicit_Dereference
(Loc
,
6066 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
6068 -- Ada 2005 (AI-251): If the type of the returned object is
6069 -- an interface then add an implicit type conversion to force
6070 -- displacement of the "this" pointer.
6072 if Is_Interface
(R_Type
) then
6073 Rewrite
(Exp
, Convert_To
(R_Type
, Relocate_Node
(Exp
)));
6076 Analyze_And_Resolve
(Exp
, R_Type
);
6079 -- Otherwise use the gigi mechanism to allocate result on the
6083 Check_Restriction
(No_Secondary_Stack
, N
);
6084 Set_Storage_Pool
(N
, RTE
(RE_SS_Pool
));
6086 -- If we are generating code for the VM do not use
6087 -- SS_Allocate since everything is heap-allocated anyway.
6089 if VM_Target
= No_VM
then
6090 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
6095 -- Implement the rules of 6.5(8-10), which require a tag check in
6096 -- the case of a limited tagged return type, and tag reassignment for
6097 -- nonlimited tagged results. These actions are needed when the return
6098 -- type is a specific tagged type and the result expression is a
6099 -- conversion or a formal parameter, because in that case the tag of
6100 -- the expression might differ from the tag of the specific result type.
6102 if Is_Tagged_Type
(Utyp
)
6103 and then not Is_Class_Wide_Type
(Utyp
)
6104 and then (Nkind_In
(Exp
, N_Type_Conversion
,
6105 N_Unchecked_Type_Conversion
)
6106 or else (Is_Entity_Name
(Exp
)
6107 and then Ekind
(Entity
(Exp
)) in Formal_Kind
))
6109 -- When the return type is limited, perform a check that the tag of
6110 -- the result is the same as the tag of the return type.
6112 if Is_Limited_Type
(R_Type
) then
6114 Make_Raise_Constraint_Error
(Loc
,
6118 Make_Selected_Component
(Loc
,
6119 Prefix
=> Duplicate_Subexpr
(Exp
),
6120 Selector_Name
=> Make_Identifier
(Loc
, Name_uTag
)),
6122 Make_Attribute_Reference
(Loc
,
6124 New_Occurrence_Of
(Base_Type
(Utyp
), Loc
),
6125 Attribute_Name
=> Name_Tag
)),
6126 Reason
=> CE_Tag_Check_Failed
));
6128 -- If the result type is a specific nonlimited tagged type, then we
6129 -- have to ensure that the tag of the result is that of the result
6130 -- type. This is handled by making a copy of the expression in
6131 -- the case where it might have a different tag, namely when the
6132 -- expression is a conversion or a formal parameter. We create a new
6133 -- object of the result type and initialize it from the expression,
6134 -- which will implicitly force the tag to be set appropriately.
6138 ExpR
: constant Node_Id
:= Relocate_Node
(Exp
);
6139 Result_Id
: constant Entity_Id
:=
6140 Make_Temporary
(Loc
, 'R', ExpR
);
6141 Result_Exp
: constant Node_Id
:=
6142 New_Occurrence_Of
(Result_Id
, Loc
);
6143 Result_Obj
: constant Node_Id
:=
6144 Make_Object_Declaration
(Loc
,
6145 Defining_Identifier
=> Result_Id
,
6146 Object_Definition
=>
6147 New_Occurrence_Of
(R_Type
, Loc
),
6148 Constant_Present
=> True,
6149 Expression
=> ExpR
);
6152 Set_Assignment_OK
(Result_Obj
);
6153 Insert_Action
(Exp
, Result_Obj
);
6155 Rewrite
(Exp
, Result_Exp
);
6156 Analyze_And_Resolve
(Exp
, R_Type
);
6160 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6161 -- a check that the level of the return expression's underlying type
6162 -- is not deeper than the level of the master enclosing the function.
6163 -- Always generate the check when the type of the return expression
6164 -- is class-wide, when it's a type conversion, or when it's a formal
6165 -- parameter. Otherwise, suppress the check in the case where the
6166 -- return expression has a specific type whose level is known not to
6167 -- be statically deeper than the function's result type.
6169 -- No runtime check needed in interface thunks since it is performed
6170 -- by the target primitive associated with the thunk.
6172 -- Note: accessibility check is skipped in the VM case, since there
6173 -- does not seem to be any practical way to implement this check.
6175 elsif Ada_Version
>= Ada_2005
6176 and then Tagged_Type_Expansion
6177 and then Is_Class_Wide_Type
(R_Type
)
6178 and then not Is_Thunk
(Current_Scope
)
6179 and then not Scope_Suppress
.Suppress
(Accessibility_Check
)
6181 (Is_Class_Wide_Type
(Etype
(Exp
))
6182 or else Nkind_In
(Exp
, N_Type_Conversion
,
6183 N_Unchecked_Type_Conversion
)
6184 or else (Is_Entity_Name
(Exp
)
6185 and then Ekind
(Entity
(Exp
)) in Formal_Kind
)
6186 or else Scope_Depth
(Enclosing_Dynamic_Scope
(Etype
(Exp
))) >
6187 Scope_Depth
(Enclosing_Dynamic_Scope
(Scope_Id
)))
6193 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6194 -- "this" to reference the base of the object. This is required to
6195 -- get access to the TSD of the object.
6197 if Is_Class_Wide_Type
(Etype
(Exp
))
6198 and then Is_Interface
(Etype
(Exp
))
6199 and then Nkind
(Exp
) = N_Explicit_Dereference
6202 Make_Explicit_Dereference
(Loc
,
6204 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
6205 Make_Function_Call
(Loc
,
6207 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
6208 Parameter_Associations
=> New_List
(
6209 Unchecked_Convert_To
(RTE
(RE_Address
),
6210 Duplicate_Subexpr
(Prefix
(Exp
)))))));
6213 Make_Attribute_Reference
(Loc
,
6214 Prefix
=> Duplicate_Subexpr
(Exp
),
6215 Attribute_Name
=> Name_Tag
);
6219 Make_Raise_Program_Error
(Loc
,
6222 Left_Opnd
=> Build_Get_Access_Level
(Loc
, Tag_Node
),
6224 Make_Integer_Literal
(Loc
,
6225 Scope_Depth
(Enclosing_Dynamic_Scope
(Scope_Id
)))),
6226 Reason
=> PE_Accessibility_Check_Failed
));
6229 -- AI05-0073: If function has a controlling access result, check that
6230 -- the tag of the return value, if it is not null, matches designated
6231 -- type of return type.
6233 -- The return expression is referenced twice in the code below, so it
6234 -- must be made free of side effects. Given that different compilers
6235 -- may evaluate these parameters in different order, both occurrences
6238 elsif Ekind
(R_Type
) = E_Anonymous_Access_Type
6239 and then Has_Controlling_Result
(Scope_Id
)
6242 Make_Raise_Constraint_Error
(Loc
,
6247 Left_Opnd
=> Duplicate_Subexpr
(Exp
),
6248 Right_Opnd
=> Make_Null
(Loc
)),
6250 Right_Opnd
=> Make_Op_Ne
(Loc
,
6252 Make_Selected_Component
(Loc
,
6253 Prefix
=> Duplicate_Subexpr
(Exp
),
6254 Selector_Name
=> Make_Identifier
(Loc
, Name_uTag
)),
6257 Make_Attribute_Reference
(Loc
,
6259 New_Occurrence_Of
(Designated_Type
(R_Type
), Loc
),
6260 Attribute_Name
=> Name_Tag
))),
6262 Reason
=> CE_Tag_Check_Failed
),
6263 Suppress
=> All_Checks
);
6266 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
6267 -- ensure that the function result does not outlive an
6268 -- object designated by one of it discriminants.
6270 if Present
(Extra_Accessibility_Of_Result
(Scope_Id
))
6271 and then Has_Unconstrained_Access_Discriminants
(R_Type
)
6274 Discrim_Source
: Node_Id
;
6276 procedure Check_Against_Result_Level
(Level
: Node_Id
);
6277 -- Check the given accessibility level against the level
6278 -- determined by the point of call. (AI05-0234).
6280 --------------------------------
6281 -- Check_Against_Result_Level --
6282 --------------------------------
6284 procedure Check_Against_Result_Level
(Level
: Node_Id
) is
6287 Make_Raise_Program_Error
(Loc
,
6293 (Extra_Accessibility_Of_Result
(Scope_Id
), Loc
)),
6294 Reason
=> PE_Accessibility_Check_Failed
));
6295 end Check_Against_Result_Level
;
6298 Discrim_Source
:= Exp
;
6299 while Nkind
(Discrim_Source
) = N_Qualified_Expression
loop
6300 Discrim_Source
:= Expression
(Discrim_Source
);
6303 if Nkind
(Discrim_Source
) = N_Identifier
6304 and then Is_Return_Object
(Entity
(Discrim_Source
))
6306 Discrim_Source
:= Entity
(Discrim_Source
);
6308 if Is_Constrained
(Etype
(Discrim_Source
)) then
6309 Discrim_Source
:= Etype
(Discrim_Source
);
6311 Discrim_Source
:= Expression
(Parent
(Discrim_Source
));
6314 elsif Nkind
(Discrim_Source
) = N_Identifier
6315 and then Nkind_In
(Original_Node
(Discrim_Source
),
6316 N_Aggregate
, N_Extension_Aggregate
)
6318 Discrim_Source
:= Original_Node
(Discrim_Source
);
6320 elsif Nkind
(Discrim_Source
) = N_Explicit_Dereference
and then
6321 Nkind
(Original_Node
(Discrim_Source
)) = N_Function_Call
6323 Discrim_Source
:= Original_Node
(Discrim_Source
);
6326 while Nkind_In
(Discrim_Source
, N_Qualified_Expression
,
6328 N_Unchecked_Type_Conversion
)
6330 Discrim_Source
:= Expression
(Discrim_Source
);
6333 case Nkind
(Discrim_Source
) is
6334 when N_Defining_Identifier
=>
6336 pragma Assert
(Is_Composite_Type
(Discrim_Source
)
6337 and then Has_Discriminants
(Discrim_Source
)
6338 and then Is_Constrained
(Discrim_Source
));
6341 Discrim
: Entity_Id
:=
6342 First_Discriminant
(Base_Type
(R_Type
));
6343 Disc_Elmt
: Elmt_Id
:=
6344 First_Elmt
(Discriminant_Constraint
6348 if Ekind
(Etype
(Discrim
)) =
6349 E_Anonymous_Access_Type
6351 Check_Against_Result_Level
6352 (Dynamic_Accessibility_Level
(Node
(Disc_Elmt
)));
6355 Next_Elmt
(Disc_Elmt
);
6356 Next_Discriminant
(Discrim
);
6357 exit when not Present
(Discrim
);
6361 when N_Aggregate | N_Extension_Aggregate
=>
6363 -- Unimplemented: extension aggregate case where discrims
6364 -- come from ancestor part, not extension part.
6367 Discrim
: Entity_Id
:=
6368 First_Discriminant
(Base_Type
(R_Type
));
6370 Disc_Exp
: Node_Id
:= Empty
;
6372 Positionals_Exhausted
6373 : Boolean := not Present
(Expressions
6376 function Associated_Expr
6377 (Comp_Id
: Entity_Id
;
6378 Associations
: List_Id
) return Node_Id
;
6380 -- Given a component and a component associations list,
6381 -- locate the expression for that component; returns
6382 -- Empty if no such expression is found.
6384 ---------------------
6385 -- Associated_Expr --
6386 ---------------------
6388 function Associated_Expr
6389 (Comp_Id
: Entity_Id
;
6390 Associations
: List_Id
) return Node_Id
6396 -- Simple linear search seems ok here
6398 Assoc
:= First
(Associations
);
6399 while Present
(Assoc
) loop
6400 Choice
:= First
(Choices
(Assoc
));
6401 while Present
(Choice
) loop
6402 if (Nkind
(Choice
) = N_Identifier
6403 and then Chars
(Choice
) = Chars
(Comp_Id
))
6404 or else (Nkind
(Choice
) = N_Others_Choice
)
6406 return Expression
(Assoc
);
6416 end Associated_Expr
;
6418 -- Start of processing for Expand_Simple_Function_Return
6421 if not Positionals_Exhausted
then
6422 Disc_Exp
:= First
(Expressions
(Discrim_Source
));
6426 if Positionals_Exhausted
then
6430 Component_Associations
(Discrim_Source
));
6433 if Ekind
(Etype
(Discrim
)) =
6434 E_Anonymous_Access_Type
6436 Check_Against_Result_Level
6437 (Dynamic_Accessibility_Level
(Disc_Exp
));
6440 Next_Discriminant
(Discrim
);
6441 exit when not Present
(Discrim
);
6443 if not Positionals_Exhausted
then
6445 Positionals_Exhausted
:= not Present
(Disc_Exp
);
6450 when N_Function_Call
=>
6452 -- No check needed (check performed by callee)
6459 Level
: constant Node_Id
:=
6460 Make_Integer_Literal
(Loc
,
6461 Object_Access_Level
(Discrim_Source
));
6464 -- Unimplemented: check for name prefix that includes
6465 -- a dereference of an access value with a dynamic
6466 -- accessibility level (e.g., an access param or a
6467 -- saooaaat) and use dynamic level in that case. For
6469 -- return Access_Param.all(Some_Index).Some_Component;
6472 Set_Etype
(Level
, Standard_Natural
);
6473 Check_Against_Result_Level
(Level
);
6480 -- If we are returning an object that may not be bit-aligned, then copy
6481 -- the value into a temporary first. This copy may need to expand to a
6482 -- loop of component operations.
6484 if Is_Possibly_Unaligned_Slice
(Exp
)
6485 or else Is_Possibly_Unaligned_Object
(Exp
)
6488 ExpR
: constant Node_Id
:= Relocate_Node
(Exp
);
6489 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', ExpR
);
6492 Make_Object_Declaration
(Loc
,
6493 Defining_Identifier
=> Tnn
,
6494 Constant_Present
=> True,
6495 Object_Definition
=> New_Occurrence_Of
(R_Type
, Loc
),
6496 Expression
=> ExpR
),
6497 Suppress
=> All_Checks
);
6498 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
6502 -- Generate call to postcondition checks if they are present
6504 if Ekind
(Scope_Id
) = E_Function
6505 and then Has_Postconditions
(Scope_Id
)
6507 -- We are going to reference the returned value twice in this case,
6508 -- once in the call to _Postconditions, and once in the actual return
6509 -- statement, but we can't have side effects happening twice, and in
6510 -- any case for efficiency we don't want to do the computation twice.
6512 -- If the returned expression is an entity name, we don't need to
6513 -- worry since it is efficient and safe to reference it twice, that's
6514 -- also true for literals other than string literals, and for the
6515 -- case of X.all where X is an entity name.
6517 if Is_Entity_Name
(Exp
)
6518 or else Nkind_In
(Exp
, N_Character_Literal
,
6521 or else (Nkind
(Exp
) = N_Explicit_Dereference
6522 and then Is_Entity_Name
(Prefix
(Exp
)))
6526 -- Otherwise we are going to need a temporary to capture the value
6530 ExpR
: Node_Id
:= Relocate_Node
(Exp
);
6531 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', ExpR
);
6534 -- In the case of discriminated objects, we have created a
6535 -- constrained subtype above, and used the underlying type.
6536 -- This transformation is post-analysis and harmless, except
6537 -- that now the call to the post-condition will be analyzed and
6538 -- type kinds have to match.
6540 if Nkind
(ExpR
) = N_Unchecked_Type_Conversion
6542 Is_Private_Type
(R_Type
) /= Is_Private_Type
(Etype
(ExpR
))
6544 ExpR
:= Expression
(ExpR
);
6547 -- For a complex expression of an elementary type, capture
6548 -- value in the temporary and use it as the reference.
6550 if Is_Elementary_Type
(R_Type
) then
6552 Make_Object_Declaration
(Loc
,
6553 Defining_Identifier
=> Tnn
,
6554 Constant_Present
=> True,
6555 Object_Definition
=> New_Occurrence_Of
(R_Type
, Loc
),
6556 Expression
=> ExpR
),
6557 Suppress
=> All_Checks
);
6559 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
6561 -- If we have something we can rename, generate a renaming of
6562 -- the object and replace the expression with a reference
6564 elsif Is_Object_Reference
(Exp
) then
6566 Make_Object_Renaming_Declaration
(Loc
,
6567 Defining_Identifier
=> Tnn
,
6568 Subtype_Mark
=> New_Occurrence_Of
(R_Type
, Loc
),
6570 Suppress
=> All_Checks
);
6572 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
6574 -- Otherwise we have something like a string literal or an
6575 -- aggregate. We could copy the value, but that would be
6576 -- inefficient. Instead we make a reference to the value and
6577 -- capture this reference with a renaming, the expression is
6578 -- then replaced by a dereference of this renaming.
6581 -- For now, copy the value, since the code below does not
6582 -- seem to work correctly ???
6585 Make_Object_Declaration
(Loc
,
6586 Defining_Identifier
=> Tnn
,
6587 Constant_Present
=> True,
6588 Object_Definition
=> New_Occurrence_Of
(R_Type
, Loc
),
6589 Expression
=> Relocate_Node
(Exp
)),
6590 Suppress
=> All_Checks
);
6592 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
6594 -- Insert_Action (Exp,
6595 -- Make_Object_Renaming_Declaration (Loc,
6596 -- Defining_Identifier => Tnn,
6597 -- Access_Definition =>
6598 -- Make_Access_Definition (Loc,
6599 -- All_Present => True,
6600 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
6602 -- Make_Reference (Loc,
6603 -- Prefix => Relocate_Node (Exp))),
6604 -- Suppress => All_Checks);
6607 -- Make_Explicit_Dereference (Loc,
6608 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
6613 -- Generate call to _postconditions
6616 Make_Procedure_Call_Statement
(Loc
,
6617 Name
=> Make_Identifier
(Loc
, Name_uPostconditions
),
6618 Parameter_Associations
=> New_List
(Duplicate_Subexpr
(Exp
))));
6621 -- Ada 2005 (AI-251): If this return statement corresponds with an
6622 -- simple return statement associated with an extended return statement
6623 -- and the type of the returned object is an interface then generate an
6624 -- implicit conversion to force displacement of the "this" pointer.
6626 if Ada_Version
>= Ada_2005
6627 and then Comes_From_Extended_Return_Statement
(N
)
6628 and then Nkind
(Expression
(N
)) = N_Identifier
6629 and then Is_Interface
(Utyp
)
6630 and then Utyp
/= Underlying_Type
(Exptyp
)
6632 Rewrite
(Exp
, Convert_To
(Utyp
, Relocate_Node
(Exp
)));
6633 Analyze_And_Resolve
(Exp
);
6635 end Expand_Simple_Function_Return
;
6637 --------------------------------
6638 -- Expand_Subprogram_Contract --
6639 --------------------------------
6641 procedure Expand_Subprogram_Contract
6643 Spec_Id
: Entity_Id
;
6644 Body_Id
: Entity_Id
)
6646 procedure Add_Invariant_And_Predicate_Checks
6647 (Subp_Id
: Entity_Id
;
6648 Stmts
: in out List_Id
;
6649 Result
: out Node_Id
);
6650 -- Process the result of function Subp_Id (if applicable) and all its
6651 -- formals. Add invariant and predicate checks where applicable. The
6652 -- routine appends all the checks to list Stmts. If Subp_Id denotes a
6653 -- function, Result contains the entity of parameter _Result, to be
6654 -- used in the creation of procedure _Postconditions.
6656 procedure Append_Enabled_Item
(Item
: Node_Id
; List
: in out List_Id
);
6657 -- Append a node to a list. If there is no list, create a new one. When
6658 -- the item denotes a pragma, it is added to the list only when it is
6661 procedure Build_Postconditions_Procedure
6662 (Subp_Id
: Entity_Id
;
6664 Result
: Entity_Id
);
6665 -- Create the body of procedure _Postconditions which handles various
6666 -- assertion actions on exit from subprogram Subp_Id. Stmts is the list
6667 -- of statements to be checked on exit. Parameter Result is the entity
6668 -- of parameter _Result when Subp_Id denotes a function.
6670 function Build_Pragma_Check_Equivalent
6672 Subp_Id
: Entity_Id
:= Empty
;
6673 Inher_Id
: Entity_Id
:= Empty
) return Node_Id
;
6674 -- Transform a [refined] pre- or postcondition denoted by Prag into an
6675 -- equivalent pragma Check. When the pre- or postcondition is inherited,
6676 -- the routine corrects the references of all formals of Inher_Id to
6677 -- point to the formals of Subp_Id.
6679 procedure Collect_Body_Postconditions
(Stmts
: in out List_Id
);
6680 -- Process all postconditions found in the declarations of the body. The
6681 -- routine appends the pragma Check equivalents to list Stmts.
6683 procedure Collect_Spec_Postconditions
6684 (Subp_Id
: Entity_Id
;
6685 Stmts
: in out List_Id
);
6686 -- Process all [inherited] postconditions of subprogram spec Subp_Id.
6687 -- The routine appends the pragma Check equivalents to list Stmts.
6689 procedure Collect_Spec_Preconditions
(Subp_Id
: Entity_Id
);
6690 -- Process all [inherited] preconditions of subprogram spec Subp_Id. The
6691 -- routine prepends the pragma Check equivalents to the declarations of
6694 procedure Prepend_To_Declarations
(Item
: Node_Id
);
6695 -- Prepend a single item to the declarations of the subprogram body
6697 procedure Process_Contract_Cases
6698 (Subp_Id
: Entity_Id
;
6699 Stmts
: in out List_Id
);
6700 -- Process pragma Contract_Cases of subprogram spec Subp_Id. The routine
6701 -- appends the expanded code to list Stmts.
6703 ----------------------------------------
6704 -- Add_Invariant_And_Predicate_Checks --
6705 ----------------------------------------
6707 procedure Add_Invariant_And_Predicate_Checks
6708 (Subp_Id
: Entity_Id
;
6709 Stmts
: in out List_Id
;
6710 Result
: out Node_Id
)
6712 procedure Add_Invariant_Access_Checks
(Id
: Entity_Id
);
6713 -- Id denotes the return value of a function or a formal parameter.
6714 -- Add an invariant check if the type of Id is access to a type with
6715 -- invariants. The routine appends the generated code to Stmts.
6717 function Invariant_Checks_OK
(Typ
: Entity_Id
) return Boolean;
6718 -- Determine whether type Typ can benefit from invariant checks. To
6719 -- qualify, the type must have a non-null invariant procedure and
6720 -- subprogram Subp_Id must appear visible from the point of view of
6723 ---------------------------------
6724 -- Add_Invariant_Access_Checks --
6725 ---------------------------------
6727 procedure Add_Invariant_Access_Checks
(Id
: Entity_Id
) is
6728 Loc
: constant Source_Ptr
:= Sloc
(N
);
6735 if Is_Access_Type
(Typ
) and then not Is_Access_Constant
(Typ
) then
6736 Typ
:= Designated_Type
(Typ
);
6738 if Invariant_Checks_OK
(Typ
) then
6740 Make_Explicit_Dereference
(Loc
,
6741 Prefix
=> New_Occurrence_Of
(Id
, Loc
));
6742 Set_Etype
(Ref
, Typ
);
6745 -- if <Id> /= null then
6746 -- <invariant_call (<Ref>)>
6751 Make_If_Statement
(Loc
,
6754 Left_Opnd
=> New_Occurrence_Of
(Id
, Loc
),
6755 Right_Opnd
=> Make_Null
(Loc
)),
6756 Then_Statements
=> New_List
(
6757 Make_Invariant_Call
(Ref
))),
6761 end Add_Invariant_Access_Checks
;
6763 -------------------------
6764 -- Invariant_Checks_OK --
6765 -------------------------
6767 function Invariant_Checks_OK
(Typ
: Entity_Id
) return Boolean is
6768 function Has_Null_Body
(Proc_Id
: Entity_Id
) return Boolean;
6769 -- Determine whether the body of procedure Proc_Id contains a sole
6770 -- null statement, possibly followed by an optional return.
6772 function Has_Public_Visibility_Of_Subprogram
return Boolean;
6773 -- Determine whether type Typ has public visibility of subprogram
6780 function Has_Null_Body
(Proc_Id
: Entity_Id
) return Boolean is
6781 Body_Id
: Entity_Id
;
6788 Spec
:= Parent
(Proc_Id
);
6789 Decl
:= Parent
(Spec
);
6791 -- Retrieve the entity of the invariant procedure body
6793 if Nkind
(Spec
) = N_Procedure_Specification
6794 and then Nkind
(Decl
) = N_Subprogram_Declaration
6796 Body_Id
:= Corresponding_Body
(Decl
);
6798 -- The body acts as a spec
6804 -- The body will be generated later
6806 if No
(Body_Id
) then
6810 Spec
:= Parent
(Body_Id
);
6811 Decl
:= Parent
(Spec
);
6814 (Nkind
(Spec
) = N_Procedure_Specification
6815 and then Nkind
(Decl
) = N_Subprogram_Body
);
6817 Stmt1
:= First
(Statements
(Handled_Statement_Sequence
(Decl
)));
6819 -- Look for a null statement followed by an optional return
6822 if Nkind
(Stmt1
) = N_Null_Statement
then
6823 Stmt2
:= Next
(Stmt1
);
6825 if Present
(Stmt2
) then
6826 return Nkind
(Stmt2
) = N_Simple_Return_Statement
;
6835 -----------------------------------------
6836 -- Has_Public_Visibility_Of_Subprogram --
6837 -----------------------------------------
6839 function Has_Public_Visibility_Of_Subprogram
return Boolean is
6840 Subp_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Subp_Id
);
6843 -- An Initialization procedure must be considered visible even
6844 -- though it is internally generated.
6846 if Is_Init_Proc
(Defining_Entity
(Subp_Decl
)) then
6849 elsif Ekind
(Scope
(Typ
)) /= E_Package
then
6852 -- Internally generated code is never publicly visible except
6853 -- for a subprogram that is the implementation of an expression
6854 -- function. In that case the visibility is determined by the
6857 elsif not Comes_From_Source
(Subp_Decl
)
6859 (Nkind
(Original_Node
(Subp_Decl
)) /= N_Expression_Function
6861 Comes_From_Source
(Defining_Entity
(Subp_Decl
)))
6865 -- Determine whether the subprogram is declared in the visible
6866 -- declarations of the package containing the type.
6869 return List_Containing
(Subp_Decl
) =
6870 Visible_Declarations
6871 (Specification
(Unit_Declaration_Node
(Scope
(Typ
))));
6873 end Has_Public_Visibility_Of_Subprogram
;
6875 -- Start of processing for Invariant_Checks_OK
6879 Has_Invariants
(Typ
)
6880 and then Present
(Invariant_Procedure
(Typ
))
6881 and then not Has_Null_Body
(Invariant_Procedure
(Typ
))
6882 and then Has_Public_Visibility_Of_Subprogram
;
6883 end Invariant_Checks_OK
;
6887 Loc
: constant Source_Ptr
:= Sloc
(N
);
6888 -- Source location of subprogram contract
6893 -- Start of processing for Add_Invariant_And_Predicate_Checks
6898 -- Do not generate any checks if no code is being generated
6900 if not Expander_Active
then
6904 -- Process the result of a function
6906 if Ekind_In
(Subp_Id
, E_Function
, E_Generic_Function
) then
6907 Typ
:= Etype
(Subp_Id
);
6909 -- Generate _Result which is used in procedure _Postconditions to
6910 -- verify the return value.
6912 Result
:= Make_Defining_Identifier
(Loc
, Name_uResult
);
6913 Set_Etype
(Result
, Typ
);
6915 -- Add an invariant check when the return type has invariants and
6916 -- the related function is visible to the outside.
6918 if Invariant_Checks_OK
(Typ
) then
6921 Make_Invariant_Call
(New_Occurrence_Of
(Result
, Loc
)),
6925 -- Add an invariant check when the return type is an access to a
6926 -- type with invariants.
6928 Add_Invariant_Access_Checks
(Result
);
6931 -- Add invariant and predicates for all formals that qualify
6933 Formal
:= First_Formal
(Subp_Id
);
6934 while Present
(Formal
) loop
6935 Typ
:= Etype
(Formal
);
6937 if Ekind
(Formal
) /= E_In_Parameter
6938 or else Is_Access_Type
(Typ
)
6940 if Invariant_Checks_OK
(Typ
) then
6943 Make_Invariant_Call
(New_Occurrence_Of
(Formal
, Loc
)),
6947 Add_Invariant_Access_Checks
(Formal
);
6949 -- Note: we used to add predicate checks for OUT and IN OUT
6950 -- formals here, but that was misguided, since such checks are
6951 -- performed on the caller side, based on the predicate of the
6952 -- actual, rather than the predicate of the formal.
6956 Next_Formal
(Formal
);
6958 end Add_Invariant_And_Predicate_Checks
;
6960 -------------------------
6961 -- Append_Enabled_Item --
6962 -------------------------
6964 procedure Append_Enabled_Item
(Item
: Node_Id
; List
: in out List_Id
) is
6966 -- Do not chain ignored or disabled pragmas
6968 if Nkind
(Item
) = N_Pragma
6969 and then (Is_Ignored
(Item
) or else Is_Disabled
(Item
))
6973 -- Otherwise, add the item
6980 -- If the pragma is a conjunct in a composite postcondition, it
6981 -- has been processed in reverse order. In the postcondition body
6982 -- if must appear before the others.
6984 if Nkind
(Item
) = N_Pragma
6985 and then From_Aspect_Specification
(Item
)
6986 and then Split_PPC
(Item
)
6988 Prepend
(Item
, List
);
6990 Append
(Item
, List
);
6993 end Append_Enabled_Item
;
6995 ------------------------------------
6996 -- Build_Postconditions_Procedure --
6997 ------------------------------------
6999 procedure Build_Postconditions_Procedure
7000 (Subp_Id
: Entity_Id
;
7004 procedure Insert_Before_First_Source_Declaration
(Stmt
: Node_Id
);
7005 -- Insert node Stmt before the first source declaration of the
7006 -- related subprogram's body. If no such declaration exists, Stmt
7007 -- becomes the last declaration.
7009 --------------------------------------------
7010 -- Insert_Before_First_Source_Declaration --
7011 --------------------------------------------
7013 procedure Insert_Before_First_Source_Declaration
(Stmt
: Node_Id
) is
7014 Decls
: constant List_Id
:= Declarations
(N
);
7018 -- Inspect the declarations of the related subprogram body looking
7019 -- for the first source declaration.
7021 if Present
(Decls
) then
7022 Decl
:= First
(Decls
);
7023 while Present
(Decl
) loop
7024 if Comes_From_Source
(Decl
) then
7025 Insert_Before
(Decl
, Stmt
);
7032 -- If we get there, then the subprogram body lacks any source
7033 -- declarations. The body of _Postconditions now acts as the
7034 -- last declaration.
7036 Append
(Stmt
, Decls
);
7038 -- Ensure that the body has a declaration list
7041 Set_Declarations
(N
, New_List
(Stmt
));
7043 end Insert_Before_First_Source_Declaration
;
7047 Loc
: constant Source_Ptr
:= Sloc
(N
);
7048 Params
: List_Id
:= No_List
;
7049 Proc_Id
: Entity_Id
;
7051 -- Start of processing for Build_Postconditions_Procedure
7054 -- Do not create the routine if no code is being generated
7056 if not Expander_Active
then
7059 -- Nothing to do if there are no actions to check on exit
7061 elsif No
(Stmts
) then
7065 Proc_Id
:= Make_Defining_Identifier
(Loc
, Name_uPostconditions
);
7067 -- The related subprogram is a function, create the specification of
7068 -- parameter _Result.
7070 if Present
(Result
) then
7071 Params
:= New_List
(
7072 Make_Parameter_Specification
(Loc
,
7073 Defining_Identifier
=> Result
,
7075 New_Occurrence_Of
(Etype
(Result
), Loc
)));
7078 -- Insert _Postconditions before the first source declaration of the
7079 -- body. This ensures that the body will not cause any premature
7080 -- freezing as it may mention types:
7082 -- procedure Proc (Obj : Array_Typ) is
7083 -- procedure _postconditions is
7086 -- end _postconditions;
7088 -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1));
7091 -- In the example above, Obj is of type T but the incorrect placement
7092 -- of _Postconditions will cause a crash in gigi due to an out of
7093 -- order reference. The body of _Postconditions must be placed after
7094 -- the declaration of Temp to preserve correct visibility.
7096 -- Note that we set an explicit End_Label in order to override the
7097 -- sloc of the implicit RETURN statement, and prevent it from
7098 -- inheriting the sloc of one of the postconditions: this would cause
7099 -- confusing debug info to be produced, interfering with coverage
7102 Insert_Before_First_Source_Declaration
(
7103 Make_Subprogram_Body
(Loc
,
7105 Make_Procedure_Specification
(Loc
,
7106 Defining_Unit_Name
=> Proc_Id
,
7107 Parameter_Specifications
=> Params
),
7109 Declarations
=> Empty_List
,
7110 Handled_Statement_Sequence
=>
7111 Make_Handled_Sequence_Of_Statements
(Loc
,
7112 Statements
=> Stmts
,
7113 End_Label
=> Make_Identifier
(Loc
, Chars
(Proc_Id
)))));
7115 -- Set the attributes of the related subprogram to capture the
7116 -- generated procedure.
7118 if Ekind_In
(Subp_Id
, E_Generic_Procedure
, E_Procedure
) then
7119 Set_Postcondition_Proc
(Subp_Id
, Proc_Id
);
7122 Set_Has_Postconditions
(Subp_Id
);
7123 end Build_Postconditions_Procedure
;
7125 -----------------------------------
7126 -- Build_Pragma_Check_Equivalent --
7127 -----------------------------------
7129 function Build_Pragma_Check_Equivalent
7131 Subp_Id
: Entity_Id
:= Empty
;
7132 Inher_Id
: Entity_Id
:= Empty
) return Node_Id
7134 Loc
: constant Source_Ptr
:= Sloc
(Prag
);
7135 Prag_Nam
: constant Name_Id
:= Pragma_Name
(Prag
);
7136 Check_Prag
: Node_Id
;
7137 Formals_Map
: Elist_Id
;
7138 Inher_Formal
: Entity_Id
;
7141 Subp_Formal
: Entity_Id
;
7144 Formals_Map
:= No_Elist
;
7146 -- When the pre- or postcondition is inherited, map the formals of
7147 -- the inherited subprogram to those of the current subprogram.
7149 if Present
(Inher_Id
) then
7150 pragma Assert
(Present
(Subp_Id
));
7152 Formals_Map
:= New_Elmt_List
;
7154 -- Create a relation <inherited formal> => <subprogram formal>
7156 Inher_Formal
:= First_Formal
(Inher_Id
);
7157 Subp_Formal
:= First_Formal
(Subp_Id
);
7158 while Present
(Inher_Formal
) and then Present
(Subp_Formal
) loop
7159 Append_Elmt
(Inher_Formal
, Formals_Map
);
7160 Append_Elmt
(Subp_Formal
, Formals_Map
);
7162 Next_Formal
(Inher_Formal
);
7163 Next_Formal
(Subp_Formal
);
7167 -- Copy the original pragma while performing substitutions (if
7174 New_Scope
=> Current_Scope
);
7176 -- Mark the pragma as being internally generated and reset the
7179 Set_Comes_From_Source
(Check_Prag
, False);
7180 Set_Analyzed
(Check_Prag
, False);
7182 -- For a postcondition pragma within a generic, preserve the pragma
7183 -- for later expansion. This is also used when an error was detected,
7184 -- thus setting Expander_Active to False.
7186 if Prag_Nam
= Name_Postcondition
and then not Expander_Active
then
7190 if Present
(Corresponding_Aspect
(Prag
)) then
7191 Nam
:= Chars
(Identifier
(Corresponding_Aspect
(Prag
)));
7196 -- Convert the copy into pragma Check by correcting the name and
7197 -- adding a check_kind argument.
7199 Set_Pragma_Identifier
7200 (Check_Prag
, Make_Identifier
(Loc
, Name_Check
));
7202 Prepend_To
(Pragma_Argument_Associations
(Check_Prag
),
7203 Make_Pragma_Argument_Association
(Loc
,
7204 Expression
=> Make_Identifier
(Loc
, Nam
)));
7206 -- Update the error message when the pragma is inherited
7208 if Present
(Inher_Id
) then
7209 Msg_Arg
:= Last
(Pragma_Argument_Associations
(Check_Prag
));
7211 if Chars
(Msg_Arg
) = Name_Message
then
7212 String_To_Name_Buffer
(Strval
(Expression
(Msg_Arg
)));
7214 -- Insert "inherited" to improve the error message
7216 if Name_Buffer
(1 .. 8) = "failed p" then
7217 Insert_Str_In_Name_Buffer
("inherited ", 8);
7218 Set_Strval
(Expression
(Msg_Arg
), String_From_Name_Buffer
);
7224 end Build_Pragma_Check_Equivalent
;
7226 ---------------------------------
7227 -- Collect_Body_Postconditions --
7228 ---------------------------------
7230 procedure Collect_Body_Postconditions
(Stmts
: in out List_Id
) is
7231 procedure Collect_Body_Postconditions_Of_Kind
(Post_Nam
: Name_Id
);
7232 -- Process all postconditions of the kind denoted by Post_Nam
7234 -----------------------------------------
7235 -- Collect_Body_Postconditions_Of_Kind --
7236 -----------------------------------------
7238 procedure Collect_Body_Postconditions_Of_Kind
(Post_Nam
: Name_Id
) is
7239 procedure Collect_Body_Postconditions_In_Decls
7240 (First_Decl
: Node_Id
);
7241 -- Process all postconditions found in a declarative list starting
7242 -- with declaration First_Decl.
7244 ------------------------------------------
7245 -- Collect_Body_Postconditions_In_Decls --
7246 ------------------------------------------
7248 procedure Collect_Body_Postconditions_In_Decls
7249 (First_Decl
: Node_Id
)
7251 Check_Prag
: Node_Id
;
7255 -- Inspect the declarative list looking for a pragma that
7256 -- matches the desired name.
7259 while Present
(Decl
) loop
7261 -- Note that non-matching pragmas are skipped
7263 if Nkind
(Decl
) = N_Pragma
then
7264 if Pragma_Name
(Decl
) = Post_Nam
then
7265 if not Analyzed
(Decl
) then
7269 Check_Prag
:= Build_Pragma_Check_Equivalent
(Decl
);
7271 if Expander_Active
then
7273 (Item
=> Check_Prag
,
7276 -- If analyzing a generic unit, save pragma for later
7279 Prepend_To_Declarations
(Check_Prag
);
7283 -- Skip internally generated code
7285 elsif not Comes_From_Source
(Decl
) then
7288 -- Postcondition pragmas are usually grouped together. There
7289 -- is no need to inspect the whole declarative list.
7297 end Collect_Body_Postconditions_In_Decls
;
7301 Unit_Decl
: constant Node_Id
:= Parent
(N
);
7303 -- Start of processing for Collect_Body_Postconditions_Of_Kind
7306 pragma Assert
(Nam_In
(Post_Nam
, Name_Postcondition
,
7307 Name_Refined_Post
));
7309 -- Inspect the declarations of the subprogram body looking for a
7310 -- pragma that matches the desired name.
7312 Collect_Body_Postconditions_In_Decls
7313 (First_Decl
=> First
(Declarations
(N
)));
7315 -- The subprogram body being processed is actually the proper body
7316 -- of a stub with a corresponding spec. The subprogram stub may
7317 -- carry a postcondition pragma in which case it must be taken
7318 -- into account. The pragma appears after the stub.
7320 if Present
(Spec_Id
) and then Nkind
(Unit_Decl
) = N_Subunit
then
7321 Collect_Body_Postconditions_In_Decls
7322 (First_Decl
=> Next
(Corresponding_Stub
(Unit_Decl
)));
7324 end Collect_Body_Postconditions_Of_Kind
;
7326 -- Start of processing for Collect_Body_Postconditions
7329 Collect_Body_Postconditions_Of_Kind
(Name_Refined_Post
);
7330 Collect_Body_Postconditions_Of_Kind
(Name_Postcondition
);
7331 end Collect_Body_Postconditions
;
7333 ---------------------------------
7334 -- Collect_Spec_Postconditions --
7335 ---------------------------------
7337 procedure Collect_Spec_Postconditions
7338 (Subp_Id
: Entity_Id
;
7339 Stmts
: in out List_Id
)
7341 Inher_Subps
: constant Subprogram_List
:=
7342 Inherited_Subprograms
(Subp_Id
);
7343 Check_Prag
: Node_Id
;
7345 Inher_Subp_Id
: Entity_Id
;
7348 -- Process the contract of the spec
7350 Prag
:= Pre_Post_Conditions
(Contract
(Subp_Id
));
7351 while Present
(Prag
) loop
7352 if Pragma_Name
(Prag
) = Name_Postcondition
then
7353 Check_Prag
:= Build_Pragma_Check_Equivalent
(Prag
);
7355 if Expander_Active
then
7357 (Item
=> Check_Prag
,
7360 -- When analyzing a generic unit, save the pragma for later
7363 Prepend_To_Declarations
(Check_Prag
);
7367 Prag
:= Next_Pragma
(Prag
);
7370 -- Process the contracts of all inherited subprograms, looking for
7371 -- class-wide postconditions.
7373 for Index
in Inher_Subps
'Range loop
7374 Inher_Subp_Id
:= Inher_Subps
(Index
);
7376 Prag
:= Pre_Post_Conditions
(Contract
(Inher_Subp_Id
));
7377 while Present
(Prag
) loop
7378 if Pragma_Name
(Prag
) = Name_Postcondition
7379 and then Class_Present
(Prag
)
7382 Build_Pragma_Check_Equivalent
7385 Inher_Id
=> Inher_Subp_Id
);
7387 if Expander_Active
then
7389 (Item
=> Check_Prag
,
7392 -- When analyzing a generic unit, save the pragma for later
7395 Prepend_To_Declarations
(Check_Prag
);
7399 Prag
:= Next_Pragma
(Prag
);
7402 end Collect_Spec_Postconditions
;
7404 --------------------------------
7405 -- Collect_Spec_Preconditions --
7406 --------------------------------
7408 procedure Collect_Spec_Preconditions
(Subp_Id
: Entity_Id
) is
7409 Class_Pre
: Node_Id
:= Empty
;
7410 -- The sole class-wide precondition pragma that applies to the
7413 procedure Add_Or_Save_Precondition
(Prag
: Node_Id
);
7414 -- Save a class-wide precondition or add a regulat precondition to
7415 -- the declarative list of the body.
7417 procedure Merge_Preconditions
(From
: Node_Id
; Into
: Node_Id
);
7418 -- Merge two class-wide preconditions by "or else"-ing them. The
7419 -- changes are accumulated in parameter Into. Update the error
7422 ------------------------------
7423 -- Add_Or_Save_Precondition --
7424 ------------------------------
7426 procedure Add_Or_Save_Precondition
(Prag
: Node_Id
) is
7427 Check_Prag
: Node_Id
;
7430 Check_Prag
:= Build_Pragma_Check_Equivalent
(Prag
);
7432 -- Save the sole class-wide precondition (if any) for the next
7433 -- step where it will be merged with inherited preconditions.
7435 if Class_Present
(Prag
) then
7436 pragma Assert
(No
(Class_Pre
));
7437 Class_Pre
:= Check_Prag
;
7439 -- Accumulate the corresponding Check pragmas to the top of the
7440 -- declarations. Prepending the items ensures that they will be
7441 -- evaluated in their original order.
7444 Prepend_To_Declarations
(Check_Prag
);
7446 end Add_Or_Save_Precondition
;
7448 -------------------------
7449 -- Merge_Preconditions --
7450 -------------------------
7452 procedure Merge_Preconditions
(From
: Node_Id
; Into
: Node_Id
) is
7453 function Expression_Arg
(Prag
: Node_Id
) return Node_Id
;
7454 -- Return the boolean expression argument of a precondition while
7455 -- updating its parenteses count for the subsequent merge.
7457 function Message_Arg
(Prag
: Node_Id
) return Node_Id
;
7458 -- Return the message argument of a precondition
7460 --------------------
7461 -- Expression_Arg --
7462 --------------------
7464 function Expression_Arg
(Prag
: Node_Id
) return Node_Id
is
7465 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
7466 Arg
: constant Node_Id
:= Get_Pragma_Arg
(Next
(First
(Args
)));
7469 if Paren_Count
(Arg
) = 0 then
7470 Set_Paren_Count
(Arg
, 1);
7480 function Message_Arg
(Prag
: Node_Id
) return Node_Id
is
7481 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
7483 return Get_Pragma_Arg
(Last
(Args
));
7488 From_Expr
: constant Node_Id
:= Expression_Arg
(From
);
7489 From_Msg
: constant Node_Id
:= Message_Arg
(From
);
7490 Into_Expr
: constant Node_Id
:= Expression_Arg
(Into
);
7491 Into_Msg
: constant Node_Id
:= Message_Arg
(Into
);
7492 Loc
: constant Source_Ptr
:= Sloc
(Into
);
7494 -- Start of processing for Merge_Preconditions
7497 -- Merge the two preconditions by "or else"-ing them
7501 Right_Opnd
=> Relocate_Node
(Into_Expr
),
7502 Left_Opnd
=> From_Expr
));
7504 -- Merge the two error messages to produce a single message of the
7507 -- failed precondition from ...
7508 -- also failed inherited precondition from ...
7510 if not Exception_Locations_Suppressed
then
7511 Start_String
(Strval
(Into_Msg
));
7512 Store_String_Char
(ASCII
.LF
);
7513 Store_String_Chars
(" also ");
7514 Store_String_Chars
(Strval
(From_Msg
));
7516 Set_Strval
(Into_Msg
, End_String
);
7518 end Merge_Preconditions
;
7522 Inher_Subps
: constant Subprogram_List
:=
7523 Inherited_Subprograms
(Subp_Id
);
7524 Subp_Decl
: constant Node_Id
:= Parent
(Parent
(Subp_Id
));
7525 Check_Prag
: Node_Id
;
7527 Inher_Subp_Id
: Entity_Id
;
7530 -- Start of processing for Collect_Spec_Preconditions
7533 -- Process the contract of the spec
7535 Prag
:= Pre_Post_Conditions
(Contract
(Subp_Id
));
7536 while Present
(Prag
) loop
7537 if Pragma_Name
(Prag
) = Name_Precondition
then
7538 Add_Or_Save_Precondition
(Prag
);
7541 Prag
:= Next_Pragma
(Prag
);
7544 -- The subprogram declaration being processed is actually a body
7545 -- stub. The stub may carry a precondition pragma in which case it
7546 -- must be taken into account. The pragma appears after the stub.
7548 if Nkind
(Subp_Decl
) = N_Subprogram_Body_Stub
then
7550 -- Inspect the declarations following the body stub
7552 Decl
:= Next
(Subp_Decl
);
7553 while Present
(Decl
) loop
7555 -- Note that non-matching pragmas are skipped
7557 if Nkind
(Decl
) = N_Pragma
then
7558 if Pragma_Name
(Decl
) = Name_Precondition
then
7559 if not Analyzed
(Decl
) then
7563 Add_Or_Save_Precondition
(Decl
);
7566 -- Skip internally generated code
7568 elsif not Comes_From_Source
(Decl
) then
7571 -- Preconditions are usually grouped together. There is no need
7572 -- to inspect the whole declarative list.
7582 -- Process the contracts of all inherited subprograms, looking for
7583 -- class-wide preconditions.
7585 for Index
in Inher_Subps
'Range loop
7586 Inher_Subp_Id
:= Inher_Subps
(Index
);
7588 Prag
:= Pre_Post_Conditions
(Contract
(Inher_Subp_Id
));
7589 while Present
(Prag
) loop
7590 if Pragma_Name
(Prag
) = Name_Precondition
7591 and then Class_Present
(Prag
)
7594 Build_Pragma_Check_Equivalent
7597 Inher_Id
=> Inher_Subp_Id
);
7599 -- The spec or an inherited subprogram already yielded a
7600 -- class-wide precondition. Merge the existing precondition
7601 -- with the current one using "or else".
7603 if Present
(Class_Pre
) then
7604 Merge_Preconditions
(Check_Prag
, Class_Pre
);
7606 Class_Pre
:= Check_Prag
;
7610 Prag
:= Next_Pragma
(Prag
);
7614 -- Add the merged class-wide preconditions (if any)
7616 if Present
(Class_Pre
) then
7617 Prepend_To_Declarations
(Class_Pre
);
7619 end Collect_Spec_Preconditions
;
7621 -----------------------------
7622 -- Prepend_To_Declarations --
7623 -----------------------------
7625 procedure Prepend_To_Declarations
(Item
: Node_Id
) is
7626 Decls
: List_Id
:= Declarations
(N
);
7629 -- Ensure that the body has a declarative list
7633 Set_Declarations
(N
, Decls
);
7636 Prepend_To
(Decls
, Item
);
7637 end Prepend_To_Declarations
;
7639 ----------------------------
7640 -- Process_Contract_Cases --
7641 ----------------------------
7643 procedure Process_Contract_Cases
7644 (Subp_Id
: Entity_Id
;
7645 Stmts
: in out List_Id
)
7650 -- Do not build the Contract_Cases circuitry if no code is being
7653 if not Expander_Active
then
7657 Prag
:= Contract_Test_Cases
(Contract
(Subp_Id
));
7658 while Present
(Prag
) loop
7659 if Pragma_Name
(Prag
) = Name_Contract_Cases
then
7660 Expand_Contract_Cases
7663 Decls
=> Declarations
(N
),
7667 Prag
:= Next_Pragma
(Prag
);
7669 end Process_Contract_Cases
;
7673 Post_Stmts
: List_Id
:= No_List
;
7675 Subp_Id
: Entity_Id
;
7677 -- Start of processing for Expand_Subprogram_Contract
7680 if Present
(Spec_Id
) then
7686 -- Do not process a predicate function as its body will end up with a
7687 -- recursive call to itself and blow up the stack.
7689 if Ekind
(Subp_Id
) = E_Function
7690 and then Is_Predicate_Function
(Subp_Id
)
7694 -- Do not process TSS subprograms
7696 elsif Get_TSS_Name
(Subp_Id
) /= TSS_Null
then
7700 -- The expansion of a subprogram contract involves the relocation of
7701 -- various contract assertions to the declarations of the body in a
7702 -- particular order. The order is as follows:
7704 -- function Example (...) return ... is
7705 -- procedure _Postconditions (...) is
7707 -- <refined postconditions from body>
7708 -- <postconditions from body>
7709 -- <postconditions from spec>
7710 -- <inherited postconditions>
7711 -- <contract case consequences>
7712 -- <invariant check of function result (if applicable)>
7713 -- <invariant and predicate checks of parameters>
7714 -- end _Postconditions;
7716 -- <inherited preconditions>
7717 -- <preconditions from spec>
7718 -- <preconditions from body>
7719 -- <refined preconditions from body>
7720 -- <contract case conditions>
7722 -- <source declarations>
7724 -- <source statements>
7726 -- _Preconditions (Result);
7730 -- Routine _Postconditions holds all contract assertions that must be
7731 -- verified on exit from the related routine.
7733 -- Collect all [inherited] preconditions from the spec, transform them
7734 -- into Check pragmas and add them to the declarations of the body in
7735 -- the order outlined above.
7737 if Present
(Spec_Id
) then
7738 Collect_Spec_Preconditions
(Spec_Id
);
7741 -- Transform all [refined] postconditions of the body into Check
7742 -- pragmas. The resulting pragmas are accumulated in list Post_Stmts.
7744 Collect_Body_Postconditions
(Post_Stmts
);
7746 -- Transform all [inherited] postconditions from the spec into Check
7747 -- pragmas. The resulting pragmas are accumulated in list Post_Stmts.
7749 if Present
(Spec_Id
) then
7750 Collect_Spec_Postconditions
(Spec_Id
, Post_Stmts
);
7752 -- Transform pragma Contract_Cases from the spec into its circuitry
7754 Process_Contract_Cases
(Spec_Id
, Post_Stmts
);
7757 -- Apply invariant and predicate checks on the result of a function (if
7758 -- applicable) and all formals. The resulting checks are accumulated in
7761 Add_Invariant_And_Predicate_Checks
(Subp_Id
, Post_Stmts
, Result
);
7763 -- Construct procedure _Postconditions
7765 Build_Postconditions_Procedure
(Subp_Id
, Post_Stmts
, Result
);
7766 end Expand_Subprogram_Contract
;
7768 --------------------------------
7769 -- Is_Build_In_Place_Function --
7770 --------------------------------
7772 function Is_Build_In_Place_Function
(E
: Entity_Id
) return Boolean is
7774 -- This function is called from Expand_Subtype_From_Expr during
7775 -- semantic analysis, even when expansion is off. In those cases
7776 -- the build_in_place expansion will not take place.
7778 if not Expander_Active
then
7782 -- For now we test whether E denotes a function or access-to-function
7783 -- type whose result subtype is inherently limited. Later this test
7784 -- may be revised to allow composite nonlimited types. Functions with
7785 -- a foreign convention or whose result type has a foreign convention
7788 if Ekind_In
(E
, E_Function
, E_Generic_Function
)
7789 or else (Ekind
(E
) = E_Subprogram_Type
7790 and then Etype
(E
) /= Standard_Void_Type
)
7792 -- Note: If the function has a foreign convention, it cannot build
7793 -- its result in place, so you're on your own. On the other hand,
7794 -- if only the return type has a foreign convention, its layout is
7795 -- intended to be compatible with the other language, but the build-
7796 -- in place machinery can ensure that the object is not copied.
7798 if Has_Foreign_Convention
(E
) then
7801 -- In Ada 2005 all functions with an inherently limited return type
7802 -- must be handled using a build-in-place profile, including the case
7803 -- of a function with a limited interface result, where the function
7804 -- may return objects of nonlimited descendants.
7807 return Is_Limited_View
(Etype
(E
))
7808 and then Ada_Version
>= Ada_2005
7809 and then not Debug_Flag_Dot_L
;
7815 end Is_Build_In_Place_Function
;
7817 -------------------------------------
7818 -- Is_Build_In_Place_Function_Call --
7819 -------------------------------------
7821 function Is_Build_In_Place_Function_Call
(N
: Node_Id
) return Boolean is
7822 Exp_Node
: Node_Id
:= N
;
7823 Function_Id
: Entity_Id
;
7826 -- Return False if the expander is currently inactive, since awareness
7827 -- of build-in-place treatment is only relevant during expansion. Note
7828 -- that Is_Build_In_Place_Function, which is called as part of this
7829 -- function, is also conditioned this way, but we need to check here as
7830 -- well to avoid blowing up on processing protected calls when expansion
7831 -- is disabled (such as with -gnatc) since those would trip over the
7832 -- raise of Program_Error below.
7834 -- In SPARK mode, build-in-place calls are not expanded, so that we
7835 -- may end up with a call that is neither resolved to an entity, nor
7836 -- an indirect call.
7838 if not Expander_Active
then
7842 -- Step past qualification or unchecked conversion (the latter can occur
7843 -- in cases of calls to 'Input).
7845 if Nkind_In
(Exp_Node
, N_Qualified_Expression
,
7846 N_Unchecked_Type_Conversion
)
7848 Exp_Node
:= Expression
(N
);
7851 if Nkind
(Exp_Node
) /= N_Function_Call
then
7855 if Is_Entity_Name
(Name
(Exp_Node
)) then
7856 Function_Id
:= Entity
(Name
(Exp_Node
));
7858 -- In the case of an explicitly dereferenced call, use the subprogram
7859 -- type generated for the dereference.
7861 elsif Nkind
(Name
(Exp_Node
)) = N_Explicit_Dereference
then
7862 Function_Id
:= Etype
(Name
(Exp_Node
));
7864 -- This may be a call to a protected function.
7866 elsif Nkind
(Name
(Exp_Node
)) = N_Selected_Component
then
7867 Function_Id
:= Etype
(Entity
(Selector_Name
(Name
(Exp_Node
))));
7870 raise Program_Error
;
7873 return Is_Build_In_Place_Function
(Function_Id
);
7875 end Is_Build_In_Place_Function_Call
;
7877 -----------------------
7878 -- Freeze_Subprogram --
7879 -----------------------
7881 procedure Freeze_Subprogram
(N
: Node_Id
) is
7882 Loc
: constant Source_Ptr
:= Sloc
(N
);
7884 procedure Register_Predefined_DT_Entry
(Prim
: Entity_Id
);
7885 -- (Ada 2005): Register a predefined primitive in all the secondary
7886 -- dispatch tables of its primitive type.
7888 ----------------------------------
7889 -- Register_Predefined_DT_Entry --
7890 ----------------------------------
7892 procedure Register_Predefined_DT_Entry
(Prim
: Entity_Id
) is
7893 Iface_DT_Ptr
: Elmt_Id
;
7894 Tagged_Typ
: Entity_Id
;
7895 Thunk_Id
: Entity_Id
;
7896 Thunk_Code
: Node_Id
;
7899 Tagged_Typ
:= Find_Dispatching_Type
(Prim
);
7901 if No
(Access_Disp_Table
(Tagged_Typ
))
7902 or else not Has_Interfaces
(Tagged_Typ
)
7903 or else not RTE_Available
(RE_Interface_Tag
)
7904 or else Restriction_Active
(No_Dispatching_Calls
)
7909 -- Skip the first two access-to-dispatch-table pointers since they
7910 -- leads to the primary dispatch table (predefined DT and user
7911 -- defined DT). We are only concerned with the secondary dispatch
7912 -- table pointers. Note that the access-to- dispatch-table pointer
7913 -- corresponds to the first implemented interface retrieved below.
7916 Next_Elmt
(Next_Elmt
(First_Elmt
(Access_Disp_Table
(Tagged_Typ
))));
7918 while Present
(Iface_DT_Ptr
)
7919 and then Ekind
(Node
(Iface_DT_Ptr
)) = E_Constant
7921 pragma Assert
(Has_Thunks
(Node
(Iface_DT_Ptr
)));
7922 Expand_Interface_Thunk
(Prim
, Thunk_Id
, Thunk_Code
);
7924 if Present
(Thunk_Code
) then
7925 Insert_Actions_After
(N
, New_List
(
7928 Build_Set_Predefined_Prim_Op_Address
(Loc
,
7930 New_Occurrence_Of
(Node
(Next_Elmt
(Iface_DT_Ptr
)), Loc
),
7931 Position
=> DT_Position
(Prim
),
7933 Unchecked_Convert_To
(RTE
(RE_Prim_Ptr
),
7934 Make_Attribute_Reference
(Loc
,
7935 Prefix
=> New_Occurrence_Of
(Thunk_Id
, Loc
),
7936 Attribute_Name
=> Name_Unrestricted_Access
))),
7938 Build_Set_Predefined_Prim_Op_Address
(Loc
,
7941 (Node
(Next_Elmt
(Next_Elmt
(Next_Elmt
(Iface_DT_Ptr
)))),
7943 Position
=> DT_Position
(Prim
),
7945 Unchecked_Convert_To
(RTE
(RE_Prim_Ptr
),
7946 Make_Attribute_Reference
(Loc
,
7947 Prefix
=> New_Occurrence_Of
(Prim
, Loc
),
7948 Attribute_Name
=> Name_Unrestricted_Access
)))));
7951 -- Skip the tag of the predefined primitives dispatch table
7953 Next_Elmt
(Iface_DT_Ptr
);
7954 pragma Assert
(Has_Thunks
(Node
(Iface_DT_Ptr
)));
7956 -- Skip tag of the no-thunks dispatch table
7958 Next_Elmt
(Iface_DT_Ptr
);
7959 pragma Assert
(not Has_Thunks
(Node
(Iface_DT_Ptr
)));
7961 -- Skip tag of predefined primitives no-thunks dispatch table
7963 Next_Elmt
(Iface_DT_Ptr
);
7964 pragma Assert
(not Has_Thunks
(Node
(Iface_DT_Ptr
)));
7966 Next_Elmt
(Iface_DT_Ptr
);
7968 end Register_Predefined_DT_Entry
;
7972 Subp
: constant Entity_Id
:= Entity
(N
);
7974 -- Start of processing for Freeze_Subprogram
7977 -- We suppress the initialization of the dispatch table entry when
7978 -- VM_Target because the dispatching mechanism is handled internally
7981 if Is_Dispatching_Operation
(Subp
)
7982 and then not Is_Abstract_Subprogram
(Subp
)
7983 and then Present
(DTC_Entity
(Subp
))
7984 and then Present
(Scope
(DTC_Entity
(Subp
)))
7985 and then Tagged_Type_Expansion
7986 and then not Restriction_Active
(No_Dispatching_Calls
)
7987 and then RTE_Available
(RE_Tag
)
7990 Typ
: constant Entity_Id
:= Scope
(DTC_Entity
(Subp
));
7993 -- Handle private overridden primitives
7995 if not Is_CPP_Class
(Typ
) then
7996 Check_Overriding_Operation
(Subp
);
7999 -- We assume that imported CPP primitives correspond with objects
8000 -- whose constructor is in the CPP side; therefore we don't need
8001 -- to generate code to register them in the dispatch table.
8003 if Is_CPP_Class
(Typ
) then
8006 -- Handle CPP primitives found in derivations of CPP_Class types.
8007 -- These primitives must have been inherited from some parent, and
8008 -- there is no need to register them in the dispatch table because
8009 -- Build_Inherit_Prims takes care of initializing these slots.
8011 elsif Is_Imported
(Subp
)
8012 and then (Convention
(Subp
) = Convention_CPP
8013 or else Convention
(Subp
) = Convention_C
)
8017 -- Generate code to register the primitive in non statically
8018 -- allocated dispatch tables
8020 elsif not Building_Static_DT
(Scope
(DTC_Entity
(Subp
))) then
8022 -- When a primitive is frozen, enter its name in its dispatch
8025 if not Is_Interface
(Typ
)
8026 or else Present
(Interface_Alias
(Subp
))
8028 if Is_Predefined_Dispatching_Operation
(Subp
) then
8029 Register_Predefined_DT_Entry
(Subp
);
8032 Insert_Actions_After
(N
,
8033 Register_Primitive
(Loc
, Prim
=> Subp
));
8039 -- Mark functions that return by reference. Note that it cannot be part
8040 -- of the normal semantic analysis of the spec since the underlying
8041 -- returned type may not be known yet (for private types).
8044 Typ
: constant Entity_Id
:= Etype
(Subp
);
8045 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
8047 if Is_Limited_View
(Typ
) then
8048 Set_Returns_By_Ref
(Subp
);
8049 elsif Present
(Utyp
) and then CW_Or_Has_Controlled_Part
(Utyp
) then
8050 Set_Returns_By_Ref
(Subp
);
8054 -- Wnen freezing a null procedure, analyze its delayed aspects now
8055 -- because we may not have reached the end of the declarative list when
8056 -- delayed aspects are normally analyzed. This ensures that dispatching
8057 -- calls are properly rewritten when the generated _Postcondition
8058 -- procedure is analyzed in the null procedure body.
8060 if Nkind
(Parent
(Subp
)) = N_Procedure_Specification
8061 and then Null_Present
(Parent
(Subp
))
8063 Analyze_Subprogram_Contract
(Subp
);
8065 end Freeze_Subprogram
;
8067 -----------------------
8068 -- Is_Null_Procedure --
8069 -----------------------
8071 function Is_Null_Procedure
(Subp
: Entity_Id
) return Boolean is
8072 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
8075 if Ekind
(Subp
) /= E_Procedure
then
8078 -- Check if this is a declared null procedure
8080 elsif Nkind
(Decl
) = N_Subprogram_Declaration
then
8081 if not Null_Present
(Specification
(Decl
)) then
8084 elsif No
(Body_To_Inline
(Decl
)) then
8087 -- Check if the body contains only a null statement, followed by
8088 -- the return statement added during expansion.
8092 Orig_Bod
: constant Node_Id
:= Body_To_Inline
(Decl
);
8098 if Nkind
(Orig_Bod
) /= N_Subprogram_Body
then
8101 -- We must skip SCIL nodes because they are currently
8102 -- implemented as special N_Null_Statement nodes.
8106 (Statements
(Handled_Statement_Sequence
(Orig_Bod
)));
8107 Stat2
:= Next_Non_SCIL_Node
(Stat
);
8110 Is_Empty_List
(Declarations
(Orig_Bod
))
8111 and then Nkind
(Stat
) = N_Null_Statement
8115 (Nkind
(Stat2
) = N_Simple_Return_Statement
8116 and then No
(Next
(Stat2
))));
8124 end Is_Null_Procedure
;
8126 -------------------------------------------
8127 -- Make_Build_In_Place_Call_In_Allocator --
8128 -------------------------------------------
8130 procedure Make_Build_In_Place_Call_In_Allocator
8131 (Allocator
: Node_Id
;
8132 Function_Call
: Node_Id
)
8134 Acc_Type
: constant Entity_Id
:= Etype
(Allocator
);
8136 Func_Call
: Node_Id
:= Function_Call
;
8137 Ref_Func_Call
: Node_Id
;
8138 Function_Id
: Entity_Id
;
8139 Result_Subt
: Entity_Id
;
8140 New_Allocator
: Node_Id
;
8141 Return_Obj_Access
: Entity_Id
; -- temp for function result
8142 Temp_Init
: Node_Id
; -- initial value of Return_Obj_Access
8143 Alloc_Form
: BIP_Allocation_Form
;
8144 Pool
: Node_Id
; -- nonnull if Alloc_Form = User_Storage_Pool
8145 Return_Obj_Actual
: Node_Id
; -- the temp.all, in caller-allocates case
8146 Chain
: Entity_Id
; -- activation chain, in case of tasks
8149 -- Step past qualification or unchecked conversion (the latter can occur
8150 -- in cases of calls to 'Input).
8152 if Nkind_In
(Func_Call
,
8153 N_Qualified_Expression
,
8154 N_Unchecked_Type_Conversion
)
8156 Func_Call
:= Expression
(Func_Call
);
8159 -- If the call has already been processed to add build-in-place actuals
8160 -- then return. This should not normally occur in an allocator context,
8161 -- but we add the protection as a defensive measure.
8163 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
8167 -- Mark the call as processed as a build-in-place call
8169 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8171 Loc
:= Sloc
(Function_Call
);
8173 if Is_Entity_Name
(Name
(Func_Call
)) then
8174 Function_Id
:= Entity
(Name
(Func_Call
));
8176 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8177 Function_Id
:= Etype
(Name
(Func_Call
));
8180 raise Program_Error
;
8183 Result_Subt
:= Available_View
(Etype
(Function_Id
));
8185 -- Create a temp for the function result. In the caller-allocates case,
8186 -- this will be initialized to the result of a new uninitialized
8187 -- allocator. Note: we do not use Allocator as the Related_Node of
8188 -- Return_Obj_Access in call to Make_Temporary below as this would
8189 -- create a sort of infinite "recursion".
8191 Return_Obj_Access
:= Make_Temporary
(Loc
, 'R');
8192 Set_Etype
(Return_Obj_Access
, Acc_Type
);
8194 -- When the result subtype is constrained, the return object is
8195 -- allocated on the caller side, and access to it is passed to the
8198 -- Here and in related routines, we must examine the full view of the
8199 -- type, because the view at the point of call may differ from that
8200 -- that in the function body, and the expansion mechanism depends on
8201 -- the characteristics of the full view.
8203 if Is_Constrained
(Underlying_Type
(Result_Subt
)) then
8205 -- Replace the initialized allocator of form "new T'(Func (...))"
8206 -- with an uninitialized allocator of form "new T", where T is the
8207 -- result subtype of the called function. The call to the function
8208 -- is handled separately further below.
8211 Make_Allocator
(Loc
,
8212 Expression
=> New_Occurrence_Of
(Result_Subt
, Loc
));
8213 Set_No_Initialization
(New_Allocator
);
8215 -- Copy attributes to new allocator. Note that the new allocator
8216 -- logically comes from source if the original one did, so copy the
8217 -- relevant flag. This ensures proper treatment of the restriction
8218 -- No_Implicit_Heap_Allocations in this case.
8220 Set_Storage_Pool
(New_Allocator
, Storage_Pool
(Allocator
));
8221 Set_Procedure_To_Call
(New_Allocator
, Procedure_To_Call
(Allocator
));
8222 Set_Comes_From_Source
(New_Allocator
, Comes_From_Source
(Allocator
));
8224 Rewrite
(Allocator
, New_Allocator
);
8226 -- Initial value of the temp is the result of the uninitialized
8229 Temp_Init
:= Relocate_Node
(Allocator
);
8231 -- Indicate that caller allocates, and pass in the return object
8233 Alloc_Form
:= Caller_Allocation
;
8234 Pool
:= Make_Null
(No_Location
);
8235 Return_Obj_Actual
:=
8236 Make_Unchecked_Type_Conversion
(Loc
,
8237 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Loc
),
8239 Make_Explicit_Dereference
(Loc
,
8240 Prefix
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
)));
8242 -- When the result subtype is unconstrained, the function itself must
8243 -- perform the allocation of the return object, so we pass parameters
8249 -- Case of a user-defined storage pool. Pass an allocation parameter
8250 -- indicating that the function should allocate its result in the
8251 -- pool, and pass the pool. Use 'Unrestricted_Access because the
8252 -- pool may not be aliased.
8254 if VM_Target
= No_VM
8255 and then Present
(Associated_Storage_Pool
(Acc_Type
))
8257 Alloc_Form
:= User_Storage_Pool
;
8259 Make_Attribute_Reference
(Loc
,
8262 (Associated_Storage_Pool
(Acc_Type
), Loc
),
8263 Attribute_Name
=> Name_Unrestricted_Access
);
8265 -- No user-defined pool; pass an allocation parameter indicating that
8266 -- the function should allocate its result on the heap.
8269 Alloc_Form
:= Global_Heap
;
8270 Pool
:= Make_Null
(No_Location
);
8273 -- The caller does not provide the return object in this case, so we
8274 -- have to pass null for the object access actual.
8276 Return_Obj_Actual
:= Empty
;
8279 -- Declare the temp object
8281 Insert_Action
(Allocator
,
8282 Make_Object_Declaration
(Loc
,
8283 Defining_Identifier
=> Return_Obj_Access
,
8284 Object_Definition
=> New_Occurrence_Of
(Acc_Type
, Loc
),
8285 Expression
=> Temp_Init
));
8287 Ref_Func_Call
:= Make_Reference
(Loc
, Func_Call
);
8289 -- Ada 2005 (AI-251): If the type of the allocator is an interface
8290 -- then generate an implicit conversion to force displacement of the
8293 if Is_Interface
(Designated_Type
(Acc_Type
)) then
8296 OK_Convert_To
(Acc_Type
, Ref_Func_Call
));
8300 Assign
: constant Node_Id
:=
8301 Make_Assignment_Statement
(Loc
,
8302 Name
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
),
8303 Expression
=> Ref_Func_Call
);
8304 -- Assign the result of the function call into the temp. In the
8305 -- caller-allocates case, this is overwriting the temp with its
8306 -- initial value, which has no effect. In the callee-allocates case,
8307 -- this is setting the temp to point to the object allocated by the
8311 -- Actions to be inserted. If there are no tasks, this is just the
8312 -- assignment statement. If the allocated object has tasks, we need
8313 -- to wrap the assignment in a block that activates them. The
8314 -- activation chain of that block must be passed to the function,
8315 -- rather than some outer chain.
8317 if Has_Task
(Result_Subt
) then
8318 Actions
:= New_List
;
8319 Build_Task_Allocate_Block_With_Init_Stmts
8320 (Actions
, Allocator
, Init_Stmts
=> New_List
(Assign
));
8321 Chain
:= Activation_Chain_Entity
(Last
(Actions
));
8323 Actions
:= New_List
(Assign
);
8327 Insert_Actions
(Allocator
, Actions
);
8330 -- When the function has a controlling result, an allocation-form
8331 -- parameter must be passed indicating that the caller is allocating
8332 -- the result object. This is needed because such a function can be
8333 -- called as a dispatching operation and must be treated similarly
8334 -- to functions with unconstrained result subtypes.
8336 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8337 (Func_Call
, Function_Id
, Alloc_Form
, Pool_Actual
=> Pool
);
8339 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8340 (Func_Call
, Function_Id
, Acc_Type
);
8342 Add_Task_Actuals_To_Build_In_Place_Call
8343 (Func_Call
, Function_Id
, Master_Actual
=> Master_Id
(Acc_Type
),
8346 -- Add an implicit actual to the function call that provides access
8347 -- to the allocated object. An unchecked conversion to the (specific)
8348 -- result subtype of the function is inserted to handle cases where
8349 -- the access type of the allocator has a class-wide designated type.
8351 Add_Access_Actual_To_Build_In_Place_Call
8352 (Func_Call
, Function_Id
, Return_Obj_Actual
);
8354 -- If the build-in-place function call returns a controlled object,
8355 -- the finalization master will require a reference to routine
8356 -- Finalize_Address of the designated type. Setting this attribute
8357 -- is done in the same manner to expansion of allocators.
8359 if Needs_Finalization
(Result_Subt
) then
8361 -- Controlled types with supressed finalization do not need to
8362 -- associate the address of their Finalize_Address primitives with
8363 -- a master since they do not need a master to begin with.
8365 if Is_Library_Level_Entity
(Acc_Type
)
8366 and then Finalize_Storage_Only
(Result_Subt
)
8370 -- Do not generate the call to Set_Finalize_Address in CodePeer mode
8371 -- because Finalize_Address is never built.
8373 elsif not CodePeer_Mode
then
8374 Insert_Action
(Allocator
,
8375 Make_Set_Finalize_Address_Call
(Loc
,
8376 Typ
=> Etype
(Function_Id
),
8377 Ptr_Typ
=> Acc_Type
));
8381 -- Finally, replace the allocator node with a reference to the temp
8383 Rewrite
(Allocator
, New_Occurrence_Of
(Return_Obj_Access
, Loc
));
8385 Analyze_And_Resolve
(Allocator
, Acc_Type
);
8386 end Make_Build_In_Place_Call_In_Allocator
;
8388 ---------------------------------------------------
8389 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8390 ---------------------------------------------------
8392 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8393 (Function_Call
: Node_Id
)
8396 Func_Call
: Node_Id
:= Function_Call
;
8397 Function_Id
: Entity_Id
;
8398 Result_Subt
: Entity_Id
;
8399 Return_Obj_Id
: Entity_Id
;
8400 Return_Obj_Decl
: Entity_Id
;
8403 -- Step past qualification or unchecked conversion (the latter can occur
8404 -- in cases of calls to 'Input).
8406 if Nkind_In
(Func_Call
, N_Qualified_Expression
,
8407 N_Unchecked_Type_Conversion
)
8409 Func_Call
:= Expression
(Func_Call
);
8412 -- If the call has already been processed to add build-in-place actuals
8413 -- then return. One place this can occur is for calls to build-in-place
8414 -- functions that occur within a call to a protected operation, where
8415 -- due to rewriting and expansion of the protected call there can be
8416 -- more than one call to Expand_Actuals for the same set of actuals.
8418 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
8422 -- Mark the call as processed as a build-in-place call
8424 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8426 Loc
:= Sloc
(Function_Call
);
8428 if Is_Entity_Name
(Name
(Func_Call
)) then
8429 Function_Id
:= Entity
(Name
(Func_Call
));
8431 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8432 Function_Id
:= Etype
(Name
(Func_Call
));
8435 raise Program_Error
;
8438 Result_Subt
:= Etype
(Function_Id
);
8440 -- If the build-in-place function returns a controlled object, then the
8441 -- object needs to be finalized immediately after the context. Since
8442 -- this case produces a transient scope, the servicing finalizer needs
8443 -- to name the returned object. Create a temporary which is initialized
8444 -- with the function call:
8446 -- Temp_Id : Func_Type := BIP_Func_Call;
8448 -- The initialization expression of the temporary will be rewritten by
8449 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8450 -- Call_In_Object_Declaration.
8452 if Needs_Finalization
(Result_Subt
) then
8454 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'R');
8455 Temp_Decl
: Node_Id
;
8458 -- Reset the guard on the function call since the following does
8459 -- not perform actual call expansion.
8461 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
, False);
8464 Make_Object_Declaration
(Loc
,
8465 Defining_Identifier
=> Temp_Id
,
8466 Object_Definition
=>
8467 New_Occurrence_Of
(Result_Subt
, Loc
),
8469 New_Copy_Tree
(Function_Call
));
8471 Insert_Action
(Function_Call
, Temp_Decl
);
8473 Rewrite
(Function_Call
, New_Occurrence_Of
(Temp_Id
, Loc
));
8474 Analyze
(Function_Call
);
8477 -- When the result subtype is constrained, an object of the subtype is
8478 -- declared and an access value designating it is passed as an actual.
8480 elsif Is_Constrained
(Underlying_Type
(Result_Subt
)) then
8482 -- Create a temporary object to hold the function result
8484 Return_Obj_Id
:= Make_Temporary
(Loc
, 'R');
8485 Set_Etype
(Return_Obj_Id
, Result_Subt
);
8488 Make_Object_Declaration
(Loc
,
8489 Defining_Identifier
=> Return_Obj_Id
,
8490 Aliased_Present
=> True,
8491 Object_Definition
=> New_Occurrence_Of
(Result_Subt
, Loc
));
8493 Set_No_Initialization
(Return_Obj_Decl
);
8495 Insert_Action
(Func_Call
, Return_Obj_Decl
);
8497 -- When the function has a controlling result, an allocation-form
8498 -- parameter must be passed indicating that the caller is allocating
8499 -- the result object. This is needed because such a function can be
8500 -- called as a dispatching operation and must be treated similarly
8501 -- to functions with unconstrained result subtypes.
8503 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8504 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
8506 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8507 (Func_Call
, Function_Id
);
8509 Add_Task_Actuals_To_Build_In_Place_Call
8510 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
8512 -- Add an implicit actual to the function call that provides access
8513 -- to the caller's return object.
8515 Add_Access_Actual_To_Build_In_Place_Call
8516 (Func_Call
, Function_Id
, New_Occurrence_Of
(Return_Obj_Id
, Loc
));
8518 -- When the result subtype is unconstrained, the function must allocate
8519 -- the return object in the secondary stack, so appropriate implicit
8520 -- parameters are added to the call to indicate that. A transient
8521 -- scope is established to ensure eventual cleanup of the result.
8524 -- Pass an allocation parameter indicating that the function should
8525 -- allocate its result on the secondary stack.
8527 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8528 (Func_Call
, Function_Id
, Alloc_Form
=> Secondary_Stack
);
8530 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8531 (Func_Call
, Function_Id
);
8533 Add_Task_Actuals_To_Build_In_Place_Call
8534 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
8536 -- Pass a null value to the function since no return object is
8537 -- available on the caller side.
8539 Add_Access_Actual_To_Build_In_Place_Call
8540 (Func_Call
, Function_Id
, Empty
);
8542 end Make_Build_In_Place_Call_In_Anonymous_Context
;
8544 --------------------------------------------
8545 -- Make_Build_In_Place_Call_In_Assignment --
8546 --------------------------------------------
8548 procedure Make_Build_In_Place_Call_In_Assignment
8550 Function_Call
: Node_Id
)
8552 Lhs
: constant Node_Id
:= Name
(Assign
);
8553 Func_Call
: Node_Id
:= Function_Call
;
8554 Func_Id
: Entity_Id
;
8558 Ptr_Typ
: Entity_Id
;
8559 Ptr_Typ_Decl
: Node_Id
;
8561 Result_Subt
: Entity_Id
;
8565 -- Step past qualification or unchecked conversion (the latter can occur
8566 -- in cases of calls to 'Input).
8568 if Nkind_In
(Func_Call
, N_Qualified_Expression
,
8569 N_Unchecked_Type_Conversion
)
8571 Func_Call
:= Expression
(Func_Call
);
8574 -- If the call has already been processed to add build-in-place actuals
8575 -- then return. This should not normally occur in an assignment context,
8576 -- but we add the protection as a defensive measure.
8578 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
8582 -- Mark the call as processed as a build-in-place call
8584 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8586 Loc
:= Sloc
(Function_Call
);
8588 if Is_Entity_Name
(Name
(Func_Call
)) then
8589 Func_Id
:= Entity
(Name
(Func_Call
));
8591 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8592 Func_Id
:= Etype
(Name
(Func_Call
));
8595 raise Program_Error
;
8598 Result_Subt
:= Etype
(Func_Id
);
8600 -- When the result subtype is unconstrained, an additional actual must
8601 -- be passed to indicate that the caller is providing the return object.
8602 -- This parameter must also be passed when the called function has a
8603 -- controlling result, because dispatching calls to the function needs
8604 -- to be treated effectively the same as calls to class-wide functions.
8606 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8607 (Func_Call
, Func_Id
, Alloc_Form
=> Caller_Allocation
);
8609 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8610 (Func_Call
, Func_Id
);
8612 Add_Task_Actuals_To_Build_In_Place_Call
8613 (Func_Call
, Func_Id
, Make_Identifier
(Loc
, Name_uMaster
));
8615 -- Add an implicit actual to the function call that provides access to
8616 -- the caller's return object.
8618 Add_Access_Actual_To_Build_In_Place_Call
8621 Make_Unchecked_Type_Conversion
(Loc
,
8622 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Loc
),
8623 Expression
=> Relocate_Node
(Lhs
)));
8625 -- Create an access type designating the function's result subtype
8627 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
8630 Make_Full_Type_Declaration
(Loc
,
8631 Defining_Identifier
=> Ptr_Typ
,
8633 Make_Access_To_Object_Definition
(Loc
,
8634 All_Present
=> True,
8635 Subtype_Indication
=>
8636 New_Occurrence_Of
(Result_Subt
, Loc
)));
8637 Insert_After_And_Analyze
(Assign
, Ptr_Typ_Decl
);
8639 -- Finally, create an access object initialized to a reference to the
8640 -- function call. We know this access value is non-null, so mark the
8641 -- entity accordingly to suppress junk access checks.
8643 New_Expr
:= Make_Reference
(Loc
, Relocate_Node
(Func_Call
));
8645 Obj_Id
:= Make_Temporary
(Loc
, 'R', New_Expr
);
8646 Set_Etype
(Obj_Id
, Ptr_Typ
);
8647 Set_Is_Known_Non_Null
(Obj_Id
);
8650 Make_Object_Declaration
(Loc
,
8651 Defining_Identifier
=> Obj_Id
,
8652 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
8653 Expression
=> New_Expr
);
8654 Insert_After_And_Analyze
(Ptr_Typ_Decl
, Obj_Decl
);
8656 Rewrite
(Assign
, Make_Null_Statement
(Loc
));
8658 -- Retrieve the target of the assignment
8660 if Nkind
(Lhs
) = N_Selected_Component
then
8661 Target
:= Selector_Name
(Lhs
);
8662 elsif Nkind
(Lhs
) = N_Type_Conversion
then
8663 Target
:= Expression
(Lhs
);
8668 -- If we are assigning to a return object or this is an expression of
8669 -- an extension aggregate, the target should either be an identifier
8670 -- or a simple expression. All other cases imply a different scenario.
8672 if Nkind
(Target
) in N_Has_Entity
then
8673 Target
:= Entity
(Target
);
8677 end Make_Build_In_Place_Call_In_Assignment
;
8679 ----------------------------------------------------
8680 -- Make_Build_In_Place_Call_In_Object_Declaration --
8681 ----------------------------------------------------
8683 procedure Make_Build_In_Place_Call_In_Object_Declaration
8684 (Object_Decl
: Node_Id
;
8685 Function_Call
: Node_Id
)
8688 Obj_Def_Id
: constant Entity_Id
:=
8689 Defining_Identifier
(Object_Decl
);
8690 Enclosing_Func
: constant Entity_Id
:=
8691 Enclosing_Subprogram
(Obj_Def_Id
);
8692 Call_Deref
: Node_Id
;
8693 Caller_Object
: Node_Id
;
8695 Fmaster_Actual
: Node_Id
:= Empty
;
8696 Func_Call
: Node_Id
:= Function_Call
;
8697 Function_Id
: Entity_Id
;
8698 Pool_Actual
: Node_Id
;
8699 Ptr_Typ
: Entity_Id
;
8700 Ptr_Typ_Decl
: Node_Id
;
8701 Pass_Caller_Acc
: Boolean := False;
8703 Result_Subt
: Entity_Id
;
8706 -- Step past qualification or unchecked conversion (the latter can occur
8707 -- in cases of calls to 'Input).
8709 if Nkind_In
(Func_Call
, N_Qualified_Expression
,
8710 N_Unchecked_Type_Conversion
)
8712 Func_Call
:= Expression
(Func_Call
);
8715 -- If the call has already been processed to add build-in-place actuals
8716 -- then return. This should not normally occur in an object declaration,
8717 -- but we add the protection as a defensive measure.
8719 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
8723 -- Mark the call as processed as a build-in-place call
8725 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8727 Loc
:= Sloc
(Function_Call
);
8729 if Is_Entity_Name
(Name
(Func_Call
)) then
8730 Function_Id
:= Entity
(Name
(Func_Call
));
8732 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8733 Function_Id
:= Etype
(Name
(Func_Call
));
8736 raise Program_Error
;
8739 Result_Subt
:= Etype
(Function_Id
);
8741 -- Create an access type designating the function's result subtype. We
8742 -- use the type of the original call because it may be a call to an
8743 -- inherited operation, which the expansion has replaced with the parent
8744 -- operation that yields the parent type. Note that this access type
8745 -- must be declared before we establish a transient scope, so that it
8746 -- receives the proper accessibility level.
8748 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
8750 Make_Full_Type_Declaration
(Loc
,
8751 Defining_Identifier
=> Ptr_Typ
,
8753 Make_Access_To_Object_Definition
(Loc
,
8754 All_Present
=> True,
8755 Subtype_Indication
=>
8756 New_Occurrence_Of
(Etype
(Function_Call
), Loc
)));
8758 -- The access type and its accompanying object must be inserted after
8759 -- the object declaration in the constrained case, so that the function
8760 -- call can be passed access to the object. In the unconstrained case,
8761 -- or if the object declaration is for a return object, the access type
8762 -- and object must be inserted before the object, since the object
8763 -- declaration is rewritten to be a renaming of a dereference of the
8764 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8765 -- the result object is in a different (transient) scope, so won't
8768 if Is_Constrained
(Underlying_Type
(Result_Subt
))
8769 and then not Is_Return_Object
(Defining_Identifier
(Object_Decl
))
8771 Insert_After_And_Analyze
(Object_Decl
, Ptr_Typ_Decl
);
8773 Insert_Action
(Object_Decl
, Ptr_Typ_Decl
);
8776 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
8777 -- elaborated in an inner (transient) scope and thus won't cause
8778 -- freezing by itself.
8781 Ptr_Typ_Freeze_Ref
: constant Node_Id
:=
8782 New_Occurrence_Of
(Ptr_Typ
, Loc
);
8784 Set_Parent
(Ptr_Typ_Freeze_Ref
, Ptr_Typ_Decl
);
8785 Freeze_Expression
(Ptr_Typ_Freeze_Ref
);
8788 -- If the the object is a return object of an enclosing build-in-place
8789 -- function, then the implicit build-in-place parameters of the
8790 -- enclosing function are simply passed along to the called function.
8791 -- (Unfortunately, this won't cover the case of extension aggregates
8792 -- where the ancestor part is a build-in-place unconstrained function
8793 -- call that should be passed along the caller's parameters. Currently
8794 -- those get mishandled by reassigning the result of the call to the
8795 -- aggregate return object, when the call result should really be
8796 -- directly built in place in the aggregate and not in a temporary. ???)
8798 if Is_Return_Object
(Defining_Identifier
(Object_Decl
)) then
8799 Pass_Caller_Acc
:= True;
8801 -- When the enclosing function has a BIP_Alloc_Form formal then we
8802 -- pass it along to the callee (such as when the enclosing function
8803 -- has an unconstrained or tagged result type).
8805 if Needs_BIP_Alloc_Form
(Enclosing_Func
) then
8806 if VM_Target
= No_VM
and then
8807 RTE_Available
(RE_Root_Storage_Pool_Ptr
)
8810 New_Occurrence_Of
(Build_In_Place_Formal
8811 (Enclosing_Func
, BIP_Storage_Pool
), Loc
);
8813 -- The build-in-place pool formal is not built on .NET/JVM
8816 Pool_Actual
:= Empty
;
8819 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8824 (Build_In_Place_Formal
(Enclosing_Func
, BIP_Alloc_Form
),
8826 Pool_Actual
=> Pool_Actual
);
8828 -- Otherwise, if enclosing function has a constrained result subtype,
8829 -- then caller allocation will be used.
8832 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8833 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
8836 if Needs_BIP_Finalization_Master
(Enclosing_Func
) then
8839 (Build_In_Place_Formal
8840 (Enclosing_Func
, BIP_Finalization_Master
), Loc
);
8843 -- Retrieve the BIPacc formal from the enclosing function and convert
8844 -- it to the access type of the callee's BIP_Object_Access formal.
8847 Make_Unchecked_Type_Conversion
(Loc
,
8851 (Build_In_Place_Formal
(Function_Id
, BIP_Object_Access
)),
8855 (Build_In_Place_Formal
(Enclosing_Func
, BIP_Object_Access
),
8858 -- In the constrained case, add an implicit actual to the function call
8859 -- that provides access to the declared object. An unchecked conversion
8860 -- to the (specific) result type of the function is inserted to handle
8861 -- the case where the object is declared with a class-wide type.
8863 elsif Is_Constrained
(Underlying_Type
(Result_Subt
)) then
8865 Make_Unchecked_Type_Conversion
(Loc
,
8866 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Loc
),
8867 Expression
=> New_Occurrence_Of
(Obj_Def_Id
, Loc
));
8869 -- When the function has a controlling result, an allocation-form
8870 -- parameter must be passed indicating that the caller is allocating
8871 -- the result object. This is needed because such a function can be
8872 -- called as a dispatching operation and must be treated similarly
8873 -- to functions with unconstrained result subtypes.
8875 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8876 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
8878 -- In other unconstrained cases, pass an indication to do the allocation
8879 -- on the secondary stack and set Caller_Object to Empty so that a null
8880 -- value will be passed for the caller's object address. A transient
8881 -- scope is established to ensure eventual cleanup of the result.
8884 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8885 (Func_Call
, Function_Id
, Alloc_Form
=> Secondary_Stack
);
8886 Caller_Object
:= Empty
;
8888 Establish_Transient_Scope
(Object_Decl
, Sec_Stack
=> True);
8891 -- Pass along any finalization master actual, which is needed in the
8892 -- case where the called function initializes a return object of an
8893 -- enclosing build-in-place function.
8895 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8896 (Func_Call
=> Func_Call
,
8897 Func_Id
=> Function_Id
,
8898 Master_Exp
=> Fmaster_Actual
);
8900 if Nkind
(Parent
(Object_Decl
)) = N_Extended_Return_Statement
8901 and then Has_Task
(Result_Subt
)
8903 -- Here we're passing along the master that was passed in to this
8906 Add_Task_Actuals_To_Build_In_Place_Call
8907 (Func_Call
, Function_Id
,
8909 New_Occurrence_Of
(Build_In_Place_Formal
8910 (Enclosing_Func
, BIP_Task_Master
), Loc
));
8913 Add_Task_Actuals_To_Build_In_Place_Call
8914 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
8917 Add_Access_Actual_To_Build_In_Place_Call
8918 (Func_Call
, Function_Id
, Caller_Object
, Is_Access
=> Pass_Caller_Acc
);
8920 -- Finally, create an access object initialized to a reference to the
8921 -- function call. We know this access value cannot be null, so mark the
8922 -- entity accordingly to suppress the access check.
8924 Def_Id
:= Make_Temporary
(Loc
, 'R', Func_Call
);
8925 Set_Etype
(Def_Id
, Ptr_Typ
);
8926 Set_Is_Known_Non_Null
(Def_Id
);
8929 Make_Object_Declaration
(Loc
,
8930 Defining_Identifier
=> Def_Id
,
8931 Constant_Present
=> True,
8932 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
8934 Make_Reference
(Loc
, Relocate_Node
(Func_Call
)));
8936 Insert_After_And_Analyze
(Ptr_Typ_Decl
, Res_Decl
);
8938 -- If the result subtype of the called function is constrained and
8939 -- is not itself the return expression of an enclosing BIP function,
8940 -- then mark the object as having no initialization.
8942 if Is_Constrained
(Underlying_Type
(Result_Subt
))
8943 and then not Is_Return_Object
(Defining_Identifier
(Object_Decl
))
8945 -- The related object declaration is encased in a transient block
8946 -- because the build-in-place function call contains at least one
8947 -- nested function call that produces a controlled transient
8950 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
8952 -- Since the build-in-place expansion decouples the call from the
8953 -- object declaration, the finalization machinery lacks the context
8954 -- which prompted the generation of the transient block. To resolve
8955 -- this scenario, store the build-in-place call.
8957 if Scope_Is_Transient
8958 and then Node_To_Be_Wrapped
= Object_Decl
8960 Set_BIP_Initialization_Call
(Obj_Def_Id
, Res_Decl
);
8963 Set_Expression
(Object_Decl
, Empty
);
8964 Set_No_Initialization
(Object_Decl
);
8966 -- In case of an unconstrained result subtype, or if the call is the
8967 -- return expression of an enclosing BIP function, rewrite the object
8968 -- declaration as an object renaming where the renamed object is a
8969 -- dereference of <function_Call>'reference:
8971 -- Obj : Subt renames <function_call>'Ref.all;
8975 Make_Explicit_Dereference
(Loc
,
8976 Prefix
=> New_Occurrence_Of
(Def_Id
, Loc
));
8978 Loc
:= Sloc
(Object_Decl
);
8979 Rewrite
(Object_Decl
,
8980 Make_Object_Renaming_Declaration
(Loc
,
8981 Defining_Identifier
=> Make_Temporary
(Loc
, 'D'),
8982 Access_Definition
=> Empty
,
8983 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Loc
),
8984 Name
=> Call_Deref
));
8986 Set_Renamed_Object
(Defining_Identifier
(Object_Decl
), Call_Deref
);
8988 Analyze
(Object_Decl
);
8990 -- Replace the internal identifier of the renaming declaration's
8991 -- entity with identifier of the original object entity. We also have
8992 -- to exchange the entities containing their defining identifiers to
8993 -- ensure the correct replacement of the object declaration by the
8994 -- object renaming declaration to avoid homograph conflicts (since
8995 -- the object declaration's defining identifier was already entered
8996 -- in current scope). The Next_Entity links of the two entities also
8997 -- have to be swapped since the entities are part of the return
8998 -- scope's entity list and the list structure would otherwise be
8999 -- corrupted. Finally, the homonym chain must be preserved as well.
9002 Renaming_Def_Id
: constant Entity_Id
:=
9003 Defining_Identifier
(Object_Decl
);
9004 Next_Entity_Temp
: constant Entity_Id
:=
9005 Next_Entity
(Renaming_Def_Id
);
9007 Set_Chars
(Renaming_Def_Id
, Chars
(Obj_Def_Id
));
9009 -- Swap next entity links in preparation for exchanging entities
9011 Set_Next_Entity
(Renaming_Def_Id
, Next_Entity
(Obj_Def_Id
));
9012 Set_Next_Entity
(Obj_Def_Id
, Next_Entity_Temp
);
9013 Set_Homonym
(Renaming_Def_Id
, Homonym
(Obj_Def_Id
));
9015 Exchange_Entities
(Renaming_Def_Id
, Obj_Def_Id
);
9017 -- Preserve source indication of original declaration, so that
9018 -- xref information is properly generated for the right entity.
9020 Preserve_Comes_From_Source
9021 (Object_Decl
, Original_Node
(Object_Decl
));
9023 Preserve_Comes_From_Source
9024 (Obj_Def_Id
, Original_Node
(Object_Decl
));
9026 Set_Comes_From_Source
(Renaming_Def_Id
, False);
9030 -- If the object entity has a class-wide Etype, then we need to change
9031 -- it to the result subtype of the function call, because otherwise the
9032 -- object will be class-wide without an explicit initialization and
9033 -- won't be allocated properly by the back end. It seems unclean to make
9034 -- such a revision to the type at this point, and we should try to
9035 -- improve this treatment when build-in-place functions with class-wide
9036 -- results are implemented. ???
9038 if Is_Class_Wide_Type
(Etype
(Defining_Identifier
(Object_Decl
))) then
9039 Set_Etype
(Defining_Identifier
(Object_Decl
), Result_Subt
);
9041 end Make_Build_In_Place_Call_In_Object_Declaration
;
9043 --------------------------------------------
9044 -- Make_CPP_Constructor_Call_In_Allocator --
9045 --------------------------------------------
9047 procedure Make_CPP_Constructor_Call_In_Allocator
9048 (Allocator
: Node_Id
;
9049 Function_Call
: Node_Id
)
9051 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
9052 Acc_Type
: constant Entity_Id
:= Etype
(Allocator
);
9053 Function_Id
: constant Entity_Id
:= Entity
(Name
(Function_Call
));
9054 Result_Subt
: constant Entity_Id
:= Available_View
(Etype
(Function_Id
));
9056 New_Allocator
: Node_Id
;
9057 Return_Obj_Access
: Entity_Id
;
9061 pragma Assert
(Nkind
(Allocator
) = N_Allocator
9062 and then Nkind
(Function_Call
) = N_Function_Call
);
9063 pragma Assert
(Convention
(Function_Id
) = Convention_CPP
9064 and then Is_Constructor
(Function_Id
));
9065 pragma Assert
(Is_Constrained
(Underlying_Type
(Result_Subt
)));
9067 -- Replace the initialized allocator of form "new T'(Func (...))" with
9068 -- an uninitialized allocator of form "new T", where T is the result
9069 -- subtype of the called function. The call to the function is handled
9070 -- separately further below.
9073 Make_Allocator
(Loc
,
9074 Expression
=> New_Occurrence_Of
(Result_Subt
, Loc
));
9075 Set_No_Initialization
(New_Allocator
);
9077 -- Copy attributes to new allocator. Note that the new allocator
9078 -- logically comes from source if the original one did, so copy the
9079 -- relevant flag. This ensures proper treatment of the restriction
9080 -- No_Implicit_Heap_Allocations in this case.
9082 Set_Storage_Pool
(New_Allocator
, Storage_Pool
(Allocator
));
9083 Set_Procedure_To_Call
(New_Allocator
, Procedure_To_Call
(Allocator
));
9084 Set_Comes_From_Source
(New_Allocator
, Comes_From_Source
(Allocator
));
9086 Rewrite
(Allocator
, New_Allocator
);
9088 -- Create a new access object and initialize it to the result of the
9089 -- new uninitialized allocator. Note: we do not use Allocator as the
9090 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9091 -- as this would create a sort of infinite "recursion".
9093 Return_Obj_Access
:= Make_Temporary
(Loc
, 'R');
9094 Set_Etype
(Return_Obj_Access
, Acc_Type
);
9097 -- Rnnn : constant ptr_T := new (T);
9098 -- Init (Rnn.all,...);
9101 Make_Object_Declaration
(Loc
,
9102 Defining_Identifier
=> Return_Obj_Access
,
9103 Constant_Present
=> True,
9104 Object_Definition
=> New_Occurrence_Of
(Acc_Type
, Loc
),
9105 Expression
=> Relocate_Node
(Allocator
));
9106 Insert_Action
(Allocator
, Tmp_Obj
);
9108 Insert_List_After_And_Analyze
(Tmp_Obj
,
9109 Build_Initialization_Call
(Loc
,
9111 Make_Explicit_Dereference
(Loc
,
9112 Prefix
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
)),
9113 Typ
=> Etype
(Function_Id
),
9114 Constructor_Ref
=> Function_Call
));
9116 -- Finally, replace the allocator node with a reference to the result of
9117 -- the function call itself (which will effectively be an access to the
9118 -- object created by the allocator).
9120 Rewrite
(Allocator
, New_Occurrence_Of
(Return_Obj_Access
, Loc
));
9122 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9123 -- generate an implicit conversion to force displacement of the "this"
9126 if Is_Interface
(Designated_Type
(Acc_Type
)) then
9127 Rewrite
(Allocator
, Convert_To
(Acc_Type
, Relocate_Node
(Allocator
)));
9130 Analyze_And_Resolve
(Allocator
, Acc_Type
);
9131 end Make_CPP_Constructor_Call_In_Allocator
;
9133 -----------------------------------
9134 -- Needs_BIP_Finalization_Master --
9135 -----------------------------------
9137 function Needs_BIP_Finalization_Master
9138 (Func_Id
: Entity_Id
) return Boolean
9140 pragma Assert
(Is_Build_In_Place_Function
(Func_Id
));
9141 Func_Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
9144 not Restriction_Active
(No_Finalization
)
9145 and then Needs_Finalization
(Func_Typ
);
9146 end Needs_BIP_Finalization_Master
;
9148 --------------------------
9149 -- Needs_BIP_Alloc_Form --
9150 --------------------------
9152 function Needs_BIP_Alloc_Form
(Func_Id
: Entity_Id
) return Boolean is
9153 pragma Assert
(Is_Build_In_Place_Function
(Func_Id
));
9154 Func_Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
9156 return not Is_Constrained
(Func_Typ
) or else Is_Tagged_Type
(Func_Typ
);
9157 end Needs_BIP_Alloc_Form
;
9159 --------------------------------------
9160 -- Needs_Result_Accessibility_Level --
9161 --------------------------------------
9163 function Needs_Result_Accessibility_Level
9164 (Func_Id
: Entity_Id
) return Boolean
9166 Func_Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
9168 function Has_Unconstrained_Access_Discriminant_Component
9169 (Comp_Typ
: Entity_Id
) return Boolean;
9170 -- Returns True if any component of the type has an unconstrained access
9173 -----------------------------------------------------
9174 -- Has_Unconstrained_Access_Discriminant_Component --
9175 -----------------------------------------------------
9177 function Has_Unconstrained_Access_Discriminant_Component
9178 (Comp_Typ
: Entity_Id
) return Boolean
9181 if not Is_Limited_Type
(Comp_Typ
) then
9184 -- Only limited types can have access discriminants with
9187 elsif Has_Unconstrained_Access_Discriminants
(Comp_Typ
) then
9190 elsif Is_Array_Type
(Comp_Typ
) then
9191 return Has_Unconstrained_Access_Discriminant_Component
9192 (Underlying_Type
(Component_Type
(Comp_Typ
)));
9194 elsif Is_Record_Type
(Comp_Typ
) then
9199 Comp
:= First_Component
(Comp_Typ
);
9200 while Present
(Comp
) loop
9201 if Has_Unconstrained_Access_Discriminant_Component
9202 (Underlying_Type
(Etype
(Comp
)))
9207 Next_Component
(Comp
);
9213 end Has_Unconstrained_Access_Discriminant_Component
;
9215 Feature_Disabled
: constant Boolean := True;
9218 -- Start of processing for Needs_Result_Accessibility_Level
9221 -- False if completion unavailable (how does this happen???)
9223 if not Present
(Func_Typ
) then
9226 elsif Feature_Disabled
then
9229 -- False if not a function, also handle enum-lit renames case
9231 elsif Func_Typ
= Standard_Void_Type
9232 or else Is_Scalar_Type
(Func_Typ
)
9236 -- Handle a corner case, a cross-dialect subp renaming. For example,
9237 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
9238 -- an Ada 2005 (or earlier) unit references predefined run-time units.
9240 elsif Present
(Alias
(Func_Id
)) then
9242 -- Unimplemented: a cross-dialect subp renaming which does not set
9243 -- the Alias attribute (e.g., a rename of a dereference of an access
9244 -- to subprogram value). ???
9246 return Present
(Extra_Accessibility_Of_Result
(Alias
(Func_Id
)));
9248 -- Remaining cases require Ada 2012 mode
9250 elsif Ada_Version
< Ada_2012
then
9253 elsif Ekind
(Func_Typ
) = E_Anonymous_Access_Type
9254 or else Is_Tagged_Type
(Func_Typ
)
9256 -- In the case of, say, a null tagged record result type, the need
9257 -- for this extra parameter might not be obvious. This function
9258 -- returns True for all tagged types for compatibility reasons.
9259 -- A function with, say, a tagged null controlling result type might
9260 -- be overridden by a primitive of an extension having an access
9261 -- discriminant and the overrider and overridden must have compatible
9262 -- calling conventions (including implicitly declared parameters).
9263 -- Similarly, values of one access-to-subprogram type might designate
9264 -- both a primitive subprogram of a given type and a function
9265 -- which is, for example, not a primitive subprogram of any type.
9266 -- Again, this requires calling convention compatibility.
9267 -- It might be possible to solve these issues by introducing
9268 -- wrappers, but that is not the approach that was chosen.
9272 elsif Has_Unconstrained_Access_Discriminants
(Func_Typ
) then
9275 elsif Has_Unconstrained_Access_Discriminant_Component
(Func_Typ
) then
9278 -- False for all other cases
9283 end Needs_Result_Accessibility_Level
;