1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2006, Free Software Foundation, Inc. --
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. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 -- This package contains routines involved in tagged types and dynamic
28 -- dispatching expansion.
30 with Types; use Types;
34 -------------------------------------
35 -- Predefined primitive operations --
36 -------------------------------------
38 -- The predefined primitive operations (PPOs) are subprograms generated
39 -- by GNAT for a particular tagged type. Their role is to provide support
40 -- for different Ada language features such as the attribute 'Size or
41 -- handling of dispatching triggers in select statements. PPOs are created
42 -- when a tagged type is expanded or frozen. These subprograms are later
43 -- collected and inserted into the dispatch table of a tagged type at
44 -- fixed positions. Some of the PPOs that manipulate data in tagged objects
45 -- require the generation of thunks.
47 -- List of predefined primitive operations
49 -- Leading underscores designate reserved names. Bracketed numerical
50 -- values represent dispatch table slot numbers.
52 -- _Size (1) - implementation of the attribute 'Size for any tagged
53 -- type. Constructs of the form Prefix'Size are converted into
56 -- _Alignment (2) - implementation of the attribute 'Alignment for
57 -- any tagged type. Constructs of the form Prefix'Alignment are
58 -- converted into Prefix._Alignment.
60 -- TSS_Stream_Read (3) - implementation of the stream attribute Read
61 -- for any tagged type.
63 -- TSS_Stream_Write (4) - implementation of the stream attribute Write
64 -- for any tagged type.
66 -- TSS_Stream_Input (5) - implementation of the stream attribute Input
67 -- for any tagged type.
69 -- TSS_Stream_Output (6) - implementation of the stream attribute
70 -- Output for any tagged type.
72 -- Op_Eq (7) - implementation of the equality operator for any non-
73 -- limited tagged type.
75 -- _Assign (8) - implementation of the assignment operator for any
76 -- non-limited tagged type.
78 -- TSS_Deep_Adjust (9) - implementation of the finalization operation
79 -- Adjust for any non-limited tagged type.
81 -- TSS_Deep_Finalize (10) - implementation of the finalization
82 -- operation Finalize for any non-limited tagged type.
84 -- _Disp_Asynchronous_Select (11) - used in the expansion of ATC with
85 -- dispatching triggers. Null implementation for limited interfaces,
86 -- full body generation for types that implement limited interfaces,
87 -- not generated for the rest of the cases. See Expand_N_Asynchronous_
88 -- Select in Exp_Ch9 for more information.
90 -- _Disp_Conditional_Select (12) - used in the expansion of conditional
91 -- selects with dispatching triggers. Null implementation for limited
92 -- interfaces, full body generation for types that implement limited
93 -- interfaces, not generated for the rest of the cases. See Expand_N_
94 -- Conditional_Entry_Call in Exp_Ch9 for more information.
96 -- _Disp_Get_Prim_Op_Kind (13) - helper routine used in the expansion
97 -- of ATC with dispatching triggers. Null implementation for limited
98 -- interfaces, full body generation for types that implement limited
99 -- interfaces, not generated for the rest of the cases.
101 -- _Disp_Get_Task_Id (14) - helper routine used in the expansion of
102 -- Abort, attributes 'Callable and 'Terminated for task interface
103 -- class-wide types. Full body generation for task types, null
104 -- implementation for limited interfaces, not generated for the rest
105 -- of the cases. See Expand_N_Attribute_Reference in Exp_Attr and
106 -- Expand_N_Abort_Statement in Exp_Ch9 for more information.
108 -- _Disp_Timed_Select (15) - used in the expansion of timed selects
109 -- with dispatching triggers. Null implementation for limited
110 -- interfaces, full body generation for types that implement limited
111 -- interfaces, not generated for the rest of the cases. See Expand_N_
112 -- Timed_Entry_Call for more information.
114 -- Lifecycle of predefined primitive operations
116 -- The specifications and bodies of the PPOs are created by
117 -- Make_Predefined_Primitive_Specs and Predefined_Primitive_Bodies
118 -- in Exp_Ch3. The generated specifications are immediately analyzed,
119 -- while the bodies are left as freeze actions to the tagged type for
120 -- which they are created.
122 -- PPOs are collected and added to the Primitive_Operations list of
123 -- a type by the regular analysis mechanism.
125 -- PPOs are frozen in Predefined_Primitive_Freeze in Exp_Ch3.
127 -- Thunks for PPOs are created in Freeze_Subprogram in Exp_Ch6, by a
128 -- call to Register_Predefined_DT_Entry, also in Exp_Ch6.
130 -- Dispatch table positions of PPOs are set in Set_All_DT_Position in
133 -- Calls to PPOs procede as regular dispatching calls. If the PPO
134 -- has a thunk, a call procedes as a regular dispatching call with
137 -- Guidelines for addition of new predefined primitive operations
139 -- Update the value of constant Default_Prim_Op_Count in A-Tags.ads
140 -- to reflect the new number of PPOs.
142 -- Introduce a new predefined name for the new PPO in Snames.ads and
145 -- Categorize the new PPO name as predefined by adding an entry in
146 -- Is_Predefined_Dispatching_Operation in Exp_Util.adb.
148 -- Generate the specification of the new PPO in Make_Predefined_
149 -- Primitive_Spec in Exp_Ch3.adb. The Is_Internal flag of the defining
150 -- identifier of the specification must be set to True.
152 -- Generate the body of the new PPO in Predefined_Primitive_Bodies in
153 -- Exp_Ch3.adb. The Is_Internal flag of the defining identifier of the
154 -- specification must be set to True.
156 -- If the new PPO requires a thunk, add an entry in Freeze_Subprogram
159 -- When generating calls to a PPO, use Find_Prim_Op from Exp_Util.ads
160 -- to retrieve the entity of the operation directly.
162 -- Number of predefined primitive operations added by the Expander
163 -- for a tagged type. If more predefined primitive operations are
164 -- added, the following items must be changed:
166 -- Ada.Tags.Defailt_Prim_Op_Count - indirect use
167 -- Exp_Disp.Default_Prim_Op_Position - indirect use
168 -- Exp_Disp.Set_All_DT_Position - direct use
170 type DT_Access_Action is
175 Register_Interface_Tag,
185 procedure Expand_Dispatching_Call (Call_Node : Node_Id);
186 -- Expand the call to the operation through the dispatch table and perform
187 -- the required tag checks when appropriate. For CPP types tag checks are
190 procedure Expand_Interface_Actuals (Call_Node : Node_Id);
191 -- Ada 2005 (AI-251): Displace all the actuals corresponding to class-wide
192 -- interfaces to reference the interface tag of the actual object
194 procedure Expand_Interface_Conversion
196 Is_Static : Boolean := True);
197 -- Ada 2005 (AI-251): N is a type-conversion node. Reference the base of
198 -- the object to give access to the interface tag associated with the
199 -- secondary dispatch table.
201 function Expand_Interface_Thunk
203 Thunk_Alias : Node_Id;
204 Thunk_Id : Entity_Id) return Node_Id;
205 -- Ada 2005 (AI-251): When a tagged type implements abstract interfaces we
206 -- generate additional subprograms (thunks) to have a layout compatible
207 -- with the C++ ABI. The thunk modifies the value of the first actual of
208 -- the call (that is, the pointer to the object) before transferring
209 -- control to the target function.
211 function Fill_DT_Entry
213 Prim : Entity_Id) return Node_Id;
214 -- Generate the code necessary to fill the appropriate entry of the
215 -- dispatch table of Prim's controlling type with Prim's address.
217 function Fill_Secondary_DT_Entry
220 Thunk_Id : Entity_Id;
221 Iface_DT_Ptr : Entity_Id) return Node_Id;
222 -- (Ada 2005): Generate the code necessary to fill the appropriate entry of
223 -- the secondary dispatch table of Prim's controlling type with Thunk_Id's
226 function Make_DT_Access_Action
228 Action : DT_Access_Action;
229 Args : List_Id) return Node_Id;
230 -- Generate a call to one of the Dispatch Table Access Subprograms defined
231 -- in Ada.Tags or in Interfaces.Cpp
233 function Make_DT (Typ : Entity_Id) return List_Id;
234 -- Expand the declarations for the Dispatch Table (or the Vtable in
235 -- the case of type whose ancestor is a CPP_Class)
237 function Make_Disp_Asynchronous_Select_Body
238 (Typ : Entity_Id) return Node_Id;
239 -- Ada 2005 (AI-345): Generate the body of the primitive operation of type
240 -- Typ used for dispatching in asynchronous selects. Generate a null body
241 -- if Typ is an interface type.
243 function Make_Disp_Asynchronous_Select_Spec
244 (Typ : Entity_Id) return Node_Id;
245 -- Ada 2005 (AI-345): Generate the specification of the primitive operation
246 -- of type Typ used for dispatching in asynchronous selects.
248 function Make_Disp_Conditional_Select_Body
249 (Typ : Entity_Id) return Node_Id;
250 -- Ada 2005 (AI-345): Generate the body of the primitive operation of type
251 -- Typ used for dispatching in conditional selects. Generate a null body
252 -- if Typ is an interface type.
254 function Make_Disp_Conditional_Select_Spec
255 (Typ : Entity_Id) return Node_Id;
256 -- Ada 2005 (AI-345): Generate the specification of the primitive operation
257 -- of type Typ used for dispatching in conditional selects.
259 function Make_Disp_Get_Prim_Op_Kind_Body
260 (Typ : Entity_Id) return Node_Id;
261 -- Ada 2005 (AI-345): Generate the body of the primitive operation of type
262 -- Typ used for retrieving the callable entity kind during dispatching in
263 -- asynchronous selects. Generate a null body if Typ is an interface type.
265 function Make_Disp_Get_Prim_Op_Kind_Spec
266 (Typ : Entity_Id) return Node_Id;
267 -- Ada 2005 (AI-345): Generate the specification of the primitive operation
268 -- of the type Typ use for retrieving the callable entity kind during
269 -- dispatching in asynchronous selects.
271 function Make_Disp_Get_Task_Id_Body
272 (Typ : Entity_Id) return Node_Id;
273 -- Ada 2005 (AI-345): Generate the body of the primitive operation of type
274 -- Typ used for retrieving the _task_id field of a task interface class-
275 -- wide type. Generate a null body if Typ is an interface or a non-task
278 function Make_Disp_Get_Task_Id_Spec
279 (Typ : Entity_Id) return Node_Id;
280 -- Ada 2005 (AI-345): Generate the specification of the primitive operation
281 -- of type Typ used for retrieving the _task_id field of a task interface
284 function Make_Disp_Timed_Select_Body
285 (Typ : Entity_Id) return Node_Id;
286 -- Ada 2005 (AI-345): Generate the body of the primitive operation of type
287 -- Typ used for dispatching in timed selects. Generate a null body if Nul
288 -- is an interface type.
290 function Make_Disp_Timed_Select_Spec
291 (Typ : Entity_Id) return Node_Id;
292 -- Ada 2005 (AI-345): Generate the specification of the primitive operation
293 -- of type Typ used for dispatching in timed selects.
295 function Make_Select_Specific_Data_Table
296 (Typ : Entity_Id) return List_Id;
297 -- Ada 2005 (AI-345): Create and populate the auxiliary table in the TSD
298 -- of Typ used for dispatching in asynchronous, conditional and timed
299 -- selects. Generate code to set the primitive operation kinds and entry
300 -- indices of primitive operations and primitive wrappers.
302 procedure Make_Secondary_DT
304 Ancestor_Typ : Entity_Id;
308 Acc_Disp_Tables : in out Elist_Id;
309 Result : out List_Id);
310 -- Ada 2005 (AI-251): Expand the declarations for the Secondary Dispatch
311 -- Table of Typ associated with Iface (each abstract interface implemented
312 -- by Typ has a secondary dispatch table). The arguments Typ, Ancestor_Typ
313 -- and Suffix_Index are used to generate an unique external name which
314 -- is added at the end of Acc_Disp_Tables; this external name will be
315 -- used later by the subprogram Exp_Ch3.Build_Init_Procedure.
317 procedure Set_All_DT_Position (Typ : Entity_Id);
318 -- Set the DT_Position field for each primitive operation. In the CPP
319 -- Class case check that no pragma CPP_Virtual is missing and that the
320 -- DT_Position are coherent
322 procedure Set_Default_Constructor (Typ : Entity_Id);
323 -- Typ is a CPP_Class type. Create the Init procedure of that type to
324 -- be the default constructor (i.e. the function returning this type,
325 -- having a pragma CPP_Constructor and no parameter)
327 procedure Write_DT (Typ : Entity_Id);
328 pragma Export (Ada, Write_DT);
329 -- Debugging procedure (to be called within gdb)