OSDN Git Service

2008-05-27 Thomas Quinot <quinot@adacore.com>
[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 --          Copyright (C) 1992-2007, 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
59    --  type is the declared return type. For operators, the type is determined
60    --  by the type of the arguments. If the arguments themselves are
61    --  overloaded, we enter the operator name in the names table for each
62    --  possible result type. In most cases, arguments are not overloaded and
63    --  only one interpretation is present anyway.
64
65    type Interp is record
66       Nam         : Entity_Id;
67       Typ         : Entity_Id;
68       Abstract_Op : Entity_Id := Empty;
69    end record;
70
71    --  Entity Abstract_Op is set to the abstract operation which potentially
72    --  disables the interpretation in Ada 2005 mode.
73
74    No_Interp : constant Interp := (Empty, Empty, Empty);
75
76    subtype Interp_Index is Int;
77
78    ---------------------
79    -- Error Reporting --
80    ---------------------
81
82    --  A common error is the use of an operator in infix notation on arguments
83    --  of a type that is not directly visible. Rather than diagnosing a type
84    --  mismatch, it is better to indicate that the type can be made use-visible
85    --  with the appropriate use clause. The global variable Candidate_Type is
86    --  set in Add_One_Interp whenever an interpretation might be legal for an
87    --  operator if the type were directly visible. This variable is used in
88    --  sem_ch4 when no legal interpretation is found.
89
90    Candidate_Type : Entity_Id;
91
92    -----------------
93    -- Subprograms --
94    -----------------
95
96    procedure Init_Interp_Tables;
97    --  Invoked by gnatf when processing multiple files
98
99    procedure Collect_Interps (N : Node_Id);
100    --  Invoked when the name N has more than one visible interpretation.
101    --  This is the high level routine which accumulates the possible
102    --  interpretations of the node. The first meaning and type of N have
103    --  already been stored in N. If the name is an expanded name, the homonyms
104    --  are only those that belong to the same scope.
105
106    function Is_Invisible_Operator
107      (N    : Node_Id;
108       T    : Entity_Id)
109       return Boolean;
110    --  Check whether a predefined operation with universal operands appears
111    --  in a context in which the operators of the expected type are not
112    --  visible.
113
114    procedure List_Interps (Nam : Node_Id; Err : Node_Id);
115    --  List candidate interpretations of an overloaded name. Used for
116    --  various error reports.
117
118    procedure Add_One_Interp
119      (N         : Node_Id;
120       E         : Entity_Id;
121       T         : Entity_Id;
122       Opnd_Type : 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,
125    --  E is an overloadable entity, and T is its type. For constructs such
126    --  as indexed expressions, the caller sets E equal to T, because the
127    --  overloading comes from other fields, and the node itself has no name
128    --  to resolve. Hidden denotes whether an interpretation has been disabled
129    --  by an abstract operator. Add_One_Interp includes semantic processing to
130    --  deal with 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_Type, which is a candidate type for one
135    --  of the operands of N, to check visibility.
136
137    procedure End_Interp_List;
138    --  End the list of interpretations of current node
139
140    procedure Get_First_Interp
141      (N  : Node_Id;
142       I  : out Interp_Index;
143       It : out Interp);
144    --  Initialize iteration over set of interpretations for Node N. The first
145    --  interpretation is placed in It, and I is initialized for subsequent
146    --  calls to Get_Next_Interp.
147
148    procedure Get_Next_Interp (I : in out Interp_Index; It : out Interp);
149    --  Iteration step over set of interpretations. Using the value in I, which
150    --  was set by a previous call to Get_First_Interp or Get_Next_Interp, the
151    --  next interpretation is placed in It, and I is updated for the next call.
152    --  The end of the list of interpretations is signalled by It.Nam = Empty.
153
154    procedure Remove_Interp (I : in out Interp_Index);
155    --  Remove an interpretation that his hidden by another, or that does not
156    --  match the context. The value of I on input was set by a call to either
157    --  Get_First_Interp or Get_Next_Interp and references the interpretation
158    --  to be removed. The only allowed use of the exit value of I is as input
159    --  to a subsequent call to Get_Next_Interp, which yields the interpretation
160    --  following the removed one.
161
162    procedure Save_Interps (Old_N : Node_Id; New_N : Node_Id);
163    --  If an overloaded node is rewritten during semantic analysis, its
164    --  possible interpretations must be linked to the copy. This procedure
165    --  transfers the overload information from Old_N, the old node, to
166    --  New_N, its new copy. It has no effect in the non-overloaded case.
167
168    function Covers (T1, T2 : Entity_Id) return Boolean;
169    --  This is the basic type compatibility routine. T1 is the expected
170    --  type, imposed by context, and T2 is the actual type. The processing
171    --  reflects both the definition of type coverage and the rules
172    --  for operand matching.
173
174    function Disambiguate
175      (N      : Node_Id;
176       I1, I2 : Interp_Index;
177       Typ    : Entity_Id)
178       return   Interp;
179    --  If more than one interpretation of a name in a call is legal, apply
180    --  preference rules (universal types first) and operator visibility in
181    --  order to remove ambiguity. I1 and I2 are the first two interpretations
182    --  that are compatible with the context, but there may be others.
183
184    function Entity_Matches_Spec (Old_S,  New_S : Entity_Id) return Boolean;
185    --  To resolve subprogram renaming and default formal subprograms in generic
186    --  definitions. Old_S is a possible interpretation of the entity being
187    --  renamed, New_S has an explicit signature. If Old_S is a subprogram, as
188    --  opposed to an operator, type and mode conformance are required.
189
190    function Find_Unique_Type (L : Node_Id; R : Node_Id) return Entity_Id;
191    --  Used in second pass of resolution,  for equality and comparison nodes.
192    --  L is the left operand, whose type is known to be correct, and R is
193    --  the right operand,  which has one interpretation compatible with that
194    --  of L. Return the type intersection of the two.
195
196    function Has_Compatible_Type
197      (N    : Node_Id;
198       Typ  : Entity_Id)
199       return Boolean;
200    --  Verify that some interpretation of the node N has a type compatible
201    --  with Typ. If N is not overloaded, then its unique type must be
202    --  compatible with Typ. Otherwise iterate through the interpretations
203    --  of N looking for a compatible one.
204
205    function Hides_Op (F : Entity_Id; Op : Entity_Id) return Boolean;
206    --  A user-defined function hides a predefined operator if it is
207    --  matches the signature of the operator, and is declared in an
208    --  open scope, or in the scope of the result type.
209
210    function Interface_Present_In_Ancestor
211      (Typ   : Entity_Id;
212       Iface : Entity_Id) return Boolean;
213    --  Ada 2005 (AI-251): Typ must be a tagged record type/subtype and Iface
214    --  must be an abstract interface type (or a class-wide abstract interface).
215    --  This function is used to check if Typ or some ancestor of Typ implements
216    --  Iface (returning True only if so).
217
218    function Intersect_Types (L, R : Node_Id) return Entity_Id;
219    --  Find the common interpretation to two analyzed nodes. If one of the
220    --  interpretations is universal, choose the non-universal one. If either
221    --  node is overloaded, find single common interpretation.
222
223    function Is_Ancestor (T1, T2 : Entity_Id) return Boolean;
224    --  T1 is a tagged type (not class-wide). Verify that it is one of the
225    --  ancestors of type T2 (which may or not be class-wide)
226
227    function Is_Subtype_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean;
228    --  Checks whether T1 is any subtype of T2 directly or indirectly. Applies
229    --  only to scalar subtypes ???
230
231    function Operator_Matches_Spec (Op, New_S : Entity_Id) return Boolean;
232    --  Used to resolve subprograms renaming operators, and calls to user
233    --  defined operators. Determines whether a given operator Op, matches
234    --  a specification, New_S.
235
236    procedure Set_Abstract_Op (I : Interp_Index; V : Entity_Id);
237    --  Set the abstract operation field of an interpretation
238
239    function Valid_Comparison_Arg (T : Entity_Id) return Boolean;
240    --  A valid argument to an ordering operator must be a discrete type, a
241    --  real type, or a one dimensional array with a discrete component type.
242
243    function Valid_Boolean_Arg (T : Entity_Id) return Boolean;
244    --  A valid argument of a boolean operator is either some boolean type,
245    --  or a one-dimensional array of boolean type.
246
247    procedure Write_Interp_Ref (Map_Ptr : Int);
248    --  Debugging procedure to display entry in Interp_Map. Would not be
249    --  needed if it were possible to debug instantiations of Table.
250
251    procedure Write_Overloads (N : Node_Id);
252    --  Debugging procedure to output info on possibly overloaded entities
253    --  for specified node.
254
255 end Sem_Type;