1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2024, 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 -- Expand routines for chapter 6 constructs
28 with Types
; use Types
;
32 procedure Expand_N_Extended_Return_Statement
(N
: Node_Id
);
33 procedure Expand_N_Function_Call
(N
: Node_Id
);
34 procedure Expand_N_Procedure_Call_Statement
(N
: Node_Id
);
35 procedure Expand_N_Return_When_Statement
(N
: Node_Id
);
36 procedure Expand_N_Simple_Return_Statement
(N
: Node_Id
);
37 procedure Expand_N_Subprogram_Body
(N
: Node_Id
);
38 procedure Expand_N_Subprogram_Body_Stub
(N
: Node_Id
);
39 procedure Expand_N_Subprogram_Declaration
(N
: Node_Id
);
41 procedure Expand_Call
(N
: Node_Id
);
42 -- This procedure contains common processing for Expand_N_Function_Call,
43 -- Expand_N_Procedure_Statement, and Expand_N_Entry_Call.
45 procedure Freeze_Subprogram
(N
: Node_Id
);
46 -- generate the appropriate expansions related to Subprogram freeze
47 -- nodes (e.g. the filling of the corresponding Dispatch Table for
48 -- Primitive Operations)
50 -- The following type defines the various forms of allocation used for the
51 -- results of build-in-place function calls.
53 type BIP_Allocation_Form
is
60 type BIP_Formal_Kind
is
61 -- Ada 2005 (AI-318-02): This type defines the kinds of implicit extra
62 -- formals created for build-in-place functions. The order of these
63 -- enumeration literals matches the order in which the formals are
64 -- declared. See Sem_Ch6.Create_Extra_Formals.
67 -- Present if result subtype is unconstrained or tagged. Indicates
68 -- whether the return object is allocated by the caller or callee, and
69 -- if the callee, whether to use the secondary stack or the heap. See
70 -- Create_Extra_Formals.
73 -- Present if result subtype is unconstrained or tagged. If
74 -- BIP_Alloc_Form = User_Storage_Pool, this is a pointer to the pool
75 -- (of type access to Root_Storage_Pool'Class). Otherwise null.
77 BIP_Finalization_Master
,
78 -- Present if result type needs finalization. Pointer to caller's
79 -- finalization master.
82 -- Present if result type contains tasks. Master associated with
86 -- Present if result type contains tasks. Caller's activation chain
89 -- Present for all build-in-place functions. Address at which to place
90 -- the return object, or null if BIP_Alloc_Form indicates allocated by
93 -- ??? We might also need to be able to pass in a constrained flag.
95 procedure Add_Extra_Actual_To_Call
96 (Subprogram_Call
: Node_Id
;
97 Extra_Formal
: Entity_Id
;
98 Extra_Actual
: Node_Id
);
99 -- Adds Extra_Actual as a named parameter association for the formal
100 -- Extra_Formal in Subprogram_Call.
102 procedure Apply_CW_Accessibility_Check
(Exp
: Node_Id
; Func
: Entity_Id
);
103 -- Ada 2005 (AI95-344): If the result type is class-wide, insert a check
104 -- that the level of the return expression's underlying type is not deeper
105 -- than the level of the master enclosing the function. Always generate the
106 -- check when the type of the return expression is class-wide, when it's a
107 -- type conversion, or when it's a formal parameter. Otherwise suppress the
108 -- check in the case where the return expression has a specific type whose
109 -- level is known not to be statically deeper than the result type of the
112 function BIP_Formal_Suffix
(Kind
: BIP_Formal_Kind
) return String;
113 -- Ada 2005 (AI-318-02): Returns a string to be used as the suffix of names
114 -- for build-in-place formal parameters of the given kind.
116 function BIP_Suffix_Kind
(E
: Entity_Id
) return BIP_Formal_Kind
;
117 -- Ada 2005 (AI-318-02): Returns the kind of the given BIP extra formal.
119 function Build_In_Place_Formal
121 Kind
: BIP_Formal_Kind
) return Entity_Id
;
122 -- Ada 2005 (AI-318-02): Locates and returns the entity for the implicit
123 -- build-in-place formal parameter of the given kind associated with the
124 -- function Func, and returns its Entity_Id. It is a bug if not found; the
125 -- caller should ensure this is called only when the extra formal exists.
127 function Build_Procedure_Body_Form
128 (Func_Id
: Entity_Id
; Func_Body
: Node_Id
) return Node_Id
;
129 -- Create a procedure body which emulates the behavior of function Func_Id.
130 -- Func_Body is the root of the body of the function before its analysis.
131 -- The returned node is the root of the procedure body which will replace
132 -- the original function body, which is not needed for the C program.
134 function Has_BIP_Extra_Formal
136 Kind
: BIP_Formal_Kind
;
137 Must_Be_Frozen
: Boolean := True) return Boolean;
138 -- Given a subprogram, subprogram type, entry or entry family, return True
139 -- if E has the BIP extra formal associated with Kind. In general this
140 -- subprogram must be invoked with a frozen entity or a subprogram type of
141 -- a dispatching call since we can only rely on the availability of extra
142 -- formals on these entities; this requirement can be relaxed using the
143 -- formal Must_Be_Frozen in scenarios where we know that the entity has
144 -- the extra formals.
146 procedure Install_Class_Preconditions_Check
(Call_Node
: Node_Id
);
147 -- Install check of class-wide preconditions on the caller.
149 function Is_Build_In_Place_Entity
(E
: Entity_Id
) return Boolean;
150 -- Ada 2005 (AI-318-02): Returns True if E is a BIP entity.
152 function Is_Build_In_Place_Function
(E
: Entity_Id
) return Boolean;
153 -- Ada 2005 (AI-318-02): Returns True if E denotes a function, generic
154 -- function, or access-to-function type for which
155 -- Is_Build_In_Place_Result_Type is True. However, we never use
156 -- build-in-place if the convention is other than Ada, because that would
157 -- disturb mixed-language programs.
159 function Is_Build_In_Place_Function_Call
(N
: Node_Id
) return Boolean;
160 -- Ada 2005 (AI-318-02): Returns True if N denotes a call to a function
161 -- that requires handling as a build-in-place call (possibly qualified or
164 function Is_Build_In_Place_Result_Type
(Typ
: Entity_Id
) return Boolean;
165 -- Ada 2005 (AI-318-02): Returns True if functions returning the type use
166 -- build-in-place protocols. For inherently limited types, this must be
167 -- True in >= Ada 2005 and must be False in Ada 95.
169 function Is_Build_In_Place_Return_Object
(E
: Entity_Id
) return Boolean;
170 -- Ada 2005 (AI-318-02): Return True if E is a return object of a function
171 -- that uses build-in-place protocols.
173 function Is_By_Reference_Return_Object
(E
: Entity_Id
) return Boolean;
174 -- Return True if E is a return object of a function whose return type is
175 -- required to be passed by reference, as defined in (RM 6.2(4-9)).
177 function Is_Null_Procedure
(Subp
: Entity_Id
) return Boolean;
178 -- Predicate to recognize stubbed procedures and null procedures, which
179 -- can be inlined unconditionally in all cases.
181 function Is_Secondary_Stack_Return_Object
(E
: Entity_Id
) return Boolean;
182 -- Return True if E is a return object of a function whose return type is
183 -- returned on the secondary stack.
185 function Is_Special_Return_Object
(E
: Entity_Id
) return Boolean;
186 -- Return True if E is the return object of a function and is handled in a
187 -- special way by the expander. In most cases, return objects are handled
188 -- like any other variables or constants but, in a few special cases, they
189 -- are further expanded into more elaborate constructs, whose common goal
190 -- is to elide the copy operation associated with the return.
192 procedure Make_Build_In_Place_Call_In_Allocator
193 (Allocator
: Node_Id
;
194 Function_Call
: Node_Id
);
195 -- Ada 2005 (AI-318-02): Handle a call to a build-in-place function that
196 -- occurs as the expression initializing an allocator, by passing access
197 -- to the allocated object as an additional parameter of the function call.
198 -- A new access object is declared that is initialized to the result of the
199 -- allocator, passed to the function, and the allocator is rewritten to
200 -- refer to that access object. Function_Call must denote either an
201 -- N_Function_Call node for which Is_Build_In_Place_Call is True, or else
202 -- an N_Qualified_Expression node applied to such a function call.
204 procedure Make_Build_In_Place_Call_In_Anonymous_Context
205 (Function_Call
: Node_Id
);
206 -- Ada 2005 (AI-318-02): Handle a call to a build-in-place function that
207 -- occurs in a context that does not provide a separate object. A temporary
208 -- object is created to act as the return object and an access to the
209 -- temporary is passed as an additional parameter of the call. This occurs
210 -- in contexts such as subprogram call actuals and object renamings.
211 -- Function_Call must denote either an N_Function_Call node for which
212 -- Is_Build_In_Place_Call is True, or else an N_Qualified_Expression node
213 -- applied to such a function call.
215 procedure Make_Build_In_Place_Call_In_Assignment
217 Function_Call
: Node_Id
);
218 -- Ada 2005 (AI-318-02): Handle a call to a build-in-place function that
219 -- occurs as the right-hand side of an assignment statement by passing
220 -- access to the left-hand side as an additional parameter of the function
221 -- call. Assign must denote a N_Assignment_Statement. Function_Call must
222 -- denote either an N_Function_Call node for which Is_Build_In_Place_Call
223 -- is True, or an N_Qualified_Expression node applied to such a function
226 procedure Make_Build_In_Place_Call_In_Object_Declaration
228 Function_Call
: Node_Id
);
229 -- Ada 2005 (AI-318-02): Handle a call to a build-in-place function that
230 -- occurs as the expression initializing an object declaration by
231 -- passing access to the declared object as an additional parameter of the
232 -- function call. Function_Call must denote either an N_Function_Call node
233 -- for which Is_Build_In_Place_Call is True, or an N_Qualified_Expression
234 -- node applied to such a function call.
236 procedure Make_Build_In_Place_Iface_Call_In_Allocator
237 (Allocator
: Node_Id
;
238 Function_Call
: Node_Id
);
239 -- Ada 2005 (AI-318-02): Handle a call to a build-in-place function that
240 -- occurs as the expression initializing an allocator, by passing access
241 -- to the allocated object as an additional parameter of the function call.
242 -- Function_Call must denote an expression containing a BIP function call
243 -- and an enclosing call to Ada.Tags.Displace to displace the pointer to
244 -- the returned BIP object to reference the secondary dispatch table of
247 procedure Make_Build_In_Place_Iface_Call_In_Anonymous_Context
248 (Function_Call
: Node_Id
);
249 -- Ada 2005 (AI-318-02): Handle a call to a build-in-place function that
250 -- occurs in a context that does not provide a separate object. A temporary
251 -- object is created to act as the return object and an access to the
252 -- temporary is passed as an additional parameter of the call. This occurs
253 -- in contexts such as subprogram call actuals and object renamings.
254 -- Function_Call must denote an expression containing a BIP function call
255 -- and an enclosing call to Ada.Tags.Displace to displace the pointer to
256 -- the returned BIP object to reference the secondary dispatch table of
259 procedure Make_Build_In_Place_Iface_Call_In_Object_Declaration
261 Function_Call
: Node_Id
);
262 -- Ada 2005 (AI-318-02): Handle a call to a build-in-place function that
263 -- occurs as the expression initializing an object declaration by passing
264 -- access to the declared object as an additional parameter of the function
265 -- call. Function_Call must denote an expression containing a BIP function
266 -- call and an enclosing call to Ada.Tags.Displace to displace the pointer
267 -- to the returned BIP object to reference the secondary dispatch table of
270 procedure Make_CPP_Constructor_Call_In_Allocator
271 (Allocator
: Node_Id
;
272 Function_Call
: Node_Id
);
273 -- Handle a call to a CPP constructor that occurs as the expression that
274 -- initializes an allocator, by passing access to the allocated object as
275 -- an additional parameter of the constructor call. A new access object is
276 -- declared that is initialized to the result of the allocator, passed to
277 -- the constructor, and the allocator is rewritten to refer to that access
278 -- object. Function_Call must denote a call to a CPP_Constructor function.
280 function Might_Have_Tasks
(Typ
: Entity_Id
) return Boolean;
281 -- Return True when type Typ has tasks or when it is a limited class-wide
282 -- type (or subtype), since it might have task components.
284 function Needs_BIP_Alloc_Form
(Func_Id
: Entity_Id
) return Boolean;
285 -- Ada 2005 (AI-318-02): Return True if the function needs an implicit
286 -- BIP_Alloc_Form parameter (see type BIP_Formal_Kind).
288 function Needs_BIP_Finalization_Master
(Func_Id
: Entity_Id
) return Boolean;
289 -- Ada 2005 (AI-318-02): Return True if the result subtype of function
290 -- Func_Id might need finalization actions. This includes build-in-place
291 -- functions with tagged result types, since they can be invoked via
292 -- dispatching calls, and descendant types may require finalization.
294 function Needs_BIP_Task_Actuals
(Func_Id
: Entity_Id
) return Boolean;
295 -- Return True if the function returns an object of a type that has tasks.
297 function Unqual_BIP_Iface_Function_Call
(Expr
: Node_Id
) return Node_Id
;
298 -- Return the inner BIP function call removing any qualification from Expr
299 -- including qualified expressions, type conversions, references, unchecked
300 -- conversions and calls to displace the pointer to the object, if Expr is
301 -- an expression containing a call displacing the pointer to the BIP object
302 -- to reference the secondary dispatch table of an interface; otherwise
305 procedure Validate_Subprogram_Calls
(N
: Node_Id
);
306 -- Check that the number of actuals (including extra actuals) of calls in
307 -- the subtree N match their corresponding formals; check also that the
308 -- names of BIP extra actuals and formals match.
311 pragma Inline
(Is_Build_In_Place_Return_Object
);