1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 3, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. --
22 -- As a special exception under Section 7 of GPL version 3, you are granted --
23 -- additional permissions described in the GCC Runtime Library Exception, --
24 -- version 3.1, as published by the Free Software Foundation. --
26 -- You should have received a copy of the GNU General Public License and --
27 -- a copy of the GCC Runtime Library Exception along with this program; --
28 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
29 -- <http://www.gnu.org/licenses/>. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- Extensive contributions were provided by Ada Core Technologies Inc. --
34 ------------------------------------------------------------------------------
36 -- For performance analysis, take into account that the operations in this
37 -- package provide the guarantee that all dispatching calls on primitive
38 -- operations of tagged types and interfaces take constant time (in terms
39 -- of source lines executed), that is to say, the cost of these calls is
40 -- independent of the number of primitives of the type or interface, and
41 -- independent of the number of ancestors or interface progenitors that a
42 -- tagged type may have.
44 -- The following subprograms of the public part of this package take constant
45 -- time (in terms of source lines executed):
47 -- Expanded_Name, Wide_Expanded_Name, Wide_Wide_Expanded_Name, External_Tag,
48 -- Is_Descendant_At_Same_Level, Parent_Tag, Type_Is_Abstract
49 -- Descendant_Tag (when used with a library-level tagged type),
50 -- Internal_Tag (when used with a library-level tagged type).
52 -- The following subprograms of the public part of this package execute in
53 -- time that is not constant (in terms of sources line executed):
55 -- Internal_Tag (when used with a locally defined tagged type), because in
56 -- such cases this routine processes the external tag, extracts from it an
57 -- address available there, and converts it into the tag value returned by
58 -- this function. The number of instructions executed is not constant since
59 -- it depends on the length of the external tag string.
61 -- Descendant_Tag (when used with a locally defined tagged type), because
62 -- it relies on the subprogram Internal_Tag() to provide its functionality.
64 -- Interface_Ancestor_Tags, because this function returns a table whose
65 -- length depends on the number of interfaces covered by a tagged type.
67 with System
.Storage_Elements
;
71 -- In accordance with Ada 2005 AI-362
74 pragma Preelaborable_Initialization
(Tag
);
76 No_Tag
: constant Tag
;
78 function Expanded_Name
(T
: Tag
) return String;
80 function Wide_Expanded_Name
(T
: Tag
) return Wide_String;
81 pragma Ada_05
(Wide_Expanded_Name
);
83 function Wide_Wide_Expanded_Name
(T
: Tag
) return Wide_Wide_String
;
84 pragma Ada_05
(Wide_Wide_Expanded_Name
);
86 function External_Tag
(T
: Tag
) return String;
88 function Internal_Tag
(External
: String) return Tag
;
90 function Descendant_Tag
92 Ancestor
: Tag
) return Tag
;
93 pragma Ada_05
(Descendant_Tag
);
95 function Is_Descendant_At_Same_Level
97 Ancestor
: Tag
) return Boolean;
98 pragma Ada_05
(Is_Descendant_At_Same_Level
);
100 function Parent_Tag
(T
: Tag
) return Tag
;
101 pragma Ada_05
(Parent_Tag
);
103 type Tag_Array
is array (Positive range <>) of Tag
;
105 function Interface_Ancestor_Tags
(T
: Tag
) return Tag_Array
;
106 pragma Ada_05
(Interface_Ancestor_Tags
);
108 function Type_Is_Abstract
(T
: Tag
) return Boolean;
109 pragma Ada_2012
(Type_Is_Abstract
);
111 Tag_Error
: exception;
114 -- Structure of the GNAT Primary Dispatch Table
116 -- +--------------------+
118 -- +--------------------+
120 -- +--------------------+ Predef Prims
121 -- | Predef_Prims -----------------------------> +------------+
122 -- +--------------------+ | table of |
123 -- | Offset_To_Top | | predefined |
124 -- +--------------------+ | primitives |
125 -- |Typeinfo_Ptr/TSD_Ptr---> Type Specific Data +------------+
126 -- Tag ---> +--------------------+ +-------------------+
127 -- | table of | | inheritance depth |
128 -- : primitive ops : +-------------------+
129 -- | pointers | | access level |
130 -- +--------------------+ +-------------------+
132 -- +-------------------+
134 -- +-------------------+
136 -- +-------------------+
137 -- | hash table link |
138 -- +-------------------+
140 -- +-------------------+
141 -- | type_is_abstract |
142 -- +-------------------+
143 -- | needs finalization|
144 -- +-------------------+
145 -- | Ifaces_Table ---> Interface Data
146 -- +-------------------+ +------------+
147 -- Select Specific Data <---- SSD | | Nb_Ifaces |
148 -- +------------------+ +-------------------+ +------------+
149 -- |table of primitive| | table of | | table |
150 -- : operation : : ancestor : : of :
151 -- | kinds | | tags | | interfaces |
152 -- +------------------+ +-------------------+ +------------+
156 -- +------------------+
158 -- Structure of the GNAT Secondary Dispatch Table
160 -- +--------------------+
162 -- +--------------------+
164 -- +--------------------+ Predef Prims
165 -- | Predef_Prims -----------------------------> +------------+
166 -- +--------------------+ | table of |
167 -- | Offset_To_Top | | predefined |
168 -- +--------------------+ | primitives |
169 -- | OSD_Ptr |---> Object Specific Data | thunks |
170 -- Tag ---> +--------------------+ +---------------+ +------------+
171 -- | table of | | num prim ops |
172 -- : primitive op : +---------------+
173 -- | thunk pointers | | table of |
174 -- +--------------------+ + primitive |
178 -- The runtime information kept for each tagged type is separated into two
179 -- objects: the Dispatch Table and the Type Specific Data record.
181 package SSE
renames System
.Storage_Elements
;
183 subtype Cstring
is String (Positive);
184 type Cstring_Ptr
is access all Cstring
;
185 pragma No_Strict_Aliasing
(Cstring_Ptr
);
187 -- Declarations for the table of interfaces
189 type Offset_To_Top_Function_Ptr
is
190 access function (This
: System
.Address
) return SSE
.Storage_Offset
;
191 -- Type definition used to call the function that is generated by the
192 -- expander in case of tagged types with discriminants that have secondary
193 -- dispatch tables. This function provides the Offset_To_Top value in this
196 type Interface_Data_Element
is record
198 Static_Offset_To_Top
: Boolean;
199 Offset_To_Top_Value
: SSE
.Storage_Offset
;
200 Offset_To_Top_Func
: Offset_To_Top_Function_Ptr
;
203 -- If some ancestor of the tagged type has discriminants the field
204 -- Static_Offset_To_Top is False and the field Offset_To_Top_Func
205 -- is used to store the access to the function generated by the
206 -- expander which provides this value; otherwise Static_Offset_To_Top
207 -- is True and such value is stored in the Offset_To_Top_Value field.
208 -- Secondary_DT references a secondary dispatch table whose contents
209 -- are pointers to the primitives of the tagged type that cover the
210 -- interface primitives. Secondary_DT gives support to dispatching
211 -- calls through interface types associated with Generic Dispatching
214 type Interfaces_Array
is array (Natural range <>) of Interface_Data_Element
;
216 type Interface_Data
(Nb_Ifaces
: Positive) is record
217 Ifaces_Table
: Interfaces_Array
(1 .. Nb_Ifaces
);
220 type Interface_Data_Ptr
is access all Interface_Data
;
221 -- Table of abstract interfaces used to give support to backward interface
222 -- conversions and also to IW_Membership.
224 -- Primitive operation kinds. These values differentiate the kinds of
225 -- callable entities stored in the dispatch table. Certain kinds may
226 -- not be used, but are added for completeness.
232 POK_Protected_Function
,
233 POK_Protected_Procedure
,
238 -- Select specific data types
240 type Select_Specific_Data_Element
is record
245 type Select_Specific_Data_Array
is
246 array (Positive range <>) of Select_Specific_Data_Element
;
248 type Select_Specific_Data
(Nb_Prim
: Positive) is record
249 SSD_Table
: Select_Specific_Data_Array
(1 .. Nb_Prim
);
250 -- NOTE: Nb_Prim is the number of non-predefined primitive operations
253 type Select_Specific_Data_Ptr
is access all Select_Specific_Data
;
254 -- A table used to store the primitive operation kind and entry index of
255 -- primitive subprograms of a type that implements a limited interface.
256 -- The Select Specific Data table resides in the Type Specific Data of a
257 -- type. This construct is used in the handling of dispatching triggers
258 -- in select statements.
260 type Prim_Ptr
is access procedure;
261 type Address_Array
is array (Positive range <>) of Prim_Ptr
;
263 subtype Dispatch_Table
is Address_Array
(1 .. 1);
264 -- Used by GDB to identify the _tags and traverse the run-time structure
265 -- associated with tagged types. For compatibility with older versions of
266 -- gdb, its name must not be changed.
268 type Tag
is access all Dispatch_Table
;
269 pragma No_Strict_Aliasing
(Tag
);
271 type Interface_Tag
is access all Dispatch_Table
;
273 No_Tag
: constant Tag
:= null;
275 -- The expander ensures that Tag objects reference the Prims_Ptr component
278 type Tag_Ptr
is access all Tag
;
279 pragma No_Strict_Aliasing
(Tag_Ptr
);
281 type Offset_To_Top_Ptr
is access all SSE
.Storage_Offset
;
282 pragma No_Strict_Aliasing
(Offset_To_Top_Ptr
);
284 type Tag_Table
is array (Natural range <>) of Tag
;
287 access function (A
: System
.Address
) return Long_Long_Integer;
289 type Type_Specific_Data
(Idepth
: Natural) is record
290 -- The discriminant Idepth is the Inheritance Depth Level: Used to
291 -- implement the membership test associated with single inheritance of
292 -- tagged types in constant-time. It also indicates the size of the
293 -- Tags_Table component.
295 Access_Level
: Natural;
296 -- Accessibility level required to give support to Ada 2005 nested type
297 -- extensions. This feature allows safe nested type extensions by
298 -- shifting the accessibility checks to certain operations, rather than
299 -- being enforced at the type declaration. In particular, by performing
300 -- run-time accessibility checks on class-wide allocators, class-wide
301 -- function return, and class-wide stream I/O, the danger of objects
302 -- outliving their type declaration can be eliminated (Ada 2005: AI-344)
305 Expanded_Name
: Cstring_Ptr
;
306 External_Tag
: Cstring_Ptr
;
308 -- Components used to support to the Ada.Tags subprograms in RM 3.9
310 -- Note: Expanded_Name is referenced by GDB to determine the actual name
311 -- of the tagged type. Its requirements are: 1) it must have this exact
312 -- name, and 2) its contents must point to a C-style Nul terminated
313 -- string containing its expanded name. GDB has no requirement on a
314 -- given position inside the record.
316 Transportable
: Boolean;
317 -- Used to check RM E.4(18), set for types that satisfy the requirements
318 -- for being used in remote calls as actuals for classwide formals or as
319 -- return values for classwide functions.
321 Type_Is_Abstract
: Boolean;
322 -- True if the type is abstract (Ada 2012: AI05-0173)
324 Needs_Finalization
: Boolean;
325 -- Used to dynamically check whether an object is controlled or not
327 Size_Func
: Size_Ptr
;
328 -- Pointer to the subprogram computing the _size of the object. Used by
329 -- the run-time whenever a call to the 'size primitive is required. We
330 -- cannot assume that the contents of dispatch tables are addresses
331 -- because in some architectures the ABI allows descriptors.
333 Interfaces_Table
: Interface_Data_Ptr
;
334 -- Pointer to the table of interface tags. It is used to implement the
335 -- membership test associated with interfaces and also for backward
336 -- abstract interface type conversions (Ada 2005:AI-251)
338 SSD
: Select_Specific_Data_Ptr
;
339 -- Pointer to a table of records used in dispatching selects. This field
340 -- has a meaningful value for all tagged types that implement a limited,
341 -- protected, synchronized or task interfaces and have non-predefined
342 -- primitive operations.
344 Tags_Table
: Tag_Table
(0 .. Idepth
);
345 -- Table of ancestor tags. Its size actually depends on the inheritance
346 -- depth level of the tagged type.
349 type Type_Specific_Data_Ptr
is access all Type_Specific_Data
;
350 pragma No_Strict_Aliasing
(Type_Specific_Data_Ptr
);
352 -- Declarations for the dispatch table record
354 type Signature_Kind
is
359 -- Tagged type kinds with respect to concurrency and limitedness
362 (TK_Abstract_Limited_Tagged
,
369 type Dispatch_Table_Wrapper
(Num_Prims
: Natural) is record
370 Signature
: Signature_Kind
;
371 Tag_Kind
: Tagged_Kind
;
372 Predef_Prims
: System
.Address
;
373 -- Pointer to the dispatch table of predefined Ada primitives
375 -- According to the C++ ABI the components Offset_To_Top and TSD are
376 -- stored just "before" the dispatch table, and they are referenced with
377 -- negative offsets referring to the base of the dispatch table. The
378 -- _Tag (or the VTable_Ptr in C++ terminology) must point to the base
379 -- of the virtual table, just after these components, to point to the
382 Offset_To_Top
: SSE
.Storage_Offset
;
383 TSD
: System
.Address
;
385 Prims_Ptr
: aliased Address_Array
(1 .. Num_Prims
);
386 -- The size of the Prims_Ptr array actually depends on the tagged type
387 -- to which it applies. For each tagged type, the expander computes the
388 -- actual array size, allocates the Dispatch_Table record accordingly.
391 type Dispatch_Table_Ptr
is access all Dispatch_Table_Wrapper
;
392 pragma No_Strict_Aliasing
(Dispatch_Table_Ptr
);
394 -- The following type declaration is used by the compiler when the program
395 -- is compiled with restriction No_Dispatching_Calls. It is also used with
396 -- interface types to generate the tag and run-time information associated
399 type No_Dispatch_Table_Wrapper
is record
400 NDT_TSD
: System
.Address
;
401 NDT_Prims_Ptr
: Natural;
404 DT_Predef_Prims_Size
: constant SSE
.Storage_Count
:=
406 (1 * (Standard
'Address_Size /
407 System
.Storage_Unit
));
408 -- Size of the Predef_Prims field of the Dispatch_Table
410 DT_Offset_To_Top_Size
: constant SSE
.Storage_Count
:=
412 (1 * (Standard
'Address_Size /
413 System
.Storage_Unit
));
414 -- Size of the Offset_To_Top field of the Dispatch Table
416 DT_Typeinfo_Ptr_Size
: constant SSE
.Storage_Count
:=
418 (1 * (Standard
'Address_Size /
419 System
.Storage_Unit
));
420 -- Size of the Typeinfo_Ptr field of the Dispatch Table
422 use type System
.Storage_Elements
.Storage_Offset
;
424 DT_Offset_To_Top_Offset
: constant SSE
.Storage_Count
:=
426 + DT_Offset_To_Top_Size
;
428 DT_Predef_Prims_Offset
: constant SSE
.Storage_Count
:=
430 + DT_Offset_To_Top_Size
431 + DT_Predef_Prims_Size
;
432 -- Offset from Prims_Ptr to Predef_Prims component
434 -- Object Specific Data record of secondary dispatch tables
436 type Object_Specific_Data_Array
is array (Positive range <>) of Positive;
438 type Object_Specific_Data
(OSD_Num_Prims
: Positive) is record
439 OSD_Table
: Object_Specific_Data_Array
(1 .. OSD_Num_Prims
);
440 -- Table used in secondary DT to reference their counterpart in the
441 -- select specific data (in the TSD of the primary DT). This construct
442 -- is used in the handling of dispatching triggers in select statements.
443 -- Nb_Prim is the number of non-predefined primitive operations.
446 type Object_Specific_Data_Ptr
is access all Object_Specific_Data
;
447 pragma No_Strict_Aliasing
(Object_Specific_Data_Ptr
);
449 -- The following subprogram specifications are placed here instead of the
450 -- package body to see them from the frontend through rtsfind.
452 function Base_Address
(This
: System
.Address
) return System
.Address
;
453 -- Ada 2005 (AI-251): Displace "This" to point to the base address of the
454 -- object (that is, the address of the primary tag of the object).
456 procedure Check_TSD
(TSD
: Type_Specific_Data_Ptr
);
457 -- Ada 2012 (AI-113): Raise Program_Error if the external tag of this TSD
458 -- is the same as the external tag for some other tagged type declaration.
460 function Displace
(This
: System
.Address
; T
: Tag
) return System
.Address
;
461 -- Ada 2005 (AI-251): Displace "This" to point to the secondary dispatch
464 function Secondary_Tag
(T
, Iface
: Tag
) return Tag
;
465 -- Ada 2005 (AI-251): Given a primary tag T associated with a tagged type
466 -- Typ, search for the secondary tag of the interface type Iface covered
469 function DT
(T
: Tag
) return Dispatch_Table_Ptr
;
470 -- Return the pointer to the TSD record associated with T
472 function Get_Entry_Index
(T
: Tag
; Position
: Positive) return Positive;
473 -- Ada 2005 (AI-251): Return a primitive operation's entry index (if entry)
474 -- given a dispatch table T and a position of a primitive operation in T.
476 function Get_Offset_Index
478 Position
: Positive) return Positive;
479 -- Ada 2005 (AI-251): Given a pointer to a secondary dispatch table (T)
480 -- and a position of an operation in the DT, retrieve the corresponding
481 -- operation's position in the primary dispatch table from the Offset
482 -- Specific Data table of T.
484 function Get_Prim_Op_Kind
486 Position
: Positive) return Prim_Op_Kind
;
487 -- Ada 2005 (AI-251): Return a primitive operation's kind given a dispatch
488 -- table T and a position of a primitive operation in T.
490 function Get_Tagged_Kind
(T
: Tag
) return Tagged_Kind
;
491 -- Ada 2005 (AI-345): Given a pointer to either a primary or a secondary
492 -- dispatch table, return the tagged kind of a type in the context of
493 -- concurrency and limitedness.
495 function IW_Membership
(This
: System
.Address
; T
: Tag
) return Boolean;
496 -- Ada 2005 (AI-251): General routine that checks if a given object
497 -- implements a tagged type. Its common usage is to check if Obj is in
498 -- Iface'Class, but it is also used to check if a class-wide interface
499 -- implements a given type (Iface_CW_Typ in T'Class). For example:
501 -- type I is interface;
502 -- type T is tagged ...
504 -- function Test (O : I'Class) is
506 -- return O in T'Class.
509 function Offset_To_Top
510 (This
: System
.Address
) return SSE
.Storage_Offset
;
511 -- Ada 2005 (AI-251): Returns the current value of the Offset_To_Top
512 -- component available in the prologue of the dispatch table. If the parent
513 -- of the tagged type has discriminants this value is stored in a record
514 -- component just immediately after the tag component.
516 function Needs_Finalization
(T
: Tag
) return Boolean;
517 -- A helper routine used in conjunction with finalization collections which
518 -- service class-wide types. The function dynamically determines whether an
519 -- object is controlled or has controlled components.
522 (Obj
: System
.Address
;
523 T
: Tag
) return SSE
.Storage_Count
;
524 -- Computes the size the ancestor part of a tagged extension object whose
525 -- address is 'obj' by calling indirectly the ancestor _size function. The
526 -- ancestor is the parent of the type represented by tag T. This function
527 -- assumes that _size is always in slot one of the dispatch table.
529 procedure Register_Interface_Offset
530 (This
: System
.Address
;
533 Offset_Value
: SSE
.Storage_Offset
;
534 Offset_Func
: Offset_To_Top_Function_Ptr
);
535 -- Register in the table of interfaces of the tagged type associated with
536 -- "This" object the offset of the record component associated with the
537 -- progenitor Interface_T (that is, the distance from "This" to the object
538 -- component containing the tag of the secondary dispatch table). In case
539 -- of constant offset, Is_Static is true and Offset_Value has such value.
540 -- In case of variable offset, Is_Static is false and Offset_Func is an
541 -- access to function that must be called to evaluate the offset.
543 procedure Register_Tag
(T
: Tag
);
544 -- Insert the Tag and its associated external_tag in a table for the sake
547 procedure Set_Dynamic_Offset_To_Top
548 (This
: System
.Address
;
550 Offset_Value
: SSE
.Storage_Offset
;
551 Offset_Func
: Offset_To_Top_Function_Ptr
);
552 -- Ada 2005 (AI-251): The compiler generates calls to this routine only
553 -- when initializing the Offset_To_Top field of dispatch tables associated
554 -- with tagged type whose parent has variable size components. "This" is
555 -- the object whose dispatch table is being initialized. Interface_T is the
556 -- interface for which the secondary dispatch table is being initialized,
557 -- and Offset_Value is the distance from "This" to the object component
558 -- containing the tag of the secondary dispatch table (a zero value means
559 -- that this interface shares the primary dispatch table). Offset_Func
560 -- references a function that must be called to evaluate the offset at
561 -- runtime. This routine also takes care of registering these values in
562 -- the table of interfaces of the type.
564 procedure Set_Entry_Index
(T
: Tag
; Position
: Positive; Value
: Positive);
565 -- Ada 2005 (AI-345): Set the entry index of a primitive operation in T's
566 -- TSD table indexed by Position.
568 procedure Set_Prim_Op_Kind
571 Value
: Prim_Op_Kind
);
572 -- Ada 2005 (AI-251): Set the kind of a primitive operation in T's TSD
573 -- table indexed by Position.
575 procedure Unregister_Tag
(T
: Tag
);
576 -- Remove a particular tag from the external tag hash table
578 Max_Predef_Prims
: constant Positive := 15;
579 -- Number of reserved slots for the following predefined ada primitives:
591 -- 11. conditional select
594 -- 14. dispatching requeue
597 -- The compiler checks that the value here is correct
599 subtype Predef_Prims_Table
is Address_Array
(1 .. Max_Predef_Prims
);
600 type Predef_Prims_Table_Ptr
is access Predef_Prims_Table
;
601 pragma No_Strict_Aliasing
(Predef_Prims_Table_Ptr
);
603 type Addr_Ptr
is access System
.Address
;
604 pragma No_Strict_Aliasing
(Addr_Ptr
);
605 -- This type is used by the frontend to generate the code that handles
606 -- dispatch table slots of types declared at the local level.