File : a-tags.adb


     ------------------------------------------------------------------------------
     --                                                                          --
     --                         GNAT RUNTIME COMPONENTS                          --
     --                                                                          --
   5 --                             A D A . T A G S                              --
     --                                                                          --
     --                                 B o d y                                  --
     --                                                                          --
     --                            $Revision: 1.32 $
  10 --                                                                          --
     --          Copyright (C) 1992-2002 Free Software Foundation, Inc.          --
     --                                                                          --
     -- GNAT is free software;  you can  redistribute it  and/or modify it under --
     -- terms of the  GNU General Public License as published  by the Free Soft- --
  15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
     -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
     -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
     -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
     -- for  more details.  You should have  received  a copy of the GNU General --
  20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
     -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
     -- MA 02111-1307, USA.                                                      --
     --                                                                          --
     -- As a special exception,  if other files  instantiate  generics from this --
  25 -- unit, or you link  this unit with other files  to produce an executable, --
     -- this  unit  does not  by itself cause  the resulting  executable  to  be --
     -- covered  by the  GNU  General  Public  License.  This exception does not --
     -- however invalidate  any other reasons why  the executable file  might be --
     -- covered by the  GNU Public License.                                      --
  30 --                                                                          --
     -- GNAT was originally developed  by the GNAT team at  New York University. --
     -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
     --                                                                          --
     ------------------------------------------------------------------------------
  35 
     with Ada.Exceptions;
     with Unchecked_Conversion;
     with GNAT.HTable;
     
  40 pragma Elaborate_All (GNAT.HTable);
     
     package body Ada.Tags is
     
     --  Structure of the GNAT Dispatch Table
  45 
     --   +----------------------+
     --   |      TSD pointer  ---|-----> Type Specific Data
     --   +----------------------+       +-------------------+
     --   | table of             |       | inheritance depth |
  50 --   :   primitive ops      :       +-------------------+
     --   |     pointers         |       |   expanded name   |
     --   +----------------------+       +-------------------+
     --                                  |   external tag    |
     --                                  +-------------------+
  55 --                                  |   Hash table link |
     --                                  +-------------------+
     --                                  | Remotely Callable |
     --                                  +-------------------+
     --                                  | Rec Ctrler offset |
  60 --                                  +-------------------+
     --                                  | table of          |
     --                                  :   ancestor        :
     --                                  |      tags         |
     --                                  +-------------------+
  65 
        subtype Cstring is String (Positive);
        type Cstring_Ptr is access all Cstring;
        type Tag_Table is array (Natural range <>) of Tag;
        pragma Suppress_Initialization (Tag_Table);
  70 
        type Wide_Boolean is (False, True);
        for Wide_Boolean'Size use Standard'Address_Size;
     
        type Type_Specific_Data is record
  75       Idepth             : Natural;
           Expanded_Name      : Cstring_Ptr;
           External_Tag       : Cstring_Ptr;
           HT_Link            : Tag;
           Remotely_Callable  : Wide_Boolean;
  80       RC_Offset          : SSE.Storage_Offset;
           Ancestor_Tags      : Tag_Table (Natural);
        end record;
     
        type Dispatch_Table is record
  85       TSD       : Type_Specific_Data_Ptr;
           Prims_Ptr : Address_Array (Positive);
        end record;
     
        -------------------------------------------
  90    -- Unchecked Conversions for Tag and TSD --
        -------------------------------------------
     
        function To_Type_Specific_Data_Ptr is
          new Unchecked_Conversion (S.Address, Type_Specific_Data_Ptr);
  95 
        function To_Address is
          new Unchecked_Conversion (Type_Specific_Data_Ptr, S.Address);
     
        ---------------------------------------------
 100    -- Unchecked Conversions for String Fields --
        ---------------------------------------------
     
        function To_Cstring_Ptr is
          new Unchecked_Conversion (S.Address, Cstring_Ptr);
 105 
        function To_Address is
          new Unchecked_Conversion (Cstring_Ptr, S.Address);
     
        -----------------------
 110    -- Local Subprograms --
        -----------------------
     
        function Length (Str : Cstring_Ptr) return Natural;
        --  Length of string represented by the given pointer (treating the
 115    --  string as a C-style string, which is Nul terminated).
     
        -------------------------
        -- External_Tag_HTable --
        -------------------------
 120 
        type HTable_Headers is range 1 .. 64;
     
        --  The following internal package defines the routines used for
        --  the instantiation of a new GNAT.HTable.Static_HTable (see
 125    --  below). See spec in g-htable.ads for details of usage.
     
        package HTable_Subprograms is
           procedure Set_HT_Link (T : Tag; Next : Tag);
           function  Get_HT_Link (T : Tag) return Tag;
 130       function Hash (F : S.Address) return HTable_Headers;
           function Equal (A, B : S.Address) return Boolean;
        end HTable_Subprograms;
     
        package External_Tag_HTable is new GNAT.HTable.Static_HTable (
 135      Header_Num => HTable_Headers,
          Element    => Dispatch_Table,
          Elmt_Ptr   => Tag,
          Null_Ptr   => null,
          Set_Next   => HTable_Subprograms.Set_HT_Link,
 140      Next       => HTable_Subprograms.Get_HT_Link,
          Key        => S.Address,
          Get_Key    => Get_External_Tag,
          Hash       => HTable_Subprograms.Hash,
          Equal      => HTable_Subprograms.Equal);
 145 
        ------------------------
        -- HTable_Subprograms --
        ------------------------
     
 150    --  Bodies of routines for hash table instantiation
     
        package body HTable_Subprograms is
     
        -----------
 155    -- Equal --
        -----------
     
           function Equal (A, B : S.Address) return Boolean is
              Str1 : Cstring_Ptr := To_Cstring_Ptr (A);
 160          Str2 : Cstring_Ptr := To_Cstring_Ptr (B);
              J    : Integer := 1;
     
           begin
              loop
 165             if Str1 (J) /= Str2 (J) then
                    return False;
     
                 elsif Str1 (J) = ASCII.NUL then
                    return True;
 170 
                 else
                    J := J + 1;
                 end if;
              end loop;
 175       end Equal;
     
           -----------------
           -- Get_HT_Link --
           -----------------
 180 
           function Get_HT_Link (T : Tag) return Tag is
           begin
              return T.TSD.HT_Link;
           end Get_HT_Link;
 185 
           ----------
           -- Hash --
           ----------
     
 190       function Hash (F : S.Address) return HTable_Headers is
              function H is new GNAT.HTable.Hash (HTable_Headers);
              Str : Cstring_Ptr := To_Cstring_Ptr (F);
              Res : constant HTable_Headers := H (Str (1 .. Length (Str)));
     
 195       begin
              return Res;
           end Hash;
     
           -----------------
 200       -- Set_HT_Link --
           -----------------
     
           procedure Set_HT_Link (T : Tag; Next : Tag) is
           begin
 205          T.TSD.HT_Link := Next;
           end Set_HT_Link;
     
        end HTable_Subprograms;
     
 210    --------------------
        --  CW_Membership --
        --------------------
     
        --  Canonical implementation of Classwide Membership corresponding to:
 215 
        --     Obj in Typ'Class
     
        --  Each dispatch table contains a reference to a table of ancestors
        --  (Ancestor_Tags) and a count of the level of inheritance "Idepth" .
 220 
        --  Obj is in Typ'Class if Typ'Tag is in the table of ancestors that are
        --  contained in the dispatch table referenced by Obj'Tag . Knowing the
        --  level of inheritance of both types, this can be computed in constant
        --  time by the formula:
 225 
        --   Obj'tag.TSD.Ancestor_Tags (Obj'tag.TSD.Idepth - Typ'tag.TSD.Idepth)
        --     = Typ'tag
     
        function CW_Membership (Obj_Tag : Tag; Typ_Tag : Tag) return Boolean is
 230       Pos : constant Integer := Obj_Tag.TSD.Idepth - Typ_Tag.TSD.Idepth;
     
        begin
           return Pos >= 0 and then Obj_Tag.TSD.Ancestor_Tags (Pos) = Typ_Tag;
        end CW_Membership;
 235 
        -------------------
        -- Expanded_Name --
        -------------------
     
 240    function Expanded_Name (T : Tag) return String is
           Result : Cstring_Ptr := T.TSD.Expanded_Name;
     
        begin
           return Result (1 .. Length (Result));
 245    end Expanded_Name;
     
        ------------------
        -- External_Tag --
        ------------------
 250 
        function External_Tag (T : Tag) return String is
           Result : Cstring_Ptr := T.TSD.External_Tag;
     
        begin
 255       return Result (1 .. Length (Result));
        end External_Tag;
     
        -----------------------
        -- Get_Expanded_Name --
 260    -----------------------
     
        function Get_Expanded_Name (T : Tag) return S.Address is
        begin
           return To_Address (T.TSD.Expanded_Name);
 265    end Get_Expanded_Name;
     
        ----------------------
        -- Get_External_Tag --
        ----------------------
 270 
        function Get_External_Tag (T : Tag) return S.Address is
        begin
           return To_Address (T.TSD.External_Tag);
        end Get_External_Tag;
 275 
        ---------------------------
        -- Get_Inheritance_Depth --
        ---------------------------
     
 280    function Get_Inheritance_Depth (T : Tag) return Natural is
        begin
           return T.TSD.Idepth;
        end Get_Inheritance_Depth;
     
 285    -------------------------
        -- Get_Prim_Op_Address --
        -------------------------
     
        function Get_Prim_Op_Address
 290      (T        : Tag;
           Position : Positive)
           return     S.Address
        is
        begin
 295       return T.Prims_Ptr (Position);
        end Get_Prim_Op_Address;
     
        -------------------
        -- Get_RC_Offset --
 300    -------------------
     
        function Get_RC_Offset (T : Tag) return SSE.Storage_Offset is
        begin
           return T.TSD.RC_Offset;
 305    end Get_RC_Offset;
     
        ---------------------------
        -- Get_Remotely_Callable --
        ---------------------------
 310 
        function Get_Remotely_Callable (T : Tag) return Boolean is
        begin
           return T.TSD.Remotely_Callable = True;
        end Get_Remotely_Callable;
 315 
        -------------
        -- Get_TSD --
        -------------
     
 320    function Get_TSD  (T : Tag) return S.Address is
        begin
           return To_Address (T.TSD);
        end Get_TSD;
     
 325    ----------------
        -- Inherit_DT --
        ----------------
     
        procedure Inherit_DT
 330     (Old_T       : Tag;
          New_T       : Tag;
          Entry_Count : Natural)
        is
        begin
 335       if Old_T /= null then
              New_T.Prims_Ptr (1 .. Entry_Count) :=
                Old_T.Prims_Ptr (1 .. Entry_Count);
           end if;
        end Inherit_DT;
 340 
        -----------------
        -- Inherit_TSD --
        -----------------
     
 345    procedure Inherit_TSD (Old_TSD : S.Address; New_Tag : Tag) is
           TSD     : constant Type_Specific_Data_Ptr :=
                       To_Type_Specific_Data_Ptr (Old_TSD);
           New_TSD : Type_Specific_Data renames New_Tag.TSD.all;
     
 350    begin
           if TSD /= null then
              New_TSD.Idepth := TSD.Idepth + 1;
              New_TSD.Ancestor_Tags (1 .. New_TSD.Idepth)
                                 := TSD.Ancestor_Tags (0 .. TSD.Idepth);
 355       else
              New_TSD.Idepth := 0;
           end if;
     
           New_TSD.Ancestor_Tags (0) := New_Tag;
 360    end Inherit_TSD;
     
        ------------------
        -- Internal_Tag --
        ------------------
 365 
        function Internal_Tag (External : String) return Tag is
           Ext_Copy : aliased String (External'First .. External'Last + 1);
           Res      : Tag;
     
 370    begin
           --  Make a copy of the string representing the external tag with
           --  a null at the end
     
           Ext_Copy (External'Range) := External;
 375       Ext_Copy (Ext_Copy'Last) := ASCII.NUL;
           Res := External_Tag_HTable.Get (Ext_Copy'Address);
     
           if Res = null then
              declare
 380             Msg1 : constant String := "unknown tagged type: ";
                 Msg2 : String (1 .. Msg1'Length + External'Length);
     
              begin
                 Msg2 (1 .. Msg1'Length) := Msg1;
 385             Msg2 (Msg1'Length + 1 .. Msg1'Length + External'Length) :=
                   External;
                 Ada.Exceptions.Raise_Exception (Tag_Error'Identity, Msg2);
              end;
           end if;
 390 
           return Res;
        end Internal_Tag;
     
        ------------
 395    -- Length --
        ------------
     
        function Length (Str : Cstring_Ptr) return Natural is
           Len : Integer := 1;
 400 
        begin
           while Str (Len) /= ASCII.Nul loop
              Len := Len + 1;
           end loop;
 405 
           return Len - 1;
        end Length;
     
        -----------------
 410    -- Parent_Size --
        -----------------
     
        --  Fake type with a tag as first component. Should match the
        --  layout of all tagged types.
 415 
        type T is record
           A : Tag;
        end record;
     
 420    type T_Ptr is access all T;
     
        function To_T_Ptr is new Unchecked_Conversion (S.Address, T_Ptr);
     
        --  The profile of the implicitly defined _size primitive
 425 
        type Acc_Size is access function (A : S.Address) return Long_Long_Integer;
        function To_Acc_Size is new Unchecked_Conversion (S.Address, Acc_Size);
     
        function Parent_Size (Obj : S.Address) return SSE.Storage_Count is
 430 
           --  Get the tag of the object
     
           Obj_Tag : constant Tag      := To_T_Ptr (Obj).A;
     
 435       --  Get the tag of the parent type through the dispatch table
     
           Parent_Tag : constant Tag      := Obj_Tag.TSD.Ancestor_Tags (1);
     
           --  Get an access to the _size primitive of the parent. We assume that
 440       --  it is always in the first slot of the distatch table
     
           F : constant Acc_Size := To_Acc_Size (Parent_Tag.Prims_Ptr (1));
     
        begin
 445       --  Here we compute the size of the _parent field of the object
     
           return SSE.Storage_Count (F.all (Obj));
        end Parent_Size;
     
 450    ------------------
        -- Register_Tag --
        ------------------
     
        procedure Register_Tag (T : Tag) is
 455    begin
           External_Tag_HTable.Set (T);
        end Register_Tag;
     
        -----------------------
 460    -- Set_Expanded_Name --
        -----------------------
     
        procedure Set_Expanded_Name (T : Tag; Value : S.Address) is
        begin
 465       T.TSD.Expanded_Name := To_Cstring_Ptr (Value);
        end Set_Expanded_Name;
     
        ----------------------
        -- Set_External_Tag --
 470    ----------------------
     
        procedure Set_External_Tag (T : Tag; Value : S.Address) is
        begin
           T.TSD.External_Tag := To_Cstring_Ptr (Value);
 475    end Set_External_Tag;
     
        ---------------------------
        -- Set_Inheritance_Depth --
        ---------------------------
 480 
        procedure Set_Inheritance_Depth
          (T     : Tag;
           Value : Natural)
        is
 485    begin
           T.TSD.Idepth := Value;
        end Set_Inheritance_Depth;
     
        -------------------------
 490    -- Set_Prim_Op_Address --
        -------------------------
     
        procedure Set_Prim_Op_Address
          (T        : Tag;
 495       Position : Positive;
           Value    : S.Address)
        is
        begin
           T.Prims_Ptr (Position) := Value;
 500    end Set_Prim_Op_Address;
     
        -------------------
        -- Set_RC_Offset --
        -------------------
 505 
        procedure Set_RC_Offset (T : Tag; Value : SSE.Storage_Offset) is
        begin
           T.TSD.RC_Offset := Value;
        end Set_RC_Offset;
 510 
        ---------------------------
        -- Set_Remotely_Callable --
        ---------------------------
     
 515    procedure Set_Remotely_Callable (T : Tag; Value : Boolean) is
        begin
           if Value then
              T.TSD.Remotely_Callable := True;
           else
 520          T.TSD.Remotely_Callable := False;
           end if;
        end Set_Remotely_Callable;
     
        -------------
 525    -- Set_TSD --
        -------------
     
        procedure Set_TSD (T : Tag; Value : S.Address) is
        begin
 530       T.TSD := To_Type_Specific_Data_Ptr (Value);
        end Set_TSD;
     
     end Ada.Tags;