Debian Codegraph @

gcc/ada/sem_type.ads

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ T Y P E --
6 -- --
7 -- S p e c --
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. 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 -- This unit contains the routines used to handle type determination,
27 -- including the routine used to support overload resolution.
28
29 with Types; use Types;
30
31 package Sem_Type is
32
33 ---------------------------------------------
34 -- Data Structures for Overload Resolution --
35 ---------------------------------------------
36
37 -- To determine the unique meaning of an identifier, overload resolution
38 -- may have to be performed if the visibility rules alone identify more
39 -- than one possible entity as the denotation of a given identifier. When
40 -- the visibility rules find such a potential ambiguity, the set of
41 -- possible interpretations must be attached to the identifier, and
42 -- overload resolution must be performed over the innermost enclosing
43 -- complete context. At the end of the resolution, either a single
44 -- interpretation is found for all identifiers in the context, or else a
45 -- type error (invalid type or ambiguous reference) must be signalled.
46
47 -- The set of interpretations of a given name is stored in a data structure
48 -- that is separate from the syntax tree, because it corresponds to
49 -- transient information. The interpretations themselves are stored in
50 -- table All_Interp. A mapping from tree nodes to sets of interpretations
51 -- called Interp_Map, is maintained by the overload resolution routines.
52 -- Both these structures are initialized at the beginning of every complete
53 -- context.
54
55 -- Corresponding to the set of interpretations for a given overloadable
56 -- identifier, there is a set of possible types corresponding to the types
57 -- that the overloaded call may return. We keep a 1-to-1 correspondence
58 -- between interpretations and types: for user-defined subprograms the type
59 -- is the declared result type Typ. For operators, the type is determined
60 -- either only by the result type Typ for arithmetic operators or by the
61 -- result type and the type of the operands Opnd_Typ for comparisoon and
62 -- equality operators. If the operands are themselves overloaded, we enter
63 -- the operator name in the names table for each possible result type.
64
65 type Interp is record
66 Nam : Entity_Id;
67 Typ : Entity_Id;
68 Opnd_Typ : Entity_Id;
69 Abstract_Op : Entity_Id;
70 end record;
71
72 -- Entity Abstract_Op is set to the abstract operation which potentially
73 -- disables the interpretation in Ada 2005 mode.
74
75 No_Interp : constant Interp := (Empty, Empty, Empty, Empty);
76
77 type Interp_Index is new Int;
78
79 ---------------------
80 -- Error Reporting --
81 ---------------------
82
83 -- A common error is the use of an operator in infix notation on arguments
84 -- of a type that is not directly visible. Rather than diagnosing a type
85 -- mismatch, it is better to indicate that the type can be made use-visible
86 -- with the appropriate use clause. The global variable Candidate_Type is
87 -- set in Add_One_Interp whenever an interpretation might be legal for an
88 -- operator if the type were directly visible. This variable is used in
89 -- Sem_Ch4 when no legal interpretation is found.
90
91 Candidate_Type : Entity_Id;
92
93 -----------------
94 -- Subprograms --
95 -----------------
96
97 procedure Init_Interp_Tables;
98 -- Initialize data structures for overload resolution
99
100 procedure Collect_Interps (N : Node_Id);
101 -- Invoked when the name N has more than one visible interpretation. This
102 -- is the high level routine which accumulates the possible interpretations
103 -- of the node. The first meaning and type of N have already been stored
104 -- in N. If the name is an expanded name, the homonyms are only those that
105 -- belong to the same scope.
106
107 function Is_Visible_Operator (N : Node_Id; Typ : Entity_Id) return Boolean;
108 -- Determine whether a predefined operation is performed in a context where
109 -- the predefined operators of base type Typ are visible. The existence of
110 -- this routine is an implementation artifact. A more straightforward but
111 -- more space-consuming choice would be to make all inherited operators
112 -- explicit in the symbol table. See also Sem_ch8.Has_Implicit_Operator.
113
114 procedure List_Interps (Nam : Node_Id; Err : Node_Id);
115 -- List candidate interpretations of an overloaded name. Used for various
116 -- error reports.
117
118 procedure Add_One_Interp
119 (N : Node_Id;
120 E : Entity_Id;
121 T : Entity_Id;
122 Opnd_Typ : Entity_Id := Empty);
123 -- Add (E, T) to the list of interpretations of the node being resolved.
124 -- For calls and operators, i.e. for nodes that have a name field, E is an
125 -- overloadable entity, and T is its type. For constructs such as indexed
126 -- expressions, the caller sets E equal to T, because the overloading comes
127 -- from other fields, and the node itself has no name to resolve. Hidden
128 -- denotes whether an interpretation has been disabled by an abstract
129 -- operator. Add_One_Interp includes semantic processing to deal with
130 -- adding entries that hide one another etc.
131 --
132 -- For operators, the legality of the operation depends on the visibility
133 -- of T and its scope. If the operator is an equality or comparison, T is
134 -- always Boolean, and we use Opnd_Typ, which is a candidate type for one
135 -- of the operands of N, to check visibility.
136
137 procedure Get_First_Interp
138 (N : Node_Id;
139 I : out Interp_Index;
140 It : out Interp);
141 -- Initialize iteration over set of interpretations for Node N. The first
142 -- interpretation is placed in It, and I is initialized for subsequent
143 -- calls to Get_Next_Interp.
144
145 procedure Get_Next_Interp (I : in out Interp_Index; It : out Interp);
146 -- Iteration step over set of interpretations. Using the value in I, which
147 -- was set by a previous call to Get_First_Interp or Get_Next_Interp, the
148 -- next interpretation is placed in It, and I is updated for the next call.
149 -- The end of the list of interpretations is signalled by It.Nam = Empty.
150
151 procedure Remove_Interp (I : in out Interp_Index);
152 -- Remove an interpretation that is hidden by another, or that does not
153 -- match the context. The value of I on input was set by a call to either
154 -- Get_First_Interp or Get_Next_Interp and references the interpretation
155 -- to be removed. The only allowed use of the exit value of I is as input
156 -- to a subsequent call to Get_Next_Interp, which yields the interpretation
157 -- following the removed one.
158
159 procedure Save_Interps (Old_N : Node_Id; New_N : Node_Id);
160 -- If an overloaded node is rewritten during semantic analysis, its
161 -- possible interpretations must be linked to the copy. This procedure
162 -- transfers the overload information (Is_Overloaded flag, and list of
163 -- interpretations) from Old_N, the old node, to New_N, its new copy.
164 -- It has no effect in the non-overloaded case.
165
166 function Covers (T1, T2 : Entity_Id) return Boolean;
167 -- This is the basic type compatibility routine. T1 is the expected type,
168 -- imposed by context, and T2 is the actual type. The processing reflects
169 -- both the definition of type coverage and the rules for operand matching;
170 -- that is, this does not exactly match the RM definition of "covers".
171
172 function Disambiguate
173 (N : Node_Id;
174 I1, I2 : Interp_Index;
175 Typ : Entity_Id) return Interp;
176 -- If more than one interpretation of a name in a call is legal, apply
177 -- preference rules (universal types first) and operator visibility in
178 -- order to remove ambiguity. I1 and I2 are the first two interpretations
179 -- that are compatible with the context, but there may be others.
180
181 function Entity_Matches_Spec (Old_S, New_S : Entity_Id) return Boolean;
182 -- To resolve subprogram renaming and default formal subprograms in generic
183 -- definitions. Old_S is a possible interpretation of the entity being
184 -- renamed, New_S has an explicit signature. If Old_S is a subprogram, as
185 -- opposed to an operator, type and mode conformance are required.
186
187 function Find_Unique_Type (L : Node_Id; R : Node_Id) return Entity_Id;
188 -- Used in type resolution for equality and comparison nodes. L and R are
189 -- the operands, whose type is known to be correct or Any_Type in case of
190 -- ambiguity. Return the type intersection of the two.
191
192 function Has_Compatible_Type (N : Node_Id; Typ : Entity_Id) return Boolean;
193 -- Verify that some interpretation of the node N has a type compatible with
194 -- Typ. If N is not overloaded, then its unique type must be compatible
195 -- with Typ. Otherwise iterate through the interpretations of N looking for
196 -- a compatible one.
197
198 function Hides_Op (F : Entity_Id; Op : Entity_Id) return Boolean;
199 -- A user-defined function hides a predefined operator if it matches the
200 -- signature of the operator, and is declared in an open scope, or in the
201 -- scope of the result type.
202
203 function Interface_Present_In_Ancestor
204 (Typ : Entity_Id;
205 Iface : Entity_Id) return Boolean;
206 -- Ada 2005 (AI-251): Typ must be a tagged record type/subtype and Iface
207 -- must be an abstract interface type (or a class-wide abstract interface).
208 -- This function is used to check if Typ or some ancestor of Typ implements
209 -- Iface (returning True only if so).
210
211 function Intersect_Types (L, R : Node_Id) return Entity_Id;
212 -- Find the common interpretation to two analyzed nodes. If one of the
213 -- interpretations is universal, choose the non-universal one. If either
214 -- node is overloaded, find single common interpretation.
215
216 function In_Generic_Actual (Exp : Node_Id) return Boolean;
217 -- Determine whether the expression is part of a generic actual. At the
218 -- time the actual is resolved the scope is already that of the instance,
219 -- but conceptually the resolution of the actual takes place in the
220 -- enclosing context and no special disambiguation rules should be applied.
221
222 function Is_Ancestor
223 (T1 : Entity_Id;
224 T2 : Entity_Id;
225 Use_Full_View : Boolean := False) return Boolean;
226 -- T1 is a type (not class-wide). Verify that it is one of the ancestors of
227 -- type T2 (which may or not be class-wide). If Use_Full_View is True, then
228 -- the full view of private parents is used when climbing T2's parents.
229 --
230 -- Note: For analysis purposes the flag Use_Full_View must be set to False
231 -- (otherwise we break the privacy contract since this routine returns true
232 -- for hidden ancestors of private types). For expansion purposes this flag
233 -- is generally set to True since the expander must know with precision the
234 -- ancestors of a tagged type. For example, if a private type derives from
235 -- an interface type then the interface may not be an ancestor of its full
236 -- view since the full-view is only required to cover the interface (RM 7.3
237 -- (7.3/2))) and this knowledge affects construction of dispatch tables.
238
239 function Is_Progenitor
240 (Iface : Entity_Id;
241 Typ : Entity_Id) return Boolean;
242 -- Determine whether the interface Iface is implemented by Typ. It requires
243 -- traversing the list of abstract interfaces of the type, as well as that
244 -- of the ancestor types. The predicate is used to determine when a formal
245 -- in the signature of an inherited operation must carry the derived type.
246
247 function Is_Subtype_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean;
248 -- Checks whether T1 is any subtype of T2 directly or indirectly
249
250 function Operator_Matches_Spec (Op, New_S : Entity_Id) return Boolean;
251 -- Used to resolve subprograms renaming operators, and calls to user
252 -- defined operators. Determines whether a given operator Op, matches
253 -- a specification, New_S.
254
255 procedure Set_Abstract_Op (I : Interp_Index; V : Entity_Id);
256 -- Set the abstract operation field of an interpretation
257
258 function Specific_Type (Typ_1, Typ_2 : Entity_Id) return Entity_Id;
259 -- If Typ_1 and Typ_2 are compatible, return the one that is not universal
260 -- or is not a "class" type (any_character, etc).
261
262 function Valid_Boolean_Arg (T : Entity_Id) return Boolean;
263 -- A valid argument of a predefined boolean operator must be a boolean type
264 -- or a 1-dimensional array of boolean type.
265
266 function Valid_Comparison_Arg (T : Entity_Id) return Boolean;
267 -- A valid argument of a predefined comparison operator must be a discrete
268 -- type, real type or a 1-dimensional array with a discrete component type.
269
270 function Valid_Equality_Arg (T : Entity_Id) return Boolean;
271 -- A valid argument of a predefined equality operator must be a nonlimited
272 -- type or an array with a limited private component whose full view is not
273 -- limited.
274
275 procedure Write_Interp (It : Interp);
276 -- Debugging procedure to display an Interp
277
278 procedure Write_Overloads (N : Node_Id);
279 -- Debugging procedure to output info on possibly overloaded entities for
280 -- specified node.
281
282 end Sem_Type;