1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2001 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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.
32 with Types
; use Types
;
36 ---------------------------------------------
37 -- Data Structures for Overload Resolution --
38 ---------------------------------------------
40 -- To determine the unique meaning of an identifier, overload resolution
41 -- may have to be performed if the visibility rules alone identify more
42 -- than one possible entity as the denotation of a given identifier. When
43 -- the visibility rules find such a potential ambiguity, the set of
44 -- possible interpretations must be attached to the identifier, and
45 -- overload resolution must be performed over the innermost enclosing
46 -- complete context. At the end of the resolution, either a single
47 -- interpretation is found for all identifiers in the context, or else a
48 -- type error (invalid type or ambiguous reference) must be signalled.
50 -- The set of interpretations of a given name is stored in a data structure
51 -- that is separate from the syntax tree, because it corresponds to
52 -- transient information. The interpretations themselves are stored in
53 -- table All_Interp. A mapping from tree nodes to sets of interpretations
54 -- called Interp_Map, is maintained by the overload resolution routines.
55 -- Both these structures are initialized at the beginning of every complete
58 -- Corresponding to the set of interpretation for a given overloadable
59 -- identifier, there is a set of possible types corresponding to the types
60 -- that the overloaded call may return. We keep a 1-to-1 correspondence
61 -- between interpretations and types: for user-defined subprograms the
62 -- type is the declared return type. For operators, the type is determined
63 -- by the type of the arguments. If the arguments themselves are
64 -- overloaded, we enter the operator name in the names table for each
65 -- possible result type. In most cases, arguments are not overloaded and
66 -- only one interpretation is present anyway.
73 No_Interp
: constant Interp
:= (Empty
, Empty
);
75 package All_Interp
is new Table
.Table
(
76 Table_Component_Type
=> Interp
,
77 Table_Index_Type
=> Int
,
79 Table_Initial
=> Alloc
.All_Interp_Initial
,
80 Table_Increment
=> Alloc
.All_Interp_Increment
,
81 Table_Name
=> "All_Interp");
83 -- The following data structures establish a mapping between nodes and
84 -- their interpretations. Eventually the Interp_Index corresponding to
85 -- the first interpretation of a node may be stored directly in the
86 -- corresponding node.
88 subtype Interp_Index
is Int
;
90 type Interp_Ref
is record
95 package Interp_Map
is new Table
.Table
(
96 Table_Component_Type
=> Interp_Ref
,
97 Table_Index_Type
=> Int
,
99 Table_Initial
=> Alloc
.Interp_Map_Initial
,
100 Table_Increment
=> Alloc
.Interp_Map_Increment
,
101 Table_Name
=> "Interp_Map");
103 -- For now Interp_Map is searched sequentially
105 ----------------------
106 -- Error Reporting --
107 ----------------------
109 -- A common error is the use of an operator in infix notation on arguments
110 -- of a type that is not directly visible. Rather than diagnosing a type
111 -- mismatch, it is better to indicate that the type can be made use-visible
112 -- with the appropriate use clause. The global variable Candidate_Type is
113 -- set in Add_One_Interp whenever an interpretation might be legal for an
114 -- operator if the type were directly visible. This variable is used in
115 -- sem_ch4 when no legal interpretation is found.
117 Candidate_Type
: Entity_Id
;
123 procedure Init_Interp_Tables
;
124 -- Invoked by gnatf when processing multiple files.
126 procedure Collect_Interps
(N
: Node_Id
);
127 -- Invoked when the name N has more than one visible interpretation.
128 -- This is the high level routine which accumulates the possible
129 -- interpretations of the node. The first meaning and type of N have
130 -- already been stored in N. If the name is an expanded name, the homonyms
131 -- are only those that belong to the same scope.
133 procedure New_Interps
(N
: Node_Id
);
134 -- Initialize collection of interpretations for the given node, which is
135 -- either an overloaded entity, or an operation whose arguments have
136 -- multiple intepretations. Interpretations can be added to only one
139 procedure Add_One_Interp
143 Opnd_Type
: Entity_Id
:= Empty
);
144 -- Add (E, T) to the list of interpretations of the node being resolved.
145 -- For calls and operators, i.e. for nodes that have a name field,
146 -- E is an overloadable entity, and T is its type. For constructs such
147 -- as indexed expressions, the caller sets E equal to T, because the
148 -- overloading comes from other fields, and the node itself has no name
149 -- to resolve. Add_One_Interp includes the semantic processing to deal
150 -- with adding entries that hide one another etc.
152 -- For operators, the legality of the operation depends on the visibility
153 -- of T and its scope. If the operator is an equality or comparison, T is
154 -- always Boolean, and we use Opnd_Type, which is a candidate type for one
155 -- of the operands of N, to check visibility.
157 procedure End_Interp_List
;
158 -- End the list of interpretations of current node.
160 procedure Get_First_Interp
162 I
: out Interp_Index
;
164 -- Initialize iteration over set of interpretations for Node N. The first
165 -- interpretation is placed in It, and I is initialized for subsequent
166 -- calls to Get_Next_Interp.
168 procedure Get_Next_Interp
(I
: in out Interp_Index
; It
: out Interp
);
169 -- Iteration step over set of interpretations. Using the value in I, which
170 -- was set by a previous call to Get_First_Interp or Get_Next_Interp, the
171 -- next interpretation is placed in It, and I is updated for the next call.
172 -- The end of the list of interpretations is signalled by It.Nam = Empty.
174 procedure Remove_Interp
(I
: in out Interp_Index
);
175 -- Remove an interpretation that his hidden by another, or that does not
176 -- match the context. The value of I on input was set by a call to either
177 -- Get_First_Interp or Get_Next_Interp and references the interpretation
178 -- to be removed. The only allowed use of the exit value of I is as input
179 -- to a subsequent call to Get_Next_Interp, which yields the interpretation
180 -- following the removed one.
182 procedure Save_Interps
(Old_N
: Node_Id
; New_N
: Node_Id
);
183 -- If an overloaded node is rewritten during semantic analysis, its
184 -- possible interpretations must be linked to the copy. This procedure
185 -- transfers the overload information from Old_N, the old node, to
186 -- New_N, its new copy. It has no effect in the non-overloaded case.
188 function Covers
(T1
, T2
: Entity_Id
) return Boolean;
189 -- This is the basic type compatibility routine. T1 is the expexted
190 -- type, imposed by context, and T2 is the actual type. The processing
191 -- reflects both the definition of type coverage and the rules
192 -- for operand matching.
194 function Disambiguate
196 I1
, I2
: Interp_Index
;
199 -- If more than one interpretation of a name in a call is legal, apply
200 -- preference rules (universal types first) and operator visibility in
201 -- order to remove ambiguity. I1 and I2 are the first two interpretations
202 -- that are compatible with the context, but there may be others.
204 function Entity_Matches_Spec
(Old_S
, New_S
: Entity_Id
) return Boolean;
205 -- To resolve subprogram renaming and default formal subprograms in generic
206 -- definitions. Old_S is a possible interpretation of the entity being
207 -- renamed, New_S has an explicit signature. If Old_S is a subprogram, as
208 -- opposed to an operator, type and mode conformance are required.
210 function Find_Unique_Type
(L
: Node_Id
; R
: Node_Id
) return Entity_Id
;
211 -- Used in second pass of resolution, for equality and comparison nodes.
212 -- L is the left operand, whose type is known to be correct, and R is
213 -- the right operand, which has one interpretation compatible with that
214 -- of L. Return the type intersection of the two.
216 function Has_Compatible_Type
220 -- Verify that some interpretation of the node N has a type compatible
221 -- with Typ. If N is not overloaded, then its unique type must be
222 -- compatible with Typ. Otherwise iterate through the interpretations
223 -- of N looking for a compatible one.
225 function Hides_Op
(F
: Entity_Id
; Op
: Entity_Id
) return Boolean;
226 -- A user-defined function hides a predefined operator if it is
227 -- matches the signature of the operator, and is declared in an
228 -- open scope, or in the scope of the result type.
230 function Intersect_Types
(L
, R
: Node_Id
) return Entity_Id
;
231 -- Find the common interpretation to two analyzed nodes. If one of the
232 -- interpretations is universal, choose the non-universal one. If either
233 -- node is overloaded, find single common interpretation.
235 function Is_Subtype_Of
(T1
: Entity_Id
; T2
: Entity_Id
) return Boolean;
236 -- Checks whether T1 is any subtype of T2 directly or indirectly. Applies
237 -- only to scalar subtypes ???
239 function Is_Ancestor
(T1
, T2
: Entity_Id
) return Boolean;
240 -- T1 is a tagged type (not class-wide). Verify that it is one of the
241 -- ancestors of type T2 (which may or not be class-wide)
243 function Operator_Matches_Spec
(Op
, New_S
: Entity_Id
) return Boolean;
244 -- Used to resolve subprograms renaming operators, and calls to user
245 -- defined operators. Determines whether a given operator Op, matches
246 -- a specification, New_S.
248 function Valid_Comparison_Arg
(T
: Entity_Id
) return Boolean;
249 -- A valid argument to an ordering operator must be a discrete type, a
250 -- real type, or a one dimensional array with a discrete component type.
252 function Valid_Boolean_Arg
(T
: Entity_Id
) return Boolean;
253 -- A valid argument of a boolean operator is either some boolean type,
254 -- or a one-dimensional array of boolean type.
256 procedure Write_Overloads
(N
: Node_Id
);
257 -- Debugging procedure to output info on possibly overloaded entities
258 -- for specified node.