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-2020, 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
28with Types; use Types;
29
30package 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      Args    : 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 (Args);
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   procedure Build_Task_Allocate_Block_With_Init_Stmts
154     (Actions    : List_Id;
155      N          : Node_Id;
156      Init_Stmts : List_Id);
157   --  Ada 2005 (AI-287): Similar to previous routine, but used to expand
158   --  allocated aggregates with default initialized components. Init_Stmts
159   --  contains the list of statements required to initialize the allocated
160   --  aggregate. It replaces the call to Init (Args) done by
161   --  Build_Task_Allocate_Block. Also used to expand allocators containing
162   --  build-in-place function calls.
163
164   function Build_Wrapper_Spec
165     (Subp_Id : Entity_Id;
166      Obj_Typ : Entity_Id;
167      Formals : List_Id) return Node_Id;
168   --  Ada 2005 (AI-345): Build the specification of a primitive operation
169   --  associated with a protected or task type. This is required to implement
170   --  dispatching calls through interfaces. Subp_Id is the primitive to be
171   --  wrapped, Obj_Typ is the type of the newly added formal parameter to
172   --  handle object notation, Formals are the original entry formals that
173   --  will be explicitly replicated.
174
175   function Concurrent_Ref (N : Node_Id) return Node_Id;
176   --  Given the name of a concurrent object (task or protected object), or
177   --  the name of an access to a concurrent object, this function returns an
178   --  expression referencing the associated Task_Id or Protection object,
179   --  respectively. Note that a special case is when the name is a reference
180   --  to a task type name. This can only happen within a task body, and the
181   --  meaning is to get the Task_Id for the currently executing task.
182
183   function Convert_Concurrent
184     (N   : Node_Id;
185      Typ : Entity_Id) return Node_Id;
186   --  N is an expression of type Typ. If the type is not a concurrent type
187   --  then it is returned unchanged. If it is a task or protected reference,
188   --  Convert_Concurrent creates an unchecked conversion node from this
189   --  expression to the corresponding concurrent record type value. We need
190   --  this in any situation where the concurrent type is used, because the
191   --  actual concurrent object is an object of the corresponding concurrent
192   --  type, and manipulations on the concurrent object actually manipulate the
193   --  corresponding object of the record type.
194
195   function Entry_Index_Expression
196     (Sloc  : Source_Ptr;
197      Ent   : Entity_Id;
198      Index : Node_Id;
199      Ttyp  : Entity_Id)
200      return  Node_Id;
201   --  Returns an expression to compute a task entry index given the name of
202   --  the entry or entry family. For the case of a task entry family, the
203   --  Index parameter contains the expression for the subscript. Ttyp is the
204   --  task type.
205
206   procedure Establish_Task_Master (N : Node_Id);
207   --  Given a subprogram body, or a block statement, or a task body, this
208   --  procedure makes the necessary transformations required of a task master
209   --  (add Enter_Master call at start, and establish a cleanup routine to make
210   --  sure Complete_Master is called on exit).
211
212   procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id);
213   --  Build Equivalent_Type for an Access_To_Protected_Subprogram.
214   --  Equivalent_Type is a record type with two components: a pointer to the
215   --  protected object, and a pointer to the operation itself.
216
217   procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id);
218   --  Expand declarations required for accept statement. See bodies of both
219   --  Expand_Accept_Declarations and Expand_N_Accept_Statement for full
220   --  details of the nature and use of these declarations, which are inserted
221   --  immediately before the accept node N. The second argument is the entity
222   --  for the corresponding entry.
223
224   procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id);
225   --  Expand the entry barrier into a function. This is called directly
226   --  from Analyze_Entry_Body so that the discriminals and privals of the
227   --  barrier can be attached to the function declaration list, and a new
228   --  set prepared for the entry body procedure, before the entry body
229   --  statement sequence can be expanded. The resulting function is analyzed
230   --  now, within the context of the protected object, to resolve calls to
231   --  other protected functions.
232
233   procedure Expand_N_Abort_Statement            (N : Node_Id);
234   procedure Expand_N_Accept_Statement           (N : Node_Id);
235   procedure Expand_N_Asynchronous_Select        (N : Node_Id);
236   procedure Expand_N_Conditional_Entry_Call     (N : Node_Id);
237   procedure Expand_N_Delay_Relative_Statement   (N : Node_Id);
238   procedure Expand_N_Delay_Until_Statement      (N : Node_Id);
239   procedure Expand_N_Entry_Body                 (N : Node_Id);
240   procedure Expand_N_Entry_Call_Statement       (N : Node_Id);
241   procedure Expand_N_Entry_Declaration          (N : Node_Id);
242   procedure Expand_N_Protected_Body             (N : Node_Id);
243
244   procedure Expand_N_Protected_Type_Declaration (N : Node_Id);
245   --  Expands protected type declarations. This results, among other things,
246   --  in the declaration of a record type for the representation of protected
247   --  objects and (if there are entries) in an entry service procedure. The
248   --  Protection value used by the GNARL to control the object will always be
249   --  the first field of the record, and the entry service procedure spec (if
250   --  it exists) will always immediately follow the record declaration. This
251   --  allows these two nodes to be found from the type, without benefit of
252   --  further attributes, using Corresponding_Record.
253
254   procedure Expand_N_Requeue_Statement            (N : Node_Id);
255   procedure Expand_N_Selective_Accept             (N : Node_Id);
256   procedure Expand_N_Single_Protected_Declaration (N : Node_Id);
257   procedure Expand_N_Single_Task_Declaration      (N : Node_Id);
258   procedure Expand_N_Task_Body                    (N : Node_Id);
259   procedure Expand_N_Task_Type_Declaration        (N : Node_Id);
260   procedure Expand_N_Timed_Entry_Call             (N : Node_Id);
261
262   procedure Expand_Protected_Body_Declarations
263     (N       : Node_Id;
264      Spec_Id : Entity_Id);
265   --  Expand declarations required for a protected body. See bodies of both
266   --  Expand_Protected_Body_Declarations and Expand_N_Protected_Body for full
267   --  details of the nature and use of these declarations. The second argument
268   --  is the entity for the corresponding protected type declaration.
269
270   function External_Subprogram (E : Entity_Id) return Entity_Id;
271   --  Return the external version of a protected operation, which locks
272   --  the object before invoking the internal protected subprogram body.
273
274   function Find_Master_Scope (E : Entity_Id) return Entity_Id;
275   --  When a type includes tasks, a master entity is created in the scope, to
276   --  be used by the runtime during activation. In general the master is the
277   --  immediate scope in which the type is declared, but in Ada 2005, in the
278   --  presence of synchronized classwide interfaces, the immediate scope of
279   --  an anonymous access type may be a transient scope, which has no run-time
280   --  presence. In this case, the scope of the master is the innermost scope
281   --  that comes from source.
282
283   function First_Protected_Operation (D : List_Id) return Node_Id;
284   --  Given the declarations list for a protected body, find the
285   --  first protected operation body.
286
287   procedure Install_Private_Data_Declarations
288     (Loc      : Source_Ptr;
289      Spec_Id  : Entity_Id;
290      Conc_Typ : Entity_Id;
291      Body_Nod : Node_Id;
292      Decls    : List_Id;
293      Barrier  : Boolean := False;
294      Family   : Boolean := False);
295   --  This routines generates several types, objects and object renamings used
296   --  in the handling of discriminants and private components of protected and
297   --  task types. It also generates the entry index for entry families. Formal
298   --  Spec_Id denotes an entry, entry family or a subprogram, Conc_Typ is the
299   --  concurrent type where Spec_Id resides, Body_Nod is the corresponding
300   --  body of Spec_Id, Decls are the declarations of the subprogram or entry.
301   --  Flag Barrier denotes whether the context is an entry barrier function.
302   --  Flag Family is used in conjunction with Barrier to denote a barrier for
303   --  an entry family.
304   --
305   --  The generated types, entities and renamings are:
306   --
307   --  * If flag Barrier is set or Spec_Id denotes a protected entry or an
308   --    entry family, generate:
309   --
310   --      type prot_typVP is access prot_typV;
311   --      _object : prot_typVP := prot_typV (_O);
312   --
313   --    where prot_typV is the corresponding record of a protected type and
314   --    _O is a formal parameter representing the concurrent object of either
315   --    the barrier function or the entry (family).
316   --
317   --  * If Conc_Typ is a protected type, create a renaming for the Protection
318   --    field _object:
319   --
320   --      conc_typR : protection_typ renames _object._object;
321   --
322   --  * If Conc_Typ has discriminants, create renamings of the form:
323   --
324   --      discr_nameD : discr_typ renames _object.discr_name;
325   --        or
326   --      discr_nameD : discr_typ renames _task.discr_name;
327   --
328   --  * If Conc_Typ denotes a protected type and has private components,
329   --    generate renamings of the form:
330   --
331   --      comp_name : comp_typ renames _object.comp_name;
332   --
333   --  * Finally, is flag Barrier and Family are set or Spec_Id denotes an
334   --    entry family, generate the entry index constant:
335   --
336   --      subtype Jnn is <Type of Index> range Low .. High;
337   --      J : constant Jnn :=
338   --            Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
339   --
340   --  All the above declarations are inserted in the order shown to the front
341   --  of Decls.
342
343   function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id;
344   --  Given the entity of the record type created for a task type, build
345   --  the call to Create_Task
346
347   function Make_Initialize_Protection
348     (Protect_Rec : Entity_Id) return List_Id;
349   --  Given the entity of the record type created for a protected type, build
350   --  a list of statements needed for proper initialization of the object.
351
352   function Next_Protected_Operation (N : Node_Id) return Node_Id;
353   --  Given a protected operation node (a subprogram or entry body), find the
354   --  following node in the declarations list.
355
356   procedure Set_Discriminals (Dec : Node_Id);
357   --  Replace discriminals in a protected type for use by the next protected
358   --  operation on the type. Each operation needs a new set of discriminals,
359   --  since it needs a unique renaming of the discriminant fields in the
360   --  record used to implement the protected type.
361
362end Exp_Ch9;
363