1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with 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_Prag
; use Exp_Prag
;
45 with Exp_Tss
; use Exp_Tss
;
46 with Exp_Util
; use Exp_Util
;
47 with Exp_VFpt
; use Exp_VFpt
;
48 with Fname
; use Fname
;
49 with Freeze
; use Freeze
;
50 with Inline
; use Inline
;
52 with Namet
; use Namet
;
53 with Nlists
; use Nlists
;
54 with Nmake
; use Nmake
;
56 with Output
; use Output
;
57 with Restrict
; use Restrict
;
58 with Rident
; use Rident
;
59 with Rtsfind
; use Rtsfind
;
61 with Sem_Aux
; use Sem_Aux
;
62 with Sem_Ch6
; use Sem_Ch6
;
63 with Sem_Ch8
; use Sem_Ch8
;
64 with Sem_Ch12
; use Sem_Ch12
;
65 with Sem_Ch13
; use Sem_Ch13
;
66 with Sem_Dim
; use Sem_Dim
;
67 with Sem_Disp
; use Sem_Disp
;
68 with Sem_Dist
; use Sem_Dist
;
69 with Sem_Eval
; use Sem_Eval
;
70 with Sem_Mech
; use Sem_Mech
;
71 with Sem_Res
; use Sem_Res
;
72 with Sem_SCIL
; use Sem_SCIL
;
73 with Sem_Util
; use Sem_Util
;
74 with Sinfo
; use Sinfo
;
75 with Sinput
; use Sinput
;
76 with Snames
; use Snames
;
77 with Stand
; use Stand
;
78 with Stringt
; use Stringt
;
79 with Targparm
; use Targparm
;
80 with Tbuild
; use Tbuild
;
81 with Uintp
; use Uintp
;
82 with Validsw
; use Validsw
;
84 package body Exp_Ch6
is
86 Inlined_Calls
: Elist_Id
:= No_Elist
;
87 Backend_Calls
: Elist_Id
:= No_Elist
;
88 -- List of frontend inlined calls and inline calls passed to the backend
90 -----------------------
91 -- Local Subprograms --
92 -----------------------
94 procedure Add_Access_Actual_To_Build_In_Place_Call
95 (Function_Call
: Node_Id
;
96 Function_Id
: Entity_Id
;
97 Return_Object
: Node_Id
;
98 Is_Access
: Boolean := False);
99 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
100 -- object name given by Return_Object and add the attribute to the end of
101 -- the actual parameter list associated with the build-in-place function
102 -- call denoted by Function_Call. However, if Is_Access is True, then
103 -- Return_Object is already an access expression, in which case it's passed
104 -- along directly to the build-in-place function. Finally, if Return_Object
105 -- is empty, then pass a null literal as the actual.
107 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
108 (Function_Call
: Node_Id
;
109 Function_Id
: Entity_Id
;
110 Alloc_Form
: BIP_Allocation_Form
:= Unspecified
;
111 Alloc_Form_Exp
: Node_Id
:= Empty
;
112 Pool_Actual
: Node_Id
:= Make_Null
(No_Location
));
113 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
114 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
115 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
116 -- otherwise pass a literal corresponding to the Alloc_Form parameter
117 -- (which must not be Unspecified in that case). Pool_Actual is the
118 -- parameter to pass to BIP_Storage_Pool.
120 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
121 (Func_Call
: Node_Id
;
123 Ptr_Typ
: Entity_Id
:= Empty
;
124 Master_Exp
: Node_Id
:= Empty
);
125 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
126 -- finalization actions, add an actual parameter which is a pointer to the
127 -- finalization master of the caller. If Master_Exp is not Empty, then that
128 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
129 -- will result in an automatic "null" value for the actual.
131 procedure Add_Task_Actuals_To_Build_In_Place_Call
132 (Function_Call
: Node_Id
;
133 Function_Id
: Entity_Id
;
134 Master_Actual
: Node_Id
);
135 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
136 -- contains tasks, add two actual parameters: the master, and a pointer to
137 -- the caller's activation chain. Master_Actual is the actual parameter
138 -- expression to pass for the master. In most cases, this is the current
139 -- master (_master). The two exceptions are: If the function call is the
140 -- initialization expression for an allocator, we pass the master of the
141 -- access type. If the function call is the initialization expression for a
142 -- return object, we pass along the master passed in by the caller. The
143 -- activation chain to pass is always the local one. Note: Master_Actual
144 -- can be Empty, but only if there are no tasks.
146 procedure Check_Overriding_Operation
(Subp
: Entity_Id
);
147 -- Subp is a dispatching operation. Check whether it may override an
148 -- inherited private operation, in which case its DT entry is that of
149 -- the hidden operation, not the one it may have received earlier.
150 -- This must be done before emitting the code to set the corresponding
151 -- DT to the address of the subprogram. The actual placement of Subp in
152 -- the proper place in the list of primitive operations is done in
153 -- Declare_Inherited_Private_Subprograms, which also has to deal with
154 -- implicit operations. This duplication is unavoidable for now???
156 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
);
157 -- This procedure is called only if the subprogram body N, whose spec
158 -- has the given entity Spec, contains a parameterless recursive call.
159 -- It attempts to generate runtime code to detect if this a case of
160 -- infinite recursion.
162 -- The body is scanned to determine dependencies. If the only external
163 -- dependencies are on a small set of scalar variables, then the values
164 -- of these variables are captured on entry to the subprogram, and if
165 -- the values are not changed for the call, we know immediately that
166 -- we have an infinite recursion.
168 procedure Expand_Ctrl_Function_Call
(N
: Node_Id
);
169 -- N is a function call which returns a controlled object. Transform the
170 -- call into a temporary which retrieves the returned object from the
171 -- secondary stack using 'reference.
173 procedure Expand_Inlined_Call
176 Orig_Subp
: Entity_Id
);
177 -- If called subprogram can be inlined by the front-end, retrieve the
178 -- analyzed body, replace formals with actuals and expand call in place.
179 -- Generate thunks for actuals that are expressions, and insert the
180 -- corresponding constant declarations before the call. If the original
181 -- call is to a derived operation, the return type is the one of the
182 -- derived operation, but the body is that of the original, so return
183 -- expressions in the body must be converted to the desired type (which
184 -- is simply not noted in the tree without inline expansion).
186 procedure Expand_Non_Function_Return
(N
: Node_Id
);
187 -- Called by Expand_N_Simple_Return_Statement in case we're returning from
188 -- a procedure body, entry body, accept statement, or extended return
189 -- statement. Note that all non-function returns are simple return
192 function Expand_Protected_Object_Reference
194 Scop
: Entity_Id
) return Node_Id
;
196 procedure Expand_Protected_Subprogram_Call
200 -- A call to a protected subprogram within the protected object may appear
201 -- as a regular call. The list of actuals must be expanded to contain a
202 -- reference to the object itself, and the call becomes a call to the
203 -- corresponding protected subprogram.
205 function Has_Unconstrained_Access_Discriminants
206 (Subtyp
: Entity_Id
) return Boolean;
207 -- Returns True if the given subtype is unconstrained and has one
208 -- or more access discriminants.
210 procedure Expand_Simple_Function_Return
(N
: Node_Id
);
211 -- Expand simple return from function. In the case where we are returning
212 -- from a function body this is called by Expand_N_Simple_Return_Statement.
214 ----------------------------------------------
215 -- Add_Access_Actual_To_Build_In_Place_Call --
216 ----------------------------------------------
218 procedure Add_Access_Actual_To_Build_In_Place_Call
219 (Function_Call
: Node_Id
;
220 Function_Id
: Entity_Id
;
221 Return_Object
: Node_Id
;
222 Is_Access
: Boolean := False)
224 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
225 Obj_Address
: Node_Id
;
226 Obj_Acc_Formal
: Entity_Id
;
229 -- Locate the implicit access parameter in the called function
231 Obj_Acc_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Object_Access
);
233 -- If no return object is provided, then pass null
235 if not Present
(Return_Object
) then
236 Obj_Address
:= Make_Null
(Loc
);
237 Set_Parent
(Obj_Address
, Function_Call
);
239 -- If Return_Object is already an expression of an access type, then use
240 -- it directly, since it must be an access value denoting the return
241 -- object, and couldn't possibly be the return object itself.
244 Obj_Address
:= Return_Object
;
245 Set_Parent
(Obj_Address
, Function_Call
);
247 -- Apply Unrestricted_Access to caller's return object
251 Make_Attribute_Reference
(Loc
,
252 Prefix
=> Return_Object
,
253 Attribute_Name
=> Name_Unrestricted_Access
);
255 Set_Parent
(Return_Object
, Obj_Address
);
256 Set_Parent
(Obj_Address
, Function_Call
);
259 Analyze_And_Resolve
(Obj_Address
, Etype
(Obj_Acc_Formal
));
261 -- Build the parameter association for the new actual and add it to the
262 -- end of the function's actuals.
264 Add_Extra_Actual_To_Call
(Function_Call
, Obj_Acc_Formal
, Obj_Address
);
265 end Add_Access_Actual_To_Build_In_Place_Call
;
267 ------------------------------------------------------
268 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
269 ------------------------------------------------------
271 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
272 (Function_Call
: Node_Id
;
273 Function_Id
: Entity_Id
;
274 Alloc_Form
: BIP_Allocation_Form
:= Unspecified
;
275 Alloc_Form_Exp
: Node_Id
:= Empty
;
276 Pool_Actual
: Node_Id
:= Make_Null
(No_Location
))
278 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
279 Alloc_Form_Actual
: Node_Id
;
280 Alloc_Form_Formal
: Node_Id
;
281 Pool_Formal
: Node_Id
;
284 -- The allocation form generally doesn't need to be passed in the case
285 -- of a constrained result subtype, since normally the caller performs
286 -- the allocation in that case. However this formal is still needed in
287 -- the case where the function has a tagged result, because generally
288 -- such functions can be called in a dispatching context and such calls
289 -- must be handled like calls to class-wide functions.
291 if Is_Constrained
(Underlying_Type
(Etype
(Function_Id
)))
292 and then not Is_Tagged_Type
(Underlying_Type
(Etype
(Function_Id
)))
297 -- Locate the implicit allocation form parameter in the called function.
298 -- Maybe it would be better for each implicit formal of a build-in-place
299 -- function to have a flag or a Uint attribute to identify it. ???
301 Alloc_Form_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Alloc_Form
);
303 if Present
(Alloc_Form_Exp
) then
304 pragma Assert
(Alloc_Form
= Unspecified
);
306 Alloc_Form_Actual
:= Alloc_Form_Exp
;
309 pragma Assert
(Alloc_Form
/= Unspecified
);
312 Make_Integer_Literal
(Loc
,
313 Intval
=> UI_From_Int
(BIP_Allocation_Form
'Pos (Alloc_Form
)));
316 Analyze_And_Resolve
(Alloc_Form_Actual
, Etype
(Alloc_Form_Formal
));
318 -- Build the parameter association for the new actual and add it to the
319 -- end of the function's actuals.
321 Add_Extra_Actual_To_Call
322 (Function_Call
, Alloc_Form_Formal
, Alloc_Form_Actual
);
324 -- Pass the Storage_Pool parameter. This parameter is omitted on
325 -- .NET/JVM/ZFP as those targets do not support pools.
328 and then RTE_Available
(RE_Root_Storage_Pool_Ptr
)
330 Pool_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Storage_Pool
);
331 Analyze_And_Resolve
(Pool_Actual
, Etype
(Pool_Formal
));
332 Add_Extra_Actual_To_Call
333 (Function_Call
, Pool_Formal
, Pool_Actual
);
335 end Add_Unconstrained_Actuals_To_Build_In_Place_Call
;
337 -----------------------------------------------------------
338 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
339 -----------------------------------------------------------
341 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
342 (Func_Call
: Node_Id
;
344 Ptr_Typ
: Entity_Id
:= Empty
;
345 Master_Exp
: Node_Id
:= Empty
)
348 if not Needs_BIP_Finalization_Master
(Func_Id
) then
353 Formal
: constant Entity_Id
:=
354 Build_In_Place_Formal
(Func_Id
, BIP_Finalization_Master
);
355 Loc
: constant Source_Ptr
:= Sloc
(Func_Call
);
358 Desig_Typ
: Entity_Id
;
361 -- If there is a finalization master actual, such as the implicit
362 -- finalization master of an enclosing build-in-place function,
363 -- then this must be added as an extra actual of the call.
365 if Present
(Master_Exp
) then
366 Actual
:= Master_Exp
;
368 -- Case where the context does not require an actual master
370 elsif No
(Ptr_Typ
) then
371 Actual
:= Make_Null
(Loc
);
374 Desig_Typ
:= Directly_Designated_Type
(Ptr_Typ
);
376 -- Check for a library-level access type whose designated type has
377 -- supressed finalization. Such an access types lack a master.
378 -- Pass a null actual to the callee in order to signal a missing
381 if Is_Library_Level_Entity
(Ptr_Typ
)
382 and then Finalize_Storage_Only
(Desig_Typ
)
384 Actual
:= Make_Null
(Loc
);
386 -- Types in need of finalization actions
388 elsif Needs_Finalization
(Desig_Typ
) then
390 -- The general mechanism of creating finalization masters for
391 -- anonymous access types is disabled by default, otherwise
392 -- finalization masters will pop all over the place. Such types
393 -- use context-specific masters.
395 if Ekind
(Ptr_Typ
) = E_Anonymous_Access_Type
396 and then No
(Finalization_Master
(Ptr_Typ
))
398 Build_Finalization_Master
400 Ins_Node
=> Associated_Node_For_Itype
(Ptr_Typ
),
401 Encl_Scope
=> Scope
(Ptr_Typ
));
404 -- Access-to-controlled types should always have a master
406 pragma Assert
(Present
(Finalization_Master
(Ptr_Typ
)));
409 Make_Attribute_Reference
(Loc
,
411 New_Occurrence_Of
(Finalization_Master
(Ptr_Typ
), Loc
),
412 Attribute_Name
=> Name_Unrestricted_Access
);
417 Actual
:= Make_Null
(Loc
);
421 Analyze_And_Resolve
(Actual
, Etype
(Formal
));
423 -- Build the parameter association for the new actual and add it to
424 -- the end of the function's actuals.
426 Add_Extra_Actual_To_Call
(Func_Call
, Formal
, Actual
);
428 end Add_Finalization_Master_Actual_To_Build_In_Place_Call
;
430 ------------------------------
431 -- Add_Extra_Actual_To_Call --
432 ------------------------------
434 procedure Add_Extra_Actual_To_Call
435 (Subprogram_Call
: Node_Id
;
436 Extra_Formal
: Entity_Id
;
437 Extra_Actual
: Node_Id
)
439 Loc
: constant Source_Ptr
:= Sloc
(Subprogram_Call
);
440 Param_Assoc
: Node_Id
;
444 Make_Parameter_Association
(Loc
,
445 Selector_Name
=> New_Occurrence_Of
(Extra_Formal
, Loc
),
446 Explicit_Actual_Parameter
=> Extra_Actual
);
448 Set_Parent
(Param_Assoc
, Subprogram_Call
);
449 Set_Parent
(Extra_Actual
, Param_Assoc
);
451 if Present
(Parameter_Associations
(Subprogram_Call
)) then
452 if Nkind
(Last
(Parameter_Associations
(Subprogram_Call
))) =
453 N_Parameter_Association
456 -- Find last named actual, and append
461 L
:= First_Actual
(Subprogram_Call
);
462 while Present
(L
) loop
463 if No
(Next_Actual
(L
)) then
464 Set_Next_Named_Actual
(Parent
(L
), Extra_Actual
);
472 Set_First_Named_Actual
(Subprogram_Call
, Extra_Actual
);
475 Append
(Param_Assoc
, To
=> Parameter_Associations
(Subprogram_Call
));
478 Set_Parameter_Associations
(Subprogram_Call
, New_List
(Param_Assoc
));
479 Set_First_Named_Actual
(Subprogram_Call
, Extra_Actual
);
481 end Add_Extra_Actual_To_Call
;
483 ---------------------------------------------
484 -- Add_Task_Actuals_To_Build_In_Place_Call --
485 ---------------------------------------------
487 procedure Add_Task_Actuals_To_Build_In_Place_Call
488 (Function_Call
: Node_Id
;
489 Function_Id
: Entity_Id
;
490 Master_Actual
: Node_Id
)
492 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
493 Result_Subt
: constant Entity_Id
:=
494 Available_View
(Etype
(Function_Id
));
496 Chain_Actual
: Node_Id
;
497 Chain_Formal
: Node_Id
;
498 Master_Formal
: Node_Id
;
501 -- No such extra parameters are needed if there are no tasks
503 if not Has_Task
(Result_Subt
) then
507 Actual
:= Master_Actual
;
509 -- Use a dummy _master actual in case of No_Task_Hierarchy
511 if Restriction_Active
(No_Task_Hierarchy
) then
512 Actual
:= New_Occurrence_Of
(RTE
(RE_Library_Task_Level
), Loc
);
514 -- In the case where we use the master associated with an access type,
515 -- the actual is an entity and requires an explicit reference.
517 elsif Nkind
(Actual
) = N_Defining_Identifier
then
518 Actual
:= New_Occurrence_Of
(Actual
, Loc
);
521 -- Locate the implicit master parameter in the called function
523 Master_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Task_Master
);
524 Analyze_And_Resolve
(Actual
, Etype
(Master_Formal
));
526 -- Build the parameter association for the new actual and add it to the
527 -- end of the function's actuals.
529 Add_Extra_Actual_To_Call
(Function_Call
, Master_Formal
, Actual
);
531 -- Locate the implicit activation chain parameter in the called function
534 Build_In_Place_Formal
(Function_Id
, BIP_Activation_Chain
);
536 -- Create the actual which is a pointer to the current activation chain
539 Make_Attribute_Reference
(Loc
,
540 Prefix
=> Make_Identifier
(Loc
, Name_uChain
),
541 Attribute_Name
=> Name_Unrestricted_Access
);
543 Analyze_And_Resolve
(Chain_Actual
, Etype
(Chain_Formal
));
545 -- Build the parameter association for the new actual and add it to the
546 -- end of the function's actuals.
548 Add_Extra_Actual_To_Call
(Function_Call
, Chain_Formal
, Chain_Actual
);
549 end Add_Task_Actuals_To_Build_In_Place_Call
;
551 -----------------------
552 -- BIP_Formal_Suffix --
553 -----------------------
555 function BIP_Formal_Suffix
(Kind
: BIP_Formal_Kind
) return String is
558 when BIP_Alloc_Form
=>
560 when BIP_Storage_Pool
=>
561 return "BIPstoragepool";
562 when BIP_Finalization_Master
=>
563 return "BIPfinalizationmaster";
564 when BIP_Task_Master
=>
565 return "BIPtaskmaster";
566 when BIP_Activation_Chain
=>
567 return "BIPactivationchain";
568 when BIP_Object_Access
=>
571 end BIP_Formal_Suffix
;
573 ---------------------------
574 -- Build_In_Place_Formal --
575 ---------------------------
577 function Build_In_Place_Formal
579 Kind
: BIP_Formal_Kind
) return Entity_Id
581 Formal_Name
: constant Name_Id
:=
583 (Chars
(Func
), BIP_Formal_Suffix
(Kind
));
584 Extra_Formal
: Entity_Id
:= Extra_Formals
(Func
);
587 -- Maybe it would be better for each implicit formal of a build-in-place
588 -- function to have a flag or a Uint attribute to identify it. ???
590 -- The return type in the function declaration may have been a limited
591 -- view, and the extra formals for the function were not generated at
592 -- that point. At the point of call the full view must be available and
593 -- the extra formals can be created.
595 if No
(Extra_Formal
) then
596 Create_Extra_Formals
(Func
);
597 Extra_Formal
:= Extra_Formals
(Func
);
601 pragma Assert
(Present
(Extra_Formal
));
602 exit when Chars
(Extra_Formal
) = Formal_Name
;
604 Next_Formal_With_Extras
(Extra_Formal
);
608 end Build_In_Place_Formal
;
610 --------------------------------
611 -- Check_Overriding_Operation --
612 --------------------------------
614 procedure Check_Overriding_Operation
(Subp
: Entity_Id
) is
615 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
616 Op_List
: constant Elist_Id
:= Primitive_Operations
(Typ
);
622 if Is_Derived_Type
(Typ
)
623 and then not Is_Private_Type
(Typ
)
624 and then In_Open_Scopes
(Scope
(Etype
(Typ
)))
625 and then Is_Base_Type
(Typ
)
627 -- Subp overrides an inherited private operation if there is an
628 -- inherited operation with a different name than Subp (see
629 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
630 -- same name as Subp.
632 Op_Elmt
:= First_Elmt
(Op_List
);
633 while Present
(Op_Elmt
) loop
634 Prim_Op
:= Node
(Op_Elmt
);
635 Par_Op
:= Alias
(Prim_Op
);
638 and then not Comes_From_Source
(Prim_Op
)
639 and then Chars
(Prim_Op
) /= Chars
(Par_Op
)
640 and then Chars
(Par_Op
) = Chars
(Subp
)
641 and then Is_Hidden
(Par_Op
)
642 and then Type_Conformant
(Prim_Op
, Subp
)
644 Set_DT_Position
(Subp
, DT_Position
(Prim_Op
));
650 end Check_Overriding_Operation
;
652 -------------------------------
653 -- Detect_Infinite_Recursion --
654 -------------------------------
656 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
) is
657 Loc
: constant Source_Ptr
:= Sloc
(N
);
659 Var_List
: constant Elist_Id
:= New_Elmt_List
;
660 -- List of globals referenced by body of procedure
662 Call_List
: constant Elist_Id
:= New_Elmt_List
;
663 -- List of recursive calls in body of procedure
665 Shad_List
: constant Elist_Id
:= New_Elmt_List
;
666 -- List of entity id's for entities created to capture the value of
667 -- referenced globals on entry to the procedure.
669 Scop
: constant Uint
:= Scope_Depth
(Spec
);
670 -- This is used to record the scope depth of the current procedure, so
671 -- that we can identify global references.
673 Max_Vars
: constant := 4;
674 -- Do not test more than four global variables
676 Count_Vars
: Natural := 0;
677 -- Count variables found so far
689 function Process
(Nod
: Node_Id
) return Traverse_Result
;
690 -- Function to traverse the subprogram body (using Traverse_Func)
696 function Process
(Nod
: Node_Id
) return Traverse_Result
is
700 if Nkind
(Nod
) = N_Procedure_Call_Statement
then
702 -- Case of one of the detected recursive calls
704 if Is_Entity_Name
(Name
(Nod
))
705 and then Has_Recursive_Call
(Entity
(Name
(Nod
)))
706 and then Entity
(Name
(Nod
)) = Spec
708 Append_Elmt
(Nod
, Call_List
);
711 -- Any other procedure call may have side effects
717 -- A call to a pure function can always be ignored
719 elsif Nkind
(Nod
) = N_Function_Call
720 and then Is_Entity_Name
(Name
(Nod
))
721 and then Is_Pure
(Entity
(Name
(Nod
)))
725 -- Case of an identifier reference
727 elsif Nkind
(Nod
) = N_Identifier
then
730 -- If no entity, then ignore the reference
732 -- Not clear why this can happen. To investigate, remove this
733 -- test and look at the crash that occurs here in 3401-004 ???
738 -- Ignore entities with no Scope, again not clear how this
739 -- can happen, to investigate, look at 4108-008 ???
741 elsif No
(Scope
(Ent
)) then
744 -- Ignore the reference if not to a more global object
746 elsif Scope_Depth
(Scope
(Ent
)) >= Scop
then
749 -- References to types, exceptions and constants are always OK
752 or else Ekind
(Ent
) = E_Exception
753 or else Ekind
(Ent
) = E_Constant
757 -- If other than a non-volatile scalar variable, we have some
758 -- kind of global reference (e.g. to a function) that we cannot
759 -- deal with so we forget the attempt.
761 elsif Ekind
(Ent
) /= E_Variable
762 or else not Is_Scalar_Type
(Etype
(Ent
))
763 or else Treat_As_Volatile
(Ent
)
767 -- Otherwise we have a reference to a global scalar
770 -- Loop through global entities already detected
772 Elm
:= First_Elmt
(Var_List
);
774 -- If not detected before, record this new global reference
777 Count_Vars
:= Count_Vars
+ 1;
779 if Count_Vars
<= Max_Vars
then
780 Append_Elmt
(Entity
(Nod
), Var_List
);
787 -- If recorded before, ignore
789 elsif Node
(Elm
) = Entity
(Nod
) then
792 -- Otherwise keep looking
802 -- For all other node kinds, recursively visit syntactic children
809 function Traverse_Body
is new Traverse_Func
(Process
);
811 -- Start of processing for Detect_Infinite_Recursion
814 -- Do not attempt detection in No_Implicit_Conditional mode, since we
815 -- won't be able to generate the code to handle the recursion in any
818 if Restriction_Active
(No_Implicit_Conditionals
) then
822 -- Otherwise do traversal and quit if we get abandon signal
824 if Traverse_Body
(N
) = Abandon
then
827 -- We must have a call, since Has_Recursive_Call was set. If not just
828 -- ignore (this is only an error check, so if we have a funny situation,
829 -- due to bugs or errors, we do not want to bomb).
831 elsif Is_Empty_Elmt_List
(Call_List
) then
835 -- Here is the case where we detect recursion at compile time
837 -- Push our current scope for analyzing the declarations and code that
838 -- we will insert for the checking.
842 -- This loop builds temporary variables for each of the referenced
843 -- globals, so that at the end of the loop the list Shad_List contains
844 -- these temporaries in one-to-one correspondence with the elements in
848 Elm
:= First_Elmt
(Var_List
);
849 while Present
(Elm
) loop
851 Ent
:= Make_Temporary
(Loc
, 'S');
852 Append_Elmt
(Ent
, Shad_List
);
854 -- Insert a declaration for this temporary at the start of the
855 -- declarations for the procedure. The temporaries are declared as
856 -- constant objects initialized to the current values of the
857 -- corresponding temporaries.
860 Make_Object_Declaration
(Loc
,
861 Defining_Identifier
=> Ent
,
862 Object_Definition
=> New_Occurrence_Of
(Etype
(Var
), Loc
),
863 Constant_Present
=> True,
864 Expression
=> New_Occurrence_Of
(Var
, Loc
));
867 Prepend
(Decl
, Declarations
(N
));
869 Insert_After
(Last
, Decl
);
877 -- Loop through calls
879 Call
:= First_Elmt
(Call_List
);
880 while Present
(Call
) loop
882 -- Build a predicate expression of the form
885 -- and then global1 = temp1
886 -- and then global2 = temp2
889 -- This predicate determines if any of the global values
890 -- referenced by the procedure have changed since the
891 -- current call, if not an infinite recursion is assured.
893 Test
:= New_Occurrence_Of
(Standard_True
, Loc
);
895 Elm1
:= First_Elmt
(Var_List
);
896 Elm2
:= First_Elmt
(Shad_List
);
897 while Present
(Elm1
) loop
903 Left_Opnd
=> New_Occurrence_Of
(Node
(Elm1
), Loc
),
904 Right_Opnd
=> New_Occurrence_Of
(Node
(Elm2
), Loc
)));
910 -- Now we replace the call with the sequence
912 -- if no-changes (see above) then
913 -- raise Storage_Error;
918 Rewrite
(Node
(Call
),
919 Make_If_Statement
(Loc
,
921 Then_Statements
=> New_List
(
922 Make_Raise_Storage_Error
(Loc
,
923 Reason
=> SE_Infinite_Recursion
)),
925 Else_Statements
=> New_List
(
926 Relocate_Node
(Node
(Call
)))));
928 Analyze
(Node
(Call
));
933 -- Remove temporary scope stack entry used for analysis
936 end Detect_Infinite_Recursion
;
942 procedure Expand_Actuals
(N
: Node_Id
; Subp
: Entity_Id
) is
943 Loc
: constant Source_Ptr
:= Sloc
(N
);
948 E_Actual
: Entity_Id
;
949 E_Formal
: Entity_Id
;
951 procedure Add_Call_By_Copy_Code
;
952 -- For cases where the parameter must be passed by copy, this routine
953 -- generates a temporary variable into which the actual is copied and
954 -- then passes this as the parameter. For an OUT or IN OUT parameter,
955 -- an assignment is also generated to copy the result back. The call
956 -- also takes care of any constraint checks required for the type
957 -- conversion case (on both the way in and the way out).
959 procedure Add_Simple_Call_By_Copy_Code
;
960 -- This is similar to the above, but is used in cases where we know
961 -- that all that is needed is to simply create a temporary and copy
962 -- the value in and out of the temporary.
964 procedure Check_Fortran_Logical
;
965 -- A value of type Logical that is passed through a formal parameter
966 -- must be normalized because .TRUE. usually does not have the same
967 -- representation as True. We assume that .FALSE. = False = 0.
968 -- What about functions that return a logical type ???
970 function Is_Legal_Copy
return Boolean;
971 -- Check that an actual can be copied before generating the temporary
972 -- to be used in the call. If the actual is of a by_reference type then
973 -- the program is illegal (this can only happen in the presence of
974 -- rep. clauses that force an incorrect alignment). If the formal is
975 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
976 -- the effect that this might lead to unaligned arguments.
978 function Make_Var
(Actual
: Node_Id
) return Entity_Id
;
979 -- Returns an entity that refers to the given actual parameter,
980 -- Actual (not including any type conversion). If Actual is an
981 -- entity name, then this entity is returned unchanged, otherwise
982 -- a renaming is created to provide an entity for the actual.
984 procedure Reset_Packed_Prefix
;
985 -- The expansion of a packed array component reference is delayed in
986 -- the context of a call. Now we need to complete the expansion, so we
987 -- unmark the analyzed bits in all prefixes.
989 ---------------------------
990 -- Add_Call_By_Copy_Code --
991 ---------------------------
993 procedure Add_Call_By_Copy_Code
is
999 F_Typ
: constant Entity_Id
:= Etype
(Formal
);
1004 if not Is_Legal_Copy
then
1008 Temp
:= Make_Temporary
(Loc
, 'T', Actual
);
1010 -- Use formal type for temp, unless formal type is an unconstrained
1011 -- array, in which case we don't have to worry about bounds checks,
1012 -- and we use the actual type, since that has appropriate bounds.
1014 if Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
1015 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1017 Indic
:= New_Occurrence_Of
(Etype
(Formal
), Loc
);
1020 if Nkind
(Actual
) = N_Type_Conversion
then
1021 V_Typ
:= Etype
(Expression
(Actual
));
1023 -- If the formal is an (in-)out parameter, capture the name
1024 -- of the variable in order to build the post-call assignment.
1026 Var
:= Make_Var
(Expression
(Actual
));
1028 Crep
:= not Same_Representation
1029 (F_Typ
, Etype
(Expression
(Actual
)));
1032 V_Typ
:= Etype
(Actual
);
1033 Var
:= Make_Var
(Actual
);
1037 -- Setup initialization for case of in out parameter, or an out
1038 -- parameter where the formal is an unconstrained array (in the
1039 -- latter case, we have to pass in an object with bounds).
1041 -- If this is an out parameter, the initial copy is wasteful, so as
1042 -- an optimization for the one-dimensional case we extract the
1043 -- bounds of the actual and build an uninitialized temporary of the
1046 if Ekind
(Formal
) = E_In_Out_Parameter
1047 or else (Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
))
1049 if Nkind
(Actual
) = N_Type_Conversion
then
1050 if Conversion_OK
(Actual
) then
1051 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1053 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1056 elsif Ekind
(Formal
) = E_Out_Parameter
1057 and then Is_Array_Type
(F_Typ
)
1058 and then Number_Dimensions
(F_Typ
) = 1
1059 and then not Has_Non_Null_Base_Init_Proc
(F_Typ
)
1061 -- Actual is a one-dimensional array or slice, and the type
1062 -- requires no initialization. Create a temporary of the
1063 -- right size, but do not copy actual into it (optimization).
1067 Make_Subtype_Indication
(Loc
,
1069 New_Occurrence_Of
(F_Typ
, Loc
),
1071 Make_Index_Or_Discriminant_Constraint
(Loc
,
1072 Constraints
=> New_List
(
1075 Make_Attribute_Reference
(Loc
,
1076 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
1077 Attribute_Name
=> Name_First
),
1079 Make_Attribute_Reference
(Loc
,
1080 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
1081 Attribute_Name
=> Name_Last
)))));
1084 Init
:= New_Occurrence_Of
(Var
, Loc
);
1087 -- An initialization is created for packed conversions as
1088 -- actuals for out parameters to enable Make_Object_Declaration
1089 -- to determine the proper subtype for N_Node. Note that this
1090 -- is wasteful because the extra copying on the call side is
1091 -- not required for such out parameters. ???
1093 elsif Ekind
(Formal
) = E_Out_Parameter
1094 and then Nkind
(Actual
) = N_Type_Conversion
1095 and then (Is_Bit_Packed_Array
(F_Typ
)
1097 Is_Bit_Packed_Array
(Etype
(Expression
(Actual
))))
1099 if Conversion_OK
(Actual
) then
1100 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1102 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1105 elsif Ekind
(Formal
) = E_In_Parameter
then
1107 -- Handle the case in which the actual is a type conversion
1109 if Nkind
(Actual
) = N_Type_Conversion
then
1110 if Conversion_OK
(Actual
) then
1111 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1113 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1116 Init
:= New_Occurrence_Of
(Var
, Loc
);
1124 Make_Object_Declaration
(Loc
,
1125 Defining_Identifier
=> Temp
,
1126 Object_Definition
=> Indic
,
1127 Expression
=> Init
);
1128 Set_Assignment_OK
(N_Node
);
1129 Insert_Action
(N
, N_Node
);
1131 -- Now, normally the deal here is that we use the defining
1132 -- identifier created by that object declaration. There is
1133 -- one exception to this. In the change of representation case
1134 -- the above declaration will end up looking like:
1136 -- temp : type := identifier;
1138 -- And in this case we might as well use the identifier directly
1139 -- and eliminate the temporary. Note that the analysis of the
1140 -- declaration was not a waste of time in that case, since it is
1141 -- what generated the necessary change of representation code. If
1142 -- the change of representation introduced additional code, as in
1143 -- a fixed-integer conversion, the expression is not an identifier
1144 -- and must be kept.
1147 and then Present
(Expression
(N_Node
))
1148 and then Is_Entity_Name
(Expression
(N_Node
))
1150 Temp
:= Entity
(Expression
(N_Node
));
1151 Rewrite
(N_Node
, Make_Null_Statement
(Loc
));
1154 -- For IN parameter, all we do is to replace the actual
1156 if Ekind
(Formal
) = E_In_Parameter
then
1157 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
1160 -- Processing for OUT or IN OUT parameter
1163 -- Kill current value indications for the temporary variable we
1164 -- created, since we just passed it as an OUT parameter.
1166 Kill_Current_Values
(Temp
);
1167 Set_Is_Known_Valid
(Temp
, False);
1169 -- If type conversion, use reverse conversion on exit
1171 if Nkind
(Actual
) = N_Type_Conversion
then
1172 if Conversion_OK
(Actual
) then
1173 Expr
:= OK_Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
1175 Expr
:= Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
1178 Expr
:= New_Occurrence_Of
(Temp
, Loc
);
1181 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
1184 -- If the actual is a conversion of a packed reference, it may
1185 -- already have been expanded by Remove_Side_Effects, and the
1186 -- resulting variable is a temporary which does not designate
1187 -- the proper out-parameter, which may not be addressable. In
1188 -- that case, generate an assignment to the original expression
1189 -- (before expansion of the packed reference) so that the proper
1190 -- expansion of assignment to a packed component can take place.
1197 if Is_Renaming_Of_Object
(Var
)
1198 and then Nkind
(Renamed_Object
(Var
)) = N_Selected_Component
1199 and then Is_Entity_Name
(Prefix
(Renamed_Object
(Var
)))
1200 and then Nkind
(Original_Node
(Prefix
(Renamed_Object
(Var
))))
1201 = N_Indexed_Component
1203 Has_Non_Standard_Rep
(Etype
(Prefix
(Renamed_Object
(Var
))))
1205 Obj
:= Renamed_Object
(Var
);
1207 Make_Selected_Component
(Loc
,
1209 New_Copy_Tree
(Original_Node
(Prefix
(Obj
))),
1210 Selector_Name
=> New_Copy
(Selector_Name
(Obj
)));
1211 Reset_Analyzed_Flags
(Lhs
);
1214 Lhs
:= New_Occurrence_Of
(Var
, Loc
);
1217 Set_Assignment_OK
(Lhs
);
1219 if Is_Access_Type
(E_Formal
)
1220 and then Is_Entity_Name
(Lhs
)
1222 Present
(Effective_Extra_Accessibility
(Entity
(Lhs
)))
1224 -- Copyback target is an Ada 2012 stand-alone object of an
1225 -- anonymous access type.
1227 pragma Assert
(Ada_Version
>= Ada_2012
);
1229 if Type_Access_Level
(E_Formal
) >
1230 Object_Access_Level
(Lhs
)
1232 Append_To
(Post_Call
,
1233 Make_Raise_Program_Error
(Loc
,
1234 Reason
=> PE_Accessibility_Check_Failed
));
1237 Append_To
(Post_Call
,
1238 Make_Assignment_Statement
(Loc
,
1240 Expression
=> Expr
));
1242 -- We would like to somehow suppress generation of the
1243 -- extra_accessibility assignment generated by the expansion
1244 -- of the above assignment statement. It's not a correctness
1245 -- issue because the following assignment renders it dead,
1246 -- but generating back-to-back assignments to the same
1247 -- target is undesirable. ???
1249 Append_To
(Post_Call
,
1250 Make_Assignment_Statement
(Loc
,
1251 Name
=> New_Occurrence_Of
(
1252 Effective_Extra_Accessibility
(Entity
(Lhs
)), Loc
),
1253 Expression
=> Make_Integer_Literal
(Loc
,
1254 Type_Access_Level
(E_Formal
))));
1257 Append_To
(Post_Call
,
1258 Make_Assignment_Statement
(Loc
,
1260 Expression
=> Expr
));
1264 end Add_Call_By_Copy_Code
;
1266 ----------------------------------
1267 -- Add_Simple_Call_By_Copy_Code --
1268 ----------------------------------
1270 procedure Add_Simple_Call_By_Copy_Code
is
1278 F_Typ
: constant Entity_Id
:= Etype
(Formal
);
1281 if not Is_Legal_Copy
then
1285 -- Use formal type for temp, unless formal type is an unconstrained
1286 -- array, in which case we don't have to worry about bounds checks,
1287 -- and we use the actual type, since that has appropriate bounds.
1289 if Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
1290 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1292 Indic
:= New_Occurrence_Of
(Etype
(Formal
), Loc
);
1295 -- Prepare to generate code
1297 Reset_Packed_Prefix
;
1299 Temp
:= Make_Temporary
(Loc
, 'T', Actual
);
1300 Incod
:= Relocate_Node
(Actual
);
1301 Outcod
:= New_Copy_Tree
(Incod
);
1303 -- Generate declaration of temporary variable, initializing it
1304 -- with the input parameter unless we have an OUT formal or
1305 -- this is an initialization call.
1307 -- If the formal is an out parameter with discriminants, the
1308 -- discriminants must be captured even if the rest of the object
1309 -- is in principle uninitialized, because the discriminants may
1310 -- be read by the called subprogram.
1312 if Ekind
(Formal
) = E_Out_Parameter
then
1315 if Has_Discriminants
(Etype
(Formal
)) then
1316 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1319 elsif Inside_Init_Proc
then
1321 -- Could use a comment here to match comment below ???
1323 if Nkind
(Actual
) /= N_Selected_Component
1325 not Has_Discriminant_Dependent_Constraint
1326 (Entity
(Selector_Name
(Actual
)))
1330 -- Otherwise, keep the component in order to generate the proper
1331 -- actual subtype, that depends on enclosing discriminants.
1339 Make_Object_Declaration
(Loc
,
1340 Defining_Identifier
=> Temp
,
1341 Object_Definition
=> Indic
,
1342 Expression
=> Incod
);
1347 -- If the call is to initialize a component of a composite type,
1348 -- and the component does not depend on discriminants, use the
1349 -- actual type of the component. This is required in case the
1350 -- component is constrained, because in general the formal of the
1351 -- initialization procedure will be unconstrained. Note that if
1352 -- the component being initialized is constrained by an enclosing
1353 -- discriminant, the presence of the initialization in the
1354 -- declaration will generate an expression for the actual subtype.
1356 Set_No_Initialization
(Decl
);
1357 Set_Object_Definition
(Decl
,
1358 New_Occurrence_Of
(Etype
(Actual
), Loc
));
1361 Insert_Action
(N
, Decl
);
1363 -- The actual is simply a reference to the temporary
1365 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
1367 -- Generate copy out if OUT or IN OUT parameter
1369 if Ekind
(Formal
) /= E_In_Parameter
then
1371 Rhs
:= New_Occurrence_Of
(Temp
, Loc
);
1373 -- Deal with conversion
1375 if Nkind
(Lhs
) = N_Type_Conversion
then
1376 Lhs
:= Expression
(Lhs
);
1377 Rhs
:= Convert_To
(Etype
(Actual
), Rhs
);
1380 Append_To
(Post_Call
,
1381 Make_Assignment_Statement
(Loc
,
1383 Expression
=> Rhs
));
1384 Set_Assignment_OK
(Name
(Last
(Post_Call
)));
1386 end Add_Simple_Call_By_Copy_Code
;
1388 ---------------------------
1389 -- Check_Fortran_Logical --
1390 ---------------------------
1392 procedure Check_Fortran_Logical
is
1393 Logical
: constant Entity_Id
:= Etype
(Formal
);
1396 -- Note: this is very incomplete, e.g. it does not handle arrays
1397 -- of logical values. This is really not the right approach at all???)
1400 if Convention
(Subp
) = Convention_Fortran
1401 and then Root_Type
(Etype
(Formal
)) = Standard_Boolean
1402 and then Ekind
(Formal
) /= E_In_Parameter
1404 Var
:= Make_Var
(Actual
);
1405 Append_To
(Post_Call
,
1406 Make_Assignment_Statement
(Loc
,
1407 Name
=> New_Occurrence_Of
(Var
, Loc
),
1409 Unchecked_Convert_To
(
1412 Left_Opnd
=> New_Occurrence_Of
(Var
, Loc
),
1414 Unchecked_Convert_To
(
1416 New_Occurrence_Of
(Standard_False
, Loc
))))));
1418 end Check_Fortran_Logical
;
1424 function Is_Legal_Copy
return Boolean is
1426 -- An attempt to copy a value of such a type can only occur if
1427 -- representation clauses give the actual a misaligned address.
1429 if Is_By_Reference_Type
(Etype
(Formal
)) then
1431 -- If the front-end does not perform full type layout, the actual
1432 -- may in fact be properly aligned but there is not enough front-
1433 -- end information to determine this. In that case gigi will emit
1434 -- an error if a copy is not legal, or generate the proper code.
1435 -- For other backends we report the error now.
1437 -- Seems wrong to be issuing an error in the expander, since it
1438 -- will be missed in -gnatc mode ???
1440 if Frontend_Layout_On_Target
then
1442 ("misaligned actual cannot be passed by reference", Actual
);
1447 -- For users of Starlet, we assume that the specification of by-
1448 -- reference mechanism is mandatory. This may lead to unaligned
1449 -- objects but at least for DEC legacy code it is known to work.
1450 -- The warning will alert users of this code that a problem may
1453 elsif Mechanism
(Formal
) = By_Reference
1454 and then Is_Valued_Procedure
(Scope
(Formal
))
1457 ("by_reference actual may be misaligned??", Actual
);
1469 function Make_Var
(Actual
: Node_Id
) return Entity_Id
is
1473 if Is_Entity_Name
(Actual
) then
1474 return Entity
(Actual
);
1477 Var
:= Make_Temporary
(Loc
, 'T', Actual
);
1480 Make_Object_Renaming_Declaration
(Loc
,
1481 Defining_Identifier
=> Var
,
1483 New_Occurrence_Of
(Etype
(Actual
), Loc
),
1484 Name
=> Relocate_Node
(Actual
));
1486 Insert_Action
(N
, N_Node
);
1491 -------------------------
1492 -- Reset_Packed_Prefix --
1493 -------------------------
1495 procedure Reset_Packed_Prefix
is
1496 Pfx
: Node_Id
:= Actual
;
1499 Set_Analyzed
(Pfx
, False);
1501 not Nkind_In
(Pfx
, N_Selected_Component
, N_Indexed_Component
);
1502 Pfx
:= Prefix
(Pfx
);
1504 end Reset_Packed_Prefix
;
1506 -- Start of processing for Expand_Actuals
1509 Post_Call
:= New_List
;
1511 Formal
:= First_Formal
(Subp
);
1512 Actual
:= First_Actual
(N
);
1513 while Present
(Formal
) loop
1514 E_Formal
:= Etype
(Formal
);
1515 E_Actual
:= Etype
(Actual
);
1517 if Is_Scalar_Type
(E_Formal
)
1518 or else Nkind
(Actual
) = N_Slice
1520 Check_Fortran_Logical
;
1524 elsif Ekind
(Formal
) /= E_Out_Parameter
then
1526 -- The unusual case of the current instance of a protected type
1527 -- requires special handling. This can only occur in the context
1528 -- of a call within the body of a protected operation.
1530 if Is_Entity_Name
(Actual
)
1531 and then Ekind
(Entity
(Actual
)) = E_Protected_Type
1532 and then In_Open_Scopes
(Entity
(Actual
))
1534 if Scope
(Subp
) /= Entity
(Actual
) then
1536 ("operation outside protected type may not "
1537 & "call back its protected operations??", Actual
);
1541 Expand_Protected_Object_Reference
(N
, Entity
(Actual
)));
1544 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1545 -- build-in-place function, then a temporary return object needs
1546 -- to be created and access to it must be passed to the function.
1547 -- Currently we limit such functions to those with inherently
1548 -- limited result subtypes, but eventually we plan to expand the
1549 -- functions that are treated as build-in-place to include other
1550 -- composite result types.
1552 if Is_Build_In_Place_Function_Call
(Actual
) then
1553 Make_Build_In_Place_Call_In_Anonymous_Context
(Actual
);
1556 Apply_Constraint_Check
(Actual
, E_Formal
);
1558 -- Out parameter case. No constraint checks on access type
1561 elsif Is_Access_Type
(E_Formal
) then
1566 elsif Has_Discriminants
(Base_Type
(E_Formal
))
1567 or else Has_Non_Null_Base_Init_Proc
(E_Formal
)
1569 Apply_Constraint_Check
(Actual
, E_Formal
);
1574 Apply_Constraint_Check
(Actual
, Base_Type
(E_Formal
));
1577 -- Processing for IN-OUT and OUT parameters
1579 if Ekind
(Formal
) /= E_In_Parameter
then
1581 -- For type conversions of arrays, apply length/range checks
1583 if Is_Array_Type
(E_Formal
)
1584 and then Nkind
(Actual
) = N_Type_Conversion
1586 if Is_Constrained
(E_Formal
) then
1587 Apply_Length_Check
(Expression
(Actual
), E_Formal
);
1589 Apply_Range_Check
(Expression
(Actual
), E_Formal
);
1593 -- If argument is a type conversion for a type that is passed
1594 -- by copy, then we must pass the parameter by copy.
1596 if Nkind
(Actual
) = N_Type_Conversion
1598 (Is_Numeric_Type
(E_Formal
)
1599 or else Is_Access_Type
(E_Formal
)
1600 or else Is_Enumeration_Type
(E_Formal
)
1601 or else Is_Bit_Packed_Array
(Etype
(Formal
))
1602 or else Is_Bit_Packed_Array
(Etype
(Expression
(Actual
)))
1604 -- Also pass by copy if change of representation
1606 or else not Same_Representation
1608 Etype
(Expression
(Actual
))))
1610 Add_Call_By_Copy_Code
;
1612 -- References to components of bit packed arrays are expanded
1613 -- at this point, rather than at the point of analysis of the
1614 -- actuals, to handle the expansion of the assignment to
1615 -- [in] out parameters.
1617 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
1618 Add_Simple_Call_By_Copy_Code
;
1620 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1621 -- because the back-end cannot cope with such objects. In other
1622 -- cases where alignment forces a copy, the back-end generates
1623 -- it properly. It should not be generated unconditionally in the
1624 -- front-end because it does not know precisely the alignment
1625 -- requirements of the target, and makes too conservative an
1626 -- estimate, leading to superfluous copies or spurious errors
1627 -- on by-reference parameters.
1629 elsif Nkind
(Actual
) = N_Selected_Component
1631 Component_May_Be_Bit_Aligned
(Entity
(Selector_Name
(Actual
)))
1632 and then not Represented_As_Scalar
(Etype
(Formal
))
1634 Add_Simple_Call_By_Copy_Code
;
1636 -- References to slices of bit packed arrays are expanded
1638 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
1639 Add_Call_By_Copy_Code
;
1641 -- References to possibly unaligned slices of arrays are expanded
1643 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
1644 Add_Call_By_Copy_Code
;
1646 -- Deal with access types where the actual subtype and the
1647 -- formal subtype are not the same, requiring a check.
1649 -- It is necessary to exclude tagged types because of "downward
1650 -- conversion" errors.
1652 elsif Is_Access_Type
(E_Formal
)
1653 and then not Same_Type
(E_Formal
, E_Actual
)
1654 and then not Is_Tagged_Type
(Designated_Type
(E_Formal
))
1656 Add_Call_By_Copy_Code
;
1658 -- If the actual is not a scalar and is marked for volatile
1659 -- treatment, whereas the formal is not volatile, then pass
1660 -- by copy unless it is a by-reference type.
1662 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1663 -- because this is the enforcement of a language rule that applies
1664 -- only to "real" volatile variables, not e.g. to the address
1665 -- clause overlay case.
1667 elsif Is_Entity_Name
(Actual
)
1668 and then Is_Volatile
(Entity
(Actual
))
1669 and then not Is_By_Reference_Type
(E_Actual
)
1670 and then not Is_Scalar_Type
(Etype
(Entity
(Actual
)))
1671 and then not Is_Volatile
(E_Formal
)
1673 Add_Call_By_Copy_Code
;
1675 elsif Nkind
(Actual
) = N_Indexed_Component
1676 and then Is_Entity_Name
(Prefix
(Actual
))
1677 and then Has_Volatile_Components
(Entity
(Prefix
(Actual
)))
1679 Add_Call_By_Copy_Code
;
1681 -- Add call-by-copy code for the case of scalar out parameters
1682 -- when it is not known at compile time that the subtype of the
1683 -- formal is a subrange of the subtype of the actual (or vice
1684 -- versa for in out parameters), in order to get range checks
1685 -- on such actuals. (Maybe this case should be handled earlier
1686 -- in the if statement???)
1688 elsif Is_Scalar_Type
(E_Formal
)
1690 (not In_Subrange_Of
(E_Formal
, E_Actual
)
1692 (Ekind
(Formal
) = E_In_Out_Parameter
1693 and then not In_Subrange_Of
(E_Actual
, E_Formal
)))
1695 -- Perhaps the setting back to False should be done within
1696 -- Add_Call_By_Copy_Code, since it could get set on other
1697 -- cases occurring above???
1699 if Do_Range_Check
(Actual
) then
1700 Set_Do_Range_Check
(Actual
, False);
1703 Add_Call_By_Copy_Code
;
1706 -- RM 3.2.4 (23/3) : A predicate is checked on in-out and out
1707 -- by-reference parameters on exit from the call. If the actual
1708 -- is a derived type and the operation is inherited, the body
1709 -- of the operation will not contain a call to the predicate
1710 -- function, so it must be done explicitly after the call. Ditto
1711 -- if the actual is an entity of a predicated subtype.
1713 -- The rule refers to by-reference types, but a check is needed
1714 -- for by-copy types as well. That check is subsumed by the rule
1715 -- for subtype conversion on assignment, but we can generate the
1716 -- required check now.
1718 -- Note that this is needed only if the subtype of the actual has
1719 -- an explicit predicate aspect, not if it inherits them from a
1720 -- base type or ancestor. The check is also superfluous if the
1721 -- subtype is elaborated before the body of the subprogram, but
1722 -- this is harder to verify, and there may be a redundant check.
1724 -- Note also that Subp may be either a subprogram entity for
1725 -- direct calls, or a type entity for indirect calls, which must
1726 -- be handled separately because the name does not denote an
1727 -- overloadable entity.
1729 -- If the formal is class-wide the corresponding postcondition
1730 -- procedure does not include a predicate call, so it has to be
1731 -- generated explicitly.
1733 if not Is_Init_Proc
(Subp
)
1734 and then (Has_Aspect
(E_Actual
, Aspect_Predicate
)
1736 Has_Aspect
(E_Actual
, Aspect_Dynamic_Predicate
)
1738 Has_Aspect
(E_Actual
, Aspect_Static_Predicate
))
1739 and then Present
(Predicate_Function
(E_Actual
))
1741 if Is_Entity_Name
(Actual
)
1743 (Is_Derived_Type
(E_Actual
)
1744 and then Is_Overloadable
(Subp
)
1745 and then Is_Inherited_Operation_For_Type
(Subp
, E_Actual
))
1747 Append_To
(Post_Call
,
1748 Make_Predicate_Check
(E_Actual
, Actual
));
1750 elsif Is_Class_Wide_Type
(E_Formal
)
1751 and then not Is_Class_Wide_Type
(E_Actual
)
1753 Append_To
(Post_Call
,
1754 Make_Predicate_Check
(E_Actual
, Actual
));
1758 -- Processing for IN parameters
1761 -- For IN parameters is in the packed array case, we expand an
1762 -- indexed component (the circuit in Exp_Ch4 deliberately left
1763 -- indexed components appearing as actuals untouched, so that
1764 -- the special processing above for the OUT and IN OUT cases
1765 -- could be performed. We could make the test in Exp_Ch4 more
1766 -- complex and have it detect the parameter mode, but it is
1767 -- easier simply to handle all cases here.)
1769 if Nkind
(Actual
) = N_Indexed_Component
1770 and then Is_Packed
(Etype
(Prefix
(Actual
)))
1772 Reset_Packed_Prefix
;
1773 Expand_Packed_Element_Reference
(Actual
);
1775 -- If we have a reference to a bit packed array, we copy it, since
1776 -- the actual must be byte aligned.
1778 -- Is this really necessary in all cases???
1780 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
1781 Add_Simple_Call_By_Copy_Code
;
1783 -- If a non-scalar actual is possibly unaligned, we need a copy
1785 elsif Is_Possibly_Unaligned_Object
(Actual
)
1786 and then not Represented_As_Scalar
(Etype
(Formal
))
1788 Add_Simple_Call_By_Copy_Code
;
1790 -- Similarly, we have to expand slices of packed arrays here
1791 -- because the result must be byte aligned.
1793 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
1794 Add_Call_By_Copy_Code
;
1796 -- Only processing remaining is to pass by copy if this is a
1797 -- reference to a possibly unaligned slice, since the caller
1798 -- expects an appropriately aligned argument.
1800 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
1801 Add_Call_By_Copy_Code
;
1803 -- An unusual case: a current instance of an enclosing task can be
1804 -- an actual, and must be replaced by a reference to self.
1806 elsif Is_Entity_Name
(Actual
)
1807 and then Is_Task_Type
(Entity
(Actual
))
1809 if In_Open_Scopes
(Entity
(Actual
)) then
1811 (Make_Function_Call
(Loc
,
1812 Name
=> New_Occurrence_Of
(RTE
(RE_Self
), Loc
))));
1815 -- A task type cannot otherwise appear as an actual
1818 raise Program_Error
;
1823 Next_Formal
(Formal
);
1824 Next_Actual
(Actual
);
1827 -- Find right place to put post call stuff if it is present
1829 if not Is_Empty_List
(Post_Call
) then
1831 -- Cases where the call is not a member of a statement list
1833 if not Is_List_Member
(N
) then
1835 P
: Node_Id
:= Parent
(N
);
1838 -- In Ada 2012 the call may be a function call in an expression
1839 -- (since OUT and IN OUT parameters are now allowed for such
1840 -- calls. The write-back of (in)-out parameters is handled
1841 -- by the back-end, but the constraint checks generated when
1842 -- subtypes of formal and actual don't match must be inserted
1843 -- in the form of assignments, at the nearest point after the
1844 -- declaration or statement that contains the call.
1846 if Ada_Version
>= Ada_2012
1847 and then Nkind
(N
) = N_Function_Call
1849 while Nkind
(P
) not in N_Declaration
1851 Nkind
(P
) not in N_Statement_Other_Than_Procedure_Call
1856 Insert_Actions_After
(P
, Post_Call
);
1858 -- If not the special Ada 2012 case of a function call, then
1859 -- we must have the triggering statement of a triggering
1860 -- alternative or an entry call alternative, and we can add
1861 -- the post call stuff to the corresponding statement list.
1864 pragma Assert
(Nkind_In
(P
, N_Triggering_Alternative
,
1865 N_Entry_Call_Alternative
));
1867 if Is_Non_Empty_List
(Statements
(P
)) then
1868 Insert_List_Before_And_Analyze
1869 (First
(Statements
(P
)), Post_Call
);
1871 Set_Statements
(P
, Post_Call
);
1877 -- Otherwise, normal case where N is in a statement sequence,
1878 -- just put the post-call stuff after the call statement.
1881 Insert_Actions_After
(N
, Post_Call
);
1885 -- The call node itself is re-analyzed in Expand_Call
1893 -- This procedure handles expansion of function calls and procedure call
1894 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1895 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
1897 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
1898 -- Provide values of actuals for all formals in Extra_Formals list
1899 -- Replace "call" to enumeration literal function by literal itself
1900 -- Rewrite call to predefined operator as operator
1901 -- Replace actuals to in-out parameters that are numeric conversions,
1902 -- with explicit assignment to temporaries before and after the call.
1903 -- Remove optional actuals if First_Optional_Parameter specified.
1905 -- Note that the list of actuals has been filled with default expressions
1906 -- during semantic analysis of the call. Only the extra actuals required
1907 -- for the 'Constrained attribute and for accessibility checks are added
1910 procedure Expand_Call
(N
: Node_Id
) is
1911 Loc
: constant Source_Ptr
:= Sloc
(N
);
1912 Call_Node
: Node_Id
:= N
;
1913 Extra_Actuals
: List_Id
:= No_List
;
1914 Prev
: Node_Id
:= Empty
;
1916 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
);
1917 -- Adds one entry to the end of the actual parameter list. Used for
1918 -- default parameters and for extra actuals (for Extra_Formals). The
1919 -- argument is an N_Parameter_Association node.
1921 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
);
1922 -- Adds an extra actual to the list of extra actuals. Expr is the
1923 -- expression for the value of the actual, EF is the entity for the
1926 procedure Do_Inline
(Subp
: Entity_Id
; Orig_Subp
: Entity_Id
);
1927 -- Check and inline the body of Subp. Invoked when compiling with
1928 -- optimizations enabled and Subp has pragma inline or inline always.
1929 -- If the subprogram is a renaming, or if it is inherited, then Subp
1930 -- references the renamed entity and Orig_Subp is the entity of the
1933 procedure Do_Inline_Always
(Subp
: Entity_Id
; Orig_Subp
: Entity_Id
);
1934 -- Check and inline the body of Subp. Invoked when compiling without
1935 -- optimizations and Subp has pragma inline always. If the subprogram is
1936 -- a renaming, or if it is inherited, then Subp references the renamed
1937 -- entity and Orig_Subp is the entity of the call node N.
1939 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
;
1940 -- Within an instance, a type derived from a non-tagged formal derived
1941 -- type inherits from the original parent, not from the actual. The
1942 -- current derivation mechanism has the derived type inherit from the
1943 -- actual, which is only correct outside of the instance. If the
1944 -- subprogram is inherited, we test for this particular case through a
1945 -- convoluted tree traversal before setting the proper subprogram to be
1948 function In_Unfrozen_Instance
(E
: Entity_Id
) return Boolean;
1949 -- Return true if E comes from an instance that is not yet frozen
1951 function Is_Direct_Deep_Call
(Subp
: Entity_Id
) return Boolean;
1952 -- Determine if Subp denotes a non-dispatching call to a Deep routine
1954 function New_Value
(From
: Node_Id
) return Node_Id
;
1955 -- From is the original Expression. New_Value is equivalent to a call
1956 -- to Duplicate_Subexpr with an explicit dereference when From is an
1957 -- access parameter.
1959 --------------------------
1960 -- Add_Actual_Parameter --
1961 --------------------------
1963 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
) is
1964 Actual_Expr
: constant Node_Id
:=
1965 Explicit_Actual_Parameter
(Insert_Param
);
1968 -- Case of insertion is first named actual
1970 if No
(Prev
) or else
1971 Nkind
(Parent
(Prev
)) /= N_Parameter_Association
1973 Set_Next_Named_Actual
1974 (Insert_Param
, First_Named_Actual
(Call_Node
));
1975 Set_First_Named_Actual
(Call_Node
, Actual_Expr
);
1978 if No
(Parameter_Associations
(Call_Node
)) then
1979 Set_Parameter_Associations
(Call_Node
, New_List
);
1982 Append
(Insert_Param
, Parameter_Associations
(Call_Node
));
1985 Insert_After
(Prev
, Insert_Param
);
1988 -- Case of insertion is not first named actual
1991 Set_Next_Named_Actual
1992 (Insert_Param
, Next_Named_Actual
(Parent
(Prev
)));
1993 Set_Next_Named_Actual
(Parent
(Prev
), Actual_Expr
);
1994 Append
(Insert_Param
, Parameter_Associations
(Call_Node
));
1997 Prev
:= Actual_Expr
;
1998 end Add_Actual_Parameter
;
2000 ----------------------
2001 -- Add_Extra_Actual --
2002 ----------------------
2004 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
) is
2005 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
2008 if Extra_Actuals
= No_List
then
2009 Extra_Actuals
:= New_List
;
2010 Set_Parent
(Extra_Actuals
, Call_Node
);
2013 Append_To
(Extra_Actuals
,
2014 Make_Parameter_Association
(Loc
,
2015 Selector_Name
=> Make_Identifier
(Loc
, Chars
(EF
)),
2016 Explicit_Actual_Parameter
=> Expr
));
2018 Analyze_And_Resolve
(Expr
, Etype
(EF
));
2020 if Nkind
(Call_Node
) = N_Function_Call
then
2021 Set_Is_Accessibility_Actual
(Parent
(Expr
));
2023 end Add_Extra_Actual
;
2029 procedure Do_Inline
(Subp
: Entity_Id
; Orig_Subp
: Entity_Id
) is
2030 Spec
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
2032 procedure Do_Backend_Inline
;
2033 -- Check that the call can be safely passed to the backend. If true
2034 -- then register the enclosing unit of Subp to Inlined_Bodies so that
2035 -- the body of Subp can be retrieved and analyzed by the backend.
2037 procedure Register_Backend_Call
(N
: Node_Id
);
2038 -- Append N to the list Backend_Calls
2040 -----------------------
2041 -- Do_Backend_Inline --
2042 -----------------------
2044 procedure Do_Backend_Inline
is
2046 -- No extra test needed for init subprograms since we know they
2047 -- are available to the backend.
2049 if Is_Init_Proc
(Subp
) then
2050 Add_Inlined_Body
(Subp
);
2051 Register_Backend_Call
(Call_Node
);
2053 -- Verify that if the body to inline is located in the current
2054 -- unit the inlining does not occur earlier. This avoids
2055 -- order-of-elaboration problems in the back end.
2057 elsif In_Same_Extended_Unit
(Call_Node
, Subp
)
2058 and then Nkind
(Spec
) = N_Subprogram_Declaration
2059 and then Earlier_In_Extended_Unit
2060 (Loc
, Sloc
(Body_To_Inline
(Spec
)))
2063 ("cannot inline& (body not seen yet)??", Call_Node
, Subp
);
2067 Backend_Inline
: Boolean := True;
2070 -- If we are compiling a package body that is not the
2071 -- main unit, it must be for inlining/instantiation
2072 -- purposes, in which case we inline the call to insure
2073 -- that the same temporaries are generated when compiling
2074 -- the body by itself. Otherwise link errors can occur.
2076 -- If the function being called is itself in the main
2077 -- unit, we cannot inline, because there is a risk of
2078 -- double elaboration and/or circularity: the inlining
2079 -- can make visible a private entity in the body of the
2080 -- main unit, that gigi will see before its sees its
2081 -- proper definition.
2083 if not (In_Extended_Main_Code_Unit
(Call_Node
))
2084 and then In_Package_Body
2087 not In_Extended_Main_Source_Unit
(Subp
);
2090 if Backend_Inline
then
2091 Add_Inlined_Body
(Subp
);
2092 Register_Backend_Call
(Call_Node
);
2096 end Do_Backend_Inline
;
2098 ---------------------------
2099 -- Register_Backend_Call --
2100 ---------------------------
2102 procedure Register_Backend_Call
(N
: Node_Id
) is
2104 if Backend_Calls
= No_Elist
then
2105 Backend_Calls
:= New_Elmt_List
;
2108 Append_Elmt
(N
, To
=> Backend_Calls
);
2109 end Register_Backend_Call
;
2111 -- Start of processing for Do_Inline
2114 -- Verify that the body to inline has already been seen
2117 or else Nkind
(Spec
) /= N_Subprogram_Declaration
2118 or else No
(Body_To_Inline
(Spec
))
2120 if Comes_From_Source
(Subp
)
2121 and then Must_Inline
(Subp
)
2124 ("cannot inline& (body not seen yet)?", Call_Node
, Subp
);
2126 -- Let the back end handle it
2133 -- If this an inherited function that returns a private type, do not
2134 -- inline if the full view is an unconstrained array, because such
2135 -- calls cannot be inlined.
2137 elsif Present
(Orig_Subp
)
2138 and then Is_Array_Type
(Etype
(Orig_Subp
))
2139 and then not Is_Constrained
(Etype
(Orig_Subp
))
2142 ("cannot inline& (unconstrained array)?", Call_Node
, Subp
);
2145 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
2149 ----------------------
2150 -- Do_Inline_Always --
2151 ----------------------
2153 procedure Do_Inline_Always
(Subp
: Entity_Id
; Orig_Subp
: Entity_Id
) is
2154 Spec
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
2155 Body_Id
: Entity_Id
;
2159 or else Nkind
(Spec
) /= N_Subprogram_Declaration
2160 or else No
(Body_To_Inline
(Spec
))
2161 or else Serious_Errors_Detected
/= 0
2166 Body_Id
:= Corresponding_Body
(Spec
);
2168 -- Verify that the body to inline has already been seen
2171 or else not Analyzed
(Body_Id
)
2173 Set_Is_Inlined
(Subp
, False);
2175 if Comes_From_Source
(Subp
) then
2177 -- Report a warning only if the call is located in the unit of
2178 -- the called subprogram; otherwise it is an error.
2180 if not In_Same_Extended_Unit
(Call_Node
, Subp
) then
2182 ("cannot inline& (body not seen yet)?", Call_Node
, Subp
,
2183 Is_Serious
=> True);
2185 elsif In_Open_Scopes
(Subp
) then
2187 -- For backward compatibility we generate the same error
2188 -- or warning of the previous implementation. This will
2189 -- be changed when we definitely incorporate the new
2192 if Front_End_Inlining
2193 and then Optimization_Level
= 0
2196 ("call to recursive subprogram cannot be inlined?p?",
2199 -- Do not emit error compiling runtime packages
2201 elsif Is_Predefined_File_Name
2202 (Unit_File_Name
(Get_Source_Unit
(Subp
)))
2205 ("call to recursive subprogram cannot be inlined??",
2210 ("call to recursive subprogram cannot be inlined",
2216 ("cannot inline& (body not seen yet)?", Call_Node
, Subp
);
2222 -- If this an inherited function that returns a private type, do not
2223 -- inline if the full view is an unconstrained array, because such
2224 -- calls cannot be inlined.
2226 elsif Present
(Orig_Subp
)
2227 and then Is_Array_Type
(Etype
(Orig_Subp
))
2228 and then not Is_Constrained
(Etype
(Orig_Subp
))
2231 ("cannot inline& (unconstrained array)?", Call_Node
, Subp
);
2233 -- If the called subprogram comes from an instance in the same
2234 -- unit, and the instance is not yet frozen, inlining might
2235 -- trigger order-of-elaboration problems.
2237 elsif In_Unfrozen_Instance
(Scope
(Subp
)) then
2239 ("cannot inline& (unfrozen instance)?", Call_Node
, Subp
);
2242 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
2244 end Do_Inline_Always
;
2246 ---------------------------
2247 -- Inherited_From_Formal --
2248 ---------------------------
2250 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
is
2252 Gen_Par
: Entity_Id
;
2253 Gen_Prim
: Elist_Id
;
2258 -- If the operation is inherited, it is attached to the corresponding
2259 -- type derivation. If the parent in the derivation is a generic
2260 -- actual, it is a subtype of the actual, and we have to recover the
2261 -- original derived type declaration to find the proper parent.
2263 if Nkind
(Parent
(S
)) /= N_Full_Type_Declaration
2264 or else not Is_Derived_Type
(Defining_Identifier
(Parent
(S
)))
2265 or else Nkind
(Type_Definition
(Original_Node
(Parent
(S
)))) /=
2266 N_Derived_Type_Definition
2267 or else not In_Instance
2274 (Type_Definition
(Original_Node
(Parent
(S
))));
2276 if Nkind
(Indic
) = N_Subtype_Indication
then
2277 Par
:= Entity
(Subtype_Mark
(Indic
));
2279 Par
:= Entity
(Indic
);
2283 if not Is_Generic_Actual_Type
(Par
)
2284 or else Is_Tagged_Type
(Par
)
2285 or else Nkind
(Parent
(Par
)) /= N_Subtype_Declaration
2286 or else not In_Open_Scopes
(Scope
(Par
))
2290 Gen_Par
:= Generic_Parent_Type
(Parent
(Par
));
2293 -- If the actual has no generic parent type, the formal is not
2294 -- a formal derived type, so nothing to inherit.
2296 if No
(Gen_Par
) then
2300 -- If the generic parent type is still the generic type, this is a
2301 -- private formal, not a derived formal, and there are no operations
2302 -- inherited from the formal.
2304 if Nkind
(Parent
(Gen_Par
)) = N_Formal_Type_Declaration
then
2308 Gen_Prim
:= Collect_Primitive_Operations
(Gen_Par
);
2310 Elmt
:= First_Elmt
(Gen_Prim
);
2311 while Present
(Elmt
) loop
2312 if Chars
(Node
(Elmt
)) = Chars
(S
) then
2318 F1
:= First_Formal
(S
);
2319 F2
:= First_Formal
(Node
(Elmt
));
2321 and then Present
(F2
)
2323 if Etype
(F1
) = Etype
(F2
)
2324 or else Etype
(F2
) = Gen_Par
2330 exit; -- not the right subprogram
2342 raise Program_Error
;
2343 end Inherited_From_Formal
;
2345 --------------------------
2346 -- In_Unfrozen_Instance --
2347 --------------------------
2349 function In_Unfrozen_Instance
(E
: Entity_Id
) return Boolean is
2354 while Present
(S
) and then S
/= Standard_Standard
loop
2355 if Is_Generic_Instance
(S
)
2356 and then Present
(Freeze_Node
(S
))
2357 and then not Analyzed
(Freeze_Node
(S
))
2366 end In_Unfrozen_Instance
;
2368 -------------------------
2369 -- Is_Direct_Deep_Call --
2370 -------------------------
2372 function Is_Direct_Deep_Call
(Subp
: Entity_Id
) return Boolean is
2374 if Is_TSS
(Subp
, TSS_Deep_Adjust
)
2375 or else Is_TSS
(Subp
, TSS_Deep_Finalize
)
2376 or else Is_TSS
(Subp
, TSS_Deep_Initialize
)
2383 Actual
:= First
(Parameter_Associations
(N
));
2384 Formal
:= First_Formal
(Subp
);
2385 while Present
(Actual
)
2386 and then Present
(Formal
)
2388 if Nkind
(Actual
) = N_Identifier
2389 and then Is_Controlling_Actual
(Actual
)
2390 and then Etype
(Actual
) = Etype
(Formal
)
2396 Next_Formal
(Formal
);
2402 end Is_Direct_Deep_Call
;
2408 function New_Value
(From
: Node_Id
) return Node_Id
is
2409 Res
: constant Node_Id
:= Duplicate_Subexpr
(From
);
2411 if Is_Access_Type
(Etype
(From
)) then
2412 return Make_Explicit_Dereference
(Sloc
(From
), Prefix
=> Res
);
2420 Curr_S
: constant Entity_Id
:= Current_Scope
;
2421 Remote
: constant Boolean := Is_Remote_Call
(Call_Node
);
2424 Orig_Subp
: Entity_Id
:= Empty
;
2425 Param_Count
: Natural := 0;
2426 Parent_Formal
: Entity_Id
;
2427 Parent_Subp
: Entity_Id
;
2431 Prev_Orig
: Node_Id
;
2432 -- Original node for an actual, which may have been rewritten. If the
2433 -- actual is a function call that has been transformed from a selected
2434 -- component, the original node is unanalyzed. Otherwise, it carries
2435 -- semantic information used to generate additional actuals.
2437 CW_Interface_Formals_Present
: Boolean := False;
2439 -- Start of processing for Expand_Call
2442 -- Expand the procedure call if the first actual has a dimension and if
2443 -- the procedure is Put (Ada 2012).
2445 if Ada_Version
>= Ada_2012
2446 and then Nkind
(Call_Node
) = N_Procedure_Call_Statement
2447 and then Present
(Parameter_Associations
(Call_Node
))
2449 Expand_Put_Call_With_Symbol
(Call_Node
);
2452 -- Ignore if previous error
2454 if Nkind
(Call_Node
) in N_Has_Etype
2455 and then Etype
(Call_Node
) = Any_Type
2460 -- Call using access to subprogram with explicit dereference
2462 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
2463 Subp
:= Etype
(Name
(Call_Node
));
2464 Parent_Subp
:= Empty
;
2466 -- Case of call to simple entry, where the Name is a selected component
2467 -- whose prefix is the task, and whose selector name is the entry name
2469 elsif Nkind
(Name
(Call_Node
)) = N_Selected_Component
then
2470 Subp
:= Entity
(Selector_Name
(Name
(Call_Node
)));
2471 Parent_Subp
:= Empty
;
2473 -- Case of call to member of entry family, where Name is an indexed
2474 -- component, with the prefix being a selected component giving the
2475 -- task and entry family name, and the index being the entry index.
2477 elsif Nkind
(Name
(Call_Node
)) = N_Indexed_Component
then
2478 Subp
:= Entity
(Selector_Name
(Prefix
(Name
(Call_Node
))));
2479 Parent_Subp
:= Empty
;
2484 Subp
:= Entity
(Name
(Call_Node
));
2485 Parent_Subp
:= Alias
(Subp
);
2487 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2488 -- if we can tell that the first parameter cannot possibly be null.
2489 -- This improves efficiency by avoiding a run-time test.
2491 -- We do not do this if Raise_Exception_Always does not exist, which
2492 -- can happen in configurable run time profiles which provide only a
2495 if Is_RTE
(Subp
, RE_Raise_Exception
)
2496 and then RTE_Available
(RE_Raise_Exception_Always
)
2499 FA
: constant Node_Id
:=
2500 Original_Node
(First_Actual
(Call_Node
));
2503 -- The case we catch is where the first argument is obtained
2504 -- using the Identity attribute (which must always be
2507 if Nkind
(FA
) = N_Attribute_Reference
2508 and then Attribute_Name
(FA
) = Name_Identity
2510 Subp
:= RTE
(RE_Raise_Exception_Always
);
2511 Set_Name
(Call_Node
, New_Occurrence_Of
(Subp
, Loc
));
2516 if Ekind
(Subp
) = E_Entry
then
2517 Parent_Subp
:= Empty
;
2521 -- Detect the following code in System.Finalization_Masters only on
2522 -- .NET/JVM targets:
2524 -- procedure Finalize (Master : in out Finalization_Master) is
2528 -- Finalize (Curr_Ptr.all);
2530 -- Since .NET/JVM compilers lack address arithmetic and Deep_Finalize
2531 -- cannot be named in library or user code, the compiler has to install
2532 -- a kludge and transform the call to Finalize into Deep_Finalize.
2534 if VM_Target
/= No_VM
2535 and then Chars
(Subp
) = Name_Finalize
2536 and then Ekind
(Curr_S
) = E_Block
2537 and then Ekind
(Scope
(Curr_S
)) = E_Procedure
2538 and then Chars
(Scope
(Curr_S
)) = Name_Finalize
2539 and then Etype
(First_Formal
(Scope
(Curr_S
))) =
2540 RTE
(RE_Finalization_Master
)
2543 Deep_Fin
: constant Entity_Id
:=
2544 Find_Prim_Op
(RTE
(RE_Root_Controlled
),
2547 -- Since Root_Controlled is a tagged type, the compiler should
2548 -- always generate Deep_Finalize for it.
2550 pragma Assert
(Present
(Deep_Fin
));
2553 -- Deep_Finalize (Curr_Ptr.all);
2556 Make_Procedure_Call_Statement
(Loc
,
2558 New_Occurrence_Of
(Deep_Fin
, Loc
),
2559 Parameter_Associations
=>
2560 New_Copy_List_Tree
(Parameter_Associations
(N
))));
2567 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2568 -- alternative in an asynchronous select or as an entry call in
2569 -- a conditional or timed select. Check whether the procedure call
2570 -- is a renaming of an entry and rewrite it as an entry call.
2572 if Ada_Version
>= Ada_2005
2573 and then Nkind
(Call_Node
) = N_Procedure_Call_Statement
2575 ((Nkind
(Parent
(Call_Node
)) = N_Triggering_Alternative
2576 and then Triggering_Statement
(Parent
(Call_Node
)) = Call_Node
)
2578 (Nkind
(Parent
(Call_Node
)) = N_Entry_Call_Alternative
2579 and then Entry_Call_Statement
(Parent
(Call_Node
)) = Call_Node
))
2583 Ren_Root
: Entity_Id
:= Subp
;
2586 -- This may be a chain of renamings, find the root
2588 if Present
(Alias
(Ren_Root
)) then
2589 Ren_Root
:= Alias
(Ren_Root
);
2592 if Present
(Original_Node
(Parent
(Parent
(Ren_Root
)))) then
2593 Ren_Decl
:= Original_Node
(Parent
(Parent
(Ren_Root
)));
2595 if Nkind
(Ren_Decl
) = N_Subprogram_Renaming_Declaration
then
2597 Make_Entry_Call_Statement
(Loc
,
2599 New_Copy_Tree
(Name
(Ren_Decl
)),
2600 Parameter_Associations
=>
2602 (Parameter_Associations
(Call_Node
))));
2610 -- First step, compute extra actuals, corresponding to any Extra_Formals
2611 -- present. Note that we do not access Extra_Formals directly, instead
2612 -- we simply note the presence of the extra formals as we process the
2613 -- regular formals collecting corresponding actuals in Extra_Actuals.
2615 -- We also generate any required range checks for actuals for in formals
2616 -- as we go through the loop, since this is a convenient place to do it.
2617 -- (Though it seems that this would be better done in Expand_Actuals???)
2619 -- Special case: Thunks must not compute the extra actuals; they must
2620 -- just propagate to the target primitive their extra actuals.
2622 if Is_Thunk
(Current_Scope
)
2623 and then Thunk_Entity
(Current_Scope
) = Subp
2624 and then Present
(Extra_Formals
(Subp
))
2626 pragma Assert
(Present
(Extra_Formals
(Current_Scope
)));
2629 Target_Formal
: Entity_Id
;
2630 Thunk_Formal
: Entity_Id
;
2633 Target_Formal
:= Extra_Formals
(Subp
);
2634 Thunk_Formal
:= Extra_Formals
(Current_Scope
);
2635 while Present
(Target_Formal
) loop
2637 (New_Occurrence_Of
(Thunk_Formal
, Loc
), Thunk_Formal
);
2639 Target_Formal
:= Extra_Formal
(Target_Formal
);
2640 Thunk_Formal
:= Extra_Formal
(Thunk_Formal
);
2643 while Is_Non_Empty_List
(Extra_Actuals
) loop
2644 Add_Actual_Parameter
(Remove_Head
(Extra_Actuals
));
2647 Expand_Actuals
(Call_Node
, Subp
);
2652 Formal
:= First_Formal
(Subp
);
2653 Actual
:= First_Actual
(Call_Node
);
2655 while Present
(Formal
) loop
2657 -- Generate range check if required
2659 if Do_Range_Check
(Actual
)
2660 and then Ekind
(Formal
) = E_In_Parameter
2662 Set_Do_Range_Check
(Actual
, False);
2663 Generate_Range_Check
2664 (Actual
, Etype
(Formal
), CE_Range_Check_Failed
);
2667 -- Prepare to examine current entry
2670 Prev_Orig
:= Original_Node
(Prev
);
2672 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2673 -- to expand it in a further round.
2675 CW_Interface_Formals_Present
:=
2676 CW_Interface_Formals_Present
2678 (Ekind
(Etype
(Formal
)) = E_Class_Wide_Type
2679 and then Is_Interface
(Etype
(Etype
(Formal
))))
2681 (Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
2682 and then Is_Interface
(Directly_Designated_Type
2683 (Etype
(Etype
(Formal
)))));
2685 -- Create possible extra actual for constrained case. Usually, the
2686 -- extra actual is of the form actual'constrained, but since this
2687 -- attribute is only available for unconstrained records, TRUE is
2688 -- expanded if the type of the formal happens to be constrained (for
2689 -- instance when this procedure is inherited from an unconstrained
2690 -- record to a constrained one) or if the actual has no discriminant
2691 -- (its type is constrained). An exception to this is the case of a
2692 -- private type without discriminants. In this case we pass FALSE
2693 -- because the object has underlying discriminants with defaults.
2695 if Present
(Extra_Constrained
(Formal
)) then
2696 if Ekind
(Etype
(Prev
)) in Private_Kind
2697 and then not Has_Discriminants
(Base_Type
(Etype
(Prev
)))
2700 (New_Occurrence_Of
(Standard_False
, Loc
),
2701 Extra_Constrained
(Formal
));
2703 elsif Is_Constrained
(Etype
(Formal
))
2704 or else not Has_Discriminants
(Etype
(Prev
))
2707 (New_Occurrence_Of
(Standard_True
, Loc
),
2708 Extra_Constrained
(Formal
));
2710 -- Do not produce extra actuals for Unchecked_Union parameters.
2711 -- Jump directly to the end of the loop.
2713 elsif Is_Unchecked_Union
(Base_Type
(Etype
(Actual
))) then
2714 goto Skip_Extra_Actual_Generation
;
2717 -- If the actual is a type conversion, then the constrained
2718 -- test applies to the actual, not the target type.
2724 -- Test for unchecked conversions as well, which can occur
2725 -- as out parameter actuals on calls to stream procedures.
2728 while Nkind_In
(Act_Prev
, N_Type_Conversion
,
2729 N_Unchecked_Type_Conversion
)
2731 Act_Prev
:= Expression
(Act_Prev
);
2734 -- If the expression is a conversion of a dereference, this
2735 -- is internally generated code that manipulates addresses,
2736 -- e.g. when building interface tables. No check should
2737 -- occur in this case, and the discriminated object is not
2740 if not Comes_From_Source
(Actual
)
2741 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
2742 and then Nkind
(Act_Prev
) = N_Explicit_Dereference
2745 (New_Occurrence_Of
(Standard_False
, Loc
),
2746 Extra_Constrained
(Formal
));
2750 (Make_Attribute_Reference
(Sloc
(Prev
),
2752 Duplicate_Subexpr_No_Checks
2753 (Act_Prev
, Name_Req
=> True),
2754 Attribute_Name
=> Name_Constrained
),
2755 Extra_Constrained
(Formal
));
2761 -- Create possible extra actual for accessibility level
2763 if Present
(Extra_Accessibility
(Formal
)) then
2765 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2766 -- attribute, then the original actual may be an aliased object
2767 -- occurring as the prefix in a call using "Object.Operation"
2768 -- notation. In that case we must pass the level of the object,
2769 -- so Prev_Orig is reset to Prev and the attribute will be
2770 -- processed by the code for Access attributes further below.
2772 if Prev_Orig
/= Prev
2773 and then Nkind
(Prev
) = N_Attribute_Reference
2775 Get_Attribute_Id
(Attribute_Name
(Prev
)) = Attribute_Access
2776 and then Is_Aliased_View
(Prev_Orig
)
2781 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2782 -- accessibility levels.
2784 if Is_Thunk
(Current_Scope
) then
2786 Parm_Ent
: Entity_Id
;
2789 if Is_Controlling_Actual
(Actual
) then
2791 -- Find the corresponding actual of the thunk
2793 Parm_Ent
:= First_Entity
(Current_Scope
);
2794 for J
in 2 .. Param_Count
loop
2795 Next_Entity
(Parm_Ent
);
2798 -- Handle unchecked conversion of access types generated
2799 -- in thunks (cf. Expand_Interface_Thunk).
2801 elsif Is_Access_Type
(Etype
(Actual
))
2802 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
2804 Parm_Ent
:= Entity
(Expression
(Actual
));
2806 else pragma Assert
(Is_Entity_Name
(Actual
));
2807 Parm_Ent
:= Entity
(Actual
);
2811 (New_Occurrence_Of
(Extra_Accessibility
(Parm_Ent
), Loc
),
2812 Extra_Accessibility
(Formal
));
2815 elsif Is_Entity_Name
(Prev_Orig
) then
2817 -- When passing an access parameter, or a renaming of an access
2818 -- parameter, as the actual to another access parameter we need
2819 -- to pass along the actual's own access level parameter. This
2820 -- is done if we are within the scope of the formal access
2821 -- parameter (if this is an inlined body the extra formal is
2824 if (Is_Formal
(Entity
(Prev_Orig
))
2826 (Present
(Renamed_Object
(Entity
(Prev_Orig
)))
2828 Is_Entity_Name
(Renamed_Object
(Entity
(Prev_Orig
)))
2831 (Entity
(Renamed_Object
(Entity
(Prev_Orig
))))))
2832 and then Ekind
(Etype
(Prev_Orig
)) = E_Anonymous_Access_Type
2833 and then In_Open_Scopes
(Scope
(Entity
(Prev_Orig
)))
2836 Parm_Ent
: constant Entity_Id
:= Param_Entity
(Prev_Orig
);
2839 pragma Assert
(Present
(Parm_Ent
));
2841 if Present
(Extra_Accessibility
(Parm_Ent
)) then
2844 (Extra_Accessibility
(Parm_Ent
), Loc
),
2845 Extra_Accessibility
(Formal
));
2847 -- If the actual access parameter does not have an
2848 -- associated extra formal providing its scope level,
2849 -- then treat the actual as having library-level
2854 (Make_Integer_Literal
(Loc
,
2855 Intval
=> Scope_Depth
(Standard_Standard
)),
2856 Extra_Accessibility
(Formal
));
2860 -- The actual is a normal access value, so just pass the level
2861 -- of the actual's access type.
2865 (Dynamic_Accessibility_Level
(Prev_Orig
),
2866 Extra_Accessibility
(Formal
));
2869 -- If the actual is an access discriminant, then pass the level
2870 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2872 elsif Nkind
(Prev_Orig
) = N_Selected_Component
2873 and then Ekind
(Entity
(Selector_Name
(Prev_Orig
))) =
2875 and then Ekind
(Etype
(Entity
(Selector_Name
(Prev_Orig
)))) =
2876 E_Anonymous_Access_Type
2879 (Make_Integer_Literal
(Loc
,
2880 Intval
=> Object_Access_Level
(Prefix
(Prev_Orig
))),
2881 Extra_Accessibility
(Formal
));
2886 case Nkind
(Prev_Orig
) is
2888 when N_Attribute_Reference
=>
2889 case Get_Attribute_Id
(Attribute_Name
(Prev_Orig
)) is
2891 -- For X'Access, pass on the level of the prefix X
2893 when Attribute_Access
=>
2895 -- If this is an Access attribute applied to the
2896 -- the current instance object passed to a type
2897 -- initialization procedure, then use the level
2898 -- of the type itself. This is not really correct,
2899 -- as there should be an extra level parameter
2900 -- passed in with _init formals (only in the case
2901 -- where the type is immutably limited), but we
2902 -- don't have an easy way currently to create such
2903 -- an extra formal (init procs aren't ever frozen).
2904 -- For now we just use the level of the type,
2905 -- which may be too shallow, but that works better
2906 -- than passing Object_Access_Level of the type,
2907 -- which can be one level too deep in some cases.
2910 if Is_Entity_Name
(Prefix
(Prev_Orig
))
2911 and then Is_Type
(Entity
(Prefix
(Prev_Orig
)))
2914 (Make_Integer_Literal
(Loc
,
2917 (Entity
(Prefix
(Prev_Orig
)))),
2918 Extra_Accessibility
(Formal
));
2922 (Make_Integer_Literal
(Loc
,
2925 (Prefix
(Prev_Orig
))),
2926 Extra_Accessibility
(Formal
));
2929 -- Treat the unchecked attributes as library-level
2931 when Attribute_Unchecked_Access |
2932 Attribute_Unrestricted_Access
=>
2934 (Make_Integer_Literal
(Loc
,
2935 Intval
=> Scope_Depth
(Standard_Standard
)),
2936 Extra_Accessibility
(Formal
));
2938 -- No other cases of attributes returning access
2939 -- values that can be passed to access parameters.
2942 raise Program_Error
;
2946 -- For allocators we pass the level of the execution of the
2947 -- called subprogram, which is one greater than the current
2952 (Make_Integer_Literal
(Loc
,
2953 Intval
=> Scope_Depth
(Current_Scope
) + 1),
2954 Extra_Accessibility
(Formal
));
2956 -- For most other cases we simply pass the level of the
2957 -- actual's access type. The type is retrieved from
2958 -- Prev rather than Prev_Orig, because in some cases
2959 -- Prev_Orig denotes an original expression that has
2960 -- not been analyzed.
2964 (Dynamic_Accessibility_Level
(Prev
),
2965 Extra_Accessibility
(Formal
));
2970 -- Perform the check of 4.6(49) that prevents a null value from being
2971 -- passed as an actual to an access parameter. Note that the check
2972 -- is elided in the common cases of passing an access attribute or
2973 -- access parameter as an actual. Also, we currently don't enforce
2974 -- this check for expander-generated actuals and when -gnatdj is set.
2976 if Ada_Version
>= Ada_2005
then
2978 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
2979 -- the intent of 6.4.1(13) is that null-exclusion checks should
2980 -- not be done for 'out' parameters, even though it refers only
2981 -- to constraint checks, and a null_exclusion is not a constraint.
2982 -- Note that AI05-0196-1 corrects this mistake in the RM.
2984 if Is_Access_Type
(Etype
(Formal
))
2985 and then Can_Never_Be_Null
(Etype
(Formal
))
2986 and then Ekind
(Formal
) /= E_Out_Parameter
2987 and then Nkind
(Prev
) /= N_Raise_Constraint_Error
2988 and then (Known_Null
(Prev
)
2989 or else not Can_Never_Be_Null
(Etype
(Prev
)))
2991 Install_Null_Excluding_Check
(Prev
);
2994 -- Ada_Version < Ada_2005
2997 if Ekind
(Etype
(Formal
)) /= E_Anonymous_Access_Type
2998 or else Access_Checks_Suppressed
(Subp
)
3002 elsif Debug_Flag_J
then
3005 elsif not Comes_From_Source
(Prev
) then
3008 elsif Is_Entity_Name
(Prev
)
3009 and then Ekind
(Etype
(Prev
)) = E_Anonymous_Access_Type
3013 elsif Nkind_In
(Prev
, N_Allocator
, N_Attribute_Reference
) then
3016 -- Suppress null checks when passing to access parameters of Java
3017 -- and CIL subprograms. (Should this be done for other foreign
3018 -- conventions as well ???)
3020 elsif Convention
(Subp
) = Convention_Java
3021 or else Convention
(Subp
) = Convention_CIL
3026 Install_Null_Excluding_Check
(Prev
);
3030 -- Perform appropriate validity checks on parameters that
3033 if Validity_Checks_On
then
3034 if (Ekind
(Formal
) = E_In_Parameter
3035 and then Validity_Check_In_Params
)
3037 (Ekind
(Formal
) = E_In_Out_Parameter
3038 and then Validity_Check_In_Out_Params
)
3040 -- If the actual is an indexed component of a packed type (or
3041 -- is an indexed or selected component whose prefix recursively
3042 -- meets this condition), it has not been expanded yet. It will
3043 -- be copied in the validity code that follows, and has to be
3044 -- expanded appropriately, so reanalyze it.
3046 -- What we do is just to unset analyzed bits on prefixes till
3047 -- we reach something that does not have a prefix.
3054 while Nkind_In
(Nod
, N_Indexed_Component
,
3055 N_Selected_Component
)
3057 Set_Analyzed
(Nod
, False);
3058 Nod
:= Prefix
(Nod
);
3062 Ensure_Valid
(Actual
);
3066 -- For Ada 2012, if a parameter is aliased, the actual must be a
3067 -- tagged type or an aliased view of an object.
3069 if Is_Aliased
(Formal
)
3070 and then not Is_Aliased_View
(Actual
)
3071 and then not Is_Tagged_Type
(Etype
(Formal
))
3074 ("actual for aliased formal& must be aliased object",
3078 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3079 -- since this is a left side reference. We only do this for calls
3080 -- from the source program since we assume that compiler generated
3081 -- calls explicitly generate any required checks. We also need it
3082 -- only if we are doing standard validity checks, since clearly it is
3083 -- not needed if validity checks are off, and in subscript validity
3084 -- checking mode, all indexed components are checked with a call
3085 -- directly from Expand_N_Indexed_Component.
3087 if Comes_From_Source
(Call_Node
)
3088 and then Ekind
(Formal
) /= E_In_Parameter
3089 and then Validity_Checks_On
3090 and then Validity_Check_Default
3091 and then not Validity_Check_Subscripts
3093 Check_Valid_Lvalue_Subscripts
(Actual
);
3096 -- Mark any scalar OUT parameter that is a simple variable as no
3097 -- longer known to be valid (unless the type is always valid). This
3098 -- reflects the fact that if an OUT parameter is never set in a
3099 -- procedure, then it can become invalid on the procedure return.
3101 if Ekind
(Formal
) = E_Out_Parameter
3102 and then Is_Entity_Name
(Actual
)
3103 and then Ekind
(Entity
(Actual
)) = E_Variable
3104 and then not Is_Known_Valid
(Etype
(Actual
))
3106 Set_Is_Known_Valid
(Entity
(Actual
), False);
3109 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3110 -- clear current values, since they can be clobbered. We are probably
3111 -- doing this in more places than we need to, but better safe than
3112 -- sorry when it comes to retaining bad current values.
3114 if Ekind
(Formal
) /= E_In_Parameter
3115 and then Is_Entity_Name
(Actual
)
3116 and then Present
(Entity
(Actual
))
3119 Ent
: constant Entity_Id
:= Entity
(Actual
);
3123 -- For an OUT or IN OUT parameter that is an assignable entity,
3124 -- we do not want to clobber the Last_Assignment field, since
3125 -- if it is set, it was precisely because it is indeed an OUT
3126 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
3127 -- since the subprogram could have returned in invalid value.
3129 if Ekind_In
(Formal
, E_Out_Parameter
, E_In_Out_Parameter
)
3130 and then Is_Assignable
(Ent
)
3132 Sav
:= Last_Assignment
(Ent
);
3133 Kill_Current_Values
(Ent
);
3134 Set_Last_Assignment
(Ent
, Sav
);
3135 Set_Is_Known_Valid
(Ent
, False);
3137 -- For all other cases, just kill the current values
3140 Kill_Current_Values
(Ent
);
3145 -- If the formal is class wide and the actual is an aggregate, force
3146 -- evaluation so that the back end who does not know about class-wide
3147 -- type, does not generate a temporary of the wrong size.
3149 if not Is_Class_Wide_Type
(Etype
(Formal
)) then
3152 elsif Nkind
(Actual
) = N_Aggregate
3153 or else (Nkind
(Actual
) = N_Qualified_Expression
3154 and then Nkind
(Expression
(Actual
)) = N_Aggregate
)
3156 Force_Evaluation
(Actual
);
3159 -- In a remote call, if the formal is of a class-wide type, check
3160 -- that the actual meets the requirements described in E.4(18).
3162 if Remote
and then Is_Class_Wide_Type
(Etype
(Formal
)) then
3163 Insert_Action
(Actual
,
3164 Make_Transportable_Check
(Loc
,
3165 Duplicate_Subexpr_Move_Checks
(Actual
)));
3168 -- This label is required when skipping extra actual generation for
3169 -- Unchecked_Union parameters.
3171 <<Skip_Extra_Actual_Generation
>>
3173 Param_Count
:= Param_Count
+ 1;
3174 Next_Actual
(Actual
);
3175 Next_Formal
(Formal
);
3178 -- If we are calling an Ada 2012 function which needs to have the
3179 -- "accessibility level determined by the point of call" (AI05-0234)
3180 -- passed in to it, then pass it in.
3182 if Ekind_In
(Subp
, E_Function
, E_Operator
, E_Subprogram_Type
)
3184 Present
(Extra_Accessibility_Of_Result
(Ultimate_Alias
(Subp
)))
3187 Ancestor
: Node_Id
:= Parent
(Call_Node
);
3188 Level
: Node_Id
:= Empty
;
3189 Defer
: Boolean := False;
3192 -- Unimplemented: if Subp returns an anonymous access type, then
3194 -- a) if the call is the operand of an explict conversion, then
3195 -- the target type of the conversion (a named access type)
3196 -- determines the accessibility level pass in;
3198 -- b) if the call defines an access discriminant of an object
3199 -- (e.g., the discriminant of an object being created by an
3200 -- allocator, or the discriminant of a function result),
3201 -- then the accessibility level to pass in is that of the
3202 -- discriminated object being initialized).
3206 while Nkind
(Ancestor
) = N_Qualified_Expression
3208 Ancestor
:= Parent
(Ancestor
);
3211 case Nkind
(Ancestor
) is
3214 -- At this point, we'd like to assign
3216 -- Level := Dynamic_Accessibility_Level (Ancestor);
3218 -- but Etype of Ancestor may not have been set yet,
3219 -- so that doesn't work.
3221 -- Handle this later in Expand_Allocator_Expression.
3225 when N_Object_Declaration | N_Object_Renaming_Declaration
=>
3227 Def_Id
: constant Entity_Id
:=
3228 Defining_Identifier
(Ancestor
);
3231 if Is_Return_Object
(Def_Id
) then
3232 if Present
(Extra_Accessibility_Of_Result
3233 (Return_Applies_To
(Scope
(Def_Id
))))
3235 -- Pass along value that was passed in if the
3236 -- routine we are returning from also has an
3237 -- Accessibility_Of_Result formal.
3241 (Extra_Accessibility_Of_Result
3242 (Return_Applies_To
(Scope
(Def_Id
))), Loc
);
3246 Make_Integer_Literal
(Loc
,
3247 Intval
=> Object_Access_Level
(Def_Id
));
3251 when N_Simple_Return_Statement
=>
3252 if Present
(Extra_Accessibility_Of_Result
3254 (Return_Statement_Entity
(Ancestor
))))
3256 -- Pass along value that was passed in if the returned
3257 -- routine also has an 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
),
3421 (First_Tag_Component
(Typ
), Loc
)),
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 -- Deal with case where call is an explicit dereference
3646 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
3648 -- Handle case of access to protected subprogram type
3650 if Is_Access_Protected_Subprogram_Type
3651 (Base_Type
(Etype
(Prefix
(Name
(Call_Node
)))))
3653 -- If this is a call through an access to protected operation, the
3654 -- prefix has the form (object'address, operation'access). Rewrite
3655 -- as a for other protected calls: the object is the 1st parameter
3656 -- of the list of actuals.
3663 Ptr
: constant Node_Id
:= Prefix
(Name
(Call_Node
));
3665 T
: constant Entity_Id
:=
3666 Equivalent_Type
(Base_Type
(Etype
(Ptr
)));
3668 D_T
: constant Entity_Id
:=
3669 Designated_Type
(Base_Type
(Etype
(Ptr
)));
3673 Make_Selected_Component
(Loc
,
3674 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
3676 New_Occurrence_Of
(First_Entity
(T
), Loc
));
3679 Make_Selected_Component
(Loc
,
3680 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
3682 New_Occurrence_Of
(Next_Entity
(First_Entity
(T
)), Loc
));
3685 Make_Explicit_Dereference
(Loc
,
3688 if Present
(Parameter_Associations
(Call_Node
)) then
3689 Parm
:= Parameter_Associations
(Call_Node
);
3694 Prepend
(Obj
, Parm
);
3696 if Etype
(D_T
) = Standard_Void_Type
then
3698 Make_Procedure_Call_Statement
(Loc
,
3700 Parameter_Associations
=> Parm
);
3703 Make_Function_Call
(Loc
,
3705 Parameter_Associations
=> Parm
);
3708 Set_First_Named_Actual
(Call
, First_Named_Actual
(Call_Node
));
3709 Set_Etype
(Call
, Etype
(D_T
));
3711 -- We do not re-analyze the call to avoid infinite recursion.
3712 -- We analyze separately the prefix and the object, and set
3713 -- the checks on the prefix that would otherwise be emitted
3714 -- when resolving a call.
3716 Rewrite
(Call_Node
, Call
);
3718 Apply_Access_Check
(Nam
);
3725 -- If this is a call to an intrinsic subprogram, then perform the
3726 -- appropriate expansion to the corresponding tree node and we
3727 -- are all done (since after that the call is gone).
3729 -- In the case where the intrinsic is to be processed by the back end,
3730 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3731 -- since the idea in this case is to pass the call unchanged. If the
3732 -- intrinsic is an inherited unchecked conversion, and the derived type
3733 -- is the target type of the conversion, we must retain it as the return
3734 -- type of the expression. Otherwise the expansion below, which uses the
3735 -- parent operation, will yield the wrong type.
3737 if Is_Intrinsic_Subprogram
(Subp
) then
3738 Expand_Intrinsic_Call
(Call_Node
, Subp
);
3740 if Nkind
(Call_Node
) = N_Unchecked_Type_Conversion
3741 and then Parent_Subp
/= Orig_Subp
3742 and then Etype
(Parent_Subp
) /= Etype
(Orig_Subp
)
3744 Set_Etype
(Call_Node
, Etype
(Orig_Subp
));
3750 if Ekind_In
(Subp
, E_Function
, E_Procedure
) then
3752 -- We perform two simple optimization on calls:
3754 -- a) replace calls to null procedures unconditionally;
3756 -- b) for To_Address, just do an unchecked conversion. Not only is
3757 -- this efficient, but it also avoids order of elaboration problems
3758 -- when address clauses are inlined (address expression elaborated
3759 -- at the wrong point).
3761 -- We perform these optimization regardless of whether we are in the
3762 -- main unit or in a unit in the context of the main unit, to ensure
3763 -- that tree generated is the same in both cases, for CodePeer use.
3765 if Is_RTE
(Subp
, RE_To_Address
) then
3767 Unchecked_Convert_To
3768 (RTE
(RE_Address
), Relocate_Node
(First_Actual
(Call_Node
))));
3771 elsif Is_Null_Procedure
(Subp
) then
3772 Rewrite
(Call_Node
, Make_Null_Statement
(Loc
));
3776 -- Handle inlining (old semantics)
3778 if Is_Inlined
(Subp
) and then not Debug_Flag_Dot_K
then
3779 Inlined_Subprogram
: declare
3781 Must_Inline
: Boolean := False;
3782 Spec
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
3785 -- Verify that the body to inline has already been seen, and
3786 -- that if the body is in the current unit the inlining does
3787 -- not occur earlier. This avoids order-of-elaboration problems
3790 -- This should be documented in sinfo/einfo ???
3793 or else Nkind
(Spec
) /= N_Subprogram_Declaration
3794 or else No
(Body_To_Inline
(Spec
))
3796 Must_Inline
:= False;
3798 -- If this an inherited function that returns a private type,
3799 -- do not inline if the full view is an unconstrained array,
3800 -- because such calls cannot be inlined.
3802 elsif Present
(Orig_Subp
)
3803 and then Is_Array_Type
(Etype
(Orig_Subp
))
3804 and then not Is_Constrained
(Etype
(Orig_Subp
))
3806 Must_Inline
:= False;
3808 elsif In_Unfrozen_Instance
(Scope
(Subp
)) then
3809 Must_Inline
:= False;
3812 Bod
:= Body_To_Inline
(Spec
);
3814 if (In_Extended_Main_Code_Unit
(Call_Node
)
3815 or else In_Extended_Main_Code_Unit
(Parent
(Call_Node
))
3816 or else Has_Pragma_Inline_Always
(Subp
))
3817 and then (not In_Same_Extended_Unit
(Sloc
(Bod
), Loc
)
3819 Earlier_In_Extended_Unit
(Sloc
(Bod
), Loc
))
3821 Must_Inline
:= True;
3823 -- If we are compiling a package body that is not the main
3824 -- unit, it must be for inlining/instantiation purposes,
3825 -- in which case we inline the call to insure that the same
3826 -- temporaries are generated when compiling the body by
3827 -- itself. Otherwise link errors can occur.
3829 -- If the function being called is itself in the main unit,
3830 -- we cannot inline, because there is a risk of double
3831 -- elaboration and/or circularity: the inlining can make
3832 -- visible a private entity in the body of the main unit,
3833 -- that gigi will see before its sees its proper definition.
3835 elsif not (In_Extended_Main_Code_Unit
(Call_Node
))
3836 and then In_Package_Body
3838 Must_Inline
:= not In_Extended_Main_Source_Unit
(Subp
);
3843 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
3846 -- Let the back end handle it
3848 Add_Inlined_Body
(Subp
);
3850 if Front_End_Inlining
3851 and then Nkind
(Spec
) = N_Subprogram_Declaration
3852 and then (In_Extended_Main_Code_Unit
(Call_Node
))
3853 and then No
(Body_To_Inline
(Spec
))
3854 and then not Has_Completion
(Subp
)
3855 and then In_Same_Extended_Unit
(Sloc
(Spec
), Loc
)
3858 ("cannot inline& (body not seen yet)?",
3862 end Inlined_Subprogram
;
3864 -- Handle inlining (new semantics)
3866 elsif Is_Inlined
(Subp
) then
3868 Spec
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
3871 if Must_Inline
(Subp
) then
3872 if In_Extended_Main_Code_Unit
(Call_Node
)
3873 and then In_Same_Extended_Unit
(Sloc
(Spec
), Loc
)
3874 and then not Has_Completion
(Subp
)
3877 ("cannot inline& (body not seen yet)?",
3881 Do_Inline_Always
(Subp
, Orig_Subp
);
3884 elsif Optimization_Level
> 0 then
3885 Do_Inline
(Subp
, Orig_Subp
);
3888 -- The call may have been inlined or may have been passed to
3889 -- the backend. No further action needed if it was inlined.
3891 if Nkind
(N
) /= N_Function_Call
then
3898 -- Check for protected subprogram. This is either an intra-object call,
3899 -- or a protected function call. Protected procedure calls are rewritten
3900 -- as entry calls and handled accordingly.
3902 -- In Ada 2005, this may be an indirect call to an access parameter that
3903 -- is an access_to_subprogram. In that case the anonymous type has a
3904 -- scope that is a protected operation, but the call is a regular one.
3905 -- In either case do not expand call if subprogram is eliminated.
3907 Scop
:= Scope
(Subp
);
3909 if Nkind
(Call_Node
) /= N_Entry_Call_Statement
3910 and then Is_Protected_Type
(Scop
)
3911 and then Ekind
(Subp
) /= E_Subprogram_Type
3912 and then not Is_Eliminated
(Subp
)
3914 -- If the call is an internal one, it is rewritten as a call to the
3915 -- corresponding unprotected subprogram.
3917 Expand_Protected_Subprogram_Call
(Call_Node
, Subp
, Scop
);
3920 -- Functions returning controlled objects need special attention. If
3921 -- the return type is limited, then the context is initialization and
3922 -- different processing applies. If the call is to a protected function,
3923 -- the expansion above will call Expand_Call recursively. Otherwise the
3924 -- function call is transformed into a temporary which obtains the
3925 -- result from the secondary stack.
3927 if Needs_Finalization
(Etype
(Subp
)) then
3928 if not Is_Limited_View
(Etype
(Subp
))
3930 (No
(First_Formal
(Subp
))
3932 not Is_Concurrent_Record_Type
(Etype
(First_Formal
(Subp
))))
3934 Expand_Ctrl_Function_Call
(Call_Node
);
3936 -- Build-in-place function calls which appear in anonymous contexts
3937 -- need a transient scope to ensure the proper finalization of the
3938 -- intermediate result after its use.
3940 elsif Is_Build_In_Place_Function_Call
(Call_Node
)
3942 Nkind_In
(Parent
(Call_Node
), N_Attribute_Reference
,
3944 N_Indexed_Component
,
3945 N_Object_Renaming_Declaration
,
3946 N_Procedure_Call_Statement
,
3947 N_Selected_Component
,
3950 Establish_Transient_Scope
(Call_Node
, Sec_Stack
=> True);
3954 -- Test for First_Optional_Parameter, and if so, truncate parameter list
3955 -- if there are optional parameters at the trailing end.
3956 -- Note: we never delete procedures for call via a pointer.
3958 if (Ekind
(Subp
) = E_Procedure
or else Ekind
(Subp
) = E_Function
)
3959 and then Present
(First_Optional_Parameter
(Subp
))
3962 Last_Keep_Arg
: Node_Id
;
3965 -- Last_Keep_Arg will hold the last actual that should be kept.
3966 -- If it remains empty at the end, it means that all parameters
3969 Last_Keep_Arg
:= Empty
;
3971 -- Find first optional parameter, must be present since we checked
3972 -- the validity of the parameter before setting it.
3974 Formal
:= First_Formal
(Subp
);
3975 Actual
:= First_Actual
(Call_Node
);
3976 while Formal
/= First_Optional_Parameter
(Subp
) loop
3977 Last_Keep_Arg
:= Actual
;
3978 Next_Formal
(Formal
);
3979 Next_Actual
(Actual
);
3982 -- We have Formal and Actual pointing to the first potentially
3983 -- droppable argument. We can drop all the trailing arguments
3984 -- whose actual matches the default. Note that we know that all
3985 -- remaining formals have defaults, because we checked that this
3986 -- requirement was met before setting First_Optional_Parameter.
3988 -- We use Fully_Conformant_Expressions to check for identity
3989 -- between formals and actuals, which may miss some cases, but
3990 -- on the other hand, this is only an optimization (if we fail
3991 -- to truncate a parameter it does not affect functionality).
3992 -- So if the default is 3 and the actual is 1+2, we consider
3993 -- them unequal, which hardly seems worrisome.
3995 while Present
(Formal
) loop
3996 if not Fully_Conformant_Expressions
3997 (Actual
, Default_Value
(Formal
))
3999 Last_Keep_Arg
:= Actual
;
4002 Next_Formal
(Formal
);
4003 Next_Actual
(Actual
);
4006 -- If no arguments, delete entire list, this is the easy case
4008 if No
(Last_Keep_Arg
) then
4009 Set_Parameter_Associations
(Call_Node
, No_List
);
4010 Set_First_Named_Actual
(Call_Node
, Empty
);
4012 -- Case where at the last retained argument is positional. This
4013 -- is also an easy case, since the retained arguments are already
4014 -- in the right form, and we don't need to worry about the order
4015 -- of arguments that get eliminated.
4017 elsif Is_List_Member
(Last_Keep_Arg
) then
4018 while Present
(Next
(Last_Keep_Arg
)) loop
4019 Discard_Node
(Remove_Next
(Last_Keep_Arg
));
4022 Set_First_Named_Actual
(Call_Node
, Empty
);
4024 -- This is the annoying case where the last retained argument
4025 -- is a named parameter. Since the original arguments are not
4026 -- in declaration order, we may have to delete some fairly
4027 -- random collection of arguments.
4035 -- First step, remove all the named parameters from the
4036 -- list (they are still chained using First_Named_Actual
4037 -- and Next_Named_Actual, so we have not lost them).
4039 Temp
:= First
(Parameter_Associations
(Call_Node
));
4041 -- Case of all parameters named, remove them all
4043 if Nkind
(Temp
) = N_Parameter_Association
then
4044 -- Suppress warnings to avoid warning on possible
4045 -- infinite loop (because Call_Node is not modified).
4047 pragma Warnings
(Off
);
4048 while Is_Non_Empty_List
4049 (Parameter_Associations
(Call_Node
))
4052 Remove_Head
(Parameter_Associations
(Call_Node
));
4054 pragma Warnings
(On
);
4056 -- Case of mixed positional/named, remove named parameters
4059 while Nkind
(Next
(Temp
)) /= N_Parameter_Association
loop
4063 while Present
(Next
(Temp
)) loop
4064 Remove
(Next
(Temp
));
4068 -- Now we loop through the named parameters, till we get
4069 -- to the last one to be retained, adding them to the list.
4070 -- Note that the Next_Named_Actual list does not need to be
4071 -- touched since we are only reordering them on the actual
4072 -- parameter association list.
4074 Passoc
:= Parent
(First_Named_Actual
(Call_Node
));
4076 Temp
:= Relocate_Node
(Passoc
);
4078 (Parameter_Associations
(Call_Node
), Temp
);
4080 Last_Keep_Arg
= Explicit_Actual_Parameter
(Passoc
);
4081 Passoc
:= Parent
(Next_Named_Actual
(Passoc
));
4084 Set_Next_Named_Actual
(Temp
, Empty
);
4087 Temp
:= Next_Named_Actual
(Passoc
);
4088 exit when No
(Temp
);
4089 Set_Next_Named_Actual
4090 (Passoc
, Next_Named_Actual
(Parent
(Temp
)));
4099 -------------------------------
4100 -- Expand_Ctrl_Function_Call --
4101 -------------------------------
4103 procedure Expand_Ctrl_Function_Call
(N
: Node_Id
) is
4105 -- Optimization, if the returned value (which is on the sec-stack) is
4106 -- returned again, no need to copy/readjust/finalize, we can just pass
4107 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
4108 -- attachment is needed
4110 if Nkind
(Parent
(N
)) = N_Simple_Return_Statement
then
4114 -- Resolution is now finished, make sure we don't start analysis again
4115 -- because of the duplication.
4119 -- A function which returns a controlled object uses the secondary
4120 -- stack. Rewrite the call into a temporary which obtains the result of
4121 -- the function using 'reference.
4123 Remove_Side_Effects
(N
);
4125 -- When the temporary function result appears inside a case or an if
4126 -- expression, its lifetime must be extended to match that of the
4127 -- context. If not, the function result would be finalized prematurely
4128 -- and the evaluation of the expression could yield the wrong result.
4130 if Within_Case_Or_If_Expression
(N
)
4131 and then Nkind
(N
) = N_Explicit_Dereference
4133 Set_Is_Processed_Transient
(Entity
(Prefix
(N
)));
4135 end Expand_Ctrl_Function_Call
;
4137 -------------------------
4138 -- Expand_Inlined_Call --
4139 -------------------------
4141 procedure Expand_Inlined_Call
4144 Orig_Subp
: Entity_Id
)
4146 Loc
: constant Source_Ptr
:= Sloc
(N
);
4147 Is_Predef
: constant Boolean :=
4148 Is_Predefined_File_Name
4149 (Unit_File_Name
(Get_Source_Unit
(Subp
)));
4150 Orig_Bod
: constant Node_Id
:=
4151 Body_To_Inline
(Unit_Declaration_Node
(Subp
));
4155 Decls
: constant List_Id
:= New_List
;
4156 Exit_Lab
: Entity_Id
:= Empty
;
4163 Ret_Type
: Entity_Id
;
4166 -- The target of the call. If context is an assignment statement then
4167 -- this is the left-hand side of the assignment, else it is a temporary
4168 -- to which the return value is assigned prior to rewriting the call.
4171 -- A separate target used when the return type is unconstrained
4174 Temp_Typ
: Entity_Id
;
4176 Return_Object
: Entity_Id
:= Empty
;
4177 -- Entity in declaration in an extended_return_statement
4180 Is_Unc_Decl
: Boolean;
4181 -- If the type returned by the function is unconstrained and the call
4182 -- can be inlined, special processing is required.
4184 procedure Make_Exit_Label
;
4185 -- Build declaration for exit label to be used in Return statements,
4186 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
4187 -- declaration). Does nothing if Exit_Lab already set.
4189 function Process_Formals
(N
: Node_Id
) return Traverse_Result
;
4190 -- Replace occurrence of a formal with the corresponding actual, or the
4191 -- thunk generated for it. Replace a return statement with an assignment
4192 -- to the target of the call, with appropriate conversions if needed.
4194 function Process_Sloc
(Nod
: Node_Id
) return Traverse_Result
;
4195 -- If the call being expanded is that of an internal subprogram, set the
4196 -- sloc of the generated block to that of the call itself, so that the
4197 -- expansion is skipped by the "next" command in gdb.
4198 -- Same processing for a subprogram in a predefined file, e.g.
4199 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change to
4200 -- simplify our own development.
4202 procedure Reset_Dispatching_Calls
(N
: Node_Id
);
4203 -- In subtree N search for occurrences of dispatching calls that use the
4204 -- Ada 2005 Object.Operation notation and the object is a formal of the
4205 -- inlined subprogram. Reset the entity associated with Operation in all
4206 -- the found occurrences.
4208 procedure Rewrite_Function_Call
(N
: Node_Id
; Blk
: Node_Id
);
4209 -- If the function body is a single expression, replace call with
4210 -- expression, else insert block appropriately.
4212 procedure Rewrite_Procedure_Call
(N
: Node_Id
; Blk
: Node_Id
);
4213 -- If procedure body has no local variables, inline body without
4214 -- creating block, otherwise rewrite call with block.
4216 function Formal_Is_Used_Once
(Formal
: Entity_Id
) return Boolean;
4217 -- Determine whether a formal parameter is used only once in Orig_Bod
4219 ---------------------
4220 -- Make_Exit_Label --
4221 ---------------------
4223 procedure Make_Exit_Label
is
4224 Lab_Ent
: Entity_Id
;
4226 if No
(Exit_Lab
) then
4227 Lab_Ent
:= Make_Temporary
(Loc
, 'L');
4228 Lab_Id
:= New_Occurrence_Of
(Lab_Ent
, Loc
);
4229 Exit_Lab
:= Make_Label
(Loc
, Lab_Id
);
4231 Make_Implicit_Label_Declaration
(Loc
,
4232 Defining_Identifier
=> Lab_Ent
,
4233 Label_Construct
=> Exit_Lab
);
4235 end Make_Exit_Label
;
4237 ---------------------
4238 -- Process_Formals --
4239 ---------------------
4241 function Process_Formals
(N
: Node_Id
) return Traverse_Result
is
4247 if Is_Entity_Name
(N
) and then Present
(Entity
(N
)) then
4250 if Is_Formal
(E
) and then Scope
(E
) = Subp
then
4251 A
:= Renamed_Object
(E
);
4253 -- Rewrite the occurrence of the formal into an occurrence of
4254 -- the actual. Also establish visibility on the proper view of
4255 -- the actual's subtype for the body's context (if the actual's
4256 -- subtype is private at the call point but its full view is
4257 -- visible to the body, then the inlined tree here must be
4258 -- analyzed with the full view).
4260 if Is_Entity_Name
(A
) then
4261 Rewrite
(N
, New_Occurrence_Of
(Entity
(A
), Loc
));
4262 Check_Private_View
(N
);
4264 elsif Nkind
(A
) = N_Defining_Identifier
then
4265 Rewrite
(N
, New_Occurrence_Of
(A
, Loc
));
4266 Check_Private_View
(N
);
4271 Rewrite
(N
, New_Copy
(A
));
4277 elsif Is_Entity_Name
(N
)
4278 and then Present
(Return_Object
)
4279 and then Chars
(N
) = Chars
(Return_Object
)
4281 -- Occurrence within an extended return statement. The return
4282 -- object is local to the body been inlined, and thus the generic
4283 -- copy is not analyzed yet, so we match by name, and replace it
4284 -- with target of call.
4286 if Nkind
(Targ
) = N_Defining_Identifier
then
4287 Rewrite
(N
, New_Occurrence_Of
(Targ
, Loc
));
4289 Rewrite
(N
, New_Copy_Tree
(Targ
));
4294 elsif Nkind
(N
) = N_Simple_Return_Statement
then
4295 if No
(Expression
(N
)) then
4298 Make_Goto_Statement
(Loc
, Name
=> New_Copy
(Lab_Id
)));
4301 if Nkind
(Parent
(N
)) = N_Handled_Sequence_Of_Statements
4302 and then Nkind
(Parent
(Parent
(N
))) = N_Subprogram_Body
4304 -- Function body is a single expression. No need for
4310 Num_Ret
:= Num_Ret
+ 1;
4314 -- Because of the presence of private types, the views of the
4315 -- expression and the context may be different, so place an
4316 -- unchecked conversion to the context type to avoid spurious
4317 -- errors, e.g. when the expression is a numeric literal and
4318 -- the context is private. If the expression is an aggregate,
4319 -- use a qualified expression, because an aggregate is not a
4320 -- legal argument of a conversion. Ditto for numeric literals,
4321 -- which must be resolved to a specific type.
4323 if Nkind_In
(Expression
(N
), N_Aggregate
,
4329 Make_Qualified_Expression
(Sloc
(N
),
4330 Subtype_Mark
=> New_Occurrence_Of
(Ret_Type
, Sloc
(N
)),
4331 Expression
=> Relocate_Node
(Expression
(N
)));
4334 Unchecked_Convert_To
4335 (Ret_Type
, Relocate_Node
(Expression
(N
)));
4338 if Nkind
(Targ
) = N_Defining_Identifier
then
4340 Make_Assignment_Statement
(Loc
,
4341 Name
=> New_Occurrence_Of
(Targ
, Loc
),
4342 Expression
=> Ret
));
4345 Make_Assignment_Statement
(Loc
,
4346 Name
=> New_Copy
(Targ
),
4347 Expression
=> Ret
));
4350 Set_Assignment_OK
(Name
(N
));
4352 if Present
(Exit_Lab
) then
4354 Make_Goto_Statement
(Loc
, Name
=> New_Copy
(Lab_Id
)));
4360 -- An extended return becomes a block whose first statement is the
4361 -- assignment of the initial expression of the return object to the
4362 -- target of the call itself.
4364 elsif Nkind
(N
) = N_Extended_Return_Statement
then
4366 Return_Decl
: constant Entity_Id
:=
4367 First
(Return_Object_Declarations
(N
));
4371 Return_Object
:= Defining_Identifier
(Return_Decl
);
4373 if Present
(Expression
(Return_Decl
)) then
4374 if Nkind
(Targ
) = N_Defining_Identifier
then
4376 Make_Assignment_Statement
(Loc
,
4377 Name
=> New_Occurrence_Of
(Targ
, Loc
),
4378 Expression
=> Expression
(Return_Decl
));
4381 Make_Assignment_Statement
(Loc
,
4382 Name
=> New_Copy
(Targ
),
4383 Expression
=> Expression
(Return_Decl
));
4386 Set_Assignment_OK
(Name
(Assign
));
4388 if No
(Handled_Statement_Sequence
(N
)) then
4389 Set_Handled_Statement_Sequence
(N
,
4390 Make_Handled_Sequence_Of_Statements
(Loc
,
4391 Statements
=> New_List
));
4395 Statements
(Handled_Statement_Sequence
(N
)));
4399 Make_Block_Statement
(Loc
,
4400 Handled_Statement_Sequence
=>
4401 Handled_Statement_Sequence
(N
)));
4406 -- Remove pragma Unreferenced since it may refer to formals that
4407 -- are not visible in the inlined body, and in any case we will
4408 -- not be posting warnings on the inlined body so it is unneeded.
4410 elsif Nkind
(N
) = N_Pragma
4411 and then Pragma_Name
(N
) = Name_Unreferenced
4413 Rewrite
(N
, Make_Null_Statement
(Sloc
(N
)));
4419 end Process_Formals
;
4421 procedure Replace_Formals
is new Traverse_Proc
(Process_Formals
);
4427 function Process_Sloc
(Nod
: Node_Id
) return Traverse_Result
is
4429 if not Debug_Generated_Code
then
4430 Set_Sloc
(Nod
, Sloc
(N
));
4431 Set_Comes_From_Source
(Nod
, False);
4437 procedure Reset_Slocs
is new Traverse_Proc
(Process_Sloc
);
4439 ------------------------------
4440 -- Reset_Dispatching_Calls --
4441 ------------------------------
4443 procedure Reset_Dispatching_Calls
(N
: Node_Id
) is
4445 function Do_Reset
(N
: Node_Id
) return Traverse_Result
;
4446 -- Comment required ???
4452 function Do_Reset
(N
: Node_Id
) return Traverse_Result
is
4454 if Nkind
(N
) = N_Procedure_Call_Statement
4455 and then Nkind
(Name
(N
)) = N_Selected_Component
4456 and then Nkind
(Prefix
(Name
(N
))) = N_Identifier
4457 and then Is_Formal
(Entity
(Prefix
(Name
(N
))))
4458 and then Is_Dispatching_Operation
4459 (Entity
(Selector_Name
(Name
(N
))))
4461 Set_Entity
(Selector_Name
(Name
(N
)), Empty
);
4467 function Do_Reset_Calls
is new Traverse_Func
(Do_Reset
);
4471 Dummy
: constant Traverse_Result
:= Do_Reset_Calls
(N
);
4472 pragma Unreferenced
(Dummy
);
4474 -- Start of processing for Reset_Dispatching_Calls
4478 end Reset_Dispatching_Calls
;
4480 ---------------------------
4481 -- Rewrite_Function_Call --
4482 ---------------------------
4484 procedure Rewrite_Function_Call
(N
: Node_Id
; Blk
: Node_Id
) is
4485 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(Blk
);
4486 Fst
: constant Node_Id
:= First
(Statements
(HSS
));
4489 -- Optimize simple case: function body is a single return statement,
4490 -- which has been expanded into an assignment.
4492 if Is_Empty_List
(Declarations
(Blk
))
4493 and then Nkind
(Fst
) = N_Assignment_Statement
4494 and then No
(Next
(Fst
))
4496 -- The function call may have been rewritten as the temporary
4497 -- that holds the result of the call, in which case remove the
4498 -- now useless declaration.
4500 if Nkind
(N
) = N_Identifier
4501 and then Nkind
(Parent
(Entity
(N
))) = N_Object_Declaration
4503 Rewrite
(Parent
(Entity
(N
)), Make_Null_Statement
(Loc
));
4506 Rewrite
(N
, Expression
(Fst
));
4508 elsif Nkind
(N
) = N_Identifier
4509 and then Nkind
(Parent
(Entity
(N
))) = N_Object_Declaration
4511 -- The block assigns the result of the call to the temporary
4513 Insert_After
(Parent
(Entity
(N
)), Blk
);
4515 -- If the context is an assignment, and the left-hand side is free of
4516 -- side-effects, the replacement is also safe.
4517 -- Can this be generalized further???
4519 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
4521 (Is_Entity_Name
(Name
(Parent
(N
)))
4523 (Nkind
(Name
(Parent
(N
))) = N_Explicit_Dereference
4524 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
)))))
4527 (Nkind
(Name
(Parent
(N
))) = N_Selected_Component
4528 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))))
4530 -- Replace assignment with the block
4533 Original_Assignment
: constant Node_Id
:= Parent
(N
);
4536 -- Preserve the original assignment node to keep the complete
4537 -- assignment subtree consistent enough for Analyze_Assignment
4538 -- to proceed (specifically, the original Lhs node must still
4539 -- have an assignment statement as its parent).
4541 -- We cannot rely on Original_Node to go back from the block
4542 -- node to the assignment node, because the assignment might
4543 -- already be a rewrite substitution.
4545 Discard_Node
(Relocate_Node
(Original_Assignment
));
4546 Rewrite
(Original_Assignment
, Blk
);
4549 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
then
4551 -- A call to a function which returns an unconstrained type
4552 -- found in the expression initializing an object-declaration is
4553 -- expanded into a procedure call which must be added after the
4554 -- object declaration.
4556 if Is_Unc_Decl
and then Debug_Flag_Dot_K
then
4557 Insert_Action_After
(Parent
(N
), Blk
);
4559 Set_Expression
(Parent
(N
), Empty
);
4560 Insert_After
(Parent
(N
), Blk
);
4563 elsif Is_Unc
and then not Debug_Flag_Dot_K
then
4564 Insert_Before
(Parent
(N
), Blk
);
4566 end Rewrite_Function_Call
;
4568 ----------------------------
4569 -- Rewrite_Procedure_Call --
4570 ----------------------------
4572 procedure Rewrite_Procedure_Call
(N
: Node_Id
; Blk
: Node_Id
) is
4573 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(Blk
);
4576 -- If there is a transient scope for N, this will be the scope of the
4577 -- actions for N, and the statements in Blk need to be within this
4578 -- scope. For example, they need to have visibility on the constant
4579 -- declarations created for the formals.
4581 -- If N needs no transient scope, and if there are no declarations in
4582 -- the inlined body, we can do a little optimization and insert the
4583 -- statements for the body directly after N, and rewrite N to a
4584 -- null statement, instead of rewriting N into a full-blown block
4587 if not Scope_Is_Transient
4588 and then Is_Empty_List
(Declarations
(Blk
))
4590 Insert_List_After
(N
, Statements
(HSS
));
4591 Rewrite
(N
, Make_Null_Statement
(Loc
));
4595 end Rewrite_Procedure_Call
;
4597 -------------------------
4598 -- Formal_Is_Used_Once --
4599 -------------------------
4601 function Formal_Is_Used_Once
(Formal
: Entity_Id
) return Boolean is
4602 Use_Counter
: Int
:= 0;
4604 function Count_Uses
(N
: Node_Id
) return Traverse_Result
;
4605 -- Traverse the tree and count the uses of the formal parameter.
4606 -- In this case, for optimization purposes, we do not need to
4607 -- continue the traversal once more than one use is encountered.
4613 function Count_Uses
(N
: Node_Id
) return Traverse_Result
is
4615 -- The original node is an identifier
4617 if Nkind
(N
) = N_Identifier
4618 and then Present
(Entity
(N
))
4620 -- Original node's entity points to the one in the copied body
4622 and then Nkind
(Entity
(N
)) = N_Identifier
4623 and then Present
(Entity
(Entity
(N
)))
4625 -- The entity of the copied node is the formal parameter
4627 and then Entity
(Entity
(N
)) = Formal
4629 Use_Counter
:= Use_Counter
+ 1;
4631 if Use_Counter
> 1 then
4633 -- Denote more than one use and abandon the traversal
4644 procedure Count_Formal_Uses
is new Traverse_Proc
(Count_Uses
);
4646 -- Start of processing for Formal_Is_Used_Once
4649 Count_Formal_Uses
(Orig_Bod
);
4650 return Use_Counter
= 1;
4651 end Formal_Is_Used_Once
;
4653 -- Start of processing for Expand_Inlined_Call
4656 -- Initializations for old/new semantics
4658 if not Debug_Flag_Dot_K
then
4659 Is_Unc
:= Is_Array_Type
(Etype
(Subp
))
4660 and then not Is_Constrained
(Etype
(Subp
));
4661 Is_Unc_Decl
:= False;
4663 Is_Unc
:= Returns_Unconstrained_Type
(Subp
)
4664 and then Optimization_Level
> 0;
4665 Is_Unc_Decl
:= Nkind
(Parent
(N
)) = N_Object_Declaration
4669 -- Check for an illegal attempt to inline a recursive procedure. If the
4670 -- subprogram has parameters this is detected when trying to supply a
4671 -- binding for parameters that already have one. For parameterless
4672 -- subprograms this must be done explicitly.
4674 if In_Open_Scopes
(Subp
) then
4675 Error_Msg_N
("call to recursive subprogram cannot be inlined??", N
);
4676 Set_Is_Inlined
(Subp
, False);
4679 -- Skip inlining if this is not a true inlining since the attribute
4680 -- Body_To_Inline is also set for renamings (see sinfo.ads)
4682 elsif Nkind
(Orig_Bod
) in N_Entity
then
4685 -- Skip inlining if the function returns an unconstrained type using
4686 -- an extended return statement since this part of the new inlining
4687 -- model which is not yet supported by the current implementation. ???
4691 Nkind
(First
(Statements
(Handled_Statement_Sequence
(Orig_Bod
))))
4692 = N_Extended_Return_Statement
4693 and then not Debug_Flag_Dot_K
4698 if Nkind
(Orig_Bod
) = N_Defining_Identifier
4699 or else Nkind
(Orig_Bod
) = N_Defining_Operator_Symbol
4701 -- Subprogram is renaming_as_body. Calls occurring after the renaming
4702 -- can be replaced with calls to the renamed entity directly, because
4703 -- the subprograms are subtype conformant. If the renamed subprogram
4704 -- is an inherited operation, we must redo the expansion because
4705 -- implicit conversions may be needed. Similarly, if the renamed
4706 -- entity is inlined, expand the call for further optimizations.
4708 Set_Name
(N
, New_Occurrence_Of
(Orig_Bod
, Loc
));
4710 if Present
(Alias
(Orig_Bod
)) or else Is_Inlined
(Orig_Bod
) then
4717 -- Register the call in the list of inlined calls
4719 if Inlined_Calls
= No_Elist
then
4720 Inlined_Calls
:= New_Elmt_List
;
4723 Append_Elmt
(N
, To
=> Inlined_Calls
);
4725 -- Use generic machinery to copy body of inlined subprogram, as if it
4726 -- were an instantiation, resetting source locations appropriately, so
4727 -- that nested inlined calls appear in the main unit.
4729 Save_Env
(Subp
, Empty
);
4730 Set_Copied_Sloc_For_Inlined_Body
(N
, Defining_Entity
(Orig_Bod
));
4734 if not Debug_Flag_Dot_K
then
4739 Bod
:= Copy_Generic_Node
(Orig_Bod
, Empty
, Instantiating
=> True);
4741 Make_Block_Statement
(Loc
,
4742 Declarations
=> Declarations
(Bod
),
4743 Handled_Statement_Sequence
=>
4744 Handled_Statement_Sequence
(Bod
));
4746 if No
(Declarations
(Bod
)) then
4747 Set_Declarations
(Blk
, New_List
);
4750 -- For the unconstrained case, capture the name of the local
4751 -- variable that holds the result. This must be the first
4752 -- declaration in the block, because its bounds cannot depend
4753 -- on local variables. Otherwise there is no way to declare the
4754 -- result outside of the block. Needless to say, in general the
4755 -- bounds will depend on the actuals in the call.
4757 -- If the context is an assignment statement, as is the case
4758 -- for the expansion of an extended return, the left-hand side
4759 -- provides bounds even if the return type is unconstrained.
4763 First_Decl
: Node_Id
;
4766 First_Decl
:= First
(Declarations
(Blk
));
4768 if Nkind
(First_Decl
) /= N_Object_Declaration
then
4772 if Nkind
(Parent
(N
)) /= N_Assignment_Statement
then
4773 Targ1
:= Defining_Identifier
(First_Decl
);
4775 Targ1
:= Name
(Parent
(N
));
4792 Copy_Generic_Node
(Orig_Bod
, Empty
, Instantiating
=> True);
4794 Make_Block_Statement
(Loc
,
4795 Declarations
=> Declarations
(Bod
),
4796 Handled_Statement_Sequence
=>
4797 Handled_Statement_Sequence
(Bod
));
4799 -- Inline a call to a function that returns an unconstrained type.
4800 -- The semantic analyzer checked that frontend-inlined functions
4801 -- returning unconstrained types have no declarations and have
4802 -- a single extended return statement. As part of its processing
4803 -- the function was split in two subprograms: a procedure P and
4804 -- a function F that has a block with a call to procedure P (see
4805 -- Split_Unconstrained_Function).
4811 (Statements
(Handled_Statement_Sequence
(Orig_Bod
))))
4812 = N_Block_Statement
);
4815 Blk_Stmt
: constant Node_Id
:=
4818 (Handled_Statement_Sequence
(Orig_Bod
)));
4819 First_Stmt
: constant Node_Id
:=
4822 (Handled_Statement_Sequence
(Blk_Stmt
)));
4823 Second_Stmt
: constant Node_Id
:= Next
(First_Stmt
);
4827 (Nkind
(First_Stmt
) = N_Procedure_Call_Statement
4828 and then Nkind
(Second_Stmt
) = N_Simple_Return_Statement
4829 and then No
(Next
(Second_Stmt
)));
4834 (Statements
(Handled_Statement_Sequence
(Orig_Bod
))),
4835 Empty
, Instantiating
=> True);
4838 -- Capture the name of the local variable that holds the
4839 -- result. This must be the first declaration in the block,
4840 -- because its bounds cannot depend on local variables.
4841 -- Otherwise there is no way to declare the result outside
4842 -- of the block. Needless to say, in general the bounds will
4843 -- depend on the actuals in the call.
4845 if Nkind
(Parent
(N
)) /= N_Assignment_Statement
then
4846 Targ1
:= Defining_Identifier
(First
(Declarations
(Blk
)));
4848 -- If the context is an assignment statement, as is the case
4849 -- for the expansion of an extended return, the left-hand
4850 -- side provides bounds even if the return type is
4854 Targ1
:= Name
(Parent
(N
));
4859 if No
(Declarations
(Bod
)) then
4860 Set_Declarations
(Blk
, New_List
);
4865 -- If this is a derived function, establish the proper return type
4867 if Present
(Orig_Subp
) and then Orig_Subp
/= Subp
then
4868 Ret_Type
:= Etype
(Orig_Subp
);
4870 Ret_Type
:= Etype
(Subp
);
4873 -- Create temporaries for the actuals that are expressions, or that are
4874 -- scalars and require copying to preserve semantics.
4876 F
:= First_Formal
(Subp
);
4877 A
:= First_Actual
(N
);
4878 while Present
(F
) loop
4879 if Present
(Renamed_Object
(F
)) then
4880 Error_Msg_N
("cannot inline call to recursive subprogram", N
);
4884 -- Reset Last_Assignment for any parameters of mode out or in out, to
4885 -- prevent spurious warnings about overwriting for assignments to the
4886 -- formal in the inlined code.
4888 if Is_Entity_Name
(A
) and then Ekind
(F
) /= E_In_Parameter
then
4889 Set_Last_Assignment
(Entity
(A
), Empty
);
4892 -- If the argument may be a controlling argument in a call within
4893 -- the inlined body, we must preserve its classwide nature to insure
4894 -- that dynamic dispatching take place subsequently. If the formal
4895 -- has a constraint it must be preserved to retain the semantics of
4898 if Is_Class_Wide_Type
(Etype
(F
))
4899 or else (Is_Access_Type
(Etype
(F
))
4900 and then Is_Class_Wide_Type
(Designated_Type
(Etype
(F
))))
4902 Temp_Typ
:= Etype
(F
);
4904 elsif Base_Type
(Etype
(F
)) = Base_Type
(Etype
(A
))
4905 and then Etype
(F
) /= Base_Type
(Etype
(F
))
4907 Temp_Typ
:= Etype
(F
);
4909 Temp_Typ
:= Etype
(A
);
4912 -- If the actual is a simple name or a literal, no need to
4913 -- create a temporary, object can be used directly.
4915 -- If the actual is a literal and the formal has its address taken,
4916 -- we cannot pass the literal itself as an argument, so its value
4917 -- must be captured in a temporary.
4919 if (Is_Entity_Name
(A
)
4921 (not Is_Scalar_Type
(Etype
(A
))
4922 or else Ekind
(Entity
(A
)) = E_Enumeration_Literal
))
4924 -- When the actual is an identifier and the corresponding formal is
4925 -- used only once in the original body, the formal can be substituted
4926 -- directly with the actual parameter.
4928 or else (Nkind
(A
) = N_Identifier
4929 and then Formal_Is_Used_Once
(F
))
4932 (Nkind_In
(A
, N_Real_Literal
,
4934 N_Character_Literal
)
4935 and then not Address_Taken
(F
))
4937 if Etype
(F
) /= Etype
(A
) then
4939 (F
, Unchecked_Convert_To
(Etype
(F
), Relocate_Node
(A
)));
4941 Set_Renamed_Object
(F
, A
);
4945 Temp
:= Make_Temporary
(Loc
, 'C');
4947 -- If the actual for an in/in-out parameter is a view conversion,
4948 -- make it into an unchecked conversion, given that an untagged
4949 -- type conversion is not a proper object for a renaming.
4951 -- In-out conversions that involve real conversions have already
4952 -- been transformed in Expand_Actuals.
4954 if Nkind
(A
) = N_Type_Conversion
4955 and then Ekind
(F
) /= E_In_Parameter
4958 Make_Unchecked_Type_Conversion
(Loc
,
4959 Subtype_Mark
=> New_Occurrence_Of
(Etype
(F
), Loc
),
4960 Expression
=> Relocate_Node
(Expression
(A
)));
4962 elsif Etype
(F
) /= Etype
(A
) then
4963 New_A
:= Unchecked_Convert_To
(Etype
(F
), Relocate_Node
(A
));
4964 Temp_Typ
:= Etype
(F
);
4967 New_A
:= Relocate_Node
(A
);
4970 Set_Sloc
(New_A
, Sloc
(N
));
4972 -- If the actual has a by-reference type, it cannot be copied,
4973 -- so its value is captured in a renaming declaration. Otherwise
4974 -- declare a local constant initialized with the actual.
4976 -- We also use a renaming declaration for expressions of an array
4977 -- type that is not bit-packed, both for efficiency reasons and to
4978 -- respect the semantics of the call: in most cases the original
4979 -- call will pass the parameter by reference, and thus the inlined
4980 -- code will have the same semantics.
4982 if Ekind
(F
) = E_In_Parameter
4983 and then not Is_By_Reference_Type
(Etype
(A
))
4985 (not Is_Array_Type
(Etype
(A
))
4986 or else not Is_Object_Reference
(A
)
4987 or else Is_Bit_Packed_Array
(Etype
(A
)))
4990 Make_Object_Declaration
(Loc
,
4991 Defining_Identifier
=> Temp
,
4992 Constant_Present
=> True,
4993 Object_Definition
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
4994 Expression
=> New_A
);
4997 Make_Object_Renaming_Declaration
(Loc
,
4998 Defining_Identifier
=> Temp
,
4999 Subtype_Mark
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
5003 Append
(Decl
, Decls
);
5004 Set_Renamed_Object
(F
, Temp
);
5011 -- Establish target of function call. If context is not assignment or
5012 -- declaration, create a temporary as a target. The declaration for the
5013 -- temporary may be subsequently optimized away if the body is a single
5014 -- expression, or if the left-hand side of the assignment is simple
5015 -- enough, i.e. an entity or an explicit dereference of one.
5017 if Ekind
(Subp
) = E_Function
then
5018 if Nkind
(Parent
(N
)) = N_Assignment_Statement
5019 and then Is_Entity_Name
(Name
(Parent
(N
)))
5021 Targ
:= Name
(Parent
(N
));
5023 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
5024 and then Nkind
(Name
(Parent
(N
))) = N_Explicit_Dereference
5025 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))
5027 Targ
:= Name
(Parent
(N
));
5029 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
5030 and then Nkind
(Name
(Parent
(N
))) = N_Selected_Component
5031 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))
5033 Targ
:= New_Copy_Tree
(Name
(Parent
(N
)));
5035 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
5036 and then Is_Limited_Type
(Etype
(Subp
))
5038 Targ
:= Defining_Identifier
(Parent
(N
));
5040 -- New semantics: In an object declaration avoid an extra copy
5041 -- of the result of a call to an inlined function that returns
5042 -- an unconstrained type
5044 elsif Debug_Flag_Dot_K
5045 and then Nkind
(Parent
(N
)) = N_Object_Declaration
5048 Targ
:= Defining_Identifier
(Parent
(N
));
5051 -- Replace call with temporary and create its declaration
5053 Temp
:= Make_Temporary
(Loc
, 'C');
5054 Set_Is_Internal
(Temp
);
5056 -- For the unconstrained case, the generated temporary has the
5057 -- same constrained declaration as the result variable. It may
5058 -- eventually be possible to remove that temporary and use the
5059 -- result variable directly.
5062 and then Nkind
(Parent
(N
)) /= N_Assignment_Statement
5065 Make_Object_Declaration
(Loc
,
5066 Defining_Identifier
=> Temp
,
5067 Object_Definition
=>
5068 New_Copy_Tree
(Object_Definition
(Parent
(Targ1
))));
5070 Replace_Formals
(Decl
);
5074 Make_Object_Declaration
(Loc
,
5075 Defining_Identifier
=> Temp
,
5076 Object_Definition
=> New_Occurrence_Of
(Ret_Type
, Loc
));
5078 Set_Etype
(Temp
, Ret_Type
);
5081 Set_No_Initialization
(Decl
);
5082 Append
(Decl
, Decls
);
5083 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
5088 Insert_Actions
(N
, Decls
);
5092 -- Special management for inlining a call to a function that returns
5093 -- an unconstrained type and initializes an object declaration: we
5094 -- avoid generating undesired extra calls and goto statements.
5097 -- function Func (...) return ...
5100 -- Result : String (1 .. 4);
5102 -- Proc (Result, ...);
5107 -- Result : String := Func (...);
5109 -- Replace this object declaration by:
5111 -- Result : String (1 .. 4);
5112 -- Proc (Result, ...);
5114 Remove_Homonym
(Targ
);
5117 Make_Object_Declaration
5119 Defining_Identifier
=> Targ
,
5120 Object_Definition
=>
5121 New_Copy_Tree
(Object_Definition
(Parent
(Targ1
))));
5122 Replace_Formals
(Decl
);
5123 Rewrite
(Parent
(N
), Decl
);
5124 Analyze
(Parent
(N
));
5126 -- Avoid spurious warnings since we know that this declaration is
5127 -- referenced by the procedure call.
5129 Set_Never_Set_In_Source
(Targ
, False);
5131 -- Remove the local declaration of the extended return stmt from the
5134 Remove
(Parent
(Targ1
));
5136 -- Update the reference to the result (since we have rewriten the
5137 -- object declaration)
5140 Blk_Call_Stmt
: Node_Id
;
5143 -- Capture the call to the procedure
5146 First
(Statements
(Handled_Statement_Sequence
(Blk
)));
5148 (Nkind
(Blk_Call_Stmt
) = N_Procedure_Call_Statement
);
5150 Remove
(First
(Parameter_Associations
(Blk_Call_Stmt
)));
5151 Prepend_To
(Parameter_Associations
(Blk_Call_Stmt
),
5152 New_Occurrence_Of
(Targ
, Loc
));
5155 -- Remove the return statement
5158 (Nkind
(Last
(Statements
(Handled_Statement_Sequence
(Blk
)))) =
5159 N_Simple_Return_Statement
);
5161 Remove
(Last
(Statements
(Handled_Statement_Sequence
(Blk
))));
5164 -- Traverse the tree and replace formals with actuals or their thunks.
5165 -- Attach block to tree before analysis and rewriting.
5167 Replace_Formals
(Blk
);
5168 Set_Parent
(Blk
, N
);
5170 if not Comes_From_Source
(Subp
) or else Is_Predef
then
5176 -- No action needed since return statement has been already removed
5180 elsif Present
(Exit_Lab
) then
5182 -- If the body was a single expression, the single return statement
5183 -- and the corresponding label are useless.
5187 Nkind
(Last
(Statements
(Handled_Statement_Sequence
(Blk
)))) =
5190 Remove
(Last
(Statements
(Handled_Statement_Sequence
(Blk
))));
5192 Append
(Lab_Decl
, (Declarations
(Blk
)));
5193 Append
(Exit_Lab
, Statements
(Handled_Statement_Sequence
(Blk
)));
5197 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
5198 -- on conflicting private views that Gigi would ignore. If this is a
5199 -- predefined unit, analyze with checks off, as is done in the non-
5200 -- inlined run-time units.
5203 I_Flag
: constant Boolean := In_Inlined_Body
;
5206 In_Inlined_Body
:= True;
5210 Style
: constant Boolean := Style_Check
;
5213 Style_Check
:= False;
5215 -- Search for dispatching calls that use the Object.Operation
5216 -- notation using an Object that is a parameter of the inlined
5217 -- function. We reset the decoration of Operation to force
5218 -- the reanalysis of the inlined dispatching call because
5219 -- the actual object has been inlined.
5221 Reset_Dispatching_Calls
(Blk
);
5223 Analyze
(Blk
, Suppress
=> All_Checks
);
5224 Style_Check
:= Style
;
5231 In_Inlined_Body
:= I_Flag
;
5234 if Ekind
(Subp
) = E_Procedure
then
5235 Rewrite_Procedure_Call
(N
, Blk
);
5238 Rewrite_Function_Call
(N
, Blk
);
5243 -- For the unconstrained case, the replacement of the call has been
5244 -- made prior to the complete analysis of the generated declarations.
5245 -- Propagate the proper type now.
5248 if Nkind
(N
) = N_Identifier
then
5249 Set_Etype
(N
, Etype
(Entity
(N
)));
5251 Set_Etype
(N
, Etype
(Targ1
));
5258 -- Cleanup mapping between formals and actuals for other expansions
5260 F
:= First_Formal
(Subp
);
5261 while Present
(F
) loop
5262 Set_Renamed_Object
(F
, Empty
);
5265 end Expand_Inlined_Call
;
5267 ----------------------------------------
5268 -- Expand_N_Extended_Return_Statement --
5269 ----------------------------------------
5271 -- If there is a Handled_Statement_Sequence, we rewrite this:
5273 -- return Result : T := <expression> do
5274 -- <handled_seq_of_stms>
5280 -- Result : T := <expression>;
5282 -- <handled_seq_of_stms>
5286 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
5288 -- return Result : T := <expression>;
5292 -- return <expression>;
5294 -- unless it's build-in-place or there's no <expression>, in which case
5298 -- Result : T := <expression>;
5303 -- Note that this case could have been written by the user as an extended
5304 -- return statement, or could have been transformed to this from a simple
5305 -- return statement.
5307 -- That is, we need to have a reified return object if there are statements
5308 -- (which might refer to it) or if we're doing build-in-place (so we can
5309 -- set its address to the final resting place or if there is no expression
5310 -- (in which case default initial values might need to be set).
5312 procedure Expand_N_Extended_Return_Statement
(N
: Node_Id
) is
5313 Loc
: constant Source_Ptr
:= Sloc
(N
);
5315 Par_Func
: constant Entity_Id
:=
5316 Return_Applies_To
(Return_Statement_Entity
(N
));
5317 Result_Subt
: constant Entity_Id
:= Etype
(Par_Func
);
5318 Ret_Obj_Id
: constant Entity_Id
:=
5319 First_Entity
(Return_Statement_Entity
(N
));
5320 Ret_Obj_Decl
: constant Node_Id
:= Parent
(Ret_Obj_Id
);
5322 Is_Build_In_Place
: constant Boolean :=
5323 Is_Build_In_Place_Function
(Par_Func
);
5328 Return_Stmt
: Node_Id
;
5331 function Build_Heap_Allocator
5332 (Temp_Id
: Entity_Id
;
5333 Temp_Typ
: Entity_Id
;
5334 Func_Id
: Entity_Id
;
5335 Ret_Typ
: Entity_Id
;
5336 Alloc_Expr
: Node_Id
) return Node_Id
;
5337 -- Create the statements necessary to allocate a return object on the
5338 -- caller's master. The master is available through implicit parameter
5339 -- BIPfinalizationmaster.
5341 -- if BIPfinalizationmaster /= null then
5343 -- type Ptr_Typ is access Ret_Typ;
5344 -- for Ptr_Typ'Storage_Pool use
5345 -- Base_Pool (BIPfinalizationmaster.all).all;
5349 -- procedure Allocate (...) is
5351 -- System.Storage_Pools.Subpools.Allocate_Any (...);
5354 -- Local := <Alloc_Expr>;
5355 -- Temp_Id := Temp_Typ (Local);
5359 -- Temp_Id is the temporary which is used to reference the internally
5360 -- created object in all allocation forms. Temp_Typ is the type of the
5361 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
5362 -- type of Func_Id. Alloc_Expr is the actual allocator.
5364 function Move_Activation_Chain
return Node_Id
;
5365 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
5367 -- From current activation chain
5368 -- To activation chain passed in by the caller
5369 -- New_Master master passed in by the caller
5371 --------------------------
5372 -- Build_Heap_Allocator --
5373 --------------------------
5375 function Build_Heap_Allocator
5376 (Temp_Id
: Entity_Id
;
5377 Temp_Typ
: Entity_Id
;
5378 Func_Id
: Entity_Id
;
5379 Ret_Typ
: Entity_Id
;
5380 Alloc_Expr
: Node_Id
) return Node_Id
5383 pragma Assert
(Is_Build_In_Place_Function
(Func_Id
));
5385 -- Processing for build-in-place object allocation. This is disabled
5386 -- on .NET/JVM because the targets do not support pools.
5388 if VM_Target
= No_VM
5389 and then Needs_Finalization
(Ret_Typ
)
5392 Decls
: constant List_Id
:= New_List
;
5393 Fin_Mas_Id
: constant Entity_Id
:=
5394 Build_In_Place_Formal
5395 (Func_Id
, BIP_Finalization_Master
);
5396 Stmts
: constant List_Id
:= New_List
;
5397 Desig_Typ
: Entity_Id
;
5398 Local_Id
: Entity_Id
;
5399 Pool_Id
: Entity_Id
;
5400 Ptr_Typ
: Entity_Id
;
5404 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
5406 Pool_Id
:= Make_Temporary
(Loc
, 'P');
5409 Make_Object_Renaming_Declaration
(Loc
,
5410 Defining_Identifier
=> Pool_Id
,
5412 New_Occurrence_Of
(RTE
(RE_Root_Storage_Pool
), Loc
),
5414 Make_Explicit_Dereference
(Loc
,
5416 Make_Function_Call
(Loc
,
5418 New_Occurrence_Of
(RTE
(RE_Base_Pool
), Loc
),
5419 Parameter_Associations
=> New_List
(
5420 Make_Explicit_Dereference
(Loc
,
5422 New_Occurrence_Of
(Fin_Mas_Id
, Loc
)))))));
5424 -- Create an access type which uses the storage pool of the
5425 -- caller's master. This additional type is necessary because
5426 -- the finalization master cannot be associated with the type
5427 -- of the temporary. Otherwise the secondary stack allocation
5430 Desig_Typ
:= Ret_Typ
;
5432 -- Ensure that the build-in-place machinery uses a fat pointer
5433 -- when allocating an unconstrained array on the heap. In this
5434 -- case the result object type is a constrained array type even
5435 -- though the function type is unconstrained.
5437 if Ekind
(Desig_Typ
) = E_Array_Subtype
then
5438 Desig_Typ
:= Base_Type
(Desig_Typ
);
5442 -- type Ptr_Typ is access Desig_Typ;
5444 Ptr_Typ
:= Make_Temporary
(Loc
, 'P');
5447 Make_Full_Type_Declaration
(Loc
,
5448 Defining_Identifier
=> Ptr_Typ
,
5450 Make_Access_To_Object_Definition
(Loc
,
5451 Subtype_Indication
=>
5452 New_Occurrence_Of
(Desig_Typ
, Loc
))));
5454 -- Perform minor decoration in order to set the master and the
5455 -- storage pool attributes.
5457 Set_Ekind
(Ptr_Typ
, E_Access_Type
);
5458 Set_Finalization_Master
(Ptr_Typ
, Fin_Mas_Id
);
5459 Set_Associated_Storage_Pool
(Ptr_Typ
, Pool_Id
);
5461 -- Create the temporary, generate:
5462 -- Local_Id : Ptr_Typ;
5464 Local_Id
:= Make_Temporary
(Loc
, 'T');
5467 Make_Object_Declaration
(Loc
,
5468 Defining_Identifier
=> Local_Id
,
5469 Object_Definition
=>
5470 New_Occurrence_Of
(Ptr_Typ
, Loc
)));
5472 -- Allocate the object, generate:
5473 -- Local_Id := <Alloc_Expr>;
5476 Make_Assignment_Statement
(Loc
,
5477 Name
=> New_Occurrence_Of
(Local_Id
, Loc
),
5478 Expression
=> Alloc_Expr
));
5481 -- Temp_Id := Temp_Typ (Local_Id);
5484 Make_Assignment_Statement
(Loc
,
5485 Name
=> New_Occurrence_Of
(Temp_Id
, Loc
),
5487 Unchecked_Convert_To
(Temp_Typ
,
5488 New_Occurrence_Of
(Local_Id
, Loc
))));
5490 -- Wrap the allocation in a block. This is further conditioned
5491 -- by checking the caller finalization master at runtime. A
5492 -- null value indicates a non-existent master, most likely due
5493 -- to a Finalize_Storage_Only allocation.
5496 -- if BIPfinalizationmaster /= null then
5505 Make_If_Statement
(Loc
,
5508 Left_Opnd
=> New_Occurrence_Of
(Fin_Mas_Id
, Loc
),
5509 Right_Opnd
=> Make_Null
(Loc
)),
5511 Then_Statements
=> New_List
(
5512 Make_Block_Statement
(Loc
,
5513 Declarations
=> Decls
,
5514 Handled_Statement_Sequence
=>
5515 Make_Handled_Sequence_Of_Statements
(Loc
,
5516 Statements
=> Stmts
))));
5519 -- For all other cases, generate:
5520 -- Temp_Id := <Alloc_Expr>;
5524 Make_Assignment_Statement
(Loc
,
5525 Name
=> New_Occurrence_Of
(Temp_Id
, Loc
),
5526 Expression
=> Alloc_Expr
);
5528 end Build_Heap_Allocator
;
5530 ---------------------------
5531 -- Move_Activation_Chain --
5532 ---------------------------
5534 function Move_Activation_Chain
return Node_Id
is
5537 Make_Procedure_Call_Statement
(Loc
,
5539 New_Occurrence_Of
(RTE
(RE_Move_Activation_Chain
), Loc
),
5541 Parameter_Associations
=> New_List
(
5545 Make_Attribute_Reference
(Loc
,
5546 Prefix
=> Make_Identifier
(Loc
, Name_uChain
),
5547 Attribute_Name
=> Name_Unrestricted_Access
),
5549 -- Destination chain
5552 (Build_In_Place_Formal
(Par_Func
, BIP_Activation_Chain
), Loc
),
5557 (Build_In_Place_Formal
(Par_Func
, BIP_Task_Master
), Loc
)));
5558 end Move_Activation_Chain
;
5560 -- Start of processing for Expand_N_Extended_Return_Statement
5563 -- Given that functionality of interface thunks is simple (just displace
5564 -- the pointer to the object) they are always handled by means of
5565 -- simple return statements.
5567 pragma Assert
(not Is_Thunk
(Current_Scope
));
5569 if Nkind
(Ret_Obj_Decl
) = N_Object_Declaration
then
5570 Exp
:= Expression
(Ret_Obj_Decl
);
5575 HSS
:= Handled_Statement_Sequence
(N
);
5577 -- If the returned object needs finalization actions, the function must
5578 -- perform the appropriate cleanup should it fail to return. The state
5579 -- of the function itself is tracked through a flag which is coupled
5580 -- with the scope finalizer. There is one flag per each return object
5581 -- in case of multiple returns.
5583 if Is_Build_In_Place
5584 and then Needs_Finalization
(Etype
(Ret_Obj_Id
))
5587 Flag_Decl
: Node_Id
;
5588 Flag_Id
: Entity_Id
;
5592 -- Recover the function body
5594 Func_Bod
:= Unit_Declaration_Node
(Par_Func
);
5596 if Nkind
(Func_Bod
) = N_Subprogram_Declaration
then
5597 Func_Bod
:= Parent
(Parent
(Corresponding_Body
(Func_Bod
)));
5600 -- Create a flag to track the function state
5602 Flag_Id
:= Make_Temporary
(Loc
, 'F');
5603 Set_Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
, Flag_Id
);
5605 -- Insert the flag at the beginning of the function declarations,
5607 -- Fnn : Boolean := False;
5610 Make_Object_Declaration
(Loc
,
5611 Defining_Identifier
=> Flag_Id
,
5612 Object_Definition
=>
5613 New_Occurrence_Of
(Standard_Boolean
, Loc
),
5615 New_Occurrence_Of
(Standard_False
, Loc
));
5617 Prepend_To
(Declarations
(Func_Bod
), Flag_Decl
);
5618 Analyze
(Flag_Decl
);
5622 -- Build a simple_return_statement that returns the return object when
5623 -- there is a statement sequence, or no expression, or the result will
5624 -- be built in place. Note however that we currently do this for all
5625 -- composite cases, even though nonlimited composite results are not yet
5626 -- built in place (though we plan to do so eventually).
5629 or else Is_Composite_Type
(Result_Subt
)
5635 -- If the extended return has a handled statement sequence, then wrap
5636 -- it in a block and use the block as the first statement.
5640 Make_Block_Statement
(Loc
,
5641 Declarations
=> New_List
,
5642 Handled_Statement_Sequence
=> HSS
));
5645 -- If the result type contains tasks, we call Move_Activation_Chain.
5646 -- Later, the cleanup code will call Complete_Master, which will
5647 -- terminate any unactivated tasks belonging to the return statement
5648 -- master. But Move_Activation_Chain updates their master to be that
5649 -- of the caller, so they will not be terminated unless the return
5650 -- statement completes unsuccessfully due to exception, abort, goto,
5651 -- or exit. As a formality, we test whether the function requires the
5652 -- result to be built in place, though that's necessarily true for
5653 -- the case of result types with task parts.
5655 if Is_Build_In_Place
5656 and then Has_Task
(Result_Subt
)
5658 -- The return expression is an aggregate for a complex type which
5659 -- contains tasks. This particular case is left unexpanded since
5660 -- the regular expansion would insert all temporaries and
5661 -- initialization code in the wrong block.
5663 if Nkind
(Exp
) = N_Aggregate
then
5664 Expand_N_Aggregate
(Exp
);
5667 -- Do not move the activation chain if the return object does not
5670 if Has_Task
(Etype
(Ret_Obj_Id
)) then
5671 Append_To
(Stmts
, Move_Activation_Chain
);
5675 -- Update the state of the function right before the object is
5678 if Is_Build_In_Place
5679 and then Needs_Finalization
(Etype
(Ret_Obj_Id
))
5682 Flag_Id
: constant Entity_Id
:=
5683 Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
);
5690 Make_Assignment_Statement
(Loc
,
5691 Name
=> New_Occurrence_Of
(Flag_Id
, Loc
),
5692 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)));
5696 -- Build a simple_return_statement that returns the return object
5699 Make_Simple_Return_Statement
(Loc
,
5700 Expression
=> New_Occurrence_Of
(Ret_Obj_Id
, Loc
));
5701 Append_To
(Stmts
, Return_Stmt
);
5703 HSS
:= Make_Handled_Sequence_Of_Statements
(Loc
, Stmts
);
5706 -- Case where we build a return statement block
5708 if Present
(HSS
) then
5710 Make_Block_Statement
(Loc
,
5711 Declarations
=> Return_Object_Declarations
(N
),
5712 Handled_Statement_Sequence
=> HSS
);
5714 -- We set the entity of the new block statement to be that of the
5715 -- return statement. This is necessary so that various fields, such
5716 -- as Finalization_Chain_Entity carry over from the return statement
5717 -- to the block. Note that this block is unusual, in that its entity
5718 -- is an E_Return_Statement rather than an E_Block.
5721 (Result
, New_Occurrence_Of
(Return_Statement_Entity
(N
), Loc
));
5723 -- If the object decl was already rewritten as a renaming, then we
5724 -- don't want to do the object allocation and transformation of of
5725 -- the return object declaration to a renaming. This case occurs
5726 -- when the return object is initialized by a call to another
5727 -- build-in-place function, and that function is responsible for
5728 -- the allocation of the return object.
5730 if Is_Build_In_Place
5731 and then Nkind
(Ret_Obj_Decl
) = N_Object_Renaming_Declaration
5734 (Nkind
(Original_Node
(Ret_Obj_Decl
)) = N_Object_Declaration
5735 and then Is_Build_In_Place_Function_Call
5736 (Expression
(Original_Node
(Ret_Obj_Decl
))));
5738 -- Return the build-in-place result by reference
5740 Set_By_Ref
(Return_Stmt
);
5742 elsif Is_Build_In_Place
then
5744 -- Locate the implicit access parameter associated with the
5745 -- caller-supplied return object and convert the return
5746 -- statement's return object declaration to a renaming of a
5747 -- dereference of the access parameter. If the return object's
5748 -- declaration includes an expression that has not already been
5749 -- expanded as separate assignments, then add an assignment
5750 -- statement to ensure the return object gets initialized.
5753 -- Result : T [:= <expression>];
5760 -- Result : T renames FuncRA.all;
5761 -- [Result := <expression;]
5766 Return_Obj_Id
: constant Entity_Id
:=
5767 Defining_Identifier
(Ret_Obj_Decl
);
5768 Return_Obj_Typ
: constant Entity_Id
:= Etype
(Return_Obj_Id
);
5769 Return_Obj_Expr
: constant Node_Id
:=
5770 Expression
(Ret_Obj_Decl
);
5771 Constr_Result
: constant Boolean :=
5772 Is_Constrained
(Result_Subt
);
5773 Obj_Alloc_Formal
: Entity_Id
;
5774 Object_Access
: Entity_Id
;
5775 Obj_Acc_Deref
: Node_Id
;
5776 Init_Assignment
: Node_Id
:= Empty
;
5779 -- Build-in-place results must be returned by reference
5781 Set_By_Ref
(Return_Stmt
);
5783 -- Retrieve the implicit access parameter passed by the caller
5786 Build_In_Place_Formal
(Par_Func
, BIP_Object_Access
);
5788 -- If the return object's declaration includes an expression
5789 -- and the declaration isn't marked as No_Initialization, then
5790 -- we need to generate an assignment to the object and insert
5791 -- it after the declaration before rewriting it as a renaming
5792 -- (otherwise we'll lose the initialization). The case where
5793 -- the result type is an interface (or class-wide interface)
5794 -- is also excluded because the context of the function call
5795 -- must be unconstrained, so the initialization will always
5796 -- be done as part of an allocator evaluation (storage pool
5797 -- or secondary stack), never to a constrained target object
5798 -- passed in by the caller. Besides the assignment being
5799 -- unneeded in this case, it avoids problems with trying to
5800 -- generate a dispatching assignment when the return expression
5801 -- is a nonlimited descendant of a limited interface (the
5802 -- interface has no assignment operation).
5804 if Present
(Return_Obj_Expr
)
5805 and then not No_Initialization
(Ret_Obj_Decl
)
5806 and then not Is_Interface
(Return_Obj_Typ
)
5809 Make_Assignment_Statement
(Loc
,
5810 Name
=> New_Occurrence_Of
(Return_Obj_Id
, Loc
),
5811 Expression
=> Relocate_Node
(Return_Obj_Expr
));
5813 Set_Etype
(Name
(Init_Assignment
), Etype
(Return_Obj_Id
));
5814 Set_Assignment_OK
(Name
(Init_Assignment
));
5815 Set_No_Ctrl_Actions
(Init_Assignment
);
5817 Set_Parent
(Name
(Init_Assignment
), Init_Assignment
);
5818 Set_Parent
(Expression
(Init_Assignment
), Init_Assignment
);
5820 Set_Expression
(Ret_Obj_Decl
, Empty
);
5822 if Is_Class_Wide_Type
(Etype
(Return_Obj_Id
))
5823 and then not Is_Class_Wide_Type
5824 (Etype
(Expression
(Init_Assignment
)))
5826 Rewrite
(Expression
(Init_Assignment
),
5827 Make_Type_Conversion
(Loc
,
5829 New_Occurrence_Of
(Etype
(Return_Obj_Id
), Loc
),
5831 Relocate_Node
(Expression
(Init_Assignment
))));
5834 -- In the case of functions where the calling context can
5835 -- determine the form of allocation needed, initialization
5836 -- is done with each part of the if statement that handles
5837 -- the different forms of allocation (this is true for
5838 -- unconstrained and tagged result subtypes).
5841 and then not Is_Tagged_Type
(Underlying_Type
(Result_Subt
))
5843 Insert_After
(Ret_Obj_Decl
, Init_Assignment
);
5847 -- When the function's subtype is unconstrained, a run-time
5848 -- test is needed to determine the form of allocation to use
5849 -- for the return object. The function has an implicit formal
5850 -- parameter indicating this. If the BIP_Alloc_Form formal has
5851 -- the value one, then the caller has passed access to an
5852 -- existing object for use as the return object. If the value
5853 -- is two, then the return object must be allocated on the
5854 -- secondary stack. Otherwise, the object must be allocated in
5855 -- a storage pool (currently only supported for the global
5856 -- heap, user-defined storage pools TBD ???). We generate an
5857 -- if statement to test the implicit allocation formal and
5858 -- initialize a local access value appropriately, creating
5859 -- allocators in the secondary stack and global heap cases.
5860 -- The special formal also exists and must be tested when the
5861 -- function has a tagged result, even when the result subtype
5862 -- is constrained, because in general such functions can be
5863 -- called in dispatching contexts and must be handled similarly
5864 -- to functions with a class-wide result.
5866 if not Constr_Result
5867 or else Is_Tagged_Type
(Underlying_Type
(Result_Subt
))
5870 Build_In_Place_Formal
(Par_Func
, BIP_Alloc_Form
);
5873 Pool_Id
: constant Entity_Id
:=
5874 Make_Temporary
(Loc
, 'P');
5875 Alloc_Obj_Id
: Entity_Id
;
5876 Alloc_Obj_Decl
: Node_Id
;
5877 Alloc_If_Stmt
: Node_Id
;
5878 Heap_Allocator
: Node_Id
;
5879 Pool_Decl
: Node_Id
;
5880 Pool_Allocator
: Node_Id
;
5881 Ptr_Type_Decl
: Node_Id
;
5882 Ref_Type
: Entity_Id
;
5883 SS_Allocator
: Node_Id
;
5886 -- Reuse the itype created for the function's implicit
5887 -- access formal. This avoids the need to create a new
5888 -- access type here, plus it allows assigning the access
5889 -- formal directly without applying a conversion.
5891 -- Ref_Type := Etype (Object_Access);
5893 -- Create an access type designating the function's
5896 Ref_Type
:= Make_Temporary
(Loc
, 'A');
5899 Make_Full_Type_Declaration
(Loc
,
5900 Defining_Identifier
=> Ref_Type
,
5902 Make_Access_To_Object_Definition
(Loc
,
5903 All_Present
=> True,
5904 Subtype_Indication
=>
5905 New_Occurrence_Of
(Return_Obj_Typ
, Loc
)));
5907 Insert_Before
(Ret_Obj_Decl
, Ptr_Type_Decl
);
5909 -- Create an access object that will be initialized to an
5910 -- access value denoting the return object, either coming
5911 -- from an implicit access value passed in by the caller
5912 -- or from the result of an allocator.
5914 Alloc_Obj_Id
:= Make_Temporary
(Loc
, 'R');
5915 Set_Etype
(Alloc_Obj_Id
, Ref_Type
);
5918 Make_Object_Declaration
(Loc
,
5919 Defining_Identifier
=> Alloc_Obj_Id
,
5920 Object_Definition
=>
5921 New_Occurrence_Of
(Ref_Type
, Loc
));
5923 Insert_Before
(Ret_Obj_Decl
, Alloc_Obj_Decl
);
5925 -- Create allocators for both the secondary stack and
5926 -- global heap. If there's an initialization expression,
5927 -- then create these as initialized allocators.
5929 if Present
(Return_Obj_Expr
)
5930 and then not No_Initialization
(Ret_Obj_Decl
)
5932 -- Always use the type of the expression for the
5933 -- qualified expression, rather than the result type.
5934 -- In general we cannot always use the result type
5935 -- for the allocator, because the expression might be
5936 -- of a specific type, such as in the case of an
5937 -- aggregate or even a nonlimited object when the
5938 -- result type is a limited class-wide interface type.
5941 Make_Allocator
(Loc
,
5943 Make_Qualified_Expression
(Loc
,
5946 (Etype
(Return_Obj_Expr
), Loc
),
5948 New_Copy_Tree
(Return_Obj_Expr
)));
5951 -- If the function returns a class-wide type we cannot
5952 -- use the return type for the allocator. Instead we
5953 -- use the type of the expression, which must be an
5954 -- aggregate of a definite type.
5956 if Is_Class_Wide_Type
(Return_Obj_Typ
) then
5958 Make_Allocator
(Loc
,
5961 (Etype
(Return_Obj_Expr
), Loc
));
5964 Make_Allocator
(Loc
,
5966 New_Occurrence_Of
(Return_Obj_Typ
, Loc
));
5969 -- If the object requires default initialization then
5970 -- that will happen later following the elaboration of
5971 -- the object renaming. If we don't turn it off here
5972 -- then the object will be default initialized twice.
5974 Set_No_Initialization
(Heap_Allocator
);
5977 -- The Pool_Allocator is just like the Heap_Allocator,
5978 -- except we set Storage_Pool and Procedure_To_Call so
5979 -- it will use the user-defined storage pool.
5981 Pool_Allocator
:= New_Copy_Tree
(Heap_Allocator
);
5983 -- Do not generate the renaming of the build-in-place
5984 -- pool parameter on .NET/JVM/ZFP because the parameter
5985 -- is not created in the first place.
5987 if VM_Target
= No_VM
5988 and then RTE_Available
(RE_Root_Storage_Pool_Ptr
)
5991 Make_Object_Renaming_Declaration
(Loc
,
5992 Defining_Identifier
=> Pool_Id
,
5995 (RTE
(RE_Root_Storage_Pool
), Loc
),
5997 Make_Explicit_Dereference
(Loc
,
5999 (Build_In_Place_Formal
6000 (Par_Func
, BIP_Storage_Pool
), Loc
)));
6001 Set_Storage_Pool
(Pool_Allocator
, Pool_Id
);
6002 Set_Procedure_To_Call
6003 (Pool_Allocator
, RTE
(RE_Allocate_Any
));
6005 Pool_Decl
:= Make_Null_Statement
(Loc
);
6008 -- If the No_Allocators restriction is active, then only
6009 -- an allocator for secondary stack allocation is needed.
6010 -- It's OK for such allocators to have Comes_From_Source
6011 -- set to False, because gigi knows not to flag them as
6012 -- being a violation of No_Implicit_Heap_Allocations.
6014 if Restriction_Active
(No_Allocators
) then
6015 SS_Allocator
:= Heap_Allocator
;
6016 Heap_Allocator
:= Make_Null
(Loc
);
6017 Pool_Allocator
:= Make_Null
(Loc
);
6019 -- Otherwise the heap and pool allocators may be needed,
6020 -- so we make another allocator for secondary stack
6024 SS_Allocator
:= New_Copy_Tree
(Heap_Allocator
);
6026 -- The heap and pool allocators are marked as
6027 -- Comes_From_Source since they correspond to an
6028 -- explicit user-written allocator (that is, it will
6029 -- only be executed on behalf of callers that call the
6030 -- function as initialization for such an allocator).
6031 -- Prevents errors when No_Implicit_Heap_Allocations
6034 Set_Comes_From_Source
(Heap_Allocator
, True);
6035 Set_Comes_From_Source
(Pool_Allocator
, True);
6038 -- The allocator is returned on the secondary stack. We
6039 -- don't do this on VM targets, since the SS is not used.
6041 if VM_Target
= No_VM
then
6042 Set_Storage_Pool
(SS_Allocator
, RTE
(RE_SS_Pool
));
6043 Set_Procedure_To_Call
6044 (SS_Allocator
, RTE
(RE_SS_Allocate
));
6046 -- The allocator is returned on the secondary stack,
6047 -- so indicate that the function return, as well as
6048 -- the block that encloses the allocator, must not
6049 -- release it. The flags must be set now because
6050 -- the decision to use the secondary stack is done
6051 -- very late in the course of expanding the return
6052 -- statement, past the point where these flags are
6055 Set_Sec_Stack_Needed_For_Return
(Par_Func
);
6056 Set_Sec_Stack_Needed_For_Return
6057 (Return_Statement_Entity
(N
));
6058 Set_Uses_Sec_Stack
(Par_Func
);
6059 Set_Uses_Sec_Stack
(Return_Statement_Entity
(N
));
6062 -- Create an if statement to test the BIP_Alloc_Form
6063 -- formal and initialize the access object to either the
6064 -- BIP_Object_Access formal (BIP_Alloc_Form =
6065 -- Caller_Allocation), the result of allocating the
6066 -- object in the secondary stack (BIP_Alloc_Form =
6067 -- Secondary_Stack), or else an allocator to create the
6068 -- return object in the heap or user-defined pool
6069 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
6071 -- ??? An unchecked type conversion must be made in the
6072 -- case of assigning the access object formal to the
6073 -- local access object, because a normal conversion would
6074 -- be illegal in some cases (such as converting access-
6075 -- to-unconstrained to access-to-constrained), but the
6076 -- the unchecked conversion will presumably fail to work
6077 -- right in just such cases. It's not clear at all how to
6081 Make_If_Statement
(Loc
,
6085 New_Occurrence_Of
(Obj_Alloc_Formal
, Loc
),
6087 Make_Integer_Literal
(Loc
,
6088 UI_From_Int
(BIP_Allocation_Form
'Pos
6089 (Caller_Allocation
)))),
6091 Then_Statements
=> New_List
(
6092 Make_Assignment_Statement
(Loc
,
6094 New_Occurrence_Of
(Alloc_Obj_Id
, Loc
),
6096 Make_Unchecked_Type_Conversion
(Loc
,
6098 New_Occurrence_Of
(Ref_Type
, Loc
),
6100 New_Occurrence_Of
(Object_Access
, Loc
)))),
6102 Elsif_Parts
=> New_List
(
6103 Make_Elsif_Part
(Loc
,
6107 New_Occurrence_Of
(Obj_Alloc_Formal
, Loc
),
6109 Make_Integer_Literal
(Loc
,
6110 UI_From_Int
(BIP_Allocation_Form
'Pos
6111 (Secondary_Stack
)))),
6113 Then_Statements
=> New_List
(
6114 Make_Assignment_Statement
(Loc
,
6116 New_Occurrence_Of
(Alloc_Obj_Id
, Loc
),
6117 Expression
=> SS_Allocator
))),
6119 Make_Elsif_Part
(Loc
,
6123 New_Occurrence_Of
(Obj_Alloc_Formal
, Loc
),
6125 Make_Integer_Literal
(Loc
,
6126 UI_From_Int
(BIP_Allocation_Form
'Pos
6129 Then_Statements
=> New_List
(
6130 Build_Heap_Allocator
6131 (Temp_Id
=> Alloc_Obj_Id
,
6132 Temp_Typ
=> Ref_Type
,
6133 Func_Id
=> Par_Func
,
6134 Ret_Typ
=> Return_Obj_Typ
,
6135 Alloc_Expr
=> Heap_Allocator
)))),
6137 Else_Statements
=> New_List
(
6139 Build_Heap_Allocator
6140 (Temp_Id
=> Alloc_Obj_Id
,
6141 Temp_Typ
=> Ref_Type
,
6142 Func_Id
=> Par_Func
,
6143 Ret_Typ
=> Return_Obj_Typ
,
6144 Alloc_Expr
=> Pool_Allocator
)));
6146 -- If a separate initialization assignment was created
6147 -- earlier, append that following the assignment of the
6148 -- implicit access formal to the access object, to ensure
6149 -- that the return object is initialized in that case. In
6150 -- this situation, the target of the assignment must be
6151 -- rewritten to denote a dereference of the access to the
6152 -- return object passed in by the caller.
6154 if Present
(Init_Assignment
) then
6155 Rewrite
(Name
(Init_Assignment
),
6156 Make_Explicit_Dereference
(Loc
,
6157 Prefix
=> New_Occurrence_Of
(Alloc_Obj_Id
, Loc
)));
6160 (Name
(Init_Assignment
), Etype
(Return_Obj_Id
));
6163 (Then_Statements
(Alloc_If_Stmt
), Init_Assignment
);
6166 Insert_Before
(Ret_Obj_Decl
, Alloc_If_Stmt
);
6168 -- Remember the local access object for use in the
6169 -- dereference of the renaming created below.
6171 Object_Access
:= Alloc_Obj_Id
;
6175 -- Replace the return object declaration with a renaming of a
6176 -- dereference of the access value designating the return
6180 Make_Explicit_Dereference
(Loc
,
6181 Prefix
=> New_Occurrence_Of
(Object_Access
, Loc
));
6183 Rewrite
(Ret_Obj_Decl
,
6184 Make_Object_Renaming_Declaration
(Loc
,
6185 Defining_Identifier
=> Return_Obj_Id
,
6186 Access_Definition
=> Empty
,
6188 New_Occurrence_Of
(Return_Obj_Typ
, Loc
),
6189 Name
=> Obj_Acc_Deref
));
6191 Set_Renamed_Object
(Return_Obj_Id
, Obj_Acc_Deref
);
6195 -- Case where we do not build a block
6198 -- We're about to drop Return_Object_Declarations on the floor, so
6199 -- we need to insert it, in case it got expanded into useful code.
6200 -- Remove side effects from expression, which may be duplicated in
6201 -- subsequent checks (see Expand_Simple_Function_Return).
6203 Insert_List_Before
(N
, Return_Object_Declarations
(N
));
6204 Remove_Side_Effects
(Exp
);
6206 -- Build simple_return_statement that returns the expression directly
6208 Return_Stmt
:= Make_Simple_Return_Statement
(Loc
, Expression
=> Exp
);
6209 Result
:= Return_Stmt
;
6212 -- Set the flag to prevent infinite recursion
6214 Set_Comes_From_Extended_Return_Statement
(Return_Stmt
);
6216 Rewrite
(N
, Result
);
6218 end Expand_N_Extended_Return_Statement
;
6220 ----------------------------
6221 -- Expand_N_Function_Call --
6222 ----------------------------
6224 procedure Expand_N_Function_Call
(N
: Node_Id
) is
6228 -- If the return value of a foreign compiled function is VAX Float, then
6229 -- expand the return (adjusts the location of the return value on
6230 -- Alpha/VMS, no-op everywhere else).
6231 -- Comes_From_Source intercepts recursive expansion.
6233 if Nkind
(N
) = N_Function_Call
6234 and then Vax_Float
(Etype
(N
))
6235 and then Present
(Name
(N
))
6236 and then Present
(Entity
(Name
(N
)))
6237 and then Has_Foreign_Convention
(Entity
(Name
(N
)))
6238 and then Comes_From_Source
(Parent
(N
))
6240 Expand_Vax_Foreign_Return
(N
);
6242 end Expand_N_Function_Call
;
6244 ---------------------------------------
6245 -- Expand_N_Procedure_Call_Statement --
6246 ---------------------------------------
6248 procedure Expand_N_Procedure_Call_Statement
(N
: Node_Id
) is
6251 end Expand_N_Procedure_Call_Statement
;
6253 --------------------------------------
6254 -- Expand_N_Simple_Return_Statement --
6255 --------------------------------------
6257 procedure Expand_N_Simple_Return_Statement
(N
: Node_Id
) is
6259 -- Defend against previous errors (i.e. the return statement calls a
6260 -- function that is not available in configurable runtime).
6262 if Present
(Expression
(N
))
6263 and then Nkind
(Expression
(N
)) = N_Empty
6265 Check_Error_Detected
;
6269 -- Distinguish the function and non-function cases:
6271 case Ekind
(Return_Applies_To
(Return_Statement_Entity
(N
))) is
6274 E_Generic_Function
=>
6275 Expand_Simple_Function_Return
(N
);
6278 E_Generic_Procedure |
6281 E_Return_Statement
=>
6282 Expand_Non_Function_Return
(N
);
6285 raise Program_Error
;
6289 when RE_Not_Available
=>
6291 end Expand_N_Simple_Return_Statement
;
6293 ------------------------------
6294 -- Expand_N_Subprogram_Body --
6295 ------------------------------
6297 -- Add poll call if ATC polling is enabled, unless the body will be inlined
6300 -- Add dummy push/pop label nodes at start and end to clear any local
6301 -- exception indications if local-exception-to-goto optimization is active.
6303 -- Add return statement if last statement in body is not a return statement
6304 -- (this makes things easier on Gigi which does not want to have to handle
6305 -- a missing return).
6307 -- Add call to Activate_Tasks if body is a task activator
6309 -- Deal with possible detection of infinite recursion
6311 -- Eliminate body completely if convention stubbed
6313 -- Encode entity names within body, since we will not need to reference
6314 -- these entities any longer in the front end.
6316 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
6318 -- Reset Pure indication if any parameter has root type System.Address
6319 -- or has any parameters of limited types, where limited means that the
6320 -- run-time view is limited (i.e. the full type is limited).
6324 procedure Expand_N_Subprogram_Body
(N
: Node_Id
) is
6325 Loc
: constant Source_Ptr
:= Sloc
(N
);
6326 H
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
6327 Body_Id
: Entity_Id
;
6330 Spec_Id
: Entity_Id
;
6332 procedure Add_Return
(S
: List_Id
);
6333 -- Append a return statement to the statement sequence S if the last
6334 -- statement is not already a return or a goto statement. Note that
6335 -- the latter test is not critical, it does not matter if we add a few
6336 -- extra returns, since they get eliminated anyway later on.
6342 procedure Add_Return
(S
: List_Id
) is
6347 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
6348 -- not relevant in this context since they are not executable.
6350 Last_Stm
:= Last
(S
);
6351 while Nkind
(Last_Stm
) in N_Pop_xxx_Label
loop
6355 -- Now insert return unless last statement is a transfer
6357 if not Is_Transfer
(Last_Stm
) then
6359 -- The source location for the return is the end label of the
6360 -- procedure if present. Otherwise use the sloc of the last
6361 -- statement in the list. If the list comes from a generated
6362 -- exception handler and we are not debugging generated code,
6363 -- all the statements within the handler are made invisible
6366 if Nkind
(Parent
(S
)) = N_Exception_Handler
6367 and then not Comes_From_Source
(Parent
(S
))
6369 Loc
:= Sloc
(Last_Stm
);
6370 elsif Present
(End_Label
(H
)) then
6371 Loc
:= Sloc
(End_Label
(H
));
6373 Loc
:= Sloc
(Last_Stm
);
6377 Rtn
: constant Node_Id
:= Make_Simple_Return_Statement
(Loc
);
6380 -- Append return statement, and set analyzed manually. We can't
6381 -- call Analyze on this return since the scope is wrong.
6383 -- Note: it almost works to push the scope and then do the
6384 -- Analyze call, but something goes wrong in some weird cases
6385 -- and it is not worth worrying about ???
6390 -- Call _Postconditions procedure if appropriate. We need to
6391 -- do this explicitly because we did not analyze the generated
6392 -- return statement above, so the call did not get inserted.
6394 if Ekind
(Spec_Id
) = E_Procedure
6395 and then Has_Postconditions
(Spec_Id
)
6397 pragma Assert
(Present
(Postcondition_Proc
(Spec_Id
)));
6399 Make_Procedure_Call_Statement
(Loc
,
6402 (Postcondition_Proc
(Spec_Id
), Loc
)));
6408 -- Start of processing for Expand_N_Subprogram_Body
6411 -- Set L to either the list of declarations if present, or to the list
6412 -- of statements if no declarations are present. This is used to insert
6413 -- new stuff at the start.
6415 if Is_Non_Empty_List
(Declarations
(N
)) then
6416 L
:= Declarations
(N
);
6418 L
:= Statements
(H
);
6421 -- If local-exception-to-goto optimization active, insert dummy push
6422 -- statements at start, and dummy pop statements at end, but inhibit
6423 -- this if we have No_Exception_Handlers, since they are useless and
6424 -- intefere with analysis, e.g. by codepeer.
6426 if (Debug_Flag_Dot_G
6427 or else Restriction_Active
(No_Exception_Propagation
))
6428 and then not Restriction_Active
(No_Exception_Handlers
)
6429 and then not CodePeer_Mode
6430 and then Is_Non_Empty_List
(L
)
6433 FS
: constant Node_Id
:= First
(L
);
6434 FL
: constant Source_Ptr
:= Sloc
(FS
);
6439 -- LS points to either last statement, if statements are present
6440 -- or to the last declaration if there are no statements present.
6441 -- It is the node after which the pop's are generated.
6443 if Is_Non_Empty_List
(Statements
(H
)) then
6444 LS
:= Last
(Statements
(H
));
6451 Insert_List_Before_And_Analyze
(FS
, New_List
(
6452 Make_Push_Constraint_Error_Label
(FL
),
6453 Make_Push_Program_Error_Label
(FL
),
6454 Make_Push_Storage_Error_Label
(FL
)));
6456 Insert_List_After_And_Analyze
(LS
, New_List
(
6457 Make_Pop_Constraint_Error_Label
(LL
),
6458 Make_Pop_Program_Error_Label
(LL
),
6459 Make_Pop_Storage_Error_Label
(LL
)));
6463 -- Find entity for subprogram
6465 Body_Id
:= Defining_Entity
(N
);
6467 if Present
(Corresponding_Spec
(N
)) then
6468 Spec_Id
:= Corresponding_Spec
(N
);
6473 -- Need poll on entry to subprogram if polling enabled. We only do this
6474 -- for non-empty subprograms, since it does not seem necessary to poll
6475 -- for a dummy null subprogram.
6477 if Is_Non_Empty_List
(L
) then
6479 -- Do not add a polling call if the subprogram is to be inlined by
6480 -- the back-end, to avoid repeated calls with multiple inlinings.
6482 if Is_Inlined
(Spec_Id
)
6483 and then Front_End_Inlining
6484 and then Optimization_Level
> 1
6488 Generate_Poll_Call
(First
(L
));
6492 -- If this is a Pure function which has any parameters whose root type
6493 -- is System.Address, reset the Pure indication, since it will likely
6494 -- cause incorrect code to be generated as the parameter is probably
6495 -- a pointer, and the fact that the same pointer is passed does not mean
6496 -- that the same value is being referenced.
6498 -- Note that if the programmer gave an explicit Pure_Function pragma,
6499 -- then we believe the programmer, and leave the subprogram Pure.
6501 -- This code should probably be at the freeze point, so that it happens
6502 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
6503 -- semantic tree has Is_Pure set properly ???
6505 if Is_Pure
(Spec_Id
)
6506 and then Is_Subprogram
(Spec_Id
)
6507 and then not Has_Pragma_Pure_Function
(Spec_Id
)
6513 F
:= First_Formal
(Spec_Id
);
6514 while Present
(F
) loop
6515 if Is_Descendent_Of_Address
(Etype
(F
))
6517 -- Note that this test is being made in the body of the
6518 -- subprogram, not the spec, so we are testing the full
6519 -- type for being limited here, as required.
6521 or else Is_Limited_Type
(Etype
(F
))
6523 Set_Is_Pure
(Spec_Id
, False);
6525 if Spec_Id
/= Body_Id
then
6526 Set_Is_Pure
(Body_Id
, False);
6537 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
6539 if Init_Or_Norm_Scalars
and then Is_Subprogram
(Spec_Id
) then
6545 -- Loop through formals
6547 F
:= First_Formal
(Spec_Id
);
6548 while Present
(F
) loop
6549 if Is_Scalar_Type
(Etype
(F
))
6550 and then Ekind
(F
) = E_Out_Parameter
6552 Check_Restriction
(No_Default_Initialization
, F
);
6554 -- Insert the initialization. We turn off validity checks
6555 -- for this assignment, since we do not want any check on
6556 -- the initial value itself (which may well be invalid).
6557 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
6559 A
:= Make_Assignment_Statement
(Loc
,
6560 Name
=> New_Occurrence_Of
(F
, Loc
),
6561 Expression
=> Get_Simple_Init_Val
(Etype
(F
), N
));
6562 Set_Suppress_Assignment_Checks
(A
);
6564 Insert_Before_And_Analyze
(First
(L
),
6565 A
, Suppress
=> Validity_Check
);
6573 -- Clear out statement list for stubbed procedure
6575 if Present
(Corresponding_Spec
(N
)) then
6576 Set_Elaboration_Flag
(N
, Spec_Id
);
6578 if Convention
(Spec_Id
) = Convention_Stubbed
6579 or else Is_Eliminated
(Spec_Id
)
6581 Set_Declarations
(N
, Empty_List
);
6582 Set_Handled_Statement_Sequence
(N
,
6583 Make_Handled_Sequence_Of_Statements
(Loc
,
6584 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
6589 -- Create a set of discriminals for the next protected subprogram body
6591 if Is_List_Member
(N
)
6592 and then Present
(Parent
(List_Containing
(N
)))
6593 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
6594 and then Present
(Next_Protected_Operation
(N
))
6596 Set_Discriminals
(Parent
(Base_Type
(Scope
(Spec_Id
))));
6599 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
6600 -- subprograms with no specs are not frozen.
6603 Typ
: constant Entity_Id
:= Etype
(Spec_Id
);
6604 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
6607 if not Acts_As_Spec
(N
)
6608 and then Nkind
(Parent
(Parent
(Spec_Id
))) /=
6609 N_Subprogram_Body_Stub
6613 elsif Is_Limited_View
(Typ
) then
6614 Set_Returns_By_Ref
(Spec_Id
);
6616 elsif Present
(Utyp
) and then CW_Or_Has_Controlled_Part
(Utyp
) then
6617 Set_Returns_By_Ref
(Spec_Id
);
6621 -- For a procedure, we add a return for all possible syntactic ends of
6624 if Ekind_In
(Spec_Id
, E_Procedure
, E_Generic_Procedure
) then
6625 Add_Return
(Statements
(H
));
6627 if Present
(Exception_Handlers
(H
)) then
6628 Except_H
:= First_Non_Pragma
(Exception_Handlers
(H
));
6629 while Present
(Except_H
) loop
6630 Add_Return
(Statements
(Except_H
));
6631 Next_Non_Pragma
(Except_H
);
6635 -- For a function, we must deal with the case where there is at least
6636 -- one missing return. What we do is to wrap the entire body of the
6637 -- function in a block:
6650 -- raise Program_Error;
6653 -- This approach is necessary because the raise must be signalled to the
6654 -- caller, not handled by any local handler (RM 6.4(11)).
6656 -- Note: we do not need to analyze the constructed sequence here, since
6657 -- it has no handler, and an attempt to analyze the handled statement
6658 -- sequence twice is risky in various ways (e.g. the issue of expanding
6659 -- cleanup actions twice).
6661 elsif Has_Missing_Return
(Spec_Id
) then
6663 Hloc
: constant Source_Ptr
:= Sloc
(H
);
6664 Blok
: constant Node_Id
:=
6665 Make_Block_Statement
(Hloc
,
6666 Handled_Statement_Sequence
=> H
);
6667 Rais
: constant Node_Id
:=
6668 Make_Raise_Program_Error
(Hloc
,
6669 Reason
=> PE_Missing_Return
);
6672 Set_Handled_Statement_Sequence
(N
,
6673 Make_Handled_Sequence_Of_Statements
(Hloc
,
6674 Statements
=> New_List
(Blok
, Rais
)));
6676 Push_Scope
(Spec_Id
);
6683 -- If subprogram contains a parameterless recursive call, then we may
6684 -- have an infinite recursion, so see if we can generate code to check
6685 -- for this possibility if storage checks are not suppressed.
6687 if Ekind
(Spec_Id
) = E_Procedure
6688 and then Has_Recursive_Call
(Spec_Id
)
6689 and then not Storage_Checks_Suppressed
(Spec_Id
)
6691 Detect_Infinite_Recursion
(N
, Spec_Id
);
6694 -- Set to encode entity names in package body before gigi is called
6696 Qualify_Entity_Names
(N
);
6697 end Expand_N_Subprogram_Body
;
6699 -----------------------------------
6700 -- Expand_N_Subprogram_Body_Stub --
6701 -----------------------------------
6703 procedure Expand_N_Subprogram_Body_Stub
(N
: Node_Id
) is
6705 if Present
(Corresponding_Body
(N
)) then
6706 Expand_N_Subprogram_Body
(
6707 Unit_Declaration_Node
(Corresponding_Body
(N
)));
6709 end Expand_N_Subprogram_Body_Stub
;
6711 -------------------------------------
6712 -- Expand_N_Subprogram_Declaration --
6713 -------------------------------------
6715 -- If the declaration appears within a protected body, it is a private
6716 -- operation of the protected type. We must create the corresponding
6717 -- protected subprogram an associated formals. For a normal protected
6718 -- operation, this is done when expanding the protected type declaration.
6720 -- If the declaration is for a null procedure, emit null body
6722 procedure Expand_N_Subprogram_Declaration
(N
: Node_Id
) is
6723 Loc
: constant Source_Ptr
:= Sloc
(N
);
6724 Subp
: constant Entity_Id
:= Defining_Entity
(N
);
6725 Scop
: constant Entity_Id
:= Scope
(Subp
);
6726 Prot_Decl
: Node_Id
;
6728 Prot_Id
: Entity_Id
;
6731 -- In SPARK, subprogram declarations are only allowed in package
6734 if Nkind
(Parent
(N
)) /= N_Package_Specification
then
6735 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
6736 Check_SPARK_Restriction
6737 ("subprogram declaration is not a library item", N
);
6739 elsif Present
(Next
(N
))
6740 and then Nkind
(Next
(N
)) = N_Pragma
6741 and then Get_Pragma_Id
(Pragma_Name
(Next
(N
))) = Pragma_Import
6743 -- In SPARK, subprogram declarations are also permitted in
6744 -- declarative parts when immediately followed by a corresponding
6745 -- pragma Import. We only check here that there is some pragma
6750 Check_SPARK_Restriction
6751 ("subprogram declaration is not allowed here", N
);
6755 -- Deal with case of protected subprogram. Do not generate protected
6756 -- operation if operation is flagged as eliminated.
6758 if Is_List_Member
(N
)
6759 and then Present
(Parent
(List_Containing
(N
)))
6760 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
6761 and then Is_Protected_Type
(Scop
)
6763 if No
(Protected_Body_Subprogram
(Subp
))
6764 and then not Is_Eliminated
(Subp
)
6767 Make_Subprogram_Declaration
(Loc
,
6769 Build_Protected_Sub_Specification
6770 (N
, Scop
, Unprotected_Mode
));
6772 -- The protected subprogram is declared outside of the protected
6773 -- body. Given that the body has frozen all entities so far, we
6774 -- analyze the subprogram and perform freezing actions explicitly.
6775 -- including the generation of an explicit freeze node, to ensure
6776 -- that gigi has the proper order of elaboration.
6777 -- If the body is a subunit, the insertion point is before the
6778 -- stub in the parent.
6780 Prot_Bod
:= Parent
(List_Containing
(N
));
6782 if Nkind
(Parent
(Prot_Bod
)) = N_Subunit
then
6783 Prot_Bod
:= Corresponding_Stub
(Parent
(Prot_Bod
));
6786 Insert_Before
(Prot_Bod
, Prot_Decl
);
6787 Prot_Id
:= Defining_Unit_Name
(Specification
(Prot_Decl
));
6788 Set_Has_Delayed_Freeze
(Prot_Id
);
6790 Push_Scope
(Scope
(Scop
));
6791 Analyze
(Prot_Decl
);
6792 Freeze_Before
(N
, Prot_Id
);
6793 Set_Protected_Body_Subprogram
(Subp
, Prot_Id
);
6795 -- Create protected operation as well. Even though the operation
6796 -- is only accessible within the body, it is possible to make it
6797 -- available outside of the protected object by using 'Access to
6798 -- provide a callback, so build protected version in all cases.
6801 Make_Subprogram_Declaration
(Loc
,
6803 Build_Protected_Sub_Specification
(N
, Scop
, Protected_Mode
));
6804 Insert_Before
(Prot_Bod
, Prot_Decl
);
6805 Analyze
(Prot_Decl
);
6810 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
6811 -- cases this is superfluous because calls to it will be automatically
6812 -- inlined, but we definitely need the body if preconditions for the
6813 -- procedure are present.
6815 elsif Nkind
(Specification
(N
)) = N_Procedure_Specification
6816 and then Null_Present
(Specification
(N
))
6819 Bod
: constant Node_Id
:= Body_To_Inline
(N
);
6822 Set_Has_Completion
(Subp
, False);
6823 Append_Freeze_Action
(Subp
, Bod
);
6825 -- The body now contains raise statements, so calls to it will
6828 Set_Is_Inlined
(Subp
, False);
6831 end Expand_N_Subprogram_Declaration
;
6833 --------------------------------
6834 -- Expand_Non_Function_Return --
6835 --------------------------------
6837 procedure Expand_Non_Function_Return
(N
: Node_Id
) is
6838 pragma Assert
(No
(Expression
(N
)));
6840 Loc
: constant Source_Ptr
:= Sloc
(N
);
6841 Scope_Id
: Entity_Id
:=
6842 Return_Applies_To
(Return_Statement_Entity
(N
));
6843 Kind
: constant Entity_Kind
:= Ekind
(Scope_Id
);
6846 Goto_Stat
: Node_Id
;
6850 -- Call _Postconditions procedure if procedure with active
6851 -- postconditions. Here, we use the Postcondition_Proc attribute,
6852 -- which is needed for implicitly-generated returns. Functions
6853 -- never have implicitly-generated returns, and there's no
6854 -- room for Postcondition_Proc in E_Function, so we look up the
6855 -- identifier Name_uPostconditions for function returns (see
6856 -- Expand_Simple_Function_Return).
6858 if Ekind
(Scope_Id
) = E_Procedure
6859 and then Has_Postconditions
(Scope_Id
)
6861 pragma Assert
(Present
(Postcondition_Proc
(Scope_Id
)));
6863 Make_Procedure_Call_Statement
(Loc
,
6864 Name
=> New_Occurrence_Of
(Postcondition_Proc
(Scope_Id
), Loc
)));
6867 -- If it is a return from a procedure do no extra steps
6869 if Kind
= E_Procedure
or else Kind
= E_Generic_Procedure
then
6872 -- If it is a nested return within an extended one, replace it with a
6873 -- return of the previously declared return object.
6875 elsif Kind
= E_Return_Statement
then
6877 Make_Simple_Return_Statement
(Loc
,
6879 New_Occurrence_Of
(First_Entity
(Scope_Id
), Loc
)));
6880 Set_Comes_From_Extended_Return_Statement
(N
);
6881 Set_Return_Statement_Entity
(N
, Scope_Id
);
6882 Expand_Simple_Function_Return
(N
);
6886 pragma Assert
(Is_Entry
(Scope_Id
));
6888 -- Look at the enclosing block to see whether the return is from an
6889 -- accept statement or an entry body.
6891 for J
in reverse 0 .. Scope_Stack
.Last
loop
6892 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
6893 exit when Is_Concurrent_Type
(Scope_Id
);
6896 -- If it is a return from accept statement it is expanded as call to
6897 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6899 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6900 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6902 if Is_Task_Type
(Scope_Id
) then
6905 Make_Procedure_Call_Statement
(Loc
,
6906 Name
=> New_Occurrence_Of
(RTE
(RE_Complete_Rendezvous
), Loc
));
6907 Insert_Before
(N
, Call
);
6908 -- why not insert actions here???
6911 Acc_Stat
:= Parent
(N
);
6912 while Nkind
(Acc_Stat
) /= N_Accept_Statement
loop
6913 Acc_Stat
:= Parent
(Acc_Stat
);
6916 Lab_Node
:= Last
(Statements
6917 (Handled_Statement_Sequence
(Acc_Stat
)));
6919 Goto_Stat
:= Make_Goto_Statement
(Loc
,
6920 Name
=> New_Occurrence_Of
6921 (Entity
(Identifier
(Lab_Node
)), Loc
));
6923 Set_Analyzed
(Goto_Stat
);
6925 Rewrite
(N
, Goto_Stat
);
6928 -- If it is a return from an entry body, put a Complete_Entry_Body call
6929 -- in front of the return.
6931 elsif Is_Protected_Type
(Scope_Id
) then
6933 Make_Procedure_Call_Statement
(Loc
,
6935 New_Occurrence_Of
(RTE
(RE_Complete_Entry_Body
), Loc
),
6936 Parameter_Associations
=> New_List
(
6937 Make_Attribute_Reference
(Loc
,
6940 (Find_Protection_Object
(Current_Scope
), Loc
),
6941 Attribute_Name
=> Name_Unchecked_Access
)));
6943 Insert_Before
(N
, Call
);
6946 end Expand_Non_Function_Return
;
6948 ---------------------------------------
6949 -- Expand_Protected_Object_Reference --
6950 ---------------------------------------
6952 function Expand_Protected_Object_Reference
6954 Scop
: Entity_Id
) return Node_Id
6956 Loc
: constant Source_Ptr
:= Sloc
(N
);
6963 Rec
:= Make_Identifier
(Loc
, Name_uObject
);
6964 Set_Etype
(Rec
, Corresponding_Record_Type
(Scop
));
6966 -- Find enclosing protected operation, and retrieve its first parameter,
6967 -- which denotes the enclosing protected object. If the enclosing
6968 -- operation is an entry, we are immediately within the protected body,
6969 -- and we can retrieve the object from the service entries procedure. A
6970 -- barrier function has the same signature as an entry. A barrier
6971 -- function is compiled within the protected object, but unlike
6972 -- protected operations its never needs locks, so that its protected
6973 -- body subprogram points to itself.
6975 Proc
:= Current_Scope
;
6976 while Present
(Proc
)
6977 and then Scope
(Proc
) /= Scop
6979 Proc
:= Scope
(Proc
);
6982 Corr
:= Protected_Body_Subprogram
(Proc
);
6986 -- Previous error left expansion incomplete.
6987 -- Nothing to do on this call.
6994 (First
(Parameter_Specifications
(Parent
(Corr
))));
6996 if Is_Subprogram
(Proc
)
6997 and then Proc
/= Corr
6999 -- Protected function or procedure
7001 Set_Entity
(Rec
, Param
);
7003 -- Rec is a reference to an entity which will not be in scope when
7004 -- the call is reanalyzed, and needs no further analysis.
7009 -- Entry or barrier function for entry body. The first parameter of
7010 -- the entry body procedure is pointer to the object. We create a
7011 -- local variable of the proper type, duplicating what is done to
7012 -- define _object later on.
7016 Obj_Ptr
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
7020 Make_Full_Type_Declaration
(Loc
,
7021 Defining_Identifier
=> Obj_Ptr
,
7023 Make_Access_To_Object_Definition
(Loc
,
7024 Subtype_Indication
=>
7026 (Corresponding_Record_Type
(Scop
), Loc
))));
7028 Insert_Actions
(N
, Decls
);
7029 Freeze_Before
(N
, Obj_Ptr
);
7032 Make_Explicit_Dereference
(Loc
,
7034 Unchecked_Convert_To
(Obj_Ptr
,
7035 New_Occurrence_Of
(Param
, Loc
)));
7037 -- Analyze new actual. Other actuals in calls are already analyzed
7038 -- and the list of actuals is not reanalyzed after rewriting.
7040 Set_Parent
(Rec
, N
);
7046 end Expand_Protected_Object_Reference
;
7048 --------------------------------------
7049 -- Expand_Protected_Subprogram_Call --
7050 --------------------------------------
7052 procedure Expand_Protected_Subprogram_Call
7060 -- If the protected object is not an enclosing scope, this is an inter-
7061 -- object function call. Inter-object procedure calls are expanded by
7062 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
7063 -- subprogram being called is in the protected body being compiled, and
7064 -- if the protected object in the call is statically the enclosing type.
7065 -- The object may be an component of some other data structure, in which
7066 -- case this must be handled as an inter-object call.
7068 if not In_Open_Scopes
(Scop
)
7069 or else not Is_Entity_Name
(Name
(N
))
7071 if Nkind
(Name
(N
)) = N_Selected_Component
then
7072 Rec
:= Prefix
(Name
(N
));
7075 pragma Assert
(Nkind
(Name
(N
)) = N_Indexed_Component
);
7076 Rec
:= Prefix
(Prefix
(Name
(N
)));
7079 Build_Protected_Subprogram_Call
(N
,
7080 Name
=> New_Occurrence_Of
(Subp
, Sloc
(N
)),
7081 Rec
=> Convert_Concurrent
(Rec
, Etype
(Rec
)),
7085 Rec
:= Expand_Protected_Object_Reference
(N
, Scop
);
7091 Build_Protected_Subprogram_Call
(N
,
7098 -- If it is a function call it can appear in elaboration code and
7099 -- the called entity must be frozen here.
7101 if Ekind
(Subp
) = E_Function
then
7102 Freeze_Expression
(Name
(N
));
7105 -- Analyze and resolve the new call. The actuals have already been
7106 -- resolved, but expansion of a function call will add extra actuals
7107 -- if needed. Analysis of a procedure call already includes resolution.
7111 if Ekind
(Subp
) = E_Function
then
7112 Resolve
(N
, Etype
(Subp
));
7114 end Expand_Protected_Subprogram_Call
;
7116 --------------------------------------------
7117 -- Has_Unconstrained_Access_Discriminants --
7118 --------------------------------------------
7120 function Has_Unconstrained_Access_Discriminants
7121 (Subtyp
: Entity_Id
) return Boolean
7126 if Has_Discriminants
(Subtyp
)
7127 and then not Is_Constrained
(Subtyp
)
7129 Discr
:= First_Discriminant
(Subtyp
);
7130 while Present
(Discr
) loop
7131 if Ekind
(Etype
(Discr
)) = E_Anonymous_Access_Type
then
7135 Next_Discriminant
(Discr
);
7140 end Has_Unconstrained_Access_Discriminants
;
7142 -----------------------------------
7143 -- Expand_Simple_Function_Return --
7144 -----------------------------------
7146 -- The "simple" comes from the syntax rule simple_return_statement. The
7147 -- semantics are not at all simple.
7149 procedure Expand_Simple_Function_Return
(N
: Node_Id
) is
7150 Loc
: constant Source_Ptr
:= Sloc
(N
);
7152 Scope_Id
: constant Entity_Id
:=
7153 Return_Applies_To
(Return_Statement_Entity
(N
));
7154 -- The function we are returning from
7156 R_Type
: constant Entity_Id
:= Etype
(Scope_Id
);
7157 -- The result type of the function
7159 Utyp
: constant Entity_Id
:= Underlying_Type
(R_Type
);
7161 Exp
: constant Node_Id
:= Expression
(N
);
7162 pragma Assert
(Present
(Exp
));
7164 Exptyp
: constant Entity_Id
:= Etype
(Exp
);
7165 -- The type of the expression (not necessarily the same as R_Type)
7167 Subtype_Ind
: Node_Id
;
7168 -- If the result type of the function is class-wide and the expression
7169 -- has a specific type, then we use the expression's type as the type of
7170 -- the return object. In cases where the expression is an aggregate that
7171 -- is built in place, this avoids the need for an expensive conversion
7172 -- of the return object to the specific type on assignments to the
7173 -- individual components.
7176 if Is_Class_Wide_Type
(R_Type
)
7177 and then not Is_Class_Wide_Type
(Etype
(Exp
))
7179 Subtype_Ind
:= New_Occurrence_Of
(Etype
(Exp
), Loc
);
7181 Subtype_Ind
:= New_Occurrence_Of
(R_Type
, Loc
);
7184 -- For the case of a simple return that does not come from an extended
7185 -- return, in the case of Ada 2005 where we are returning a limited
7186 -- type, we rewrite "return <expression>;" to be:
7188 -- return _anon_ : <return_subtype> := <expression>
7190 -- The expansion produced by Expand_N_Extended_Return_Statement will
7191 -- contain simple return statements (for example, a block containing
7192 -- simple return of the return object), which brings us back here with
7193 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
7194 -- checking for a simple return that does not come from an extended
7195 -- return is to avoid this infinite recursion.
7197 -- The reason for this design is that for Ada 2005 limited returns, we
7198 -- need to reify the return object, so we can build it "in place", and
7199 -- we need a block statement to hang finalization and tasking stuff.
7201 -- ??? In order to avoid disruption, we avoid translating to extended
7202 -- return except in the cases where we really need to (Ada 2005 for
7203 -- inherently limited). We might prefer to do this translation in all
7204 -- cases (except perhaps for the case of Ada 95 inherently limited),
7205 -- in order to fully exercise the Expand_N_Extended_Return_Statement
7206 -- code. This would also allow us to do the build-in-place optimization
7207 -- for efficiency even in cases where it is semantically not required.
7209 -- As before, we check the type of the return expression rather than the
7210 -- return type of the function, because the latter may be a limited
7211 -- class-wide interface type, which is not a limited type, even though
7212 -- the type of the expression may be.
7214 if not Comes_From_Extended_Return_Statement
(N
)
7215 and then Is_Limited_View
(Etype
(Expression
(N
)))
7216 and then Ada_Version
>= Ada_2005
7217 and then not Debug_Flag_Dot_L
7219 -- The functionality of interface thunks is simple and it is always
7220 -- handled by means of simple return statements. This leaves their
7221 -- expansion simple and clean.
7223 and then not Is_Thunk
(Current_Scope
)
7226 Return_Object_Entity
: constant Entity_Id
:=
7227 Make_Temporary
(Loc
, 'R', Exp
);
7229 Obj_Decl
: constant Node_Id
:=
7230 Make_Object_Declaration
(Loc
,
7231 Defining_Identifier
=> Return_Object_Entity
,
7232 Object_Definition
=> Subtype_Ind
,
7235 Ext
: constant Node_Id
:=
7236 Make_Extended_Return_Statement
(Loc
,
7237 Return_Object_Declarations
=> New_List
(Obj_Decl
));
7238 -- Do not perform this high-level optimization if the result type
7239 -- is an interface because the "this" pointer must be displaced.
7248 -- Here we have a simple return statement that is part of the expansion
7249 -- of an extended return statement (either written by the user, or
7250 -- generated by the above code).
7252 -- Always normalize C/Fortran boolean result. This is not always needed,
7253 -- but it seems a good idea to minimize the passing around of non-
7254 -- normalized values, and in any case this handles the processing of
7255 -- barrier functions for protected types, which turn the condition into
7256 -- a return statement.
7258 if Is_Boolean_Type
(Exptyp
)
7259 and then Nonzero_Is_True
(Exptyp
)
7261 Adjust_Condition
(Exp
);
7262 Adjust_Result_Type
(Exp
, Exptyp
);
7265 -- Do validity check if enabled for returns
7267 if Validity_Checks_On
7268 and then Validity_Check_Returns
7273 -- Check the result expression of a scalar function against the subtype
7274 -- of the function by inserting a conversion. This conversion must
7275 -- eventually be performed for other classes of types, but for now it's
7276 -- only done for scalars.
7279 if Is_Scalar_Type
(Exptyp
) then
7280 Rewrite
(Exp
, Convert_To
(R_Type
, Exp
));
7282 -- The expression is resolved to ensure that the conversion gets
7283 -- expanded to generate a possible constraint check.
7285 Analyze_And_Resolve
(Exp
, R_Type
);
7288 -- Deal with returning variable length objects and controlled types
7290 -- Nothing to do if we are returning by reference, or this is not a
7291 -- type that requires special processing (indicated by the fact that
7292 -- it requires a cleanup scope for the secondary stack case).
7294 if Is_Limited_View
(Exptyp
)
7295 or else Is_Limited_Interface
(Exptyp
)
7299 -- No copy needed for thunks returning interface type objects since
7300 -- the object is returned by reference and the maximum functionality
7301 -- required is just to displace the pointer.
7303 elsif Is_Thunk
(Current_Scope
) and then Is_Interface
(Exptyp
) then
7306 elsif not Requires_Transient_Scope
(R_Type
) then
7308 -- Mutable records with no variable length components are not
7309 -- returned on the sec-stack, so we need to make sure that the
7310 -- backend will only copy back the size of the actual value, and not
7311 -- the maximum size. We create an actual subtype for this purpose.
7314 Ubt
: constant Entity_Id
:= Underlying_Type
(Base_Type
(Exptyp
));
7318 if Has_Discriminants
(Ubt
)
7319 and then not Is_Constrained
(Ubt
)
7320 and then not Has_Unchecked_Union
(Ubt
)
7322 Decl
:= Build_Actual_Subtype
(Ubt
, Exp
);
7323 Ent
:= Defining_Identifier
(Decl
);
7324 Insert_Action
(Exp
, Decl
);
7325 Rewrite
(Exp
, Unchecked_Convert_To
(Ent
, Exp
));
7326 Analyze_And_Resolve
(Exp
);
7330 -- Here if secondary stack is used
7333 -- Make sure that no surrounding block will reclaim the secondary
7334 -- stack on which we are going to put the result. Not only may this
7335 -- introduce secondary stack leaks but worse, if the reclamation is
7336 -- done too early, then the result we are returning may get
7343 while Ekind
(S
) = E_Block
or else Ekind
(S
) = E_Loop
loop
7344 Set_Sec_Stack_Needed_For_Return
(S
, True);
7345 S
:= Enclosing_Dynamic_Scope
(S
);
7348 -- The enclosing function itself must be marked as well, to
7349 -- prevent premature secondary stack cleanup.
7351 if Ekind
(S
) = E_Function
then
7352 Set_Sec_Stack_Needed_For_Return
(Scope_Id
);
7356 -- Optimize the case where the result is a function call. In this
7357 -- case either the result is already on the secondary stack, or is
7358 -- already being returned with the stack pointer depressed and no
7359 -- further processing is required except to set the By_Ref flag
7360 -- to ensure that gigi does not attempt an extra unnecessary copy.
7361 -- (actually not just unnecessary but harmfully wrong in the case
7362 -- of a controlled type, where gigi does not know how to do a copy).
7363 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
7364 -- for array types if the constrained status of the target type is
7365 -- different from that of the expression.
7367 if Requires_Transient_Scope
(Exptyp
)
7369 (not Is_Array_Type
(Exptyp
)
7370 or else Is_Constrained
(Exptyp
) = Is_Constrained
(R_Type
)
7371 or else CW_Or_Has_Controlled_Part
(Utyp
))
7372 and then Nkind
(Exp
) = N_Function_Call
7376 -- Remove side effects from the expression now so that other parts
7377 -- of the expander do not have to reanalyze this node without this
7380 Rewrite
(Exp
, Duplicate_Subexpr_No_Checks
(Exp
));
7382 -- For controlled types, do the allocation on the secondary stack
7383 -- manually in order to call adjust at the right time:
7385 -- type Anon1 is access R_Type;
7386 -- for Anon1'Storage_pool use ss_pool;
7387 -- Anon2 : anon1 := new R_Type'(expr);
7388 -- return Anon2.all;
7390 -- We do the same for classwide types that are not potentially
7391 -- controlled (by the virtue of restriction No_Finalization) because
7392 -- gigi is not able to properly allocate class-wide types.
7394 elsif CW_Or_Has_Controlled_Part
(Utyp
) then
7396 Loc
: constant Source_Ptr
:= Sloc
(N
);
7397 Acc_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
7398 Alloc_Node
: Node_Id
;
7402 Set_Ekind
(Acc_Typ
, E_Access_Type
);
7404 Set_Associated_Storage_Pool
(Acc_Typ
, RTE
(RE_SS_Pool
));
7406 -- This is an allocator for the secondary stack, and it's fine
7407 -- to have Comes_From_Source set False on it, as gigi knows not
7408 -- to flag it as a violation of No_Implicit_Heap_Allocations.
7411 Make_Allocator
(Loc
,
7413 Make_Qualified_Expression
(Loc
,
7414 Subtype_Mark
=> New_Occurrence_Of
(Etype
(Exp
), Loc
),
7415 Expression
=> Relocate_Node
(Exp
)));
7417 -- We do not want discriminant checks on the declaration,
7418 -- given that it gets its value from the allocator.
7420 Set_No_Initialization
(Alloc_Node
);
7422 Temp
:= Make_Temporary
(Loc
, 'R', Alloc_Node
);
7424 Insert_List_Before_And_Analyze
(N
, New_List
(
7425 Make_Full_Type_Declaration
(Loc
,
7426 Defining_Identifier
=> Acc_Typ
,
7428 Make_Access_To_Object_Definition
(Loc
,
7429 Subtype_Indication
=> Subtype_Ind
)),
7431 Make_Object_Declaration
(Loc
,
7432 Defining_Identifier
=> Temp
,
7433 Object_Definition
=> New_Occurrence_Of
(Acc_Typ
, Loc
),
7434 Expression
=> Alloc_Node
)));
7437 Make_Explicit_Dereference
(Loc
,
7438 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
7440 -- Ada 2005 (AI-251): If the type of the returned object is
7441 -- an interface then add an implicit type conversion to force
7442 -- displacement of the "this" pointer.
7444 if Is_Interface
(R_Type
) then
7445 Rewrite
(Exp
, Convert_To
(R_Type
, Relocate_Node
(Exp
)));
7448 Analyze_And_Resolve
(Exp
, R_Type
);
7451 -- Otherwise use the gigi mechanism to allocate result on the
7455 Check_Restriction
(No_Secondary_Stack
, N
);
7456 Set_Storage_Pool
(N
, RTE
(RE_SS_Pool
));
7458 -- If we are generating code for the VM do not use
7459 -- SS_Allocate since everything is heap-allocated anyway.
7461 if VM_Target
= No_VM
then
7462 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
7467 -- Implement the rules of 6.5(8-10), which require a tag check in
7468 -- the case of a limited tagged return type, and tag reassignment for
7469 -- nonlimited tagged results. These actions are needed when the return
7470 -- type is a specific tagged type and the result expression is a
7471 -- conversion or a formal parameter, because in that case the tag of
7472 -- the expression might differ from the tag of the specific result type.
7474 if Is_Tagged_Type
(Utyp
)
7475 and then not Is_Class_Wide_Type
(Utyp
)
7476 and then (Nkind_In
(Exp
, N_Type_Conversion
,
7477 N_Unchecked_Type_Conversion
)
7478 or else (Is_Entity_Name
(Exp
)
7479 and then Ekind
(Entity
(Exp
)) in Formal_Kind
))
7481 -- When the return type is limited, perform a check that the tag of
7482 -- the result is the same as the tag of the return type.
7484 if Is_Limited_Type
(R_Type
) then
7486 Make_Raise_Constraint_Error
(Loc
,
7490 Make_Selected_Component
(Loc
,
7491 Prefix
=> Duplicate_Subexpr
(Exp
),
7492 Selector_Name
=> Make_Identifier
(Loc
, Name_uTag
)),
7494 Make_Attribute_Reference
(Loc
,
7496 New_Occurrence_Of
(Base_Type
(Utyp
), Loc
),
7497 Attribute_Name
=> Name_Tag
)),
7498 Reason
=> CE_Tag_Check_Failed
));
7500 -- If the result type is a specific nonlimited tagged type, then we
7501 -- have to ensure that the tag of the result is that of the result
7502 -- type. This is handled by making a copy of the expression in
7503 -- the case where it might have a different tag, namely when the
7504 -- expression is a conversion or a formal parameter. We create a new
7505 -- object of the result type and initialize it from the expression,
7506 -- which will implicitly force the tag to be set appropriately.
7510 ExpR
: constant Node_Id
:= Relocate_Node
(Exp
);
7511 Result_Id
: constant Entity_Id
:=
7512 Make_Temporary
(Loc
, 'R', ExpR
);
7513 Result_Exp
: constant Node_Id
:=
7514 New_Occurrence_Of
(Result_Id
, Loc
);
7515 Result_Obj
: constant Node_Id
:=
7516 Make_Object_Declaration
(Loc
,
7517 Defining_Identifier
=> Result_Id
,
7518 Object_Definition
=>
7519 New_Occurrence_Of
(R_Type
, Loc
),
7520 Constant_Present
=> True,
7521 Expression
=> ExpR
);
7524 Set_Assignment_OK
(Result_Obj
);
7525 Insert_Action
(Exp
, Result_Obj
);
7527 Rewrite
(Exp
, Result_Exp
);
7528 Analyze_And_Resolve
(Exp
, R_Type
);
7532 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
7533 -- a check that the level of the return expression's underlying type
7534 -- is not deeper than the level of the master enclosing the function.
7535 -- Always generate the check when the type of the return expression
7536 -- is class-wide, when it's a type conversion, or when it's a formal
7537 -- parameter. Otherwise, suppress the check in the case where the
7538 -- return expression has a specific type whose level is known not to
7539 -- be statically deeper than the function's result type.
7541 -- No runtime check needed in interface thunks since it is performed
7542 -- by the target primitive associated with the thunk.
7544 -- Note: accessibility check is skipped in the VM case, since there
7545 -- does not seem to be any practical way to implement this check.
7547 elsif Ada_Version
>= Ada_2005
7548 and then Tagged_Type_Expansion
7549 and then Is_Class_Wide_Type
(R_Type
)
7550 and then not Is_Thunk
(Current_Scope
)
7551 and then not Scope_Suppress
.Suppress
(Accessibility_Check
)
7553 (Is_Class_Wide_Type
(Etype
(Exp
))
7554 or else Nkind_In
(Exp
, N_Type_Conversion
,
7555 N_Unchecked_Type_Conversion
)
7556 or else (Is_Entity_Name
(Exp
)
7557 and then Ekind
(Entity
(Exp
)) in Formal_Kind
)
7558 or else Scope_Depth
(Enclosing_Dynamic_Scope
(Etype
(Exp
))) >
7559 Scope_Depth
(Enclosing_Dynamic_Scope
(Scope_Id
)))
7565 -- Ada 2005 (AI-251): In class-wide interface objects we displace
7566 -- "this" to reference the base of the object. This is required to
7567 -- get access to the TSD of the object.
7569 if Is_Class_Wide_Type
(Etype
(Exp
))
7570 and then Is_Interface
(Etype
(Exp
))
7571 and then Nkind
(Exp
) = N_Explicit_Dereference
7574 Make_Explicit_Dereference
(Loc
,
7576 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
7577 Make_Function_Call
(Loc
,
7579 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
7580 Parameter_Associations
=> New_List
(
7581 Unchecked_Convert_To
(RTE
(RE_Address
),
7582 Duplicate_Subexpr
(Prefix
(Exp
)))))));
7585 Make_Attribute_Reference
(Loc
,
7586 Prefix
=> Duplicate_Subexpr
(Exp
),
7587 Attribute_Name
=> Name_Tag
);
7591 Make_Raise_Program_Error
(Loc
,
7594 Left_Opnd
=> Build_Get_Access_Level
(Loc
, Tag_Node
),
7596 Make_Integer_Literal
(Loc
,
7597 Scope_Depth
(Enclosing_Dynamic_Scope
(Scope_Id
)))),
7598 Reason
=> PE_Accessibility_Check_Failed
));
7601 -- AI05-0073: If function has a controlling access result, check that
7602 -- the tag of the return value, if it is not null, matches designated
7603 -- type of return type.
7605 -- The return expression is referenced twice in the code below, so it
7606 -- must be made free of side effects. Given that different compilers
7607 -- may evaluate these parameters in different order, both occurrences
7610 elsif Ekind
(R_Type
) = E_Anonymous_Access_Type
7611 and then Has_Controlling_Result
(Scope_Id
)
7614 Make_Raise_Constraint_Error
(Loc
,
7619 Left_Opnd
=> Duplicate_Subexpr
(Exp
),
7620 Right_Opnd
=> Make_Null
(Loc
)),
7622 Right_Opnd
=> Make_Op_Ne
(Loc
,
7624 Make_Selected_Component
(Loc
,
7625 Prefix
=> Duplicate_Subexpr
(Exp
),
7626 Selector_Name
=> Make_Identifier
(Loc
, Name_uTag
)),
7629 Make_Attribute_Reference
(Loc
,
7631 New_Occurrence_Of
(Designated_Type
(R_Type
), Loc
),
7632 Attribute_Name
=> Name_Tag
))),
7634 Reason
=> CE_Tag_Check_Failed
),
7635 Suppress
=> All_Checks
);
7638 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
7639 -- ensure that the function result does not outlive an
7640 -- object designated by one of it discriminants.
7642 if Present
(Extra_Accessibility_Of_Result
(Scope_Id
))
7643 and then Has_Unconstrained_Access_Discriminants
(R_Type
)
7646 Discrim_Source
: Node_Id
;
7648 procedure Check_Against_Result_Level
(Level
: Node_Id
);
7649 -- Check the given accessibility level against the level
7650 -- determined by the point of call. (AI05-0234).
7652 --------------------------------
7653 -- Check_Against_Result_Level --
7654 --------------------------------
7656 procedure Check_Against_Result_Level
(Level
: Node_Id
) is
7659 Make_Raise_Program_Error
(Loc
,
7665 (Extra_Accessibility_Of_Result
(Scope_Id
), Loc
)),
7666 Reason
=> PE_Accessibility_Check_Failed
));
7667 end Check_Against_Result_Level
;
7670 Discrim_Source
:= Exp
;
7671 while Nkind
(Discrim_Source
) = N_Qualified_Expression
loop
7672 Discrim_Source
:= Expression
(Discrim_Source
);
7675 if Nkind
(Discrim_Source
) = N_Identifier
7676 and then Is_Return_Object
(Entity
(Discrim_Source
))
7678 Discrim_Source
:= Entity
(Discrim_Source
);
7680 if Is_Constrained
(Etype
(Discrim_Source
)) then
7681 Discrim_Source
:= Etype
(Discrim_Source
);
7683 Discrim_Source
:= Expression
(Parent
(Discrim_Source
));
7686 elsif Nkind
(Discrim_Source
) = N_Identifier
7687 and then Nkind_In
(Original_Node
(Discrim_Source
),
7688 N_Aggregate
, N_Extension_Aggregate
)
7690 Discrim_Source
:= Original_Node
(Discrim_Source
);
7692 elsif Nkind
(Discrim_Source
) = N_Explicit_Dereference
and then
7693 Nkind
(Original_Node
(Discrim_Source
)) = N_Function_Call
7695 Discrim_Source
:= Original_Node
(Discrim_Source
);
7698 while Nkind_In
(Discrim_Source
, N_Qualified_Expression
,
7700 N_Unchecked_Type_Conversion
)
7702 Discrim_Source
:= Expression
(Discrim_Source
);
7705 case Nkind
(Discrim_Source
) is
7706 when N_Defining_Identifier
=>
7708 pragma Assert
(Is_Composite_Type
(Discrim_Source
)
7709 and then Has_Discriminants
(Discrim_Source
)
7710 and then Is_Constrained
(Discrim_Source
));
7713 Discrim
: Entity_Id
:=
7714 First_Discriminant
(Base_Type
(R_Type
));
7715 Disc_Elmt
: Elmt_Id
:=
7716 First_Elmt
(Discriminant_Constraint
7720 if Ekind
(Etype
(Discrim
)) =
7721 E_Anonymous_Access_Type
7723 Check_Against_Result_Level
7724 (Dynamic_Accessibility_Level
(Node
(Disc_Elmt
)));
7727 Next_Elmt
(Disc_Elmt
);
7728 Next_Discriminant
(Discrim
);
7729 exit when not Present
(Discrim
);
7733 when N_Aggregate | N_Extension_Aggregate
=>
7735 -- Unimplemented: extension aggregate case where discrims
7736 -- come from ancestor part, not extension part.
7739 Discrim
: Entity_Id
:=
7740 First_Discriminant
(Base_Type
(R_Type
));
7742 Disc_Exp
: Node_Id
:= Empty
;
7744 Positionals_Exhausted
7745 : Boolean := not Present
(Expressions
7748 function Associated_Expr
7749 (Comp_Id
: Entity_Id
;
7750 Associations
: List_Id
) return Node_Id
;
7752 -- Given a component and a component associations list,
7753 -- locate the expression for that component; returns
7754 -- Empty if no such expression is found.
7756 ---------------------
7757 -- Associated_Expr --
7758 ---------------------
7760 function Associated_Expr
7761 (Comp_Id
: Entity_Id
;
7762 Associations
: List_Id
) return Node_Id
7768 -- Simple linear search seems ok here
7770 Assoc
:= First
(Associations
);
7771 while Present
(Assoc
) loop
7772 Choice
:= First
(Choices
(Assoc
));
7773 while Present
(Choice
) loop
7774 if (Nkind
(Choice
) = N_Identifier
7775 and then Chars
(Choice
) = Chars
(Comp_Id
))
7776 or else (Nkind
(Choice
) = N_Others_Choice
)
7778 return Expression
(Assoc
);
7788 end Associated_Expr
;
7790 -- Start of processing for Expand_Simple_Function_Return
7793 if not Positionals_Exhausted
then
7794 Disc_Exp
:= First
(Expressions
(Discrim_Source
));
7798 if Positionals_Exhausted
then
7802 Component_Associations
(Discrim_Source
));
7805 if Ekind
(Etype
(Discrim
)) =
7806 E_Anonymous_Access_Type
7808 Check_Against_Result_Level
7809 (Dynamic_Accessibility_Level
(Disc_Exp
));
7812 Next_Discriminant
(Discrim
);
7813 exit when not Present
(Discrim
);
7815 if not Positionals_Exhausted
then
7817 Positionals_Exhausted
:= not Present
(Disc_Exp
);
7822 when N_Function_Call
=>
7824 -- No check needed (check performed by callee)
7831 Level
: constant Node_Id
:=
7832 Make_Integer_Literal
(Loc
,
7833 Object_Access_Level
(Discrim_Source
));
7836 -- Unimplemented: check for name prefix that includes
7837 -- a dereference of an access value with a dynamic
7838 -- accessibility level (e.g., an access param or a
7839 -- saooaaat) and use dynamic level in that case. For
7841 -- return Access_Param.all(Some_Index).Some_Component;
7844 Set_Etype
(Level
, Standard_Natural
);
7845 Check_Against_Result_Level
(Level
);
7852 -- If we are returning an object that may not be bit-aligned, then copy
7853 -- the value into a temporary first. This copy may need to expand to a
7854 -- loop of component operations.
7856 if Is_Possibly_Unaligned_Slice
(Exp
)
7857 or else Is_Possibly_Unaligned_Object
(Exp
)
7860 ExpR
: constant Node_Id
:= Relocate_Node
(Exp
);
7861 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', ExpR
);
7864 Make_Object_Declaration
(Loc
,
7865 Defining_Identifier
=> Tnn
,
7866 Constant_Present
=> True,
7867 Object_Definition
=> New_Occurrence_Of
(R_Type
, Loc
),
7868 Expression
=> ExpR
),
7869 Suppress
=> All_Checks
);
7870 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
7874 -- Generate call to postcondition checks if they are present
7876 if Ekind
(Scope_Id
) = E_Function
7877 and then Has_Postconditions
(Scope_Id
)
7879 -- We are going to reference the returned value twice in this case,
7880 -- once in the call to _Postconditions, and once in the actual return
7881 -- statement, but we can't have side effects happening twice, and in
7882 -- any case for efficiency we don't want to do the computation twice.
7884 -- If the returned expression is an entity name, we don't need to
7885 -- worry since it is efficient and safe to reference it twice, that's
7886 -- also true for literals other than string literals, and for the
7887 -- case of X.all where X is an entity name.
7889 if Is_Entity_Name
(Exp
)
7890 or else Nkind_In
(Exp
, N_Character_Literal
,
7893 or else (Nkind
(Exp
) = N_Explicit_Dereference
7894 and then Is_Entity_Name
(Prefix
(Exp
)))
7898 -- Otherwise we are going to need a temporary to capture the value
7902 ExpR
: Node_Id
:= Relocate_Node
(Exp
);
7903 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', ExpR
);
7906 -- In the case of discriminated objects, we have created a
7907 -- constrained subtype above, and used the underlying type.
7908 -- This transformation is post-analysis and harmless, except
7909 -- that now the call to the post-condition will be analyzed and
7910 -- type kinds have to match.
7912 if Nkind
(ExpR
) = N_Unchecked_Type_Conversion
7914 Is_Private_Type
(R_Type
) /= Is_Private_Type
(Etype
(ExpR
))
7916 ExpR
:= Expression
(ExpR
);
7919 -- For a complex expression of an elementary type, capture
7920 -- value in the temporary and use it as the reference.
7922 if Is_Elementary_Type
(R_Type
) then
7924 Make_Object_Declaration
(Loc
,
7925 Defining_Identifier
=> Tnn
,
7926 Constant_Present
=> True,
7927 Object_Definition
=> New_Occurrence_Of
(R_Type
, Loc
),
7928 Expression
=> ExpR
),
7929 Suppress
=> All_Checks
);
7931 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
7933 -- If we have something we can rename, generate a renaming of
7934 -- the object and replace the expression with a reference
7936 elsif Is_Object_Reference
(Exp
) then
7938 Make_Object_Renaming_Declaration
(Loc
,
7939 Defining_Identifier
=> Tnn
,
7940 Subtype_Mark
=> New_Occurrence_Of
(R_Type
, Loc
),
7942 Suppress
=> All_Checks
);
7944 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
7946 -- Otherwise we have something like a string literal or an
7947 -- aggregate. We could copy the value, but that would be
7948 -- inefficient. Instead we make a reference to the value and
7949 -- capture this reference with a renaming, the expression is
7950 -- then replaced by a dereference of this renaming.
7953 -- For now, copy the value, since the code below does not
7954 -- seem to work correctly ???
7957 Make_Object_Declaration
(Loc
,
7958 Defining_Identifier
=> Tnn
,
7959 Constant_Present
=> True,
7960 Object_Definition
=> New_Occurrence_Of
(R_Type
, Loc
),
7961 Expression
=> Relocate_Node
(Exp
)),
7962 Suppress
=> All_Checks
);
7964 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
7966 -- Insert_Action (Exp,
7967 -- Make_Object_Renaming_Declaration (Loc,
7968 -- Defining_Identifier => Tnn,
7969 -- Access_Definition =>
7970 -- Make_Access_Definition (Loc,
7971 -- All_Present => True,
7972 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
7974 -- Make_Reference (Loc,
7975 -- Prefix => Relocate_Node (Exp))),
7976 -- Suppress => All_Checks);
7979 -- Make_Explicit_Dereference (Loc,
7980 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
7985 -- Generate call to _postconditions
7988 Make_Procedure_Call_Statement
(Loc
,
7989 Name
=> Make_Identifier
(Loc
, Name_uPostconditions
),
7990 Parameter_Associations
=> New_List
(Duplicate_Subexpr
(Exp
))));
7993 -- Ada 2005 (AI-251): If this return statement corresponds with an
7994 -- simple return statement associated with an extended return statement
7995 -- and the type of the returned object is an interface then generate an
7996 -- implicit conversion to force displacement of the "this" pointer.
7998 if Ada_Version
>= Ada_2005
7999 and then Comes_From_Extended_Return_Statement
(N
)
8000 and then Nkind
(Expression
(N
)) = N_Identifier
8001 and then Is_Interface
(Utyp
)
8002 and then Utyp
/= Underlying_Type
(Exptyp
)
8004 Rewrite
(Exp
, Convert_To
(Utyp
, Relocate_Node
(Exp
)));
8005 Analyze_And_Resolve
(Exp
);
8007 end Expand_Simple_Function_Return
;
8009 --------------------------------
8010 -- Expand_Subprogram_Contract --
8011 --------------------------------
8013 procedure Expand_Subprogram_Contract
8015 Spec_Id
: Entity_Id
;
8016 Body_Id
: Entity_Id
)
8018 procedure Add_Invariant_And_Predicate_Checks
8019 (Subp_Id
: Entity_Id
;
8020 Stmts
: in out List_Id
;
8021 Result
: out Node_Id
);
8022 -- Process the result of function Subp_Id (if applicable) and all its
8023 -- formals. Add invariant and predicate checks where applicable. The
8024 -- routine appends all the checks to list Stmts. If Subp_Id denotes a
8025 -- function, Result contains the entity of parameter _Result, to be
8026 -- used in the creation of procedure _Postconditions.
8028 procedure Append_Enabled_Item
(Item
: Node_Id
; List
: in out List_Id
);
8029 -- Append a node to a list. If there is no list, create a new one. When
8030 -- the item denotes a pragma, it is added to the list only when it is
8033 procedure Build_Postconditions_Procedure
8034 (Subp_Id
: Entity_Id
;
8036 Result
: Entity_Id
);
8037 -- Create the body of procedure _Postconditions which handles various
8038 -- assertion actions on exit from subprogram Subp_Id. Stmts is the list
8039 -- of statements to be checked on exit. Parameter Result is the entity
8040 -- of parameter _Result when Subp_Id denotes a function.
8042 function Build_Pragma_Check_Equivalent
8044 Subp_Id
: Entity_Id
:= Empty
;
8045 Inher_Id
: Entity_Id
:= Empty
) return Node_Id
;
8046 -- Transform a [refined] pre- or postcondition denoted by Prag into an
8047 -- equivalent pragma Check. When the pre- or postcondition is inherited,
8048 -- the routine corrects the references of all formals of Inher_Id to
8049 -- point to the formals of Subp_Id.
8051 procedure Collect_Body_Postconditions
(Stmts
: in out List_Id
);
8052 -- Process all postconditions found in the declarations of the body. The
8053 -- routine appends the pragma Check equivalents to list Stmts.
8055 procedure Collect_Spec_Postconditions
8056 (Subp_Id
: Entity_Id
;
8057 Stmts
: in out List_Id
);
8058 -- Process all [inherited] postconditions of subprogram spec Subp_Id.
8059 -- The routine appends the pragma Check equivalents to list Stmts.
8061 procedure Collect_Spec_Preconditions
(Subp_Id
: Entity_Id
);
8062 -- Process all [inherited] preconditions of subprogram spec Subp_Id. The
8063 -- routine prepends the pragma Check equivalents to the declarations of
8066 procedure Prepend_To_Declarations
(Item
: Node_Id
);
8067 -- Prepend a single item to the declarations of the subprogram body
8069 procedure Process_Contract_Cases
8070 (Subp_Id
: Entity_Id
;
8071 Stmts
: in out List_Id
);
8072 -- Process pragma Contract_Cases of subprogram spec Subp_Id. The routine
8073 -- appends the expanded code to list Stmts.
8075 ----------------------------------------
8076 -- Add_Invariant_And_Predicate_Checks --
8077 ----------------------------------------
8079 procedure Add_Invariant_And_Predicate_Checks
8080 (Subp_Id
: Entity_Id
;
8081 Stmts
: in out List_Id
;
8082 Result
: out Node_Id
)
8084 procedure Add_Invariant_Access_Checks
(Id
: Entity_Id
);
8085 -- Id denotes the return value of a function or a formal parameter.
8086 -- Add an invariant check if the type of Id is access to a type with
8087 -- invariants. The routine appends the generated code to Stmts.
8089 function Invariant_Checks_OK
(Typ
: Entity_Id
) return Boolean;
8090 -- Determine whether type Typ can benefit from invariant checks. To
8091 -- qualify, the type must have a non-null invariant procedure and
8092 -- subprogram Subp_Id must appear visible from the point of view of
8095 function Predicate_Checks_OK
(Typ
: Entity_Id
) return Boolean;
8096 -- Determine whether type Typ can benefit from predicate checks. To
8097 -- qualify, the type must have at least one checked predicate.
8099 ---------------------------------
8100 -- Add_Invariant_Access_Checks --
8101 ---------------------------------
8103 procedure Add_Invariant_Access_Checks
(Id
: Entity_Id
) is
8104 Loc
: constant Source_Ptr
:= Sloc
(N
);
8111 if Is_Access_Type
(Typ
) and then not Is_Access_Constant
(Typ
) then
8112 Typ
:= Designated_Type
(Typ
);
8114 if Invariant_Checks_OK
(Typ
) then
8116 Make_Explicit_Dereference
(Loc
,
8117 Prefix
=> New_Occurrence_Of
(Id
, Loc
));
8118 Set_Etype
(Ref
, Typ
);
8121 -- if <Id> /= null then
8122 -- <invariant_call (<Ref>)>
8127 Make_If_Statement
(Loc
,
8130 Left_Opnd
=> New_Occurrence_Of
(Id
, Loc
),
8131 Right_Opnd
=> Make_Null
(Loc
)),
8132 Then_Statements
=> New_List
(
8133 Make_Invariant_Call
(Ref
))),
8137 end Add_Invariant_Access_Checks
;
8139 -------------------------
8140 -- Invariant_Checks_OK --
8141 -------------------------
8143 function Invariant_Checks_OK
(Typ
: Entity_Id
) return Boolean is
8144 function Has_Null_Body
(Proc_Id
: Entity_Id
) return Boolean;
8145 -- Determine whether the body of procedure Proc_Id contains a sole
8146 -- null statement, possibly followed by an optional return.
8148 function Has_Public_Visibility_Of_Subprogram
return Boolean;
8149 -- Determine whether type Typ has public visibility of subprogram
8156 function Has_Null_Body
(Proc_Id
: Entity_Id
) return Boolean is
8157 Body_Id
: Entity_Id
;
8164 Spec
:= Parent
(Proc_Id
);
8165 Decl
:= Parent
(Spec
);
8167 -- Retrieve the entity of the invariant procedure body
8169 if Nkind
(Spec
) = N_Procedure_Specification
8170 and then Nkind
(Decl
) = N_Subprogram_Declaration
8172 Body_Id
:= Corresponding_Body
(Decl
);
8174 -- The body acts as a spec
8180 -- The body will be generated later
8182 if No
(Body_Id
) then
8186 Spec
:= Parent
(Body_Id
);
8187 Decl
:= Parent
(Spec
);
8190 (Nkind
(Spec
) = N_Procedure_Specification
8191 and then Nkind
(Decl
) = N_Subprogram_Body
);
8193 Stmt1
:= First
(Statements
(Handled_Statement_Sequence
(Decl
)));
8195 -- Look for a null statement followed by an optional return
8198 if Nkind
(Stmt1
) = N_Null_Statement
then
8199 Stmt2
:= Next
(Stmt1
);
8201 if Present
(Stmt2
) then
8202 return Nkind
(Stmt2
) = N_Simple_Return_Statement
;
8211 -----------------------------------------
8212 -- Has_Public_Visibility_Of_Subprogram --
8213 -----------------------------------------
8215 function Has_Public_Visibility_Of_Subprogram
return Boolean is
8216 Subp_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Subp_Id
);
8219 -- An Initialization procedure must be considered visible even
8220 -- though it is internally generated.
8222 if Is_Init_Proc
(Defining_Entity
(Subp_Decl
)) then
8225 elsif Ekind
(Scope
(Typ
)) /= E_Package
then
8228 -- Internally generated code is never publicly visible except
8229 -- for a subprogram that is the implementation of an expression
8230 -- function. In that case the visibility is determined by the
8233 elsif not Comes_From_Source
(Subp_Decl
)
8235 (Nkind
(Original_Node
(Subp_Decl
)) /= N_Expression_Function
8237 Comes_From_Source
(Defining_Entity
(Subp_Decl
)))
8241 -- Determine whether the subprogram is declared in the visible
8242 -- declarations of the package containing the type.
8245 return List_Containing
(Subp_Decl
) =
8246 Visible_Declarations
8247 (Specification
(Unit_Declaration_Node
(Scope
(Typ
))));
8249 end Has_Public_Visibility_Of_Subprogram
;
8251 -- Start of processing for Invariant_Checks_OK
8255 Has_Invariants
(Typ
)
8256 and then Present
(Invariant_Procedure
(Typ
))
8257 and then not Has_Null_Body
(Invariant_Procedure
(Typ
))
8258 and then Has_Public_Visibility_Of_Subprogram
;
8259 end Invariant_Checks_OK
;
8261 -------------------------
8262 -- Predicate_Checks_OK --
8263 -------------------------
8265 function Predicate_Checks_OK
(Typ
: Entity_Id
) return Boolean is
8266 function Has_Checked_Predicate
return Boolean;
8267 -- Determine whether type Typ has or inherits at least one
8268 -- predicate aspect or pragma, for which the applicable policy is
8271 ---------------------------
8272 -- Has_Checked_Predicate --
8273 ---------------------------
8275 function Has_Checked_Predicate
return Boolean is
8280 -- Climb the ancestor type chain staring from the input. This
8281 -- is done because the input type may lack aspect/pragma
8282 -- predicate and simply inherit those from its ancestor.
8284 -- Note that predicate pragmas correspond to all three cases
8285 -- of predicate aspects (Predicate, Dynamic_Predicate, and
8286 -- Static_Predicate), so this routine checks for all three
8290 while Present
(Anc
) loop
8291 Pred
:= Get_Pragma
(Anc
, Pragma_Predicate
);
8293 if Present
(Pred
) and then not Is_Ignored
(Pred
) then
8297 Anc
:= Nearest_Ancestor
(Anc
);
8301 end Has_Checked_Predicate
;
8303 -- Start of processing for Predicate_Checks_OK
8307 Has_Predicates
(Typ
)
8308 and then Present
(Predicate_Function
(Typ
))
8309 and then Has_Checked_Predicate
;
8310 end Predicate_Checks_OK
;
8314 Loc
: constant Source_Ptr
:= Sloc
(N
);
8318 -- Start of processing for Add_Invariant_And_Predicate_Checks
8323 -- Do not generate any checks if no code is being generated
8325 if not Expander_Active
then
8329 -- Process the result of a function
8331 if Ekind_In
(Subp_Id
, E_Function
, E_Generic_Function
) then
8332 Typ
:= Etype
(Subp_Id
);
8334 -- Generate _Result which is used in procedure _Postconditions to
8335 -- verify the return value.
8337 Result
:= Make_Defining_Identifier
(Loc
, Name_uResult
);
8338 Set_Etype
(Result
, Typ
);
8340 -- Add an invariant check when the return type has invariants and
8341 -- the related function is visible to the outside.
8343 if Invariant_Checks_OK
(Typ
) then
8346 Make_Invariant_Call
(New_Occurrence_Of
(Result
, Loc
)),
8350 -- Add an invariant check when the return type is an access to a
8351 -- type with invariants.
8353 Add_Invariant_Access_Checks
(Result
);
8356 -- Add invariant and predicates for all formals that qualify
8358 Formal
:= First_Formal
(Subp_Id
);
8359 while Present
(Formal
) loop
8360 Typ
:= Etype
(Formal
);
8362 if Ekind
(Formal
) /= E_In_Parameter
8363 or else Is_Access_Type
(Typ
)
8365 if Invariant_Checks_OK
(Typ
) then
8368 Make_Invariant_Call
(New_Occurrence_Of
(Formal
, Loc
)),
8372 Add_Invariant_Access_Checks
(Formal
);
8374 if Predicate_Checks_OK
(Typ
) then
8377 Make_Predicate_Check
8378 (Typ
, New_Occurrence_Of
(Formal
, Loc
)),
8383 Next_Formal
(Formal
);
8385 end Add_Invariant_And_Predicate_Checks
;
8387 -------------------------
8388 -- Append_Enabled_Item --
8389 -------------------------
8391 procedure Append_Enabled_Item
(Item
: Node_Id
; List
: in out List_Id
) is
8393 -- Do not chain ignored or disabled pragmas
8395 if Nkind
(Item
) = N_Pragma
8396 and then (Is_Ignored
(Item
) or else Is_Disabled
(Item
))
8400 -- Otherwise, add the item
8407 -- If the pragma is a conjunct in a composite postcondition, it
8408 -- has been processed in reverse order. In the postcondition body
8409 -- if must appear before the others.
8411 if Nkind
(Item
) = N_Pragma
8412 and then From_Aspect_Specification
(Item
)
8413 and then Split_PPC
(Item
)
8415 Prepend
(Item
, List
);
8417 Append
(Item
, List
);
8420 end Append_Enabled_Item
;
8422 ------------------------------------
8423 -- Build_Postconditions_Procedure --
8424 ------------------------------------
8426 procedure Build_Postconditions_Procedure
8427 (Subp_Id
: Entity_Id
;
8431 procedure Insert_Before_First_Source_Declaration
(Stmt
: Node_Id
);
8432 -- Insert node Stmt before the first source declaration of the
8433 -- related subprogram's body. If no such declaration exists, Stmt
8434 -- becomes the last declaration.
8436 --------------------------------------------
8437 -- Insert_Before_First_Source_Declaration --
8438 --------------------------------------------
8440 procedure Insert_Before_First_Source_Declaration
(Stmt
: Node_Id
) is
8441 Decls
: constant List_Id
:= Declarations
(N
);
8445 -- Inspect the declarations of the related subprogram body looking
8446 -- for the first source declaration.
8448 if Present
(Decls
) then
8449 Decl
:= First
(Decls
);
8450 while Present
(Decl
) loop
8451 if Comes_From_Source
(Decl
) then
8452 Insert_Before
(Decl
, Stmt
);
8459 -- If we get there, then the subprogram body lacks any source
8460 -- declarations. The body of _Postconditions now acts as the
8461 -- last declaration.
8463 Append
(Stmt
, Decls
);
8465 -- Ensure that the body has a declaration list
8468 Set_Declarations
(N
, New_List
(Stmt
));
8470 end Insert_Before_First_Source_Declaration
;
8474 Loc
: constant Source_Ptr
:= Sloc
(N
);
8475 Params
: List_Id
:= No_List
;
8476 Proc_Id
: Entity_Id
;
8478 -- Start of processing for Build_Postconditions_Procedure
8481 -- Do not create the routine if no code is being generated
8483 if not Expander_Active
then
8486 -- Nothing to do if there are no actions to check on exit
8488 elsif No
(Stmts
) then
8492 Proc_Id
:= Make_Defining_Identifier
(Loc
, Name_uPostconditions
);
8494 -- The related subprogram is a function, create the specification of
8495 -- parameter _Result.
8497 if Present
(Result
) then
8498 Params
:= New_List
(
8499 Make_Parameter_Specification
(Loc
,
8500 Defining_Identifier
=> Result
,
8502 New_Occurrence_Of
(Etype
(Result
), Loc
)));
8505 -- Insert _Postconditions before the first source declaration of the
8506 -- body. This ensures that the body will not cause any premature
8507 -- freezing as it may mention types:
8509 -- procedure Proc (Obj : Array_Typ) is
8510 -- procedure _postconditions is
8513 -- end _postconditions;
8515 -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1));
8518 -- In the example above, Obj is of type T but the incorrect placement
8519 -- of _Postconditions will cause a crash in gigi due to an out of
8520 -- order reference. The body of _Postconditions must be placed after
8521 -- the declaration of Temp to preserve correct visibility.
8523 Insert_Before_First_Source_Declaration
(
8524 Make_Subprogram_Body
(Loc
,
8526 Make_Procedure_Specification
(Loc
,
8527 Defining_Unit_Name
=> Proc_Id
,
8528 Parameter_Specifications
=> Params
),
8530 Declarations
=> Empty_List
,
8531 Handled_Statement_Sequence
=>
8532 Make_Handled_Sequence_Of_Statements
(Loc
, Stmts
)));
8534 -- Set the attributes of the related subprogram to capture the
8535 -- generated procedure.
8537 if Ekind_In
(Subp_Id
, E_Generic_Procedure
, E_Procedure
) then
8538 Set_Postcondition_Proc
(Subp_Id
, Proc_Id
);
8541 Set_Has_Postconditions
(Subp_Id
);
8542 end Build_Postconditions_Procedure
;
8544 -----------------------------------
8545 -- Build_Pragma_Check_Equivalent --
8546 -----------------------------------
8548 function Build_Pragma_Check_Equivalent
8550 Subp_Id
: Entity_Id
:= Empty
;
8551 Inher_Id
: Entity_Id
:= Empty
) return Node_Id
8553 Loc
: constant Source_Ptr
:= Sloc
(Prag
);
8554 Prag_Nam
: constant Name_Id
:= Pragma_Name
(Prag
);
8555 Check_Prag
: Node_Id
;
8556 Formals_Map
: Elist_Id
;
8557 Inher_Formal
: Entity_Id
;
8560 Subp_Formal
: Entity_Id
;
8563 Formals_Map
:= No_Elist
;
8565 -- When the pre- or postcondition is inherited, map the formals of
8566 -- the inherited subprogram to those of the current subprogram.
8568 if Present
(Inher_Id
) then
8569 pragma Assert
(Present
(Subp_Id
));
8571 Formals_Map
:= New_Elmt_List
;
8573 -- Create a relation <inherited formal> => <subprogram formal>
8575 Inher_Formal
:= First_Formal
(Inher_Id
);
8576 Subp_Formal
:= First_Formal
(Subp_Id
);
8577 while Present
(Inher_Formal
) and then Present
(Subp_Formal
) loop
8578 Append_Elmt
(Inher_Formal
, Formals_Map
);
8579 Append_Elmt
(Subp_Formal
, Formals_Map
);
8581 Next_Formal
(Inher_Formal
);
8582 Next_Formal
(Subp_Formal
);
8586 -- Copy the original pragma while performing substitutions (if
8593 New_Scope
=> Current_Scope
);
8595 -- Mark the pragma as being internally generated and reset the
8598 Set_Comes_From_Source
(Check_Prag
, False);
8599 Set_Analyzed
(Check_Prag
, False);
8601 -- For a postcondition pragma within a generic, preserve the pragma
8602 -- for later expansion. This is also used when an error was detected,
8603 -- thus setting Expander_Active to False.
8605 if Prag_Nam
= Name_Postcondition
and then not Expander_Active
then
8609 if Present
(Corresponding_Aspect
(Prag
)) then
8610 Nam
:= Chars
(Identifier
(Corresponding_Aspect
(Prag
)));
8615 -- Convert the copy into pragma Check by correcting the name and
8616 -- adding a check_kind argument.
8618 Set_Pragma_Identifier
8619 (Check_Prag
, Make_Identifier
(Loc
, Name_Check
));
8621 Prepend_To
(Pragma_Argument_Associations
(Check_Prag
),
8622 Make_Pragma_Argument_Association
(Loc
,
8623 Expression
=> Make_Identifier
(Loc
, Nam
)));
8625 -- Update the error message when the pragma is inherited
8627 if Present
(Inher_Id
) then
8628 Msg_Arg
:= Last
(Pragma_Argument_Associations
(Check_Prag
));
8630 if Chars
(Msg_Arg
) = Name_Message
then
8631 String_To_Name_Buffer
(Strval
(Expression
(Msg_Arg
)));
8633 -- Insert "inherited" to improve the error message
8635 if Name_Buffer
(1 .. 8) = "failed p" then
8636 Insert_Str_In_Name_Buffer
("inherited ", 8);
8637 Set_Strval
(Expression
(Msg_Arg
), String_From_Name_Buffer
);
8643 end Build_Pragma_Check_Equivalent
;
8645 ---------------------------------
8646 -- Collect_Body_Postconditions --
8647 ---------------------------------
8649 procedure Collect_Body_Postconditions
(Stmts
: in out List_Id
) is
8650 procedure Collect_Body_Postconditions_Of_Kind
(Post_Nam
: Name_Id
);
8651 -- Process all postconditions of the kind denoted by Post_Nam
8653 -----------------------------------------
8654 -- Collect_Body_Postconditions_Of_Kind --
8655 -----------------------------------------
8657 procedure Collect_Body_Postconditions_Of_Kind
(Post_Nam
: Name_Id
) is
8658 procedure Collect_Body_Postconditions_In_Decls
8659 (First_Decl
: Node_Id
);
8660 -- Process all postconditions found in a declarative list starting
8661 -- with declaration First_Decl.
8663 ------------------------------------------
8664 -- Collect_Body_Postconditions_In_Decls --
8665 ------------------------------------------
8667 procedure Collect_Body_Postconditions_In_Decls
8668 (First_Decl
: Node_Id
)
8670 Check_Prag
: Node_Id
;
8674 -- Inspect the declarative list looking for a pragma that
8675 -- matches the desired name.
8678 while Present
(Decl
) loop
8680 -- Note that non-matching pragmas are skipped
8682 if Nkind
(Decl
) = N_Pragma
then
8683 if Pragma_Name
(Decl
) = Post_Nam
then
8684 if not Analyzed
(Decl
) then
8688 Check_Prag
:= Build_Pragma_Check_Equivalent
(Decl
);
8690 if Expander_Active
then
8692 (Item
=> Check_Prag
,
8695 -- If analyzing a generic unit, save pragma for later
8698 Prepend_To_Declarations
(Check_Prag
);
8702 -- Skip internally generated code
8704 elsif not Comes_From_Source
(Decl
) then
8707 -- Postcondition pragmas are usually grouped together. There
8708 -- is no need to inspect the whole declarative list.
8716 end Collect_Body_Postconditions_In_Decls
;
8720 Unit_Decl
: constant Node_Id
:= Parent
(N
);
8722 -- Start of processing for Collect_Body_Postconditions_Of_Kind
8725 pragma Assert
(Nam_In
(Post_Nam
, Name_Postcondition
,
8726 Name_Refined_Post
));
8728 -- Inspect the declarations of the subprogram body looking for a
8729 -- pragma that matches the desired name.
8731 Collect_Body_Postconditions_In_Decls
8732 (First_Decl
=> First
(Declarations
(N
)));
8734 -- The subprogram body being processed is actually the proper body
8735 -- of a stub with a corresponding spec. The subprogram stub may
8736 -- carry a postcondition pragma in which case it must be taken
8737 -- into account. The pragma appears after the stub.
8739 if Present
(Spec_Id
) and then Nkind
(Unit_Decl
) = N_Subunit
then
8740 Collect_Body_Postconditions_In_Decls
8741 (First_Decl
=> Next
(Corresponding_Stub
(Unit_Decl
)));
8743 end Collect_Body_Postconditions_Of_Kind
;
8745 -- Start of processing for Collect_Body_Postconditions
8748 Collect_Body_Postconditions_Of_Kind
(Name_Refined_Post
);
8749 Collect_Body_Postconditions_Of_Kind
(Name_Postcondition
);
8750 end Collect_Body_Postconditions
;
8752 ---------------------------------
8753 -- Collect_Spec_Postconditions --
8754 ---------------------------------
8756 procedure Collect_Spec_Postconditions
8757 (Subp_Id
: Entity_Id
;
8758 Stmts
: in out List_Id
)
8760 Inher_Subps
: constant Subprogram_List
:=
8761 Inherited_Subprograms
(Subp_Id
);
8762 Check_Prag
: Node_Id
;
8764 Inher_Subp_Id
: Entity_Id
;
8767 -- Process the contract of the spec
8769 Prag
:= Pre_Post_Conditions
(Contract
(Subp_Id
));
8770 while Present
(Prag
) loop
8771 if Pragma_Name
(Prag
) = Name_Postcondition
then
8772 Check_Prag
:= Build_Pragma_Check_Equivalent
(Prag
);
8774 if Expander_Active
then
8776 (Item
=> Check_Prag
,
8779 -- When analyzing a generic unit, save the pragma for later
8782 Prepend_To_Declarations
(Check_Prag
);
8786 Prag
:= Next_Pragma
(Prag
);
8789 -- Process the contracts of all inherited subprograms, looking for
8790 -- class-wide postconditions.
8792 for Index
in Inher_Subps
'Range loop
8793 Inher_Subp_Id
:= Inher_Subps
(Index
);
8795 Prag
:= Pre_Post_Conditions
(Contract
(Inher_Subp_Id
));
8796 while Present
(Prag
) loop
8797 if Pragma_Name
(Prag
) = Name_Postcondition
8798 and then Class_Present
(Prag
)
8801 Build_Pragma_Check_Equivalent
8804 Inher_Id
=> Inher_Subp_Id
);
8806 if Expander_Active
then
8808 (Item
=> Check_Prag
,
8811 -- When analyzing a generic unit, save the pragma for later
8814 Prepend_To_Declarations
(Check_Prag
);
8818 Prag
:= Next_Pragma
(Prag
);
8821 end Collect_Spec_Postconditions
;
8823 --------------------------------
8824 -- Collect_Spec_Preconditions --
8825 --------------------------------
8827 procedure Collect_Spec_Preconditions
(Subp_Id
: Entity_Id
) is
8828 Class_Pre
: Node_Id
:= Empty
;
8829 -- The sole class-wide precondition pragma that applies to the
8832 procedure Add_Or_Save_Precondition
(Prag
: Node_Id
);
8833 -- Save a class-wide precondition or add a regulat precondition to
8834 -- the declarative list of the body.
8836 procedure Merge_Preconditions
(From
: Node_Id
; Into
: Node_Id
);
8837 -- Merge two class-wide preconditions by "or else"-ing them. The
8838 -- changes are accumulated in parameter Into. Update the error
8841 ------------------------------
8842 -- Add_Or_Save_Precondition --
8843 ------------------------------
8845 procedure Add_Or_Save_Precondition
(Prag
: Node_Id
) is
8846 Check_Prag
: Node_Id
;
8849 Check_Prag
:= Build_Pragma_Check_Equivalent
(Prag
);
8851 -- Save the sole class-wide precondition (if any) for the next
8852 -- step where it will be merged with inherited preconditions.
8854 if Class_Present
(Prag
) then
8855 pragma Assert
(No
(Class_Pre
));
8856 Class_Pre
:= Check_Prag
;
8858 -- Accumulate the corresponding Check pragmas to the top of the
8859 -- declarations. Prepending the items ensures that they will be
8860 -- evaluated in their original order.
8863 Prepend_To_Declarations
(Check_Prag
);
8865 end Add_Or_Save_Precondition
;
8867 -------------------------
8868 -- Merge_Preconditions --
8869 -------------------------
8871 procedure Merge_Preconditions
(From
: Node_Id
; Into
: Node_Id
) is
8872 function Expression_Arg
(Prag
: Node_Id
) return Node_Id
;
8873 -- Return the boolean expression argument of a precondition while
8874 -- updating its parenteses count for the subsequent merge.
8876 function Message_Arg
(Prag
: Node_Id
) return Node_Id
;
8877 -- Return the message argument of a precondition
8879 --------------------
8880 -- Expression_Arg --
8881 --------------------
8883 function Expression_Arg
(Prag
: Node_Id
) return Node_Id
is
8884 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
8885 Arg
: constant Node_Id
:= Get_Pragma_Arg
(Next
(First
(Args
)));
8888 if Paren_Count
(Arg
) = 0 then
8889 Set_Paren_Count
(Arg
, 1);
8899 function Message_Arg
(Prag
: Node_Id
) return Node_Id
is
8900 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
8902 return Get_Pragma_Arg
(Last
(Args
));
8907 From_Expr
: constant Node_Id
:= Expression_Arg
(From
);
8908 From_Msg
: constant Node_Id
:= Message_Arg
(From
);
8909 Into_Expr
: constant Node_Id
:= Expression_Arg
(Into
);
8910 Into_Msg
: constant Node_Id
:= Message_Arg
(Into
);
8911 Loc
: constant Source_Ptr
:= Sloc
(Into
);
8913 -- Start of processing for Merge_Preconditions
8916 -- Merge the two preconditions by "or else"-ing them
8920 Right_Opnd
=> Relocate_Node
(Into_Expr
),
8921 Left_Opnd
=> From_Expr
));
8923 -- Merge the two error messages to produce a single message of the
8926 -- failed precondition from ...
8927 -- also failed inherited precondition from ...
8929 if not Exception_Locations_Suppressed
then
8930 Start_String
(Strval
(Into_Msg
));
8931 Store_String_Char
(ASCII
.LF
);
8932 Store_String_Chars
(" also ");
8933 Store_String_Chars
(Strval
(From_Msg
));
8935 Set_Strval
(Into_Msg
, End_String
);
8937 end Merge_Preconditions
;
8941 Inher_Subps
: constant Subprogram_List
:=
8942 Inherited_Subprograms
(Subp_Id
);
8943 Subp_Decl
: constant Node_Id
:= Parent
(Parent
(Subp_Id
));
8944 Check_Prag
: Node_Id
;
8946 Inher_Subp_Id
: Entity_Id
;
8949 -- Start of processing for Collect_Spec_Preconditions
8952 -- Process the contract of the spec
8954 Prag
:= Pre_Post_Conditions
(Contract
(Subp_Id
));
8955 while Present
(Prag
) loop
8956 if Pragma_Name
(Prag
) = Name_Precondition
then
8957 Add_Or_Save_Precondition
(Prag
);
8960 Prag
:= Next_Pragma
(Prag
);
8963 -- The subprogram declaration being processed is actually a body
8964 -- stub. The stub may carry a precondition pragma in which case it
8965 -- must be taken into account. The pragma appears after the stub.
8967 if Nkind
(Subp_Decl
) = N_Subprogram_Body_Stub
then
8969 -- Inspect the declarations following the body stub
8971 Decl
:= Next
(Subp_Decl
);
8972 while Present
(Decl
) loop
8974 -- Note that non-matching pragmas are skipped
8976 if Nkind
(Decl
) = N_Pragma
then
8977 if Pragma_Name
(Decl
) = Name_Precondition
then
8978 if not Analyzed
(Decl
) then
8982 Add_Or_Save_Precondition
(Decl
);
8985 -- Skip internally generated code
8987 elsif not Comes_From_Source
(Decl
) then
8990 -- Preconditions are usually grouped together. There is no need
8991 -- to inspect the whole declarative list.
9001 -- Process the contracts of all inherited subprograms, looking for
9002 -- class-wide preconditions.
9004 for Index
in Inher_Subps
'Range loop
9005 Inher_Subp_Id
:= Inher_Subps
(Index
);
9007 Prag
:= Pre_Post_Conditions
(Contract
(Inher_Subp_Id
));
9008 while Present
(Prag
) loop
9009 if Pragma_Name
(Prag
) = Name_Precondition
9010 and then Class_Present
(Prag
)
9013 Build_Pragma_Check_Equivalent
9016 Inher_Id
=> Inher_Subp_Id
);
9018 -- The spec or an inherited subprogram already yielded a
9019 -- class-wide precondition. Merge the existing precondition
9020 -- with the current one using "or else".
9022 if Present
(Class_Pre
) then
9023 Merge_Preconditions
(Check_Prag
, Class_Pre
);
9025 Class_Pre
:= Check_Prag
;
9029 Prag
:= Next_Pragma
(Prag
);
9033 -- Add the merged class-wide preconditions (if any)
9035 if Present
(Class_Pre
) then
9036 Prepend_To_Declarations
(Class_Pre
);
9038 end Collect_Spec_Preconditions
;
9040 -----------------------------
9041 -- Prepend_To_Declarations --
9042 -----------------------------
9044 procedure Prepend_To_Declarations
(Item
: Node_Id
) is
9045 Decls
: List_Id
:= Declarations
(N
);
9048 -- Ensure that the body has a declarative list
9052 Set_Declarations
(N
, Decls
);
9055 Prepend_To
(Decls
, Item
);
9056 end Prepend_To_Declarations
;
9058 ----------------------------
9059 -- Process_Contract_Cases --
9060 ----------------------------
9062 procedure Process_Contract_Cases
9063 (Subp_Id
: Entity_Id
;
9064 Stmts
: in out List_Id
)
9069 -- Do not build the Contract_Cases circuitry if no code is being
9072 if not Expander_Active
then
9076 Prag
:= Contract_Test_Cases
(Contract
(Subp_Id
));
9077 while Present
(Prag
) loop
9078 if Pragma_Name
(Prag
) = Name_Contract_Cases
then
9079 Expand_Contract_Cases
9082 Decls
=> Declarations
(N
),
9086 Prag
:= Next_Pragma
(Prag
);
9088 end Process_Contract_Cases
;
9092 Post_Stmts
: List_Id
:= No_List
;
9094 Subp_Id
: Entity_Id
;
9096 -- Start of processing for Expand_Subprogram_Contract
9099 if Present
(Spec_Id
) then
9105 -- Do not process a predicate function as its body will end up with a
9106 -- recursive call to itself and blow up the stack.
9108 if Ekind
(Subp_Id
) = E_Function
9109 and then Is_Predicate_Function
(Subp_Id
)
9113 -- Do not process TSS subprograms
9115 elsif Get_TSS_Name
(Subp_Id
) /= TSS_Null
then
9119 -- The expansion of a subprogram contract involves the relocation of
9120 -- various contract assertions to the declarations of the body in a
9121 -- particular order. The order is as follows:
9123 -- function Example (...) return ... is
9124 -- procedure _Postconditions (...) is
9126 -- <refined postconditions from body>
9127 -- <postconditions from body>
9128 -- <postconditions from spec>
9129 -- <inherited postconditions>
9130 -- <contract case consequences>
9131 -- <invariant check of function result (if applicable)>
9132 -- <invariant and predicate checks of parameters>
9133 -- end _Postconditions;
9135 -- <inherited preconditions>
9136 -- <preconditions from spec>
9137 -- <preconditions from body>
9138 -- <refined preconditions from body>
9139 -- <contract case conditions>
9141 -- <source declarations>
9143 -- <source statements>
9145 -- _Preconditions (Result);
9149 -- Routine _Postconditions holds all contract assertions that must be
9150 -- verified on exit from the related routine.
9152 -- Collect all [inherited] preconditions from the spec, transform them
9153 -- into Check pragmas and add them to the declarations of the body in
9154 -- the order outlined above.
9156 if Present
(Spec_Id
) then
9157 Collect_Spec_Preconditions
(Spec_Id
);
9160 -- Transform all [refined] postconditions of the body into Check
9161 -- pragmas. The resulting pragmas are accumulated in list Post_Stmts.
9163 Collect_Body_Postconditions
(Post_Stmts
);
9165 -- Transform all [inherited] postconditions from the spec into Check
9166 -- pragmas. The resulting pragmas are accumulated in list Post_Stmts.
9168 if Present
(Spec_Id
) then
9169 Collect_Spec_Postconditions
(Spec_Id
, Post_Stmts
);
9171 -- Transform pragma Contract_Cases from the spec into its circuitry
9173 Process_Contract_Cases
(Spec_Id
, Post_Stmts
);
9176 -- Apply invariant and predicate checks on the result of a function (if
9177 -- applicable) and all formals. The resulting checks are accumulated in
9180 Add_Invariant_And_Predicate_Checks
(Subp_Id
, Post_Stmts
, Result
);
9182 -- Construct procedure _Postconditions
9184 Build_Postconditions_Procedure
(Subp_Id
, Post_Stmts
, Result
);
9185 end Expand_Subprogram_Contract
;
9187 --------------------------------
9188 -- Is_Build_In_Place_Function --
9189 --------------------------------
9191 function Is_Build_In_Place_Function
(E
: Entity_Id
) return Boolean is
9193 -- This function is called from Expand_Subtype_From_Expr during
9194 -- semantic analysis, even when expansion is off. In those cases
9195 -- the build_in_place expansion will not take place.
9197 if not Expander_Active
then
9201 -- For now we test whether E denotes a function or access-to-function
9202 -- type whose result subtype is inherently limited. Later this test
9203 -- may be revised to allow composite nonlimited types. Functions with
9204 -- a foreign convention or whose result type has a foreign convention
9207 if Ekind_In
(E
, E_Function
, E_Generic_Function
)
9208 or else (Ekind
(E
) = E_Subprogram_Type
9209 and then Etype
(E
) /= Standard_Void_Type
)
9211 -- Note: If the function has a foreign convention, it cannot build
9212 -- its result in place, so you're on your own. On the other hand,
9213 -- if only the return type has a foreign convention, its layout is
9214 -- intended to be compatible with the other language, but the build-
9215 -- in place machinery can ensure that the object is not copied.
9217 if Has_Foreign_Convention
(E
) then
9220 -- In Ada 2005 all functions with an inherently limited return type
9221 -- must be handled using a build-in-place profile, including the case
9222 -- of a function with a limited interface result, where the function
9223 -- may return objects of nonlimited descendants.
9226 return Is_Limited_View
(Etype
(E
))
9227 and then Ada_Version
>= Ada_2005
9228 and then not Debug_Flag_Dot_L
;
9234 end Is_Build_In_Place_Function
;
9236 -------------------------------------
9237 -- Is_Build_In_Place_Function_Call --
9238 -------------------------------------
9240 function Is_Build_In_Place_Function_Call
(N
: Node_Id
) return Boolean is
9241 Exp_Node
: Node_Id
:= N
;
9242 Function_Id
: Entity_Id
;
9245 -- Return False if the expander is currently inactive, since awareness
9246 -- of build-in-place treatment is only relevant during expansion. Note
9247 -- that Is_Build_In_Place_Function, which is called as part of this
9248 -- function, is also conditioned this way, but we need to check here as
9249 -- well to avoid blowing up on processing protected calls when expansion
9250 -- is disabled (such as with -gnatc) since those would trip over the
9251 -- raise of Program_Error below.
9253 -- In SPARK mode, build-in-place calls are not expanded, so that we
9254 -- may end up with a call that is neither resolved to an entity, nor
9255 -- an indirect call.
9257 if not Expander_Active
then
9261 -- Step past qualification or unchecked conversion (the latter can occur
9262 -- in cases of calls to 'Input).
9264 if Nkind_In
(Exp_Node
, N_Qualified_Expression
,
9265 N_Unchecked_Type_Conversion
)
9267 Exp_Node
:= Expression
(N
);
9270 if Nkind
(Exp_Node
) /= N_Function_Call
then
9274 if Is_Entity_Name
(Name
(Exp_Node
)) then
9275 Function_Id
:= Entity
(Name
(Exp_Node
));
9277 -- In the case of an explicitly dereferenced call, use the subprogram
9278 -- type generated for the dereference.
9280 elsif Nkind
(Name
(Exp_Node
)) = N_Explicit_Dereference
then
9281 Function_Id
:= Etype
(Name
(Exp_Node
));
9283 -- This may be a call to a protected function.
9285 elsif Nkind
(Name
(Exp_Node
)) = N_Selected_Component
then
9286 Function_Id
:= Etype
(Entity
(Selector_Name
(Name
(Exp_Node
))));
9289 raise Program_Error
;
9292 return Is_Build_In_Place_Function
(Function_Id
);
9294 end Is_Build_In_Place_Function_Call
;
9296 -----------------------
9297 -- Freeze_Subprogram --
9298 -----------------------
9300 procedure Freeze_Subprogram
(N
: Node_Id
) is
9301 Loc
: constant Source_Ptr
:= Sloc
(N
);
9303 procedure Register_Predefined_DT_Entry
(Prim
: Entity_Id
);
9304 -- (Ada 2005): Register a predefined primitive in all the secondary
9305 -- dispatch tables of its primitive type.
9307 ----------------------------------
9308 -- Register_Predefined_DT_Entry --
9309 ----------------------------------
9311 procedure Register_Predefined_DT_Entry
(Prim
: Entity_Id
) is
9312 Iface_DT_Ptr
: Elmt_Id
;
9313 Tagged_Typ
: Entity_Id
;
9314 Thunk_Id
: Entity_Id
;
9315 Thunk_Code
: Node_Id
;
9318 Tagged_Typ
:= Find_Dispatching_Type
(Prim
);
9320 if No
(Access_Disp_Table
(Tagged_Typ
))
9321 or else not Has_Interfaces
(Tagged_Typ
)
9322 or else not RTE_Available
(RE_Interface_Tag
)
9323 or else Restriction_Active
(No_Dispatching_Calls
)
9328 -- Skip the first two access-to-dispatch-table pointers since they
9329 -- leads to the primary dispatch table (predefined DT and user
9330 -- defined DT). We are only concerned with the secondary dispatch
9331 -- table pointers. Note that the access-to- dispatch-table pointer
9332 -- corresponds to the first implemented interface retrieved below.
9335 Next_Elmt
(Next_Elmt
(First_Elmt
(Access_Disp_Table
(Tagged_Typ
))));
9337 while Present
(Iface_DT_Ptr
)
9338 and then Ekind
(Node
(Iface_DT_Ptr
)) = E_Constant
9340 pragma Assert
(Has_Thunks
(Node
(Iface_DT_Ptr
)));
9341 Expand_Interface_Thunk
(Prim
, Thunk_Id
, Thunk_Code
);
9343 if Present
(Thunk_Code
) then
9344 Insert_Actions_After
(N
, New_List
(
9347 Build_Set_Predefined_Prim_Op_Address
(Loc
,
9349 New_Occurrence_Of
(Node
(Next_Elmt
(Iface_DT_Ptr
)), Loc
),
9350 Position
=> DT_Position
(Prim
),
9352 Unchecked_Convert_To
(RTE
(RE_Prim_Ptr
),
9353 Make_Attribute_Reference
(Loc
,
9354 Prefix
=> New_Occurrence_Of
(Thunk_Id
, Loc
),
9355 Attribute_Name
=> Name_Unrestricted_Access
))),
9357 Build_Set_Predefined_Prim_Op_Address
(Loc
,
9360 (Node
(Next_Elmt
(Next_Elmt
(Next_Elmt
(Iface_DT_Ptr
)))),
9362 Position
=> DT_Position
(Prim
),
9364 Unchecked_Convert_To
(RTE
(RE_Prim_Ptr
),
9365 Make_Attribute_Reference
(Loc
,
9366 Prefix
=> New_Occurrence_Of
(Prim
, Loc
),
9367 Attribute_Name
=> Name_Unrestricted_Access
)))));
9370 -- Skip the tag of the predefined primitives dispatch table
9372 Next_Elmt
(Iface_DT_Ptr
);
9373 pragma Assert
(Has_Thunks
(Node
(Iface_DT_Ptr
)));
9375 -- Skip tag of the no-thunks dispatch table
9377 Next_Elmt
(Iface_DT_Ptr
);
9378 pragma Assert
(not Has_Thunks
(Node
(Iface_DT_Ptr
)));
9380 -- Skip tag of predefined primitives no-thunks dispatch table
9382 Next_Elmt
(Iface_DT_Ptr
);
9383 pragma Assert
(not Has_Thunks
(Node
(Iface_DT_Ptr
)));
9385 Next_Elmt
(Iface_DT_Ptr
);
9387 end Register_Predefined_DT_Entry
;
9391 Subp
: constant Entity_Id
:= Entity
(N
);
9393 -- Start of processing for Freeze_Subprogram
9396 -- We suppress the initialization of the dispatch table entry when
9397 -- VM_Target because the dispatching mechanism is handled internally
9400 if Is_Dispatching_Operation
(Subp
)
9401 and then not Is_Abstract_Subprogram
(Subp
)
9402 and then Present
(DTC_Entity
(Subp
))
9403 and then Present
(Scope
(DTC_Entity
(Subp
)))
9404 and then Tagged_Type_Expansion
9405 and then not Restriction_Active
(No_Dispatching_Calls
)
9406 and then RTE_Available
(RE_Tag
)
9409 Typ
: constant Entity_Id
:= Scope
(DTC_Entity
(Subp
));
9412 -- Handle private overridden primitives
9414 if not Is_CPP_Class
(Typ
) then
9415 Check_Overriding_Operation
(Subp
);
9418 -- We assume that imported CPP primitives correspond with objects
9419 -- whose constructor is in the CPP side; therefore we don't need
9420 -- to generate code to register them in the dispatch table.
9422 if Is_CPP_Class
(Typ
) then
9425 -- Handle CPP primitives found in derivations of CPP_Class types.
9426 -- These primitives must have been inherited from some parent, and
9427 -- there is no need to register them in the dispatch table because
9428 -- Build_Inherit_Prims takes care of initializing these slots.
9430 elsif Is_Imported
(Subp
)
9431 and then (Convention
(Subp
) = Convention_CPP
9432 or else Convention
(Subp
) = Convention_C
)
9436 -- Generate code to register the primitive in non statically
9437 -- allocated dispatch tables
9439 elsif not Building_Static_DT
(Scope
(DTC_Entity
(Subp
))) then
9441 -- When a primitive is frozen, enter its name in its dispatch
9444 if not Is_Interface
(Typ
)
9445 or else Present
(Interface_Alias
(Subp
))
9447 if Is_Predefined_Dispatching_Operation
(Subp
) then
9448 Register_Predefined_DT_Entry
(Subp
);
9451 Insert_Actions_After
(N
,
9452 Register_Primitive
(Loc
, Prim
=> Subp
));
9458 -- Mark functions that return by reference. Note that it cannot be part
9459 -- of the normal semantic analysis of the spec since the underlying
9460 -- returned type may not be known yet (for private types).
9463 Typ
: constant Entity_Id
:= Etype
(Subp
);
9464 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
9466 if Is_Limited_View
(Typ
) then
9467 Set_Returns_By_Ref
(Subp
);
9468 elsif Present
(Utyp
) and then CW_Or_Has_Controlled_Part
(Utyp
) then
9469 Set_Returns_By_Ref
(Subp
);
9473 -- Wnen freezing a null procedure, analyze its delayed aspects now
9474 -- because we may not have reached the end of the declarative list when
9475 -- delayed aspects are normally analyzed. This ensures that dispatching
9476 -- calls are properly rewritten when the generated _Postcondition
9477 -- procedure is analyzed in the null procedure body.
9479 if Nkind
(Parent
(Subp
)) = N_Procedure_Specification
9480 and then Null_Present
(Parent
(Subp
))
9482 Analyze_Subprogram_Contract
(Subp
);
9484 end Freeze_Subprogram
;
9486 -----------------------
9487 -- Is_Null_Procedure --
9488 -----------------------
9490 function Is_Null_Procedure
(Subp
: Entity_Id
) return Boolean is
9491 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
9494 if Ekind
(Subp
) /= E_Procedure
then
9497 -- Check if this is a declared null procedure
9499 elsif Nkind
(Decl
) = N_Subprogram_Declaration
then
9500 if not Null_Present
(Specification
(Decl
)) then
9503 elsif No
(Body_To_Inline
(Decl
)) then
9506 -- Check if the body contains only a null statement, followed by
9507 -- the return statement added during expansion.
9511 Orig_Bod
: constant Node_Id
:= Body_To_Inline
(Decl
);
9517 if Nkind
(Orig_Bod
) /= N_Subprogram_Body
then
9520 -- We must skip SCIL nodes because they are currently
9521 -- implemented as special N_Null_Statement nodes.
9525 (Statements
(Handled_Statement_Sequence
(Orig_Bod
)));
9526 Stat2
:= Next_Non_SCIL_Node
(Stat
);
9529 Is_Empty_List
(Declarations
(Orig_Bod
))
9530 and then Nkind
(Stat
) = N_Null_Statement
9534 (Nkind
(Stat2
) = N_Simple_Return_Statement
9535 and then No
(Next
(Stat2
))));
9543 end Is_Null_Procedure
;
9545 -------------------------------------------
9546 -- Make_Build_In_Place_Call_In_Allocator --
9547 -------------------------------------------
9549 procedure Make_Build_In_Place_Call_In_Allocator
9550 (Allocator
: Node_Id
;
9551 Function_Call
: Node_Id
)
9553 Acc_Type
: constant Entity_Id
:= Etype
(Allocator
);
9555 Func_Call
: Node_Id
:= Function_Call
;
9556 Function_Id
: Entity_Id
;
9557 Result_Subt
: Entity_Id
;
9558 New_Allocator
: Node_Id
;
9559 Return_Obj_Access
: Entity_Id
;
9562 -- Step past qualification or unchecked conversion (the latter can occur
9563 -- in cases of calls to 'Input).
9565 if Nkind_In
(Func_Call
,
9566 N_Qualified_Expression
,
9567 N_Unchecked_Type_Conversion
)
9569 Func_Call
:= Expression
(Func_Call
);
9572 -- If the call has already been processed to add build-in-place actuals
9573 -- then return. This should not normally occur in an allocator context,
9574 -- but we add the protection as a defensive measure.
9576 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
9580 -- Mark the call as processed as a build-in-place call
9582 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
9584 Loc
:= Sloc
(Function_Call
);
9586 if Is_Entity_Name
(Name
(Func_Call
)) then
9587 Function_Id
:= Entity
(Name
(Func_Call
));
9589 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
9590 Function_Id
:= Etype
(Name
(Func_Call
));
9593 raise Program_Error
;
9596 Result_Subt
:= Available_View
(Etype
(Function_Id
));
9598 -- Check whether return type includes tasks. This may not have been done
9599 -- previously, if the type was a limited view.
9601 if Has_Task
(Result_Subt
) then
9602 Build_Activation_Chain_Entity
(Allocator
);
9605 -- When the result subtype is constrained, the return object must be
9606 -- allocated on the caller side, and access to it is passed to the
9609 -- Here and in related routines, we must examine the full view of the
9610 -- type, because the view at the point of call may differ from that
9611 -- that in the function body, and the expansion mechanism depends on
9612 -- the characteristics of the full view.
9614 if Is_Constrained
(Underlying_Type
(Result_Subt
)) then
9616 -- Replace the initialized allocator of form "new T'(Func (...))"
9617 -- with an uninitialized allocator of form "new T", where T is the
9618 -- result subtype of the called function. The call to the function
9619 -- is handled separately further below.
9622 Make_Allocator
(Loc
,
9623 Expression
=> New_Occurrence_Of
(Result_Subt
, Loc
));
9624 Set_No_Initialization
(New_Allocator
);
9626 -- Copy attributes to new allocator. Note that the new allocator
9627 -- logically comes from source if the original one did, so copy the
9628 -- relevant flag. This ensures proper treatment of the restriction
9629 -- No_Implicit_Heap_Allocations in this case.
9631 Set_Storage_Pool
(New_Allocator
, Storage_Pool
(Allocator
));
9632 Set_Procedure_To_Call
(New_Allocator
, Procedure_To_Call
(Allocator
));
9633 Set_Comes_From_Source
(New_Allocator
, Comes_From_Source
(Allocator
));
9635 Rewrite
(Allocator
, New_Allocator
);
9637 -- Create a new access object and initialize it to the result of the
9638 -- new uninitialized allocator. Note: we do not use Allocator as the
9639 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9640 -- as this would create a sort of infinite "recursion".
9642 Return_Obj_Access
:= Make_Temporary
(Loc
, 'R');
9643 Set_Etype
(Return_Obj_Access
, Acc_Type
);
9645 Insert_Action
(Allocator
,
9646 Make_Object_Declaration
(Loc
,
9647 Defining_Identifier
=> Return_Obj_Access
,
9648 Object_Definition
=> New_Occurrence_Of
(Acc_Type
, Loc
),
9649 Expression
=> Relocate_Node
(Allocator
)));
9651 -- When the function has a controlling result, an allocation-form
9652 -- parameter must be passed indicating that the caller is allocating
9653 -- the result object. This is needed because such a function can be
9654 -- called as a dispatching operation and must be treated similarly
9655 -- to functions with unconstrained result subtypes.
9657 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9658 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
9660 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9661 (Func_Call
, Function_Id
, Acc_Type
);
9663 Add_Task_Actuals_To_Build_In_Place_Call
9664 (Func_Call
, Function_Id
, Master_Actual
=> Master_Id
(Acc_Type
));
9666 -- Add an implicit actual to the function call that provides access
9667 -- to the allocated object. An unchecked conversion to the (specific)
9668 -- result subtype of the function is inserted to handle cases where
9669 -- the access type of the allocator has a class-wide designated type.
9671 Add_Access_Actual_To_Build_In_Place_Call
9674 Make_Unchecked_Type_Conversion
(Loc
,
9675 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Loc
),
9677 Make_Explicit_Dereference
(Loc
,
9678 Prefix
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
))));
9680 -- When the result subtype is unconstrained, the function itself must
9681 -- perform the allocation of the return object, so we pass parameters
9682 -- indicating that. We don't yet handle the case where the allocation
9683 -- must be done in a user-defined storage pool, which will require
9684 -- passing another actual or two to provide allocation/deallocation
9688 -- Case of a user-defined storage pool. Pass an allocation parameter
9689 -- indicating that the function should allocate its result in the
9690 -- pool, and pass the pool. Use 'Unrestricted_Access because the
9691 -- pool may not be aliased.
9693 if VM_Target
= No_VM
9694 and then Present
(Associated_Storage_Pool
(Acc_Type
))
9696 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9697 (Func_Call
, Function_Id
, Alloc_Form
=> User_Storage_Pool
,
9699 Make_Attribute_Reference
(Loc
,
9702 (Associated_Storage_Pool
(Acc_Type
), Loc
),
9703 Attribute_Name
=> Name_Unrestricted_Access
));
9705 -- No user-defined pool; pass an allocation parameter indicating that
9706 -- the function should allocate its result on the heap.
9709 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9710 (Func_Call
, Function_Id
, Alloc_Form
=> Global_Heap
);
9713 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9714 (Func_Call
, Function_Id
, Acc_Type
);
9716 Add_Task_Actuals_To_Build_In_Place_Call
9717 (Func_Call
, Function_Id
, Master_Actual
=> Master_Id
(Acc_Type
));
9719 -- The caller does not provide the return object in this case, so we
9720 -- have to pass null for the object access actual.
9722 Add_Access_Actual_To_Build_In_Place_Call
9723 (Func_Call
, Function_Id
, Return_Object
=> Empty
);
9726 -- If the build-in-place function call returns a controlled object,
9727 -- the finalization master will require a reference to routine
9728 -- Finalize_Address of the designated type. Setting this attribute
9729 -- is done in the same manner to expansion of allocators.
9731 if Needs_Finalization
(Result_Subt
) then
9733 -- Controlled types with supressed finalization do not need to
9734 -- associate the address of their Finalize_Address primitives with
9735 -- a master since they do not need a master to begin with.
9737 if Is_Library_Level_Entity
(Acc_Type
)
9738 and then Finalize_Storage_Only
(Result_Subt
)
9742 -- Do not generate the call to Set_Finalize_Address in CodePeer mode
9743 -- because Finalize_Address is never built.
9745 elsif not CodePeer_Mode
then
9746 Insert_Action
(Allocator
,
9747 Make_Set_Finalize_Address_Call
(Loc
,
9748 Typ
=> Etype
(Function_Id
),
9749 Ptr_Typ
=> Acc_Type
));
9753 -- Finally, replace the allocator node with a reference to the result
9754 -- of the function call itself (which will effectively be an access
9755 -- to the object created by the allocator).
9757 Rewrite
(Allocator
, Make_Reference
(Loc
, Relocate_Node
(Function_Call
)));
9759 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9760 -- generate an implicit conversion to force displacement of the "this"
9763 if Is_Interface
(Designated_Type
(Acc_Type
)) then
9764 Rewrite
(Allocator
, Convert_To
(Acc_Type
, Relocate_Node
(Allocator
)));
9767 Analyze_And_Resolve
(Allocator
, Acc_Type
);
9768 end Make_Build_In_Place_Call_In_Allocator
;
9770 ---------------------------------------------------
9771 -- Make_Build_In_Place_Call_In_Anonymous_Context --
9772 ---------------------------------------------------
9774 procedure Make_Build_In_Place_Call_In_Anonymous_Context
9775 (Function_Call
: Node_Id
)
9778 Func_Call
: Node_Id
:= Function_Call
;
9779 Function_Id
: Entity_Id
;
9780 Result_Subt
: Entity_Id
;
9781 Return_Obj_Id
: Entity_Id
;
9782 Return_Obj_Decl
: Entity_Id
;
9785 -- Step past qualification or unchecked conversion (the latter can occur
9786 -- in cases of calls to 'Input).
9788 if Nkind_In
(Func_Call
, N_Qualified_Expression
,
9789 N_Unchecked_Type_Conversion
)
9791 Func_Call
:= Expression
(Func_Call
);
9794 -- If the call has already been processed to add build-in-place actuals
9795 -- then return. One place this can occur is for calls to build-in-place
9796 -- functions that occur within a call to a protected operation, where
9797 -- due to rewriting and expansion of the protected call there can be
9798 -- more than one call to Expand_Actuals for the same set of actuals.
9800 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
9804 -- Mark the call as processed as a build-in-place call
9806 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
9808 Loc
:= Sloc
(Function_Call
);
9810 if Is_Entity_Name
(Name
(Func_Call
)) then
9811 Function_Id
:= Entity
(Name
(Func_Call
));
9813 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
9814 Function_Id
:= Etype
(Name
(Func_Call
));
9817 raise Program_Error
;
9820 Result_Subt
:= Etype
(Function_Id
);
9822 -- If the build-in-place function returns a controlled object, then the
9823 -- object needs to be finalized immediately after the context. Since
9824 -- this case produces a transient scope, the servicing finalizer needs
9825 -- to name the returned object. Create a temporary which is initialized
9826 -- with the function call:
9828 -- Temp_Id : Func_Type := BIP_Func_Call;
9830 -- The initialization expression of the temporary will be rewritten by
9831 -- the expander using the appropriate mechanism in Make_Build_In_Place_
9832 -- Call_In_Object_Declaration.
9834 if Needs_Finalization
(Result_Subt
) then
9836 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'R');
9837 Temp_Decl
: Node_Id
;
9840 -- Reset the guard on the function call since the following does
9841 -- not perform actual call expansion.
9843 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
, False);
9846 Make_Object_Declaration
(Loc
,
9847 Defining_Identifier
=> Temp_Id
,
9848 Object_Definition
=>
9849 New_Occurrence_Of
(Result_Subt
, Loc
),
9851 New_Copy_Tree
(Function_Call
));
9853 Insert_Action
(Function_Call
, Temp_Decl
);
9855 Rewrite
(Function_Call
, New_Occurrence_Of
(Temp_Id
, Loc
));
9856 Analyze
(Function_Call
);
9859 -- When the result subtype is constrained, an object of the subtype is
9860 -- declared and an access value designating it is passed as an actual.
9862 elsif Is_Constrained
(Underlying_Type
(Result_Subt
)) then
9864 -- Create a temporary object to hold the function result
9866 Return_Obj_Id
:= Make_Temporary
(Loc
, 'R');
9867 Set_Etype
(Return_Obj_Id
, Result_Subt
);
9870 Make_Object_Declaration
(Loc
,
9871 Defining_Identifier
=> Return_Obj_Id
,
9872 Aliased_Present
=> True,
9873 Object_Definition
=> New_Occurrence_Of
(Result_Subt
, Loc
));
9875 Set_No_Initialization
(Return_Obj_Decl
);
9877 Insert_Action
(Func_Call
, Return_Obj_Decl
);
9879 -- When the function has a controlling result, an allocation-form
9880 -- parameter must be passed indicating that the caller is allocating
9881 -- the result object. This is needed because such a function can be
9882 -- called as a dispatching operation and must be treated similarly
9883 -- to functions with unconstrained result subtypes.
9885 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9886 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
9888 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9889 (Func_Call
, Function_Id
);
9891 Add_Task_Actuals_To_Build_In_Place_Call
9892 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
9894 -- Add an implicit actual to the function call that provides access
9895 -- to the caller's return object.
9897 Add_Access_Actual_To_Build_In_Place_Call
9898 (Func_Call
, Function_Id
, New_Occurrence_Of
(Return_Obj_Id
, Loc
));
9900 -- When the result subtype is unconstrained, the function must allocate
9901 -- the return object in the secondary stack, so appropriate implicit
9902 -- parameters are added to the call to indicate that. A transient
9903 -- scope is established to ensure eventual cleanup of the result.
9906 -- Pass an allocation parameter indicating that the function should
9907 -- allocate its result on the secondary stack.
9909 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9910 (Func_Call
, Function_Id
, Alloc_Form
=> Secondary_Stack
);
9912 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9913 (Func_Call
, Function_Id
);
9915 Add_Task_Actuals_To_Build_In_Place_Call
9916 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
9918 -- Pass a null value to the function since no return object is
9919 -- available on the caller side.
9921 Add_Access_Actual_To_Build_In_Place_Call
9922 (Func_Call
, Function_Id
, Empty
);
9924 end Make_Build_In_Place_Call_In_Anonymous_Context
;
9926 --------------------------------------------
9927 -- Make_Build_In_Place_Call_In_Assignment --
9928 --------------------------------------------
9930 procedure Make_Build_In_Place_Call_In_Assignment
9932 Function_Call
: Node_Id
)
9934 Lhs
: constant Node_Id
:= Name
(Assign
);
9935 Func_Call
: Node_Id
:= Function_Call
;
9936 Func_Id
: Entity_Id
;
9940 Ptr_Typ
: Entity_Id
;
9941 Ptr_Typ_Decl
: Node_Id
;
9943 Result_Subt
: Entity_Id
;
9947 -- Step past qualification or unchecked conversion (the latter can occur
9948 -- in cases of calls to 'Input).
9950 if Nkind_In
(Func_Call
, N_Qualified_Expression
,
9951 N_Unchecked_Type_Conversion
)
9953 Func_Call
:= Expression
(Func_Call
);
9956 -- If the call has already been processed to add build-in-place actuals
9957 -- then return. This should not normally occur in an assignment context,
9958 -- but we add the protection as a defensive measure.
9960 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
9964 -- Mark the call as processed as a build-in-place call
9966 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
9968 Loc
:= Sloc
(Function_Call
);
9970 if Is_Entity_Name
(Name
(Func_Call
)) then
9971 Func_Id
:= Entity
(Name
(Func_Call
));
9973 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
9974 Func_Id
:= Etype
(Name
(Func_Call
));
9977 raise Program_Error
;
9980 Result_Subt
:= Etype
(Func_Id
);
9982 -- When the result subtype is unconstrained, an additional actual must
9983 -- be passed to indicate that the caller is providing the return object.
9984 -- This parameter must also be passed when the called function has a
9985 -- controlling result, because dispatching calls to the function needs
9986 -- to be treated effectively the same as calls to class-wide functions.
9988 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9989 (Func_Call
, Func_Id
, Alloc_Form
=> Caller_Allocation
);
9991 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9992 (Func_Call
, Func_Id
);
9994 Add_Task_Actuals_To_Build_In_Place_Call
9995 (Func_Call
, Func_Id
, Make_Identifier
(Loc
, Name_uMaster
));
9997 -- Add an implicit actual to the function call that provides access to
9998 -- the caller's return object.
10000 Add_Access_Actual_To_Build_In_Place_Call
10003 Make_Unchecked_Type_Conversion
(Loc
,
10004 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Loc
),
10005 Expression
=> Relocate_Node
(Lhs
)));
10007 -- Create an access type designating the function's result subtype
10009 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
10012 Make_Full_Type_Declaration
(Loc
,
10013 Defining_Identifier
=> Ptr_Typ
,
10015 Make_Access_To_Object_Definition
(Loc
,
10016 All_Present
=> True,
10017 Subtype_Indication
=>
10018 New_Occurrence_Of
(Result_Subt
, Loc
)));
10019 Insert_After_And_Analyze
(Assign
, Ptr_Typ_Decl
);
10021 -- Finally, create an access object initialized to a reference to the
10022 -- function call. We know this access value is non-null, so mark the
10023 -- entity accordingly to suppress junk access checks.
10025 New_Expr
:= Make_Reference
(Loc
, Relocate_Node
(Func_Call
));
10027 Obj_Id
:= Make_Temporary
(Loc
, 'R', New_Expr
);
10028 Set_Etype
(Obj_Id
, Ptr_Typ
);
10029 Set_Is_Known_Non_Null
(Obj_Id
);
10032 Make_Object_Declaration
(Loc
,
10033 Defining_Identifier
=> Obj_Id
,
10034 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
10035 Expression
=> New_Expr
);
10036 Insert_After_And_Analyze
(Ptr_Typ_Decl
, Obj_Decl
);
10038 Rewrite
(Assign
, Make_Null_Statement
(Loc
));
10040 -- Retrieve the target of the assignment
10042 if Nkind
(Lhs
) = N_Selected_Component
then
10043 Target
:= Selector_Name
(Lhs
);
10044 elsif Nkind
(Lhs
) = N_Type_Conversion
then
10045 Target
:= Expression
(Lhs
);
10050 -- If we are assigning to a return object or this is an expression of
10051 -- an extension aggregate, the target should either be an identifier
10052 -- or a simple expression. All other cases imply a different scenario.
10054 if Nkind
(Target
) in N_Has_Entity
then
10055 Target
:= Entity
(Target
);
10059 end Make_Build_In_Place_Call_In_Assignment
;
10061 ----------------------------------------------------
10062 -- Make_Build_In_Place_Call_In_Object_Declaration --
10063 ----------------------------------------------------
10065 procedure Make_Build_In_Place_Call_In_Object_Declaration
10066 (Object_Decl
: Node_Id
;
10067 Function_Call
: Node_Id
)
10070 Obj_Def_Id
: constant Entity_Id
:=
10071 Defining_Identifier
(Object_Decl
);
10072 Enclosing_Func
: constant Entity_Id
:=
10073 Enclosing_Subprogram
(Obj_Def_Id
);
10074 Call_Deref
: Node_Id
;
10075 Caller_Object
: Node_Id
;
10076 Def_Id
: Entity_Id
;
10077 Fmaster_Actual
: Node_Id
:= Empty
;
10078 Func_Call
: Node_Id
:= Function_Call
;
10079 Function_Id
: Entity_Id
;
10080 Pool_Actual
: Node_Id
;
10081 Ptr_Typ_Decl
: Node_Id
;
10082 Pass_Caller_Acc
: Boolean := False;
10083 New_Expr
: Node_Id
;
10084 Ref_Type
: Entity_Id
;
10085 Res_Decl
: Node_Id
;
10086 Result_Subt
: Entity_Id
;
10089 -- Step past qualification or unchecked conversion (the latter can occur
10090 -- in cases of calls to 'Input).
10092 if Nkind_In
(Func_Call
, N_Qualified_Expression
,
10093 N_Unchecked_Type_Conversion
)
10095 Func_Call
:= Expression
(Func_Call
);
10098 -- If the call has already been processed to add build-in-place actuals
10099 -- then return. This should not normally occur in an object declaration,
10100 -- but we add the protection as a defensive measure.
10102 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
10106 -- Mark the call as processed as a build-in-place call
10108 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
10110 Loc
:= Sloc
(Function_Call
);
10112 if Is_Entity_Name
(Name
(Func_Call
)) then
10113 Function_Id
:= Entity
(Name
(Func_Call
));
10115 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
10116 Function_Id
:= Etype
(Name
(Func_Call
));
10119 raise Program_Error
;
10122 Result_Subt
:= Etype
(Function_Id
);
10124 -- If the the object is a return object of an enclosing build-in-place
10125 -- function, then the implicit build-in-place parameters of the
10126 -- enclosing function are simply passed along to the called function.
10127 -- (Unfortunately, this won't cover the case of extension aggregates
10128 -- where the ancestor part is a build-in-place unconstrained function
10129 -- call that should be passed along the caller's parameters. Currently
10130 -- those get mishandled by reassigning the result of the call to the
10131 -- aggregate return object, when the call result should really be
10132 -- directly built in place in the aggregate and not in a temporary. ???)
10134 if Is_Return_Object
(Defining_Identifier
(Object_Decl
)) then
10135 Pass_Caller_Acc
:= True;
10137 -- When the enclosing function has a BIP_Alloc_Form formal then we
10138 -- pass it along to the callee (such as when the enclosing function
10139 -- has an unconstrained or tagged result type).
10141 if Needs_BIP_Alloc_Form
(Enclosing_Func
) then
10142 if VM_Target
= No_VM
and then
10143 RTE_Available
(RE_Root_Storage_Pool_Ptr
)
10146 New_Occurrence_Of
(Build_In_Place_Formal
10147 (Enclosing_Func
, BIP_Storage_Pool
), Loc
);
10149 -- The build-in-place pool formal is not built on .NET/JVM
10152 Pool_Actual
:= Empty
;
10155 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10160 (Build_In_Place_Formal
(Enclosing_Func
, BIP_Alloc_Form
),
10162 Pool_Actual
=> Pool_Actual
);
10164 -- Otherwise, if enclosing function has a constrained result subtype,
10165 -- then caller allocation will be used.
10168 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10169 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
10172 if Needs_BIP_Finalization_Master
(Enclosing_Func
) then
10175 (Build_In_Place_Formal
10176 (Enclosing_Func
, BIP_Finalization_Master
), Loc
);
10179 -- Retrieve the BIPacc formal from the enclosing function and convert
10180 -- it to the access type of the callee's BIP_Object_Access formal.
10183 Make_Unchecked_Type_Conversion
(Loc
,
10187 (Build_In_Place_Formal
(Function_Id
, BIP_Object_Access
)),
10191 (Build_In_Place_Formal
(Enclosing_Func
, BIP_Object_Access
),
10194 -- In the constrained case, add an implicit actual to the function call
10195 -- that provides access to the declared object. An unchecked conversion
10196 -- to the (specific) result type of the function is inserted to handle
10197 -- the case where the object is declared with a class-wide type.
10199 elsif Is_Constrained
(Underlying_Type
(Result_Subt
)) then
10201 Make_Unchecked_Type_Conversion
(Loc
,
10202 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Loc
),
10203 Expression
=> New_Occurrence_Of
(Obj_Def_Id
, Loc
));
10205 -- When the function has a controlling result, an allocation-form
10206 -- parameter must be passed indicating that the caller is allocating
10207 -- the result object. This is needed because such a function can be
10208 -- called as a dispatching operation and must be treated similarly
10209 -- to functions with unconstrained result subtypes.
10211 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10212 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
10214 -- In other unconstrained cases, pass an indication to do the allocation
10215 -- on the secondary stack and set Caller_Object to Empty so that a null
10216 -- value will be passed for the caller's object address. A transient
10217 -- scope is established to ensure eventual cleanup of the result.
10220 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10221 (Func_Call
, Function_Id
, Alloc_Form
=> Secondary_Stack
);
10222 Caller_Object
:= Empty
;
10224 Establish_Transient_Scope
(Object_Decl
, Sec_Stack
=> True);
10227 -- Pass along any finalization master actual, which is needed in the
10228 -- case where the called function initializes a return object of an
10229 -- enclosing build-in-place function.
10231 Add_Finalization_Master_Actual_To_Build_In_Place_Call
10232 (Func_Call
=> Func_Call
,
10233 Func_Id
=> Function_Id
,
10234 Master_Exp
=> Fmaster_Actual
);
10236 if Nkind
(Parent
(Object_Decl
)) = N_Extended_Return_Statement
10237 and then Has_Task
(Result_Subt
)
10239 -- Here we're passing along the master that was passed in to this
10242 Add_Task_Actuals_To_Build_In_Place_Call
10243 (Func_Call
, Function_Id
,
10245 New_Occurrence_Of
(Build_In_Place_Formal
10246 (Enclosing_Func
, BIP_Task_Master
), Loc
));
10249 Add_Task_Actuals_To_Build_In_Place_Call
10250 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
10253 Add_Access_Actual_To_Build_In_Place_Call
10254 (Func_Call
, Function_Id
, Caller_Object
, Is_Access
=> Pass_Caller_Acc
);
10256 -- Create an access type designating the function's result subtype. We
10257 -- use the type of the original expression because it may be a call to
10258 -- an inherited operation, which the expansion has replaced with the
10259 -- parent operation that yields the parent type.
10261 Ref_Type
:= Make_Temporary
(Loc
, 'A');
10264 Make_Full_Type_Declaration
(Loc
,
10265 Defining_Identifier
=> Ref_Type
,
10267 Make_Access_To_Object_Definition
(Loc
,
10268 All_Present
=> True,
10269 Subtype_Indication
=>
10270 New_Occurrence_Of
(Etype
(Function_Call
), Loc
)));
10272 -- The access type and its accompanying object must be inserted after
10273 -- the object declaration in the constrained case, so that the function
10274 -- call can be passed access to the object. In the unconstrained case,
10275 -- or if the object declaration is for a return object, the access type
10276 -- and object must be inserted before the object, since the object
10277 -- declaration is rewritten to be a renaming of a dereference of the
10280 if Is_Constrained
(Underlying_Type
(Result_Subt
))
10281 and then not Is_Return_Object
(Defining_Identifier
(Object_Decl
))
10283 Insert_After_And_Analyze
(Object_Decl
, Ptr_Typ_Decl
);
10285 Insert_Action
(Object_Decl
, Ptr_Typ_Decl
);
10288 -- Finally, create an access object initialized to a reference to the
10289 -- function call. We know this access value cannot be null, so mark the
10290 -- entity accordingly to suppress the access check.
10292 New_Expr
:= Make_Reference
(Loc
, Relocate_Node
(Func_Call
));
10294 Def_Id
:= Make_Temporary
(Loc
, 'R', New_Expr
);
10295 Set_Etype
(Def_Id
, Ref_Type
);
10296 Set_Is_Known_Non_Null
(Def_Id
);
10299 Make_Object_Declaration
(Loc
,
10300 Defining_Identifier
=> Def_Id
,
10301 Object_Definition
=> New_Occurrence_Of
(Ref_Type
, Loc
),
10302 Expression
=> New_Expr
);
10303 Insert_After_And_Analyze
(Ptr_Typ_Decl
, Res_Decl
);
10305 -- If the result subtype of the called function is constrained and
10306 -- is not itself the return expression of an enclosing BIP function,
10307 -- then mark the object as having no initialization.
10309 if Is_Constrained
(Underlying_Type
(Result_Subt
))
10310 and then not Is_Return_Object
(Defining_Identifier
(Object_Decl
))
10312 -- The related object declaration is encased in a transient block
10313 -- because the build-in-place function call contains at least one
10314 -- nested function call that produces a controlled transient
10317 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
10319 -- Since the build-in-place expansion decouples the call from the
10320 -- object declaration, the finalization machinery lacks the context
10321 -- which prompted the generation of the transient block. To resolve
10322 -- this scenario, store the build-in-place call.
10324 if Scope_Is_Transient
10325 and then Node_To_Be_Wrapped
= Object_Decl
10327 Set_BIP_Initialization_Call
(Obj_Def_Id
, Res_Decl
);
10330 Set_Expression
(Object_Decl
, Empty
);
10331 Set_No_Initialization
(Object_Decl
);
10333 -- In case of an unconstrained result subtype, or if the call is the
10334 -- return expression of an enclosing BIP function, rewrite the object
10335 -- declaration as an object renaming where the renamed object is a
10336 -- dereference of <function_Call>'reference:
10338 -- Obj : Subt renames <function_call>'Ref.all;
10342 Make_Explicit_Dereference
(Loc
,
10343 Prefix
=> New_Occurrence_Of
(Def_Id
, Loc
));
10345 Loc
:= Sloc
(Object_Decl
);
10346 Rewrite
(Object_Decl
,
10347 Make_Object_Renaming_Declaration
(Loc
,
10348 Defining_Identifier
=> Make_Temporary
(Loc
, 'D'),
10349 Access_Definition
=> Empty
,
10350 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Loc
),
10351 Name
=> Call_Deref
));
10353 Set_Renamed_Object
(Defining_Identifier
(Object_Decl
), Call_Deref
);
10355 Analyze
(Object_Decl
);
10357 -- Replace the internal identifier of the renaming declaration's
10358 -- entity with identifier of the original object entity. We also have
10359 -- to exchange the entities containing their defining identifiers to
10360 -- ensure the correct replacement of the object declaration by the
10361 -- object renaming declaration to avoid homograph conflicts (since
10362 -- the object declaration's defining identifier was already entered
10363 -- in current scope). The Next_Entity links of the two entities also
10364 -- have to be swapped since the entities are part of the return
10365 -- scope's entity list and the list structure would otherwise be
10366 -- corrupted. Finally, the homonym chain must be preserved as well.
10369 Renaming_Def_Id
: constant Entity_Id
:=
10370 Defining_Identifier
(Object_Decl
);
10371 Next_Entity_Temp
: constant Entity_Id
:=
10372 Next_Entity
(Renaming_Def_Id
);
10374 Set_Chars
(Renaming_Def_Id
, Chars
(Obj_Def_Id
));
10376 -- Swap next entity links in preparation for exchanging entities
10378 Set_Next_Entity
(Renaming_Def_Id
, Next_Entity
(Obj_Def_Id
));
10379 Set_Next_Entity
(Obj_Def_Id
, Next_Entity_Temp
);
10380 Set_Homonym
(Renaming_Def_Id
, Homonym
(Obj_Def_Id
));
10382 Exchange_Entities
(Renaming_Def_Id
, Obj_Def_Id
);
10384 -- Preserve source indication of original declaration, so that
10385 -- xref information is properly generated for the right entity.
10387 Preserve_Comes_From_Source
10388 (Object_Decl
, Original_Node
(Object_Decl
));
10390 Preserve_Comes_From_Source
10391 (Obj_Def_Id
, Original_Node
(Object_Decl
));
10393 Set_Comes_From_Source
(Renaming_Def_Id
, False);
10397 -- If the object entity has a class-wide Etype, then we need to change
10398 -- it to the result subtype of the function call, because otherwise the
10399 -- object will be class-wide without an explicit initialization and
10400 -- won't be allocated properly by the back end. It seems unclean to make
10401 -- such a revision to the type at this point, and we should try to
10402 -- improve this treatment when build-in-place functions with class-wide
10403 -- results are implemented. ???
10405 if Is_Class_Wide_Type
(Etype
(Defining_Identifier
(Object_Decl
))) then
10406 Set_Etype
(Defining_Identifier
(Object_Decl
), Result_Subt
);
10408 end Make_Build_In_Place_Call_In_Object_Declaration
;
10410 --------------------------------------------
10411 -- Make_CPP_Constructor_Call_In_Allocator --
10412 --------------------------------------------
10414 procedure Make_CPP_Constructor_Call_In_Allocator
10415 (Allocator
: Node_Id
;
10416 Function_Call
: Node_Id
)
10418 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
10419 Acc_Type
: constant Entity_Id
:= Etype
(Allocator
);
10420 Function_Id
: constant Entity_Id
:= Entity
(Name
(Function_Call
));
10421 Result_Subt
: constant Entity_Id
:= Available_View
(Etype
(Function_Id
));
10423 New_Allocator
: Node_Id
;
10424 Return_Obj_Access
: Entity_Id
;
10428 pragma Assert
(Nkind
(Allocator
) = N_Allocator
10429 and then Nkind
(Function_Call
) = N_Function_Call
);
10430 pragma Assert
(Convention
(Function_Id
) = Convention_CPP
10431 and then Is_Constructor
(Function_Id
));
10432 pragma Assert
(Is_Constrained
(Underlying_Type
(Result_Subt
)));
10434 -- Replace the initialized allocator of form "new T'(Func (...))" with
10435 -- an uninitialized allocator of form "new T", where T is the result
10436 -- subtype of the called function. The call to the function is handled
10437 -- separately further below.
10440 Make_Allocator
(Loc
,
10441 Expression
=> New_Occurrence_Of
(Result_Subt
, Loc
));
10442 Set_No_Initialization
(New_Allocator
);
10444 -- Copy attributes to new allocator. Note that the new allocator
10445 -- logically comes from source if the original one did, so copy the
10446 -- relevant flag. This ensures proper treatment of the restriction
10447 -- No_Implicit_Heap_Allocations in this case.
10449 Set_Storage_Pool
(New_Allocator
, Storage_Pool
(Allocator
));
10450 Set_Procedure_To_Call
(New_Allocator
, Procedure_To_Call
(Allocator
));
10451 Set_Comes_From_Source
(New_Allocator
, Comes_From_Source
(Allocator
));
10453 Rewrite
(Allocator
, New_Allocator
);
10455 -- Create a new access object and initialize it to the result of the
10456 -- new uninitialized allocator. Note: we do not use Allocator as the
10457 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
10458 -- as this would create a sort of infinite "recursion".
10460 Return_Obj_Access
:= Make_Temporary
(Loc
, 'R');
10461 Set_Etype
(Return_Obj_Access
, Acc_Type
);
10464 -- Rnnn : constant ptr_T := new (T);
10465 -- Init (Rnn.all,...);
10468 Make_Object_Declaration
(Loc
,
10469 Defining_Identifier
=> Return_Obj_Access
,
10470 Constant_Present
=> True,
10471 Object_Definition
=> New_Occurrence_Of
(Acc_Type
, Loc
),
10472 Expression
=> Relocate_Node
(Allocator
));
10473 Insert_Action
(Allocator
, Tmp_Obj
);
10475 Insert_List_After_And_Analyze
(Tmp_Obj
,
10476 Build_Initialization_Call
(Loc
,
10478 Make_Explicit_Dereference
(Loc
,
10479 Prefix
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
)),
10480 Typ
=> Etype
(Function_Id
),
10481 Constructor_Ref
=> Function_Call
));
10483 -- Finally, replace the allocator node with a reference to the result of
10484 -- the function call itself (which will effectively be an access to the
10485 -- object created by the allocator).
10487 Rewrite
(Allocator
, New_Occurrence_Of
(Return_Obj_Access
, Loc
));
10489 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
10490 -- generate an implicit conversion to force displacement of the "this"
10493 if Is_Interface
(Designated_Type
(Acc_Type
)) then
10494 Rewrite
(Allocator
, Convert_To
(Acc_Type
, Relocate_Node
(Allocator
)));
10497 Analyze_And_Resolve
(Allocator
, Acc_Type
);
10498 end Make_CPP_Constructor_Call_In_Allocator
;
10500 -----------------------------------
10501 -- Needs_BIP_Finalization_Master --
10502 -----------------------------------
10504 function Needs_BIP_Finalization_Master
10505 (Func_Id
: Entity_Id
) return Boolean
10507 pragma Assert
(Is_Build_In_Place_Function
(Func_Id
));
10508 Func_Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
10511 not Restriction_Active
(No_Finalization
)
10512 and then Needs_Finalization
(Func_Typ
);
10513 end Needs_BIP_Finalization_Master
;
10515 --------------------------
10516 -- Needs_BIP_Alloc_Form --
10517 --------------------------
10519 function Needs_BIP_Alloc_Form
(Func_Id
: Entity_Id
) return Boolean is
10520 pragma Assert
(Is_Build_In_Place_Function
(Func_Id
));
10521 Func_Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
10523 return not Is_Constrained
(Func_Typ
) or else Is_Tagged_Type
(Func_Typ
);
10524 end Needs_BIP_Alloc_Form
;
10526 --------------------------------------
10527 -- Needs_Result_Accessibility_Level --
10528 --------------------------------------
10530 function Needs_Result_Accessibility_Level
10531 (Func_Id
: Entity_Id
) return Boolean
10533 Func_Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
10535 function Has_Unconstrained_Access_Discriminant_Component
10536 (Comp_Typ
: Entity_Id
) return Boolean;
10537 -- Returns True if any component of the type has an unconstrained access
10540 -----------------------------------------------------
10541 -- Has_Unconstrained_Access_Discriminant_Component --
10542 -----------------------------------------------------
10544 function Has_Unconstrained_Access_Discriminant_Component
10545 (Comp_Typ
: Entity_Id
) return Boolean
10548 if not Is_Limited_Type
(Comp_Typ
) then
10551 -- Only limited types can have access discriminants with
10554 elsif Has_Unconstrained_Access_Discriminants
(Comp_Typ
) then
10557 elsif Is_Array_Type
(Comp_Typ
) then
10558 return Has_Unconstrained_Access_Discriminant_Component
10559 (Underlying_Type
(Component_Type
(Comp_Typ
)));
10561 elsif Is_Record_Type
(Comp_Typ
) then
10566 Comp
:= First_Component
(Comp_Typ
);
10567 while Present
(Comp
) loop
10568 if Has_Unconstrained_Access_Discriminant_Component
10569 (Underlying_Type
(Etype
(Comp
)))
10574 Next_Component
(Comp
);
10580 end Has_Unconstrained_Access_Discriminant_Component
;
10582 Feature_Disabled
: constant Boolean := True;
10585 -- Start of processing for Needs_Result_Accessibility_Level
10588 -- False if completion unavailable (how does this happen???)
10590 if not Present
(Func_Typ
) then
10593 elsif Feature_Disabled
then
10596 -- False if not a function, also handle enum-lit renames case
10598 elsif Func_Typ
= Standard_Void_Type
10599 or else Is_Scalar_Type
(Func_Typ
)
10603 -- Handle a corner case, a cross-dialect subp renaming. For example,
10604 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
10605 -- an Ada 2005 (or earlier) unit references predefined run-time units.
10607 elsif Present
(Alias
(Func_Id
)) then
10609 -- Unimplemented: a cross-dialect subp renaming which does not set
10610 -- the Alias attribute (e.g., a rename of a dereference of an access
10611 -- to subprogram value). ???
10613 return Present
(Extra_Accessibility_Of_Result
(Alias
(Func_Id
)));
10615 -- Remaining cases require Ada 2012 mode
10617 elsif Ada_Version
< Ada_2012
then
10620 elsif Ekind
(Func_Typ
) = E_Anonymous_Access_Type
10621 or else Is_Tagged_Type
(Func_Typ
)
10623 -- In the case of, say, a null tagged record result type, the need
10624 -- for this extra parameter might not be obvious. This function
10625 -- returns True for all tagged types for compatibility reasons.
10626 -- A function with, say, a tagged null controlling result type might
10627 -- be overridden by a primitive of an extension having an access
10628 -- discriminant and the overrider and overridden must have compatible
10629 -- calling conventions (including implicitly declared parameters).
10630 -- Similarly, values of one access-to-subprogram type might designate
10631 -- both a primitive subprogram of a given type and a function
10632 -- which is, for example, not a primitive subprogram of any type.
10633 -- Again, this requires calling convention compatibility.
10634 -- It might be possible to solve these issues by introducing
10635 -- wrappers, but that is not the approach that was chosen.
10639 elsif Has_Unconstrained_Access_Discriminants
(Func_Typ
) then
10642 elsif Has_Unconstrained_Access_Discriminant_Component
(Func_Typ
) then
10645 -- False for all other cases
10650 end Needs_Result_Accessibility_Level
;
10652 ------------------------
10653 -- List_Inlining_Info --
10654 ------------------------
10656 procedure List_Inlining_Info
is
10662 if not Debug_Flag_Dot_J
then
10666 -- Generate listing of calls inlined by the frontend
10668 if Present
(Inlined_Calls
) then
10670 Elmt
:= First_Elmt
(Inlined_Calls
);
10671 while Present
(Elmt
) loop
10672 Nod
:= Node
(Elmt
);
10674 if In_Extended_Main_Code_Unit
(Nod
) then
10675 Count
:= Count
+ 1;
10678 Write_Str
("Listing of frontend inlined calls");
10685 Write_Location
(Sloc
(Nod
));
10694 -- Generate listing of calls passed to the backend
10696 if Present
(Backend_Calls
) then
10699 Elmt
:= First_Elmt
(Backend_Calls
);
10700 while Present
(Elmt
) loop
10701 Nod
:= Node
(Elmt
);
10703 if In_Extended_Main_Code_Unit
(Nod
) then
10704 Count
:= Count
+ 1;
10707 Write_Str
("Listing of inlined calls passed to the backend");
10714 Write_Location
(Sloc
(Nod
));
10721 end List_Inlining_Info
;