1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, 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 Aspects
; use Aspects
;
27 with Atree
; use Atree
;
28 with Checks
; use Checks
;
29 with Debug
; use Debug
;
30 with Einfo
; use Einfo
;
31 with Errout
; use Errout
;
32 with Elists
; use Elists
;
33 with Exp_Aggr
; use Exp_Aggr
;
34 with Exp_Atag
; use Exp_Atag
;
35 with Exp_Ch2
; use Exp_Ch2
;
36 with Exp_Ch3
; use Exp_Ch3
;
37 with Exp_Ch7
; use Exp_Ch7
;
38 with Exp_Ch9
; use Exp_Ch9
;
39 with Exp_Dbug
; use Exp_Dbug
;
40 with Exp_Disp
; use Exp_Disp
;
41 with Exp_Dist
; use Exp_Dist
;
42 with Exp_Intr
; use Exp_Intr
;
43 with Exp_Pakd
; use Exp_Pakd
;
44 with Exp_Tss
; use Exp_Tss
;
45 with Exp_Util
; use Exp_Util
;
46 with Exp_VFpt
; use Exp_VFpt
;
47 with Fname
; use Fname
;
48 with Freeze
; use Freeze
;
49 with Inline
; use Inline
;
51 with Namet
; use Namet
;
52 with Nlists
; use Nlists
;
53 with Nmake
; use Nmake
;
55 with Output
; use Output
;
56 with Restrict
; use Restrict
;
57 with Rident
; use Rident
;
58 with Rtsfind
; use Rtsfind
;
60 with Sem_Aux
; use Sem_Aux
;
61 with Sem_Ch6
; use Sem_Ch6
;
62 with Sem_Ch8
; use Sem_Ch8
;
63 with Sem_Ch12
; use Sem_Ch12
;
64 with Sem_Ch13
; use Sem_Ch13
;
65 with Sem_Dim
; use Sem_Dim
;
66 with Sem_Disp
; use Sem_Disp
;
67 with Sem_Dist
; use Sem_Dist
;
68 with Sem_Eval
; use Sem_Eval
;
69 with Sem_Mech
; use Sem_Mech
;
70 with Sem_Res
; use Sem_Res
;
71 with Sem_SCIL
; use Sem_SCIL
;
72 with Sem_Util
; use Sem_Util
;
73 with Sinfo
; use Sinfo
;
74 with Sinput
; use Sinput
;
75 with Snames
; use Snames
;
76 with Stand
; use Stand
;
77 with Stringt
; use Stringt
;
78 with Targparm
; use Targparm
;
79 with Tbuild
; use Tbuild
;
80 with Uintp
; use Uintp
;
81 with Validsw
; use Validsw
;
83 package body Exp_Ch6
is
85 Inlined_Calls
: Elist_Id
:= No_Elist
;
86 Backend_Calls
: Elist_Id
:= No_Elist
;
87 -- List of frontend inlined calls and inline calls passed to the backend
89 -----------------------
90 -- Local Subprograms --
91 -----------------------
93 procedure Add_Access_Actual_To_Build_In_Place_Call
94 (Function_Call
: Node_Id
;
95 Function_Id
: Entity_Id
;
96 Return_Object
: Node_Id
;
97 Is_Access
: Boolean := False);
98 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
99 -- object name given by Return_Object and add the attribute to the end of
100 -- the actual parameter list associated with the build-in-place function
101 -- call denoted by Function_Call. However, if Is_Access is True, then
102 -- Return_Object is already an access expression, in which case it's passed
103 -- along directly to the build-in-place function. Finally, if Return_Object
104 -- is empty, then pass a null literal as the actual.
106 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
107 (Function_Call
: Node_Id
;
108 Function_Id
: Entity_Id
;
109 Alloc_Form
: BIP_Allocation_Form
:= Unspecified
;
110 Alloc_Form_Exp
: Node_Id
:= Empty
;
111 Pool_Actual
: Node_Id
:= Make_Null
(No_Location
));
112 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
113 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
114 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
115 -- otherwise pass a literal corresponding to the Alloc_Form parameter
116 -- (which must not be Unspecified in that case). Pool_Actual is the
117 -- parameter to pass to BIP_Storage_Pool.
119 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
120 (Func_Call
: Node_Id
;
122 Ptr_Typ
: Entity_Id
:= Empty
;
123 Master_Exp
: Node_Id
:= Empty
);
124 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
125 -- finalization actions, add an actual parameter which is a pointer to the
126 -- finalization master of the caller. If Master_Exp is not Empty, then that
127 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
128 -- will result in an automatic "null" value for the actual.
130 procedure Add_Task_Actuals_To_Build_In_Place_Call
131 (Function_Call
: Node_Id
;
132 Function_Id
: Entity_Id
;
133 Master_Actual
: Node_Id
);
134 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
135 -- contains tasks, add two actual parameters: the master, and a pointer to
136 -- the caller's activation chain. Master_Actual is the actual parameter
137 -- expression to pass for the master. In most cases, this is the current
138 -- master (_master). The two exceptions are: If the function call is the
139 -- initialization expression for an allocator, we pass the master of the
140 -- access type. If the function call is the initialization expression for a
141 -- return object, we pass along the master passed in by the caller. The
142 -- activation chain to pass is always the local one. Note: Master_Actual
143 -- can be Empty, but only if there are no tasks.
145 procedure Check_Overriding_Operation
(Subp
: Entity_Id
);
146 -- Subp is a dispatching operation. Check whether it may override an
147 -- inherited private operation, in which case its DT entry is that of
148 -- the hidden operation, not the one it may have received earlier.
149 -- This must be done before emitting the code to set the corresponding
150 -- DT to the address of the subprogram. The actual placement of Subp in
151 -- the proper place in the list of primitive operations is done in
152 -- Declare_Inherited_Private_Subprograms, which also has to deal with
153 -- implicit operations. This duplication is unavoidable for now???
155 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
);
156 -- This procedure is called only if the subprogram body N, whose spec
157 -- has the given entity Spec, contains a parameterless recursive call.
158 -- It attempts to generate runtime code to detect if this a case of
159 -- infinite recursion.
161 -- The body is scanned to determine dependencies. If the only external
162 -- dependencies are on a small set of scalar variables, then the values
163 -- of these variables are captured on entry to the subprogram, and if
164 -- the values are not changed for the call, we know immediately that
165 -- we have an infinite recursion.
167 procedure Expand_Ctrl_Function_Call
(N
: Node_Id
);
168 -- N is a function call which returns a controlled object. Transform the
169 -- call into a temporary which retrieves the returned object from the
170 -- secondary stack using 'reference.
172 procedure Expand_Inlined_Call
175 Orig_Subp
: Entity_Id
);
176 -- If called subprogram can be inlined by the front-end, retrieve the
177 -- analyzed body, replace formals with actuals and expand call in place.
178 -- Generate thunks for actuals that are expressions, and insert the
179 -- corresponding constant declarations before the call. If the original
180 -- call is to a derived operation, the return type is the one of the
181 -- derived operation, but the body is that of the original, so return
182 -- expressions in the body must be converted to the desired type (which
183 -- is simply not noted in the tree without inline expansion).
185 procedure Expand_Non_Function_Return
(N
: Node_Id
);
186 -- Called by Expand_N_Simple_Return_Statement in case we're returning from
187 -- a procedure body, entry body, accept statement, or extended return
188 -- statement. Note that all non-function returns are simple return
191 function Expand_Protected_Object_Reference
193 Scop
: Entity_Id
) return Node_Id
;
195 procedure Expand_Protected_Subprogram_Call
199 -- A call to a protected subprogram within the protected object may appear
200 -- as a regular call. The list of actuals must be expanded to contain a
201 -- reference to the object itself, and the call becomes a call to the
202 -- corresponding protected subprogram.
204 function Has_Unconstrained_Access_Discriminants
205 (Subtyp
: Entity_Id
) return Boolean;
206 -- Returns True if the given subtype is unconstrained and has one
207 -- or more access discriminants.
209 procedure Expand_Simple_Function_Return
(N
: Node_Id
);
210 -- Expand simple return from function. In the case where we are returning
211 -- from a function body this is called by Expand_N_Simple_Return_Statement.
213 ----------------------------------------------
214 -- Add_Access_Actual_To_Build_In_Place_Call --
215 ----------------------------------------------
217 procedure Add_Access_Actual_To_Build_In_Place_Call
218 (Function_Call
: Node_Id
;
219 Function_Id
: Entity_Id
;
220 Return_Object
: Node_Id
;
221 Is_Access
: Boolean := False)
223 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
224 Obj_Address
: Node_Id
;
225 Obj_Acc_Formal
: Entity_Id
;
228 -- Locate the implicit access parameter in the called function
230 Obj_Acc_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Object_Access
);
232 -- If no return object is provided, then pass null
234 if not Present
(Return_Object
) then
235 Obj_Address
:= Make_Null
(Loc
);
236 Set_Parent
(Obj_Address
, Function_Call
);
238 -- If Return_Object is already an expression of an access type, then use
239 -- it directly, since it must be an access value denoting the return
240 -- object, and couldn't possibly be the return object itself.
243 Obj_Address
:= Return_Object
;
244 Set_Parent
(Obj_Address
, Function_Call
);
246 -- Apply Unrestricted_Access to caller's return object
250 Make_Attribute_Reference
(Loc
,
251 Prefix
=> Return_Object
,
252 Attribute_Name
=> Name_Unrestricted_Access
);
254 Set_Parent
(Return_Object
, Obj_Address
);
255 Set_Parent
(Obj_Address
, Function_Call
);
258 Analyze_And_Resolve
(Obj_Address
, Etype
(Obj_Acc_Formal
));
260 -- Build the parameter association for the new actual and add it to the
261 -- end of the function's actuals.
263 Add_Extra_Actual_To_Call
(Function_Call
, Obj_Acc_Formal
, Obj_Address
);
264 end Add_Access_Actual_To_Build_In_Place_Call
;
266 ------------------------------------------------------
267 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
268 ------------------------------------------------------
270 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
271 (Function_Call
: Node_Id
;
272 Function_Id
: Entity_Id
;
273 Alloc_Form
: BIP_Allocation_Form
:= Unspecified
;
274 Alloc_Form_Exp
: Node_Id
:= Empty
;
275 Pool_Actual
: Node_Id
:= Make_Null
(No_Location
))
277 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
278 Alloc_Form_Actual
: Node_Id
;
279 Alloc_Form_Formal
: Node_Id
;
280 Pool_Formal
: Node_Id
;
283 -- The allocation form generally doesn't need to be passed in the case
284 -- of a constrained result subtype, since normally the caller performs
285 -- the allocation in that case. However this formal is still needed in
286 -- the case where the function has a tagged result, because generally
287 -- such functions can be called in a dispatching context and such calls
288 -- must be handled like calls to class-wide functions.
290 if Is_Constrained
(Underlying_Type
(Etype
(Function_Id
)))
291 and then not Is_Tagged_Type
(Underlying_Type
(Etype
(Function_Id
)))
296 -- Locate the implicit allocation form parameter in the called function.
297 -- Maybe it would be better for each implicit formal of a build-in-place
298 -- function to have a flag or a Uint attribute to identify it. ???
300 Alloc_Form_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Alloc_Form
);
302 if Present
(Alloc_Form_Exp
) then
303 pragma Assert
(Alloc_Form
= Unspecified
);
305 Alloc_Form_Actual
:= Alloc_Form_Exp
;
308 pragma Assert
(Alloc_Form
/= Unspecified
);
311 Make_Integer_Literal
(Loc
,
312 Intval
=> UI_From_Int
(BIP_Allocation_Form
'Pos (Alloc_Form
)));
315 Analyze_And_Resolve
(Alloc_Form_Actual
, Etype
(Alloc_Form_Formal
));
317 -- Build the parameter association for the new actual and add it to the
318 -- end of the function's actuals.
320 Add_Extra_Actual_To_Call
321 (Function_Call
, Alloc_Form_Formal
, Alloc_Form_Actual
);
323 -- Pass the Storage_Pool parameter. This parameter is omitted on
324 -- .NET/JVM/ZFP as those targets do not support pools.
327 and then RTE_Available
(RE_Root_Storage_Pool_Ptr
)
329 Pool_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Storage_Pool
);
330 Analyze_And_Resolve
(Pool_Actual
, Etype
(Pool_Formal
));
331 Add_Extra_Actual_To_Call
332 (Function_Call
, Pool_Formal
, Pool_Actual
);
334 end Add_Unconstrained_Actuals_To_Build_In_Place_Call
;
336 -----------------------------------------------------------
337 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
338 -----------------------------------------------------------
340 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
341 (Func_Call
: Node_Id
;
343 Ptr_Typ
: Entity_Id
:= Empty
;
344 Master_Exp
: Node_Id
:= Empty
)
347 if not Needs_BIP_Finalization_Master
(Func_Id
) then
352 Formal
: constant Entity_Id
:=
353 Build_In_Place_Formal
(Func_Id
, BIP_Finalization_Master
);
354 Loc
: constant Source_Ptr
:= Sloc
(Func_Call
);
357 Desig_Typ
: Entity_Id
;
360 -- If there is a finalization master actual, such as the implicit
361 -- finalization master of an enclosing build-in-place function,
362 -- then this must be added as an extra actual of the call.
364 if Present
(Master_Exp
) then
365 Actual
:= Master_Exp
;
367 -- Case where the context does not require an actual master
369 elsif No
(Ptr_Typ
) then
370 Actual
:= Make_Null
(Loc
);
373 Desig_Typ
:= Directly_Designated_Type
(Ptr_Typ
);
375 -- Check for a library-level access type whose designated type has
376 -- supressed finalization. Such an access types lack a master.
377 -- Pass a null actual to the callee in order to signal a missing
380 if Is_Library_Level_Entity
(Ptr_Typ
)
381 and then Finalize_Storage_Only
(Desig_Typ
)
383 Actual
:= Make_Null
(Loc
);
385 -- Types in need of finalization actions
387 elsif Needs_Finalization
(Desig_Typ
) then
389 -- The general mechanism of creating finalization masters for
390 -- anonymous access types is disabled by default, otherwise
391 -- finalization masters will pop all over the place. Such types
392 -- use context-specific masters.
394 if Ekind
(Ptr_Typ
) = E_Anonymous_Access_Type
395 and then No
(Finalization_Master
(Ptr_Typ
))
397 Build_Finalization_Master
399 Ins_Node
=> Associated_Node_For_Itype
(Ptr_Typ
),
400 Encl_Scope
=> Scope
(Ptr_Typ
));
403 -- Access-to-controlled types should always have a master
405 pragma Assert
(Present
(Finalization_Master
(Ptr_Typ
)));
408 Make_Attribute_Reference
(Loc
,
410 New_Reference_To
(Finalization_Master
(Ptr_Typ
), Loc
),
411 Attribute_Name
=> Name_Unrestricted_Access
);
416 Actual
:= Make_Null
(Loc
);
420 Analyze_And_Resolve
(Actual
, Etype
(Formal
));
422 -- Build the parameter association for the new actual and add it to
423 -- the end of the function's actuals.
425 Add_Extra_Actual_To_Call
(Func_Call
, Formal
, Actual
);
427 end Add_Finalization_Master_Actual_To_Build_In_Place_Call
;
429 ------------------------------
430 -- Add_Extra_Actual_To_Call --
431 ------------------------------
433 procedure Add_Extra_Actual_To_Call
434 (Subprogram_Call
: Node_Id
;
435 Extra_Formal
: Entity_Id
;
436 Extra_Actual
: Node_Id
)
438 Loc
: constant Source_Ptr
:= Sloc
(Subprogram_Call
);
439 Param_Assoc
: Node_Id
;
443 Make_Parameter_Association
(Loc
,
444 Selector_Name
=> New_Occurrence_Of
(Extra_Formal
, Loc
),
445 Explicit_Actual_Parameter
=> Extra_Actual
);
447 Set_Parent
(Param_Assoc
, Subprogram_Call
);
448 Set_Parent
(Extra_Actual
, Param_Assoc
);
450 if Present
(Parameter_Associations
(Subprogram_Call
)) then
451 if Nkind
(Last
(Parameter_Associations
(Subprogram_Call
))) =
452 N_Parameter_Association
455 -- Find last named actual, and append
460 L
:= First_Actual
(Subprogram_Call
);
461 while Present
(L
) loop
462 if No
(Next_Actual
(L
)) then
463 Set_Next_Named_Actual
(Parent
(L
), Extra_Actual
);
471 Set_First_Named_Actual
(Subprogram_Call
, Extra_Actual
);
474 Append
(Param_Assoc
, To
=> Parameter_Associations
(Subprogram_Call
));
477 Set_Parameter_Associations
(Subprogram_Call
, New_List
(Param_Assoc
));
478 Set_First_Named_Actual
(Subprogram_Call
, Extra_Actual
);
480 end Add_Extra_Actual_To_Call
;
482 ---------------------------------------------
483 -- Add_Task_Actuals_To_Build_In_Place_Call --
484 ---------------------------------------------
486 procedure Add_Task_Actuals_To_Build_In_Place_Call
487 (Function_Call
: Node_Id
;
488 Function_Id
: Entity_Id
;
489 Master_Actual
: Node_Id
)
491 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
492 Result_Subt
: constant Entity_Id
:=
493 Available_View
(Etype
(Function_Id
));
495 Chain_Actual
: Node_Id
;
496 Chain_Formal
: Node_Id
;
497 Master_Formal
: Node_Id
;
500 -- No such extra parameters are needed if there are no tasks
502 if not Has_Task
(Result_Subt
) then
506 Actual
:= Master_Actual
;
508 -- Use a dummy _master actual in case of No_Task_Hierarchy
510 if Restriction_Active
(No_Task_Hierarchy
) then
511 Actual
:= New_Occurrence_Of
(RTE
(RE_Library_Task_Level
), Loc
);
513 -- In the case where we use the master associated with an access type,
514 -- the actual is an entity and requires an explicit reference.
516 elsif Nkind
(Actual
) = N_Defining_Identifier
then
517 Actual
:= New_Reference_To
(Actual
, Loc
);
520 -- Locate the implicit master parameter in the called function
522 Master_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Task_Master
);
523 Analyze_And_Resolve
(Actual
, Etype
(Master_Formal
));
525 -- Build the parameter association for the new actual and add it to the
526 -- end of the function's actuals.
528 Add_Extra_Actual_To_Call
(Function_Call
, Master_Formal
, Actual
);
530 -- Locate the implicit activation chain parameter in the called function
533 Build_In_Place_Formal
(Function_Id
, BIP_Activation_Chain
);
535 -- Create the actual which is a pointer to the current activation chain
538 Make_Attribute_Reference
(Loc
,
539 Prefix
=> Make_Identifier
(Loc
, Name_uChain
),
540 Attribute_Name
=> Name_Unrestricted_Access
);
542 Analyze_And_Resolve
(Chain_Actual
, Etype
(Chain_Formal
));
544 -- Build the parameter association for the new actual and add it to the
545 -- end of the function's actuals.
547 Add_Extra_Actual_To_Call
(Function_Call
, Chain_Formal
, Chain_Actual
);
548 end Add_Task_Actuals_To_Build_In_Place_Call
;
550 -----------------------
551 -- BIP_Formal_Suffix --
552 -----------------------
554 function BIP_Formal_Suffix
(Kind
: BIP_Formal_Kind
) return String is
557 when BIP_Alloc_Form
=>
559 when BIP_Storage_Pool
=>
560 return "BIPstoragepool";
561 when BIP_Finalization_Master
=>
562 return "BIPfinalizationmaster";
563 when BIP_Task_Master
=>
564 return "BIPtaskmaster";
565 when BIP_Activation_Chain
=>
566 return "BIPactivationchain";
567 when BIP_Object_Access
=>
570 end BIP_Formal_Suffix
;
572 ---------------------------
573 -- Build_In_Place_Formal --
574 ---------------------------
576 function Build_In_Place_Formal
578 Kind
: BIP_Formal_Kind
) return Entity_Id
580 Formal_Name
: constant Name_Id
:=
582 (Chars
(Func
), BIP_Formal_Suffix
(Kind
));
583 Extra_Formal
: Entity_Id
:= Extra_Formals
(Func
);
586 -- Maybe it would be better for each implicit formal of a build-in-place
587 -- function to have a flag or a Uint attribute to identify it. ???
589 -- The return type in the function declaration may have been a limited
590 -- view, and the extra formals for the function were not generated at
591 -- that point. At the point of call the full view must be available and
592 -- the extra formals can be created.
594 if No
(Extra_Formal
) then
595 Create_Extra_Formals
(Func
);
596 Extra_Formal
:= Extra_Formals
(Func
);
600 pragma Assert
(Present
(Extra_Formal
));
601 exit when Chars
(Extra_Formal
) = Formal_Name
;
603 Next_Formal_With_Extras
(Extra_Formal
);
607 end Build_In_Place_Formal
;
609 --------------------------------
610 -- Check_Overriding_Operation --
611 --------------------------------
613 procedure Check_Overriding_Operation
(Subp
: Entity_Id
) is
614 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
615 Op_List
: constant Elist_Id
:= Primitive_Operations
(Typ
);
621 if Is_Derived_Type
(Typ
)
622 and then not Is_Private_Type
(Typ
)
623 and then In_Open_Scopes
(Scope
(Etype
(Typ
)))
624 and then Is_Base_Type
(Typ
)
626 -- Subp overrides an inherited private operation if there is an
627 -- inherited operation with a different name than Subp (see
628 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
629 -- same name as Subp.
631 Op_Elmt
:= First_Elmt
(Op_List
);
632 while Present
(Op_Elmt
) loop
633 Prim_Op
:= Node
(Op_Elmt
);
634 Par_Op
:= Alias
(Prim_Op
);
637 and then not Comes_From_Source
(Prim_Op
)
638 and then Chars
(Prim_Op
) /= Chars
(Par_Op
)
639 and then Chars
(Par_Op
) = Chars
(Subp
)
640 and then Is_Hidden
(Par_Op
)
641 and then Type_Conformant
(Prim_Op
, Subp
)
643 Set_DT_Position
(Subp
, DT_Position
(Prim_Op
));
649 end Check_Overriding_Operation
;
651 -------------------------------
652 -- Detect_Infinite_Recursion --
653 -------------------------------
655 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
) is
656 Loc
: constant Source_Ptr
:= Sloc
(N
);
658 Var_List
: constant Elist_Id
:= New_Elmt_List
;
659 -- List of globals referenced by body of procedure
661 Call_List
: constant Elist_Id
:= New_Elmt_List
;
662 -- List of recursive calls in body of procedure
664 Shad_List
: constant Elist_Id
:= New_Elmt_List
;
665 -- List of entity id's for entities created to capture the value of
666 -- referenced globals on entry to the procedure.
668 Scop
: constant Uint
:= Scope_Depth
(Spec
);
669 -- This is used to record the scope depth of the current procedure, so
670 -- that we can identify global references.
672 Max_Vars
: constant := 4;
673 -- Do not test more than four global variables
675 Count_Vars
: Natural := 0;
676 -- Count variables found so far
688 function Process
(Nod
: Node_Id
) return Traverse_Result
;
689 -- Function to traverse the subprogram body (using Traverse_Func)
695 function Process
(Nod
: Node_Id
) return Traverse_Result
is
699 if Nkind
(Nod
) = N_Procedure_Call_Statement
then
701 -- Case of one of the detected recursive calls
703 if Is_Entity_Name
(Name
(Nod
))
704 and then Has_Recursive_Call
(Entity
(Name
(Nod
)))
705 and then Entity
(Name
(Nod
)) = Spec
707 Append_Elmt
(Nod
, Call_List
);
710 -- Any other procedure call may have side effects
716 -- A call to a pure function can always be ignored
718 elsif Nkind
(Nod
) = N_Function_Call
719 and then Is_Entity_Name
(Name
(Nod
))
720 and then Is_Pure
(Entity
(Name
(Nod
)))
724 -- Case of an identifier reference
726 elsif Nkind
(Nod
) = N_Identifier
then
729 -- If no entity, then ignore the reference
731 -- Not clear why this can happen. To investigate, remove this
732 -- test and look at the crash that occurs here in 3401-004 ???
737 -- Ignore entities with no Scope, again not clear how this
738 -- can happen, to investigate, look at 4108-008 ???
740 elsif No
(Scope
(Ent
)) then
743 -- Ignore the reference if not to a more global object
745 elsif Scope_Depth
(Scope
(Ent
)) >= Scop
then
748 -- References to types, exceptions and constants are always OK
751 or else Ekind
(Ent
) = E_Exception
752 or else Ekind
(Ent
) = E_Constant
756 -- If other than a non-volatile scalar variable, we have some
757 -- kind of global reference (e.g. to a function) that we cannot
758 -- deal with so we forget the attempt.
760 elsif Ekind
(Ent
) /= E_Variable
761 or else not Is_Scalar_Type
(Etype
(Ent
))
762 or else Treat_As_Volatile
(Ent
)
766 -- Otherwise we have a reference to a global scalar
769 -- Loop through global entities already detected
771 Elm
:= First_Elmt
(Var_List
);
773 -- If not detected before, record this new global reference
776 Count_Vars
:= Count_Vars
+ 1;
778 if Count_Vars
<= Max_Vars
then
779 Append_Elmt
(Entity
(Nod
), Var_List
);
786 -- If recorded before, ignore
788 elsif Node
(Elm
) = Entity
(Nod
) then
791 -- Otherwise keep looking
801 -- For all other node kinds, recursively visit syntactic children
808 function Traverse_Body
is new Traverse_Func
(Process
);
810 -- Start of processing for Detect_Infinite_Recursion
813 -- Do not attempt detection in No_Implicit_Conditional mode, since we
814 -- won't be able to generate the code to handle the recursion in any
817 if Restriction_Active
(No_Implicit_Conditionals
) then
821 -- Otherwise do traversal and quit if we get abandon signal
823 if Traverse_Body
(N
) = Abandon
then
826 -- We must have a call, since Has_Recursive_Call was set. If not just
827 -- ignore (this is only an error check, so if we have a funny situation,
828 -- due to bugs or errors, we do not want to bomb!)
830 elsif Is_Empty_Elmt_List
(Call_List
) then
834 -- Here is the case where we detect recursion at compile time
836 -- Push our current scope for analyzing the declarations and code that
837 -- we will insert for the checking.
841 -- This loop builds temporary variables for each of the referenced
842 -- globals, so that at the end of the loop the list Shad_List contains
843 -- these temporaries in one-to-one correspondence with the elements in
847 Elm
:= First_Elmt
(Var_List
);
848 while Present
(Elm
) loop
850 Ent
:= Make_Temporary
(Loc
, 'S');
851 Append_Elmt
(Ent
, Shad_List
);
853 -- Insert a declaration for this temporary at the start of the
854 -- declarations for the procedure. The temporaries are declared as
855 -- constant objects initialized to the current values of the
856 -- corresponding temporaries.
859 Make_Object_Declaration
(Loc
,
860 Defining_Identifier
=> Ent
,
861 Object_Definition
=> New_Occurrence_Of
(Etype
(Var
), Loc
),
862 Constant_Present
=> True,
863 Expression
=> New_Occurrence_Of
(Var
, Loc
));
866 Prepend
(Decl
, Declarations
(N
));
868 Insert_After
(Last
, Decl
);
876 -- Loop through calls
878 Call
:= First_Elmt
(Call_List
);
879 while Present
(Call
) loop
881 -- Build a predicate expression of the form
884 -- and then global1 = temp1
885 -- and then global2 = temp2
888 -- This predicate determines if any of the global values
889 -- referenced by the procedure have changed since the
890 -- current call, if not an infinite recursion is assured.
892 Test
:= New_Occurrence_Of
(Standard_True
, Loc
);
894 Elm1
:= First_Elmt
(Var_List
);
895 Elm2
:= First_Elmt
(Shad_List
);
896 while Present
(Elm1
) loop
902 Left_Opnd
=> New_Occurrence_Of
(Node
(Elm1
), Loc
),
903 Right_Opnd
=> New_Occurrence_Of
(Node
(Elm2
), Loc
)));
909 -- Now we replace the call with the sequence
911 -- if no-changes (see above) then
912 -- raise Storage_Error;
917 Rewrite
(Node
(Call
),
918 Make_If_Statement
(Loc
,
920 Then_Statements
=> New_List
(
921 Make_Raise_Storage_Error
(Loc
,
922 Reason
=> SE_Infinite_Recursion
)),
924 Else_Statements
=> New_List
(
925 Relocate_Node
(Node
(Call
)))));
927 Analyze
(Node
(Call
));
932 -- Remove temporary scope stack entry used for analysis
935 end Detect_Infinite_Recursion
;
941 procedure Expand_Actuals
(N
: Node_Id
; Subp
: Entity_Id
) is
942 Loc
: constant Source_Ptr
:= Sloc
(N
);
947 E_Actual
: Entity_Id
;
948 E_Formal
: Entity_Id
;
950 procedure Add_Call_By_Copy_Code
;
951 -- For cases where the parameter must be passed by copy, this routine
952 -- generates a temporary variable into which the actual is copied and
953 -- then passes this as the parameter. For an OUT or IN OUT parameter,
954 -- an assignment is also generated to copy the result back. The call
955 -- also takes care of any constraint checks required for the type
956 -- conversion case (on both the way in and the way out).
958 procedure Add_Simple_Call_By_Copy_Code
;
959 -- This is similar to the above, but is used in cases where we know
960 -- that all that is needed is to simply create a temporary and copy
961 -- the value in and out of the temporary.
963 procedure Check_Fortran_Logical
;
964 -- A value of type Logical that is passed through a formal parameter
965 -- must be normalized because .TRUE. usually does not have the same
966 -- representation as True. We assume that .FALSE. = False = 0.
967 -- What about functions that return a logical type ???
969 function Is_Legal_Copy
return Boolean;
970 -- Check that an actual can be copied before generating the temporary
971 -- to be used in the call. If the actual is of a by_reference type then
972 -- the program is illegal (this can only happen in the presence of
973 -- rep. clauses that force an incorrect alignment). If the formal is
974 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
975 -- the effect that this might lead to unaligned arguments.
977 function Make_Var
(Actual
: Node_Id
) return Entity_Id
;
978 -- Returns an entity that refers to the given actual parameter,
979 -- Actual (not including any type conversion). If Actual is an
980 -- entity name, then this entity is returned unchanged, otherwise
981 -- a renaming is created to provide an entity for the actual.
983 procedure Reset_Packed_Prefix
;
984 -- The expansion of a packed array component reference is delayed in
985 -- the context of a call. Now we need to complete the expansion, so we
986 -- unmark the analyzed bits in all prefixes.
988 ---------------------------
989 -- Add_Call_By_Copy_Code --
990 ---------------------------
992 procedure Add_Call_By_Copy_Code
is
998 F_Typ
: constant Entity_Id
:= Etype
(Formal
);
1003 if not Is_Legal_Copy
then
1007 Temp
:= Make_Temporary
(Loc
, 'T', Actual
);
1009 -- Use formal type for temp, unless formal type is an unconstrained
1010 -- array, in which case we don't have to worry about bounds checks,
1011 -- and we use the actual type, since that has appropriate bounds.
1013 if Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
1014 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1016 Indic
:= New_Occurrence_Of
(Etype
(Formal
), Loc
);
1019 if Nkind
(Actual
) = N_Type_Conversion
then
1020 V_Typ
:= Etype
(Expression
(Actual
));
1022 -- If the formal is an (in-)out parameter, capture the name
1023 -- of the variable in order to build the post-call assignment.
1025 Var
:= Make_Var
(Expression
(Actual
));
1027 Crep
:= not Same_Representation
1028 (F_Typ
, Etype
(Expression
(Actual
)));
1031 V_Typ
:= Etype
(Actual
);
1032 Var
:= Make_Var
(Actual
);
1036 -- Setup initialization for case of in out parameter, or an out
1037 -- parameter where the formal is an unconstrained array (in the
1038 -- latter case, we have to pass in an object with bounds).
1040 -- If this is an out parameter, the initial copy is wasteful, so as
1041 -- an optimization for the one-dimensional case we extract the
1042 -- bounds of the actual and build an uninitialized temporary of the
1045 if Ekind
(Formal
) = E_In_Out_Parameter
1046 or else (Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
))
1048 if Nkind
(Actual
) = N_Type_Conversion
then
1049 if Conversion_OK
(Actual
) then
1050 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1052 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1055 elsif Ekind
(Formal
) = E_Out_Parameter
1056 and then Is_Array_Type
(F_Typ
)
1057 and then Number_Dimensions
(F_Typ
) = 1
1058 and then not Has_Non_Null_Base_Init_Proc
(F_Typ
)
1060 -- Actual is a one-dimensional array or slice, and the type
1061 -- requires no initialization. Create a temporary of the
1062 -- right size, but do not copy actual into it (optimization).
1066 Make_Subtype_Indication
(Loc
,
1068 New_Occurrence_Of
(F_Typ
, Loc
),
1070 Make_Index_Or_Discriminant_Constraint
(Loc
,
1071 Constraints
=> New_List
(
1074 Make_Attribute_Reference
(Loc
,
1075 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
1076 Attribute_Name
=> Name_First
),
1078 Make_Attribute_Reference
(Loc
,
1079 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
1080 Attribute_Name
=> Name_Last
)))));
1083 Init
:= New_Occurrence_Of
(Var
, Loc
);
1086 -- An initialization is created for packed conversions as
1087 -- actuals for out parameters to enable Make_Object_Declaration
1088 -- to determine the proper subtype for N_Node. Note that this
1089 -- is wasteful because the extra copying on the call side is
1090 -- not required for such out parameters. ???
1092 elsif Ekind
(Formal
) = E_Out_Parameter
1093 and then Nkind
(Actual
) = N_Type_Conversion
1094 and then (Is_Bit_Packed_Array
(F_Typ
)
1096 Is_Bit_Packed_Array
(Etype
(Expression
(Actual
))))
1098 if Conversion_OK
(Actual
) then
1099 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1101 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1104 elsif Ekind
(Formal
) = E_In_Parameter
then
1106 -- Handle the case in which the actual is a type conversion
1108 if Nkind
(Actual
) = N_Type_Conversion
then
1109 if Conversion_OK
(Actual
) then
1110 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1112 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1115 Init
:= New_Occurrence_Of
(Var
, Loc
);
1123 Make_Object_Declaration
(Loc
,
1124 Defining_Identifier
=> Temp
,
1125 Object_Definition
=> Indic
,
1126 Expression
=> Init
);
1127 Set_Assignment_OK
(N_Node
);
1128 Insert_Action
(N
, N_Node
);
1130 -- Now, normally the deal here is that we use the defining
1131 -- identifier created by that object declaration. There is
1132 -- one exception to this. In the change of representation case
1133 -- the above declaration will end up looking like:
1135 -- temp : type := identifier;
1137 -- And in this case we might as well use the identifier directly
1138 -- and eliminate the temporary. Note that the analysis of the
1139 -- declaration was not a waste of time in that case, since it is
1140 -- what generated the necessary change of representation code. If
1141 -- the change of representation introduced additional code, as in
1142 -- a fixed-integer conversion, the expression is not an identifier
1143 -- and must be kept.
1146 and then Present
(Expression
(N_Node
))
1147 and then Is_Entity_Name
(Expression
(N_Node
))
1149 Temp
:= Entity
(Expression
(N_Node
));
1150 Rewrite
(N_Node
, Make_Null_Statement
(Loc
));
1153 -- For IN parameter, all we do is to replace the actual
1155 if Ekind
(Formal
) = E_In_Parameter
then
1156 Rewrite
(Actual
, New_Reference_To
(Temp
, Loc
));
1159 -- Processing for OUT or IN OUT parameter
1162 -- Kill current value indications for the temporary variable we
1163 -- created, since we just passed it as an OUT parameter.
1165 Kill_Current_Values
(Temp
);
1166 Set_Is_Known_Valid
(Temp
, False);
1168 -- If type conversion, use reverse conversion on exit
1170 if Nkind
(Actual
) = N_Type_Conversion
then
1171 if Conversion_OK
(Actual
) then
1172 Expr
:= OK_Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
1174 Expr
:= Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
1177 Expr
:= New_Occurrence_Of
(Temp
, Loc
);
1180 Rewrite
(Actual
, New_Reference_To
(Temp
, Loc
));
1183 -- If the actual is a conversion of a packed reference, it may
1184 -- already have been expanded by Remove_Side_Effects, and the
1185 -- resulting variable is a temporary which does not designate
1186 -- the proper out-parameter, which may not be addressable. In
1187 -- that case, generate an assignment to the original expression
1188 -- (before expansion of the packed reference) so that the proper
1189 -- expansion of assignment to a packed component can take place.
1196 if Is_Renaming_Of_Object
(Var
)
1197 and then Nkind
(Renamed_Object
(Var
)) = N_Selected_Component
1198 and then Is_Entity_Name
(Prefix
(Renamed_Object
(Var
)))
1199 and then Nkind
(Original_Node
(Prefix
(Renamed_Object
(Var
))))
1200 = N_Indexed_Component
1202 Has_Non_Standard_Rep
(Etype
(Prefix
(Renamed_Object
(Var
))))
1204 Obj
:= Renamed_Object
(Var
);
1206 Make_Selected_Component
(Loc
,
1208 New_Copy_Tree
(Original_Node
(Prefix
(Obj
))),
1209 Selector_Name
=> New_Copy
(Selector_Name
(Obj
)));
1210 Reset_Analyzed_Flags
(Lhs
);
1213 Lhs
:= New_Occurrence_Of
(Var
, Loc
);
1216 Set_Assignment_OK
(Lhs
);
1218 if Is_Access_Type
(E_Formal
)
1219 and then Is_Entity_Name
(Lhs
)
1221 Present
(Effective_Extra_Accessibility
(Entity
(Lhs
)))
1223 -- Copyback target is an Ada 2012 stand-alone object of an
1224 -- anonymous access type.
1226 pragma Assert
(Ada_Version
>= Ada_2012
);
1228 if Type_Access_Level
(E_Formal
) >
1229 Object_Access_Level
(Lhs
)
1231 Append_To
(Post_Call
,
1232 Make_Raise_Program_Error
(Loc
,
1233 Reason
=> PE_Accessibility_Check_Failed
));
1236 Append_To
(Post_Call
,
1237 Make_Assignment_Statement
(Loc
,
1239 Expression
=> Expr
));
1241 -- We would like to somehow suppress generation of the
1242 -- extra_accessibility assignment generated by the expansion
1243 -- of the above assignment statement. It's not a correctness
1244 -- issue because the following assignment renders it dead,
1245 -- but generating back-to-back assignments to the same
1246 -- target is undesirable. ???
1248 Append_To
(Post_Call
,
1249 Make_Assignment_Statement
(Loc
,
1250 Name
=> New_Occurrence_Of
(
1251 Effective_Extra_Accessibility
(Entity
(Lhs
)), Loc
),
1252 Expression
=> Make_Integer_Literal
(Loc
,
1253 Type_Access_Level
(E_Formal
))));
1256 Append_To
(Post_Call
,
1257 Make_Assignment_Statement
(Loc
,
1259 Expression
=> Expr
));
1263 end Add_Call_By_Copy_Code
;
1265 ----------------------------------
1266 -- Add_Simple_Call_By_Copy_Code --
1267 ----------------------------------
1269 procedure Add_Simple_Call_By_Copy_Code
is
1277 F_Typ
: constant Entity_Id
:= Etype
(Formal
);
1280 if not Is_Legal_Copy
then
1284 -- Use formal type for temp, unless formal type is an unconstrained
1285 -- array, in which case we don't have to worry about bounds checks,
1286 -- and we use the actual type, since that has appropriate bounds.
1288 if Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
1289 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1291 Indic
:= New_Occurrence_Of
(Etype
(Formal
), Loc
);
1294 -- Prepare to generate code
1296 Reset_Packed_Prefix
;
1298 Temp
:= Make_Temporary
(Loc
, 'T', Actual
);
1299 Incod
:= Relocate_Node
(Actual
);
1300 Outcod
:= New_Copy_Tree
(Incod
);
1302 -- Generate declaration of temporary variable, initializing it
1303 -- with the input parameter unless we have an OUT formal or
1304 -- this is an initialization call.
1306 -- If the formal is an out parameter with discriminants, the
1307 -- discriminants must be captured even if the rest of the object
1308 -- is in principle uninitialized, because the discriminants may
1309 -- be read by the called subprogram.
1311 if Ekind
(Formal
) = E_Out_Parameter
then
1314 if Has_Discriminants
(Etype
(Formal
)) then
1315 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1318 elsif Inside_Init_Proc
then
1320 -- Could use a comment here to match comment below ???
1322 if Nkind
(Actual
) /= N_Selected_Component
1324 not Has_Discriminant_Dependent_Constraint
1325 (Entity
(Selector_Name
(Actual
)))
1329 -- Otherwise, keep the component in order to generate the proper
1330 -- actual subtype, that depends on enclosing discriminants.
1338 Make_Object_Declaration
(Loc
,
1339 Defining_Identifier
=> Temp
,
1340 Object_Definition
=> Indic
,
1341 Expression
=> Incod
);
1346 -- If the call is to initialize a component of a composite type,
1347 -- and the component does not depend on discriminants, use the
1348 -- actual type of the component. This is required in case the
1349 -- component is constrained, because in general the formal of the
1350 -- initialization procedure will be unconstrained. Note that if
1351 -- the component being initialized is constrained by an enclosing
1352 -- discriminant, the presence of the initialization in the
1353 -- declaration will generate an expression for the actual subtype.
1355 Set_No_Initialization
(Decl
);
1356 Set_Object_Definition
(Decl
,
1357 New_Occurrence_Of
(Etype
(Actual
), Loc
));
1360 Insert_Action
(N
, Decl
);
1362 -- The actual is simply a reference to the temporary
1364 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
1366 -- Generate copy out if OUT or IN OUT parameter
1368 if Ekind
(Formal
) /= E_In_Parameter
then
1370 Rhs
:= New_Occurrence_Of
(Temp
, Loc
);
1372 -- Deal with conversion
1374 if Nkind
(Lhs
) = N_Type_Conversion
then
1375 Lhs
:= Expression
(Lhs
);
1376 Rhs
:= Convert_To
(Etype
(Actual
), Rhs
);
1379 Append_To
(Post_Call
,
1380 Make_Assignment_Statement
(Loc
,
1382 Expression
=> Rhs
));
1383 Set_Assignment_OK
(Name
(Last
(Post_Call
)));
1385 end Add_Simple_Call_By_Copy_Code
;
1387 ---------------------------
1388 -- Check_Fortran_Logical --
1389 ---------------------------
1391 procedure Check_Fortran_Logical
is
1392 Logical
: constant Entity_Id
:= Etype
(Formal
);
1395 -- Note: this is very incomplete, e.g. it does not handle arrays
1396 -- of logical values. This is really not the right approach at all???)
1399 if Convention
(Subp
) = Convention_Fortran
1400 and then Root_Type
(Etype
(Formal
)) = Standard_Boolean
1401 and then Ekind
(Formal
) /= E_In_Parameter
1403 Var
:= Make_Var
(Actual
);
1404 Append_To
(Post_Call
,
1405 Make_Assignment_Statement
(Loc
,
1406 Name
=> New_Occurrence_Of
(Var
, Loc
),
1408 Unchecked_Convert_To
(
1411 Left_Opnd
=> New_Occurrence_Of
(Var
, Loc
),
1413 Unchecked_Convert_To
(
1415 New_Occurrence_Of
(Standard_False
, Loc
))))));
1417 end Check_Fortran_Logical
;
1423 function Is_Legal_Copy
return Boolean is
1425 -- An attempt to copy a value of such a type can only occur if
1426 -- representation clauses give the actual a misaligned address.
1428 if Is_By_Reference_Type
(Etype
(Formal
)) then
1430 -- If the front-end does not perform full type layout, the actual
1431 -- may in fact be properly aligned but there is not enough front-
1432 -- end information to determine this. In that case gigi will emit
1433 -- an error if a copy is not legal, or generate the proper code.
1434 -- For other backends we report the error now.
1436 -- Seems wrong to be issuing an error in the expander, since it
1437 -- will be missed in -gnatc mode ???
1439 if Frontend_Layout_On_Target
then
1441 ("misaligned actual cannot be passed by reference", Actual
);
1446 -- For users of Starlet, we assume that the specification of by-
1447 -- reference mechanism is mandatory. This may lead to unaligned
1448 -- objects but at least for DEC legacy code it is known to work.
1449 -- The warning will alert users of this code that a problem may
1452 elsif Mechanism
(Formal
) = By_Reference
1453 and then Is_Valued_Procedure
(Scope
(Formal
))
1456 ("by_reference actual may be misaligned??", Actual
);
1468 function Make_Var
(Actual
: Node_Id
) return Entity_Id
is
1472 if Is_Entity_Name
(Actual
) then
1473 return Entity
(Actual
);
1476 Var
:= Make_Temporary
(Loc
, 'T', Actual
);
1479 Make_Object_Renaming_Declaration
(Loc
,
1480 Defining_Identifier
=> Var
,
1482 New_Occurrence_Of
(Etype
(Actual
), Loc
),
1483 Name
=> Relocate_Node
(Actual
));
1485 Insert_Action
(N
, N_Node
);
1490 -------------------------
1491 -- Reset_Packed_Prefix --
1492 -------------------------
1494 procedure Reset_Packed_Prefix
is
1495 Pfx
: Node_Id
:= Actual
;
1498 Set_Analyzed
(Pfx
, False);
1500 not Nkind_In
(Pfx
, N_Selected_Component
, N_Indexed_Component
);
1501 Pfx
:= Prefix
(Pfx
);
1503 end Reset_Packed_Prefix
;
1505 -- Start of processing for Expand_Actuals
1508 Post_Call
:= New_List
;
1510 Formal
:= First_Formal
(Subp
);
1511 Actual
:= First_Actual
(N
);
1512 while Present
(Formal
) loop
1513 E_Formal
:= Etype
(Formal
);
1514 E_Actual
:= Etype
(Actual
);
1516 if Is_Scalar_Type
(E_Formal
)
1517 or else Nkind
(Actual
) = N_Slice
1519 Check_Fortran_Logical
;
1523 elsif Ekind
(Formal
) /= E_Out_Parameter
then
1525 -- The unusual case of the current instance of a protected type
1526 -- requires special handling. This can only occur in the context
1527 -- of a call within the body of a protected operation.
1529 if Is_Entity_Name
(Actual
)
1530 and then Ekind
(Entity
(Actual
)) = E_Protected_Type
1531 and then In_Open_Scopes
(Entity
(Actual
))
1533 if Scope
(Subp
) /= Entity
(Actual
) then
1535 ("operation outside protected type may not "
1536 & "call back its protected operations??", Actual
);
1540 Expand_Protected_Object_Reference
(N
, Entity
(Actual
)));
1543 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1544 -- build-in-place function, then a temporary return object needs
1545 -- to be created and access to it must be passed to the function.
1546 -- Currently we limit such functions to those with inherently
1547 -- limited result subtypes, but eventually we plan to expand the
1548 -- functions that are treated as build-in-place to include other
1549 -- composite result types.
1551 if Is_Build_In_Place_Function_Call
(Actual
) then
1552 Make_Build_In_Place_Call_In_Anonymous_Context
(Actual
);
1555 Apply_Constraint_Check
(Actual
, E_Formal
);
1557 -- Out parameter case. No constraint checks on access type
1560 elsif Is_Access_Type
(E_Formal
) then
1565 elsif Has_Discriminants
(Base_Type
(E_Formal
))
1566 or else Has_Non_Null_Base_Init_Proc
(E_Formal
)
1568 Apply_Constraint_Check
(Actual
, E_Formal
);
1573 Apply_Constraint_Check
(Actual
, Base_Type
(E_Formal
));
1576 -- Processing for IN-OUT and OUT parameters
1578 if Ekind
(Formal
) /= E_In_Parameter
then
1580 -- For type conversions of arrays, apply length/range checks
1582 if Is_Array_Type
(E_Formal
)
1583 and then Nkind
(Actual
) = N_Type_Conversion
1585 if Is_Constrained
(E_Formal
) then
1586 Apply_Length_Check
(Expression
(Actual
), E_Formal
);
1588 Apply_Range_Check
(Expression
(Actual
), E_Formal
);
1592 -- If argument is a type conversion for a type that is passed
1593 -- by copy, then we must pass the parameter by copy.
1595 if Nkind
(Actual
) = N_Type_Conversion
1597 (Is_Numeric_Type
(E_Formal
)
1598 or else Is_Access_Type
(E_Formal
)
1599 or else Is_Enumeration_Type
(E_Formal
)
1600 or else Is_Bit_Packed_Array
(Etype
(Formal
))
1601 or else Is_Bit_Packed_Array
(Etype
(Expression
(Actual
)))
1603 -- Also pass by copy if change of representation
1605 or else not Same_Representation
1607 Etype
(Expression
(Actual
))))
1609 Add_Call_By_Copy_Code
;
1611 -- References to components of bit packed arrays are expanded
1612 -- at this point, rather than at the point of analysis of the
1613 -- actuals, to handle the expansion of the assignment to
1614 -- [in] out parameters.
1616 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
1617 Add_Simple_Call_By_Copy_Code
;
1619 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1620 -- because the back-end cannot cope with such objects. In other
1621 -- cases where alignment forces a copy, the back-end generates
1622 -- it properly. It should not be generated unconditionally in the
1623 -- front-end because it does not know precisely the alignment
1624 -- requirements of the target, and makes too conservative an
1625 -- estimate, leading to superfluous copies or spurious errors
1626 -- on by-reference parameters.
1628 elsif Nkind
(Actual
) = N_Selected_Component
1630 Component_May_Be_Bit_Aligned
(Entity
(Selector_Name
(Actual
)))
1631 and then not Represented_As_Scalar
(Etype
(Formal
))
1633 Add_Simple_Call_By_Copy_Code
;
1635 -- References to slices of bit packed arrays are expanded
1637 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
1638 Add_Call_By_Copy_Code
;
1640 -- References to possibly unaligned slices of arrays are expanded
1642 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
1643 Add_Call_By_Copy_Code
;
1645 -- Deal with access types where the actual subtype and the
1646 -- formal subtype are not the same, requiring a check.
1648 -- It is necessary to exclude tagged types because of "downward
1649 -- conversion" errors.
1651 elsif Is_Access_Type
(E_Formal
)
1652 and then not Same_Type
(E_Formal
, E_Actual
)
1653 and then not Is_Tagged_Type
(Designated_Type
(E_Formal
))
1655 Add_Call_By_Copy_Code
;
1657 -- If the actual is not a scalar and is marked for volatile
1658 -- treatment, whereas the formal is not volatile, then pass
1659 -- by copy unless it is a by-reference type.
1661 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1662 -- because this is the enforcement of a language rule that applies
1663 -- only to "real" volatile variables, not e.g. to the address
1664 -- clause overlay case.
1666 elsif Is_Entity_Name
(Actual
)
1667 and then Is_Volatile
(Entity
(Actual
))
1668 and then not Is_By_Reference_Type
(E_Actual
)
1669 and then not Is_Scalar_Type
(Etype
(Entity
(Actual
)))
1670 and then not Is_Volatile
(E_Formal
)
1672 Add_Call_By_Copy_Code
;
1674 elsif Nkind
(Actual
) = N_Indexed_Component
1675 and then Is_Entity_Name
(Prefix
(Actual
))
1676 and then Has_Volatile_Components
(Entity
(Prefix
(Actual
)))
1678 Add_Call_By_Copy_Code
;
1680 -- Add call-by-copy code for the case of scalar out parameters
1681 -- when it is not known at compile time that the subtype of the
1682 -- formal is a subrange of the subtype of the actual (or vice
1683 -- versa for in out parameters), in order to get range checks
1684 -- on such actuals. (Maybe this case should be handled earlier
1685 -- in the if statement???)
1687 elsif Is_Scalar_Type
(E_Formal
)
1689 (not In_Subrange_Of
(E_Formal
, E_Actual
)
1691 (Ekind
(Formal
) = E_In_Out_Parameter
1692 and then not In_Subrange_Of
(E_Actual
, E_Formal
)))
1694 -- Perhaps the setting back to False should be done within
1695 -- Add_Call_By_Copy_Code, since it could get set on other
1696 -- cases occurring above???
1698 if Do_Range_Check
(Actual
) then
1699 Set_Do_Range_Check
(Actual
, False);
1702 Add_Call_By_Copy_Code
;
1705 -- RM 3.2.4 (23/3) : A predicate is checked on in-out and out
1706 -- by-reference parameters on exit from the call. If the actual
1707 -- is a derived type and the operation is inherited, the body
1708 -- of the operation will not contain a call to the predicate
1709 -- function, so it must be done explicitly after the call. Ditto
1710 -- if the actual is an entity of a predicated subtype.
1712 -- The rule refers to by-reference types, but a check is needed
1713 -- for by-copy types as well. That check is subsumed by the rule
1714 -- for subtype conversion on assignment, but we can generate the
1715 -- required check now.
1717 -- Note that this is needed only if the subtype of the actual has
1718 -- an explicit predicate aspect, not if it inherits them from a
1719 -- base type or ancestor. The check is also superfluous if the
1720 -- subtype is elaborated before the body of the subprogram, but
1721 -- this is harder to verify, and there may be a redundant check.
1723 -- Note also that Subp may be either a subprogram entity for
1724 -- direct calls, or a type entity for indirect calls, which must
1725 -- be handled separately because the name does not denote an
1726 -- overloadable entity.
1728 -- If the formal is class-wide the corresponding postcondition
1729 -- procedure does not include a predicate call, so it has to be
1730 -- generated explicitly.
1732 if not Is_Init_Proc
(Subp
)
1733 and then (Has_Aspect
(E_Actual
, Aspect_Predicate
)
1735 Has_Aspect
(E_Actual
, Aspect_Dynamic_Predicate
)
1737 Has_Aspect
(E_Actual
, Aspect_Static_Predicate
))
1738 and then Present
(Predicate_Function
(E_Actual
))
1740 if Is_Entity_Name
(Actual
)
1742 (Is_Derived_Type
(E_Actual
)
1743 and then Is_Overloadable
(Subp
)
1744 and then Is_Inherited_Operation_For_Type
(Subp
, E_Actual
))
1746 Append_To
(Post_Call
,
1747 Make_Predicate_Check
(E_Actual
, Actual
));
1749 elsif Is_Class_Wide_Type
(E_Formal
)
1750 and then not Is_Class_Wide_Type
(E_Actual
)
1752 Append_To
(Post_Call
,
1753 Make_Predicate_Check
(E_Actual
, Actual
));
1757 -- Processing for IN parameters
1760 -- For IN parameters is in the packed array case, we expand an
1761 -- indexed component (the circuit in Exp_Ch4 deliberately left
1762 -- indexed components appearing as actuals untouched, so that
1763 -- the special processing above for the OUT and IN OUT cases
1764 -- could be performed. We could make the test in Exp_Ch4 more
1765 -- complex and have it detect the parameter mode, but it is
1766 -- easier simply to handle all cases here.)
1768 if Nkind
(Actual
) = N_Indexed_Component
1769 and then Is_Packed
(Etype
(Prefix
(Actual
)))
1771 Reset_Packed_Prefix
;
1772 Expand_Packed_Element_Reference
(Actual
);
1774 -- If we have a reference to a bit packed array, we copy it, since
1775 -- the actual must be byte aligned.
1777 -- Is this really necessary in all cases???
1779 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
1780 Add_Simple_Call_By_Copy_Code
;
1782 -- If a non-scalar actual is possibly unaligned, we need a copy
1784 elsif Is_Possibly_Unaligned_Object
(Actual
)
1785 and then not Represented_As_Scalar
(Etype
(Formal
))
1787 Add_Simple_Call_By_Copy_Code
;
1789 -- Similarly, we have to expand slices of packed arrays here
1790 -- because the result must be byte aligned.
1792 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
1793 Add_Call_By_Copy_Code
;
1795 -- Only processing remaining is to pass by copy if this is a
1796 -- reference to a possibly unaligned slice, since the caller
1797 -- expects an appropriately aligned argument.
1799 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
1800 Add_Call_By_Copy_Code
;
1802 -- An unusual case: a current instance of an enclosing task can be
1803 -- an actual, and must be replaced by a reference to self.
1805 elsif Is_Entity_Name
(Actual
)
1806 and then Is_Task_Type
(Entity
(Actual
))
1808 if In_Open_Scopes
(Entity
(Actual
)) then
1810 (Make_Function_Call
(Loc
,
1811 Name
=> New_Reference_To
(RTE
(RE_Self
), Loc
))));
1814 -- A task type cannot otherwise appear as an actual
1817 raise Program_Error
;
1822 Next_Formal
(Formal
);
1823 Next_Actual
(Actual
);
1826 -- Find right place to put post call stuff if it is present
1828 if not Is_Empty_List
(Post_Call
) then
1830 -- Cases where the call is not a member of a statement list
1832 if not Is_List_Member
(N
) then
1834 P
: Node_Id
:= Parent
(N
);
1837 -- In Ada 2012 the call may be a function call in an expression
1838 -- (since OUT and IN OUT parameters are now allowed for such
1839 -- calls. The write-back of (in)-out parameters is handled
1840 -- by the back-end, but the constraint checks generated when
1841 -- subtypes of formal and actual don't match must be inserted
1842 -- in the form of assignments, at the nearest point after the
1843 -- declaration or statement that contains the call.
1845 if Ada_Version
>= Ada_2012
1846 and then Nkind
(N
) = N_Function_Call
1848 while Nkind
(P
) not in N_Declaration
1850 Nkind
(P
) not in N_Statement_Other_Than_Procedure_Call
1855 Insert_Actions_After
(P
, Post_Call
);
1857 -- If not the special Ada 2012 case of a function call, then
1858 -- we must have the triggering statement of a triggering
1859 -- alternative or an entry call alternative, and we can add
1860 -- the post call stuff to the corresponding statement list.
1863 pragma Assert
(Nkind_In
(P
, N_Triggering_Alternative
,
1864 N_Entry_Call_Alternative
));
1866 if Is_Non_Empty_List
(Statements
(P
)) then
1867 Insert_List_Before_And_Analyze
1868 (First
(Statements
(P
)), Post_Call
);
1870 Set_Statements
(P
, Post_Call
);
1876 -- Otherwise, normal case where N is in a statement sequence,
1877 -- just put the post-call stuff after the call statement.
1880 Insert_Actions_After
(N
, Post_Call
);
1884 -- The call node itself is re-analyzed in Expand_Call
1892 -- This procedure handles expansion of function calls and procedure call
1893 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1894 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
1896 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
1897 -- Provide values of actuals for all formals in Extra_Formals list
1898 -- Replace "call" to enumeration literal function by literal itself
1899 -- Rewrite call to predefined operator as operator
1900 -- Replace actuals to in-out parameters that are numeric conversions,
1901 -- with explicit assignment to temporaries before and after the call.
1902 -- Remove optional actuals if First_Optional_Parameter specified.
1904 -- Note that the list of actuals has been filled with default expressions
1905 -- during semantic analysis of the call. Only the extra actuals required
1906 -- for the 'Constrained attribute and for accessibility checks are added
1909 procedure Expand_Call
(N
: Node_Id
) is
1910 Loc
: constant Source_Ptr
:= Sloc
(N
);
1911 Call_Node
: Node_Id
:= N
;
1912 Extra_Actuals
: List_Id
:= No_List
;
1913 Prev
: Node_Id
:= Empty
;
1915 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
);
1916 -- Adds one entry to the end of the actual parameter list. Used for
1917 -- default parameters and for extra actuals (for Extra_Formals). The
1918 -- argument is an N_Parameter_Association node.
1920 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
);
1921 -- Adds an extra actual to the list of extra actuals. Expr is the
1922 -- expression for the value of the actual, EF is the entity for the
1925 procedure Do_Inline
(Subp
: Entity_Id
; Orig_Subp
: Entity_Id
);
1926 -- Check and inline the body of Subp. Invoked when compiling with
1927 -- optimizations enabled and Subp has pragma inline or inline always.
1928 -- If the subprogram is a renaming, or if it is inherited, then Subp
1929 -- references the renamed entity and Orig_Subp is the entity of the
1932 procedure Do_Inline_Always
(Subp
: Entity_Id
; Orig_Subp
: Entity_Id
);
1933 -- Check and inline the body of Subp. Invoked when compiling without
1934 -- optimizations and Subp has pragma inline always. If the subprogram is
1935 -- a renaming, or if it is inherited, then Subp references the renamed
1936 -- entity and Orig_Subp is the entity of the call node N.
1938 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
;
1939 -- Within an instance, a type derived from a non-tagged formal derived
1940 -- type inherits from the original parent, not from the actual. The
1941 -- current derivation mechanism has the derived type inherit from the
1942 -- actual, which is only correct outside of the instance. If the
1943 -- subprogram is inherited, we test for this particular case through a
1944 -- convoluted tree traversal before setting the proper subprogram to be
1947 function In_Unfrozen_Instance
(E
: Entity_Id
) return Boolean;
1948 -- Return true if E comes from an instance that is not yet frozen
1950 function Is_Direct_Deep_Call
(Subp
: Entity_Id
) return Boolean;
1951 -- Determine if Subp denotes a non-dispatching call to a Deep routine
1953 function New_Value
(From
: Node_Id
) return Node_Id
;
1954 -- From is the original Expression. New_Value is equivalent to a call
1955 -- to Duplicate_Subexpr with an explicit dereference when From is an
1956 -- access parameter.
1958 --------------------------
1959 -- Add_Actual_Parameter --
1960 --------------------------
1962 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
) is
1963 Actual_Expr
: constant Node_Id
:=
1964 Explicit_Actual_Parameter
(Insert_Param
);
1967 -- Case of insertion is first named actual
1969 if No
(Prev
) or else
1970 Nkind
(Parent
(Prev
)) /= N_Parameter_Association
1972 Set_Next_Named_Actual
1973 (Insert_Param
, First_Named_Actual
(Call_Node
));
1974 Set_First_Named_Actual
(Call_Node
, Actual_Expr
);
1977 if No
(Parameter_Associations
(Call_Node
)) then
1978 Set_Parameter_Associations
(Call_Node
, New_List
);
1981 Append
(Insert_Param
, Parameter_Associations
(Call_Node
));
1984 Insert_After
(Prev
, Insert_Param
);
1987 -- Case of insertion is not first named actual
1990 Set_Next_Named_Actual
1991 (Insert_Param
, Next_Named_Actual
(Parent
(Prev
)));
1992 Set_Next_Named_Actual
(Parent
(Prev
), Actual_Expr
);
1993 Append
(Insert_Param
, Parameter_Associations
(Call_Node
));
1996 Prev
:= Actual_Expr
;
1997 end Add_Actual_Parameter
;
1999 ----------------------
2000 -- Add_Extra_Actual --
2001 ----------------------
2003 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
) is
2004 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
2007 if Extra_Actuals
= No_List
then
2008 Extra_Actuals
:= New_List
;
2009 Set_Parent
(Extra_Actuals
, Call_Node
);
2012 Append_To
(Extra_Actuals
,
2013 Make_Parameter_Association
(Loc
,
2014 Selector_Name
=> Make_Identifier
(Loc
, Chars
(EF
)),
2015 Explicit_Actual_Parameter
=> Expr
));
2017 Analyze_And_Resolve
(Expr
, Etype
(EF
));
2019 if Nkind
(Call_Node
) = N_Function_Call
then
2020 Set_Is_Accessibility_Actual
(Parent
(Expr
));
2022 end Add_Extra_Actual
;
2028 procedure Do_Inline
(Subp
: Entity_Id
; Orig_Subp
: Entity_Id
) is
2029 Spec
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
2031 procedure Do_Backend_Inline
;
2032 -- Check that the call can be safely passed to the backend. If true
2033 -- then register the enclosing unit of Subp to Inlined_Bodies so that
2034 -- the body of Subp can be retrieved and analyzed by the backend.
2036 procedure Register_Backend_Call
(N
: Node_Id
);
2037 -- Append N to the list Backend_Calls
2039 -----------------------
2040 -- Do_Backend_Inline --
2041 -----------------------
2043 procedure Do_Backend_Inline
is
2045 -- No extra test needed for init subprograms since we know they
2046 -- are available to the backend!
2048 if Is_Init_Proc
(Subp
) then
2049 Add_Inlined_Body
(Subp
);
2050 Register_Backend_Call
(Call_Node
);
2052 -- Verify that if the body to inline is located in the current
2053 -- unit the inlining does not occur earlier. This avoids
2054 -- order-of-elaboration problems in the back end.
2056 elsif In_Same_Extended_Unit
(Call_Node
, Subp
)
2057 and then Nkind
(Spec
) = N_Subprogram_Declaration
2058 and then Earlier_In_Extended_Unit
2059 (Loc
, Sloc
(Body_To_Inline
(Spec
)))
2062 ("cannot inline& (body not seen yet)??", Call_Node
, Subp
);
2066 Backend_Inline
: Boolean := True;
2069 -- If we are compiling a package body that is not the
2070 -- main unit, it must be for inlining/instantiation
2071 -- purposes, in which case we inline the call to insure
2072 -- that the same temporaries are generated when compiling
2073 -- the body by itself. Otherwise link errors can occur.
2075 -- If the function being called is itself in the main
2076 -- unit, we cannot inline, because there is a risk of
2077 -- double elaboration and/or circularity: the inlining
2078 -- can make visible a private entity in the body of the
2079 -- main unit, that gigi will see before its sees its
2080 -- proper definition.
2082 if not (In_Extended_Main_Code_Unit
(Call_Node
))
2083 and then In_Package_Body
2086 not In_Extended_Main_Source_Unit
(Subp
);
2089 if Backend_Inline
then
2090 Add_Inlined_Body
(Subp
);
2091 Register_Backend_Call
(Call_Node
);
2095 end Do_Backend_Inline
;
2097 ---------------------------
2098 -- Register_Backend_Call --
2099 ---------------------------
2101 procedure Register_Backend_Call
(N
: Node_Id
) is
2103 if Backend_Calls
= No_Elist
then
2104 Backend_Calls
:= New_Elmt_List
;
2107 Append_Elmt
(N
, To
=> Backend_Calls
);
2108 end Register_Backend_Call
;
2110 -- Start of processing for Do_Inline
2113 -- Verify that the body to inline has already been seen
2116 or else Nkind
(Spec
) /= N_Subprogram_Declaration
2117 or else No
(Body_To_Inline
(Spec
))
2119 if Comes_From_Source
(Subp
)
2120 and then Must_Inline
(Subp
)
2123 ("cannot inline& (body not seen yet)?", Call_Node
, Subp
);
2125 -- Let the back end handle it
2132 -- If this an inherited function that returns a private type, do not
2133 -- inline if the full view is an unconstrained array, because such
2134 -- calls cannot be inlined.
2136 elsif Present
(Orig_Subp
)
2137 and then Is_Array_Type
(Etype
(Orig_Subp
))
2138 and then not Is_Constrained
(Etype
(Orig_Subp
))
2141 ("cannot inline& (unconstrained array)?", Call_Node
, Subp
);
2144 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
2148 ----------------------
2149 -- Do_Inline_Always --
2150 ----------------------
2152 procedure Do_Inline_Always
(Subp
: Entity_Id
; Orig_Subp
: Entity_Id
) is
2153 Spec
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
2154 Body_Id
: Entity_Id
;
2158 or else Nkind
(Spec
) /= N_Subprogram_Declaration
2159 or else No
(Body_To_Inline
(Spec
))
2160 or else Serious_Errors_Detected
/= 0
2165 Body_Id
:= Corresponding_Body
(Spec
);
2167 -- Verify that the body to inline has already been seen
2170 or else not Analyzed
(Body_Id
)
2172 Set_Is_Inlined
(Subp
, False);
2174 if Comes_From_Source
(Subp
) then
2176 -- Report a warning only if the call is located in the unit of
2177 -- the called subprogram; otherwise it is an error.
2179 if not In_Same_Extended_Unit
(Call_Node
, Subp
) then
2181 ("cannot inline& (body not seen yet)?", Call_Node
, Subp
,
2182 Is_Serious
=> True);
2184 elsif In_Open_Scopes
(Subp
) then
2186 -- For backward compatibility we generate the same error
2187 -- or warning of the previous implementation. This will
2188 -- be changed when we definitely incorporate the new
2191 if Front_End_Inlining
2192 and then Optimization_Level
= 0
2195 ("call to recursive subprogram cannot be inlined?p?",
2198 -- Do not emit error compiling runtime packages
2200 elsif Is_Predefined_File_Name
2201 (Unit_File_Name
(Get_Source_Unit
(Subp
)))
2204 ("call to recursive subprogram cannot be inlined??",
2209 ("call to recursive subprogram cannot be inlined",
2215 ("cannot inline& (body not seen yet)?", Call_Node
, Subp
);
2221 -- If this an inherited function that returns a private type, do not
2222 -- inline if the full view is an unconstrained array, because such
2223 -- calls cannot be inlined.
2225 elsif Present
(Orig_Subp
)
2226 and then Is_Array_Type
(Etype
(Orig_Subp
))
2227 and then not Is_Constrained
(Etype
(Orig_Subp
))
2230 ("cannot inline& (unconstrained array)?", Call_Node
, Subp
);
2232 -- If the called subprogram comes from an instance in the same
2233 -- unit, and the instance is not yet frozen, inlining might
2234 -- trigger order-of-elaboration problems.
2236 elsif In_Unfrozen_Instance
(Scope
(Subp
)) then
2238 ("cannot inline& (unfrozen instance)?", Call_Node
, Subp
);
2241 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
2243 end Do_Inline_Always
;
2245 ---------------------------
2246 -- Inherited_From_Formal --
2247 ---------------------------
2249 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
is
2251 Gen_Par
: Entity_Id
;
2252 Gen_Prim
: Elist_Id
;
2257 -- If the operation is inherited, it is attached to the corresponding
2258 -- type derivation. If the parent in the derivation is a generic
2259 -- actual, it is a subtype of the actual, and we have to recover the
2260 -- original derived type declaration to find the proper parent.
2262 if Nkind
(Parent
(S
)) /= N_Full_Type_Declaration
2263 or else not Is_Derived_Type
(Defining_Identifier
(Parent
(S
)))
2264 or else Nkind
(Type_Definition
(Original_Node
(Parent
(S
)))) /=
2265 N_Derived_Type_Definition
2266 or else not In_Instance
2273 (Type_Definition
(Original_Node
(Parent
(S
))));
2275 if Nkind
(Indic
) = N_Subtype_Indication
then
2276 Par
:= Entity
(Subtype_Mark
(Indic
));
2278 Par
:= Entity
(Indic
);
2282 if not Is_Generic_Actual_Type
(Par
)
2283 or else Is_Tagged_Type
(Par
)
2284 or else Nkind
(Parent
(Par
)) /= N_Subtype_Declaration
2285 or else not In_Open_Scopes
(Scope
(Par
))
2289 Gen_Par
:= Generic_Parent_Type
(Parent
(Par
));
2292 -- If the actual has no generic parent type, the formal is not
2293 -- a formal derived type, so nothing to inherit.
2295 if No
(Gen_Par
) then
2299 -- If the generic parent type is still the generic type, this is a
2300 -- private formal, not a derived formal, and there are no operations
2301 -- inherited from the formal.
2303 if Nkind
(Parent
(Gen_Par
)) = N_Formal_Type_Declaration
then
2307 Gen_Prim
:= Collect_Primitive_Operations
(Gen_Par
);
2309 Elmt
:= First_Elmt
(Gen_Prim
);
2310 while Present
(Elmt
) loop
2311 if Chars
(Node
(Elmt
)) = Chars
(S
) then
2317 F1
:= First_Formal
(S
);
2318 F2
:= First_Formal
(Node
(Elmt
));
2320 and then Present
(F2
)
2322 if Etype
(F1
) = Etype
(F2
)
2323 or else Etype
(F2
) = Gen_Par
2329 exit; -- not the right subprogram
2341 raise Program_Error
;
2342 end Inherited_From_Formal
;
2344 --------------------------
2345 -- In_Unfrozen_Instance --
2346 --------------------------
2348 function In_Unfrozen_Instance
(E
: Entity_Id
) return Boolean is
2353 while Present
(S
) and then S
/= Standard_Standard
loop
2354 if Is_Generic_Instance
(S
)
2355 and then Present
(Freeze_Node
(S
))
2356 and then not Analyzed
(Freeze_Node
(S
))
2365 end In_Unfrozen_Instance
;
2367 -------------------------
2368 -- Is_Direct_Deep_Call --
2369 -------------------------
2371 function Is_Direct_Deep_Call
(Subp
: Entity_Id
) return Boolean is
2373 if Is_TSS
(Subp
, TSS_Deep_Adjust
)
2374 or else Is_TSS
(Subp
, TSS_Deep_Finalize
)
2375 or else Is_TSS
(Subp
, TSS_Deep_Initialize
)
2382 Actual
:= First
(Parameter_Associations
(N
));
2383 Formal
:= First_Formal
(Subp
);
2384 while Present
(Actual
)
2385 and then Present
(Formal
)
2387 if Nkind
(Actual
) = N_Identifier
2388 and then Is_Controlling_Actual
(Actual
)
2389 and then Etype
(Actual
) = Etype
(Formal
)
2395 Next_Formal
(Formal
);
2401 end Is_Direct_Deep_Call
;
2407 function New_Value
(From
: Node_Id
) return Node_Id
is
2408 Res
: constant Node_Id
:= Duplicate_Subexpr
(From
);
2410 if Is_Access_Type
(Etype
(From
)) then
2411 return Make_Explicit_Dereference
(Sloc
(From
), Prefix
=> Res
);
2419 Curr_S
: constant Entity_Id
:= Current_Scope
;
2420 Remote
: constant Boolean := Is_Remote_Call
(Call_Node
);
2423 Orig_Subp
: Entity_Id
:= Empty
;
2424 Param_Count
: Natural := 0;
2425 Parent_Formal
: Entity_Id
;
2426 Parent_Subp
: Entity_Id
;
2430 Prev_Orig
: Node_Id
;
2431 -- Original node for an actual, which may have been rewritten. If the
2432 -- actual is a function call that has been transformed from a selected
2433 -- component, the original node is unanalyzed. Otherwise, it carries
2434 -- semantic information used to generate additional actuals.
2436 CW_Interface_Formals_Present
: Boolean := False;
2438 -- Start of processing for Expand_Call
2441 -- Expand the procedure call if the first actual has a dimension and if
2442 -- the procedure is Put (Ada 2012).
2444 if Ada_Version
>= Ada_2012
2445 and then Nkind
(Call_Node
) = N_Procedure_Call_Statement
2446 and then Present
(Parameter_Associations
(Call_Node
))
2448 Expand_Put_Call_With_Symbol
(Call_Node
);
2451 -- Ignore if previous error
2453 if Nkind
(Call_Node
) in N_Has_Etype
2454 and then Etype
(Call_Node
) = Any_Type
2459 -- Call using access to subprogram with explicit dereference
2461 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
2462 Subp
:= Etype
(Name
(Call_Node
));
2463 Parent_Subp
:= Empty
;
2465 -- Case of call to simple entry, where the Name is a selected component
2466 -- whose prefix is the task, and whose selector name is the entry name
2468 elsif Nkind
(Name
(Call_Node
)) = N_Selected_Component
then
2469 Subp
:= Entity
(Selector_Name
(Name
(Call_Node
)));
2470 Parent_Subp
:= Empty
;
2472 -- Case of call to member of entry family, where Name is an indexed
2473 -- component, with the prefix being a selected component giving the
2474 -- task and entry family name, and the index being the entry index.
2476 elsif Nkind
(Name
(Call_Node
)) = N_Indexed_Component
then
2477 Subp
:= Entity
(Selector_Name
(Prefix
(Name
(Call_Node
))));
2478 Parent_Subp
:= Empty
;
2483 Subp
:= Entity
(Name
(Call_Node
));
2484 Parent_Subp
:= Alias
(Subp
);
2486 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2487 -- if we can tell that the first parameter cannot possibly be null.
2488 -- This improves efficiency by avoiding a run-time test.
2490 -- We do not do this if Raise_Exception_Always does not exist, which
2491 -- can happen in configurable run time profiles which provide only a
2494 if Is_RTE
(Subp
, RE_Raise_Exception
)
2495 and then RTE_Available
(RE_Raise_Exception_Always
)
2498 FA
: constant Node_Id
:=
2499 Original_Node
(First_Actual
(Call_Node
));
2502 -- The case we catch is where the first argument is obtained
2503 -- using the Identity attribute (which must always be
2506 if Nkind
(FA
) = N_Attribute_Reference
2507 and then Attribute_Name
(FA
) = Name_Identity
2509 Subp
:= RTE
(RE_Raise_Exception_Always
);
2510 Set_Name
(Call_Node
, New_Occurrence_Of
(Subp
, Loc
));
2515 if Ekind
(Subp
) = E_Entry
then
2516 Parent_Subp
:= Empty
;
2520 -- Detect the following code in System.Finalization_Masters only on
2521 -- .NET/JVM targets:
2523 -- procedure Finalize (Master : in out Finalization_Master) is
2527 -- Finalize (Curr_Ptr.all);
2529 -- Since .NET/JVM compilers lack address arithmetic and Deep_Finalize
2530 -- cannot be named in library or user code, the compiler has to install
2531 -- a kludge and transform the call to Finalize into Deep_Finalize.
2533 if VM_Target
/= No_VM
2534 and then Chars
(Subp
) = Name_Finalize
2535 and then Ekind
(Curr_S
) = E_Block
2536 and then Ekind
(Scope
(Curr_S
)) = E_Procedure
2537 and then Chars
(Scope
(Curr_S
)) = Name_Finalize
2538 and then Etype
(First_Formal
(Scope
(Curr_S
))) =
2539 RTE
(RE_Finalization_Master
)
2542 Deep_Fin
: constant Entity_Id
:=
2543 Find_Prim_Op
(RTE
(RE_Root_Controlled
),
2546 -- Since Root_Controlled is a tagged type, the compiler should
2547 -- always generate Deep_Finalize for it.
2549 pragma Assert
(Present
(Deep_Fin
));
2552 -- Deep_Finalize (Curr_Ptr.all);
2555 Make_Procedure_Call_Statement
(Loc
,
2557 New_Reference_To
(Deep_Fin
, Loc
),
2558 Parameter_Associations
=>
2559 New_Copy_List_Tree
(Parameter_Associations
(N
))));
2566 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2567 -- alternative in an asynchronous select or as an entry call in
2568 -- a conditional or timed select. Check whether the procedure call
2569 -- is a renaming of an entry and rewrite it as an entry call.
2571 if Ada_Version
>= Ada_2005
2572 and then Nkind
(Call_Node
) = N_Procedure_Call_Statement
2574 ((Nkind
(Parent
(Call_Node
)) = N_Triggering_Alternative
2575 and then Triggering_Statement
(Parent
(Call_Node
)) = Call_Node
)
2577 (Nkind
(Parent
(Call_Node
)) = N_Entry_Call_Alternative
2578 and then Entry_Call_Statement
(Parent
(Call_Node
)) = Call_Node
))
2582 Ren_Root
: Entity_Id
:= Subp
;
2585 -- This may be a chain of renamings, find the root
2587 if Present
(Alias
(Ren_Root
)) then
2588 Ren_Root
:= Alias
(Ren_Root
);
2591 if Present
(Original_Node
(Parent
(Parent
(Ren_Root
)))) then
2592 Ren_Decl
:= Original_Node
(Parent
(Parent
(Ren_Root
)));
2594 if Nkind
(Ren_Decl
) = N_Subprogram_Renaming_Declaration
then
2596 Make_Entry_Call_Statement
(Loc
,
2598 New_Copy_Tree
(Name
(Ren_Decl
)),
2599 Parameter_Associations
=>
2601 (Parameter_Associations
(Call_Node
))));
2609 -- First step, compute extra actuals, corresponding to any Extra_Formals
2610 -- present. Note that we do not access Extra_Formals directly, instead
2611 -- we simply note the presence of the extra formals as we process the
2612 -- regular formals collecting corresponding actuals in Extra_Actuals.
2614 -- We also generate any required range checks for actuals for in formals
2615 -- as we go through the loop, since this is a convenient place to do it.
2616 -- (Though it seems that this would be better done in Expand_Actuals???)
2618 -- Special case: Thunks must not compute the extra actuals; they must
2619 -- just propagate to the target primitive their extra actuals.
2621 if Is_Thunk
(Current_Scope
)
2622 and then Thunk_Entity
(Current_Scope
) = Subp
2623 and then Present
(Extra_Formals
(Subp
))
2625 pragma Assert
(Present
(Extra_Formals
(Current_Scope
)));
2628 Target_Formal
: Entity_Id
;
2629 Thunk_Formal
: Entity_Id
;
2632 Target_Formal
:= Extra_Formals
(Subp
);
2633 Thunk_Formal
:= Extra_Formals
(Current_Scope
);
2634 while Present
(Target_Formal
) loop
2636 (New_Occurrence_Of
(Thunk_Formal
, Loc
), Thunk_Formal
);
2638 Target_Formal
:= Extra_Formal
(Target_Formal
);
2639 Thunk_Formal
:= Extra_Formal
(Thunk_Formal
);
2642 while Is_Non_Empty_List
(Extra_Actuals
) loop
2643 Add_Actual_Parameter
(Remove_Head
(Extra_Actuals
));
2646 Expand_Actuals
(Call_Node
, Subp
);
2651 Formal
:= First_Formal
(Subp
);
2652 Actual
:= First_Actual
(Call_Node
);
2654 while Present
(Formal
) loop
2656 -- Generate range check if required
2658 if Do_Range_Check
(Actual
)
2659 and then Ekind
(Formal
) = E_In_Parameter
2661 Set_Do_Range_Check
(Actual
, False);
2662 Generate_Range_Check
2663 (Actual
, Etype
(Formal
), CE_Range_Check_Failed
);
2666 -- Prepare to examine current entry
2669 Prev_Orig
:= Original_Node
(Prev
);
2671 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2672 -- to expand it in a further round.
2674 CW_Interface_Formals_Present
:=
2675 CW_Interface_Formals_Present
2677 (Ekind
(Etype
(Formal
)) = E_Class_Wide_Type
2678 and then Is_Interface
(Etype
(Etype
(Formal
))))
2680 (Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
2681 and then Is_Interface
(Directly_Designated_Type
2682 (Etype
(Etype
(Formal
)))));
2684 -- Create possible extra actual for constrained case. Usually, the
2685 -- extra actual is of the form actual'constrained, but since this
2686 -- attribute is only available for unconstrained records, TRUE is
2687 -- expanded if the type of the formal happens to be constrained (for
2688 -- instance when this procedure is inherited from an unconstrained
2689 -- record to a constrained one) or if the actual has no discriminant
2690 -- (its type is constrained). An exception to this is the case of a
2691 -- private type without discriminants. In this case we pass FALSE
2692 -- because the object has underlying discriminants with defaults.
2694 if Present
(Extra_Constrained
(Formal
)) then
2695 if Ekind
(Etype
(Prev
)) in Private_Kind
2696 and then not Has_Discriminants
(Base_Type
(Etype
(Prev
)))
2699 (New_Occurrence_Of
(Standard_False
, Loc
),
2700 Extra_Constrained
(Formal
));
2702 elsif Is_Constrained
(Etype
(Formal
))
2703 or else not Has_Discriminants
(Etype
(Prev
))
2706 (New_Occurrence_Of
(Standard_True
, Loc
),
2707 Extra_Constrained
(Formal
));
2709 -- Do not produce extra actuals for Unchecked_Union parameters.
2710 -- Jump directly to the end of the loop.
2712 elsif Is_Unchecked_Union
(Base_Type
(Etype
(Actual
))) then
2713 goto Skip_Extra_Actual_Generation
;
2716 -- If the actual is a type conversion, then the constrained
2717 -- test applies to the actual, not the target type.
2723 -- Test for unchecked conversions as well, which can occur
2724 -- as out parameter actuals on calls to stream procedures.
2727 while Nkind_In
(Act_Prev
, N_Type_Conversion
,
2728 N_Unchecked_Type_Conversion
)
2730 Act_Prev
:= Expression
(Act_Prev
);
2733 -- If the expression is a conversion of a dereference, this
2734 -- is internally generated code that manipulates addresses,
2735 -- e.g. when building interface tables. No check should
2736 -- occur in this case, and the discriminated object is not
2739 if not Comes_From_Source
(Actual
)
2740 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
2741 and then Nkind
(Act_Prev
) = N_Explicit_Dereference
2744 (New_Occurrence_Of
(Standard_False
, Loc
),
2745 Extra_Constrained
(Formal
));
2749 (Make_Attribute_Reference
(Sloc
(Prev
),
2751 Duplicate_Subexpr_No_Checks
2752 (Act_Prev
, Name_Req
=> True),
2753 Attribute_Name
=> Name_Constrained
),
2754 Extra_Constrained
(Formal
));
2760 -- Create possible extra actual for accessibility level
2762 if Present
(Extra_Accessibility
(Formal
)) then
2764 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2765 -- attribute, then the original actual may be an aliased object
2766 -- occurring as the prefix in a call using "Object.Operation"
2767 -- notation. In that case we must pass the level of the object,
2768 -- so Prev_Orig is reset to Prev and the attribute will be
2769 -- processed by the code for Access attributes further below.
2771 if Prev_Orig
/= Prev
2772 and then Nkind
(Prev
) = N_Attribute_Reference
2774 Get_Attribute_Id
(Attribute_Name
(Prev
)) = Attribute_Access
2775 and then Is_Aliased_View
(Prev_Orig
)
2780 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2781 -- accessibility levels.
2783 if Is_Thunk
(Current_Scope
) then
2785 Parm_Ent
: Entity_Id
;
2788 if Is_Controlling_Actual
(Actual
) then
2790 -- Find the corresponding actual of the thunk
2792 Parm_Ent
:= First_Entity
(Current_Scope
);
2793 for J
in 2 .. Param_Count
loop
2794 Next_Entity
(Parm_Ent
);
2797 -- Handle unchecked conversion of access types generated
2798 -- in thunks (cf. Expand_Interface_Thunk).
2800 elsif Is_Access_Type
(Etype
(Actual
))
2801 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
2803 Parm_Ent
:= Entity
(Expression
(Actual
));
2805 else pragma Assert
(Is_Entity_Name
(Actual
));
2806 Parm_Ent
:= Entity
(Actual
);
2810 (New_Occurrence_Of
(Extra_Accessibility
(Parm_Ent
), Loc
),
2811 Extra_Accessibility
(Formal
));
2814 elsif Is_Entity_Name
(Prev_Orig
) then
2816 -- When passing an access parameter, or a renaming of an access
2817 -- parameter, as the actual to another access parameter we need
2818 -- to pass along the actual's own access level parameter. This
2819 -- is done if we are within the scope of the formal access
2820 -- parameter (if this is an inlined body the extra formal is
2823 if (Is_Formal
(Entity
(Prev_Orig
))
2825 (Present
(Renamed_Object
(Entity
(Prev_Orig
)))
2827 Is_Entity_Name
(Renamed_Object
(Entity
(Prev_Orig
)))
2830 (Entity
(Renamed_Object
(Entity
(Prev_Orig
))))))
2831 and then Ekind
(Etype
(Prev_Orig
)) = E_Anonymous_Access_Type
2832 and then In_Open_Scopes
(Scope
(Entity
(Prev_Orig
)))
2835 Parm_Ent
: constant Entity_Id
:= Param_Entity
(Prev_Orig
);
2838 pragma Assert
(Present
(Parm_Ent
));
2840 if Present
(Extra_Accessibility
(Parm_Ent
)) then
2843 (Extra_Accessibility
(Parm_Ent
), Loc
),
2844 Extra_Accessibility
(Formal
));
2846 -- If the actual access parameter does not have an
2847 -- associated extra formal providing its scope level,
2848 -- then treat the actual as having library-level
2853 (Make_Integer_Literal
(Loc
,
2854 Intval
=> Scope_Depth
(Standard_Standard
)),
2855 Extra_Accessibility
(Formal
));
2859 -- The actual is a normal access value, so just pass the level
2860 -- of the actual's access type.
2864 (Dynamic_Accessibility_Level
(Prev_Orig
),
2865 Extra_Accessibility
(Formal
));
2868 -- If the actual is an access discriminant, then pass the level
2869 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2871 elsif Nkind
(Prev_Orig
) = N_Selected_Component
2872 and then Ekind
(Entity
(Selector_Name
(Prev_Orig
))) =
2874 and then Ekind
(Etype
(Entity
(Selector_Name
(Prev_Orig
)))) =
2875 E_Anonymous_Access_Type
2878 (Make_Integer_Literal
(Loc
,
2879 Intval
=> Object_Access_Level
(Prefix
(Prev_Orig
))),
2880 Extra_Accessibility
(Formal
));
2885 case Nkind
(Prev_Orig
) is
2887 when N_Attribute_Reference
=>
2888 case Get_Attribute_Id
(Attribute_Name
(Prev_Orig
)) is
2890 -- For X'Access, pass on the level of the prefix X
2892 when Attribute_Access
=>
2894 -- If this is an Access attribute applied to the
2895 -- the current instance object passed to a type
2896 -- initialization procedure, then use the level
2897 -- of the type itself. This is not really correct,
2898 -- as there should be an extra level parameter
2899 -- passed in with _init formals (only in the case
2900 -- where the type is immutably limited), but we
2901 -- don't have an easy way currently to create such
2902 -- an extra formal (init procs aren't ever frozen).
2903 -- For now we just use the level of the type,
2904 -- which may be too shallow, but that works better
2905 -- than passing Object_Access_Level of the type,
2906 -- which can be one level too deep in some cases.
2909 if Is_Entity_Name
(Prefix
(Prev_Orig
))
2910 and then Is_Type
(Entity
(Prefix
(Prev_Orig
)))
2913 (Make_Integer_Literal
(Loc
,
2916 (Entity
(Prefix
(Prev_Orig
)))),
2917 Extra_Accessibility
(Formal
));
2921 (Make_Integer_Literal
(Loc
,
2924 (Prefix
(Prev_Orig
))),
2925 Extra_Accessibility
(Formal
));
2928 -- Treat the unchecked attributes as library-level
2930 when Attribute_Unchecked_Access |
2931 Attribute_Unrestricted_Access
=>
2933 (Make_Integer_Literal
(Loc
,
2934 Intval
=> Scope_Depth
(Standard_Standard
)),
2935 Extra_Accessibility
(Formal
));
2937 -- No other cases of attributes returning access
2938 -- values that can be passed to access parameters.
2941 raise Program_Error
;
2945 -- For allocators we pass the level of the execution of the
2946 -- called subprogram, which is one greater than the current
2951 (Make_Integer_Literal
(Loc
,
2952 Intval
=> Scope_Depth
(Current_Scope
) + 1),
2953 Extra_Accessibility
(Formal
));
2955 -- For most other cases we simply pass the level of the
2956 -- actual's access type. The type is retrieved from
2957 -- Prev rather than Prev_Orig, because in some cases
2958 -- Prev_Orig denotes an original expression that has
2959 -- not been analyzed.
2963 (Dynamic_Accessibility_Level
(Prev
),
2964 Extra_Accessibility
(Formal
));
2969 -- Perform the check of 4.6(49) that prevents a null value from being
2970 -- passed as an actual to an access parameter. Note that the check
2971 -- is elided in the common cases of passing an access attribute or
2972 -- access parameter as an actual. Also, we currently don't enforce
2973 -- this check for expander-generated actuals and when -gnatdj is set.
2975 if Ada_Version
>= Ada_2005
then
2977 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
2978 -- the intent of 6.4.1(13) is that null-exclusion checks should
2979 -- not be done for 'out' parameters, even though it refers only
2980 -- to constraint checks, and a null_exclusion is not a constraint.
2981 -- Note that AI05-0196-1 corrects this mistake in the RM.
2983 if Is_Access_Type
(Etype
(Formal
))
2984 and then Can_Never_Be_Null
(Etype
(Formal
))
2985 and then Ekind
(Formal
) /= E_Out_Parameter
2986 and then Nkind
(Prev
) /= N_Raise_Constraint_Error
2987 and then (Known_Null
(Prev
)
2988 or else not Can_Never_Be_Null
(Etype
(Prev
)))
2990 Install_Null_Excluding_Check
(Prev
);
2993 -- Ada_Version < Ada_2005
2996 if Ekind
(Etype
(Formal
)) /= E_Anonymous_Access_Type
2997 or else Access_Checks_Suppressed
(Subp
)
3001 elsif Debug_Flag_J
then
3004 elsif not Comes_From_Source
(Prev
) then
3007 elsif Is_Entity_Name
(Prev
)
3008 and then Ekind
(Etype
(Prev
)) = E_Anonymous_Access_Type
3012 elsif Nkind_In
(Prev
, N_Allocator
, N_Attribute_Reference
) then
3015 -- Suppress null checks when passing to access parameters of Java
3016 -- and CIL subprograms. (Should this be done for other foreign
3017 -- conventions as well ???)
3019 elsif Convention
(Subp
) = Convention_Java
3020 or else Convention
(Subp
) = Convention_CIL
3025 Install_Null_Excluding_Check
(Prev
);
3029 -- Perform appropriate validity checks on parameters that
3032 if Validity_Checks_On
then
3033 if (Ekind
(Formal
) = E_In_Parameter
3034 and then Validity_Check_In_Params
)
3036 (Ekind
(Formal
) = E_In_Out_Parameter
3037 and then Validity_Check_In_Out_Params
)
3039 -- If the actual is an indexed component of a packed type (or
3040 -- is an indexed or selected component whose prefix recursively
3041 -- meets this condition), it has not been expanded yet. It will
3042 -- be copied in the validity code that follows, and has to be
3043 -- expanded appropriately, so reanalyze it.
3045 -- What we do is just to unset analyzed bits on prefixes till
3046 -- we reach something that does not have a prefix.
3053 while Nkind_In
(Nod
, N_Indexed_Component
,
3054 N_Selected_Component
)
3056 Set_Analyzed
(Nod
, False);
3057 Nod
:= Prefix
(Nod
);
3061 Ensure_Valid
(Actual
);
3065 -- For Ada 2012, if a parameter is aliased, the actual must be a
3066 -- tagged type or an aliased view of an object.
3068 if Is_Aliased
(Formal
)
3069 and then not Is_Aliased_View
(Actual
)
3070 and then not Is_Tagged_Type
(Etype
(Formal
))
3073 ("actual for aliased formal& must be aliased object",
3077 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3078 -- since this is a left side reference. We only do this for calls
3079 -- from the source program since we assume that compiler generated
3080 -- calls explicitly generate any required checks. We also need it
3081 -- only if we are doing standard validity checks, since clearly it is
3082 -- not needed if validity checks are off, and in subscript validity
3083 -- checking mode, all indexed components are checked with a call
3084 -- directly from Expand_N_Indexed_Component.
3086 if Comes_From_Source
(Call_Node
)
3087 and then Ekind
(Formal
) /= E_In_Parameter
3088 and then Validity_Checks_On
3089 and then Validity_Check_Default
3090 and then not Validity_Check_Subscripts
3092 Check_Valid_Lvalue_Subscripts
(Actual
);
3095 -- Mark any scalar OUT parameter that is a simple variable as no
3096 -- longer known to be valid (unless the type is always valid). This
3097 -- reflects the fact that if an OUT parameter is never set in a
3098 -- procedure, then it can become invalid on the procedure return.
3100 if Ekind
(Formal
) = E_Out_Parameter
3101 and then Is_Entity_Name
(Actual
)
3102 and then Ekind
(Entity
(Actual
)) = E_Variable
3103 and then not Is_Known_Valid
(Etype
(Actual
))
3105 Set_Is_Known_Valid
(Entity
(Actual
), False);
3108 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3109 -- clear current values, since they can be clobbered. We are probably
3110 -- doing this in more places than we need to, but better safe than
3111 -- sorry when it comes to retaining bad current values!
3113 if Ekind
(Formal
) /= E_In_Parameter
3114 and then Is_Entity_Name
(Actual
)
3115 and then Present
(Entity
(Actual
))
3118 Ent
: constant Entity_Id
:= Entity
(Actual
);
3122 -- For an OUT or IN OUT parameter that is an assignable entity,
3123 -- we do not want to clobber the Last_Assignment field, since
3124 -- if it is set, it was precisely because it is indeed an OUT
3125 -- or IN OUT parameter! We do reset the Is_Known_Valid flag
3126 -- since the subprogram could have returned in invalid value.
3128 if Ekind_In
(Formal
, E_Out_Parameter
, E_In_Out_Parameter
)
3129 and then Is_Assignable
(Ent
)
3131 Sav
:= Last_Assignment
(Ent
);
3132 Kill_Current_Values
(Ent
);
3133 Set_Last_Assignment
(Ent
, Sav
);
3134 Set_Is_Known_Valid
(Ent
, False);
3136 -- For all other cases, just kill the current values
3139 Kill_Current_Values
(Ent
);
3144 -- If the formal is class wide and the actual is an aggregate, force
3145 -- evaluation so that the back end who does not know about class-wide
3146 -- type, does not generate a temporary of the wrong size.
3148 if not Is_Class_Wide_Type
(Etype
(Formal
)) then
3151 elsif Nkind
(Actual
) = N_Aggregate
3152 or else (Nkind
(Actual
) = N_Qualified_Expression
3153 and then Nkind
(Expression
(Actual
)) = N_Aggregate
)
3155 Force_Evaluation
(Actual
);
3158 -- In a remote call, if the formal is of a class-wide type, check
3159 -- that the actual meets the requirements described in E.4(18).
3161 if Remote
and then Is_Class_Wide_Type
(Etype
(Formal
)) then
3162 Insert_Action
(Actual
,
3163 Make_Transportable_Check
(Loc
,
3164 Duplicate_Subexpr_Move_Checks
(Actual
)));
3167 -- This label is required when skipping extra actual generation for
3168 -- Unchecked_Union parameters.
3170 <<Skip_Extra_Actual_Generation
>>
3172 Param_Count
:= Param_Count
+ 1;
3173 Next_Actual
(Actual
);
3174 Next_Formal
(Formal
);
3177 -- If we are calling an Ada 2012 function which needs to have the
3178 -- "accessibility level determined by the point of call" (AI05-0234)
3179 -- passed in to it, then pass it in.
3181 if Ekind_In
(Subp
, E_Function
, E_Operator
, E_Subprogram_Type
)
3183 Present
(Extra_Accessibility_Of_Result
(Ultimate_Alias
(Subp
)))
3186 Ancestor
: Node_Id
:= Parent
(Call_Node
);
3187 Level
: Node_Id
:= Empty
;
3188 Defer
: Boolean := False;
3191 -- Unimplemented: if Subp returns an anonymous access type, then
3193 -- a) if the call is the operand of an explict conversion, then
3194 -- the target type of the conversion (a named access type)
3195 -- determines the accessibility level pass in;
3197 -- b) if the call defines an access discriminant of an object
3198 -- (e.g., the discriminant of an object being created by an
3199 -- allocator, or the discriminant of a function result),
3200 -- then the accessibility level to pass in is that of the
3201 -- discriminated object being initialized).
3205 while Nkind
(Ancestor
) = N_Qualified_Expression
3207 Ancestor
:= Parent
(Ancestor
);
3210 case Nkind
(Ancestor
) is
3213 -- At this point, we'd like to assign
3215 -- Level := Dynamic_Accessibility_Level (Ancestor);
3217 -- but Etype of Ancestor may not have been set yet,
3218 -- so that doesn't work.
3220 -- Handle this later in Expand_Allocator_Expression.
3224 when N_Object_Declaration | N_Object_Renaming_Declaration
=>
3226 Def_Id
: constant Entity_Id
:=
3227 Defining_Identifier
(Ancestor
);
3230 if Is_Return_Object
(Def_Id
) then
3231 if Present
(Extra_Accessibility_Of_Result
3232 (Return_Applies_To
(Scope
(Def_Id
))))
3234 -- Pass along value that was passed in if the
3235 -- routine we are returning from also has an
3236 -- Accessibility_Of_Result formal.
3240 (Extra_Accessibility_Of_Result
3241 (Return_Applies_To
(Scope
(Def_Id
))), Loc
);
3245 Make_Integer_Literal
(Loc
,
3246 Intval
=> Object_Access_Level
(Def_Id
));
3250 when N_Simple_Return_Statement
=>
3251 if Present
(Extra_Accessibility_Of_Result
3253 (Return_Statement_Entity
(Ancestor
))))
3255 -- Pass along value that was passed in if the routine
3256 -- we are returning from also has an
3257 -- Accessibility_Of_Result formal.
3261 (Extra_Accessibility_Of_Result
3263 (Return_Statement_Entity
(Ancestor
))), Loc
);
3271 if not Present
(Level
) then
3273 -- The "innermost master that evaluates the function call".
3275 -- ??? - Should we use Integer'Last here instead in order
3276 -- to deal with (some of) the problems associated with
3277 -- calls to subps whose enclosing scope is unknown (e.g.,
3278 -- Anon_Access_To_Subp_Param.all)?
3280 Level
:= Make_Integer_Literal
(Loc
,
3281 Scope_Depth
(Current_Scope
) + 1);
3286 Extra_Accessibility_Of_Result
(Ultimate_Alias
(Subp
)));
3291 -- If we are expanding the RHS of an assignment we need to check if tag
3292 -- propagation is needed. You might expect this processing to be in
3293 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3294 -- assignment might be transformed to a declaration for an unconstrained
3295 -- value if the expression is classwide.
3297 if Nkind
(Call_Node
) = N_Function_Call
3298 and then Is_Tag_Indeterminate
(Call_Node
)
3299 and then Is_Entity_Name
(Name
(Call_Node
))
3302 Ass
: Node_Id
:= Empty
;
3305 if Nkind
(Parent
(Call_Node
)) = N_Assignment_Statement
then
3306 Ass
:= Parent
(Call_Node
);
3308 elsif Nkind
(Parent
(Call_Node
)) = N_Qualified_Expression
3309 and then Nkind
(Parent
(Parent
(Call_Node
))) =
3310 N_Assignment_Statement
3312 Ass
:= Parent
(Parent
(Call_Node
));
3314 elsif Nkind
(Parent
(Call_Node
)) = N_Explicit_Dereference
3315 and then Nkind
(Parent
(Parent
(Call_Node
))) =
3316 N_Assignment_Statement
3318 Ass
:= Parent
(Parent
(Call_Node
));
3322 and then Is_Class_Wide_Type
(Etype
(Name
(Ass
)))
3324 if Is_Access_Type
(Etype
(Call_Node
)) then
3325 if Designated_Type
(Etype
(Call_Node
)) /=
3326 Root_Type
(Etype
(Name
(Ass
)))
3329 ("tag-indeterminate expression "
3330 & " must have designated type& (RM 5.2 (6))",
3331 Call_Node
, Root_Type
(Etype
(Name
(Ass
))));
3333 Propagate_Tag
(Name
(Ass
), Call_Node
);
3336 elsif Etype
(Call_Node
) /= Root_Type
(Etype
(Name
(Ass
))) then
3338 ("tag-indeterminate expression must have type&"
3340 Call_Node
, Root_Type
(Etype
(Name
(Ass
))));
3343 Propagate_Tag
(Name
(Ass
), Call_Node
);
3346 -- The call will be rewritten as a dispatching call, and
3347 -- expanded as such.
3354 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3355 -- it to point to the correct secondary virtual table
3357 if Nkind
(Call_Node
) in N_Subprogram_Call
3358 and then CW_Interface_Formals_Present
3360 Expand_Interface_Actuals
(Call_Node
);
3363 -- Deals with Dispatch_Call if we still have a call, before expanding
3364 -- extra actuals since this will be done on the re-analysis of the
3365 -- dispatching call. Note that we do not try to shorten the actual list
3366 -- for a dispatching call, it would not make sense to do so. Expansion
3367 -- of dispatching calls is suppressed when VM_Target, because the VM
3368 -- back-ends directly handle the generation of dispatching calls and
3369 -- would have to undo any expansion to an indirect call.
3371 if Nkind
(Call_Node
) in N_Subprogram_Call
3372 and then Present
(Controlling_Argument
(Call_Node
))
3375 Call_Typ
: constant Entity_Id
:= Etype
(Call_Node
);
3376 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
3377 Eq_Prim_Op
: Entity_Id
:= Empty
;
3380 Prev_Call
: Node_Id
;
3383 if not Is_Limited_Type
(Typ
) then
3384 Eq_Prim_Op
:= Find_Prim_Op
(Typ
, Name_Op_Eq
);
3387 if Tagged_Type_Expansion
then
3388 Expand_Dispatching_Call
(Call_Node
);
3390 -- The following return is worrisome. Is it really OK to skip
3391 -- all remaining processing in this procedure ???
3398 Apply_Tag_Checks
(Call_Node
);
3400 -- If this is a dispatching "=", we must first compare the
3401 -- tags so we generate: x.tag = y.tag and then x = y
3403 if Subp
= Eq_Prim_Op
then
3405 -- Mark the node as analyzed to avoid reanalizing this
3406 -- dispatching call (which would cause a never-ending loop)
3408 Prev_Call
:= Relocate_Node
(Call_Node
);
3409 Set_Analyzed
(Prev_Call
);
3411 Param
:= First_Actual
(Call_Node
);
3417 Make_Selected_Component
(Loc
,
3418 Prefix
=> New_Value
(Param
),
3420 New_Reference_To
(First_Tag_Component
(Typ
),
3424 Make_Selected_Component
(Loc
,
3426 Unchecked_Convert_To
(Typ
,
3427 New_Value
(Next_Actual
(Param
))),
3430 (First_Tag_Component
(Typ
), Loc
))),
3431 Right_Opnd
=> Prev_Call
);
3433 Rewrite
(Call_Node
, New_Call
);
3436 (Call_Node
, Call_Typ
, Suppress
=> All_Checks
);
3439 -- Expansion of a dispatching call results in an indirect call,
3440 -- which in turn causes current values to be killed (see
3441 -- Resolve_Call), so on VM targets we do the call here to
3442 -- ensure consistent warnings between VM and non-VM targets.
3444 Kill_Current_Values
;
3447 -- If this is a dispatching "=" then we must update the reference
3448 -- to the call node because we generated:
3449 -- x.tag = y.tag and then x = y
3451 if Subp
= Eq_Prim_Op
then
3452 Call_Node
:= Right_Opnd
(Call_Node
);
3457 -- Similarly, expand calls to RCI subprograms on which pragma
3458 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3459 -- later. Do this only when the call comes from source since we
3460 -- do not want such a rewriting to occur in expanded code.
3462 if Is_All_Remote_Call
(Call_Node
) then
3463 Expand_All_Calls_Remote_Subprogram_Call
(Call_Node
);
3465 -- Similarly, do not add extra actuals for an entry call whose entity
3466 -- is a protected procedure, or for an internal protected subprogram
3467 -- call, because it will be rewritten as a protected subprogram call
3468 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3470 elsif Is_Protected_Type
(Scope
(Subp
))
3471 and then (Ekind
(Subp
) = E_Procedure
3472 or else Ekind
(Subp
) = E_Function
)
3476 -- During that loop we gathered the extra actuals (the ones that
3477 -- correspond to Extra_Formals), so now they can be appended.
3480 while Is_Non_Empty_List
(Extra_Actuals
) loop
3481 Add_Actual_Parameter
(Remove_Head
(Extra_Actuals
));
3485 -- At this point we have all the actuals, so this is the point at which
3486 -- the various expansion activities for actuals is carried out.
3488 Expand_Actuals
(Call_Node
, Subp
);
3490 -- Verify that the actuals do not share storage. This check must be done
3491 -- on the caller side rather that inside the subprogram to avoid issues
3492 -- of parameter passing.
3494 if Check_Aliasing_Of_Parameters
then
3495 Apply_Parameter_Aliasing_Checks
(Call_Node
, Subp
);
3498 -- If the subprogram is a renaming, or if it is inherited, replace it in
3499 -- the call with the name of the actual subprogram being called. If this
3500 -- is a dispatching call, the run-time decides what to call. The Alias
3501 -- attribute does not apply to entries.
3503 if Nkind
(Call_Node
) /= N_Entry_Call_Statement
3504 and then No
(Controlling_Argument
(Call_Node
))
3505 and then Present
(Parent_Subp
)
3506 and then not Is_Direct_Deep_Call
(Subp
)
3508 if Present
(Inherited_From_Formal
(Subp
)) then
3509 Parent_Subp
:= Inherited_From_Formal
(Subp
);
3511 Parent_Subp
:= Ultimate_Alias
(Parent_Subp
);
3514 -- The below setting of Entity is suspect, see F109-018 discussion???
3516 Set_Entity
(Name
(Call_Node
), Parent_Subp
);
3518 if Is_Abstract_Subprogram
(Parent_Subp
)
3519 and then not In_Instance
3522 ("cannot call abstract subprogram &!",
3523 Name
(Call_Node
), Parent_Subp
);
3526 -- Inspect all formals of derived subprogram Subp. Compare parameter
3527 -- types with the parent subprogram and check whether an actual may
3528 -- need a type conversion to the corresponding formal of the parent
3531 -- Not clear whether intrinsic subprograms need such conversions. ???
3533 if not Is_Intrinsic_Subprogram
(Parent_Subp
)
3534 or else Is_Generic_Instance
(Parent_Subp
)
3537 procedure Convert
(Act
: Node_Id
; Typ
: Entity_Id
);
3538 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3539 -- and resolve the newly generated construct.
3545 procedure Convert
(Act
: Node_Id
; Typ
: Entity_Id
) is
3547 Rewrite
(Act
, OK_Convert_To
(Typ
, Relocate_Node
(Act
)));
3554 Actual_Typ
: Entity_Id
;
3555 Formal_Typ
: Entity_Id
;
3556 Parent_Typ
: Entity_Id
;
3559 Actual
:= First_Actual
(Call_Node
);
3560 Formal
:= First_Formal
(Subp
);
3561 Parent_Formal
:= First_Formal
(Parent_Subp
);
3562 while Present
(Formal
) loop
3563 Actual_Typ
:= Etype
(Actual
);
3564 Formal_Typ
:= Etype
(Formal
);
3565 Parent_Typ
:= Etype
(Parent_Formal
);
3567 -- For an IN parameter of a scalar type, the parent formal
3568 -- type and derived formal type differ or the parent formal
3569 -- type and actual type do not match statically.
3571 if Is_Scalar_Type
(Formal_Typ
)
3572 and then Ekind
(Formal
) = E_In_Parameter
3573 and then Formal_Typ
/= Parent_Typ
3575 not Subtypes_Statically_Match
(Parent_Typ
, Actual_Typ
)
3576 and then not Raises_Constraint_Error
(Actual
)
3578 Convert
(Actual
, Parent_Typ
);
3579 Enable_Range_Check
(Actual
);
3581 -- If the actual has been marked as requiring a range
3582 -- check, then generate it here.
3584 if Do_Range_Check
(Actual
) then
3585 Set_Do_Range_Check
(Actual
, False);
3586 Generate_Range_Check
3587 (Actual
, Etype
(Formal
), CE_Range_Check_Failed
);
3590 -- For access types, the parent formal type and actual type
3593 elsif Is_Access_Type
(Formal_Typ
)
3594 and then Base_Type
(Parent_Typ
) /= Base_Type
(Actual_Typ
)
3596 if Ekind
(Formal
) /= E_In_Parameter
then
3597 Convert
(Actual
, Parent_Typ
);
3599 elsif Ekind
(Parent_Typ
) = E_Anonymous_Access_Type
3600 and then Designated_Type
(Parent_Typ
) /=
3601 Designated_Type
(Actual_Typ
)
3602 and then not Is_Controlling_Formal
(Formal
)
3604 -- This unchecked conversion is not necessary unless
3605 -- inlining is enabled, because in that case the type
3606 -- mismatch may become visible in the body about to be
3610 Unchecked_Convert_To
(Parent_Typ
,
3611 Relocate_Node
(Actual
)));
3613 Resolve
(Actual
, Parent_Typ
);
3616 -- For array and record types, the parent formal type and
3617 -- derived formal type have different sizes or pragma Pack
3620 elsif ((Is_Array_Type
(Formal_Typ
)
3621 and then Is_Array_Type
(Parent_Typ
))
3623 (Is_Record_Type
(Formal_Typ
)
3624 and then Is_Record_Type
(Parent_Typ
)))
3626 (Esize
(Formal_Typ
) /= Esize
(Parent_Typ
)
3627 or else Has_Pragma_Pack
(Formal_Typ
) /=
3628 Has_Pragma_Pack
(Parent_Typ
))
3630 Convert
(Actual
, Parent_Typ
);
3633 Next_Actual
(Actual
);
3634 Next_Formal
(Formal
);
3635 Next_Formal
(Parent_Formal
);
3641 Subp
:= Parent_Subp
;
3644 -- Check for violation of No_Abort_Statements
3646 if Restriction_Check_Required
(No_Abort_Statements
)
3647 and then Is_RTE
(Subp
, RE_Abort_Task
)
3649 Check_Restriction
(No_Abort_Statements
, Call_Node
);
3651 -- Check for violation of No_Dynamic_Attachment
3653 elsif Restriction_Check_Required
(No_Dynamic_Attachment
)
3654 and then RTU_Loaded
(Ada_Interrupts
)
3655 and then (Is_RTE
(Subp
, RE_Is_Reserved
) or else
3656 Is_RTE
(Subp
, RE_Is_Attached
) or else
3657 Is_RTE
(Subp
, RE_Current_Handler
) or else
3658 Is_RTE
(Subp
, RE_Attach_Handler
) or else
3659 Is_RTE
(Subp
, RE_Exchange_Handler
) or else
3660 Is_RTE
(Subp
, RE_Detach_Handler
) or else
3661 Is_RTE
(Subp
, RE_Reference
))
3663 Check_Restriction
(No_Dynamic_Attachment
, Call_Node
);
3666 -- Deal with case where call is an explicit dereference
3668 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
3670 -- Handle case of access to protected subprogram type
3672 if Is_Access_Protected_Subprogram_Type
3673 (Base_Type
(Etype
(Prefix
(Name
(Call_Node
)))))
3675 -- If this is a call through an access to protected operation, the
3676 -- prefix has the form (object'address, operation'access). Rewrite
3677 -- as a for other protected calls: the object is the 1st parameter
3678 -- of the list of actuals.
3685 Ptr
: constant Node_Id
:= Prefix
(Name
(Call_Node
));
3687 T
: constant Entity_Id
:=
3688 Equivalent_Type
(Base_Type
(Etype
(Ptr
)));
3690 D_T
: constant Entity_Id
:=
3691 Designated_Type
(Base_Type
(Etype
(Ptr
)));
3695 Make_Selected_Component
(Loc
,
3696 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
3698 New_Occurrence_Of
(First_Entity
(T
), Loc
));
3701 Make_Selected_Component
(Loc
,
3702 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
3704 New_Occurrence_Of
(Next_Entity
(First_Entity
(T
)), Loc
));
3707 Make_Explicit_Dereference
(Loc
,
3710 if Present
(Parameter_Associations
(Call_Node
)) then
3711 Parm
:= Parameter_Associations
(Call_Node
);
3716 Prepend
(Obj
, Parm
);
3718 if Etype
(D_T
) = Standard_Void_Type
then
3720 Make_Procedure_Call_Statement
(Loc
,
3722 Parameter_Associations
=> Parm
);
3725 Make_Function_Call
(Loc
,
3727 Parameter_Associations
=> Parm
);
3730 Set_First_Named_Actual
(Call
, First_Named_Actual
(Call_Node
));
3731 Set_Etype
(Call
, Etype
(D_T
));
3733 -- We do not re-analyze the call to avoid infinite recursion.
3734 -- We analyze separately the prefix and the object, and set
3735 -- the checks on the prefix that would otherwise be emitted
3736 -- when resolving a call.
3738 Rewrite
(Call_Node
, Call
);
3740 Apply_Access_Check
(Nam
);
3747 -- If this is a call to an intrinsic subprogram, then perform the
3748 -- appropriate expansion to the corresponding tree node and we
3749 -- are all done (since after that the call is gone!)
3751 -- In the case where the intrinsic is to be processed by the back end,
3752 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3753 -- since the idea in this case is to pass the call unchanged. If the
3754 -- intrinsic is an inherited unchecked conversion, and the derived type
3755 -- is the target type of the conversion, we must retain it as the return
3756 -- type of the expression. Otherwise the expansion below, which uses the
3757 -- parent operation, will yield the wrong type.
3759 if Is_Intrinsic_Subprogram
(Subp
) then
3760 Expand_Intrinsic_Call
(Call_Node
, Subp
);
3762 if Nkind
(Call_Node
) = N_Unchecked_Type_Conversion
3763 and then Parent_Subp
/= Orig_Subp
3764 and then Etype
(Parent_Subp
) /= Etype
(Orig_Subp
)
3766 Set_Etype
(Call_Node
, Etype
(Orig_Subp
));
3772 if Ekind_In
(Subp
, E_Function
, E_Procedure
) then
3774 -- We perform two simple optimization on calls:
3776 -- a) replace calls to null procedures unconditionally;
3778 -- b) for To_Address, just do an unchecked conversion. Not only is
3779 -- this efficient, but it also avoids order of elaboration problems
3780 -- when address clauses are inlined (address expression elaborated
3781 -- at the wrong point).
3783 -- We perform these optimization regardless of whether we are in the
3784 -- main unit or in a unit in the context of the main unit, to ensure
3785 -- that tree generated is the same in both cases, for CodePeer use.
3787 if Is_RTE
(Subp
, RE_To_Address
) then
3789 Unchecked_Convert_To
3790 (RTE
(RE_Address
), Relocate_Node
(First_Actual
(Call_Node
))));
3793 elsif Is_Null_Procedure
(Subp
) then
3794 Rewrite
(Call_Node
, Make_Null_Statement
(Loc
));
3798 -- Handle inlining (old semantics)
3800 if Is_Inlined
(Subp
) and then not Debug_Flag_Dot_K
then
3801 Inlined_Subprogram
: declare
3803 Must_Inline
: Boolean := False;
3804 Spec
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
3807 -- Verify that the body to inline has already been seen, and
3808 -- that if the body is in the current unit the inlining does
3809 -- not occur earlier. This avoids order-of-elaboration problems
3812 -- This should be documented in sinfo/einfo ???
3815 or else Nkind
(Spec
) /= N_Subprogram_Declaration
3816 or else No
(Body_To_Inline
(Spec
))
3818 Must_Inline
:= False;
3820 -- If this an inherited function that returns a private type,
3821 -- do not inline if the full view is an unconstrained array,
3822 -- because such calls cannot be inlined.
3824 elsif Present
(Orig_Subp
)
3825 and then Is_Array_Type
(Etype
(Orig_Subp
))
3826 and then not Is_Constrained
(Etype
(Orig_Subp
))
3828 Must_Inline
:= False;
3830 elsif In_Unfrozen_Instance
(Scope
(Subp
)) then
3831 Must_Inline
:= False;
3834 Bod
:= Body_To_Inline
(Spec
);
3836 if (In_Extended_Main_Code_Unit
(Call_Node
)
3837 or else In_Extended_Main_Code_Unit
(Parent
(Call_Node
))
3838 or else Has_Pragma_Inline_Always
(Subp
))
3839 and then (not In_Same_Extended_Unit
(Sloc
(Bod
), Loc
)
3841 Earlier_In_Extended_Unit
(Sloc
(Bod
), Loc
))
3843 Must_Inline
:= True;
3845 -- If we are compiling a package body that is not the main
3846 -- unit, it must be for inlining/instantiation purposes,
3847 -- in which case we inline the call to insure that the same
3848 -- temporaries are generated when compiling the body by
3849 -- itself. Otherwise link errors can occur.
3851 -- If the function being called is itself in the main unit,
3852 -- we cannot inline, because there is a risk of double
3853 -- elaboration and/or circularity: the inlining can make
3854 -- visible a private entity in the body of the main unit,
3855 -- that gigi will see before its sees its proper definition.
3857 elsif not (In_Extended_Main_Code_Unit
(Call_Node
))
3858 and then In_Package_Body
3860 Must_Inline
:= not In_Extended_Main_Source_Unit
(Subp
);
3865 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
3868 -- Let the back end handle it
3870 Add_Inlined_Body
(Subp
);
3872 if Front_End_Inlining
3873 and then Nkind
(Spec
) = N_Subprogram_Declaration
3874 and then (In_Extended_Main_Code_Unit
(Call_Node
))
3875 and then No
(Body_To_Inline
(Spec
))
3876 and then not Has_Completion
(Subp
)
3877 and then In_Same_Extended_Unit
(Sloc
(Spec
), Loc
)
3880 ("cannot inline& (body not seen yet)?",
3884 end Inlined_Subprogram
;
3886 -- Handle inlining (new semantics)
3888 elsif Is_Inlined
(Subp
) then
3890 Spec
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
3893 if Must_Inline
(Subp
) then
3894 if In_Extended_Main_Code_Unit
(Call_Node
)
3895 and then In_Same_Extended_Unit
(Sloc
(Spec
), Loc
)
3896 and then not Has_Completion
(Subp
)
3899 ("cannot inline& (body not seen yet)?",
3903 Do_Inline_Always
(Subp
, Orig_Subp
);
3906 elsif Optimization_Level
> 0 then
3907 Do_Inline
(Subp
, Orig_Subp
);
3910 -- The call may have been inlined or may have been passed to
3911 -- the backend. No further action needed if it was inlined.
3913 if Nkind
(N
) /= N_Function_Call
then
3920 -- Check for protected subprogram. This is either an intra-object call,
3921 -- or a protected function call. Protected procedure calls are rewritten
3922 -- as entry calls and handled accordingly.
3924 -- In Ada 2005, this may be an indirect call to an access parameter that
3925 -- is an access_to_subprogram. In that case the anonymous type has a
3926 -- scope that is a protected operation, but the call is a regular one.
3927 -- In either case do not expand call if subprogram is eliminated.
3929 Scop
:= Scope
(Subp
);
3931 if Nkind
(Call_Node
) /= N_Entry_Call_Statement
3932 and then Is_Protected_Type
(Scop
)
3933 and then Ekind
(Subp
) /= E_Subprogram_Type
3934 and then not Is_Eliminated
(Subp
)
3936 -- If the call is an internal one, it is rewritten as a call to the
3937 -- corresponding unprotected subprogram.
3939 Expand_Protected_Subprogram_Call
(Call_Node
, Subp
, Scop
);
3942 -- Functions returning controlled objects need special attention. If
3943 -- the return type is limited, then the context is initialization and
3944 -- different processing applies. If the call is to a protected function,
3945 -- the expansion above will call Expand_Call recursively. Otherwise the
3946 -- function call is transformed into a temporary which obtains the
3947 -- result from the secondary stack.
3949 if Needs_Finalization
(Etype
(Subp
)) then
3950 if not Is_Limited_View
(Etype
(Subp
))
3952 (No
(First_Formal
(Subp
))
3954 not Is_Concurrent_Record_Type
(Etype
(First_Formal
(Subp
))))
3956 Expand_Ctrl_Function_Call
(Call_Node
);
3958 -- Build-in-place function calls which appear in anonymous contexts
3959 -- need a transient scope to ensure the proper finalization of the
3960 -- intermediate result after its use.
3962 elsif Is_Build_In_Place_Function_Call
(Call_Node
)
3964 Nkind_In
(Parent
(Call_Node
), N_Attribute_Reference
,
3966 N_Indexed_Component
,
3967 N_Object_Renaming_Declaration
,
3968 N_Procedure_Call_Statement
,
3969 N_Selected_Component
,
3972 Establish_Transient_Scope
(Call_Node
, Sec_Stack
=> True);
3976 -- Test for First_Optional_Parameter, and if so, truncate parameter list
3977 -- if there are optional parameters at the trailing end.
3978 -- Note: we never delete procedures for call via a pointer.
3980 if (Ekind
(Subp
) = E_Procedure
or else Ekind
(Subp
) = E_Function
)
3981 and then Present
(First_Optional_Parameter
(Subp
))
3984 Last_Keep_Arg
: Node_Id
;
3987 -- Last_Keep_Arg will hold the last actual that should be kept.
3988 -- If it remains empty at the end, it means that all parameters
3991 Last_Keep_Arg
:= Empty
;
3993 -- Find first optional parameter, must be present since we checked
3994 -- the validity of the parameter before setting it.
3996 Formal
:= First_Formal
(Subp
);
3997 Actual
:= First_Actual
(Call_Node
);
3998 while Formal
/= First_Optional_Parameter
(Subp
) loop
3999 Last_Keep_Arg
:= Actual
;
4000 Next_Formal
(Formal
);
4001 Next_Actual
(Actual
);
4004 -- We have Formal and Actual pointing to the first potentially
4005 -- droppable argument. We can drop all the trailing arguments
4006 -- whose actual matches the default. Note that we know that all
4007 -- remaining formals have defaults, because we checked that this
4008 -- requirement was met before setting First_Optional_Parameter.
4010 -- We use Fully_Conformant_Expressions to check for identity
4011 -- between formals and actuals, which may miss some cases, but
4012 -- on the other hand, this is only an optimization (if we fail
4013 -- to truncate a parameter it does not affect functionality).
4014 -- So if the default is 3 and the actual is 1+2, we consider
4015 -- them unequal, which hardly seems worrisome.
4017 while Present
(Formal
) loop
4018 if not Fully_Conformant_Expressions
4019 (Actual
, Default_Value
(Formal
))
4021 Last_Keep_Arg
:= Actual
;
4024 Next_Formal
(Formal
);
4025 Next_Actual
(Actual
);
4028 -- If no arguments, delete entire list, this is the easy case
4030 if No
(Last_Keep_Arg
) then
4031 Set_Parameter_Associations
(Call_Node
, No_List
);
4032 Set_First_Named_Actual
(Call_Node
, Empty
);
4034 -- Case where at the last retained argument is positional. This
4035 -- is also an easy case, since the retained arguments are already
4036 -- in the right form, and we don't need to worry about the order
4037 -- of arguments that get eliminated.
4039 elsif Is_List_Member
(Last_Keep_Arg
) then
4040 while Present
(Next
(Last_Keep_Arg
)) loop
4041 Discard_Node
(Remove_Next
(Last_Keep_Arg
));
4044 Set_First_Named_Actual
(Call_Node
, Empty
);
4046 -- This is the annoying case where the last retained argument
4047 -- is a named parameter. Since the original arguments are not
4048 -- in declaration order, we may have to delete some fairly
4049 -- random collection of arguments.
4057 -- First step, remove all the named parameters from the
4058 -- list (they are still chained using First_Named_Actual
4059 -- and Next_Named_Actual, so we have not lost them!)
4061 Temp
:= First
(Parameter_Associations
(Call_Node
));
4063 -- Case of all parameters named, remove them all
4065 if Nkind
(Temp
) = N_Parameter_Association
then
4066 -- Suppress warnings to avoid warning on possible
4067 -- infinite loop (because Call_Node is not modified).
4069 pragma Warnings
(Off
);
4070 while Is_Non_Empty_List
4071 (Parameter_Associations
(Call_Node
))
4074 Remove_Head
(Parameter_Associations
(Call_Node
));
4076 pragma Warnings
(On
);
4078 -- Case of mixed positional/named, remove named parameters
4081 while Nkind
(Next
(Temp
)) /= N_Parameter_Association
loop
4085 while Present
(Next
(Temp
)) loop
4086 Remove
(Next
(Temp
));
4090 -- Now we loop through the named parameters, till we get
4091 -- to the last one to be retained, adding them to the list.
4092 -- Note that the Next_Named_Actual list does not need to be
4093 -- touched since we are only reordering them on the actual
4094 -- parameter association list.
4096 Passoc
:= Parent
(First_Named_Actual
(Call_Node
));
4098 Temp
:= Relocate_Node
(Passoc
);
4100 (Parameter_Associations
(Call_Node
), Temp
);
4102 Last_Keep_Arg
= Explicit_Actual_Parameter
(Passoc
);
4103 Passoc
:= Parent
(Next_Named_Actual
(Passoc
));
4106 Set_Next_Named_Actual
(Temp
, Empty
);
4109 Temp
:= Next_Named_Actual
(Passoc
);
4110 exit when No
(Temp
);
4111 Set_Next_Named_Actual
4112 (Passoc
, Next_Named_Actual
(Parent
(Temp
)));
4121 ---------------------------
4122 -- Expand_Contract_Cases --
4123 ---------------------------
4125 -- Pragma Contract_Cases is expanded in the following manner:
4128 -- Flag_1 : Boolean := False;
4130 -- Flag_N : Boolean := False;
4131 -- Flag_N+1 : Boolean := False; -- when "others" present
4132 -- Count : Natural := 0;
4134 -- <preconditions (if any)>
4136 -- if Case_Guard_1 then
4138 -- Count := Count + 1;
4141 -- if Case_Guard_N then
4143 -- Count := Count + 1;
4146 -- if Count = 0 then
4147 -- raise Assertion_Error with "xxx contract cases incomplete";
4149 -- Flag_N+1 := True; -- when "others" present
4151 -- elsif Count > 1 then
4153 -- Str0 : constant String :=
4154 -- "contract cases overlap for subprogram ABC";
4155 -- Str1 : constant String :=
4157 -- Str0 & "case guard at xxx evaluates to True"
4159 -- StrN : constant String :=
4161 -- StrN-1 & "case guard at xxx evaluates to True"
4164 -- raise Assertion_Error with StrN;
4168 -- procedure _Postconditions is
4170 -- <postconditions (if any)>
4172 -- if Flag_1 and then not Consequence_1 then
4173 -- raise Assertion_Error with "failed contract case at xxx";
4176 -- if Flag_N[+1] and then not Consequence_N[+1] then
4177 -- raise Assertion_Error with "failed contract case at xxx";
4179 -- end _Postconditions;
4184 procedure Expand_Contract_Cases
4186 Subp_Id
: Entity_Id
;
4188 Stmts
: in out List_Id
)
4190 Loc
: constant Source_Ptr
:= Sloc
(CCs
);
4192 procedure Case_Guard_Error
4195 Error_Loc
: Source_Ptr
;
4196 Msg
: in out Entity_Id
);
4197 -- Given a declarative list Decls, status flag Flag, the location of the
4198 -- error and a string Msg, construct the following check:
4199 -- Msg : constant String :=
4201 -- Msg & "case guard at Error_Loc evaluates to True"
4203 -- The resulting code is added to Decls
4205 procedure Consequence_Error
4206 (Checks
: in out Node_Id
;
4209 -- Given an if statement Checks, status flag Flag and a consequence
4210 -- Conseq, construct the following check:
4211 -- [els]if Flag and then not Conseq then
4212 -- raise Assertion_Error
4213 -- with "failed contract case at Sloc (Conseq)";
4215 -- The resulting code is added to Checks
4217 function Declaration_Of
(Id
: Entity_Id
) return Node_Id
;
4218 -- Given the entity Id of a boolean flag, generate:
4219 -- Id : Boolean := False;
4221 function Increment
(Id
: Entity_Id
) return Node_Id
;
4222 -- Given the entity Id of a numerical variable, generate:
4225 function Set
(Id
: Entity_Id
) return Node_Id
;
4226 -- Given the entity Id of a boolean variable, generate:
4229 ----------------------
4230 -- Case_Guard_Error --
4231 ----------------------
4233 procedure Case_Guard_Error
4236 Error_Loc
: Source_Ptr
;
4237 Msg
: in out Entity_Id
)
4239 New_Line
: constant Character := Character'Val (10);
4240 New_Msg
: constant Entity_Id
:= Make_Temporary
(Loc
, 'S');
4244 Store_String_Char
(New_Line
);
4245 Store_String_Chars
(" case guard at ");
4246 Store_String_Chars
(Build_Location_String
(Error_Loc
));
4247 Store_String_Chars
(" evaluates to True");
4250 -- New_Msg : constant String :=
4252 -- Msg & "case guard at Error_Loc evaluates to True"
4256 Make_Object_Declaration
(Loc
,
4257 Defining_Identifier
=> New_Msg
,
4258 Constant_Present
=> True,
4259 Object_Definition
=> New_Reference_To
(Standard_String
, Loc
),
4261 Make_If_Expression
(Loc
,
4262 Expressions
=> New_List
(
4263 New_Reference_To
(Flag
, Loc
),
4265 Make_Op_Concat
(Loc
,
4266 Left_Opnd
=> New_Reference_To
(Msg
, Loc
),
4267 Right_Opnd
=> Make_String_Literal
(Loc
, End_String
)),
4269 New_Reference_To
(Msg
, Loc
)))));
4272 end Case_Guard_Error
;
4274 -----------------------
4275 -- Consequence_Error --
4276 -----------------------
4278 procedure Consequence_Error
4279 (Checks
: in out Node_Id
;
4288 -- Flag and then not Conseq
4292 Left_Opnd
=> New_Reference_To
(Flag
, Loc
),
4295 Right_Opnd
=> Relocate_Node
(Conseq
)));
4298 -- raise Assertion_Error
4299 -- with "failed contract case at Sloc (Conseq)";
4302 Store_String_Chars
("failed contract case at ");
4303 Store_String_Chars
(Build_Location_String
(Sloc
(Conseq
)));
4306 Make_Procedure_Call_Statement
(Loc
,
4308 New_Reference_To
(RTE
(RE_Raise_Assert_Failure
), Loc
),
4309 Parameter_Associations
=> New_List
(
4310 Make_String_Literal
(Loc
, End_String
)));
4314 Make_Implicit_If_Statement
(CCs
,
4316 Then_Statements
=> New_List
(Error
));
4319 if No
(Elsif_Parts
(Checks
)) then
4320 Set_Elsif_Parts
(Checks
, New_List
);
4323 Append_To
(Elsif_Parts
(Checks
),
4324 Make_Elsif_Part
(Loc
,
4326 Then_Statements
=> New_List
(Error
)));
4328 end Consequence_Error
;
4330 --------------------
4331 -- Declaration_Of --
4332 --------------------
4334 function Declaration_Of
(Id
: Entity_Id
) return Node_Id
is
4337 Make_Object_Declaration
(Loc
,
4338 Defining_Identifier
=> Id
,
4339 Object_Definition
=> New_Reference_To
(Standard_Boolean
, Loc
),
4340 Expression
=> New_Reference_To
(Standard_False
, Loc
));
4347 function Increment
(Id
: Entity_Id
) return Node_Id
is
4350 Make_Assignment_Statement
(Loc
,
4351 Name
=> New_Reference_To
(Id
, Loc
),
4354 Left_Opnd
=> New_Reference_To
(Id
, Loc
),
4355 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)));
4362 function Set
(Id
: Entity_Id
) return Node_Id
is
4365 Make_Assignment_Statement
(Loc
,
4366 Name
=> New_Reference_To
(Id
, Loc
),
4367 Expression
=> New_Reference_To
(Standard_True
, Loc
));
4372 Aggr
: constant Node_Id
:=
4374 (Pragma_Argument_Associations
(CCs
)));
4375 Case_Guard
: Node_Id
;
4376 CG_Checks
: Node_Id
;
4379 Conseq_Checks
: Node_Id
:= Empty
;
4381 Error_Decls
: List_Id
;
4383 Msg_Str
: Entity_Id
;
4384 Multiple_PCs
: Boolean;
4385 Others_Flag
: Entity_Id
:= Empty
;
4386 Post_Case
: Node_Id
;
4388 -- Start of processing for Expand_Contract_Cases
4391 -- Do nothing if pragma is not enabled. If pragma is disabled, it has
4392 -- already been rewritten as a Null statement.
4394 if Is_Ignored
(CCs
) then
4397 -- Guard against malformed contract cases
4399 elsif Nkind
(Aggr
) /= N_Aggregate
then
4403 Multiple_PCs
:= List_Length
(Component_Associations
(Aggr
)) > 1;
4405 -- Create the counter which tracks the number of case guards that
4406 -- evaluate to True.
4408 -- Count : Natural := 0;
4410 Count
:= Make_Temporary
(Loc
, 'C');
4413 Make_Object_Declaration
(Loc
,
4414 Defining_Identifier
=> Count
,
4415 Object_Definition
=> New_Reference_To
(Standard_Natural
, Loc
),
4416 Expression
=> Make_Integer_Literal
(Loc
, 0)));
4418 -- Create the base error message for multiple overlapping case guards
4420 -- Msg_Str : constant String :=
4421 -- "contract cases overlap for subprogram Subp_Id";
4423 if Multiple_PCs
then
4424 Msg_Str
:= Make_Temporary
(Loc
, 'S');
4427 Store_String_Chars
("contract cases overlap for subprogram ");
4428 Store_String_Chars
(Get_Name_String
(Chars
(Subp_Id
)));
4430 Error_Decls
:= New_List
(
4431 Make_Object_Declaration
(Loc
,
4432 Defining_Identifier
=> Msg_Str
,
4433 Constant_Present
=> True,
4434 Object_Definition
=> New_Reference_To
(Standard_String
, Loc
),
4435 Expression
=> Make_String_Literal
(Loc
, End_String
)));
4438 -- Process individual post cases
4440 Post_Case
:= First
(Component_Associations
(Aggr
));
4441 while Present
(Post_Case
) loop
4442 Case_Guard
:= First
(Choices
(Post_Case
));
4443 Conseq
:= Expression
(Post_Case
);
4445 -- The "others" choice requires special processing
4447 if Nkind
(Case_Guard
) = N_Others_Choice
then
4448 Others_Flag
:= Make_Temporary
(Loc
, 'F');
4449 Prepend_To
(Decls
, Declaration_Of
(Others_Flag
));
4451 -- Check possible overlap between a case guard and "others"
4453 if Multiple_PCs
and Exception_Extra_Info
then
4455 (Decls
=> Error_Decls
,
4456 Flag
=> Others_Flag
,
4457 Error_Loc
=> Sloc
(Case_Guard
),
4461 -- Check the corresponding consequence of "others"
4464 (Checks
=> Conseq_Checks
,
4465 Flag
=> Others_Flag
,
4468 -- Regular post case
4471 -- Create the flag which tracks the state of its associated case
4474 Flag
:= Make_Temporary
(Loc
, 'F');
4475 Prepend_To
(Decls
, Declaration_Of
(Flag
));
4477 -- The flag is set when the case guard is evaluated to True
4478 -- if Case_Guard then
4480 -- Count := Count + 1;
4484 Make_Implicit_If_Statement
(CCs
,
4485 Condition
=> Relocate_Node
(Case_Guard
),
4486 Then_Statements
=> New_List
(
4488 Increment
(Count
))));
4490 -- Check whether this case guard overlaps with another one
4492 if Multiple_PCs
and Exception_Extra_Info
then
4494 (Decls
=> Error_Decls
,
4496 Error_Loc
=> Sloc
(Case_Guard
),
4500 -- The corresponding consequence of the case guard which evaluated
4501 -- to True must hold on exit from the subprogram.
4504 (Checks
=> Conseq_Checks
,
4512 -- Raise Assertion_Error when none of the case guards evaluate to True.
4513 -- The only exception is when we have "others", in which case there is
4514 -- no error because "others" acts as a default True.
4519 if Present
(Others_Flag
) then
4520 CG_Stmts
:= New_List
(Set
(Others_Flag
));
4523 -- raise Assertion_Error with "xxx contract cases incomplete";
4527 Store_String_Chars
(Build_Location_String
(Loc
));
4528 Store_String_Chars
(" contract cases incomplete");
4530 CG_Stmts
:= New_List
(
4531 Make_Procedure_Call_Statement
(Loc
,
4533 New_Reference_To
(RTE
(RE_Raise_Assert_Failure
), Loc
),
4534 Parameter_Associations
=> New_List
(
4535 Make_String_Literal
(Loc
, End_String
))));
4539 Make_Implicit_If_Statement
(CCs
,
4542 Left_Opnd
=> New_Reference_To
(Count
, Loc
),
4543 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
4544 Then_Statements
=> CG_Stmts
);
4546 -- Detect a possible failure due to several case guards evaluating to
4550 -- elsif Count > 0 then
4554 -- raise Assertion_Error with <Msg_Str>;
4557 if Multiple_PCs
then
4558 Set_Elsif_Parts
(CG_Checks
, New_List
(
4559 Make_Elsif_Part
(Loc
,
4562 Left_Opnd
=> New_Reference_To
(Count
, Loc
),
4563 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)),
4565 Then_Statements
=> New_List
(
4566 Make_Block_Statement
(Loc
,
4567 Declarations
=> Error_Decls
,
4568 Handled_Statement_Sequence
=>
4569 Make_Handled_Sequence_Of_Statements
(Loc
,
4570 Statements
=> New_List
(
4571 Make_Procedure_Call_Statement
(Loc
,
4574 (RTE
(RE_Raise_Assert_Failure
), Loc
),
4575 Parameter_Associations
=> New_List
(
4576 New_Reference_To
(Msg_Str
, Loc
))))))))));
4579 Append_To
(Decls
, CG_Checks
);
4581 -- Raise Assertion_Error when the corresponding consequence of a case
4582 -- guard that evaluated to True fails.
4588 Append_To
(Stmts
, Conseq_Checks
);
4589 end Expand_Contract_Cases
;
4591 -------------------------------
4592 -- Expand_Ctrl_Function_Call --
4593 -------------------------------
4595 procedure Expand_Ctrl_Function_Call
(N
: Node_Id
) is
4597 -- Optimization, if the returned value (which is on the sec-stack) is
4598 -- returned again, no need to copy/readjust/finalize, we can just pass
4599 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
4600 -- attachment is needed
4602 if Nkind
(Parent
(N
)) = N_Simple_Return_Statement
then
4606 -- Resolution is now finished, make sure we don't start analysis again
4607 -- because of the duplication.
4611 -- A function which returns a controlled object uses the secondary
4612 -- stack. Rewrite the call into a temporary which obtains the result of
4613 -- the function using 'reference.
4615 Remove_Side_Effects
(N
);
4617 -- When the temporary function result appears inside a case or an if
4618 -- expression, its lifetime must be extended to match that of the
4619 -- context. If not, the function result would be finalized prematurely
4620 -- and the evaluation of the expression could yield the wrong result.
4622 if Within_Case_Or_If_Expression
(N
)
4623 and then Nkind
(N
) = N_Explicit_Dereference
4625 Set_Is_Processed_Transient
(Entity
(Prefix
(N
)));
4627 end Expand_Ctrl_Function_Call
;
4629 -------------------------
4630 -- Expand_Inlined_Call --
4631 -------------------------
4633 procedure Expand_Inlined_Call
4636 Orig_Subp
: Entity_Id
)
4638 Loc
: constant Source_Ptr
:= Sloc
(N
);
4639 Is_Predef
: constant Boolean :=
4640 Is_Predefined_File_Name
4641 (Unit_File_Name
(Get_Source_Unit
(Subp
)));
4642 Orig_Bod
: constant Node_Id
:=
4643 Body_To_Inline
(Unit_Declaration_Node
(Subp
));
4647 Decls
: constant List_Id
:= New_List
;
4648 Exit_Lab
: Entity_Id
:= Empty
;
4655 Ret_Type
: Entity_Id
;
4658 -- The target of the call. If context is an assignment statement then
4659 -- this is the left-hand side of the assignment, else it is a temporary
4660 -- to which the return value is assigned prior to rewriting the call.
4663 -- A separate target used when the return type is unconstrained
4666 Temp_Typ
: Entity_Id
;
4668 Return_Object
: Entity_Id
:= Empty
;
4669 -- Entity in declaration in an extended_return_statement
4672 Is_Unc_Decl
: Boolean;
4673 -- If the type returned by the function is unconstrained and the call
4674 -- can be inlined, special processing is required.
4676 procedure Make_Exit_Label
;
4677 -- Build declaration for exit label to be used in Return statements,
4678 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
4679 -- declaration). Does nothing if Exit_Lab already set.
4681 function Process_Formals
(N
: Node_Id
) return Traverse_Result
;
4682 -- Replace occurrence of a formal with the corresponding actual, or the
4683 -- thunk generated for it. Replace a return statement with an assignment
4684 -- to the target of the call, with appropriate conversions if needed.
4686 function Process_Sloc
(Nod
: Node_Id
) return Traverse_Result
;
4687 -- If the call being expanded is that of an internal subprogram, set the
4688 -- sloc of the generated block to that of the call itself, so that the
4689 -- expansion is skipped by the "next" command in gdb.
4690 -- Same processing for a subprogram in a predefined file, e.g.
4691 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change to
4692 -- simplify our own development.
4694 procedure Reset_Dispatching_Calls
(N
: Node_Id
);
4695 -- In subtree N search for occurrences of dispatching calls that use the
4696 -- Ada 2005 Object.Operation notation and the object is a formal of the
4697 -- inlined subprogram. Reset the entity associated with Operation in all
4698 -- the found occurrences.
4700 procedure Rewrite_Function_Call
(N
: Node_Id
; Blk
: Node_Id
);
4701 -- If the function body is a single expression, replace call with
4702 -- expression, else insert block appropriately.
4704 procedure Rewrite_Procedure_Call
(N
: Node_Id
; Blk
: Node_Id
);
4705 -- If procedure body has no local variables, inline body without
4706 -- creating block, otherwise rewrite call with block.
4708 function Formal_Is_Used_Once
(Formal
: Entity_Id
) return Boolean;
4709 -- Determine whether a formal parameter is used only once in Orig_Bod
4711 ---------------------
4712 -- Make_Exit_Label --
4713 ---------------------
4715 procedure Make_Exit_Label
is
4716 Lab_Ent
: Entity_Id
;
4718 if No
(Exit_Lab
) then
4719 Lab_Ent
:= Make_Temporary
(Loc
, 'L');
4720 Lab_Id
:= New_Reference_To
(Lab_Ent
, Loc
);
4721 Exit_Lab
:= Make_Label
(Loc
, Lab_Id
);
4723 Make_Implicit_Label_Declaration
(Loc
,
4724 Defining_Identifier
=> Lab_Ent
,
4725 Label_Construct
=> Exit_Lab
);
4727 end Make_Exit_Label
;
4729 ---------------------
4730 -- Process_Formals --
4731 ---------------------
4733 function Process_Formals
(N
: Node_Id
) return Traverse_Result
is
4739 if Is_Entity_Name
(N
) and then Present
(Entity
(N
)) then
4742 if Is_Formal
(E
) and then Scope
(E
) = Subp
then
4743 A
:= Renamed_Object
(E
);
4745 -- Rewrite the occurrence of the formal into an occurrence of
4746 -- the actual. Also establish visibility on the proper view of
4747 -- the actual's subtype for the body's context (if the actual's
4748 -- subtype is private at the call point but its full view is
4749 -- visible to the body, then the inlined tree here must be
4750 -- analyzed with the full view).
4752 if Is_Entity_Name
(A
) then
4753 Rewrite
(N
, New_Occurrence_Of
(Entity
(A
), Loc
));
4754 Check_Private_View
(N
);
4756 elsif Nkind
(A
) = N_Defining_Identifier
then
4757 Rewrite
(N
, New_Occurrence_Of
(A
, Loc
));
4758 Check_Private_View
(N
);
4763 Rewrite
(N
, New_Copy
(A
));
4769 elsif Is_Entity_Name
(N
)
4770 and then Present
(Return_Object
)
4771 and then Chars
(N
) = Chars
(Return_Object
)
4773 -- Occurrence within an extended return statement. The return
4774 -- object is local to the body been inlined, and thus the generic
4775 -- copy is not analyzed yet, so we match by name, and replace it
4776 -- with target of call.
4778 if Nkind
(Targ
) = N_Defining_Identifier
then
4779 Rewrite
(N
, New_Occurrence_Of
(Targ
, Loc
));
4781 Rewrite
(N
, New_Copy_Tree
(Targ
));
4786 elsif Nkind
(N
) = N_Simple_Return_Statement
then
4787 if No
(Expression
(N
)) then
4790 Make_Goto_Statement
(Loc
, Name
=> New_Copy
(Lab_Id
)));
4793 if Nkind
(Parent
(N
)) = N_Handled_Sequence_Of_Statements
4794 and then Nkind
(Parent
(Parent
(N
))) = N_Subprogram_Body
4796 -- Function body is a single expression. No need for
4802 Num_Ret
:= Num_Ret
+ 1;
4806 -- Because of the presence of private types, the views of the
4807 -- expression and the context may be different, so place an
4808 -- unchecked conversion to the context type to avoid spurious
4809 -- errors, e.g. when the expression is a numeric literal and
4810 -- the context is private. If the expression is an aggregate,
4811 -- use a qualified expression, because an aggregate is not a
4812 -- legal argument of a conversion. Ditto for numeric literals,
4813 -- which must be resolved to a specific type.
4815 if Nkind_In
(Expression
(N
), N_Aggregate
,
4821 Make_Qualified_Expression
(Sloc
(N
),
4822 Subtype_Mark
=> New_Occurrence_Of
(Ret_Type
, Sloc
(N
)),
4823 Expression
=> Relocate_Node
(Expression
(N
)));
4826 Unchecked_Convert_To
4827 (Ret_Type
, Relocate_Node
(Expression
(N
)));
4830 if Nkind
(Targ
) = N_Defining_Identifier
then
4832 Make_Assignment_Statement
(Loc
,
4833 Name
=> New_Occurrence_Of
(Targ
, Loc
),
4834 Expression
=> Ret
));
4837 Make_Assignment_Statement
(Loc
,
4838 Name
=> New_Copy
(Targ
),
4839 Expression
=> Ret
));
4842 Set_Assignment_OK
(Name
(N
));
4844 if Present
(Exit_Lab
) then
4846 Make_Goto_Statement
(Loc
, Name
=> New_Copy
(Lab_Id
)));
4852 -- An extended return becomes a block whose first statement is the
4853 -- assignment of the initial expression of the return object to the
4854 -- target of the call itself.
4856 elsif Nkind
(N
) = N_Extended_Return_Statement
then
4858 Return_Decl
: constant Entity_Id
:=
4859 First
(Return_Object_Declarations
(N
));
4863 Return_Object
:= Defining_Identifier
(Return_Decl
);
4865 if Present
(Expression
(Return_Decl
)) then
4866 if Nkind
(Targ
) = N_Defining_Identifier
then
4868 Make_Assignment_Statement
(Loc
,
4869 Name
=> New_Occurrence_Of
(Targ
, Loc
),
4870 Expression
=> Expression
(Return_Decl
));
4873 Make_Assignment_Statement
(Loc
,
4874 Name
=> New_Copy
(Targ
),
4875 Expression
=> Expression
(Return_Decl
));
4878 Set_Assignment_OK
(Name
(Assign
));
4880 if No
(Handled_Statement_Sequence
(N
)) then
4881 Set_Handled_Statement_Sequence
(N
,
4882 Make_Handled_Sequence_Of_Statements
(Loc
,
4883 Statements
=> New_List
));
4887 Statements
(Handled_Statement_Sequence
(N
)));
4891 Make_Block_Statement
(Loc
,
4892 Handled_Statement_Sequence
=>
4893 Handled_Statement_Sequence
(N
)));
4898 -- Remove pragma Unreferenced since it may refer to formals that
4899 -- are not visible in the inlined body, and in any case we will
4900 -- not be posting warnings on the inlined body so it is unneeded.
4902 elsif Nkind
(N
) = N_Pragma
4903 and then Pragma_Name
(N
) = Name_Unreferenced
4905 Rewrite
(N
, Make_Null_Statement
(Sloc
(N
)));
4911 end Process_Formals
;
4913 procedure Replace_Formals
is new Traverse_Proc
(Process_Formals
);
4919 function Process_Sloc
(Nod
: Node_Id
) return Traverse_Result
is
4921 if not Debug_Generated_Code
then
4922 Set_Sloc
(Nod
, Sloc
(N
));
4923 Set_Comes_From_Source
(Nod
, False);
4929 procedure Reset_Slocs
is new Traverse_Proc
(Process_Sloc
);
4931 ------------------------------
4932 -- Reset_Dispatching_Calls --
4933 ------------------------------
4935 procedure Reset_Dispatching_Calls
(N
: Node_Id
) is
4937 function Do_Reset
(N
: Node_Id
) return Traverse_Result
;
4938 -- Comment required ???
4944 function Do_Reset
(N
: Node_Id
) return Traverse_Result
is
4946 if Nkind
(N
) = N_Procedure_Call_Statement
4947 and then Nkind
(Name
(N
)) = N_Selected_Component
4948 and then Nkind
(Prefix
(Name
(N
))) = N_Identifier
4949 and then Is_Formal
(Entity
(Prefix
(Name
(N
))))
4950 and then Is_Dispatching_Operation
4951 (Entity
(Selector_Name
(Name
(N
))))
4953 Set_Entity
(Selector_Name
(Name
(N
)), Empty
);
4959 function Do_Reset_Calls
is new Traverse_Func
(Do_Reset
);
4963 Dummy
: constant Traverse_Result
:= Do_Reset_Calls
(N
);
4964 pragma Unreferenced
(Dummy
);
4966 -- Start of processing for Reset_Dispatching_Calls
4970 end Reset_Dispatching_Calls
;
4972 ---------------------------
4973 -- Rewrite_Function_Call --
4974 ---------------------------
4976 procedure Rewrite_Function_Call
(N
: Node_Id
; Blk
: Node_Id
) is
4977 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(Blk
);
4978 Fst
: constant Node_Id
:= First
(Statements
(HSS
));
4981 -- Optimize simple case: function body is a single return statement,
4982 -- which has been expanded into an assignment.
4984 if Is_Empty_List
(Declarations
(Blk
))
4985 and then Nkind
(Fst
) = N_Assignment_Statement
4986 and then No
(Next
(Fst
))
4988 -- The function call may have been rewritten as the temporary
4989 -- that holds the result of the call, in which case remove the
4990 -- now useless declaration.
4992 if Nkind
(N
) = N_Identifier
4993 and then Nkind
(Parent
(Entity
(N
))) = N_Object_Declaration
4995 Rewrite
(Parent
(Entity
(N
)), Make_Null_Statement
(Loc
));
4998 Rewrite
(N
, Expression
(Fst
));
5000 elsif Nkind
(N
) = N_Identifier
5001 and then Nkind
(Parent
(Entity
(N
))) = N_Object_Declaration
5003 -- The block assigns the result of the call to the temporary
5005 Insert_After
(Parent
(Entity
(N
)), Blk
);
5007 -- If the context is an assignment, and the left-hand side is free of
5008 -- side-effects, the replacement is also safe.
5009 -- Can this be generalized further???
5011 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
5013 (Is_Entity_Name
(Name
(Parent
(N
)))
5015 (Nkind
(Name
(Parent
(N
))) = N_Explicit_Dereference
5016 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
)))))
5019 (Nkind
(Name
(Parent
(N
))) = N_Selected_Component
5020 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))))
5022 -- Replace assignment with the block
5025 Original_Assignment
: constant Node_Id
:= Parent
(N
);
5028 -- Preserve the original assignment node to keep the complete
5029 -- assignment subtree consistent enough for Analyze_Assignment
5030 -- to proceed (specifically, the original Lhs node must still
5031 -- have an assignment statement as its parent).
5033 -- We cannot rely on Original_Node to go back from the block
5034 -- node to the assignment node, because the assignment might
5035 -- already be a rewrite substitution.
5037 Discard_Node
(Relocate_Node
(Original_Assignment
));
5038 Rewrite
(Original_Assignment
, Blk
);
5041 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
then
5043 -- A call to a function which returns an unconstrained type
5044 -- found in the expression initializing an object-declaration is
5045 -- expanded into a procedure call which must be added after the
5046 -- object declaration.
5048 if Is_Unc_Decl
and then Debug_Flag_Dot_K
then
5049 Insert_Action_After
(Parent
(N
), Blk
);
5051 Set_Expression
(Parent
(N
), Empty
);
5052 Insert_After
(Parent
(N
), Blk
);
5055 elsif Is_Unc
and then not Debug_Flag_Dot_K
then
5056 Insert_Before
(Parent
(N
), Blk
);
5058 end Rewrite_Function_Call
;
5060 ----------------------------
5061 -- Rewrite_Procedure_Call --
5062 ----------------------------
5064 procedure Rewrite_Procedure_Call
(N
: Node_Id
; Blk
: Node_Id
) is
5065 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(Blk
);
5068 -- If there is a transient scope for N, this will be the scope of the
5069 -- actions for N, and the statements in Blk need to be within this
5070 -- scope. For example, they need to have visibility on the constant
5071 -- declarations created for the formals.
5073 -- If N needs no transient scope, and if there are no declarations in
5074 -- the inlined body, we can do a little optimization and insert the
5075 -- statements for the body directly after N, and rewrite N to a
5076 -- null statement, instead of rewriting N into a full-blown block
5079 if not Scope_Is_Transient
5080 and then Is_Empty_List
(Declarations
(Blk
))
5082 Insert_List_After
(N
, Statements
(HSS
));
5083 Rewrite
(N
, Make_Null_Statement
(Loc
));
5087 end Rewrite_Procedure_Call
;
5089 -------------------------
5090 -- Formal_Is_Used_Once --
5091 -------------------------
5093 function Formal_Is_Used_Once
(Formal
: Entity_Id
) return Boolean is
5094 Use_Counter
: Int
:= 0;
5096 function Count_Uses
(N
: Node_Id
) return Traverse_Result
;
5097 -- Traverse the tree and count the uses of the formal parameter.
5098 -- In this case, for optimization purposes, we do not need to
5099 -- continue the traversal once more than one use is encountered.
5105 function Count_Uses
(N
: Node_Id
) return Traverse_Result
is
5107 -- The original node is an identifier
5109 if Nkind
(N
) = N_Identifier
5110 and then Present
(Entity
(N
))
5112 -- Original node's entity points to the one in the copied body
5114 and then Nkind
(Entity
(N
)) = N_Identifier
5115 and then Present
(Entity
(Entity
(N
)))
5117 -- The entity of the copied node is the formal parameter
5119 and then Entity
(Entity
(N
)) = Formal
5121 Use_Counter
:= Use_Counter
+ 1;
5123 if Use_Counter
> 1 then
5125 -- Denote more than one use and abandon the traversal
5136 procedure Count_Formal_Uses
is new Traverse_Proc
(Count_Uses
);
5138 -- Start of processing for Formal_Is_Used_Once
5141 Count_Formal_Uses
(Orig_Bod
);
5142 return Use_Counter
= 1;
5143 end Formal_Is_Used_Once
;
5145 -- Start of processing for Expand_Inlined_Call
5148 -- Initializations for old/new semantics
5150 if not Debug_Flag_Dot_K
then
5151 Is_Unc
:= Is_Array_Type
(Etype
(Subp
))
5152 and then not Is_Constrained
(Etype
(Subp
));
5153 Is_Unc_Decl
:= False;
5155 Is_Unc
:= Returns_Unconstrained_Type
(Subp
)
5156 and then Optimization_Level
> 0;
5157 Is_Unc_Decl
:= Nkind
(Parent
(N
)) = N_Object_Declaration
5161 -- Check for an illegal attempt to inline a recursive procedure. If the
5162 -- subprogram has parameters this is detected when trying to supply a
5163 -- binding for parameters that already have one. For parameterless
5164 -- subprograms this must be done explicitly.
5166 if In_Open_Scopes
(Subp
) then
5167 Error_Msg_N
("call to recursive subprogram cannot be inlined??", N
);
5168 Set_Is_Inlined
(Subp
, False);
5171 -- Skip inlining if this is not a true inlining since the attribute
5172 -- Body_To_Inline is also set for renamings (see sinfo.ads)
5174 elsif Nkind
(Orig_Bod
) in N_Entity
then
5177 -- Skip inlining if the function returns an unconstrained type using
5178 -- an extended return statement since this part of the new inlining
5179 -- model which is not yet supported by the current implementation. ???
5183 Nkind
(First
(Statements
(Handled_Statement_Sequence
(Orig_Bod
))))
5184 = N_Extended_Return_Statement
5185 and then not Debug_Flag_Dot_K
5190 if Nkind
(Orig_Bod
) = N_Defining_Identifier
5191 or else Nkind
(Orig_Bod
) = N_Defining_Operator_Symbol
5193 -- Subprogram is renaming_as_body. Calls occurring after the renaming
5194 -- can be replaced with calls to the renamed entity directly, because
5195 -- the subprograms are subtype conformant. If the renamed subprogram
5196 -- is an inherited operation, we must redo the expansion because
5197 -- implicit conversions may be needed. Similarly, if the renamed
5198 -- entity is inlined, expand the call for further optimizations.
5200 Set_Name
(N
, New_Occurrence_Of
(Orig_Bod
, Loc
));
5202 if Present
(Alias
(Orig_Bod
)) or else Is_Inlined
(Orig_Bod
) then
5209 -- Register the call in the list of inlined calls
5211 if Inlined_Calls
= No_Elist
then
5212 Inlined_Calls
:= New_Elmt_List
;
5215 Append_Elmt
(N
, To
=> Inlined_Calls
);
5217 -- Use generic machinery to copy body of inlined subprogram, as if it
5218 -- were an instantiation, resetting source locations appropriately, so
5219 -- that nested inlined calls appear in the main unit.
5221 Save_Env
(Subp
, Empty
);
5222 Set_Copied_Sloc_For_Inlined_Body
(N
, Defining_Entity
(Orig_Bod
));
5226 if not Debug_Flag_Dot_K
then
5231 Bod
:= Copy_Generic_Node
(Orig_Bod
, Empty
, Instantiating
=> True);
5233 Make_Block_Statement
(Loc
,
5234 Declarations
=> Declarations
(Bod
),
5235 Handled_Statement_Sequence
=>
5236 Handled_Statement_Sequence
(Bod
));
5238 if No
(Declarations
(Bod
)) then
5239 Set_Declarations
(Blk
, New_List
);
5242 -- For the unconstrained case, capture the name of the local
5243 -- variable that holds the result. This must be the first
5244 -- declaration in the block, because its bounds cannot depend
5245 -- on local variables. Otherwise there is no way to declare the
5246 -- result outside of the block. Needless to say, in general the
5247 -- bounds will depend on the actuals in the call.
5249 -- If the context is an assignment statement, as is the case
5250 -- for the expansion of an extended return, the left-hand side
5251 -- provides bounds even if the return type is unconstrained.
5255 First_Decl
: Node_Id
;
5258 First_Decl
:= First
(Declarations
(Blk
));
5260 if Nkind
(First_Decl
) /= N_Object_Declaration
then
5264 if Nkind
(Parent
(N
)) /= N_Assignment_Statement
then
5265 Targ1
:= Defining_Identifier
(First_Decl
);
5267 Targ1
:= Name
(Parent
(N
));
5284 Copy_Generic_Node
(Orig_Bod
, Empty
, Instantiating
=> True);
5286 Make_Block_Statement
(Loc
,
5287 Declarations
=> Declarations
(Bod
),
5288 Handled_Statement_Sequence
=>
5289 Handled_Statement_Sequence
(Bod
));
5291 -- Inline a call to a function that returns an unconstrained type.
5292 -- The semantic analyzer checked that frontend-inlined functions
5293 -- returning unconstrained types have no declarations and have
5294 -- a single extended return statement. As part of its processing
5295 -- the function was split in two subprograms: a procedure P and
5296 -- a function F that has a block with a call to procedure P (see
5297 -- Split_Unconstrained_Function).
5303 (Statements
(Handled_Statement_Sequence
(Orig_Bod
))))
5304 = N_Block_Statement
);
5307 Blk_Stmt
: constant Node_Id
:=
5310 (Handled_Statement_Sequence
(Orig_Bod
)));
5311 First_Stmt
: constant Node_Id
:=
5314 (Handled_Statement_Sequence
(Blk_Stmt
)));
5315 Second_Stmt
: constant Node_Id
:= Next
(First_Stmt
);
5319 (Nkind
(First_Stmt
) = N_Procedure_Call_Statement
5320 and then Nkind
(Second_Stmt
) = N_Simple_Return_Statement
5321 and then No
(Next
(Second_Stmt
)));
5326 (Statements
(Handled_Statement_Sequence
(Orig_Bod
))),
5327 Empty
, Instantiating
=> True);
5330 -- Capture the name of the local variable that holds the
5331 -- result. This must be the first declaration in the block,
5332 -- because its bounds cannot depend on local variables.
5333 -- Otherwise there is no way to declare the result outside
5334 -- of the block. Needless to say, in general the bounds will
5335 -- depend on the actuals in the call.
5337 if Nkind
(Parent
(N
)) /= N_Assignment_Statement
then
5338 Targ1
:= Defining_Identifier
(First
(Declarations
(Blk
)));
5340 -- If the context is an assignment statement, as is the case
5341 -- for the expansion of an extended return, the left-hand
5342 -- side provides bounds even if the return type is
5346 Targ1
:= Name
(Parent
(N
));
5351 if No
(Declarations
(Bod
)) then
5352 Set_Declarations
(Blk
, New_List
);
5357 -- If this is a derived function, establish the proper return type
5359 if Present
(Orig_Subp
) and then Orig_Subp
/= Subp
then
5360 Ret_Type
:= Etype
(Orig_Subp
);
5362 Ret_Type
:= Etype
(Subp
);
5365 -- Create temporaries for the actuals that are expressions, or that are
5366 -- scalars and require copying to preserve semantics.
5368 F
:= First_Formal
(Subp
);
5369 A
:= First_Actual
(N
);
5370 while Present
(F
) loop
5371 if Present
(Renamed_Object
(F
)) then
5372 Error_Msg_N
("cannot inline call to recursive subprogram", N
);
5376 -- Reset Last_Assignment for any parameters of mode out or in out, to
5377 -- prevent spurious warnings about overwriting for assignments to the
5378 -- formal in the inlined code.
5380 if Is_Entity_Name
(A
) and then Ekind
(F
) /= E_In_Parameter
then
5381 Set_Last_Assignment
(Entity
(A
), Empty
);
5384 -- If the argument may be a controlling argument in a call within
5385 -- the inlined body, we must preserve its classwide nature to insure
5386 -- that dynamic dispatching take place subsequently. If the formal
5387 -- has a constraint it must be preserved to retain the semantics of
5390 if Is_Class_Wide_Type
(Etype
(F
))
5391 or else (Is_Access_Type
(Etype
(F
))
5392 and then Is_Class_Wide_Type
(Designated_Type
(Etype
(F
))))
5394 Temp_Typ
:= Etype
(F
);
5396 elsif Base_Type
(Etype
(F
)) = Base_Type
(Etype
(A
))
5397 and then Etype
(F
) /= Base_Type
(Etype
(F
))
5399 Temp_Typ
:= Etype
(F
);
5401 Temp_Typ
:= Etype
(A
);
5404 -- If the actual is a simple name or a literal, no need to
5405 -- create a temporary, object can be used directly.
5407 -- If the actual is a literal and the formal has its address taken,
5408 -- we cannot pass the literal itself as an argument, so its value
5409 -- must be captured in a temporary.
5411 if (Is_Entity_Name
(A
)
5413 (not Is_Scalar_Type
(Etype
(A
))
5414 or else Ekind
(Entity
(A
)) = E_Enumeration_Literal
))
5416 -- When the actual is an identifier and the corresponding formal is
5417 -- used only once in the original body, the formal can be substituted
5418 -- directly with the actual parameter.
5420 or else (Nkind
(A
) = N_Identifier
5421 and then Formal_Is_Used_Once
(F
))
5424 (Nkind_In
(A
, N_Real_Literal
,
5426 N_Character_Literal
)
5427 and then not Address_Taken
(F
))
5429 if Etype
(F
) /= Etype
(A
) then
5431 (F
, Unchecked_Convert_To
(Etype
(F
), Relocate_Node
(A
)));
5433 Set_Renamed_Object
(F
, A
);
5437 Temp
:= Make_Temporary
(Loc
, 'C');
5439 -- If the actual for an in/in-out parameter is a view conversion,
5440 -- make it into an unchecked conversion, given that an untagged
5441 -- type conversion is not a proper object for a renaming.
5443 -- In-out conversions that involve real conversions have already
5444 -- been transformed in Expand_Actuals.
5446 if Nkind
(A
) = N_Type_Conversion
5447 and then Ekind
(F
) /= E_In_Parameter
5450 Make_Unchecked_Type_Conversion
(Loc
,
5451 Subtype_Mark
=> New_Occurrence_Of
(Etype
(F
), Loc
),
5452 Expression
=> Relocate_Node
(Expression
(A
)));
5454 elsif Etype
(F
) /= Etype
(A
) then
5455 New_A
:= Unchecked_Convert_To
(Etype
(F
), Relocate_Node
(A
));
5456 Temp_Typ
:= Etype
(F
);
5459 New_A
:= Relocate_Node
(A
);
5462 Set_Sloc
(New_A
, Sloc
(N
));
5464 -- If the actual has a by-reference type, it cannot be copied,
5465 -- so its value is captured in a renaming declaration. Otherwise
5466 -- declare a local constant initialized with the actual.
5468 -- We also use a renaming declaration for expressions of an array
5469 -- type that is not bit-packed, both for efficiency reasons and to
5470 -- respect the semantics of the call: in most cases the original
5471 -- call will pass the parameter by reference, and thus the inlined
5472 -- code will have the same semantics.
5474 if Ekind
(F
) = E_In_Parameter
5475 and then not Is_By_Reference_Type
(Etype
(A
))
5477 (not Is_Array_Type
(Etype
(A
))
5478 or else not Is_Object_Reference
(A
)
5479 or else Is_Bit_Packed_Array
(Etype
(A
)))
5482 Make_Object_Declaration
(Loc
,
5483 Defining_Identifier
=> Temp
,
5484 Constant_Present
=> True,
5485 Object_Definition
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
5486 Expression
=> New_A
);
5489 Make_Object_Renaming_Declaration
(Loc
,
5490 Defining_Identifier
=> Temp
,
5491 Subtype_Mark
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
5495 Append
(Decl
, Decls
);
5496 Set_Renamed_Object
(F
, Temp
);
5503 -- Establish target of function call. If context is not assignment or
5504 -- declaration, create a temporary as a target. The declaration for the
5505 -- temporary may be subsequently optimized away if the body is a single
5506 -- expression, or if the left-hand side of the assignment is simple
5507 -- enough, i.e. an entity or an explicit dereference of one.
5509 if Ekind
(Subp
) = E_Function
then
5510 if Nkind
(Parent
(N
)) = N_Assignment_Statement
5511 and then Is_Entity_Name
(Name
(Parent
(N
)))
5513 Targ
:= Name
(Parent
(N
));
5515 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
5516 and then Nkind
(Name
(Parent
(N
))) = N_Explicit_Dereference
5517 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))
5519 Targ
:= Name
(Parent
(N
));
5521 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
5522 and then Nkind
(Name
(Parent
(N
))) = N_Selected_Component
5523 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))
5525 Targ
:= New_Copy_Tree
(Name
(Parent
(N
)));
5527 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
5528 and then Is_Limited_Type
(Etype
(Subp
))
5530 Targ
:= Defining_Identifier
(Parent
(N
));
5532 -- New semantics: In an object declaration avoid an extra copy
5533 -- of the result of a call to an inlined function that returns
5534 -- an unconstrained type
5536 elsif Debug_Flag_Dot_K
5537 and then Nkind
(Parent
(N
)) = N_Object_Declaration
5540 Targ
:= Defining_Identifier
(Parent
(N
));
5543 -- Replace call with temporary and create its declaration
5545 Temp
:= Make_Temporary
(Loc
, 'C');
5546 Set_Is_Internal
(Temp
);
5548 -- For the unconstrained case, the generated temporary has the
5549 -- same constrained declaration as the result variable. It may
5550 -- eventually be possible to remove that temporary and use the
5551 -- result variable directly.
5554 and then Nkind
(Parent
(N
)) /= N_Assignment_Statement
5557 Make_Object_Declaration
(Loc
,
5558 Defining_Identifier
=> Temp
,
5559 Object_Definition
=>
5560 New_Copy_Tree
(Object_Definition
(Parent
(Targ1
))));
5562 Replace_Formals
(Decl
);
5566 Make_Object_Declaration
(Loc
,
5567 Defining_Identifier
=> Temp
,
5568 Object_Definition
=> New_Occurrence_Of
(Ret_Type
, Loc
));
5570 Set_Etype
(Temp
, Ret_Type
);
5573 Set_No_Initialization
(Decl
);
5574 Append
(Decl
, Decls
);
5575 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
5580 Insert_Actions
(N
, Decls
);
5584 -- Special management for inlining a call to a function that returns
5585 -- an unconstrained type and initializes an object declaration: we
5586 -- avoid generating undesired extra calls and goto statements.
5589 -- function Func (...) return ...
5592 -- Result : String (1 .. 4);
5594 -- Proc (Result, ...);
5599 -- Result : String := Func (...);
5601 -- Replace this object declaration by:
5603 -- Result : String (1 .. 4);
5604 -- Proc (Result, ...);
5606 Remove_Homonym
(Targ
);
5609 Make_Object_Declaration
5611 Defining_Identifier
=> Targ
,
5612 Object_Definition
=>
5613 New_Copy_Tree
(Object_Definition
(Parent
(Targ1
))));
5614 Replace_Formals
(Decl
);
5615 Rewrite
(Parent
(N
), Decl
);
5616 Analyze
(Parent
(N
));
5618 -- Avoid spurious warnings since we know that this declaration is
5619 -- referenced by the procedure call.
5621 Set_Never_Set_In_Source
(Targ
, False);
5623 -- Remove the local declaration of the extended return stmt from the
5626 Remove
(Parent
(Targ1
));
5628 -- Update the reference to the result (since we have rewriten the
5629 -- object declaration)
5632 Blk_Call_Stmt
: Node_Id
;
5635 -- Capture the call to the procedure
5638 First
(Statements
(Handled_Statement_Sequence
(Blk
)));
5640 (Nkind
(Blk_Call_Stmt
) = N_Procedure_Call_Statement
);
5642 Remove
(First
(Parameter_Associations
(Blk_Call_Stmt
)));
5643 Prepend_To
(Parameter_Associations
(Blk_Call_Stmt
),
5644 New_Reference_To
(Targ
, Loc
));
5647 -- Remove the return statement
5650 (Nkind
(Last
(Statements
(Handled_Statement_Sequence
(Blk
)))) =
5651 N_Simple_Return_Statement
);
5653 Remove
(Last
(Statements
(Handled_Statement_Sequence
(Blk
))));
5656 -- Traverse the tree and replace formals with actuals or their thunks.
5657 -- Attach block to tree before analysis and rewriting.
5659 Replace_Formals
(Blk
);
5660 Set_Parent
(Blk
, N
);
5662 if not Comes_From_Source
(Subp
) or else Is_Predef
then
5668 -- No action needed since return statement has been already removed!
5672 elsif Present
(Exit_Lab
) then
5674 -- If the body was a single expression, the single return statement
5675 -- and the corresponding label are useless.
5679 Nkind
(Last
(Statements
(Handled_Statement_Sequence
(Blk
)))) =
5682 Remove
(Last
(Statements
(Handled_Statement_Sequence
(Blk
))));
5684 Append
(Lab_Decl
, (Declarations
(Blk
)));
5685 Append
(Exit_Lab
, Statements
(Handled_Statement_Sequence
(Blk
)));
5689 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
5690 -- on conflicting private views that Gigi would ignore. If this is a
5691 -- predefined unit, analyze with checks off, as is done in the non-
5692 -- inlined run-time units.
5695 I_Flag
: constant Boolean := In_Inlined_Body
;
5698 In_Inlined_Body
:= True;
5702 Style
: constant Boolean := Style_Check
;
5705 Style_Check
:= False;
5707 -- Search for dispatching calls that use the Object.Operation
5708 -- notation using an Object that is a parameter of the inlined
5709 -- function. We reset the decoration of Operation to force
5710 -- the reanalysis of the inlined dispatching call because
5711 -- the actual object has been inlined.
5713 Reset_Dispatching_Calls
(Blk
);
5715 Analyze
(Blk
, Suppress
=> All_Checks
);
5716 Style_Check
:= Style
;
5723 In_Inlined_Body
:= I_Flag
;
5726 if Ekind
(Subp
) = E_Procedure
then
5727 Rewrite_Procedure_Call
(N
, Blk
);
5730 Rewrite_Function_Call
(N
, Blk
);
5735 -- For the unconstrained case, the replacement of the call has been
5736 -- made prior to the complete analysis of the generated declarations.
5737 -- Propagate the proper type now.
5740 if Nkind
(N
) = N_Identifier
then
5741 Set_Etype
(N
, Etype
(Entity
(N
)));
5743 Set_Etype
(N
, Etype
(Targ1
));
5750 -- Cleanup mapping between formals and actuals for other expansions
5752 F
:= First_Formal
(Subp
);
5753 while Present
(F
) loop
5754 Set_Renamed_Object
(F
, Empty
);
5757 end Expand_Inlined_Call
;
5759 ----------------------------------------
5760 -- Expand_N_Extended_Return_Statement --
5761 ----------------------------------------
5763 -- If there is a Handled_Statement_Sequence, we rewrite this:
5765 -- return Result : T := <expression> do
5766 -- <handled_seq_of_stms>
5772 -- Result : T := <expression>;
5774 -- <handled_seq_of_stms>
5778 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
5780 -- return Result : T := <expression>;
5784 -- return <expression>;
5786 -- unless it's build-in-place or there's no <expression>, in which case
5790 -- Result : T := <expression>;
5795 -- Note that this case could have been written by the user as an extended
5796 -- return statement, or could have been transformed to this from a simple
5797 -- return statement.
5799 -- That is, we need to have a reified return object if there are statements
5800 -- (which might refer to it) or if we're doing build-in-place (so we can
5801 -- set its address to the final resting place or if there is no expression
5802 -- (in which case default initial values might need to be set).
5804 procedure Expand_N_Extended_Return_Statement
(N
: Node_Id
) is
5805 Loc
: constant Source_Ptr
:= Sloc
(N
);
5807 Par_Func
: constant Entity_Id
:=
5808 Return_Applies_To
(Return_Statement_Entity
(N
));
5809 Result_Subt
: constant Entity_Id
:= Etype
(Par_Func
);
5810 Ret_Obj_Id
: constant Entity_Id
:=
5811 First_Entity
(Return_Statement_Entity
(N
));
5812 Ret_Obj_Decl
: constant Node_Id
:= Parent
(Ret_Obj_Id
);
5814 Is_Build_In_Place
: constant Boolean :=
5815 Is_Build_In_Place_Function
(Par_Func
);
5820 Return_Stmt
: Node_Id
;
5823 function Build_Heap_Allocator
5824 (Temp_Id
: Entity_Id
;
5825 Temp_Typ
: Entity_Id
;
5826 Func_Id
: Entity_Id
;
5827 Ret_Typ
: Entity_Id
;
5828 Alloc_Expr
: Node_Id
) return Node_Id
;
5829 -- Create the statements necessary to allocate a return object on the
5830 -- caller's master. The master is available through implicit parameter
5831 -- BIPfinalizationmaster.
5833 -- if BIPfinalizationmaster /= null then
5835 -- type Ptr_Typ is access Ret_Typ;
5836 -- for Ptr_Typ'Storage_Pool use
5837 -- Base_Pool (BIPfinalizationmaster.all).all;
5841 -- procedure Allocate (...) is
5843 -- System.Storage_Pools.Subpools.Allocate_Any (...);
5846 -- Local := <Alloc_Expr>;
5847 -- Temp_Id := Temp_Typ (Local);
5851 -- Temp_Id is the temporary which is used to reference the internally
5852 -- created object in all allocation forms. Temp_Typ is the type of the
5853 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
5854 -- type of Func_Id. Alloc_Expr is the actual allocator.
5856 function Move_Activation_Chain
return Node_Id
;
5857 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
5859 -- From current activation chain
5860 -- To activation chain passed in by the caller
5861 -- New_Master master passed in by the caller
5863 --------------------------
5864 -- Build_Heap_Allocator --
5865 --------------------------
5867 function Build_Heap_Allocator
5868 (Temp_Id
: Entity_Id
;
5869 Temp_Typ
: Entity_Id
;
5870 Func_Id
: Entity_Id
;
5871 Ret_Typ
: Entity_Id
;
5872 Alloc_Expr
: Node_Id
) return Node_Id
5875 pragma Assert
(Is_Build_In_Place_Function
(Func_Id
));
5877 -- Processing for build-in-place object allocation. This is disabled
5878 -- on .NET/JVM because the targets do not support pools.
5880 if VM_Target
= No_VM
5881 and then Needs_Finalization
(Ret_Typ
)
5884 Decls
: constant List_Id
:= New_List
;
5885 Fin_Mas_Id
: constant Entity_Id
:=
5886 Build_In_Place_Formal
5887 (Func_Id
, BIP_Finalization_Master
);
5888 Stmts
: constant List_Id
:= New_List
;
5889 Desig_Typ
: Entity_Id
;
5890 Local_Id
: Entity_Id
;
5891 Pool_Id
: Entity_Id
;
5892 Ptr_Typ
: Entity_Id
;
5896 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
5898 Pool_Id
:= Make_Temporary
(Loc
, 'P');
5901 Make_Object_Renaming_Declaration
(Loc
,
5902 Defining_Identifier
=> Pool_Id
,
5904 New_Reference_To
(RTE
(RE_Root_Storage_Pool
), Loc
),
5906 Make_Explicit_Dereference
(Loc
,
5908 Make_Function_Call
(Loc
,
5910 New_Reference_To
(RTE
(RE_Base_Pool
), Loc
),
5911 Parameter_Associations
=> New_List
(
5912 Make_Explicit_Dereference
(Loc
,
5914 New_Reference_To
(Fin_Mas_Id
, Loc
)))))));
5916 -- Create an access type which uses the storage pool of the
5917 -- caller's master. This additional type is necessary because
5918 -- the finalization master cannot be associated with the type
5919 -- of the temporary. Otherwise the secondary stack allocation
5922 Desig_Typ
:= Ret_Typ
;
5924 -- Ensure that the build-in-place machinery uses a fat pointer
5925 -- when allocating an unconstrained array on the heap. In this
5926 -- case the result object type is a constrained array type even
5927 -- though the function type is unconstrained.
5929 if Ekind
(Desig_Typ
) = E_Array_Subtype
then
5930 Desig_Typ
:= Base_Type
(Desig_Typ
);
5934 -- type Ptr_Typ is access Desig_Typ;
5936 Ptr_Typ
:= Make_Temporary
(Loc
, 'P');
5939 Make_Full_Type_Declaration
(Loc
,
5940 Defining_Identifier
=> Ptr_Typ
,
5942 Make_Access_To_Object_Definition
(Loc
,
5943 Subtype_Indication
=>
5944 New_Reference_To
(Desig_Typ
, Loc
))));
5946 -- Perform minor decoration in order to set the master and the
5947 -- storage pool attributes.
5949 Set_Ekind
(Ptr_Typ
, E_Access_Type
);
5950 Set_Finalization_Master
(Ptr_Typ
, Fin_Mas_Id
);
5951 Set_Associated_Storage_Pool
(Ptr_Typ
, Pool_Id
);
5953 -- Create the temporary, generate:
5954 -- Local_Id : Ptr_Typ;
5956 Local_Id
:= Make_Temporary
(Loc
, 'T');
5959 Make_Object_Declaration
(Loc
,
5960 Defining_Identifier
=> Local_Id
,
5961 Object_Definition
=>
5962 New_Reference_To
(Ptr_Typ
, Loc
)));
5964 -- Allocate the object, generate:
5965 -- Local_Id := <Alloc_Expr>;
5968 Make_Assignment_Statement
(Loc
,
5969 Name
=> New_Reference_To
(Local_Id
, Loc
),
5970 Expression
=> Alloc_Expr
));
5973 -- Temp_Id := Temp_Typ (Local_Id);
5976 Make_Assignment_Statement
(Loc
,
5977 Name
=> New_Reference_To
(Temp_Id
, Loc
),
5979 Unchecked_Convert_To
(Temp_Typ
,
5980 New_Reference_To
(Local_Id
, Loc
))));
5982 -- Wrap the allocation in a block. This is further conditioned
5983 -- by checking the caller finalization master at runtime. A
5984 -- null value indicates a non-existent master, most likely due
5985 -- to a Finalize_Storage_Only allocation.
5988 -- if BIPfinalizationmaster /= null then
5997 Make_If_Statement
(Loc
,
6000 Left_Opnd
=> New_Reference_To
(Fin_Mas_Id
, Loc
),
6001 Right_Opnd
=> Make_Null
(Loc
)),
6003 Then_Statements
=> New_List
(
6004 Make_Block_Statement
(Loc
,
6005 Declarations
=> Decls
,
6006 Handled_Statement_Sequence
=>
6007 Make_Handled_Sequence_Of_Statements
(Loc
,
6008 Statements
=> Stmts
))));
6011 -- For all other cases, generate:
6012 -- Temp_Id := <Alloc_Expr>;
6016 Make_Assignment_Statement
(Loc
,
6017 Name
=> New_Reference_To
(Temp_Id
, Loc
),
6018 Expression
=> Alloc_Expr
);
6020 end Build_Heap_Allocator
;
6022 ---------------------------
6023 -- Move_Activation_Chain --
6024 ---------------------------
6026 function Move_Activation_Chain
return Node_Id
is
6029 Make_Procedure_Call_Statement
(Loc
,
6031 New_Reference_To
(RTE
(RE_Move_Activation_Chain
), Loc
),
6033 Parameter_Associations
=> New_List
(
6037 Make_Attribute_Reference
(Loc
,
6038 Prefix
=> Make_Identifier
(Loc
, Name_uChain
),
6039 Attribute_Name
=> Name_Unrestricted_Access
),
6041 -- Destination chain
6044 (Build_In_Place_Formal
(Par_Func
, BIP_Activation_Chain
), Loc
),
6049 (Build_In_Place_Formal
(Par_Func
, BIP_Task_Master
), Loc
)));
6050 end Move_Activation_Chain
;
6052 -- Start of processing for Expand_N_Extended_Return_Statement
6055 -- Given that functionality of interface thunks is simple (just displace
6056 -- the pointer to the object) they are always handled by means of
6057 -- simple return statements.
6059 pragma Assert
(not Is_Thunk
(Current_Scope
));
6061 if Nkind
(Ret_Obj_Decl
) = N_Object_Declaration
then
6062 Exp
:= Expression
(Ret_Obj_Decl
);
6067 HSS
:= Handled_Statement_Sequence
(N
);
6069 -- If the returned object needs finalization actions, the function must
6070 -- perform the appropriate cleanup should it fail to return. The state
6071 -- of the function itself is tracked through a flag which is coupled
6072 -- with the scope finalizer. There is one flag per each return object
6073 -- in case of multiple returns.
6075 if Is_Build_In_Place
6076 and then Needs_Finalization
(Etype
(Ret_Obj_Id
))
6079 Flag_Decl
: Node_Id
;
6080 Flag_Id
: Entity_Id
;
6084 -- Recover the function body
6086 Func_Bod
:= Unit_Declaration_Node
(Par_Func
);
6088 if Nkind
(Func_Bod
) = N_Subprogram_Declaration
then
6089 Func_Bod
:= Parent
(Parent
(Corresponding_Body
(Func_Bod
)));
6092 -- Create a flag to track the function state
6094 Flag_Id
:= Make_Temporary
(Loc
, 'F');
6095 Set_Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
, Flag_Id
);
6097 -- Insert the flag at the beginning of the function declarations,
6099 -- Fnn : Boolean := False;
6102 Make_Object_Declaration
(Loc
,
6103 Defining_Identifier
=> Flag_Id
,
6104 Object_Definition
=>
6105 New_Reference_To
(Standard_Boolean
, Loc
),
6106 Expression
=> New_Reference_To
(Standard_False
, Loc
));
6108 Prepend_To
(Declarations
(Func_Bod
), Flag_Decl
);
6109 Analyze
(Flag_Decl
);
6113 -- Build a simple_return_statement that returns the return object when
6114 -- there is a statement sequence, or no expression, or the result will
6115 -- be built in place. Note however that we currently do this for all
6116 -- composite cases, even though nonlimited composite results are not yet
6117 -- built in place (though we plan to do so eventually).
6120 or else Is_Composite_Type
(Result_Subt
)
6126 -- If the extended return has a handled statement sequence, then wrap
6127 -- it in a block and use the block as the first statement.
6131 Make_Block_Statement
(Loc
,
6132 Declarations
=> New_List
,
6133 Handled_Statement_Sequence
=> HSS
));
6136 -- If the result type contains tasks, we call Move_Activation_Chain.
6137 -- Later, the cleanup code will call Complete_Master, which will
6138 -- terminate any unactivated tasks belonging to the return statement
6139 -- master. But Move_Activation_Chain updates their master to be that
6140 -- of the caller, so they will not be terminated unless the return
6141 -- statement completes unsuccessfully due to exception, abort, goto,
6142 -- or exit. As a formality, we test whether the function requires the
6143 -- result to be built in place, though that's necessarily true for
6144 -- the case of result types with task parts.
6146 if Is_Build_In_Place
6147 and then Has_Task
(Result_Subt
)
6149 -- The return expression is an aggregate for a complex type which
6150 -- contains tasks. This particular case is left unexpanded since
6151 -- the regular expansion would insert all temporaries and
6152 -- initialization code in the wrong block.
6154 if Nkind
(Exp
) = N_Aggregate
then
6155 Expand_N_Aggregate
(Exp
);
6158 -- Do not move the activation chain if the return object does not
6161 if Has_Task
(Etype
(Ret_Obj_Id
)) then
6162 Append_To
(Stmts
, Move_Activation_Chain
);
6166 -- Update the state of the function right before the object is
6169 if Is_Build_In_Place
6170 and then Needs_Finalization
(Etype
(Ret_Obj_Id
))
6173 Flag_Id
: constant Entity_Id
:=
6174 Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
);
6181 Make_Assignment_Statement
(Loc
,
6182 Name
=> New_Reference_To
(Flag_Id
, Loc
),
6183 Expression
=> New_Reference_To
(Standard_True
, Loc
)));
6187 -- Build a simple_return_statement that returns the return object
6190 Make_Simple_Return_Statement
(Loc
,
6191 Expression
=> New_Occurrence_Of
(Ret_Obj_Id
, Loc
));
6192 Append_To
(Stmts
, Return_Stmt
);
6194 HSS
:= Make_Handled_Sequence_Of_Statements
(Loc
, Stmts
);
6197 -- Case where we build a return statement block
6199 if Present
(HSS
) then
6201 Make_Block_Statement
(Loc
,
6202 Declarations
=> Return_Object_Declarations
(N
),
6203 Handled_Statement_Sequence
=> HSS
);
6205 -- We set the entity of the new block statement to be that of the
6206 -- return statement. This is necessary so that various fields, such
6207 -- as Finalization_Chain_Entity carry over from the return statement
6208 -- to the block. Note that this block is unusual, in that its entity
6209 -- is an E_Return_Statement rather than an E_Block.
6212 (Result
, New_Occurrence_Of
(Return_Statement_Entity
(N
), Loc
));
6214 -- If the object decl was already rewritten as a renaming, then we
6215 -- don't want to do the object allocation and transformation of of
6216 -- the return object declaration to a renaming. This case occurs
6217 -- when the return object is initialized by a call to another
6218 -- build-in-place function, and that function is responsible for
6219 -- the allocation of the return object.
6221 if Is_Build_In_Place
6222 and then Nkind
(Ret_Obj_Decl
) = N_Object_Renaming_Declaration
6225 (Nkind
(Original_Node
(Ret_Obj_Decl
)) = N_Object_Declaration
6226 and then Is_Build_In_Place_Function_Call
6227 (Expression
(Original_Node
(Ret_Obj_Decl
))));
6229 -- Return the build-in-place result by reference
6231 Set_By_Ref
(Return_Stmt
);
6233 elsif Is_Build_In_Place
then
6235 -- Locate the implicit access parameter associated with the
6236 -- caller-supplied return object and convert the return
6237 -- statement's return object declaration to a renaming of a
6238 -- dereference of the access parameter. If the return object's
6239 -- declaration includes an expression that has not already been
6240 -- expanded as separate assignments, then add an assignment
6241 -- statement to ensure the return object gets initialized.
6244 -- Result : T [:= <expression>];
6251 -- Result : T renames FuncRA.all;
6252 -- [Result := <expression;]
6257 Return_Obj_Id
: constant Entity_Id
:=
6258 Defining_Identifier
(Ret_Obj_Decl
);
6259 Return_Obj_Typ
: constant Entity_Id
:= Etype
(Return_Obj_Id
);
6260 Return_Obj_Expr
: constant Node_Id
:=
6261 Expression
(Ret_Obj_Decl
);
6262 Constr_Result
: constant Boolean :=
6263 Is_Constrained
(Result_Subt
);
6264 Obj_Alloc_Formal
: Entity_Id
;
6265 Object_Access
: Entity_Id
;
6266 Obj_Acc_Deref
: Node_Id
;
6267 Init_Assignment
: Node_Id
:= Empty
;
6270 -- Build-in-place results must be returned by reference
6272 Set_By_Ref
(Return_Stmt
);
6274 -- Retrieve the implicit access parameter passed by the caller
6277 Build_In_Place_Formal
(Par_Func
, BIP_Object_Access
);
6279 -- If the return object's declaration includes an expression
6280 -- and the declaration isn't marked as No_Initialization, then
6281 -- we need to generate an assignment to the object and insert
6282 -- it after the declaration before rewriting it as a renaming
6283 -- (otherwise we'll lose the initialization). The case where
6284 -- the result type is an interface (or class-wide interface)
6285 -- is also excluded because the context of the function call
6286 -- must be unconstrained, so the initialization will always
6287 -- be done as part of an allocator evaluation (storage pool
6288 -- or secondary stack), never to a constrained target object
6289 -- passed in by the caller. Besides the assignment being
6290 -- unneeded in this case, it avoids problems with trying to
6291 -- generate a dispatching assignment when the return expression
6292 -- is a nonlimited descendant of a limited interface (the
6293 -- interface has no assignment operation).
6295 if Present
(Return_Obj_Expr
)
6296 and then not No_Initialization
(Ret_Obj_Decl
)
6297 and then not Is_Interface
(Return_Obj_Typ
)
6300 Make_Assignment_Statement
(Loc
,
6301 Name
=> New_Reference_To
(Return_Obj_Id
, Loc
),
6302 Expression
=> Relocate_Node
(Return_Obj_Expr
));
6304 Set_Etype
(Name
(Init_Assignment
), Etype
(Return_Obj_Id
));
6305 Set_Assignment_OK
(Name
(Init_Assignment
));
6306 Set_No_Ctrl_Actions
(Init_Assignment
);
6308 Set_Parent
(Name
(Init_Assignment
), Init_Assignment
);
6309 Set_Parent
(Expression
(Init_Assignment
), Init_Assignment
);
6311 Set_Expression
(Ret_Obj_Decl
, Empty
);
6313 if Is_Class_Wide_Type
(Etype
(Return_Obj_Id
))
6314 and then not Is_Class_Wide_Type
6315 (Etype
(Expression
(Init_Assignment
)))
6317 Rewrite
(Expression
(Init_Assignment
),
6318 Make_Type_Conversion
(Loc
,
6320 New_Occurrence_Of
(Etype
(Return_Obj_Id
), Loc
),
6322 Relocate_Node
(Expression
(Init_Assignment
))));
6325 -- In the case of functions where the calling context can
6326 -- determine the form of allocation needed, initialization
6327 -- is done with each part of the if statement that handles
6328 -- the different forms of allocation (this is true for
6329 -- unconstrained and tagged result subtypes).
6332 and then not Is_Tagged_Type
(Underlying_Type
(Result_Subt
))
6334 Insert_After
(Ret_Obj_Decl
, Init_Assignment
);
6338 -- When the function's subtype is unconstrained, a run-time
6339 -- test is needed to determine the form of allocation to use
6340 -- for the return object. The function has an implicit formal
6341 -- parameter indicating this. If the BIP_Alloc_Form formal has
6342 -- the value one, then the caller has passed access to an
6343 -- existing object for use as the return object. If the value
6344 -- is two, then the return object must be allocated on the
6345 -- secondary stack. Otherwise, the object must be allocated in
6346 -- a storage pool (currently only supported for the global
6347 -- heap, user-defined storage pools TBD ???). We generate an
6348 -- if statement to test the implicit allocation formal and
6349 -- initialize a local access value appropriately, creating
6350 -- allocators in the secondary stack and global heap cases.
6351 -- The special formal also exists and must be tested when the
6352 -- function has a tagged result, even when the result subtype
6353 -- is constrained, because in general such functions can be
6354 -- called in dispatching contexts and must be handled similarly
6355 -- to functions with a class-wide result.
6357 if not Constr_Result
6358 or else Is_Tagged_Type
(Underlying_Type
(Result_Subt
))
6361 Build_In_Place_Formal
(Par_Func
, BIP_Alloc_Form
);
6364 Pool_Id
: constant Entity_Id
:=
6365 Make_Temporary
(Loc
, 'P');
6366 Alloc_Obj_Id
: Entity_Id
;
6367 Alloc_Obj_Decl
: Node_Id
;
6368 Alloc_If_Stmt
: Node_Id
;
6369 Heap_Allocator
: Node_Id
;
6370 Pool_Decl
: Node_Id
;
6371 Pool_Allocator
: Node_Id
;
6372 Ptr_Type_Decl
: Node_Id
;
6373 Ref_Type
: Entity_Id
;
6374 SS_Allocator
: Node_Id
;
6377 -- Reuse the itype created for the function's implicit
6378 -- access formal. This avoids the need to create a new
6379 -- access type here, plus it allows assigning the access
6380 -- formal directly without applying a conversion.
6382 -- Ref_Type := Etype (Object_Access);
6384 -- Create an access type designating the function's
6387 Ref_Type
:= Make_Temporary
(Loc
, 'A');
6390 Make_Full_Type_Declaration
(Loc
,
6391 Defining_Identifier
=> Ref_Type
,
6393 Make_Access_To_Object_Definition
(Loc
,
6394 All_Present
=> True,
6395 Subtype_Indication
=>
6396 New_Reference_To
(Return_Obj_Typ
, Loc
)));
6398 Insert_Before
(Ret_Obj_Decl
, Ptr_Type_Decl
);
6400 -- Create an access object that will be initialized to an
6401 -- access value denoting the return object, either coming
6402 -- from an implicit access value passed in by the caller
6403 -- or from the result of an allocator.
6405 Alloc_Obj_Id
:= Make_Temporary
(Loc
, 'R');
6406 Set_Etype
(Alloc_Obj_Id
, Ref_Type
);
6409 Make_Object_Declaration
(Loc
,
6410 Defining_Identifier
=> Alloc_Obj_Id
,
6411 Object_Definition
=>
6412 New_Reference_To
(Ref_Type
, Loc
));
6414 Insert_Before
(Ret_Obj_Decl
, Alloc_Obj_Decl
);
6416 -- Create allocators for both the secondary stack and
6417 -- global heap. If there's an initialization expression,
6418 -- then create these as initialized allocators.
6420 if Present
(Return_Obj_Expr
)
6421 and then not No_Initialization
(Ret_Obj_Decl
)
6423 -- Always use the type of the expression for the
6424 -- qualified expression, rather than the result type.
6425 -- In general we cannot always use the result type
6426 -- for the allocator, because the expression might be
6427 -- of a specific type, such as in the case of an
6428 -- aggregate or even a nonlimited object when the
6429 -- result type is a limited class-wide interface type.
6432 Make_Allocator
(Loc
,
6434 Make_Qualified_Expression
(Loc
,
6437 (Etype
(Return_Obj_Expr
), Loc
),
6439 New_Copy_Tree
(Return_Obj_Expr
)));
6442 -- If the function returns a class-wide type we cannot
6443 -- use the return type for the allocator. Instead we
6444 -- use the type of the expression, which must be an
6445 -- aggregate of a definite type.
6447 if Is_Class_Wide_Type
(Return_Obj_Typ
) then
6449 Make_Allocator
(Loc
,
6452 (Etype
(Return_Obj_Expr
), Loc
));
6455 Make_Allocator
(Loc
,
6457 New_Reference_To
(Return_Obj_Typ
, Loc
));
6460 -- If the object requires default initialization then
6461 -- that will happen later following the elaboration of
6462 -- the object renaming. If we don't turn it off here
6463 -- then the object will be default initialized twice.
6465 Set_No_Initialization
(Heap_Allocator
);
6468 -- The Pool_Allocator is just like the Heap_Allocator,
6469 -- except we set Storage_Pool and Procedure_To_Call so
6470 -- it will use the user-defined storage pool.
6472 Pool_Allocator
:= New_Copy_Tree
(Heap_Allocator
);
6474 -- Do not generate the renaming of the build-in-place
6475 -- pool parameter on .NET/JVM/ZFP because the parameter
6476 -- is not created in the first place.
6478 if VM_Target
= No_VM
6479 and then RTE_Available
(RE_Root_Storage_Pool_Ptr
)
6482 Make_Object_Renaming_Declaration
(Loc
,
6483 Defining_Identifier
=> Pool_Id
,
6486 (RTE
(RE_Root_Storage_Pool
), Loc
),
6488 Make_Explicit_Dereference
(Loc
,
6490 (Build_In_Place_Formal
6491 (Par_Func
, BIP_Storage_Pool
), Loc
)));
6492 Set_Storage_Pool
(Pool_Allocator
, Pool_Id
);
6493 Set_Procedure_To_Call
6494 (Pool_Allocator
, RTE
(RE_Allocate_Any
));
6496 Pool_Decl
:= Make_Null_Statement
(Loc
);
6499 -- If the No_Allocators restriction is active, then only
6500 -- an allocator for secondary stack allocation is needed.
6501 -- It's OK for such allocators to have Comes_From_Source
6502 -- set to False, because gigi knows not to flag them as
6503 -- being a violation of No_Implicit_Heap_Allocations.
6505 if Restriction_Active
(No_Allocators
) then
6506 SS_Allocator
:= Heap_Allocator
;
6507 Heap_Allocator
:= Make_Null
(Loc
);
6508 Pool_Allocator
:= Make_Null
(Loc
);
6510 -- Otherwise the heap and pool allocators may be needed,
6511 -- so we make another allocator for secondary stack
6515 SS_Allocator
:= New_Copy_Tree
(Heap_Allocator
);
6517 -- The heap and pool allocators are marked as
6518 -- Comes_From_Source since they correspond to an
6519 -- explicit user-written allocator (that is, it will
6520 -- only be executed on behalf of callers that call the
6521 -- function as initialization for such an allocator).
6522 -- Prevents errors when No_Implicit_Heap_Allocations
6525 Set_Comes_From_Source
(Heap_Allocator
, True);
6526 Set_Comes_From_Source
(Pool_Allocator
, True);
6529 -- The allocator is returned on the secondary stack. We
6530 -- don't do this on VM targets, since the SS is not used.
6532 if VM_Target
= No_VM
then
6533 Set_Storage_Pool
(SS_Allocator
, RTE
(RE_SS_Pool
));
6534 Set_Procedure_To_Call
6535 (SS_Allocator
, RTE
(RE_SS_Allocate
));
6537 -- The allocator is returned on the secondary stack,
6538 -- so indicate that the function return, as well as
6539 -- the block that encloses the allocator, must not
6540 -- release it. The flags must be set now because
6541 -- the decision to use the secondary stack is done
6542 -- very late in the course of expanding the return
6543 -- statement, past the point where these flags are
6546 Set_Sec_Stack_Needed_For_Return
(Par_Func
);
6547 Set_Sec_Stack_Needed_For_Return
6548 (Return_Statement_Entity
(N
));
6549 Set_Uses_Sec_Stack
(Par_Func
);
6550 Set_Uses_Sec_Stack
(Return_Statement_Entity
(N
));
6553 -- Create an if statement to test the BIP_Alloc_Form
6554 -- formal and initialize the access object to either the
6555 -- BIP_Object_Access formal (BIP_Alloc_Form =
6556 -- Caller_Allocation), the result of allocating the
6557 -- object in the secondary stack (BIP_Alloc_Form =
6558 -- Secondary_Stack), or else an allocator to create the
6559 -- return object in the heap or user-defined pool
6560 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
6562 -- ??? An unchecked type conversion must be made in the
6563 -- case of assigning the access object formal to the
6564 -- local access object, because a normal conversion would
6565 -- be illegal in some cases (such as converting access-
6566 -- to-unconstrained to access-to-constrained), but the
6567 -- the unchecked conversion will presumably fail to work
6568 -- right in just such cases. It's not clear at all how to
6572 Make_If_Statement
(Loc
,
6576 New_Reference_To
(Obj_Alloc_Formal
, Loc
),
6578 Make_Integer_Literal
(Loc
,
6579 UI_From_Int
(BIP_Allocation_Form
'Pos
6580 (Caller_Allocation
)))),
6582 Then_Statements
=> New_List
(
6583 Make_Assignment_Statement
(Loc
,
6585 New_Reference_To
(Alloc_Obj_Id
, Loc
),
6587 Make_Unchecked_Type_Conversion
(Loc
,
6589 New_Reference_To
(Ref_Type
, Loc
),
6591 New_Reference_To
(Object_Access
, Loc
)))),
6593 Elsif_Parts
=> New_List
(
6594 Make_Elsif_Part
(Loc
,
6598 New_Reference_To
(Obj_Alloc_Formal
, Loc
),
6600 Make_Integer_Literal
(Loc
,
6601 UI_From_Int
(BIP_Allocation_Form
'Pos
6602 (Secondary_Stack
)))),
6604 Then_Statements
=> New_List
(
6605 Make_Assignment_Statement
(Loc
,
6607 New_Reference_To
(Alloc_Obj_Id
, Loc
),
6608 Expression
=> SS_Allocator
))),
6610 Make_Elsif_Part
(Loc
,
6614 New_Reference_To
(Obj_Alloc_Formal
, Loc
),
6616 Make_Integer_Literal
(Loc
,
6617 UI_From_Int
(BIP_Allocation_Form
'Pos
6620 Then_Statements
=> New_List
(
6621 Build_Heap_Allocator
6622 (Temp_Id
=> Alloc_Obj_Id
,
6623 Temp_Typ
=> Ref_Type
,
6624 Func_Id
=> Par_Func
,
6625 Ret_Typ
=> Return_Obj_Typ
,
6626 Alloc_Expr
=> Heap_Allocator
)))),
6628 Else_Statements
=> New_List
(
6630 Build_Heap_Allocator
6631 (Temp_Id
=> Alloc_Obj_Id
,
6632 Temp_Typ
=> Ref_Type
,
6633 Func_Id
=> Par_Func
,
6634 Ret_Typ
=> Return_Obj_Typ
,
6635 Alloc_Expr
=> Pool_Allocator
)));
6637 -- If a separate initialization assignment was created
6638 -- earlier, append that following the assignment of the
6639 -- implicit access formal to the access object, to ensure
6640 -- that the return object is initialized in that case. In
6641 -- this situation, the target of the assignment must be
6642 -- rewritten to denote a dereference of the access to the
6643 -- return object passed in by the caller.
6645 if Present
(Init_Assignment
) then
6646 Rewrite
(Name
(Init_Assignment
),
6647 Make_Explicit_Dereference
(Loc
,
6648 Prefix
=> New_Reference_To
(Alloc_Obj_Id
, Loc
)));
6651 (Name
(Init_Assignment
), Etype
(Return_Obj_Id
));
6654 (Then_Statements
(Alloc_If_Stmt
), Init_Assignment
);
6657 Insert_Before
(Ret_Obj_Decl
, Alloc_If_Stmt
);
6659 -- Remember the local access object for use in the
6660 -- dereference of the renaming created below.
6662 Object_Access
:= Alloc_Obj_Id
;
6666 -- Replace the return object declaration with a renaming of a
6667 -- dereference of the access value designating the return
6671 Make_Explicit_Dereference
(Loc
,
6672 Prefix
=> New_Reference_To
(Object_Access
, Loc
));
6674 Rewrite
(Ret_Obj_Decl
,
6675 Make_Object_Renaming_Declaration
(Loc
,
6676 Defining_Identifier
=> Return_Obj_Id
,
6677 Access_Definition
=> Empty
,
6679 New_Occurrence_Of
(Return_Obj_Typ
, Loc
),
6680 Name
=> Obj_Acc_Deref
));
6682 Set_Renamed_Object
(Return_Obj_Id
, Obj_Acc_Deref
);
6686 -- Case where we do not build a block
6689 -- We're about to drop Return_Object_Declarations on the floor, so
6690 -- we need to insert it, in case it got expanded into useful code.
6691 -- Remove side effects from expression, which may be duplicated in
6692 -- subsequent checks (see Expand_Simple_Function_Return).
6694 Insert_List_Before
(N
, Return_Object_Declarations
(N
));
6695 Remove_Side_Effects
(Exp
);
6697 -- Build simple_return_statement that returns the expression directly
6699 Return_Stmt
:= Make_Simple_Return_Statement
(Loc
, Expression
=> Exp
);
6700 Result
:= Return_Stmt
;
6703 -- Set the flag to prevent infinite recursion
6705 Set_Comes_From_Extended_Return_Statement
(Return_Stmt
);
6707 Rewrite
(N
, Result
);
6709 end Expand_N_Extended_Return_Statement
;
6711 ----------------------------
6712 -- Expand_N_Function_Call --
6713 ----------------------------
6715 procedure Expand_N_Function_Call
(N
: Node_Id
) is
6719 -- If the return value of a foreign compiled function is VAX Float, then
6720 -- expand the return (adjusts the location of the return value on
6721 -- Alpha/VMS, no-op everywhere else).
6722 -- Comes_From_Source intercepts recursive expansion.
6724 if Nkind
(N
) = N_Function_Call
6725 and then Vax_Float
(Etype
(N
))
6726 and then Present
(Name
(N
))
6727 and then Present
(Entity
(Name
(N
)))
6728 and then Has_Foreign_Convention
(Entity
(Name
(N
)))
6729 and then Comes_From_Source
(Parent
(N
))
6731 Expand_Vax_Foreign_Return
(N
);
6733 end Expand_N_Function_Call
;
6735 ---------------------------------------
6736 -- Expand_N_Procedure_Call_Statement --
6737 ---------------------------------------
6739 procedure Expand_N_Procedure_Call_Statement
(N
: Node_Id
) is
6742 end Expand_N_Procedure_Call_Statement
;
6744 --------------------------------------
6745 -- Expand_N_Simple_Return_Statement --
6746 --------------------------------------
6748 procedure Expand_N_Simple_Return_Statement
(N
: Node_Id
) is
6750 -- Defend against previous errors (i.e. the return statement calls a
6751 -- function that is not available in configurable runtime).
6753 if Present
(Expression
(N
))
6754 and then Nkind
(Expression
(N
)) = N_Empty
6756 Check_Error_Detected
;
6760 -- Distinguish the function and non-function cases:
6762 case Ekind
(Return_Applies_To
(Return_Statement_Entity
(N
))) is
6765 E_Generic_Function
=>
6766 Expand_Simple_Function_Return
(N
);
6769 E_Generic_Procedure |
6772 E_Return_Statement
=>
6773 Expand_Non_Function_Return
(N
);
6776 raise Program_Error
;
6780 when RE_Not_Available
=>
6782 end Expand_N_Simple_Return_Statement
;
6784 ------------------------------
6785 -- Expand_N_Subprogram_Body --
6786 ------------------------------
6788 -- Add poll call if ATC polling is enabled, unless the body will be inlined
6791 -- Add dummy push/pop label nodes at start and end to clear any local
6792 -- exception indications if local-exception-to-goto optimization is active.
6794 -- Add return statement if last statement in body is not a return statement
6795 -- (this makes things easier on Gigi which does not want to have to handle
6796 -- a missing return).
6798 -- Add call to Activate_Tasks if body is a task activator
6800 -- Deal with possible detection of infinite recursion
6802 -- Eliminate body completely if convention stubbed
6804 -- Encode entity names within body, since we will not need to reference
6805 -- these entities any longer in the front end.
6807 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
6809 -- Reset Pure indication if any parameter has root type System.Address
6810 -- or has any parameters of limited types, where limited means that the
6811 -- run-time view is limited (i.e. the full type is limited).
6815 procedure Expand_N_Subprogram_Body
(N
: Node_Id
) is
6816 Loc
: constant Source_Ptr
:= Sloc
(N
);
6817 H
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
6818 Body_Id
: Entity_Id
;
6821 Spec_Id
: Entity_Id
;
6823 procedure Add_Return
(S
: List_Id
);
6824 -- Append a return statement to the statement sequence S if the last
6825 -- statement is not already a return or a goto statement. Note that
6826 -- the latter test is not critical, it does not matter if we add a few
6827 -- extra returns, since they get eliminated anyway later on.
6833 procedure Add_Return
(S
: List_Id
) is
6838 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
6839 -- not relevant in this context since they are not executable.
6841 Last_Stm
:= Last
(S
);
6842 while Nkind
(Last_Stm
) in N_Pop_xxx_Label
loop
6846 -- Now insert return unless last statement is a transfer
6848 if not Is_Transfer
(Last_Stm
) then
6850 -- The source location for the return is the end label of the
6851 -- procedure if present. Otherwise use the sloc of the last
6852 -- statement in the list. If the list comes from a generated
6853 -- exception handler and we are not debugging generated code,
6854 -- all the statements within the handler are made invisible
6857 if Nkind
(Parent
(S
)) = N_Exception_Handler
6858 and then not Comes_From_Source
(Parent
(S
))
6860 Loc
:= Sloc
(Last_Stm
);
6861 elsif Present
(End_Label
(H
)) then
6862 Loc
:= Sloc
(End_Label
(H
));
6864 Loc
:= Sloc
(Last_Stm
);
6868 Rtn
: constant Node_Id
:= Make_Simple_Return_Statement
(Loc
);
6871 -- Append return statement, and set analyzed manually. We can't
6872 -- call Analyze on this return since the scope is wrong.
6874 -- Note: it almost works to push the scope and then do the
6875 -- Analyze call, but something goes wrong in some weird cases
6876 -- and it is not worth worrying about ???
6881 -- Call _Postconditions procedure if appropriate. We need to
6882 -- do this explicitly because we did not analyze the generated
6883 -- return statement above, so the call did not get inserted.
6885 if Ekind
(Spec_Id
) = E_Procedure
6886 and then Has_Postconditions
(Spec_Id
)
6888 pragma Assert
(Present
(Postcondition_Proc
(Spec_Id
)));
6890 Make_Procedure_Call_Statement
(Loc
,
6892 New_Reference_To
(Postcondition_Proc
(Spec_Id
), Loc
)));
6898 -- Start of processing for Expand_N_Subprogram_Body
6901 -- Set L to either the list of declarations if present, or to the list
6902 -- of statements if no declarations are present. This is used to insert
6903 -- new stuff at the start.
6905 if Is_Non_Empty_List
(Declarations
(N
)) then
6906 L
:= Declarations
(N
);
6908 L
:= Statements
(H
);
6911 -- If local-exception-to-goto optimization active, insert dummy push
6912 -- statements at start, and dummy pop statements at end, but inhibit
6913 -- this if we have No_Exception_Handlers, since they are useless and
6914 -- intefere with analysis, e.g. by codepeer.
6916 if (Debug_Flag_Dot_G
6917 or else Restriction_Active
(No_Exception_Propagation
))
6918 and then not Restriction_Active
(No_Exception_Handlers
)
6919 and then not CodePeer_Mode
6920 and then Is_Non_Empty_List
(L
)
6923 FS
: constant Node_Id
:= First
(L
);
6924 FL
: constant Source_Ptr
:= Sloc
(FS
);
6929 -- LS points to either last statement, if statements are present
6930 -- or to the last declaration if there are no statements present.
6931 -- It is the node after which the pop's are generated.
6933 if Is_Non_Empty_List
(Statements
(H
)) then
6934 LS
:= Last
(Statements
(H
));
6941 Insert_List_Before_And_Analyze
(FS
, New_List
(
6942 Make_Push_Constraint_Error_Label
(FL
),
6943 Make_Push_Program_Error_Label
(FL
),
6944 Make_Push_Storage_Error_Label
(FL
)));
6946 Insert_List_After_And_Analyze
(LS
, New_List
(
6947 Make_Pop_Constraint_Error_Label
(LL
),
6948 Make_Pop_Program_Error_Label
(LL
),
6949 Make_Pop_Storage_Error_Label
(LL
)));
6953 -- Find entity for subprogram
6955 Body_Id
:= Defining_Entity
(N
);
6957 if Present
(Corresponding_Spec
(N
)) then
6958 Spec_Id
:= Corresponding_Spec
(N
);
6963 -- Need poll on entry to subprogram if polling enabled. We only do this
6964 -- for non-empty subprograms, since it does not seem necessary to poll
6965 -- for a dummy null subprogram.
6967 if Is_Non_Empty_List
(L
) then
6969 -- Do not add a polling call if the subprogram is to be inlined by
6970 -- the back-end, to avoid repeated calls with multiple inlinings.
6972 if Is_Inlined
(Spec_Id
)
6973 and then Front_End_Inlining
6974 and then Optimization_Level
> 1
6978 Generate_Poll_Call
(First
(L
));
6982 -- If this is a Pure function which has any parameters whose root type
6983 -- is System.Address, reset the Pure indication, since it will likely
6984 -- cause incorrect code to be generated as the parameter is probably
6985 -- a pointer, and the fact that the same pointer is passed does not mean
6986 -- that the same value is being referenced.
6988 -- Note that if the programmer gave an explicit Pure_Function pragma,
6989 -- then we believe the programmer, and leave the subprogram Pure.
6991 -- This code should probably be at the freeze point, so that it happens
6992 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
6993 -- semantic tree has Is_Pure set properly ???
6995 if Is_Pure
(Spec_Id
)
6996 and then Is_Subprogram
(Spec_Id
)
6997 and then not Has_Pragma_Pure_Function
(Spec_Id
)
7003 F
:= First_Formal
(Spec_Id
);
7004 while Present
(F
) loop
7005 if Is_Descendent_Of_Address
(Etype
(F
))
7007 -- Note that this test is being made in the body of the
7008 -- subprogram, not the spec, so we are testing the full
7009 -- type for being limited here, as required.
7011 or else Is_Limited_Type
(Etype
(F
))
7013 Set_Is_Pure
(Spec_Id
, False);
7015 if Spec_Id
/= Body_Id
then
7016 Set_Is_Pure
(Body_Id
, False);
7027 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
7029 if Init_Or_Norm_Scalars
and then Is_Subprogram
(Spec_Id
) then
7035 -- Loop through formals
7037 F
:= First_Formal
(Spec_Id
);
7038 while Present
(F
) loop
7039 if Is_Scalar_Type
(Etype
(F
))
7040 and then Ekind
(F
) = E_Out_Parameter
7042 Check_Restriction
(No_Default_Initialization
, F
);
7044 -- Insert the initialization. We turn off validity checks
7045 -- for this assignment, since we do not want any check on
7046 -- the initial value itself (which may well be invalid).
7047 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
7049 A
:= Make_Assignment_Statement
(Loc
,
7050 Name
=> New_Occurrence_Of
(F
, Loc
),
7051 Expression
=> Get_Simple_Init_Val
(Etype
(F
), N
));
7052 Set_Suppress_Assignment_Checks
(A
);
7054 Insert_Before_And_Analyze
(First
(L
),
7055 A
, Suppress
=> Validity_Check
);
7063 -- Clear out statement list for stubbed procedure
7065 if Present
(Corresponding_Spec
(N
)) then
7066 Set_Elaboration_Flag
(N
, Spec_Id
);
7068 if Convention
(Spec_Id
) = Convention_Stubbed
7069 or else Is_Eliminated
(Spec_Id
)
7071 Set_Declarations
(N
, Empty_List
);
7072 Set_Handled_Statement_Sequence
(N
,
7073 Make_Handled_Sequence_Of_Statements
(Loc
,
7074 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
7079 -- Create a set of discriminals for the next protected subprogram body
7081 if Is_List_Member
(N
)
7082 and then Present
(Parent
(List_Containing
(N
)))
7083 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
7084 and then Present
(Next_Protected_Operation
(N
))
7086 Set_Discriminals
(Parent
(Base_Type
(Scope
(Spec_Id
))));
7089 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
7090 -- subprograms with no specs are not frozen.
7093 Typ
: constant Entity_Id
:= Etype
(Spec_Id
);
7094 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
7097 if not Acts_As_Spec
(N
)
7098 and then Nkind
(Parent
(Parent
(Spec_Id
))) /=
7099 N_Subprogram_Body_Stub
7103 elsif Is_Limited_View
(Typ
) then
7104 Set_Returns_By_Ref
(Spec_Id
);
7106 elsif Present
(Utyp
) and then CW_Or_Has_Controlled_Part
(Utyp
) then
7107 Set_Returns_By_Ref
(Spec_Id
);
7111 -- For a procedure, we add a return for all possible syntactic ends of
7114 if Ekind_In
(Spec_Id
, E_Procedure
, E_Generic_Procedure
) then
7115 Add_Return
(Statements
(H
));
7117 if Present
(Exception_Handlers
(H
)) then
7118 Except_H
:= First_Non_Pragma
(Exception_Handlers
(H
));
7119 while Present
(Except_H
) loop
7120 Add_Return
(Statements
(Except_H
));
7121 Next_Non_Pragma
(Except_H
);
7125 -- For a function, we must deal with the case where there is at least
7126 -- one missing return. What we do is to wrap the entire body of the
7127 -- function in a block:
7140 -- raise Program_Error;
7143 -- This approach is necessary because the raise must be signalled to the
7144 -- caller, not handled by any local handler (RM 6.4(11)).
7146 -- Note: we do not need to analyze the constructed sequence here, since
7147 -- it has no handler, and an attempt to analyze the handled statement
7148 -- sequence twice is risky in various ways (e.g. the issue of expanding
7149 -- cleanup actions twice).
7151 elsif Has_Missing_Return
(Spec_Id
) then
7153 Hloc
: constant Source_Ptr
:= Sloc
(H
);
7154 Blok
: constant Node_Id
:=
7155 Make_Block_Statement
(Hloc
,
7156 Handled_Statement_Sequence
=> H
);
7157 Rais
: constant Node_Id
:=
7158 Make_Raise_Program_Error
(Hloc
,
7159 Reason
=> PE_Missing_Return
);
7162 Set_Handled_Statement_Sequence
(N
,
7163 Make_Handled_Sequence_Of_Statements
(Hloc
,
7164 Statements
=> New_List
(Blok
, Rais
)));
7166 Push_Scope
(Spec_Id
);
7173 -- If subprogram contains a parameterless recursive call, then we may
7174 -- have an infinite recursion, so see if we can generate code to check
7175 -- for this possibility if storage checks are not suppressed.
7177 if Ekind
(Spec_Id
) = E_Procedure
7178 and then Has_Recursive_Call
(Spec_Id
)
7179 and then not Storage_Checks_Suppressed
(Spec_Id
)
7181 Detect_Infinite_Recursion
(N
, Spec_Id
);
7184 -- Set to encode entity names in package body before gigi is called
7186 Qualify_Entity_Names
(N
);
7187 end Expand_N_Subprogram_Body
;
7189 -----------------------------------
7190 -- Expand_N_Subprogram_Body_Stub --
7191 -----------------------------------
7193 procedure Expand_N_Subprogram_Body_Stub
(N
: Node_Id
) is
7195 if Present
(Corresponding_Body
(N
)) then
7196 Expand_N_Subprogram_Body
(
7197 Unit_Declaration_Node
(Corresponding_Body
(N
)));
7199 end Expand_N_Subprogram_Body_Stub
;
7201 -------------------------------------
7202 -- Expand_N_Subprogram_Declaration --
7203 -------------------------------------
7205 -- If the declaration appears within a protected body, it is a private
7206 -- operation of the protected type. We must create the corresponding
7207 -- protected subprogram an associated formals. For a normal protected
7208 -- operation, this is done when expanding the protected type declaration.
7210 -- If the declaration is for a null procedure, emit null body
7212 procedure Expand_N_Subprogram_Declaration
(N
: Node_Id
) is
7213 Loc
: constant Source_Ptr
:= Sloc
(N
);
7214 Subp
: constant Entity_Id
:= Defining_Entity
(N
);
7215 Scop
: constant Entity_Id
:= Scope
(Subp
);
7216 Prot_Decl
: Node_Id
;
7218 Prot_Id
: Entity_Id
;
7221 -- In SPARK, subprogram declarations are only allowed in package
7224 if Nkind
(Parent
(N
)) /= N_Package_Specification
then
7225 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
7226 Check_SPARK_Restriction
7227 ("subprogram declaration is not a library item", N
);
7229 elsif Present
(Next
(N
))
7230 and then Nkind
(Next
(N
)) = N_Pragma
7231 and then Get_Pragma_Id
(Pragma_Name
(Next
(N
))) = Pragma_Import
7233 -- In SPARK, subprogram declarations are also permitted in
7234 -- declarative parts when immediately followed by a corresponding
7235 -- pragma Import. We only check here that there is some pragma
7240 Check_SPARK_Restriction
7241 ("subprogram declaration is not allowed here", N
);
7245 -- Deal with case of protected subprogram. Do not generate protected
7246 -- operation if operation is flagged as eliminated.
7248 if Is_List_Member
(N
)
7249 and then Present
(Parent
(List_Containing
(N
)))
7250 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
7251 and then Is_Protected_Type
(Scop
)
7253 if No
(Protected_Body_Subprogram
(Subp
))
7254 and then not Is_Eliminated
(Subp
)
7257 Make_Subprogram_Declaration
(Loc
,
7259 Build_Protected_Sub_Specification
7260 (N
, Scop
, Unprotected_Mode
));
7262 -- The protected subprogram is declared outside of the protected
7263 -- body. Given that the body has frozen all entities so far, we
7264 -- analyze the subprogram and perform freezing actions explicitly.
7265 -- including the generation of an explicit freeze node, to ensure
7266 -- that gigi has the proper order of elaboration.
7267 -- If the body is a subunit, the insertion point is before the
7268 -- stub in the parent.
7270 Prot_Bod
:= Parent
(List_Containing
(N
));
7272 if Nkind
(Parent
(Prot_Bod
)) = N_Subunit
then
7273 Prot_Bod
:= Corresponding_Stub
(Parent
(Prot_Bod
));
7276 Insert_Before
(Prot_Bod
, Prot_Decl
);
7277 Prot_Id
:= Defining_Unit_Name
(Specification
(Prot_Decl
));
7278 Set_Has_Delayed_Freeze
(Prot_Id
);
7280 Push_Scope
(Scope
(Scop
));
7281 Analyze
(Prot_Decl
);
7282 Freeze_Before
(N
, Prot_Id
);
7283 Set_Protected_Body_Subprogram
(Subp
, Prot_Id
);
7285 -- Create protected operation as well. Even though the operation
7286 -- is only accessible within the body, it is possible to make it
7287 -- available outside of the protected object by using 'Access to
7288 -- provide a callback, so build protected version in all cases.
7291 Make_Subprogram_Declaration
(Loc
,
7293 Build_Protected_Sub_Specification
(N
, Scop
, Protected_Mode
));
7294 Insert_Before
(Prot_Bod
, Prot_Decl
);
7295 Analyze
(Prot_Decl
);
7300 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
7301 -- cases this is superfluous because calls to it will be automatically
7302 -- inlined, but we definitely need the body if preconditions for the
7303 -- procedure are present.
7305 elsif Nkind
(Specification
(N
)) = N_Procedure_Specification
7306 and then Null_Present
(Specification
(N
))
7309 Bod
: constant Node_Id
:= Body_To_Inline
(N
);
7312 Set_Has_Completion
(Subp
, False);
7313 Append_Freeze_Action
(Subp
, Bod
);
7315 -- The body now contains raise statements, so calls to it will
7318 Set_Is_Inlined
(Subp
, False);
7321 end Expand_N_Subprogram_Declaration
;
7323 --------------------------------
7324 -- Expand_Non_Function_Return --
7325 --------------------------------
7327 procedure Expand_Non_Function_Return
(N
: Node_Id
) is
7328 pragma Assert
(No
(Expression
(N
)));
7330 Loc
: constant Source_Ptr
:= Sloc
(N
);
7331 Scope_Id
: Entity_Id
:=
7332 Return_Applies_To
(Return_Statement_Entity
(N
));
7333 Kind
: constant Entity_Kind
:= Ekind
(Scope_Id
);
7336 Goto_Stat
: Node_Id
;
7340 -- Call _Postconditions procedure if procedure with active
7341 -- postconditions. Here, we use the Postcondition_Proc attribute,
7342 -- which is needed for implicitly-generated returns. Functions
7343 -- never have implicitly-generated returns, and there's no
7344 -- room for Postcondition_Proc in E_Function, so we look up the
7345 -- identifier Name_uPostconditions for function returns (see
7346 -- Expand_Simple_Function_Return).
7348 if Ekind
(Scope_Id
) = E_Procedure
7349 and then Has_Postconditions
(Scope_Id
)
7351 pragma Assert
(Present
(Postcondition_Proc
(Scope_Id
)));
7353 Make_Procedure_Call_Statement
(Loc
,
7354 Name
=> New_Reference_To
(Postcondition_Proc
(Scope_Id
), Loc
)));
7357 -- If it is a return from a procedure do no extra steps
7359 if Kind
= E_Procedure
or else Kind
= E_Generic_Procedure
then
7362 -- If it is a nested return within an extended one, replace it with a
7363 -- return of the previously declared return object.
7365 elsif Kind
= E_Return_Statement
then
7367 Make_Simple_Return_Statement
(Loc
,
7369 New_Occurrence_Of
(First_Entity
(Scope_Id
), Loc
)));
7370 Set_Comes_From_Extended_Return_Statement
(N
);
7371 Set_Return_Statement_Entity
(N
, Scope_Id
);
7372 Expand_Simple_Function_Return
(N
);
7376 pragma Assert
(Is_Entry
(Scope_Id
));
7378 -- Look at the enclosing block to see whether the return is from an
7379 -- accept statement or an entry body.
7381 for J
in reverse 0 .. Scope_Stack
.Last
loop
7382 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
7383 exit when Is_Concurrent_Type
(Scope_Id
);
7386 -- If it is a return from accept statement it is expanded as call to
7387 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
7389 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
7390 -- Expand_N_Accept_Alternative in exp_ch9.adb)
7392 if Is_Task_Type
(Scope_Id
) then
7395 Make_Procedure_Call_Statement
(Loc
,
7396 Name
=> New_Reference_To
(RTE
(RE_Complete_Rendezvous
), Loc
));
7397 Insert_Before
(N
, Call
);
7398 -- why not insert actions here???
7401 Acc_Stat
:= Parent
(N
);
7402 while Nkind
(Acc_Stat
) /= N_Accept_Statement
loop
7403 Acc_Stat
:= Parent
(Acc_Stat
);
7406 Lab_Node
:= Last
(Statements
7407 (Handled_Statement_Sequence
(Acc_Stat
)));
7409 Goto_Stat
:= Make_Goto_Statement
(Loc
,
7410 Name
=> New_Occurrence_Of
7411 (Entity
(Identifier
(Lab_Node
)), Loc
));
7413 Set_Analyzed
(Goto_Stat
);
7415 Rewrite
(N
, Goto_Stat
);
7418 -- If it is a return from an entry body, put a Complete_Entry_Body call
7419 -- in front of the return.
7421 elsif Is_Protected_Type
(Scope_Id
) then
7423 Make_Procedure_Call_Statement
(Loc
,
7425 New_Reference_To
(RTE
(RE_Complete_Entry_Body
), Loc
),
7426 Parameter_Associations
=> New_List
(
7427 Make_Attribute_Reference
(Loc
,
7430 (Find_Protection_Object
(Current_Scope
), Loc
),
7431 Attribute_Name
=> Name_Unchecked_Access
)));
7433 Insert_Before
(N
, Call
);
7436 end Expand_Non_Function_Return
;
7438 ---------------------------------------
7439 -- Expand_Protected_Object_Reference --
7440 ---------------------------------------
7442 function Expand_Protected_Object_Reference
7444 Scop
: Entity_Id
) return Node_Id
7446 Loc
: constant Source_Ptr
:= Sloc
(N
);
7453 Rec
:= Make_Identifier
(Loc
, Name_uObject
);
7454 Set_Etype
(Rec
, Corresponding_Record_Type
(Scop
));
7456 -- Find enclosing protected operation, and retrieve its first parameter,
7457 -- which denotes the enclosing protected object. If the enclosing
7458 -- operation is an entry, we are immediately within the protected body,
7459 -- and we can retrieve the object from the service entries procedure. A
7460 -- barrier function has the same signature as an entry. A barrier
7461 -- function is compiled within the protected object, but unlike
7462 -- protected operations its never needs locks, so that its protected
7463 -- body subprogram points to itself.
7465 Proc
:= Current_Scope
;
7466 while Present
(Proc
)
7467 and then Scope
(Proc
) /= Scop
7469 Proc
:= Scope
(Proc
);
7472 Corr
:= Protected_Body_Subprogram
(Proc
);
7476 -- Previous error left expansion incomplete.
7477 -- Nothing to do on this call.
7484 (First
(Parameter_Specifications
(Parent
(Corr
))));
7486 if Is_Subprogram
(Proc
)
7487 and then Proc
/= Corr
7489 -- Protected function or procedure
7491 Set_Entity
(Rec
, Param
);
7493 -- Rec is a reference to an entity which will not be in scope when
7494 -- the call is reanalyzed, and needs no further analysis.
7499 -- Entry or barrier function for entry body. The first parameter of
7500 -- the entry body procedure is pointer to the object. We create a
7501 -- local variable of the proper type, duplicating what is done to
7502 -- define _object later on.
7506 Obj_Ptr
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
7510 Make_Full_Type_Declaration
(Loc
,
7511 Defining_Identifier
=> Obj_Ptr
,
7513 Make_Access_To_Object_Definition
(Loc
,
7514 Subtype_Indication
=>
7516 (Corresponding_Record_Type
(Scop
), Loc
))));
7518 Insert_Actions
(N
, Decls
);
7519 Freeze_Before
(N
, Obj_Ptr
);
7522 Make_Explicit_Dereference
(Loc
,
7524 Unchecked_Convert_To
(Obj_Ptr
,
7525 New_Occurrence_Of
(Param
, Loc
)));
7527 -- Analyze new actual. Other actuals in calls are already analyzed
7528 -- and the list of actuals is not reanalyzed after rewriting.
7530 Set_Parent
(Rec
, N
);
7536 end Expand_Protected_Object_Reference
;
7538 --------------------------------------
7539 -- Expand_Protected_Subprogram_Call --
7540 --------------------------------------
7542 procedure Expand_Protected_Subprogram_Call
7550 -- If the protected object is not an enclosing scope, this is an inter-
7551 -- object function call. Inter-object procedure calls are expanded by
7552 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
7553 -- subprogram being called is in the protected body being compiled, and
7554 -- if the protected object in the call is statically the enclosing type.
7555 -- The object may be an component of some other data structure, in which
7556 -- case this must be handled as an inter-object call.
7558 if not In_Open_Scopes
(Scop
)
7559 or else not Is_Entity_Name
(Name
(N
))
7561 if Nkind
(Name
(N
)) = N_Selected_Component
then
7562 Rec
:= Prefix
(Name
(N
));
7565 pragma Assert
(Nkind
(Name
(N
)) = N_Indexed_Component
);
7566 Rec
:= Prefix
(Prefix
(Name
(N
)));
7569 Build_Protected_Subprogram_Call
(N
,
7570 Name
=> New_Occurrence_Of
(Subp
, Sloc
(N
)),
7571 Rec
=> Convert_Concurrent
(Rec
, Etype
(Rec
)),
7575 Rec
:= Expand_Protected_Object_Reference
(N
, Scop
);
7581 Build_Protected_Subprogram_Call
(N
,
7588 -- If it is a function call it can appear in elaboration code and
7589 -- the called entity must be frozen here.
7591 if Ekind
(Subp
) = E_Function
then
7592 Freeze_Expression
(Name
(N
));
7595 -- Analyze and resolve the new call. The actuals have already been
7596 -- resolved, but expansion of a function call will add extra actuals
7597 -- if needed. Analysis of a procedure call already includes resolution.
7601 if Ekind
(Subp
) = E_Function
then
7602 Resolve
(N
, Etype
(Subp
));
7604 end Expand_Protected_Subprogram_Call
;
7606 --------------------------------------------
7607 -- Has_Unconstrained_Access_Discriminants --
7608 --------------------------------------------
7610 function Has_Unconstrained_Access_Discriminants
7611 (Subtyp
: Entity_Id
) return Boolean
7616 if Has_Discriminants
(Subtyp
)
7617 and then not Is_Constrained
(Subtyp
)
7619 Discr
:= First_Discriminant
(Subtyp
);
7620 while Present
(Discr
) loop
7621 if Ekind
(Etype
(Discr
)) = E_Anonymous_Access_Type
then
7625 Next_Discriminant
(Discr
);
7630 end Has_Unconstrained_Access_Discriminants
;
7632 -----------------------------------
7633 -- Expand_Simple_Function_Return --
7634 -----------------------------------
7636 -- The "simple" comes from the syntax rule simple_return_statement. The
7637 -- semantics are not at all simple!
7639 procedure Expand_Simple_Function_Return
(N
: Node_Id
) is
7640 Loc
: constant Source_Ptr
:= Sloc
(N
);
7642 Scope_Id
: constant Entity_Id
:=
7643 Return_Applies_To
(Return_Statement_Entity
(N
));
7644 -- The function we are returning from
7646 R_Type
: constant Entity_Id
:= Etype
(Scope_Id
);
7647 -- The result type of the function
7649 Utyp
: constant Entity_Id
:= Underlying_Type
(R_Type
);
7651 Exp
: constant Node_Id
:= Expression
(N
);
7652 pragma Assert
(Present
(Exp
));
7654 Exptyp
: constant Entity_Id
:= Etype
(Exp
);
7655 -- The type of the expression (not necessarily the same as R_Type)
7657 Subtype_Ind
: Node_Id
;
7658 -- If the result type of the function is class-wide and the expression
7659 -- has a specific type, then we use the expression's type as the type of
7660 -- the return object. In cases where the expression is an aggregate that
7661 -- is built in place, this avoids the need for an expensive conversion
7662 -- of the return object to the specific type on assignments to the
7663 -- individual components.
7666 if Is_Class_Wide_Type
(R_Type
)
7667 and then not Is_Class_Wide_Type
(Etype
(Exp
))
7669 Subtype_Ind
:= New_Occurrence_Of
(Etype
(Exp
), Loc
);
7671 Subtype_Ind
:= New_Occurrence_Of
(R_Type
, Loc
);
7674 -- For the case of a simple return that does not come from an extended
7675 -- return, in the case of Ada 2005 where we are returning a limited
7676 -- type, we rewrite "return <expression>;" to be:
7678 -- return _anon_ : <return_subtype> := <expression>
7680 -- The expansion produced by Expand_N_Extended_Return_Statement will
7681 -- contain simple return statements (for example, a block containing
7682 -- simple return of the return object), which brings us back here with
7683 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
7684 -- checking for a simple return that does not come from an extended
7685 -- return is to avoid this infinite recursion.
7687 -- The reason for this design is that for Ada 2005 limited returns, we
7688 -- need to reify the return object, so we can build it "in place", and
7689 -- we need a block statement to hang finalization and tasking stuff.
7691 -- ??? In order to avoid disruption, we avoid translating to extended
7692 -- return except in the cases where we really need to (Ada 2005 for
7693 -- inherently limited). We might prefer to do this translation in all
7694 -- cases (except perhaps for the case of Ada 95 inherently limited),
7695 -- in order to fully exercise the Expand_N_Extended_Return_Statement
7696 -- code. This would also allow us to do the build-in-place optimization
7697 -- for efficiency even in cases where it is semantically not required.
7699 -- As before, we check the type of the return expression rather than the
7700 -- return type of the function, because the latter may be a limited
7701 -- class-wide interface type, which is not a limited type, even though
7702 -- the type of the expression may be.
7704 if not Comes_From_Extended_Return_Statement
(N
)
7705 and then Is_Limited_View
(Etype
(Expression
(N
)))
7706 and then Ada_Version
>= Ada_2005
7707 and then not Debug_Flag_Dot_L
7709 -- The functionality of interface thunks is simple and it is always
7710 -- handled by means of simple return statements. This leaves their
7711 -- expansion simple and clean.
7713 and then not Is_Thunk
(Current_Scope
)
7716 Return_Object_Entity
: constant Entity_Id
:=
7717 Make_Temporary
(Loc
, 'R', Exp
);
7719 Obj_Decl
: constant Node_Id
:=
7720 Make_Object_Declaration
(Loc
,
7721 Defining_Identifier
=> Return_Object_Entity
,
7722 Object_Definition
=> Subtype_Ind
,
7725 Ext
: constant Node_Id
:=
7726 Make_Extended_Return_Statement
(Loc
,
7727 Return_Object_Declarations
=> New_List
(Obj_Decl
));
7728 -- Do not perform this high-level optimization if the result type
7729 -- is an interface because the "this" pointer must be displaced.
7738 -- Here we have a simple return statement that is part of the expansion
7739 -- of an extended return statement (either written by the user, or
7740 -- generated by the above code).
7742 -- Always normalize C/Fortran boolean result. This is not always needed,
7743 -- but it seems a good idea to minimize the passing around of non-
7744 -- normalized values, and in any case this handles the processing of
7745 -- barrier functions for protected types, which turn the condition into
7746 -- a return statement.
7748 if Is_Boolean_Type
(Exptyp
)
7749 and then Nonzero_Is_True
(Exptyp
)
7751 Adjust_Condition
(Exp
);
7752 Adjust_Result_Type
(Exp
, Exptyp
);
7755 -- Do validity check if enabled for returns
7757 if Validity_Checks_On
7758 and then Validity_Check_Returns
7763 -- Check the result expression of a scalar function against the subtype
7764 -- of the function by inserting a conversion. This conversion must
7765 -- eventually be performed for other classes of types, but for now it's
7766 -- only done for scalars.
7769 if Is_Scalar_Type
(Exptyp
) then
7770 Rewrite
(Exp
, Convert_To
(R_Type
, Exp
));
7772 -- The expression is resolved to ensure that the conversion gets
7773 -- expanded to generate a possible constraint check.
7775 Analyze_And_Resolve
(Exp
, R_Type
);
7778 -- Deal with returning variable length objects and controlled types
7780 -- Nothing to do if we are returning by reference, or this is not a
7781 -- type that requires special processing (indicated by the fact that
7782 -- it requires a cleanup scope for the secondary stack case).
7784 if Is_Limited_View
(Exptyp
)
7785 or else Is_Limited_Interface
(Exptyp
)
7789 -- No copy needed for thunks returning interface type objects since
7790 -- the object is returned by reference and the maximum functionality
7791 -- required is just to displace the pointer.
7793 elsif Is_Thunk
(Current_Scope
) and then Is_Interface
(Exptyp
) then
7796 elsif not Requires_Transient_Scope
(R_Type
) then
7798 -- Mutable records with no variable length components are not
7799 -- returned on the sec-stack, so we need to make sure that the
7800 -- backend will only copy back the size of the actual value, and not
7801 -- the maximum size. We create an actual subtype for this purpose.
7804 Ubt
: constant Entity_Id
:= Underlying_Type
(Base_Type
(Exptyp
));
7808 if Has_Discriminants
(Ubt
)
7809 and then not Is_Constrained
(Ubt
)
7810 and then not Has_Unchecked_Union
(Ubt
)
7812 Decl
:= Build_Actual_Subtype
(Ubt
, Exp
);
7813 Ent
:= Defining_Identifier
(Decl
);
7814 Insert_Action
(Exp
, Decl
);
7815 Rewrite
(Exp
, Unchecked_Convert_To
(Ent
, Exp
));
7816 Analyze_And_Resolve
(Exp
);
7820 -- Here if secondary stack is used
7823 -- Make sure that no surrounding block will reclaim the secondary
7824 -- stack on which we are going to put the result. Not only may this
7825 -- introduce secondary stack leaks but worse, if the reclamation is
7826 -- done too early, then the result we are returning may get
7833 while Ekind
(S
) = E_Block
or else Ekind
(S
) = E_Loop
loop
7834 Set_Sec_Stack_Needed_For_Return
(S
, True);
7835 S
:= Enclosing_Dynamic_Scope
(S
);
7839 -- Optimize the case where the result is a function call. In this
7840 -- case either the result is already on the secondary stack, or is
7841 -- already being returned with the stack pointer depressed and no
7842 -- further processing is required except to set the By_Ref flag
7843 -- to ensure that gigi does not attempt an extra unnecessary copy.
7844 -- (actually not just unnecessary but harmfully wrong in the case
7845 -- of a controlled type, where gigi does not know how to do a copy).
7846 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
7847 -- for array types if the constrained status of the target type is
7848 -- different from that of the expression.
7850 if Requires_Transient_Scope
(Exptyp
)
7852 (not Is_Array_Type
(Exptyp
)
7853 or else Is_Constrained
(Exptyp
) = Is_Constrained
(R_Type
)
7854 or else CW_Or_Has_Controlled_Part
(Utyp
))
7855 and then Nkind
(Exp
) = N_Function_Call
7859 -- Remove side effects from the expression now so that other parts
7860 -- of the expander do not have to reanalyze this node without this
7863 Rewrite
(Exp
, Duplicate_Subexpr_No_Checks
(Exp
));
7865 -- For controlled types, do the allocation on the secondary stack
7866 -- manually in order to call adjust at the right time:
7868 -- type Anon1 is access R_Type;
7869 -- for Anon1'Storage_pool use ss_pool;
7870 -- Anon2 : anon1 := new R_Type'(expr);
7871 -- return Anon2.all;
7873 -- We do the same for classwide types that are not potentially
7874 -- controlled (by the virtue of restriction No_Finalization) because
7875 -- gigi is not able to properly allocate class-wide types.
7877 elsif CW_Or_Has_Controlled_Part
(Utyp
) then
7879 Loc
: constant Source_Ptr
:= Sloc
(N
);
7880 Acc_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
7881 Alloc_Node
: Node_Id
;
7885 Set_Ekind
(Acc_Typ
, E_Access_Type
);
7887 Set_Associated_Storage_Pool
(Acc_Typ
, RTE
(RE_SS_Pool
));
7889 -- This is an allocator for the secondary stack, and it's fine
7890 -- to have Comes_From_Source set False on it, as gigi knows not
7891 -- to flag it as a violation of No_Implicit_Heap_Allocations.
7894 Make_Allocator
(Loc
,
7896 Make_Qualified_Expression
(Loc
,
7897 Subtype_Mark
=> New_Reference_To
(Etype
(Exp
), Loc
),
7898 Expression
=> Relocate_Node
(Exp
)));
7900 -- We do not want discriminant checks on the declaration,
7901 -- given that it gets its value from the allocator.
7903 Set_No_Initialization
(Alloc_Node
);
7905 Temp
:= Make_Temporary
(Loc
, 'R', Alloc_Node
);
7907 Insert_List_Before_And_Analyze
(N
, New_List
(
7908 Make_Full_Type_Declaration
(Loc
,
7909 Defining_Identifier
=> Acc_Typ
,
7911 Make_Access_To_Object_Definition
(Loc
,
7912 Subtype_Indication
=> Subtype_Ind
)),
7914 Make_Object_Declaration
(Loc
,
7915 Defining_Identifier
=> Temp
,
7916 Object_Definition
=> New_Reference_To
(Acc_Typ
, Loc
),
7917 Expression
=> Alloc_Node
)));
7920 Make_Explicit_Dereference
(Loc
,
7921 Prefix
=> New_Reference_To
(Temp
, Loc
)));
7923 -- Ada 2005 (AI-251): If the type of the returned object is
7924 -- an interface then add an implicit type conversion to force
7925 -- displacement of the "this" pointer.
7927 if Is_Interface
(R_Type
) then
7928 Rewrite
(Exp
, Convert_To
(R_Type
, Relocate_Node
(Exp
)));
7931 Analyze_And_Resolve
(Exp
, R_Type
);
7934 -- Otherwise use the gigi mechanism to allocate result on the
7938 Check_Restriction
(No_Secondary_Stack
, N
);
7939 Set_Storage_Pool
(N
, RTE
(RE_SS_Pool
));
7941 -- If we are generating code for the VM do not use
7942 -- SS_Allocate since everything is heap-allocated anyway.
7944 if VM_Target
= No_VM
then
7945 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
7950 -- Implement the rules of 6.5(8-10), which require a tag check in
7951 -- the case of a limited tagged return type, and tag reassignment for
7952 -- nonlimited tagged results. These actions are needed when the return
7953 -- type is a specific tagged type and the result expression is a
7954 -- conversion or a formal parameter, because in that case the tag of
7955 -- the expression might differ from the tag of the specific result type.
7957 if Is_Tagged_Type
(Utyp
)
7958 and then not Is_Class_Wide_Type
(Utyp
)
7959 and then (Nkind_In
(Exp
, N_Type_Conversion
,
7960 N_Unchecked_Type_Conversion
)
7961 or else (Is_Entity_Name
(Exp
)
7962 and then Ekind
(Entity
(Exp
)) in Formal_Kind
))
7964 -- When the return type is limited, perform a check that the tag of
7965 -- the result is the same as the tag of the return type.
7967 if Is_Limited_Type
(R_Type
) then
7969 Make_Raise_Constraint_Error
(Loc
,
7973 Make_Selected_Component
(Loc
,
7974 Prefix
=> Duplicate_Subexpr
(Exp
),
7975 Selector_Name
=> Make_Identifier
(Loc
, Name_uTag
)),
7977 Make_Attribute_Reference
(Loc
,
7979 New_Occurrence_Of
(Base_Type
(Utyp
), Loc
),
7980 Attribute_Name
=> Name_Tag
)),
7981 Reason
=> CE_Tag_Check_Failed
));
7983 -- If the result type is a specific nonlimited tagged type, then we
7984 -- have to ensure that the tag of the result is that of the result
7985 -- type. This is handled by making a copy of the expression in
7986 -- the case where it might have a different tag, namely when the
7987 -- expression is a conversion or a formal parameter. We create a new
7988 -- object of the result type and initialize it from the expression,
7989 -- which will implicitly force the tag to be set appropriately.
7993 ExpR
: constant Node_Id
:= Relocate_Node
(Exp
);
7994 Result_Id
: constant Entity_Id
:=
7995 Make_Temporary
(Loc
, 'R', ExpR
);
7996 Result_Exp
: constant Node_Id
:=
7997 New_Reference_To
(Result_Id
, Loc
);
7998 Result_Obj
: constant Node_Id
:=
7999 Make_Object_Declaration
(Loc
,
8000 Defining_Identifier
=> Result_Id
,
8001 Object_Definition
=>
8002 New_Reference_To
(R_Type
, Loc
),
8003 Constant_Present
=> True,
8004 Expression
=> ExpR
);
8007 Set_Assignment_OK
(Result_Obj
);
8008 Insert_Action
(Exp
, Result_Obj
);
8010 Rewrite
(Exp
, Result_Exp
);
8011 Analyze_And_Resolve
(Exp
, R_Type
);
8015 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
8016 -- a check that the level of the return expression's underlying type
8017 -- is not deeper than the level of the master enclosing the function.
8018 -- Always generate the check when the type of the return expression
8019 -- is class-wide, when it's a type conversion, or when it's a formal
8020 -- parameter. Otherwise, suppress the check in the case where the
8021 -- return expression has a specific type whose level is known not to
8022 -- be statically deeper than the function's result type.
8024 -- No runtime check needed in interface thunks since it is performed
8025 -- by the target primitive associated with the thunk.
8027 -- Note: accessibility check is skipped in the VM case, since there
8028 -- does not seem to be any practical way to implement this check.
8030 elsif Ada_Version
>= Ada_2005
8031 and then Tagged_Type_Expansion
8032 and then Is_Class_Wide_Type
(R_Type
)
8033 and then not Is_Thunk
(Current_Scope
)
8034 and then not Scope_Suppress
.Suppress
(Accessibility_Check
)
8036 (Is_Class_Wide_Type
(Etype
(Exp
))
8037 or else Nkind_In
(Exp
, N_Type_Conversion
,
8038 N_Unchecked_Type_Conversion
)
8039 or else (Is_Entity_Name
(Exp
)
8040 and then Ekind
(Entity
(Exp
)) in Formal_Kind
)
8041 or else Scope_Depth
(Enclosing_Dynamic_Scope
(Etype
(Exp
))) >
8042 Scope_Depth
(Enclosing_Dynamic_Scope
(Scope_Id
)))
8048 -- Ada 2005 (AI-251): In class-wide interface objects we displace
8049 -- "this" to reference the base of the object. This is required to
8050 -- get access to the TSD of the object.
8052 if Is_Class_Wide_Type
(Etype
(Exp
))
8053 and then Is_Interface
(Etype
(Exp
))
8054 and then Nkind
(Exp
) = N_Explicit_Dereference
8057 Make_Explicit_Dereference
(Loc
,
8059 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
8060 Make_Function_Call
(Loc
,
8062 New_Reference_To
(RTE
(RE_Base_Address
), Loc
),
8063 Parameter_Associations
=> New_List
(
8064 Unchecked_Convert_To
(RTE
(RE_Address
),
8065 Duplicate_Subexpr
(Prefix
(Exp
)))))));
8068 Make_Attribute_Reference
(Loc
,
8069 Prefix
=> Duplicate_Subexpr
(Exp
),
8070 Attribute_Name
=> Name_Tag
);
8074 Make_Raise_Program_Error
(Loc
,
8077 Left_Opnd
=> Build_Get_Access_Level
(Loc
, Tag_Node
),
8079 Make_Integer_Literal
(Loc
,
8080 Scope_Depth
(Enclosing_Dynamic_Scope
(Scope_Id
)))),
8081 Reason
=> PE_Accessibility_Check_Failed
));
8084 -- AI05-0073: If function has a controlling access result, check that
8085 -- the tag of the return value, if it is not null, matches designated
8086 -- type of return type.
8088 -- The return expression is referenced twice in the code below, so it
8089 -- must be made free of side effects. Given that different compilers
8090 -- may evaluate these parameters in different order, both occurrences
8093 elsif Ekind
(R_Type
) = E_Anonymous_Access_Type
8094 and then Has_Controlling_Result
(Scope_Id
)
8097 Make_Raise_Constraint_Error
(Loc
,
8102 Left_Opnd
=> Duplicate_Subexpr
(Exp
),
8103 Right_Opnd
=> Make_Null
(Loc
)),
8105 Right_Opnd
=> Make_Op_Ne
(Loc
,
8107 Make_Selected_Component
(Loc
,
8108 Prefix
=> Duplicate_Subexpr
(Exp
),
8109 Selector_Name
=> Make_Identifier
(Loc
, Name_uTag
)),
8112 Make_Attribute_Reference
(Loc
,
8114 New_Occurrence_Of
(Designated_Type
(R_Type
), Loc
),
8115 Attribute_Name
=> Name_Tag
))),
8117 Reason
=> CE_Tag_Check_Failed
),
8118 Suppress
=> All_Checks
);
8121 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
8122 -- ensure that the function result does not outlive an
8123 -- object designated by one of it discriminants.
8125 if Present
(Extra_Accessibility_Of_Result
(Scope_Id
))
8126 and then Has_Unconstrained_Access_Discriminants
(R_Type
)
8129 Discrim_Source
: Node_Id
;
8131 procedure Check_Against_Result_Level
(Level
: Node_Id
);
8132 -- Check the given accessibility level against the level
8133 -- determined by the point of call. (AI05-0234).
8135 --------------------------------
8136 -- Check_Against_Result_Level --
8137 --------------------------------
8139 procedure Check_Against_Result_Level
(Level
: Node_Id
) is
8142 Make_Raise_Program_Error
(Loc
,
8148 (Extra_Accessibility_Of_Result
(Scope_Id
), Loc
)),
8149 Reason
=> PE_Accessibility_Check_Failed
));
8150 end Check_Against_Result_Level
;
8153 Discrim_Source
:= Exp
;
8154 while Nkind
(Discrim_Source
) = N_Qualified_Expression
loop
8155 Discrim_Source
:= Expression
(Discrim_Source
);
8158 if Nkind
(Discrim_Source
) = N_Identifier
8159 and then Is_Return_Object
(Entity
(Discrim_Source
))
8161 Discrim_Source
:= Entity
(Discrim_Source
);
8163 if Is_Constrained
(Etype
(Discrim_Source
)) then
8164 Discrim_Source
:= Etype
(Discrim_Source
);
8166 Discrim_Source
:= Expression
(Parent
(Discrim_Source
));
8169 elsif Nkind
(Discrim_Source
) = N_Identifier
8170 and then Nkind_In
(Original_Node
(Discrim_Source
),
8171 N_Aggregate
, N_Extension_Aggregate
)
8173 Discrim_Source
:= Original_Node
(Discrim_Source
);
8175 elsif Nkind
(Discrim_Source
) = N_Explicit_Dereference
and then
8176 Nkind
(Original_Node
(Discrim_Source
)) = N_Function_Call
8178 Discrim_Source
:= Original_Node
(Discrim_Source
);
8181 while Nkind_In
(Discrim_Source
, N_Qualified_Expression
,
8183 N_Unchecked_Type_Conversion
)
8185 Discrim_Source
:= Expression
(Discrim_Source
);
8188 case Nkind
(Discrim_Source
) is
8189 when N_Defining_Identifier
=>
8191 pragma Assert
(Is_Composite_Type
(Discrim_Source
)
8192 and then Has_Discriminants
(Discrim_Source
)
8193 and then Is_Constrained
(Discrim_Source
));
8196 Discrim
: Entity_Id
:=
8197 First_Discriminant
(Base_Type
(R_Type
));
8198 Disc_Elmt
: Elmt_Id
:=
8199 First_Elmt
(Discriminant_Constraint
8203 if Ekind
(Etype
(Discrim
)) =
8204 E_Anonymous_Access_Type
8206 Check_Against_Result_Level
8207 (Dynamic_Accessibility_Level
(Node
(Disc_Elmt
)));
8210 Next_Elmt
(Disc_Elmt
);
8211 Next_Discriminant
(Discrim
);
8212 exit when not Present
(Discrim
);
8216 when N_Aggregate | N_Extension_Aggregate
=>
8218 -- Unimplemented: extension aggregate case where discrims
8219 -- come from ancestor part, not extension part.
8222 Discrim
: Entity_Id
:=
8223 First_Discriminant
(Base_Type
(R_Type
));
8225 Disc_Exp
: Node_Id
:= Empty
;
8227 Positionals_Exhausted
8228 : Boolean := not Present
(Expressions
8231 function Associated_Expr
8232 (Comp_Id
: Entity_Id
;
8233 Associations
: List_Id
) return Node_Id
;
8235 -- Given a component and a component associations list,
8236 -- locate the expression for that component; returns
8237 -- Empty if no such expression is found.
8239 ---------------------
8240 -- Associated_Expr --
8241 ---------------------
8243 function Associated_Expr
8244 (Comp_Id
: Entity_Id
;
8245 Associations
: List_Id
) return Node_Id
8251 -- Simple linear search seems ok here
8253 Assoc
:= First
(Associations
);
8254 while Present
(Assoc
) loop
8255 Choice
:= First
(Choices
(Assoc
));
8256 while Present
(Choice
) loop
8257 if (Nkind
(Choice
) = N_Identifier
8258 and then Chars
(Choice
) = Chars
(Comp_Id
))
8259 or else (Nkind
(Choice
) = N_Others_Choice
)
8261 return Expression
(Assoc
);
8271 end Associated_Expr
;
8273 -- Start of processing for Expand_Simple_Function_Return
8276 if not Positionals_Exhausted
then
8277 Disc_Exp
:= First
(Expressions
(Discrim_Source
));
8281 if Positionals_Exhausted
then
8285 Component_Associations
(Discrim_Source
));
8288 if Ekind
(Etype
(Discrim
)) =
8289 E_Anonymous_Access_Type
8291 Check_Against_Result_Level
8292 (Dynamic_Accessibility_Level
(Disc_Exp
));
8295 Next_Discriminant
(Discrim
);
8296 exit when not Present
(Discrim
);
8298 if not Positionals_Exhausted
then
8300 Positionals_Exhausted
:= not Present
(Disc_Exp
);
8305 when N_Function_Call
=>
8307 -- No check needed (check performed by callee)
8314 Level
: constant Node_Id
:=
8315 Make_Integer_Literal
(Loc
,
8316 Object_Access_Level
(Discrim_Source
));
8319 -- Unimplemented: check for name prefix that includes
8320 -- a dereference of an access value with a dynamic
8321 -- accessibility level (e.g., an access param or a
8322 -- saooaaat) and use dynamic level in that case. For
8324 -- return Access_Param.all(Some_Index).Some_Component;
8327 Set_Etype
(Level
, Standard_Natural
);
8328 Check_Against_Result_Level
(Level
);
8335 -- If we are returning an object that may not be bit-aligned, then copy
8336 -- the value into a temporary first. This copy may need to expand to a
8337 -- loop of component operations.
8339 if Is_Possibly_Unaligned_Slice
(Exp
)
8340 or else Is_Possibly_Unaligned_Object
(Exp
)
8343 ExpR
: constant Node_Id
:= Relocate_Node
(Exp
);
8344 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', ExpR
);
8347 Make_Object_Declaration
(Loc
,
8348 Defining_Identifier
=> Tnn
,
8349 Constant_Present
=> True,
8350 Object_Definition
=> New_Occurrence_Of
(R_Type
, Loc
),
8351 Expression
=> ExpR
),
8352 Suppress
=> All_Checks
);
8353 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
8357 -- Generate call to postcondition checks if they are present
8359 if Ekind
(Scope_Id
) = E_Function
8360 and then Has_Postconditions
(Scope_Id
)
8362 -- We are going to reference the returned value twice in this case,
8363 -- once in the call to _Postconditions, and once in the actual return
8364 -- statement, but we can't have side effects happening twice, and in
8365 -- any case for efficiency we don't want to do the computation twice.
8367 -- If the returned expression is an entity name, we don't need to
8368 -- worry since it is efficient and safe to reference it twice, that's
8369 -- also true for literals other than string literals, and for the
8370 -- case of X.all where X is an entity name.
8372 if Is_Entity_Name
(Exp
)
8373 or else Nkind_In
(Exp
, N_Character_Literal
,
8376 or else (Nkind
(Exp
) = N_Explicit_Dereference
8377 and then Is_Entity_Name
(Prefix
(Exp
)))
8381 -- Otherwise we are going to need a temporary to capture the value
8385 ExpR
: Node_Id
:= Relocate_Node
(Exp
);
8386 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', ExpR
);
8389 -- In the case of discriminated objects, we have created a
8390 -- constrained subtype above, and used the underlying type.
8391 -- This transformation is post-analysis and harmless, except
8392 -- that now the call to the post-condition will be analyzed and
8393 -- type kinds have to match.
8395 if Nkind
(ExpR
) = N_Unchecked_Type_Conversion
8397 Is_Private_Type
(R_Type
) /= Is_Private_Type
(Etype
(ExpR
))
8399 ExpR
:= Expression
(ExpR
);
8402 -- For a complex expression of an elementary type, capture
8403 -- value in the temporary and use it as the reference.
8405 if Is_Elementary_Type
(R_Type
) then
8407 Make_Object_Declaration
(Loc
,
8408 Defining_Identifier
=> Tnn
,
8409 Constant_Present
=> True,
8410 Object_Definition
=> New_Occurrence_Of
(R_Type
, Loc
),
8411 Expression
=> ExpR
),
8412 Suppress
=> All_Checks
);
8414 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
8416 -- If we have something we can rename, generate a renaming of
8417 -- the object and replace the expression with a reference
8419 elsif Is_Object_Reference
(Exp
) then
8421 Make_Object_Renaming_Declaration
(Loc
,
8422 Defining_Identifier
=> Tnn
,
8423 Subtype_Mark
=> New_Occurrence_Of
(R_Type
, Loc
),
8425 Suppress
=> All_Checks
);
8427 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
8429 -- Otherwise we have something like a string literal or an
8430 -- aggregate. We could copy the value, but that would be
8431 -- inefficient. Instead we make a reference to the value and
8432 -- capture this reference with a renaming, the expression is
8433 -- then replaced by a dereference of this renaming.
8436 -- For now, copy the value, since the code below does not
8437 -- seem to work correctly ???
8440 Make_Object_Declaration
(Loc
,
8441 Defining_Identifier
=> Tnn
,
8442 Constant_Present
=> True,
8443 Object_Definition
=> New_Occurrence_Of
(R_Type
, Loc
),
8444 Expression
=> Relocate_Node
(Exp
)),
8445 Suppress
=> All_Checks
);
8447 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
8449 -- Insert_Action (Exp,
8450 -- Make_Object_Renaming_Declaration (Loc,
8451 -- Defining_Identifier => Tnn,
8452 -- Access_Definition =>
8453 -- Make_Access_Definition (Loc,
8454 -- All_Present => True,
8455 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
8457 -- Make_Reference (Loc,
8458 -- Prefix => Relocate_Node (Exp))),
8459 -- Suppress => All_Checks);
8462 -- Make_Explicit_Dereference (Loc,
8463 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
8468 -- Generate call to _postconditions
8471 Make_Procedure_Call_Statement
(Loc
,
8472 Name
=> Make_Identifier
(Loc
, Name_uPostconditions
),
8473 Parameter_Associations
=> New_List
(Duplicate_Subexpr
(Exp
))));
8476 -- Ada 2005 (AI-251): If this return statement corresponds with an
8477 -- simple return statement associated with an extended return statement
8478 -- and the type of the returned object is an interface then generate an
8479 -- implicit conversion to force displacement of the "this" pointer.
8481 if Ada_Version
>= Ada_2005
8482 and then Comes_From_Extended_Return_Statement
(N
)
8483 and then Nkind
(Expression
(N
)) = N_Identifier
8484 and then Is_Interface
(Utyp
)
8485 and then Utyp
/= Underlying_Type
(Exptyp
)
8487 Rewrite
(Exp
, Convert_To
(Utyp
, Relocate_Node
(Exp
)));
8488 Analyze_And_Resolve
(Exp
);
8490 end Expand_Simple_Function_Return
;
8492 --------------------------------
8493 -- Expand_Subprogram_Contract --
8494 --------------------------------
8496 procedure Expand_Subprogram_Contract
8498 Spec_Id
: Entity_Id
;
8499 Body_Id
: Entity_Id
)
8501 procedure Add_Invariant_And_Predicate_Checks
8502 (Subp_Id
: Entity_Id
;
8503 Stmts
: in out List_Id
;
8504 Result
: out Node_Id
);
8505 -- Process the result of function Subp_Id (if applicable) and all its
8506 -- formals. Add invariant and predicate checks where applicable. The
8507 -- routine appends all the checks to list Stmts. If Subp_Id denotes a
8508 -- function, Result contains the entity of parameter _Result, to be
8509 -- used in the creation of procedure _Postconditions.
8511 procedure Append_Enabled_Item
(Item
: Node_Id
; List
: in out List_Id
);
8512 -- Append a node to a list. If there is no list, create a new one. When
8513 -- the item denotes a pragma, it is added to the list only when it is
8516 procedure Build_Postconditions_Procedure
8517 (Subp_Id
: Entity_Id
;
8519 Result
: Entity_Id
);
8520 -- Create the body of procedure _Postconditions which handles various
8521 -- assertion actions on exit from subprogram Subp_Id. Stmts is the list
8522 -- of statements to be checked on exit. Parameter Result is the entity
8523 -- of parameter _Result when Subp_Id denotes a function.
8525 function Build_Pragma_Check_Equivalent
8527 Subp_Id
: Entity_Id
:= Empty
;
8528 Inher_Id
: Entity_Id
:= Empty
) return Node_Id
;
8529 -- Transform a [refined] pre- or postcondition denoted by Prag into an
8530 -- equivalent pragma Check. When the pre- or postcondition is inherited,
8531 -- the routine corrects the references of all formals of Inher_Id to
8532 -- point to the formals of Subp_Id.
8534 procedure Collect_Body_Postconditions
(Stmts
: in out List_Id
);
8535 -- Process all postconditions found in the declarations of the body. The
8536 -- routine appends the pragma Check equivalents to list Stmts.
8538 procedure Collect_Spec_Postconditions
8539 (Subp_Id
: Entity_Id
;
8540 Stmts
: in out List_Id
);
8541 -- Process all [inherited] postconditions of subprogram spec Subp_Id.
8542 -- The routine appends the pragma Check equivalents to list Stmts.
8544 procedure Collect_Spec_Preconditions
(Subp_Id
: Entity_Id
);
8545 -- Process all [inherited] preconditions of subprogram spec Subp_Id. The
8546 -- routine prepends the pragma Check equivalents to the declarations of
8549 procedure Prepend_To_Declarations
(Item
: Node_Id
);
8550 -- Prepend a single item to the declarations of the subprogram body
8552 procedure Process_Contract_Cases
8553 (Subp_Id
: Entity_Id
;
8554 Stmts
: in out List_Id
);
8555 -- Process pragma Contract_Cases of subprogram spec Subp_Id. The routine
8556 -- appends the expanded code to list Stmts.
8558 ----------------------------------------
8559 -- Add_Invariant_And_Predicate_Checks --
8560 ----------------------------------------
8562 procedure Add_Invariant_And_Predicate_Checks
8563 (Subp_Id
: Entity_Id
;
8564 Stmts
: in out List_Id
;
8565 Result
: out Node_Id
)
8567 procedure Add_Invariant_Access_Checks
(Id
: Entity_Id
);
8568 -- Id denotes the return value of a function or a formal parameter.
8569 -- Add an invariant check if the type of Id is access to a type with
8570 -- invariants. The routine appends the generated code to Stmts.
8572 function Invariant_Checks_OK
(Typ
: Entity_Id
) return Boolean;
8573 -- Determine whether type Typ can benefit from invariant checks. To
8574 -- qualify, the type must have a non-null invariant procedure and
8575 -- subprogram Subp_Id must appear visible from the point of view of
8578 function Predicate_Checks_OK
(Typ
: Entity_Id
) return Boolean;
8579 -- Determine whether type Typ can benefit from predicate checks. To
8580 -- qualify, the type must have at least one checked predicate.
8582 ---------------------------------
8583 -- Add_Invariant_Access_Checks --
8584 ---------------------------------
8586 procedure Add_Invariant_Access_Checks
(Id
: Entity_Id
) is
8587 Loc
: constant Source_Ptr
:= Sloc
(N
);
8594 if Is_Access_Type
(Typ
) and then not Is_Access_Constant
(Typ
) then
8595 Typ
:= Designated_Type
(Typ
);
8597 if Invariant_Checks_OK
(Typ
) then
8599 Make_Explicit_Dereference
(Loc
,
8600 Prefix
=> New_Occurrence_Of
(Id
, Loc
));
8601 Set_Etype
(Ref
, Typ
);
8604 -- if <Id> /= null then
8605 -- <invariant_call (<Ref>)>
8610 Make_If_Statement
(Loc
,
8613 Left_Opnd
=> New_Occurrence_Of
(Id
, Loc
),
8614 Right_Opnd
=> Make_Null
(Loc
)),
8615 Then_Statements
=> New_List
(
8616 Make_Invariant_Call
(Ref
))),
8620 end Add_Invariant_Access_Checks
;
8622 -------------------------
8623 -- Invariant_Checks_OK --
8624 -------------------------
8626 function Invariant_Checks_OK
(Typ
: Entity_Id
) return Boolean is
8627 function Has_Null_Body
(Proc_Id
: Entity_Id
) return Boolean;
8628 -- Determine whether the body of procedure Proc_Id contains a sole
8629 -- null statement, possibly followed by an optional return.
8631 function Has_Public_Visibility_Of_Subprogram
return Boolean;
8632 -- Determine whether type Typ has public visibility of subprogram
8639 function Has_Null_Body
(Proc_Id
: Entity_Id
) return Boolean is
8640 Body_Id
: Entity_Id
;
8647 Spec
:= Parent
(Proc_Id
);
8648 Decl
:= Parent
(Spec
);
8650 -- Retrieve the entity of the invariant procedure body
8652 if Nkind
(Spec
) = N_Procedure_Specification
8653 and then Nkind
(Decl
) = N_Subprogram_Declaration
8655 Body_Id
:= Corresponding_Body
(Decl
);
8657 -- The body acts as a spec
8663 -- The body will be generated later
8665 if No
(Body_Id
) then
8669 Spec
:= Parent
(Body_Id
);
8670 Decl
:= Parent
(Spec
);
8673 (Nkind
(Spec
) = N_Procedure_Specification
8674 and then Nkind
(Decl
) = N_Subprogram_Body
);
8676 Stmt1
:= First
(Statements
(Handled_Statement_Sequence
(Decl
)));
8678 -- Look for a null statement followed by an optional return
8681 if Nkind
(Stmt1
) = N_Null_Statement
then
8682 Stmt2
:= Next
(Stmt1
);
8684 if Present
(Stmt2
) then
8685 return Nkind
(Stmt2
) = N_Simple_Return_Statement
;
8694 -----------------------------------------
8695 -- Has_Public_Visibility_Of_Subprogram --
8696 -----------------------------------------
8698 function Has_Public_Visibility_Of_Subprogram
return Boolean is
8699 Subp_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Subp_Id
);
8700 Vis_Decls
: constant List_Id
:=
8701 Visible_Declarations
(Specification
8702 (Unit_Declaration_Node
(Scope
(Typ
))));
8704 -- An Initialization procedure must be considered visible even
8705 -- though it is internally generated.
8707 if Is_Init_Proc
(Defining_Entity
(Subp_Decl
)) then
8710 -- Internally generated code is never publicly visible except
8711 -- for a subprogram that is the implementation of an expression
8712 -- function. In that case the visibility is determined by the
8715 elsif not Comes_From_Source
(Subp_Decl
)
8717 (Nkind
(Original_Node
(Subp_Decl
)) /= N_Expression_Function
8719 Comes_From_Source
(Defining_Entity
(Subp_Decl
)))
8723 -- Determine whether the subprogram is declared in the visible
8724 -- declarations of the package containing the type.
8727 return List_Containing
(Subp_Decl
) = Vis_Decls
;
8729 end Has_Public_Visibility_Of_Subprogram
;
8731 -- Start of processing for Invariant_Checks_OK
8735 Has_Invariants
(Typ
)
8736 and then Present
(Invariant_Procedure
(Typ
))
8737 and then not Has_Null_Body
(Invariant_Procedure
(Typ
))
8738 and then Has_Public_Visibility_Of_Subprogram
;
8739 end Invariant_Checks_OK
;
8741 -------------------------
8742 -- Predicate_Checks_OK --
8743 -------------------------
8745 function Predicate_Checks_OK
(Typ
: Entity_Id
) return Boolean is
8746 function Has_Checked_Predicate
return Boolean;
8747 -- Determine whether type Typ has or inherits at least one
8748 -- predicate aspect or pragma, for which the applicable policy is
8751 ---------------------------
8752 -- Has_Checked_Predicate --
8753 ---------------------------
8755 function Has_Checked_Predicate
return Boolean is
8760 -- Climb the ancestor type chain staring from the input. This
8761 -- is done because the input type may lack aspect/pragma
8762 -- predicate and simply inherit those from its ancestor.
8764 -- Note that predicate pragmas include all three cases of
8765 -- predicate aspects (Predicate, Dynamic_Predicate,
8766 -- Static_Predicate), so this routine checks for all three
8770 while Present
(Anc
) loop
8771 Pred
:= Get_Pragma
(Anc
, Pragma_Predicate
);
8773 if Present
(Pred
) and then not Is_Ignored
(Pred
) then
8777 Anc
:= Nearest_Ancestor
(Anc
);
8781 end Has_Checked_Predicate
;
8783 -- Start of processing for Predicate_Checks_OK
8787 Has_Predicates
(Typ
)
8788 and then Present
(Predicate_Function
(Typ
))
8789 and then Has_Checked_Predicate
;
8790 end Predicate_Checks_OK
;
8794 Loc
: constant Source_Ptr
:= Sloc
(N
);
8798 -- Start of processing for Add_Invariant_And_Predicate_Checks
8803 -- Do not generate any checks if no code is being generated
8805 if not Expander_Active
then
8809 -- Process the result of a function
8811 if Ekind_In
(Subp_Id
, E_Function
, E_Generic_Function
) then
8812 Typ
:= Etype
(Subp_Id
);
8814 -- Generate _Result which is used in procedure _Postconditions to
8815 -- verify the return value.
8817 Result
:= Make_Defining_Identifier
(Loc
, Name_uResult
);
8818 Set_Etype
(Result
, Typ
);
8820 -- Add an invariant check when the return type has invariants and
8821 -- the related function is visible to the outside.
8823 if Invariant_Checks_OK
(Typ
) then
8826 Make_Invariant_Call
(New_Occurrence_Of
(Result
, Loc
)),
8830 -- Add an invariant check when the return type is an access to a
8831 -- type with invariants.
8833 Add_Invariant_Access_Checks
(Result
);
8836 -- Add invariant and predicates for all formals that qualify
8838 Formal
:= First_Formal
(Subp_Id
);
8839 while Present
(Formal
) loop
8840 Typ
:= Etype
(Formal
);
8842 if Ekind
(Formal
) /= E_In_Parameter
8843 or else Is_Access_Type
(Typ
)
8845 if Invariant_Checks_OK
(Typ
) then
8848 Make_Invariant_Call
(New_Occurrence_Of
(Formal
, Loc
)),
8852 Add_Invariant_Access_Checks
(Formal
);
8854 if Predicate_Checks_OK
(Typ
) then
8857 Make_Predicate_Check
8858 (Typ
, New_Reference_To
(Formal
, Loc
)),
8863 Next_Formal
(Formal
);
8865 end Add_Invariant_And_Predicate_Checks
;
8867 -------------------------
8868 -- Append_Enabled_Item --
8869 -------------------------
8871 procedure Append_Enabled_Item
(Item
: Node_Id
; List
: in out List_Id
) is
8873 -- Do not chain ignored or disabled pragmas
8875 if Nkind
(Item
) = N_Pragma
8876 and then (Is_Ignored
(Item
) or else Is_Disabled
(Item
))
8887 Append
(Item
, List
);
8889 end Append_Enabled_Item
;
8891 ------------------------------------
8892 -- Build_Postconditions_Procedure --
8893 ------------------------------------
8895 procedure Build_Postconditions_Procedure
8896 (Subp_Id
: Entity_Id
;
8900 procedure Insert_After_Last_Declaration
(Stmt
: Node_Id
);
8901 -- Insert node Stmt after the last declaration of the subprogram body
8903 -----------------------------------
8904 -- Insert_After_Last_Declaration --
8905 -----------------------------------
8907 procedure Insert_After_Last_Declaration
(Stmt
: Node_Id
) is
8908 Decls
: List_Id
:= Declarations
(N
);
8911 -- Ensure that the body has a declaration list
8915 Set_Declarations
(N
, Decls
);
8918 Append_To
(Decls
, Stmt
);
8919 end Insert_After_Last_Declaration
;
8923 Loc
: constant Source_Ptr
:= Sloc
(N
);
8924 Params
: List_Id
:= No_List
;
8925 Proc_Id
: Entity_Id
;
8927 -- Start of processing for Build_Postconditions_Procedure
8930 -- Do not create the routine if no code is being generated
8932 if not Expander_Active
then
8935 -- Nothing to do if there are no actions to check on exit
8937 elsif No
(Stmts
) then
8941 Proc_Id
:= Make_Defining_Identifier
(Loc
, Name_uPostconditions
);
8943 -- The related subprogram is a function, create the specification of
8944 -- parameter _Result.
8946 if Present
(Result
) then
8947 Params
:= New_List
(
8948 Make_Parameter_Specification
(Loc
,
8949 Defining_Identifier
=> Result
,
8951 New_Reference_To
(Etype
(Result
), Loc
)));
8954 -- Insert _Postconditions after the last declaration of the body.
8955 -- This ensures that the body will not cause any premature freezing
8956 -- as it may mention types:
8958 -- procedure Proc (Obj : Array_Typ) is
8959 -- procedure _postconditions is
8962 -- end _postconditions;
8964 -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1));
8967 -- In the example above, Obj is of type T but the incorrect placement
8968 -- of _Postconditions will cause a crash in gigi due to an out of
8969 -- order reference. The body of _Postconditions must be placed after
8970 -- the declaration of Temp to preserve correct visibility.
8972 Insert_After_Last_Declaration
(
8973 Make_Subprogram_Body
(Loc
,
8975 Make_Procedure_Specification
(Loc
,
8976 Defining_Unit_Name
=> Proc_Id
,
8977 Parameter_Specifications
=> Params
),
8979 Declarations
=> Empty_List
,
8980 Handled_Statement_Sequence
=>
8981 Make_Handled_Sequence_Of_Statements
(Loc
, Stmts
)));
8983 -- Set the attributes of the related subprogram to capture the
8984 -- generated procedure.
8986 if Ekind_In
(Subp_Id
, E_Generic_Procedure
, E_Procedure
) then
8987 Set_Postcondition_Proc
(Subp_Id
, Proc_Id
);
8990 Set_Has_Postconditions
(Subp_Id
);
8991 end Build_Postconditions_Procedure
;
8993 -----------------------------------
8994 -- Build_Pragma_Check_Equivalent --
8995 -----------------------------------
8997 function Build_Pragma_Check_Equivalent
8999 Subp_Id
: Entity_Id
:= Empty
;
9000 Inher_Id
: Entity_Id
:= Empty
) return Node_Id
9002 Loc
: constant Source_Ptr
:= Sloc
(Prag
);
9003 Prag_Nam
: constant Name_Id
:= Pragma_Name
(Prag
);
9004 Check_Prag
: Node_Id
;
9005 Formals_Map
: Elist_Id
;
9006 Inher_Formal
: Entity_Id
;
9009 Subp_Formal
: Entity_Id
;
9012 Formals_Map
:= No_Elist
;
9014 -- When the pre- or postcondition is inherited, map the formals of
9015 -- the inherited subprogram to those of the current subprogram.
9017 if Present
(Inher_Id
) then
9018 pragma Assert
(Present
(Subp_Id
));
9020 Formals_Map
:= New_Elmt_List
;
9022 -- Create a relation <inherited formal> => <subprogram formal>
9024 Inher_Formal
:= First_Formal
(Inher_Id
);
9025 Subp_Formal
:= First_Formal
(Subp_Id
);
9026 while Present
(Inher_Formal
) and then Present
(Subp_Formal
) loop
9027 Append_Elmt
(Inher_Formal
, Formals_Map
);
9028 Append_Elmt
(Subp_Formal
, Formals_Map
);
9030 Next_Formal
(Inher_Formal
);
9031 Next_Formal
(Subp_Formal
);
9035 -- Copy the original pragma while performing substitutions (if
9042 New_Scope
=> Current_Scope
);
9044 -- Mark the pragma as being internally generated and reset the
9047 Set_Comes_From_Source
(Check_Prag
, False);
9048 Set_Analyzed
(Check_Prag
, False);
9050 -- For a postcondition pragma within a generic, preserve the pragma
9051 -- for later expansion. This is also used when an error was detected,
9052 -- thus setting Expander_Active to False.
9054 if Prag_Nam
= Name_Postcondition
and then not Expander_Active
then
9058 if Present
(Corresponding_Aspect
(Prag
)) then
9059 Nam
:= Chars
(Identifier
(Corresponding_Aspect
(Prag
)));
9064 -- Convert the copy into pragma Check by correcting the name and
9065 -- adding a check_kind argument.
9067 Set_Pragma_Identifier
9068 (Check_Prag
, Make_Identifier
(Loc
, Name_Check
));
9070 Prepend_To
(Pragma_Argument_Associations
(Check_Prag
),
9071 Make_Pragma_Argument_Association
(Loc
,
9072 Expression
=> Make_Identifier
(Loc
, Nam
)));
9074 -- Update the error message when the pragma is inherited
9076 if Present
(Inher_Id
) then
9077 Msg_Arg
:= Last
(Pragma_Argument_Associations
(Check_Prag
));
9079 if Chars
(Msg_Arg
) = Name_Message
then
9080 String_To_Name_Buffer
(Strval
(Expression
(Msg_Arg
)));
9082 -- Insert "inherited" to improve the error message
9084 if Name_Buffer
(1 .. 8) = "failed p" then
9085 Insert_Str_In_Name_Buffer
("inherited ", 8);
9086 Set_Strval
(Expression
(Msg_Arg
), String_From_Name_Buffer
);
9092 end Build_Pragma_Check_Equivalent
;
9094 ---------------------------------
9095 -- Collect_Body_Postconditions --
9096 ---------------------------------
9098 procedure Collect_Body_Postconditions
(Stmts
: in out List_Id
) is
9099 procedure Collect_Body_Postconditions_Of_Kind
(Post_Nam
: Name_Id
);
9100 -- Process postconditions of a particular kind denoted by Post_Nam
9102 -----------------------------------------
9103 -- Collect_Body_Postconditions_Of_Kind --
9104 -----------------------------------------
9106 procedure Collect_Body_Postconditions_Of_Kind
(Post_Nam
: Name_Id
) is
9107 Check_Prag
: Node_Id
;
9111 pragma Assert
(Nam_In
(Post_Nam
, Name_Postcondition
,
9112 Name_Refined_Post
));
9114 -- Inspect the declarations of the subprogram body looking for a
9115 -- pragma that matches the desired name.
9117 Decl
:= First
(Declarations
(N
));
9118 while Present
(Decl
) loop
9119 if Nkind
(Decl
) = N_Pragma
then
9120 if Pragma_Name
(Decl
) = Post_Nam
then
9122 Check_Prag
:= Build_Pragma_Check_Equivalent
(Decl
);
9124 if Expander_Active
then
9126 (Item
=> Check_Prag
,
9129 -- When analyzing a generic unit, save the pragma for
9133 Prepend_To_Declarations
(Check_Prag
);
9137 -- Skip internally generated code
9139 elsif not Comes_From_Source
(Decl
) then
9142 -- Postconditions in bodies are usually grouped at the top of
9143 -- the declarations. There is no point in inspecting the whole
9152 end Collect_Body_Postconditions_Of_Kind
;
9154 -- Start of processing for Collect_Body_Postconditions
9157 Collect_Body_Postconditions_Of_Kind
(Name_Refined_Post
);
9158 Collect_Body_Postconditions_Of_Kind
(Name_Postcondition
);
9159 end Collect_Body_Postconditions
;
9161 ---------------------------------
9162 -- Collect_Spec_Postconditions --
9163 ---------------------------------
9165 procedure Collect_Spec_Postconditions
9166 (Subp_Id
: Entity_Id
;
9167 Stmts
: in out List_Id
)
9169 Inher_Subps
: constant Subprogram_List
:=
9170 Inherited_Subprograms
(Subp_Id
);
9171 Check_Prag
: Node_Id
;
9173 Inher_Subp_Id
: Entity_Id
;
9176 -- Process the contract of the spec
9178 Prag
:= Pre_Post_Conditions
(Contract
(Subp_Id
));
9179 while Present
(Prag
) loop
9180 if Pragma_Name
(Prag
) = Name_Postcondition
then
9181 Check_Prag
:= Build_Pragma_Check_Equivalent
(Prag
);
9183 if Expander_Active
then
9185 (Item
=> Check_Prag
,
9188 -- When analyzing a generic unit, save the pragma for later
9191 Prepend_To_Declarations
(Check_Prag
);
9195 Prag
:= Next_Pragma
(Prag
);
9198 -- Process the contracts of all inherited subprograms, looking for
9199 -- class-wide postconditions.
9201 for Index
in Inher_Subps
'Range loop
9202 Inher_Subp_Id
:= Inher_Subps
(Index
);
9204 Prag
:= Pre_Post_Conditions
(Contract
(Inher_Subp_Id
));
9205 while Present
(Prag
) loop
9206 if Pragma_Name
(Prag
) = Name_Postcondition
9207 and then Class_Present
(Prag
)
9210 Build_Pragma_Check_Equivalent
9213 Inher_Id
=> Inher_Subp_Id
);
9215 if Expander_Active
then
9217 (Item
=> Check_Prag
,
9220 -- When analyzing a generic unit, save the pragma for later
9223 Prepend_To_Declarations
(Check_Prag
);
9227 Prag
:= Next_Pragma
(Prag
);
9230 end Collect_Spec_Postconditions
;
9232 --------------------------------
9233 -- Collect_Spec_Preconditions --
9234 --------------------------------
9236 procedure Collect_Spec_Preconditions
(Subp_Id
: Entity_Id
) is
9237 procedure Merge_Preconditions
(From
: Node_Id
; Into
: Node_Id
);
9238 -- Merge two class-wide preconditions by "or else"-ing them. The
9239 -- changes are accumulated in parameter Into. Update the error
9242 -------------------------
9243 -- Merge_Preconditions --
9244 -------------------------
9246 procedure Merge_Preconditions
(From
: Node_Id
; Into
: Node_Id
) is
9247 function Expression_Arg
(Prag
: Node_Id
) return Node_Id
;
9248 -- Return the boolean expression argument of a precondition while
9249 -- updating its parenteses count for the subsequent merge.
9251 function Message_Arg
(Prag
: Node_Id
) return Node_Id
;
9252 -- Return the message argument of a precondition
9254 --------------------
9255 -- Expression_Arg --
9256 --------------------
9258 function Expression_Arg
(Prag
: Node_Id
) return Node_Id
is
9259 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
9260 Arg
: constant Node_Id
:= Get_Pragma_Arg
(Next
(First
(Args
)));
9263 if Paren_Count
(Arg
) = 0 then
9264 Set_Paren_Count
(Arg
, 1);
9274 function Message_Arg
(Prag
: Node_Id
) return Node_Id
is
9275 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
9277 return Get_Pragma_Arg
(Last
(Args
));
9282 From_Expr
: constant Node_Id
:= Expression_Arg
(From
);
9283 From_Msg
: constant Node_Id
:= Message_Arg
(From
);
9284 Into_Expr
: constant Node_Id
:= Expression_Arg
(Into
);
9285 Into_Msg
: constant Node_Id
:= Message_Arg
(Into
);
9286 Loc
: constant Source_Ptr
:= Sloc
(Into
);
9288 -- Start of processing for Merge_Preconditions
9291 -- Merge the two preconditions by "or else"-ing them
9295 Right_Opnd
=> Relocate_Node
(Into_Expr
),
9296 Left_Opnd
=> From_Expr
));
9298 -- Merge the two error messages to produce a single message of the
9301 -- failed precondition from ...
9302 -- also failed inherited precondition from ...
9304 if not Exception_Locations_Suppressed
then
9305 Start_String
(Strval
(Into_Msg
));
9306 Store_String_Char
(ASCII
.LF
);
9307 Store_String_Chars
(" also ");
9308 Store_String_Chars
(Strval
(From_Msg
));
9310 Set_Strval
(Into_Msg
, End_String
);
9312 end Merge_Preconditions
;
9316 Inher_Subps
: constant Subprogram_List
:=
9317 Inherited_Subprograms
(Subp_Id
);
9318 Check_Prag
: Node_Id
;
9319 Class_Pre
: Node_Id
:= Empty
;
9320 Inher_Subp_Id
: Entity_Id
;
9323 -- Start of processing for Collect_Spec_Preconditions
9326 -- Process the contract of the spec
9328 Prag
:= Pre_Post_Conditions
(Contract
(Subp_Id
));
9329 while Present
(Prag
) loop
9330 if Pragma_Name
(Prag
) = Name_Precondition
then
9331 Check_Prag
:= Build_Pragma_Check_Equivalent
(Prag
);
9333 -- Save the sole class-wide precondition (if any) for the next
9334 -- step where it will be merged with inherited preconditions.
9336 if Class_Present
(Prag
) then
9337 Class_Pre
:= Check_Prag
;
9339 -- Accumulate the corresponding Check pragmas to the top of the
9340 -- declarations. Prepending the items ensures that they will
9341 -- be evaluated in their original order.
9344 Prepend_To_Declarations
(Check_Prag
);
9348 Prag
:= Next_Pragma
(Prag
);
9351 -- Process the contracts of all inherited subprograms, looking for
9352 -- class-wide preconditions.
9354 for Index
in Inher_Subps
'Range loop
9355 Inher_Subp_Id
:= Inher_Subps
(Index
);
9357 Prag
:= Pre_Post_Conditions
(Contract
(Inher_Subp_Id
));
9358 while Present
(Prag
) loop
9359 if Pragma_Name
(Prag
) = Name_Precondition
9360 and then Class_Present
(Prag
)
9363 Build_Pragma_Check_Equivalent
9366 Inher_Id
=> Inher_Subp_Id
);
9368 -- The spec or an inherited subprogram already yielded a
9369 -- class-wide precondition. Merge the existing precondition
9370 -- with the current one using "or else".
9372 if Present
(Class_Pre
) then
9373 Merge_Preconditions
(Check_Prag
, Class_Pre
);
9375 Class_Pre
:= Check_Prag
;
9379 Prag
:= Next_Pragma
(Prag
);
9383 -- Add the merged class-wide preconditions (if any)
9385 if Present
(Class_Pre
) then
9386 Prepend_To_Declarations
(Class_Pre
);
9388 end Collect_Spec_Preconditions
;
9390 -----------------------------
9391 -- Prepend_To_Declarations --
9392 -----------------------------
9394 procedure Prepend_To_Declarations
(Item
: Node_Id
) is
9395 Decls
: List_Id
:= Declarations
(N
);
9398 -- Ensure that the body has a declarative list
9402 Set_Declarations
(N
, Decls
);
9405 Prepend_To
(Decls
, Item
);
9406 end Prepend_To_Declarations
;
9408 ----------------------------
9409 -- Process_Contract_Cases --
9410 ----------------------------
9412 procedure Process_Contract_Cases
9413 (Subp_Id
: Entity_Id
;
9414 Stmts
: in out List_Id
)
9419 -- Do not build the Contract_Cases circuitry if no code is being
9422 if not Expander_Active
then
9426 Prag
:= Contract_Test_Cases
(Contract
(Subp_Id
));
9427 while Present
(Prag
) loop
9428 if Pragma_Name
(Prag
) = Name_Contract_Cases
then
9429 Expand_Contract_Cases
9432 Decls
=> Declarations
(N
),
9436 Prag
:= Next_Pragma
(Prag
);
9438 end Process_Contract_Cases
;
9442 Post_Stmts
: List_Id
:= No_List
;
9444 Subp_Id
: Entity_Id
;
9446 -- Start of processing for Expand_Subprogram_Contract
9449 if Present
(Spec_Id
) then
9455 -- Do not process a predicate function as its body will end up with a
9456 -- recursive call to itself and blow up the stack.
9458 if Ekind
(Subp_Id
) = E_Function
9459 and then Is_Predicate_Function
(Subp_Id
)
9463 -- Do not process TSS subprograms
9465 elsif Get_TSS_Name
(Subp_Id
) /= TSS_Null
then
9469 -- The expansion of a subprogram contract involves the relocation of
9470 -- various contract assertions to the declarations of the body in a
9471 -- particular order. The order is as follows:
9473 -- function Example (...) return ... is
9474 -- procedure _Postconditions (...) is
9476 -- <refined postconditions from body>
9477 -- <postconditions from body>
9478 -- <postconditions from spec>
9479 -- <inherited postconditions>
9481 -- <invariant check of function result (if applicable)>
9482 -- <invariant and predicate checks of parameters>
9483 -- end _Postconditions;
9485 -- <inherited preconditions>
9486 -- <preconditions from spec>
9487 -- <preconditions from body>
9488 -- <refined preconditions from body>
9490 -- <source declarations>
9492 -- <source statements>
9494 -- _Preconditions (Result);
9498 -- Routine _Postconditions holds all contract assertions that must be
9499 -- verified on exit from the related routine.
9501 -- Collect all [inherited] preconditions from the spec, transform them
9502 -- into Check pragmas and add them to the declarations of the body in
9503 -- the order outlined above.
9505 if Present
(Spec_Id
) then
9506 Collect_Spec_Preconditions
(Spec_Id
);
9509 -- Transform all [refined] postconditions of the body into Check
9510 -- pragmas. The resulting pragmas are accumulated in list Post_Stmts.
9512 Collect_Body_Postconditions
(Post_Stmts
);
9514 -- Transform all [inherited] postconditions from the spec into Check
9515 -- pragmas. The resulting pragmas are accumulated in list Post_Stmts.
9517 if Present
(Spec_Id
) then
9518 Collect_Spec_Postconditions
(Spec_Id
, Post_Stmts
);
9520 -- Transform pragma Contract_Cases from the spec into its circuitry
9522 Process_Contract_Cases
(Spec_Id
, Post_Stmts
);
9525 -- Apply invariant and predicate checks on the result of a function (if
9526 -- applicable) and all formals. The resulting checks are accumulated in
9529 Add_Invariant_And_Predicate_Checks
(Subp_Id
, Post_Stmts
, Result
);
9531 -- Construct procedure _Postconditions
9533 Build_Postconditions_Procedure
(Subp_Id
, Post_Stmts
, Result
);
9534 end Expand_Subprogram_Contract
;
9536 --------------------------------
9537 -- Is_Build_In_Place_Function --
9538 --------------------------------
9540 function Is_Build_In_Place_Function
(E
: Entity_Id
) return Boolean is
9542 -- This function is called from Expand_Subtype_From_Expr during
9543 -- semantic analysis, even when expansion is off. In those cases
9544 -- the build_in_place expansion will not take place.
9546 if not Expander_Active
then
9550 -- For now we test whether E denotes a function or access-to-function
9551 -- type whose result subtype is inherently limited. Later this test may
9552 -- be revised to allow composite nonlimited types. Functions with a
9553 -- foreign convention or whose result type has a foreign convention
9556 if Ekind_In
(E
, E_Function
, E_Generic_Function
)
9557 or else (Ekind
(E
) = E_Subprogram_Type
9558 and then Etype
(E
) /= Standard_Void_Type
)
9560 -- Note: If you have Convention (C) on an inherently limited type,
9561 -- you're on your own. That is, the C code will have to be carefully
9562 -- written to know about the Ada conventions.
9564 if Has_Foreign_Convention
(E
)
9565 or else Has_Foreign_Convention
(Etype
(E
))
9569 -- In Ada 2005 all functions with an inherently limited return type
9570 -- must be handled using a build-in-place profile, including the case
9571 -- of a function with a limited interface result, where the function
9572 -- may return objects of nonlimited descendants.
9575 return Is_Limited_View
(Etype
(E
))
9576 and then Ada_Version
>= Ada_2005
9577 and then not Debug_Flag_Dot_L
;
9583 end Is_Build_In_Place_Function
;
9585 -------------------------------------
9586 -- Is_Build_In_Place_Function_Call --
9587 -------------------------------------
9589 function Is_Build_In_Place_Function_Call
(N
: Node_Id
) return Boolean is
9590 Exp_Node
: Node_Id
:= N
;
9591 Function_Id
: Entity_Id
;
9594 -- Return False when the expander is inactive, since awareness of
9595 -- build-in-place treatment is only relevant during expansion. Note that
9596 -- Is_Build_In_Place_Function, which is called as part of this function,
9597 -- is also conditioned this way, but we need to check here as well to
9598 -- avoid blowing up on processing protected calls when expansion is
9599 -- disabled (such as with -gnatc) since those would trip over the raise
9600 -- of Program_Error below.
9602 -- In SPARK mode, build-in-place calls are not expanded, so that we
9603 -- may end up with a call that is neither resolved to an entity, nor
9604 -- an indirect call.
9606 if not Full_Expander_Active
then
9610 -- Step past qualification or unchecked conversion (the latter can occur
9611 -- in cases of calls to 'Input).
9613 if Nkind_In
(Exp_Node
, N_Qualified_Expression
,
9614 N_Unchecked_Type_Conversion
)
9616 Exp_Node
:= Expression
(N
);
9619 if Nkind
(Exp_Node
) /= N_Function_Call
then
9623 if Is_Entity_Name
(Name
(Exp_Node
)) then
9624 Function_Id
:= Entity
(Name
(Exp_Node
));
9626 -- In the case of an explicitly dereferenced call, use the subprogram
9627 -- type generated for the dereference.
9629 elsif Nkind
(Name
(Exp_Node
)) = N_Explicit_Dereference
then
9630 Function_Id
:= Etype
(Name
(Exp_Node
));
9632 -- This may be a call to a protected function.
9634 elsif Nkind
(Name
(Exp_Node
)) = N_Selected_Component
then
9635 Function_Id
:= Etype
(Entity
(Selector_Name
(Name
(Exp_Node
))));
9638 raise Program_Error
;
9641 return Is_Build_In_Place_Function
(Function_Id
);
9643 end Is_Build_In_Place_Function_Call
;
9645 -----------------------
9646 -- Freeze_Subprogram --
9647 -----------------------
9649 procedure Freeze_Subprogram
(N
: Node_Id
) is
9650 Loc
: constant Source_Ptr
:= Sloc
(N
);
9652 procedure Register_Predefined_DT_Entry
(Prim
: Entity_Id
);
9653 -- (Ada 2005): Register a predefined primitive in all the secondary
9654 -- dispatch tables of its primitive type.
9656 ----------------------------------
9657 -- Register_Predefined_DT_Entry --
9658 ----------------------------------
9660 procedure Register_Predefined_DT_Entry
(Prim
: Entity_Id
) is
9661 Iface_DT_Ptr
: Elmt_Id
;
9662 Tagged_Typ
: Entity_Id
;
9663 Thunk_Id
: Entity_Id
;
9664 Thunk_Code
: Node_Id
;
9667 Tagged_Typ
:= Find_Dispatching_Type
(Prim
);
9669 if No
(Access_Disp_Table
(Tagged_Typ
))
9670 or else not Has_Interfaces
(Tagged_Typ
)
9671 or else not RTE_Available
(RE_Interface_Tag
)
9672 or else Restriction_Active
(No_Dispatching_Calls
)
9677 -- Skip the first two access-to-dispatch-table pointers since they
9678 -- leads to the primary dispatch table (predefined DT and user
9679 -- defined DT). We are only concerned with the secondary dispatch
9680 -- table pointers. Note that the access-to- dispatch-table pointer
9681 -- corresponds to the first implemented interface retrieved below.
9684 Next_Elmt
(Next_Elmt
(First_Elmt
(Access_Disp_Table
(Tagged_Typ
))));
9686 while Present
(Iface_DT_Ptr
)
9687 and then Ekind
(Node
(Iface_DT_Ptr
)) = E_Constant
9689 pragma Assert
(Has_Thunks
(Node
(Iface_DT_Ptr
)));
9690 Expand_Interface_Thunk
(Prim
, Thunk_Id
, Thunk_Code
);
9692 if Present
(Thunk_Code
) then
9693 Insert_Actions_After
(N
, New_List
(
9696 Build_Set_Predefined_Prim_Op_Address
(Loc
,
9698 New_Reference_To
(Node
(Next_Elmt
(Iface_DT_Ptr
)), Loc
),
9699 Position
=> DT_Position
(Prim
),
9701 Unchecked_Convert_To
(RTE
(RE_Prim_Ptr
),
9702 Make_Attribute_Reference
(Loc
,
9703 Prefix
=> New_Reference_To
(Thunk_Id
, Loc
),
9704 Attribute_Name
=> Name_Unrestricted_Access
))),
9706 Build_Set_Predefined_Prim_Op_Address
(Loc
,
9709 (Node
(Next_Elmt
(Next_Elmt
(Next_Elmt
(Iface_DT_Ptr
)))),
9711 Position
=> DT_Position
(Prim
),
9713 Unchecked_Convert_To
(RTE
(RE_Prim_Ptr
),
9714 Make_Attribute_Reference
(Loc
,
9715 Prefix
=> New_Reference_To
(Prim
, Loc
),
9716 Attribute_Name
=> Name_Unrestricted_Access
)))));
9719 -- Skip the tag of the predefined primitives dispatch table
9721 Next_Elmt
(Iface_DT_Ptr
);
9722 pragma Assert
(Has_Thunks
(Node
(Iface_DT_Ptr
)));
9724 -- Skip tag of the no-thunks dispatch table
9726 Next_Elmt
(Iface_DT_Ptr
);
9727 pragma Assert
(not Has_Thunks
(Node
(Iface_DT_Ptr
)));
9729 -- Skip tag of predefined primitives no-thunks dispatch table
9731 Next_Elmt
(Iface_DT_Ptr
);
9732 pragma Assert
(not Has_Thunks
(Node
(Iface_DT_Ptr
)));
9734 Next_Elmt
(Iface_DT_Ptr
);
9736 end Register_Predefined_DT_Entry
;
9740 Subp
: constant Entity_Id
:= Entity
(N
);
9742 -- Start of processing for Freeze_Subprogram
9745 -- We suppress the initialization of the dispatch table entry when
9746 -- VM_Target because the dispatching mechanism is handled internally
9749 if Is_Dispatching_Operation
(Subp
)
9750 and then not Is_Abstract_Subprogram
(Subp
)
9751 and then Present
(DTC_Entity
(Subp
))
9752 and then Present
(Scope
(DTC_Entity
(Subp
)))
9753 and then Tagged_Type_Expansion
9754 and then not Restriction_Active
(No_Dispatching_Calls
)
9755 and then RTE_Available
(RE_Tag
)
9758 Typ
: constant Entity_Id
:= Scope
(DTC_Entity
(Subp
));
9761 -- Handle private overridden primitives
9763 if not Is_CPP_Class
(Typ
) then
9764 Check_Overriding_Operation
(Subp
);
9767 -- We assume that imported CPP primitives correspond with objects
9768 -- whose constructor is in the CPP side; therefore we don't need
9769 -- to generate code to register them in the dispatch table.
9771 if Is_CPP_Class
(Typ
) then
9774 -- Handle CPP primitives found in derivations of CPP_Class types.
9775 -- These primitives must have been inherited from some parent, and
9776 -- there is no need to register them in the dispatch table because
9777 -- Build_Inherit_Prims takes care of the initialization of these
9780 elsif Is_Imported
(Subp
)
9781 and then (Convention
(Subp
) = Convention_CPP
9782 or else Convention
(Subp
) = Convention_C
)
9786 -- Generate code to register the primitive in non statically
9787 -- allocated dispatch tables
9789 elsif not Building_Static_DT
(Scope
(DTC_Entity
(Subp
))) then
9791 -- When a primitive is frozen, enter its name in its dispatch
9794 if not Is_Interface
(Typ
)
9795 or else Present
(Interface_Alias
(Subp
))
9797 if Is_Predefined_Dispatching_Operation
(Subp
) then
9798 Register_Predefined_DT_Entry
(Subp
);
9801 Insert_Actions_After
(N
,
9802 Register_Primitive
(Loc
, Prim
=> Subp
));
9808 -- Mark functions that return by reference. Note that it cannot be part
9809 -- of the normal semantic analysis of the spec since the underlying
9810 -- returned type may not be known yet (for private types).
9813 Typ
: constant Entity_Id
:= Etype
(Subp
);
9814 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
9816 if Is_Limited_View
(Typ
) then
9817 Set_Returns_By_Ref
(Subp
);
9818 elsif Present
(Utyp
) and then CW_Or_Has_Controlled_Part
(Utyp
) then
9819 Set_Returns_By_Ref
(Subp
);
9823 -- Wnen freezing a null procedure, analyze its delayed aspects now
9824 -- because we may not have reached the end of the declarative list when
9825 -- delayed aspects are normally analyzed. This ensures that dispatching
9826 -- calls are properly rewritten when the generated _Postcondition
9827 -- procedure is analyzed in the null procedure body.
9829 if Nkind
(Parent
(Subp
)) = N_Procedure_Specification
9830 and then Null_Present
(Parent
(Subp
))
9832 Analyze_Subprogram_Contract
(Subp
);
9834 end Freeze_Subprogram
;
9836 -----------------------
9837 -- Is_Null_Procedure --
9838 -----------------------
9840 function Is_Null_Procedure
(Subp
: Entity_Id
) return Boolean is
9841 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
9844 if Ekind
(Subp
) /= E_Procedure
then
9847 -- Check if this is a declared null procedure
9849 elsif Nkind
(Decl
) = N_Subprogram_Declaration
then
9850 if not Null_Present
(Specification
(Decl
)) then
9853 elsif No
(Body_To_Inline
(Decl
)) then
9856 -- Check if the body contains only a null statement, followed by
9857 -- the return statement added during expansion.
9861 Orig_Bod
: constant Node_Id
:= Body_To_Inline
(Decl
);
9867 if Nkind
(Orig_Bod
) /= N_Subprogram_Body
then
9870 -- We must skip SCIL nodes because they are currently
9871 -- implemented as special N_Null_Statement nodes.
9875 (Statements
(Handled_Statement_Sequence
(Orig_Bod
)));
9876 Stat2
:= Next_Non_SCIL_Node
(Stat
);
9879 Is_Empty_List
(Declarations
(Orig_Bod
))
9880 and then Nkind
(Stat
) = N_Null_Statement
9884 (Nkind
(Stat2
) = N_Simple_Return_Statement
9885 and then No
(Next
(Stat2
))));
9893 end Is_Null_Procedure
;
9895 -------------------------------------------
9896 -- Make_Build_In_Place_Call_In_Allocator --
9897 -------------------------------------------
9899 procedure Make_Build_In_Place_Call_In_Allocator
9900 (Allocator
: Node_Id
;
9901 Function_Call
: Node_Id
)
9903 Acc_Type
: constant Entity_Id
:= Etype
(Allocator
);
9905 Func_Call
: Node_Id
:= Function_Call
;
9906 Function_Id
: Entity_Id
;
9907 Result_Subt
: Entity_Id
;
9908 New_Allocator
: Node_Id
;
9909 Return_Obj_Access
: Entity_Id
;
9912 -- Step past qualification or unchecked conversion (the latter can occur
9913 -- in cases of calls to 'Input).
9915 if Nkind_In
(Func_Call
,
9916 N_Qualified_Expression
,
9917 N_Unchecked_Type_Conversion
)
9919 Func_Call
:= Expression
(Func_Call
);
9922 -- If the call has already been processed to add build-in-place actuals
9923 -- then return. This should not normally occur in an allocator context,
9924 -- but we add the protection as a defensive measure.
9926 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
9930 -- Mark the call as processed as a build-in-place call
9932 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
9934 Loc
:= Sloc
(Function_Call
);
9936 if Is_Entity_Name
(Name
(Func_Call
)) then
9937 Function_Id
:= Entity
(Name
(Func_Call
));
9939 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
9940 Function_Id
:= Etype
(Name
(Func_Call
));
9943 raise Program_Error
;
9946 Result_Subt
:= Available_View
(Etype
(Function_Id
));
9948 -- Check whether return type includes tasks. This may not have been done
9949 -- previously, if the type was a limited view.
9951 if Has_Task
(Result_Subt
) then
9952 Build_Activation_Chain_Entity
(Allocator
);
9955 -- When the result subtype is constrained, the return object must be
9956 -- allocated on the caller side, and access to it is passed to the
9959 -- Here and in related routines, we must examine the full view of the
9960 -- type, because the view at the point of call may differ from that
9961 -- that in the function body, and the expansion mechanism depends on
9962 -- the characteristics of the full view.
9964 if Is_Constrained
(Underlying_Type
(Result_Subt
)) then
9966 -- Replace the initialized allocator of form "new T'(Func (...))"
9967 -- with an uninitialized allocator of form "new T", where T is the
9968 -- result subtype of the called function. The call to the function
9969 -- is handled separately further below.
9972 Make_Allocator
(Loc
,
9973 Expression
=> New_Reference_To
(Result_Subt
, Loc
));
9974 Set_No_Initialization
(New_Allocator
);
9976 -- Copy attributes to new allocator. Note that the new allocator
9977 -- logically comes from source if the original one did, so copy the
9978 -- relevant flag. This ensures proper treatment of the restriction
9979 -- No_Implicit_Heap_Allocations in this case.
9981 Set_Storage_Pool
(New_Allocator
, Storage_Pool
(Allocator
));
9982 Set_Procedure_To_Call
(New_Allocator
, Procedure_To_Call
(Allocator
));
9983 Set_Comes_From_Source
(New_Allocator
, Comes_From_Source
(Allocator
));
9985 Rewrite
(Allocator
, New_Allocator
);
9987 -- Create a new access object and initialize it to the result of the
9988 -- new uninitialized allocator. Note: we do not use Allocator as the
9989 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9990 -- as this would create a sort of infinite "recursion".
9992 Return_Obj_Access
:= Make_Temporary
(Loc
, 'R');
9993 Set_Etype
(Return_Obj_Access
, Acc_Type
);
9995 Insert_Action
(Allocator
,
9996 Make_Object_Declaration
(Loc
,
9997 Defining_Identifier
=> Return_Obj_Access
,
9998 Object_Definition
=> New_Reference_To
(Acc_Type
, Loc
),
9999 Expression
=> Relocate_Node
(Allocator
)));
10001 -- When the function has a controlling result, an allocation-form
10002 -- parameter must be passed indicating that the caller is allocating
10003 -- the result object. This is needed because such a function can be
10004 -- called as a dispatching operation and must be treated similarly
10005 -- to functions with unconstrained result subtypes.
10007 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10008 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
10010 Add_Finalization_Master_Actual_To_Build_In_Place_Call
10011 (Func_Call
, Function_Id
, Acc_Type
);
10013 Add_Task_Actuals_To_Build_In_Place_Call
10014 (Func_Call
, Function_Id
, Master_Actual
=> Master_Id
(Acc_Type
));
10016 -- Add an implicit actual to the function call that provides access
10017 -- to the allocated object. An unchecked conversion to the (specific)
10018 -- result subtype of the function is inserted to handle cases where
10019 -- the access type of the allocator has a class-wide designated type.
10021 Add_Access_Actual_To_Build_In_Place_Call
10024 Make_Unchecked_Type_Conversion
(Loc
,
10025 Subtype_Mark
=> New_Reference_To
(Result_Subt
, Loc
),
10027 Make_Explicit_Dereference
(Loc
,
10028 Prefix
=> New_Reference_To
(Return_Obj_Access
, Loc
))));
10030 -- When the result subtype is unconstrained, the function itself must
10031 -- perform the allocation of the return object, so we pass parameters
10032 -- indicating that. We don't yet handle the case where the allocation
10033 -- must be done in a user-defined storage pool, which will require
10034 -- passing another actual or two to provide allocation/deallocation
10038 -- Case of a user-defined storage pool. Pass an allocation parameter
10039 -- indicating that the function should allocate its result in the
10040 -- pool, and pass the pool. Use 'Unrestricted_Access because the
10041 -- pool may not be aliased.
10043 if VM_Target
= No_VM
10044 and then Present
(Associated_Storage_Pool
(Acc_Type
))
10046 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10047 (Func_Call
, Function_Id
, Alloc_Form
=> User_Storage_Pool
,
10049 Make_Attribute_Reference
(Loc
,
10052 (Associated_Storage_Pool
(Acc_Type
), Loc
),
10053 Attribute_Name
=> Name_Unrestricted_Access
));
10055 -- No user-defined pool; pass an allocation parameter indicating that
10056 -- the function should allocate its result on the heap.
10059 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10060 (Func_Call
, Function_Id
, Alloc_Form
=> Global_Heap
);
10063 Add_Finalization_Master_Actual_To_Build_In_Place_Call
10064 (Func_Call
, Function_Id
, Acc_Type
);
10066 Add_Task_Actuals_To_Build_In_Place_Call
10067 (Func_Call
, Function_Id
, Master_Actual
=> Master_Id
(Acc_Type
));
10069 -- The caller does not provide the return object in this case, so we
10070 -- have to pass null for the object access actual.
10072 Add_Access_Actual_To_Build_In_Place_Call
10073 (Func_Call
, Function_Id
, Return_Object
=> Empty
);
10076 -- If the build-in-place function call returns a controlled object,
10077 -- the finalization master will require a reference to routine
10078 -- Finalize_Address of the designated type. Setting this attribute
10079 -- is done in the same manner to expansion of allocators.
10081 if Needs_Finalization
(Result_Subt
) then
10083 -- Controlled types with supressed finalization do not need to
10084 -- associate the address of their Finalize_Address primitives with
10085 -- a master since they do not need a master to begin with.
10087 if Is_Library_Level_Entity
(Acc_Type
)
10088 and then Finalize_Storage_Only
(Result_Subt
)
10092 -- Do not generate the call to Set_Finalize_Address in CodePeer mode
10093 -- because Finalize_Address is never built.
10095 elsif not CodePeer_Mode
then
10096 Insert_Action
(Allocator
,
10097 Make_Set_Finalize_Address_Call
(Loc
,
10098 Typ
=> Etype
(Function_Id
),
10099 Ptr_Typ
=> Acc_Type
));
10103 -- Finally, replace the allocator node with a reference to the result
10104 -- of the function call itself (which will effectively be an access
10105 -- to the object created by the allocator).
10107 Rewrite
(Allocator
, Make_Reference
(Loc
, Relocate_Node
(Function_Call
)));
10109 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
10110 -- generate an implicit conversion to force displacement of the "this"
10113 if Is_Interface
(Designated_Type
(Acc_Type
)) then
10114 Rewrite
(Allocator
, Convert_To
(Acc_Type
, Relocate_Node
(Allocator
)));
10117 Analyze_And_Resolve
(Allocator
, Acc_Type
);
10118 end Make_Build_In_Place_Call_In_Allocator
;
10120 ---------------------------------------------------
10121 -- Make_Build_In_Place_Call_In_Anonymous_Context --
10122 ---------------------------------------------------
10124 procedure Make_Build_In_Place_Call_In_Anonymous_Context
10125 (Function_Call
: Node_Id
)
10128 Func_Call
: Node_Id
:= Function_Call
;
10129 Function_Id
: Entity_Id
;
10130 Result_Subt
: Entity_Id
;
10131 Return_Obj_Id
: Entity_Id
;
10132 Return_Obj_Decl
: Entity_Id
;
10135 -- Step past qualification or unchecked conversion (the latter can occur
10136 -- in cases of calls to 'Input).
10138 if Nkind_In
(Func_Call
, N_Qualified_Expression
,
10139 N_Unchecked_Type_Conversion
)
10141 Func_Call
:= Expression
(Func_Call
);
10144 -- If the call has already been processed to add build-in-place actuals
10145 -- then return. One place this can occur is for calls to build-in-place
10146 -- functions that occur within a call to a protected operation, where
10147 -- due to rewriting and expansion of the protected call there can be
10148 -- more than one call to Expand_Actuals for the same set of actuals.
10150 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
10154 -- Mark the call as processed as a build-in-place call
10156 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
10158 Loc
:= Sloc
(Function_Call
);
10160 if Is_Entity_Name
(Name
(Func_Call
)) then
10161 Function_Id
:= Entity
(Name
(Func_Call
));
10163 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
10164 Function_Id
:= Etype
(Name
(Func_Call
));
10167 raise Program_Error
;
10170 Result_Subt
:= Etype
(Function_Id
);
10172 -- If the build-in-place function returns a controlled object, then the
10173 -- object needs to be finalized immediately after the context. Since
10174 -- this case produces a transient scope, the servicing finalizer needs
10175 -- to name the returned object. Create a temporary which is initialized
10176 -- with the function call:
10178 -- Temp_Id : Func_Type := BIP_Func_Call;
10180 -- The initialization expression of the temporary will be rewritten by
10181 -- the expander using the appropriate mechanism in Make_Build_In_Place_
10182 -- Call_In_Object_Declaration.
10184 if Needs_Finalization
(Result_Subt
) then
10186 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'R');
10187 Temp_Decl
: Node_Id
;
10190 -- Reset the guard on the function call since the following does
10191 -- not perform actual call expansion.
10193 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
, False);
10196 Make_Object_Declaration
(Loc
,
10197 Defining_Identifier
=> Temp_Id
,
10198 Object_Definition
=>
10199 New_Reference_To
(Result_Subt
, Loc
),
10201 New_Copy_Tree
(Function_Call
));
10203 Insert_Action
(Function_Call
, Temp_Decl
);
10205 Rewrite
(Function_Call
, New_Reference_To
(Temp_Id
, Loc
));
10206 Analyze
(Function_Call
);
10209 -- When the result subtype is constrained, an object of the subtype is
10210 -- declared and an access value designating it is passed as an actual.
10212 elsif Is_Constrained
(Underlying_Type
(Result_Subt
)) then
10214 -- Create a temporary object to hold the function result
10216 Return_Obj_Id
:= Make_Temporary
(Loc
, 'R');
10217 Set_Etype
(Return_Obj_Id
, Result_Subt
);
10220 Make_Object_Declaration
(Loc
,
10221 Defining_Identifier
=> Return_Obj_Id
,
10222 Aliased_Present
=> True,
10223 Object_Definition
=> New_Reference_To
(Result_Subt
, Loc
));
10225 Set_No_Initialization
(Return_Obj_Decl
);
10227 Insert_Action
(Func_Call
, Return_Obj_Decl
);
10229 -- When the function has a controlling result, an allocation-form
10230 -- parameter must be passed indicating that the caller is allocating
10231 -- the result object. This is needed because such a function can be
10232 -- called as a dispatching operation and must be treated similarly
10233 -- to functions with unconstrained result subtypes.
10235 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10236 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
10238 Add_Finalization_Master_Actual_To_Build_In_Place_Call
10239 (Func_Call
, Function_Id
);
10241 Add_Task_Actuals_To_Build_In_Place_Call
10242 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
10244 -- Add an implicit actual to the function call that provides access
10245 -- to the caller's return object.
10247 Add_Access_Actual_To_Build_In_Place_Call
10248 (Func_Call
, Function_Id
, New_Reference_To
(Return_Obj_Id
, Loc
));
10250 -- When the result subtype is unconstrained, the function must allocate
10251 -- the return object in the secondary stack, so appropriate implicit
10252 -- parameters are added to the call to indicate that. A transient
10253 -- scope is established to ensure eventual cleanup of the result.
10256 -- Pass an allocation parameter indicating that the function should
10257 -- allocate its result on the secondary stack.
10259 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10260 (Func_Call
, Function_Id
, Alloc_Form
=> Secondary_Stack
);
10262 Add_Finalization_Master_Actual_To_Build_In_Place_Call
10263 (Func_Call
, Function_Id
);
10265 Add_Task_Actuals_To_Build_In_Place_Call
10266 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
10268 -- Pass a null value to the function since no return object is
10269 -- available on the caller side.
10271 Add_Access_Actual_To_Build_In_Place_Call
10272 (Func_Call
, Function_Id
, Empty
);
10274 end Make_Build_In_Place_Call_In_Anonymous_Context
;
10276 --------------------------------------------
10277 -- Make_Build_In_Place_Call_In_Assignment --
10278 --------------------------------------------
10280 procedure Make_Build_In_Place_Call_In_Assignment
10282 Function_Call
: Node_Id
)
10284 Lhs
: constant Node_Id
:= Name
(Assign
);
10285 Func_Call
: Node_Id
:= Function_Call
;
10286 Func_Id
: Entity_Id
;
10288 Obj_Decl
: Node_Id
;
10289 Obj_Id
: Entity_Id
;
10290 Ptr_Typ
: Entity_Id
;
10291 Ptr_Typ_Decl
: Node_Id
;
10292 New_Expr
: Node_Id
;
10293 Result_Subt
: Entity_Id
;
10297 -- Step past qualification or unchecked conversion (the latter can occur
10298 -- in cases of calls to 'Input).
10300 if Nkind_In
(Func_Call
, N_Qualified_Expression
,
10301 N_Unchecked_Type_Conversion
)
10303 Func_Call
:= Expression
(Func_Call
);
10306 -- If the call has already been processed to add build-in-place actuals
10307 -- then return. This should not normally occur in an assignment context,
10308 -- but we add the protection as a defensive measure.
10310 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
10314 -- Mark the call as processed as a build-in-place call
10316 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
10318 Loc
:= Sloc
(Function_Call
);
10320 if Is_Entity_Name
(Name
(Func_Call
)) then
10321 Func_Id
:= Entity
(Name
(Func_Call
));
10323 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
10324 Func_Id
:= Etype
(Name
(Func_Call
));
10327 raise Program_Error
;
10330 Result_Subt
:= Etype
(Func_Id
);
10332 -- When the result subtype is unconstrained, an additional actual must
10333 -- be passed to indicate that the caller is providing the return object.
10334 -- This parameter must also be passed when the called function has a
10335 -- controlling result, because dispatching calls to the function needs
10336 -- to be treated effectively the same as calls to class-wide functions.
10338 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10339 (Func_Call
, Func_Id
, Alloc_Form
=> Caller_Allocation
);
10341 Add_Finalization_Master_Actual_To_Build_In_Place_Call
10342 (Func_Call
, Func_Id
);
10344 Add_Task_Actuals_To_Build_In_Place_Call
10345 (Func_Call
, Func_Id
, Make_Identifier
(Loc
, Name_uMaster
));
10347 -- Add an implicit actual to the function call that provides access to
10348 -- the caller's return object.
10350 Add_Access_Actual_To_Build_In_Place_Call
10353 Make_Unchecked_Type_Conversion
(Loc
,
10354 Subtype_Mark
=> New_Reference_To
(Result_Subt
, Loc
),
10355 Expression
=> Relocate_Node
(Lhs
)));
10357 -- Create an access type designating the function's result subtype
10359 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
10362 Make_Full_Type_Declaration
(Loc
,
10363 Defining_Identifier
=> Ptr_Typ
,
10365 Make_Access_To_Object_Definition
(Loc
,
10366 All_Present
=> True,
10367 Subtype_Indication
=>
10368 New_Reference_To
(Result_Subt
, Loc
)));
10369 Insert_After_And_Analyze
(Assign
, Ptr_Typ_Decl
);
10371 -- Finally, create an access object initialized to a reference to the
10372 -- function call. We know this access value is non-null, so mark the
10373 -- entity accordingly to suppress junk access checks.
10375 New_Expr
:= Make_Reference
(Loc
, Relocate_Node
(Func_Call
));
10377 Obj_Id
:= Make_Temporary
(Loc
, 'R', New_Expr
);
10378 Set_Etype
(Obj_Id
, Ptr_Typ
);
10379 Set_Is_Known_Non_Null
(Obj_Id
);
10382 Make_Object_Declaration
(Loc
,
10383 Defining_Identifier
=> Obj_Id
,
10384 Object_Definition
=> New_Reference_To
(Ptr_Typ
, Loc
),
10385 Expression
=> New_Expr
);
10386 Insert_After_And_Analyze
(Ptr_Typ_Decl
, Obj_Decl
);
10388 Rewrite
(Assign
, Make_Null_Statement
(Loc
));
10390 -- Retrieve the target of the assignment
10392 if Nkind
(Lhs
) = N_Selected_Component
then
10393 Target
:= Selector_Name
(Lhs
);
10394 elsif Nkind
(Lhs
) = N_Type_Conversion
then
10395 Target
:= Expression
(Lhs
);
10400 -- If we are assigning to a return object or this is an expression of
10401 -- an extension aggregate, the target should either be an identifier
10402 -- or a simple expression. All other cases imply a different scenario.
10404 if Nkind
(Target
) in N_Has_Entity
then
10405 Target
:= Entity
(Target
);
10409 end Make_Build_In_Place_Call_In_Assignment
;
10411 ----------------------------------------------------
10412 -- Make_Build_In_Place_Call_In_Object_Declaration --
10413 ----------------------------------------------------
10415 procedure Make_Build_In_Place_Call_In_Object_Declaration
10416 (Object_Decl
: Node_Id
;
10417 Function_Call
: Node_Id
)
10420 Obj_Def_Id
: constant Entity_Id
:=
10421 Defining_Identifier
(Object_Decl
);
10422 Enclosing_Func
: constant Entity_Id
:=
10423 Enclosing_Subprogram
(Obj_Def_Id
);
10424 Call_Deref
: Node_Id
;
10425 Caller_Object
: Node_Id
;
10426 Def_Id
: Entity_Id
;
10427 Fmaster_Actual
: Node_Id
:= Empty
;
10428 Func_Call
: Node_Id
:= Function_Call
;
10429 Function_Id
: Entity_Id
;
10430 Pool_Actual
: Node_Id
;
10431 Ptr_Typ_Decl
: Node_Id
;
10432 Pass_Caller_Acc
: Boolean := False;
10433 New_Expr
: Node_Id
;
10434 Ref_Type
: Entity_Id
;
10435 Result_Subt
: Entity_Id
;
10438 -- Step past qualification or unchecked conversion (the latter can occur
10439 -- in cases of calls to 'Input).
10441 if Nkind_In
(Func_Call
, N_Qualified_Expression
,
10442 N_Unchecked_Type_Conversion
)
10444 Func_Call
:= Expression
(Func_Call
);
10447 -- If the call has already been processed to add build-in-place actuals
10448 -- then return. This should not normally occur in an object declaration,
10449 -- but we add the protection as a defensive measure.
10451 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
10455 -- Mark the call as processed as a build-in-place call
10457 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
10459 Loc
:= Sloc
(Function_Call
);
10461 if Is_Entity_Name
(Name
(Func_Call
)) then
10462 Function_Id
:= Entity
(Name
(Func_Call
));
10464 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
10465 Function_Id
:= Etype
(Name
(Func_Call
));
10468 raise Program_Error
;
10471 Result_Subt
:= Etype
(Function_Id
);
10473 -- If the the object is a return object of an enclosing build-in-place
10474 -- function, then the implicit build-in-place parameters of the
10475 -- enclosing function are simply passed along to the called function.
10476 -- (Unfortunately, this won't cover the case of extension aggregates
10477 -- where the ancestor part is a build-in-place unconstrained function
10478 -- call that should be passed along the caller's parameters. Currently
10479 -- those get mishandled by reassigning the result of the call to the
10480 -- aggregate return object, when the call result should really be
10481 -- directly built in place in the aggregate and not in a temporary. ???)
10483 if Is_Return_Object
(Defining_Identifier
(Object_Decl
)) then
10484 Pass_Caller_Acc
:= True;
10486 -- When the enclosing function has a BIP_Alloc_Form formal then we
10487 -- pass it along to the callee (such as when the enclosing function
10488 -- has an unconstrained or tagged result type).
10490 if Needs_BIP_Alloc_Form
(Enclosing_Func
) then
10491 if VM_Target
= No_VM
and then
10492 RTE_Available
(RE_Root_Storage_Pool_Ptr
)
10495 New_Reference_To
(Build_In_Place_Formal
10496 (Enclosing_Func
, BIP_Storage_Pool
), Loc
);
10498 -- The build-in-place pool formal is not built on .NET/JVM
10501 Pool_Actual
:= Empty
;
10504 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10509 (Build_In_Place_Formal
(Enclosing_Func
, BIP_Alloc_Form
),
10511 Pool_Actual
=> Pool_Actual
);
10513 -- Otherwise, if enclosing function has a constrained result subtype,
10514 -- then caller allocation will be used.
10517 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10518 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
10521 if Needs_BIP_Finalization_Master
(Enclosing_Func
) then
10524 (Build_In_Place_Formal
10525 (Enclosing_Func
, BIP_Finalization_Master
), Loc
);
10528 -- Retrieve the BIPacc formal from the enclosing function and convert
10529 -- it to the access type of the callee's BIP_Object_Access formal.
10532 Make_Unchecked_Type_Conversion
(Loc
,
10536 (Build_In_Place_Formal
(Function_Id
, BIP_Object_Access
)),
10540 (Build_In_Place_Formal
(Enclosing_Func
, BIP_Object_Access
),
10543 -- In the constrained case, add an implicit actual to the function call
10544 -- that provides access to the declared object. An unchecked conversion
10545 -- to the (specific) result type of the function is inserted to handle
10546 -- the case where the object is declared with a class-wide type.
10548 elsif Is_Constrained
(Underlying_Type
(Result_Subt
)) then
10550 Make_Unchecked_Type_Conversion
(Loc
,
10551 Subtype_Mark
=> New_Reference_To
(Result_Subt
, Loc
),
10552 Expression
=> New_Reference_To
(Obj_Def_Id
, Loc
));
10554 -- When the function has a controlling result, an allocation-form
10555 -- parameter must be passed indicating that the caller is allocating
10556 -- the result object. This is needed because such a function can be
10557 -- called as a dispatching operation and must be treated similarly
10558 -- to functions with unconstrained result subtypes.
10560 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10561 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
10563 -- In other unconstrained cases, pass an indication to do the allocation
10564 -- on the secondary stack and set Caller_Object to Empty so that a null
10565 -- value will be passed for the caller's object address. A transient
10566 -- scope is established to ensure eventual cleanup of the result.
10569 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10570 (Func_Call
, Function_Id
, Alloc_Form
=> Secondary_Stack
);
10571 Caller_Object
:= Empty
;
10573 Establish_Transient_Scope
(Object_Decl
, Sec_Stack
=> True);
10576 -- Pass along any finalization master actual, which is needed in the
10577 -- case where the called function initializes a return object of an
10578 -- enclosing build-in-place function.
10580 Add_Finalization_Master_Actual_To_Build_In_Place_Call
10581 (Func_Call
=> Func_Call
,
10582 Func_Id
=> Function_Id
,
10583 Master_Exp
=> Fmaster_Actual
);
10585 if Nkind
(Parent
(Object_Decl
)) = N_Extended_Return_Statement
10586 and then Has_Task
(Result_Subt
)
10588 -- Here we're passing along the master that was passed in to this
10591 Add_Task_Actuals_To_Build_In_Place_Call
10592 (Func_Call
, Function_Id
,
10594 New_Reference_To
(Build_In_Place_Formal
10595 (Enclosing_Func
, BIP_Task_Master
), Loc
));
10598 Add_Task_Actuals_To_Build_In_Place_Call
10599 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
10602 Add_Access_Actual_To_Build_In_Place_Call
10603 (Func_Call
, Function_Id
, Caller_Object
, Is_Access
=> Pass_Caller_Acc
);
10605 -- Create an access type designating the function's result subtype. We
10606 -- use the type of the original expression because it may be a call to
10607 -- an inherited operation, which the expansion has replaced with the
10608 -- parent operation that yields the parent type.
10610 Ref_Type
:= Make_Temporary
(Loc
, 'A');
10613 Make_Full_Type_Declaration
(Loc
,
10614 Defining_Identifier
=> Ref_Type
,
10616 Make_Access_To_Object_Definition
(Loc
,
10617 All_Present
=> True,
10618 Subtype_Indication
=>
10619 New_Reference_To
(Etype
(Function_Call
), Loc
)));
10621 -- The access type and its accompanying object must be inserted after
10622 -- the object declaration in the constrained case, so that the function
10623 -- call can be passed access to the object. In the unconstrained case,
10624 -- or if the object declaration is for a return object, the access type
10625 -- and object must be inserted before the object, since the object
10626 -- declaration is rewritten to be a renaming of a dereference of the
10629 if Is_Constrained
(Underlying_Type
(Result_Subt
))
10630 and then not Is_Return_Object
(Defining_Identifier
(Object_Decl
))
10632 Insert_After_And_Analyze
(Object_Decl
, Ptr_Typ_Decl
);
10634 Insert_Action
(Object_Decl
, Ptr_Typ_Decl
);
10637 -- Finally, create an access object initialized to a reference to the
10638 -- function call. We know this access value cannot be null, so mark the
10639 -- entity accordingly to suppress the access check.
10641 New_Expr
:= Make_Reference
(Loc
, Relocate_Node
(Func_Call
));
10643 Def_Id
:= Make_Temporary
(Loc
, 'R', New_Expr
);
10644 Set_Etype
(Def_Id
, Ref_Type
);
10645 Set_Is_Known_Non_Null
(Def_Id
);
10647 Insert_After_And_Analyze
(Ptr_Typ_Decl
,
10648 Make_Object_Declaration
(Loc
,
10649 Defining_Identifier
=> Def_Id
,
10650 Object_Definition
=> New_Reference_To
(Ref_Type
, Loc
),
10651 Expression
=> New_Expr
));
10653 -- If the result subtype of the called function is constrained and
10654 -- is not itself the return expression of an enclosing BIP function,
10655 -- then mark the object as having no initialization.
10657 if Is_Constrained
(Underlying_Type
(Result_Subt
))
10658 and then not Is_Return_Object
(Defining_Identifier
(Object_Decl
))
10660 Set_Expression
(Object_Decl
, Empty
);
10661 Set_No_Initialization
(Object_Decl
);
10663 -- In case of an unconstrained result subtype, or if the call is the
10664 -- return expression of an enclosing BIP function, rewrite the object
10665 -- declaration as an object renaming where the renamed object is a
10666 -- dereference of <function_Call>'reference:
10668 -- Obj : Subt renames <function_call>'Ref.all;
10672 Make_Explicit_Dereference
(Loc
,
10673 Prefix
=> New_Reference_To
(Def_Id
, Loc
));
10675 Loc
:= Sloc
(Object_Decl
);
10676 Rewrite
(Object_Decl
,
10677 Make_Object_Renaming_Declaration
(Loc
,
10678 Defining_Identifier
=> Make_Temporary
(Loc
, 'D'),
10679 Access_Definition
=> Empty
,
10680 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Loc
),
10681 Name
=> Call_Deref
));
10683 Set_Renamed_Object
(Defining_Identifier
(Object_Decl
), Call_Deref
);
10685 Analyze
(Object_Decl
);
10687 -- Replace the internal identifier of the renaming declaration's
10688 -- entity with identifier of the original object entity. We also have
10689 -- to exchange the entities containing their defining identifiers to
10690 -- ensure the correct replacement of the object declaration by the
10691 -- object renaming declaration to avoid homograph conflicts (since
10692 -- the object declaration's defining identifier was already entered
10693 -- in current scope). The Next_Entity links of the two entities also
10694 -- have to be swapped since the entities are part of the return
10695 -- scope's entity list and the list structure would otherwise be
10696 -- corrupted. Finally, the homonym chain must be preserved as well.
10699 Renaming_Def_Id
: constant Entity_Id
:=
10700 Defining_Identifier
(Object_Decl
);
10701 Next_Entity_Temp
: constant Entity_Id
:=
10702 Next_Entity
(Renaming_Def_Id
);
10704 Set_Chars
(Renaming_Def_Id
, Chars
(Obj_Def_Id
));
10706 -- Swap next entity links in preparation for exchanging entities
10708 Set_Next_Entity
(Renaming_Def_Id
, Next_Entity
(Obj_Def_Id
));
10709 Set_Next_Entity
(Obj_Def_Id
, Next_Entity_Temp
);
10710 Set_Homonym
(Renaming_Def_Id
, Homonym
(Obj_Def_Id
));
10712 Exchange_Entities
(Renaming_Def_Id
, Obj_Def_Id
);
10714 -- Preserve source indication of original declaration, so that
10715 -- xref information is properly generated for the right entity.
10717 Preserve_Comes_From_Source
10718 (Object_Decl
, Original_Node
(Object_Decl
));
10720 Preserve_Comes_From_Source
10721 (Obj_Def_Id
, Original_Node
(Object_Decl
));
10723 Set_Comes_From_Source
(Renaming_Def_Id
, False);
10727 -- If the object entity has a class-wide Etype, then we need to change
10728 -- it to the result subtype of the function call, because otherwise the
10729 -- object will be class-wide without an explicit initialization and
10730 -- won't be allocated properly by the back end. It seems unclean to make
10731 -- such a revision to the type at this point, and we should try to
10732 -- improve this treatment when build-in-place functions with class-wide
10733 -- results are implemented. ???
10735 if Is_Class_Wide_Type
(Etype
(Defining_Identifier
(Object_Decl
))) then
10736 Set_Etype
(Defining_Identifier
(Object_Decl
), Result_Subt
);
10738 end Make_Build_In_Place_Call_In_Object_Declaration
;
10740 --------------------------------------------
10741 -- Make_CPP_Constructor_Call_In_Allocator --
10742 --------------------------------------------
10744 procedure Make_CPP_Constructor_Call_In_Allocator
10745 (Allocator
: Node_Id
;
10746 Function_Call
: Node_Id
)
10748 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
10749 Acc_Type
: constant Entity_Id
:= Etype
(Allocator
);
10750 Function_Id
: constant Entity_Id
:= Entity
(Name
(Function_Call
));
10751 Result_Subt
: constant Entity_Id
:= Available_View
(Etype
(Function_Id
));
10753 New_Allocator
: Node_Id
;
10754 Return_Obj_Access
: Entity_Id
;
10758 pragma Assert
(Nkind
(Allocator
) = N_Allocator
10759 and then Nkind
(Function_Call
) = N_Function_Call
);
10760 pragma Assert
(Convention
(Function_Id
) = Convention_CPP
10761 and then Is_Constructor
(Function_Id
));
10762 pragma Assert
(Is_Constrained
(Underlying_Type
(Result_Subt
)));
10764 -- Replace the initialized allocator of form "new T'(Func (...))" with
10765 -- an uninitialized allocator of form "new T", where T is the result
10766 -- subtype of the called function. The call to the function is handled
10767 -- separately further below.
10770 Make_Allocator
(Loc
,
10771 Expression
=> New_Reference_To
(Result_Subt
, Loc
));
10772 Set_No_Initialization
(New_Allocator
);
10774 -- Copy attributes to new allocator. Note that the new allocator
10775 -- logically comes from source if the original one did, so copy the
10776 -- relevant flag. This ensures proper treatment of the restriction
10777 -- No_Implicit_Heap_Allocations in this case.
10779 Set_Storage_Pool
(New_Allocator
, Storage_Pool
(Allocator
));
10780 Set_Procedure_To_Call
(New_Allocator
, Procedure_To_Call
(Allocator
));
10781 Set_Comes_From_Source
(New_Allocator
, Comes_From_Source
(Allocator
));
10783 Rewrite
(Allocator
, New_Allocator
);
10785 -- Create a new access object and initialize it to the result of the
10786 -- new uninitialized allocator. Note: we do not use Allocator as the
10787 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
10788 -- as this would create a sort of infinite "recursion".
10790 Return_Obj_Access
:= Make_Temporary
(Loc
, 'R');
10791 Set_Etype
(Return_Obj_Access
, Acc_Type
);
10794 -- Rnnn : constant ptr_T := new (T);
10795 -- Init (Rnn.all,...);
10798 Make_Object_Declaration
(Loc
,
10799 Defining_Identifier
=> Return_Obj_Access
,
10800 Constant_Present
=> True,
10801 Object_Definition
=> New_Reference_To
(Acc_Type
, Loc
),
10802 Expression
=> Relocate_Node
(Allocator
));
10803 Insert_Action
(Allocator
, Tmp_Obj
);
10805 Insert_List_After_And_Analyze
(Tmp_Obj
,
10806 Build_Initialization_Call
(Loc
,
10808 Make_Explicit_Dereference
(Loc
,
10809 Prefix
=> New_Reference_To
(Return_Obj_Access
, Loc
)),
10810 Typ
=> Etype
(Function_Id
),
10811 Constructor_Ref
=> Function_Call
));
10813 -- Finally, replace the allocator node with a reference to the result of
10814 -- the function call itself (which will effectively be an access to the
10815 -- object created by the allocator).
10817 Rewrite
(Allocator
, New_Reference_To
(Return_Obj_Access
, Loc
));
10819 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
10820 -- generate an implicit conversion to force displacement of the "this"
10823 if Is_Interface
(Designated_Type
(Acc_Type
)) then
10824 Rewrite
(Allocator
, Convert_To
(Acc_Type
, Relocate_Node
(Allocator
)));
10827 Analyze_And_Resolve
(Allocator
, Acc_Type
);
10828 end Make_CPP_Constructor_Call_In_Allocator
;
10830 -----------------------------------
10831 -- Needs_BIP_Finalization_Master --
10832 -----------------------------------
10834 function Needs_BIP_Finalization_Master
10835 (Func_Id
: Entity_Id
) return Boolean
10837 pragma Assert
(Is_Build_In_Place_Function
(Func_Id
));
10838 Func_Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
10841 not Restriction_Active
(No_Finalization
)
10842 and then Needs_Finalization
(Func_Typ
);
10843 end Needs_BIP_Finalization_Master
;
10845 --------------------------
10846 -- Needs_BIP_Alloc_Form --
10847 --------------------------
10849 function Needs_BIP_Alloc_Form
(Func_Id
: Entity_Id
) return Boolean is
10850 pragma Assert
(Is_Build_In_Place_Function
(Func_Id
));
10851 Func_Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
10853 return not Is_Constrained
(Func_Typ
) or else Is_Tagged_Type
(Func_Typ
);
10854 end Needs_BIP_Alloc_Form
;
10856 --------------------------------------
10857 -- Needs_Result_Accessibility_Level --
10858 --------------------------------------
10860 function Needs_Result_Accessibility_Level
10861 (Func_Id
: Entity_Id
) return Boolean
10863 Func_Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
10865 function Has_Unconstrained_Access_Discriminant_Component
10866 (Comp_Typ
: Entity_Id
) return Boolean;
10867 -- Returns True if any component of the type has an unconstrained access
10870 -----------------------------------------------------
10871 -- Has_Unconstrained_Access_Discriminant_Component --
10872 -----------------------------------------------------
10874 function Has_Unconstrained_Access_Discriminant_Component
10875 (Comp_Typ
: Entity_Id
) return Boolean
10878 if not Is_Limited_Type
(Comp_Typ
) then
10881 -- Only limited types can have access discriminants with
10884 elsif Has_Unconstrained_Access_Discriminants
(Comp_Typ
) then
10887 elsif Is_Array_Type
(Comp_Typ
) then
10888 return Has_Unconstrained_Access_Discriminant_Component
10889 (Underlying_Type
(Component_Type
(Comp_Typ
)));
10891 elsif Is_Record_Type
(Comp_Typ
) then
10896 Comp
:= First_Component
(Comp_Typ
);
10897 while Present
(Comp
) loop
10898 if Has_Unconstrained_Access_Discriminant_Component
10899 (Underlying_Type
(Etype
(Comp
)))
10904 Next_Component
(Comp
);
10910 end Has_Unconstrained_Access_Discriminant_Component
;
10912 Feature_Disabled
: constant Boolean := True;
10915 -- Start of processing for Needs_Result_Accessibility_Level
10918 -- False if completion unavailable (how does this happen???)
10920 if not Present
(Func_Typ
) then
10923 elsif Feature_Disabled
then
10926 -- False if not a function, also handle enum-lit renames case
10928 elsif Func_Typ
= Standard_Void_Type
10929 or else Is_Scalar_Type
(Func_Typ
)
10933 -- Handle a corner case, a cross-dialect subp renaming. For example,
10934 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
10935 -- an Ada 2005 (or earlier) unit references predefined run-time units.
10937 elsif Present
(Alias
(Func_Id
)) then
10939 -- Unimplemented: a cross-dialect subp renaming which does not set
10940 -- the Alias attribute (e.g., a rename of a dereference of an access
10941 -- to subprogram value). ???
10943 return Present
(Extra_Accessibility_Of_Result
(Alias
(Func_Id
)));
10945 -- Remaining cases require Ada 2012 mode
10947 elsif Ada_Version
< Ada_2012
then
10950 elsif Ekind
(Func_Typ
) = E_Anonymous_Access_Type
10951 or else Is_Tagged_Type
(Func_Typ
)
10953 -- In the case of, say, a null tagged record result type, the need
10954 -- for this extra parameter might not be obvious. This function
10955 -- returns True for all tagged types for compatibility reasons.
10956 -- A function with, say, a tagged null controlling result type might
10957 -- be overridden by a primitive of an extension having an access
10958 -- discriminant and the overrider and overridden must have compatible
10959 -- calling conventions (including implicitly declared parameters).
10960 -- Similarly, values of one access-to-subprogram type might designate
10961 -- both a primitive subprogram of a given type and a function
10962 -- which is, for example, not a primitive subprogram of any type.
10963 -- Again, this requires calling convention compatibility.
10964 -- It might be possible to solve these issues by introducing
10965 -- wrappers, but that is not the approach that was chosen.
10969 elsif Has_Unconstrained_Access_Discriminants
(Func_Typ
) then
10972 elsif Has_Unconstrained_Access_Discriminant_Component
(Func_Typ
) then
10975 -- False for all other cases
10980 end Needs_Result_Accessibility_Level
;
10982 ------------------------
10983 -- List_Inlining_Info --
10984 ------------------------
10986 procedure List_Inlining_Info
is
10992 if not Debug_Flag_Dot_J
then
10996 -- Generate listing of calls inlined by the frontend
10998 if Present
(Inlined_Calls
) then
11000 Elmt
:= First_Elmt
(Inlined_Calls
);
11001 while Present
(Elmt
) loop
11002 Nod
:= Node
(Elmt
);
11004 if In_Extended_Main_Code_Unit
(Nod
) then
11005 Count
:= Count
+ 1;
11008 Write_Str
("Listing of frontend inlined calls");
11015 Write_Location
(Sloc
(Nod
));
11024 -- Generate listing of calls passed to the backend
11026 if Present
(Backend_Calls
) then
11029 Elmt
:= First_Elmt
(Backend_Calls
);
11030 while Present
(Elmt
) loop
11031 Nod
:= Node
(Elmt
);
11033 if In_Extended_Main_Code_Unit
(Nod
) then
11034 Count
:= Count
+ 1;
11037 Write_Str
("Listing of inlined calls passed to the backend");
11044 Write_Location
(Sloc
(Nod
));
11051 end List_Inlining_Info
;