| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- E X P _ C H 6 -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 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. -- |
| 20 |
-- -- |
| 21 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 22 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 23 |
-- -- |
| 24 |
------------------------------------------------------------------------------ |
| 25 |
|
| 26 |
-- Expand routines for chapter 6 constructs |
| 27 |
|
| 28 |
with Types; use Types; |
| 29 |
|
| 30 |
package Exp_Ch6 is |
| 31 |
|
| 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); |
| 40 |
|
| 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. |
| 44 |
|
| 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) |
| 49 |
|
| 50 |
-- Ada 2005 (AI-318-02): The following type defines the various forms of |
| 51 |
-- allocation used for the result of build-in-place function calls. |
| 52 |
|
| 53 |
type BIP_Allocation_Form is |
| 54 |
(Unspecified, |
| 55 |
Caller_Allocation, |
| 56 |
Secondary_Stack, |
| 57 |
Global_Heap, |
| 58 |
User_Storage_Pool); |
| 59 |
|
| 60 |
-- Ada 2005 (AI-318-02): This type defines the kinds of implicit extra |
| 61 |
-- formals created for build-in-place functions. The order of these |
| 62 |
-- enumeration literals matches the order in which the formals are |
| 63 |
-- declared. See Sem_Ch6.Create_Extra_Formals. |
| 64 |
|
| 65 |
type BIP_Formal_Kind is |
| 66 |
(BIP_Alloc_Form, |
| 67 |
-- Present if result subtype is returned on the secondary stack or is |
| 68 |
-- tagged: in this case, this indicates whether the return object is |
| 69 |
-- allocated by the caller or callee, and if the callee, whether to |
| 70 |
-- use the secondary stack, the global heap or a storage pool. Also |
| 71 |
-- present if result type needs finalization. |
| 72 |
|
| 73 |
BIP_Storage_Pool, |
| 74 |
-- Present if result subtype is returned on the secondary stack or is |
| 75 |
-- tagged: in this case, if BIP_Alloc_Form = User_Storage_Pool, this |
| 76 |
-- is a pointer to the pool (of type Root_Storage_Pool_Ptr); otherwise |
| 77 |
-- this is null. Also present if result type needs finalization. |
| 78 |
|
| 79 |
BIP_Collection, |
| 80 |
-- Present if result type needs finalization. Pointer to the collection |
| 81 |
-- of the access type used by the caller. |
| 82 |
|
| 83 |
BIP_Task_Master, |
| 84 |
-- Present if result type contains tasks. Master associated with |
| 85 |
-- calling context. |
| 86 |
|
| 87 |
BIP_Activation_Chain, |
| 88 |
-- Present if result type contains tasks. Caller's activation chain |
| 89 |
|
| 90 |
BIP_Object_Access); |
| 91 |
-- Present for all build-in-place functions. Address at which to place |
| 92 |
-- the return object, or null if BIP_Alloc_Form indicates allocated by |
| 93 |
-- callee. |
| 94 |
-- |
| 95 |
-- ??? We might also need to be able to pass in a constrained flag. |
| 96 |
|
| 97 |
procedure Add_Extra_Actual_To_Call |
| 98 |
(Subprogram_Call : Node_Id; |
| 99 |
Extra_Formal : Entity_Id; |
| 100 |
Extra_Actual : Node_Id); |
| 101 |
-- Adds Extra_Actual as a named parameter association for the formal |
| 102 |
-- Extra_Formal in Subprogram_Call. |
| 103 |
|
| 104 |
procedure Apply_CW_Accessibility_Check (Exp : Node_Id; Func : Entity_Id); |
| 105 |
-- Ada 2005 (AI95-344): If the result type is class-wide, insert a check |
| 106 |
-- that the level of the return expression's underlying type is not deeper |
| 107 |
-- than the level of the master enclosing the function. Always generate the |
| 108 |
-- check when the type of the return expression is class-wide, when it's a |
| 109 |
-- type conversion, or when it's a formal parameter. Otherwise suppress the |
| 110 |
-- check in the case where the return expression has a specific type whose |
| 111 |
-- level is known not to be statically deeper than the result type of the |
| 112 |
-- function. |
| 113 |
|
| 114 |
function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String; |
| 115 |
-- Ada 2005 (AI-318-02): Returns a string to be used as the suffix of names |
| 116 |
-- for build-in-place formal parameters of the given kind. |
| 117 |
|
| 118 |
function BIP_Suffix_Kind (E : Entity_Id) return BIP_Formal_Kind; |
| 119 |
-- Ada 2005 (AI-318-02): Returns the kind of the given BIP extra formal. |
| 120 |
|
| 121 |
function Build_In_Place_Formal |
| 122 |
(Func : Entity_Id; |
| 123 |
Kind : BIP_Formal_Kind) return Entity_Id; |
| 124 |
-- Ada 2005 (AI-318-02): Locates and returns the entity for the implicit |
| 125 |
-- build-in-place formal parameter of the given kind associated with the |
| 126 |
-- function Func, and returns its Entity_Id. It is a bug if not found; the |
| 127 |
-- caller should ensure this is called only when the extra formal exists. |
| 128 |
|
| 129 |
function Build_Procedure_Body_Form |
| 130 |
(Func_Id : Entity_Id; Func_Body : Node_Id) return Node_Id; |
| 131 |
-- Create a procedure body which emulates the behavior of function Func_Id. |
| 132 |
-- Func_Body is the root of the body of the function before its analysis. |
| 133 |
-- The returned node is the root of the procedure body which will replace |
| 134 |
-- the original function body, which is not needed for the C program. |
| 135 |
|
| 136 |
function Has_BIP_Extra_Formal |
| 137 |
(E : Entity_Id; |
| 138 |
Kind : BIP_Formal_Kind; |
| 139 |
Must_Be_Frozen : Boolean := True) return Boolean; |
| 140 |
-- Given a subprogram, subprogram type, entry or entry family, return True |
| 141 |
-- if E has the BIP extra formal associated with Kind. In general this |
| 142 |
-- subprogram must be invoked with a frozen entity or a subprogram type of |
| 143 |
-- a dispatching call since we can only rely on the availability of extra |
| 144 |
-- formals on these entities; this requirement can be relaxed using the |
| 145 |
-- formal Must_Be_Frozen in scenarios where we know that the entity has |
| 146 |
-- the extra formals. |
| 147 |
|
| 148 |
procedure Install_Class_Preconditions_Check (Call_Node : Node_Id); |
| 149 |
-- Install check of class-wide preconditions on the caller. |
| 150 |
|
| 151 |
function Is_Build_In_Place_Entity (E : Entity_Id) return Boolean; |
| 152 |
-- Ada 2005 (AI-318-02): Returns True if E is a BIP entity. |
| 153 |
|
| 154 |
function Is_Build_In_Place_Function (E : Entity_Id) return Boolean; |
| 155 |
-- Ada 2005 (AI-318-02): Returns True if E denotes a function, generic |
| 156 |
-- function, or access-to-function type for which |
| 157 |
-- Is_Build_In_Place_Result_Type is True. However, we never use |
| 158 |
-- build-in-place if the convention is other than Ada, because that would |
| 159 |
-- disturb mixed-language programs. |
| 160 |
|
| 161 |
function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean; |
| 162 |
-- Ada 2005 (AI-318-02): Returns True if N denotes a call to a function |
| 163 |
-- that requires handling as a build-in-place call (possibly qualified or |
| 164 |
-- converted). |
| 165 |
|
| 166 |
function Is_Build_In_Place_Result_Type (Typ : Entity_Id) return Boolean; |
| 167 |
-- Ada 2005 (AI-318-02): Returns True if functions returning the type use |
| 168 |
-- build-in-place protocols. For inherently limited types, this must be |
| 169 |
-- True in >= Ada 2005 and must be False in Ada 95. |
| 170 |
|
| 171 |
function Is_Build_In_Place_Return_Object (E : Entity_Id) return Boolean; |
| 172 |
-- Ada 2005 (AI-318-02): Return True if E is a return object of a function |
| 173 |
-- that uses build-in-place protocols. |
| 174 |
|
| 175 |
function Is_By_Reference_Return_Object (E : Entity_Id) return Boolean; |
| 176 |
-- Return True if E is a return object of a function whose return type is |
| 177 |
-- required to be passed by reference, as defined in (RM 6.2(4-9)). |
| 178 |
|
| 179 |
function Is_Null_Procedure (Subp : Entity_Id) return Boolean; |
| 180 |
-- Predicate to recognize stubbed procedures and null procedures, which |
| 181 |
-- can be inlined unconditionally in all cases. |
| 182 |
|
| 183 |
function Is_Secondary_Stack_Return_Object (E : Entity_Id) return Boolean; |
| 184 |
-- Return True if E is a return object of a function whose return type is |
| 185 |
-- returned on the secondary stack. |
| 186 |
|
| 187 |
function Is_Special_Return_Object (E : Entity_Id) return Boolean; |
| 188 |
-- Return True if E is the return object of a function and is handled in a |
| 189 |
-- special way by the expander. In most cases, return objects are handled |
| 190 |
-- like any other variables or constants but, in a few special cases, they |
| 191 |
-- are further expanded into more elaborate constructs, whose common goal |
| 192 |
-- is to elide the copy operation associated with the return. |
| 193 |
|
| 194 |
procedure Make_Build_In_Place_Call_In_Allocator |
| 195 |
(Allocator : Node_Id; |
| 196 |
Function_Call : Node_Id); |
| 197 |
-- Ada 2005 (AI-318-02): Handle a call to a build-in-place function that |
| 198 |
-- occurs as the expression initializing an allocator, by passing access |
| 199 |
-- to the allocated object as an additional parameter of the function call. |
| 200 |
-- A new access object is declared that is initialized to the result of the |
| 201 |
-- allocator, passed to the function, and the allocator is rewritten to |
| 202 |
-- refer to that access object. Function_Call must denote either an |
| 203 |
-- N_Function_Call node for which Is_Build_In_Place_Call is True, or else |
| 204 |
-- an N_Qualified_Expression node applied to such a function call. |
| 205 |
|
| 206 |
procedure Make_Build_In_Place_Call_In_Anonymous_Context |
| 207 |
(Function_Call : Node_Id); |
| 208 |
-- Ada 2005 (AI-318-02): Handle a call to a build-in-place function that |
| 209 |
-- occurs in a context that does not provide a separate object. A temporary |
| 210 |
-- object is created to act as the return object and an access to the |
| 211 |
-- temporary is passed as an additional parameter of the call. This occurs |
| 212 |
-- in contexts such as subprogram call actuals and object renamings. |
| 213 |
-- Function_Call must denote either an N_Function_Call node for which |
| 214 |
-- Is_Build_In_Place_Call is True, or else an N_Qualified_Expression node |
| 215 |
-- applied to such a function call. |
| 216 |
|
| 217 |
procedure Make_Build_In_Place_Call_In_Assignment |
| 218 |
(Assign : Node_Id; |
| 219 |
Function_Call : Node_Id); |
| 220 |
-- Ada 2005 (AI-318-02): Handle a call to a build-in-place function that |
| 221 |
-- occurs as the right-hand side of an assignment statement by passing |
| 222 |
-- access to the left-hand side as an additional parameter of the function |
| 223 |
-- call. Assign must denote a N_Assignment_Statement. Function_Call must |
| 224 |
-- denote either an N_Function_Call node for which Is_Build_In_Place_Call |
| 225 |
-- is True, or an N_Qualified_Expression node applied to such a function |
| 226 |
-- call. |
| 227 |
|
| 228 |
procedure Make_Build_In_Place_Call_In_Object_Declaration |
| 229 |
(Obj_Decl : Node_Id; |
| 230 |
Function_Call : Node_Id); |
| 231 |
-- Ada 2005 (AI-318-02): Handle a call to a build-in-place function that |
| 232 |
-- occurs as the expression initializing an object declaration by |
| 233 |
-- passing access to the declared object as an additional parameter of the |
| 234 |
-- function call. Function_Call must denote either an N_Function_Call node |
| 235 |
-- for which Is_Build_In_Place_Call is True, or an N_Qualified_Expression |
| 236 |
-- node applied to such a function call. |
| 237 |
|
| 238 |
procedure Make_Build_In_Place_Iface_Call_In_Allocator |
| 239 |
(Allocator : Node_Id; |
| 240 |
Function_Call : Node_Id); |
| 241 |
-- Ada 2005 (AI-318-02): Handle a call to a build-in-place function that |
| 242 |
-- occurs as the expression initializing an allocator, by passing access |
| 243 |
-- to the allocated object as an additional parameter of the function call. |
| 244 |
-- Function_Call must denote an expression containing a BIP function call |
| 245 |
-- and an enclosing call to Ada.Tags.Displace to displace the pointer to |
| 246 |
-- the returned BIP object to reference the secondary dispatch table of |
| 247 |
-- an interface. |
| 248 |
|
| 249 |
procedure Make_Build_In_Place_Iface_Call_In_Anonymous_Context |
| 250 |
(Function_Call : Node_Id); |
| 251 |
-- Ada 2005 (AI-318-02): Handle a call to a build-in-place function that |
| 252 |
-- occurs in a context that does not provide a separate object. A temporary |
| 253 |
-- object is created to act as the return object and an access to the |
| 254 |
-- temporary is passed as an additional parameter of the call. This occurs |
| 255 |
-- in contexts such as subprogram call actuals and object renamings. |
| 256 |
-- Function_Call must denote an expression containing a BIP function call |
| 257 |
-- and an enclosing call to Ada.Tags.Displace to displace the pointer to |
| 258 |
-- the returned BIP object to reference the secondary dispatch table of |
| 259 |
-- an interface. |
| 260 |
|
| 261 |
procedure Make_Build_In_Place_Iface_Call_In_Object_Declaration |
| 262 |
(Obj_Decl : Node_Id; |
| 263 |
Function_Call : Node_Id); |
| 264 |
-- Ada 2005 (AI-318-02): Handle a call to a build-in-place function that |
| 265 |
-- occurs as the expression initializing an object declaration by passing |
| 266 |
-- access to the declared object as an additional parameter of the function |
| 267 |
-- call. Function_Call must denote an expression containing a BIP function |
| 268 |
-- call and an enclosing call to Ada.Tags.Displace to displace the pointer |
| 269 |
-- to the returned BIP object to reference the secondary dispatch table of |
| 270 |
-- an interface. |
| 271 |
|
| 272 |
procedure Make_CPP_Constructor_Call_In_Allocator |
| 273 |
(Allocator : Node_Id; |
| 274 |
Function_Call : Node_Id); |
| 275 |
-- Handle a call to a CPP constructor that occurs as the expression that |
| 276 |
-- initializes an allocator, by passing access to the allocated object as |
| 277 |
-- an additional parameter of the constructor call. A new access object is |
| 278 |
-- declared that is initialized to the result of the allocator, passed to |
| 279 |
-- the constructor, and the allocator is rewritten to refer to that access |
| 280 |
-- object. Function_Call must denote a call to a CPP_Constructor function. |
| 281 |
|
| 282 |
function Might_Have_Tasks (Typ : Entity_Id) return Boolean; |
| 283 |
-- Return True when type Typ has tasks or when it is a limited class-wide |
| 284 |
-- type (or subtype), since it might have task components. |
| 285 |
|
| 286 |
function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean; |
| 287 |
-- Ada 2005 (AI-318-02): Return True if the function needs an implicit |
| 288 |
-- BIP_Alloc_Form parameter (see type BIP_Formal_Kind). |
| 289 |
|
| 290 |
function Needs_BIP_Collection (Func_Id : Entity_Id) return Boolean; |
| 291 |
-- Ada 2005 (AI-318-02): Return True if the result subtype of function |
| 292 |
-- Func_Id might need finalization actions. This includes build-in-place |
| 293 |
-- functions with tagged result types, since they can be invoked via |
| 294 |
-- dispatching calls, and descendant types may require finalization. |
| 295 |
|
| 296 |
function Needs_BIP_Task_Actuals (Func_Id : Entity_Id) return Boolean; |
| 297 |
-- Ada 2005 (AI-318-02): Return True if the function needs implicit |
| 298 |
-- BIP_Task_Master and BIP_Activation_Chain parameters. |
| 299 |
|
| 300 |
function Unqual_BIP_Iface_Function_Call (Expr : Node_Id) return Node_Id; |
| 301 |
-- Return the inner BIP function call removing any qualification from Expr |
| 302 |
-- including qualified expressions, type conversions, references, unchecked |
| 303 |
-- conversions and calls to displace the pointer to the object, if Expr is |
| 304 |
-- an expression containing a call displacing the pointer to the BIP object |
| 305 |
-- to reference the secondary dispatch table of an interface; otherwise |
| 306 |
-- return Empty. |
| 307 |
|
| 308 |
procedure Validate_Subprogram_Calls (N : Node_Id); |
| 309 |
-- Check that the number of actuals (including extra actuals) of calls in |
| 310 |
-- the subtree N match their corresponding formals; check also that the |
| 311 |
-- names of BIP extra actuals and formals match. |
| 312 |
|
| 313 |
private |
| 314 |
pragma Inline (Is_Build_In_Place_Return_Object); |
| 315 |
|
| 316 |
end Exp_Ch6; |