1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
9 -- Copyright (C) 1992-2005 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 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
36 with System
.Storage_Elements
; use System
.Storage_Elements
;
38 pragma Elaborate_All
(System
.HTable
);
40 package body Ada
.Tags
is
42 -- Structure of the GNAT Dispatch Table
44 -- +-----------------------+
46 -- +-----------------------+
47 -- | Typeinfo_Ptr/TSD_Ptr |----> Type Specific Data
48 -- Tag ---> +-----------------------+ +-------------------+
49 -- | table of | | inheritance depth |
50 -- : primitive ops : +-------------------+
51 -- | pointers | | expanded name |
52 -- +-----------------------+ +-------------------+
54 -- +-------------------+
55 -- | Hash table link |
56 -- +-------------------+
57 -- | Remotely Callable |
58 -- +-------------------+
59 -- | Rec Ctrler offset |
60 -- +-------------------+
62 -- +-------------------+
66 -- +-------------------+
70 -- +-------------------+
74 -- +-------------------+
78 -- +-------------------+
80 subtype Cstring
is String (Positive);
81 type Cstring_Ptr
is access all Cstring
;
83 -- We suppress index checks because the declared size in the record below
84 -- is a dummy size of one (see below).
86 type Tag_Table
is array (Natural range <>) of Tag
;
87 pragma Suppress_Initialization
(Tag_Table
);
88 pragma Suppress
(Index_Check
, On
=> Tag_Table
);
90 type Prim_Op_Kind_Table
is array (Natural range <>) of Prim_Op_Kind
;
91 pragma Suppress_Initialization
(Prim_Op_Kind_Table
);
92 pragma Suppress
(Index_Check
, On
=> Prim_Op_Kind_Table
);
94 type Entry_Index_Table
is array (Natural range <>) of Positive;
95 pragma Suppress_Initialization
(Entry_Index_Table
);
96 pragma Suppress
(Index_Check
, On
=> Entry_Index_Table
);
98 type Type_Specific_Data
is record
100 -- Inheritance Depth Level: Used to implement the membership test
101 -- associated with single inheritance of tagged types in constant-time.
102 -- In addition it also indicates the size of the first table stored in
103 -- the Tags_Table component (see comment below).
105 Access_Level
: Natural;
106 -- Accessibility level required to give support to Ada 2005 nested type
107 -- extensions. This feature allows safe nested type extensions by
108 -- shifting the accessibility checks to certain operations, rather than
109 -- being enforced at the type declaration. In particular, by performing
110 -- run-time accessibility checks on class-wide allocators, class-wide
111 -- function return, and class-wide stream I/O, the danger of objects
112 -- outliving their type declaration can be eliminated (Ada 2005: AI-344)
114 Expanded_Name
: Cstring_Ptr
;
115 External_Tag
: Cstring_Ptr
;
117 -- Components used to give support to the Ada.Tags subprograms described
120 Remotely_Callable
: Boolean;
121 -- Used to check ARM E.4 (18)
123 RC_Offset
: SSE
.Storage_Offset
;
124 -- Controller Offset: Used to give support to tagged controlled objects
125 -- (see Get_Deep_Controller at s-finimp)
127 Num_Interfaces
: Natural;
128 -- Number of abstract interface types implemented by the tagged type.
129 -- The value Idepth+Num_Interfaces indicates the end of the second table
130 -- stored in the Tags_Table component. It is used to implement the
131 -- membership test associated with interfaces (Ada 2005:AI-251)
133 Tags_Table
: Tag_Table
(0 .. 1);
134 -- The size of the Tags_Table array actually depends on the tagged type
135 -- to which it applies. The compiler ensures that has enough space to
136 -- store all the entries of the two tables phisically stored there: the
137 -- "table of ancestor tags" and the "table of interface tags". For this
138 -- purpose we are using the same mechanism as for the Prims_Ptr array in
139 -- the Dispatch_Table record. See comments below on Prims_Ptr for
142 POK_Table
: Prim_Op_Kind_Table
(1 .. 1);
143 Ent_Index_Table
: Entry_Index_Table
(1 .. 1);
144 -- Two auxiliary tables used for dispatching in asynchronous,
145 -- conditional and timed selects. Their size depends on the number
146 -- of primitive operations. Indexing in these two tables is performed
147 -- by subtracting the number of predefined primitive operations from
148 -- the given index value. POK_Table contains the callable entity kinds
149 -- of all non-predefined primitive operations. Ent_Index_Table contains
150 -- the entry index of primitive entry wrappers.
153 type Dispatch_Table
is record
154 -- Offset_To_Top : Natural;
155 -- Typeinfo_Ptr : System.Address;
157 -- According to the C++ ABI the components Offset_To_Top and
158 -- Typeinfo_Ptr are stored just "before" the dispatch table (that is,
159 -- the Prims_Ptr table), and they are referenced with negative offsets
160 -- referring to the base of the dispatch table. The _Tag (or the
161 -- VTable_Ptr in C++ terminology) must point to the base of the virtual
162 -- table, just after these components, to point to the Prims_Ptr table.
163 -- For this purpose the expander generates a Prims_Ptr table that has
164 -- enough space for these additional components, and generates code that
165 -- displaces the _Tag to point after these components.
167 Prims_Ptr
: Address_Array
(1 .. 1);
168 -- The size of the Prims_Ptr array actually depends on the tagged type
169 -- to which it applies. For each tagged type, the expander computes the
170 -- actual array size, allocates the Dispatch_Table record accordingly,
171 -- and generates code that displaces the base of the record after the
172 -- Typeinfo_Ptr component. For this reason the first two components have
173 -- been commented in the previous declaration. The access to these
174 -- components is done by means of local functions.
176 -- To avoid the use of discriminants to define the actual size of the
177 -- dispatch table, we used to declare the tag as a pointer to a record
178 -- that contains an arbitrary array of addresses, using Positive as its
179 -- index. This ensures that there are never range checks when accessing
180 -- the dispatch table, but it prevents GDB from displaying tagged types
181 -- properly. A better approach is to declare this record type as holding
182 -- small number of addresses, and to explicitly suppress checks on it.
184 -- Note that in both cases, this type is never allocated, and serves
185 -- only to declare the corresponding access type.
188 ---------------------------------------------
189 -- Unchecked Conversions for String Fields --
190 ---------------------------------------------
192 function To_Address
is
193 new Unchecked_Conversion
(Cstring_Ptr
, System
.Address
);
195 function To_Cstring_Ptr
is
196 new Unchecked_Conversion
(System
.Address
, Cstring_Ptr
);
198 ------------------------------------------------
199 -- Unchecked Conversions for other components --
200 ------------------------------------------------
202 type Storage_Offset_Ptr
is access System
.Storage_Elements
.Storage_Offset
;
204 function To_Storage_Offset_Ptr
is
205 new Unchecked_Conversion
(System
.Address
, Storage_Offset_Ptr
);
207 -----------------------
208 -- Local Subprograms --
209 -----------------------
211 function Length
(Str
: Cstring_Ptr
) return Natural;
212 -- Length of string represented by the given pointer (treating the string
213 -- as a C-style string, which is Nul terminated).
215 function Offset_To_Top
216 (T
: Tag
) return System
.Storage_Elements
.Storage_Offset
;
217 -- Returns the current value of the offset_to_top component available in
218 -- the prologue of the dispatch table.
220 function Typeinfo_Ptr
(T
: Tag
) return System
.Address
;
221 -- Returns the current value of the typeinfo_ptr component available in
222 -- the prologue of the dispatch table.
224 pragma Unreferenced
(Typeinfo_Ptr
);
225 -- These functions will be used for full compatibility with the C++ ABI
227 -------------------------
228 -- External_Tag_HTable --
229 -------------------------
231 type HTable_Headers
is range 1 .. 64;
233 -- The following internal package defines the routines used for the
234 -- instantiation of a new System.HTable.Static_HTable (see below). See
235 -- spec in g-htable.ads for details of usage.
237 package HTable_Subprograms
is
238 procedure Set_HT_Link
(T
: Tag
; Next
: Tag
);
239 function Get_HT_Link
(T
: Tag
) return Tag
;
240 function Hash
(F
: System
.Address
) return HTable_Headers
;
241 function Equal
(A
, B
: System
.Address
) return Boolean;
242 end HTable_Subprograms
;
244 package External_Tag_HTable
is new System
.HTable
.Static_HTable
(
245 Header_Num
=> HTable_Headers
,
246 Element
=> Dispatch_Table
,
249 Set_Next
=> HTable_Subprograms
.Set_HT_Link
,
250 Next
=> HTable_Subprograms
.Get_HT_Link
,
251 Key
=> System
.Address
,
252 Get_Key
=> Get_External_Tag
,
253 Hash
=> HTable_Subprograms
.Hash
,
254 Equal
=> HTable_Subprograms
.Equal
);
256 ------------------------
257 -- HTable_Subprograms --
258 ------------------------
260 -- Bodies of routines for hash table instantiation
262 package body HTable_Subprograms
is
268 function Equal
(A
, B
: System
.Address
) return Boolean is
269 Str1
: constant Cstring_Ptr
:= To_Cstring_Ptr
(A
);
270 Str2
: constant Cstring_Ptr
:= To_Cstring_Ptr
(B
);
274 if Str1
(J
) /= Str2
(J
) then
276 elsif Str1
(J
) = ASCII
.NUL
then
288 function Get_HT_Link
(T
: Tag
) return Tag
is
290 return TSD
(T
).HT_Link
;
297 function Hash
(F
: System
.Address
) return HTable_Headers
is
298 function H
is new System
.HTable
.Hash
(HTable_Headers
);
299 Str
: constant Cstring_Ptr
:= To_Cstring_Ptr
(F
);
300 Res
: constant HTable_Headers
:= H
(Str
(1 .. Length
(Str
)));
309 procedure Set_HT_Link
(T
: Tag
; Next
: Tag
) is
311 TSD
(T
).HT_Link
:= Next
;
314 end HTable_Subprograms
;
320 -- Canonical implementation of Classwide Membership corresponding to:
324 -- Each dispatch table contains a reference to a table of ancestors (stored
325 -- in the first part of the Tags_Table) and a count of the level of
326 -- inheritance "Idepth".
328 -- Obj is in Typ'Class if Typ'Tag is in the table of ancestors that are
329 -- contained in the dispatch table referenced by Obj'Tag . Knowing the
330 -- level of inheritance of both types, this can be computed in constant
331 -- time by the formula:
333 -- Obj'tag.TSD.Ancestor_Tags (Obj'tag.TSD.Idepth - Typ'tag.TSD.Idepth)
336 function CW_Membership
(Obj_Tag
: Tag
; Typ_Tag
: Tag
) return Boolean is
337 Pos
: constant Integer := TSD
(Obj_Tag
).Idepth
- TSD
(Typ_Tag
).Idepth
;
339 return Pos
>= 0 and then TSD
(Obj_Tag
).Tags_Table
(Pos
) = Typ_Tag
;
346 -- Canonical implementation of Classwide Membership corresponding to:
348 -- Obj in Iface'Class
350 -- Each dispatch table contains a table with the tags of all the
351 -- implemented interfaces.
353 -- Obj is in Iface'Class if Iface'Tag is found in the table of interfaces
354 -- that are contained in the dispatch table referenced by Obj'Tag.
356 function IW_Membership
357 (This
: System
.Address
;
358 T
: Tag
) return Boolean
360 Curr_DT
: constant Tag
:= To_Tag_Ptr
(This
).all;
361 Obj_Base
: constant System
.Address
:= This
- Offset_To_Top
(Curr_DT
);
362 Obj_DT
: constant Tag
:= To_Tag_Ptr
(Obj_Base
).all;
364 Obj_TSD
: constant Type_Specific_Data_Ptr
:= TSD
(Obj_DT
);
365 Last_Id
: constant Natural := Obj_TSD
.Idepth
+ Obj_TSD
.Num_Interfaces
;
369 if Obj_TSD
.Num_Interfaces
> 0 then
371 -- Traverse the ancestor tags table plus the interface tags table.
372 -- The former part is required to give support to:
373 -- Iface_CW in Typ'Class
377 if Obj_TSD
.Tags_Table
(Id
) = T
then
382 exit when Id
> Last_Id
;
393 function Descendant_Tag
(External
: String; Ancestor
: Tag
) return Tag
is
394 Int_Tag
: constant Tag
:= Internal_Tag
(External
);
397 if not Is_Descendant_At_Same_Level
(Int_Tag
, Ancestor
) then
408 function Expanded_Name
(T
: Tag
) return String is
409 Result
: Cstring_Ptr
;
416 Result
:= TSD
(T
).Expanded_Name
;
417 return Result
(1 .. Length
(Result
));
424 function External_Tag
(T
: Tag
) return String is
425 Result
: Cstring_Ptr
;
431 Result
:= TSD
(T
).External_Tag
;
433 return Result
(1 .. Length
(Result
));
436 ----------------------
437 -- Get_Access_Level --
438 ----------------------
440 function Get_Access_Level
(T
: Tag
) return Natural is
442 return TSD
(T
).Access_Level
;
443 end Get_Access_Level
;
445 ---------------------
446 -- Get_Entry_Index --
447 ---------------------
449 function Get_Entry_Index
451 Position
: Positive) return Positive is
453 return TSD
(T
).Ent_Index_Table
(Position
- Default_Prim_Op_Count
);
456 ----------------------
457 -- Get_External_Tag --
458 ----------------------
460 function Get_External_Tag
(T
: Tag
) return System
.Address
is
462 return To_Address
(TSD
(T
).External_Tag
);
463 end Get_External_Tag
;
465 -------------------------
466 -- Get_Prim_Op_Address --
467 -------------------------
469 function Get_Prim_Op_Address
471 Position
: Positive) return System
.Address
is
473 return T
.Prims_Ptr
(Position
);
474 end Get_Prim_Op_Address
;
476 ----------------------
477 -- Get_Prim_Op_Kind --
478 ----------------------
480 function Get_Prim_Op_Kind
482 Position
: Positive) return Prim_Op_Kind
is
484 return TSD
(T
).POK_Table
(Position
- Default_Prim_Op_Count
);
485 end Get_Prim_Op_Kind
;
491 function Get_RC_Offset
(T
: Tag
) return SSE
.Storage_Offset
is
493 return TSD
(T
).RC_Offset
;
496 ---------------------------
497 -- Get_Remotely_Callable --
498 ---------------------------
500 function Get_Remotely_Callable
(T
: Tag
) return Boolean is
502 return TSD
(T
).Remotely_Callable
;
503 end Get_Remotely_Callable
;
512 Entry_Count
: Natural)
515 if Old_T
/= null then
516 New_T
.Prims_Ptr
(1 .. Entry_Count
) :=
517 Old_T
.Prims_Ptr
(1 .. Entry_Count
);
525 procedure Inherit_TSD
(Old_Tag
: Tag
; New_Tag
: Tag
) is
526 New_TSD_Ptr
: constant Type_Specific_Data_Ptr
:= TSD
(New_Tag
);
527 Old_TSD_Ptr
: Type_Specific_Data_Ptr
;
530 if Old_Tag
/= null then
531 Old_TSD_Ptr
:= TSD
(Old_Tag
);
532 New_TSD_Ptr
.Idepth
:= Old_TSD_Ptr
.Idepth
+ 1;
533 New_TSD_Ptr
.Num_Interfaces
:= Old_TSD_Ptr
.Num_Interfaces
;
535 -- Copy the "table of ancestor tags" plus the "table of interfaces"
538 New_TSD_Ptr
.Tags_Table
539 (1 .. New_TSD_Ptr
.Idepth
+ New_TSD_Ptr
.Num_Interfaces
) :=
540 Old_TSD_Ptr
.Tags_Table
541 (0 .. Old_TSD_Ptr
.Idepth
+ Old_TSD_Ptr
.Num_Interfaces
);
543 New_TSD_Ptr
.Idepth
:= 0;
544 New_TSD_Ptr
.Num_Interfaces
:= 0;
547 New_TSD_Ptr
.Tags_Table
(0) := New_Tag
;
554 function Internal_Tag
(External
: String) return Tag
is
555 Ext_Copy
: aliased String (External
'First .. External
'Last + 1);
559 -- Make a copy of the string representing the external tag with
562 Ext_Copy
(External
'Range) := External
;
563 Ext_Copy
(Ext_Copy
'Last) := ASCII
.NUL
;
564 Res
:= External_Tag_HTable
.Get
(Ext_Copy
'Address);
568 Msg1
: constant String := "unknown tagged type: ";
569 Msg2
: String (1 .. Msg1
'Length + External
'Length);
571 Msg2
(1 .. Msg1
'Length) := Msg1
;
572 Msg2
(Msg1
'Length + 1 .. Msg1
'Length + External
'Length) :=
574 Ada
.Exceptions
.Raise_Exception
(Tag_Error
'Identity, Msg2
);
581 ---------------------------------
582 -- Is_Descendant_At_Same_Level --
583 ---------------------------------
585 function Is_Descendant_At_Same_Level
587 Ancestor
: Tag
) return Boolean
590 return CW_Membership
(Descendant
, Ancestor
)
591 and then TSD
(Descendant
).Access_Level
= TSD
(Ancestor
).Access_Level
;
592 end Is_Descendant_At_Same_Level
;
598 function Length
(Str
: Cstring_Ptr
) return Natural is
602 while Str
(Len
) /= ASCII
.Nul
loop
613 function Offset_To_Top
614 (T
: Tag
) return System
.Storage_Elements
.Storage_Offset
616 Offset_To_Top_Ptr
: constant Storage_Offset_Ptr
:=
617 To_Storage_Offset_Ptr
(To_Address
(T
)
618 - DT_Typeinfo_Ptr_Size
619 - DT_Offset_To_Top_Size
);
621 return Offset_To_Top_Ptr
.all;
629 is access function (A
: System
.Address
) return Long_Long_Integer;
631 function To_Acc_Size
is new Unchecked_Conversion
(System
.Address
, Acc_Size
);
632 -- The profile of the implicitly defined _size primitive
635 (Obj
: System
.Address
;
636 T
: Tag
) return SSE
.Storage_Count
638 Parent_Tag
: constant Tag
:= TSD
(T
).Tags_Table
(1);
639 -- The tag of the parent type through the dispatch table
641 F
: constant Acc_Size
:= To_Acc_Size
(Parent_Tag
.Prims_Ptr
(1));
642 -- Access to the _size primitive of the parent. We assume that it is
643 -- always in the first slot of the dispatch table
646 -- Here we compute the size of the _parent field of the object
648 return SSE
.Storage_Count
(F
.all (Obj
));
655 function Parent_Tag
(T
: Tag
) return Tag
is
661 -- The Parent_Tag of a root-level tagged type is defined to be No_Tag.
662 -- The first entry in the Ancestors_Tags array will be null for such
663 -- a type, but it's better to be explicit about returning No_Tag in
666 if TSD
(T
).Idepth
= 0 then
669 return TSD
(T
).Tags_Table
(1);
673 ----------------------------
674 -- Register_Interface_Tag --
675 ----------------------------
677 procedure Register_Interface_Tag
681 New_T_TSD
: constant Type_Specific_Data_Ptr
:= TSD
(T
);
684 -- Check if the interface is already registered
686 if New_T_TSD
.Num_Interfaces
> 0 then
688 Id
: Natural := New_T_TSD
.Idepth
+ 1;
689 Last_Id
: constant Natural := New_T_TSD
.Idepth
690 + New_T_TSD
.Num_Interfaces
;
693 if New_T_TSD
.Tags_Table
(Id
) = Interface_T
then
698 exit when Id
> Last_Id
;
703 New_T_TSD
.Num_Interfaces
:= New_T_TSD
.Num_Interfaces
+ 1;
704 Index
:= New_T_TSD
.Idepth
+ New_T_TSD
.Num_Interfaces
;
705 New_T_TSD
.Tags_Table
(Index
) := Interface_T
;
706 end Register_Interface_Tag
;
712 procedure Register_Tag
(T
: Tag
) is
714 External_Tag_HTable
.Set
(T
);
717 ----------------------
718 -- Set_Access_Level --
719 ----------------------
721 procedure Set_Access_Level
(T
: Tag
; Value
: Natural) is
723 TSD
(T
).Access_Level
:= Value
;
724 end Set_Access_Level
;
726 ---------------------
727 -- Set_Entry_Index --
728 ---------------------
730 procedure Set_Entry_Index
735 TSD
(T
).Ent_Index_Table
(Position
- Default_Prim_Op_Count
) := Value
;
738 -----------------------
739 -- Set_Expanded_Name --
740 -----------------------
742 procedure Set_Expanded_Name
(T
: Tag
; Value
: System
.Address
) is
744 TSD
(T
).Expanded_Name
:= To_Cstring_Ptr
(Value
);
745 end Set_Expanded_Name
;
747 ----------------------
748 -- Set_External_Tag --
749 ----------------------
751 procedure Set_External_Tag
(T
: Tag
; Value
: System
.Address
) is
753 TSD
(T
).External_Tag
:= To_Cstring_Ptr
(Value
);
754 end Set_External_Tag
;
756 -----------------------
757 -- Set_Offset_To_Top --
758 -----------------------
760 procedure Set_Offset_To_Top
762 Value
: System
.Storage_Elements
.Storage_Offset
)
764 Offset_To_Top_Ptr
: constant Storage_Offset_Ptr
:=
765 To_Storage_Offset_Ptr
(To_Address
(T
)
766 - DT_Typeinfo_Ptr_Size
767 - DT_Offset_To_Top_Size
);
769 Offset_To_Top_Ptr
.all := Value
;
770 end Set_Offset_To_Top
;
772 -------------------------
773 -- Set_Prim_Op_Address --
774 -------------------------
776 procedure Set_Prim_Op_Address
779 Value
: System
.Address
) is
781 T
.Prims_Ptr
(Position
) := Value
;
782 end Set_Prim_Op_Address
;
784 ----------------------
785 -- Set_Prim_Op_Kind --
786 ----------------------
788 procedure Set_Prim_Op_Kind
791 Value
: Prim_Op_Kind
) is
793 TSD
(T
).POK_Table
(Position
- Default_Prim_Op_Count
) := Value
;
794 end Set_Prim_Op_Kind
;
800 procedure Set_RC_Offset
(T
: Tag
; Value
: SSE
.Storage_Offset
) is
802 TSD
(T
).RC_Offset
:= Value
;
805 ---------------------------
806 -- Set_Remotely_Callable --
807 ---------------------------
809 procedure Set_Remotely_Callable
(T
: Tag
; Value
: Boolean) is
811 TSD
(T
).Remotely_Callable
:= Value
;
812 end Set_Remotely_Callable
;
818 procedure Set_TSD
(T
: Tag
; Value
: System
.Address
) is
819 TSD_Ptr
: constant Addr_Ptr
:=
820 To_Addr_Ptr
(To_Address
(T
) - DT_Typeinfo_Ptr_Size
);
822 TSD_Ptr
.all := Value
;
829 function Typeinfo_Ptr
(T
: Tag
) return System
.Address
is
830 TSD_Ptr
: constant Addr_Ptr
:=
831 To_Addr_Ptr
(To_Address
(T
) - DT_Typeinfo_Ptr_Size
);
840 function TSD
(T
: Tag
) return Type_Specific_Data_Ptr
is
841 TSD_Ptr
: constant Addr_Ptr
:=
842 To_Addr_Ptr
(To_Address
(T
) - DT_Typeinfo_Ptr_Size
);
844 return To_Type_Specific_Data_Ptr
(TSD_Ptr
.all);