OSDN Git Service

2011-12-12 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / exp_disp.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             E X P _ D I S P                              --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2011, 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 --  This package contains routines involved in tagged types and dynamic
27 --  dispatching expansion.
28
29 with Types; use Types;
30
31 package Exp_Disp is
32
33    -------------------------------------
34    -- Predefined primitive operations --
35    -------------------------------------
36
37    --  The predefined primitive operations (PPOs) are subprograms generated
38    --  by GNAT for a particular tagged type. Their role is to provide support
39    --  for different Ada language features such as the attribute 'Size or
40    --  handling of dispatching triggers in select statements. PPOs are created
41    --  when a tagged type is expanded or frozen. These subprograms are later
42    --  collected and inserted into the dispatch table of a tagged type at
43    --  fixed positions. Some of the PPOs that manipulate data in tagged objects
44    --  require the generation of thunks.
45
46    --  List of predefined primitive operations
47
48    --    Leading underscores designate reserved names. Bracketed numerical
49    --    values represent dispatch table slot numbers.
50
51    --      _Size (1) - implementation of the attribute 'Size for any tagged
52    --      type. Constructs of the form Prefix'Size are converted into
53    --      Prefix._Size.
54
55    --      TSS_Stream_Read (2) - implementation of the stream attribute Read
56    --      for any tagged type.
57
58    --      TSS_Stream_Write (3) - implementation of the stream attribute Write
59    --      for any tagged type.
60
61    --      TSS_Stream_Input (4) - implementation of the stream attribute Input
62    --      for any tagged type.
63
64    --      TSS_Stream_Output (5) - implementation of the stream attribute
65    --      Output for any tagged type.
66
67    --      Op_Eq (6) - implementation of the equality operator for any non-
68    --      limited tagged type.
69
70    --      _Assign (7) - implementation of the assignment operator for any
71    --      non-limited tagged type.
72
73    --      TSS_Deep_Adjust (8) - implementation of the finalization operation
74    --      Adjust for any non-limited tagged type.
75
76    --      TSS_Deep_Finalize (9) - implementation of the finalization
77    --      operation Finalize for any non-limited tagged type.
78
79    --      _Disp_Asynchronous_Select (10) - used in the expansion of ATC with
80    --      dispatching triggers. Null implementation for limited interfaces,
81    --      full body generation for types that implement limited interfaces,
82    --      not generated for the rest of the cases. See Expand_N_Asynchronous_
83    --      Select in Exp_Ch9 for more information.
84
85    --      _Disp_Conditional_Select (11) - used in the expansion of conditional
86    --      selects with dispatching triggers. Null implementation for limited
87    --      interfaces, full body generation for types that implement limited
88    --      interfaces, not generated for the rest of the cases. See Expand_N_
89    --      Conditional_Entry_Call in Exp_Ch9 for more information.
90
91    --      _Disp_Get_Prim_Op_Kind (12) - helper routine used in the expansion
92    --      of ATC with dispatching triggers. Null implementation for limited
93    --      interfaces, full body generation for types that implement limited
94    --      interfaces, not generated for the rest of the cases.
95
96    --      _Disp_Get_Task_Id (13) - helper routine used in the expansion of
97    --      Abort, attributes 'Callable and 'Terminated for task interface
98    --      class-wide types. Full body generation for task types, null
99    --      implementation for limited interfaces, not generated for the rest
100    --      of the cases. See Expand_N_Attribute_Reference in Exp_Attr and
101    --      Expand_N_Abort_Statement in Exp_Ch9 for more information.
102
103    --      _Disp_Requeue (14) - used in the expansion of dispatching requeue
104    --      statements. Null implementation is provided for protected, task
105    --      and synchronized interfaces. Protected and task types implementing
106    --      concurrent interfaces receive full bodies. See Expand_N_Requeue_
107    --      Statement in Exp_Ch9 for more information.
108
109    --      _Disp_Timed_Select (15) - used in the expansion of timed selects
110    --      with dispatching triggers. Null implementation for limited
111    --      interfaces, full body generation for types that implement limited
112    --      interfaces, not generated for the rest of the cases. See Expand_N_
113    --      Timed_Entry_Call for more information.
114
115    --  Life cycle of predefined primitive operations
116
117    --      The specifications and bodies of the PPOs are created by
118    --      Make_Predefined_Primitive_Specs and Predefined_Primitive_Bodies
119    --      in Exp_Ch3. The generated specifications are immediately analyzed,
120    --      while the bodies are left as freeze actions to the tagged type for
121    --      which they are created.
122
123    --      PPOs are collected and added to the Primitive_Operations list of
124    --      a type by the regular analysis mechanism.
125
126    --      PPOs are frozen by Exp_Ch3.Predefined_Primitive_Freeze
127
128    --      Thunks for PPOs are created by Make_DT
129
130    --      Dispatch table positions of PPOs are set by Set_All_DT_Position
131
132    --      Calls to PPOs proceed as regular dispatching calls. If the PPO
133    --      has a thunk, a call proceeds as a regular dispatching call with
134    --      a thunk.
135
136    --  Guidelines for addition of new predefined primitive operations
137
138    --      Update the value of constant Max_Predef_Prims in a-tags.ads to
139    --      indicate the new number of PPOs.
140
141    --      Introduce a new predefined name for the new PPO in Snames.ads and
142    --      Snames.adb.
143
144    --      Categorize the new PPO name as predefined by adding an entry in
145    --      Is_Predefined_Dispatching_Operation in Exp_Disp.
146
147    --      Generate the specification of the new PPO in Make_Predefined_
148    --      Primitive_Spec in Exp_Ch3.adb. The Is_Internal flag of the defining
149    --      identifier of the specification must be set to True.
150
151    --      Generate the body of the new PPO in Predefined_Primitive_Bodies in
152    --      Exp_Ch3.adb. The Is_Internal flag of the defining identifier of the
153    --      specification must be set to True.
154
155    --      If the new PPO requires a thunk, add an entry in Freeze_Subprogram
156    --      in Exp_Ch6.adb.
157
158    --      When generating calls to a PPO, use Find_Prim_Op from Exp_Util.ads
159    --      to retrieve the entity of the operation directly.
160
161    --  Number of predefined primitive operations added by the Expander
162    --  for a tagged type. If more predefined primitive operations are
163    --  added, the following items must be changed:
164
165    --    Ada.Tags.Max_Predef_Prims         - indirect use
166    --    Exp_Disp.Default_Prim_Op_Position - indirect use
167    --    Exp_Disp.Set_All_DT_Position      - direct   use
168
169    procedure Apply_Tag_Checks (Call_Node : Node_Id);
170    --  Generate checks required on dispatching calls
171
172    function Building_Static_DT (Typ : Entity_Id) return Boolean;
173    pragma Inline (Building_Static_DT);
174    --  Returns true when building statically allocated dispatch tables
175
176    procedure Build_Static_Dispatch_Tables (N : Node_Id);
177    --  N is a library level package declaration or package body. Build the
178    --  static dispatch table of the tagged types defined at library level. In
179    --  case of package declarations with private part the generated nodes are
180    --  added at the end of the list of private declarations. Otherwise they are
181    --  added to the end of the list of public declarations. In case of package
182    --  bodies they are added to the end of the list of declarations of the
183    --  package body.
184
185    function Convert_Tag_To_Interface
186      (Typ : Entity_Id; Expr : Node_Id) return Node_Id;
187    pragma Inline (Convert_Tag_To_Interface);
188    --  This function is used in class-wide interface conversions; the expanded
189    --  code generated to convert a tagged object to a class-wide interface type
190    --  involves referencing the tag component containing the secondary dispatch
191    --  table associated with the interface. Given the expression Expr that
192    --  references a tag component, we cannot generate an unchecked conversion
193    --  to leave the expression decorated with the class-wide interface type Typ
194    --  because an unchecked conversion cannot be seen as a no-op. An unchecked
195    --  conversion is conceptually a function call and therefore the RM allows
196    --  the backend to obtain a copy of the value of the actual object and store
197    --  it in some other place (like a register); in such case the interface
198    --  conversion is not equivalent to a displacement of the pointer to the
199    --  interface and any further displacement fails. Although the functionality
200    --  of this function is simple and could be done directly, the purpose of
201    --  this routine is to leave well documented in the sources these
202    --  occurrences.
203
204    --  If Expr is an N_Selected_Component that references a tag generate:
205    --     type ityp is non null access Typ;
206    --     ityp!(Expr'Address).all
207
208    --  if Expr is an N_Function_Call to Ada.Tags.Displace then generate:
209    --     type ityp is non null access Typ;
210    --     ityp!(Expr).all
211
212    function CPP_Num_Prims (Typ : Entity_Id) return Nat;
213    --  Return the number of primitives of the C++ part of the dispatch table.
214    --  For types that are not derivations of CPP types return 0.
215
216    procedure Expand_Dispatching_Call (Call_Node : Node_Id);
217    --  Expand the call to the operation through the dispatch table and perform
218    --  the required tag checks when appropriate. For CPP types tag checks are
219    --  not relevant.
220
221    procedure Expand_Interface_Actuals (Call_Node : Node_Id);
222    --  Ada 2005 (AI-251): Displace all the actuals corresponding to class-wide
223    --  interfaces to reference the interface tag of the actual object
224
225    procedure Expand_Interface_Conversion
226      (N         : Node_Id;
227       Is_Static : Boolean := True);
228    --  Ada 2005 (AI-251): N is a type-conversion node. Reference the base of
229    --  the object to give access to the interface tag associated with the
230    --  secondary dispatch table.
231
232    procedure Expand_Interface_Thunk
233      (Prim       : Node_Id;
234       Thunk_Id   : out Entity_Id;
235       Thunk_Code : out Node_Id);
236    --  Ada 2005 (AI-251): When a tagged type implements abstract interfaces we
237    --  generate additional subprograms (thunks) associated with each primitive
238    --  Prim to have a layout compatible with the C++ ABI. The thunk displaces
239    --  the pointers to the actuals that depend on the controlling type before
240    --  transferring control to the target subprogram. If there is no need to
241    --  generate the thunk then Thunk_Id and Thunk_Code are set to Empty.
242    --  Otherwise they are set to the defining identifier and the subprogram
243    --  body of the generated thunk.
244
245    function Has_CPP_Constructors (Typ : Entity_Id) return Boolean;
246    --  Returns true if the type has CPP constructors
247
248    function Is_Predefined_Dispatching_Operation (E : Entity_Id) return Boolean;
249    --  Ada 2005 (AI-251): Determines if E is a predefined primitive operation
250
251    function Is_Predefined_Internal_Operation (E : Entity_Id) return Boolean;
252    --  Similar to the previous one, but excludes stream operations, because
253    --  these may be overridden, and need extra formals, like user-defined
254    --  operations.
255
256    function Is_Predefined_Interface_Primitive (E : Entity_Id) return Boolean;
257    --  Ada 2005 (AI-345): Returns True if E is one of the predefined primitives
258    --  required to implement interfaces.
259
260    function Make_DT (Typ : Entity_Id; N : Node_Id := Empty) return List_Id;
261    --  Expand the declarations for the Dispatch Table. The node N is the
262    --  declaration that forces the generation of the table. It is used to place
263    --  error messages when the declaration leads to the freezing of a given
264    --  primitive operation that has an incomplete non- tagged formal.
265
266    function Make_Disp_Asynchronous_Select_Body
267      (Typ : Entity_Id) return Node_Id;
268    --  Ada 2005 (AI-345): Generate the body of the primitive operation of type
269    --  Typ used for dispatching in asynchronous selects. Generate a null body
270    --  if Typ is an interface type.
271
272    function Make_Disp_Asynchronous_Select_Spec
273      (Typ : Entity_Id) return Node_Id;
274    --  Ada 2005 (AI-345): Generate the specification of the primitive operation
275    --  of type Typ used for dispatching in asynchronous selects.
276
277    function Make_Disp_Conditional_Select_Body
278      (Typ : Entity_Id) return Node_Id;
279    --  Ada 2005 (AI-345): Generate the body of the primitive operation of type
280    --  Typ used for dispatching in conditional selects. Generate a null body
281    --  if Typ is an interface type.
282
283    function Make_Disp_Conditional_Select_Spec
284      (Typ : Entity_Id) return Node_Id;
285    --  Ada 2005 (AI-345): Generate the specification of the primitive operation
286    --  of type Typ used for dispatching in conditional selects.
287
288    function Make_Disp_Get_Prim_Op_Kind_Body
289      (Typ : Entity_Id) return Node_Id;
290    --  Ada 2005 (AI-345): Generate the body of the primitive operation of type
291    --  Typ used for retrieving the callable entity kind during dispatching in
292    --  asynchronous selects. Generate a null body if Typ is an interface type.
293
294    function Make_Disp_Get_Prim_Op_Kind_Spec
295      (Typ : Entity_Id) return Node_Id;
296    --  Ada 2005 (AI-345): Generate the specification of the primitive operation
297    --  of the type Typ use for retrieving the callable entity kind during
298    --  dispatching in asynchronous selects.
299
300    function Make_Disp_Get_Task_Id_Body
301      (Typ : Entity_Id) return Node_Id;
302    --  Ada 2005 (AI-345): Generate body of the primitive operation of type Typ
303    --  used for retrieving the _task_id field of a task interface class- wide
304    --  type. Generate a null body if Typ is an interface or a non-task type.
305
306    function Make_Disp_Get_Task_Id_Spec
307      (Typ : Entity_Id) return Node_Id;
308    --  Ada 2005 (AI-345): Generate the specification of the primitive operation
309    --  of type Typ used for retrieving the _task_id field of a task interface
310    --  class-wide type.
311
312    function Make_Disp_Requeue_Body
313      (Typ : Entity_Id) return Node_Id;
314    --  Ada 2005 (AI05-0030): Generate the body of the primitive operation of
315    --  type Typ used for dispatching on requeue statements. Generate a body
316    --  containing a single null-statement if Typ is an interface type.
317
318    function Make_Disp_Requeue_Spec
319      (Typ : Entity_Id) return Node_Id;
320    --  Ada 2005 (AI05-0030): Generate the specification of the primitive
321    --  operation of type Typ used for dispatching requeue statements.
322
323    function Make_Disp_Timed_Select_Body
324      (Typ : Entity_Id) return Node_Id;
325    --  Ada 2005 (AI-345): Generate the body of the primitive operation of type
326    --  Typ used for dispatching in timed selects. Generate a body containing
327    --  a single null-statement if Typ is an interface type.
328
329    function Make_Disp_Timed_Select_Spec
330      (Typ : Entity_Id) return Node_Id;
331    --  Ada 2005 (AI-345): Generate the specification of the primitive operation
332    --  of type Typ used for dispatching in timed selects.
333
334    function Make_Select_Specific_Data_Table
335      (Typ : Entity_Id) return List_Id;
336    --  Ada 2005 (AI-345): Create and populate the auxiliary table in the TSD
337    --  of Typ used for dispatching in asynchronous, conditional and timed
338    --  selects. Generate code to set the primitive operation kinds and entry
339    --  indexes of primitive operations and primitive wrappers.
340
341    function Make_Tags (Typ : Entity_Id) return List_Id;
342    --  Generate the entities associated with the primary and secondary tags of
343    --  Typ and fill the contents of Access_Disp_Table. In case of library level
344    --  tagged types this routine imports the forward declaration of the tag
345    --  entity, that will be declared and exported by Make_DT.
346
347    function Make_VM_TSD (Typ : Entity_Id) return List_Id;
348    --  Build the Type Specific Data record associated with tagged type Typ.
349    --  Invoked only when generating code for VM targets.
350
351    function Register_Primitive
352      (Loc     : Source_Ptr;
353       Prim    : Entity_Id) return List_Id;
354    --  Build code to register Prim in the primary or secondary dispatch table.
355    --  If Prim is associated with a secondary dispatch table then generate also
356    --  its thunk and register it in the associated secondary dispatch table.
357    --  In general the dispatch tables are always generated by Make_DT and
358    --  Make_Secondary_DT; this routine is only used in two corner cases:
359    --
360    --    1) To construct the dispatch table of a tagged type whose parent
361    --       is a CPP_Class (see Build_Init_Procedure).
362    --    2) To handle late overriding of dispatching operations (see
363    --       Check_Dispatching_Operation and Make_DT).
364    --
365    --  The caller is responsible for inserting the generated code in the
366    --  proper place.
367
368    procedure Set_All_DT_Position (Typ : Entity_Id);
369    --  Set the DT_Position field for each primitive operation. In the CPP
370    --  Class case check that no pragma CPP_Virtual is missing and that the
371    --  DT_Position are coherent
372
373    procedure Set_CPP_Constructors (Typ : Entity_Id);
374    --  Typ is a CPP_Class type. Create the Init procedures of that type
375    --  required to handle its default and non-default constructors. The
376    --  functions to which pragma CPP_Constructor is applied in the sources
377    --  are functions returning this type, and having an implicit access to the
378    --  target object in its first argument; such implicit argument is explicit
379    --  in the IP procedures built here.
380
381    procedure Set_DTC_Entity_Value
382      (Tagged_Type : Entity_Id;
383       Prim        : Entity_Id);
384    --  Set the definite value of the DTC_Entity value associated with a given
385    --  primitive of a tagged type.
386
387    procedure Write_DT (Typ : Entity_Id);
388    pragma Export (Ada, Write_DT);
389    --  Debugging procedure (to be called within gdb)
390
391 end Exp_Disp;