FSF GCC merge 02/23/03
[official-gcc.git] / gcc / ada / sem_type.ads
blob2eb4cae2adb2f1b5616c2c1a6d599389247a9d0f
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ T Y P E --
6 -- --
7 -- S p e c --
8 -- --
9 -- --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
11 -- --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 -- --
26 ------------------------------------------------------------------------------
28 -- This unit contains the routines used to handle type determination,
29 -- including the routine used to support overload resolution.
31 with Alloc;
32 with Table;
33 with Types; use Types;
35 package Sem_Type is
37 ---------------------------------------------
38 -- Data Structures for Overload Resolution --
39 ---------------------------------------------
41 -- To determine the unique meaning of an identifier, overload resolution
42 -- may have to be performed if the visibility rules alone identify more
43 -- than one possible entity as the denotation of a given identifier. When
44 -- the visibility rules find such a potential ambiguity, the set of
45 -- possible interpretations must be attached to the identifier, and
46 -- overload resolution must be performed over the innermost enclosing
47 -- complete context. At the end of the resolution, either a single
48 -- interpretation is found for all identifiers in the context, or else a
49 -- type error (invalid type or ambiguous reference) must be signalled.
51 -- The set of interpretations of a given name is stored in a data structure
52 -- that is separate from the syntax tree, because it corresponds to
53 -- transient information. The interpretations themselves are stored in
54 -- table All_Interp. A mapping from tree nodes to sets of interpretations
55 -- called Interp_Map, is maintained by the overload resolution routines.
56 -- Both these structures are initialized at the beginning of every complete
57 -- context.
59 -- Corresponding to the set of interpretation for a given overloadable
60 -- identifier, there is a set of possible types corresponding to the types
61 -- that the overloaded call may return. We keep a 1-to-1 correspondence
62 -- between interpretations and types: for user-defined subprograms the
63 -- type is the declared return type. For operators, the type is determined
64 -- by the type of the arguments. If the arguments themselves are
65 -- overloaded, we enter the operator name in the names table for each
66 -- possible result type. In most cases, arguments are not overloaded and
67 -- only one interpretation is present anyway.
69 type Interp is record
70 Nam : Entity_Id;
71 Typ : Entity_Id;
72 end record;
74 No_Interp : constant Interp := (Empty, Empty);
76 package All_Interp is new Table.Table (
77 Table_Component_Type => Interp,
78 Table_Index_Type => Int,
79 Table_Low_Bound => 0,
80 Table_Initial => Alloc.All_Interp_Initial,
81 Table_Increment => Alloc.All_Interp_Increment,
82 Table_Name => "All_Interp");
84 -- The following data structures establish a mapping between nodes and
85 -- their interpretations. Eventually the Interp_Index corresponding to
86 -- the first interpretation of a node may be stored directly in the
87 -- corresponding node.
89 subtype Interp_Index is Int;
91 type Interp_Ref is record
92 Node : Node_Id;
93 Index : Interp_Index;
94 end record;
96 package Interp_Map is new Table.Table (
97 Table_Component_Type => Interp_Ref,
98 Table_Index_Type => Int,
99 Table_Low_Bound => 0,
100 Table_Initial => Alloc.Interp_Map_Initial,
101 Table_Increment => Alloc.Interp_Map_Increment,
102 Table_Name => "Interp_Map");
104 -- For now Interp_Map is searched sequentially
106 ----------------------
107 -- Error Reporting --
108 ----------------------
110 -- A common error is the use of an operator in infix notation on arguments
111 -- of a type that is not directly visible. Rather than diagnosing a type
112 -- mismatch, it is better to indicate that the type can be made use-visible
113 -- with the appropriate use clause. The global variable Candidate_Type is
114 -- set in Add_One_Interp whenever an interpretation might be legal for an
115 -- operator if the type were directly visible. This variable is used in
116 -- sem_ch4 when no legal interpretation is found.
118 Candidate_Type : Entity_Id;
120 -----------------
121 -- Subprograms --
122 -----------------
124 procedure Init_Interp_Tables;
125 -- Invoked by gnatf when processing multiple files.
127 procedure Collect_Interps (N : Node_Id);
128 -- Invoked when the name N has more than one visible interpretation.
129 -- This is the high level routine which accumulates the possible
130 -- interpretations of the node. The first meaning and type of N have
131 -- already been stored in N. If the name is an expanded name, the homonyms
132 -- are only those that belong to the same scope.
134 procedure New_Interps (N : Node_Id);
135 -- Initialize collection of interpretations for the given node, which is
136 -- either an overloaded entity, or an operation whose arguments have
137 -- multiple intepretations. Interpretations can be added to only one
138 -- node at a time.
140 procedure Add_One_Interp
141 (N : Node_Id;
142 E : Entity_Id;
143 T : Entity_Id;
144 Opnd_Type : Entity_Id := Empty);
145 -- Add (E, T) to the list of interpretations of the node being resolved.
146 -- For calls and operators, i.e. for nodes that have a name field,
147 -- E is an overloadable entity, and T is its type. For constructs such
148 -- as indexed expressions, the caller sets E equal to T, because the
149 -- overloading comes from other fields, and the node itself has no name
150 -- to resolve. Add_One_Interp includes the semantic processing to deal
151 -- with adding entries that hide one another etc.
153 -- For operators, the legality of the operation depends on the visibility
154 -- of T and its scope. If the operator is an equality or comparison, T is
155 -- always Boolean, and we use Opnd_Type, which is a candidate type for one
156 -- of the operands of N, to check visibility.
158 procedure End_Interp_List;
159 -- End the list of interpretations of current node.
161 procedure Get_First_Interp
162 (N : Node_Id;
163 I : out Interp_Index;
164 It : out Interp);
165 -- Initialize iteration over set of interpretations for Node N. The first
166 -- interpretation is placed in It, and I is initialized for subsequent
167 -- calls to Get_Next_Interp.
169 procedure Get_Next_Interp (I : in out Interp_Index; It : out Interp);
170 -- Iteration step over set of interpretations. Using the value in I, which
171 -- was set by a previous call to Get_First_Interp or Get_Next_Interp, the
172 -- next interpretation is placed in It, and I is updated for the next call.
173 -- The end of the list of interpretations is signalled by It.Nam = Empty.
175 procedure Remove_Interp (I : in out Interp_Index);
176 -- Remove an interpretation that his hidden by another, or that does not
177 -- match the context. The value of I on input was set by a call to either
178 -- Get_First_Interp or Get_Next_Interp and references the interpretation
179 -- to be removed. The only allowed use of the exit value of I is as input
180 -- to a subsequent call to Get_Next_Interp, which yields the interpretation
181 -- following the removed one.
183 procedure Save_Interps (Old_N : Node_Id; New_N : Node_Id);
184 -- If an overloaded node is rewritten during semantic analysis, its
185 -- possible interpretations must be linked to the copy. This procedure
186 -- transfers the overload information from Old_N, the old node, to
187 -- New_N, its new copy. It has no effect in the non-overloaded case.
189 function Covers (T1, T2 : Entity_Id) return Boolean;
190 -- This is the basic type compatibility routine. T1 is the expexted
191 -- type, imposed by context, and T2 is the actual type. The processing
192 -- reflects both the definition of type coverage and the rules
193 -- for operand matching.
195 function Disambiguate
196 (N : Node_Id;
197 I1, I2 : Interp_Index;
198 Typ : Entity_Id)
199 return Interp;
200 -- If more than one interpretation of a name in a call is legal, apply
201 -- preference rules (universal types first) and operator visibility in
202 -- order to remove ambiguity. I1 and I2 are the first two interpretations
203 -- that are compatible with the context, but there may be others.
205 function Entity_Matches_Spec (Old_S, New_S : Entity_Id) return Boolean;
206 -- To resolve subprogram renaming and default formal subprograms in generic
207 -- definitions. Old_S is a possible interpretation of the entity being
208 -- renamed, New_S has an explicit signature. If Old_S is a subprogram, as
209 -- opposed to an operator, type and mode conformance are required.
211 function Find_Unique_Type (L : Node_Id; R : Node_Id) return Entity_Id;
212 -- Used in second pass of resolution, for equality and comparison nodes.
213 -- L is the left operand, whose type is known to be correct, and R is
214 -- the right operand, which has one interpretation compatible with that
215 -- of L. Return the type intersection of the two.
217 function Has_Compatible_Type
218 (N : Node_Id;
219 Typ : Entity_Id)
220 return Boolean;
221 -- Verify that some interpretation of the node N has a type compatible
222 -- with Typ. If N is not overloaded, then its unique type must be
223 -- compatible with Typ. Otherwise iterate through the interpretations
224 -- of N looking for a compatible one.
226 function Hides_Op (F : Entity_Id; Op : Entity_Id) return Boolean;
227 -- A user-defined function hides a predefined operator if it is
228 -- matches the signature of the operator, and is declared in an
229 -- open scope, or in the scope of the result type.
231 function Intersect_Types (L, R : Node_Id) return Entity_Id;
232 -- Find the common interpretation to two analyzed nodes. If one of the
233 -- interpretations is universal, choose the non-universal one. If either
234 -- node is overloaded, find single common interpretation.
236 function Is_Subtype_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean;
237 -- Checks whether T1 is any subtype of T2 directly or indirectly. Applies
238 -- only to scalar subtypes ???
240 function Is_Ancestor (T1, T2 : Entity_Id) return Boolean;
241 -- T1 is a tagged type (not class-wide). Verify that it is one of the
242 -- ancestors of type T2 (which may or not be class-wide)
244 function Operator_Matches_Spec (Op, New_S : Entity_Id) return Boolean;
245 -- Used to resolve subprograms renaming operators, and calls to user
246 -- defined operators. Determines whether a given operator Op, matches
247 -- a specification, New_S.
249 function Valid_Comparison_Arg (T : Entity_Id) return Boolean;
250 -- A valid argument to an ordering operator must be a discrete type, a
251 -- real type, or a one dimensional array with a discrete component type.
253 function Valid_Boolean_Arg (T : Entity_Id) return Boolean;
254 -- A valid argument of a boolean operator is either some boolean type,
255 -- or a one-dimensional array of boolean type.
257 procedure Write_Overloads (N : Node_Id);
258 -- Debugging procedure to output info on possibly overloaded entities
259 -- for specified node.
261 end Sem_Type;