annotate gcc/ada/sem_type.ads @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- S E M _ T Y P E --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
kono
parents:
diff changeset
9 -- Copyright (C) 1992-2013, Free Software Foundation, Inc. --
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
kono
parents:
diff changeset
17 -- for more details. You should have received a copy of the GNU General --
kono
parents:
diff changeset
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
kono
parents:
diff changeset
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
kono
parents:
diff changeset
20 -- --
kono
parents:
diff changeset
21 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
23 -- --
kono
parents:
diff changeset
24 ------------------------------------------------------------------------------
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 -- This unit contains the routines used to handle type determination,
kono
parents:
diff changeset
27 -- including the routine used to support overload resolution.
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 with Types; use Types;
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 package Sem_Type is
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 ---------------------------------------------
kono
parents:
diff changeset
34 -- Data Structures for Overload Resolution --
kono
parents:
diff changeset
35 ---------------------------------------------
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 -- To determine the unique meaning of an identifier, overload resolution
kono
parents:
diff changeset
38 -- may have to be performed if the visibility rules alone identify more
kono
parents:
diff changeset
39 -- than one possible entity as the denotation of a given identifier. When
kono
parents:
diff changeset
40 -- the visibility rules find such a potential ambiguity, the set of
kono
parents:
diff changeset
41 -- possible interpretations must be attached to the identifier, and
kono
parents:
diff changeset
42 -- overload resolution must be performed over the innermost enclosing
kono
parents:
diff changeset
43 -- complete context. At the end of the resolution, either a single
kono
parents:
diff changeset
44 -- interpretation is found for all identifiers in the context, or else a
kono
parents:
diff changeset
45 -- type error (invalid type or ambiguous reference) must be signalled.
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 -- The set of interpretations of a given name is stored in a data structure
kono
parents:
diff changeset
48 -- that is separate from the syntax tree, because it corresponds to
kono
parents:
diff changeset
49 -- transient information. The interpretations themselves are stored in
kono
parents:
diff changeset
50 -- table All_Interp. A mapping from tree nodes to sets of interpretations
kono
parents:
diff changeset
51 -- called Interp_Map, is maintained by the overload resolution routines.
kono
parents:
diff changeset
52 -- Both these structures are initialized at the beginning of every complete
kono
parents:
diff changeset
53 -- context.
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 -- Corresponding to the set of interpretations for a given overloadable
kono
parents:
diff changeset
56 -- identifier, there is a set of possible types corresponding to the types
kono
parents:
diff changeset
57 -- that the overloaded call may return. We keep a 1-to-1 correspondence
kono
parents:
diff changeset
58 -- between interpretations and types: for user-defined subprograms the type
kono
parents:
diff changeset
59 -- is the declared return type. For operators, the type is determined by
kono
parents:
diff changeset
60 -- the type of the arguments. If the arguments themselves are overloaded,
kono
parents:
diff changeset
61 -- we enter the operator name in the names table for each possible result
kono
parents:
diff changeset
62 -- type. In most cases, arguments are not overloaded and only one
kono
parents:
diff changeset
63 -- interpretation is present anyway.
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 type Interp is record
kono
parents:
diff changeset
66 Nam : Entity_Id;
kono
parents:
diff changeset
67 Typ : Entity_Id;
kono
parents:
diff changeset
68 Abstract_Op : Entity_Id := Empty;
kono
parents:
diff changeset
69 end record;
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 -- Entity Abstract_Op is set to the abstract operation which potentially
kono
parents:
diff changeset
72 -- disables the interpretation in Ada 2005 mode.
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 No_Interp : constant Interp := (Empty, Empty, Empty);
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 type Interp_Index is new Int;
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 ---------------------
kono
parents:
diff changeset
79 -- Error Reporting --
kono
parents:
diff changeset
80 ---------------------
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 -- A common error is the use of an operator in infix notation on arguments
kono
parents:
diff changeset
83 -- of a type that is not directly visible. Rather than diagnosing a type
kono
parents:
diff changeset
84 -- mismatch, it is better to indicate that the type can be made use-visible
kono
parents:
diff changeset
85 -- with the appropriate use clause. The global variable Candidate_Type is
kono
parents:
diff changeset
86 -- set in Add_One_Interp whenever an interpretation might be legal for an
kono
parents:
diff changeset
87 -- operator if the type were directly visible. This variable is used in
kono
parents:
diff changeset
88 -- sem_ch4 when no legal interpretation is found.
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 Candidate_Type : Entity_Id;
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 -----------------
kono
parents:
diff changeset
93 -- Subprograms --
kono
parents:
diff changeset
94 -----------------
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 procedure Init_Interp_Tables;
kono
parents:
diff changeset
97 -- Invoked by gnatf when processing multiple files
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 procedure Collect_Interps (N : Node_Id);
kono
parents:
diff changeset
100 -- Invoked when the name N has more than one visible interpretation. This
kono
parents:
diff changeset
101 -- is the high level routine which accumulates the possible interpretations
kono
parents:
diff changeset
102 -- of the node. The first meaning and type of N have already been stored
kono
parents:
diff changeset
103 -- in N. If the name is an expanded name, the homonyms are only those that
kono
parents:
diff changeset
104 -- belong to the same scope.
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 function Is_Invisible_Operator (N : Node_Id; T : Entity_Id) return Boolean;
kono
parents:
diff changeset
107 -- Check whether a predefined operation with universal operands appears in
kono
parents:
diff changeset
108 -- a context in which the operators of the expected type are not visible.
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 procedure List_Interps (Nam : Node_Id; Err : Node_Id);
kono
parents:
diff changeset
111 -- List candidate interpretations of an overloaded name. Used for various
kono
parents:
diff changeset
112 -- error reports.
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 procedure Add_One_Interp
kono
parents:
diff changeset
115 (N : Node_Id;
kono
parents:
diff changeset
116 E : Entity_Id;
kono
parents:
diff changeset
117 T : Entity_Id;
kono
parents:
diff changeset
118 Opnd_Type : Entity_Id := Empty);
kono
parents:
diff changeset
119 -- Add (E, T) to the list of interpretations of the node being resolved.
kono
parents:
diff changeset
120 -- For calls and operators, i.e. for nodes that have a name field, E is an
kono
parents:
diff changeset
121 -- overloadable entity, and T is its type. For constructs such as indexed
kono
parents:
diff changeset
122 -- expressions, the caller sets E equal to T, because the overloading comes
kono
parents:
diff changeset
123 -- from other fields, and the node itself has no name to resolve. Hidden
kono
parents:
diff changeset
124 -- denotes whether an interpretation has been disabled by an abstract
kono
parents:
diff changeset
125 -- operator. Add_One_Interp includes semantic processing to deal with
kono
parents:
diff changeset
126 -- adding entries that hide one another etc.
kono
parents:
diff changeset
127 --
kono
parents:
diff changeset
128 -- For operators, the legality of the operation depends on the visibility
kono
parents:
diff changeset
129 -- of T and its scope. If the operator is an equality or comparison, T is
kono
parents:
diff changeset
130 -- always Boolean, and we use Opnd_Type, which is a candidate type for one
kono
parents:
diff changeset
131 -- of the operands of N, to check visibility.
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 procedure End_Interp_List;
kono
parents:
diff changeset
134 -- End the list of interpretations of current node
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 procedure Get_First_Interp
kono
parents:
diff changeset
137 (N : Node_Id;
kono
parents:
diff changeset
138 I : out Interp_Index;
kono
parents:
diff changeset
139 It : out Interp);
kono
parents:
diff changeset
140 -- Initialize iteration over set of interpretations for Node N. The first
kono
parents:
diff changeset
141 -- interpretation is placed in It, and I is initialized for subsequent
kono
parents:
diff changeset
142 -- calls to Get_Next_Interp.
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 procedure Get_Next_Interp (I : in out Interp_Index; It : out Interp);
kono
parents:
diff changeset
145 -- Iteration step over set of interpretations. Using the value in I, which
kono
parents:
diff changeset
146 -- was set by a previous call to Get_First_Interp or Get_Next_Interp, the
kono
parents:
diff changeset
147 -- next interpretation is placed in It, and I is updated for the next call.
kono
parents:
diff changeset
148 -- The end of the list of interpretations is signalled by It.Nam = Empty.
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 procedure Remove_Interp (I : in out Interp_Index);
kono
parents:
diff changeset
151 -- Remove an interpretation that is hidden by another, or that does not
kono
parents:
diff changeset
152 -- match the context. The value of I on input was set by a call to either
kono
parents:
diff changeset
153 -- Get_First_Interp or Get_Next_Interp and references the interpretation
kono
parents:
diff changeset
154 -- to be removed. The only allowed use of the exit value of I is as input
kono
parents:
diff changeset
155 -- to a subsequent call to Get_Next_Interp, which yields the interpretation
kono
parents:
diff changeset
156 -- following the removed one.
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 procedure Save_Interps (Old_N : Node_Id; New_N : Node_Id);
kono
parents:
diff changeset
159 -- If an overloaded node is rewritten during semantic analysis, its
kono
parents:
diff changeset
160 -- possible interpretations must be linked to the copy. This procedure
kono
parents:
diff changeset
161 -- transfers the overload information (Is_Overloaded flag, and list of
kono
parents:
diff changeset
162 -- interpretations) from Old_N, the old node, to New_N, its new copy.
kono
parents:
diff changeset
163 -- It has no effect in the non-overloaded case.
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 function Covers (T1, T2 : Entity_Id) return Boolean;
kono
parents:
diff changeset
166 -- This is the basic type compatibility routine. T1 is the expected type,
kono
parents:
diff changeset
167 -- imposed by context, and T2 is the actual type. The processing reflects
kono
parents:
diff changeset
168 -- both the definition of type coverage and the rules for operand matching;
kono
parents:
diff changeset
169 -- that is, this does not exactly match the RM definition of "covers".
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 function Disambiguate
kono
parents:
diff changeset
172 (N : Node_Id;
kono
parents:
diff changeset
173 I1, I2 : Interp_Index;
kono
parents:
diff changeset
174 Typ : Entity_Id) return Interp;
kono
parents:
diff changeset
175 -- If more than one interpretation of a name in a call is legal, apply
kono
parents:
diff changeset
176 -- preference rules (universal types first) and operator visibility in
kono
parents:
diff changeset
177 -- order to remove ambiguity. I1 and I2 are the first two interpretations
kono
parents:
diff changeset
178 -- that are compatible with the context, but there may be others.
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 function Entity_Matches_Spec (Old_S, New_S : Entity_Id) return Boolean;
kono
parents:
diff changeset
181 -- To resolve subprogram renaming and default formal subprograms in generic
kono
parents:
diff changeset
182 -- definitions. Old_S is a possible interpretation of the entity being
kono
parents:
diff changeset
183 -- renamed, New_S has an explicit signature. If Old_S is a subprogram, as
kono
parents:
diff changeset
184 -- opposed to an operator, type and mode conformance are required.
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 function Find_Unique_Type (L : Node_Id; R : Node_Id) return Entity_Id;
kono
parents:
diff changeset
187 -- Used in second pass of resolution, for equality and comparison nodes. L
kono
parents:
diff changeset
188 -- is the left operand, whose type is known to be correct, and R is the
kono
parents:
diff changeset
189 -- right operand, which has one interpretation compatible with that of L.
kono
parents:
diff changeset
190 -- Return the type intersection of the two.
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 function Has_Compatible_Type (N : Node_Id; Typ : Entity_Id) return Boolean;
kono
parents:
diff changeset
193 -- Verify that some interpretation of the node N has a type compatible with
kono
parents:
diff changeset
194 -- Typ. If N is not overloaded, then its unique type must be compatible
kono
parents:
diff changeset
195 -- with Typ. Otherwise iterate through the interpretations of N looking for
kono
parents:
diff changeset
196 -- a compatible one.
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 function Hides_Op (F : Entity_Id; Op : Entity_Id) return Boolean;
kono
parents:
diff changeset
199 -- A user-defined function hides a predefined operator if it is matches the
kono
parents:
diff changeset
200 -- signature of the operator, and is declared in an open scope, or in the
kono
parents:
diff changeset
201 -- scope of the result type.
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 function Interface_Present_In_Ancestor
kono
parents:
diff changeset
204 (Typ : Entity_Id;
kono
parents:
diff changeset
205 Iface : Entity_Id) return Boolean;
kono
parents:
diff changeset
206 -- Ada 2005 (AI-251): Typ must be a tagged record type/subtype and Iface
kono
parents:
diff changeset
207 -- must be an abstract interface type (or a class-wide abstract interface).
kono
parents:
diff changeset
208 -- This function is used to check if Typ or some ancestor of Typ implements
kono
parents:
diff changeset
209 -- Iface (returning True only if so).
kono
parents:
diff changeset
210
kono
parents:
diff changeset
211 function Intersect_Types (L, R : Node_Id) return Entity_Id;
kono
parents:
diff changeset
212 -- Find the common interpretation to two analyzed nodes. If one of the
kono
parents:
diff changeset
213 -- interpretations is universal, choose the non-universal one. If either
kono
parents:
diff changeset
214 -- node is overloaded, find single common interpretation.
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 function In_Generic_Actual (Exp : Node_Id) return Boolean;
kono
parents:
diff changeset
217 -- Determine whether the expression is part of a generic actual. At the
kono
parents:
diff changeset
218 -- time the actual is resolved the scope is already that of the instance,
kono
parents:
diff changeset
219 -- but conceptually the resolution of the actual takes place in the
kono
parents:
diff changeset
220 -- enclosing context and no special disambiguation rules should be applied.
kono
parents:
diff changeset
221
kono
parents:
diff changeset
222 function Is_Ancestor
kono
parents:
diff changeset
223 (T1 : Entity_Id;
kono
parents:
diff changeset
224 T2 : Entity_Id;
kono
parents:
diff changeset
225 Use_Full_View : Boolean := False) return Boolean;
kono
parents:
diff changeset
226 -- T1 is a tagged type (not class-wide). Verify that it is one of the
kono
parents:
diff changeset
227 -- ancestors of type T2 (which may or not be class-wide). If Use_Full_View
kono
parents:
diff changeset
228 -- is True then the full-view of private parents is used when climbing
kono
parents:
diff changeset
229 -- through the parents of T2.
kono
parents:
diff changeset
230 --
kono
parents:
diff changeset
231 -- Note: For analysis purposes the flag Use_Full_View must be set to False
kono
parents:
diff changeset
232 -- (otherwise we break the privacy contract since this routine returns true
kono
parents:
diff changeset
233 -- for hidden ancestors of private types). For expansion purposes this flag
kono
parents:
diff changeset
234 -- is generally set to True since the expander must know with precision the
kono
parents:
diff changeset
235 -- ancestors of a tagged type. For example, if a private type derives from
kono
parents:
diff changeset
236 -- an interface type then the interface may not be an ancestor of its full
kono
parents:
diff changeset
237 -- view since the full-view is only required to cover the interface (RM 7.3
kono
parents:
diff changeset
238 -- (7.3/2))) and this knowledge affects construction of dispatch tables.
kono
parents:
diff changeset
239
kono
parents:
diff changeset
240 function Is_Progenitor
kono
parents:
diff changeset
241 (Iface : Entity_Id;
kono
parents:
diff changeset
242 Typ : Entity_Id) return Boolean;
kono
parents:
diff changeset
243 -- Determine whether the interface Iface is implemented by Typ. It requires
kono
parents:
diff changeset
244 -- traversing the list of abstract interfaces of the type, as well as that
kono
parents:
diff changeset
245 -- of the ancestor types. The predicate is used to determine when a formal
kono
parents:
diff changeset
246 -- in the signature of an inherited operation must carry the derived type.
kono
parents:
diff changeset
247
kono
parents:
diff changeset
248 function Is_Subtype_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean;
kono
parents:
diff changeset
249 -- Checks whether T1 is any subtype of T2 directly or indirectly. Applies
kono
parents:
diff changeset
250 -- only to scalar subtypes???
kono
parents:
diff changeset
251
kono
parents:
diff changeset
252 function Operator_Matches_Spec (Op, New_S : Entity_Id) return Boolean;
kono
parents:
diff changeset
253 -- Used to resolve subprograms renaming operators, and calls to user
kono
parents:
diff changeset
254 -- defined operators. Determines whether a given operator Op, matches
kono
parents:
diff changeset
255 -- a specification, New_S.
kono
parents:
diff changeset
256
kono
parents:
diff changeset
257 procedure Set_Abstract_Op (I : Interp_Index; V : Entity_Id);
kono
parents:
diff changeset
258 -- Set the abstract operation field of an interpretation
kono
parents:
diff changeset
259
kono
parents:
diff changeset
260 function Valid_Comparison_Arg (T : Entity_Id) return Boolean;
kono
parents:
diff changeset
261 -- A valid argument to an ordering operator must be a discrete type, a
kono
parents:
diff changeset
262 -- real type, or a one dimensional array with a discrete component type.
kono
parents:
diff changeset
263
kono
parents:
diff changeset
264 function Valid_Boolean_Arg (T : Entity_Id) return Boolean;
kono
parents:
diff changeset
265 -- A valid argument of a boolean operator is either some boolean type, or a
kono
parents:
diff changeset
266 -- one-dimensional array of boolean type.
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 procedure Write_Interp (It : Interp);
kono
parents:
diff changeset
269 -- Debugging procedure to display an Interp
kono
parents:
diff changeset
270
kono
parents:
diff changeset
271 procedure Write_Interp_Ref (Map_Ptr : Int);
kono
parents:
diff changeset
272 -- Debugging procedure to display entry in Interp_Map. Would not be needed
kono
parents:
diff changeset
273 -- if it were possible to debug instantiations of Table.
kono
parents:
diff changeset
274
kono
parents:
diff changeset
275 procedure Write_Overloads (N : Node_Id);
kono
parents:
diff changeset
276 -- Debugging procedure to output info on possibly overloaded entities for
kono
parents:
diff changeset
277 -- specified node.
kono
parents:
diff changeset
278
kono
parents:
diff changeset
279 end Sem_Type;