| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- E X P _ C H 9 -- |
| 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 9 constructs |
| 27 |
|
| 28 |
with Types; use Types; |
| 29 |
|
| 30 |
package Exp_Ch9 is |
| 31 |
|
| 32 |
type Subprogram_Protection_Mode is |
| 33 |
(Dispatching_Mode, |
| 34 |
Protected_Mode, |
| 35 |
Unprotected_Mode); |
| 36 |
-- This type is used to distinguish the different protection modes of a |
| 37 |
-- protected subprogram. |
| 38 |
|
| 39 |
procedure Build_Activation_Chain_Entity (N : Node_Id); |
| 40 |
-- Given a declaration N of an object that is a task, or contains tasks |
| 41 |
-- (other than allocators to tasks) this routine ensures that an activation |
| 42 |
-- chain has been declared in the appropriate scope, building the required |
| 43 |
-- declaration for the chain variable if not. The name of this variable |
| 44 |
-- is always _Chain and it is accessed by name. |
| 45 |
|
| 46 |
function Build_Call_With_Task (N : Node_Id; E : Entity_Id) return Node_Id; |
| 47 |
-- N is a node representing the name of a task or an access to a task. |
| 48 |
-- The value returned is a call to the function whose name is the entity |
| 49 |
-- E (typically a runtime routine entity obtained using RTE) with the |
| 50 |
-- Task_Id of the associated task as the parameter. The caller is |
| 51 |
-- responsible for analyzing and resolving the resulting tree. |
| 52 |
|
| 53 |
procedure Build_Class_Wide_Master (Typ : Entity_Id); |
| 54 |
-- Given an access-to-limited class-wide type or an access-to-limited |
| 55 |
-- interface, ensure that the designated type has a _master and generate |
| 56 |
-- a renaming of the said master to service the access type. |
| 57 |
|
| 58 |
function Build_Master_Declaration (Loc : Source_Ptr) return Node_Id; |
| 59 |
-- For targets supporting tasks, generate: |
| 60 |
-- _Master : constant Integer := Current_Master.all; |
| 61 |
-- For targets where tasks or tasking hierarchies are prohibited, generate: |
| 62 |
-- _Master : constant Master_Id := 3; |
| 63 |
|
| 64 |
procedure Build_Master_Entity (Obj_Or_Typ : Entity_Id); |
| 65 |
-- Given the name of an object or a type which is either a task, contains |
| 66 |
-- tasks or designates tasks, create a _master in the appropriate scope |
| 67 |
-- which captures the value of Current_Master. Mark the nearest enclosing |
| 68 |
-- body or block as being a task master. |
| 69 |
|
| 70 |
procedure Build_Master_Renaming |
| 71 |
(Ptr_Typ : Entity_Id; |
| 72 |
Ins_Nod : Node_Id := Empty); |
| 73 |
-- Given an access type Ptr_Typ whose designated type is either a task or |
| 74 |
-- contains tasks, create a renaming of the form: |
| 75 |
-- |
| 76 |
-- <Ptr_Typ>M : Master_Id renames _Master; |
| 77 |
-- |
| 78 |
-- where _master denotes the task master of the enclosing context. Ins_Nod |
| 79 |
-- is used to provide a specific insertion node for the renaming. |
| 80 |
|
| 81 |
function Build_Protected_Sub_Specification |
| 82 |
(N : Node_Id; |
| 83 |
Prot_Typ : Entity_Id; |
| 84 |
Mode : Subprogram_Protection_Mode) return Node_Id; |
| 85 |
-- Build the specification for protected subprogram. This is called when |
| 86 |
-- expanding a protected type, and also when expanding the declaration for |
| 87 |
-- an Access_To_Protected_Subprogram type. In the latter case, Prot_Typ is |
| 88 |
-- empty, and the first parameter of the signature of the protected op is |
| 89 |
-- of type System.Address. |
| 90 |
|
| 91 |
procedure Build_Protected_Subprogram_Call |
| 92 |
(N : Node_Id; |
| 93 |
Name : Node_Id; |
| 94 |
Rec : Node_Id; |
| 95 |
External : Boolean := True); |
| 96 |
-- The node N is a subprogram or entry call to a protected subprogram. This |
| 97 |
-- procedure rewrites this call with the appropriate expansion. Name is the |
| 98 |
-- subprogram, and Rec is the record corresponding to the protected object. |
| 99 |
-- External is False if the call is to another protected subprogram within |
| 100 |
-- the same object. |
| 101 |
|
| 102 |
procedure Build_Protected_Subprogram_Call_Cleanup |
| 103 |
(Op_Spec : Node_Id; |
| 104 |
Conc_Typ : Node_Id; |
| 105 |
Loc : Source_Ptr; |
| 106 |
Stmts : List_Id); |
| 107 |
-- Append to Stmts the cleanups after a call to a protected subprogram |
| 108 |
-- whose specification is Op_Spec. Conc_Typ is the concurrent type and Loc |
| 109 |
-- the sloc for appended statements. The cleanup will either unlock the |
| 110 |
-- protected object or serve pending entries. |
| 111 |
|
| 112 |
procedure Build_Task_Activation_Call (N : Node_Id); |
| 113 |
-- This procedure is called for constructs that can be task activators, |
| 114 |
-- i.e. task bodies, subprogram bodies, package bodies and blocks. If the |
| 115 |
-- construct is a task activator (as indicated by the non-empty setting of |
| 116 |
-- Activation_Chain_Entity, either in the construct, or, in the case of a |
| 117 |
-- package body, in its associated package spec), then a call to |
| 118 |
-- Activate_Tasks with this entity as the single parameter is inserted at |
| 119 |
-- the start of the statements of the activator. |
| 120 |
|
| 121 |
procedure Build_Task_Allocate_Block |
| 122 |
(Actions : List_Id; |
| 123 |
N : Node_Id; |
| 124 |
Init_Stmts : List_Id); |
| 125 |
-- This routine is used in the case of allocators where the designated type |
| 126 |
-- is a task or contains tasks. In this case, the normal initialize call |
| 127 |
-- is replaced by: |
| 128 |
-- |
| 129 |
-- blockname : label; |
| 130 |
-- blockname : declare |
| 131 |
-- _Chain : Activation_Chain; |
| 132 |
-- |
| 133 |
-- procedure _Expunge is |
| 134 |
-- begin |
| 135 |
-- Expunge_Unactivated_Tasks (_Chain); |
| 136 |
-- end; |
| 137 |
-- |
| 138 |
-- begin |
| 139 |
-- Init_Stmts; |
| 140 |
-- Activate_Tasks (_Chain); |
| 141 |
-- at end |
| 142 |
-- _Expunge; |
| 143 |
-- end; |
| 144 |
-- |
| 145 |
-- to get the task or tasks created and initialized. The expunge call |
| 146 |
-- ensures that any tasks that get created but not activated due to an |
| 147 |
-- exception are properly expunged (it has no effect in the normal case). |
| 148 |
-- The argument N is the allocator, and Args is the list of arguments for |
| 149 |
-- the initialization call, constructed by the caller, which uses the |
| 150 |
-- Master_Id of the access type as the _Master parameter, and _Chain |
| 151 |
-- (defined above) as the _Chain parameter. |
| 152 |
|
| 153 |
function Build_Wrapper_Spec |
| 154 |
(Subp_Id : Entity_Id; |
| 155 |
Obj_Typ : Entity_Id; |
| 156 |
Formals : List_Id) return Node_Id; |
| 157 |
-- Ada 2005 (AI-345): Build the specification of a primitive operation |
| 158 |
-- associated with a protected or task type. This is required to implement |
| 159 |
-- dispatching calls through interfaces. Subp_Id is the primitive to be |
| 160 |
-- wrapped, Obj_Typ is the type of the newly added formal parameter to |
| 161 |
-- handle object notation, Formals are the original entry formals that |
| 162 |
-- will be explicitly replicated. |
| 163 |
|
| 164 |
function Concurrent_Ref (N : Node_Id) return Node_Id; |
| 165 |
-- Given the name of a concurrent object (task or protected object), or |
| 166 |
-- the name of an access to a concurrent object, this function returns an |
| 167 |
-- expression referencing the associated Task_Id or Protection object, |
| 168 |
-- respectively. Note that a special case is when the name is a reference |
| 169 |
-- to a task type name. This can only happen within a task body, and the |
| 170 |
-- meaning is to get the Task_Id for the currently executing task. |
| 171 |
|
| 172 |
function Convert_Concurrent |
| 173 |
(N : Node_Id; |
| 174 |
Typ : Entity_Id) return Node_Id; |
| 175 |
-- N is an expression of type Typ. If the type is not a concurrent type |
| 176 |
-- then it is returned unchanged. If it is a task or protected reference, |
| 177 |
-- Convert_Concurrent creates an unchecked conversion node from this |
| 178 |
-- expression to the corresponding concurrent record type value. We need |
| 179 |
-- this in any situation where the concurrent type is used, because the |
| 180 |
-- actual concurrent object is an object of the corresponding concurrent |
| 181 |
-- type, and manipulations on the concurrent object actually manipulate the |
| 182 |
-- corresponding object of the record type. |
| 183 |
|
| 184 |
function Entry_Index_Expression |
| 185 |
(Sloc : Source_Ptr; |
| 186 |
Ent : Entity_Id; |
| 187 |
Index : Node_Id; |
| 188 |
Ttyp : Entity_Id) |
| 189 |
return Node_Id; |
| 190 |
-- Returns an expression to compute a task entry index given the name of |
| 191 |
-- the entry or entry family. For the case of a task entry family, the |
| 192 |
-- Index parameter contains the expression for the subscript. Ttyp is the |
| 193 |
-- task type. |
| 194 |
|
| 195 |
procedure Establish_Task_Master (N : Node_Id); |
| 196 |
-- Given a subprogram body, or a block statement, or a task body, this |
| 197 |
-- procedure makes the necessary transformations required of a task master |
| 198 |
-- (add Enter_Master call at start, and establish a cleanup routine to make |
| 199 |
-- sure Complete_Master is called on exit). |
| 200 |
|
| 201 |
procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id); |
| 202 |
-- Build Equivalent_Type for an Access_To_Protected_Subprogram. |
| 203 |
-- Equivalent_Type is a record type with two components: a pointer to the |
| 204 |
-- protected object, and a pointer to the operation itself. |
| 205 |
|
| 206 |
procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id); |
| 207 |
-- Expand declarations required for accept statement. See bodies of both |
| 208 |
-- Expand_Accept_Declarations and Expand_N_Accept_Statement for full |
| 209 |
-- details of the nature and use of these declarations, which are inserted |
| 210 |
-- immediately before the accept node N. The second argument is the entity |
| 211 |
-- for the corresponding entry. |
| 212 |
|
| 213 |
procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id); |
| 214 |
-- Expand the entry barrier into a function. This is called directly |
| 215 |
-- from Analyze_Entry_Body so that the discriminals and privals of the |
| 216 |
-- barrier can be attached to the function declaration list, and a new |
| 217 |
-- set prepared for the entry body procedure, before the entry body |
| 218 |
-- statement sequence can be expanded. The resulting function is analyzed |
| 219 |
-- now, within the context of the protected object, to resolve calls to |
| 220 |
-- other protected functions. |
| 221 |
|
| 222 |
procedure Expand_N_Abort_Statement (N : Node_Id); |
| 223 |
procedure Expand_N_Accept_Statement (N : Node_Id); |
| 224 |
procedure Expand_N_Asynchronous_Select (N : Node_Id); |
| 225 |
procedure Expand_N_Conditional_Entry_Call (N : Node_Id); |
| 226 |
procedure Expand_N_Delay_Relative_Statement (N : Node_Id); |
| 227 |
procedure Expand_N_Delay_Until_Statement (N : Node_Id); |
| 228 |
procedure Expand_N_Entry_Body (N : Node_Id); |
| 229 |
procedure Expand_N_Entry_Call_Statement (N : Node_Id); |
| 230 |
procedure Expand_N_Entry_Declaration (N : Node_Id); |
| 231 |
procedure Expand_N_Protected_Body (N : Node_Id); |
| 232 |
|
| 233 |
procedure Expand_N_Protected_Type_Declaration (N : Node_Id); |
| 234 |
-- Expands protected type declarations. This results, among other things, |
| 235 |
-- in the declaration of a record type for the representation of protected |
| 236 |
-- objects and (if there are entries) in an entry service procedure. The |
| 237 |
-- Protection value used by the GNARL to control the object will always be |
| 238 |
-- the first field of the record, and the entry service procedure spec (if |
| 239 |
-- it exists) will always immediately follow the record declaration. This |
| 240 |
-- allows these two nodes to be found from the type, without benefit of |
| 241 |
-- further attributes, using Corresponding_Record. |
| 242 |
|
| 243 |
procedure Expand_N_Requeue_Statement (N : Node_Id); |
| 244 |
procedure Expand_N_Selective_Accept (N : Node_Id); |
| 245 |
procedure Expand_N_Single_Protected_Declaration (N : Node_Id); |
| 246 |
procedure Expand_N_Single_Task_Declaration (N : Node_Id); |
| 247 |
procedure Expand_N_Task_Body (N : Node_Id); |
| 248 |
procedure Expand_N_Task_Type_Declaration (N : Node_Id); |
| 249 |
procedure Expand_N_Timed_Entry_Call (N : Node_Id); |
| 250 |
|
| 251 |
procedure Expand_Protected_Body_Declarations |
| 252 |
(N : Node_Id; |
| 253 |
Spec_Id : Entity_Id); |
| 254 |
-- Expand declarations required for a protected body. See bodies of both |
| 255 |
-- Expand_Protected_Body_Declarations and Expand_N_Protected_Body for full |
| 256 |
-- details of the nature and use of these declarations. The second argument |
| 257 |
-- is the entity for the corresponding protected type declaration. |
| 258 |
|
| 259 |
function External_Subprogram (E : Entity_Id) return Entity_Id; |
| 260 |
-- Return the external version of a protected operation, which locks |
| 261 |
-- the object before invoking the internal protected subprogram body. |
| 262 |
|
| 263 |
function Find_Master_Scope (E : Entity_Id) return Entity_Id; |
| 264 |
-- When a type includes tasks, a master entity is created in the scope, to |
| 265 |
-- be used by the runtime during activation. In general the master is the |
| 266 |
-- immediate scope in which the type is declared, but in Ada 2005, in the |
| 267 |
-- presence of synchronized classwide interfaces, the immediate scope of |
| 268 |
-- an anonymous access type may be a transient scope, which has no run-time |
| 269 |
-- presence. In this case, the scope of the master is the innermost scope |
| 270 |
-- that comes from source. |
| 271 |
|
| 272 |
function First_Protected_Operation (D : List_Id) return Node_Id; |
| 273 |
-- Given the declarations list for a protected body, find the |
| 274 |
-- first protected operation body. |
| 275 |
|
| 276 |
procedure Install_Private_Data_Declarations |
| 277 |
(Loc : Source_Ptr; |
| 278 |
Spec_Id : Entity_Id; |
| 279 |
Conc_Typ : Entity_Id; |
| 280 |
Body_Nod : Node_Id; |
| 281 |
Decls : List_Id; |
| 282 |
Barrier : Boolean := False; |
| 283 |
Family : Boolean := False); |
| 284 |
-- This routines generates several types, objects and object renamings used |
| 285 |
-- in the handling of discriminants and private components of protected and |
| 286 |
-- task types. It also generates the entry index for entry families. Formal |
| 287 |
-- Spec_Id denotes an entry, entry family or a subprogram, Conc_Typ is the |
| 288 |
-- concurrent type where Spec_Id resides, Body_Nod is the corresponding |
| 289 |
-- body of Spec_Id, Decls are the declarations of the subprogram or entry. |
| 290 |
-- Flag Barrier denotes whether the context is an entry barrier function. |
| 291 |
-- Flag Family is used in conjunction with Barrier to denote a barrier for |
| 292 |
-- an entry family. |
| 293 |
-- |
| 294 |
-- The generated types, entities and renamings are: |
| 295 |
-- |
| 296 |
-- * If flag Barrier is set or Spec_Id denotes a protected entry or an |
| 297 |
-- entry family, generate: |
| 298 |
-- |
| 299 |
-- type prot_typVP is access prot_typV; |
| 300 |
-- _object : prot_typVP := prot_typV (_O); |
| 301 |
-- |
| 302 |
-- where prot_typV is the corresponding record of a protected type and |
| 303 |
-- _O is a formal parameter representing the concurrent object of either |
| 304 |
-- the barrier function or the entry (family). |
| 305 |
-- |
| 306 |
-- * If Conc_Typ is a protected type, create a renaming for the Protection |
| 307 |
-- field _object: |
| 308 |
-- |
| 309 |
-- conc_typR : protection_typ renames _object._object; |
| 310 |
-- |
| 311 |
-- * If Conc_Typ has discriminants, create renamings of the form: |
| 312 |
-- |
| 313 |
-- discr_nameD : discr_typ renames _object.discr_name; |
| 314 |
-- or |
| 315 |
-- discr_nameD : discr_typ renames _task.discr_name; |
| 316 |
-- |
| 317 |
-- * If Conc_Typ denotes a protected type and has private components, |
| 318 |
-- generate renamings of the form: |
| 319 |
-- |
| 320 |
-- comp_name : comp_typ renames _object.comp_name; |
| 321 |
-- |
| 322 |
-- * Finally, is flag Barrier and Family are set or Spec_Id denotes an |
| 323 |
-- entry family, generate the entry index constant: |
| 324 |
-- |
| 325 |
-- subtype Jnn is <Type of Index> range Low .. High; |
| 326 |
-- J : constant Jnn := |
| 327 |
-- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First)); |
| 328 |
-- |
| 329 |
-- All the above declarations are inserted in the order shown to the front |
| 330 |
-- of Decls. |
| 331 |
|
| 332 |
function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id; |
| 333 |
-- Given the entity of the record type created for a task type, build |
| 334 |
-- the call to Create_Task |
| 335 |
|
| 336 |
function Make_Initialize_Protection |
| 337 |
(Protect_Rec : Entity_Id) return List_Id; |
| 338 |
-- Given the entity of the record type created for a protected type, build |
| 339 |
-- a list of statements needed for proper initialization of the object. |
| 340 |
|
| 341 |
function Next_Protected_Operation (N : Node_Id) return Node_Id; |
| 342 |
-- Given a protected operation node (a subprogram or entry body), find the |
| 343 |
-- following node in the declarations list. |
| 344 |
|
| 345 |
procedure Set_Discriminals (Dec : Node_Id); |
| 346 |
-- Replace discriminals in a protected type for use by the next protected |
| 347 |
-- operation on the type. Each operation needs a new set of discriminals, |
| 348 |
-- since it needs a unique renaming of the discriminant fields in the |
| 349 |
-- record used to implement the protected type. |
| 350 |
|
| 351 |
end Exp_Ch9; |