OSDN Git Service

ada:
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-tags.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                             A D A . T A G S                              --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2011, Free Software Foundation, Inc.         --
10 --                                                                          --
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. --
14 --                                                                          --
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.                                     --
21 --                                                                          --
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.               --
25 --                                                                          --
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/>.                                          --
30 --                                                                          --
31 -- GNAT was originally developed  by the GNAT team at  New York University. --
32 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
33 --                                                                          --
34 ------------------------------------------------------------------------------
35
36 with System;
37 with System.Storage_Elements;
38
39 package Ada.Tags is
40    pragma Preelaborate_05;
41    --  In accordance with Ada 2005 AI-362
42
43    type Tag is private;
44    pragma Preelaborable_Initialization (Tag);
45
46    No_Tag : constant Tag;
47
48    function Expanded_Name (T : Tag) return String;
49
50    function Wide_Expanded_Name (T : Tag) return Wide_String;
51    pragma Ada_05 (Wide_Expanded_Name);
52
53    function Wide_Wide_Expanded_Name (T : Tag) return Wide_Wide_String;
54    pragma Ada_05 (Wide_Wide_Expanded_Name);
55
56    function External_Tag (T : Tag) return String;
57
58    function Internal_Tag (External : String) return Tag;
59
60    function Descendant_Tag
61      (External : String;
62       Ancestor : Tag) return Tag;
63    pragma Ada_05 (Descendant_Tag);
64
65    function Is_Descendant_At_Same_Level
66      (Descendant : Tag;
67       Ancestor   : Tag) return Boolean;
68    pragma Ada_05 (Is_Descendant_At_Same_Level);
69
70    function Parent_Tag (T : Tag) return Tag;
71    pragma Ada_05 (Parent_Tag);
72
73    type Tag_Array is array (Positive range <>) of Tag;
74
75    function Interface_Ancestor_Tags (T : Tag) return Tag_Array;
76    pragma Ada_05 (Interface_Ancestor_Tags);
77
78    function Type_Is_Abstract (T : Tag) return Boolean;
79    pragma Ada_2012 (Type_Is_Abstract);
80
81    Tag_Error : exception;
82
83 private
84    --  Structure of the GNAT Primary Dispatch Table
85
86    --           +--------------------+
87    --           |      Signature     |
88    --           +--------------------+
89    --           |     Tagged_Kind    |
90    --           +--------------------+                            Predef Prims
91    --           |    Predef_Prims -----------------------------> +------------+
92    --           +--------------------+                           |  table of  |
93    --           |    Offset_To_Top   |                           | predefined |
94    --           +--------------------+                           | primitives |
95    --           |Typeinfo_Ptr/TSD_Ptr---> Type Specific Data     +------------+
96    --  Tag ---> +--------------------+   +-------------------+
97    --           |      table of      |   | inheritance depth |
98    --           :   primitive ops    :   +-------------------+
99    --           |      pointers      |   |   access level    |
100    --           +--------------------+   +-------------------+
101    --                                    |   expanded name   |
102    --                                    +-------------------+
103    --                                    |   external tag    |
104    --                                    +-------------------+
105    --                                    |   hash table link |
106    --                                    +-------------------+
107    --                                    |   transportable   |
108    --                                    +-------------------+
109    --                                    |  type_is_abstract |
110    --                                    +-------------------+
111    --                                    | needs finalization|
112    --                                    +-------------------+
113    --                                    |   Ifaces_Table   ---> Interface Data
114    --                                    +-------------------+   +------------+
115    --         Select Specific Data  <----        SSD         |   |  Nb_Ifaces |
116    --         +------------------+       +-------------------+   +------------+
117    --         |table of primitive|       | table of          |   |  table     |
118    --         :   operation      :       :    ancestor       :   :    of      :
119    --         |      kinds       |       |       tags        |   | interfaces |
120    --         +------------------+       +-------------------+   +------------+
121    --         |table of          |
122    --         :   entry          :
123    --         |      indexes     |
124    --         +------------------+
125
126    --  Structure of the GNAT Secondary Dispatch Table
127
128    --           +--------------------+
129    --           |      Signature     |
130    --           +--------------------+
131    --           |     Tagged_Kind    |
132    --           +--------------------+                            Predef Prims
133    --           |    Predef_Prims -----------------------------> +------------+
134    --           +--------------------+                           |  table of  |
135    --           |    Offset_To_Top   |                           | predefined |
136    --           +--------------------+                           | primitives |
137    --           |       OSD_Ptr      |---> Object Specific Data  |   thunks   |
138    --  Tag ---> +--------------------+      +---------------+    +------------+
139    --           |      table of      |      | num prim ops  |
140    --           :    primitive op    :      +---------------+
141    --           |   thunk pointers   |      | table of      |
142    --           +--------------------+      +   primitive   |
143    --                                       |    op offsets |
144    --                                       +---------------+
145
146    --  The runtime information kept for each tagged type is separated into two
147    --  objects: the Dispatch Table and the Type Specific Data record.
148
149    package SSE renames System.Storage_Elements;
150
151    subtype Cstring is String (Positive);
152    type Cstring_Ptr is access all Cstring;
153    pragma No_Strict_Aliasing (Cstring_Ptr);
154
155    --  Declarations for the table of interfaces
156
157    type Offset_To_Top_Function_Ptr is
158      access function (This : System.Address) return SSE.Storage_Offset;
159    --  Type definition used to call the function that is generated by the
160    --  expander in case of tagged types with discriminants that have secondary
161    --  dispatch tables. This function provides the Offset_To_Top value in this
162    --  specific case.
163
164    type Interface_Data_Element is record
165       Iface_Tag            : Tag;
166       Static_Offset_To_Top : Boolean;
167       Offset_To_Top_Value  : SSE.Storage_Offset;
168       Offset_To_Top_Func   : Offset_To_Top_Function_Ptr;
169       Secondary_DT         : Tag;
170    end record;
171    --  If some ancestor of the tagged type has discriminants the field
172    --  Static_Offset_To_Top is False and the field Offset_To_Top_Func
173    --  is used to store the access to the function generated by the
174    --  expander which provides this value; otherwise Static_Offset_To_Top
175    --  is True and such value is stored in the Offset_To_Top_Value field.
176    --  Secondary_DT references a secondary dispatch table whose contents
177    --  are pointers to the primitives of the tagged type that cover the
178    --  interface primitives. Secondary_DT gives support to dispatching
179    --  calls through interface types associated with Generic Dispatching
180    --  Constructors.
181
182    type Interfaces_Array is array (Natural range <>) of Interface_Data_Element;
183
184    type Interface_Data (Nb_Ifaces : Positive) is record
185       Ifaces_Table : Interfaces_Array (1 .. Nb_Ifaces);
186    end record;
187
188    type Interface_Data_Ptr is access all Interface_Data;
189    --  Table of abstract interfaces used to give support to backward interface
190    --  conversions and also to IW_Membership.
191
192    --  Primitive operation kinds. These values differentiate the kinds of
193    --  callable entities stored in the dispatch table. Certain kinds may
194    --  not be used, but are added for completeness.
195
196    type Prim_Op_Kind is
197      (POK_Function,
198       POK_Procedure,
199       POK_Protected_Entry,
200       POK_Protected_Function,
201       POK_Protected_Procedure,
202       POK_Task_Entry,
203       POK_Task_Function,
204       POK_Task_Procedure);
205
206    --  Select specific data types
207
208    type Select_Specific_Data_Element is record
209       Index : Positive;
210       Kind  : Prim_Op_Kind;
211    end record;
212
213    type Select_Specific_Data_Array is
214      array (Positive range <>) of Select_Specific_Data_Element;
215
216    type Select_Specific_Data (Nb_Prim : Positive) is record
217       SSD_Table : Select_Specific_Data_Array (1 .. Nb_Prim);
218       --  NOTE: Nb_Prim is the number of non-predefined primitive operations
219    end record;
220
221    type Select_Specific_Data_Ptr is access all Select_Specific_Data;
222    --  A table used to store the primitive operation kind and entry index of
223    --  primitive subprograms of a type that implements a limited interface.
224    --  The Select Specific Data table resides in the Type Specific Data of a
225    --  type. This construct is used in the handling of dispatching triggers
226    --  in select statements.
227
228    type Prim_Ptr is access procedure;
229    type Address_Array is array (Positive range <>) of Prim_Ptr;
230
231    subtype Dispatch_Table is Address_Array (1 .. 1);
232    --  Used by GDB to identify the _tags and traverse the run-time structure
233    --  associated with tagged types. For compatibility with older versions of
234    --  gdb, its name must not be changed.
235
236    type Tag is access all Dispatch_Table;
237    pragma No_Strict_Aliasing (Tag);
238
239    type Interface_Tag is access all Dispatch_Table;
240
241    No_Tag : constant Tag := null;
242
243    --  The expander ensures that Tag objects reference the Prims_Ptr component
244    --  of the wrapper.
245
246    type Tag_Ptr is access all Tag;
247    pragma No_Strict_Aliasing (Tag_Ptr);
248
249    type Offset_To_Top_Ptr is access all SSE.Storage_Offset;
250    pragma No_Strict_Aliasing (Offset_To_Top_Ptr);
251
252    type Tag_Table is array (Natural range <>) of Tag;
253
254    type Size_Ptr is
255      access function (A : System.Address) return Long_Long_Integer;
256
257    type Type_Specific_Data (Idepth : Natural) is record
258    --  The discriminant Idepth is the Inheritance Depth Level: Used to
259    --  implement the membership test associated with single inheritance of
260    --  tagged types in constant-time. It also indicates the size of the
261    --  Tags_Table component.
262
263       Access_Level : Natural;
264       --  Accessibility level required to give support to Ada 2005 nested type
265       --  extensions. This feature allows safe nested type extensions by
266       --  shifting the accessibility checks to certain operations, rather than
267       --  being enforced at the type declaration. In particular, by performing
268       --  run-time accessibility checks on class-wide allocators, class-wide
269       --  function return, and class-wide stream I/O, the danger of objects
270       --  outliving their type declaration can be eliminated (Ada 2005: AI-344)
271
272       Expanded_Name : Cstring_Ptr;
273       External_Tag  : Cstring_Ptr;
274       HT_Link       : Tag_Ptr;
275       --  Components used to support to the Ada.Tags subprograms in RM 3.9
276
277       --  Note: Expanded_Name is referenced by GDB to determine the actual name
278       --  of the tagged type. Its requirements are: 1) it must have this exact
279       --  name, and 2) its contents must point to a C-style Nul terminated
280       --  string containing its expanded name. GDB has no requirement on a
281       --  given position inside the record.
282
283       Transportable : Boolean;
284       --  Used to check RM E.4(18), set for types that satisfy the requirements
285       --  for being used in remote calls as actuals for classwide formals or as
286       --  return values for classwide functions.
287
288       Type_Is_Abstract : Boolean;
289       --  True if the type is abstract (Ada 2012: AI05-0173)
290
291       Needs_Finalization : Boolean;
292       --  Used to dynamically check whether an object is controlled or not
293
294       Size_Func : Size_Ptr;
295       --  Pointer to the subprogram computing the _size of the object. Used by
296       --  the run-time whenever a call to the 'size primitive is required. We
297       --  cannot assume that the contents of dispatch tables are addresses
298       --  because in some architectures the ABI allows descriptors.
299
300       Interfaces_Table : Interface_Data_Ptr;
301       --  Pointer to the table of interface tags. It is used to implement the
302       --  membership test associated with interfaces and also for backward
303       --  abstract interface type conversions (Ada 2005:AI-251)
304
305       SSD : Select_Specific_Data_Ptr;
306       --  Pointer to a table of records used in dispatching selects. This field
307       --  has a meaningful value for all tagged types that implement a limited,
308       --  protected, synchronized or task interfaces and have non-predefined
309       --  primitive operations.
310
311       Tags_Table : Tag_Table (0 .. Idepth);
312       --  Table of ancestor tags. Its size actually depends on the inheritance
313       --  depth level of the tagged type.
314    end record;
315
316    type Type_Specific_Data_Ptr is access all Type_Specific_Data;
317    pragma No_Strict_Aliasing (Type_Specific_Data_Ptr);
318
319    --  Declarations for the dispatch table record
320
321    type Signature_Kind is
322       (Unknown,
323        Primary_DT,
324        Secondary_DT);
325
326    --  Tagged type kinds with respect to concurrency and limitedness
327
328    type Tagged_Kind is
329      (TK_Abstract_Limited_Tagged,
330       TK_Abstract_Tagged,
331       TK_Limited_Tagged,
332       TK_Protected,
333       TK_Tagged,
334       TK_Task);
335
336    type Dispatch_Table_Wrapper (Num_Prims : Natural) is record
337       Signature     : Signature_Kind;
338       Tag_Kind      : Tagged_Kind;
339       Predef_Prims  : System.Address;
340       --  Pointer to the dispatch table of predefined Ada primitives
341
342       --  According to the C++ ABI the components Offset_To_Top and TSD are
343       --  stored just "before" the dispatch table, and they are referenced with
344       --  negative offsets referring to the base of the dispatch table. The
345       --   _Tag (or the VTable_Ptr in C++ terminology) must point to the base
346       --  of the virtual table, just after these components, to point to the
347       --  Prims_Ptr table.
348
349       Offset_To_Top : SSE.Storage_Offset;
350       TSD           : System.Address;
351
352       Prims_Ptr : aliased Address_Array (1 .. Num_Prims);
353       --  The size of the Prims_Ptr array actually depends on the tagged type
354       --  to which it applies. For each tagged type, the expander computes the
355       --  actual array size, allocates the Dispatch_Table record accordingly.
356    end record;
357
358    type Dispatch_Table_Ptr is access all Dispatch_Table_Wrapper;
359    pragma No_Strict_Aliasing (Dispatch_Table_Ptr);
360
361    --  The following type declaration is used by the compiler when the program
362    --  is compiled with restriction No_Dispatching_Calls. It is also used with
363    --  interface types to generate the tag and run-time information associated
364    --  with them.
365
366    type No_Dispatch_Table_Wrapper is record
367       NDT_TSD       : System.Address;
368       NDT_Prims_Ptr : Natural;
369    end record;
370
371    DT_Predef_Prims_Size : constant SSE.Storage_Count :=
372                             SSE.Storage_Count
373                               (1 * (Standard'Address_Size /
374                                       System.Storage_Unit));
375    --  Size of the Predef_Prims field of the Dispatch_Table
376
377    DT_Offset_To_Top_Size : constant SSE.Storage_Count :=
378                              SSE.Storage_Count
379                                (1 * (Standard'Address_Size /
380                                        System.Storage_Unit));
381    --  Size of the Offset_To_Top field of the Dispatch Table
382
383    DT_Typeinfo_Ptr_Size : constant SSE.Storage_Count :=
384                             SSE.Storage_Count
385                               (1 * (Standard'Address_Size /
386                                       System.Storage_Unit));
387    --  Size of the Typeinfo_Ptr field of the Dispatch Table
388
389    use type System.Storage_Elements.Storage_Offset;
390
391    DT_Offset_To_Top_Offset : constant SSE.Storage_Count :=
392                                DT_Typeinfo_Ptr_Size
393                                  + DT_Offset_To_Top_Size;
394
395    DT_Predef_Prims_Offset : constant SSE.Storage_Count :=
396                               DT_Typeinfo_Ptr_Size
397                                 + DT_Offset_To_Top_Size
398                                 + DT_Predef_Prims_Size;
399    --  Offset from Prims_Ptr to Predef_Prims component
400
401    --  Object Specific Data record of secondary dispatch tables
402
403    type Object_Specific_Data_Array is array (Positive range <>) of Positive;
404
405    type Object_Specific_Data (OSD_Num_Prims : Positive) is record
406       OSD_Table : Object_Specific_Data_Array (1 .. OSD_Num_Prims);
407       --  Table used in secondary DT to reference their counterpart in the
408       --  select specific data (in the TSD of the primary DT). This construct
409       --  is used in the handling of dispatching triggers in select statements.
410       --  Nb_Prim is the number of non-predefined primitive operations.
411    end record;
412
413    type Object_Specific_Data_Ptr is access all Object_Specific_Data;
414    pragma No_Strict_Aliasing (Object_Specific_Data_Ptr);
415
416    --  The following subprogram specifications are placed here instead of the
417    --  package body to see them from the frontend through rtsfind.
418
419    function Base_Address (This : System.Address) return System.Address;
420    --  Ada 2005 (AI-251): Displace "This" to point to the base address of the
421    --  object (that is, the address of the primary tag of the object).
422
423    procedure Check_TSD (TSD : Type_Specific_Data_Ptr);
424    --  Ada 2012 (AI-113): Raise Program_Error if the external tag of this TSD
425    --  is the same as the external tag for some other tagged type declaration.
426
427    function Displace (This : System.Address; T : Tag) return System.Address;
428    --  Ada 2005 (AI-251): Displace "This" to point to the secondary dispatch
429    --  table of T.
430
431    function Secondary_Tag (T, Iface : Tag) return Tag;
432    --  Ada 2005 (AI-251): Given a primary tag T associated with a tagged type
433    --  Typ, search for the secondary tag of the interface type Iface covered
434    --  by Typ.
435
436    function DT (T : Tag) return Dispatch_Table_Ptr;
437    --  Return the pointer to the TSD record associated with T
438
439    function Get_Entry_Index (T : Tag; Position : Positive) return Positive;
440    --  Ada 2005 (AI-251): Return a primitive operation's entry index (if entry)
441    --  given a dispatch table T and a position of a primitive operation in T.
442
443    function Get_Offset_Index
444      (T        : Tag;
445       Position : Positive) return Positive;
446    --  Ada 2005 (AI-251): Given a pointer to a secondary dispatch table (T)
447    --  and a position of an operation in the DT, retrieve the corresponding
448    --  operation's position in the primary dispatch table from the Offset
449    --  Specific Data table of T.
450
451    function Get_Prim_Op_Kind
452      (T        : Tag;
453       Position : Positive) return Prim_Op_Kind;
454    --  Ada 2005 (AI-251): Return a primitive operation's kind given a dispatch
455    --  table T and a position of a primitive operation in T.
456
457    function Get_Tagged_Kind (T : Tag) return Tagged_Kind;
458    --  Ada 2005 (AI-345): Given a pointer to either a primary or a secondary
459    --  dispatch table, return the tagged kind of a type in the context of
460    --  concurrency and limitedness.
461
462    function IW_Membership (This : System.Address; T : Tag) return Boolean;
463    --  Ada 2005 (AI-251): General routine that checks if a given object
464    --  implements a tagged type. Its common usage is to check if Obj is in
465    --  Iface'Class, but it is also used to check if a class-wide interface
466    --  implements a given type (Iface_CW_Typ in T'Class). For example:
467    --
468    --      type I is interface;
469    --      type T is tagged ...
470    --
471    --      function Test (O : I'Class) is
472    --      begin
473    --         return O in T'Class.
474    --      end Test;
475
476    function Offset_To_Top
477      (This : System.Address) return SSE.Storage_Offset;
478    --  Ada 2005 (AI-251): Returns the current value of the Offset_To_Top
479    --  component available in the prologue of the dispatch table. If the parent
480    --  of the tagged type has discriminants this value is stored in a record
481    --  component just immediately after the tag component.
482
483    function Needs_Finalization (T : Tag) return Boolean;
484    --  A helper routine used in conjunction with finalization collections which
485    --  service class-wide types. The function dynamically determines whether an
486    --  object is controlled or has controlled components.
487
488    function Parent_Size
489      (Obj : System.Address;
490       T   : Tag) return SSE.Storage_Count;
491    --  Computes the size the ancestor part of a tagged extension object whose
492    --  address is 'obj' by calling indirectly the ancestor _size function. The
493    --  ancestor is the parent of the type represented by tag T. This function
494    --  assumes that _size is always in slot one of the dispatch table.
495
496    pragma Export (Ada, Parent_Size, "ada__tags__parent_size");
497    --  This procedure is used in s-finimp and is thus exported manually
498
499    procedure Register_Interface_Offset
500      (This         : System.Address;
501       Interface_T  : Tag;
502       Is_Static    : Boolean;
503       Offset_Value : SSE.Storage_Offset;
504       Offset_Func  : Offset_To_Top_Function_Ptr);
505    --  Register in the table of interfaces of the tagged type associated with
506    --  "This" object the offset of the record component associated with the
507    --  progenitor Interface_T (that is, the distance from "This" to the object
508    --  component containing the tag of the secondary dispatch table). In case
509    --  of constant offset, Is_Static is true and Offset_Value has such value.
510    --  In case of variable offset, Is_Static is false and Offset_Func is an
511    --  access to function that must be called to evaluate the offset.
512
513    procedure Register_Tag (T : Tag);
514    --  Insert the Tag and its associated external_tag in a table for the sake
515    --  of Internal_Tag.
516
517    procedure Set_Dynamic_Offset_To_Top
518      (This         : System.Address;
519       Interface_T  : Tag;
520       Offset_Value : SSE.Storage_Offset;
521       Offset_Func  : Offset_To_Top_Function_Ptr);
522    --  Ada 2005 (AI-251): The compiler generates calls to this routine only
523    --  when initializing the Offset_To_Top field of dispatch tables associated
524    --  with tagged type whose parent has variable size components. "This" is
525    --  the object whose dispatch table is being initialized. Interface_T is the
526    --  interface for which the secondary dispatch table is being initialized,
527    --  and Offset_Value is the distance from "This" to the object component
528    --  containing the tag of the secondary dispatch table (a zero value means
529    --  that this interface shares the primary dispatch table). Offset_Func
530    --  references a function that must be called to evaluate the offset at
531    --  runtime. This routine also takes care of registering these values in
532    --  the table of interfaces of the type.
533
534    procedure Set_Entry_Index (T : Tag; Position : Positive; Value : Positive);
535    --  Ada 2005 (AI-345): Set the entry index of a primitive operation in T's
536    --  TSD table indexed by Position.
537
538    procedure Set_Prim_Op_Kind
539      (T        : Tag;
540       Position : Positive;
541       Value    : Prim_Op_Kind);
542    --  Ada 2005 (AI-251): Set the kind of a primitive operation in T's TSD
543    --  table indexed by Position.
544
545    procedure Unregister_Tag (T : Tag);
546    --  Remove a particular tag from the external tag hash table
547
548    Max_Predef_Prims : constant Positive := 16;
549    --  Number of reserved slots for the following predefined ada primitives:
550    --
551    --    1. Size
552    --    2. Alignment,
553    --    3. Read
554    --    4. Write
555    --    5. Input
556    --    6. Output
557    --    7. "="
558    --    8. assignment
559    --    9. deep adjust
560    --   10. deep finalize
561    --   11. async select
562    --   12. conditional select
563    --   13. prim_op kind
564    --   14. task_id
565    --   15. dispatching requeue
566    --   16. timed select
567    --
568    --  The compiler checks that the value here is correct
569
570    subtype Predef_Prims_Table  is Address_Array (1 .. Max_Predef_Prims);
571    type Predef_Prims_Table_Ptr is access Predef_Prims_Table;
572    pragma No_Strict_Aliasing (Predef_Prims_Table_Ptr);
573
574    type Addr_Ptr is access System.Address;
575    pragma No_Strict_Aliasing (Addr_Ptr);
576    --  This type is used by the frontend to generate the code that handles
577    --  dispatch table slots of types declared at the local level.
578
579 end Ada.Tags;