OSDN Git Service

2008-04-08 Hristian Kirtchev <kirtchev@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / exp_ch9.ads
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-2008, 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    function Build_Corresponding_Record
54      (N    : Node_Id;
55       Ctyp : Node_Id;
56       Loc  : Source_Ptr) return Node_Id;
57    --  Common to tasks and protected types. Copy discriminant specifications,
58    --  build record declaration. N is the type declaration, Ctyp is the
59    --  concurrent entity (task type or protected type).
60
61    procedure Build_Master_Entity (E : Entity_Id);
62    --  Given an entity E for the declaration of an object containing tasks
63    --  or of a type declaration for an allocator whose designated type is a
64    --  task or contains tasks, this routine marks the appropriate enclosing
65    --  context as a master, and also declares a variable called _Master in
66    --  the current declarative part which captures the value of Current_Master
67    --  (if not already built by a prior call). We build this object (instead
68    --  of just calling Current_Master) for two reasons. First it is clearly
69    --  more efficient to call Current_Master only once for a bunch of tasks
70    --  in the same declarative part, and second it makes things easier in
71    --  generating the initialization routines, since they can just reference
72    --  the object _Master by name, and they will get the proper Current_Master
73    --  value at the outer level, and copy in the parameter value for the outer
74    --  initialization call if the call is for a nested component). Note that
75    --  in the case of nested packages, we only really need to make one such
76    --  object at the outer level, but it is much easier to generate one per
77    --  declarative part.
78
79    function Build_Protected_Sub_Specification
80      (N        : Node_Id;
81       Prot_Typ : Entity_Id;
82       Mode     : Subprogram_Protection_Mode) return Node_Id;
83    --  Build the specification for protected subprogram. This is called when
84    --  expanding a protected type, and also when expanding the declaration for
85    --  an Access_To_Protected_Subprogram type. In the latter case, Prot_Typ is
86    --  empty, and the first parameter of the signature of the protected op is
87    --  of type System.Address.
88
89    procedure Build_Protected_Subprogram_Call
90      (N        : Node_Id;
91       Name     : Node_Id;
92       Rec      : Node_Id;
93       External : Boolean := True);
94    --  The node N is a subprogram or entry call to a protected subprogram.
95    --  This procedure rewrites this call with the appropriate expansion.
96    --  Name is the subprogram, and Rec is the record corresponding to the
97    --  protected object. External is False if the call is to another
98    --  protected subprogram within the same object.
99
100    procedure Build_Task_Activation_Call (N : Node_Id);
101    --  This procedure is called for constructs that can be task activators
102    --  i.e. task bodies, subprogram bodies, package bodies and blocks. If
103    --  the construct is a task activator (as indicated by the non-empty
104    --  setting of Activation_Chain_Entity, either in the construct, or, in
105    --  the case of a package body, in its associated package spec), then
106    --  a call to Activate_Tasks with this entity as the single parameter
107    --  is inserted at the start of the statements of the activator.
108
109    procedure Build_Task_Allocate_Block
110      (Actions : List_Id;
111       N       : Node_Id;
112       Args    : List_Id);
113    --  This routine is used in the case of allocators where the designated
114    --  type is a task or contains tasks. In this case, the normal initialize
115    --  call is replaced by:
116    --
117    --    blockname : label;
118    --    blockname : declare
119    --       _Chain  : Activation_Chain;
120    --
121    --       procedure _Expunge is
122    --       begin
123    --         Expunge_Unactivated_Tasks (_Chain);
124    --       end;
125    --
126    --    begin
127    --       Init (Args);
128    --       Activate_Tasks (_Chain);
129    --    at end
130    --       _Expunge;
131    --    end;
132    --
133    --  to get the task or tasks created and initialized. The expunge call
134    --  ensures that any tasks that get created but not activated due to an
135    --  exception are properly expunged (it has no effect in the normal case)
136    --  The argument N is the allocator, and Args is the list of arguments
137    --  for the initialization call, constructed by the caller, which uses
138    --  the Master_Id of the access type as the _Master parameter, and _Chain
139    --  (defined above) as the _Chain parameter.
140
141    procedure Build_Task_Allocate_Block_With_Init_Stmts
142      (Actions    : List_Id;
143       N          : Node_Id;
144       Init_Stmts : List_Id);
145    --  Ada 2005 (AI-287): Similar to previous routine, but used to expand
146    --  allocated aggregates with default initialized components. Init_Stmts
147    --  contains the list of statements required to initialize the allocated
148    --  aggregate. It replaces the call to Init (Args) done by
149    --  Build_Task_Allocate_Block.
150
151    function Concurrent_Ref (N : Node_Id) return Node_Id;
152    --  Given the name of a concurrent object (task or protected object), or
153    --  the name of an access to a concurrent object, this function returns an
154    --  expression referencing the associated Task_Id or Protection object,
155    --  respectively. Note that a special case is when the name is a reference
156    --  to a task type name. This can only happen within a task body, and the
157    --  meaning is to get the Task_Id for the currently executing task.
158
159    function Convert_Concurrent
160      (N    : Node_Id;
161       Typ  : Entity_Id)
162       return Node_Id;
163    --  N is an expression of type Typ. If the type is not a concurrent
164    --  type then it is returned unchanged. If it is a task or protected
165    --  reference, Convert_Concurrent creates an unchecked conversion node
166    --  from this expression to the corresponding concurrent record type
167    --  value. We need this in any situation where the concurrent type is
168    --  used, because the actual concurrent object is an object of the
169    --  corresponding concurrent type, and manipulations on the concurrent
170    --  object actually manipulate the corresponding object of the record
171    --  type.
172
173    function Entry_Index_Expression
174      (Sloc  : Source_Ptr;
175       Ent   : Entity_Id;
176       Index : Node_Id;
177       Ttyp  : Entity_Id)
178       return  Node_Id;
179    --  Returns an expression to compute a task entry index given the name
180    --  of the entry or entry family. For the case of a task entry family,
181    --  the Index parameter contains the expression for the subscript.
182    --  Ttyp is the task type.
183
184    procedure Establish_Task_Master (N : Node_Id);
185    --  Given a subprogram body, or a block statement, or a task body, this
186    --  procedure makes the necessary transformations required of a task
187    --  master (add Enter_Master call at start, and establish a cleanup
188    --  routine to make sure Complete_Master is called on exit).
189
190    procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id);
191    --  Build Equivalent_Type for an Access_to_protected_Subprogram
192
193    procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id);
194    --  Expand declarations required for accept statement. See bodies of
195    --  both Expand_Accept_Declarations and Expand_N_Accept_Statement for
196    --  full details of the nature and use of these declarations, which
197    --  are inserted immediately before the accept node N. The second
198    --  argument is the entity for the corresponding entry.
199
200    procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id);
201    --  Expand the entry barrier into a function. This is called directly
202    --  from Analyze_Entry_Body so that the discriminals and privals of the
203    --  barrier can be attached to the function declaration list, and a new
204    --  set prepared for the entry body procedure, before the entry body
205    --  statement sequence can be expanded. The resulting function is analyzed
206    --  now, within the context of the protected object, to resolve calls to
207    --  other protected functions.
208
209    procedure Expand_N_Abort_Statement            (N : Node_Id);
210    procedure Expand_N_Accept_Statement           (N : Node_Id);
211    procedure Expand_N_Asynchronous_Select        (N : Node_Id);
212    procedure Expand_N_Conditional_Entry_Call     (N : Node_Id);
213    procedure Expand_N_Delay_Relative_Statement   (N : Node_Id);
214    procedure Expand_N_Delay_Until_Statement      (N : Node_Id);
215    procedure Expand_N_Entry_Body                 (N : Node_Id);
216    procedure Expand_N_Entry_Call_Statement       (N : Node_Id);
217    procedure Expand_N_Entry_Declaration          (N : Node_Id);
218    procedure Expand_N_Protected_Body             (N : Node_Id);
219
220    procedure Expand_N_Protected_Type_Declaration (N : Node_Id);
221    --  Expands protected type declarations. This results, among other things,
222    --  in the declaration of a record type for the representation of protected
223    --  objects and (if there are entries) in an entry service procedure. The
224    --  Protection value used by the GNARL to control the object will always be
225    --  the first field of the record, and the entry service procedure spec (if
226    --  it exists) will always immediately follow the record declaration. This
227    --  allows these two nodes to be found from the type, without benefit of
228    --  further attributes, using Corresponding_Record.
229
230    procedure Expand_N_Requeue_Statement          (N : Node_Id);
231    procedure Expand_N_Selective_Accept           (N : Node_Id);
232    procedure Expand_N_Single_Task_Declaration    (N : Node_Id);
233    procedure Expand_N_Task_Body                  (N : Node_Id);
234    procedure Expand_N_Task_Type_Declaration      (N : Node_Id);
235    procedure Expand_N_Timed_Entry_Call           (N : Node_Id);
236
237    procedure Expand_Protected_Body_Declarations
238      (N       : Node_Id;
239       Spec_Id : Entity_Id);
240    --  Expand declarations required for a protected body. See bodies of both
241    --  Expand_Protected_Body_Declarations and Expand_N_Protected_Body for full
242    --  details of the nature and use of these declarations. The second argument
243    --  is the entity for the corresponding protected type declaration.
244
245    function External_Subprogram (E : Entity_Id) return Entity_Id;
246    --  return the external version of a protected operation, which locks
247    --  the object before invoking the internal protected subprogram body.
248
249    function First_Protected_Operation (D : List_Id) return Node_Id;
250    --  Given the declarations list for a protected body, find the
251    --  first protected operation body.
252
253    procedure Install_Private_Data_Declarations
254      (Loc      : Source_Ptr;
255       Spec_Id  : Entity_Id;
256       Conc_Typ : Entity_Id;
257       Body_Nod : Node_Id;
258       Decls    : List_Id;
259       Barrier  : Boolean := False;
260       Family   : Boolean := False);
261    --  This routines generates several types, objects and object renamings used
262    --  in the handling of discriminants and private components of protected and
263    --  task types. It also generates the entry index for entry families. Formal
264    --  Spec_Id denotes an entry, entry family or a subprogram, Conc_Typ is the
265    --  concurrent type where Spec_Id resides, Body_Nod is the corresponding
266    --  body of Spec_Id, Decls are the declarations of the subprogram or entry.
267    --  Flag Barrier denotes whether the context is an entry barrier function.
268    --  Flag Family is used in conjunction with Barrier to denote a barrier for
269    --  an entry family.
270    --
271    --  The generated types, entities and renamings are:
272    --
273    --  * If flag Barrier is set or Spec_Id denotes a protected entry or an
274    --    entry family, generate:
275    --
276    --      type prot_typVP is access prot_typV;
277    --      _object : prot_typVP := prot_typV (_O);
278    --
279    --    where prot_typV is the corresponding record of a protected type and
280    --    _O is a formal parameter representing the concurrent object of either
281    --    the barrier function or the entry (family).
282    --
283    --  * If Conc_Typ is a protected type, create a renaming for the Protection
284    --    field _object:
285    --
286    --      conc_typR : protection_typ renames _object._object;
287    --
288    --  * If Conc_Typ has discriminants, create renamings of the form:
289    --
290    --      discr_nameD : discr_typ renames _object.discr_name;
291    --        or
292    --      discr_nameD : discr_typ renames _task.discr_name;
293    --
294    --  * If Conc_Typ denotes a protected type and has private components,
295    --    generate renamings of the form:
296    --
297    --      comp_name : comp_typ renames _object.comp_name;
298    --
299    --  * Finally, is flag Barrier and Family are set or Spec_Id denotes an
300    --    entry family, generate the entry index constant:
301    --
302    --      subtype Jnn is <Type of Index> range Low .. High;
303    --      J : constant Jnn :=
304    --            Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
305    --
306    --  All the above declarations are inserted in the order shown to the front
307    --  of Decls.
308
309    function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id;
310    --  Given the entity of the record type created for a task type, build
311    --  the call to Create_Task
312
313    function Make_Initialize_Protection
314      (Protect_Rec : Entity_Id) return List_Id;
315    --  Given the entity of the record type created for a protected type, build
316    --  a list of statements needed for proper initialization of the object.
317
318    function Next_Protected_Operation (N : Node_Id) return Node_Id;
319    --  Given a protected operation node (a subprogram or entry body), find the
320    --  following node in the declarations list.
321
322    procedure Set_Discriminals (Dec : Node_Id);
323    --  Replace discriminals in a protected type for use by the next protected
324    --  operation on the type. Each operation needs a new set of discirminals,
325    --  since it needs a unique renaming of the discriminant fields in the
326    --  record used to implement the protected type.
327
328 end Exp_Ch9;