OSDN Git Service

2007-04-20 Vincent Celier <celier@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-2007, 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 2,  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 COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 --  Expand routines for chapter 9 constructs
28
29 with Namet; use Namet;
30 with Types; use Types;
31
32 package Exp_Ch9 is
33
34    type Subprogram_Protection_Mode is
35      (Dispatching_Mode,
36       Protected_Mode,
37       Unprotected_Mode);
38    --  This type is used to distinguish the different protection modes of a
39    --  protected subprogram.
40
41    procedure Add_Discriminal_Declarations
42      (Decls : List_Id;
43       Typ   : Entity_Id;
44       Name  : Name_Id;
45       Loc   : Source_Ptr);
46    --  This routine is used to add discriminal declarations to task and
47    --  protected operation bodies. The discriminants are available by normal
48    --  selection from the concurrent object (whose name is passed as the third
49    --  parameter). Discriminant references inside the body have already
50    --  been replaced by references to the corresponding discriminals. The
51    --  declarations constructed by this procedure hook the references up with
52    --  the objects:
53    --
54    --    discriminal_name : discr_type renames name.discriminant_name;
55    --
56    --  Obviously we could have expanded the discriminant references in the
57    --  first place to be the appropriate selection, but this turns out to
58    --  be hard to do because it would introduce difference in handling of
59    --  discriminant references depending on their location.
60
61    procedure Add_Private_Declarations
62      (Decls : List_Id;
63       Typ   : Entity_Id;
64       Name  : Name_Id;
65       Loc : Source_Ptr);
66    --  This routine is used to add private declarations to protected bodies.
67    --  These are analogous to the discriminal declarations added to tasks
68    --  and protected operations, and consist of a renaming of each private
69    --  object to a selection from the concurrent object passed as an extra
70    --  parameter to each such operation:
71    --    private_name : private_type renames name.private_name;
72    --  As with discriminals, private references inside the protected
73    --  subprogram bodies have already been replaced by references to the
74    --  corresponding privals.
75
76    procedure Build_Activation_Chain_Entity (N : Node_Id);
77    --  Given a declaration N of an object that is a task, or contains tasks
78    --  (other than allocators to tasks) this routine ensures that an activation
79    --  chain has been declared in the appropriate scope, building the required
80    --  declaration for the chain variable if not. The name of this variable
81    --  is always _Chain and it is accessed by name.
82
83    function Build_Call_With_Task (N : Node_Id; E : Entity_Id) return Node_Id;
84    --  N is a node representing the name of a task or an access to a task.
85    --  The value returned is a call to the function whose name is the entity
86    --  E (typically a runtime routine entity obtained using RTE) with the
87    --  Task_Id of the associated task as the parameter. The caller is
88    --  responsible for analyzing and resolving the resulting tree.
89
90    function Build_Corresponding_Record
91      (N    : Node_Id;
92       Ctyp : Node_Id;
93       Loc  : Source_Ptr) return Node_Id;
94    --  Common to tasks and protected types. Copy discriminant specifications,
95    --  build record declaration. N is the type declaration, Ctyp is the
96    --  concurrent entity (task type or protected type).
97
98    procedure Build_Master_Entity (E : Entity_Id);
99    --  Given an entity E for the declaration of an object containing tasks
100    --  or of a type declaration for an allocator whose designated type is a
101    --  task or contains tasks, this routine marks the appropriate enclosing
102    --  context as a master, and also declares a variable called _Master in
103    --  the current declarative part which captures the value of Current_Master
104    --  (if not already built by a prior call). We build this object (instead
105    --  of just calling Current_Master) for two reasons. First it is clearly
106    --  more efficient to call Current_Master only once for a bunch of tasks
107    --  in the same declarative part, and second it makes things easier in
108    --  generating the initialization routines, since they can just reference
109    --  the object _Master by name, and they will get the proper Current_Master
110    --  value at the outer level, and copy in the parameter value for the outer
111    --  initialization call if the call is for a nested component). Note that
112    --  in the case of nested packages, we only really need to make one such
113    --  object at the outer level, but it is much easier to generate one per
114    --  declarative part.
115
116    function Build_Protected_Sub_Specification
117      (N       : Node_Id;
118       Prottyp : Entity_Id;
119       Mode    : Subprogram_Protection_Mode) return Node_Id;
120    --  Build specification for protected subprogram. This is called when
121    --  expanding a protected type, and also when expanding the declaration for
122    --  an Access_To_Protected_Subprogram type. In the latter case, Prottyp is
123    --  empty, and the first parameter of the signature of the protected op is
124    --  of type System.Address.
125
126    procedure Build_Protected_Subprogram_Call
127      (N        : Node_Id;
128       Name     : Node_Id;
129       Rec      : Node_Id;
130       External : Boolean := True);
131    --  The node N is a subprogram or entry call to a protected subprogram.
132    --  This procedure rewrites this call with the appropriate expansion.
133    --  Name is the subprogram, and Rec is the record corresponding to the
134    --  protected object. External is False if the call is to another
135    --  protected subprogram within the same object.
136
137    procedure Build_Task_Activation_Call (N : Node_Id);
138    --  This procedure is called for constructs that can be task activators
139    --  i.e. task bodies, subprogram bodies, package bodies and blocks. If
140    --  the construct is a task activator (as indicated by the non-empty
141    --  setting of Activation_Chain_Entity, either in the construct, or, in
142    --  the case of a package body, in its associated package spec), then
143    --  a call to Activate_Tasks with this entity as the single parameter
144    --  is inserted at the start of the statements of the activator.
145
146    procedure Build_Task_Allocate_Block
147      (Actions : List_Id;
148       N       : Node_Id;
149       Args    : List_Id);
150    --  This routine is used in the case of allocators where the designated
151    --  type is a task or contains tasks. In this case, the normal initialize
152    --  call is replaced by:
153    --
154    --    blockname : label;
155    --    blockname : declare
156    --       _Chain  : Activation_Chain;
157    --
158    --       procedure _Expunge is
159    --       begin
160    --         Expunge_Unactivated_Tasks (_Chain);
161    --       end;
162    --
163    --    begin
164    --       Init (Args);
165    --       Activate_Tasks (_Chain);
166    --    at end
167    --       _Expunge;
168    --    end;
169    --
170    --  to get the task or tasks created and initialized. The expunge call
171    --  ensures that any tasks that get created but not activated due to an
172    --  exception are properly expunged (it has no effect in the normal case)
173    --  The argument N is the allocator, and Args is the list of arguments
174    --  for the initialization call, constructed by the caller, which uses
175    --  the Master_Id of the access type as the _Master parameter, and _Chain
176    --  (defined above) as the _Chain parameter.
177
178    procedure Build_Task_Allocate_Block_With_Init_Stmts
179      (Actions    : List_Id;
180       N          : Node_Id;
181       Init_Stmts : List_Id);
182    --  Ada 2005 (AI-287): Similar to previous routine, but used to expand
183    --  allocated aggregates with default initialized components. Init_Stmts
184    --  contains the list of statements required to initialize the allocated
185    --  aggregate. It replaces the call to Init (Args) done by
186    --  Build_Task_Allocate_Block.
187
188    function Concurrent_Ref (N : Node_Id) return Node_Id;
189    --  Given the name of a concurrent object (task or protected object), or
190    --  the name of an access to a concurrent object, this function returns an
191    --  expression referencing the associated Task_Id or Protection object,
192    --  respectively. Note that a special case is when the name is a reference
193    --  to a task type name. This can only happen within a task body, and the
194    --  meaning is to get the Task_Id for the currently executing task.
195
196    function Convert_Concurrent
197      (N    : Node_Id;
198       Typ  : Entity_Id)
199       return Node_Id;
200    --  N is an expression of type Typ. If the type is not a concurrent
201    --  type then it is returned unchanged. If it is a task or protected
202    --  reference, Convert_Concurrent creates an unchecked conversion node
203    --  from this expression to the corresponding concurrent record type
204    --  value. We need this in any situation where the concurrent type is
205    --  used, because the actual concurrent object is an object of the
206    --  corresponding concurrent type, and manipulations on the concurrent
207    --  object actually manipulate the corresponding object of the record
208    --  type.
209
210    function Entry_Index_Expression
211      (Sloc  : Source_Ptr;
212       Ent   : Entity_Id;
213       Index : Node_Id;
214       Ttyp  : Entity_Id)
215       return  Node_Id;
216    --  Returns an expression to compute a task entry index given the name
217    --  of the entry or entry family. For the case of a task entry family,
218    --  the Index parameter contains the expression for the subscript.
219    --  Ttyp is the task type.
220
221    procedure Establish_Task_Master (N : Node_Id);
222    --  Given a subprogram body, or a block statement, or a task body, this
223    --  proccedure makes the necessary transformations required of a task
224    --  master (add Enter_Master call at start, and establish a cleanup
225    --  routine to make sure Complete_Master is called on exit).
226
227    procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id);
228    --  Build Equivalent_Type for an Access_to_protected_Subprogram
229
230    procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id);
231    --  Expand declarations required for accept statement. See bodies of
232    --  both Expand_Accept_Declarations and Expand_N_Accept_Statement for
233    --  full details of the nature and use of these declarations, which
234    --  are inserted immediately before the accept node N. The second
235    --  argument is the entity for the corresponding entry.
236
237    procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id);
238    --  Expand the entry barrier into a function. This is called directly
239    --  from Analyze_Entry_Body so that the discriminals and privals of the
240    --  barrier can be attached to the function declaration list, and a new
241    --  set prepared for the entry body procedure, bedore the entry body
242    --  statement sequence can be expanded. The resulting function is analyzed
243    --  now, within the context of the protected object, to resolve calls to
244    --  other protected functions.
245
246    procedure Expand_Entry_Body_Declarations (N : Node_Id);
247    --  Expand declarations required for the expansion of the
248    --  statements of the body.
249
250    procedure Expand_N_Abort_Statement            (N : Node_Id);
251    procedure Expand_N_Accept_Statement           (N : Node_Id);
252    procedure Expand_N_Asynchronous_Select        (N : Node_Id);
253    procedure Expand_N_Conditional_Entry_Call     (N : Node_Id);
254    procedure Expand_N_Delay_Relative_Statement   (N : Node_Id);
255    procedure Expand_N_Delay_Until_Statement      (N : Node_Id);
256    procedure Expand_N_Entry_Body                 (N : Node_Id);
257    procedure Expand_N_Entry_Call_Statement       (N : Node_Id);
258    procedure Expand_N_Entry_Declaration          (N : Node_Id);
259    procedure Expand_N_Protected_Body             (N : Node_Id);
260
261    procedure Expand_N_Protected_Type_Declaration (N : Node_Id);
262    --  Expands protected type declarations. This results, among other things,
263    --  in the declaration of a record type for the representation of protected
264    --  objects and (if there are entries) in an entry service procedure. The
265    --  Protection value used by the GNARL to control the object will always be
266    --  the first field of the record, and the entry service procedure spec (if
267    --  it exists) will always immediately follow the record declaration. This
268    --  allows these two nodes to be found from the type, without benefit of
269    --  further attributes, using Corresponding_Record.
270
271    procedure Expand_N_Requeue_Statement          (N : Node_Id);
272    procedure Expand_N_Selective_Accept           (N : Node_Id);
273    procedure Expand_N_Single_Task_Declaration    (N : Node_Id);
274    procedure Expand_N_Task_Body                  (N : Node_Id);
275    procedure Expand_N_Task_Type_Declaration      (N : Node_Id);
276    procedure Expand_N_Timed_Entry_Call           (N : Node_Id);
277
278    procedure Expand_Protected_Body_Declarations
279      (N       : Node_Id;
280       Spec_Id : Entity_Id);
281    --  Expand declarations required for a protected body. See bodies of
282    --  both Expand_Protected_Body_Declarations and Expand_N_Protected_Body
283    --  for full details of the nature and use of these declarations.
284    --  The second argument is the entity for the corresponding
285    --  protected type declaration.
286
287    function External_Subprogram (E : Entity_Id) return Entity_Id;
288    --  return the external version of a protected operation, which locks
289    --  the object before invoking the internal protected subprogram body.
290
291    function First_Protected_Operation (D : List_Id) return Node_Id;
292    --  Given the declarations list for a protected body, find the
293    --  first protected operation body.
294
295    function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id;
296    --  Given the entity of the record type created for a task type, build
297    --  the call to Create_Task
298
299    function Make_Initialize_Protection
300      (Protect_Rec : Entity_Id)
301       return        List_Id;
302    --  Given the entity of the record type created for a protected type, build
303    --  a list of statements needed for proper initialization of the object.
304
305    function Next_Protected_Operation (N : Node_Id) return Node_Id;
306    --  Given a protected operation node (a subprogram or entry body),
307    --  find the following node in the declarations list.
308
309    procedure Set_Discriminals (Dec : Node_Id);
310    --  Replace discriminals in a protected type for use by the
311    --  next protected operation on the type. Each operation needs a
312    --  new set of discirminals, since it needs a unique renaming of
313    --  the discriminant fields in the record used to implement the
314    --  protected type.
315
316    procedure Set_Privals
317       (Dec           : Node_Id;
318        Op            : Node_Id;
319        Loc           : Source_Ptr;
320        After_Barrier : Boolean := False);
321    --  Associates a new set of privals (placeholders for later access to
322    --  private components of protected objects) with the private object
323    --  declarations of a protected object. These will be used to expand
324    --  the references to private objects in the next protected
325    --  subprogram or entry body to be expanded.
326    --
327    --  The flag After_Barrier indicates whether this is called after building
328    --  the barrier function for an entry body. This flag determines whether
329    --  the privals should have source names (which simplifies debugging) or
330    --  internally generated names. Entry barriers contain no debuggable code,
331    --  and there may be visibility conflicts between an entry index and a
332    --  a prival, so  privals for barrier function have internal names.
333
334 end Exp_Ch9;