OSDN Git Service

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