Debian Codegraph @

gcc/ada/mutably_tagged.ads

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- M U T A B L Y _ T A G G E D --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2024-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. 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 -- Semantic and expansion utility routines dealing with mutably tagged types
27
28 with Types; use Types;
29
30 package Mutably_Tagged is
31
32 --------------------------------------------
33 -- Implementation of Mutably Tagged Types --
34 --------------------------------------------
35
36 -- This package implements mutably tagged types via the Size'class aspect
37 -- which enables the creation of class-wide types with a specific maximum
38 -- size. This allows such types to be used directly in record components,
39 -- in object declarations without an initial expression, and to be assigned
40 -- a value from any type in a mutably tagged type's hierarchy.
41
42 -- For example, this structure allows Base_Type and its derivatives to be
43 -- treated as components with a predictable size:
44
45 -- type Base_Type is tagged null record
46 -- with Size'Class => 128;
47
48 -- type Container is record
49 -- Component : Base_Type'Class;
50 -- end record;
51
52 -- The core of their implementation involves creating an "equivalent" type
53 -- for each class-wide type that adheres to the Size'Class constraint. This
54 -- is achieved by using the function Make_CW_Equivalent_Type from Exp_Util,
55 -- which generates a type that is compatible in size and structure with any
56 -- derived type of the base class-wide type.
57
58 -- Once the class-wide equivalent type is generated, all declarations of
59 -- mutably tagged typed objects get rewritten as declarations of objects
60 -- with the equivalent type. References to these objects also then get
61 -- wrapped in unchecked conversions to the mutably tagged class-wide type.
62
63 function Corresponding_Mutably_Tagged_Type
64 (CW_Equiv_Typ : Entity_Id) return Entity_Id;
65 -- Given a class-wide equivalent type obtain the related mutably tagged
66 -- class-wide type.
67
68 function Depends_On_Mutably_Tagged_Ext_Comp (N : Node_Id) return Boolean;
69 -- Return true if the given node N contains a reference to a component
70 -- of a mutably tagged object which comes from a type extension.
71
72 function Get_Corresponding_Mutably_Tagged_Type_If_Present
73 (Typ : Entity_Id) return Entity_Id;
74 -- Obtain the corresponding mutably tagged type associated with Typ when
75 -- Typ is a mutably tagged class-wide equivalent type. Otherwise, just
76 -- return Typ.
77
78 function Get_Corresponding_Tagged_Type_If_Present
79 (Typ : Entity_Id) return Entity_Id;
80 -- Obtain the corresponding tag type associated with Typ when
81 -- Typ is a mutably tagged class-wide equivalent type. Otherwise, Just
82 -- return Typ.
83
84 -- This function is mostly used when we need a concrete type to generate
85 -- initialization for mutably tagged types.
86
87 function Is_Mutably_Tagged_Conversion (N : Node_Id) return Boolean;
88 -- Return True if expression N is an object of a mutably tagged class-wide
89 -- equivalent type which has been expanded into a type conversion to
90 -- its related mutably tagged class-wide type.
91
92 function Is_Mutably_Tagged_CW_Equivalent_Type
93 (Typ : Entity_Id) return Boolean;
94 -- Determine if Typ is a class-wide equivalent type
95
96 procedure Make_Mutably_Tagged_Conversion
97 (N : Node_Id;
98 Typ : Entity_Id := Empty;
99 Force : Boolean := False);
100 -- Expand a reference N to a given mutably tagged type Typ. When Typ is not
101 -- present the closest associated mutably tagged type in the hierarchy is
102 -- used.
103
104 -- Force is used to ignore certain predicates which avoid generating the
105 -- conversion (e.g. when N is on the left-hand side of an assignment).
106
107 function Make_CW_Size_Compile_Check
108 (New_Typ : Entity_Id;
109 Mut_Tag_Typ : Entity_Id) return Node_Id;
110 -- Generate a type size check on New_Typ based on the size set in
111 -- the mutably tagged type Mut_Tag_Typ.
112
113 function Make_Mutably_Tagged_CW_Check
114 (N : Node_Id;
115 Tag : Node_Id) return Node_Id;
116 -- Generate class-wide membership test for a given expression N based on
117 -- Tag.
118
119 end Mutably_Tagged;