Debian Codegraph @

gcc/ada/libgnat/a-tags.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- A D A . T A G S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, 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. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 with Ada.Exceptions;
33
34 with System.HTable;
35 with System.Storage_Elements; use System.Storage_Elements;
36 with System.WCh_Con; use System.WCh_Con;
37 with System.WCh_StW; use System.WCh_StW;
38
39 pragma Elaborate (System.HTable);
40 -- Elaborate needed instead of Elaborate_All to avoid elaboration cycles
41 -- when polling is turned on. This is safe because HTable doesn't do anything
42 -- at elaboration time; it just contains a generic package we want to
43 -- instantiate.
44
45 package body Ada.Tags is
46
47 -----------------------
48 -- Local Subprograms --
49 -----------------------
50
51 function Get_External_Tag (T : Tag) return System.Address;
52 -- Returns address of a null terminated string containing the external name
53
54 function Is_Primary_DT (T : Tag) return Boolean;
55 -- Given a tag returns True if it has the signature of a primary dispatch
56 -- table. This is Inline_Always since it is called from other Inline_
57 -- Always subprograms where we want no out of line code to be generated.
58
59 function IW_Membership
60 (Descendant_TSD : Type_Specific_Data_Ptr;
61 T : Tag) return Boolean;
62 -- Subsidiary function of IW_Membership and CW_Membership which factorizes
63 -- the functionality needed to check if a given descendant implements an
64 -- interface tag T.
65
66 function Length (Str : Cstring_Ptr) return Natural;
67 -- Length of string represented by the given pointer (treating the string
68 -- as a C-style string, which is Nul terminated). See comment in body
69 -- explaining why we cannot use the normal strlen built-in.
70
71 function OSD (T : Tag) return Object_Specific_Data_Ptr;
72 -- Ada 2005 (AI-251): Given a pointer T to a secondary dispatch table,
73 -- retrieve the address of the record containing the Object Specific
74 -- Data table.
75
76 function SSD (T : Tag) return Select_Specific_Data_Ptr;
77 -- Ada 2005 (AI-251): Given a pointer T to a dispatch Table, retrieves the
78 -- address of the record containing the Select Specific Data in T's TSD.
79
80 pragma Inline_Always (Get_External_Tag);
81 pragma Inline_Always (Is_Primary_DT);
82 pragma Inline_Always (OSD);
83 pragma Inline_Always (SSD);
84
85 -- Unchecked conversions
86
87 function To_Address is
88 new Unchecked_Conversion (Cstring_Ptr, System.Address);
89
90 function To_Cstring_Ptr is
91 new Unchecked_Conversion (System.Address, Cstring_Ptr);
92
93 -- Disable warnings on possible aliasing problem
94
95 function To_Tag is
96 new Unchecked_Conversion (System.Address, Tag);
97
98 function To_Tag (S : Integer_Address) return Tag is
99 (To_Tag (To_Address (S)));
100
101 function To_Dispatch_Table_Ptr is
102 new Ada.Unchecked_Conversion (Tag, Dispatch_Table_Ptr);
103
104 function To_Dispatch_Table_Ptr is
105 new Ada.Unchecked_Conversion (System.Address, Dispatch_Table_Ptr);
106
107 function To_Object_Specific_Data_Ptr is
108 new Ada.Unchecked_Conversion (System.Address, Object_Specific_Data_Ptr);
109
110 function To_Tag_Ptr is
111 new Ada.Unchecked_Conversion (System.Address, Tag_Ptr);
112
113 -------------------------------
114 -- Inline_Always Subprograms --
115 -------------------------------
116
117 -- Inline_always subprograms must be placed before their first call to
118 -- avoid defeating the frontend inlining mechanism and thus ensure the
119 -- generation of their correct debug info.
120
121 ----------------------
122 -- Get_External_Tag --
123 ----------------------
124
125 function Get_External_Tag (T : Tag) return System.Address is
126 TSD_Ptr : constant Addr_Ptr :=
127 To_Addr_Ptr (To_Address (T) - DT_Typeinfo_Ptr_Size);
128 TSD : constant Type_Specific_Data_Ptr :=
129 To_Type_Specific_Data_Ptr (TSD_Ptr.all);
130 begin
131 return To_Address (TSD.External_Tag);
132 end Get_External_Tag;
133
134 -----------------
135 -- Is_Abstract --
136 -----------------
137
138 function Is_Abstract (T : Tag) return Boolean is
139 TSD_Ptr : Addr_Ptr;
140 TSD : Type_Specific_Data_Ptr;
141
142 begin
143 if T = No_Tag then
144 raise Tag_Error;
145 end if;
146
147 TSD_Ptr := To_Addr_Ptr (To_Address (T) - DT_Typeinfo_Ptr_Size);
148 TSD := To_Type_Specific_Data_Ptr (TSD_Ptr.all);
149 return TSD.Is_Abstract;
150 end Is_Abstract;
151
152 -------------------
153 -- Is_Primary_DT --
154 -------------------
155
156 function Is_Primary_DT (T : Tag) return Boolean is
157 begin
158 return DT (T).Signature = Primary_DT;
159 end Is_Primary_DT;
160
161 ---------
162 -- OSD --
163 ---------
164
165 function OSD (T : Tag) return Object_Specific_Data_Ptr is
166 OSD_Ptr : constant Addr_Ptr :=
167 To_Addr_Ptr (To_Address (T) - DT_Typeinfo_Ptr_Size);
168 begin
169 return To_Object_Specific_Data_Ptr (OSD_Ptr.all);
170 end OSD;
171
172 ---------
173 -- SSD --
174 ---------
175
176 function SSD (T : Tag) return Select_Specific_Data_Ptr is
177 TSD_Ptr : constant Addr_Ptr :=
178 To_Addr_Ptr (To_Address (T) - DT_Typeinfo_Ptr_Size);
179 TSD : constant Type_Specific_Data_Ptr :=
180 To_Type_Specific_Data_Ptr (TSD_Ptr.all);
181 begin
182 return TSD.SSD;
183 end SSD;
184
185 -------------------------
186 -- External_Tag_HTable --
187 -------------------------
188
189 type HTable_Headers is range 1 .. 64;
190
191 -- The following internal package defines the routines used for the
192 -- instantiation of a new System.HTable.Static_HTable (see below). See
193 -- spec in g-htable.ads for details of usage.
194
195 package HTable_Subprograms is
196 procedure Set_HT_Link (T : Tag; Next : Tag);
197 function Get_HT_Link (T : Tag) return Tag;
198 function Hash (F : System.Address) return HTable_Headers;
199 function Equal (A, B : System.Address) return Boolean;
200 end HTable_Subprograms;
201
202 package External_Tag_HTable is new System.HTable.Static_HTable (
203 Header_Num => HTable_Headers,
204 Element => Dispatch_Table,
205 Elmt_Ptr => Tag,
206 Null_Ptr => null,
207 Set_Next => HTable_Subprograms.Set_HT_Link,
208 Next => HTable_Subprograms.Get_HT_Link,
209 Key => System.Address,
210 Get_Key => Get_External_Tag,
211 Hash => HTable_Subprograms.Hash,
212 Equal => HTable_Subprograms.Equal);
213
214 ------------------------
215 -- HTable_Subprograms --
216 ------------------------
217
218 -- Bodies of routines for hash table instantiation
219
220 package body HTable_Subprograms is
221
222 -----------
223 -- Equal --
224 -----------
225
226 function Equal (A, B : System.Address) return Boolean is
227 Str1 : constant Cstring_Ptr := To_Cstring_Ptr (A);
228 Str2 : constant Cstring_Ptr := To_Cstring_Ptr (B);
229 J : Integer;
230 begin
231 J := 1;
232 loop
233 if Str1 (J) /= Str2 (J) then
234 return False;
235 elsif Str1 (J) = ASCII.NUL then
236 return True;
237 else
238 J := J + 1;
239 end if;
240 end loop;
241 end Equal;
242
243 -----------------
244 -- Get_HT_Link --
245 -----------------
246
247 function Get_HT_Link (T : Tag) return Tag is
248 TSD_Ptr : constant Addr_Ptr :=
249 To_Addr_Ptr (To_Address (T) - DT_Typeinfo_Ptr_Size);
250 TSD : constant Type_Specific_Data_Ptr :=
251 To_Type_Specific_Data_Ptr (TSD_Ptr.all);
252 begin
253 return TSD.HT_Link.all;
254 end Get_HT_Link;
255
256 ----------
257 -- Hash --
258 ----------
259
260 function Hash (F : System.Address) return HTable_Headers is
261 function H is new System.HTable.Hash (HTable_Headers);
262 Str : constant Cstring_Ptr := To_Cstring_Ptr (F);
263 Res : constant HTable_Headers := H (Str (1 .. Length (Str)));
264 begin
265 return Res;
266 end Hash;
267
268 -----------------
269 -- Set_HT_Link --
270 -----------------
271
272 procedure Set_HT_Link (T : Tag; Next : Tag) is
273 TSD_Ptr : constant Addr_Ptr :=
274 To_Addr_Ptr (To_Address (T) - DT_Typeinfo_Ptr_Size);
275 TSD : constant Type_Specific_Data_Ptr :=
276 To_Type_Specific_Data_Ptr (TSD_Ptr.all);
277 begin
278 TSD.HT_Link.all := Next;
279 end Set_HT_Link;
280
281 end HTable_Subprograms;
282
283 ------------------
284 -- Base_Address --
285 ------------------
286
287 function Base_Address (This : System.Address) return System.Address is
288 begin
289 return This + Offset_To_Top (This);
290 end Base_Address;
291
292 ---------------
293 -- Check_TSD --
294 ---------------
295
296 procedure Check_TSD (TSD : Type_Specific_Data_Ptr) is
297 T : Tag;
298
299 E_Tag_Len : constant Integer := Length (TSD.External_Tag);
300 E_Tag : String (1 .. E_Tag_Len);
301 for E_Tag'Address use TSD.External_Tag.all'Address;
302 pragma Import (Ada, E_Tag);
303
304 Dup_Ext_Tag : constant String := "duplicated external tag """;
305
306 begin
307 -- Verify that the external tag of this TSD is not registered in the
308 -- runtime hash table.
309
310 T := External_Tag_HTable.Get (To_Address (TSD.External_Tag));
311
312 if T /= null then
313
314 -- Avoid concatenation, as it is not allowed in no run time mode
315
316 declare
317 Msg : String (1 .. Dup_Ext_Tag'Length + E_Tag_Len + 1);
318 begin
319 Msg (1 .. Dup_Ext_Tag'Length) := Dup_Ext_Tag;
320 Msg (Dup_Ext_Tag'Length + 1 .. Dup_Ext_Tag'Length + E_Tag_Len) :=
321 E_Tag;
322 Msg (Msg'Last) := '"';
323 raise Program_Error with Msg;
324 end;
325 end if;
326 end Check_TSD;
327
328 --------------------
329 -- Descendant_Tag --
330 --------------------
331
332 function Descendant_Tag (External : String; Ancestor : Tag) return Tag is
333 Int_Tag : constant Tag := Internal_Tag (External);
334 begin
335 if not Is_Descendant_At_Same_Level (Int_Tag, Ancestor) then
336 raise Tag_Error;
337 else
338 return Int_Tag;
339 end if;
340 end Descendant_Tag;
341
342 --------------
343 -- Displace --
344 --------------
345
346 function Displace (This : System.Address; T : Tag) return System.Address is
347 Iface_Table : Interface_Data_Ptr;
348 Obj_Base : System.Address;
349 Obj_DT : Dispatch_Table_Ptr;
350 Obj_DT_Tag : Tag;
351
352 begin
353 if System."=" (This, System.Null_Address) then
354 return System.Null_Address;
355 end if;
356
357 Obj_Base := Base_Address (This);
358 Obj_DT_Tag := To_Tag_Ptr (Obj_Base).all;
359 Obj_DT := DT (To_Tag_Ptr (Obj_Base).all);
360 Iface_Table := To_Type_Specific_Data_Ptr (Obj_DT.TSD).Interfaces_Table;
361
362 if Iface_Table /= null then
363 for Id in 1 .. Iface_Table.Nb_Ifaces loop
364 if Iface_Table.Ifaces_Table (Id).Iface_Tag = T then
365
366 -- Case of Static value of Offset_To_Top
367
368 if Iface_Table.Ifaces_Table (Id).Static_Offset_To_Top then
369 Obj_Base := Obj_Base -
370 Iface_Table.Ifaces_Table (Id).Offset_To_Top_Value;
371
372 -- Otherwise call the function generated by the expander to
373 -- provide the value.
374
375 else
376 Obj_Base := Obj_Base -
377 Iface_Table.Ifaces_Table (Id).Offset_To_Top_Func.all
378 (Obj_Base);
379 end if;
380
381 return Obj_Base;
382 end if;
383 end loop;
384 end if;
385
386 -- Check if T is an immediate ancestor. This is required to handle
387 -- conversion of class-wide interfaces to tagged types.
388
389 if CW_Membership (Obj_DT_Tag, T) then
390 return Obj_Base;
391 end if;
392
393 -- If the object does not implement the interface we must raise CE
394
395 raise Constraint_Error with "invalid interface conversion";
396 end Displace;
397
398 --------
399 -- DT --
400 --------
401
402 function DT (T : Tag) return Dispatch_Table_Ptr is
403 Offset : constant SSE.Storage_Offset :=
404 To_Dispatch_Table_Ptr (T).Prims_Ptr'Position;
405 begin
406 return To_Dispatch_Table_Ptr (To_Address (T) - Offset);
407 end DT;
408
409 -------------------
410 -- IW_Membership --
411 -------------------
412
413 function IW_Membership
414 (Descendant_TSD : Type_Specific_Data_Ptr;
415 T : Tag) return Boolean
416 is
417 Iface_Table : Interface_Data_Ptr;
418
419 begin
420 Iface_Table := Descendant_TSD.Interfaces_Table;
421
422 if Iface_Table /= null then
423 for Id in 1 .. Iface_Table.Nb_Ifaces loop
424 if Iface_Table.Ifaces_Table (Id).Iface_Tag = T then
425 return True;
426 end if;
427 end loop;
428 end if;
429
430 -- Look for the tag in the ancestor tags table. This is required for:
431 -- Iface_CW in Typ'Class
432
433 for Id in 0 .. Descendant_TSD.Idepth loop
434 if Descendant_TSD.Tags_Table (Id) = T then
435 return True;
436 end if;
437 end loop;
438
439 return False;
440 end IW_Membership;
441
442 -------------------
443 -- IW_Membership --
444 -------------------
445
446 -- Canonical implementation of Classwide Membership corresponding to:
447
448 -- Obj in Iface'Class
449
450 -- Each dispatch table contains a table with the tags of all the
451 -- implemented interfaces.
452
453 -- Obj is in Iface'Class if Iface'Tag is found in the table of interfaces
454 -- that are contained in the dispatch table referenced by Obj'Tag.
455
456 function IW_Membership (This : System.Address; T : Tag) return Boolean is
457 Obj_Base : System.Address;
458 Obj_DT : Dispatch_Table_Ptr;
459 Obj_TSD : Type_Specific_Data_Ptr;
460
461 begin
462 Obj_Base := Base_Address (This);
463 Obj_DT := DT (To_Tag_Ptr (Obj_Base).all);
464 Obj_TSD := To_Type_Specific_Data_Ptr (Obj_DT.TSD);
465
466 return IW_Membership (Obj_TSD, T);
467 end IW_Membership;
468
469 -------------------
470 -- Expanded_Name --
471 -------------------
472
473 function Expanded_Name (T : Tag) return String is
474 Result : Cstring_Ptr;
475 TSD_Ptr : Addr_Ptr;
476 TSD : Type_Specific_Data_Ptr;
477
478 begin
479 if T = No_Tag then
480 raise Tag_Error;
481 end if;
482
483 TSD_Ptr := To_Addr_Ptr (To_Address (T) - DT_Typeinfo_Ptr_Size);
484 TSD := To_Type_Specific_Data_Ptr (TSD_Ptr.all);
485 Result := TSD.Expanded_Name;
486 return Result (1 .. Length (Result));
487 end Expanded_Name;
488
489 ------------------
490 -- External_Tag --
491 ------------------
492
493 function External_Tag (T : Tag) return String is
494 Result : Cstring_Ptr;
495 TSD_Ptr : Addr_Ptr;
496 TSD : Type_Specific_Data_Ptr;
497
498 begin
499 if T = No_Tag then
500 raise Tag_Error;
501 end if;
502
503 TSD_Ptr := To_Addr_Ptr (To_Address (T) - DT_Typeinfo_Ptr_Size);
504 TSD := To_Type_Specific_Data_Ptr (TSD_Ptr.all);
505 Result := TSD.External_Tag;
506 return Result (1 .. Length (Result));
507 end External_Tag;
508
509 ---------------------
510 -- Get_Entry_Index --
511 ---------------------
512
513 function Get_Entry_Index (T : Tag; Position : Positive) return Positive is
514 begin
515 return SSD (T).SSD_Table (Position).Index;
516 end Get_Entry_Index;
517
518 ----------------------
519 -- Get_Prim_Op_Kind --
520 ----------------------
521
522 function Get_Prim_Op_Kind
523 (T : Tag;
524 Position : Positive) return Prim_Op_Kind
525 is
526 begin
527 return SSD (T).SSD_Table (Position).Kind;
528 end Get_Prim_Op_Kind;
529
530 ----------------------
531 -- Get_Offset_Index --
532 ----------------------
533
534 function Get_Offset_Index
535 (T : Tag;
536 Position : Positive) return Positive
537 is
538 begin
539 if Is_Primary_DT (T) then
540 return Position;
541 else
542 return OSD (T).OSD_Table (Position);
543 end if;
544 end Get_Offset_Index;
545
546 ---------------------
547 -- Get_Tagged_Kind --
548 ---------------------
549
550 function Get_Tagged_Kind (T : Tag) return Tagged_Kind is
551 begin
552 return DT (T).Tag_Kind;
553 end Get_Tagged_Kind;
554
555 -----------------------------
556 -- Interface_Ancestor_Tags --
557 -----------------------------
558
559 function Interface_Ancestor_Tags (T : Tag) return Tag_Array is
560 TSD_Ptr : Addr_Ptr;
561 TSD : Type_Specific_Data_Ptr;
562 Iface_Table : Interface_Data_Ptr;
563 begin
564 if T = No_Tag then
565 raise Tag_Error;
566 end if;
567
568 TSD_Ptr := To_Addr_Ptr (To_Address (T) - DT_Typeinfo_Ptr_Size);
569 TSD := To_Type_Specific_Data_Ptr (TSD_Ptr.all);
570 Iface_Table := TSD.Interfaces_Table;
571
572 if Iface_Table = null then
573 declare
574 Table : Tag_Array (1 .. 0);
575 begin
576 return Table;
577 end;
578
579 else
580 declare
581 Table : Tag_Array (1 .. Iface_Table.Nb_Ifaces);
582 begin
583 for J in 1 .. Iface_Table.Nb_Ifaces loop
584 Table (J) := Iface_Table.Ifaces_Table (J).Iface_Tag;
585 end loop;
586
587 return Table;
588 end;
589 end if;
590 end Interface_Ancestor_Tags;
591
592 ------------------
593 -- Internal_Tag --
594 ------------------
595
596 -- Internal tags have the following format:
597 -- "Internal tag at 16#ADDRESS#: <full-name-of-tagged-type>"
598
599 Internal_Tag_Header : constant String := "Internal tag at ";
600 Header_Separator : constant Character := '#';
601
602 function Internal_Tag (External : String) return Tag is
603 pragma Unsuppress (All_Checks);
604 -- To make T'Class'Input robust in the case of bad data
605
606 Res : Tag := null;
607
608 begin
609 -- Raise Tag_Error for empty strings and very long strings. This makes
610 -- T'Class'Input robust in the case of bad data, for example
611 --
612 -- String (123456789..1234)
613 --
614 -- The limit of 10,000 characters is arbitrary, but is unlikely to be
615 -- exceeded by legitimate external tag names.
616
617 if External'Length not in 1 .. 10_000 then
618 raise Tag_Error;
619 end if;
620
621 -- Handle locally defined tagged types
622
623 if External'Length > Internal_Tag_Header'Length
624 and then
625 External (External'First ..
626 External'First + Internal_Tag_Header'Length - 1) =
627 Internal_Tag_Header
628 then
629 declare
630 Addr_First : constant Natural :=
631 External'First + Internal_Tag_Header'Length;
632 Addr_Last : Natural;
633 Addr : Integer_Address;
634
635 begin
636 -- Search the second separator (#) to identify the address
637
638 Addr_Last := Addr_First;
639
640 for J in 1 .. 2 loop
641 while Addr_Last <= External'Last
642 and then External (Addr_Last) /= Header_Separator
643 loop
644 Addr_Last := Addr_Last + 1;
645 end loop;
646
647 -- Skip the first separator
648
649 if J = 1 then
650 Addr_Last := Addr_Last + 1;
651 end if;
652 end loop;
653
654 if Addr_Last <= External'Last then
655
656 -- Protect the run-time against wrong internal tags. We
657 -- cannot use exception handlers here because it would
658 -- disable the use of this run-time compiling with
659 -- restriction No_Exception_Handler.
660
661 declare
662 C : Character;
663 Wrong_Tag : Boolean := False;
664
665 begin
666 if External (Addr_First) /= '1'
667 or else External (Addr_First + 1) /= '6'
668 or else External (Addr_First + 2) /= '#'
669 then
670 Wrong_Tag := True;
671
672 else
673 for J in Addr_First + 3 .. Addr_Last - 1 loop
674 C := External (J);
675
676 if not (C in '0' .. '9')
677 and then not (C in 'A' .. 'F')
678 and then not (C in 'a' .. 'f')
679 then
680 Wrong_Tag := True;
681 exit;
682 end if;
683 end loop;
684 end if;
685
686 -- Convert the numeric value into a tag
687
688 if not Wrong_Tag then
689 Addr := Integer_Address'Value
690 (External (Addr_First .. Addr_Last));
691
692 -- Internal tags never have value 0
693
694 if Addr /= 0 then
695 return To_Tag (Addr);
696 end if;
697 end if;
698 end;
699 end if;
700 end;
701
702 -- Handle library-level tagged types
703
704 else
705 -- Make NUL-terminated copy of external tag string
706
707 declare
708 Ext_Copy : aliased String (External'First .. External'Last + 1);
709 pragma Assert (Ext_Copy'Length > 1); -- See Length check at top
710 begin
711 Ext_Copy (External'Range) := External;
712 Ext_Copy (Ext_Copy'Last) := ASCII.NUL;
713 Res := External_Tag_HTable.Get (Ext_Copy'Address);
714 end;
715 end if;
716
717 if Res = null then
718 declare
719 Msg1 : constant String := "unknown tagged type: ";
720 Msg2 : String (1 .. Msg1'Length + External'Length);
721
722 begin
723 Msg2 (1 .. Msg1'Length) := Msg1;
724 Msg2 (Msg1'Length + 1 .. Msg1'Length + External'Length) :=
725 External;
726 Ada.Exceptions.Raise_Exception (Tag_Error'Identity, Msg2);
727 end;
728 end if;
729
730 return Res;
731 end Internal_Tag;
732
733 ---------------------------------
734 -- Is_Descendant_At_Same_Level --
735 ---------------------------------
736
737 function Is_Descendant_At_Same_Level
738 (Descendant : Tag;
739 Ancestor : Tag) return Boolean
740 is
741 begin
742 if Descendant = No_Tag or else Ancestor = No_Tag then
743 raise Tag_Error;
744
745 elsif Descendant = Ancestor then
746 return True;
747
748 else
749 declare
750 D_TSD_Ptr : constant Addr_Ptr :=
751 To_Addr_Ptr (To_Address (Descendant) - DT_Typeinfo_Ptr_Size);
752 A_TSD_Ptr : constant Addr_Ptr :=
753 To_Addr_Ptr (To_Address (Ancestor) - DT_Typeinfo_Ptr_Size);
754 D_TSD : constant Type_Specific_Data_Ptr :=
755 To_Type_Specific_Data_Ptr (D_TSD_Ptr.all);
756 A_TSD : constant Type_Specific_Data_Ptr :=
757 To_Type_Specific_Data_Ptr (A_TSD_Ptr.all);
758 begin
759 return
760 D_TSD.Access_Level = A_TSD.Access_Level
761 and then (CW_Membership (Descendant, Ancestor)
762 or else IW_Membership (D_TSD, Ancestor));
763 end;
764 end if;
765 end Is_Descendant_At_Same_Level;
766
767 ------------
768 -- Length --
769 ------------
770
771 -- Note: This unit is used in the Ravenscar runtime library, so it cannot
772 -- depend on System.CTRL. Furthermore, this happens on CPUs where the GCC
773 -- intrinsic strlen may not be available, so we need to recode our own Ada
774 -- version here.
775
776 function Length (Str : Cstring_Ptr) return Natural is
777 Len : Integer;
778
779 begin
780 Len := 1;
781 while Str (Len) /= ASCII.NUL loop
782 Len := Len + 1;
783 end loop;
784
785 return Len - 1;
786 end Length;
787
788 -------------------
789 -- Offset_To_Top --
790 -------------------
791
792 function Offset_To_Top
793 (This : System.Address) return SSE.Storage_Offset
794 is
795 Tag_Size : constant SSE.Storage_Count :=
796 SSE.Storage_Count (1 * (Standard'Address_Size / System.Storage_Unit));
797
798 type Storage_Offset_Ptr is access SSE.Storage_Offset;
799 function To_Storage_Offset_Ptr is
800 new Unchecked_Conversion (System.Address, Storage_Offset_Ptr);
801
802 Curr_DT : Dispatch_Table_Ptr;
803
804 begin
805 Curr_DT := DT (To_Tag_Ptr (This).all);
806
807 -- See the documentation of Dispatch_Table_Wrapper.Offset_To_Top
808
809 if Curr_DT.Offset_To_Top = SSE.Storage_Offset'Last then
810
811 -- The parent record type has variable-size components, so the
812 -- instance-specific offset is stored in the tagged record, right
813 -- after the reference to Curr_DT (which is a secondary dispatch
814 -- table).
815
816 return To_Storage_Offset_Ptr (This + Tag_Size).all;
817
818 else
819 -- The offset is compile-time known, so it is simply stored in the
820 -- Offset_To_Top field.
821
822 return Curr_DT.Offset_To_Top;
823 end if;
824 end Offset_To_Top;
825
826 ------------------------
827 -- Needs_Finalization --
828 ------------------------
829
830 function Needs_Finalization (T : Tag) return Boolean is
831 TSD_Ptr : constant Addr_Ptr :=
832 To_Addr_Ptr (To_Address (T) - DT_Typeinfo_Ptr_Size);
833 TSD : constant Type_Specific_Data_Ptr :=
834 To_Type_Specific_Data_Ptr (TSD_Ptr.all);
835 begin
836 return TSD.Needs_Finalization;
837 end Needs_Finalization;
838
839 -----------------
840 -- Parent_Size --
841 -----------------
842
843 function Parent_Size
844 (Obj : System.Address;
845 T : Tag) return SSE.Storage_Count
846 is
847 Parent_Slot : constant Positive := 1;
848 -- The tag of the parent is always in the first slot of the table of
849 -- ancestor tags.
850
851 TSD_Ptr : constant Addr_Ptr :=
852 To_Addr_Ptr (To_Address (T) - DT_Typeinfo_Ptr_Size);
853 TSD : constant Type_Specific_Data_Ptr :=
854 To_Type_Specific_Data_Ptr (TSD_Ptr.all);
855 -- Pointer to the TSD
856
857 Parent_Tag : constant Tag := TSD.Tags_Table (Parent_Slot);
858 Parent_TSD_Ptr : constant Addr_Ptr :=
859 To_Addr_Ptr (To_Address (Parent_Tag) - DT_Typeinfo_Ptr_Size);
860 Parent_TSD : constant Type_Specific_Data_Ptr :=
861 To_Type_Specific_Data_Ptr (Parent_TSD_Ptr.all);
862
863 begin
864 -- Here we compute the size of the _parent field of the object
865
866 return SSE.Storage_Count (Parent_TSD.Size_Func.all (Obj));
867 end Parent_Size;
868
869 ----------------
870 -- Parent_Tag --
871 ----------------
872
873 function Parent_Tag (T : Tag) return Tag is
874 TSD_Ptr : Addr_Ptr;
875 TSD : Type_Specific_Data_Ptr;
876
877 begin
878 if T = No_Tag then
879 raise Tag_Error;
880 end if;
881
882 TSD_Ptr := To_Addr_Ptr (To_Address (T) - DT_Typeinfo_Ptr_Size);
883 TSD := To_Type_Specific_Data_Ptr (TSD_Ptr.all);
884
885 -- The Parent_Tag of a root-level tagged type is defined to be No_Tag.
886 -- The first entry in the Ancestors_Tags array will be null for such
887 -- a type, but it's better to be explicit about returning No_Tag in
888 -- this case.
889
890 if TSD.Idepth = 0 then
891 return No_Tag;
892 else
893 return TSD.Tags_Table (1);
894 end if;
895 end Parent_Tag;
896
897 -------------------------------
898 -- Register_Interface_Offset --
899 -------------------------------
900
901 procedure Register_Interface_Offset
902 (Prim_T : Tag;
903 Interface_T : Tag;
904 Is_Static : Boolean;
905 Offset_Value : SSE.Storage_Offset;
906 Offset_Func : Offset_To_Top_Function_Ptr)
907 is
908 Prim_DT : constant Dispatch_Table_Ptr := DT (Prim_T);
909 Iface_Table : constant Interface_Data_Ptr :=
910 To_Type_Specific_Data_Ptr (Prim_DT.TSD).Interfaces_Table;
911
912 begin
913 -- Save Offset_Value in the table of interfaces of the primary DT.
914 -- This data will be used by the subprogram "Displace" to give support
915 -- to backward abstract interface type conversions.
916
917 -- Register the offset in the table of interfaces
918
919 if Iface_Table /= null then
920 for Id in 1 .. Iface_Table.Nb_Ifaces loop
921 if Iface_Table.Ifaces_Table (Id).Iface_Tag = Interface_T then
922 if Is_Static or else Offset_Value = 0 then
923 Iface_Table.Ifaces_Table (Id).Static_Offset_To_Top := True;
924 Iface_Table.Ifaces_Table (Id).Offset_To_Top_Value :=
925 Offset_Value;
926 else
927 Iface_Table.Ifaces_Table (Id).Static_Offset_To_Top := False;
928 Iface_Table.Ifaces_Table (Id).Offset_To_Top_Func :=
929 Offset_Func;
930 end if;
931
932 return;
933 end if;
934 end loop;
935 end if;
936
937 -- If we arrive here there is some error in the run-time data structure
938
939 raise Program_Error;
940 end Register_Interface_Offset;
941
942 ------------------
943 -- Register_Tag --
944 ------------------
945
946 procedure Register_Tag (T : Tag) is
947 begin
948 External_Tag_HTable.Set (T);
949 end Register_Tag;
950
951 -------------------
952 -- Secondary_Tag --
953 -------------------
954
955 function Secondary_Tag (T, Iface : Tag) return Tag is
956 Iface_Table : Interface_Data_Ptr;
957 Obj_DT : Dispatch_Table_Ptr;
958
959 begin
960 if not Is_Primary_DT (T) then
961 raise Program_Error;
962 end if;
963
964 Obj_DT := DT (T);
965 Iface_Table := To_Type_Specific_Data_Ptr (Obj_DT.TSD).Interfaces_Table;
966
967 if Iface_Table /= null then
968 for Id in 1 .. Iface_Table.Nb_Ifaces loop
969 if Iface_Table.Ifaces_Table (Id).Iface_Tag = Iface then
970 return Iface_Table.Ifaces_Table (Id).Secondary_DT;
971 end if;
972 end loop;
973 end if;
974
975 -- If the object does not implement the interface we must raise CE
976
977 raise Constraint_Error with "invalid interface conversion";
978 end Secondary_Tag;
979
980 ---------------------
981 -- Set_Entry_Index --
982 ---------------------
983
984 procedure Set_Entry_Index
985 (T : Tag;
986 Position : Positive;
987 Value : Positive)
988 is
989 begin
990 SSD (T).SSD_Table (Position).Index := Value;
991 end Set_Entry_Index;
992
993 -------------------------------
994 -- Set_Dynamic_Offset_To_Top --
995 -------------------------------
996
997 procedure Set_Dynamic_Offset_To_Top
998 (This : System.Address;
999 Prim_T : Tag;
1000 Interface_T : Tag;
1001 Offset_Value : SSE.Storage_Offset;
1002 Offset_Func : Offset_To_Top_Function_Ptr)
1003 is
1004 Sec_Base : System.Address;
1005 Sec_DT : Dispatch_Table_Ptr;
1006
1007 begin
1008 -- Save the offset to top field in the secondary dispatch table
1009
1010 if Offset_Value /= 0 then
1011 Sec_Base := This - Offset_Value;
1012 Sec_DT := DT (To_Tag_Ptr (Sec_Base).all);
1013 Sec_DT.Offset_To_Top := SSE.Storage_Offset'Last;
1014 end if;
1015
1016 Register_Interface_Offset
1017 (Prim_T, Interface_T, False, Offset_Value, Offset_Func);
1018 end Set_Dynamic_Offset_To_Top;
1019
1020 ----------------------
1021 -- Set_Prim_Op_Kind --
1022 ----------------------
1023
1024 procedure Set_Prim_Op_Kind
1025 (T : Tag;
1026 Position : Positive;
1027 Value : Prim_Op_Kind)
1028 is
1029 begin
1030 SSD (T).SSD_Table (Position).Kind := Value;
1031 end Set_Prim_Op_Kind;
1032
1033 --------------------
1034 -- Unregister_Tag --
1035 --------------------
1036
1037 procedure Unregister_Tag (T : Tag) is
1038 begin
1039 External_Tag_HTable.Remove (Get_External_Tag (T));
1040 end Unregister_Tag;
1041
1042 ------------------------
1043 -- Wide_Expanded_Name --
1044 ------------------------
1045
1046 WC_Encoding : constant Character;
1047 pragma Import (C, WC_Encoding, "__gl_wc_encoding");
1048 -- Encoding method for source, as exported by binder
1049
1050 function Wide_Expanded_Name (T : Tag) return Wide_String is
1051 S : constant String := Expanded_Name (T);
1052 W : Wide_String (1 .. S'Length);
1053 L : Natural;
1054 begin
1055 String_To_Wide_String
1056 (S, W, L, Get_WC_Encoding_Method (WC_Encoding));
1057 return W (1 .. L);
1058 end Wide_Expanded_Name;
1059
1060 -----------------------------
1061 -- Wide_Wide_Expanded_Name --
1062 -----------------------------
1063
1064 function Wide_Wide_Expanded_Name (T : Tag) return Wide_Wide_String is
1065 S : constant String := Expanded_Name (T);
1066 W : Wide_Wide_String (1 .. S'Length);
1067 L : Natural;
1068 begin
1069 String_To_Wide_Wide_String
1070 (S, W, L, Get_WC_Encoding_Method (WC_Encoding));
1071 return W (1 .. L);
1072 end Wide_Wide_Expanded_Name;
1073
1074 end Ada.Tags;