1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
9 -- Copyright (C) 1992-2004 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 ------------------------------------------------------------------------------
38 with Unchecked_Conversion
;
40 pragma Elaborate_All
(System
.HTable
);
42 package body Ada
.Tags
is
44 -- Structure of the GNAT Dispatch Table
46 -- +----------------------+
47 -- | TSD pointer ---|-----> Type Specific Data
48 -- +----------------------+ +-------------------+
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 -- +-------------------+
64 -- +-------------------+
66 subtype Cstring
is String (Positive);
67 type Cstring_Ptr
is access all Cstring
;
68 type Tag_Table
is array (Natural range <>) of Tag
;
69 pragma Suppress_Initialization
(Tag_Table
);
71 type Wide_Boolean
is new Boolean;
72 -- This name should probably be changed sometime ??? and indeed
73 -- probably this field could simply be of type Standard.Boolean.
75 type Type_Specific_Data
is record
77 Expanded_Name
: Cstring_Ptr
;
78 External_Tag
: Cstring_Ptr
;
80 Remotely_Callable
: Wide_Boolean
;
81 RC_Offset
: SSE
.Storage_Offset
;
82 Ancestor_Tags
: Tag_Table
(Natural);
85 type Dispatch_Table
is record
86 TSD
: Type_Specific_Data_Ptr
;
87 Prims_Ptr
: Address_Array
(Positive);
90 ---------------------------------------------
91 -- Unchecked Conversions for String Fields --
92 ---------------------------------------------
94 function To_Cstring_Ptr
is
95 new Unchecked_Conversion
(System
.Address
, Cstring_Ptr
);
97 function To_Address
is
98 new Unchecked_Conversion
(Cstring_Ptr
, System
.Address
);
100 -----------------------
101 -- Local Subprograms --
102 -----------------------
104 function Length
(Str
: Cstring_Ptr
) return Natural;
105 -- Length of string represented by the given pointer (treating the
106 -- string as a C-style string, which is Nul terminated).
108 -------------------------
109 -- External_Tag_HTable --
110 -------------------------
112 type HTable_Headers
is range 1 .. 64;
114 -- The following internal package defines the routines used for
115 -- the instantiation of a new System.HTable.Static_HTable (see
116 -- below). See spec in g-htable.ads for details of usage.
118 package HTable_Subprograms
is
119 procedure Set_HT_Link
(T
: Tag
; Next
: Tag
);
120 function Get_HT_Link
(T
: Tag
) return Tag
;
121 function Hash
(F
: System
.Address
) return HTable_Headers
;
122 function Equal
(A
, B
: System
.Address
) return Boolean;
123 end HTable_Subprograms
;
125 package External_Tag_HTable
is new System
.HTable
.Static_HTable
(
126 Header_Num
=> HTable_Headers
,
127 Element
=> Dispatch_Table
,
130 Set_Next
=> HTable_Subprograms
.Set_HT_Link
,
131 Next
=> HTable_Subprograms
.Get_HT_Link
,
132 Key
=> System
.Address
,
133 Get_Key
=> Get_External_Tag
,
134 Hash
=> HTable_Subprograms
.Hash
,
135 Equal
=> HTable_Subprograms
.Equal
);
137 ------------------------
138 -- HTable_Subprograms --
139 ------------------------
141 -- Bodies of routines for hash table instantiation
143 package body HTable_Subprograms
is
149 function Equal
(A
, B
: System
.Address
) return Boolean is
150 Str1
: constant Cstring_Ptr
:= To_Cstring_Ptr
(A
);
151 Str2
: constant Cstring_Ptr
:= To_Cstring_Ptr
(B
);
156 if Str1
(J
) /= Str2
(J
) then
159 elsif Str1
(J
) = ASCII
.NUL
then
172 function Get_HT_Link
(T
: Tag
) return Tag
is
174 return T
.TSD
.HT_Link
;
181 function Hash
(F
: System
.Address
) return HTable_Headers
is
182 function H
is new System
.HTable
.Hash
(HTable_Headers
);
183 Str
: constant Cstring_Ptr
:= To_Cstring_Ptr
(F
);
184 Res
: constant HTable_Headers
:= H
(Str
(1 .. Length
(Str
)));
194 procedure Set_HT_Link
(T
: Tag
; Next
: Tag
) is
196 T
.TSD
.HT_Link
:= Next
;
199 end HTable_Subprograms
;
205 -- Canonical implementation of Classwide Membership corresponding to:
209 -- Each dispatch table contains a reference to a table of ancestors
210 -- (Ancestor_Tags) and a count of the level of inheritance "Idepth" .
212 -- Obj is in Typ'Class if Typ'Tag is in the table of ancestors that are
213 -- contained in the dispatch table referenced by Obj'Tag . Knowing the
214 -- level of inheritance of both types, this can be computed in constant
215 -- time by the formula:
217 -- Obj'tag.TSD.Ancestor_Tags (Obj'tag.TSD.Idepth - Typ'tag.TSD.Idepth)
220 function CW_Membership
(Obj_Tag
: Tag
; Typ_Tag
: Tag
) return Boolean is
221 Pos
: constant Integer := Obj_Tag
.TSD
.Idepth
- Typ_Tag
.TSD
.Idepth
;
224 return Pos
>= 0 and then Obj_Tag
.TSD
.Ancestor_Tags
(Pos
) = Typ_Tag
;
231 function Expanded_Name
(T
: Tag
) return String is
232 Result
: constant Cstring_Ptr
:= T
.TSD
.Expanded_Name
;
235 return Result
(1 .. Length
(Result
));
242 function External_Tag
(T
: Tag
) return String is
243 Result
: constant Cstring_Ptr
:= T
.TSD
.External_Tag
;
246 return Result
(1 .. Length
(Result
));
249 -----------------------
250 -- Get_Expanded_Name --
251 -----------------------
253 function Get_Expanded_Name
(T
: Tag
) return System
.Address
is
255 return To_Address
(T
.TSD
.Expanded_Name
);
256 end Get_Expanded_Name
;
258 ----------------------
259 -- Get_External_Tag --
260 ----------------------
262 function Get_External_Tag
(T
: Tag
) return System
.Address
is
264 return To_Address
(T
.TSD
.External_Tag
);
265 end Get_External_Tag
;
267 ---------------------------
268 -- Get_Inheritance_Depth --
269 ---------------------------
271 function Get_Inheritance_Depth
(T
: Tag
) return Natural is
274 end Get_Inheritance_Depth
;
276 -------------------------
277 -- Get_Prim_Op_Address --
278 -------------------------
280 function Get_Prim_Op_Address
282 Position
: Positive) return System
.Address
285 return T
.Prims_Ptr
(Position
);
286 end Get_Prim_Op_Address
;
292 function Get_RC_Offset
(T
: Tag
) return SSE
.Storage_Offset
is
294 return T
.TSD
.RC_Offset
;
297 ---------------------------
298 -- Get_Remotely_Callable --
299 ---------------------------
301 function Get_Remotely_Callable
(T
: Tag
) return Boolean is
303 return T
.TSD
.Remotely_Callable
= True;
304 end Get_Remotely_Callable
;
310 function Get_TSD
(T
: Tag
) return System
.Address
is
312 return To_Address
(T
.TSD
);
322 Entry_Count
: Natural)
325 if Old_T
/= null then
326 New_T
.Prims_Ptr
(1 .. Entry_Count
) :=
327 Old_T
.Prims_Ptr
(1 .. Entry_Count
);
335 procedure Inherit_TSD
(Old_TSD
: System
.Address
; New_Tag
: Tag
) is
336 TSD
: constant Type_Specific_Data_Ptr
:=
337 To_Type_Specific_Data_Ptr
(Old_TSD
);
338 New_TSD
: Type_Specific_Data
renames New_Tag
.TSD
.all;
342 New_TSD
.Idepth
:= TSD
.Idepth
+ 1;
343 New_TSD
.Ancestor_Tags
(1 .. New_TSD
.Idepth
)
344 := TSD
.Ancestor_Tags
(0 .. TSD
.Idepth
);
349 New_TSD
.Ancestor_Tags
(0) := New_Tag
;
356 function Internal_Tag
(External
: String) return Tag
is
357 Ext_Copy
: aliased String (External
'First .. External
'Last + 1);
361 -- Make a copy of the string representing the external tag with
364 Ext_Copy
(External
'Range) := External
;
365 Ext_Copy
(Ext_Copy
'Last) := ASCII
.NUL
;
366 Res
:= External_Tag_HTable
.Get
(Ext_Copy
'Address);
370 Msg1
: constant String := "unknown tagged type: ";
371 Msg2
: String (1 .. Msg1
'Length + External
'Length);
374 Msg2
(1 .. Msg1
'Length) := Msg1
;
375 Msg2
(Msg1
'Length + 1 .. Msg1
'Length + External
'Length) :=
377 Ada
.Exceptions
.Raise_Exception
(Tag_Error
'Identity, Msg2
);
388 function Length
(Str
: Cstring_Ptr
) return Natural is
392 while Str
(Len
) /= ASCII
.Nul
loop
404 is access function (A
: System
.Address
) return Long_Long_Integer;
406 function To_Acc_Size
is new Unchecked_Conversion
(System
.Address
, Acc_Size
);
407 -- The profile of the implicitly defined _size primitive
410 (Obj
: System
.Address
;
411 T
: Tag
) return SSE
.Storage_Count
413 Parent_Tag
: constant Tag
:= T
.TSD
.Ancestor_Tags
(1);
414 -- The tag of the parent type through the dispatch table
416 F
: constant Acc_Size
:= To_Acc_Size
(Parent_Tag
.Prims_Ptr
(1));
417 -- Access to the _size primitive of the parent. We assume that
418 -- it is always in the first slot of the distatch table
421 -- Here we compute the size of the _parent field of the object
423 return SSE
.Storage_Count
(F
.all (Obj
));
430 function Parent_Tag
(T
: Tag
) return Tag
is
432 return T
.TSD
.Ancestor_Tags
(1);
439 procedure Register_Tag
(T
: Tag
) is
441 External_Tag_HTable
.Set
(T
);
444 -----------------------
445 -- Set_Expanded_Name --
446 -----------------------
448 procedure Set_Expanded_Name
(T
: Tag
; Value
: System
.Address
) is
450 T
.TSD
.Expanded_Name
:= To_Cstring_Ptr
(Value
);
451 end Set_Expanded_Name
;
453 ----------------------
454 -- Set_External_Tag --
455 ----------------------
457 procedure Set_External_Tag
(T
: Tag
; Value
: System
.Address
) is
459 T
.TSD
.External_Tag
:= To_Cstring_Ptr
(Value
);
460 end Set_External_Tag
;
462 ---------------------------
463 -- Set_Inheritance_Depth --
464 ---------------------------
466 procedure Set_Inheritance_Depth
471 T
.TSD
.Idepth
:= Value
;
472 end Set_Inheritance_Depth
;
474 -------------------------
475 -- Set_Prim_Op_Address --
476 -------------------------
478 procedure Set_Prim_Op_Address
481 Value
: System
.Address
)
484 T
.Prims_Ptr
(Position
) := Value
;
485 end Set_Prim_Op_Address
;
491 procedure Set_RC_Offset
(T
: Tag
; Value
: SSE
.Storage_Offset
) is
493 T
.TSD
.RC_Offset
:= Value
;
496 ---------------------------
497 -- Set_Remotely_Callable --
498 ---------------------------
500 procedure Set_Remotely_Callable
(T
: Tag
; Value
: Boolean) is
503 T
.TSD
.Remotely_Callable
:= True;
505 T
.TSD
.Remotely_Callable
:= False;
507 end Set_Remotely_Callable
;
513 procedure Set_TSD
(T
: Tag
; Value
: System
.Address
) is
515 T
.TSD
:= To_Type_Specific_Data_Ptr
(Value
);