1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005, Free Software Foundation, Inc. --
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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 -- This unit contains the routines used to handle type determination,
28 -- including the routine used to support overload resolution.
30 with Types
; use Types
;
34 ---------------------------------------------
35 -- Data Structures for Overload Resolution --
36 ---------------------------------------------
38 -- To determine the unique meaning of an identifier, overload resolution
39 -- may have to be performed if the visibility rules alone identify more
40 -- than one possible entity as the denotation of a given identifier. When
41 -- the visibility rules find such a potential ambiguity, the set of
42 -- possible interpretations must be attached to the identifier, and
43 -- overload resolution must be performed over the innermost enclosing
44 -- complete context. At the end of the resolution, either a single
45 -- interpretation is found for all identifiers in the context, or else a
46 -- type error (invalid type or ambiguous reference) must be signalled.
48 -- The set of interpretations of a given name is stored in a data structure
49 -- that is separate from the syntax tree, because it corresponds to
50 -- transient information. The interpretations themselves are stored in
51 -- table All_Interp. A mapping from tree nodes to sets of interpretations
52 -- called Interp_Map, is maintained by the overload resolution routines.
53 -- Both these structures are initialized at the beginning of every complete
56 -- Corresponding to the set of interpretation for a given overloadable
57 -- identifier, there is a set of possible types corresponding to the types
58 -- that the overloaded call may return. We keep a 1-to-1 correspondence
59 -- between interpretations and types: for user-defined subprograms the
60 -- type is the declared return type. For operators, the type is determined
61 -- by the type of the arguments. If the arguments themselves are
62 -- overloaded, we enter the operator name in the names table for each
63 -- possible result type. In most cases, arguments are not overloaded and
64 -- only one interpretation is present anyway.
71 No_Interp
: constant Interp
:= (Empty
, Empty
);
73 subtype Interp_Index
is Int
;
79 -- A common error is the use of an operator in infix notation on arguments
80 -- of a type that is not directly visible. Rather than diagnosing a type
81 -- mismatch, it is better to indicate that the type can be made use-visible
82 -- with the appropriate use clause. The global variable Candidate_Type is
83 -- set in Add_One_Interp whenever an interpretation might be legal for an
84 -- operator if the type were directly visible. This variable is used in
85 -- sem_ch4 when no legal interpretation is found.
87 Candidate_Type
: Entity_Id
;
93 procedure Init_Interp_Tables
;
94 -- Invoked by gnatf when processing multiple files
96 procedure Collect_Interps
(N
: Node_Id
);
97 -- Invoked when the name N has more than one visible interpretation.
98 -- This is the high level routine which accumulates the possible
99 -- interpretations of the node. The first meaning and type of N have
100 -- already been stored in N. If the name is an expanded name, the homonyms
101 -- are only those that belong to the same scope.
103 function Is_Invisible_Operator
107 -- Check whether a predefined operation with universal operands appears
108 -- in a context in which the operators of the expected type are not
111 procedure List_Interps
(Nam
: Node_Id
; Err
: Node_Id
);
112 -- List candidate interpretations of an overloaded name. Used for
113 -- various error reports.
115 procedure Add_One_Interp
119 Opnd_Type
: Entity_Id
:= Empty
);
120 -- Add (E, T) to the list of interpretations of the node being resolved.
121 -- For calls and operators, i.e. for nodes that have a name field,
122 -- E is an overloadable entity, and T is its type. For constructs such
123 -- as indexed expressions, the caller sets E equal to T, because the
124 -- overloading comes from other fields, and the node itself has no name
125 -- to resolve. Add_One_Interp includes the semantic processing to deal
126 -- with adding entries that hide one another etc.
128 -- For operators, the legality of the operation depends on the visibility
129 -- of T and its scope. If the operator is an equality or comparison, T is
130 -- always Boolean, and we use Opnd_Type, which is a candidate type for one
131 -- of the operands of N, to check visibility.
133 procedure End_Interp_List
;
134 -- End the list of interpretations of current node
136 procedure Get_First_Interp
138 I
: out Interp_Index
;
140 -- Initialize iteration over set of interpretations for Node N. The first
141 -- interpretation is placed in It, and I is initialized for subsequent
142 -- calls to Get_Next_Interp.
144 procedure Get_Next_Interp
(I
: in out Interp_Index
; It
: out Interp
);
145 -- Iteration step over set of interpretations. Using the value in I, which
146 -- was set by a previous call to Get_First_Interp or Get_Next_Interp, the
147 -- next interpretation is placed in It, and I is updated for the next call.
148 -- The end of the list of interpretations is signalled by It.Nam = Empty.
150 procedure Remove_Interp
(I
: in out Interp_Index
);
151 -- Remove an interpretation that his hidden by another, or that does not
152 -- match the context. The value of I on input was set by a call to either
153 -- Get_First_Interp or Get_Next_Interp and references the interpretation
154 -- to be removed. The only allowed use of the exit value of I is as input
155 -- to a subsequent call to Get_Next_Interp, which yields the interpretation
156 -- following the removed one.
158 procedure Save_Interps
(Old_N
: Node_Id
; New_N
: Node_Id
);
159 -- If an overloaded node is rewritten during semantic analysis, its
160 -- possible interpretations must be linked to the copy. This procedure
161 -- transfers the overload information from Old_N, the old node, to
162 -- New_N, its new copy. It has no effect in the non-overloaded case.
164 function Covers
(T1
, T2
: Entity_Id
) return Boolean;
165 -- This is the basic type compatibility routine. T1 is the expected
166 -- type, imposed by context, and T2 is the actual type. The processing
167 -- reflects both the definition of type coverage and the rules
168 -- for operand matching.
170 function Disambiguate
172 I1
, I2
: Interp_Index
;
175 -- If more than one interpretation of a name in a call is legal, apply
176 -- preference rules (universal types first) and operator visibility in
177 -- order to remove ambiguity. I1 and I2 are the first two interpretations
178 -- that are compatible with the context, but there may be others.
180 function Entity_Matches_Spec
(Old_S
, New_S
: Entity_Id
) return Boolean;
181 -- To resolve subprogram renaming and default formal subprograms in generic
182 -- definitions. Old_S is a possible interpretation of the entity being
183 -- renamed, New_S has an explicit signature. If Old_S is a subprogram, as
184 -- opposed to an operator, type and mode conformance are required.
186 function Find_Unique_Type
(L
: Node_Id
; R
: Node_Id
) return Entity_Id
;
187 -- Used in second pass of resolution, for equality and comparison nodes.
188 -- L is the left operand, whose type is known to be correct, and R is
189 -- the right operand, which has one interpretation compatible with that
190 -- of L. Return the type intersection of the two.
192 function Has_Compatible_Type
196 -- Verify that some interpretation of the node N has a type compatible
197 -- with Typ. If N is not overloaded, then its unique type must be
198 -- compatible with Typ. Otherwise iterate through the interpretations
199 -- of N looking for a compatible one.
201 function Hides_Op
(F
: Entity_Id
; Op
: Entity_Id
) return Boolean;
202 -- A user-defined function hides a predefined operator if it is
203 -- matches the signature of the operator, and is declared in an
204 -- open scope, or in the scope of the result type.
206 function Interface_Present_In_Ancestor
208 Iface
: Entity_Id
) return Boolean;
209 -- Ada 2005 (AI-251): Typ must be a tagged record type/subtype and Iface
210 -- must be an abstract interface type. This function is used to check if
211 -- some ancestor of Typ implements Iface.
213 function Intersect_Types
(L
, R
: Node_Id
) return Entity_Id
;
214 -- Find the common interpretation to two analyzed nodes. If one of the
215 -- interpretations is universal, choose the non-universal one. If either
216 -- node is overloaded, find single common interpretation.
218 function Is_Subtype_Of
(T1
: Entity_Id
; T2
: Entity_Id
) return Boolean;
219 -- Checks whether T1 is any subtype of T2 directly or indirectly. Applies
220 -- only to scalar subtypes ???
222 function Is_Ancestor
(T1
, T2
: Entity_Id
) return Boolean;
223 -- T1 is a tagged type (not class-wide). Verify that it is one of the
224 -- ancestors of type T2 (which may or not be class-wide)
226 function Operator_Matches_Spec
(Op
, New_S
: Entity_Id
) return Boolean;
227 -- Used to resolve subprograms renaming operators, and calls to user
228 -- defined operators. Determines whether a given operator Op, matches
229 -- a specification, New_S.
231 function Valid_Comparison_Arg
(T
: Entity_Id
) return Boolean;
232 -- A valid argument to an ordering operator must be a discrete type, a
233 -- real type, or a one dimensional array with a discrete component type.
235 function Valid_Boolean_Arg
(T
: Entity_Id
) return Boolean;
236 -- A valid argument of a boolean operator is either some boolean type,
237 -- or a one-dimensional array of boolean type.
239 procedure Write_Interp_Ref
(Map_Ptr
: Int
);
240 -- Debugging procedure to display entry in Interp_Map. Would not be
241 -- needed if it were possible to debug instantiations of Table.
243 procedure Write_Overloads
(N
: Node_Id
);
244 -- Debugging procedure to output info on possibly overloaded entities
245 -- for specified node.