1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Checks
; use Checks
;
28 with Debug
; use Debug
;
29 with Einfo
; use Einfo
;
30 with Errout
; use Errout
;
31 with Elists
; use Elists
;
32 with Exp_Aggr
; use Exp_Aggr
;
33 with Exp_Atag
; use Exp_Atag
;
34 with Exp_Ch2
; use Exp_Ch2
;
35 with Exp_Ch3
; use Exp_Ch3
;
36 with Exp_Ch7
; use Exp_Ch7
;
37 with Exp_Ch9
; use Exp_Ch9
;
38 with Exp_Dbug
; use Exp_Dbug
;
39 with Exp_Disp
; use Exp_Disp
;
40 with Exp_Dist
; use Exp_Dist
;
41 with Exp_Intr
; use Exp_Intr
;
42 with Exp_Pakd
; use Exp_Pakd
;
43 with Exp_Prag
; use Exp_Prag
;
44 with Exp_Tss
; use Exp_Tss
;
45 with Exp_Util
; use Exp_Util
;
46 with Freeze
; use Freeze
;
47 with Inline
; use Inline
;
49 with Namet
; use Namet
;
50 with Nlists
; use Nlists
;
51 with Nmake
; use Nmake
;
53 with Restrict
; use Restrict
;
54 with Rident
; use Rident
;
55 with Rtsfind
; use Rtsfind
;
57 with Sem_Aux
; use Sem_Aux
;
58 with Sem_Ch6
; use Sem_Ch6
;
59 with Sem_Ch8
; use Sem_Ch8
;
60 with Sem_Ch13
; use Sem_Ch13
;
61 with Sem_Dim
; use Sem_Dim
;
62 with Sem_Disp
; use Sem_Disp
;
63 with Sem_Dist
; use Sem_Dist
;
64 with Sem_Eval
; use Sem_Eval
;
65 with Sem_Mech
; use Sem_Mech
;
66 with Sem_Res
; use Sem_Res
;
67 with Sem_SCIL
; use Sem_SCIL
;
68 with Sem_Util
; use Sem_Util
;
69 with Sinfo
; use Sinfo
;
70 with Snames
; use Snames
;
71 with Stand
; use Stand
;
72 with Stringt
; use Stringt
;
73 with Targparm
; use Targparm
;
74 with Tbuild
; use Tbuild
;
75 with Uintp
; use Uintp
;
76 with Validsw
; use Validsw
;
78 package body Exp_Ch6
is
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 procedure Add_Access_Actual_To_Build_In_Place_Call
85 (Function_Call
: Node_Id
;
86 Function_Id
: Entity_Id
;
87 Return_Object
: Node_Id
;
88 Is_Access
: Boolean := False);
89 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
90 -- object name given by Return_Object and add the attribute to the end of
91 -- the actual parameter list associated with the build-in-place function
92 -- call denoted by Function_Call. However, if Is_Access is True, then
93 -- Return_Object is already an access expression, in which case it's passed
94 -- along directly to the build-in-place function. Finally, if Return_Object
95 -- is empty, then pass a null literal as the actual.
97 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
98 (Function_Call
: Node_Id
;
99 Function_Id
: Entity_Id
;
100 Alloc_Form
: BIP_Allocation_Form
:= Unspecified
;
101 Alloc_Form_Exp
: Node_Id
:= Empty
;
102 Pool_Actual
: Node_Id
:= Make_Null
(No_Location
));
103 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
104 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
105 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
106 -- otherwise pass a literal corresponding to the Alloc_Form parameter
107 -- (which must not be Unspecified in that case). Pool_Actual is the
108 -- parameter to pass to BIP_Storage_Pool.
110 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
111 (Func_Call
: Node_Id
;
113 Ptr_Typ
: Entity_Id
:= Empty
;
114 Master_Exp
: Node_Id
:= Empty
);
115 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
116 -- finalization actions, add an actual parameter which is a pointer to the
117 -- finalization master of the caller. If Master_Exp is not Empty, then that
118 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
119 -- will result in an automatic "null" value for the actual.
121 procedure Add_Task_Actuals_To_Build_In_Place_Call
122 (Function_Call
: Node_Id
;
123 Function_Id
: Entity_Id
;
124 Master_Actual
: Node_Id
;
125 Chain
: Node_Id
:= Empty
);
126 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
127 -- contains tasks, add two actual parameters: the master, and a pointer to
128 -- the caller's activation chain. Master_Actual is the actual parameter
129 -- expression to pass for the master. In most cases, this is the current
130 -- master (_master). The two exceptions are: If the function call is the
131 -- initialization expression for an allocator, we pass the master of the
132 -- access type. If the function call is the initialization expression for a
133 -- return object, we pass along the master passed in by the caller. In most
134 -- contexts, the activation chain to pass is the local one, which is
135 -- indicated by No (Chain). However, in an allocator, the caller passes in
136 -- the activation Chain. Note: Master_Actual can be Empty, but only if
137 -- there are no tasks.
139 procedure Check_Overriding_Operation
(Subp
: Entity_Id
);
140 -- Subp is a dispatching operation. Check whether it may override an
141 -- inherited private operation, in which case its DT entry is that of
142 -- the hidden operation, not the one it may have received earlier.
143 -- This must be done before emitting the code to set the corresponding
144 -- DT to the address of the subprogram. The actual placement of Subp in
145 -- the proper place in the list of primitive operations is done in
146 -- Declare_Inherited_Private_Subprograms, which also has to deal with
147 -- implicit operations. This duplication is unavoidable for now???
149 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
);
150 -- This procedure is called only if the subprogram body N, whose spec
151 -- has the given entity Spec, contains a parameterless recursive call.
152 -- It attempts to generate runtime code to detect if this a case of
153 -- infinite recursion.
155 -- The body is scanned to determine dependencies. If the only external
156 -- dependencies are on a small set of scalar variables, then the values
157 -- of these variables are captured on entry to the subprogram, and if
158 -- the values are not changed for the call, we know immediately that
159 -- we have an infinite recursion.
161 procedure Expand_Actuals
(N
: in out Node_Id
; Subp
: Entity_Id
);
162 -- For each actual of an in-out or out parameter which is a numeric
163 -- (view) conversion of the form T (A), where A denotes a variable,
164 -- we insert the declaration:
166 -- Temp : T[ := T (A)];
168 -- prior to the call. Then we replace the actual with a reference to Temp,
169 -- and append the assignment:
171 -- A := TypeA (Temp);
173 -- after the call. Here TypeA is the actual type of variable A. For out
174 -- parameters, the initial declaration has no expression. If A is not an
175 -- entity name, we generate instead:
177 -- Var : TypeA renames A;
178 -- Temp : T := Var; -- omitting expression for out parameter.
180 -- Var := TypeA (Temp);
182 -- For other in-out parameters, we emit the required constraint checks
183 -- before and/or after the call.
185 -- For all parameter modes, actuals that denote components and slices of
186 -- packed arrays are expanded into suitable temporaries.
188 -- For non-scalar objects that are possibly unaligned, add call by copy
189 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
191 -- For OUT and IN OUT parameters, add predicate checks after the call
192 -- based on the predicates of the actual type.
194 -- The parameter N is IN OUT because in some cases, the expansion code
195 -- rewrites the call as an expression actions with the call inside. In
196 -- this case N is reset to point to the inside call so that the caller
197 -- can continue processing of this call.
199 procedure Expand_Ctrl_Function_Call
(N
: Node_Id
);
200 -- N is a function call which returns a controlled object. Transform the
201 -- call into a temporary which retrieves the returned object from the
202 -- secondary stack using 'reference.
204 procedure Expand_Non_Function_Return
(N
: Node_Id
);
205 -- Expand a simple return statement found in a procedure body, entry body,
206 -- accept statement, or an extended return statement. Note that all non-
207 -- function returns are simple return statements.
209 function Expand_Protected_Object_Reference
211 Scop
: Entity_Id
) return Node_Id
;
213 procedure Expand_Protected_Subprogram_Call
217 -- A call to a protected subprogram within the protected object may appear
218 -- as a regular call. The list of actuals must be expanded to contain a
219 -- reference to the object itself, and the call becomes a call to the
220 -- corresponding protected subprogram.
222 function Has_Unconstrained_Access_Discriminants
223 (Subtyp
: Entity_Id
) return Boolean;
224 -- Returns True if the given subtype is unconstrained and has one
225 -- or more access discriminants.
227 procedure Expand_Simple_Function_Return
(N
: Node_Id
);
228 -- Expand simple return from function. In the case where we are returning
229 -- from a function body this is called by Expand_N_Simple_Return_Statement.
231 ----------------------------------------------
232 -- Add_Access_Actual_To_Build_In_Place_Call --
233 ----------------------------------------------
235 procedure Add_Access_Actual_To_Build_In_Place_Call
236 (Function_Call
: Node_Id
;
237 Function_Id
: Entity_Id
;
238 Return_Object
: Node_Id
;
239 Is_Access
: Boolean := False)
241 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
242 Obj_Address
: Node_Id
;
243 Obj_Acc_Formal
: Entity_Id
;
246 -- Locate the implicit access parameter in the called function
248 Obj_Acc_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Object_Access
);
250 -- If no return object is provided, then pass null
252 if not Present
(Return_Object
) then
253 Obj_Address
:= Make_Null
(Loc
);
254 Set_Parent
(Obj_Address
, Function_Call
);
256 -- If Return_Object is already an expression of an access type, then use
257 -- it directly, since it must be an access value denoting the return
258 -- object, and couldn't possibly be the return object itself.
261 Obj_Address
:= Return_Object
;
262 Set_Parent
(Obj_Address
, Function_Call
);
264 -- Apply Unrestricted_Access to caller's return object
268 Make_Attribute_Reference
(Loc
,
269 Prefix
=> Return_Object
,
270 Attribute_Name
=> Name_Unrestricted_Access
);
272 Set_Parent
(Return_Object
, Obj_Address
);
273 Set_Parent
(Obj_Address
, Function_Call
);
276 Analyze_And_Resolve
(Obj_Address
, Etype
(Obj_Acc_Formal
));
278 -- Build the parameter association for the new actual and add it to the
279 -- end of the function's actuals.
281 Add_Extra_Actual_To_Call
(Function_Call
, Obj_Acc_Formal
, Obj_Address
);
282 end Add_Access_Actual_To_Build_In_Place_Call
;
284 ------------------------------------------------------
285 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
286 ------------------------------------------------------
288 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
289 (Function_Call
: Node_Id
;
290 Function_Id
: Entity_Id
;
291 Alloc_Form
: BIP_Allocation_Form
:= Unspecified
;
292 Alloc_Form_Exp
: Node_Id
:= Empty
;
293 Pool_Actual
: Node_Id
:= Make_Null
(No_Location
))
295 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
296 Alloc_Form_Actual
: Node_Id
;
297 Alloc_Form_Formal
: Node_Id
;
298 Pool_Formal
: Node_Id
;
301 -- The allocation form generally doesn't need to be passed in the case
302 -- of a constrained result subtype, since normally the caller performs
303 -- the allocation in that case. However this formal is still needed in
304 -- the case where the function has a tagged result, because generally
305 -- such functions can be called in a dispatching context and such calls
306 -- must be handled like calls to class-wide functions.
308 if Is_Constrained
(Underlying_Type
(Etype
(Function_Id
)))
309 and then not Is_Tagged_Type
(Underlying_Type
(Etype
(Function_Id
)))
314 -- Locate the implicit allocation form parameter in the called function.
315 -- Maybe it would be better for each implicit formal of a build-in-place
316 -- function to have a flag or a Uint attribute to identify it. ???
318 Alloc_Form_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Alloc_Form
);
320 if Present
(Alloc_Form_Exp
) then
321 pragma Assert
(Alloc_Form
= Unspecified
);
323 Alloc_Form_Actual
:= Alloc_Form_Exp
;
326 pragma Assert
(Alloc_Form
/= Unspecified
);
329 Make_Integer_Literal
(Loc
,
330 Intval
=> UI_From_Int
(BIP_Allocation_Form
'Pos (Alloc_Form
)));
333 Analyze_And_Resolve
(Alloc_Form_Actual
, Etype
(Alloc_Form_Formal
));
335 -- Build the parameter association for the new actual and add it to the
336 -- end of the function's actuals.
338 Add_Extra_Actual_To_Call
339 (Function_Call
, Alloc_Form_Formal
, Alloc_Form_Actual
);
341 -- Pass the Storage_Pool parameter. This parameter is omitted on
342 -- .NET/JVM/ZFP as those targets do not support pools.
345 and then RTE_Available
(RE_Root_Storage_Pool_Ptr
)
347 Pool_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Storage_Pool
);
348 Analyze_And_Resolve
(Pool_Actual
, Etype
(Pool_Formal
));
349 Add_Extra_Actual_To_Call
350 (Function_Call
, Pool_Formal
, Pool_Actual
);
352 end Add_Unconstrained_Actuals_To_Build_In_Place_Call
;
354 -----------------------------------------------------------
355 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
356 -----------------------------------------------------------
358 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
359 (Func_Call
: Node_Id
;
361 Ptr_Typ
: Entity_Id
:= Empty
;
362 Master_Exp
: Node_Id
:= Empty
)
365 if not Needs_BIP_Finalization_Master
(Func_Id
) then
370 Formal
: constant Entity_Id
:=
371 Build_In_Place_Formal
(Func_Id
, BIP_Finalization_Master
);
372 Loc
: constant Source_Ptr
:= Sloc
(Func_Call
);
375 Desig_Typ
: Entity_Id
;
378 -- If there is a finalization master actual, such as the implicit
379 -- finalization master of an enclosing build-in-place function,
380 -- then this must be added as an extra actual of the call.
382 if Present
(Master_Exp
) then
383 Actual
:= Master_Exp
;
385 -- Case where the context does not require an actual master
387 elsif No
(Ptr_Typ
) then
388 Actual
:= Make_Null
(Loc
);
391 Desig_Typ
:= Directly_Designated_Type
(Ptr_Typ
);
393 -- Check for a library-level access type whose designated type has
394 -- supressed finalization. Such an access types lack a master.
395 -- Pass a null actual to the callee in order to signal a missing
398 if Is_Library_Level_Entity
(Ptr_Typ
)
399 and then Finalize_Storage_Only
(Desig_Typ
)
401 Actual
:= Make_Null
(Loc
);
403 -- Types in need of finalization actions
405 elsif Needs_Finalization
(Desig_Typ
) then
407 -- The general mechanism of creating finalization masters for
408 -- anonymous access types is disabled by default, otherwise
409 -- finalization masters will pop all over the place. Such types
410 -- use context-specific masters.
412 if Ekind
(Ptr_Typ
) = E_Anonymous_Access_Type
413 and then No
(Finalization_Master
(Ptr_Typ
))
415 Build_Finalization_Master
417 For_Anonymous
=> True,
418 Context_Scope
=> Scope
(Ptr_Typ
),
419 Insertion_Node
=> Associated_Node_For_Itype
(Ptr_Typ
));
422 -- Access-to-controlled types should always have a master
424 pragma Assert
(Present
(Finalization_Master
(Ptr_Typ
)));
427 Make_Attribute_Reference
(Loc
,
429 New_Occurrence_Of
(Finalization_Master
(Ptr_Typ
), Loc
),
430 Attribute_Name
=> Name_Unrestricted_Access
);
435 Actual
:= Make_Null
(Loc
);
439 Analyze_And_Resolve
(Actual
, Etype
(Formal
));
441 -- Build the parameter association for the new actual and add it to
442 -- the end of the function's actuals.
444 Add_Extra_Actual_To_Call
(Func_Call
, Formal
, Actual
);
446 end Add_Finalization_Master_Actual_To_Build_In_Place_Call
;
448 ------------------------------
449 -- Add_Extra_Actual_To_Call --
450 ------------------------------
452 procedure Add_Extra_Actual_To_Call
453 (Subprogram_Call
: Node_Id
;
454 Extra_Formal
: Entity_Id
;
455 Extra_Actual
: Node_Id
)
457 Loc
: constant Source_Ptr
:= Sloc
(Subprogram_Call
);
458 Param_Assoc
: Node_Id
;
462 Make_Parameter_Association
(Loc
,
463 Selector_Name
=> New_Occurrence_Of
(Extra_Formal
, Loc
),
464 Explicit_Actual_Parameter
=> Extra_Actual
);
466 Set_Parent
(Param_Assoc
, Subprogram_Call
);
467 Set_Parent
(Extra_Actual
, Param_Assoc
);
469 if Present
(Parameter_Associations
(Subprogram_Call
)) then
470 if Nkind
(Last
(Parameter_Associations
(Subprogram_Call
))) =
471 N_Parameter_Association
474 -- Find last named actual, and append
479 L
:= First_Actual
(Subprogram_Call
);
480 while Present
(L
) loop
481 if No
(Next_Actual
(L
)) then
482 Set_Next_Named_Actual
(Parent
(L
), Extra_Actual
);
490 Set_First_Named_Actual
(Subprogram_Call
, Extra_Actual
);
493 Append
(Param_Assoc
, To
=> Parameter_Associations
(Subprogram_Call
));
496 Set_Parameter_Associations
(Subprogram_Call
, New_List
(Param_Assoc
));
497 Set_First_Named_Actual
(Subprogram_Call
, Extra_Actual
);
499 end Add_Extra_Actual_To_Call
;
501 ---------------------------------------------
502 -- Add_Task_Actuals_To_Build_In_Place_Call --
503 ---------------------------------------------
505 procedure Add_Task_Actuals_To_Build_In_Place_Call
506 (Function_Call
: Node_Id
;
507 Function_Id
: Entity_Id
;
508 Master_Actual
: Node_Id
;
509 Chain
: Node_Id
:= Empty
)
511 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
512 Result_Subt
: constant Entity_Id
:=
513 Available_View
(Etype
(Function_Id
));
515 Chain_Actual
: Node_Id
;
516 Chain_Formal
: Node_Id
;
517 Master_Formal
: Node_Id
;
520 -- No such extra parameters are needed if there are no tasks
522 if not Has_Task
(Result_Subt
) then
526 Actual
:= Master_Actual
;
528 -- Use a dummy _master actual in case of No_Task_Hierarchy
530 if Restriction_Active
(No_Task_Hierarchy
) then
531 Actual
:= New_Occurrence_Of
(RTE
(RE_Library_Task_Level
), Loc
);
533 -- In the case where we use the master associated with an access type,
534 -- the actual is an entity and requires an explicit reference.
536 elsif Nkind
(Actual
) = N_Defining_Identifier
then
537 Actual
:= New_Occurrence_Of
(Actual
, Loc
);
540 -- Locate the implicit master parameter in the called function
542 Master_Formal
:= Build_In_Place_Formal
(Function_Id
, BIP_Task_Master
);
543 Analyze_And_Resolve
(Actual
, Etype
(Master_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
, Master_Formal
, Actual
);
550 -- Locate the implicit activation chain parameter in the called function
553 Build_In_Place_Formal
(Function_Id
, BIP_Activation_Chain
);
555 -- Create the actual which is a pointer to the current activation chain
559 Make_Attribute_Reference
(Loc
,
560 Prefix
=> Make_Identifier
(Loc
, Name_uChain
),
561 Attribute_Name
=> Name_Unrestricted_Access
);
563 -- Allocator case; make a reference to the Chain passed in by the caller
567 Make_Attribute_Reference
(Loc
,
568 Prefix
=> New_Occurrence_Of
(Chain
, Loc
),
569 Attribute_Name
=> Name_Unrestricted_Access
);
572 Analyze_And_Resolve
(Chain_Actual
, Etype
(Chain_Formal
));
574 -- Build the parameter association for the new actual and add it to the
575 -- end of the function's actuals.
577 Add_Extra_Actual_To_Call
(Function_Call
, Chain_Formal
, Chain_Actual
);
578 end Add_Task_Actuals_To_Build_In_Place_Call
;
580 -----------------------
581 -- BIP_Formal_Suffix --
582 -----------------------
584 function BIP_Formal_Suffix
(Kind
: BIP_Formal_Kind
) return String is
587 when BIP_Alloc_Form
=>
589 when BIP_Storage_Pool
=>
590 return "BIPstoragepool";
591 when BIP_Finalization_Master
=>
592 return "BIPfinalizationmaster";
593 when BIP_Task_Master
=>
594 return "BIPtaskmaster";
595 when BIP_Activation_Chain
=>
596 return "BIPactivationchain";
597 when BIP_Object_Access
=>
600 end BIP_Formal_Suffix
;
602 ---------------------------
603 -- Build_In_Place_Formal --
604 ---------------------------
606 function Build_In_Place_Formal
608 Kind
: BIP_Formal_Kind
) return Entity_Id
610 Formal_Name
: constant Name_Id
:=
612 (Chars
(Func
), BIP_Formal_Suffix
(Kind
));
613 Extra_Formal
: Entity_Id
:= Extra_Formals
(Func
);
616 -- Maybe it would be better for each implicit formal of a build-in-place
617 -- function to have a flag or a Uint attribute to identify it. ???
619 -- The return type in the function declaration may have been a limited
620 -- view, and the extra formals for the function were not generated at
621 -- that point. At the point of call the full view must be available and
622 -- the extra formals can be created.
624 if No
(Extra_Formal
) then
625 Create_Extra_Formals
(Func
);
626 Extra_Formal
:= Extra_Formals
(Func
);
630 pragma Assert
(Present
(Extra_Formal
));
631 exit when Chars
(Extra_Formal
) = Formal_Name
;
633 Next_Formal_With_Extras
(Extra_Formal
);
637 end Build_In_Place_Formal
;
639 --------------------------------
640 -- Check_Overriding_Operation --
641 --------------------------------
643 procedure Check_Overriding_Operation
(Subp
: Entity_Id
) is
644 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
645 Op_List
: constant Elist_Id
:= Primitive_Operations
(Typ
);
651 if Is_Derived_Type
(Typ
)
652 and then not Is_Private_Type
(Typ
)
653 and then In_Open_Scopes
(Scope
(Etype
(Typ
)))
654 and then Is_Base_Type
(Typ
)
656 -- Subp overrides an inherited private operation if there is an
657 -- inherited operation with a different name than Subp (see
658 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
659 -- same name as Subp.
661 Op_Elmt
:= First_Elmt
(Op_List
);
662 while Present
(Op_Elmt
) loop
663 Prim_Op
:= Node
(Op_Elmt
);
664 Par_Op
:= Alias
(Prim_Op
);
667 and then not Comes_From_Source
(Prim_Op
)
668 and then Chars
(Prim_Op
) /= Chars
(Par_Op
)
669 and then Chars
(Par_Op
) = Chars
(Subp
)
670 and then Is_Hidden
(Par_Op
)
671 and then Type_Conformant
(Prim_Op
, Subp
)
673 Set_DT_Position
(Subp
, DT_Position
(Prim_Op
));
679 end Check_Overriding_Operation
;
681 -------------------------------
682 -- Detect_Infinite_Recursion --
683 -------------------------------
685 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
) is
686 Loc
: constant Source_Ptr
:= Sloc
(N
);
688 Var_List
: constant Elist_Id
:= New_Elmt_List
;
689 -- List of globals referenced by body of procedure
691 Call_List
: constant Elist_Id
:= New_Elmt_List
;
692 -- List of recursive calls in body of procedure
694 Shad_List
: constant Elist_Id
:= New_Elmt_List
;
695 -- List of entity id's for entities created to capture the value of
696 -- referenced globals on entry to the procedure.
698 Scop
: constant Uint
:= Scope_Depth
(Spec
);
699 -- This is used to record the scope depth of the current procedure, so
700 -- that we can identify global references.
702 Max_Vars
: constant := 4;
703 -- Do not test more than four global variables
705 Count_Vars
: Natural := 0;
706 -- Count variables found so far
718 function Process
(Nod
: Node_Id
) return Traverse_Result
;
719 -- Function to traverse the subprogram body (using Traverse_Func)
725 function Process
(Nod
: Node_Id
) return Traverse_Result
is
729 if Nkind
(Nod
) = N_Procedure_Call_Statement
then
731 -- Case of one of the detected recursive calls
733 if Is_Entity_Name
(Name
(Nod
))
734 and then Has_Recursive_Call
(Entity
(Name
(Nod
)))
735 and then Entity
(Name
(Nod
)) = Spec
737 Append_Elmt
(Nod
, Call_List
);
740 -- Any other procedure call may have side effects
746 -- A call to a pure function can always be ignored
748 elsif Nkind
(Nod
) = N_Function_Call
749 and then Is_Entity_Name
(Name
(Nod
))
750 and then Is_Pure
(Entity
(Name
(Nod
)))
754 -- Case of an identifier reference
756 elsif Nkind
(Nod
) = N_Identifier
then
759 -- If no entity, then ignore the reference
761 -- Not clear why this can happen. To investigate, remove this
762 -- test and look at the crash that occurs here in 3401-004 ???
767 -- Ignore entities with no Scope, again not clear how this
768 -- can happen, to investigate, look at 4108-008 ???
770 elsif No
(Scope
(Ent
)) then
773 -- Ignore the reference if not to a more global object
775 elsif Scope_Depth
(Scope
(Ent
)) >= Scop
then
778 -- References to types, exceptions and constants are always OK
781 or else Ekind
(Ent
) = E_Exception
782 or else Ekind
(Ent
) = E_Constant
786 -- If other than a non-volatile scalar variable, we have some
787 -- kind of global reference (e.g. to a function) that we cannot
788 -- deal with so we forget the attempt.
790 elsif Ekind
(Ent
) /= E_Variable
791 or else not Is_Scalar_Type
(Etype
(Ent
))
792 or else Treat_As_Volatile
(Ent
)
796 -- Otherwise we have a reference to a global scalar
799 -- Loop through global entities already detected
801 Elm
:= First_Elmt
(Var_List
);
803 -- If not detected before, record this new global reference
806 Count_Vars
:= Count_Vars
+ 1;
808 if Count_Vars
<= Max_Vars
then
809 Append_Elmt
(Entity
(Nod
), Var_List
);
816 -- If recorded before, ignore
818 elsif Node
(Elm
) = Entity
(Nod
) then
821 -- Otherwise keep looking
831 -- For all other node kinds, recursively visit syntactic children
838 function Traverse_Body
is new Traverse_Func
(Process
);
840 -- Start of processing for Detect_Infinite_Recursion
843 -- Do not attempt detection in No_Implicit_Conditional mode, since we
844 -- won't be able to generate the code to handle the recursion in any
847 if Restriction_Active
(No_Implicit_Conditionals
) then
851 -- Otherwise do traversal and quit if we get abandon signal
853 if Traverse_Body
(N
) = Abandon
then
856 -- We must have a call, since Has_Recursive_Call was set. If not just
857 -- ignore (this is only an error check, so if we have a funny situation,
858 -- due to bugs or errors, we do not want to bomb).
860 elsif Is_Empty_Elmt_List
(Call_List
) then
864 -- Here is the case where we detect recursion at compile time
866 -- Push our current scope for analyzing the declarations and code that
867 -- we will insert for the checking.
871 -- This loop builds temporary variables for each of the referenced
872 -- globals, so that at the end of the loop the list Shad_List contains
873 -- these temporaries in one-to-one correspondence with the elements in
877 Elm
:= First_Elmt
(Var_List
);
878 while Present
(Elm
) loop
880 Ent
:= Make_Temporary
(Loc
, 'S');
881 Append_Elmt
(Ent
, Shad_List
);
883 -- Insert a declaration for this temporary at the start of the
884 -- declarations for the procedure. The temporaries are declared as
885 -- constant objects initialized to the current values of the
886 -- corresponding temporaries.
889 Make_Object_Declaration
(Loc
,
890 Defining_Identifier
=> Ent
,
891 Object_Definition
=> New_Occurrence_Of
(Etype
(Var
), Loc
),
892 Constant_Present
=> True,
893 Expression
=> New_Occurrence_Of
(Var
, Loc
));
896 Prepend
(Decl
, Declarations
(N
));
898 Insert_After
(Last
, Decl
);
906 -- Loop through calls
908 Call
:= First_Elmt
(Call_List
);
909 while Present
(Call
) loop
911 -- Build a predicate expression of the form
914 -- and then global1 = temp1
915 -- and then global2 = temp2
918 -- This predicate determines if any of the global values
919 -- referenced by the procedure have changed since the
920 -- current call, if not an infinite recursion is assured.
922 Test
:= New_Occurrence_Of
(Standard_True
, Loc
);
924 Elm1
:= First_Elmt
(Var_List
);
925 Elm2
:= First_Elmt
(Shad_List
);
926 while Present
(Elm1
) loop
932 Left_Opnd
=> New_Occurrence_Of
(Node
(Elm1
), Loc
),
933 Right_Opnd
=> New_Occurrence_Of
(Node
(Elm2
), Loc
)));
939 -- Now we replace the call with the sequence
941 -- if no-changes (see above) then
942 -- raise Storage_Error;
947 Rewrite
(Node
(Call
),
948 Make_If_Statement
(Loc
,
950 Then_Statements
=> New_List
(
951 Make_Raise_Storage_Error
(Loc
,
952 Reason
=> SE_Infinite_Recursion
)),
954 Else_Statements
=> New_List
(
955 Relocate_Node
(Node
(Call
)))));
957 Analyze
(Node
(Call
));
962 -- Remove temporary scope stack entry used for analysis
965 end Detect_Infinite_Recursion
;
975 procedure Expand_Actuals
(N
: in out Node_Id
; Subp
: Entity_Id
) is
976 Loc
: constant Source_Ptr
:= Sloc
(N
);
981 E_Actual
: Entity_Id
;
982 E_Formal
: Entity_Id
;
984 procedure Add_Call_By_Copy_Code
;
985 -- For cases where the parameter must be passed by copy, this routine
986 -- generates a temporary variable into which the actual is copied and
987 -- then passes this as the parameter. For an OUT or IN OUT parameter,
988 -- an assignment is also generated to copy the result back. The call
989 -- also takes care of any constraint checks required for the type
990 -- conversion case (on both the way in and the way out).
992 procedure Add_Simple_Call_By_Copy_Code
;
993 -- This is similar to the above, but is used in cases where we know
994 -- that all that is needed is to simply create a temporary and copy
995 -- the value in and out of the temporary.
997 procedure Check_Fortran_Logical
;
998 -- A value of type Logical that is passed through a formal parameter
999 -- must be normalized because .TRUE. usually does not have the same
1000 -- representation as True. We assume that .FALSE. = False = 0.
1001 -- What about functions that return a logical type ???
1003 function Is_Legal_Copy
return Boolean;
1004 -- Check that an actual can be copied before generating the temporary
1005 -- to be used in the call. If the actual is of a by_reference type then
1006 -- the program is illegal (this can only happen in the presence of
1007 -- rep. clauses that force an incorrect alignment). If the formal is
1008 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
1009 -- the effect that this might lead to unaligned arguments.
1011 function Make_Var
(Actual
: Node_Id
) return Entity_Id
;
1012 -- Returns an entity that refers to the given actual parameter, Actual
1013 -- (not including any type conversion). If Actual is an entity name,
1014 -- then this entity is returned unchanged, otherwise a renaming is
1015 -- created to provide an entity for the actual.
1017 procedure Reset_Packed_Prefix
;
1018 -- The expansion of a packed array component reference is delayed in
1019 -- the context of a call. Now we need to complete the expansion, so we
1020 -- unmark the analyzed bits in all prefixes.
1022 ---------------------------
1023 -- Add_Call_By_Copy_Code --
1024 ---------------------------
1026 procedure Add_Call_By_Copy_Code
is
1032 F_Typ
: constant Entity_Id
:= Etype
(Formal
);
1037 if not Is_Legal_Copy
then
1041 Temp
:= Make_Temporary
(Loc
, 'T', Actual
);
1043 -- Use formal type for temp, unless formal type is an unconstrained
1044 -- array, in which case we don't have to worry about bounds checks,
1045 -- and we use the actual type, since that has appropriate bounds.
1047 if Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
1048 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1050 Indic
:= New_Occurrence_Of
(Etype
(Formal
), Loc
);
1053 if Nkind
(Actual
) = N_Type_Conversion
then
1054 V_Typ
:= Etype
(Expression
(Actual
));
1056 -- If the formal is an (in-)out parameter, capture the name
1057 -- of the variable in order to build the post-call assignment.
1059 Var
:= Make_Var
(Expression
(Actual
));
1061 Crep
:= not Same_Representation
1062 (F_Typ
, Etype
(Expression
(Actual
)));
1065 V_Typ
:= Etype
(Actual
);
1066 Var
:= Make_Var
(Actual
);
1070 -- Setup initialization for case of in out parameter, or an out
1071 -- parameter where the formal is an unconstrained array (in the
1072 -- latter case, we have to pass in an object with bounds).
1074 -- If this is an out parameter, the initial copy is wasteful, so as
1075 -- an optimization for the one-dimensional case we extract the
1076 -- bounds of the actual and build an uninitialized temporary of the
1079 if Ekind
(Formal
) = E_In_Out_Parameter
1080 or else (Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
))
1082 if Nkind
(Actual
) = N_Type_Conversion
then
1083 if Conversion_OK
(Actual
) then
1084 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1086 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1089 elsif Ekind
(Formal
) = E_Out_Parameter
1090 and then Is_Array_Type
(F_Typ
)
1091 and then Number_Dimensions
(F_Typ
) = 1
1092 and then not Has_Non_Null_Base_Init_Proc
(F_Typ
)
1094 -- Actual is a one-dimensional array or slice, and the type
1095 -- requires no initialization. Create a temporary of the
1096 -- right size, but do not copy actual into it (optimization).
1100 Make_Subtype_Indication
(Loc
,
1101 Subtype_Mark
=> New_Occurrence_Of
(F_Typ
, Loc
),
1103 Make_Index_Or_Discriminant_Constraint
(Loc
,
1104 Constraints
=> New_List
(
1107 Make_Attribute_Reference
(Loc
,
1108 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
1109 Attribute_Name
=> Name_First
),
1111 Make_Attribute_Reference
(Loc
,
1112 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
1113 Attribute_Name
=> Name_Last
)))));
1116 Init
:= New_Occurrence_Of
(Var
, Loc
);
1119 -- An initialization is created for packed conversions as
1120 -- actuals for out parameters to enable Make_Object_Declaration
1121 -- to determine the proper subtype for N_Node. Note that this
1122 -- is wasteful because the extra copying on the call side is
1123 -- not required for such out parameters. ???
1125 elsif Ekind
(Formal
) = E_Out_Parameter
1126 and then Nkind
(Actual
) = N_Type_Conversion
1127 and then (Is_Bit_Packed_Array
(F_Typ
)
1129 Is_Bit_Packed_Array
(Etype
(Expression
(Actual
))))
1131 if Conversion_OK
(Actual
) then
1132 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1134 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1137 elsif Ekind
(Formal
) = E_In_Parameter
then
1139 -- Handle the case in which the actual is a type conversion
1141 if Nkind
(Actual
) = N_Type_Conversion
then
1142 if Conversion_OK
(Actual
) then
1143 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1145 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
1148 Init
:= New_Occurrence_Of
(Var
, Loc
);
1156 Make_Object_Declaration
(Loc
,
1157 Defining_Identifier
=> Temp
,
1158 Object_Definition
=> Indic
,
1159 Expression
=> Init
);
1160 Set_Assignment_OK
(N_Node
);
1161 Insert_Action
(N
, N_Node
);
1163 -- Now, normally the deal here is that we use the defining
1164 -- identifier created by that object declaration. There is
1165 -- one exception to this. In the change of representation case
1166 -- the above declaration will end up looking like:
1168 -- temp : type := identifier;
1170 -- And in this case we might as well use the identifier directly
1171 -- and eliminate the temporary. Note that the analysis of the
1172 -- declaration was not a waste of time in that case, since it is
1173 -- what generated the necessary change of representation code. If
1174 -- the change of representation introduced additional code, as in
1175 -- a fixed-integer conversion, the expression is not an identifier
1176 -- and must be kept.
1179 and then Present
(Expression
(N_Node
))
1180 and then Is_Entity_Name
(Expression
(N_Node
))
1182 Temp
:= Entity
(Expression
(N_Node
));
1183 Rewrite
(N_Node
, Make_Null_Statement
(Loc
));
1186 -- For IN parameter, all we do is to replace the actual
1188 if Ekind
(Formal
) = E_In_Parameter
then
1189 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
1192 -- Processing for OUT or IN OUT parameter
1195 -- Kill current value indications for the temporary variable we
1196 -- created, since we just passed it as an OUT parameter.
1198 Kill_Current_Values
(Temp
);
1199 Set_Is_Known_Valid
(Temp
, False);
1201 -- If type conversion, use reverse conversion on exit
1203 if Nkind
(Actual
) = N_Type_Conversion
then
1204 if Conversion_OK
(Actual
) then
1205 Expr
:= OK_Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
1207 Expr
:= Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
1210 Expr
:= New_Occurrence_Of
(Temp
, Loc
);
1213 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
1216 -- If the actual is a conversion of a packed reference, it may
1217 -- already have been expanded by Remove_Side_Effects, and the
1218 -- resulting variable is a temporary which does not designate
1219 -- the proper out-parameter, which may not be addressable. In
1220 -- that case, generate an assignment to the original expression
1221 -- (before expansion of the packed reference) so that the proper
1222 -- expansion of assignment to a packed component can take place.
1229 if Is_Renaming_Of_Object
(Var
)
1230 and then Nkind
(Renamed_Object
(Var
)) = N_Selected_Component
1231 and then Is_Entity_Name
(Prefix
(Renamed_Object
(Var
)))
1232 and then Nkind
(Original_Node
(Prefix
(Renamed_Object
(Var
))))
1233 = N_Indexed_Component
1235 Has_Non_Standard_Rep
(Etype
(Prefix
(Renamed_Object
(Var
))))
1237 Obj
:= Renamed_Object
(Var
);
1239 Make_Selected_Component
(Loc
,
1241 New_Copy_Tree
(Original_Node
(Prefix
(Obj
))),
1242 Selector_Name
=> New_Copy
(Selector_Name
(Obj
)));
1243 Reset_Analyzed_Flags
(Lhs
);
1246 Lhs
:= New_Occurrence_Of
(Var
, Loc
);
1249 Set_Assignment_OK
(Lhs
);
1251 if Is_Access_Type
(E_Formal
)
1252 and then Is_Entity_Name
(Lhs
)
1254 Present
(Effective_Extra_Accessibility
(Entity
(Lhs
)))
1256 -- Copyback target is an Ada 2012 stand-alone object of an
1257 -- anonymous access type.
1259 pragma Assert
(Ada_Version
>= Ada_2012
);
1261 if Type_Access_Level
(E_Formal
) >
1262 Object_Access_Level
(Lhs
)
1264 Append_To
(Post_Call
,
1265 Make_Raise_Program_Error
(Loc
,
1266 Reason
=> PE_Accessibility_Check_Failed
));
1269 Append_To
(Post_Call
,
1270 Make_Assignment_Statement
(Loc
,
1272 Expression
=> Expr
));
1274 -- We would like to somehow suppress generation of the
1275 -- extra_accessibility assignment generated by the expansion
1276 -- of the above assignment statement. It's not a correctness
1277 -- issue because the following assignment renders it dead,
1278 -- but generating back-to-back assignments to the same
1279 -- target is undesirable. ???
1281 Append_To
(Post_Call
,
1282 Make_Assignment_Statement
(Loc
,
1283 Name
=> New_Occurrence_Of
(
1284 Effective_Extra_Accessibility
(Entity
(Lhs
)), Loc
),
1285 Expression
=> Make_Integer_Literal
(Loc
,
1286 Type_Access_Level
(E_Formal
))));
1289 Append_To
(Post_Call
,
1290 Make_Assignment_Statement
(Loc
,
1292 Expression
=> Expr
));
1296 end Add_Call_By_Copy_Code
;
1298 ----------------------------------
1299 -- Add_Simple_Call_By_Copy_Code --
1300 ----------------------------------
1302 procedure Add_Simple_Call_By_Copy_Code
is
1310 F_Typ
: constant Entity_Id
:= Etype
(Formal
);
1313 if not Is_Legal_Copy
then
1317 -- Use formal type for temp, unless formal type is an unconstrained
1318 -- array, in which case we don't have to worry about bounds checks,
1319 -- and we use the actual type, since that has appropriate bounds.
1321 if Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
1322 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1324 Indic
:= New_Occurrence_Of
(Etype
(Formal
), Loc
);
1327 -- Prepare to generate code
1329 Reset_Packed_Prefix
;
1331 Temp
:= Make_Temporary
(Loc
, 'T', Actual
);
1332 Incod
:= Relocate_Node
(Actual
);
1333 Outcod
:= New_Copy_Tree
(Incod
);
1335 -- Generate declaration of temporary variable, initializing it
1336 -- with the input parameter unless we have an OUT formal or
1337 -- this is an initialization call.
1339 -- If the formal is an out parameter with discriminants, the
1340 -- discriminants must be captured even if the rest of the object
1341 -- is in principle uninitialized, because the discriminants may
1342 -- be read by the called subprogram.
1344 if Ekind
(Formal
) = E_Out_Parameter
then
1347 if Has_Discriminants
(Etype
(Formal
)) then
1348 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
1351 elsif Inside_Init_Proc
then
1353 -- Could use a comment here to match comment below ???
1355 if Nkind
(Actual
) /= N_Selected_Component
1357 not Has_Discriminant_Dependent_Constraint
1358 (Entity
(Selector_Name
(Actual
)))
1362 -- Otherwise, keep the component in order to generate the proper
1363 -- actual subtype, that depends on enclosing discriminants.
1371 Make_Object_Declaration
(Loc
,
1372 Defining_Identifier
=> Temp
,
1373 Object_Definition
=> Indic
,
1374 Expression
=> Incod
);
1379 -- If the call is to initialize a component of a composite type,
1380 -- and the component does not depend on discriminants, use the
1381 -- actual type of the component. This is required in case the
1382 -- component is constrained, because in general the formal of the
1383 -- initialization procedure will be unconstrained. Note that if
1384 -- the component being initialized is constrained by an enclosing
1385 -- discriminant, the presence of the initialization in the
1386 -- declaration will generate an expression for the actual subtype.
1388 Set_No_Initialization
(Decl
);
1389 Set_Object_Definition
(Decl
,
1390 New_Occurrence_Of
(Etype
(Actual
), Loc
));
1393 Insert_Action
(N
, Decl
);
1395 -- The actual is simply a reference to the temporary
1397 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
1399 -- Generate copy out if OUT or IN OUT parameter
1401 if Ekind
(Formal
) /= E_In_Parameter
then
1403 Rhs
:= New_Occurrence_Of
(Temp
, Loc
);
1405 -- Deal with conversion
1407 if Nkind
(Lhs
) = N_Type_Conversion
then
1408 Lhs
:= Expression
(Lhs
);
1409 Rhs
:= Convert_To
(Etype
(Actual
), Rhs
);
1412 Append_To
(Post_Call
,
1413 Make_Assignment_Statement
(Loc
,
1415 Expression
=> Rhs
));
1416 Set_Assignment_OK
(Name
(Last
(Post_Call
)));
1418 end Add_Simple_Call_By_Copy_Code
;
1420 ---------------------------
1421 -- Check_Fortran_Logical --
1422 ---------------------------
1424 procedure Check_Fortran_Logical
is
1425 Logical
: constant Entity_Id
:= Etype
(Formal
);
1428 -- Note: this is very incomplete, e.g. it does not handle arrays
1429 -- of logical values. This is really not the right approach at all???)
1432 if Convention
(Subp
) = Convention_Fortran
1433 and then Root_Type
(Etype
(Formal
)) = Standard_Boolean
1434 and then Ekind
(Formal
) /= E_In_Parameter
1436 Var
:= Make_Var
(Actual
);
1437 Append_To
(Post_Call
,
1438 Make_Assignment_Statement
(Loc
,
1439 Name
=> New_Occurrence_Of
(Var
, Loc
),
1441 Unchecked_Convert_To
(
1444 Left_Opnd
=> New_Occurrence_Of
(Var
, Loc
),
1446 Unchecked_Convert_To
(
1448 New_Occurrence_Of
(Standard_False
, Loc
))))));
1450 end Check_Fortran_Logical
;
1456 function Is_Legal_Copy
return Boolean is
1458 -- An attempt to copy a value of such a type can only occur if
1459 -- representation clauses give the actual a misaligned address.
1461 if Is_By_Reference_Type
(Etype
(Formal
)) then
1463 -- If the front-end does not perform full type layout, the actual
1464 -- may in fact be properly aligned but there is not enough front-
1465 -- end information to determine this. In that case gigi will emit
1466 -- an error if a copy is not legal, or generate the proper code.
1467 -- For other backends we report the error now.
1469 -- Seems wrong to be issuing an error in the expander, since it
1470 -- will be missed in -gnatc mode ???
1472 if Frontend_Layout_On_Target
then
1474 ("misaligned actual cannot be passed by reference", Actual
);
1479 -- For users of Starlet, we assume that the specification of by-
1480 -- reference mechanism is mandatory. This may lead to unaligned
1481 -- objects but at least for DEC legacy code it is known to work.
1482 -- The warning will alert users of this code that a problem may
1485 elsif Mechanism
(Formal
) = By_Reference
1486 and then Is_Valued_Procedure
(Scope
(Formal
))
1489 ("by_reference actual may be misaligned??", Actual
);
1501 function Make_Var
(Actual
: Node_Id
) return Entity_Id
is
1505 if Is_Entity_Name
(Actual
) then
1506 return Entity
(Actual
);
1509 Var
:= Make_Temporary
(Loc
, 'T', Actual
);
1512 Make_Object_Renaming_Declaration
(Loc
,
1513 Defining_Identifier
=> Var
,
1515 New_Occurrence_Of
(Etype
(Actual
), Loc
),
1516 Name
=> Relocate_Node
(Actual
));
1518 Insert_Action
(N
, N_Node
);
1523 -------------------------
1524 -- Reset_Packed_Prefix --
1525 -------------------------
1527 procedure Reset_Packed_Prefix
is
1528 Pfx
: Node_Id
:= Actual
;
1531 Set_Analyzed
(Pfx
, False);
1533 not Nkind_In
(Pfx
, N_Selected_Component
, N_Indexed_Component
);
1534 Pfx
:= Prefix
(Pfx
);
1536 end Reset_Packed_Prefix
;
1538 -- Start of processing for Expand_Actuals
1541 Post_Call
:= New_List
;
1543 Formal
:= First_Formal
(Subp
);
1544 Actual
:= First_Actual
(N
);
1545 while Present
(Formal
) loop
1546 E_Formal
:= Etype
(Formal
);
1547 E_Actual
:= Etype
(Actual
);
1549 if Is_Scalar_Type
(E_Formal
)
1550 or else Nkind
(Actual
) = N_Slice
1552 Check_Fortran_Logical
;
1556 elsif Ekind
(Formal
) /= E_Out_Parameter
then
1558 -- The unusual case of the current instance of a protected type
1559 -- requires special handling. This can only occur in the context
1560 -- of a call within the body of a protected operation.
1562 if Is_Entity_Name
(Actual
)
1563 and then Ekind
(Entity
(Actual
)) = E_Protected_Type
1564 and then In_Open_Scopes
(Entity
(Actual
))
1566 if Scope
(Subp
) /= Entity
(Actual
) then
1568 ("operation outside protected type may not "
1569 & "call back its protected operations??", Actual
);
1573 Expand_Protected_Object_Reference
(N
, Entity
(Actual
)));
1576 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1577 -- build-in-place function, then a temporary return object needs
1578 -- to be created and access to it must be passed to the function.
1579 -- Currently we limit such functions to those with inherently
1580 -- limited result subtypes, but eventually we plan to expand the
1581 -- functions that are treated as build-in-place to include other
1582 -- composite result types.
1584 if Is_Build_In_Place_Function_Call
(Actual
) then
1585 Make_Build_In_Place_Call_In_Anonymous_Context
(Actual
);
1588 Apply_Constraint_Check
(Actual
, E_Formal
);
1590 -- Out parameter case. No constraint checks on access type
1593 elsif Is_Access_Type
(E_Formal
) then
1598 elsif Has_Discriminants
(Base_Type
(E_Formal
))
1599 or else Has_Non_Null_Base_Init_Proc
(E_Formal
)
1601 Apply_Constraint_Check
(Actual
, E_Formal
);
1606 Apply_Constraint_Check
(Actual
, Base_Type
(E_Formal
));
1609 -- Processing for IN-OUT and OUT parameters
1611 if Ekind
(Formal
) /= E_In_Parameter
then
1613 -- For type conversions of arrays, apply length/range checks
1615 if Is_Array_Type
(E_Formal
)
1616 and then Nkind
(Actual
) = N_Type_Conversion
1618 if Is_Constrained
(E_Formal
) then
1619 Apply_Length_Check
(Expression
(Actual
), E_Formal
);
1621 Apply_Range_Check
(Expression
(Actual
), E_Formal
);
1625 -- If argument is a type conversion for a type that is passed
1626 -- by copy, then we must pass the parameter by copy.
1628 if Nkind
(Actual
) = N_Type_Conversion
1630 (Is_Numeric_Type
(E_Formal
)
1631 or else Is_Access_Type
(E_Formal
)
1632 or else Is_Enumeration_Type
(E_Formal
)
1633 or else Is_Bit_Packed_Array
(Etype
(Formal
))
1634 or else Is_Bit_Packed_Array
(Etype
(Expression
(Actual
)))
1636 -- Also pass by copy if change of representation
1638 or else not Same_Representation
1640 Etype
(Expression
(Actual
))))
1642 Add_Call_By_Copy_Code
;
1644 -- References to components of bit packed arrays are expanded
1645 -- at this point, rather than at the point of analysis of the
1646 -- actuals, to handle the expansion of the assignment to
1647 -- [in] out parameters.
1649 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
1650 Add_Simple_Call_By_Copy_Code
;
1652 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1653 -- because the back-end cannot cope with such objects. In other
1654 -- cases where alignment forces a copy, the back-end generates
1655 -- it properly. It should not be generated unconditionally in the
1656 -- front-end because it does not know precisely the alignment
1657 -- requirements of the target, and makes too conservative an
1658 -- estimate, leading to superfluous copies or spurious errors
1659 -- on by-reference parameters.
1661 elsif Nkind
(Actual
) = N_Selected_Component
1663 Component_May_Be_Bit_Aligned
(Entity
(Selector_Name
(Actual
)))
1664 and then not Represented_As_Scalar
(Etype
(Formal
))
1666 Add_Simple_Call_By_Copy_Code
;
1668 -- References to slices of bit packed arrays are expanded
1670 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
1671 Add_Call_By_Copy_Code
;
1673 -- References to possibly unaligned slices of arrays are expanded
1675 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
1676 Add_Call_By_Copy_Code
;
1678 -- Deal with access types where the actual subtype and the
1679 -- formal subtype are not the same, requiring a check.
1681 -- It is necessary to exclude tagged types because of "downward
1682 -- conversion" errors.
1684 elsif Is_Access_Type
(E_Formal
)
1685 and then not Same_Type
(E_Formal
, E_Actual
)
1686 and then not Is_Tagged_Type
(Designated_Type
(E_Formal
))
1688 Add_Call_By_Copy_Code
;
1690 -- If the actual is not a scalar and is marked for volatile
1691 -- treatment, whereas the formal is not volatile, then pass
1692 -- by copy unless it is a by-reference type.
1694 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1695 -- because this is the enforcement of a language rule that applies
1696 -- only to "real" volatile variables, not e.g. to the address
1697 -- clause overlay case.
1699 elsif Is_Entity_Name
(Actual
)
1700 and then Is_Volatile
(Entity
(Actual
))
1701 and then not Is_By_Reference_Type
(E_Actual
)
1702 and then not Is_Scalar_Type
(Etype
(Entity
(Actual
)))
1703 and then not Is_Volatile
(E_Formal
)
1705 Add_Call_By_Copy_Code
;
1707 elsif Nkind
(Actual
) = N_Indexed_Component
1708 and then Is_Entity_Name
(Prefix
(Actual
))
1709 and then Has_Volatile_Components
(Entity
(Prefix
(Actual
)))
1711 Add_Call_By_Copy_Code
;
1713 -- Add call-by-copy code for the case of scalar out parameters
1714 -- when it is not known at compile time that the subtype of the
1715 -- formal is a subrange of the subtype of the actual (or vice
1716 -- versa for in out parameters), in order to get range checks
1717 -- on such actuals. (Maybe this case should be handled earlier
1718 -- in the if statement???)
1720 elsif Is_Scalar_Type
(E_Formal
)
1722 (not In_Subrange_Of
(E_Formal
, E_Actual
)
1724 (Ekind
(Formal
) = E_In_Out_Parameter
1725 and then not In_Subrange_Of
(E_Actual
, E_Formal
)))
1727 -- Perhaps the setting back to False should be done within
1728 -- Add_Call_By_Copy_Code, since it could get set on other
1729 -- cases occurring above???
1731 if Do_Range_Check
(Actual
) then
1732 Set_Do_Range_Check
(Actual
, False);
1735 Add_Call_By_Copy_Code
;
1738 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
1739 -- by-reference parameters on exit from the call. If the actual
1740 -- is a derived type and the operation is inherited, the body
1741 -- of the operation will not contain a call to the predicate
1742 -- function, so it must be done explicitly after the call. Ditto
1743 -- if the actual is an entity of a predicated subtype.
1745 -- The rule refers to by-reference types, but a check is needed
1746 -- for by-copy types as well. That check is subsumed by the rule
1747 -- for subtype conversion on assignment, but we can generate the
1748 -- required check now.
1750 -- Note also that Subp may be either a subprogram entity for
1751 -- direct calls, or a type entity for indirect calls, which must
1752 -- be handled separately because the name does not denote an
1753 -- overloadable entity.
1755 By_Ref_Predicate_Check
: declare
1756 Aund
: constant Entity_Id
:= Underlying_Type
(E_Actual
);
1759 function Is_Public_Subp
return Boolean;
1760 -- Check whether the subprogram being called is a visible
1761 -- operation of the type of the actual. Used to determine
1762 -- whether an invariant check must be generated on the
1765 ---------------------
1766 -- Is_Public_Subp --
1767 ---------------------
1769 function Is_Public_Subp
return Boolean is
1770 Pack
: constant Entity_Id
:= Scope
(Subp
);
1771 Subp_Decl
: Node_Id
;
1774 if not Is_Subprogram
(Subp
) then
1777 -- The operation may be inherited, or a primitive of the
1781 Nkind_In
(Parent
(Subp
), N_Private_Extension_Declaration
,
1782 N_Full_Type_Declaration
)
1784 Subp_Decl
:= Parent
(Subp
);
1787 Subp_Decl
:= Unit_Declaration_Node
(Subp
);
1790 return Ekind
(Pack
) = E_Package
1792 List_Containing
(Subp_Decl
) =
1793 Visible_Declarations
1794 (Specification
(Unit_Declaration_Node
(Pack
)));
1797 -- Start of processing for By_Ref_Predicate_Check
1806 if Has_Predicates
(Atyp
)
1807 and then Present
(Predicate_Function
(Atyp
))
1809 -- Skip predicate checks for special cases
1811 and then Predicate_Tests_On_Arguments
(Subp
)
1813 Append_To
(Post_Call
,
1814 Make_Predicate_Check
(Atyp
, Actual
));
1817 -- We generated caller-side invariant checks in two cases:
1819 -- a) when calling an inherited operation, where there is an
1820 -- implicit view conversion of the actual to the parent type.
1822 -- b) When the conversion is explicit
1824 -- We treat these cases separately because the required
1825 -- conversion for a) is added later when expanding the call.
1827 if Has_Invariants
(Etype
(Actual
))
1829 Nkind
(Parent
(Subp
)) = N_Private_Extension_Declaration
1831 if Comes_From_Source
(N
) and then Is_Public_Subp
then
1832 Append_To
(Post_Call
, Make_Invariant_Call
(Actual
));
1835 elsif Nkind
(Actual
) = N_Type_Conversion
1836 and then Has_Invariants
(Etype
(Expression
(Actual
)))
1838 if Comes_From_Source
(N
) and then Is_Public_Subp
then
1839 Append_To
(Post_Call
,
1840 Make_Invariant_Call
(Expression
(Actual
)));
1843 end By_Ref_Predicate_Check
;
1845 -- Processing for IN parameters
1848 -- For IN parameters is in the packed array case, we expand an
1849 -- indexed component (the circuit in Exp_Ch4 deliberately left
1850 -- indexed components appearing as actuals untouched, so that
1851 -- the special processing above for the OUT and IN OUT cases
1852 -- could be performed. We could make the test in Exp_Ch4 more
1853 -- complex and have it detect the parameter mode, but it is
1854 -- easier simply to handle all cases here.)
1856 if Nkind
(Actual
) = N_Indexed_Component
1857 and then Is_Packed
(Etype
(Prefix
(Actual
)))
1859 Reset_Packed_Prefix
;
1860 Expand_Packed_Element_Reference
(Actual
);
1862 -- If we have a reference to a bit packed array, we copy it, since
1863 -- the actual must be byte aligned.
1865 -- Is this really necessary in all cases???
1867 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
1868 Add_Simple_Call_By_Copy_Code
;
1870 -- If a non-scalar actual is possibly unaligned, we need a copy
1872 elsif Is_Possibly_Unaligned_Object
(Actual
)
1873 and then not Represented_As_Scalar
(Etype
(Formal
))
1875 Add_Simple_Call_By_Copy_Code
;
1877 -- Similarly, we have to expand slices of packed arrays here
1878 -- because the result must be byte aligned.
1880 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
1881 Add_Call_By_Copy_Code
;
1883 -- Only processing remaining is to pass by copy if this is a
1884 -- reference to a possibly unaligned slice, since the caller
1885 -- expects an appropriately aligned argument.
1887 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
1888 Add_Call_By_Copy_Code
;
1890 -- An unusual case: a current instance of an enclosing task can be
1891 -- an actual, and must be replaced by a reference to self.
1893 elsif Is_Entity_Name
(Actual
)
1894 and then Is_Task_Type
(Entity
(Actual
))
1896 if In_Open_Scopes
(Entity
(Actual
)) then
1898 (Make_Function_Call
(Loc
,
1899 Name
=> New_Occurrence_Of
(RTE
(RE_Self
), Loc
))));
1902 -- A task type cannot otherwise appear as an actual
1905 raise Program_Error
;
1910 Next_Formal
(Formal
);
1911 Next_Actual
(Actual
);
1914 -- Find right place to put post call stuff if it is present
1916 if not Is_Empty_List
(Post_Call
) then
1918 -- Cases where the call is not a member of a statement list
1920 if not Is_List_Member
(N
) then
1922 -- In Ada 2012 the call may be a function call in an expression
1923 -- (since OUT and IN OUT parameters are now allowed for such
1924 -- calls). The write-back of (in)-out parameters is handled
1925 -- by the back-end, but the constraint checks generated when
1926 -- subtypes of formal and actual don't match must be inserted
1927 -- in the form of assignments.
1929 if Ada_Version
>= Ada_2012
1930 and then Nkind
(N
) = N_Function_Call
1932 -- We used to just do handle this by climbing up parents to
1933 -- a non-statement/declaration and then simply making a call
1934 -- to Insert_Actions_After (P, Post_Call), but that doesn't
1935 -- work. If we are in the middle of an expression, e.g. the
1936 -- condition of an IF, this call would insert after the IF
1937 -- statement, which is much too late to be doing the write
1938 -- back. For example:
1940 -- if Clobber (X) then
1941 -- Put_Line (X'Img);
1946 -- Now assume Clobber changes X, if we put the write back
1947 -- after the IF, the Put_Line gets the wrong value and the
1948 -- goto causes the write back to be skipped completely.
1950 -- To deal with this, we replace the call by
1953 -- Tnnn : function-result-type renames function-call;
1954 -- Post_Call actions
1959 -- Note: this won't do in Modify_Tree_For_C mode, but we
1960 -- will deal with that later (it will require creating a
1961 -- declaration for Temp, using Insert_Declaration) ???
1964 Tnnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
1965 FRTyp
: constant Entity_Id
:= Etype
(N
);
1966 Name
: constant Node_Id
:= Relocate_Node
(N
);
1969 Prepend_To
(Post_Call
,
1970 Make_Object_Renaming_Declaration
(Loc
,
1971 Defining_Identifier
=> Tnnn
,
1972 Subtype_Mark
=> New_Occurrence_Of
(FRTyp
, Loc
),
1976 Make_Expression_With_Actions
(Loc
,
1977 Actions
=> Post_Call
,
1978 Expression
=> New_Occurrence_Of
(Tnnn
, Loc
)));
1980 -- We don't want to just blindly call Analyze_And_Resolve
1981 -- because that would cause unwanted recursion on the call.
1982 -- So for a moment set the call as analyzed to prevent that
1983 -- recursion, and get the rest analyzed properly, then reset
1984 -- the analyzed flag, so our caller can continue.
1986 Set_Analyzed
(Name
, True);
1987 Analyze_And_Resolve
(N
, FRTyp
);
1988 Set_Analyzed
(Name
, False);
1990 -- Reset calling argument to point to function call inside
1991 -- the expression with actions so the caller can continue
1992 -- to process the call.
1997 -- If not the special Ada 2012 case of a function call, then
1998 -- we must have the triggering statement of a triggering
1999 -- alternative or an entry call alternative, and we can add
2000 -- the post call stuff to the corresponding statement list.
2008 pragma Assert
(Nkind_In
(P
, N_Triggering_Alternative
,
2009 N_Entry_Call_Alternative
));
2011 if Is_Non_Empty_List
(Statements
(P
)) then
2012 Insert_List_Before_And_Analyze
2013 (First
(Statements
(P
)), Post_Call
);
2015 Set_Statements
(P
, Post_Call
);
2022 -- Otherwise, normal case where N is in a statement sequence,
2023 -- just put the post-call stuff after the call statement.
2026 Insert_Actions_After
(N
, Post_Call
);
2031 -- The call node itself is re-analyzed in Expand_Call
2039 -- This procedure handles expansion of function calls and procedure call
2040 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
2041 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
2043 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
2044 -- Provide values of actuals for all formals in Extra_Formals list
2045 -- Replace "call" to enumeration literal function by literal itself
2046 -- Rewrite call to predefined operator as operator
2047 -- Replace actuals to in-out parameters that are numeric conversions,
2048 -- with explicit assignment to temporaries before and after the call.
2050 -- Note that the list of actuals has been filled with default expressions
2051 -- during semantic analysis of the call. Only the extra actuals required
2052 -- for the 'Constrained attribute and for accessibility checks are added
2055 procedure Expand_Call
(N
: Node_Id
) is
2056 Loc
: constant Source_Ptr
:= Sloc
(N
);
2057 Call_Node
: Node_Id
:= N
;
2058 Extra_Actuals
: List_Id
:= No_List
;
2059 Prev
: Node_Id
:= Empty
;
2061 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
);
2062 -- Adds one entry to the end of the actual parameter list. Used for
2063 -- default parameters and for extra actuals (for Extra_Formals). The
2064 -- argument is an N_Parameter_Association node.
2066 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
);
2067 -- Adds an extra actual to the list of extra actuals. Expr is the
2068 -- expression for the value of the actual, EF is the entity for the
2071 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
;
2072 -- Within an instance, a type derived from an untagged formal derived
2073 -- type inherits from the original parent, not from the actual. The
2074 -- current derivation mechanism has the derived type inherit from the
2075 -- actual, which is only correct outside of the instance. If the
2076 -- subprogram is inherited, we test for this particular case through a
2077 -- convoluted tree traversal before setting the proper subprogram to be
2080 function In_Unfrozen_Instance
(E
: Entity_Id
) return Boolean;
2081 -- Return true if E comes from an instance that is not yet frozen
2083 function Is_Direct_Deep_Call
(Subp
: Entity_Id
) return Boolean;
2084 -- Determine if Subp denotes a non-dispatching call to a Deep routine
2086 function New_Value
(From
: Node_Id
) return Node_Id
;
2087 -- From is the original Expression. New_Value is equivalent to a call
2088 -- to Duplicate_Subexpr with an explicit dereference when From is an
2089 -- access parameter.
2091 --------------------------
2092 -- Add_Actual_Parameter --
2093 --------------------------
2095 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
) is
2096 Actual_Expr
: constant Node_Id
:=
2097 Explicit_Actual_Parameter
(Insert_Param
);
2100 -- Case of insertion is first named actual
2102 if No
(Prev
) or else
2103 Nkind
(Parent
(Prev
)) /= N_Parameter_Association
2105 Set_Next_Named_Actual
2106 (Insert_Param
, First_Named_Actual
(Call_Node
));
2107 Set_First_Named_Actual
(Call_Node
, Actual_Expr
);
2110 if No
(Parameter_Associations
(Call_Node
)) then
2111 Set_Parameter_Associations
(Call_Node
, New_List
);
2114 Append
(Insert_Param
, Parameter_Associations
(Call_Node
));
2117 Insert_After
(Prev
, Insert_Param
);
2120 -- Case of insertion is not first named actual
2123 Set_Next_Named_Actual
2124 (Insert_Param
, Next_Named_Actual
(Parent
(Prev
)));
2125 Set_Next_Named_Actual
(Parent
(Prev
), Actual_Expr
);
2126 Append
(Insert_Param
, Parameter_Associations
(Call_Node
));
2129 Prev
:= Actual_Expr
;
2130 end Add_Actual_Parameter
;
2132 ----------------------
2133 -- Add_Extra_Actual --
2134 ----------------------
2136 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
) is
2137 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
2140 if Extra_Actuals
= No_List
then
2141 Extra_Actuals
:= New_List
;
2142 Set_Parent
(Extra_Actuals
, Call_Node
);
2145 Append_To
(Extra_Actuals
,
2146 Make_Parameter_Association
(Loc
,
2147 Selector_Name
=> New_Occurrence_Of
(EF
, Loc
),
2148 Explicit_Actual_Parameter
=> Expr
));
2150 Analyze_And_Resolve
(Expr
, Etype
(EF
));
2152 if Nkind
(Call_Node
) = N_Function_Call
then
2153 Set_Is_Accessibility_Actual
(Parent
(Expr
));
2155 end Add_Extra_Actual
;
2157 ---------------------------
2158 -- Inherited_From_Formal --
2159 ---------------------------
2161 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
is
2163 Gen_Par
: Entity_Id
;
2164 Gen_Prim
: Elist_Id
;
2169 -- If the operation is inherited, it is attached to the corresponding
2170 -- type derivation. If the parent in the derivation is a generic
2171 -- actual, it is a subtype of the actual, and we have to recover the
2172 -- original derived type declaration to find the proper parent.
2174 if Nkind
(Parent
(S
)) /= N_Full_Type_Declaration
2175 or else not Is_Derived_Type
(Defining_Identifier
(Parent
(S
)))
2176 or else Nkind
(Type_Definition
(Original_Node
(Parent
(S
)))) /=
2177 N_Derived_Type_Definition
2178 or else not In_Instance
2185 (Type_Definition
(Original_Node
(Parent
(S
))));
2187 if Nkind
(Indic
) = N_Subtype_Indication
then
2188 Par
:= Entity
(Subtype_Mark
(Indic
));
2190 Par
:= Entity
(Indic
);
2194 if not Is_Generic_Actual_Type
(Par
)
2195 or else Is_Tagged_Type
(Par
)
2196 or else Nkind
(Parent
(Par
)) /= N_Subtype_Declaration
2197 or else not In_Open_Scopes
(Scope
(Par
))
2201 Gen_Par
:= Generic_Parent_Type
(Parent
(Par
));
2204 -- If the actual has no generic parent type, the formal is not
2205 -- a formal derived type, so nothing to inherit.
2207 if No
(Gen_Par
) then
2211 -- If the generic parent type is still the generic type, this is a
2212 -- private formal, not a derived formal, and there are no operations
2213 -- inherited from the formal.
2215 if Nkind
(Parent
(Gen_Par
)) = N_Formal_Type_Declaration
then
2219 Gen_Prim
:= Collect_Primitive_Operations
(Gen_Par
);
2221 Elmt
:= First_Elmt
(Gen_Prim
);
2222 while Present
(Elmt
) loop
2223 if Chars
(Node
(Elmt
)) = Chars
(S
) then
2229 F1
:= First_Formal
(S
);
2230 F2
:= First_Formal
(Node
(Elmt
));
2232 and then Present
(F2
)
2234 if Etype
(F1
) = Etype
(F2
)
2235 or else Etype
(F2
) = Gen_Par
2241 exit; -- not the right subprogram
2253 raise Program_Error
;
2254 end Inherited_From_Formal
;
2256 --------------------------
2257 -- In_Unfrozen_Instance --
2258 --------------------------
2260 function In_Unfrozen_Instance
(E
: Entity_Id
) return Boolean is
2265 while Present
(S
) and then S
/= Standard_Standard
loop
2266 if Is_Generic_Instance
(S
)
2267 and then Present
(Freeze_Node
(S
))
2268 and then not Analyzed
(Freeze_Node
(S
))
2277 end In_Unfrozen_Instance
;
2279 -------------------------
2280 -- Is_Direct_Deep_Call --
2281 -------------------------
2283 function Is_Direct_Deep_Call
(Subp
: Entity_Id
) return Boolean is
2285 if Is_TSS
(Subp
, TSS_Deep_Adjust
)
2286 or else Is_TSS
(Subp
, TSS_Deep_Finalize
)
2287 or else Is_TSS
(Subp
, TSS_Deep_Initialize
)
2294 Actual
:= First
(Parameter_Associations
(N
));
2295 Formal
:= First_Formal
(Subp
);
2296 while Present
(Actual
)
2297 and then Present
(Formal
)
2299 if Nkind
(Actual
) = N_Identifier
2300 and then Is_Controlling_Actual
(Actual
)
2301 and then Etype
(Actual
) = Etype
(Formal
)
2307 Next_Formal
(Formal
);
2313 end Is_Direct_Deep_Call
;
2319 function New_Value
(From
: Node_Id
) return Node_Id
is
2320 Res
: constant Node_Id
:= Duplicate_Subexpr
(From
);
2322 if Is_Access_Type
(Etype
(From
)) then
2323 return Make_Explicit_Dereference
(Sloc
(From
), Prefix
=> Res
);
2331 Curr_S
: constant Entity_Id
:= Current_Scope
;
2332 Remote
: constant Boolean := Is_Remote_Call
(Call_Node
);
2335 Orig_Subp
: Entity_Id
:= Empty
;
2336 Param_Count
: Natural := 0;
2337 Parent_Formal
: Entity_Id
;
2338 Parent_Subp
: Entity_Id
;
2342 Prev_Orig
: Node_Id
;
2343 -- Original node for an actual, which may have been rewritten. If the
2344 -- actual is a function call that has been transformed from a selected
2345 -- component, the original node is unanalyzed. Otherwise, it carries
2346 -- semantic information used to generate additional actuals.
2348 CW_Interface_Formals_Present
: Boolean := False;
2350 -- Start of processing for Expand_Call
2353 -- Expand the procedure call if the first actual has a dimension and if
2354 -- the procedure is Put (Ada 2012).
2356 if Ada_Version
>= Ada_2012
2357 and then Nkind
(Call_Node
) = N_Procedure_Call_Statement
2358 and then Present
(Parameter_Associations
(Call_Node
))
2360 Expand_Put_Call_With_Symbol
(Call_Node
);
2363 -- Ignore if previous error
2365 if Nkind
(Call_Node
) in N_Has_Etype
2366 and then Etype
(Call_Node
) = Any_Type
2371 -- Call using access to subprogram with explicit dereference
2373 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
2374 Subp
:= Etype
(Name
(Call_Node
));
2375 Parent_Subp
:= Empty
;
2377 -- Case of call to simple entry, where the Name is a selected component
2378 -- whose prefix is the task, and whose selector name is the entry name
2380 elsif Nkind
(Name
(Call_Node
)) = N_Selected_Component
then
2381 Subp
:= Entity
(Selector_Name
(Name
(Call_Node
)));
2382 Parent_Subp
:= Empty
;
2384 -- Case of call to member of entry family, where Name is an indexed
2385 -- component, with the prefix being a selected component giving the
2386 -- task and entry family name, and the index being the entry index.
2388 elsif Nkind
(Name
(Call_Node
)) = N_Indexed_Component
then
2389 Subp
:= Entity
(Selector_Name
(Prefix
(Name
(Call_Node
))));
2390 Parent_Subp
:= Empty
;
2395 Subp
:= Entity
(Name
(Call_Node
));
2396 Parent_Subp
:= Alias
(Subp
);
2398 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2399 -- if we can tell that the first parameter cannot possibly be null.
2400 -- This improves efficiency by avoiding a run-time test.
2402 -- We do not do this if Raise_Exception_Always does not exist, which
2403 -- can happen in configurable run time profiles which provide only a
2406 if Is_RTE
(Subp
, RE_Raise_Exception
)
2407 and then RTE_Available
(RE_Raise_Exception_Always
)
2410 FA
: constant Node_Id
:=
2411 Original_Node
(First_Actual
(Call_Node
));
2414 -- The case we catch is where the first argument is obtained
2415 -- using the Identity attribute (which must always be
2418 if Nkind
(FA
) = N_Attribute_Reference
2419 and then Attribute_Name
(FA
) = Name_Identity
2421 Subp
:= RTE
(RE_Raise_Exception_Always
);
2422 Set_Name
(Call_Node
, New_Occurrence_Of
(Subp
, Loc
));
2427 if Ekind
(Subp
) = E_Entry
then
2428 Parent_Subp
:= Empty
;
2432 -- Detect the following code in System.Finalization_Masters only on
2433 -- .NET/JVM targets:
2435 -- procedure Finalize (Master : in out Finalization_Master) is
2439 -- Finalize (Curr_Ptr.all);
2441 -- Since .NET/JVM compilers lack address arithmetic and Deep_Finalize
2442 -- cannot be named in library or user code, the compiler has to deal
2443 -- with this by transforming the call to Finalize into Deep_Finalize.
2445 if VM_Target
/= No_VM
2446 and then Chars
(Subp
) = Name_Finalize
2447 and then Ekind
(Curr_S
) = E_Block
2448 and then Ekind
(Scope
(Curr_S
)) = E_Procedure
2449 and then Chars
(Scope
(Curr_S
)) = Name_Finalize
2450 and then Etype
(First_Formal
(Scope
(Curr_S
))) =
2451 RTE
(RE_Finalization_Master
)
2454 Deep_Fin
: constant Entity_Id
:=
2455 Find_Prim_Op
(RTE
(RE_Root_Controlled
),
2458 -- Since Root_Controlled is a tagged type, the compiler should
2459 -- always generate Deep_Finalize for it.
2461 pragma Assert
(Present
(Deep_Fin
));
2464 -- Deep_Finalize (Curr_Ptr.all);
2467 Make_Procedure_Call_Statement
(Loc
,
2469 New_Occurrence_Of
(Deep_Fin
, Loc
),
2470 Parameter_Associations
=>
2471 New_Copy_List_Tree
(Parameter_Associations
(N
))));
2478 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2479 -- alternative in an asynchronous select or as an entry call in
2480 -- a conditional or timed select. Check whether the procedure call
2481 -- is a renaming of an entry and rewrite it as an entry call.
2483 if Ada_Version
>= Ada_2005
2484 and then Nkind
(Call_Node
) = N_Procedure_Call_Statement
2486 ((Nkind
(Parent
(Call_Node
)) = N_Triggering_Alternative
2487 and then Triggering_Statement
(Parent
(Call_Node
)) = Call_Node
)
2489 (Nkind
(Parent
(Call_Node
)) = N_Entry_Call_Alternative
2490 and then Entry_Call_Statement
(Parent
(Call_Node
)) = Call_Node
))
2494 Ren_Root
: Entity_Id
:= Subp
;
2497 -- This may be a chain of renamings, find the root
2499 if Present
(Alias
(Ren_Root
)) then
2500 Ren_Root
:= Alias
(Ren_Root
);
2503 if Present
(Original_Node
(Parent
(Parent
(Ren_Root
)))) then
2504 Ren_Decl
:= Original_Node
(Parent
(Parent
(Ren_Root
)));
2506 if Nkind
(Ren_Decl
) = N_Subprogram_Renaming_Declaration
then
2508 Make_Entry_Call_Statement
(Loc
,
2510 New_Copy_Tree
(Name
(Ren_Decl
)),
2511 Parameter_Associations
=>
2513 (Parameter_Associations
(Call_Node
))));
2521 -- First step, compute extra actuals, corresponding to any Extra_Formals
2522 -- present. Note that we do not access Extra_Formals directly, instead
2523 -- we simply note the presence of the extra formals as we process the
2524 -- regular formals collecting corresponding actuals in Extra_Actuals.
2526 -- We also generate any required range checks for actuals for in formals
2527 -- as we go through the loop, since this is a convenient place to do it.
2528 -- (Though it seems that this would be better done in Expand_Actuals???)
2530 -- Special case: Thunks must not compute the extra actuals; they must
2531 -- just propagate to the target primitive their extra actuals.
2533 if Is_Thunk
(Current_Scope
)
2534 and then Thunk_Entity
(Current_Scope
) = Subp
2535 and then Present
(Extra_Formals
(Subp
))
2537 pragma Assert
(Present
(Extra_Formals
(Current_Scope
)));
2540 Target_Formal
: Entity_Id
;
2541 Thunk_Formal
: Entity_Id
;
2544 Target_Formal
:= Extra_Formals
(Subp
);
2545 Thunk_Formal
:= Extra_Formals
(Current_Scope
);
2546 while Present
(Target_Formal
) loop
2548 (New_Occurrence_Of
(Thunk_Formal
, Loc
), Thunk_Formal
);
2550 Target_Formal
:= Extra_Formal
(Target_Formal
);
2551 Thunk_Formal
:= Extra_Formal
(Thunk_Formal
);
2554 while Is_Non_Empty_List
(Extra_Actuals
) loop
2555 Add_Actual_Parameter
(Remove_Head
(Extra_Actuals
));
2558 Expand_Actuals
(Call_Node
, Subp
);
2563 Formal
:= First_Formal
(Subp
);
2564 Actual
:= First_Actual
(Call_Node
);
2566 while Present
(Formal
) loop
2568 -- Generate range check if required
2570 if Do_Range_Check
(Actual
)
2571 and then Ekind
(Formal
) = E_In_Parameter
2573 Generate_Range_Check
2574 (Actual
, Etype
(Formal
), CE_Range_Check_Failed
);
2577 -- Prepare to examine current entry
2580 Prev_Orig
:= Original_Node
(Prev
);
2582 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2583 -- to expand it in a further round.
2585 CW_Interface_Formals_Present
:=
2586 CW_Interface_Formals_Present
2588 (Ekind
(Etype
(Formal
)) = E_Class_Wide_Type
2589 and then Is_Interface
(Etype
(Etype
(Formal
))))
2591 (Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
2592 and then Is_Interface
(Directly_Designated_Type
2593 (Etype
(Etype
(Formal
)))));
2595 -- Create possible extra actual for constrained case. Usually, the
2596 -- extra actual is of the form actual'constrained, but since this
2597 -- attribute is only available for unconstrained records, TRUE is
2598 -- expanded if the type of the formal happens to be constrained (for
2599 -- instance when this procedure is inherited from an unconstrained
2600 -- record to a constrained one) or if the actual has no discriminant
2601 -- (its type is constrained). An exception to this is the case of a
2602 -- private type without discriminants. In this case we pass FALSE
2603 -- because the object has underlying discriminants with defaults.
2605 if Present
(Extra_Constrained
(Formal
)) then
2606 if Ekind
(Etype
(Prev
)) in Private_Kind
2607 and then not Has_Discriminants
(Base_Type
(Etype
(Prev
)))
2610 (New_Occurrence_Of
(Standard_False
, Loc
),
2611 Extra_Constrained
(Formal
));
2613 elsif Is_Constrained
(Etype
(Formal
))
2614 or else not Has_Discriminants
(Etype
(Prev
))
2617 (New_Occurrence_Of
(Standard_True
, Loc
),
2618 Extra_Constrained
(Formal
));
2620 -- Do not produce extra actuals for Unchecked_Union parameters.
2621 -- Jump directly to the end of the loop.
2623 elsif Is_Unchecked_Union
(Base_Type
(Etype
(Actual
))) then
2624 goto Skip_Extra_Actual_Generation
;
2627 -- If the actual is a type conversion, then the constrained
2628 -- test applies to the actual, not the target type.
2634 -- Test for unchecked conversions as well, which can occur
2635 -- as out parameter actuals on calls to stream procedures.
2638 while Nkind_In
(Act_Prev
, N_Type_Conversion
,
2639 N_Unchecked_Type_Conversion
)
2641 Act_Prev
:= Expression
(Act_Prev
);
2644 -- If the expression is a conversion of a dereference, this
2645 -- is internally generated code that manipulates addresses,
2646 -- e.g. when building interface tables. No check should
2647 -- occur in this case, and the discriminated object is not
2650 if not Comes_From_Source
(Actual
)
2651 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
2652 and then Nkind
(Act_Prev
) = N_Explicit_Dereference
2655 (New_Occurrence_Of
(Standard_False
, Loc
),
2656 Extra_Constrained
(Formal
));
2660 (Make_Attribute_Reference
(Sloc
(Prev
),
2662 Duplicate_Subexpr_No_Checks
2663 (Act_Prev
, Name_Req
=> True),
2664 Attribute_Name
=> Name_Constrained
),
2665 Extra_Constrained
(Formal
));
2671 -- Create possible extra actual for accessibility level
2673 if Present
(Extra_Accessibility
(Formal
)) then
2675 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2676 -- attribute, then the original actual may be an aliased object
2677 -- occurring as the prefix in a call using "Object.Operation"
2678 -- notation. In that case we must pass the level of the object,
2679 -- so Prev_Orig is reset to Prev and the attribute will be
2680 -- processed by the code for Access attributes further below.
2682 if Prev_Orig
/= Prev
2683 and then Nkind
(Prev
) = N_Attribute_Reference
2685 Get_Attribute_Id
(Attribute_Name
(Prev
)) = Attribute_Access
2686 and then Is_Aliased_View
(Prev_Orig
)
2691 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2692 -- accessibility levels.
2694 if Is_Thunk
(Current_Scope
) then
2696 Parm_Ent
: Entity_Id
;
2699 if Is_Controlling_Actual
(Actual
) then
2701 -- Find the corresponding actual of the thunk
2703 Parm_Ent
:= First_Entity
(Current_Scope
);
2704 for J
in 2 .. Param_Count
loop
2705 Next_Entity
(Parm_Ent
);
2708 -- Handle unchecked conversion of access types generated
2709 -- in thunks (cf. Expand_Interface_Thunk).
2711 elsif Is_Access_Type
(Etype
(Actual
))
2712 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
2714 Parm_Ent
:= Entity
(Expression
(Actual
));
2716 else pragma Assert
(Is_Entity_Name
(Actual
));
2717 Parm_Ent
:= Entity
(Actual
);
2721 (New_Occurrence_Of
(Extra_Accessibility
(Parm_Ent
), Loc
),
2722 Extra_Accessibility
(Formal
));
2725 elsif Is_Entity_Name
(Prev_Orig
) then
2727 -- When passing an access parameter, or a renaming of an access
2728 -- parameter, as the actual to another access parameter we need
2729 -- to pass along the actual's own access level parameter. This
2730 -- is done if we are within the scope of the formal access
2731 -- parameter (if this is an inlined body the extra formal is
2734 if (Is_Formal
(Entity
(Prev_Orig
))
2736 (Present
(Renamed_Object
(Entity
(Prev_Orig
)))
2738 Is_Entity_Name
(Renamed_Object
(Entity
(Prev_Orig
)))
2741 (Entity
(Renamed_Object
(Entity
(Prev_Orig
))))))
2742 and then Ekind
(Etype
(Prev_Orig
)) = E_Anonymous_Access_Type
2743 and then In_Open_Scopes
(Scope
(Entity
(Prev_Orig
)))
2746 Parm_Ent
: constant Entity_Id
:= Param_Entity
(Prev_Orig
);
2749 pragma Assert
(Present
(Parm_Ent
));
2751 if Present
(Extra_Accessibility
(Parm_Ent
)) then
2754 (Extra_Accessibility
(Parm_Ent
), Loc
),
2755 Extra_Accessibility
(Formal
));
2757 -- If the actual access parameter does not have an
2758 -- associated extra formal providing its scope level,
2759 -- then treat the actual as having library-level
2764 (Make_Integer_Literal
(Loc
,
2765 Intval
=> Scope_Depth
(Standard_Standard
)),
2766 Extra_Accessibility
(Formal
));
2770 -- The actual is a normal access value, so just pass the level
2771 -- of the actual's access type.
2775 (Dynamic_Accessibility_Level
(Prev_Orig
),
2776 Extra_Accessibility
(Formal
));
2779 -- If the actual is an access discriminant, then pass the level
2780 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2782 elsif Nkind
(Prev_Orig
) = N_Selected_Component
2783 and then Ekind
(Entity
(Selector_Name
(Prev_Orig
))) =
2785 and then Ekind
(Etype
(Entity
(Selector_Name
(Prev_Orig
)))) =
2786 E_Anonymous_Access_Type
2789 (Make_Integer_Literal
(Loc
,
2790 Intval
=> Object_Access_Level
(Prefix
(Prev_Orig
))),
2791 Extra_Accessibility
(Formal
));
2796 case Nkind
(Prev_Orig
) is
2798 when N_Attribute_Reference
=>
2799 case Get_Attribute_Id
(Attribute_Name
(Prev_Orig
)) is
2801 -- For X'Access, pass on the level of the prefix X
2803 when Attribute_Access
=>
2805 -- If this is an Access attribute applied to the
2806 -- the current instance object passed to a type
2807 -- initialization procedure, then use the level
2808 -- of the type itself. This is not really correct,
2809 -- as there should be an extra level parameter
2810 -- passed in with _init formals (only in the case
2811 -- where the type is immutably limited), but we
2812 -- don't have an easy way currently to create such
2813 -- an extra formal (init procs aren't ever frozen).
2814 -- For now we just use the level of the type,
2815 -- which may be too shallow, but that works better
2816 -- than passing Object_Access_Level of the type,
2817 -- which can be one level too deep in some cases.
2820 if Is_Entity_Name
(Prefix
(Prev_Orig
))
2821 and then Is_Type
(Entity
(Prefix
(Prev_Orig
)))
2824 (Make_Integer_Literal
(Loc
,
2827 (Entity
(Prefix
(Prev_Orig
)))),
2828 Extra_Accessibility
(Formal
));
2832 (Make_Integer_Literal
(Loc
,
2835 (Prefix
(Prev_Orig
))),
2836 Extra_Accessibility
(Formal
));
2839 -- Treat the unchecked attributes as library-level
2841 when Attribute_Unchecked_Access |
2842 Attribute_Unrestricted_Access
=>
2844 (Make_Integer_Literal
(Loc
,
2845 Intval
=> Scope_Depth
(Standard_Standard
)),
2846 Extra_Accessibility
(Formal
));
2848 -- No other cases of attributes returning access
2849 -- values that can be passed to access parameters.
2852 raise Program_Error
;
2856 -- For allocators we pass the level of the execution of the
2857 -- called subprogram, which is one greater than the current
2862 (Make_Integer_Literal
(Loc
,
2863 Intval
=> Scope_Depth
(Current_Scope
) + 1),
2864 Extra_Accessibility
(Formal
));
2866 -- For most other cases we simply pass the level of the
2867 -- actual's access type. The type is retrieved from
2868 -- Prev rather than Prev_Orig, because in some cases
2869 -- Prev_Orig denotes an original expression that has
2870 -- not been analyzed.
2874 (Dynamic_Accessibility_Level
(Prev
),
2875 Extra_Accessibility
(Formal
));
2880 -- Perform the check of 4.6(49) that prevents a null value from being
2881 -- passed as an actual to an access parameter. Note that the check
2882 -- is elided in the common cases of passing an access attribute or
2883 -- access parameter as an actual. Also, we currently don't enforce
2884 -- this check for expander-generated actuals and when -gnatdj is set.
2886 if Ada_Version
>= Ada_2005
then
2888 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
2889 -- the intent of 6.4.1(13) is that null-exclusion checks should
2890 -- not be done for 'out' parameters, even though it refers only
2891 -- to constraint checks, and a null_exclusion is not a constraint.
2892 -- Note that AI05-0196-1 corrects this mistake in the RM.
2894 if Is_Access_Type
(Etype
(Formal
))
2895 and then Can_Never_Be_Null
(Etype
(Formal
))
2896 and then Ekind
(Formal
) /= E_Out_Parameter
2897 and then Nkind
(Prev
) /= N_Raise_Constraint_Error
2898 and then (Known_Null
(Prev
)
2899 or else not Can_Never_Be_Null
(Etype
(Prev
)))
2901 Install_Null_Excluding_Check
(Prev
);
2904 -- Ada_Version < Ada_2005
2907 if Ekind
(Etype
(Formal
)) /= E_Anonymous_Access_Type
2908 or else Access_Checks_Suppressed
(Subp
)
2912 elsif Debug_Flag_J
then
2915 elsif not Comes_From_Source
(Prev
) then
2918 elsif Is_Entity_Name
(Prev
)
2919 and then Ekind
(Etype
(Prev
)) = E_Anonymous_Access_Type
2923 elsif Nkind_In
(Prev
, N_Allocator
, N_Attribute_Reference
) then
2926 -- Suppress null checks when passing to access parameters of Java
2927 -- and CIL subprograms. (Should this be done for other foreign
2928 -- conventions as well ???)
2930 elsif Convention
(Subp
) = Convention_Java
2931 or else Convention
(Subp
) = Convention_CIL
2936 Install_Null_Excluding_Check
(Prev
);
2940 -- Perform appropriate validity checks on parameters that
2943 if Validity_Checks_On
then
2944 if (Ekind
(Formal
) = E_In_Parameter
2945 and then Validity_Check_In_Params
)
2947 (Ekind
(Formal
) = E_In_Out_Parameter
2948 and then Validity_Check_In_Out_Params
)
2950 -- If the actual is an indexed component of a packed type (or
2951 -- is an indexed or selected component whose prefix recursively
2952 -- meets this condition), it has not been expanded yet. It will
2953 -- be copied in the validity code that follows, and has to be
2954 -- expanded appropriately, so reanalyze it.
2956 -- What we do is just to unset analyzed bits on prefixes till
2957 -- we reach something that does not have a prefix.
2964 while Nkind_In
(Nod
, N_Indexed_Component
,
2965 N_Selected_Component
)
2967 Set_Analyzed
(Nod
, False);
2968 Nod
:= Prefix
(Nod
);
2972 Ensure_Valid
(Actual
);
2976 -- For IN OUT and OUT parameters, ensure that subscripts are valid
2977 -- since this is a left side reference. We only do this for calls
2978 -- from the source program since we assume that compiler generated
2979 -- calls explicitly generate any required checks. We also need it
2980 -- only if we are doing standard validity checks, since clearly it is
2981 -- not needed if validity checks are off, and in subscript validity
2982 -- checking mode, all indexed components are checked with a call
2983 -- directly from Expand_N_Indexed_Component.
2985 if Comes_From_Source
(Call_Node
)
2986 and then Ekind
(Formal
) /= E_In_Parameter
2987 and then Validity_Checks_On
2988 and then Validity_Check_Default
2989 and then not Validity_Check_Subscripts
2991 Check_Valid_Lvalue_Subscripts
(Actual
);
2994 -- Mark any scalar OUT parameter that is a simple variable as no
2995 -- longer known to be valid (unless the type is always valid). This
2996 -- reflects the fact that if an OUT parameter is never set in a
2997 -- procedure, then it can become invalid on the procedure return.
2999 if Ekind
(Formal
) = E_Out_Parameter
3000 and then Is_Entity_Name
(Actual
)
3001 and then Ekind
(Entity
(Actual
)) = E_Variable
3002 and then not Is_Known_Valid
(Etype
(Actual
))
3004 Set_Is_Known_Valid
(Entity
(Actual
), False);
3007 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3008 -- clear current values, since they can be clobbered. We are probably
3009 -- doing this in more places than we need to, but better safe than
3010 -- sorry when it comes to retaining bad current values.
3012 if Ekind
(Formal
) /= E_In_Parameter
3013 and then Is_Entity_Name
(Actual
)
3014 and then Present
(Entity
(Actual
))
3017 Ent
: constant Entity_Id
:= Entity
(Actual
);
3021 -- For an OUT or IN OUT parameter that is an assignable entity,
3022 -- we do not want to clobber the Last_Assignment field, since
3023 -- if it is set, it was precisely because it is indeed an OUT
3024 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
3025 -- since the subprogram could have returned in invalid value.
3027 if Ekind_In
(Formal
, E_Out_Parameter
, E_In_Out_Parameter
)
3028 and then Is_Assignable
(Ent
)
3030 Sav
:= Last_Assignment
(Ent
);
3031 Kill_Current_Values
(Ent
);
3032 Set_Last_Assignment
(Ent
, Sav
);
3033 Set_Is_Known_Valid
(Ent
, False);
3035 -- For all other cases, just kill the current values
3038 Kill_Current_Values
(Ent
);
3043 -- If the formal is class wide and the actual is an aggregate, force
3044 -- evaluation so that the back end who does not know about class-wide
3045 -- type, does not generate a temporary of the wrong size.
3047 if not Is_Class_Wide_Type
(Etype
(Formal
)) then
3050 elsif Nkind
(Actual
) = N_Aggregate
3051 or else (Nkind
(Actual
) = N_Qualified_Expression
3052 and then Nkind
(Expression
(Actual
)) = N_Aggregate
)
3054 Force_Evaluation
(Actual
);
3057 -- In a remote call, if the formal is of a class-wide type, check
3058 -- that the actual meets the requirements described in E.4(18).
3060 if Remote
and then Is_Class_Wide_Type
(Etype
(Formal
)) then
3061 Insert_Action
(Actual
,
3062 Make_Transportable_Check
(Loc
,
3063 Duplicate_Subexpr_Move_Checks
(Actual
)));
3066 -- This label is required when skipping extra actual generation for
3067 -- Unchecked_Union parameters.
3069 <<Skip_Extra_Actual_Generation
>>
3071 Param_Count
:= Param_Count
+ 1;
3072 Next_Actual
(Actual
);
3073 Next_Formal
(Formal
);
3076 -- If we are calling an Ada 2012 function which needs to have the
3077 -- "accessibility level determined by the point of call" (AI05-0234)
3078 -- passed in to it, then pass it in.
3080 if Ekind_In
(Subp
, E_Function
, E_Operator
, E_Subprogram_Type
)
3082 Present
(Extra_Accessibility_Of_Result
(Ultimate_Alias
(Subp
)))
3085 Ancestor
: Node_Id
:= Parent
(Call_Node
);
3086 Level
: Node_Id
:= Empty
;
3087 Defer
: Boolean := False;
3090 -- Unimplemented: if Subp returns an anonymous access type, then
3092 -- a) if the call is the operand of an explict conversion, then
3093 -- the target type of the conversion (a named access type)
3094 -- determines the accessibility level pass in;
3096 -- b) if the call defines an access discriminant of an object
3097 -- (e.g., the discriminant of an object being created by an
3098 -- allocator, or the discriminant of a function result),
3099 -- then the accessibility level to pass in is that of the
3100 -- discriminated object being initialized).
3104 while Nkind
(Ancestor
) = N_Qualified_Expression
3106 Ancestor
:= Parent
(Ancestor
);
3109 case Nkind
(Ancestor
) is
3112 -- At this point, we'd like to assign
3114 -- Level := Dynamic_Accessibility_Level (Ancestor);
3116 -- but Etype of Ancestor may not have been set yet,
3117 -- so that doesn't work.
3119 -- Handle this later in Expand_Allocator_Expression.
3123 when N_Object_Declaration | N_Object_Renaming_Declaration
=>
3125 Def_Id
: constant Entity_Id
:=
3126 Defining_Identifier
(Ancestor
);
3129 if Is_Return_Object
(Def_Id
) then
3130 if Present
(Extra_Accessibility_Of_Result
3131 (Return_Applies_To
(Scope
(Def_Id
))))
3133 -- Pass along value that was passed in if the
3134 -- routine we are returning from also has an
3135 -- Accessibility_Of_Result formal.
3139 (Extra_Accessibility_Of_Result
3140 (Return_Applies_To
(Scope
(Def_Id
))), Loc
);
3144 Make_Integer_Literal
(Loc
,
3145 Intval
=> Object_Access_Level
(Def_Id
));
3149 when N_Simple_Return_Statement
=>
3150 if Present
(Extra_Accessibility_Of_Result
3152 (Return_Statement_Entity
(Ancestor
))))
3154 -- Pass along value that was passed in if the returned
3155 -- routine also has an Accessibility_Of_Result formal.
3159 (Extra_Accessibility_Of_Result
3161 (Return_Statement_Entity
(Ancestor
))), Loc
);
3169 if not Present
(Level
) then
3171 -- The "innermost master that evaluates the function call".
3173 -- ??? - Should we use Integer'Last here instead in order
3174 -- to deal with (some of) the problems associated with
3175 -- calls to subps whose enclosing scope is unknown (e.g.,
3176 -- Anon_Access_To_Subp_Param.all)?
3178 Level
:= Make_Integer_Literal
(Loc
,
3179 Scope_Depth
(Current_Scope
) + 1);
3184 Extra_Accessibility_Of_Result
(Ultimate_Alias
(Subp
)));
3189 -- If we are expanding the RHS of an assignment we need to check if tag
3190 -- propagation is needed. You might expect this processing to be in
3191 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3192 -- assignment might be transformed to a declaration for an unconstrained
3193 -- value if the expression is classwide.
3195 if Nkind
(Call_Node
) = N_Function_Call
3196 and then Is_Tag_Indeterminate
(Call_Node
)
3197 and then Is_Entity_Name
(Name
(Call_Node
))
3200 Ass
: Node_Id
:= Empty
;
3203 if Nkind
(Parent
(Call_Node
)) = N_Assignment_Statement
then
3204 Ass
:= Parent
(Call_Node
);
3206 elsif Nkind
(Parent
(Call_Node
)) = N_Qualified_Expression
3207 and then Nkind
(Parent
(Parent
(Call_Node
))) =
3208 N_Assignment_Statement
3210 Ass
:= Parent
(Parent
(Call_Node
));
3212 elsif Nkind
(Parent
(Call_Node
)) = N_Explicit_Dereference
3213 and then Nkind
(Parent
(Parent
(Call_Node
))) =
3214 N_Assignment_Statement
3216 Ass
:= Parent
(Parent
(Call_Node
));
3220 and then Is_Class_Wide_Type
(Etype
(Name
(Ass
)))
3222 if Is_Access_Type
(Etype
(Call_Node
)) then
3223 if Designated_Type
(Etype
(Call_Node
)) /=
3224 Root_Type
(Etype
(Name
(Ass
)))
3227 ("tag-indeterminate expression "
3228 & " must have designated type& (RM 5.2 (6))",
3229 Call_Node
, Root_Type
(Etype
(Name
(Ass
))));
3231 Propagate_Tag
(Name
(Ass
), Call_Node
);
3234 elsif Etype
(Call_Node
) /= Root_Type
(Etype
(Name
(Ass
))) then
3236 ("tag-indeterminate expression must have type&"
3238 Call_Node
, Root_Type
(Etype
(Name
(Ass
))));
3241 Propagate_Tag
(Name
(Ass
), Call_Node
);
3244 -- The call will be rewritten as a dispatching call, and
3245 -- expanded as such.
3252 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3253 -- it to point to the correct secondary virtual table
3255 if Nkind
(Call_Node
) in N_Subprogram_Call
3256 and then CW_Interface_Formals_Present
3258 Expand_Interface_Actuals
(Call_Node
);
3261 -- Deals with Dispatch_Call if we still have a call, before expanding
3262 -- extra actuals since this will be done on the re-analysis of the
3263 -- dispatching call. Note that we do not try to shorten the actual list
3264 -- for a dispatching call, it would not make sense to do so. Expansion
3265 -- of dispatching calls is suppressed when VM_Target, because the VM
3266 -- back-ends directly handle the generation of dispatching calls and
3267 -- would have to undo any expansion to an indirect call.
3269 if Nkind
(Call_Node
) in N_Subprogram_Call
3270 and then Present
(Controlling_Argument
(Call_Node
))
3273 Call_Typ
: constant Entity_Id
:= Etype
(Call_Node
);
3274 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
3275 Eq_Prim_Op
: Entity_Id
:= Empty
;
3278 Prev_Call
: Node_Id
;
3281 if not Is_Limited_Type
(Typ
) then
3282 Eq_Prim_Op
:= Find_Prim_Op
(Typ
, Name_Op_Eq
);
3285 if Tagged_Type_Expansion
then
3286 Expand_Dispatching_Call
(Call_Node
);
3288 -- The following return is worrisome. Is it really OK to skip
3289 -- all remaining processing in this procedure ???
3296 Apply_Tag_Checks
(Call_Node
);
3298 -- If this is a dispatching "=", we must first compare the
3299 -- tags so we generate: x.tag = y.tag and then x = y
3301 if Subp
= Eq_Prim_Op
then
3303 -- Mark the node as analyzed to avoid reanalizing this
3304 -- dispatching call (which would cause a never-ending loop)
3306 Prev_Call
:= Relocate_Node
(Call_Node
);
3307 Set_Analyzed
(Prev_Call
);
3309 Param
:= First_Actual
(Call_Node
);
3315 Make_Selected_Component
(Loc
,
3316 Prefix
=> New_Value
(Param
),
3319 (First_Tag_Component
(Typ
), Loc
)),
3322 Make_Selected_Component
(Loc
,
3324 Unchecked_Convert_To
(Typ
,
3325 New_Value
(Next_Actual
(Param
))),
3328 (First_Tag_Component
(Typ
), Loc
))),
3329 Right_Opnd
=> Prev_Call
);
3331 Rewrite
(Call_Node
, New_Call
);
3334 (Call_Node
, Call_Typ
, Suppress
=> All_Checks
);
3337 -- Expansion of a dispatching call results in an indirect call,
3338 -- which in turn causes current values to be killed (see
3339 -- Resolve_Call), so on VM targets we do the call here to
3340 -- ensure consistent warnings between VM and non-VM targets.
3342 Kill_Current_Values
;
3345 -- If this is a dispatching "=" then we must update the reference
3346 -- to the call node because we generated:
3347 -- x.tag = y.tag and then x = y
3349 if Subp
= Eq_Prim_Op
then
3350 Call_Node
:= Right_Opnd
(Call_Node
);
3355 -- Similarly, expand calls to RCI subprograms on which pragma
3356 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3357 -- later. Do this only when the call comes from source since we
3358 -- do not want such a rewriting to occur in expanded code.
3360 if Is_All_Remote_Call
(Call_Node
) then
3361 Expand_All_Calls_Remote_Subprogram_Call
(Call_Node
);
3363 -- Similarly, do not add extra actuals for an entry call whose entity
3364 -- is a protected procedure, or for an internal protected subprogram
3365 -- call, because it will be rewritten as a protected subprogram call
3366 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3368 elsif Is_Protected_Type
(Scope
(Subp
))
3369 and then (Ekind
(Subp
) = E_Procedure
3370 or else Ekind
(Subp
) = E_Function
)
3374 -- During that loop we gathered the extra actuals (the ones that
3375 -- correspond to Extra_Formals), so now they can be appended.
3378 while Is_Non_Empty_List
(Extra_Actuals
) loop
3379 Add_Actual_Parameter
(Remove_Head
(Extra_Actuals
));
3383 -- At this point we have all the actuals, so this is the point at which
3384 -- the various expansion activities for actuals is carried out.
3386 Expand_Actuals
(Call_Node
, Subp
);
3388 -- Verify that the actuals do not share storage. This check must be done
3389 -- on the caller side rather that inside the subprogram to avoid issues
3390 -- of parameter passing.
3392 if Check_Aliasing_Of_Parameters
then
3393 Apply_Parameter_Aliasing_Checks
(Call_Node
, Subp
);
3396 -- If the subprogram is a renaming, or if it is inherited, replace it in
3397 -- the call with the name of the actual subprogram being called. If this
3398 -- is a dispatching call, the run-time decides what to call. The Alias
3399 -- attribute does not apply to entries.
3401 if Nkind
(Call_Node
) /= N_Entry_Call_Statement
3402 and then No
(Controlling_Argument
(Call_Node
))
3403 and then Present
(Parent_Subp
)
3404 and then not Is_Direct_Deep_Call
(Subp
)
3406 if Present
(Inherited_From_Formal
(Subp
)) then
3407 Parent_Subp
:= Inherited_From_Formal
(Subp
);
3409 Parent_Subp
:= Ultimate_Alias
(Parent_Subp
);
3412 -- The below setting of Entity is suspect, see F109-018 discussion???
3414 Set_Entity
(Name
(Call_Node
), Parent_Subp
);
3416 if Is_Abstract_Subprogram
(Parent_Subp
)
3417 and then not In_Instance
3420 ("cannot call abstract subprogram &!",
3421 Name
(Call_Node
), Parent_Subp
);
3424 -- Inspect all formals of derived subprogram Subp. Compare parameter
3425 -- types with the parent subprogram and check whether an actual may
3426 -- need a type conversion to the corresponding formal of the parent
3429 -- Not clear whether intrinsic subprograms need such conversions. ???
3431 if not Is_Intrinsic_Subprogram
(Parent_Subp
)
3432 or else Is_Generic_Instance
(Parent_Subp
)
3435 procedure Convert
(Act
: Node_Id
; Typ
: Entity_Id
);
3436 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3437 -- and resolve the newly generated construct.
3443 procedure Convert
(Act
: Node_Id
; Typ
: Entity_Id
) is
3445 Rewrite
(Act
, OK_Convert_To
(Typ
, Relocate_Node
(Act
)));
3452 Actual_Typ
: Entity_Id
;
3453 Formal_Typ
: Entity_Id
;
3454 Parent_Typ
: Entity_Id
;
3457 Actual
:= First_Actual
(Call_Node
);
3458 Formal
:= First_Formal
(Subp
);
3459 Parent_Formal
:= First_Formal
(Parent_Subp
);
3460 while Present
(Formal
) loop
3461 Actual_Typ
:= Etype
(Actual
);
3462 Formal_Typ
:= Etype
(Formal
);
3463 Parent_Typ
:= Etype
(Parent_Formal
);
3465 -- For an IN parameter of a scalar type, the parent formal
3466 -- type and derived formal type differ or the parent formal
3467 -- type and actual type do not match statically.
3469 if Is_Scalar_Type
(Formal_Typ
)
3470 and then Ekind
(Formal
) = E_In_Parameter
3471 and then Formal_Typ
/= Parent_Typ
3473 not Subtypes_Statically_Match
(Parent_Typ
, Actual_Typ
)
3474 and then not Raises_Constraint_Error
(Actual
)
3476 Convert
(Actual
, Parent_Typ
);
3477 Enable_Range_Check
(Actual
);
3479 -- If the actual has been marked as requiring a range
3480 -- check, then generate it here.
3482 if Do_Range_Check
(Actual
) then
3483 Generate_Range_Check
3484 (Actual
, Etype
(Formal
), CE_Range_Check_Failed
);
3487 -- For access types, the parent formal type and actual type
3490 elsif Is_Access_Type
(Formal_Typ
)
3491 and then Base_Type
(Parent_Typ
) /= Base_Type
(Actual_Typ
)
3493 if Ekind
(Formal
) /= E_In_Parameter
then
3494 Convert
(Actual
, Parent_Typ
);
3496 elsif Ekind
(Parent_Typ
) = E_Anonymous_Access_Type
3497 and then Designated_Type
(Parent_Typ
) /=
3498 Designated_Type
(Actual_Typ
)
3499 and then not Is_Controlling_Formal
(Formal
)
3501 -- This unchecked conversion is not necessary unless
3502 -- inlining is enabled, because in that case the type
3503 -- mismatch may become visible in the body about to be
3507 Unchecked_Convert_To
(Parent_Typ
,
3508 Relocate_Node
(Actual
)));
3510 Resolve
(Actual
, Parent_Typ
);
3513 -- If there is a change of representation, then generate a
3514 -- warning, and do the change of representation.
3516 elsif not Same_Representation
(Formal_Typ
, Parent_Typ
) then
3518 ("??change of representation required", Actual
);
3519 Convert
(Actual
, Parent_Typ
);
3521 -- For array and record types, the parent formal type and
3522 -- derived formal type have different sizes or pragma Pack
3525 elsif ((Is_Array_Type
(Formal_Typ
)
3526 and then Is_Array_Type
(Parent_Typ
))
3528 (Is_Record_Type
(Formal_Typ
)
3529 and then Is_Record_Type
(Parent_Typ
)))
3531 (Esize
(Formal_Typ
) /= Esize
(Parent_Typ
)
3532 or else Has_Pragma_Pack
(Formal_Typ
) /=
3533 Has_Pragma_Pack
(Parent_Typ
))
3535 Convert
(Actual
, Parent_Typ
);
3538 Next_Actual
(Actual
);
3539 Next_Formal
(Formal
);
3540 Next_Formal
(Parent_Formal
);
3546 Subp
:= Parent_Subp
;
3549 -- Deal with case where call is an explicit dereference
3551 if Nkind
(Name
(Call_Node
)) = N_Explicit_Dereference
then
3553 -- Handle case of access to protected subprogram type
3555 if Is_Access_Protected_Subprogram_Type
3556 (Base_Type
(Etype
(Prefix
(Name
(Call_Node
)))))
3558 -- If this is a call through an access to protected operation, the
3559 -- prefix has the form (object'address, operation'access). Rewrite
3560 -- as a for other protected calls: the object is the 1st parameter
3561 -- of the list of actuals.
3568 Ptr
: constant Node_Id
:= Prefix
(Name
(Call_Node
));
3570 T
: constant Entity_Id
:=
3571 Equivalent_Type
(Base_Type
(Etype
(Ptr
)));
3573 D_T
: constant Entity_Id
:=
3574 Designated_Type
(Base_Type
(Etype
(Ptr
)));
3578 Make_Selected_Component
(Loc
,
3579 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
3581 New_Occurrence_Of
(First_Entity
(T
), Loc
));
3584 Make_Selected_Component
(Loc
,
3585 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
3587 New_Occurrence_Of
(Next_Entity
(First_Entity
(T
)), Loc
));
3590 Make_Explicit_Dereference
(Loc
,
3593 if Present
(Parameter_Associations
(Call_Node
)) then
3594 Parm
:= Parameter_Associations
(Call_Node
);
3599 Prepend
(Obj
, Parm
);
3601 if Etype
(D_T
) = Standard_Void_Type
then
3603 Make_Procedure_Call_Statement
(Loc
,
3605 Parameter_Associations
=> Parm
);
3608 Make_Function_Call
(Loc
,
3610 Parameter_Associations
=> Parm
);
3613 Set_First_Named_Actual
(Call
, First_Named_Actual
(Call_Node
));
3614 Set_Etype
(Call
, Etype
(D_T
));
3616 -- We do not re-analyze the call to avoid infinite recursion.
3617 -- We analyze separately the prefix and the object, and set
3618 -- the checks on the prefix that would otherwise be emitted
3619 -- when resolving a call.
3621 Rewrite
(Call_Node
, Call
);
3623 Apply_Access_Check
(Nam
);
3630 -- If this is a call to an intrinsic subprogram, then perform the
3631 -- appropriate expansion to the corresponding tree node and we
3632 -- are all done (since after that the call is gone).
3634 -- In the case where the intrinsic is to be processed by the back end,
3635 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3636 -- since the idea in this case is to pass the call unchanged. If the
3637 -- intrinsic is an inherited unchecked conversion, and the derived type
3638 -- is the target type of the conversion, we must retain it as the return
3639 -- type of the expression. Otherwise the expansion below, which uses the
3640 -- parent operation, will yield the wrong type.
3642 if Is_Intrinsic_Subprogram
(Subp
) then
3643 Expand_Intrinsic_Call
(Call_Node
, Subp
);
3645 if Nkind
(Call_Node
) = N_Unchecked_Type_Conversion
3646 and then Parent_Subp
/= Orig_Subp
3647 and then Etype
(Parent_Subp
) /= Etype
(Orig_Subp
)
3649 Set_Etype
(Call_Node
, Etype
(Orig_Subp
));
3655 if Ekind_In
(Subp
, E_Function
, E_Procedure
) then
3657 -- We perform two simple optimization on calls:
3659 -- a) replace calls to null procedures unconditionally;
3661 -- b) for To_Address, just do an unchecked conversion. Not only is
3662 -- this efficient, but it also avoids order of elaboration problems
3663 -- when address clauses are inlined (address expression elaborated
3664 -- at the wrong point).
3666 -- We perform these optimization regardless of whether we are in the
3667 -- main unit or in a unit in the context of the main unit, to ensure
3668 -- that tree generated is the same in both cases, for CodePeer use.
3670 if Is_RTE
(Subp
, RE_To_Address
) then
3672 Unchecked_Convert_To
3673 (RTE
(RE_Address
), Relocate_Node
(First_Actual
(Call_Node
))));
3676 elsif Is_Null_Procedure
(Subp
) then
3677 Rewrite
(Call_Node
, Make_Null_Statement
(Loc
));
3681 -- Handle inlining. No action needed if the subprogram is not inlined
3683 if not Is_Inlined
(Subp
) then
3686 -- Handle frontend inlining
3688 elsif not Back_End_Inlining
then
3689 Inlined_Subprogram
: declare
3691 Must_Inline
: Boolean := False;
3692 Spec
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
3695 -- Verify that the body to inline has already been seen, and
3696 -- that if the body is in the current unit the inlining does
3697 -- not occur earlier. This avoids order-of-elaboration problems
3700 -- This should be documented in sinfo/einfo ???
3703 or else Nkind
(Spec
) /= N_Subprogram_Declaration
3704 or else No
(Body_To_Inline
(Spec
))
3706 Must_Inline
:= False;
3708 -- If this an inherited function that returns a private type,
3709 -- do not inline if the full view is an unconstrained array,
3710 -- because such calls cannot be inlined.
3712 elsif Present
(Orig_Subp
)
3713 and then Is_Array_Type
(Etype
(Orig_Subp
))
3714 and then not Is_Constrained
(Etype
(Orig_Subp
))
3716 Must_Inline
:= False;
3718 elsif In_Unfrozen_Instance
(Scope
(Subp
)) then
3719 Must_Inline
:= False;
3722 Bod
:= Body_To_Inline
(Spec
);
3724 if (In_Extended_Main_Code_Unit
(Call_Node
)
3725 or else In_Extended_Main_Code_Unit
(Parent
(Call_Node
))
3726 or else Has_Pragma_Inline_Always
(Subp
))
3727 and then (not In_Same_Extended_Unit
(Sloc
(Bod
), Loc
)
3729 Earlier_In_Extended_Unit
(Sloc
(Bod
), Loc
))
3731 Must_Inline
:= True;
3733 -- If we are compiling a package body that is not the main
3734 -- unit, it must be for inlining/instantiation purposes,
3735 -- in which case we inline the call to insure that the same
3736 -- temporaries are generated when compiling the body by
3737 -- itself. Otherwise link errors can occur.
3739 -- If the function being called is itself in the main unit,
3740 -- we cannot inline, because there is a risk of double
3741 -- elaboration and/or circularity: the inlining can make
3742 -- visible a private entity in the body of the main unit,
3743 -- that gigi will see before its sees its proper definition.
3745 elsif not (In_Extended_Main_Code_Unit
(Call_Node
))
3746 and then In_Package_Body
3748 Must_Inline
:= not In_Extended_Main_Source_Unit
(Subp
);
3753 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
3756 -- Let the back end handle it
3758 Add_Inlined_Body
(Subp
, Call_Node
);
3760 if Front_End_Inlining
3761 and then Nkind
(Spec
) = N_Subprogram_Declaration
3762 and then (In_Extended_Main_Code_Unit
(Call_Node
))
3763 and then No
(Body_To_Inline
(Spec
))
3764 and then not Has_Completion
(Subp
)
3765 and then In_Same_Extended_Unit
(Sloc
(Spec
), Loc
)
3768 ("cannot inline& (body not seen yet)?",
3772 end Inlined_Subprogram
;
3774 -- Back end inlining: let the back end handle it
3776 elsif No
(Unit_Declaration_Node
(Subp
))
3777 or else Nkind
(Unit_Declaration_Node
(Subp
)) /=
3778 N_Subprogram_Declaration
3779 or else No
(Body_To_Inline
(Unit_Declaration_Node
(Subp
)))
3780 or else Nkind
(Body_To_Inline
(Unit_Declaration_Node
(Subp
))) in
3783 Add_Inlined_Body
(Subp
, Call_Node
);
3785 -- Front end expansion of simple functions returning unconstrained
3786 -- types (see Check_And_Split_Unconstrained_Function). Note that the
3787 -- case of a simple renaming (Body_To_Inline in N_Entity above, see
3788 -- also Build_Renamed_Body) cannot be expanded here because this may
3789 -- give rise to order-of-elaboration issues for the types of the
3790 -- parameters of the subprogram, if any.
3793 Expand_Inlined_Call
(Call_Node
, Subp
, Orig_Subp
);
3797 -- Check for protected subprogram. This is either an intra-object call,
3798 -- or a protected function call. Protected procedure calls are rewritten
3799 -- as entry calls and handled accordingly.
3801 -- In Ada 2005, this may be an indirect call to an access parameter that
3802 -- is an access_to_subprogram. In that case the anonymous type has a
3803 -- scope that is a protected operation, but the call is a regular one.
3804 -- In either case do not expand call if subprogram is eliminated.
3806 Scop
:= Scope
(Subp
);
3808 if Nkind
(Call_Node
) /= N_Entry_Call_Statement
3809 and then Is_Protected_Type
(Scop
)
3810 and then Ekind
(Subp
) /= E_Subprogram_Type
3811 and then not Is_Eliminated
(Subp
)
3813 -- If the call is an internal one, it is rewritten as a call to the
3814 -- corresponding unprotected subprogram.
3816 Expand_Protected_Subprogram_Call
(Call_Node
, Subp
, Scop
);
3819 -- Functions returning controlled objects need special attention. If
3820 -- the return type is limited, then the context is initialization and
3821 -- different processing applies. If the call is to a protected function,
3822 -- the expansion above will call Expand_Call recursively. Otherwise the
3823 -- function call is transformed into a temporary which obtains the
3824 -- result from the secondary stack.
3826 if Needs_Finalization
(Etype
(Subp
)) then
3827 if not Is_Limited_View
(Etype
(Subp
))
3829 (No
(First_Formal
(Subp
))
3831 not Is_Concurrent_Record_Type
(Etype
(First_Formal
(Subp
))))
3833 Expand_Ctrl_Function_Call
(Call_Node
);
3835 -- Build-in-place function calls which appear in anonymous contexts
3836 -- need a transient scope to ensure the proper finalization of the
3837 -- intermediate result after its use.
3839 elsif Is_Build_In_Place_Function_Call
(Call_Node
)
3841 Nkind_In
(Parent
(Call_Node
), N_Attribute_Reference
,
3843 N_Indexed_Component
,
3844 N_Object_Renaming_Declaration
,
3845 N_Procedure_Call_Statement
,
3846 N_Selected_Component
,
3849 Establish_Transient_Scope
(Call_Node
, Sec_Stack
=> True);
3854 -------------------------------
3855 -- Expand_Ctrl_Function_Call --
3856 -------------------------------
3858 procedure Expand_Ctrl_Function_Call
(N
: Node_Id
) is
3859 function Is_Element_Reference
(N
: Node_Id
) return Boolean;
3860 -- Determine whether node N denotes a reference to an Ada 2012 container
3863 --------------------------
3864 -- Is_Element_Reference --
3865 --------------------------
3867 function Is_Element_Reference
(N
: Node_Id
) return Boolean is
3868 Ref
: constant Node_Id
:= Original_Node
(N
);
3871 -- Analysis marks an element reference by setting the generalized
3872 -- indexing attribute of an indexed component before the component
3873 -- is rewritten into a function call.
3876 Nkind
(Ref
) = N_Indexed_Component
3877 and then Present
(Generalized_Indexing
(Ref
));
3878 end Is_Element_Reference
;
3882 Is_Elem_Ref
: constant Boolean := Is_Element_Reference
(N
);
3884 -- Start of processing for Expand_Ctrl_Function_Call
3887 -- Optimization, if the returned value (which is on the sec-stack) is
3888 -- returned again, no need to copy/readjust/finalize, we can just pass
3889 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
3890 -- attachment is needed
3892 if Nkind
(Parent
(N
)) = N_Simple_Return_Statement
then
3896 -- Resolution is now finished, make sure we don't start analysis again
3897 -- because of the duplication.
3901 -- A function which returns a controlled object uses the secondary
3902 -- stack. Rewrite the call into a temporary which obtains the result of
3903 -- the function using 'reference.
3905 Remove_Side_Effects
(N
);
3907 -- When the temporary function result appears inside a case expression
3908 -- or an if expression, its lifetime must be extended to match that of
3909 -- the context. If not, the function result will be finalized too early
3910 -- and the evaluation of the expression could yield incorrect result. An
3911 -- exception to this rule are references to Ada 2012 container elements.
3912 -- Such references must be finalized at the end of each iteration of the
3913 -- related quantified expression, otherwise the container will remain
3917 and then Within_Case_Or_If_Expression
(N
)
3918 and then Nkind
(N
) = N_Explicit_Dereference
3920 Set_Is_Processed_Transient
(Entity
(Prefix
(N
)));
3922 end Expand_Ctrl_Function_Call
;
3924 ----------------------------------------
3925 -- Expand_N_Extended_Return_Statement --
3926 ----------------------------------------
3928 -- If there is a Handled_Statement_Sequence, we rewrite this:
3930 -- return Result : T := <expression> do
3931 -- <handled_seq_of_stms>
3937 -- Result : T := <expression>;
3939 -- <handled_seq_of_stms>
3943 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
3945 -- return Result : T := <expression>;
3949 -- return <expression>;
3951 -- unless it's build-in-place or there's no <expression>, in which case
3955 -- Result : T := <expression>;
3960 -- Note that this case could have been written by the user as an extended
3961 -- return statement, or could have been transformed to this from a simple
3962 -- return statement.
3964 -- That is, we need to have a reified return object if there are statements
3965 -- (which might refer to it) or if we're doing build-in-place (so we can
3966 -- set its address to the final resting place or if there is no expression
3967 -- (in which case default initial values might need to be set).
3969 procedure Expand_N_Extended_Return_Statement
(N
: Node_Id
) is
3970 Loc
: constant Source_Ptr
:= Sloc
(N
);
3972 Par_Func
: constant Entity_Id
:=
3973 Return_Applies_To
(Return_Statement_Entity
(N
));
3974 Result_Subt
: constant Entity_Id
:= Etype
(Par_Func
);
3975 Ret_Obj_Id
: constant Entity_Id
:=
3976 First_Entity
(Return_Statement_Entity
(N
));
3977 Ret_Obj_Decl
: constant Node_Id
:= Parent
(Ret_Obj_Id
);
3979 Is_Build_In_Place
: constant Boolean :=
3980 Is_Build_In_Place_Function
(Par_Func
);
3985 Return_Stmt
: Node_Id
;
3988 function Build_Heap_Allocator
3989 (Temp_Id
: Entity_Id
;
3990 Temp_Typ
: Entity_Id
;
3991 Func_Id
: Entity_Id
;
3992 Ret_Typ
: Entity_Id
;
3993 Alloc_Expr
: Node_Id
) return Node_Id
;
3994 -- Create the statements necessary to allocate a return object on the
3995 -- caller's master. The master is available through implicit parameter
3996 -- BIPfinalizationmaster.
3998 -- if BIPfinalizationmaster /= null then
4000 -- type Ptr_Typ is access Ret_Typ;
4001 -- for Ptr_Typ'Storage_Pool use
4002 -- Base_Pool (BIPfinalizationmaster.all).all;
4006 -- procedure Allocate (...) is
4008 -- System.Storage_Pools.Subpools.Allocate_Any (...);
4011 -- Local := <Alloc_Expr>;
4012 -- Temp_Id := Temp_Typ (Local);
4016 -- Temp_Id is the temporary which is used to reference the internally
4017 -- created object in all allocation forms. Temp_Typ is the type of the
4018 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
4019 -- type of Func_Id. Alloc_Expr is the actual allocator.
4021 function Move_Activation_Chain
return Node_Id
;
4022 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
4024 -- From current activation chain
4025 -- To activation chain passed in by the caller
4026 -- New_Master master passed in by the caller
4028 --------------------------
4029 -- Build_Heap_Allocator --
4030 --------------------------
4032 function Build_Heap_Allocator
4033 (Temp_Id
: Entity_Id
;
4034 Temp_Typ
: Entity_Id
;
4035 Func_Id
: Entity_Id
;
4036 Ret_Typ
: Entity_Id
;
4037 Alloc_Expr
: Node_Id
) return Node_Id
4040 pragma Assert
(Is_Build_In_Place_Function
(Func_Id
));
4042 -- Processing for build-in-place object allocation. This is disabled
4043 -- on .NET/JVM because the targets do not support pools.
4045 if VM_Target
= No_VM
4046 and then Needs_Finalization
(Ret_Typ
)
4049 Decls
: constant List_Id
:= New_List
;
4050 Fin_Mas_Id
: constant Entity_Id
:=
4051 Build_In_Place_Formal
4052 (Func_Id
, BIP_Finalization_Master
);
4053 Stmts
: constant List_Id
:= New_List
;
4054 Desig_Typ
: Entity_Id
;
4055 Local_Id
: Entity_Id
;
4056 Pool_Id
: Entity_Id
;
4057 Ptr_Typ
: Entity_Id
;
4061 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
4063 Pool_Id
:= Make_Temporary
(Loc
, 'P');
4066 Make_Object_Renaming_Declaration
(Loc
,
4067 Defining_Identifier
=> Pool_Id
,
4069 New_Occurrence_Of
(RTE
(RE_Root_Storage_Pool
), Loc
),
4071 Make_Explicit_Dereference
(Loc
,
4073 Make_Function_Call
(Loc
,
4075 New_Occurrence_Of
(RTE
(RE_Base_Pool
), Loc
),
4076 Parameter_Associations
=> New_List
(
4077 Make_Explicit_Dereference
(Loc
,
4079 New_Occurrence_Of
(Fin_Mas_Id
, Loc
)))))));
4081 -- Create an access type which uses the storage pool of the
4082 -- caller's master. This additional type is necessary because
4083 -- the finalization master cannot be associated with the type
4084 -- of the temporary. Otherwise the secondary stack allocation
4087 Desig_Typ
:= Ret_Typ
;
4089 -- Ensure that the build-in-place machinery uses a fat pointer
4090 -- when allocating an unconstrained array on the heap. In this
4091 -- case the result object type is a constrained array type even
4092 -- though the function type is unconstrained.
4094 if Ekind
(Desig_Typ
) = E_Array_Subtype
then
4095 Desig_Typ
:= Base_Type
(Desig_Typ
);
4099 -- type Ptr_Typ is access Desig_Typ;
4101 Ptr_Typ
:= Make_Temporary
(Loc
, 'P');
4104 Make_Full_Type_Declaration
(Loc
,
4105 Defining_Identifier
=> Ptr_Typ
,
4107 Make_Access_To_Object_Definition
(Loc
,
4108 Subtype_Indication
=>
4109 New_Occurrence_Of
(Desig_Typ
, Loc
))));
4111 -- Perform minor decoration in order to set the master and the
4112 -- storage pool attributes.
4114 Set_Ekind
(Ptr_Typ
, E_Access_Type
);
4115 Set_Finalization_Master
(Ptr_Typ
, Fin_Mas_Id
);
4116 Set_Associated_Storage_Pool
(Ptr_Typ
, Pool_Id
);
4118 -- Create the temporary, generate:
4119 -- Local_Id : Ptr_Typ;
4121 Local_Id
:= Make_Temporary
(Loc
, 'T');
4124 Make_Object_Declaration
(Loc
,
4125 Defining_Identifier
=> Local_Id
,
4126 Object_Definition
=>
4127 New_Occurrence_Of
(Ptr_Typ
, Loc
)));
4129 -- Allocate the object, generate:
4130 -- Local_Id := <Alloc_Expr>;
4133 Make_Assignment_Statement
(Loc
,
4134 Name
=> New_Occurrence_Of
(Local_Id
, Loc
),
4135 Expression
=> Alloc_Expr
));
4138 -- Temp_Id := Temp_Typ (Local_Id);
4141 Make_Assignment_Statement
(Loc
,
4142 Name
=> New_Occurrence_Of
(Temp_Id
, Loc
),
4144 Unchecked_Convert_To
(Temp_Typ
,
4145 New_Occurrence_Of
(Local_Id
, Loc
))));
4147 -- Wrap the allocation in a block. This is further conditioned
4148 -- by checking the caller finalization master at runtime. A
4149 -- null value indicates a non-existent master, most likely due
4150 -- to a Finalize_Storage_Only allocation.
4153 -- if BIPfinalizationmaster /= null then
4162 Make_If_Statement
(Loc
,
4165 Left_Opnd
=> New_Occurrence_Of
(Fin_Mas_Id
, Loc
),
4166 Right_Opnd
=> Make_Null
(Loc
)),
4168 Then_Statements
=> New_List
(
4169 Make_Block_Statement
(Loc
,
4170 Declarations
=> Decls
,
4171 Handled_Statement_Sequence
=>
4172 Make_Handled_Sequence_Of_Statements
(Loc
,
4173 Statements
=> Stmts
))));
4176 -- For all other cases, generate:
4177 -- Temp_Id := <Alloc_Expr>;
4181 Make_Assignment_Statement
(Loc
,
4182 Name
=> New_Occurrence_Of
(Temp_Id
, Loc
),
4183 Expression
=> Alloc_Expr
);
4185 end Build_Heap_Allocator
;
4187 ---------------------------
4188 -- Move_Activation_Chain --
4189 ---------------------------
4191 function Move_Activation_Chain
return Node_Id
is
4194 Make_Procedure_Call_Statement
(Loc
,
4196 New_Occurrence_Of
(RTE
(RE_Move_Activation_Chain
), Loc
),
4198 Parameter_Associations
=> New_List
(
4202 Make_Attribute_Reference
(Loc
,
4203 Prefix
=> Make_Identifier
(Loc
, Name_uChain
),
4204 Attribute_Name
=> Name_Unrestricted_Access
),
4206 -- Destination chain
4209 (Build_In_Place_Formal
(Par_Func
, BIP_Activation_Chain
), Loc
),
4214 (Build_In_Place_Formal
(Par_Func
, BIP_Task_Master
), Loc
)));
4215 end Move_Activation_Chain
;
4217 -- Start of processing for Expand_N_Extended_Return_Statement
4220 -- Given that functionality of interface thunks is simple (just displace
4221 -- the pointer to the object) they are always handled by means of
4222 -- simple return statements.
4224 pragma Assert
(not Is_Thunk
(Current_Scope
));
4226 if Nkind
(Ret_Obj_Decl
) = N_Object_Declaration
then
4227 Exp
:= Expression
(Ret_Obj_Decl
);
4232 HSS
:= Handled_Statement_Sequence
(N
);
4234 -- If the returned object needs finalization actions, the function must
4235 -- perform the appropriate cleanup should it fail to return. The state
4236 -- of the function itself is tracked through a flag which is coupled
4237 -- with the scope finalizer. There is one flag per each return object
4238 -- in case of multiple returns.
4240 if Is_Build_In_Place
4241 and then Needs_Finalization
(Etype
(Ret_Obj_Id
))
4244 Flag_Decl
: Node_Id
;
4245 Flag_Id
: Entity_Id
;
4249 -- Recover the function body
4251 Func_Bod
:= Unit_Declaration_Node
(Par_Func
);
4253 if Nkind
(Func_Bod
) = N_Subprogram_Declaration
then
4254 Func_Bod
:= Parent
(Parent
(Corresponding_Body
(Func_Bod
)));
4257 -- Create a flag to track the function state
4259 Flag_Id
:= Make_Temporary
(Loc
, 'F');
4260 Set_Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
, Flag_Id
);
4262 -- Insert the flag at the beginning of the function declarations,
4264 -- Fnn : Boolean := False;
4267 Make_Object_Declaration
(Loc
,
4268 Defining_Identifier
=> Flag_Id
,
4269 Object_Definition
=>
4270 New_Occurrence_Of
(Standard_Boolean
, Loc
),
4272 New_Occurrence_Of
(Standard_False
, Loc
));
4274 Prepend_To
(Declarations
(Func_Bod
), Flag_Decl
);
4275 Analyze
(Flag_Decl
);
4279 -- Build a simple_return_statement that returns the return object when
4280 -- there is a statement sequence, or no expression, or the result will
4281 -- be built in place. Note however that we currently do this for all
4282 -- composite cases, even though nonlimited composite results are not yet
4283 -- built in place (though we plan to do so eventually).
4286 or else Is_Composite_Type
(Result_Subt
)
4292 -- If the extended return has a handled statement sequence, then wrap
4293 -- it in a block and use the block as the first statement.
4297 Make_Block_Statement
(Loc
,
4298 Declarations
=> New_List
,
4299 Handled_Statement_Sequence
=> HSS
));
4302 -- If the result type contains tasks, we call Move_Activation_Chain.
4303 -- Later, the cleanup code will call Complete_Master, which will
4304 -- terminate any unactivated tasks belonging to the return statement
4305 -- master. But Move_Activation_Chain updates their master to be that
4306 -- of the caller, so they will not be terminated unless the return
4307 -- statement completes unsuccessfully due to exception, abort, goto,
4308 -- or exit. As a formality, we test whether the function requires the
4309 -- result to be built in place, though that's necessarily true for
4310 -- the case of result types with task parts.
4312 if Is_Build_In_Place
4313 and then Has_Task
(Result_Subt
)
4315 -- The return expression is an aggregate for a complex type which
4316 -- contains tasks. This particular case is left unexpanded since
4317 -- the regular expansion would insert all temporaries and
4318 -- initialization code in the wrong block.
4320 if Nkind
(Exp
) = N_Aggregate
then
4321 Expand_N_Aggregate
(Exp
);
4324 -- Do not move the activation chain if the return object does not
4327 if Has_Task
(Etype
(Ret_Obj_Id
)) then
4328 Append_To
(Stmts
, Move_Activation_Chain
);
4332 -- Update the state of the function right before the object is
4335 if Is_Build_In_Place
4336 and then Needs_Finalization
(Etype
(Ret_Obj_Id
))
4339 Flag_Id
: constant Entity_Id
:=
4340 Status_Flag_Or_Transient_Decl
(Ret_Obj_Id
);
4347 Make_Assignment_Statement
(Loc
,
4348 Name
=> New_Occurrence_Of
(Flag_Id
, Loc
),
4349 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)));
4353 -- Build a simple_return_statement that returns the return object
4356 Make_Simple_Return_Statement
(Loc
,
4357 Expression
=> New_Occurrence_Of
(Ret_Obj_Id
, Loc
));
4358 Append_To
(Stmts
, Return_Stmt
);
4360 HSS
:= Make_Handled_Sequence_Of_Statements
(Loc
, Stmts
);
4363 -- Case where we build a return statement block
4365 if Present
(HSS
) then
4367 Make_Block_Statement
(Loc
,
4368 Declarations
=> Return_Object_Declarations
(N
),
4369 Handled_Statement_Sequence
=> HSS
);
4371 -- We set the entity of the new block statement to be that of the
4372 -- return statement. This is necessary so that various fields, such
4373 -- as Finalization_Chain_Entity carry over from the return statement
4374 -- to the block. Note that this block is unusual, in that its entity
4375 -- is an E_Return_Statement rather than an E_Block.
4378 (Result
, New_Occurrence_Of
(Return_Statement_Entity
(N
), Loc
));
4380 -- If the object decl was already rewritten as a renaming, then we
4381 -- don't want to do the object allocation and transformation of of
4382 -- the return object declaration to a renaming. This case occurs
4383 -- when the return object is initialized by a call to another
4384 -- build-in-place function, and that function is responsible for
4385 -- the allocation of the return object.
4387 if Is_Build_In_Place
4388 and then Nkind
(Ret_Obj_Decl
) = N_Object_Renaming_Declaration
4391 (Nkind
(Original_Node
(Ret_Obj_Decl
)) = N_Object_Declaration
4392 and then Is_Build_In_Place_Function_Call
4393 (Expression
(Original_Node
(Ret_Obj_Decl
))));
4395 -- Return the build-in-place result by reference
4397 Set_By_Ref
(Return_Stmt
);
4399 elsif Is_Build_In_Place
then
4401 -- Locate the implicit access parameter associated with the
4402 -- caller-supplied return object and convert the return
4403 -- statement's return object declaration to a renaming of a
4404 -- dereference of the access parameter. If the return object's
4405 -- declaration includes an expression that has not already been
4406 -- expanded as separate assignments, then add an assignment
4407 -- statement to ensure the return object gets initialized.
4410 -- Result : T [:= <expression>];
4417 -- Result : T renames FuncRA.all;
4418 -- [Result := <expression;]
4423 Return_Obj_Id
: constant Entity_Id
:=
4424 Defining_Identifier
(Ret_Obj_Decl
);
4425 Return_Obj_Typ
: constant Entity_Id
:= Etype
(Return_Obj_Id
);
4426 Return_Obj_Expr
: constant Node_Id
:=
4427 Expression
(Ret_Obj_Decl
);
4428 Constr_Result
: constant Boolean :=
4429 Is_Constrained
(Result_Subt
);
4430 Obj_Alloc_Formal
: Entity_Id
;
4431 Object_Access
: Entity_Id
;
4432 Obj_Acc_Deref
: Node_Id
;
4433 Init_Assignment
: Node_Id
:= Empty
;
4436 -- Build-in-place results must be returned by reference
4438 Set_By_Ref
(Return_Stmt
);
4440 -- Retrieve the implicit access parameter passed by the caller
4443 Build_In_Place_Formal
(Par_Func
, BIP_Object_Access
);
4445 -- If the return object's declaration includes an expression
4446 -- and the declaration isn't marked as No_Initialization, then
4447 -- we need to generate an assignment to the object and insert
4448 -- it after the declaration before rewriting it as a renaming
4449 -- (otherwise we'll lose the initialization). The case where
4450 -- the result type is an interface (or class-wide interface)
4451 -- is also excluded because the context of the function call
4452 -- must be unconstrained, so the initialization will always
4453 -- be done as part of an allocator evaluation (storage pool
4454 -- or secondary stack), never to a constrained target object
4455 -- passed in by the caller. Besides the assignment being
4456 -- unneeded in this case, it avoids problems with trying to
4457 -- generate a dispatching assignment when the return expression
4458 -- is a nonlimited descendant of a limited interface (the
4459 -- interface has no assignment operation).
4461 if Present
(Return_Obj_Expr
)
4462 and then not No_Initialization
(Ret_Obj_Decl
)
4463 and then not Is_Interface
(Return_Obj_Typ
)
4466 Make_Assignment_Statement
(Loc
,
4467 Name
=> New_Occurrence_Of
(Return_Obj_Id
, Loc
),
4468 Expression
=> Relocate_Node
(Return_Obj_Expr
));
4470 Set_Etype
(Name
(Init_Assignment
), Etype
(Return_Obj_Id
));
4471 Set_Assignment_OK
(Name
(Init_Assignment
));
4472 Set_No_Ctrl_Actions
(Init_Assignment
);
4474 Set_Parent
(Name
(Init_Assignment
), Init_Assignment
);
4475 Set_Parent
(Expression
(Init_Assignment
), Init_Assignment
);
4477 Set_Expression
(Ret_Obj_Decl
, Empty
);
4479 if Is_Class_Wide_Type
(Etype
(Return_Obj_Id
))
4480 and then not Is_Class_Wide_Type
4481 (Etype
(Expression
(Init_Assignment
)))
4483 Rewrite
(Expression
(Init_Assignment
),
4484 Make_Type_Conversion
(Loc
,
4486 New_Occurrence_Of
(Etype
(Return_Obj_Id
), Loc
),
4488 Relocate_Node
(Expression
(Init_Assignment
))));
4491 -- In the case of functions where the calling context can
4492 -- determine the form of allocation needed, initialization
4493 -- is done with each part of the if statement that handles
4494 -- the different forms of allocation (this is true for
4495 -- unconstrained and tagged result subtypes).
4498 and then not Is_Tagged_Type
(Underlying_Type
(Result_Subt
))
4500 Insert_After
(Ret_Obj_Decl
, Init_Assignment
);
4504 -- When the function's subtype is unconstrained, a run-time
4505 -- test is needed to determine the form of allocation to use
4506 -- for the return object. The function has an implicit formal
4507 -- parameter indicating this. If the BIP_Alloc_Form formal has
4508 -- the value one, then the caller has passed access to an
4509 -- existing object for use as the return object. If the value
4510 -- is two, then the return object must be allocated on the
4511 -- secondary stack. Otherwise, the object must be allocated in
4512 -- a storage pool (currently only supported for the global
4513 -- heap, user-defined storage pools TBD ???). We generate an
4514 -- if statement to test the implicit allocation formal and
4515 -- initialize a local access value appropriately, creating
4516 -- allocators in the secondary stack and global heap cases.
4517 -- The special formal also exists and must be tested when the
4518 -- function has a tagged result, even when the result subtype
4519 -- is constrained, because in general such functions can be
4520 -- called in dispatching contexts and must be handled similarly
4521 -- to functions with a class-wide result.
4523 if not Constr_Result
4524 or else Is_Tagged_Type
(Underlying_Type
(Result_Subt
))
4527 Build_In_Place_Formal
(Par_Func
, BIP_Alloc_Form
);
4530 Pool_Id
: constant Entity_Id
:=
4531 Make_Temporary
(Loc
, 'P');
4532 Alloc_Obj_Id
: Entity_Id
;
4533 Alloc_Obj_Decl
: Node_Id
;
4534 Alloc_If_Stmt
: Node_Id
;
4535 Heap_Allocator
: Node_Id
;
4536 Pool_Decl
: Node_Id
;
4537 Pool_Allocator
: Node_Id
;
4538 Ptr_Type_Decl
: Node_Id
;
4539 Ref_Type
: Entity_Id
;
4540 SS_Allocator
: Node_Id
;
4543 -- Reuse the itype created for the function's implicit
4544 -- access formal. This avoids the need to create a new
4545 -- access type here, plus it allows assigning the access
4546 -- formal directly without applying a conversion.
4548 -- Ref_Type := Etype (Object_Access);
4550 -- Create an access type designating the function's
4553 Ref_Type
:= Make_Temporary
(Loc
, 'A');
4556 Make_Full_Type_Declaration
(Loc
,
4557 Defining_Identifier
=> Ref_Type
,
4559 Make_Access_To_Object_Definition
(Loc
,
4560 All_Present
=> True,
4561 Subtype_Indication
=>
4562 New_Occurrence_Of
(Return_Obj_Typ
, Loc
)));
4564 Insert_Before
(Ret_Obj_Decl
, Ptr_Type_Decl
);
4566 -- Create an access object that will be initialized to an
4567 -- access value denoting the return object, either coming
4568 -- from an implicit access value passed in by the caller
4569 -- or from the result of an allocator.
4571 Alloc_Obj_Id
:= Make_Temporary
(Loc
, 'R');
4572 Set_Etype
(Alloc_Obj_Id
, Ref_Type
);
4575 Make_Object_Declaration
(Loc
,
4576 Defining_Identifier
=> Alloc_Obj_Id
,
4577 Object_Definition
=>
4578 New_Occurrence_Of
(Ref_Type
, Loc
));
4580 Insert_Before
(Ret_Obj_Decl
, Alloc_Obj_Decl
);
4582 -- Create allocators for both the secondary stack and
4583 -- global heap. If there's an initialization expression,
4584 -- then create these as initialized allocators.
4586 if Present
(Return_Obj_Expr
)
4587 and then not No_Initialization
(Ret_Obj_Decl
)
4589 -- Always use the type of the expression for the
4590 -- qualified expression, rather than the result type.
4591 -- In general we cannot always use the result type
4592 -- for the allocator, because the expression might be
4593 -- of a specific type, such as in the case of an
4594 -- aggregate or even a nonlimited object when the
4595 -- result type is a limited class-wide interface type.
4598 Make_Allocator
(Loc
,
4600 Make_Qualified_Expression
(Loc
,
4603 (Etype
(Return_Obj_Expr
), Loc
),
4605 New_Copy_Tree
(Return_Obj_Expr
)));
4608 -- If the function returns a class-wide type we cannot
4609 -- use the return type for the allocator. Instead we
4610 -- use the type of the expression, which must be an
4611 -- aggregate of a definite type.
4613 if Is_Class_Wide_Type
(Return_Obj_Typ
) then
4615 Make_Allocator
(Loc
,
4618 (Etype
(Return_Obj_Expr
), Loc
));
4621 Make_Allocator
(Loc
,
4623 New_Occurrence_Of
(Return_Obj_Typ
, Loc
));
4626 -- If the object requires default initialization then
4627 -- that will happen later following the elaboration of
4628 -- the object renaming. If we don't turn it off here
4629 -- then the object will be default initialized twice.
4631 Set_No_Initialization
(Heap_Allocator
);
4634 -- The Pool_Allocator is just like the Heap_Allocator,
4635 -- except we set Storage_Pool and Procedure_To_Call so
4636 -- it will use the user-defined storage pool.
4638 Pool_Allocator
:= New_Copy_Tree
(Heap_Allocator
);
4640 -- Do not generate the renaming of the build-in-place
4641 -- pool parameter on .NET/JVM/ZFP because the parameter
4642 -- is not created in the first place.
4644 if VM_Target
= No_VM
4645 and then RTE_Available
(RE_Root_Storage_Pool_Ptr
)
4648 Make_Object_Renaming_Declaration
(Loc
,
4649 Defining_Identifier
=> Pool_Id
,
4652 (RTE
(RE_Root_Storage_Pool
), Loc
),
4654 Make_Explicit_Dereference
(Loc
,
4656 (Build_In_Place_Formal
4657 (Par_Func
, BIP_Storage_Pool
), Loc
)));
4658 Set_Storage_Pool
(Pool_Allocator
, Pool_Id
);
4659 Set_Procedure_To_Call
4660 (Pool_Allocator
, RTE
(RE_Allocate_Any
));
4662 Pool_Decl
:= Make_Null_Statement
(Loc
);
4665 -- If the No_Allocators restriction is active, then only
4666 -- an allocator for secondary stack allocation is needed.
4667 -- It's OK for such allocators to have Comes_From_Source
4668 -- set to False, because gigi knows not to flag them as
4669 -- being a violation of No_Implicit_Heap_Allocations.
4671 if Restriction_Active
(No_Allocators
) then
4672 SS_Allocator
:= Heap_Allocator
;
4673 Heap_Allocator
:= Make_Null
(Loc
);
4674 Pool_Allocator
:= Make_Null
(Loc
);
4676 -- Otherwise the heap and pool allocators may be needed,
4677 -- so we make another allocator for secondary stack
4681 SS_Allocator
:= New_Copy_Tree
(Heap_Allocator
);
4683 -- The heap and pool allocators are marked as
4684 -- Comes_From_Source since they correspond to an
4685 -- explicit user-written allocator (that is, it will
4686 -- only be executed on behalf of callers that call the
4687 -- function as initialization for such an allocator).
4688 -- Prevents errors when No_Implicit_Heap_Allocations
4691 Set_Comes_From_Source
(Heap_Allocator
, True);
4692 Set_Comes_From_Source
(Pool_Allocator
, True);
4695 -- The allocator is returned on the secondary stack. We
4696 -- don't do this on VM targets, since the SS is not used.
4698 if VM_Target
= No_VM
then
4699 Set_Storage_Pool
(SS_Allocator
, RTE
(RE_SS_Pool
));
4700 Set_Procedure_To_Call
4701 (SS_Allocator
, RTE
(RE_SS_Allocate
));
4703 -- The allocator is returned on the secondary stack,
4704 -- so indicate that the function return, as well as
4705 -- the block that encloses the allocator, must not
4706 -- release it. The flags must be set now because
4707 -- the decision to use the secondary stack is done
4708 -- very late in the course of expanding the return
4709 -- statement, past the point where these flags are
4712 Set_Sec_Stack_Needed_For_Return
(Par_Func
);
4713 Set_Sec_Stack_Needed_For_Return
4714 (Return_Statement_Entity
(N
));
4715 Set_Uses_Sec_Stack
(Par_Func
);
4716 Set_Uses_Sec_Stack
(Return_Statement_Entity
(N
));
4719 -- Create an if statement to test the BIP_Alloc_Form
4720 -- formal and initialize the access object to either the
4721 -- BIP_Object_Access formal (BIP_Alloc_Form =
4722 -- Caller_Allocation), the result of allocating the
4723 -- object in the secondary stack (BIP_Alloc_Form =
4724 -- Secondary_Stack), or else an allocator to create the
4725 -- return object in the heap or user-defined pool
4726 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
4728 -- ??? An unchecked type conversion must be made in the
4729 -- case of assigning the access object formal to the
4730 -- local access object, because a normal conversion would
4731 -- be illegal in some cases (such as converting access-
4732 -- to-unconstrained to access-to-constrained), but the
4733 -- the unchecked conversion will presumably fail to work
4734 -- right in just such cases. It's not clear at all how to
4738 Make_If_Statement
(Loc
,
4742 New_Occurrence_Of
(Obj_Alloc_Formal
, Loc
),
4744 Make_Integer_Literal
(Loc
,
4745 UI_From_Int
(BIP_Allocation_Form
'Pos
4746 (Caller_Allocation
)))),
4748 Then_Statements
=> New_List
(
4749 Make_Assignment_Statement
(Loc
,
4751 New_Occurrence_Of
(Alloc_Obj_Id
, Loc
),
4753 Make_Unchecked_Type_Conversion
(Loc
,
4755 New_Occurrence_Of
(Ref_Type
, Loc
),
4757 New_Occurrence_Of
(Object_Access
, Loc
)))),
4759 Elsif_Parts
=> New_List
(
4760 Make_Elsif_Part
(Loc
,
4764 New_Occurrence_Of
(Obj_Alloc_Formal
, Loc
),
4766 Make_Integer_Literal
(Loc
,
4767 UI_From_Int
(BIP_Allocation_Form
'Pos
4768 (Secondary_Stack
)))),
4770 Then_Statements
=> New_List
(
4771 Make_Assignment_Statement
(Loc
,
4773 New_Occurrence_Of
(Alloc_Obj_Id
, Loc
),
4774 Expression
=> SS_Allocator
))),
4776 Make_Elsif_Part
(Loc
,
4780 New_Occurrence_Of
(Obj_Alloc_Formal
, Loc
),
4782 Make_Integer_Literal
(Loc
,
4783 UI_From_Int
(BIP_Allocation_Form
'Pos
4786 Then_Statements
=> New_List
(
4787 Build_Heap_Allocator
4788 (Temp_Id
=> Alloc_Obj_Id
,
4789 Temp_Typ
=> Ref_Type
,
4790 Func_Id
=> Par_Func
,
4791 Ret_Typ
=> Return_Obj_Typ
,
4792 Alloc_Expr
=> Heap_Allocator
)))),
4794 Else_Statements
=> New_List
(
4796 Build_Heap_Allocator
4797 (Temp_Id
=> Alloc_Obj_Id
,
4798 Temp_Typ
=> Ref_Type
,
4799 Func_Id
=> Par_Func
,
4800 Ret_Typ
=> Return_Obj_Typ
,
4801 Alloc_Expr
=> Pool_Allocator
)));
4803 -- If a separate initialization assignment was created
4804 -- earlier, append that following the assignment of the
4805 -- implicit access formal to the access object, to ensure
4806 -- that the return object is initialized in that case. In
4807 -- this situation, the target of the assignment must be
4808 -- rewritten to denote a dereference of the access to the
4809 -- return object passed in by the caller.
4811 if Present
(Init_Assignment
) then
4812 Rewrite
(Name
(Init_Assignment
),
4813 Make_Explicit_Dereference
(Loc
,
4814 Prefix
=> New_Occurrence_Of
(Alloc_Obj_Id
, Loc
)));
4817 (Name
(Init_Assignment
), Etype
(Return_Obj_Id
));
4820 (Then_Statements
(Alloc_If_Stmt
), Init_Assignment
);
4823 Insert_Before
(Ret_Obj_Decl
, Alloc_If_Stmt
);
4825 -- Remember the local access object for use in the
4826 -- dereference of the renaming created below.
4828 Object_Access
:= Alloc_Obj_Id
;
4832 -- Replace the return object declaration with a renaming of a
4833 -- dereference of the access value designating the return
4837 Make_Explicit_Dereference
(Loc
,
4838 Prefix
=> New_Occurrence_Of
(Object_Access
, Loc
));
4840 Rewrite
(Ret_Obj_Decl
,
4841 Make_Object_Renaming_Declaration
(Loc
,
4842 Defining_Identifier
=> Return_Obj_Id
,
4843 Access_Definition
=> Empty
,
4845 New_Occurrence_Of
(Return_Obj_Typ
, Loc
),
4846 Name
=> Obj_Acc_Deref
));
4848 Set_Renamed_Object
(Return_Obj_Id
, Obj_Acc_Deref
);
4852 -- Case where we do not build a block
4855 -- We're about to drop Return_Object_Declarations on the floor, so
4856 -- we need to insert it, in case it got expanded into useful code.
4857 -- Remove side effects from expression, which may be duplicated in
4858 -- subsequent checks (see Expand_Simple_Function_Return).
4860 Insert_List_Before
(N
, Return_Object_Declarations
(N
));
4861 Remove_Side_Effects
(Exp
);
4863 -- Build simple_return_statement that returns the expression directly
4865 Return_Stmt
:= Make_Simple_Return_Statement
(Loc
, Expression
=> Exp
);
4866 Result
:= Return_Stmt
;
4869 -- Set the flag to prevent infinite recursion
4871 Set_Comes_From_Extended_Return_Statement
(Return_Stmt
);
4873 Rewrite
(N
, Result
);
4875 end Expand_N_Extended_Return_Statement
;
4877 ----------------------------
4878 -- Expand_N_Function_Call --
4879 ----------------------------
4881 procedure Expand_N_Function_Call
(N
: Node_Id
) is
4884 end Expand_N_Function_Call
;
4886 ---------------------------------------
4887 -- Expand_N_Procedure_Call_Statement --
4888 ---------------------------------------
4890 procedure Expand_N_Procedure_Call_Statement
(N
: Node_Id
) is
4893 end Expand_N_Procedure_Call_Statement
;
4895 --------------------------------------
4896 -- Expand_N_Simple_Return_Statement --
4897 --------------------------------------
4899 procedure Expand_N_Simple_Return_Statement
(N
: Node_Id
) is
4901 -- Defend against previous errors (i.e. the return statement calls a
4902 -- function that is not available in configurable runtime).
4904 if Present
(Expression
(N
))
4905 and then Nkind
(Expression
(N
)) = N_Empty
4907 Check_Error_Detected
;
4911 -- Distinguish the function and non-function cases:
4913 case Ekind
(Return_Applies_To
(Return_Statement_Entity
(N
))) is
4916 E_Generic_Function
=>
4917 Expand_Simple_Function_Return
(N
);
4920 E_Generic_Procedure |
4923 E_Return_Statement
=>
4924 Expand_Non_Function_Return
(N
);
4927 raise Program_Error
;
4931 when RE_Not_Available
=>
4933 end Expand_N_Simple_Return_Statement
;
4935 ------------------------------
4936 -- Expand_N_Subprogram_Body --
4937 ------------------------------
4939 -- Add poll call if ATC polling is enabled, unless the body will be inlined
4942 -- Add dummy push/pop label nodes at start and end to clear any local
4943 -- exception indications if local-exception-to-goto optimization is active.
4945 -- Add return statement if last statement in body is not a return statement
4946 -- (this makes things easier on Gigi which does not want to have to handle
4947 -- a missing return).
4949 -- Add call to Activate_Tasks if body is a task activator
4951 -- Deal with possible detection of infinite recursion
4953 -- Eliminate body completely if convention stubbed
4955 -- Encode entity names within body, since we will not need to reference
4956 -- these entities any longer in the front end.
4958 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
4960 -- Reset Pure indication if any parameter has root type System.Address
4961 -- or has any parameters of limited types, where limited means that the
4962 -- run-time view is limited (i.e. the full type is limited).
4966 procedure Expand_N_Subprogram_Body
(N
: Node_Id
) is
4967 Loc
: constant Source_Ptr
:= Sloc
(N
);
4968 H
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
4969 Body_Id
: Entity_Id
;
4972 Spec_Id
: Entity_Id
;
4974 procedure Add_Return
(S
: List_Id
);
4975 -- Append a return statement to the statement sequence S if the last
4976 -- statement is not already a return or a goto statement. Note that
4977 -- the latter test is not critical, it does not matter if we add a few
4978 -- extra returns, since they get eliminated anyway later on.
4984 procedure Add_Return
(S
: List_Id
) is
4985 Last_Stmt
: Node_Id
;
4990 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
4991 -- not relevant in this context since they are not executable.
4993 Last_Stmt
:= Last
(S
);
4994 while Nkind
(Last_Stmt
) in N_Pop_xxx_Label
loop
4998 -- Now insert return unless last statement is a transfer
5000 if not Is_Transfer
(Last_Stmt
) then
5002 -- The source location for the return is the end label of the
5003 -- procedure if present. Otherwise use the sloc of the last
5004 -- statement in the list. If the list comes from a generated
5005 -- exception handler and we are not debugging generated code,
5006 -- all the statements within the handler are made invisible
5009 if Nkind
(Parent
(S
)) = N_Exception_Handler
5010 and then not Comes_From_Source
(Parent
(S
))
5012 Loc
:= Sloc
(Last_Stmt
);
5013 elsif Present
(End_Label
(H
)) then
5014 Loc
:= Sloc
(End_Label
(H
));
5016 Loc
:= Sloc
(Last_Stmt
);
5019 -- Append return statement, and set analyzed manually. We can't
5020 -- call Analyze on this return since the scope is wrong.
5022 -- Note: it almost works to push the scope and then do the Analyze
5023 -- call, but something goes wrong in some weird cases and it is
5024 -- not worth worrying about ???
5026 Stmt
:= Make_Simple_Return_Statement
(Loc
);
5028 -- The return statement is handled properly, and the call to the
5029 -- postcondition, inserted below, does not require information
5030 -- from the body either. However, that call is analyzed in the
5031 -- enclosing scope, and an elaboration check might improperly be
5032 -- added to it. A guard in Sem_Elab is needed to prevent that
5033 -- spurious check, see Check_Elab_Call.
5035 Append_To
(S
, Stmt
);
5036 Set_Analyzed
(Stmt
);
5038 -- Call the _Postconditions procedure if the related subprogram
5039 -- has contract assertions that need to be verified on exit.
5041 if Ekind
(Spec_Id
) = E_Procedure
5042 and then Present
(Postconditions_Proc
(Spec_Id
))
5044 Insert_Action
(Stmt
,
5045 Make_Procedure_Call_Statement
(Loc
,
5047 New_Occurrence_Of
(Postconditions_Proc
(Spec_Id
), Loc
)));
5052 -- Start of processing for Expand_N_Subprogram_Body
5055 -- Set L to either the list of declarations if present, or to the list
5056 -- of statements if no declarations are present. This is used to insert
5057 -- new stuff at the start.
5059 if Is_Non_Empty_List
(Declarations
(N
)) then
5060 L
:= Declarations
(N
);
5062 L
:= Statements
(H
);
5065 -- If local-exception-to-goto optimization active, insert dummy push
5066 -- statements at start, and dummy pop statements at end, but inhibit
5067 -- this if we have No_Exception_Handlers, since they are useless and
5068 -- intefere with analysis, e.g. by codepeer.
5070 if (Debug_Flag_Dot_G
5071 or else Restriction_Active
(No_Exception_Propagation
))
5072 and then not Restriction_Active
(No_Exception_Handlers
)
5073 and then not CodePeer_Mode
5074 and then Is_Non_Empty_List
(L
)
5077 FS
: constant Node_Id
:= First
(L
);
5078 FL
: constant Source_Ptr
:= Sloc
(FS
);
5083 -- LS points to either last statement, if statements are present
5084 -- or to the last declaration if there are no statements present.
5085 -- It is the node after which the pop's are generated.
5087 if Is_Non_Empty_List
(Statements
(H
)) then
5088 LS
:= Last
(Statements
(H
));
5095 Insert_List_Before_And_Analyze
(FS
, New_List
(
5096 Make_Push_Constraint_Error_Label
(FL
),
5097 Make_Push_Program_Error_Label
(FL
),
5098 Make_Push_Storage_Error_Label
(FL
)));
5100 Insert_List_After_And_Analyze
(LS
, New_List
(
5101 Make_Pop_Constraint_Error_Label
(LL
),
5102 Make_Pop_Program_Error_Label
(LL
),
5103 Make_Pop_Storage_Error_Label
(LL
)));
5107 -- Find entity for subprogram
5109 Body_Id
:= Defining_Entity
(N
);
5111 if Present
(Corresponding_Spec
(N
)) then
5112 Spec_Id
:= Corresponding_Spec
(N
);
5117 -- Need poll on entry to subprogram if polling enabled. We only do this
5118 -- for non-empty subprograms, since it does not seem necessary to poll
5119 -- for a dummy null subprogram.
5121 if Is_Non_Empty_List
(L
) then
5123 -- Do not add a polling call if the subprogram is to be inlined by
5124 -- the back-end, to avoid repeated calls with multiple inlinings.
5126 if Is_Inlined
(Spec_Id
)
5127 and then Front_End_Inlining
5128 and then Optimization_Level
> 1
5132 Generate_Poll_Call
(First
(L
));
5136 -- If this is a Pure function which has any parameters whose root type
5137 -- is System.Address, reset the Pure indication, since it will likely
5138 -- cause incorrect code to be generated as the parameter is probably
5139 -- a pointer, and the fact that the same pointer is passed does not mean
5140 -- that the same value is being referenced.
5142 -- Note that if the programmer gave an explicit Pure_Function pragma,
5143 -- then we believe the programmer, and leave the subprogram Pure.
5145 -- This code should probably be at the freeze point, so that it happens
5146 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
5147 -- semantic tree has Is_Pure set properly ???
5149 if Is_Pure
(Spec_Id
)
5150 and then Is_Subprogram
(Spec_Id
)
5151 and then not Has_Pragma_Pure_Function
(Spec_Id
)
5157 F
:= First_Formal
(Spec_Id
);
5158 while Present
(F
) loop
5159 if Is_Descendent_Of_Address
(Etype
(F
))
5161 -- Note that this test is being made in the body of the
5162 -- subprogram, not the spec, so we are testing the full
5163 -- type for being limited here, as required.
5165 or else Is_Limited_Type
(Etype
(F
))
5167 Set_Is_Pure
(Spec_Id
, False);
5169 if Spec_Id
/= Body_Id
then
5170 Set_Is_Pure
(Body_Id
, False);
5181 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5183 if Init_Or_Norm_Scalars
and then Is_Subprogram
(Spec_Id
) then
5189 -- Loop through formals
5191 F
:= First_Formal
(Spec_Id
);
5192 while Present
(F
) loop
5193 if Is_Scalar_Type
(Etype
(F
))
5194 and then Ekind
(F
) = E_Out_Parameter
5196 Check_Restriction
(No_Default_Initialization
, F
);
5198 -- Insert the initialization. We turn off validity checks
5199 -- for this assignment, since we do not want any check on
5200 -- the initial value itself (which may well be invalid).
5201 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5204 Make_Assignment_Statement
(Loc
,
5205 Name
=> New_Occurrence_Of
(F
, Loc
),
5206 Expression
=> Get_Simple_Init_Val
(Etype
(F
), N
));
5207 Set_Suppress_Assignment_Checks
(A
);
5209 Insert_Before_And_Analyze
(First
(L
),
5210 A
, Suppress
=> Validity_Check
);
5218 -- Clear out statement list for stubbed procedure
5220 if Present
(Corresponding_Spec
(N
)) then
5221 Set_Elaboration_Flag
(N
, Spec_Id
);
5223 if Convention
(Spec_Id
) = Convention_Stubbed
5224 or else Is_Eliminated
(Spec_Id
)
5226 Set_Declarations
(N
, Empty_List
);
5227 Set_Handled_Statement_Sequence
(N
,
5228 Make_Handled_Sequence_Of_Statements
(Loc
,
5229 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
5234 -- Create a set of discriminals for the next protected subprogram body
5236 if Is_List_Member
(N
)
5237 and then Present
(Parent
(List_Containing
(N
)))
5238 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
5239 and then Present
(Next_Protected_Operation
(N
))
5241 Set_Discriminals
(Parent
(Base_Type
(Scope
(Spec_Id
))));
5244 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5245 -- subprograms with no specs are not frozen.
5248 Typ
: constant Entity_Id
:= Etype
(Spec_Id
);
5249 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
5252 if not Acts_As_Spec
(N
)
5253 and then Nkind
(Parent
(Parent
(Spec_Id
))) /=
5254 N_Subprogram_Body_Stub
5258 elsif Is_Limited_View
(Typ
) then
5259 Set_Returns_By_Ref
(Spec_Id
);
5261 elsif Present
(Utyp
) and then CW_Or_Has_Controlled_Part
(Utyp
) then
5262 Set_Returns_By_Ref
(Spec_Id
);
5266 -- For a procedure, we add a return for all possible syntactic ends of
5269 if Ekind_In
(Spec_Id
, E_Procedure
, E_Generic_Procedure
) then
5270 Add_Return
(Statements
(H
));
5272 if Present
(Exception_Handlers
(H
)) then
5273 Except_H
:= First_Non_Pragma
(Exception_Handlers
(H
));
5274 while Present
(Except_H
) loop
5275 Add_Return
(Statements
(Except_H
));
5276 Next_Non_Pragma
(Except_H
);
5280 -- For a function, we must deal with the case where there is at least
5281 -- one missing return. What we do is to wrap the entire body of the
5282 -- function in a block:
5295 -- raise Program_Error;
5298 -- This approach is necessary because the raise must be signalled to the
5299 -- caller, not handled by any local handler (RM 6.4(11)).
5301 -- Note: we do not need to analyze the constructed sequence here, since
5302 -- it has no handler, and an attempt to analyze the handled statement
5303 -- sequence twice is risky in various ways (e.g. the issue of expanding
5304 -- cleanup actions twice).
5306 elsif Has_Missing_Return
(Spec_Id
) then
5308 Hloc
: constant Source_Ptr
:= Sloc
(H
);
5309 Blok
: constant Node_Id
:=
5310 Make_Block_Statement
(Hloc
,
5311 Handled_Statement_Sequence
=> H
);
5312 Rais
: constant Node_Id
:=
5313 Make_Raise_Program_Error
(Hloc
,
5314 Reason
=> PE_Missing_Return
);
5317 Set_Handled_Statement_Sequence
(N
,
5318 Make_Handled_Sequence_Of_Statements
(Hloc
,
5319 Statements
=> New_List
(Blok
, Rais
)));
5321 Push_Scope
(Spec_Id
);
5328 -- If subprogram contains a parameterless recursive call, then we may
5329 -- have an infinite recursion, so see if we can generate code to check
5330 -- for this possibility if storage checks are not suppressed.
5332 if Ekind
(Spec_Id
) = E_Procedure
5333 and then Has_Recursive_Call
(Spec_Id
)
5334 and then not Storage_Checks_Suppressed
(Spec_Id
)
5336 Detect_Infinite_Recursion
(N
, Spec_Id
);
5339 -- Set to encode entity names in package body before gigi is called
5341 Qualify_Entity_Names
(N
);
5342 end Expand_N_Subprogram_Body
;
5344 -----------------------------------
5345 -- Expand_N_Subprogram_Body_Stub --
5346 -----------------------------------
5348 procedure Expand_N_Subprogram_Body_Stub
(N
: Node_Id
) is
5350 if Present
(Corresponding_Body
(N
)) then
5351 Expand_N_Subprogram_Body
(
5352 Unit_Declaration_Node
(Corresponding_Body
(N
)));
5354 end Expand_N_Subprogram_Body_Stub
;
5356 -------------------------------------
5357 -- Expand_N_Subprogram_Declaration --
5358 -------------------------------------
5360 -- If the declaration appears within a protected body, it is a private
5361 -- operation of the protected type. We must create the corresponding
5362 -- protected subprogram an associated formals. For a normal protected
5363 -- operation, this is done when expanding the protected type declaration.
5365 -- If the declaration is for a null procedure, emit null body
5367 procedure Expand_N_Subprogram_Declaration
(N
: Node_Id
) is
5368 Loc
: constant Source_Ptr
:= Sloc
(N
);
5369 Subp
: constant Entity_Id
:= Defining_Entity
(N
);
5370 Scop
: constant Entity_Id
:= Scope
(Subp
);
5371 Prot_Decl
: Node_Id
;
5373 Prot_Id
: Entity_Id
;
5376 -- In SPARK, subprogram declarations are only allowed in package
5379 if Nkind
(Parent
(N
)) /= N_Package_Specification
then
5380 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5381 Check_SPARK_05_Restriction
5382 ("subprogram declaration is not a library item", N
);
5384 elsif Present
(Next
(N
))
5385 and then Nkind
(Next
(N
)) = N_Pragma
5386 and then Get_Pragma_Id
(Pragma_Name
(Next
(N
))) = Pragma_Import
5388 -- In SPARK, subprogram declarations are also permitted in
5389 -- declarative parts when immediately followed by a corresponding
5390 -- pragma Import. We only check here that there is some pragma
5395 Check_SPARK_05_Restriction
5396 ("subprogram declaration is not allowed here", N
);
5400 -- Deal with case of protected subprogram. Do not generate protected
5401 -- operation if operation is flagged as eliminated.
5403 if Is_List_Member
(N
)
5404 and then Present
(Parent
(List_Containing
(N
)))
5405 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
5406 and then Is_Protected_Type
(Scop
)
5408 if No
(Protected_Body_Subprogram
(Subp
))
5409 and then not Is_Eliminated
(Subp
)
5412 Make_Subprogram_Declaration
(Loc
,
5414 Build_Protected_Sub_Specification
5415 (N
, Scop
, Unprotected_Mode
));
5417 -- The protected subprogram is declared outside of the protected
5418 -- body. Given that the body has frozen all entities so far, we
5419 -- analyze the subprogram and perform freezing actions explicitly.
5420 -- including the generation of an explicit freeze node, to ensure
5421 -- that gigi has the proper order of elaboration.
5422 -- If the body is a subunit, the insertion point is before the
5423 -- stub in the parent.
5425 Prot_Bod
:= Parent
(List_Containing
(N
));
5427 if Nkind
(Parent
(Prot_Bod
)) = N_Subunit
then
5428 Prot_Bod
:= Corresponding_Stub
(Parent
(Prot_Bod
));
5431 Insert_Before
(Prot_Bod
, Prot_Decl
);
5432 Prot_Id
:= Defining_Unit_Name
(Specification
(Prot_Decl
));
5433 Set_Has_Delayed_Freeze
(Prot_Id
);
5435 Push_Scope
(Scope
(Scop
));
5436 Analyze
(Prot_Decl
);
5437 Freeze_Before
(N
, Prot_Id
);
5438 Set_Protected_Body_Subprogram
(Subp
, Prot_Id
);
5440 -- Create protected operation as well. Even though the operation
5441 -- is only accessible within the body, it is possible to make it
5442 -- available outside of the protected object by using 'Access to
5443 -- provide a callback, so build protected version in all cases.
5446 Make_Subprogram_Declaration
(Loc
,
5448 Build_Protected_Sub_Specification
(N
, Scop
, Protected_Mode
));
5449 Insert_Before
(Prot_Bod
, Prot_Decl
);
5450 Analyze
(Prot_Decl
);
5455 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
5456 -- cases this is superfluous because calls to it will be automatically
5457 -- inlined, but we definitely need the body if preconditions for the
5458 -- procedure are present.
5460 elsif Nkind
(Specification
(N
)) = N_Procedure_Specification
5461 and then Null_Present
(Specification
(N
))
5464 Bod
: constant Node_Id
:= Body_To_Inline
(N
);
5467 Set_Has_Completion
(Subp
, False);
5468 Append_Freeze_Action
(Subp
, Bod
);
5470 -- The body now contains raise statements, so calls to it will
5473 Set_Is_Inlined
(Subp
, False);
5476 end Expand_N_Subprogram_Declaration
;
5478 --------------------------------
5479 -- Expand_Non_Function_Return --
5480 --------------------------------
5482 procedure Expand_Non_Function_Return
(N
: Node_Id
) is
5483 pragma Assert
(No
(Expression
(N
)));
5485 Loc
: constant Source_Ptr
:= Sloc
(N
);
5486 Scope_Id
: Entity_Id
:= Return_Applies_To
(Return_Statement_Entity
(N
));
5487 Kind
: constant Entity_Kind
:= Ekind
(Scope_Id
);
5490 Goto_Stat
: Node_Id
;
5494 -- Call the _Postconditions procedure if the related subprogram has
5495 -- contract assertions that need to be verified on exit.
5497 if Ekind_In
(Scope_Id
, E_Entry
, E_Entry_Family
, E_Procedure
)
5498 and then Present
(Postconditions_Proc
(Scope_Id
))
5501 Make_Procedure_Call_Statement
(Loc
,
5502 Name
=> New_Occurrence_Of
(Postconditions_Proc
(Scope_Id
), Loc
)));
5505 -- If it is a return from a procedure do no extra steps
5507 if Kind
= E_Procedure
or else Kind
= E_Generic_Procedure
then
5510 -- If it is a nested return within an extended one, replace it with a
5511 -- return of the previously declared return object.
5513 elsif Kind
= E_Return_Statement
then
5515 Make_Simple_Return_Statement
(Loc
,
5517 New_Occurrence_Of
(First_Entity
(Scope_Id
), Loc
)));
5518 Set_Comes_From_Extended_Return_Statement
(N
);
5519 Set_Return_Statement_Entity
(N
, Scope_Id
);
5520 Expand_Simple_Function_Return
(N
);
5524 pragma Assert
(Is_Entry
(Scope_Id
));
5526 -- Look at the enclosing block to see whether the return is from an
5527 -- accept statement or an entry body.
5529 for J
in reverse 0 .. Scope_Stack
.Last
loop
5530 Scope_Id
:= Scope_Stack
.Table
(J
).Entity
;
5531 exit when Is_Concurrent_Type
(Scope_Id
);
5534 -- If it is a return from accept statement it is expanded as call to
5535 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
5537 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
5538 -- Expand_N_Accept_Alternative in exp_ch9.adb)
5540 if Is_Task_Type
(Scope_Id
) then
5543 Make_Procedure_Call_Statement
(Loc
,
5544 Name
=> New_Occurrence_Of
(RTE
(RE_Complete_Rendezvous
), Loc
));
5545 Insert_Before
(N
, Call
);
5546 -- why not insert actions here???
5549 Acc_Stat
:= Parent
(N
);
5550 while Nkind
(Acc_Stat
) /= N_Accept_Statement
loop
5551 Acc_Stat
:= Parent
(Acc_Stat
);
5554 Lab_Node
:= Last
(Statements
5555 (Handled_Statement_Sequence
(Acc_Stat
)));
5557 Goto_Stat
:= Make_Goto_Statement
(Loc
,
5558 Name
=> New_Occurrence_Of
5559 (Entity
(Identifier
(Lab_Node
)), Loc
));
5561 Set_Analyzed
(Goto_Stat
);
5563 Rewrite
(N
, Goto_Stat
);
5566 -- If it is a return from an entry body, put a Complete_Entry_Body call
5567 -- in front of the return.
5569 elsif Is_Protected_Type
(Scope_Id
) then
5571 Make_Procedure_Call_Statement
(Loc
,
5573 New_Occurrence_Of
(RTE
(RE_Complete_Entry_Body
), Loc
),
5574 Parameter_Associations
=> New_List
(
5575 Make_Attribute_Reference
(Loc
,
5578 (Find_Protection_Object
(Current_Scope
), Loc
),
5579 Attribute_Name
=> Name_Unchecked_Access
)));
5581 Insert_Before
(N
, Call
);
5584 end Expand_Non_Function_Return
;
5586 ---------------------------------------
5587 -- Expand_Protected_Object_Reference --
5588 ---------------------------------------
5590 function Expand_Protected_Object_Reference
5592 Scop
: Entity_Id
) return Node_Id
5594 Loc
: constant Source_Ptr
:= Sloc
(N
);
5601 Rec
:= Make_Identifier
(Loc
, Name_uObject
);
5602 Set_Etype
(Rec
, Corresponding_Record_Type
(Scop
));
5604 -- Find enclosing protected operation, and retrieve its first parameter,
5605 -- which denotes the enclosing protected object. If the enclosing
5606 -- operation is an entry, we are immediately within the protected body,
5607 -- and we can retrieve the object from the service entries procedure. A
5608 -- barrier function has the same signature as an entry. A barrier
5609 -- function is compiled within the protected object, but unlike
5610 -- protected operations its never needs locks, so that its protected
5611 -- body subprogram points to itself.
5613 Proc
:= Current_Scope
;
5614 while Present
(Proc
)
5615 and then Scope
(Proc
) /= Scop
5617 Proc
:= Scope
(Proc
);
5620 Corr
:= Protected_Body_Subprogram
(Proc
);
5624 -- Previous error left expansion incomplete.
5625 -- Nothing to do on this call.
5632 (First
(Parameter_Specifications
(Parent
(Corr
))));
5634 if Is_Subprogram
(Proc
) and then Proc
/= Corr
then
5636 -- Protected function or procedure
5638 Set_Entity
(Rec
, Param
);
5640 -- Rec is a reference to an entity which will not be in scope when
5641 -- the call is reanalyzed, and needs no further analysis.
5646 -- Entry or barrier function for entry body. The first parameter of
5647 -- the entry body procedure is pointer to the object. We create a
5648 -- local variable of the proper type, duplicating what is done to
5649 -- define _object later on.
5653 Obj_Ptr
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
5657 Make_Full_Type_Declaration
(Loc
,
5658 Defining_Identifier
=> Obj_Ptr
,
5660 Make_Access_To_Object_Definition
(Loc
,
5661 Subtype_Indication
=>
5663 (Corresponding_Record_Type
(Scop
), Loc
))));
5665 Insert_Actions
(N
, Decls
);
5666 Freeze_Before
(N
, Obj_Ptr
);
5669 Make_Explicit_Dereference
(Loc
,
5671 Unchecked_Convert_To
(Obj_Ptr
,
5672 New_Occurrence_Of
(Param
, Loc
)));
5674 -- Analyze new actual. Other actuals in calls are already analyzed
5675 -- and the list of actuals is not reanalyzed after rewriting.
5677 Set_Parent
(Rec
, N
);
5683 end Expand_Protected_Object_Reference
;
5685 --------------------------------------
5686 -- Expand_Protected_Subprogram_Call --
5687 --------------------------------------
5689 procedure Expand_Protected_Subprogram_Call
5696 procedure Freeze_Called_Function
;
5697 -- If it is a function call it can appear in elaboration code and
5698 -- the called entity must be frozen before the call. This must be
5699 -- done before the call is expanded, as the expansion may rewrite it
5700 -- to something other than a call (e.g. a temporary initialized in a
5701 -- transient block).
5703 ----------------------------
5704 -- Freeze_Called_Function --
5705 ----------------------------
5707 procedure Freeze_Called_Function
is
5709 if Ekind
(Subp
) = E_Function
then
5710 Freeze_Expression
(Name
(N
));
5712 end Freeze_Called_Function
;
5714 -- Start of processing for Expand_Protected_Subprogram_Call
5717 -- If the protected object is not an enclosing scope, this is an inter-
5718 -- object function call. Inter-object procedure calls are expanded by
5719 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
5720 -- subprogram being called is in the protected body being compiled, and
5721 -- if the protected object in the call is statically the enclosing type.
5722 -- The object may be an component of some other data structure, in which
5723 -- case this must be handled as an inter-object call.
5725 if not In_Open_Scopes
(Scop
)
5726 or else not Is_Entity_Name
(Name
(N
))
5728 if Nkind
(Name
(N
)) = N_Selected_Component
then
5729 Rec
:= Prefix
(Name
(N
));
5732 pragma Assert
(Nkind
(Name
(N
)) = N_Indexed_Component
);
5733 Rec
:= Prefix
(Prefix
(Name
(N
)));
5736 Freeze_Called_Function
;
5737 Build_Protected_Subprogram_Call
(N
,
5738 Name
=> New_Occurrence_Of
(Subp
, Sloc
(N
)),
5739 Rec
=> Convert_Concurrent
(Rec
, Etype
(Rec
)),
5743 Rec
:= Expand_Protected_Object_Reference
(N
, Scop
);
5749 Freeze_Called_Function
;
5750 Build_Protected_Subprogram_Call
(N
,
5757 -- Analyze and resolve the new call. The actuals have already been
5758 -- resolved, but expansion of a function call will add extra actuals
5759 -- if needed. Analysis of a procedure call already includes resolution.
5763 if Ekind
(Subp
) = E_Function
then
5764 Resolve
(N
, Etype
(Subp
));
5766 end Expand_Protected_Subprogram_Call
;
5768 --------------------------------------------
5769 -- Has_Unconstrained_Access_Discriminants --
5770 --------------------------------------------
5772 function Has_Unconstrained_Access_Discriminants
5773 (Subtyp
: Entity_Id
) return Boolean
5778 if Has_Discriminants
(Subtyp
)
5779 and then not Is_Constrained
(Subtyp
)
5781 Discr
:= First_Discriminant
(Subtyp
);
5782 while Present
(Discr
) loop
5783 if Ekind
(Etype
(Discr
)) = E_Anonymous_Access_Type
then
5787 Next_Discriminant
(Discr
);
5792 end Has_Unconstrained_Access_Discriminants
;
5794 -----------------------------------
5795 -- Expand_Simple_Function_Return --
5796 -----------------------------------
5798 -- The "simple" comes from the syntax rule simple_return_statement. The
5799 -- semantics are not at all simple.
5801 procedure Expand_Simple_Function_Return
(N
: Node_Id
) is
5802 Loc
: constant Source_Ptr
:= Sloc
(N
);
5804 Scope_Id
: constant Entity_Id
:=
5805 Return_Applies_To
(Return_Statement_Entity
(N
));
5806 -- The function we are returning from
5808 R_Type
: constant Entity_Id
:= Etype
(Scope_Id
);
5809 -- The result type of the function
5811 Utyp
: constant Entity_Id
:= Underlying_Type
(R_Type
);
5813 Exp
: constant Node_Id
:= Expression
(N
);
5814 pragma Assert
(Present
(Exp
));
5816 Exptyp
: constant Entity_Id
:= Etype
(Exp
);
5817 -- The type of the expression (not necessarily the same as R_Type)
5819 Subtype_Ind
: Node_Id
;
5820 -- If the result type of the function is class-wide and the expression
5821 -- has a specific type, then we use the expression's type as the type of
5822 -- the return object. In cases where the expression is an aggregate that
5823 -- is built in place, this avoids the need for an expensive conversion
5824 -- of the return object to the specific type on assignments to the
5825 -- individual components.
5828 if Is_Class_Wide_Type
(R_Type
)
5829 and then not Is_Class_Wide_Type
(Etype
(Exp
))
5831 Subtype_Ind
:= New_Occurrence_Of
(Etype
(Exp
), Loc
);
5833 Subtype_Ind
:= New_Occurrence_Of
(R_Type
, Loc
);
5836 -- For the case of a simple return that does not come from an extended
5837 -- return, in the case of Ada 2005 where we are returning a limited
5838 -- type, we rewrite "return <expression>;" to be:
5840 -- return _anon_ : <return_subtype> := <expression>
5842 -- The expansion produced by Expand_N_Extended_Return_Statement will
5843 -- contain simple return statements (for example, a block containing
5844 -- simple return of the return object), which brings us back here with
5845 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
5846 -- checking for a simple return that does not come from an extended
5847 -- return is to avoid this infinite recursion.
5849 -- The reason for this design is that for Ada 2005 limited returns, we
5850 -- need to reify the return object, so we can build it "in place", and
5851 -- we need a block statement to hang finalization and tasking stuff.
5853 -- ??? In order to avoid disruption, we avoid translating to extended
5854 -- return except in the cases where we really need to (Ada 2005 for
5855 -- inherently limited). We might prefer to do this translation in all
5856 -- cases (except perhaps for the case of Ada 95 inherently limited),
5857 -- in order to fully exercise the Expand_N_Extended_Return_Statement
5858 -- code. This would also allow us to do the build-in-place optimization
5859 -- for efficiency even in cases where it is semantically not required.
5861 -- As before, we check the type of the return expression rather than the
5862 -- return type of the function, because the latter may be a limited
5863 -- class-wide interface type, which is not a limited type, even though
5864 -- the type of the expression may be.
5866 if not Comes_From_Extended_Return_Statement
(N
)
5867 and then Is_Limited_View
(Etype
(Expression
(N
)))
5868 and then Ada_Version
>= Ada_2005
5869 and then not Debug_Flag_Dot_L
5871 -- The functionality of interface thunks is simple and it is always
5872 -- handled by means of simple return statements. This leaves their
5873 -- expansion simple and clean.
5875 and then not Is_Thunk
(Current_Scope
)
5878 Return_Object_Entity
: constant Entity_Id
:=
5879 Make_Temporary
(Loc
, 'R', Exp
);
5881 Obj_Decl
: constant Node_Id
:=
5882 Make_Object_Declaration
(Loc
,
5883 Defining_Identifier
=> Return_Object_Entity
,
5884 Object_Definition
=> Subtype_Ind
,
5887 Ext
: constant Node_Id
:=
5888 Make_Extended_Return_Statement
(Loc
,
5889 Return_Object_Declarations
=> New_List
(Obj_Decl
));
5890 -- Do not perform this high-level optimization if the result type
5891 -- is an interface because the "this" pointer must be displaced.
5900 -- Here we have a simple return statement that is part of the expansion
5901 -- of an extended return statement (either written by the user, or
5902 -- generated by the above code).
5904 -- Always normalize C/Fortran boolean result. This is not always needed,
5905 -- but it seems a good idea to minimize the passing around of non-
5906 -- normalized values, and in any case this handles the processing of
5907 -- barrier functions for protected types, which turn the condition into
5908 -- a return statement.
5910 if Is_Boolean_Type
(Exptyp
)
5911 and then Nonzero_Is_True
(Exptyp
)
5913 Adjust_Condition
(Exp
);
5914 Adjust_Result_Type
(Exp
, Exptyp
);
5917 -- Do validity check if enabled for returns
5919 if Validity_Checks_On
5920 and then Validity_Check_Returns
5925 -- Check the result expression of a scalar function against the subtype
5926 -- of the function by inserting a conversion. This conversion must
5927 -- eventually be performed for other classes of types, but for now it's
5928 -- only done for scalars.
5931 if Is_Scalar_Type
(Exptyp
) then
5932 Rewrite
(Exp
, Convert_To
(R_Type
, Exp
));
5934 -- The expression is resolved to ensure that the conversion gets
5935 -- expanded to generate a possible constraint check.
5937 Analyze_And_Resolve
(Exp
, R_Type
);
5940 -- Deal with returning variable length objects and controlled types
5942 -- Nothing to do if we are returning by reference, or this is not a
5943 -- type that requires special processing (indicated by the fact that
5944 -- it requires a cleanup scope for the secondary stack case).
5946 if Is_Limited_View
(Exptyp
)
5947 or else Is_Limited_Interface
(Exptyp
)
5951 -- No copy needed for thunks returning interface type objects since
5952 -- the object is returned by reference and the maximum functionality
5953 -- required is just to displace the pointer.
5955 elsif Is_Thunk
(Current_Scope
) and then Is_Interface
(Exptyp
) then
5958 -- If the call is within a thunk and the type is a limited view, the
5959 -- backend will eventually see the non-limited view of the type.
5961 elsif Is_Thunk
(Current_Scope
) and then Is_Incomplete_Type
(Exptyp
) then
5964 elsif not Requires_Transient_Scope
(R_Type
) then
5966 -- Mutable records with no variable length components are not
5967 -- returned on the sec-stack, so we need to make sure that the
5968 -- backend will only copy back the size of the actual value, and not
5969 -- the maximum size. We create an actual subtype for this purpose.
5972 Ubt
: constant Entity_Id
:= Underlying_Type
(Base_Type
(Exptyp
));
5976 if Has_Discriminants
(Ubt
)
5977 and then not Is_Constrained
(Ubt
)
5978 and then not Has_Unchecked_Union
(Ubt
)
5980 Decl
:= Build_Actual_Subtype
(Ubt
, Exp
);
5981 Ent
:= Defining_Identifier
(Decl
);
5982 Insert_Action
(Exp
, Decl
);
5983 Rewrite
(Exp
, Unchecked_Convert_To
(Ent
, Exp
));
5984 Analyze_And_Resolve
(Exp
);
5988 -- Here if secondary stack is used
5991 -- Prevent the reclamation of the secondary stack by all enclosing
5992 -- blocks and loops as well as the related function, otherwise the
5993 -- result will be reclaimed too early or even clobbered. Due to a
5994 -- possible mix of internally generated blocks, source blocks and
5995 -- loops, the scope stack may not be contiguous as all labels are
5996 -- inserted at the top level within the related function. Instead,
5997 -- perform a parent-based traversal and mark all appropriate
6005 while Present
(P
) loop
6007 -- Mark the label of a source or internally generated block or
6010 if Nkind_In
(P
, N_Block_Statement
, N_Loop_Statement
) then
6011 Set_Sec_Stack_Needed_For_Return
(Entity
(Identifier
(P
)));
6013 -- Mark the enclosing function
6015 elsif Nkind
(P
) = N_Subprogram_Body
then
6016 if Present
(Corresponding_Spec
(P
)) then
6017 Set_Sec_Stack_Needed_For_Return
(Corresponding_Spec
(P
));
6019 Set_Sec_Stack_Needed_For_Return
(Defining_Entity
(P
));
6022 -- Do not go beyond the enclosing function
6031 -- Optimize the case where the result is a function call. In this
6032 -- case either the result is already on the secondary stack, or is
6033 -- already being returned with the stack pointer depressed and no
6034 -- further processing is required except to set the By_Ref flag
6035 -- to ensure that gigi does not attempt an extra unnecessary copy.
6036 -- (actually not just unnecessary but harmfully wrong in the case
6037 -- of a controlled type, where gigi does not know how to do a copy).
6038 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
6039 -- for array types if the constrained status of the target type is
6040 -- different from that of the expression.
6042 if Requires_Transient_Scope
(Exptyp
)
6044 (not Is_Array_Type
(Exptyp
)
6045 or else Is_Constrained
(Exptyp
) = Is_Constrained
(R_Type
)
6046 or else CW_Or_Has_Controlled_Part
(Utyp
))
6047 and then Nkind
(Exp
) = N_Function_Call
6051 -- Remove side effects from the expression now so that other parts
6052 -- of the expander do not have to reanalyze this node without this
6055 Rewrite
(Exp
, Duplicate_Subexpr_No_Checks
(Exp
));
6057 -- For controlled types, do the allocation on the secondary stack
6058 -- manually in order to call adjust at the right time:
6060 -- type Anon1 is access R_Type;
6061 -- for Anon1'Storage_pool use ss_pool;
6062 -- Anon2 : anon1 := new R_Type'(expr);
6063 -- return Anon2.all;
6065 -- We do the same for classwide types that are not potentially
6066 -- controlled (by the virtue of restriction No_Finalization) because
6067 -- gigi is not able to properly allocate class-wide types.
6069 elsif CW_Or_Has_Controlled_Part
(Utyp
) then
6071 Loc
: constant Source_Ptr
:= Sloc
(N
);
6072 Acc_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
6073 Alloc_Node
: Node_Id
;
6077 Set_Ekind
(Acc_Typ
, E_Access_Type
);
6079 Set_Associated_Storage_Pool
(Acc_Typ
, RTE
(RE_SS_Pool
));
6081 -- This is an allocator for the secondary stack, and it's fine
6082 -- to have Comes_From_Source set False on it, as gigi knows not
6083 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6086 Make_Allocator
(Loc
,
6088 Make_Qualified_Expression
(Loc
,
6089 Subtype_Mark
=> New_Occurrence_Of
(Etype
(Exp
), Loc
),
6090 Expression
=> Relocate_Node
(Exp
)));
6092 -- We do not want discriminant checks on the declaration,
6093 -- given that it gets its value from the allocator.
6095 Set_No_Initialization
(Alloc_Node
);
6097 Temp
:= Make_Temporary
(Loc
, 'R', Alloc_Node
);
6099 Insert_List_Before_And_Analyze
(N
, New_List
(
6100 Make_Full_Type_Declaration
(Loc
,
6101 Defining_Identifier
=> Acc_Typ
,
6103 Make_Access_To_Object_Definition
(Loc
,
6104 Subtype_Indication
=> Subtype_Ind
)),
6106 Make_Object_Declaration
(Loc
,
6107 Defining_Identifier
=> Temp
,
6108 Object_Definition
=> New_Occurrence_Of
(Acc_Typ
, Loc
),
6109 Expression
=> Alloc_Node
)));
6112 Make_Explicit_Dereference
(Loc
,
6113 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
6115 -- Ada 2005 (AI-251): If the type of the returned object is
6116 -- an interface then add an implicit type conversion to force
6117 -- displacement of the "this" pointer.
6119 if Is_Interface
(R_Type
) then
6120 Rewrite
(Exp
, Convert_To
(R_Type
, Relocate_Node
(Exp
)));
6123 Analyze_And_Resolve
(Exp
, R_Type
);
6126 -- Otherwise use the gigi mechanism to allocate result on the
6130 Check_Restriction
(No_Secondary_Stack
, N
);
6131 Set_Storage_Pool
(N
, RTE
(RE_SS_Pool
));
6133 -- If we are generating code for the VM do not use
6134 -- SS_Allocate since everything is heap-allocated anyway.
6136 if VM_Target
= No_VM
then
6137 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
6142 -- Implement the rules of 6.5(8-10), which require a tag check in
6143 -- the case of a limited tagged return type, and tag reassignment for
6144 -- nonlimited tagged results. These actions are needed when the return
6145 -- type is a specific tagged type and the result expression is a
6146 -- conversion or a formal parameter, because in that case the tag of
6147 -- the expression might differ from the tag of the specific result type.
6149 if Is_Tagged_Type
(Utyp
)
6150 and then not Is_Class_Wide_Type
(Utyp
)
6151 and then (Nkind_In
(Exp
, N_Type_Conversion
,
6152 N_Unchecked_Type_Conversion
)
6153 or else (Is_Entity_Name
(Exp
)
6154 and then Ekind
(Entity
(Exp
)) in Formal_Kind
))
6156 -- When the return type is limited, perform a check that the tag of
6157 -- the result is the same as the tag of the return type.
6159 if Is_Limited_Type
(R_Type
) then
6161 Make_Raise_Constraint_Error
(Loc
,
6165 Make_Selected_Component
(Loc
,
6166 Prefix
=> Duplicate_Subexpr
(Exp
),
6167 Selector_Name
=> Make_Identifier
(Loc
, Name_uTag
)),
6169 Make_Attribute_Reference
(Loc
,
6171 New_Occurrence_Of
(Base_Type
(Utyp
), Loc
),
6172 Attribute_Name
=> Name_Tag
)),
6173 Reason
=> CE_Tag_Check_Failed
));
6175 -- If the result type is a specific nonlimited tagged type, then we
6176 -- have to ensure that the tag of the result is that of the result
6177 -- type. This is handled by making a copy of the expression in
6178 -- the case where it might have a different tag, namely when the
6179 -- expression is a conversion or a formal parameter. We create a new
6180 -- object of the result type and initialize it from the expression,
6181 -- which will implicitly force the tag to be set appropriately.
6185 ExpR
: constant Node_Id
:= Relocate_Node
(Exp
);
6186 Result_Id
: constant Entity_Id
:=
6187 Make_Temporary
(Loc
, 'R', ExpR
);
6188 Result_Exp
: constant Node_Id
:=
6189 New_Occurrence_Of
(Result_Id
, Loc
);
6190 Result_Obj
: constant Node_Id
:=
6191 Make_Object_Declaration
(Loc
,
6192 Defining_Identifier
=> Result_Id
,
6193 Object_Definition
=>
6194 New_Occurrence_Of
(R_Type
, Loc
),
6195 Constant_Present
=> True,
6196 Expression
=> ExpR
);
6199 Set_Assignment_OK
(Result_Obj
);
6200 Insert_Action
(Exp
, Result_Obj
);
6202 Rewrite
(Exp
, Result_Exp
);
6203 Analyze_And_Resolve
(Exp
, R_Type
);
6207 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6208 -- a check that the level of the return expression's underlying type
6209 -- is not deeper than the level of the master enclosing the function.
6210 -- Always generate the check when the type of the return expression
6211 -- is class-wide, when it's a type conversion, or when it's a formal
6212 -- parameter. Otherwise, suppress the check in the case where the
6213 -- return expression has a specific type whose level is known not to
6214 -- be statically deeper than the function's result type.
6216 -- No runtime check needed in interface thunks since it is performed
6217 -- by the target primitive associated with the thunk.
6219 -- Note: accessibility check is skipped in the VM case, since there
6220 -- does not seem to be any practical way to implement this check.
6222 elsif Ada_Version
>= Ada_2005
6223 and then Tagged_Type_Expansion
6224 and then Is_Class_Wide_Type
(R_Type
)
6225 and then not Is_Thunk
(Current_Scope
)
6226 and then not Scope_Suppress
.Suppress
(Accessibility_Check
)
6228 (Is_Class_Wide_Type
(Etype
(Exp
))
6229 or else Nkind_In
(Exp
, N_Type_Conversion
,
6230 N_Unchecked_Type_Conversion
)
6231 or else (Is_Entity_Name
(Exp
)
6232 and then Ekind
(Entity
(Exp
)) in Formal_Kind
)
6233 or else Scope_Depth
(Enclosing_Dynamic_Scope
(Etype
(Exp
))) >
6234 Scope_Depth
(Enclosing_Dynamic_Scope
(Scope_Id
)))
6240 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6241 -- "this" to reference the base of the object. This is required to
6242 -- get access to the TSD of the object.
6244 if Is_Class_Wide_Type
(Etype
(Exp
))
6245 and then Is_Interface
(Etype
(Exp
))
6246 and then Nkind
(Exp
) = N_Explicit_Dereference
6249 Make_Explicit_Dereference
(Loc
,
6251 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
6252 Make_Function_Call
(Loc
,
6254 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
6255 Parameter_Associations
=> New_List
(
6256 Unchecked_Convert_To
(RTE
(RE_Address
),
6257 Duplicate_Subexpr
(Prefix
(Exp
)))))));
6260 Make_Attribute_Reference
(Loc
,
6261 Prefix
=> Duplicate_Subexpr
(Exp
),
6262 Attribute_Name
=> Name_Tag
);
6266 Make_Raise_Program_Error
(Loc
,
6269 Left_Opnd
=> Build_Get_Access_Level
(Loc
, Tag_Node
),
6271 Make_Integer_Literal
(Loc
,
6272 Scope_Depth
(Enclosing_Dynamic_Scope
(Scope_Id
)))),
6273 Reason
=> PE_Accessibility_Check_Failed
));
6276 -- AI05-0073: If function has a controlling access result, check that
6277 -- the tag of the return value, if it is not null, matches designated
6278 -- type of return type.
6280 -- The return expression is referenced twice in the code below, so it
6281 -- must be made free of side effects. Given that different compilers
6282 -- may evaluate these parameters in different order, both occurrences
6285 elsif Ekind
(R_Type
) = E_Anonymous_Access_Type
6286 and then Has_Controlling_Result
(Scope_Id
)
6289 Make_Raise_Constraint_Error
(Loc
,
6294 Left_Opnd
=> Duplicate_Subexpr
(Exp
),
6295 Right_Opnd
=> Make_Null
(Loc
)),
6297 Right_Opnd
=> Make_Op_Ne
(Loc
,
6299 Make_Selected_Component
(Loc
,
6300 Prefix
=> Duplicate_Subexpr
(Exp
),
6301 Selector_Name
=> Make_Identifier
(Loc
, Name_uTag
)),
6304 Make_Attribute_Reference
(Loc
,
6306 New_Occurrence_Of
(Designated_Type
(R_Type
), Loc
),
6307 Attribute_Name
=> Name_Tag
))),
6309 Reason
=> CE_Tag_Check_Failed
),
6310 Suppress
=> All_Checks
);
6313 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
6314 -- ensure that the function result does not outlive an
6315 -- object designated by one of it discriminants.
6317 if Present
(Extra_Accessibility_Of_Result
(Scope_Id
))
6318 and then Has_Unconstrained_Access_Discriminants
(R_Type
)
6321 Discrim_Source
: Node_Id
;
6323 procedure Check_Against_Result_Level
(Level
: Node_Id
);
6324 -- Check the given accessibility level against the level
6325 -- determined by the point of call. (AI05-0234).
6327 --------------------------------
6328 -- Check_Against_Result_Level --
6329 --------------------------------
6331 procedure Check_Against_Result_Level
(Level
: Node_Id
) is
6334 Make_Raise_Program_Error
(Loc
,
6340 (Extra_Accessibility_Of_Result
(Scope_Id
), Loc
)),
6341 Reason
=> PE_Accessibility_Check_Failed
));
6342 end Check_Against_Result_Level
;
6345 Discrim_Source
:= Exp
;
6346 while Nkind
(Discrim_Source
) = N_Qualified_Expression
loop
6347 Discrim_Source
:= Expression
(Discrim_Source
);
6350 if Nkind
(Discrim_Source
) = N_Identifier
6351 and then Is_Return_Object
(Entity
(Discrim_Source
))
6353 Discrim_Source
:= Entity
(Discrim_Source
);
6355 if Is_Constrained
(Etype
(Discrim_Source
)) then
6356 Discrim_Source
:= Etype
(Discrim_Source
);
6358 Discrim_Source
:= Expression
(Parent
(Discrim_Source
));
6361 elsif Nkind
(Discrim_Source
) = N_Identifier
6362 and then Nkind_In
(Original_Node
(Discrim_Source
),
6363 N_Aggregate
, N_Extension_Aggregate
)
6365 Discrim_Source
:= Original_Node
(Discrim_Source
);
6367 elsif Nkind
(Discrim_Source
) = N_Explicit_Dereference
and then
6368 Nkind
(Original_Node
(Discrim_Source
)) = N_Function_Call
6370 Discrim_Source
:= Original_Node
(Discrim_Source
);
6373 while Nkind_In
(Discrim_Source
, N_Qualified_Expression
,
6375 N_Unchecked_Type_Conversion
)
6377 Discrim_Source
:= Expression
(Discrim_Source
);
6380 case Nkind
(Discrim_Source
) is
6381 when N_Defining_Identifier
=>
6383 pragma Assert
(Is_Composite_Type
(Discrim_Source
)
6384 and then Has_Discriminants
(Discrim_Source
)
6385 and then Is_Constrained
(Discrim_Source
));
6388 Discrim
: Entity_Id
:=
6389 First_Discriminant
(Base_Type
(R_Type
));
6390 Disc_Elmt
: Elmt_Id
:=
6391 First_Elmt
(Discriminant_Constraint
6395 if Ekind
(Etype
(Discrim
)) =
6396 E_Anonymous_Access_Type
6398 Check_Against_Result_Level
6399 (Dynamic_Accessibility_Level
(Node
(Disc_Elmt
)));
6402 Next_Elmt
(Disc_Elmt
);
6403 Next_Discriminant
(Discrim
);
6404 exit when not Present
(Discrim
);
6408 when N_Aggregate | N_Extension_Aggregate
=>
6410 -- Unimplemented: extension aggregate case where discrims
6411 -- come from ancestor part, not extension part.
6414 Discrim
: Entity_Id
:=
6415 First_Discriminant
(Base_Type
(R_Type
));
6417 Disc_Exp
: Node_Id
:= Empty
;
6419 Positionals_Exhausted
6420 : Boolean := not Present
(Expressions
6423 function Associated_Expr
6424 (Comp_Id
: Entity_Id
;
6425 Associations
: List_Id
) return Node_Id
;
6427 -- Given a component and a component associations list,
6428 -- locate the expression for that component; returns
6429 -- Empty if no such expression is found.
6431 ---------------------
6432 -- Associated_Expr --
6433 ---------------------
6435 function Associated_Expr
6436 (Comp_Id
: Entity_Id
;
6437 Associations
: List_Id
) return Node_Id
6443 -- Simple linear search seems ok here
6445 Assoc
:= First
(Associations
);
6446 while Present
(Assoc
) loop
6447 Choice
:= First
(Choices
(Assoc
));
6448 while Present
(Choice
) loop
6449 if (Nkind
(Choice
) = N_Identifier
6450 and then Chars
(Choice
) = Chars
(Comp_Id
))
6451 or else (Nkind
(Choice
) = N_Others_Choice
)
6453 return Expression
(Assoc
);
6463 end Associated_Expr
;
6465 -- Start of processing for Expand_Simple_Function_Return
6468 if not Positionals_Exhausted
then
6469 Disc_Exp
:= First
(Expressions
(Discrim_Source
));
6473 if Positionals_Exhausted
then
6477 Component_Associations
(Discrim_Source
));
6480 if Ekind
(Etype
(Discrim
)) =
6481 E_Anonymous_Access_Type
6483 Check_Against_Result_Level
6484 (Dynamic_Accessibility_Level
(Disc_Exp
));
6487 Next_Discriminant
(Discrim
);
6488 exit when not Present
(Discrim
);
6490 if not Positionals_Exhausted
then
6492 Positionals_Exhausted
:= not Present
(Disc_Exp
);
6497 when N_Function_Call
=>
6499 -- No check needed (check performed by callee)
6506 Level
: constant Node_Id
:=
6507 Make_Integer_Literal
(Loc
,
6508 Object_Access_Level
(Discrim_Source
));
6511 -- Unimplemented: check for name prefix that includes
6512 -- a dereference of an access value with a dynamic
6513 -- accessibility level (e.g., an access param or a
6514 -- saooaaat) and use dynamic level in that case. For
6516 -- return Access_Param.all(Some_Index).Some_Component;
6519 Set_Etype
(Level
, Standard_Natural
);
6520 Check_Against_Result_Level
(Level
);
6527 -- If we are returning an object that may not be bit-aligned, then copy
6528 -- the value into a temporary first. This copy may need to expand to a
6529 -- loop of component operations.
6531 if Is_Possibly_Unaligned_Slice
(Exp
)
6532 or else Is_Possibly_Unaligned_Object
(Exp
)
6535 ExpR
: constant Node_Id
:= Relocate_Node
(Exp
);
6536 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', ExpR
);
6539 Make_Object_Declaration
(Loc
,
6540 Defining_Identifier
=> Tnn
,
6541 Constant_Present
=> True,
6542 Object_Definition
=> New_Occurrence_Of
(R_Type
, Loc
),
6543 Expression
=> ExpR
),
6544 Suppress
=> All_Checks
);
6545 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
6549 -- Call the _Postconditions procedure if the related function has
6550 -- contract assertions that need to be verified on exit.
6552 if Ekind
(Scope_Id
) = E_Function
6553 and then Present
(Postconditions_Proc
(Scope_Id
))
6555 -- We are going to reference the returned value twice in this case,
6556 -- once in the call to _Postconditions, and once in the actual return
6557 -- statement, but we can't have side effects happening twice, and in
6558 -- any case for efficiency we don't want to do the computation twice.
6560 -- If the returned expression is an entity name, we don't need to
6561 -- worry since it is efficient and safe to reference it twice, that's
6562 -- also true for literals other than string literals, and for the
6563 -- case of X.all where X is an entity name.
6565 if Is_Entity_Name
(Exp
)
6566 or else Nkind_In
(Exp
, N_Character_Literal
,
6569 or else (Nkind
(Exp
) = N_Explicit_Dereference
6570 and then Is_Entity_Name
(Prefix
(Exp
)))
6574 -- Otherwise we are going to need a temporary to capture the value
6578 ExpR
: Node_Id
:= Relocate_Node
(Exp
);
6579 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', ExpR
);
6582 -- In the case of discriminated objects, we have created a
6583 -- constrained subtype above, and used the underlying type.
6584 -- This transformation is post-analysis and harmless, except
6585 -- that now the call to the post-condition will be analyzed and
6586 -- type kinds have to match.
6588 if Nkind
(ExpR
) = N_Unchecked_Type_Conversion
6590 Is_Private_Type
(R_Type
) /= Is_Private_Type
(Etype
(ExpR
))
6592 ExpR
:= Expression
(ExpR
);
6595 -- For a complex expression of an elementary type, capture
6596 -- value in the temporary and use it as the reference.
6598 if Is_Elementary_Type
(R_Type
) then
6600 Make_Object_Declaration
(Loc
,
6601 Defining_Identifier
=> Tnn
,
6602 Constant_Present
=> True,
6603 Object_Definition
=> New_Occurrence_Of
(R_Type
, Loc
),
6604 Expression
=> ExpR
),
6605 Suppress
=> All_Checks
);
6607 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
6609 -- If we have something we can rename, generate a renaming of
6610 -- the object and replace the expression with a reference
6612 elsif Is_Object_Reference
(Exp
) then
6614 Make_Object_Renaming_Declaration
(Loc
,
6615 Defining_Identifier
=> Tnn
,
6616 Subtype_Mark
=> New_Occurrence_Of
(R_Type
, Loc
),
6618 Suppress
=> All_Checks
);
6620 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
6622 -- Otherwise we have something like a string literal or an
6623 -- aggregate. We could copy the value, but that would be
6624 -- inefficient. Instead we make a reference to the value and
6625 -- capture this reference with a renaming, the expression is
6626 -- then replaced by a dereference of this renaming.
6629 -- For now, copy the value, since the code below does not
6630 -- seem to work correctly ???
6633 Make_Object_Declaration
(Loc
,
6634 Defining_Identifier
=> Tnn
,
6635 Constant_Present
=> True,
6636 Object_Definition
=> New_Occurrence_Of
(R_Type
, Loc
),
6637 Expression
=> Relocate_Node
(Exp
)),
6638 Suppress
=> All_Checks
);
6640 Rewrite
(Exp
, New_Occurrence_Of
(Tnn
, Loc
));
6642 -- Insert_Action (Exp,
6643 -- Make_Object_Renaming_Declaration (Loc,
6644 -- Defining_Identifier => Tnn,
6645 -- Access_Definition =>
6646 -- Make_Access_Definition (Loc,
6647 -- All_Present => True,
6648 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
6650 -- Make_Reference (Loc,
6651 -- Prefix => Relocate_Node (Exp))),
6652 -- Suppress => All_Checks);
6655 -- Make_Explicit_Dereference (Loc,
6656 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
6661 -- Generate call to _Postconditions
6664 Make_Procedure_Call_Statement
(Loc
,
6666 New_Occurrence_Of
(Postconditions_Proc
(Scope_Id
), Loc
),
6667 Parameter_Associations
=> New_List
(Duplicate_Subexpr
(Exp
))));
6670 -- Ada 2005 (AI-251): If this return statement corresponds with an
6671 -- simple return statement associated with an extended return statement
6672 -- and the type of the returned object is an interface then generate an
6673 -- implicit conversion to force displacement of the "this" pointer.
6675 if Ada_Version
>= Ada_2005
6676 and then Comes_From_Extended_Return_Statement
(N
)
6677 and then Nkind
(Expression
(N
)) = N_Identifier
6678 and then Is_Interface
(Utyp
)
6679 and then Utyp
/= Underlying_Type
(Exptyp
)
6681 Rewrite
(Exp
, Convert_To
(Utyp
, Relocate_Node
(Exp
)));
6682 Analyze_And_Resolve
(Exp
);
6684 end Expand_Simple_Function_Return
;
6686 --------------------------------
6687 -- Expand_Subprogram_Contract --
6688 --------------------------------
6690 procedure Expand_Subprogram_Contract
(N
: Node_Id
) is
6691 Body_Id
: constant Entity_Id
:= Defining_Entity
(N
);
6692 Spec_Id
: constant Entity_Id
:= Corresponding_Spec
(N
);
6694 procedure Add_Invariant_And_Predicate_Checks
6695 (Subp_Id
: Entity_Id
;
6696 Stmts
: in out List_Id
;
6697 Result
: out Node_Id
);
6698 -- Process the result of function Subp_Id (if applicable) and all its
6699 -- formals. Add invariant and predicate checks where applicable. The
6700 -- routine appends all the checks to list Stmts. If Subp_Id denotes a
6701 -- function, Result contains the entity of parameter _Result, to be
6702 -- used in the creation of procedure _Postconditions.
6704 procedure Append_Enabled_Item
(Item
: Node_Id
; List
: in out List_Id
);
6705 -- Append a node to a list. If there is no list, create a new one. When
6706 -- the item denotes a pragma, it is added to the list only when it is
6709 procedure Build_Postconditions_Procedure
6710 (Subp_Id
: Entity_Id
;
6712 Result
: Entity_Id
);
6713 -- Create the body of procedure _Postconditions which handles various
6714 -- assertion actions on exit from subprogram Subp_Id. Stmts is the list
6715 -- of statements to be checked on exit. Parameter Result is the entity
6716 -- of parameter _Result when Subp_Id denotes a function.
6718 function Build_Pragma_Check_Equivalent
6720 Subp_Id
: Entity_Id
:= Empty
;
6721 Inher_Id
: Entity_Id
:= Empty
) return Node_Id
;
6722 -- Transform a [refined] pre- or postcondition denoted by Prag into an
6723 -- equivalent pragma Check. When the pre- or postcondition is inherited,
6724 -- the routine corrects the references of all formals of Inher_Id to
6725 -- point to the formals of Subp_Id.
6727 procedure Process_Contract_Cases
(Stmts
: in out List_Id
);
6728 -- Process pragma Contract_Cases. This routine prepends items to the
6729 -- body declarations and appends items to list Stmts.
6731 procedure Process_Postconditions
(Stmts
: in out List_Id
);
6732 -- Collect all [inherited] spec and body postconditions and accumulate
6733 -- their pragma Check equivalents in list Stmts.
6735 procedure Process_Preconditions
;
6736 -- Collect all [inherited] spec and body preconditions and prepend their
6737 -- pragma Check equivalents to the declarations of the body.
6739 ----------------------------------------
6740 -- Add_Invariant_And_Predicate_Checks --
6741 ----------------------------------------
6743 procedure Add_Invariant_And_Predicate_Checks
6744 (Subp_Id
: Entity_Id
;
6745 Stmts
: in out List_Id
;
6746 Result
: out Node_Id
)
6748 procedure Add_Invariant_Access_Checks
(Id
: Entity_Id
);
6749 -- Id denotes the return value of a function or a formal parameter.
6750 -- Add an invariant check if the type of Id is access to a type with
6751 -- invariants. The routine appends the generated code to Stmts.
6753 function Invariant_Checks_OK
(Typ
: Entity_Id
) return Boolean;
6754 -- Determine whether type Typ can benefit from invariant checks. To
6755 -- qualify, the type must have a non-null invariant procedure and
6756 -- subprogram Subp_Id must appear visible from the point of view of
6759 ---------------------------------
6760 -- Add_Invariant_Access_Checks --
6761 ---------------------------------
6763 procedure Add_Invariant_Access_Checks
(Id
: Entity_Id
) is
6764 Loc
: constant Source_Ptr
:= Sloc
(N
);
6771 if Is_Access_Type
(Typ
) and then not Is_Access_Constant
(Typ
) then
6772 Typ
:= Designated_Type
(Typ
);
6774 if Invariant_Checks_OK
(Typ
) then
6776 Make_Explicit_Dereference
(Loc
,
6777 Prefix
=> New_Occurrence_Of
(Id
, Loc
));
6778 Set_Etype
(Ref
, Typ
);
6781 -- if <Id> /= null then
6782 -- <invariant_call (<Ref>)>
6787 Make_If_Statement
(Loc
,
6790 Left_Opnd
=> New_Occurrence_Of
(Id
, Loc
),
6791 Right_Opnd
=> Make_Null
(Loc
)),
6792 Then_Statements
=> New_List
(
6793 Make_Invariant_Call
(Ref
))),
6797 end Add_Invariant_Access_Checks
;
6799 -------------------------
6800 -- Invariant_Checks_OK --
6801 -------------------------
6803 function Invariant_Checks_OK
(Typ
: Entity_Id
) return Boolean is
6804 function Has_Null_Body
(Proc_Id
: Entity_Id
) return Boolean;
6805 -- Determine whether the body of procedure Proc_Id contains a sole
6806 -- null statement, possibly followed by an optional return.
6808 function Has_Public_Visibility_Of_Subprogram
return Boolean;
6809 -- Determine whether type Typ has public visibility of subprogram
6816 function Has_Null_Body
(Proc_Id
: Entity_Id
) return Boolean is
6817 Body_Id
: Entity_Id
;
6824 Spec
:= Parent
(Proc_Id
);
6825 Decl
:= Parent
(Spec
);
6827 -- Retrieve the entity of the invariant procedure body
6829 if Nkind
(Spec
) = N_Procedure_Specification
6830 and then Nkind
(Decl
) = N_Subprogram_Declaration
6832 Body_Id
:= Corresponding_Body
(Decl
);
6834 -- The body acts as a spec
6840 -- The body will be generated later
6842 if No
(Body_Id
) then
6846 Spec
:= Parent
(Body_Id
);
6847 Decl
:= Parent
(Spec
);
6850 (Nkind
(Spec
) = N_Procedure_Specification
6851 and then Nkind
(Decl
) = N_Subprogram_Body
);
6853 Stmt1
:= First
(Statements
(Handled_Statement_Sequence
(Decl
)));
6855 -- Look for a null statement followed by an optional return
6858 if Nkind
(Stmt1
) = N_Null_Statement
then
6859 Stmt2
:= Next
(Stmt1
);
6861 if Present
(Stmt2
) then
6862 return Nkind
(Stmt2
) = N_Simple_Return_Statement
;
6871 -----------------------------------------
6872 -- Has_Public_Visibility_Of_Subprogram --
6873 -----------------------------------------
6875 function Has_Public_Visibility_Of_Subprogram
return Boolean is
6876 Subp_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Subp_Id
);
6879 -- An Initialization procedure must be considered visible even
6880 -- though it is internally generated.
6882 if Is_Init_Proc
(Defining_Entity
(Subp_Decl
)) then
6885 elsif Ekind
(Scope
(Typ
)) /= E_Package
then
6888 -- Internally generated code is never publicly visible except
6889 -- for a subprogram that is the implementation of an expression
6890 -- function. In that case the visibility is determined by the
6893 elsif not Comes_From_Source
(Subp_Decl
)
6895 (Nkind
(Original_Node
(Subp_Decl
)) /= N_Expression_Function
6897 Comes_From_Source
(Defining_Entity
(Subp_Decl
)))
6901 -- Determine whether the subprogram is declared in the visible
6902 -- declarations of the package containing the type.
6905 return List_Containing
(Subp_Decl
) =
6906 Visible_Declarations
6907 (Specification
(Unit_Declaration_Node
(Scope
(Typ
))));
6909 end Has_Public_Visibility_Of_Subprogram
;
6911 -- Start of processing for Invariant_Checks_OK
6915 Has_Invariants
(Typ
)
6916 and then Present
(Invariant_Procedure
(Typ
))
6917 and then not Has_Null_Body
(Invariant_Procedure
(Typ
))
6918 and then Has_Public_Visibility_Of_Subprogram
;
6919 end Invariant_Checks_OK
;
6923 Loc
: constant Source_Ptr
:= Sloc
(N
);
6924 -- Source location of subprogram contract
6929 -- Start of processing for Add_Invariant_And_Predicate_Checks
6934 -- Process the result of a function
6936 if Ekind
(Subp_Id
) = E_Function
then
6937 Typ
:= Etype
(Subp_Id
);
6939 -- Generate _Result which is used in procedure _Postconditions to
6940 -- verify the return value.
6942 Result
:= Make_Defining_Identifier
(Loc
, Name_uResult
);
6943 Set_Etype
(Result
, Typ
);
6945 -- Add an invariant check when the return type has invariants and
6946 -- the related function is visible to the outside.
6948 if Invariant_Checks_OK
(Typ
) then
6951 Make_Invariant_Call
(New_Occurrence_Of
(Result
, Loc
)),
6955 -- Add an invariant check when the return type is an access to a
6956 -- type with invariants.
6958 Add_Invariant_Access_Checks
(Result
);
6961 -- Add invariant and predicates for all formals that qualify
6963 Formal
:= First_Formal
(Subp_Id
);
6964 while Present
(Formal
) loop
6965 Typ
:= Etype
(Formal
);
6967 if Ekind
(Formal
) /= E_In_Parameter
6968 or else Is_Access_Type
(Typ
)
6970 if Invariant_Checks_OK
(Typ
) then
6973 Make_Invariant_Call
(New_Occurrence_Of
(Formal
, Loc
)),
6977 Add_Invariant_Access_Checks
(Formal
);
6979 -- Note: we used to add predicate checks for OUT and IN OUT
6980 -- formals here, but that was misguided, since such checks are
6981 -- performed on the caller side, based on the predicate of the
6982 -- actual, rather than the predicate of the formal.
6986 Next_Formal
(Formal
);
6988 end Add_Invariant_And_Predicate_Checks
;
6990 -------------------------
6991 -- Append_Enabled_Item --
6992 -------------------------
6994 procedure Append_Enabled_Item
(Item
: Node_Id
; List
: in out List_Id
) is
6996 -- Do not chain ignored or disabled pragmas
6998 if Nkind
(Item
) = N_Pragma
6999 and then (Is_Ignored
(Item
) or else Is_Disabled
(Item
))
7003 -- Otherwise, add the item
7010 -- If the pragma is a conjunct in a composite postcondition, it
7011 -- has been processed in reverse order. In the postcondition body
7012 -- if must appear before the others.
7014 if Nkind
(Item
) = N_Pragma
7015 and then From_Aspect_Specification
(Item
)
7016 and then Split_PPC
(Item
)
7018 Prepend
(Item
, List
);
7020 Append
(Item
, List
);
7023 end Append_Enabled_Item
;
7025 ------------------------------------
7026 -- Build_Postconditions_Procedure --
7027 ------------------------------------
7029 procedure Build_Postconditions_Procedure
7030 (Subp_Id
: Entity_Id
;
7034 procedure Insert_Before_First_Source_Declaration
(Stmt
: Node_Id
);
7035 -- Insert node Stmt before the first source declaration of the
7036 -- related subprogram's body. If no such declaration exists, Stmt
7037 -- becomes the last declaration.
7039 --------------------------------------------
7040 -- Insert_Before_First_Source_Declaration --
7041 --------------------------------------------
7043 procedure Insert_Before_First_Source_Declaration
(Stmt
: Node_Id
) is
7044 Decls
: constant List_Id
:= Declarations
(N
);
7048 -- Inspect the declarations of the related subprogram body looking
7049 -- for the first source declaration.
7051 if Present
(Decls
) then
7052 Decl
:= First
(Decls
);
7053 while Present
(Decl
) loop
7054 if Comes_From_Source
(Decl
) then
7055 Insert_Before
(Decl
, Stmt
);
7062 -- If we get there, then the subprogram body lacks any source
7063 -- declarations. The body of _Postconditions now acts as the
7064 -- last declaration.
7066 Append
(Stmt
, Decls
);
7068 -- Ensure that the body has a declaration list
7071 Set_Declarations
(N
, New_List
(Stmt
));
7073 end Insert_Before_First_Source_Declaration
;
7077 Loc
: constant Source_Ptr
:= Sloc
(N
);
7078 Params
: List_Id
:= No_List
;
7080 Proc_Id
: Entity_Id
;
7082 -- Start of processing for Build_Postconditions_Procedure
7085 -- Nothing to do if there are no actions to check on exit
7091 Proc_Id
:= Make_Defining_Identifier
(Loc
, Name_uPostconditions
);
7092 Set_Debug_Info_Needed
(Proc_Id
);
7093 Set_Postconditions_Proc
(Subp_Id
, Proc_Id
);
7095 -- The related subprogram is a function, create the specification of
7096 -- parameter _Result.
7098 if Present
(Result
) then
7099 Params
:= New_List
(
7100 Make_Parameter_Specification
(Loc
,
7101 Defining_Identifier
=> Result
,
7103 New_Occurrence_Of
(Etype
(Result
), Loc
)));
7106 -- Insert _Postconditions before the first source declaration of the
7107 -- body. This ensures that the body will not cause any premature
7108 -- freezing as it may mention types:
7110 -- procedure Proc (Obj : Array_Typ) is
7111 -- procedure _postconditions is
7114 -- end _postconditions;
7116 -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1));
7119 -- In the example above, Obj is of type T but the incorrect placement
7120 -- of _Postconditions will cause a crash in gigi due to an out of
7121 -- order reference. The body of _Postconditions must be placed after
7122 -- the declaration of Temp to preserve correct visibility.
7124 -- Set an explicit End_Lavel to override the sloc of the implicit
7125 -- RETURN statement, and prevent it from inheriting the sloc of one
7126 -- the postconditions: this would cause confusing debug into to be
7127 -- produced, interfering with coverage analysis tools.
7130 Make_Subprogram_Body
(Loc
,
7132 Make_Procedure_Specification
(Loc
,
7133 Defining_Unit_Name
=> Proc_Id
,
7134 Parameter_Specifications
=> Params
),
7136 Declarations
=> Empty_List
,
7137 Handled_Statement_Sequence
=>
7138 Make_Handled_Sequence_Of_Statements
(Loc
,
7139 Statements
=> Stmts
,
7140 End_Label
=> Make_Identifier
(Loc
, Chars
(Proc_Id
))));
7142 Insert_Before_First_Source_Declaration
(Proc_Bod
);
7144 end Build_Postconditions_Procedure
;
7146 -----------------------------------
7147 -- Build_Pragma_Check_Equivalent --
7148 -----------------------------------
7150 function Build_Pragma_Check_Equivalent
7152 Subp_Id
: Entity_Id
:= Empty
;
7153 Inher_Id
: Entity_Id
:= Empty
) return Node_Id
7155 Loc
: constant Source_Ptr
:= Sloc
(Prag
);
7156 Prag_Nam
: constant Name_Id
:= Pragma_Name
(Prag
);
7157 Check_Prag
: Node_Id
;
7158 Formals_Map
: Elist_Id
;
7159 Inher_Formal
: Entity_Id
;
7162 Subp_Formal
: Entity_Id
;
7165 Formals_Map
:= No_Elist
;
7167 -- When the pre- or postcondition is inherited, map the formals of
7168 -- the inherited subprogram to those of the current subprogram.
7170 if Present
(Inher_Id
) then
7171 pragma Assert
(Present
(Subp_Id
));
7173 Formals_Map
:= New_Elmt_List
;
7175 -- Create a relation <inherited formal> => <subprogram formal>
7177 Inher_Formal
:= First_Formal
(Inher_Id
);
7178 Subp_Formal
:= First_Formal
(Subp_Id
);
7179 while Present
(Inher_Formal
) and then Present
(Subp_Formal
) loop
7180 Append_Elmt
(Inher_Formal
, Formals_Map
);
7181 Append_Elmt
(Subp_Formal
, Formals_Map
);
7183 Next_Formal
(Inher_Formal
);
7184 Next_Formal
(Subp_Formal
);
7188 -- Copy the original pragma while performing substitutions (if
7195 New_Scope
=> Current_Scope
);
7197 -- Mark the pragma as being internally generated and reset the
7200 Set_Comes_From_Source
(Check_Prag
, False);
7201 Set_Analyzed
(Check_Prag
, False);
7203 if Present
(Corresponding_Aspect
(Prag
)) then
7204 Nam
:= Chars
(Identifier
(Corresponding_Aspect
(Prag
)));
7209 -- Convert the copy into pragma Check by correcting the name and
7210 -- adding a check_kind argument.
7212 Set_Pragma_Identifier
7213 (Check_Prag
, Make_Identifier
(Loc
, Name_Check
));
7215 Prepend_To
(Pragma_Argument_Associations
(Check_Prag
),
7216 Make_Pragma_Argument_Association
(Loc
,
7217 Expression
=> Make_Identifier
(Loc
, Nam
)));
7219 -- Update the error message when the pragma is inherited
7221 if Present
(Inher_Id
) then
7222 Msg_Arg
:= Last
(Pragma_Argument_Associations
(Check_Prag
));
7224 if Chars
(Msg_Arg
) = Name_Message
then
7225 String_To_Name_Buffer
(Strval
(Expression
(Msg_Arg
)));
7227 -- Insert "inherited" to improve the error message
7229 if Name_Buffer
(1 .. 8) = "failed p" then
7230 Insert_Str_In_Name_Buffer
("inherited ", 8);
7231 Set_Strval
(Expression
(Msg_Arg
), String_From_Name_Buffer
);
7237 end Build_Pragma_Check_Equivalent
;
7239 ----------------------------
7240 -- Process_Contract_Cases --
7241 ----------------------------
7243 procedure Process_Contract_Cases
(Stmts
: in out List_Id
) is
7244 procedure Process_Contract_Cases_For
(Subp_Id
: Entity_Id
);
7245 -- Process pragma Contract_Cases for subprogram Subp_Id
7247 --------------------------------
7248 -- Process_Contract_Cases_For --
7249 --------------------------------
7251 procedure Process_Contract_Cases_For
(Subp_Id
: Entity_Id
) is
7252 Items
: constant Node_Id
:= Contract
(Subp_Id
);
7256 if Present
(Items
) then
7257 Prag
:= Contract_Test_Cases
(Items
);
7258 while Present
(Prag
) loop
7259 if Pragma_Name
(Prag
) = Name_Contract_Cases
then
7260 Expand_Contract_Cases
7263 Decls
=> Declarations
(N
),
7267 Prag
:= Next_Pragma
(Prag
);
7270 end Process_Contract_Cases_For
;
7272 -- Start of processing for Process_Contract_Cases
7275 Process_Contract_Cases_For
(Body_Id
);
7277 if Present
(Spec_Id
) then
7278 Process_Contract_Cases_For
(Spec_Id
);
7280 end Process_Contract_Cases
;
7282 ----------------------------
7283 -- Process_Postconditions --
7284 ----------------------------
7286 procedure Process_Postconditions
(Stmts
: in out List_Id
) is
7287 procedure Process_Body_Postconditions
(Post_Nam
: Name_Id
);
7288 -- Collect all [refined] postconditions of a specific kind denoted
7289 -- by Post_Nam that belong to the body and generate pragma Check
7290 -- equivalents in list Stmts.
7292 procedure Process_Spec_Postconditions
;
7293 -- Collect all [inherited] postconditions of the spec and generate
7294 -- pragma Check equivalents in list Stmts.
7296 ---------------------------------
7297 -- Process_Body_Postconditions --
7298 ---------------------------------
7300 procedure Process_Body_Postconditions
(Post_Nam
: Name_Id
) is
7301 Items
: constant Node_Id
:= Contract
(Body_Id
);
7302 Unit_Decl
: constant Node_Id
:= Parent
(N
);
7307 -- Process the contract
7309 if Present
(Items
) then
7310 Prag
:= Pre_Post_Conditions
(Items
);
7311 while Present
(Prag
) loop
7312 if Pragma_Name
(Prag
) = Post_Nam
then
7314 (Item
=> Build_Pragma_Check_Equivalent
(Prag
),
7318 Prag
:= Next_Pragma
(Prag
);
7322 -- The subprogram body being processed is actually the proper body
7323 -- of a stub with a corresponding spec. The subprogram stub may
7324 -- carry a postcondition pragma in which case it must be taken
7325 -- into account. The pragma appears after the stub.
7327 if Present
(Spec_Id
) and then Nkind
(Unit_Decl
) = N_Subunit
then
7328 Decl
:= Next
(Corresponding_Stub
(Unit_Decl
));
7329 while Present
(Decl
) loop
7331 -- Note that non-matching pragmas are skipped
7333 if Nkind
(Decl
) = N_Pragma
then
7334 if Pragma_Name
(Decl
) = Post_Nam
then
7336 (Item
=> Build_Pragma_Check_Equivalent
(Decl
),
7340 -- Skip internally generated code
7342 elsif not Comes_From_Source
(Decl
) then
7345 -- Postcondition pragmas are usually grouped together. There
7346 -- is no need to inspect the whole declarative list.
7355 end Process_Body_Postconditions
;
7357 ---------------------------------
7358 -- Process_Spec_Postconditions --
7359 ---------------------------------
7361 procedure Process_Spec_Postconditions
is
7362 Subps
: constant Subprogram_List
:=
7363 Inherited_Subprograms
(Spec_Id
);
7366 Subp_Id
: Entity_Id
;
7369 -- Process the contract
7371 Items
:= Contract
(Spec_Id
);
7373 if Present
(Items
) then
7374 Prag
:= Pre_Post_Conditions
(Items
);
7375 while Present
(Prag
) loop
7376 if Pragma_Name
(Prag
) = Name_Postcondition
then
7378 (Item
=> Build_Pragma_Check_Equivalent
(Prag
),
7382 Prag
:= Next_Pragma
(Prag
);
7386 -- Process the contracts of all inherited subprograms, looking for
7387 -- class-wide postconditions.
7389 for Index
in Subps
'Range loop
7390 Subp_Id
:= Subps
(Index
);
7391 Items
:= Contract
(Subp_Id
);
7393 if Present
(Items
) then
7394 Prag
:= Pre_Post_Conditions
(Items
);
7395 while Present
(Prag
) loop
7396 if Pragma_Name
(Prag
) = Name_Postcondition
7397 and then Class_Present
(Prag
)
7401 Build_Pragma_Check_Equivalent
7404 Inher_Id
=> Subp_Id
),
7408 Prag
:= Next_Pragma
(Prag
);
7412 end Process_Spec_Postconditions
;
7414 -- Start of processing for Process_Postconditions
7417 -- The processing of postconditions is done in reverse order (body
7418 -- first) to ensure the following arrangement:
7420 -- <refined postconditions from body>
7421 -- <postconditions from body>
7422 -- <postconditions from spec>
7423 -- <inherited postconditions>
7425 Process_Body_Postconditions
(Name_Refined_Post
);
7426 Process_Body_Postconditions
(Name_Postcondition
);
7428 if Present
(Spec_Id
) then
7429 Process_Spec_Postconditions
;
7431 end Process_Postconditions
;
7433 ---------------------------
7434 -- Process_Preconditions --
7435 ---------------------------
7437 procedure Process_Preconditions
is
7438 Class_Pre
: Node_Id
:= Empty
;
7439 -- The sole [inherited] class-wide precondition pragma that applies
7440 -- to the subprogram.
7442 Insert_Node
: Node_Id
:= Empty
;
7443 -- The insertion node after which all pragma Check equivalents are
7446 procedure Merge_Preconditions
(From
: Node_Id
; Into
: Node_Id
);
7447 -- Merge two class-wide preconditions by "or else"-ing them. The
7448 -- changes are accumulated in parameter Into. Update the error
7451 procedure Prepend_To_Decls
(Item
: Node_Id
);
7452 -- Prepend a single item to the declarations of the subprogram body
7454 procedure Prepend_To_Decls_Or_Save
(Prag
: Node_Id
);
7455 -- Save a class-wide precondition into Class_Pre or prepend a normal
7456 -- precondition ot the declarations of the body and analyze it.
7458 procedure Process_Inherited_Preconditions
;
7459 -- Collect all inherited class-wide preconditions and merge them into
7460 -- one big precondition to be evaluated as pragma Check.
7462 procedure Process_Preconditions_For
(Subp_Id
: Entity_Id
);
7463 -- Collect all preconditions of subprogram Subp_Id and prepend their
7464 -- pragma Check equivalents to the declarations of the body.
7466 -------------------------
7467 -- Merge_Preconditions --
7468 -------------------------
7470 procedure Merge_Preconditions
(From
: Node_Id
; Into
: Node_Id
) is
7471 function Expression_Arg
(Prag
: Node_Id
) return Node_Id
;
7472 -- Return the boolean expression argument of a precondition while
7473 -- updating its parenteses count for the subsequent merge.
7475 function Message_Arg
(Prag
: Node_Id
) return Node_Id
;
7476 -- Return the message argument of a precondition
7478 --------------------
7479 -- Expression_Arg --
7480 --------------------
7482 function Expression_Arg
(Prag
: Node_Id
) return Node_Id
is
7483 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
7484 Arg
: constant Node_Id
:= Get_Pragma_Arg
(Next
(First
(Args
)));
7487 if Paren_Count
(Arg
) = 0 then
7488 Set_Paren_Count
(Arg
, 1);
7498 function Message_Arg
(Prag
: Node_Id
) return Node_Id
is
7499 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
7501 return Get_Pragma_Arg
(Last
(Args
));
7506 From_Expr
: constant Node_Id
:= Expression_Arg
(From
);
7507 From_Msg
: constant Node_Id
:= Message_Arg
(From
);
7508 Into_Expr
: constant Node_Id
:= Expression_Arg
(Into
);
7509 Into_Msg
: constant Node_Id
:= Message_Arg
(Into
);
7510 Loc
: constant Source_Ptr
:= Sloc
(Into
);
7512 -- Start of processing for Merge_Preconditions
7515 -- Merge the two preconditions by "or else"-ing them
7519 Right_Opnd
=> Relocate_Node
(Into_Expr
),
7520 Left_Opnd
=> From_Expr
));
7522 -- Merge the two error messages to produce a single message of the
7525 -- failed precondition from ...
7526 -- also failed inherited precondition from ...
7528 if not Exception_Locations_Suppressed
then
7529 Start_String
(Strval
(Into_Msg
));
7530 Store_String_Char
(ASCII
.LF
);
7531 Store_String_Chars
(" also ");
7532 Store_String_Chars
(Strval
(From_Msg
));
7534 Set_Strval
(Into_Msg
, End_String
);
7536 end Merge_Preconditions
;
7538 ----------------------
7539 -- Prepend_To_Decls --
7540 ----------------------
7542 procedure Prepend_To_Decls
(Item
: Node_Id
) is
7543 Decls
: List_Id
:= Declarations
(N
);
7546 -- Ensure that the body has a declarative list
7550 Set_Declarations
(N
, Decls
);
7553 Prepend_To
(Decls
, Item
);
7554 end Prepend_To_Decls
;
7556 ------------------------------
7557 -- Prepend_To_Decls_Or_Save --
7558 ------------------------------
7560 procedure Prepend_To_Decls_Or_Save
(Prag
: Node_Id
) is
7561 Check_Prag
: Node_Id
;
7564 Check_Prag
:= Build_Pragma_Check_Equivalent
(Prag
);
7566 -- Save the sole class-wide precondition (if any) for the next
7567 -- step where it will be merged with inherited preconditions.
7569 if Class_Present
(Prag
) then
7570 pragma Assert
(No
(Class_Pre
));
7571 Class_Pre
:= Check_Prag
;
7573 -- Accumulate the corresponding Check pragmas at the top of the
7574 -- declarations. Prepending the items ensures that they will be
7575 -- evaluated in their original order.
7578 if Present
(Insert_Node
) then
7579 Insert_After
(Insert_Node
, Check_Prag
);
7581 Prepend_To_Decls
(Check_Prag
);
7584 Analyze
(Check_Prag
);
7586 end Prepend_To_Decls_Or_Save
;
7588 -------------------------------------
7589 -- Process_Inherited_Preconditions --
7590 -------------------------------------
7592 procedure Process_Inherited_Preconditions
is
7593 Subps
: constant Subprogram_List
:=
7594 Inherited_Subprograms
(Spec_Id
);
7595 Check_Prag
: Node_Id
;
7598 Subp_Id
: Entity_Id
;
7601 -- Process the contracts of all inherited subprograms, looking for
7602 -- class-wide preconditions.
7604 for Index
in Subps
'Range loop
7605 Subp_Id
:= Subps
(Index
);
7606 Items
:= Contract
(Subp_Id
);
7608 if Present
(Items
) then
7609 Prag
:= Pre_Post_Conditions
(Items
);
7610 while Present
(Prag
) loop
7611 if Pragma_Name
(Prag
) = Name_Precondition
7612 and then Class_Present
(Prag
)
7615 Build_Pragma_Check_Equivalent
7618 Inher_Id
=> Subp_Id
);
7620 -- The spec or an inherited subprogram already yielded
7621 -- a class-wide precondition. Merge the existing
7622 -- precondition with the current one using "or else".
7624 if Present
(Class_Pre
) then
7625 Merge_Preconditions
(Check_Prag
, Class_Pre
);
7627 Class_Pre
:= Check_Prag
;
7631 Prag
:= Next_Pragma
(Prag
);
7636 -- Add the merged class-wide preconditions
7638 if Present
(Class_Pre
) then
7639 Prepend_To_Decls
(Class_Pre
);
7640 Analyze
(Class_Pre
);
7642 end Process_Inherited_Preconditions
;
7644 -------------------------------
7645 -- Process_Preconditions_For --
7646 -------------------------------
7648 procedure Process_Preconditions_For
(Subp_Id
: Entity_Id
) is
7649 Items
: constant Node_Id
:= Contract
(Subp_Id
);
7652 Subp_Decl
: Node_Id
;
7655 -- Process the contract
7657 if Present
(Items
) then
7658 Prag
:= Pre_Post_Conditions
(Items
);
7659 while Present
(Prag
) loop
7660 if Pragma_Name
(Prag
) = Name_Precondition
then
7661 Prepend_To_Decls_Or_Save
(Prag
);
7664 Prag
:= Next_Pragma
(Prag
);
7668 -- The subprogram declaration being processed is actually a body
7669 -- stub. The stub may carry a precondition pragma in which case it
7670 -- must be taken into account. The pragma appears after the stub.
7672 Subp_Decl
:= Unit_Declaration_Node
(Subp_Id
);
7674 if Nkind
(Subp_Decl
) = N_Subprogram_Body_Stub
then
7676 -- Inspect the declarations following the body stub
7678 Decl
:= Next
(Subp_Decl
);
7679 while Present
(Decl
) loop
7681 -- Note that non-matching pragmas are skipped
7683 if Nkind
(Decl
) = N_Pragma
then
7684 if Pragma_Name
(Decl
) = Name_Precondition
then
7685 Prepend_To_Decls_Or_Save
(Decl
);
7688 -- Skip internally generated code
7690 elsif not Comes_From_Source
(Decl
) then
7693 -- Preconditions are usually grouped together. There is no
7694 -- need to inspect the whole declarative list.
7703 end Process_Preconditions_For
;
7707 Decls
: constant List_Id
:= Declarations
(N
);
7710 -- Start of processing for Process_Preconditions
7713 -- Find the last internally generate declaration starting from the
7714 -- top of the body declarations. This ensures that discriminals and
7715 -- subtypes are properly visible to the pragma Check equivalents.
7717 if Present
(Decls
) then
7718 Decl
:= First
(Decls
);
7720 while Present
(Decl
) loop
7721 if Comes_From_Source
(Decl
) then
7724 Insert_Node
:= Decl
;
7731 -- The processing of preconditions is done in reverse order (body
7732 -- first) because each pragma Check equivalent is inserted at the
7733 -- top of the declarations. This ensures that the final order is
7734 -- consistent with following diagram:
7736 -- <inherited preconditions>
7737 -- <preconditions from spec>
7738 -- <preconditions from body>
7740 Process_Preconditions_For
(Body_Id
);
7742 if Present
(Spec_Id
) then
7743 Process_Preconditions_For
(Spec_Id
);
7744 Process_Inherited_Preconditions
;
7746 end Process_Preconditions
;
7750 Restore_Scope
: Boolean := False;
7752 Stmts
: List_Id
:= No_List
;
7753 Subp_Id
: Entity_Id
;
7755 -- Start of processing for Expand_Subprogram_Contract
7758 -- Obtain the entity of the initial declaration
7760 if Present
(Spec_Id
) then
7766 -- Do not perform expansion activity when it is not needed
7768 if not Expander_Active
then
7771 -- ASIS requires an unaltered tree
7773 elsif ASIS_Mode
then
7776 -- GNATprove does not need the executable semantics of a contract
7778 elsif GNATprove_Mode
then
7781 -- The contract of a generic subprogram or one declared in a generic
7782 -- context is not expanded as the corresponding instance will provide
7783 -- the executable semantics of the contract.
7785 elsif Is_Generic_Subprogram
(Subp_Id
) or else Inside_A_Generic
then
7788 -- All subprograms carry a contract, but for some it is not significant
7789 -- and should not be processed. This is a small optimization.
7791 elsif not Has_Significant_Contract
(Subp_Id
) then
7795 -- Do not re-expand the same contract. This scenario occurs when a
7796 -- construct is rewritten into something else during its analysis
7797 -- (expression functions for instance).
7799 if Has_Expanded_Contract
(Subp_Id
) then
7802 -- Otherwise mark the subprogram
7805 Set_Has_Expanded_Contract
(Subp_Id
);
7808 -- Ensure that the formal parameters are visible when expanding all
7811 if not In_Open_Scopes
(Subp_Id
) then
7812 Restore_Scope
:= True;
7813 Push_Scope
(Subp_Id
);
7815 if Is_Generic_Subprogram
(Subp_Id
) then
7816 Install_Generic_Formals
(Subp_Id
);
7818 Install_Formals
(Subp_Id
);
7822 -- The expansion of a subprogram contract involves the creation of Check
7823 -- pragmas to verify the contract assertions of the spec and body in a
7824 -- particular order. The order is as follows:
7826 -- function Example (...) return ... is
7827 -- procedure _Postconditions (...) is
7829 -- <refined postconditions from body>
7830 -- <postconditions from body>
7831 -- <postconditions from spec>
7832 -- <inherited postconditions>
7833 -- <contract case consequences>
7834 -- <invariant check of function result>
7835 -- <invariant and predicate checks of parameters>
7836 -- end _Postconditions;
7838 -- <inherited preconditions>
7839 -- <preconditions from spec>
7840 -- <preconditions from body>
7841 -- <contract case conditions>
7843 -- <source declarations>
7845 -- <source statements>
7847 -- _Preconditions (Result);
7851 -- Routine _Postconditions holds all contract assertions that must be
7852 -- verified on exit from the related subprogram.
7854 -- Step 1: Handle all preconditions. This action must come before the
7855 -- processing of pragma Contract_Cases because the pragma prepends items
7856 -- to the body declarations.
7858 Process_Preconditions
;
7860 -- Step 2: Handle all postconditions. This action must come before the
7861 -- processing of pragma Contract_Cases because the pragma appends items
7864 Process_Postconditions
(Stmts
);
7866 -- Step 3: Handle pragma Contract_Cases. This action must come before
7867 -- the processing of invariants and predicates because those append
7868 -- items to list Smts.
7870 Process_Contract_Cases
(Stmts
);
7872 -- Step 4: Apply invariant and predicate checks on a function result and
7873 -- all formals. The resulting checks are accumulated in list Stmts.
7875 Add_Invariant_And_Predicate_Checks
(Subp_Id
, Stmts
, Result
);
7877 -- Step 5: Construct procedure _Postconditions
7879 Build_Postconditions_Procedure
(Subp_Id
, Stmts
, Result
);
7881 if Restore_Scope
then
7884 end Expand_Subprogram_Contract
;
7886 --------------------------------
7887 -- Is_Build_In_Place_Function --
7888 --------------------------------
7890 function Is_Build_In_Place_Function
(E
: Entity_Id
) return Boolean is
7892 -- This function is called from Expand_Subtype_From_Expr during
7893 -- semantic analysis, even when expansion is off. In those cases
7894 -- the build_in_place expansion will not take place.
7896 if not Expander_Active
then
7900 -- For now we test whether E denotes a function or access-to-function
7901 -- type whose result subtype is inherently limited. Later this test
7902 -- may be revised to allow composite nonlimited types. Functions with
7903 -- a foreign convention or whose result type has a foreign convention
7906 if Ekind_In
(E
, E_Function
, E_Generic_Function
)
7907 or else (Ekind
(E
) = E_Subprogram_Type
7908 and then Etype
(E
) /= Standard_Void_Type
)
7910 -- Note: If the function has a foreign convention, it cannot build
7911 -- its result in place, so you're on your own. On the other hand,
7912 -- if only the return type has a foreign convention, its layout is
7913 -- intended to be compatible with the other language, but the build-
7914 -- in place machinery can ensure that the object is not copied.
7916 if Has_Foreign_Convention
(E
) then
7919 -- In Ada 2005 all functions with an inherently limited return type
7920 -- must be handled using a build-in-place profile, including the case
7921 -- of a function with a limited interface result, where the function
7922 -- may return objects of nonlimited descendants.
7925 return Is_Limited_View
(Etype
(E
))
7926 and then Ada_Version
>= Ada_2005
7927 and then not Debug_Flag_Dot_L
;
7933 end Is_Build_In_Place_Function
;
7935 -------------------------------------
7936 -- Is_Build_In_Place_Function_Call --
7937 -------------------------------------
7939 function Is_Build_In_Place_Function_Call
(N
: Node_Id
) return Boolean is
7940 Exp_Node
: Node_Id
:= N
;
7941 Function_Id
: Entity_Id
;
7944 -- Return False if the expander is currently inactive, since awareness
7945 -- of build-in-place treatment is only relevant during expansion. Note
7946 -- that Is_Build_In_Place_Function, which is called as part of this
7947 -- function, is also conditioned this way, but we need to check here as
7948 -- well to avoid blowing up on processing protected calls when expansion
7949 -- is disabled (such as with -gnatc) since those would trip over the
7950 -- raise of Program_Error below.
7952 -- In SPARK mode, build-in-place calls are not expanded, so that we
7953 -- may end up with a call that is neither resolved to an entity, nor
7954 -- an indirect call.
7956 if not Expander_Active
then
7960 -- Step past qualification or unchecked conversion (the latter can occur
7961 -- in cases of calls to 'Input).
7963 if Nkind_In
(Exp_Node
, N_Qualified_Expression
,
7964 N_Unchecked_Type_Conversion
)
7966 Exp_Node
:= Expression
(N
);
7969 if Nkind
(Exp_Node
) /= N_Function_Call
then
7973 if Is_Entity_Name
(Name
(Exp_Node
)) then
7974 Function_Id
:= Entity
(Name
(Exp_Node
));
7976 -- In the case of an explicitly dereferenced call, use the subprogram
7977 -- type generated for the dereference.
7979 elsif Nkind
(Name
(Exp_Node
)) = N_Explicit_Dereference
then
7980 Function_Id
:= Etype
(Name
(Exp_Node
));
7982 -- This may be a call to a protected function.
7984 elsif Nkind
(Name
(Exp_Node
)) = N_Selected_Component
then
7985 Function_Id
:= Etype
(Entity
(Selector_Name
(Name
(Exp_Node
))));
7988 raise Program_Error
;
7991 return Is_Build_In_Place_Function
(Function_Id
);
7993 end Is_Build_In_Place_Function_Call
;
7995 -----------------------
7996 -- Freeze_Subprogram --
7997 -----------------------
7999 procedure Freeze_Subprogram
(N
: Node_Id
) is
8000 Loc
: constant Source_Ptr
:= Sloc
(N
);
8002 procedure Register_Predefined_DT_Entry
(Prim
: Entity_Id
);
8003 -- (Ada 2005): Register a predefined primitive in all the secondary
8004 -- dispatch tables of its primitive type.
8006 ----------------------------------
8007 -- Register_Predefined_DT_Entry --
8008 ----------------------------------
8010 procedure Register_Predefined_DT_Entry
(Prim
: Entity_Id
) is
8011 Iface_DT_Ptr
: Elmt_Id
;
8012 Tagged_Typ
: Entity_Id
;
8013 Thunk_Id
: Entity_Id
;
8014 Thunk_Code
: Node_Id
;
8017 Tagged_Typ
:= Find_Dispatching_Type
(Prim
);
8019 if No
(Access_Disp_Table
(Tagged_Typ
))
8020 or else not Has_Interfaces
(Tagged_Typ
)
8021 or else not RTE_Available
(RE_Interface_Tag
)
8022 or else Restriction_Active
(No_Dispatching_Calls
)
8027 -- Skip the first two access-to-dispatch-table pointers since they
8028 -- leads to the primary dispatch table (predefined DT and user
8029 -- defined DT). We are only concerned with the secondary dispatch
8030 -- table pointers. Note that the access-to- dispatch-table pointer
8031 -- corresponds to the first implemented interface retrieved below.
8034 Next_Elmt
(Next_Elmt
(First_Elmt
(Access_Disp_Table
(Tagged_Typ
))));
8036 while Present
(Iface_DT_Ptr
)
8037 and then Ekind
(Node
(Iface_DT_Ptr
)) = E_Constant
8039 pragma Assert
(Has_Thunks
(Node
(Iface_DT_Ptr
)));
8040 Expand_Interface_Thunk
(Prim
, Thunk_Id
, Thunk_Code
);
8042 if Present
(Thunk_Code
) then
8043 Insert_Actions_After
(N
, New_List
(
8046 Build_Set_Predefined_Prim_Op_Address
(Loc
,
8048 New_Occurrence_Of
(Node
(Next_Elmt
(Iface_DT_Ptr
)), Loc
),
8049 Position
=> DT_Position
(Prim
),
8051 Unchecked_Convert_To
(RTE
(RE_Prim_Ptr
),
8052 Make_Attribute_Reference
(Loc
,
8053 Prefix
=> New_Occurrence_Of
(Thunk_Id
, Loc
),
8054 Attribute_Name
=> Name_Unrestricted_Access
))),
8056 Build_Set_Predefined_Prim_Op_Address
(Loc
,
8059 (Node
(Next_Elmt
(Next_Elmt
(Next_Elmt
(Iface_DT_Ptr
)))),
8061 Position
=> DT_Position
(Prim
),
8063 Unchecked_Convert_To
(RTE
(RE_Prim_Ptr
),
8064 Make_Attribute_Reference
(Loc
,
8065 Prefix
=> New_Occurrence_Of
(Prim
, Loc
),
8066 Attribute_Name
=> Name_Unrestricted_Access
)))));
8069 -- Skip the tag of the predefined primitives dispatch table
8071 Next_Elmt
(Iface_DT_Ptr
);
8072 pragma Assert
(Has_Thunks
(Node
(Iface_DT_Ptr
)));
8074 -- Skip tag of the no-thunks dispatch table
8076 Next_Elmt
(Iface_DT_Ptr
);
8077 pragma Assert
(not Has_Thunks
(Node
(Iface_DT_Ptr
)));
8079 -- Skip tag of predefined primitives no-thunks dispatch table
8081 Next_Elmt
(Iface_DT_Ptr
);
8082 pragma Assert
(not Has_Thunks
(Node
(Iface_DT_Ptr
)));
8084 Next_Elmt
(Iface_DT_Ptr
);
8086 end Register_Predefined_DT_Entry
;
8090 Subp
: constant Entity_Id
:= Entity
(N
);
8092 -- Start of processing for Freeze_Subprogram
8095 -- We suppress the initialization of the dispatch table entry when
8096 -- VM_Target because the dispatching mechanism is handled internally
8099 if Is_Dispatching_Operation
(Subp
)
8100 and then not Is_Abstract_Subprogram
(Subp
)
8101 and then Present
(DTC_Entity
(Subp
))
8102 and then Present
(Scope
(DTC_Entity
(Subp
)))
8103 and then Tagged_Type_Expansion
8104 and then not Restriction_Active
(No_Dispatching_Calls
)
8105 and then RTE_Available
(RE_Tag
)
8108 Typ
: constant Entity_Id
:= Scope
(DTC_Entity
(Subp
));
8111 -- Handle private overridden primitives
8113 if not Is_CPP_Class
(Typ
) then
8114 Check_Overriding_Operation
(Subp
);
8117 -- We assume that imported CPP primitives correspond with objects
8118 -- whose constructor is in the CPP side; therefore we don't need
8119 -- to generate code to register them in the dispatch table.
8121 if Is_CPP_Class
(Typ
) then
8124 -- Handle CPP primitives found in derivations of CPP_Class types.
8125 -- These primitives must have been inherited from some parent, and
8126 -- there is no need to register them in the dispatch table because
8127 -- Build_Inherit_Prims takes care of initializing these slots.
8129 elsif Is_Imported
(Subp
)
8130 and then (Convention
(Subp
) = Convention_CPP
8131 or else Convention
(Subp
) = Convention_C
)
8135 -- Generate code to register the primitive in non statically
8136 -- allocated dispatch tables
8138 elsif not Building_Static_DT
(Scope
(DTC_Entity
(Subp
))) then
8140 -- When a primitive is frozen, enter its name in its dispatch
8143 if not Is_Interface
(Typ
)
8144 or else Present
(Interface_Alias
(Subp
))
8146 if Is_Predefined_Dispatching_Operation
(Subp
) then
8147 Register_Predefined_DT_Entry
(Subp
);
8150 Insert_Actions_After
(N
,
8151 Register_Primitive
(Loc
, Prim
=> Subp
));
8157 -- Mark functions that return by reference. Note that it cannot be part
8158 -- of the normal semantic analysis of the spec since the underlying
8159 -- returned type may not be known yet (for private types).
8162 Typ
: constant Entity_Id
:= Etype
(Subp
);
8163 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
8165 if Is_Limited_View
(Typ
) then
8166 Set_Returns_By_Ref
(Subp
);
8167 elsif Present
(Utyp
) and then CW_Or_Has_Controlled_Part
(Utyp
) then
8168 Set_Returns_By_Ref
(Subp
);
8172 -- Wnen freezing a null procedure, analyze its delayed aspects now
8173 -- because we may not have reached the end of the declarative list when
8174 -- delayed aspects are normally analyzed. This ensures that dispatching
8175 -- calls are properly rewritten when the generated _Postcondition
8176 -- procedure is analyzed in the null procedure body.
8178 if Nkind
(Parent
(Subp
)) = N_Procedure_Specification
8179 and then Null_Present
(Parent
(Subp
))
8181 Analyze_Subprogram_Contract
(Subp
);
8183 end Freeze_Subprogram
;
8185 -----------------------
8186 -- Is_Null_Procedure --
8187 -----------------------
8189 function Is_Null_Procedure
(Subp
: Entity_Id
) return Boolean is
8190 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
8193 if Ekind
(Subp
) /= E_Procedure
then
8196 -- Check if this is a declared null procedure
8198 elsif Nkind
(Decl
) = N_Subprogram_Declaration
then
8199 if not Null_Present
(Specification
(Decl
)) then
8202 elsif No
(Body_To_Inline
(Decl
)) then
8205 -- Check if the body contains only a null statement, followed by
8206 -- the return statement added during expansion.
8210 Orig_Bod
: constant Node_Id
:= Body_To_Inline
(Decl
);
8216 if Nkind
(Orig_Bod
) /= N_Subprogram_Body
then
8219 -- We must skip SCIL nodes because they are currently
8220 -- implemented as special N_Null_Statement nodes.
8224 (Statements
(Handled_Statement_Sequence
(Orig_Bod
)));
8225 Stat2
:= Next_Non_SCIL_Node
(Stat
);
8228 Is_Empty_List
(Declarations
(Orig_Bod
))
8229 and then Nkind
(Stat
) = N_Null_Statement
8233 (Nkind
(Stat2
) = N_Simple_Return_Statement
8234 and then No
(Next
(Stat2
))));
8242 end Is_Null_Procedure
;
8244 -------------------------------------------
8245 -- Make_Build_In_Place_Call_In_Allocator --
8246 -------------------------------------------
8248 procedure Make_Build_In_Place_Call_In_Allocator
8249 (Allocator
: Node_Id
;
8250 Function_Call
: Node_Id
)
8252 Acc_Type
: constant Entity_Id
:= Etype
(Allocator
);
8254 Func_Call
: Node_Id
:= Function_Call
;
8255 Ref_Func_Call
: Node_Id
;
8256 Function_Id
: Entity_Id
;
8257 Result_Subt
: Entity_Id
;
8258 New_Allocator
: Node_Id
;
8259 Return_Obj_Access
: Entity_Id
; -- temp for function result
8260 Temp_Init
: Node_Id
; -- initial value of Return_Obj_Access
8261 Alloc_Form
: BIP_Allocation_Form
;
8262 Pool
: Node_Id
; -- nonnull if Alloc_Form = User_Storage_Pool
8263 Return_Obj_Actual
: Node_Id
; -- the temp.all, in caller-allocates case
8264 Chain
: Entity_Id
; -- activation chain, in case of tasks
8267 -- Step past qualification or unchecked conversion (the latter can occur
8268 -- in cases of calls to 'Input).
8270 if Nkind_In
(Func_Call
,
8271 N_Qualified_Expression
,
8272 N_Unchecked_Type_Conversion
)
8274 Func_Call
:= Expression
(Func_Call
);
8277 -- If the call has already been processed to add build-in-place actuals
8278 -- then return. This should not normally occur in an allocator context,
8279 -- but we add the protection as a defensive measure.
8281 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
8285 -- Mark the call as processed as a build-in-place call
8287 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8289 Loc
:= Sloc
(Function_Call
);
8291 if Is_Entity_Name
(Name
(Func_Call
)) then
8292 Function_Id
:= Entity
(Name
(Func_Call
));
8294 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8295 Function_Id
:= Etype
(Name
(Func_Call
));
8298 raise Program_Error
;
8301 Result_Subt
:= Available_View
(Etype
(Function_Id
));
8303 -- Create a temp for the function result. In the caller-allocates case,
8304 -- this will be initialized to the result of a new uninitialized
8305 -- allocator. Note: we do not use Allocator as the Related_Node of
8306 -- Return_Obj_Access in call to Make_Temporary below as this would
8307 -- create a sort of infinite "recursion".
8309 Return_Obj_Access
:= Make_Temporary
(Loc
, 'R');
8310 Set_Etype
(Return_Obj_Access
, Acc_Type
);
8312 -- When the result subtype is constrained, the return object is
8313 -- allocated on the caller side, and access to it is passed to the
8316 -- Here and in related routines, we must examine the full view of the
8317 -- type, because the view at the point of call may differ from that
8318 -- that in the function body, and the expansion mechanism depends on
8319 -- the characteristics of the full view.
8321 if Is_Constrained
(Underlying_Type
(Result_Subt
)) then
8323 -- Replace the initialized allocator of form "new T'(Func (...))"
8324 -- with an uninitialized allocator of form "new T", where T is the
8325 -- result subtype of the called function. The call to the function
8326 -- is handled separately further below.
8329 Make_Allocator
(Loc
,
8330 Expression
=> New_Occurrence_Of
(Result_Subt
, Loc
));
8331 Set_No_Initialization
(New_Allocator
);
8333 -- Copy attributes to new allocator. Note that the new allocator
8334 -- logically comes from source if the original one did, so copy the
8335 -- relevant flag. This ensures proper treatment of the restriction
8336 -- No_Implicit_Heap_Allocations in this case.
8338 Set_Storage_Pool
(New_Allocator
, Storage_Pool
(Allocator
));
8339 Set_Procedure_To_Call
(New_Allocator
, Procedure_To_Call
(Allocator
));
8340 Set_Comes_From_Source
(New_Allocator
, Comes_From_Source
(Allocator
));
8342 Rewrite
(Allocator
, New_Allocator
);
8344 -- Initial value of the temp is the result of the uninitialized
8347 Temp_Init
:= Relocate_Node
(Allocator
);
8349 -- Indicate that caller allocates, and pass in the return object
8351 Alloc_Form
:= Caller_Allocation
;
8352 Pool
:= Make_Null
(No_Location
);
8353 Return_Obj_Actual
:=
8354 Make_Unchecked_Type_Conversion
(Loc
,
8355 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Loc
),
8357 Make_Explicit_Dereference
(Loc
,
8358 Prefix
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
)));
8360 -- When the result subtype is unconstrained, the function itself must
8361 -- perform the allocation of the return object, so we pass parameters
8367 -- Case of a user-defined storage pool. Pass an allocation parameter
8368 -- indicating that the function should allocate its result in the
8369 -- pool, and pass the pool. Use 'Unrestricted_Access because the
8370 -- pool may not be aliased.
8372 if VM_Target
= No_VM
8373 and then Present
(Associated_Storage_Pool
(Acc_Type
))
8375 Alloc_Form
:= User_Storage_Pool
;
8377 Make_Attribute_Reference
(Loc
,
8380 (Associated_Storage_Pool
(Acc_Type
), Loc
),
8381 Attribute_Name
=> Name_Unrestricted_Access
);
8383 -- No user-defined pool; pass an allocation parameter indicating that
8384 -- the function should allocate its result on the heap.
8387 Alloc_Form
:= Global_Heap
;
8388 Pool
:= Make_Null
(No_Location
);
8391 -- The caller does not provide the return object in this case, so we
8392 -- have to pass null for the object access actual.
8394 Return_Obj_Actual
:= Empty
;
8397 -- Declare the temp object
8399 Insert_Action
(Allocator
,
8400 Make_Object_Declaration
(Loc
,
8401 Defining_Identifier
=> Return_Obj_Access
,
8402 Object_Definition
=> New_Occurrence_Of
(Acc_Type
, Loc
),
8403 Expression
=> Temp_Init
));
8405 Ref_Func_Call
:= Make_Reference
(Loc
, Func_Call
);
8407 -- Ada 2005 (AI-251): If the type of the allocator is an interface
8408 -- then generate an implicit conversion to force displacement of the
8411 if Is_Interface
(Designated_Type
(Acc_Type
)) then
8414 OK_Convert_To
(Acc_Type
, Ref_Func_Call
));
8418 Assign
: constant Node_Id
:=
8419 Make_Assignment_Statement
(Loc
,
8420 Name
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
),
8421 Expression
=> Ref_Func_Call
);
8422 -- Assign the result of the function call into the temp. In the
8423 -- caller-allocates case, this is overwriting the temp with its
8424 -- initial value, which has no effect. In the callee-allocates case,
8425 -- this is setting the temp to point to the object allocated by the
8429 -- Actions to be inserted. If there are no tasks, this is just the
8430 -- assignment statement. If the allocated object has tasks, we need
8431 -- to wrap the assignment in a block that activates them. The
8432 -- activation chain of that block must be passed to the function,
8433 -- rather than some outer chain.
8435 if Has_Task
(Result_Subt
) then
8436 Actions
:= New_List
;
8437 Build_Task_Allocate_Block_With_Init_Stmts
8438 (Actions
, Allocator
, Init_Stmts
=> New_List
(Assign
));
8439 Chain
:= Activation_Chain_Entity
(Last
(Actions
));
8441 Actions
:= New_List
(Assign
);
8445 Insert_Actions
(Allocator
, Actions
);
8448 -- When the function has a controlling result, an allocation-form
8449 -- parameter must be passed indicating that the caller is allocating
8450 -- the result object. This is needed because such a function can be
8451 -- called as a dispatching operation and must be treated similarly
8452 -- to functions with unconstrained result subtypes.
8454 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8455 (Func_Call
, Function_Id
, Alloc_Form
, Pool_Actual
=> Pool
);
8457 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8458 (Func_Call
, Function_Id
, Acc_Type
);
8460 Add_Task_Actuals_To_Build_In_Place_Call
8461 (Func_Call
, Function_Id
, Master_Actual
=> Master_Id
(Acc_Type
),
8464 -- Add an implicit actual to the function call that provides access
8465 -- to the allocated object. An unchecked conversion to the (specific)
8466 -- result subtype of the function is inserted to handle cases where
8467 -- the access type of the allocator has a class-wide designated type.
8469 Add_Access_Actual_To_Build_In_Place_Call
8470 (Func_Call
, Function_Id
, Return_Obj_Actual
);
8472 -- Finally, replace the allocator node with a reference to the temp
8474 Rewrite
(Allocator
, New_Occurrence_Of
(Return_Obj_Access
, Loc
));
8476 Analyze_And_Resolve
(Allocator
, Acc_Type
);
8477 end Make_Build_In_Place_Call_In_Allocator
;
8479 ---------------------------------------------------
8480 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8481 ---------------------------------------------------
8483 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8484 (Function_Call
: Node_Id
)
8487 Func_Call
: Node_Id
:= Function_Call
;
8488 Function_Id
: Entity_Id
;
8489 Result_Subt
: Entity_Id
;
8490 Return_Obj_Id
: Entity_Id
;
8491 Return_Obj_Decl
: Entity_Id
;
8494 -- Step past qualification or unchecked conversion (the latter can occur
8495 -- in cases of calls to 'Input).
8497 if Nkind_In
(Func_Call
, N_Qualified_Expression
,
8498 N_Unchecked_Type_Conversion
)
8500 Func_Call
:= Expression
(Func_Call
);
8503 -- If the call has already been processed to add build-in-place actuals
8504 -- then return. One place this can occur is for calls to build-in-place
8505 -- functions that occur within a call to a protected operation, where
8506 -- due to rewriting and expansion of the protected call there can be
8507 -- more than one call to Expand_Actuals for the same set of actuals.
8509 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
8513 -- Mark the call as processed as a build-in-place call
8515 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8517 Loc
:= Sloc
(Function_Call
);
8519 if Is_Entity_Name
(Name
(Func_Call
)) then
8520 Function_Id
:= Entity
(Name
(Func_Call
));
8522 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8523 Function_Id
:= Etype
(Name
(Func_Call
));
8526 raise Program_Error
;
8529 Result_Subt
:= Etype
(Function_Id
);
8531 -- If the build-in-place function returns a controlled object, then the
8532 -- object needs to be finalized immediately after the context. Since
8533 -- this case produces a transient scope, the servicing finalizer needs
8534 -- to name the returned object. Create a temporary which is initialized
8535 -- with the function call:
8537 -- Temp_Id : Func_Type := BIP_Func_Call;
8539 -- The initialization expression of the temporary will be rewritten by
8540 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8541 -- Call_In_Object_Declaration.
8543 if Needs_Finalization
(Result_Subt
) then
8545 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'R');
8546 Temp_Decl
: Node_Id
;
8549 -- Reset the guard on the function call since the following does
8550 -- not perform actual call expansion.
8552 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
, False);
8555 Make_Object_Declaration
(Loc
,
8556 Defining_Identifier
=> Temp_Id
,
8557 Object_Definition
=>
8558 New_Occurrence_Of
(Result_Subt
, Loc
),
8560 New_Copy_Tree
(Function_Call
));
8562 Insert_Action
(Function_Call
, Temp_Decl
);
8564 Rewrite
(Function_Call
, New_Occurrence_Of
(Temp_Id
, Loc
));
8565 Analyze
(Function_Call
);
8568 -- When the result subtype is constrained, an object of the subtype is
8569 -- declared and an access value designating it is passed as an actual.
8571 elsif Is_Constrained
(Underlying_Type
(Result_Subt
)) then
8573 -- Create a temporary object to hold the function result
8575 Return_Obj_Id
:= Make_Temporary
(Loc
, 'R');
8576 Set_Etype
(Return_Obj_Id
, Result_Subt
);
8579 Make_Object_Declaration
(Loc
,
8580 Defining_Identifier
=> Return_Obj_Id
,
8581 Aliased_Present
=> True,
8582 Object_Definition
=> New_Occurrence_Of
(Result_Subt
, Loc
));
8584 Set_No_Initialization
(Return_Obj_Decl
);
8586 Insert_Action
(Func_Call
, Return_Obj_Decl
);
8588 -- When the function has a controlling result, an allocation-form
8589 -- parameter must be passed indicating that the caller is allocating
8590 -- the result object. This is needed because such a function can be
8591 -- called as a dispatching operation and must be treated similarly
8592 -- to functions with unconstrained result subtypes.
8594 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8595 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
8597 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8598 (Func_Call
, Function_Id
);
8600 Add_Task_Actuals_To_Build_In_Place_Call
8601 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
8603 -- Add an implicit actual to the function call that provides access
8604 -- to the caller's return object.
8606 Add_Access_Actual_To_Build_In_Place_Call
8607 (Func_Call
, Function_Id
, New_Occurrence_Of
(Return_Obj_Id
, Loc
));
8609 -- When the result subtype is unconstrained, the function must allocate
8610 -- the return object in the secondary stack, so appropriate implicit
8611 -- parameters are added to the call to indicate that. A transient
8612 -- scope is established to ensure eventual cleanup of the result.
8615 -- Pass an allocation parameter indicating that the function should
8616 -- allocate its result on the secondary stack.
8618 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8619 (Func_Call
, Function_Id
, Alloc_Form
=> Secondary_Stack
);
8621 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8622 (Func_Call
, Function_Id
);
8624 Add_Task_Actuals_To_Build_In_Place_Call
8625 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
8627 -- Pass a null value to the function since no return object is
8628 -- available on the caller side.
8630 Add_Access_Actual_To_Build_In_Place_Call
8631 (Func_Call
, Function_Id
, Empty
);
8633 end Make_Build_In_Place_Call_In_Anonymous_Context
;
8635 --------------------------------------------
8636 -- Make_Build_In_Place_Call_In_Assignment --
8637 --------------------------------------------
8639 procedure Make_Build_In_Place_Call_In_Assignment
8641 Function_Call
: Node_Id
)
8643 Lhs
: constant Node_Id
:= Name
(Assign
);
8644 Func_Call
: Node_Id
:= Function_Call
;
8645 Func_Id
: Entity_Id
;
8649 Ptr_Typ
: Entity_Id
;
8650 Ptr_Typ_Decl
: Node_Id
;
8652 Result_Subt
: Entity_Id
;
8656 -- Step past qualification or unchecked conversion (the latter can occur
8657 -- in cases of calls to 'Input).
8659 if Nkind_In
(Func_Call
, N_Qualified_Expression
,
8660 N_Unchecked_Type_Conversion
)
8662 Func_Call
:= Expression
(Func_Call
);
8665 -- If the call has already been processed to add build-in-place actuals
8666 -- then return. This should not normally occur in an assignment context,
8667 -- but we add the protection as a defensive measure.
8669 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
8673 -- Mark the call as processed as a build-in-place call
8675 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8677 Loc
:= Sloc
(Function_Call
);
8679 if Is_Entity_Name
(Name
(Func_Call
)) then
8680 Func_Id
:= Entity
(Name
(Func_Call
));
8682 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8683 Func_Id
:= Etype
(Name
(Func_Call
));
8686 raise Program_Error
;
8689 Result_Subt
:= Etype
(Func_Id
);
8691 -- When the result subtype is unconstrained, an additional actual must
8692 -- be passed to indicate that the caller is providing the return object.
8693 -- This parameter must also be passed when the called function has a
8694 -- controlling result, because dispatching calls to the function needs
8695 -- to be treated effectively the same as calls to class-wide functions.
8697 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8698 (Func_Call
, Func_Id
, Alloc_Form
=> Caller_Allocation
);
8700 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8701 (Func_Call
, Func_Id
);
8703 Add_Task_Actuals_To_Build_In_Place_Call
8704 (Func_Call
, Func_Id
, Make_Identifier
(Loc
, Name_uMaster
));
8706 -- Add an implicit actual to the function call that provides access to
8707 -- the caller's return object.
8709 Add_Access_Actual_To_Build_In_Place_Call
8712 Make_Unchecked_Type_Conversion
(Loc
,
8713 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Loc
),
8714 Expression
=> Relocate_Node
(Lhs
)));
8716 -- Create an access type designating the function's result subtype
8718 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
8721 Make_Full_Type_Declaration
(Loc
,
8722 Defining_Identifier
=> Ptr_Typ
,
8724 Make_Access_To_Object_Definition
(Loc
,
8725 All_Present
=> True,
8726 Subtype_Indication
=>
8727 New_Occurrence_Of
(Result_Subt
, Loc
)));
8728 Insert_After_And_Analyze
(Assign
, Ptr_Typ_Decl
);
8730 -- Finally, create an access object initialized to a reference to the
8731 -- function call. We know this access value is non-null, so mark the
8732 -- entity accordingly to suppress junk access checks.
8734 New_Expr
:= Make_Reference
(Loc
, Relocate_Node
(Func_Call
));
8736 Obj_Id
:= Make_Temporary
(Loc
, 'R', New_Expr
);
8737 Set_Etype
(Obj_Id
, Ptr_Typ
);
8738 Set_Is_Known_Non_Null
(Obj_Id
);
8741 Make_Object_Declaration
(Loc
,
8742 Defining_Identifier
=> Obj_Id
,
8743 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
8744 Expression
=> New_Expr
);
8745 Insert_After_And_Analyze
(Ptr_Typ_Decl
, Obj_Decl
);
8747 Rewrite
(Assign
, Make_Null_Statement
(Loc
));
8749 -- Retrieve the target of the assignment
8751 if Nkind
(Lhs
) = N_Selected_Component
then
8752 Target
:= Selector_Name
(Lhs
);
8753 elsif Nkind
(Lhs
) = N_Type_Conversion
then
8754 Target
:= Expression
(Lhs
);
8759 -- If we are assigning to a return object or this is an expression of
8760 -- an extension aggregate, the target should either be an identifier
8761 -- or a simple expression. All other cases imply a different scenario.
8763 if Nkind
(Target
) in N_Has_Entity
then
8764 Target
:= Entity
(Target
);
8768 end Make_Build_In_Place_Call_In_Assignment
;
8770 ----------------------------------------------------
8771 -- Make_Build_In_Place_Call_In_Object_Declaration --
8772 ----------------------------------------------------
8774 procedure Make_Build_In_Place_Call_In_Object_Declaration
8775 (Object_Decl
: Node_Id
;
8776 Function_Call
: Node_Id
)
8779 Obj_Def_Id
: constant Entity_Id
:=
8780 Defining_Identifier
(Object_Decl
);
8781 Enclosing_Func
: constant Entity_Id
:=
8782 Enclosing_Subprogram
(Obj_Def_Id
);
8783 Call_Deref
: Node_Id
;
8784 Caller_Object
: Node_Id
;
8786 Fmaster_Actual
: Node_Id
:= Empty
;
8787 Func_Call
: Node_Id
:= Function_Call
;
8788 Function_Id
: Entity_Id
;
8789 Pool_Actual
: Node_Id
;
8790 Ptr_Typ
: Entity_Id
;
8791 Ptr_Typ_Decl
: Node_Id
;
8792 Pass_Caller_Acc
: Boolean := False;
8794 Result_Subt
: Entity_Id
;
8797 -- Step past qualification or unchecked conversion (the latter can occur
8798 -- in cases of calls to 'Input).
8800 if Nkind_In
(Func_Call
, N_Qualified_Expression
,
8801 N_Unchecked_Type_Conversion
)
8803 Func_Call
:= Expression
(Func_Call
);
8806 -- If the call has already been processed to add build-in-place actuals
8807 -- then return. This should not normally occur in an object declaration,
8808 -- but we add the protection as a defensive measure.
8810 if Is_Expanded_Build_In_Place_Call
(Func_Call
) then
8814 -- Mark the call as processed as a build-in-place call
8816 Set_Is_Expanded_Build_In_Place_Call
(Func_Call
);
8818 Loc
:= Sloc
(Function_Call
);
8820 if Is_Entity_Name
(Name
(Func_Call
)) then
8821 Function_Id
:= Entity
(Name
(Func_Call
));
8823 elsif Nkind
(Name
(Func_Call
)) = N_Explicit_Dereference
then
8824 Function_Id
:= Etype
(Name
(Func_Call
));
8827 raise Program_Error
;
8830 Result_Subt
:= Etype
(Function_Id
);
8832 -- Create an access type designating the function's result subtype. We
8833 -- use the type of the original call because it may be a call to an
8834 -- inherited operation, which the expansion has replaced with the parent
8835 -- operation that yields the parent type. Note that this access type
8836 -- must be declared before we establish a transient scope, so that it
8837 -- receives the proper accessibility level.
8839 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
8841 Make_Full_Type_Declaration
(Loc
,
8842 Defining_Identifier
=> Ptr_Typ
,
8844 Make_Access_To_Object_Definition
(Loc
,
8845 All_Present
=> True,
8846 Subtype_Indication
=>
8847 New_Occurrence_Of
(Etype
(Function_Call
), Loc
)));
8849 -- The access type and its accompanying object must be inserted after
8850 -- the object declaration in the constrained case, so that the function
8851 -- call can be passed access to the object. In the unconstrained case,
8852 -- or if the object declaration is for a return object, the access type
8853 -- and object must be inserted before the object, since the object
8854 -- declaration is rewritten to be a renaming of a dereference of the
8855 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8856 -- the result object is in a different (transient) scope, so won't
8859 if Is_Constrained
(Underlying_Type
(Result_Subt
))
8860 and then not Is_Return_Object
(Defining_Identifier
(Object_Decl
))
8862 Insert_After_And_Analyze
(Object_Decl
, Ptr_Typ_Decl
);
8864 Insert_Action
(Object_Decl
, Ptr_Typ_Decl
);
8867 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
8868 -- elaborated in an inner (transient) scope and thus won't cause
8869 -- freezing by itself.
8872 Ptr_Typ_Freeze_Ref
: constant Node_Id
:=
8873 New_Occurrence_Of
(Ptr_Typ
, Loc
);
8875 Set_Parent
(Ptr_Typ_Freeze_Ref
, Ptr_Typ_Decl
);
8876 Freeze_Expression
(Ptr_Typ_Freeze_Ref
);
8879 -- If the the object is a return object of an enclosing build-in-place
8880 -- function, then the implicit build-in-place parameters of the
8881 -- enclosing function are simply passed along to the called function.
8882 -- (Unfortunately, this won't cover the case of extension aggregates
8883 -- where the ancestor part is a build-in-place unconstrained function
8884 -- call that should be passed along the caller's parameters. Currently
8885 -- those get mishandled by reassigning the result of the call to the
8886 -- aggregate return object, when the call result should really be
8887 -- directly built in place in the aggregate and not in a temporary. ???)
8889 if Is_Return_Object
(Defining_Identifier
(Object_Decl
)) then
8890 Pass_Caller_Acc
:= True;
8892 -- When the enclosing function has a BIP_Alloc_Form formal then we
8893 -- pass it along to the callee (such as when the enclosing function
8894 -- has an unconstrained or tagged result type).
8896 if Needs_BIP_Alloc_Form
(Enclosing_Func
) then
8897 if VM_Target
= No_VM
and then
8898 RTE_Available
(RE_Root_Storage_Pool_Ptr
)
8901 New_Occurrence_Of
(Build_In_Place_Formal
8902 (Enclosing_Func
, BIP_Storage_Pool
), Loc
);
8904 -- The build-in-place pool formal is not built on .NET/JVM
8907 Pool_Actual
:= Empty
;
8910 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8915 (Build_In_Place_Formal
(Enclosing_Func
, BIP_Alloc_Form
),
8917 Pool_Actual
=> Pool_Actual
);
8919 -- Otherwise, if enclosing function has a constrained result subtype,
8920 -- then caller allocation will be used.
8923 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8924 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
8927 if Needs_BIP_Finalization_Master
(Enclosing_Func
) then
8930 (Build_In_Place_Formal
8931 (Enclosing_Func
, BIP_Finalization_Master
), Loc
);
8934 -- Retrieve the BIPacc formal from the enclosing function and convert
8935 -- it to the access type of the callee's BIP_Object_Access formal.
8938 Make_Unchecked_Type_Conversion
(Loc
,
8942 (Build_In_Place_Formal
(Function_Id
, BIP_Object_Access
)),
8946 (Build_In_Place_Formal
(Enclosing_Func
, BIP_Object_Access
),
8949 -- In the constrained case, add an implicit actual to the function call
8950 -- that provides access to the declared object. An unchecked conversion
8951 -- to the (specific) result type of the function is inserted to handle
8952 -- the case where the object is declared with a class-wide type.
8954 elsif Is_Constrained
(Underlying_Type
(Result_Subt
)) then
8956 Make_Unchecked_Type_Conversion
(Loc
,
8957 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Loc
),
8958 Expression
=> New_Occurrence_Of
(Obj_Def_Id
, Loc
));
8960 -- When the function has a controlling result, an allocation-form
8961 -- parameter must be passed indicating that the caller is allocating
8962 -- the result object. This is needed because such a function can be
8963 -- called as a dispatching operation and must be treated similarly
8964 -- to functions with unconstrained result subtypes.
8966 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8967 (Func_Call
, Function_Id
, Alloc_Form
=> Caller_Allocation
);
8969 -- In other unconstrained cases, pass an indication to do the allocation
8970 -- on the secondary stack and set Caller_Object to Empty so that a null
8971 -- value will be passed for the caller's object address. A transient
8972 -- scope is established to ensure eventual cleanup of the result.
8975 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8976 (Func_Call
, Function_Id
, Alloc_Form
=> Secondary_Stack
);
8977 Caller_Object
:= Empty
;
8979 Establish_Transient_Scope
(Object_Decl
, Sec_Stack
=> True);
8982 -- Pass along any finalization master actual, which is needed in the
8983 -- case where the called function initializes a return object of an
8984 -- enclosing build-in-place function.
8986 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8987 (Func_Call
=> Func_Call
,
8988 Func_Id
=> Function_Id
,
8989 Master_Exp
=> Fmaster_Actual
);
8991 if Nkind
(Parent
(Object_Decl
)) = N_Extended_Return_Statement
8992 and then Has_Task
(Result_Subt
)
8994 -- Here we're passing along the master that was passed in to this
8997 Add_Task_Actuals_To_Build_In_Place_Call
8998 (Func_Call
, Function_Id
,
9000 New_Occurrence_Of
(Build_In_Place_Formal
9001 (Enclosing_Func
, BIP_Task_Master
), Loc
));
9004 Add_Task_Actuals_To_Build_In_Place_Call
9005 (Func_Call
, Function_Id
, Make_Identifier
(Loc
, Name_uMaster
));
9008 Add_Access_Actual_To_Build_In_Place_Call
9009 (Func_Call
, Function_Id
, Caller_Object
, Is_Access
=> Pass_Caller_Acc
);
9011 -- Finally, create an access object initialized to a reference to the
9012 -- function call. We know this access value cannot be null, so mark the
9013 -- entity accordingly to suppress the access check.
9015 Def_Id
:= Make_Temporary
(Loc
, 'R', Func_Call
);
9016 Set_Etype
(Def_Id
, Ptr_Typ
);
9017 Set_Is_Known_Non_Null
(Def_Id
);
9020 Make_Object_Declaration
(Loc
,
9021 Defining_Identifier
=> Def_Id
,
9022 Constant_Present
=> True,
9023 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
),
9025 Make_Reference
(Loc
, Relocate_Node
(Func_Call
)));
9027 Insert_After_And_Analyze
(Ptr_Typ_Decl
, Res_Decl
);
9029 -- If the result subtype of the called function is constrained and
9030 -- is not itself the return expression of an enclosing BIP function,
9031 -- then mark the object as having no initialization.
9033 if Is_Constrained
(Underlying_Type
(Result_Subt
))
9034 and then not Is_Return_Object
(Defining_Identifier
(Object_Decl
))
9036 -- The related object declaration is encased in a transient block
9037 -- because the build-in-place function call contains at least one
9038 -- nested function call that produces a controlled transient
9041 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
9043 -- Since the build-in-place expansion decouples the call from the
9044 -- object declaration, the finalization machinery lacks the context
9045 -- which prompted the generation of the transient block. To resolve
9046 -- this scenario, store the build-in-place call.
9048 if Scope_Is_Transient
9049 and then Node_To_Be_Wrapped
= Object_Decl
9051 Set_BIP_Initialization_Call
(Obj_Def_Id
, Res_Decl
);
9054 Set_Expression
(Object_Decl
, Empty
);
9055 Set_No_Initialization
(Object_Decl
);
9057 -- In case of an unconstrained result subtype, or if the call is the
9058 -- return expression of an enclosing BIP function, rewrite the object
9059 -- declaration as an object renaming where the renamed object is a
9060 -- dereference of <function_Call>'reference:
9062 -- Obj : Subt renames <function_call>'Ref.all;
9066 Make_Explicit_Dereference
(Loc
,
9067 Prefix
=> New_Occurrence_Of
(Def_Id
, Loc
));
9069 Loc
:= Sloc
(Object_Decl
);
9070 Rewrite
(Object_Decl
,
9071 Make_Object_Renaming_Declaration
(Loc
,
9072 Defining_Identifier
=> Make_Temporary
(Loc
, 'D'),
9073 Access_Definition
=> Empty
,
9074 Subtype_Mark
=> New_Occurrence_Of
(Result_Subt
, Loc
),
9075 Name
=> Call_Deref
));
9077 Set_Renamed_Object
(Defining_Identifier
(Object_Decl
), Call_Deref
);
9079 Analyze
(Object_Decl
);
9081 -- Replace the internal identifier of the renaming declaration's
9082 -- entity with identifier of the original object entity. We also have
9083 -- to exchange the entities containing their defining identifiers to
9084 -- ensure the correct replacement of the object declaration by the
9085 -- object renaming declaration to avoid homograph conflicts (since
9086 -- the object declaration's defining identifier was already entered
9087 -- in current scope). The Next_Entity links of the two entities also
9088 -- have to be swapped since the entities are part of the return
9089 -- scope's entity list and the list structure would otherwise be
9090 -- corrupted. Finally, the homonym chain must be preserved as well.
9093 Renaming_Def_Id
: constant Entity_Id
:=
9094 Defining_Identifier
(Object_Decl
);
9095 Next_Entity_Temp
: constant Entity_Id
:=
9096 Next_Entity
(Renaming_Def_Id
);
9098 Set_Chars
(Renaming_Def_Id
, Chars
(Obj_Def_Id
));
9100 -- Swap next entity links in preparation for exchanging entities
9102 Set_Next_Entity
(Renaming_Def_Id
, Next_Entity
(Obj_Def_Id
));
9103 Set_Next_Entity
(Obj_Def_Id
, Next_Entity_Temp
);
9104 Set_Homonym
(Renaming_Def_Id
, Homonym
(Obj_Def_Id
));
9106 Exchange_Entities
(Renaming_Def_Id
, Obj_Def_Id
);
9108 -- Preserve source indication of original declaration, so that
9109 -- xref information is properly generated for the right entity.
9111 Preserve_Comes_From_Source
9112 (Object_Decl
, Original_Node
(Object_Decl
));
9114 Preserve_Comes_From_Source
9115 (Obj_Def_Id
, Original_Node
(Object_Decl
));
9117 Set_Comes_From_Source
(Renaming_Def_Id
, False);
9121 -- If the object entity has a class-wide Etype, then we need to change
9122 -- it to the result subtype of the function call, because otherwise the
9123 -- object will be class-wide without an explicit initialization and
9124 -- won't be allocated properly by the back end. It seems unclean to make
9125 -- such a revision to the type at this point, and we should try to
9126 -- improve this treatment when build-in-place functions with class-wide
9127 -- results are implemented. ???
9129 if Is_Class_Wide_Type
(Etype
(Defining_Identifier
(Object_Decl
))) then
9130 Set_Etype
(Defining_Identifier
(Object_Decl
), Result_Subt
);
9132 end Make_Build_In_Place_Call_In_Object_Declaration
;
9134 --------------------------------------------
9135 -- Make_CPP_Constructor_Call_In_Allocator --
9136 --------------------------------------------
9138 procedure Make_CPP_Constructor_Call_In_Allocator
9139 (Allocator
: Node_Id
;
9140 Function_Call
: Node_Id
)
9142 Loc
: constant Source_Ptr
:= Sloc
(Function_Call
);
9143 Acc_Type
: constant Entity_Id
:= Etype
(Allocator
);
9144 Function_Id
: constant Entity_Id
:= Entity
(Name
(Function_Call
));
9145 Result_Subt
: constant Entity_Id
:= Available_View
(Etype
(Function_Id
));
9147 New_Allocator
: Node_Id
;
9148 Return_Obj_Access
: Entity_Id
;
9152 pragma Assert
(Nkind
(Allocator
) = N_Allocator
9153 and then Nkind
(Function_Call
) = N_Function_Call
);
9154 pragma Assert
(Convention
(Function_Id
) = Convention_CPP
9155 and then Is_Constructor
(Function_Id
));
9156 pragma Assert
(Is_Constrained
(Underlying_Type
(Result_Subt
)));
9158 -- Replace the initialized allocator of form "new T'(Func (...))" with
9159 -- an uninitialized allocator of form "new T", where T is the result
9160 -- subtype of the called function. The call to the function is handled
9161 -- separately further below.
9164 Make_Allocator
(Loc
,
9165 Expression
=> New_Occurrence_Of
(Result_Subt
, Loc
));
9166 Set_No_Initialization
(New_Allocator
);
9168 -- Copy attributes to new allocator. Note that the new allocator
9169 -- logically comes from source if the original one did, so copy the
9170 -- relevant flag. This ensures proper treatment of the restriction
9171 -- No_Implicit_Heap_Allocations in this case.
9173 Set_Storage_Pool
(New_Allocator
, Storage_Pool
(Allocator
));
9174 Set_Procedure_To_Call
(New_Allocator
, Procedure_To_Call
(Allocator
));
9175 Set_Comes_From_Source
(New_Allocator
, Comes_From_Source
(Allocator
));
9177 Rewrite
(Allocator
, New_Allocator
);
9179 -- Create a new access object and initialize it to the result of the
9180 -- new uninitialized allocator. Note: we do not use Allocator as the
9181 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9182 -- as this would create a sort of infinite "recursion".
9184 Return_Obj_Access
:= Make_Temporary
(Loc
, 'R');
9185 Set_Etype
(Return_Obj_Access
, Acc_Type
);
9188 -- Rnnn : constant ptr_T := new (T);
9189 -- Init (Rnn.all,...);
9192 Make_Object_Declaration
(Loc
,
9193 Defining_Identifier
=> Return_Obj_Access
,
9194 Constant_Present
=> True,
9195 Object_Definition
=> New_Occurrence_Of
(Acc_Type
, Loc
),
9196 Expression
=> Relocate_Node
(Allocator
));
9197 Insert_Action
(Allocator
, Tmp_Obj
);
9199 Insert_List_After_And_Analyze
(Tmp_Obj
,
9200 Build_Initialization_Call
(Loc
,
9202 Make_Explicit_Dereference
(Loc
,
9203 Prefix
=> New_Occurrence_Of
(Return_Obj_Access
, Loc
)),
9204 Typ
=> Etype
(Function_Id
),
9205 Constructor_Ref
=> Function_Call
));
9207 -- Finally, replace the allocator node with a reference to the result of
9208 -- the function call itself (which will effectively be an access to the
9209 -- object created by the allocator).
9211 Rewrite
(Allocator
, New_Occurrence_Of
(Return_Obj_Access
, Loc
));
9213 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9214 -- generate an implicit conversion to force displacement of the "this"
9217 if Is_Interface
(Designated_Type
(Acc_Type
)) then
9218 Rewrite
(Allocator
, Convert_To
(Acc_Type
, Relocate_Node
(Allocator
)));
9221 Analyze_And_Resolve
(Allocator
, Acc_Type
);
9222 end Make_CPP_Constructor_Call_In_Allocator
;
9224 -----------------------------------
9225 -- Needs_BIP_Finalization_Master --
9226 -----------------------------------
9228 function Needs_BIP_Finalization_Master
9229 (Func_Id
: Entity_Id
) return Boolean
9231 pragma Assert
(Is_Build_In_Place_Function
(Func_Id
));
9232 Func_Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
9235 not Restriction_Active
(No_Finalization
)
9236 and then Needs_Finalization
(Func_Typ
);
9237 end Needs_BIP_Finalization_Master
;
9239 --------------------------
9240 -- Needs_BIP_Alloc_Form --
9241 --------------------------
9243 function Needs_BIP_Alloc_Form
(Func_Id
: Entity_Id
) return Boolean is
9244 pragma Assert
(Is_Build_In_Place_Function
(Func_Id
));
9245 Func_Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
9247 return not Is_Constrained
(Func_Typ
) or else Is_Tagged_Type
(Func_Typ
);
9248 end Needs_BIP_Alloc_Form
;
9250 --------------------------------------
9251 -- Needs_Result_Accessibility_Level --
9252 --------------------------------------
9254 function Needs_Result_Accessibility_Level
9255 (Func_Id
: Entity_Id
) return Boolean
9257 Func_Typ
: constant Entity_Id
:= Underlying_Type
(Etype
(Func_Id
));
9259 function Has_Unconstrained_Access_Discriminant_Component
9260 (Comp_Typ
: Entity_Id
) return Boolean;
9261 -- Returns True if any component of the type has an unconstrained access
9264 -----------------------------------------------------
9265 -- Has_Unconstrained_Access_Discriminant_Component --
9266 -----------------------------------------------------
9268 function Has_Unconstrained_Access_Discriminant_Component
9269 (Comp_Typ
: Entity_Id
) return Boolean
9272 if not Is_Limited_Type
(Comp_Typ
) then
9275 -- Only limited types can have access discriminants with
9278 elsif Has_Unconstrained_Access_Discriminants
(Comp_Typ
) then
9281 elsif Is_Array_Type
(Comp_Typ
) then
9282 return Has_Unconstrained_Access_Discriminant_Component
9283 (Underlying_Type
(Component_Type
(Comp_Typ
)));
9285 elsif Is_Record_Type
(Comp_Typ
) then
9290 Comp
:= First_Component
(Comp_Typ
);
9291 while Present
(Comp
) loop
9292 if Has_Unconstrained_Access_Discriminant_Component
9293 (Underlying_Type
(Etype
(Comp
)))
9298 Next_Component
(Comp
);
9304 end Has_Unconstrained_Access_Discriminant_Component
;
9306 Feature_Disabled
: constant Boolean := True;
9309 -- Start of processing for Needs_Result_Accessibility_Level
9312 -- False if completion unavailable (how does this happen???)
9314 if not Present
(Func_Typ
) then
9317 elsif Feature_Disabled
then
9320 -- False if not a function, also handle enum-lit renames case
9322 elsif Func_Typ
= Standard_Void_Type
9323 or else Is_Scalar_Type
(Func_Typ
)
9327 -- Handle a corner case, a cross-dialect subp renaming. For example,
9328 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
9329 -- an Ada 2005 (or earlier) unit references predefined run-time units.
9331 elsif Present
(Alias
(Func_Id
)) then
9333 -- Unimplemented: a cross-dialect subp renaming which does not set
9334 -- the Alias attribute (e.g., a rename of a dereference of an access
9335 -- to subprogram value). ???
9337 return Present
(Extra_Accessibility_Of_Result
(Alias
(Func_Id
)));
9339 -- Remaining cases require Ada 2012 mode
9341 elsif Ada_Version
< Ada_2012
then
9344 elsif Ekind
(Func_Typ
) = E_Anonymous_Access_Type
9345 or else Is_Tagged_Type
(Func_Typ
)
9347 -- In the case of, say, a null tagged record result type, the need
9348 -- for this extra parameter might not be obvious. This function
9349 -- returns True for all tagged types for compatibility reasons.
9350 -- A function with, say, a tagged null controlling result type might
9351 -- be overridden by a primitive of an extension having an access
9352 -- discriminant and the overrider and overridden must have compatible
9353 -- calling conventions (including implicitly declared parameters).
9354 -- Similarly, values of one access-to-subprogram type might designate
9355 -- both a primitive subprogram of a given type and a function
9356 -- which is, for example, not a primitive subprogram of any type.
9357 -- Again, this requires calling convention compatibility.
9358 -- It might be possible to solve these issues by introducing
9359 -- wrappers, but that is not the approach that was chosen.
9363 elsif Has_Unconstrained_Access_Discriminants
(Func_Typ
) then
9366 elsif Has_Unconstrained_Access_Discriminant_Component
(Func_Typ
) then
9369 -- False for all other cases
9374 end Needs_Result_Accessibility_Level
;