]>
Commit | Line | Data |
---|---|---|
996ae0b0 RK |
1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- S E M _ C H 4 -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
0310af44 | 9 | -- Copyright (C) 1992-2016, Free Software Foundation, Inc. -- |
996ae0b0 RK |
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- -- | |
157a9bf5 | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
996ae0b0 RK |
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 -- | |
157a9bf5 ES |
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. -- | |
996ae0b0 RK |
20 | -- -- |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
71ff80dc | 22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
996ae0b0 RK |
23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
d50f4827 | 26 | with Aspects; use Aspects; |
996ae0b0 RK |
27 | with Atree; use Atree; |
28 | with Debug; use Debug; | |
29 | with Einfo; use Einfo; | |
35ae2ed8 | 30 | with Elists; use Elists; |
996ae0b0 RK |
31 | with Errout; use Errout; |
32 | with Exp_Util; use Exp_Util; | |
d935a36e | 33 | with Fname; use Fname; |
996ae0b0 | 34 | with Itypes; use Itypes; |
d935a36e | 35 | with Lib; use Lib; |
996ae0b0 RK |
36 | with Lib.Xref; use Lib.Xref; |
37 | with Namet; use Namet; | |
d469eabe | 38 | with Namet.Sp; use Namet.Sp; |
996ae0b0 RK |
39 | with Nlists; use Nlists; |
40 | with Nmake; use Nmake; | |
41 | with Opt; use Opt; | |
42 | with Output; use Output; | |
43 | with Restrict; use Restrict; | |
6e937c1c | 44 | with Rident; use Rident; |
996ae0b0 | 45 | with Sem; use Sem; |
a4100e55 | 46 | with Sem_Aux; use Sem_Aux; |
19d846a0 | 47 | with Sem_Case; use Sem_Case; |
996ae0b0 RK |
48 | with Sem_Cat; use Sem_Cat; |
49 | with Sem_Ch3; use Sem_Ch3; | |
d469eabe | 50 | with Sem_Ch6; use Sem_Ch6; |
996ae0b0 | 51 | with Sem_Ch8; use Sem_Ch8; |
dec6faf1 | 52 | with Sem_Dim; use Sem_Dim; |
b67a385c | 53 | with Sem_Disp; use Sem_Disp; |
996ae0b0 RK |
54 | with Sem_Dist; use Sem_Dist; |
55 | with Sem_Eval; use Sem_Eval; | |
56 | with Sem_Res; use Sem_Res; | |
996ae0b0 | 57 | with Sem_Type; use Sem_Type; |
19d846a0 RD |
58 | with Sem_Util; use Sem_Util; |
59 | with Sem_Warn; use Sem_Warn; | |
996ae0b0 RK |
60 | with Stand; use Stand; |
61 | with Sinfo; use Sinfo; | |
62 | with Snames; use Snames; | |
63 | with Tbuild; use Tbuild; | |
b727a82b | 64 | with Uintp; use Uintp; |
996ae0b0 | 65 | |
996ae0b0 RK |
66 | package body Sem_Ch4 is |
67 | ||
22e89283 AC |
68 | -- Tables which speed up the identification of dangerous calls to Ada 2012 |
69 | -- functions with writable actuals (AI05-0144). | |
70 | ||
71 | -- The following table enumerates the Ada constructs which may evaluate in | |
72 | -- arbitrary order. It does not cover all the language constructs which can | |
73 | -- be evaluated in arbitrary order but the subset needed for AI05-0144. | |
74 | ||
75 | Has_Arbitrary_Evaluation_Order : constant array (Node_Kind) of Boolean := | |
76 | (N_Aggregate => True, | |
77 | N_Assignment_Statement => True, | |
78 | N_Entry_Call_Statement => True, | |
79 | N_Extension_Aggregate => True, | |
80 | N_Full_Type_Declaration => True, | |
81 | N_Indexed_Component => True, | |
82 | N_Object_Declaration => True, | |
83 | N_Pragma => True, | |
84 | N_Range => True, | |
85 | N_Slice => True, | |
213999c2 AC |
86 | N_Array_Type_Definition => True, |
87 | N_Membership_Test => True, | |
88 | N_Binary_Op => True, | |
89 | N_Subprogram_Call => True, | |
22e89283 AC |
90 | others => False); |
91 | ||
92 | -- The following table enumerates the nodes on which we stop climbing when | |
93 | -- locating the outermost Ada construct that can be evaluated in arbitrary | |
94 | -- order. | |
95 | ||
96 | Stop_Subtree_Climbing : constant array (Node_Kind) of Boolean := | |
97 | (N_Aggregate => True, | |
98 | N_Assignment_Statement => True, | |
99 | N_Entry_Call_Statement => True, | |
100 | N_Extended_Return_Statement => True, | |
101 | N_Extension_Aggregate => True, | |
102 | N_Full_Type_Declaration => True, | |
103 | N_Object_Declaration => True, | |
104 | N_Object_Renaming_Declaration => True, | |
105 | N_Package_Specification => True, | |
106 | N_Pragma => True, | |
107 | N_Procedure_Call_Statement => True, | |
108 | N_Simple_Return_Statement => True, | |
213999c2 | 109 | N_Has_Condition => True, |
22e89283 AC |
110 | others => False); |
111 | ||
996ae0b0 RK |
112 | ----------------------- |
113 | -- Local Subprograms -- | |
114 | ----------------------- | |
115 | ||
fe39cf20 BD |
116 | procedure Analyze_Concatenation_Rest (N : Node_Id); |
117 | -- Does the "rest" of the work of Analyze_Concatenation, after the left | |
118 | -- operand has been analyzed. See Analyze_Concatenation for details. | |
119 | ||
996ae0b0 | 120 | procedure Analyze_Expression (N : Node_Id); |
80211802 AC |
121 | -- For expressions that are not names, this is just a call to analyze. If |
122 | -- the expression is a name, it may be a call to a parameterless function, | |
123 | -- and if so must be converted into an explicit call node and analyzed as | |
124 | -- such. This deproceduring must be done during the first pass of overload | |
125 | -- resolution, because otherwise a procedure call with overloaded actuals | |
126 | -- may fail to resolve. | |
996ae0b0 RK |
127 | |
128 | procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id); | |
80211802 AC |
129 | -- Analyze a call of the form "+"(x, y), etc. The prefix of the call is an |
130 | -- operator name or an expanded name whose selector is an operator name, | |
131 | -- and one possible interpretation is as a predefined operator. | |
996ae0b0 RK |
132 | |
133 | procedure Analyze_Overloaded_Selected_Component (N : Node_Id); | |
134 | -- If the prefix of a selected_component is overloaded, the proper | |
135 | -- interpretation that yields a record type with the proper selector | |
136 | -- name must be selected. | |
137 | ||
138 | procedure Analyze_User_Defined_Binary_Op (N : Node_Id; Op_Id : Entity_Id); | |
139 | -- Procedure to analyze a user defined binary operator, which is resolved | |
140 | -- like a function, but instead of a list of actuals it is presented | |
141 | -- with the left and right operands of an operator node. | |
142 | ||
143 | procedure Analyze_User_Defined_Unary_Op (N : Node_Id; Op_Id : Entity_Id); | |
144 | -- Procedure to analyze a user defined unary operator, which is resolved | |
145 | -- like a function, but instead of a list of actuals, it is presented with | |
146 | -- the operand of the operator node. | |
147 | ||
148 | procedure Ambiguous_Operands (N : Node_Id); | |
0877856b | 149 | -- For equality, membership, and comparison operators with overloaded |
996ae0b0 RK |
150 | -- arguments, list possible interpretations. |
151 | ||
996ae0b0 | 152 | procedure Analyze_One_Call |
ec6078e3 ES |
153 | (N : Node_Id; |
154 | Nam : Entity_Id; | |
155 | Report : Boolean; | |
156 | Success : out Boolean; | |
157 | Skip_First : Boolean := False); | |
996ae0b0 RK |
158 | -- Check one interpretation of an overloaded subprogram name for |
159 | -- compatibility with the types of the actuals in a call. If there is a | |
160 | -- single interpretation which does not match, post error if Report is | |
161 | -- set to True. | |
162 | -- | |
163 | -- Nam is the entity that provides the formals against which the actuals | |
164 | -- are checked. Nam is either the name of a subprogram, or the internal | |
165 | -- subprogram type constructed for an access_to_subprogram. If the actuals | |
166 | -- are compatible with Nam, then Nam is added to the list of candidate | |
167 | -- interpretations for N, and Success is set to True. | |
ec6078e3 ES |
168 | -- |
169 | -- The flag Skip_First is used when analyzing a call that was rewritten | |
170 | -- from object notation. In this case the first actual may have to receive | |
171 | -- an explicit dereference, depending on the first formal of the operation | |
172 | -- being called. The caller will have verified that the object is legal | |
173 | -- for the call. If the remaining parameters match, the first parameter | |
174 | -- will rewritten as a dereference if needed, prior to completing analysis. | |
996ae0b0 RK |
175 | |
176 | procedure Check_Misspelled_Selector | |
177 | (Prefix : Entity_Id; | |
178 | Sel : Node_Id); | |
80211802 | 179 | -- Give possible misspelling message if Sel seems likely to be a mis- |
8dbf3473 AC |
180 | -- spelling of one of the selectors of the Prefix. This is called by |
181 | -- Analyze_Selected_Component after producing an invalid selector error | |
182 | -- message. | |
996ae0b0 RK |
183 | |
184 | function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean; | |
f3d57416 | 185 | -- Verify that type T is declared in scope S. Used to find interpretations |
996ae0b0 RK |
186 | -- for operators given by expanded names. This is abstracted as a separate |
187 | -- function to handle extensions to System, where S is System, but T is | |
188 | -- declared in the extension. | |
189 | ||
190 | procedure Find_Arithmetic_Types | |
191 | (L, R : Node_Id; | |
192 | Op_Id : Entity_Id; | |
193 | N : Node_Id); | |
80211802 AC |
194 | -- L and R are the operands of an arithmetic operator. Find consistent |
195 | -- pairs of interpretations for L and R that have a numeric type consistent | |
196 | -- with the semantics of the operator. | |
996ae0b0 RK |
197 | |
198 | procedure Find_Comparison_Types | |
199 | (L, R : Node_Id; | |
200 | Op_Id : Entity_Id; | |
201 | N : Node_Id); | |
80211802 AC |
202 | -- L and R are operands of a comparison operator. Find consistent pairs of |
203 | -- interpretations for L and R. | |
996ae0b0 RK |
204 | |
205 | procedure Find_Concatenation_Types | |
206 | (L, R : Node_Id; | |
207 | Op_Id : Entity_Id; | |
208 | N : Node_Id); | |
6e73e3ab | 209 | -- For the four varieties of concatenation |
996ae0b0 RK |
210 | |
211 | procedure Find_Equality_Types | |
212 | (L, R : Node_Id; | |
213 | Op_Id : Entity_Id; | |
214 | N : Node_Id); | |
6e73e3ab | 215 | -- Ditto for equality operators |
996ae0b0 RK |
216 | |
217 | procedure Find_Boolean_Types | |
218 | (L, R : Node_Id; | |
219 | Op_Id : Entity_Id; | |
220 | N : Node_Id); | |
6e73e3ab | 221 | -- Ditto for binary logical operations |
996ae0b0 RK |
222 | |
223 | procedure Find_Negation_Types | |
224 | (R : Node_Id; | |
225 | Op_Id : Entity_Id; | |
226 | N : Node_Id); | |
6e73e3ab | 227 | -- Find consistent interpretation for operand of negation operator |
996ae0b0 RK |
228 | |
229 | procedure Find_Non_Universal_Interpretations | |
230 | (N : Node_Id; | |
231 | R : Node_Id; | |
232 | Op_Id : Entity_Id; | |
233 | T1 : Entity_Id); | |
234 | -- For equality and comparison operators, the result is always boolean, | |
235 | -- and the legality of the operation is determined from the visibility | |
236 | -- of the operand types. If one of the operands has a universal interpre- | |
237 | -- tation, the legality check uses some compatible non-universal | |
238 | -- interpretation of the other operand. N can be an operator node, or | |
be4c5193 AC |
239 | -- a function call whose name is an operator designator. Any_Access, which |
240 | -- is the initial type of the literal NULL, is a universal type for the | |
241 | -- purpose of this routine. | |
996ae0b0 | 242 | |
d469eabe HK |
243 | function Find_Primitive_Operation (N : Node_Id) return Boolean; |
244 | -- Find candidate interpretations for the name Obj.Proc when it appears | |
245 | -- in a subprogram renaming declaration. | |
246 | ||
996ae0b0 RK |
247 | procedure Find_Unary_Types |
248 | (R : Node_Id; | |
249 | Op_Id : Entity_Id; | |
250 | N : Node_Id); | |
6e73e3ab | 251 | -- Unary arithmetic types: plus, minus, abs |
996ae0b0 RK |
252 | |
253 | procedure Check_Arithmetic_Pair | |
254 | (T1, T2 : Entity_Id; | |
255 | Op_Id : Entity_Id; | |
256 | N : Node_Id); | |
84dad556 AC |
257 | -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid types |
258 | -- for left and right operand. Determine whether they constitute a valid | |
259 | -- pair for the given operator, and record the corresponding interpretation | |
260 | -- of the operator node. The node N may be an operator node (the usual | |
261 | -- case) or a function call whose prefix is an operator designator. In | |
262 | -- both cases Op_Id is the operator name itself. | |
996ae0b0 RK |
263 | |
264 | procedure Diagnose_Call (N : Node_Id; Nam : Node_Id); | |
265 | -- Give detailed information on overloaded call where none of the | |
266 | -- interpretations match. N is the call node, Nam the designator for | |
267 | -- the overloaded entity being called. | |
268 | ||
269 | function Junk_Operand (N : Node_Id) return Boolean; | |
270 | -- Test for an operand that is an inappropriate entity (e.g. a package | |
271 | -- name or a label). If so, issue an error message and return True. If | |
272 | -- the operand is not an inappropriate entity kind, return False. | |
273 | ||
274 | procedure Operator_Check (N : Node_Id); | |
da709d08 AC |
275 | -- Verify that an operator has received some valid interpretation. If none |
276 | -- was found, determine whether a use clause would make the operation | |
277 | -- legal. The variable Candidate_Type (defined in Sem_Type) is set for | |
278 | -- every type compatible with the operator, even if the operator for the | |
279 | -- type is not directly visible. The routine uses this type to emit a more | |
280 | -- informative message. | |
996ae0b0 | 281 | |
d469eabe | 282 | function Process_Implicit_Dereference_Prefix |
da709d08 | 283 | (E : Entity_Id; |
d469eabe | 284 | P : Node_Id) return Entity_Id; |
da709d08 | 285 | -- Called when P is the prefix of an implicit dereference, denoting an |
d469eabe HK |
286 | -- object E. The function returns the designated type of the prefix, taking |
287 | -- into account that the designated type of an anonymous access type may be | |
288 | -- a limited view, when the non-limited view is visible. | |
84dad556 | 289 | -- |
d469eabe HK |
290 | -- If in semantics only mode (-gnatc or generic), the function also records |
291 | -- that the prefix is a reference to E, if any. Normally, such a reference | |
292 | -- is generated only when the implicit dereference is expanded into an | |
293 | -- explicit one, but for consistency we must generate the reference when | |
294 | -- expansion is disabled as well. | |
6e73e3ab | 295 | |
30c20106 AC |
296 | procedure Remove_Abstract_Operations (N : Node_Id); |
297 | -- Ada 2005: implementation of AI-310. An abstract non-dispatching | |
298 | -- operation is not a candidate interpretation. | |
299 | ||
d50f4827 AC |
300 | function Try_Container_Indexing |
301 | (N : Node_Id; | |
302 | Prefix : Node_Id; | |
50878404 | 303 | Exprs : List_Id) return Boolean; |
d50f4827 AC |
304 | -- AI05-0139: Generalized indexing to support iterators over containers |
305 | ||
996ae0b0 | 306 | function Try_Indexed_Call |
aab883ec ES |
307 | (N : Node_Id; |
308 | Nam : Entity_Id; | |
309 | Typ : Entity_Id; | |
310 | Skip_First : Boolean) return Boolean; | |
311 | -- If a function has defaults for all its actuals, a call to it may in fact | |
312 | -- be an indexing on the result of the call. Try_Indexed_Call attempts the | |
313 | -- interpretation as an indexing, prior to analysis as a call. If both are | |
314 | -- possible, the node is overloaded with both interpretations (same symbol | |
315 | -- but two different types). If the call is written in prefix form, the | |
316 | -- prefix becomes the first parameter in the call, and only the remaining | |
317 | -- actuals must be checked for the presence of defaults. | |
996ae0b0 RK |
318 | |
319 | function Try_Indirect_Call | |
91b1417d AC |
320 | (N : Node_Id; |
321 | Nam : Entity_Id; | |
322 | Typ : Entity_Id) return Boolean; | |
aab883ec ES |
323 | -- Similarly, a function F that needs no actuals can return an access to a |
324 | -- subprogram, and the call F (X) interpreted as F.all (X). In this case | |
325 | -- the call may be overloaded with both interpretations. | |
996ae0b0 | 326 | |
8cf23b91 | 327 | function Try_Object_Operation |
11bc76df AC |
328 | (N : Node_Id; |
329 | CW_Test_Only : Boolean := False) return Boolean; | |
0d57c6f4 RD |
330 | -- Ada 2005 (AI-252): Support the object.operation notation. If node N |
331 | -- is a call in this notation, it is transformed into a normal subprogram | |
332 | -- call where the prefix is a parameter, and True is returned. If node | |
84dad556 | 333 | -- N is not of this form, it is unchanged, and False is returned. If |
8cf23b91 AC |
334 | -- CW_Test_Only is true then N is an N_Selected_Component node which |
335 | -- is part of a call to an entry or procedure of a tagged concurrent | |
336 | -- type and this routine is invoked to search for class-wide subprograms | |
337 | -- conflicting with the target entity. | |
35ae2ed8 | 338 | |
b4592168 GD |
339 | procedure wpo (T : Entity_Id); |
340 | pragma Warnings (Off, wpo); | |
341 | -- Used for debugging: obtain list of primitive operations even if | |
342 | -- type is not frozen and dispatch table is not built yet. | |
343 | ||
996ae0b0 RK |
344 | ------------------------ |
345 | -- Ambiguous_Operands -- | |
346 | ------------------------ | |
347 | ||
348 | procedure Ambiguous_Operands (N : Node_Id) is | |
fbf5a39b | 349 | procedure List_Operand_Interps (Opnd : Node_Id); |
996ae0b0 | 350 | |
4c46b835 AC |
351 | -------------------------- |
352 | -- List_Operand_Interps -- | |
353 | -------------------------- | |
354 | ||
fbf5a39b | 355 | procedure List_Operand_Interps (Opnd : Node_Id) is |
996ae0b0 RK |
356 | Nam : Node_Id; |
357 | Err : Node_Id := N; | |
358 | ||
359 | begin | |
360 | if Is_Overloaded (Opnd) then | |
361 | if Nkind (Opnd) in N_Op then | |
362 | Nam := Opnd; | |
84dad556 | 363 | |
996ae0b0 RK |
364 | elsif Nkind (Opnd) = N_Function_Call then |
365 | Nam := Name (Opnd); | |
84dad556 | 366 | |
44a10091 AC |
367 | elsif Ada_Version >= Ada_2012 then |
368 | declare | |
369 | It : Interp; | |
370 | I : Interp_Index; | |
371 | ||
372 | begin | |
373 | Get_First_Interp (Opnd, I, It); | |
374 | while Present (It.Nam) loop | |
375 | if Has_Implicit_Dereference (It.Typ) then | |
376 | Error_Msg_N | |
377 | ("can be interpreted as implicit dereference", Opnd); | |
378 | return; | |
379 | end if; | |
380 | ||
381 | Get_Next_Interp (I, It); | |
382 | end loop; | |
383 | end; | |
384 | ||
996ae0b0 RK |
385 | return; |
386 | end if; | |
387 | ||
388 | else | |
389 | return; | |
390 | end if; | |
391 | ||
392 | if Opnd = Left_Opnd (N) then | |
84dad556 AC |
393 | Error_Msg_N |
394 | ("\left operand has the following interpretations", N); | |
996ae0b0 | 395 | else |
ed2233dc | 396 | Error_Msg_N |
996ae0b0 RK |
397 | ("\right operand has the following interpretations", N); |
398 | Err := Opnd; | |
399 | end if; | |
400 | ||
fbf5a39b AC |
401 | List_Interps (Nam, Err); |
402 | end List_Operand_Interps; | |
996ae0b0 | 403 | |
4c46b835 AC |
404 | -- Start of processing for Ambiguous_Operands |
405 | ||
996ae0b0 | 406 | begin |
b67a385c | 407 | if Nkind (N) in N_Membership_Test then |
ed2233dc | 408 | Error_Msg_N ("ambiguous operands for membership", N); |
996ae0b0 | 409 | |
d469eabe | 410 | elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then |
ed2233dc | 411 | Error_Msg_N ("ambiguous operands for equality", N); |
996ae0b0 RK |
412 | |
413 | else | |
ed2233dc | 414 | Error_Msg_N ("ambiguous operands for comparison", N); |
996ae0b0 RK |
415 | end if; |
416 | ||
417 | if All_Errors_Mode then | |
fbf5a39b AC |
418 | List_Operand_Interps (Left_Opnd (N)); |
419 | List_Operand_Interps (Right_Opnd (N)); | |
996ae0b0 | 420 | else |
555360a5 | 421 | Error_Msg_N ("\use -gnatf switch for details", N); |
996ae0b0 RK |
422 | end if; |
423 | end Ambiguous_Operands; | |
424 | ||
425 | ----------------------- | |
426 | -- Analyze_Aggregate -- | |
427 | ----------------------- | |
428 | ||
429 | -- Most of the analysis of Aggregates requires that the type be known, | |
430 | -- and is therefore put off until resolution. | |
431 | ||
432 | procedure Analyze_Aggregate (N : Node_Id) is | |
433 | begin | |
434 | if No (Etype (N)) then | |
435 | Set_Etype (N, Any_Composite); | |
436 | end if; | |
437 | end Analyze_Aggregate; | |
438 | ||
439 | ----------------------- | |
440 | -- Analyze_Allocator -- | |
441 | ----------------------- | |
442 | ||
443 | procedure Analyze_Allocator (N : Node_Id) is | |
444 | Loc : constant Source_Ptr := Sloc (N); | |
07fc65c4 | 445 | Sav_Errs : constant Nat := Serious_Errors_Detected; |
b67a385c | 446 | E : Node_Id := Expression (N); |
996ae0b0 RK |
447 | Acc_Type : Entity_Id; |
448 | Type_Id : Entity_Id; | |
87003b28 RD |
449 | P : Node_Id; |
450 | C : Node_Id; | |
b3b26ace | 451 | Onode : Node_Id; |
996ae0b0 RK |
452 | |
453 | begin | |
ce5ba43a | 454 | Check_SPARK_05_Restriction ("allocator is not allowed", N); |
1d801f21 | 455 | |
87003b28 RD |
456 | -- Deal with allocator restrictions |
457 | ||
50cff367 | 458 | -- In accordance with H.4(7), the No_Allocators restriction only applies |
87003b28 | 459 | -- to user-written allocators. The same consideration applies to the |
d8941160 | 460 | -- No_Standard_Allocators_Before_Elaboration restriction. |
50cff367 GD |
461 | |
462 | if Comes_From_Source (N) then | |
463 | Check_Restriction (No_Allocators, N); | |
87003b28 | 464 | |
57f4c288 | 465 | -- Processing for No_Standard_Allocators_After_Elaboration, loop to |
489c6e19 | 466 | -- look at enclosing context, checking task/main subprogram case. |
87003b28 RD |
467 | |
468 | C := N; | |
469 | P := Parent (C); | |
470 | while Present (P) loop | |
471 | ||
b3b26ace AC |
472 | -- For the task case we need a handled sequence of statements, |
473 | -- where the occurrence of the allocator is within the statements | |
474 | -- and the parent is a task body | |
87003b28 RD |
475 | |
476 | if Nkind (P) = N_Handled_Sequence_Of_Statements | |
477 | and then Is_List_Member (C) | |
478 | and then List_Containing (C) = Statements (P) | |
479 | then | |
b3b26ace AC |
480 | Onode := Original_Node (Parent (P)); |
481 | ||
87003b28 | 482 | -- Check for allocator within task body, this is a definite |
d8941160 RD |
483 | -- violation of No_Allocators_After_Elaboration we can detect |
484 | -- at compile time. | |
87003b28 | 485 | |
b3b26ace | 486 | if Nkind (Onode) = N_Task_Body then |
57f4c288 ES |
487 | Check_Restriction |
488 | (No_Standard_Allocators_After_Elaboration, N); | |
87003b28 RD |
489 | exit; |
490 | end if; | |
b3b26ace | 491 | end if; |
87003b28 | 492 | |
b3b26ace AC |
493 | -- The other case is appearance in a subprogram body. This is |
494 | -- a violation if this is a library level subprogram with no | |
495 | -- parameters. Note that this is now a static error even if the | |
496 | -- subprogram is not the main program (this is a change, in an | |
497 | -- earlier version only the main program was affected, and the | |
498 | -- check had to be done in the binder. | |
87003b28 | 499 | |
b3b26ace AC |
500 | if Nkind (P) = N_Subprogram_Body |
501 | and then Nkind (Parent (P)) = N_Compilation_Unit | |
502 | and then No (Parameter_Specifications (Specification (P))) | |
503 | then | |
504 | Check_Restriction | |
505 | (No_Standard_Allocators_After_Elaboration, N); | |
87003b28 RD |
506 | end if; |
507 | ||
508 | C := P; | |
509 | P := Parent (C); | |
510 | end loop; | |
50cff367 | 511 | end if; |
996ae0b0 | 512 | |
df170605 AC |
513 | -- Ada 2012 (AI05-0111-3): Analyze the subpool_specification, if |
514 | -- any. The expected type for the name is any type. A non-overloading | |
515 | -- rule then requires it to be of a type descended from | |
f0f88eb6 RD |
516 | -- System.Storage_Pools.Subpools.Subpool_Handle. |
517 | ||
518 | -- This isn't exactly what the AI says, but it seems to be the right | |
519 | -- rule. The AI should be fixed.??? | |
df170605 AC |
520 | |
521 | declare | |
522 | Subpool : constant Node_Id := Subpool_Handle_Name (N); | |
f0f88eb6 | 523 | |
df170605 AC |
524 | begin |
525 | if Present (Subpool) then | |
526 | Analyze (Subpool); | |
f0f88eb6 | 527 | |
df170605 AC |
528 | if Is_Overloaded (Subpool) then |
529 | Error_Msg_N ("ambiguous subpool handle", Subpool); | |
530 | end if; | |
531 | ||
f0f88eb6 | 532 | -- Check that Etype (Subpool) is descended from Subpool_Handle |
df170605 AC |
533 | |
534 | Resolve (Subpool); | |
535 | end if; | |
536 | end; | |
537 | ||
538 | -- Analyze the qualified expression or subtype indication | |
87003b28 | 539 | |
996ae0b0 RK |
540 | if Nkind (E) = N_Qualified_Expression then |
541 | Acc_Type := Create_Itype (E_Allocator_Type, N); | |
542 | Set_Etype (Acc_Type, Acc_Type); | |
996ae0b0 | 543 | Find_Type (Subtype_Mark (E)); |
45c8b94b ES |
544 | |
545 | -- Analyze the qualified expression, and apply the name resolution | |
f0f88eb6 | 546 | -- rule given in 4.7(3). |
45c8b94b ES |
547 | |
548 | Analyze (E); | |
549 | Type_Id := Etype (E); | |
996ae0b0 RK |
550 | Set_Directly_Designated_Type (Acc_Type, Type_Id); |
551 | ||
996ae0b0 RK |
552 | -- A qualified expression requires an exact match of the type, |
553 | -- class-wide matching is not allowed. | |
554 | ||
45c8b94b ES |
555 | -- if Is_Class_Wide_Type (Type_Id) |
556 | -- and then Base_Type | |
557 | -- (Etype (Expression (E))) /= Base_Type (Type_Id) | |
558 | -- then | |
559 | -- Wrong_Type (Expression (E), Type_Id); | |
560 | -- end if; | |
996ae0b0 | 561 | |
996ae0b0 | 562 | -- We don't analyze the qualified expression itself because it's |
f3691f46 ES |
563 | -- part of the allocator. It is fully analyzed and resolved when |
564 | -- the allocator is resolved with the context type. | |
996ae0b0 RK |
565 | |
566 | Set_Etype (E, Type_Id); | |
567 | ||
aab883ec | 568 | -- Case where allocator has a subtype indication |
4c46b835 | 569 | |
996ae0b0 RK |
570 | else |
571 | declare | |
758c442c GD |
572 | Def_Id : Entity_Id; |
573 | Base_Typ : Entity_Id; | |
996ae0b0 RK |
574 | |
575 | begin | |
576 | -- If the allocator includes a N_Subtype_Indication then a | |
577 | -- constraint is present, otherwise the node is a subtype mark. | |
578 | -- Introduce an explicit subtype declaration into the tree | |
579 | -- defining some anonymous subtype and rewrite the allocator to | |
580 | -- use this subtype rather than the subtype indication. | |
581 | ||
582 | -- It is important to introduce the explicit subtype declaration | |
583 | -- so that the bounds of the subtype indication are attached to | |
584 | -- the tree in case the allocator is inside a generic unit. | |
585 | ||
3ba1a9eb AC |
586 | -- Finally, if there is no subtype indication and the type is |
587 | -- a tagged unconstrained type with discriminants, the designated | |
588 | -- object is constrained by their default values, and it is | |
589 | -- simplest to introduce an explicit constraint now. In some cases | |
590 | -- this is done during expansion, but freeze actions are certain | |
591 | -- to be emitted in the proper order if constraint is explicit. | |
592 | ||
593 | if Is_Entity_Name (E) and then Expander_Active then | |
594 | Find_Type (E); | |
595 | Type_Id := Entity (E); | |
596 | ||
597 | if Is_Tagged_Type (Type_Id) | |
598 | and then Has_Discriminants (Type_Id) | |
599 | and then not Is_Constrained (Type_Id) | |
3702225c AC |
600 | and then |
601 | Present | |
602 | (Discriminant_Default_Value | |
603 | (First_Discriminant (Type_Id))) | |
3ba1a9eb AC |
604 | then |
605 | declare | |
3702225c | 606 | Constr : constant List_Id := New_List; |
3ba1a9eb AC |
607 | Loc : constant Source_Ptr := Sloc (E); |
608 | Discr : Entity_Id := First_Discriminant (Type_Id); | |
3ba1a9eb AC |
609 | |
610 | begin | |
611 | if Present (Discriminant_Default_Value (Discr)) then | |
612 | while Present (Discr) loop | |
613 | Append (Discriminant_Default_Value (Discr), Constr); | |
614 | Next_Discriminant (Discr); | |
615 | end loop; | |
616 | ||
3702225c AC |
617 | Rewrite (E, |
618 | Make_Subtype_Indication (Loc, | |
619 | Subtype_Mark => New_Occurrence_Of (Type_Id, Loc), | |
620 | Constraint => | |
621 | Make_Index_Or_Discriminant_Constraint (Loc, | |
622 | Constraints => Constr))); | |
3ba1a9eb AC |
623 | end if; |
624 | end; | |
625 | end if; | |
626 | end if; | |
627 | ||
996ae0b0 RK |
628 | if Nkind (E) = N_Subtype_Indication then |
629 | ||
630 | -- A constraint is only allowed for a composite type in Ada | |
631 | -- 95. In Ada 83, a constraint is also allowed for an | |
632 | -- access-to-composite type, but the constraint is ignored. | |
633 | ||
634 | Find_Type (Subtype_Mark (E)); | |
758c442c | 635 | Base_Typ := Entity (Subtype_Mark (E)); |
996ae0b0 | 636 | |
758c442c | 637 | if Is_Elementary_Type (Base_Typ) then |
0ab80019 | 638 | if not (Ada_Version = Ada_83 |
758c442c | 639 | and then Is_Access_Type (Base_Typ)) |
996ae0b0 RK |
640 | then |
641 | Error_Msg_N ("constraint not allowed here", E); | |
642 | ||
24657705 | 643 | if Nkind (Constraint (E)) = |
3702225c | 644 | N_Index_Or_Discriminant_Constraint |
996ae0b0 | 645 | then |
4e7a4f6e | 646 | Error_Msg_N -- CODEFIX |
996ae0b0 RK |
647 | ("\if qualified expression was meant, " & |
648 | "use apostrophe", Constraint (E)); | |
649 | end if; | |
650 | end if; | |
651 | ||
652 | -- Get rid of the bogus constraint: | |
653 | ||
654 | Rewrite (E, New_Copy_Tree (Subtype_Mark (E))); | |
655 | Analyze_Allocator (N); | |
656 | return; | |
657 | end if; | |
658 | ||
659 | if Expander_Active then | |
092ef350 | 660 | Def_Id := Make_Temporary (Loc, 'S'); |
996ae0b0 RK |
661 | |
662 | Insert_Action (E, | |
663 | Make_Subtype_Declaration (Loc, | |
664 | Defining_Identifier => Def_Id, | |
665 | Subtype_Indication => Relocate_Node (E))); | |
666 | ||
07fc65c4 | 667 | if Sav_Errs /= Serious_Errors_Detected |
d469eabe HK |
668 | and then Nkind (Constraint (E)) = |
669 | N_Index_Or_Discriminant_Constraint | |
996ae0b0 | 670 | then |
4e7a4f6e | 671 | Error_Msg_N -- CODEFIX |
a90bd866 RD |
672 | ("if qualified expression was meant, " |
673 | & "use apostrophe!", Constraint (E)); | |
996ae0b0 RK |
674 | end if; |
675 | ||
676 | E := New_Occurrence_Of (Def_Id, Loc); | |
677 | Rewrite (Expression (N), E); | |
678 | end if; | |
679 | end if; | |
680 | ||
681 | Type_Id := Process_Subtype (E, N); | |
682 | Acc_Type := Create_Itype (E_Allocator_Type, N); | |
84dad556 | 683 | Set_Etype (Acc_Type, Acc_Type); |
996ae0b0 RK |
684 | Set_Directly_Designated_Type (Acc_Type, Type_Id); |
685 | Check_Fully_Declared (Type_Id, N); | |
686 | ||
1baa4d2d | 687 | -- Ada 2005 (AI-231): If the designated type is itself an access |
16b05213 | 688 | -- type that excludes null, its default initialization will |
75ad5042 ES |
689 | -- be a null object, and we can insert an unconditional raise |
690 | -- before the allocator. | |
2820d220 | 691 | |
bfae1846 | 692 | -- Ada 2012 (AI-104): A not null indication here is altogether |
518ade91 AC |
693 | -- illegal. |
694 | ||
2820d220 | 695 | if Can_Never_Be_Null (Type_Id) then |
75ad5042 ES |
696 | declare |
697 | Not_Null_Check : constant Node_Id := | |
698 | Make_Raise_Constraint_Error (Sloc (E), | |
699 | Reason => CE_Null_Not_Allowed); | |
40b93859 | 700 | |
75ad5042 | 701 | begin |
7b55fea6 | 702 | if Expander_Active then |
75ad5042 ES |
703 | Insert_Action (N, Not_Null_Check); |
704 | Analyze (Not_Null_Check); | |
40b93859 | 705 | |
685bc70f AC |
706 | elsif Warn_On_Ada_2012_Compatibility then |
707 | Error_Msg_N | |
708 | ("null value not allowed here in Ada 2012?y?", E); | |
75ad5042 ES |
709 | end if; |
710 | end; | |
2820d220 AC |
711 | end if; |
712 | ||
996ae0b0 RK |
713 | -- Check for missing initialization. Skip this check if we already |
714 | -- had errors on analyzing the allocator, since in that case these | |
24657705 | 715 | -- are probably cascaded errors. |
996ae0b0 | 716 | |
83496138 | 717 | if not Is_Definite_Subtype (Type_Id) |
07fc65c4 | 718 | and then Serious_Errors_Detected = Sav_Errs |
996ae0b0 | 719 | then |
a4956515 AC |
720 | -- The build-in-place machinery may produce an allocator when |
721 | -- the designated type is indefinite but the underlying type is | |
722 | -- not. In this case the unknown discriminants are meaningless | |
723 | -- and should not trigger error messages. Check the parent node | |
724 | -- because the allocator is marked as coming from source. | |
725 | ||
726 | if Present (Underlying_Type (Type_Id)) | |
83496138 | 727 | and then Is_Definite_Subtype (Underlying_Type (Type_Id)) |
a4956515 AC |
728 | and then not Comes_From_Source (Parent (N)) |
729 | then | |
730 | null; | |
731 | ||
732 | elsif Is_Class_Wide_Type (Type_Id) then | |
996ae0b0 RK |
733 | Error_Msg_N |
734 | ("initialization required in class-wide allocation", N); | |
a4956515 | 735 | |
996ae0b0 | 736 | else |
0791fbe9 | 737 | if Ada_Version < Ada_2005 |
24657705 HK |
738 | and then Is_Limited_Type (Type_Id) |
739 | then | |
740 | Error_Msg_N ("unconstrained allocation not allowed", N); | |
741 | ||
742 | if Is_Array_Type (Type_Id) then | |
743 | Error_Msg_N | |
744 | ("\constraint with array bounds required", N); | |
745 | ||
746 | elsif Has_Unknown_Discriminants (Type_Id) then | |
747 | null; | |
748 | ||
749 | else pragma Assert (Has_Discriminants (Type_Id)); | |
750 | Error_Msg_N | |
751 | ("\constraint with discriminant values required", N); | |
752 | end if; | |
753 | ||
754 | -- Limited Ada 2005 and general non-limited case | |
755 | ||
756 | else | |
757 | Error_Msg_N | |
84dad556 AC |
758 | ("uninitialized unconstrained allocation not " |
759 | & "allowed", N); | |
24657705 HK |
760 | |
761 | if Is_Array_Type (Type_Id) then | |
762 | Error_Msg_N | |
84dad556 AC |
763 | ("\qualified expression or constraint with " |
764 | & "array bounds required", N); | |
24657705 HK |
765 | |
766 | elsif Has_Unknown_Discriminants (Type_Id) then | |
767 | Error_Msg_N ("\qualified expression required", N); | |
768 | ||
769 | else pragma Assert (Has_Discriminants (Type_Id)); | |
770 | Error_Msg_N | |
84dad556 AC |
771 | ("\qualified expression or constraint with " |
772 | & "discriminant values required", N); | |
24657705 HK |
773 | end if; |
774 | end if; | |
996ae0b0 RK |
775 | end if; |
776 | end if; | |
777 | end; | |
778 | end if; | |
779 | ||
aab883ec | 780 | if Is_Abstract_Type (Type_Id) then |
996ae0b0 RK |
781 | Error_Msg_N ("cannot allocate abstract object", E); |
782 | end if; | |
783 | ||
784 | if Has_Task (Designated_Type (Acc_Type)) then | |
6e937c1c | 785 | Check_Restriction (No_Tasking, N); |
fbf5a39b | 786 | Check_Restriction (Max_Tasks, N); |
996ae0b0 | 787 | Check_Restriction (No_Task_Allocators, N); |
70b3b953 GD |
788 | end if; |
789 | ||
02bb0765 AC |
790 | -- Check restriction against dynamically allocated protected objects |
791 | ||
792 | if Has_Protected (Designated_Type (Acc_Type)) then | |
793 | Check_Restriction (No_Protected_Type_Allocators, N); | |
794 | end if; | |
795 | ||
646e2823 AC |
796 | -- AI05-0013-1: No_Nested_Finalization forbids allocators if the access |
797 | -- type is nested, and the designated type needs finalization. The rule | |
798 | -- is conservative in that class-wide types need finalization. | |
799 | ||
800 | if Needs_Finalization (Designated_Type (Acc_Type)) | |
801 | and then not Is_Library_Level_Entity (Acc_Type) | |
802 | then | |
803 | Check_Restriction (No_Nested_Finalization, N); | |
804 | end if; | |
805 | ||
70b3b953 GD |
806 | -- Check that an allocator of a nested access type doesn't create a |
807 | -- protected object when restriction No_Local_Protected_Objects applies. | |
70b3b953 | 808 | |
96e90ac1 | 809 | if Has_Protected (Designated_Type (Acc_Type)) |
70b3b953 GD |
810 | and then not Is_Library_Level_Entity (Acc_Type) |
811 | then | |
812 | Check_Restriction (No_Local_Protected_Objects, N); | |
996ae0b0 RK |
813 | end if; |
814 | ||
4969efdf AC |
815 | -- Likewise for No_Local_Timing_Events |
816 | ||
817 | if Has_Timing_Event (Designated_Type (Acc_Type)) | |
818 | and then not Is_Library_Level_Entity (Acc_Type) | |
819 | then | |
820 | Check_Restriction (No_Local_Timing_Events, N); | |
821 | end if; | |
822 | ||
ffe9aba8 AC |
823 | -- If the No_Streams restriction is set, check that the type of the |
824 | -- object is not, and does not contain, any subtype derived from | |
825 | -- Ada.Streams.Root_Stream_Type. Note that we guard the call to | |
826 | -- Has_Stream just for efficiency reasons. There is no point in | |
827 | -- spending time on a Has_Stream check if the restriction is not set. | |
828 | ||
7a963087 | 829 | if Restriction_Check_Required (No_Streams) then |
ffe9aba8 AC |
830 | if Has_Stream (Designated_Type (Acc_Type)) then |
831 | Check_Restriction (No_Streams, N); | |
832 | end if; | |
833 | end if; | |
834 | ||
996ae0b0 RK |
835 | Set_Etype (N, Acc_Type); |
836 | ||
837 | if not Is_Library_Level_Entity (Acc_Type) then | |
838 | Check_Restriction (No_Local_Allocators, N); | |
839 | end if; | |
2820d220 | 840 | |
07fc65c4 | 841 | if Serious_Errors_Detected > Sav_Errs then |
996ae0b0 RK |
842 | Set_Error_Posted (N); |
843 | Set_Etype (N, Any_Type); | |
844 | end if; | |
996ae0b0 RK |
845 | end Analyze_Allocator; |
846 | ||
847 | --------------------------- | |
848 | -- Analyze_Arithmetic_Op -- | |
849 | --------------------------- | |
850 | ||
851 | procedure Analyze_Arithmetic_Op (N : Node_Id) is | |
852 | L : constant Node_Id := Left_Opnd (N); | |
853 | R : constant Node_Id := Right_Opnd (N); | |
854 | Op_Id : Entity_Id; | |
855 | ||
856 | begin | |
857 | Candidate_Type := Empty; | |
858 | Analyze_Expression (L); | |
859 | Analyze_Expression (R); | |
860 | ||
d469eabe HK |
861 | -- If the entity is already set, the node is the instantiation of a |
862 | -- generic node with a non-local reference, or was manufactured by a | |
863 | -- call to Make_Op_xxx. In either case the entity is known to be valid, | |
864 | -- and we do not need to collect interpretations, instead we just get | |
865 | -- the single possible interpretation. | |
996ae0b0 RK |
866 | |
867 | Op_Id := Entity (N); | |
868 | ||
869 | if Present (Op_Id) then | |
870 | if Ekind (Op_Id) = E_Operator then | |
871 | ||
d469eabe | 872 | if Nkind_In (N, N_Op_Divide, N_Op_Mod, N_Op_Multiply, N_Op_Rem) |
996ae0b0 RK |
873 | and then Treat_Fixed_As_Integer (N) |
874 | then | |
875 | null; | |
876 | else | |
877 | Set_Etype (N, Any_Type); | |
878 | Find_Arithmetic_Types (L, R, Op_Id, N); | |
879 | end if; | |
880 | ||
881 | else | |
882 | Set_Etype (N, Any_Type); | |
883 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
884 | end if; | |
885 | ||
886 | -- Entity is not already set, so we do need to collect interpretations | |
887 | ||
888 | else | |
996ae0b0 RK |
889 | Set_Etype (N, Any_Type); |
890 | ||
84dad556 | 891 | Op_Id := Get_Name_Entity_Id (Chars (N)); |
996ae0b0 RK |
892 | while Present (Op_Id) loop |
893 | if Ekind (Op_Id) = E_Operator | |
894 | and then Present (Next_Entity (First_Entity (Op_Id))) | |
895 | then | |
896 | Find_Arithmetic_Types (L, R, Op_Id, N); | |
897 | ||
898 | -- The following may seem superfluous, because an operator cannot | |
899 | -- be generic, but this ignores the cleverness of the author of | |
900 | -- ACVC bc1013a. | |
901 | ||
902 | elsif Is_Overloadable (Op_Id) then | |
903 | Analyze_User_Defined_Binary_Op (N, Op_Id); | |
904 | end if; | |
905 | ||
906 | Op_Id := Homonym (Op_Id); | |
907 | end loop; | |
908 | end if; | |
909 | ||
910 | Operator_Check (N); | |
22e89283 | 911 | Check_Function_Writable_Actuals (N); |
996ae0b0 RK |
912 | end Analyze_Arithmetic_Op; |
913 | ||
914 | ------------------ | |
915 | -- Analyze_Call -- | |
916 | ------------------ | |
917 | ||
4c46b835 AC |
918 | -- Function, procedure, and entry calls are checked here. The Name in |
919 | -- the call may be overloaded. The actuals have been analyzed and may | |
920 | -- themselves be overloaded. On exit from this procedure, the node N | |
921 | -- may have zero, one or more interpretations. In the first case an | |
922 | -- error message is produced. In the last case, the node is flagged | |
923 | -- as overloaded and the interpretations are collected in All_Interp. | |
996ae0b0 RK |
924 | |
925 | -- If the name is an Access_To_Subprogram, it cannot be overloaded, but | |
926 | -- the type-checking is similar to that of other calls. | |
927 | ||
928 | procedure Analyze_Call (N : Node_Id) is | |
929 | Actuals : constant List_Id := Parameter_Associations (N); | |
63319f58 | 930 | Nam : Node_Id; |
996ae0b0 RK |
931 | X : Interp_Index; |
932 | It : Interp; | |
933 | Nam_Ent : Entity_Id; | |
63319f58 RD |
934 | Success : Boolean := False; |
935 | ||
936 | Deref : Boolean := False; | |
1cb17b78 AC |
937 | -- Flag indicates whether an interpretation of the prefix is a |
938 | -- parameterless call that returns an access_to_subprogram. | |
996ae0b0 | 939 | |
23685ae6 AC |
940 | procedure Check_Mixed_Parameter_And_Named_Associations; |
941 | -- Check that parameter and named associations are not mixed. This is | |
942 | -- a restriction in SPARK mode. | |
943 | ||
288cbbbd JM |
944 | procedure Check_Writable_Actuals (N : Node_Id); |
945 | -- If the call has out or in-out parameters then mark its outermost | |
946 | -- enclosing construct as a node on which the writable actuals check | |
947 | -- must be performed. | |
948 | ||
996ae0b0 | 949 | function Name_Denotes_Function return Boolean; |
5ff22245 ES |
950 | -- If the type of the name is an access to subprogram, this may be the |
951 | -- type of a name, or the return type of the function being called. If | |
952 | -- the name is not an entity then it can denote a protected function. | |
953 | -- Until we distinguish Etype from Return_Type, we must use this routine | |
954 | -- to resolve the meaning of the name in the call. | |
955 | ||
956 | procedure No_Interpretation; | |
957 | -- Output error message when no valid interpretation exists | |
996ae0b0 | 958 | |
23685ae6 AC |
959 | -------------------------------------------------- |
960 | -- Check_Mixed_Parameter_And_Named_Associations -- | |
961 | -------------------------------------------------- | |
962 | ||
963 | procedure Check_Mixed_Parameter_And_Named_Associations is | |
964 | Actual : Node_Id; | |
965 | Named_Seen : Boolean; | |
f5afb270 | 966 | |
23685ae6 | 967 | begin |
23685ae6 | 968 | Named_Seen := False; |
f5afb270 AC |
969 | |
970 | Actual := First (Actuals); | |
23685ae6 AC |
971 | while Present (Actual) loop |
972 | case Nkind (Actual) is | |
973 | when N_Parameter_Association => | |
974 | if Named_Seen then | |
ce5ba43a | 975 | Check_SPARK_05_Restriction |
23685ae6 AC |
976 | ("named association cannot follow positional one", |
977 | Actual); | |
978 | exit; | |
979 | end if; | |
84dad556 | 980 | |
23685ae6 AC |
981 | when others => |
982 | Named_Seen := True; | |
983 | end case; | |
984 | ||
985 | Next (Actual); | |
986 | end loop; | |
987 | end Check_Mixed_Parameter_And_Named_Associations; | |
988 | ||
288cbbbd JM |
989 | ---------------------------- |
990 | -- Check_Writable_Actuals -- | |
991 | ---------------------------- | |
992 | ||
993 | -- The identification of conflicts in calls to functions with writable | |
551e1935 | 994 | -- actuals is performed in the analysis phase of the front end to ensure |
288cbbbd JM |
995 | -- that it reports exactly the same errors compiling with and without |
996 | -- expansion enabled. It is performed in two stages: | |
997 | ||
551e1935 AC |
998 | -- 1) When a call to a function with out-mode parameters is found, |
999 | -- we climb to the outermost enclosing construct that can be | |
288cbbbd JM |
1000 | -- evaluated in arbitrary order and we mark it with the flag |
1001 | -- Check_Actuals. | |
1002 | ||
551e1935 AC |
1003 | -- 2) When the analysis of the marked node is complete, we traverse |
1004 | -- its decorated subtree searching for conflicts (see function | |
1005 | -- Sem_Util.Check_Function_Writable_Actuals). | |
288cbbbd | 1006 | |
551e1935 AC |
1007 | -- The unique exception to this general rule is for aggregates, since |
1008 | -- their analysis is performed by the front end in the resolution | |
1009 | -- phase. For aggregates we do not climb to their enclosing construct: | |
288cbbbd JM |
1010 | -- we restrict the analysis to the subexpressions initializing the |
1011 | -- aggregate components. | |
1012 | ||
1013 | -- This implies that the analysis of expressions containing aggregates | |
551e1935 | 1014 | -- is not complete, since there may be conflicts on writable actuals |
288cbbbd JM |
1015 | -- involving subexpressions of the enclosing logical or arithmetic |
1016 | -- expressions. However, we cannot wait and perform the analysis when | |
551e1935 | 1017 | -- the whole subtree is resolved, since the subtrees may be transformed, |
288cbbbd JM |
1018 | -- thus adding extra complexity and computation cost to identify and |
1019 | -- report exactly the same errors compiling with and without expansion | |
1020 | -- enabled. | |
1021 | ||
1022 | procedure Check_Writable_Actuals (N : Node_Id) is | |
288cbbbd JM |
1023 | begin |
1024 | if Comes_From_Source (N) | |
1025 | and then Present (Get_Subprogram_Entity (N)) | |
1026 | and then Has_Out_Or_In_Out_Parameter (Get_Subprogram_Entity (N)) | |
1027 | then | |
1028 | -- For procedures and entries there is no need to climb since | |
1029 | -- we only need to check if the actuals of this call invoke | |
1030 | -- functions whose out-mode parameters overlap. | |
1031 | ||
1032 | if Nkind (N) /= N_Function_Call then | |
1033 | Set_Check_Actuals (N); | |
1034 | ||
1035 | -- For calls to functions we climb to the outermost enclosing | |
1036 | -- construct where the out-mode actuals of this function may | |
1037 | -- introduce conflicts. | |
1038 | ||
1039 | else | |
1040 | declare | |
1041 | Outermost : Node_Id; | |
1042 | P : Node_Id := N; | |
1043 | ||
1044 | begin | |
1045 | while Present (P) loop | |
1046 | ||
551e1935 | 1047 | -- For object declarations we can climb to the node from |
288cbbbd JM |
1048 | -- its object definition branch or from its initializing |
1049 | -- expression. We prefer to mark the child node as the | |
1050 | -- outermost construct to avoid adding further complexity | |
551e1935 | 1051 | -- to the routine that will later take care of |
288cbbbd JM |
1052 | -- performing the writable actuals check. |
1053 | ||
22e89283 AC |
1054 | if Has_Arbitrary_Evaluation_Order (Nkind (P)) |
1055 | and then not Nkind_In (P, N_Assignment_Statement, | |
1056 | N_Object_Declaration) | |
288cbbbd JM |
1057 | then |
1058 | Outermost := P; | |
1059 | end if; | |
1060 | ||
1061 | -- Avoid climbing more than needed! | |
1062 | ||
22e89283 | 1063 | exit when Stop_Subtree_Climbing (Nkind (P)) |
288cbbbd JM |
1064 | or else (Nkind (P) = N_Range |
1065 | and then not | |
22e89283 | 1066 | Nkind_In (Parent (P), N_In, N_Not_In)); |
288cbbbd JM |
1067 | |
1068 | P := Parent (P); | |
1069 | end loop; | |
1070 | ||
1071 | Set_Check_Actuals (Outermost); | |
1072 | end; | |
1073 | end if; | |
1074 | end if; | |
1075 | end Check_Writable_Actuals; | |
1076 | ||
996ae0b0 RK |
1077 | --------------------------- |
1078 | -- Name_Denotes_Function -- | |
1079 | --------------------------- | |
1080 | ||
1081 | function Name_Denotes_Function return Boolean is | |
1082 | begin | |
1083 | if Is_Entity_Name (Nam) then | |
1084 | return Ekind (Entity (Nam)) = E_Function; | |
996ae0b0 RK |
1085 | elsif Nkind (Nam) = N_Selected_Component then |
1086 | return Ekind (Entity (Selector_Name (Nam))) = E_Function; | |
996ae0b0 RK |
1087 | else |
1088 | return False; | |
1089 | end if; | |
1090 | end Name_Denotes_Function; | |
1091 | ||
5ff22245 ES |
1092 | ----------------------- |
1093 | -- No_Interpretation -- | |
1094 | ----------------------- | |
1095 | ||
1096 | procedure No_Interpretation is | |
1097 | L : constant Boolean := Is_List_Member (N); | |
1098 | K : constant Node_Kind := Nkind (Parent (N)); | |
1099 | ||
1100 | begin | |
1101 | -- If the node is in a list whose parent is not an expression then it | |
1102 | -- must be an attempted procedure call. | |
1103 | ||
1104 | if L and then K not in N_Subexpr then | |
1105 | if Ekind (Entity (Nam)) = E_Generic_Procedure then | |
1106 | Error_Msg_NE | |
1107 | ("must instantiate generic procedure& before call", | |
1108 | Nam, Entity (Nam)); | |
1109 | else | |
84dad556 | 1110 | Error_Msg_N ("procedure or entry name expected", Nam); |
5ff22245 ES |
1111 | end if; |
1112 | ||
1113 | -- Check for tasking cases where only an entry call will do | |
1114 | ||
1115 | elsif not L | |
1116 | and then Nkind_In (K, N_Entry_Call_Alternative, | |
1117 | N_Triggering_Alternative) | |
1118 | then | |
1119 | Error_Msg_N ("entry name expected", Nam); | |
1120 | ||
1121 | -- Otherwise give general error message | |
1122 | ||
1123 | else | |
1124 | Error_Msg_N ("invalid prefix in call", Nam); | |
1125 | end if; | |
1126 | end No_Interpretation; | |
1127 | ||
996ae0b0 RK |
1128 | -- Start of processing for Analyze_Call |
1129 | ||
1130 | begin | |
6480338a | 1131 | if Restriction_Check_Required (SPARK_05) then |
23685ae6 AC |
1132 | Check_Mixed_Parameter_And_Named_Associations; |
1133 | end if; | |
1134 | ||
996ae0b0 RK |
1135 | -- Initialize the type of the result of the call to the error type, |
1136 | -- which will be reset if the type is successfully resolved. | |
1137 | ||
1138 | Set_Etype (N, Any_Type); | |
1139 | ||
63319f58 RD |
1140 | Nam := Name (N); |
1141 | ||
996ae0b0 RK |
1142 | if not Is_Overloaded (Nam) then |
1143 | ||
1144 | -- Only one interpretation to check | |
1145 | ||
1146 | if Ekind (Etype (Nam)) = E_Subprogram_Type then | |
1147 | Nam_Ent := Etype (Nam); | |
1148 | ||
758c442c GD |
1149 | -- If the prefix is an access_to_subprogram, this may be an indirect |
1150 | -- call. This is the case if the name in the call is not an entity | |
1151 | -- name, or if it is a function name in the context of a procedure | |
1152 | -- call. In this latter case, we have a call to a parameterless | |
1153 | -- function that returns a pointer_to_procedure which is the entity | |
5ff22245 ES |
1154 | -- being called. Finally, F (X) may be a call to a parameterless |
1155 | -- function that returns a pointer to a function with parameters. | |
80e59506 | 1156 | -- Note that if F returns an access-to-subprogram whose designated |
4bb9c7b9 AC |
1157 | -- type is an array, F (X) cannot be interpreted as an indirect call |
1158 | -- through the result of the call to F. | |
758c442c | 1159 | |
996ae0b0 RK |
1160 | elsif Is_Access_Type (Etype (Nam)) |
1161 | and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type | |
758c442c GD |
1162 | and then |
1163 | (not Name_Denotes_Function | |
b2834fbd AC |
1164 | or else Nkind (N) = N_Procedure_Call_Statement |
1165 | or else | |
1166 | (Nkind (Parent (N)) /= N_Explicit_Dereference | |
1167 | and then Is_Entity_Name (Nam) | |
1168 | and then No (First_Formal (Entity (Nam))) | |
1169 | and then not | |
1170 | Is_Array_Type (Etype (Designated_Type (Etype (Nam)))) | |
1171 | and then Present (Actuals))) | |
996ae0b0 RK |
1172 | then |
1173 | Nam_Ent := Designated_Type (Etype (Nam)); | |
1174 | Insert_Explicit_Dereference (Nam); | |
1175 | ||
1176 | -- Selected component case. Simple entry or protected operation, | |
1177 | -- where the entry name is given by the selector name. | |
1178 | ||
1179 | elsif Nkind (Nam) = N_Selected_Component then | |
1180 | Nam_Ent := Entity (Selector_Name (Nam)); | |
1181 | ||
bce79204 AC |
1182 | if not Ekind_In (Nam_Ent, E_Entry, |
1183 | E_Entry_Family, | |
1184 | E_Function, | |
1185 | E_Procedure) | |
996ae0b0 RK |
1186 | then |
1187 | Error_Msg_N ("name in call is not a callable entity", Nam); | |
1188 | Set_Etype (N, Any_Type); | |
1189 | return; | |
1190 | end if; | |
1191 | ||
1192 | -- If the name is an Indexed component, it can be a call to a member | |
1193 | -- of an entry family. The prefix must be a selected component whose | |
1194 | -- selector is the entry. Analyze_Procedure_Call normalizes several | |
1195 | -- kinds of call into this form. | |
1196 | ||
1197 | elsif Nkind (Nam) = N_Indexed_Component then | |
996ae0b0 RK |
1198 | if Nkind (Prefix (Nam)) = N_Selected_Component then |
1199 | Nam_Ent := Entity (Selector_Name (Prefix (Nam))); | |
996ae0b0 RK |
1200 | else |
1201 | Error_Msg_N ("name in call is not a callable entity", Nam); | |
1202 | Set_Etype (N, Any_Type); | |
1203 | return; | |
996ae0b0 RK |
1204 | end if; |
1205 | ||
1206 | elsif not Is_Entity_Name (Nam) then | |
1207 | Error_Msg_N ("name in call is not a callable entity", Nam); | |
1208 | Set_Etype (N, Any_Type); | |
1209 | return; | |
1210 | ||
1211 | else | |
1212 | Nam_Ent := Entity (Nam); | |
1213 | ||
5f50020a ES |
1214 | -- If not overloadable, this may be a generalized indexing |
1215 | -- operation with named associations. Rewrite again as an | |
1216 | -- indexed component and analyze as container indexing. | |
996ae0b0 RK |
1217 | |
1218 | if not Is_Overloadable (Nam_Ent) then | |
32bba3c9 AC |
1219 | if Present |
1220 | (Find_Value_Of_Aspect | |
1221 | (Etype (Nam_Ent), Aspect_Constant_Indexing)) | |
5f50020a ES |
1222 | then |
1223 | Replace (N, | |
1224 | Make_Indexed_Component (Sloc (N), | |
32bba3c9 | 1225 | Prefix => Nam, |
5f50020a ES |
1226 | Expressions => Parameter_Associations (N))); |
1227 | ||
1228 | if Try_Container_Indexing (N, Nam, Expressions (N)) then | |
1229 | return; | |
1230 | else | |
1231 | No_Interpretation; | |
1232 | end if; | |
1233 | ||
1234 | else | |
1235 | No_Interpretation; | |
1236 | end if; | |
32bba3c9 | 1237 | |
5ff22245 ES |
1238 | return; |
1239 | end if; | |
1240 | end if; | |
996ae0b0 | 1241 | |
5ff22245 ES |
1242 | -- Operations generated for RACW stub types are called only through |
1243 | -- dispatching, and can never be the static interpretation of a call. | |
996ae0b0 | 1244 | |
5ff22245 ES |
1245 | if Is_RACW_Stub_Type_Operation (Nam_Ent) then |
1246 | No_Interpretation; | |
1247 | return; | |
996ae0b0 RK |
1248 | end if; |
1249 | ||
1250 | Analyze_One_Call (N, Nam_Ent, True, Success); | |
1251 | ||
ec6078e3 ES |
1252 | -- If this is an indirect call, the return type of the access_to |
1253 | -- subprogram may be an incomplete type. At the point of the call, | |
947430d5 AC |
1254 | -- use the full type if available, and at the same time update the |
1255 | -- return type of the access_to_subprogram. | |
ec6078e3 ES |
1256 | |
1257 | if Success | |
d469eabe | 1258 | and then Nkind (Nam) = N_Explicit_Dereference |
ec6078e3 ES |
1259 | and then Ekind (Etype (N)) = E_Incomplete_Type |
1260 | and then Present (Full_View (Etype (N))) | |
1261 | then | |
1262 | Set_Etype (N, Full_View (Etype (N))); | |
1263 | Set_Etype (Nam_Ent, Etype (N)); | |
1264 | end if; | |
1265 | ||
a7e68e7f HK |
1266 | -- Overloaded call |
1267 | ||
996ae0b0 | 1268 | else |
5ff22245 ES |
1269 | -- An overloaded selected component must denote overloaded operations |
1270 | -- of a concurrent type. The interpretations are attached to the | |
1271 | -- simple name of those operations. | |
996ae0b0 RK |
1272 | |
1273 | if Nkind (Nam) = N_Selected_Component then | |
1274 | Nam := Selector_Name (Nam); | |
1275 | end if; | |
1276 | ||
1277 | Get_First_Interp (Nam, X, It); | |
996ae0b0 RK |
1278 | while Present (It.Nam) loop |
1279 | Nam_Ent := It.Nam; | |
1cb17b78 | 1280 | Deref := False; |
996ae0b0 RK |
1281 | |
1282 | -- Name may be call that returns an access to subprogram, or more | |
1283 | -- generally an overloaded expression one of whose interpretations | |
947430d5 AC |
1284 | -- yields an access to subprogram. If the name is an entity, we do |
1285 | -- not dereference, because the node is a call that returns the | |
1286 | -- access type: note difference between f(x), where the call may | |
1287 | -- return an access subprogram type, and f(x)(y), where the type | |
1288 | -- returned by the call to f is implicitly dereferenced to analyze | |
1289 | -- the outer call. | |
996ae0b0 RK |
1290 | |
1291 | if Is_Access_Type (Nam_Ent) then | |
1292 | Nam_Ent := Designated_Type (Nam_Ent); | |
1293 | ||
1294 | elsif Is_Access_Type (Etype (Nam_Ent)) | |
1cb17b78 AC |
1295 | and then |
1296 | (not Is_Entity_Name (Nam) | |
1297 | or else Nkind (N) = N_Procedure_Call_Statement) | |
996ae0b0 RK |
1298 | and then Ekind (Designated_Type (Etype (Nam_Ent))) |
1299 | = E_Subprogram_Type | |
1300 | then | |
1301 | Nam_Ent := Designated_Type (Etype (Nam_Ent)); | |
1cb17b78 AC |
1302 | |
1303 | if Is_Entity_Name (Nam) then | |
1304 | Deref := True; | |
1305 | end if; | |
996ae0b0 RK |
1306 | end if; |
1307 | ||
7415029d AC |
1308 | -- If the call has been rewritten from a prefixed call, the first |
1309 | -- parameter has been analyzed, but may need a subsequent | |
1310 | -- dereference, so skip its analysis now. | |
1311 | ||
1312 | if N /= Original_Node (N) | |
1313 | and then Nkind (Original_Node (N)) = Nkind (N) | |
1314 | and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N))) | |
1315 | and then Present (Parameter_Associations (N)) | |
1316 | and then Present (Etype (First (Parameter_Associations (N)))) | |
1317 | then | |
1318 | Analyze_One_Call | |
1319 | (N, Nam_Ent, False, Success, Skip_First => True); | |
1320 | else | |
1321 | Analyze_One_Call (N, Nam_Ent, False, Success); | |
1322 | end if; | |
996ae0b0 RK |
1323 | |
1324 | -- If the interpretation succeeds, mark the proper type of the | |
1325 | -- prefix (any valid candidate will do). If not, remove the | |
1326 | -- candidate interpretation. This only needs to be done for | |
1327 | -- overloaded protected operations, for other entities disambi- | |
1328 | -- guation is done directly in Resolve. | |
1329 | ||
1330 | if Success then | |
1cb17b78 AC |
1331 | if Deref |
1332 | and then Nkind (Parent (N)) /= N_Explicit_Dereference | |
1333 | then | |
1334 | Set_Entity (Nam, It.Nam); | |
1335 | Insert_Explicit_Dereference (Nam); | |
1336 | Set_Etype (Nam, Nam_Ent); | |
1337 | ||
1338 | else | |
1339 | Set_Etype (Nam, It.Typ); | |
1340 | end if; | |
996ae0b0 | 1341 | |
d469eabe HK |
1342 | elsif Nkind_In (Name (N), N_Selected_Component, |
1343 | N_Function_Call) | |
fbf5a39b | 1344 | then |
996ae0b0 RK |
1345 | Remove_Interp (X); |
1346 | end if; | |
1347 | ||
1348 | Get_Next_Interp (X, It); | |
1349 | end loop; | |
1350 | ||
a7e68e7f HK |
1351 | -- If the name is the result of a function call, it can only be a |
1352 | -- call to a function returning an access to subprogram. Insert | |
1353 | -- explicit dereference. | |
996ae0b0 RK |
1354 | |
1355 | if Nkind (Nam) = N_Function_Call then | |
1356 | Insert_Explicit_Dereference (Nam); | |
1357 | end if; | |
1358 | ||
1359 | if Etype (N) = Any_Type then | |
1360 | ||
1361 | -- None of the interpretations is compatible with the actuals | |
1362 | ||
1363 | Diagnose_Call (N, Nam); | |
1364 | ||
1365 | -- Special checks for uninstantiated put routines | |
1366 | ||
1367 | if Nkind (N) = N_Procedure_Call_Statement | |
1368 | and then Is_Entity_Name (Nam) | |
1369 | and then Chars (Nam) = Name_Put | |
1370 | and then List_Length (Actuals) = 1 | |
1371 | then | |
1372 | declare | |
1373 | Arg : constant Node_Id := First (Actuals); | |
1374 | Typ : Entity_Id; | |
1375 | ||
1376 | begin | |
1377 | if Nkind (Arg) = N_Parameter_Association then | |
1378 | Typ := Etype (Explicit_Actual_Parameter (Arg)); | |
1379 | else | |
1380 | Typ := Etype (Arg); | |
1381 | end if; | |
1382 | ||
1383 | if Is_Signed_Integer_Type (Typ) then | |
1384 | Error_Msg_N | |
a90bd866 RD |
1385 | ("possible missing instantiation of " |
1386 | & "'Text_'I'O.'Integer_'I'O!", Nam); | |
996ae0b0 RK |
1387 | |
1388 | elsif Is_Modular_Integer_Type (Typ) then | |
1389 | Error_Msg_N | |
a90bd866 RD |
1390 | ("possible missing instantiation of " |
1391 | & "'Text_'I'O.'Modular_'I'O!", Nam); | |
996ae0b0 RK |
1392 | |
1393 | elsif Is_Floating_Point_Type (Typ) then | |
1394 | Error_Msg_N | |
a90bd866 RD |
1395 | ("possible missing instantiation of " |
1396 | & "'Text_'I'O.'Float_'I'O!", Nam); | |
996ae0b0 RK |
1397 | |
1398 | elsif Is_Ordinary_Fixed_Point_Type (Typ) then | |
1399 | Error_Msg_N | |
a90bd866 RD |
1400 | ("possible missing instantiation of " |
1401 | & "'Text_'I'O.'Fixed_'I'O!", Nam); | |
996ae0b0 RK |
1402 | |
1403 | elsif Is_Decimal_Fixed_Point_Type (Typ) then | |
1404 | Error_Msg_N | |
a90bd866 RD |
1405 | ("possible missing instantiation of " |
1406 | & "'Text_'I'O.'Decimal_'I'O!", Nam); | |
996ae0b0 RK |
1407 | |
1408 | elsif Is_Enumeration_Type (Typ) then | |
1409 | Error_Msg_N | |
a90bd866 RD |
1410 | ("possible missing instantiation of " |
1411 | & "'Text_'I'O.'Enumeration_'I'O!", Nam); | |
996ae0b0 RK |
1412 | end if; |
1413 | end; | |
1414 | end if; | |
1415 | ||
1416 | elsif not Is_Overloaded (N) | |
1417 | and then Is_Entity_Name (Nam) | |
1418 | then | |
aab883ec ES |
1419 | -- Resolution yields a single interpretation. Verify that the |
1420 | -- reference has capitalization consistent with the declaration. | |
996ae0b0 | 1421 | |
e7ba564f | 1422 | Set_Entity_With_Checks (Nam, Entity (Nam)); |
996ae0b0 RK |
1423 | Generate_Reference (Entity (Nam), Nam); |
1424 | ||
1425 | Set_Etype (Nam, Etype (Entity (Nam))); | |
30c20106 AC |
1426 | else |
1427 | Remove_Abstract_Operations (N); | |
996ae0b0 RK |
1428 | end if; |
1429 | ||
1430 | End_Interp_List; | |
1431 | end if; | |
288cbbbd JM |
1432 | |
1433 | if Ada_Version >= Ada_2012 then | |
1434 | ||
1435 | -- Check if the call contains a function with writable actuals | |
1436 | ||
1437 | Check_Writable_Actuals (N); | |
1438 | ||
551e1935 AC |
1439 | -- If found and the outermost construct that can be evaluated in |
1440 | -- an arbitrary order is precisely this call, then check all its | |
288cbbbd JM |
1441 | -- actuals. |
1442 | ||
22e89283 | 1443 | Check_Function_Writable_Actuals (N); |
288cbbbd | 1444 | end if; |
996ae0b0 RK |
1445 | end Analyze_Call; |
1446 | ||
19d846a0 RD |
1447 | ----------------------------- |
1448 | -- Analyze_Case_Expression -- | |
1449 | ----------------------------- | |
1450 | ||
1451 | procedure Analyze_Case_Expression (N : Node_Id) is | |
19d846a0 RD |
1452 | procedure Non_Static_Choice_Error (Choice : Node_Id); |
1453 | -- Error routine invoked by the generic instantiation below when | |
1454 | -- the case expression has a non static choice. | |
1455 | ||
15918371 AC |
1456 | package Case_Choices_Analysis is new |
1457 | Generic_Analyze_Choices | |
1458 | (Process_Associated_Node => No_OP); | |
1459 | use Case_Choices_Analysis; | |
1460 | ||
1461 | package Case_Choices_Checking is new | |
1462 | Generic_Check_Choices | |
1463 | (Process_Empty_Choice => No_OP, | |
19d846a0 RD |
1464 | Process_Non_Static_Choice => Non_Static_Choice_Error, |
1465 | Process_Associated_Node => No_OP); | |
15918371 | 1466 | use Case_Choices_Checking; |
19d846a0 | 1467 | |
19d846a0 RD |
1468 | ----------------------------- |
1469 | -- Non_Static_Choice_Error -- | |
1470 | ----------------------------- | |
1471 | ||
1472 | procedure Non_Static_Choice_Error (Choice : Node_Id) is | |
1473 | begin | |
1474 | Flag_Non_Static_Expr | |
1475 | ("choice given in case expression is not static!", Choice); | |
1476 | end Non_Static_Choice_Error; | |
1477 | ||
752b81d9 AC |
1478 | -- Local variables |
1479 | ||
1480 | Expr : constant Node_Id := Expression (N); | |
752b81d9 AC |
1481 | Alt : Node_Id; |
1482 | Exp_Type : Entity_Id; | |
1483 | Exp_Btype : Entity_Id; | |
1484 | ||
308aab0b AC |
1485 | FirstX : Node_Id := Empty; |
1486 | -- First expression in the case for which there is some type information | |
1487 | -- available, i.e. it is not Any_Type, which can happen because of some | |
1488 | -- error, or from the use of e.g. raise Constraint_Error. | |
1489 | ||
752b81d9 | 1490 | Others_Present : Boolean; |
15918371 | 1491 | -- Indicates if Others was present |
752b81d9 | 1492 | |
e49de265 | 1493 | Wrong_Alt : Node_Id := Empty; |
10671e7a AC |
1494 | -- For error reporting |
1495 | ||
19d846a0 RD |
1496 | -- Start of processing for Analyze_Case_Expression |
1497 | ||
1498 | begin | |
1499 | if Comes_From_Source (N) then | |
c86cf714 | 1500 | Check_Compiler_Unit ("case expression", N); |
19d846a0 RD |
1501 | end if; |
1502 | ||
1503 | Analyze_And_Resolve (Expr, Any_Discrete); | |
1504 | Check_Unset_Reference (Expr); | |
1505 | Exp_Type := Etype (Expr); | |
1506 | Exp_Btype := Base_Type (Exp_Type); | |
1507 | ||
1508 | Alt := First (Alternatives (N)); | |
1509 | while Present (Alt) loop | |
1510 | Analyze (Expression (Alt)); | |
308aab0b AC |
1511 | |
1512 | if No (FirstX) and then Etype (Expression (Alt)) /= Any_Type then | |
1513 | FirstX := Expression (Alt); | |
1514 | end if; | |
1515 | ||
19d846a0 RD |
1516 | Next (Alt); |
1517 | end loop; | |
1518 | ||
308aab0b AC |
1519 | -- Get our initial type from the first expression for which we got some |
1520 | -- useful type information from the expression. | |
1521 | ||
19d846a0 RD |
1522 | if not Is_Overloaded (FirstX) then |
1523 | Set_Etype (N, Etype (FirstX)); | |
1524 | ||
1525 | else | |
1526 | declare | |
1527 | I : Interp_Index; | |
1528 | It : Interp; | |
1529 | ||
1530 | begin | |
1531 | Set_Etype (N, Any_Type); | |
1532 | ||
1533 | Get_First_Interp (FirstX, I, It); | |
1534 | while Present (It.Nam) loop | |
1535 | ||
308e6f3a RW |
1536 | -- For each interpretation of the first expression, we only |
1537 | -- add the interpretation if every other expression in the | |
19d846a0 RD |
1538 | -- case expression alternatives has a compatible type. |
1539 | ||
1540 | Alt := Next (First (Alternatives (N))); | |
1541 | while Present (Alt) loop | |
1542 | exit when not Has_Compatible_Type (Expression (Alt), It.Typ); | |
1543 | Next (Alt); | |
1544 | end loop; | |
1545 | ||
1546 | if No (Alt) then | |
1547 | Add_One_Interp (N, It.Typ, It.Typ); | |
10671e7a AC |
1548 | else |
1549 | Wrong_Alt := Alt; | |
19d846a0 RD |
1550 | end if; |
1551 | ||
1552 | Get_Next_Interp (I, It); | |
1553 | end loop; | |
1554 | end; | |
1555 | end if; | |
1556 | ||
1557 | Exp_Btype := Base_Type (Exp_Type); | |
1558 | ||
1559 | -- The expression must be of a discrete type which must be determinable | |
1560 | -- independently of the context in which the expression occurs, but | |
1561 | -- using the fact that the expression must be of a discrete type. | |
1562 | -- Moreover, the type this expression must not be a character literal | |
1563 | -- (which is always ambiguous). | |
1564 | ||
1565 | -- If error already reported by Resolve, nothing more to do | |
1566 | ||
15918371 | 1567 | if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then |
19d846a0 RD |
1568 | return; |
1569 | ||
bf0b0e5e AC |
1570 | -- Special casee message for character literal |
1571 | ||
19d846a0 RD |
1572 | elsif Exp_Btype = Any_Character then |
1573 | Error_Msg_N | |
1574 | ("character literal as case expression is ambiguous", Expr); | |
1575 | return; | |
1576 | end if; | |
1577 | ||
10671e7a | 1578 | if Etype (N) = Any_Type and then Present (Wrong_Alt) then |
bf0b0e5e AC |
1579 | Error_Msg_N |
1580 | ("type incompatible with that of previous alternatives", | |
1581 | Expression (Wrong_Alt)); | |
10671e7a AC |
1582 | return; |
1583 | end if; | |
1584 | ||
19d846a0 RD |
1585 | -- If the case expression is a formal object of mode in out, then |
1586 | -- treat it as having a nonstatic subtype by forcing use of the base | |
1587 | -- type (which has to get passed to Check_Case_Choices below). Also | |
1588 | -- use base type when the case expression is parenthesized. | |
1589 | ||
1590 | if Paren_Count (Expr) > 0 | |
1591 | or else (Is_Entity_Name (Expr) | |
1592 | and then Ekind (Entity (Expr)) = E_Generic_In_Out_Parameter) | |
1593 | then | |
1594 | Exp_Type := Exp_Btype; | |
1595 | end if; | |
1596 | ||
752b81d9 AC |
1597 | -- The case expression alternatives cover the range of a static subtype |
1598 | -- subject to aspect Static_Predicate. Do not check the choices when the | |
1599 | -- case expression has not been fully analyzed yet because this may lead | |
1600 | -- to bogus errors. | |
1601 | ||
edab6088 | 1602 | if Is_OK_Static_Subtype (Exp_Type) |
ee4eee0a | 1603 | and then Has_Static_Predicate_Aspect (Exp_Type) |
752b81d9 AC |
1604 | and then In_Spec_Expression |
1605 | then | |
1606 | null; | |
1607 | ||
15918371 | 1608 | -- Call Analyze_Choices and Check_Choices to do the rest of the work |
19d846a0 | 1609 | |
752b81d9 | 1610 | else |
15918371 AC |
1611 | Analyze_Choices (Alternatives (N), Exp_Type); |
1612 | Check_Choices (N, Alternatives (N), Exp_Type, Others_Present); | |
752b81d9 | 1613 | end if; |
19d846a0 RD |
1614 | |
1615 | if Exp_Type = Universal_Integer and then not Others_Present then | |
1616 | Error_Msg_N | |
1617 | ("case on universal integer requires OTHERS choice", Expr); | |
1618 | end if; | |
1619 | end Analyze_Case_Expression; | |
1620 | ||
996ae0b0 RK |
1621 | --------------------------- |
1622 | -- Analyze_Comparison_Op -- | |
1623 | --------------------------- | |
1624 | ||
1625 | procedure Analyze_Comparison_Op (N : Node_Id) is | |
1626 | L : constant Node_Id := Left_Opnd (N); | |
1627 | R : constant Node_Id := Right_Opnd (N); | |
1628 | Op_Id : Entity_Id := Entity (N); | |
1629 | ||
1630 | begin | |
1631 | Set_Etype (N, Any_Type); | |
1632 | Candidate_Type := Empty; | |
1633 | ||
1634 | Analyze_Expression (L); | |
1635 | Analyze_Expression (R); | |
1636 | ||
1637 | if Present (Op_Id) then | |
996ae0b0 RK |
1638 | if Ekind (Op_Id) = E_Operator then |
1639 | Find_Comparison_Types (L, R, Op_Id, N); | |
1640 | else | |
1641 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
1642 | end if; | |
1643 | ||
1644 | if Is_Overloaded (L) then | |
1645 | Set_Etype (L, Intersect_Types (L, R)); | |
1646 | end if; | |
1647 | ||
1648 | else | |
1649 | Op_Id := Get_Name_Entity_Id (Chars (N)); | |
996ae0b0 | 1650 | while Present (Op_Id) loop |
996ae0b0 RK |
1651 | if Ekind (Op_Id) = E_Operator then |
1652 | Find_Comparison_Types (L, R, Op_Id, N); | |
1653 | else | |
1654 | Analyze_User_Defined_Binary_Op (N, Op_Id); | |
1655 | end if; | |
1656 | ||
1657 | Op_Id := Homonym (Op_Id); | |
1658 | end loop; | |
1659 | end if; | |
1660 | ||
1661 | Operator_Check (N); | |
22e89283 | 1662 | Check_Function_Writable_Actuals (N); |
996ae0b0 RK |
1663 | end Analyze_Comparison_Op; |
1664 | ||
1665 | --------------------------- | |
1666 | -- Analyze_Concatenation -- | |
1667 | --------------------------- | |
1668 | ||
fe39cf20 BD |
1669 | procedure Analyze_Concatenation (N : Node_Id) is |
1670 | ||
1671 | -- We wish to avoid deep recursion, because concatenations are often | |
1672 | -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left | |
1673 | -- operands nonrecursively until we find something that is not a | |
1674 | -- concatenation (A in this case), or has already been analyzed. We | |
1675 | -- analyze that, and then walk back up the tree following Parent | |
1676 | -- pointers, calling Analyze_Concatenation_Rest to do the rest of the | |
1677 | -- work at each level. The Parent pointers allow us to avoid recursion, | |
1678 | -- and thus avoid running out of memory. | |
1679 | ||
1680 | NN : Node_Id := N; | |
1681 | L : Node_Id; | |
1682 | ||
1683 | begin | |
1684 | Candidate_Type := Empty; | |
1685 | ||
1686 | -- The following code is equivalent to: | |
1687 | ||
1688 | -- Set_Etype (N, Any_Type); | |
1689 | -- Analyze_Expression (Left_Opnd (N)); | |
1690 | -- Analyze_Concatenation_Rest (N); | |
1691 | ||
1692 | -- where the Analyze_Expression call recurses back here if the left | |
1693 | -- operand is a concatenation. | |
1694 | ||
1695 | -- Walk down left operands | |
1696 | ||
1697 | loop | |
1698 | Set_Etype (NN, Any_Type); | |
1699 | L := Left_Opnd (NN); | |
1700 | exit when Nkind (L) /= N_Op_Concat or else Analyzed (L); | |
1701 | NN := L; | |
1702 | end loop; | |
1703 | ||
1704 | -- Now (given the above example) NN is A&B and L is A | |
1705 | ||
1706 | -- First analyze L ... | |
1707 | ||
1708 | Analyze_Expression (L); | |
1709 | ||
1710 | -- ... then walk NN back up until we reach N (where we started), calling | |
1711 | -- Analyze_Concatenation_Rest along the way. | |
1712 | ||
1713 | loop | |
1714 | Analyze_Concatenation_Rest (NN); | |
1715 | exit when NN = N; | |
1716 | NN := Parent (NN); | |
1717 | end loop; | |
1718 | end Analyze_Concatenation; | |
1719 | ||
1720 | -------------------------------- | |
1721 | -- Analyze_Concatenation_Rest -- | |
1722 | -------------------------------- | |
1723 | ||
996ae0b0 RK |
1724 | -- If the only one-dimensional array type in scope is String, |
1725 | -- this is the resulting type of the operation. Otherwise there | |
1726 | -- will be a concatenation operation defined for each user-defined | |
1727 | -- one-dimensional array. | |
1728 | ||
fe39cf20 | 1729 | procedure Analyze_Concatenation_Rest (N : Node_Id) is |
996ae0b0 RK |
1730 | L : constant Node_Id := Left_Opnd (N); |
1731 | R : constant Node_Id := Right_Opnd (N); | |
1732 | Op_Id : Entity_Id := Entity (N); | |
1733 | LT : Entity_Id; | |
1734 | RT : Entity_Id; | |
1735 | ||
1736 | begin | |
996ae0b0 RK |
1737 | Analyze_Expression (R); |
1738 | ||
cd3cd5b1 AC |
1739 | -- If the entity is present, the node appears in an instance, and |
1740 | -- denotes a predefined concatenation operation. The resulting type is | |
1741 | -- obtained from the arguments when possible. If the arguments are | |
1742 | -- aggregates, the array type and the concatenation type must be | |
fbf5a39b | 1743 | -- visible. |
996ae0b0 RK |
1744 | |
1745 | if Present (Op_Id) then | |
1746 | if Ekind (Op_Id) = E_Operator then | |
996ae0b0 RK |
1747 | LT := Base_Type (Etype (L)); |
1748 | RT := Base_Type (Etype (R)); | |
1749 | ||
1750 | if Is_Array_Type (LT) | |
1751 | and then (RT = LT or else RT = Base_Type (Component_Type (LT))) | |
1752 | then | |
1753 | Add_One_Interp (N, Op_Id, LT); | |
1754 | ||
1755 | elsif Is_Array_Type (RT) | |
1756 | and then LT = Base_Type (Component_Type (RT)) | |
1757 | then | |
1758 | Add_One_Interp (N, Op_Id, RT); | |
1759 | ||
fbf5a39b AC |
1760 | -- If one operand is a string type or a user-defined array type, |
1761 | -- and the other is a literal, result is of the specific type. | |
1762 | ||
1763 | elsif | |
1764 | (Root_Type (LT) = Standard_String | |
1765 | or else Scope (LT) /= Standard_Standard) | |
1766 | and then Etype (R) = Any_String | |
1767 | then | |
1768 | Add_One_Interp (N, Op_Id, LT); | |
1769 | ||
1770 | elsif | |
1771 | (Root_Type (RT) = Standard_String | |
1772 | or else Scope (RT) /= Standard_Standard) | |
1773 | and then Etype (L) = Any_String | |
1774 | then | |
1775 | Add_One_Interp (N, Op_Id, RT); | |
1776 | ||
1777 | elsif not Is_Generic_Type (Etype (Op_Id)) then | |
996ae0b0 | 1778 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); |
fbf5a39b AC |
1779 | |
1780 | else | |
4c46b835 | 1781 | -- Type and its operations must be visible |
fbf5a39b AC |
1782 | |
1783 | Set_Entity (N, Empty); | |
1784 | Analyze_Concatenation (N); | |
996ae0b0 RK |
1785 | end if; |
1786 | ||
1787 | else | |
1788 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
1789 | end if; | |
1790 | ||
1791 | else | |
1a8fae99 | 1792 | Op_Id := Get_Name_Entity_Id (Name_Op_Concat); |
996ae0b0 RK |
1793 | while Present (Op_Id) loop |
1794 | if Ekind (Op_Id) = E_Operator then | |
1a8fae99 ES |
1795 | |
1796 | -- Do not consider operators declared in dead code, they can | |
1797 | -- not be part of the resolution. | |
1798 | ||
1799 | if Is_Eliminated (Op_Id) then | |
1800 | null; | |
1801 | else | |
1802 | Find_Concatenation_Types (L, R, Op_Id, N); | |
1803 | end if; | |
1804 | ||
996ae0b0 RK |
1805 | else |
1806 | Analyze_User_Defined_Binary_Op (N, Op_Id); | |
1807 | end if; | |
1808 | ||
1809 | Op_Id := Homonym (Op_Id); | |
1810 | end loop; | |
1811 | end if; | |
1812 | ||
1813 | Operator_Check (N); | |
fe39cf20 | 1814 | end Analyze_Concatenation_Rest; |
996ae0b0 | 1815 | |
996ae0b0 RK |
1816 | ------------------------- |
1817 | -- Analyze_Equality_Op -- | |
1818 | ------------------------- | |
1819 | ||
1820 | procedure Analyze_Equality_Op (N : Node_Id) is | |
4c46b835 AC |
1821 | Loc : constant Source_Ptr := Sloc (N); |
1822 | L : constant Node_Id := Left_Opnd (N); | |
1823 | R : constant Node_Id := Right_Opnd (N); | |
1824 | Op_Id : Entity_Id; | |
996ae0b0 RK |
1825 | |
1826 | begin | |
1827 | Set_Etype (N, Any_Type); | |
1828 | Candidate_Type := Empty; | |
1829 | ||
1830 | Analyze_Expression (L); | |
1831 | Analyze_Expression (R); | |
1832 | ||
1833 | -- If the entity is set, the node is a generic instance with a non-local | |
1834 | -- reference to the predefined operator or to a user-defined function. | |
1835 | -- It can also be an inequality that is expanded into the negation of a | |
1836 | -- call to a user-defined equality operator. | |
1837 | ||
1838 | -- For the predefined case, the result is Boolean, regardless of the | |
21d7ef70 | 1839 | -- type of the operands. The operands may even be limited, if they are |
996ae0b0 RK |
1840 | -- generic actuals. If they are overloaded, label the left argument with |
1841 | -- the common type that must be present, or with the type of the formal | |
1842 | -- of the user-defined function. | |
1843 | ||
1844 | if Present (Entity (N)) then | |
996ae0b0 RK |
1845 | Op_Id := Entity (N); |
1846 | ||
1847 | if Ekind (Op_Id) = E_Operator then | |
1848 | Add_One_Interp (N, Op_Id, Standard_Boolean); | |
1849 | else | |
1850 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
1851 | end if; | |
1852 | ||
1853 | if Is_Overloaded (L) then | |
996ae0b0 RK |
1854 | if Ekind (Op_Id) = E_Operator then |
1855 | Set_Etype (L, Intersect_Types (L, R)); | |
1856 | else | |
1857 | Set_Etype (L, Etype (First_Formal (Op_Id))); | |
1858 | end if; | |
1859 | end if; | |
1860 | ||
1861 | else | |
1862 | Op_Id := Get_Name_Entity_Id (Chars (N)); | |
996ae0b0 | 1863 | while Present (Op_Id) loop |
996ae0b0 RK |
1864 | if Ekind (Op_Id) = E_Operator then |
1865 | Find_Equality_Types (L, R, Op_Id, N); | |
1866 | else | |
1867 | Analyze_User_Defined_Binary_Op (N, Op_Id); | |
1868 | end if; | |
1869 | ||
1870 | Op_Id := Homonym (Op_Id); | |
1871 | end loop; | |
1872 | end if; | |
1873 | ||
84dad556 AC |
1874 | -- If there was no match, and the operator is inequality, this may be |
1875 | -- a case where inequality has not been made explicit, as for tagged | |
1876 | -- types. Analyze the node as the negation of an equality operation. | |
1877 | -- This cannot be done earlier, because before analysis we cannot rule | |
1878 | -- out the presence of an explicit inequality. | |
996ae0b0 RK |
1879 | |
1880 | if Etype (N) = Any_Type | |
1881 | and then Nkind (N) = N_Op_Ne | |
1882 | then | |
1883 | Op_Id := Get_Name_Entity_Id (Name_Op_Eq); | |
996ae0b0 | 1884 | while Present (Op_Id) loop |
996ae0b0 RK |
1885 | if Ekind (Op_Id) = E_Operator then |
1886 | Find_Equality_Types (L, R, Op_Id, N); | |
1887 | else | |
1888 | Analyze_User_Defined_Binary_Op (N, Op_Id); | |
1889 | end if; | |
1890 | ||
1891 | Op_Id := Homonym (Op_Id); | |
1892 | end loop; | |
1893 | ||
1894 | if Etype (N) /= Any_Type then | |
1895 | Op_Id := Entity (N); | |
1896 | ||
1897 | Rewrite (N, | |
1898 | Make_Op_Not (Loc, | |
1899 | Right_Opnd => | |
1900 | Make_Op_Eq (Loc, | |
aab883ec ES |
1901 | Left_Opnd => Left_Opnd (N), |
1902 | Right_Opnd => Right_Opnd (N)))); | |
996ae0b0 RK |
1903 | |
1904 | Set_Entity (Right_Opnd (N), Op_Id); | |
1905 | Analyze (N); | |
1906 | end if; | |
1907 | end if; | |
1908 | ||
1909 | Operator_Check (N); | |
22e89283 | 1910 | Check_Function_Writable_Actuals (N); |
996ae0b0 RK |
1911 | end Analyze_Equality_Op; |
1912 | ||
1913 | ---------------------------------- | |
1914 | -- Analyze_Explicit_Dereference -- | |
1915 | ---------------------------------- | |
1916 | ||
1917 | procedure Analyze_Explicit_Dereference (N : Node_Id) is | |
1918 | Loc : constant Source_Ptr := Sloc (N); | |
1919 | P : constant Node_Id := Prefix (N); | |
1920 | T : Entity_Id; | |
1921 | I : Interp_Index; | |
1922 | It : Interp; | |
1923 | New_N : Node_Id; | |
1924 | ||
1925 | function Is_Function_Type return Boolean; | |
4c46b835 AC |
1926 | -- Check whether node may be interpreted as an implicit function call |
1927 | ||
1928 | ---------------------- | |
1929 | -- Is_Function_Type -- | |
1930 | ---------------------- | |
996ae0b0 RK |
1931 | |
1932 | function Is_Function_Type return Boolean is | |
4c46b835 AC |
1933 | I : Interp_Index; |
1934 | It : Interp; | |
996ae0b0 RK |
1935 | |
1936 | begin | |
1937 | if not Is_Overloaded (N) then | |
1938 | return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type | |
1939 | and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type; | |
1940 | ||
1941 | else | |
1942 | Get_First_Interp (N, I, It); | |
996ae0b0 RK |
1943 | while Present (It.Nam) loop |
1944 | if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type | |
1945 | or else Etype (Base_Type (It.Typ)) = Standard_Void_Type | |
1946 | then | |
1947 | return False; | |
1948 | end if; | |
1949 | ||
1950 | Get_Next_Interp (I, It); | |
1951 | end loop; | |
1952 | ||
1953 | return True; | |
1954 | end if; | |
1955 | end Is_Function_Type; | |
1956 | ||
98123480 | 1957 | -- Start of processing for Analyze_Explicit_Dereference |
4c46b835 | 1958 | |
996ae0b0 | 1959 | begin |
11bc76df AC |
1960 | -- If source node, check SPARK restriction. We guard this with the |
1961 | -- source node check, because ??? | |
1962 | ||
36b8f95f | 1963 | if Comes_From_Source (N) then |
ce5ba43a | 1964 | Check_SPARK_05_Restriction ("explicit dereference is not allowed", N); |
36b8f95f | 1965 | end if; |
1d801f21 | 1966 | |
226a7fa4 AC |
1967 | -- In formal verification mode, keep track of all reads and writes |
1968 | -- through explicit dereferences. | |
1969 | ||
f5da7a97 | 1970 | if GNATprove_Mode then |
06b599fd | 1971 | SPARK_Specific.Generate_Dereference (N); |
226a7fa4 AC |
1972 | end if; |
1973 | ||
996ae0b0 RK |
1974 | Analyze (P); |
1975 | Set_Etype (N, Any_Type); | |
1976 | ||
1977 | -- Test for remote access to subprogram type, and if so return | |
1978 | -- after rewriting the original tree. | |
1979 | ||
1980 | if Remote_AST_E_Dereference (P) then | |
1981 | return; | |
1982 | end if; | |
1983 | ||
1984 | -- Normal processing for other than remote access to subprogram type | |
1985 | ||
1986 | if not Is_Overloaded (P) then | |
1987 | if Is_Access_Type (Etype (P)) then | |
1988 | ||
f3d57416 | 1989 | -- Set the Etype. We need to go through Is_For_Access_Subtypes to |
0a36105d JM |
1990 | -- avoid other problems caused by the Private_Subtype and it is |
1991 | -- safe to go to the Base_Type because this is the same as | |
1992 | -- converting the access value to its Base_Type. | |
996ae0b0 RK |
1993 | |
1994 | declare | |
1995 | DT : Entity_Id := Designated_Type (Etype (P)); | |
1996 | ||
1997 | begin | |
1998 | if Ekind (DT) = E_Private_Subtype | |
1999 | and then Is_For_Access_Subtype (DT) | |
2000 | then | |
2001 | DT := Base_Type (DT); | |
2002 | end if; | |
2003 | ||
0a36105d | 2004 | -- An explicit dereference is a legal occurrence of an |
0c6826a5 AC |
2005 | -- incomplete type imported through a limited_with clause, if |
2006 | -- the full view is visible, or if we are within an instance | |
2007 | -- body, where the enclosing body has a regular with_clause | |
2008 | -- on the unit. | |
0a36105d | 2009 | |
7b56a91b AC |
2010 | if From_Limited_With (DT) |
2011 | and then not From_Limited_With (Scope (DT)) | |
0a36105d JM |
2012 | and then |
2013 | (Is_Immediately_Visible (Scope (DT)) | |
2014 | or else | |
2015 | (Is_Child_Unit (Scope (DT)) | |
bff469f7 AC |
2016 | and then Is_Visible_Lib_Unit (Scope (DT))) |
2017 | or else In_Instance_Body) | |
0a36105d JM |
2018 | then |
2019 | Set_Etype (N, Available_View (DT)); | |
2020 | ||
2021 | else | |
2022 | Set_Etype (N, DT); | |
2023 | end if; | |
996ae0b0 RK |
2024 | end; |
2025 | ||
2026 | elsif Etype (P) /= Any_Type then | |
2027 | Error_Msg_N ("prefix of dereference must be an access type", N); | |
2028 | return; | |
2029 | end if; | |
2030 | ||
2031 | else | |
2032 | Get_First_Interp (P, I, It); | |
996ae0b0 RK |
2033 | while Present (It.Nam) loop |
2034 | T := It.Typ; | |
2035 | ||
2036 | if Is_Access_Type (T) then | |
2037 | Add_One_Interp (N, Designated_Type (T), Designated_Type (T)); | |
2038 | end if; | |
2039 | ||
2040 | Get_Next_Interp (I, It); | |
2041 | end loop; | |
2042 | ||
6e73e3ab | 2043 | -- Error if no interpretation of the prefix has an access type |
996ae0b0 RK |
2044 | |
2045 | if Etype (N) = Any_Type then | |
2046 | Error_Msg_N | |
2047 | ("access type required in prefix of explicit dereference", P); | |
2048 | Set_Etype (N, Any_Type); | |
2049 | return; | |
2050 | end if; | |
2051 | end if; | |
2052 | ||
2053 | if Is_Function_Type | |
2054 | and then Nkind (Parent (N)) /= N_Indexed_Component | |
2055 | ||
2056 | and then (Nkind (Parent (N)) /= N_Function_Call | |
2057 | or else N /= Name (Parent (N))) | |
2058 | ||
2059 | and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement | |
2060 | or else N /= Name (Parent (N))) | |
2061 | ||
2062 | and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration | |
2063 | and then (Nkind (Parent (N)) /= N_Attribute_Reference | |
2064 | or else | |
2065 | (Attribute_Name (Parent (N)) /= Name_Address | |
2066 | and then | |
2067 | Attribute_Name (Parent (N)) /= Name_Access)) | |
2068 | then | |
2069 | -- Name is a function call with no actuals, in a context that | |
2070 | -- requires deproceduring (including as an actual in an enclosing | |
98123480 | 2071 | -- function or procedure call). There are some pathological cases |
996ae0b0 RK |
2072 | -- where the prefix might include functions that return access to |
2073 | -- subprograms and others that return a regular type. Disambiguation | |
98123480 | 2074 | -- of those has to take place in Resolve. |
996ae0b0 RK |
2075 | |
2076 | New_N := | |
2077 | Make_Function_Call (Loc, | |
2078 | Name => Make_Explicit_Dereference (Loc, P), | |
2079 | Parameter_Associations => New_List); | |
2080 | ||
2081 | -- If the prefix is overloaded, remove operations that have formals, | |
2082 | -- we know that this is a parameterless call. | |
2083 | ||
2084 | if Is_Overloaded (P) then | |
2085 | Get_First_Interp (P, I, It); | |
996ae0b0 RK |
2086 | while Present (It.Nam) loop |
2087 | T := It.Typ; | |
2088 | ||
2089 | if No (First_Formal (Base_Type (Designated_Type (T)))) then | |
2090 | Set_Etype (P, T); | |
2091 | else | |
2092 | Remove_Interp (I); | |
2093 | end if; | |
2094 | ||
2095 | Get_Next_Interp (I, It); | |
2096 | end loop; | |
2097 | end if; | |
2098 | ||
2099 | Rewrite (N, New_N); | |
2100 | Analyze (N); | |
98123480 ES |
2101 | |
2102 | elsif not Is_Function_Type | |
2103 | and then Is_Overloaded (N) | |
2104 | then | |
2105 | -- The prefix may include access to subprograms and other access | |
1cb17b78 | 2106 | -- types. If the context selects the interpretation that is a |
56a7a3ab TQ |
2107 | -- function call (not a procedure call) we cannot rewrite the node |
2108 | -- yet, but we include the result of the call interpretation. | |
98123480 ES |
2109 | |
2110 | Get_First_Interp (N, I, It); | |
2111 | while Present (It.Nam) loop | |
2112 | if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type | |
2113 | and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type | |
1cb17b78 | 2114 | and then Nkind (Parent (N)) /= N_Procedure_Call_Statement |
98123480 ES |
2115 | then |
2116 | Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ)); | |
2117 | end if; | |
2118 | ||
2119 | Get_Next_Interp (I, It); | |
2120 | end loop; | |
996ae0b0 RK |
2121 | end if; |
2122 | ||
2123 | -- A value of remote access-to-class-wide must not be dereferenced | |
2124 | -- (RM E.2.2(16)). | |
2125 | ||
2126 | Validate_Remote_Access_To_Class_Wide_Type (N); | |
996ae0b0 RK |
2127 | end Analyze_Explicit_Dereference; |
2128 | ||
2129 | ------------------------ | |
2130 | -- Analyze_Expression -- | |
2131 | ------------------------ | |
2132 | ||
2133 | procedure Analyze_Expression (N : Node_Id) is | |
2134 | begin | |
5f50020a ES |
2135 | |
2136 | -- If the expression is an indexed component that will be rewritten | |
2137 | -- as a container indexing, it has already been analyzed. | |
2138 | ||
2139 | if Nkind (N) = N_Indexed_Component | |
2140 | and then Present (Generalized_Indexing (N)) | |
2141 | then | |
2142 | null; | |
2143 | ||
2144 | else | |
2145 | Analyze (N); | |
2146 | Check_Parameterless_Call (N); | |
2147 | end if; | |
996ae0b0 RK |
2148 | end Analyze_Expression; |
2149 | ||
955871d3 AC |
2150 | ------------------------------------- |
2151 | -- Analyze_Expression_With_Actions -- | |
2152 | ------------------------------------- | |
2153 | ||
2154 | procedure Analyze_Expression_With_Actions (N : Node_Id) is | |
2155 | A : Node_Id; | |
2156 | ||
2157 | begin | |
2158 | A := First (Actions (N)); | |
752b81d9 | 2159 | while Present (A) loop |
955871d3 AC |
2160 | Analyze (A); |
2161 | Next (A); | |
955871d3 AC |
2162 | end loop; |
2163 | ||
2ffcbaa5 AC |
2164 | Analyze_Expression (Expression (N)); |
2165 | Set_Etype (N, Etype (Expression (N))); | |
955871d3 AC |
2166 | end Analyze_Expression_With_Actions; |
2167 | ||
9b16cb57 RD |
2168 | --------------------------- |
2169 | -- Analyze_If_Expression -- | |
2170 | --------------------------- | |
2171 | ||
2172 | procedure Analyze_If_Expression (N : Node_Id) is | |
2173 | Condition : constant Node_Id := First (Expressions (N)); | |
2174 | Then_Expr : constant Node_Id := Next (Condition); | |
2175 | Else_Expr : Node_Id; | |
2176 | ||
2177 | begin | |
2178 | -- Defend against error of missing expressions from previous error | |
2179 | ||
2180 | if No (Then_Expr) then | |
ee2ba856 | 2181 | Check_Error_Detected; |
9b16cb57 RD |
2182 | return; |
2183 | end if; | |
2184 | ||
08988ed9 | 2185 | if Comes_From_Source (N) then |
ce5ba43a | 2186 | Check_SPARK_05_Restriction ("if expression is not allowed", N); |
08988ed9 | 2187 | end if; |
9b16cb57 RD |
2188 | |
2189 | Else_Expr := Next (Then_Expr); | |
2190 | ||
2191 | if Comes_From_Source (N) then | |
c86cf714 | 2192 | Check_Compiler_Unit ("if expression", N); |
9b16cb57 RD |
2193 | end if; |
2194 | ||
ac072cb2 AC |
2195 | -- Analyze and resolve the condition. We need to resolve this now so |
2196 | -- that it gets folded to True/False if possible, before we analyze | |
2197 | -- the THEN/ELSE branches, because when analyzing these branches, we | |
2198 | -- may call Is_Statically_Unevaluated, which expects the condition of | |
2199 | -- an enclosing IF to have been analyze/resolved/evaluated. | |
2200 | ||
9b16cb57 | 2201 | Analyze_Expression (Condition); |
ac072cb2 AC |
2202 | Resolve (Condition, Any_Boolean); |
2203 | ||
2204 | -- Analyze THEN expression and (if present) ELSE expression. For those | |
2205 | -- we delay resolution in the normal manner, because of overloading etc. | |
2206 | ||
9b16cb57 RD |
2207 | Analyze_Expression (Then_Expr); |
2208 | ||
2209 | if Present (Else_Expr) then | |
2210 | Analyze_Expression (Else_Expr); | |
2211 | end if; | |
2212 | ||
2213 | -- If then expression not overloaded, then that decides the type | |
2214 | ||
2215 | if not Is_Overloaded (Then_Expr) then | |
2216 | Set_Etype (N, Etype (Then_Expr)); | |
2217 | ||
2218 | -- Case where then expression is overloaded | |
2219 | ||
2220 | else | |
2221 | declare | |
2222 | I : Interp_Index; | |
2223 | It : Interp; | |
2224 | ||
2225 | begin | |
2226 | Set_Etype (N, Any_Type); | |
2227 | ||
bc795e3e | 2228 | -- Loop through interpretations of Then_Expr |
9b16cb57 RD |
2229 | |
2230 | Get_First_Interp (Then_Expr, I, It); | |
445e5888 | 2231 | while Present (It.Nam) loop |
9b16cb57 | 2232 | |
bc795e3e | 2233 | -- Add possible interpretation of Then_Expr if no Else_Expr, or |
0c6826a5 | 2234 | -- Else_Expr is present and has a compatible type. |
9b16cb57 | 2235 | |
445e5888 AC |
2236 | if No (Else_Expr) |
2237 | or else Has_Compatible_Type (Else_Expr, It.Typ) | |
2238 | then | |
2239 | Add_One_Interp (N, It.Typ, It.Typ); | |
2240 | end if; | |
9b16cb57 | 2241 | |
445e5888 AC |
2242 | Get_Next_Interp (I, It); |
2243 | end loop; | |
7408c4a5 | 2244 | |
bc38dbb4 AC |
2245 | -- If no valid interpretation has been found, then the type of the |
2246 | -- ELSE expression does not match any interpretation of the THEN | |
2247 | -- expression. | |
7408c4a5 AC |
2248 | |
2249 | if Etype (N) = Any_Type then | |
2250 | Error_Msg_N | |
2251 | ("type incompatible with that of `THEN` expression", | |
2252 | Else_Expr); | |
2253 | return; | |
2254 | end if; | |
9b16cb57 RD |
2255 | end; |
2256 | end if; | |
2257 | end Analyze_If_Expression; | |
2258 | ||
996ae0b0 RK |
2259 | ------------------------------------ |
2260 | -- Analyze_Indexed_Component_Form -- | |
2261 | ------------------------------------ | |
2262 | ||
2263 | procedure Analyze_Indexed_Component_Form (N : Node_Id) is | |
fbf5a39b AC |
2264 | P : constant Node_Id := Prefix (N); |
2265 | Exprs : constant List_Id := Expressions (N); | |
2266 | Exp : Node_Id; | |
2267 | P_T : Entity_Id; | |
2268 | E : Node_Id; | |
2269 | U_N : Entity_Id; | |
996ae0b0 RK |
2270 | |
2271 | procedure Process_Function_Call; | |
0c6826a5 AC |
2272 | -- Prefix in indexed component form is an overloadable entity, so the |
2273 | -- node is a function call. Reformat it as such. | |
996ae0b0 RK |
2274 | |
2275 | procedure Process_Indexed_Component; | |
2276 | -- Prefix in indexed component form is actually an indexed component. | |
2277 | -- This routine processes it, knowing that the prefix is already | |
2278 | -- resolved. | |
2279 | ||
2280 | procedure Process_Indexed_Component_Or_Slice; | |
2281 | -- An indexed component with a single index may designate a slice if | |
2282 | -- the index is a subtype mark. This routine disambiguates these two | |
2283 | -- cases by resolving the prefix to see if it is a subtype mark. | |
2284 | ||
2285 | procedure Process_Overloaded_Indexed_Component; | |
2286 | -- If the prefix of an indexed component is overloaded, the proper | |
2287 | -- interpretation is selected by the index types and the context. | |
2288 | ||
2289 | --------------------------- | |
2290 | -- Process_Function_Call -- | |
2291 | --------------------------- | |
2292 | ||
2293 | procedure Process_Function_Call is | |
1725676d | 2294 | Loc : constant Source_Ptr := Sloc (N); |
f5afb270 AC |
2295 | Actual : Node_Id; |
2296 | ||
996ae0b0 RK |
2297 | begin |
2298 | Change_Node (N, N_Function_Call); | |
2299 | Set_Name (N, P); | |
2300 | Set_Parameter_Associations (N, Exprs); | |
996ae0b0 | 2301 | |
401093c1 | 2302 | -- Analyze actuals prior to analyzing the call itself |
0a36105d | 2303 | |
4c46b835 | 2304 | Actual := First (Parameter_Associations (N)); |
996ae0b0 RK |
2305 | while Present (Actual) loop |
2306 | Analyze (Actual); | |
2307 | Check_Parameterless_Call (Actual); | |
0a36105d JM |
2308 | |
2309 | -- Move to next actual. Note that we use Next, not Next_Actual | |
2310 | -- here. The reason for this is a bit subtle. If a function call | |
0c6826a5 AC |
2311 | -- includes named associations, the parser recognizes the node |
2312 | -- as a call, and it is analyzed as such. If all associations are | |
0a36105d JM |
2313 | -- positional, the parser builds an indexed_component node, and |
2314 | -- it is only after analysis of the prefix that the construct | |
2315 | -- is recognized as a call, in which case Process_Function_Call | |
2316 | -- rewrites the node and analyzes the actuals. If the list of | |
2317 | -- actuals is malformed, the parser may leave the node as an | |
2318 | -- indexed component (despite the presence of named associations). | |
2319 | -- The iterator Next_Actual is equivalent to Next if the list is | |
2320 | -- positional, but follows the normalized chain of actuals when | |
2321 | -- named associations are present. In this case normalization has | |
2322 | -- not taken place, and actuals remain unanalyzed, which leads to | |
2323 | -- subsequent crashes or loops if there is an attempt to continue | |
2324 | -- analysis of the program. | |
2325 | ||
1725676d AC |
2326 | -- IF there is a single actual and it is a type name, the node |
2327 | -- can only be interpreted as a slice of a parameterless call. | |
2328 | -- Rebuild the node as such and analyze. | |
2329 | ||
2330 | if No (Next (Actual)) | |
2331 | and then Is_Entity_Name (Actual) | |
2332 | and then Is_Type (Entity (Actual)) | |
2333 | and then Is_Discrete_Type (Entity (Actual)) | |
2334 | then | |
2335 | Replace (N, | |
adc876a8 AC |
2336 | Make_Slice (Loc, |
2337 | Prefix => P, | |
2338 | Discrete_Range => | |
2339 | New_Occurrence_Of (Entity (Actual), Loc))); | |
1725676d AC |
2340 | Analyze (N); |
2341 | return; | |
2342 | ||
2343 | else | |
2344 | Next (Actual); | |
2345 | end if; | |
996ae0b0 RK |
2346 | end loop; |
2347 | ||
2348 | Analyze_Call (N); | |
2349 | end Process_Function_Call; | |
2350 | ||
2351 | ------------------------------- | |
2352 | -- Process_Indexed_Component -- | |
2353 | ------------------------------- | |
2354 | ||
2355 | procedure Process_Indexed_Component is | |
fe39cf20 BD |
2356 | Exp : Node_Id; |
2357 | Array_Type : Entity_Id; | |
2358 | Index : Node_Id; | |
2359 | Pent : Entity_Id := Empty; | |
996ae0b0 RK |
2360 | |
2361 | begin | |
2362 | Exp := First (Exprs); | |
2363 | ||
2364 | if Is_Overloaded (P) then | |
2365 | Process_Overloaded_Indexed_Component; | |
2366 | ||
2367 | else | |
2368 | Array_Type := Etype (P); | |
2369 | ||
6e73e3ab AC |
2370 | if Is_Entity_Name (P) then |
2371 | Pent := Entity (P); | |
2372 | elsif Nkind (P) = N_Selected_Component | |
2373 | and then Is_Entity_Name (Selector_Name (P)) | |
2374 | then | |
2375 | Pent := Entity (Selector_Name (P)); | |
2376 | end if; | |
2377 | ||
2378 | -- Prefix must be appropriate for an array type, taking into | |
2379 | -- account a possible implicit dereference. | |
996ae0b0 RK |
2380 | |
2381 | if Is_Access_Type (Array_Type) then | |
324ac540 AC |
2382 | Error_Msg_NW |
2383 | (Warn_On_Dereference, "?d?implicit dereference", N); | |
d469eabe | 2384 | Array_Type := Process_Implicit_Dereference_Prefix (Pent, P); |
996ae0b0 RK |
2385 | end if; |
2386 | ||
2387 | if Is_Array_Type (Array_Type) then | |
2388 | null; | |
2389 | ||
6e73e3ab | 2390 | elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then |
996ae0b0 RK |
2391 | Analyze (Exp); |
2392 | Set_Etype (N, Any_Type); | |
2393 | ||
2394 | if not Has_Compatible_Type | |
6e73e3ab | 2395 | (Exp, Entry_Index_Type (Pent)) |
996ae0b0 RK |
2396 | then |
2397 | Error_Msg_N ("invalid index type in entry name", N); | |
2398 | ||
2399 | elsif Present (Next (Exp)) then | |
2400 | Error_Msg_N ("too many subscripts in entry reference", N); | |
2401 | ||
2402 | else | |
2403 | Set_Etype (N, Etype (P)); | |
2404 | end if; | |
2405 | ||
2406 | return; | |
2407 | ||
2408 | elsif Is_Record_Type (Array_Type) | |
2409 | and then Remote_AST_I_Dereference (P) | |
2410 | then | |
2411 | return; | |
2412 | ||
50878404 | 2413 | elsif Try_Container_Indexing (N, P, Exprs) then |
d50f4827 AC |
2414 | return; |
2415 | ||
996ae0b0 RK |
2416 | elsif Array_Type = Any_Type then |
2417 | Set_Etype (N, Any_Type); | |
6465b6a7 AC |
2418 | |
2419 | -- In most cases the analysis of the prefix will have emitted | |
2420 | -- an error already, but if the prefix may be interpreted as a | |
2421 | -- call in prefixed notation, the report is left to the caller. | |
2422 | -- To prevent cascaded errors, report only if no previous ones. | |
2423 | ||
2424 | if Serious_Errors_Detected = 0 then | |
2425 | Error_Msg_N ("invalid prefix in indexed component", P); | |
2426 | ||
2427 | if Nkind (P) = N_Expanded_Name then | |
2428 | Error_Msg_NE ("\& is not visible", P, Selector_Name (P)); | |
2429 | end if; | |
2430 | end if; | |
2431 | ||
996ae0b0 RK |
2432 | return; |
2433 | ||
2434 | -- Here we definitely have a bad indexing | |
2435 | ||
2436 | else | |
2437 | if Nkind (Parent (N)) = N_Requeue_Statement | |
6e73e3ab | 2438 | and then Present (Pent) and then Ekind (Pent) = E_Entry |
996ae0b0 RK |
2439 | then |
2440 | Error_Msg_N | |
2441 | ("REQUEUE does not permit parameters", First (Exprs)); | |
2442 | ||
2443 | elsif Is_Entity_Name (P) | |
2444 | and then Etype (P) = Standard_Void_Type | |
2445 | then | |
0c6826a5 | 2446 | Error_Msg_NE ("incorrect use of &", P, Entity (P)); |
996ae0b0 RK |
2447 | |
2448 | else | |
2449 | Error_Msg_N ("array type required in indexed component", P); | |
2450 | end if; | |
2451 | ||
2452 | Set_Etype (N, Any_Type); | |
2453 | return; | |
2454 | end if; | |
2455 | ||
2456 | Index := First_Index (Array_Type); | |
996ae0b0 RK |
2457 | while Present (Index) and then Present (Exp) loop |
2458 | if not Has_Compatible_Type (Exp, Etype (Index)) then | |
2459 | Wrong_Type (Exp, Etype (Index)); | |
2460 | Set_Etype (N, Any_Type); | |
2461 | return; | |
2462 | end if; | |
2463 | ||
2464 | Next_Index (Index); | |
2465 | Next (Exp); | |
2466 | end loop; | |
2467 | ||
2468 | Set_Etype (N, Component_Type (Array_Type)); | |
44a10091 | 2469 | Check_Implicit_Dereference (N, Etype (N)); |
996ae0b0 RK |
2470 | |
2471 | if Present (Index) then | |
2472 | Error_Msg_N | |
2473 | ("too few subscripts in array reference", First (Exprs)); | |
2474 | ||
2475 | elsif Present (Exp) then | |
2476 | Error_Msg_N ("too many subscripts in array reference", Exp); | |
2477 | end if; | |
2478 | end if; | |
996ae0b0 RK |
2479 | end Process_Indexed_Component; |
2480 | ||
2481 | ---------------------------------------- | |
2482 | -- Process_Indexed_Component_Or_Slice -- | |
2483 | ---------------------------------------- | |
2484 | ||
2485 | procedure Process_Indexed_Component_Or_Slice is | |
2486 | begin | |
2487 | Exp := First (Exprs); | |
996ae0b0 RK |
2488 | while Present (Exp) loop |
2489 | Analyze_Expression (Exp); | |
2490 | Next (Exp); | |
2491 | end loop; | |
2492 | ||
2493 | Exp := First (Exprs); | |
2494 | ||
0c6826a5 AC |
2495 | -- If one index is present, and it is a subtype name, then the node |
2496 | -- denotes a slice (note that the case of an explicit range for a | |
2497 | -- slice was already built as an N_Slice node in the first place, | |
2498 | -- so that case is not handled here). | |
996ae0b0 RK |
2499 | |
2500 | -- We use a replace rather than a rewrite here because this is one | |
2501 | -- of the cases in which the tree built by the parser is plain wrong. | |
2502 | ||
2503 | if No (Next (Exp)) | |
2504 | and then Is_Entity_Name (Exp) | |
2505 | and then Is_Type (Entity (Exp)) | |
2506 | then | |
2507 | Replace (N, | |
2508 | Make_Slice (Sloc (N), | |
2509 | Prefix => P, | |
2510 | Discrete_Range => New_Copy (Exp))); | |
2511 | Analyze (N); | |
2512 | ||
2513 | -- Otherwise (more than one index present, or single index is not | |
2514 | -- a subtype name), then we have the indexed component case. | |
2515 | ||
2516 | else | |
2517 | Process_Indexed_Component; | |
2518 | end if; | |
2519 | end Process_Indexed_Component_Or_Slice; | |
2520 | ||
2521 | ------------------------------------------ | |
2522 | -- Process_Overloaded_Indexed_Component -- | |
2523 | ------------------------------------------ | |
2524 | ||
2525 | procedure Process_Overloaded_Indexed_Component is | |
2526 | Exp : Node_Id; | |
2527 | I : Interp_Index; | |
2528 | It : Interp; | |
2529 | Typ : Entity_Id; | |
2530 | Index : Node_Id; | |
2531 | Found : Boolean; | |
2532 | ||
2533 | begin | |
2534 | Set_Etype (N, Any_Type); | |
996ae0b0 | 2535 | |
4c46b835 | 2536 | Get_First_Interp (P, I, It); |
996ae0b0 RK |
2537 | while Present (It.Nam) loop |
2538 | Typ := It.Typ; | |
2539 | ||
2540 | if Is_Access_Type (Typ) then | |
2541 | Typ := Designated_Type (Typ); | |
324ac540 AC |
2542 | Error_Msg_NW |
2543 | (Warn_On_Dereference, "?d?implicit dereference", N); | |
996ae0b0 RK |
2544 | end if; |
2545 | ||
2546 | if Is_Array_Type (Typ) then | |
2547 | ||
2548 | -- Got a candidate: verify that index types are compatible | |
2549 | ||
2550 | Index := First_Index (Typ); | |
2551 | Found := True; | |
996ae0b0 | 2552 | Exp := First (Exprs); |
996ae0b0 RK |
2553 | while Present (Index) and then Present (Exp) loop |
2554 | if Has_Compatible_Type (Exp, Etype (Index)) then | |
2555 | null; | |
2556 | else | |
2557 | Found := False; | |
2558 | Remove_Interp (I); | |
2559 | exit; | |
2560 | end if; | |
2561 | ||
2562 | Next_Index (Index); | |
2563 | Next (Exp); | |
2564 | end loop; | |
2565 | ||
2566 | if Found and then No (Index) and then No (Exp) then | |
44a10091 AC |
2567 | declare |
2568 | CT : constant Entity_Id := | |
2569 | Base_Type (Component_Type (Typ)); | |
2570 | begin | |
2571 | Add_One_Interp (N, CT, CT); | |
2572 | Check_Implicit_Dereference (N, CT); | |
2573 | end; | |
996ae0b0 | 2574 | end if; |
57a8057a | 2575 | |
50878404 | 2576 | elsif Try_Container_Indexing (N, P, Exprs) then |
57a8057a AC |
2577 | return; |
2578 | ||
996ae0b0 RK |
2579 | end if; |
2580 | ||
2581 | Get_Next_Interp (I, It); | |
2582 | end loop; | |
2583 | ||
2584 | if Etype (N) = Any_Type then | |
ad6b5b00 | 2585 | Error_Msg_N ("no legal interpretation for indexed component", N); |
996ae0b0 RK |
2586 | Set_Is_Overloaded (N, False); |
2587 | end if; | |
2588 | ||
2589 | End_Interp_List; | |
2590 | end Process_Overloaded_Indexed_Component; | |
2591 | ||
4c46b835 | 2592 | -- Start of processing for Analyze_Indexed_Component_Form |
996ae0b0 RK |
2593 | |
2594 | begin | |
2595 | -- Get name of array, function or type | |
2596 | ||
2597 | Analyze (P); | |
d469eabe | 2598 | |
24778dbb AC |
2599 | -- If P is an explicit dereference whose prefix is of a remote access- |
2600 | -- to-subprogram type, then N has already been rewritten as a subprogram | |
2601 | -- call and analyzed. | |
2602 | ||
d3b00ce3 | 2603 | if Nkind (N) in N_Subprogram_Call then |
24778dbb | 2604 | return; |
d469eabe | 2605 | |
24778dbb AC |
2606 | -- When the prefix is attribute 'Loop_Entry and the sole expression of |
2607 | -- the indexed component denotes a loop name, the indexed form is turned | |
2608 | -- into an attribute reference. | |
fbf5a39b | 2609 | |
24778dbb AC |
2610 | elsif Nkind (N) = N_Attribute_Reference |
2611 | and then Attribute_Name (N) = Name_Loop_Entry | |
2612 | then | |
fbf5a39b AC |
2613 | return; |
2614 | end if; | |
2615 | ||
2616 | pragma Assert (Nkind (N) = N_Indexed_Component); | |
2617 | ||
996ae0b0 RK |
2618 | P_T := Base_Type (Etype (P)); |
2619 | ||
878f708a | 2620 | if Is_Entity_Name (P) and then Present (Entity (P)) then |
996ae0b0 RK |
2621 | U_N := Entity (P); |
2622 | ||
aab883ec | 2623 | if Is_Type (U_N) then |
996ae0b0 | 2624 | |
4c46b835 | 2625 | -- Reformat node as a type conversion |
996ae0b0 RK |
2626 | |
2627 | E := Remove_Head (Exprs); | |
2628 | ||
2629 | if Present (First (Exprs)) then | |
2630 | Error_Msg_N | |
2631 | ("argument of type conversion must be single expression", N); | |
2632 | end if; | |
2633 | ||
2634 | Change_Node (N, N_Type_Conversion); | |
2635 | Set_Subtype_Mark (N, P); | |
2636 | Set_Etype (N, U_N); | |
2637 | Set_Expression (N, E); | |
2638 | ||
2639 | -- After changing the node, call for the specific Analysis | |
2640 | -- routine directly, to avoid a double call to the expander. | |
2641 | ||
2642 | Analyze_Type_Conversion (N); | |
2643 | return; | |
2644 | end if; | |
2645 | ||
2646 | if Is_Overloadable (U_N) then | |
2647 | Process_Function_Call; | |
2648 | ||
2649 | elsif Ekind (Etype (P)) = E_Subprogram_Type | |
2650 | or else (Is_Access_Type (Etype (P)) | |
2651 | and then | |
bce79204 AC |
2652 | Ekind (Designated_Type (Etype (P))) = |
2653 | E_Subprogram_Type) | |
996ae0b0 RK |
2654 | then |
2655 | -- Call to access_to-subprogram with possible implicit dereference | |
2656 | ||
2657 | Process_Function_Call; | |
2658 | ||
fbf5a39b AC |
2659 | elsif Is_Generic_Subprogram (U_N) then |
2660 | ||
4c46b835 | 2661 | -- A common beginner's (or C++ templates fan) error |
996ae0b0 RK |
2662 | |
2663 | Error_Msg_N ("generic subprogram cannot be called", N); | |
2664 | Set_Etype (N, Any_Type); | |
2665 | return; | |
2666 | ||
2667 | else | |
2668 | Process_Indexed_Component_Or_Slice; | |
2669 | end if; | |
2670 | ||
2671 | -- If not an entity name, prefix is an expression that may denote | |
2672 | -- an array or an access-to-subprogram. | |
2673 | ||
2674 | else | |
fbf5a39b | 2675 | if Ekind (P_T) = E_Subprogram_Type |
996ae0b0 RK |
2676 | or else (Is_Access_Type (P_T) |
2677 | and then | |
bce79204 | 2678 | Ekind (Designated_Type (P_T)) = E_Subprogram_Type) |
996ae0b0 RK |
2679 | then |
2680 | Process_Function_Call; | |
2681 | ||
2682 | elsif Nkind (P) = N_Selected_Component | |
3d918396 | 2683 | and then Present (Entity (Selector_Name (P))) |
ffe9aba8 | 2684 | and then Is_Overloadable (Entity (Selector_Name (P))) |
996ae0b0 RK |
2685 | then |
2686 | Process_Function_Call; | |
2687 | ||
3d918396 AC |
2688 | -- In ASIS mode within a generic, a prefixed call is analyzed and |
2689 | -- partially rewritten but the original indexed component has not | |
2690 | -- yet been rewritten as a call. Perform the replacement now. | |
2691 | ||
2692 | elsif Nkind (P) = N_Selected_Component | |
2693 | and then Nkind (Parent (P)) = N_Function_Call | |
2694 | and then ASIS_Mode | |
2695 | then | |
2696 | Rewrite (N, Parent (P)); | |
2697 | Analyze (N); | |
2698 | ||
996ae0b0 RK |
2699 | else |
2700 | -- Indexed component, slice, or a call to a member of a family | |
2701 | -- entry, which will be converted to an entry call later. | |
fbf5a39b | 2702 | |
996ae0b0 RK |
2703 | Process_Indexed_Component_Or_Slice; |
2704 | end if; | |
2705 | end if; | |
5f49133f AC |
2706 | |
2707 | Analyze_Dimension (N); | |
996ae0b0 RK |
2708 | end Analyze_Indexed_Component_Form; |
2709 | ||
2710 | ------------------------ | |
2711 | -- Analyze_Logical_Op -- | |
2712 | ------------------------ | |
2713 | ||
2714 | procedure Analyze_Logical_Op (N : Node_Id) is | |
2715 | L : constant Node_Id := Left_Opnd (N); | |
2716 | R : constant Node_Id := Right_Opnd (N); | |
2717 | Op_Id : Entity_Id := Entity (N); | |
2718 | ||
2719 | begin | |
2720 | Set_Etype (N, Any_Type); | |
2721 | Candidate_Type := Empty; | |
2722 | ||
2723 | Analyze_Expression (L); | |
2724 | Analyze_Expression (R); | |
2725 | ||
2726 | if Present (Op_Id) then | |
2727 | ||
2728 | if Ekind (Op_Id) = E_Operator then | |
2729 | Find_Boolean_Types (L, R, Op_Id, N); | |
2730 | else | |
2731 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
2732 | end if; | |
2733 | ||
2734 | else | |
2735 | Op_Id := Get_Name_Entity_Id (Chars (N)); | |
996ae0b0 RK |
2736 | while Present (Op_Id) loop |
2737 | if Ekind (Op_Id) = E_Operator then | |
2738 | Find_Boolean_Types (L, R, Op_Id, N); | |
2739 | else | |
2740 | Analyze_User_Defined_Binary_Op (N, Op_Id); | |
2741 | end if; | |
2742 | ||
2743 | Op_Id := Homonym (Op_Id); | |
2744 | end loop; | |
2745 | end if; | |
2746 | ||
2747 | Operator_Check (N); | |
22e89283 | 2748 | Check_Function_Writable_Actuals (N); |
996ae0b0 RK |
2749 | end Analyze_Logical_Op; |
2750 | ||
2751 | --------------------------- | |
2752 | -- Analyze_Membership_Op -- | |
2753 | --------------------------- | |
2754 | ||
2755 | procedure Analyze_Membership_Op (N : Node_Id) is | |
66150d01 | 2756 | Loc : constant Source_Ptr := Sloc (N); |
f2acf80c AC |
2757 | L : constant Node_Id := Left_Opnd (N); |
2758 | R : constant Node_Id := Right_Opnd (N); | |
996ae0b0 RK |
2759 | |
2760 | Index : Interp_Index; | |
2761 | It : Interp; | |
2762 | Found : Boolean := False; | |
2763 | I_F : Interp_Index; | |
2764 | T_F : Entity_Id; | |
2765 | ||
2766 | procedure Try_One_Interp (T1 : Entity_Id); | |
2767 | -- Routine to try one proposed interpretation. Note that the context | |
2768 | -- of the operation plays no role in resolving the arguments, so that | |
2769 | -- if there is more than one interpretation of the operands that is | |
2770 | -- compatible with a membership test, the operation is ambiguous. | |
2771 | ||
4c46b835 AC |
2772 | -------------------- |
2773 | -- Try_One_Interp -- | |
2774 | -------------------- | |
2775 | ||
996ae0b0 RK |
2776 | procedure Try_One_Interp (T1 : Entity_Id) is |
2777 | begin | |
2778 | if Has_Compatible_Type (R, T1) then | |
2779 | if Found | |
2780 | and then Base_Type (T1) /= Base_Type (T_F) | |
2781 | then | |
2782 | It := Disambiguate (L, I_F, Index, Any_Type); | |
2783 | ||
2784 | if It = No_Interp then | |
2785 | Ambiguous_Operands (N); | |
2786 | Set_Etype (L, Any_Type); | |
2787 | return; | |
2788 | ||
2789 | else | |
2790 | T_F := It.Typ; | |
2791 | end if; | |
2792 | ||
2793 | else | |
2794 | Found := True; | |
2795 | T_F := T1; | |
2796 | I_F := Index; | |
2797 | end if; | |
2798 | ||
2799 | Set_Etype (L, T_F); | |
2800 | end if; | |
996ae0b0 RK |
2801 | end Try_One_Interp; |
2802 | ||
197e4514 AC |
2803 | procedure Analyze_Set_Membership; |
2804 | -- If a set of alternatives is present, analyze each and find the | |
2805 | -- common type to which they must all resolve. | |
2806 | ||
2807 | ---------------------------- | |
2808 | -- Analyze_Set_Membership -- | |
2809 | ---------------------------- | |
2810 | ||
2811 | procedure Analyze_Set_Membership is | |
2812 | Alt : Node_Id; | |
2813 | Index : Interp_Index; | |
2814 | It : Interp; | |
197e4514 AC |
2815 | Candidate_Interps : Node_Id; |
2816 | Common_Type : Entity_Id := Empty; | |
2817 | ||
2818 | begin | |
e917e3b8 | 2819 | if Comes_From_Source (N) then |
c86cf714 | 2820 | Check_Compiler_Unit ("set membership", N); |
e917e3b8 AC |
2821 | end if; |
2822 | ||
197e4514 AC |
2823 | Analyze (L); |
2824 | Candidate_Interps := L; | |
2825 | ||
2826 | if not Is_Overloaded (L) then | |
2827 | Common_Type := Etype (L); | |
2828 | ||
2829 | Alt := First (Alternatives (N)); | |
2830 | while Present (Alt) loop | |
2831 | Analyze (Alt); | |
2832 | ||
2833 | if not Has_Compatible_Type (Alt, Common_Type) then | |
2834 | Wrong_Type (Alt, Common_Type); | |
2835 | end if; | |
2836 | ||
2837 | Next (Alt); | |
2838 | end loop; | |
2839 | ||
2840 | else | |
2841 | Alt := First (Alternatives (N)); | |
2842 | while Present (Alt) loop | |
2843 | Analyze (Alt); | |
2844 | if not Is_Overloaded (Alt) then | |
2845 | Common_Type := Etype (Alt); | |
2846 | ||
2847 | else | |
2848 | Get_First_Interp (Alt, Index, It); | |
2849 | while Present (It.Typ) loop | |
442c0581 RD |
2850 | if not |
2851 | Has_Compatible_Type (Candidate_Interps, It.Typ) | |
197e4514 AC |
2852 | then |
2853 | Remove_Interp (Index); | |
2854 | end if; | |
442c0581 | 2855 | |
197e4514 AC |
2856 | Get_Next_Interp (Index, It); |
2857 | end loop; | |
2858 | ||
2859 | Get_First_Interp (Alt, Index, It); | |
442c0581 | 2860 | |
197e4514 AC |
2861 | if No (It.Typ) then |
2862 | Error_Msg_N ("alternative has no legal type", Alt); | |
2863 | return; | |
2864 | end if; | |
2865 | ||
442c0581 RD |
2866 | -- If alternative is not overloaded, we have a unique type |
2867 | -- for all of them. | |
197e4514 AC |
2868 | |
2869 | Set_Etype (Alt, It.Typ); | |
2870 | Get_Next_Interp (Index, It); | |
2871 | ||
2872 | if No (It.Typ) then | |
2873 | Set_Is_Overloaded (Alt, False); | |
2874 | Common_Type := Etype (Alt); | |
2875 | end if; | |
2876 | ||
2877 | Candidate_Interps := Alt; | |
2878 | end if; | |
2879 | ||
2880 | Next (Alt); | |
2881 | end loop; | |
2882 | end if; | |
2883 | ||
2884 | Set_Etype (N, Standard_Boolean); | |
2885 | ||
2886 | if Present (Common_Type) then | |
2887 | Set_Etype (L, Common_Type); | |
cd1a470a AC |
2888 | |
2889 | -- The left operand may still be overloaded, to be resolved using | |
2890 | -- the Common_Type. | |
197e4514 AC |
2891 | |
2892 | else | |
2893 | Error_Msg_N ("cannot resolve membership operation", N); | |
2894 | end if; | |
2895 | end Analyze_Set_Membership; | |
2896 | ||
996ae0b0 RK |
2897 | -- Start of processing for Analyze_Membership_Op |
2898 | ||
2899 | begin | |
2900 | Analyze_Expression (L); | |
2901 | ||
e917e3b8 | 2902 | if No (R) and then Ada_Version >= Ada_2012 then |
197e4514 | 2903 | Analyze_Set_Membership; |
22e89283 | 2904 | Check_Function_Writable_Actuals (N); |
288cbbbd | 2905 | |
197e4514 AC |
2906 | return; |
2907 | end if; | |
2908 | ||
996ae0b0 RK |
2909 | if Nkind (R) = N_Range |
2910 | or else (Nkind (R) = N_Attribute_Reference | |
2911 | and then Attribute_Name (R) = Name_Range) | |
2912 | then | |
2913 | Analyze (R); | |
2914 | ||
2915 | if not Is_Overloaded (L) then | |
2916 | Try_One_Interp (Etype (L)); | |
2917 | ||
2918 | else | |
2919 | Get_First_Interp (L, Index, It); | |
996ae0b0 RK |
2920 | while Present (It.Typ) loop |
2921 | Try_One_Interp (It.Typ); | |
2922 | Get_Next_Interp (Index, It); | |
2923 | end loop; | |
2924 | end if; | |
2925 | ||
f6b5dc8e | 2926 | -- If not a range, it can be a subtype mark, or else it is a degenerate |
b0186f71 AC |
2927 | -- membership test with a singleton value, i.e. a test for equality, |
2928 | -- if the types are compatible. | |
996ae0b0 RK |
2929 | |
2930 | else | |
66150d01 | 2931 | Analyze (R); |
7483c888 | 2932 | |
66150d01 AC |
2933 | if Is_Entity_Name (R) |
2934 | and then Is_Type (Entity (R)) | |
2935 | then | |
2936 | Find_Type (R); | |
996ae0b0 | 2937 | Check_Fully_Declared (Entity (R), R); |
66150d01 | 2938 | |
b0186f71 AC |
2939 | elsif Ada_Version >= Ada_2012 |
2940 | and then Has_Compatible_Type (R, Etype (L)) | |
2941 | then | |
66150d01 AC |
2942 | if Nkind (N) = N_In then |
2943 | Rewrite (N, | |
2944 | Make_Op_Eq (Loc, | |
2945 | Left_Opnd => L, | |
2946 | Right_Opnd => R)); | |
2947 | else | |
2948 | Rewrite (N, | |
2949 | Make_Op_Ne (Loc, | |
2950 | Left_Opnd => L, | |
2951 | Right_Opnd => R)); | |
2952 | end if; | |
2953 | ||
2954 | Analyze (N); | |
2955 | return; | |
2956 | ||
2957 | else | |
b0186f71 AC |
2958 | -- In all versions of the language, if we reach this point there |
2959 | -- is a previous error that will be diagnosed below. | |
66150d01 AC |
2960 | |
2961 | Find_Type (R); | |
996ae0b0 RK |
2962 | end if; |
2963 | end if; | |
2964 | ||
2965 | -- Compatibility between expression and subtype mark or range is | |
2966 | -- checked during resolution. The result of the operation is Boolean | |
2967 | -- in any case. | |
2968 | ||
2969 | Set_Etype (N, Standard_Boolean); | |
fe45e59e ES |
2970 | |
2971 | if Comes_From_Source (N) | |
197e4514 | 2972 | and then Present (Right_Opnd (N)) |
fe45e59e ES |
2973 | and then Is_CPP_Class (Etype (Etype (Right_Opnd (N)))) |
2974 | then | |
2975 | Error_Msg_N ("membership test not applicable to cpp-class types", N); | |
2976 | end if; | |
288cbbbd | 2977 | |
22e89283 | 2978 | Check_Function_Writable_Actuals (N); |
996ae0b0 RK |
2979 | end Analyze_Membership_Op; |
2980 | ||
b727a82b AC |
2981 | ----------------- |
2982 | -- Analyze_Mod -- | |
2983 | ----------------- | |
2984 | ||
2985 | procedure Analyze_Mod (N : Node_Id) is | |
2986 | begin | |
2987 | -- A special warning check, if we have an expression of the form: | |
2988 | -- expr mod 2 * literal | |
2989 | -- where literal is 64 or less, then probably what was meant was | |
2990 | -- expr mod 2 ** literal | |
2991 | -- so issue an appropriate warning. | |
2992 | ||
2993 | if Warn_On_Suspicious_Modulus_Value | |
2994 | and then Nkind (Right_Opnd (N)) = N_Integer_Literal | |
2995 | and then Intval (Right_Opnd (N)) = Uint_2 | |
2996 | and then Nkind (Parent (N)) = N_Op_Multiply | |
2997 | and then Nkind (Right_Opnd (Parent (N))) = N_Integer_Literal | |
2998 | and then Intval (Right_Opnd (Parent (N))) <= Uint_64 | |
2999 | then | |
3000 | Error_Msg_N | |
324ac540 | 3001 | ("suspicious MOD value, was '*'* intended'??M?", Parent (N)); |
b727a82b AC |
3002 | end if; |
3003 | ||
3004 | -- Remaining processing is same as for other arithmetic operators | |
3005 | ||
3006 | Analyze_Arithmetic_Op (N); | |
3007 | end Analyze_Mod; | |
3008 | ||
996ae0b0 RK |
3009 | ---------------------- |
3010 | -- Analyze_Negation -- | |
3011 | ---------------------- | |
3012 | ||
3013 | procedure Analyze_Negation (N : Node_Id) is | |
3014 | R : constant Node_Id := Right_Opnd (N); | |
3015 | Op_Id : Entity_Id := Entity (N); | |
3016 | ||
3017 | begin | |
3018 | Set_Etype (N, Any_Type); | |
3019 | Candidate_Type := Empty; | |
3020 | ||
3021 | Analyze_Expression (R); | |
3022 | ||
3023 | if Present (Op_Id) then | |
3024 | if Ekind (Op_Id) = E_Operator then | |
3025 | Find_Negation_Types (R, Op_Id, N); | |
3026 | else | |
3027 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
3028 | end if; | |
3029 | ||
3030 | else | |
3031 | Op_Id := Get_Name_Entity_Id (Chars (N)); | |
996ae0b0 RK |
3032 | while Present (Op_Id) loop |
3033 | if Ekind (Op_Id) = E_Operator then | |
3034 | Find_Negation_Types (R, Op_Id, N); | |
3035 | else | |
3036 | Analyze_User_Defined_Unary_Op (N, Op_Id); | |
3037 | end if; | |
3038 | ||
3039 | Op_Id := Homonym (Op_Id); | |
3040 | end loop; | |
3041 | end if; | |
3042 | ||
3043 | Operator_Check (N); | |
3044 | end Analyze_Negation; | |
3045 | ||
15ce9ca2 AC |
3046 | ------------------ |
3047 | -- Analyze_Null -- | |
3048 | ------------------ | |
996ae0b0 RK |
3049 | |
3050 | procedure Analyze_Null (N : Node_Id) is | |
3051 | begin | |
ce5ba43a | 3052 | Check_SPARK_05_Restriction ("null is not allowed", N); |
1d801f21 | 3053 | |
996ae0b0 RK |
3054 | Set_Etype (N, Any_Access); |
3055 | end Analyze_Null; | |
3056 | ||
3057 | ---------------------- | |
3058 | -- Analyze_One_Call -- | |
3059 | ---------------------- | |
3060 | ||
3061 | procedure Analyze_One_Call | |
ec6078e3 ES |
3062 | (N : Node_Id; |
3063 | Nam : Entity_Id; | |
3064 | Report : Boolean; | |
3065 | Success : out Boolean; | |
3066 | Skip_First : Boolean := False) | |
996ae0b0 | 3067 | is |
d469eabe HK |
3068 | Actuals : constant List_Id := Parameter_Associations (N); |
3069 | Prev_T : constant Entity_Id := Etype (N); | |
3070 | ||
aab883ec ES |
3071 | Must_Skip : constant Boolean := Skip_First |
3072 | or else Nkind (Original_Node (N)) = N_Selected_Component | |
3073 | or else | |
3074 | (Nkind (Original_Node (N)) = N_Indexed_Component | |
3075 | and then Nkind (Prefix (Original_Node (N))) | |
3076 | = N_Selected_Component); | |
3077 | -- The first formal must be omitted from the match when trying to find | |
3078 | -- a primitive operation that is a possible interpretation, and also | |
3079 | -- after the call has been rewritten, because the corresponding actual | |
3080 | -- is already known to be compatible, and because this may be an | |
3081 | -- indexing of a call with default parameters. | |
3082 | ||
53cf4600 ES |
3083 | Formal : Entity_Id; |
3084 | Actual : Node_Id; | |
3085 | Is_Indexed : Boolean := False; | |
3086 | Is_Indirect : Boolean := False; | |
3087 | Subp_Type : constant Entity_Id := Etype (Nam); | |
3088 | Norm_OK : Boolean; | |
996ae0b0 | 3089 | |
1d2d8a8f AC |
3090 | function Compatible_Types_In_Predicate |
3091 | (T1 : Entity_Id; | |
3092 | T2 : Entity_Id) return Boolean; | |
3093 | -- For an Ada 2012 predicate or invariant, a call may mention an | |
3094 | -- incomplete type, while resolution of the corresponding predicate | |
3095 | -- function may see the full view, as a consequence of the delayed | |
3096 | -- resolution of the corresponding expressions. This may occur in | |
3097 | -- the body of a predicate function, or in a call to such. Anomalies | |
3098 | -- involving private and full views can also happen. In each case, | |
3099 | -- rewrite node or add conversions to remove spurious type errors. | |
3100 | ||
3101 | procedure Indicate_Name_And_Type; | |
3102 | -- If candidate interpretation matches, indicate name and type of result | |
3103 | -- on call node. | |
3104 | ||
157a9bf5 ES |
3105 | function Operator_Hidden_By (Fun : Entity_Id) return Boolean; |
3106 | -- There may be a user-defined operator that hides the current | |
3107 | -- interpretation. We must check for this independently of the | |
3108 | -- analysis of the call with the user-defined operation, because | |
3109 | -- the parameter names may be wrong and yet the hiding takes place. | |
3110 | -- This fixes a problem with ACATS test B34014O. | |
3111 | -- | |
3112 | -- When the type Address is a visible integer type, and the DEC | |
3113 | -- system extension is visible, the predefined operator may be | |
3114 | -- hidden as well, by one of the address operations in auxdec. | |
3115 | -- Finally, The abstract operations on address do not hide the | |
3116 | -- predefined operator (this is the purpose of making them abstract). | |
3117 | ||
1d2d8a8f AC |
3118 | ----------------------------------- |
3119 | -- Compatible_Types_In_Predicate -- | |
3120 | ----------------------------------- | |
3121 | ||
3122 | function Compatible_Types_In_Predicate | |
3123 | (T1 : Entity_Id; | |
3124 | T2 : Entity_Id) return Boolean | |
3125 | is | |
3126 | function Common_Type (T : Entity_Id) return Entity_Id; | |
3127 | -- Find non-private full view if any, without going to ancestor type | |
3128 | -- (as opposed to Underlying_Type). | |
3129 | ||
3130 | ----------------- | |
3131 | -- Common_Type -- | |
3132 | ----------------- | |
3133 | ||
3134 | function Common_Type (T : Entity_Id) return Entity_Id is | |
3135 | begin | |
3136 | if Is_Private_Type (T) and then Present (Full_View (T)) then | |
3137 | return Base_Type (Full_View (T)); | |
3138 | else | |
3139 | return Base_Type (T); | |
3140 | end if; | |
3141 | end Common_Type; | |
3142 | ||
3143 | -- Start of processing for Compatible_Types_In_Predicate | |
3144 | ||
3145 | begin | |
3146 | if (Ekind (Current_Scope) = E_Function | |
3147 | and then Is_Predicate_Function (Current_Scope)) | |
3148 | or else | |
3149 | (Ekind (Nam) = E_Function | |
3150 | and then Is_Predicate_Function (Nam)) | |
3151 | then | |
3152 | if Is_Incomplete_Type (T1) | |
3153 | and then Present (Full_View (T1)) | |
3154 | and then Full_View (T1) = T2 | |
3155 | then | |
3156 | Set_Etype (Formal, Etype (Actual)); | |
3157 | return True; | |
3158 | ||
3159 | elsif Common_Type (T1) = Common_Type (T2) then | |
3160 | Rewrite (Actual, Unchecked_Convert_To (Etype (Formal), Actual)); | |
3161 | return True; | |
3162 | ||
3163 | else | |
3164 | return False; | |
3165 | end if; | |
3166 | ||
3167 | else | |
3168 | return False; | |
3169 | end if; | |
3170 | end Compatible_Types_In_Predicate; | |
996ae0b0 | 3171 | |
fbf5a39b AC |
3172 | ---------------------------- |
3173 | -- Indicate_Name_And_Type -- | |
3174 | ---------------------------- | |
996ae0b0 | 3175 | |
fbf5a39b | 3176 | procedure Indicate_Name_And_Type is |
996ae0b0 RK |
3177 | begin |
3178 | Add_One_Interp (N, Nam, Etype (Nam)); | |
44a10091 | 3179 | Check_Implicit_Dereference (N, Etype (Nam)); |
996ae0b0 RK |
3180 | Success := True; |
3181 | ||
3182 | -- If the prefix of the call is a name, indicate the entity | |
3183 | -- being called. If it is not a name, it is an expression that | |
3184 | -- denotes an access to subprogram or else an entry or family. In | |
3185 | -- the latter case, the name is a selected component, and the entity | |
3186 | -- being called is noted on the selector. | |
3187 | ||
3188 | if not Is_Type (Nam) then | |
a3f2babd | 3189 | if Is_Entity_Name (Name (N)) then |
996ae0b0 | 3190 | Set_Entity (Name (N), Nam); |
d9307840 | 3191 | Set_Etype (Name (N), Etype (Nam)); |
996ae0b0 RK |
3192 | |
3193 | elsif Nkind (Name (N)) = N_Selected_Component then | |
3194 | Set_Entity (Selector_Name (Name (N)), Nam); | |
3195 | end if; | |
3196 | end if; | |
3197 | ||
3198 | if Debug_Flag_E and not Report then | |
3199 | Write_Str (" Overloaded call "); | |
3200 | Write_Int (Int (N)); | |
3201 | Write_Str (" compatible with "); | |
3202 | Write_Int (Int (Nam)); | |
3203 | Write_Eol; | |
3204 | end if; | |
fbf5a39b | 3205 | end Indicate_Name_And_Type; |
996ae0b0 | 3206 | |
157a9bf5 ES |
3207 | ------------------------ |
3208 | -- Operator_Hidden_By -- | |
3209 | ------------------------ | |
3210 | ||
3211 | function Operator_Hidden_By (Fun : Entity_Id) return Boolean is | |
3212 | Act1 : constant Node_Id := First_Actual (N); | |
3213 | Act2 : constant Node_Id := Next_Actual (Act1); | |
3214 | Form1 : constant Entity_Id := First_Formal (Fun); | |
3215 | Form2 : constant Entity_Id := Next_Formal (Form1); | |
3216 | ||
3217 | begin | |
e4deba8e | 3218 | if Ekind (Fun) /= E_Function or else Is_Abstract_Subprogram (Fun) then |
157a9bf5 ES |
3219 | return False; |
3220 | ||
3221 | elsif not Has_Compatible_Type (Act1, Etype (Form1)) then | |
3222 | return False; | |
3223 | ||
3224 | elsif Present (Form2) then | |
e4deba8e RD |
3225 | if No (Act2) |
3226 | or else not Has_Compatible_Type (Act2, Etype (Form2)) | |
157a9bf5 ES |
3227 | then |
3228 | return False; | |
3229 | end if; | |
3230 | ||
3231 | elsif Present (Act2) then | |
3232 | return False; | |
3233 | end if; | |
3234 | ||
3235 | -- Now we know that the arity of the operator matches the function, | |
3236 | -- and the function call is a valid interpretation. The function | |
3237 | -- hides the operator if it has the right signature, or if one of | |
3238 | -- its operands is a non-abstract operation on Address when this is | |
3239 | -- a visible integer type. | |
3240 | ||
3241 | return Hides_Op (Fun, Nam) | |
d9d25d04 | 3242 | or else Is_Descendant_Of_Address (Etype (Form1)) |
157a9bf5 ES |
3243 | or else |
3244 | (Present (Form2) | |
d9d25d04 | 3245 | and then Is_Descendant_Of_Address (Etype (Form2))); |
157a9bf5 ES |
3246 | end Operator_Hidden_By; |
3247 | ||
996ae0b0 RK |
3248 | -- Start of processing for Analyze_One_Call |
3249 | ||
3250 | begin | |
3251 | Success := False; | |
3252 | ||
157a9bf5 ES |
3253 | -- If the subprogram has no formals or if all the formals have defaults, |
3254 | -- and the return type is an array type, the node may denote an indexing | |
3255 | -- of the result of a parameterless call. In Ada 2005, the subprogram | |
3256 | -- may have one non-defaulted formal, and the call may have been written | |
3257 | -- in prefix notation, so that the rebuilt parameter list has more than | |
3258 | -- one actual. | |
996ae0b0 | 3259 | |
53cf4600 ES |
3260 | if not Is_Overloadable (Nam) |
3261 | and then Ekind (Nam) /= E_Subprogram_Type | |
3262 | and then Ekind (Nam) /= E_Entry_Family | |
3263 | then | |
3264 | return; | |
3265 | end if; | |
3266 | ||
80e59506 | 3267 | -- An indexing requires at least one actual. The name of the call cannot |
4bb9c7b9 AC |
3268 | -- be an implicit indirect call, so it cannot be a generated explicit |
3269 | -- dereference. | |
e1f3cb58 AC |
3270 | |
3271 | if not Is_Empty_List (Actuals) | |
aab883ec ES |
3272 | and then |
3273 | (Needs_No_Actuals (Nam) | |
3274 | or else | |
3275 | (Needs_One_Actual (Nam) | |
e4deba8e | 3276 | and then Present (Next_Actual (First (Actuals))))) |
996ae0b0 | 3277 | then |
4bb9c7b9 AC |
3278 | if Is_Array_Type (Subp_Type) |
3279 | and then | |
3280 | (Nkind (Name (N)) /= N_Explicit_Dereference | |
3281 | or else Comes_From_Source (Name (N))) | |
3282 | then | |
aab883ec | 3283 | Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip); |
996ae0b0 RK |
3284 | |
3285 | elsif Is_Access_Type (Subp_Type) | |
3286 | and then Is_Array_Type (Designated_Type (Subp_Type)) | |
3287 | then | |
3288 | Is_Indexed := | |
aab883ec ES |
3289 | Try_Indexed_Call |
3290 | (N, Nam, Designated_Type (Subp_Type), Must_Skip); | |
996ae0b0 | 3291 | |
758c442c | 3292 | -- The prefix can also be a parameterless function that returns an |
f3d57416 | 3293 | -- access to subprogram, in which case this is an indirect call. |
53cf4600 ES |
3294 | -- If this succeeds, an explicit dereference is added later on, |
3295 | -- in Analyze_Call or Resolve_Call. | |
758c442c | 3296 | |
996ae0b0 | 3297 | elsif Is_Access_Type (Subp_Type) |
401093c1 | 3298 | and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type |
996ae0b0 | 3299 | then |
53cf4600 | 3300 | Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type); |
996ae0b0 RK |
3301 | end if; |
3302 | ||
3303 | end if; | |
3304 | ||
5ff22245 | 3305 | -- If the call has been transformed into a slice, it is of the form |
30783513 | 3306 | -- F (Subtype) where F is parameterless. The node has been rewritten in |
5ff22245 ES |
3307 | -- Try_Indexed_Call and there is nothing else to do. |
3308 | ||
3309 | if Is_Indexed | |
21d7ef70 | 3310 | and then Nkind (N) = N_Slice |
5ff22245 ES |
3311 | then |
3312 | return; | |
3313 | end if; | |
3314 | ||
53cf4600 ES |
3315 | Normalize_Actuals |
3316 | (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK); | |
996ae0b0 RK |
3317 | |
3318 | if not Norm_OK then | |
3319 | ||
53cf4600 | 3320 | -- If an indirect call is a possible interpretation, indicate |
80e59506 | 3321 | -- success to the caller. This may be an indexing of an explicit |
4bb9c7b9 | 3322 | -- dereference of a call that returns an access type (see above). |
53cf4600 | 3323 | |
4bb9c7b9 AC |
3324 | if Is_Indirect |
3325 | or else (Is_Indexed | |
3326 | and then Nkind (Name (N)) = N_Explicit_Dereference | |
3327 | and then Comes_From_Source (Name (N))) | |
3328 | then | |
53cf4600 ES |
3329 | Success := True; |
3330 | return; | |
3331 | ||
996ae0b0 RK |
3332 | -- Mismatch in number or names of parameters |
3333 | ||
53cf4600 | 3334 | elsif Debug_Flag_E then |
996ae0b0 RK |
3335 | Write_Str (" normalization fails in call "); |
3336 | Write_Int (Int (N)); | |
3337 | Write_Str (" with subprogram "); | |
3338 | Write_Int (Int (Nam)); | |
3339 | Write_Eol; | |
3340 | end if; | |
3341 | ||
3342 | -- If the context expects a function call, discard any interpretation | |
3343 | -- that is a procedure. If the node is not overloaded, leave as is for | |
3344 | -- better error reporting when type mismatch is found. | |
3345 | ||
3346 | elsif Nkind (N) = N_Function_Call | |
3347 | and then Is_Overloaded (Name (N)) | |
3348 | and then Ekind (Nam) = E_Procedure | |
3349 | then | |
3350 | return; | |
3351 | ||
4c46b835 | 3352 | -- Ditto for function calls in a procedure context |
996ae0b0 RK |
3353 | |
3354 | elsif Nkind (N) = N_Procedure_Call_Statement | |
3355 | and then Is_Overloaded (Name (N)) | |
3356 | and then Etype (Nam) /= Standard_Void_Type | |
3357 | then | |
3358 | return; | |
3359 | ||
fe45e59e | 3360 | elsif No (Actuals) then |
996ae0b0 RK |
3361 | |
3362 | -- If Normalize succeeds, then there are default parameters for | |
3363 | -- all formals. | |
3364 | ||
fbf5a39b | 3365 | Indicate_Name_And_Type; |
996ae0b0 RK |
3366 | |
3367 | elsif Ekind (Nam) = E_Operator then | |
996ae0b0 RK |
3368 | if Nkind (N) = N_Procedure_Call_Statement then |
3369 | return; | |
3370 | end if; | |
3371 | ||
3372 | -- This can occur when the prefix of the call is an operator | |
3373 | -- name or an expanded name whose selector is an operator name. | |
3374 | ||
3375 | Analyze_Operator_Call (N, Nam); | |
3376 | ||
3377 | if Etype (N) /= Prev_T then | |
3378 | ||
157a9bf5 | 3379 | -- Check that operator is not hidden by a function interpretation |
996ae0b0 RK |
3380 | |
3381 | if Is_Overloaded (Name (N)) then | |
3382 | declare | |
3383 | I : Interp_Index; | |
3384 | It : Interp; | |
3385 | ||
3386 | begin | |
3387 | Get_First_Interp (Name (N), I, It); | |
996ae0b0 | 3388 | while Present (It.Nam) loop |
157a9bf5 | 3389 | if Operator_Hidden_By (It.Nam) then |
996ae0b0 RK |
3390 | Set_Etype (N, Prev_T); |
3391 | return; | |
3392 | end if; | |
3393 | ||
3394 | Get_Next_Interp (I, It); | |
3395 | end loop; | |
3396 | end; | |
3397 | end if; | |
3398 | ||
3399 | -- If operator matches formals, record its name on the call. | |
3400 | -- If the operator is overloaded, Resolve will select the | |
3401 | -- correct one from the list of interpretations. The call | |
3402 | -- node itself carries the first candidate. | |
3403 | ||
3404 | Set_Entity (Name (N), Nam); | |
3405 | Success := True; | |
3406 | ||
3407 | elsif Report and then Etype (N) = Any_Type then | |
3408 | Error_Msg_N ("incompatible arguments for operator", N); | |
3409 | end if; | |
3410 | ||
3411 | else | |
3412 | -- Normalize_Actuals has chained the named associations in the | |
3413 | -- correct order of the formals. | |
3414 | ||
3415 | Actual := First_Actual (N); | |
3416 | Formal := First_Formal (Nam); | |
ec6078e3 | 3417 | |
df3e68b1 HK |
3418 | -- If we are analyzing a call rewritten from object notation, skip |
3419 | -- first actual, which may be rewritten later as an explicit | |
3420 | -- dereference. | |
ec6078e3 | 3421 | |
aab883ec | 3422 | if Must_Skip then |
ec6078e3 ES |
3423 | Next_Actual (Actual); |
3424 | Next_Formal (Formal); | |
3425 | end if; | |
3426 | ||
996ae0b0 | 3427 | while Present (Actual) and then Present (Formal) loop |
fbf5a39b AC |
3428 | if Nkind (Parent (Actual)) /= N_Parameter_Association |
3429 | or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal) | |
996ae0b0 | 3430 | then |
9c510803 ES |
3431 | -- The actual can be compatible with the formal, but we must |
3432 | -- also check that the context is not an address type that is | |
7a5b62b0 | 3433 | -- visibly an integer type. In this case the use of literals is |
d9d25d04 | 3434 | -- illegal, except in the body of descendants of system, where |
7a5b62b0 | 3435 | -- arithmetic operations on address are of course used. |
9c510803 ES |
3436 | |
3437 | if Has_Compatible_Type (Actual, Etype (Formal)) | |
3438 | and then | |
3439 | (Etype (Actual) /= Universal_Integer | |
d9d25d04 | 3440 | or else not Is_Descendant_Of_Address (Etype (Formal)) |
9c510803 ES |
3441 | or else |
3442 | Is_Predefined_File_Name | |
3443 | (Unit_File_Name (Get_Source_Unit (N)))) | |
3444 | then | |
996ae0b0 RK |
3445 | Next_Actual (Actual); |
3446 | Next_Formal (Formal); | |
3447 | ||
061828e3 AC |
3448 | -- In Allow_Integer_Address mode, we allow an actual integer to |
3449 | -- match a formal address type and vice versa. We only do this | |
3450 | -- if we are certain that an error will otherwise be issued | |
3451 | ||
3452 | elsif Address_Integer_Convert_OK | |
3453 | (Etype (Actual), Etype (Formal)) | |
3454 | and then (Report and not Is_Indexed and not Is_Indirect) | |
3455 | then | |
3456 | -- Handle this case by introducing an unchecked conversion | |
3457 | ||
3458 | Rewrite (Actual, | |
3459 | Unchecked_Convert_To (Etype (Formal), | |
3460 | Relocate_Node (Actual))); | |
3461 | Analyze_And_Resolve (Actual, Etype (Formal)); | |
3462 | Next_Actual (Actual); | |
3463 | Next_Formal (Formal); | |
3464 | ||
a8a42b93 AC |
3465 | -- Under relaxed RM semantics silently replace occurrences of |
3466 | -- null by System.Address_Null. We only do this if we know that | |
3467 | -- an error will otherwise be issued. | |
3468 | ||
3469 | elsif Null_To_Null_Address_Convert_OK (Actual, Etype (Formal)) | |
3470 | and then (Report and not Is_Indexed and not Is_Indirect) | |
3471 | then | |
3472 | Replace_Null_By_Null_Address (Actual); | |
3473 | Analyze_And_Resolve (Actual, Etype (Formal)); | |
3474 | Next_Actual (Actual); | |
3475 | Next_Formal (Formal); | |
3476 | ||
1d2d8a8f AC |
3477 | elsif Compatible_Types_In_Predicate |
3478 | (Etype (Formal), Etype (Actual)) | |
a921e83c | 3479 | then |
a921e83c AC |
3480 | Next_Actual (Actual); |
3481 | Next_Formal (Formal); | |
3482 | ||
bb072d1c AC |
3483 | -- In a complex case where an enclosing generic and a nested |
3484 | -- generic package, both declared with partially parameterized | |
3485 | -- formal subprograms with the same names, are instantiated | |
3486 | -- with the same type, the types of the actual parameter and | |
3487 | -- that of the formal may appear incompatible at first sight. | |
3488 | ||
3489 | -- generic | |
3490 | -- type Outer_T is private; | |
3491 | -- with function Func (Formal : Outer_T) | |
3492 | -- return ... is <>; | |
3493 | ||
3494 | -- package Outer_Gen is | |
3495 | -- generic | |
3496 | -- type Inner_T is private; | |
3497 | -- with function Func (Formal : Inner_T) -- (1) | |
d030f3a4 | 3498 | -- return ... is <>; |
bb072d1c AC |
3499 | |
3500 | -- package Inner_Gen is | |
3501 | -- function Inner_Func (Formal : Inner_T) -- (2) | |
d030f3a4 | 3502 | -- return ... is (Func (Formal)); |
bb072d1c AC |
3503 | -- end Inner_Gen; |
3504 | -- end Outer_Generic; | |
3505 | ||
3506 | -- package Outer_Inst is new Outer_Gen (Actual_T); | |
3507 | -- package Inner_Inst is new Outer_Inst.Inner_Gen (Actual_T); | |
3508 | ||
3509 | -- In the example above, the type of parameter | |
3510 | -- Inner_Func.Formal at (2) is incompatible with the type of | |
3511 | -- Func.Formal at (1) in the context of instantiations | |
d030f3a4 AC |
3512 | -- Outer_Inst and Inner_Inst. In reality both types are generic |
3513 | -- actual subtypes renaming base type Actual_T as part of the | |
3514 | -- generic prologues for the instantiations. | |
3515 | ||
3516 | -- Recognize this case and add a type conversion to allow this | |
3517 | -- kind of generic actual subtype conformance. Note that this | |
3518 | -- is done only when the call is non-overloaded because the | |
3519 | -- resolution mechanism already has the means to disambiguate | |
3520 | -- similar cases. | |
bb072d1c AC |
3521 | |
3522 | elsif not Is_Overloaded (Name (N)) | |
3523 | and then Is_Type (Etype (Actual)) | |
3524 | and then Is_Type (Etype (Formal)) | |
3525 | and then Is_Generic_Actual_Type (Etype (Actual)) | |
3526 | and then Is_Generic_Actual_Type (Etype (Formal)) | |
3527 | and then Base_Type (Etype (Actual)) = | |
3528 | Base_Type (Etype (Formal)) | |
3529 | then | |
3530 | Rewrite (Actual, | |
3531 | Convert_To (Etype (Formal), Relocate_Node (Actual))); | |
3532 | Analyze_And_Resolve (Actual, Etype (Formal)); | |
3533 | Next_Actual (Actual); | |
3534 | Next_Formal (Formal); | |
3535 | ||
3536 | -- Handle failed type check | |
3537 | ||
996ae0b0 RK |
3538 | else |
3539 | if Debug_Flag_E then | |
3540 | Write_Str (" type checking fails in call "); | |
3541 | Write_Int (Int (N)); | |
3542 | Write_Str (" with formal "); | |
3543 | Write_Int (Int (Formal)); | |
3544 | Write_Str (" in subprogram "); | |
3545 | Write_Int (Int (Nam)); | |
3546 | Write_Eol; | |
3547 | end if; | |
3548 | ||
061828e3 AC |
3549 | -- Comment needed on the following test??? |
3550 | ||
53cf4600 | 3551 | if Report and not Is_Indexed and not Is_Indirect then |
758c442c GD |
3552 | |
3553 | -- Ada 2005 (AI-251): Complete the error notification | |
8f2eeab7 | 3554 | -- to help new Ada 2005 users. |
758c442c GD |
3555 | |
3556 | if Is_Class_Wide_Type (Etype (Formal)) | |
3557 | and then Is_Interface (Etype (Etype (Formal))) | |
3558 | and then not Interface_Present_In_Ancestor | |
3559 | (Typ => Etype (Actual), | |
3560 | Iface => Etype (Etype (Formal))) | |
3561 | then | |
758c442c | 3562 | Error_Msg_NE |
ec6078e3 | 3563 | ("(Ada 2005) does not implement interface }", |
758c442c GD |
3564 | Actual, Etype (Etype (Formal))); |
3565 | end if; | |
3566 | ||
996ae0b0 RK |
3567 | Wrong_Type (Actual, Etype (Formal)); |
3568 | ||
3569 | if Nkind (Actual) = N_Op_Eq | |
3570 | and then Nkind (Left_Opnd (Actual)) = N_Identifier | |
3571 | then | |
3572 | Formal := First_Formal (Nam); | |
996ae0b0 | 3573 | while Present (Formal) loop |
996ae0b0 | 3574 | if Chars (Left_Opnd (Actual)) = Chars (Formal) then |
4e7a4f6e | 3575 | Error_Msg_N -- CODEFIX |
fbf5a39b | 3576 | ("possible misspelling of `='>`!", Actual); |
996ae0b0 RK |
3577 | exit; |
3578 | end if; | |
3579 | ||
3580 | Next_Formal (Formal); | |
3581 | end loop; | |
3582 | end if; | |
3583 | ||
3584 | if All_Errors_Mode then | |
3585 | Error_Msg_Sloc := Sloc (Nam); | |
3586 | ||
3b42c566 RD |
3587 | if Etype (Formal) = Any_Type then |
3588 | Error_Msg_N | |
3589 | ("there is no legal actual parameter", Actual); | |
3590 | end if; | |
3591 | ||
996ae0b0 RK |
3592 | if Is_Overloadable (Nam) |
3593 | and then Present (Alias (Nam)) | |
3594 | and then not Comes_From_Source (Nam) | |
3595 | then | |
3596 | Error_Msg_NE | |
401093c1 ES |
3597 | ("\\ =='> in call to inherited operation & #!", |
3598 | Actual, Nam); | |
7324bf49 AC |
3599 | |
3600 | elsif Ekind (Nam) = E_Subprogram_Type then | |
3601 | declare | |
3602 | Access_To_Subprogram_Typ : | |
3603 | constant Entity_Id := | |
3604 | Defining_Identifier | |
3605 | (Associated_Node_For_Itype (Nam)); | |
3606 | begin | |
a90bd866 RD |
3607 | Error_Msg_NE |
3608 | ("\\ =='> in call to dereference of &#!", | |
3609 | Actual, Access_To_Subprogram_Typ); | |
7324bf49 AC |
3610 | end; |
3611 | ||
996ae0b0 | 3612 | else |
401093c1 ES |
3613 | Error_Msg_NE |
3614 | ("\\ =='> in call to &#!", Actual, Nam); | |
7324bf49 | 3615 | |
996ae0b0 RK |
3616 | end if; |
3617 | end if; | |
3618 | end if; | |
3619 | ||
3620 | return; | |
3621 | end if; | |
3622 | ||
3623 | else | |
3624 | -- Normalize_Actuals has verified that a default value exists | |
3625 | -- for this formal. Current actual names a subsequent formal. | |
3626 | ||
3627 | Next_Formal (Formal); | |
3628 | end if; | |
3629 | end loop; | |
3630 | ||
4c46b835 | 3631 | -- On exit, all actuals match |
996ae0b0 | 3632 | |
fbf5a39b | 3633 | Indicate_Name_And_Type; |
996ae0b0 RK |
3634 | end if; |
3635 | end Analyze_One_Call; | |
3636 | ||
15ce9ca2 AC |
3637 | --------------------------- |
3638 | -- Analyze_Operator_Call -- | |
3639 | --------------------------- | |
996ae0b0 RK |
3640 | |
3641 | procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is | |
3642 | Op_Name : constant Name_Id := Chars (Op_Id); | |
3643 | Act1 : constant Node_Id := First_Actual (N); | |
3644 | Act2 : constant Node_Id := Next_Actual (Act1); | |
3645 | ||
3646 | begin | |
4c46b835 AC |
3647 | -- Binary operator case |
3648 | ||
996ae0b0 RK |
3649 | if Present (Act2) then |
3650 | ||
4c46b835 | 3651 | -- If more than two operands, then not binary operator after all |
996ae0b0 RK |
3652 | |
3653 | if Present (Next_Actual (Act2)) then | |
996ae0b0 | 3654 | return; |
b7539c3b | 3655 | end if; |
996ae0b0 | 3656 | |
b7539c3b | 3657 | -- Otherwise action depends on operator |
996ae0b0 | 3658 | |
b7539c3b AC |
3659 | case Op_Name is |
3660 | when Name_Op_Add | | |
3661 | Name_Op_Subtract | | |
3662 | Name_Op_Multiply | | |
3663 | Name_Op_Divide | | |
3664 | Name_Op_Mod | | |
3665 | Name_Op_Rem | | |
3666 | Name_Op_Expon => | |
3667 | Find_Arithmetic_Types (Act1, Act2, Op_Id, N); | |
996ae0b0 | 3668 | |
b7539c3b AC |
3669 | when Name_Op_And | |
3670 | Name_Op_Or | | |
3671 | Name_Op_Xor => | |
3672 | Find_Boolean_Types (Act1, Act2, Op_Id, N); | |
996ae0b0 | 3673 | |
b7539c3b AC |
3674 | when Name_Op_Lt | |
3675 | Name_Op_Le | | |
3676 | Name_Op_Gt | | |
3677 | Name_Op_Ge => | |
3678 | Find_Comparison_Types (Act1, Act2, Op_Id, N); | |
996ae0b0 | 3679 | |
b7539c3b AC |
3680 | when Name_Op_Eq | |
3681 | Name_Op_Ne => | |
3682 | Find_Equality_Types (Act1, Act2, Op_Id, N); | |
996ae0b0 | 3683 | |
b7539c3b AC |
3684 | when Name_Op_Concat => |
3685 | Find_Concatenation_Types (Act1, Act2, Op_Id, N); | |
996ae0b0 | 3686 | |
b7539c3b AC |
3687 | -- Is this when others, or should it be an abort??? |
3688 | ||
3689 | when others => | |
3690 | null; | |
3691 | end case; | |
996ae0b0 | 3692 | |
4c46b835 | 3693 | -- Unary operator case |
996ae0b0 | 3694 | |
4c46b835 | 3695 | else |
b7539c3b AC |
3696 | case Op_Name is |
3697 | when Name_Op_Subtract | | |
3698 | Name_Op_Add | | |
3699 | Name_Op_Abs => | |
3700 | Find_Unary_Types (Act1, Op_Id, N); | |
996ae0b0 | 3701 | |
b7539c3b AC |
3702 | when Name_Op_Not => |
3703 | Find_Negation_Types (Act1, Op_Id, N); | |
996ae0b0 | 3704 | |
b7539c3b | 3705 | -- Is this when others correct, or should it be an abort??? |
996ae0b0 | 3706 | |
b7539c3b AC |
3707 | when others => |
3708 | null; | |
3709 | end case; | |
996ae0b0 RK |
3710 | end if; |
3711 | end Analyze_Operator_Call; | |
3712 | ||
3713 | ------------------------------------------- | |
3714 | -- Analyze_Overloaded_Selected_Component -- | |
3715 | ------------------------------------------- | |
3716 | ||
3717 | procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is | |
fbf5a39b AC |
3718 | Nam : constant Node_Id := Prefix (N); |
3719 | Sel : constant Node_Id := Selector_Name (N); | |
996ae0b0 | 3720 | Comp : Entity_Id; |
996ae0b0 RK |
3721 | I : Interp_Index; |
3722 | It : Interp; | |
3723 | T : Entity_Id; | |
3724 | ||
3725 | begin | |
4c46b835 | 3726 | Set_Etype (Sel, Any_Type); |
996ae0b0 | 3727 | |
4c46b835 | 3728 | Get_First_Interp (Nam, I, It); |
996ae0b0 RK |
3729 | while Present (It.Typ) loop |
3730 | if Is_Access_Type (It.Typ) then | |
3731 | T := Designated_Type (It.Typ); | |
324ac540 | 3732 | Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N); |
996ae0b0 RK |
3733 | else |
3734 | T := It.Typ; | |
3735 | end if; | |
3736 | ||
95eb8b69 AC |
3737 | -- Locate the component. For a private prefix the selector can denote |
3738 | -- a discriminant. | |
3739 | ||
3740 | if Is_Record_Type (T) or else Is_Private_Type (T) then | |
d469eabe HK |
3741 | |
3742 | -- If the prefix is a class-wide type, the visible components are | |
3743 | -- those of the base type. | |
3744 | ||
3745 | if Is_Class_Wide_Type (T) then | |
3746 | T := Etype (T); | |
3747 | end if; | |
3748 | ||
996ae0b0 | 3749 | Comp := First_Entity (T); |
996ae0b0 | 3750 | while Present (Comp) loop |
996ae0b0 RK |
3751 | if Chars (Comp) = Chars (Sel) |
3752 | and then Is_Visible_Component (Comp) | |
3753 | then | |
996ae0b0 | 3754 | |
f16d05d9 AC |
3755 | -- AI05-105: if the context is an object renaming with |
3756 | -- an anonymous access type, the expected type of the | |
3757 | -- object must be anonymous. This is a name resolution rule. | |
996ae0b0 | 3758 | |
f16d05d9 AC |
3759 | if Nkind (Parent (N)) /= N_Object_Renaming_Declaration |
3760 | or else No (Access_Definition (Parent (N))) | |
3761 | or else Ekind (Etype (Comp)) = E_Anonymous_Access_Type | |
3762 | or else | |
3763 | Ekind (Etype (Comp)) = E_Anonymous_Access_Subprogram_Type | |
3764 | then | |
3765 | Set_Entity (Sel, Comp); | |
3766 | Set_Etype (Sel, Etype (Comp)); | |
3767 | Add_One_Interp (N, Etype (Comp), Etype (Comp)); | |
44a10091 | 3768 | Check_Implicit_Dereference (N, Etype (Comp)); |
f16d05d9 AC |
3769 | |
3770 | -- This also specifies a candidate to resolve the name. | |
3771 | -- Further overloading will be resolved from context. | |
3772 | -- The selector name itself does not carry overloading | |
3773 | -- information. | |
3774 | ||
3775 | Set_Etype (Nam, It.Typ); | |
3776 | ||
3777 | else | |
b61ee1aa | 3778 | -- Named access type in the context of a renaming |
f16d05d9 AC |
3779 | -- declaration with an access definition. Remove |
3780 | -- inapplicable candidate. | |
3781 | ||
3782 | Remove_Interp (I); | |
3783 | end if; | |
996ae0b0 RK |
3784 | end if; |
3785 | ||
3786 | Next_Entity (Comp); | |
3787 | end loop; | |
3788 | ||
3789 | elsif Is_Concurrent_Type (T) then | |
3790 | Comp := First_Entity (T); | |
996ae0b0 RK |
3791 | while Present (Comp) |
3792 | and then Comp /= First_Private_Entity (T) | |
3793 | loop | |
3794 | if Chars (Comp) = Chars (Sel) then | |
3795 | if Is_Overloadable (Comp) then | |
3796 | Add_One_Interp (Sel, Comp, Etype (Comp)); | |
3797 | else | |
e7ba564f | 3798 | Set_Entity_With_Checks (Sel, Comp); |
996ae0b0 RK |
3799 | Generate_Reference (Comp, Sel); |
3800 | end if; | |
3801 | ||
3802 | Set_Etype (Sel, Etype (Comp)); | |
3803 | Set_Etype (N, Etype (Comp)); | |
3804 | Set_Etype (Nam, It.Typ); | |
3805 | ||
09494c32 AC |
3806 | -- For access type case, introduce explicit dereference for |
3807 | -- more uniform treatment of entry calls. Do this only once | |
3808 | -- if several interpretations yield an access type. | |
996ae0b0 | 3809 | |
d469eabe HK |
3810 | if Is_Access_Type (Etype (Nam)) |
3811 | and then Nkind (Nam) /= N_Explicit_Dereference | |
3812 | then | |
996ae0b0 | 3813 | Insert_Explicit_Dereference (Nam); |
fbf5a39b | 3814 | Error_Msg_NW |
324ac540 | 3815 | (Warn_On_Dereference, "?d?implicit dereference", N); |
996ae0b0 RK |
3816 | end if; |
3817 | end if; | |
3818 | ||
3819 | Next_Entity (Comp); | |
3820 | end loop; | |
3821 | ||
3822 | Set_Is_Overloaded (N, Is_Overloaded (Sel)); | |
996ae0b0 RK |
3823 | end if; |
3824 | ||
3825 | Get_Next_Interp (I, It); | |
3826 | end loop; | |
3827 | ||
0a36105d JM |
3828 | if Etype (N) = Any_Type |
3829 | and then not Try_Object_Operation (N) | |
3830 | then | |
996ae0b0 RK |
3831 | Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel); |
3832 | Set_Entity (Sel, Any_Id); | |
3833 | Set_Etype (Sel, Any_Type); | |
3834 | end if; | |
996ae0b0 RK |
3835 | end Analyze_Overloaded_Selected_Component; |
3836 | ||
3837 | ---------------------------------- | |
3838 | -- Analyze_Qualified_Expression -- | |
3839 | ---------------------------------- | |
3840 | ||
3841 | procedure Analyze_Qualified_Expression (N : Node_Id) is | |
3842 | Mark : constant Entity_Id := Subtype_Mark (N); | |
45c8b94b ES |
3843 | Expr : constant Node_Id := Expression (N); |
3844 | I : Interp_Index; | |
3845 | It : Interp; | |
996ae0b0 RK |
3846 | T : Entity_Id; |
3847 | ||
3848 | begin | |
45c8b94b ES |
3849 | Analyze_Expression (Expr); |
3850 | ||
996ae0b0 RK |
3851 | Set_Etype (N, Any_Type); |
3852 | Find_Type (Mark); | |
3853 | T := Entity (Mark); | |
45c8b94b | 3854 | Set_Etype (N, T); |
996ae0b0 RK |
3855 | |
3856 | if T = Any_Type then | |
3857 | return; | |
3858 | end if; | |
996ae0b0 | 3859 | |
4c46b835 | 3860 | Check_Fully_Declared (T, N); |
45c8b94b ES |
3861 | |
3862 | -- If expected type is class-wide, check for exact match before | |
3863 | -- expansion, because if the expression is a dispatching call it | |
3864 | -- may be rewritten as explicit dereference with class-wide result. | |
3865 | -- If expression is overloaded, retain only interpretations that | |
3866 | -- will yield exact matches. | |
3867 | ||
3868 | if Is_Class_Wide_Type (T) then | |
3869 | if not Is_Overloaded (Expr) then | |
9fe696a3 | 3870 | if Base_Type (Etype (Expr)) /= Base_Type (T) then |
45c8b94b ES |
3871 | if Nkind (Expr) = N_Aggregate then |
3872 | Error_Msg_N ("type of aggregate cannot be class-wide", Expr); | |
3873 | else | |
3874 | Wrong_Type (Expr, T); | |
3875 | end if; | |
3876 | end if; | |
3877 | ||
3878 | else | |
3879 | Get_First_Interp (Expr, I, It); | |
3880 | ||
3881 | while Present (It.Nam) loop | |
3882 | if Base_Type (It.Typ) /= Base_Type (T) then | |
3883 | Remove_Interp (I); | |
3884 | end if; | |
3885 | ||
3886 | Get_Next_Interp (I, It); | |
3887 | end loop; | |
3888 | end if; | |
3889 | end if; | |
3890 | ||
996ae0b0 RK |
3891 | Set_Etype (N, T); |
3892 | end Analyze_Qualified_Expression; | |
3893 | ||
a961aa79 AC |
3894 | ----------------------------------- |
3895 | -- Analyze_Quantified_Expression -- | |
3896 | ----------------------------------- | |
3897 | ||
3898 | procedure Analyze_Quantified_Expression (N : Node_Id) is | |
4856cc2a | 3899 | function Is_Empty_Range (Typ : Entity_Id) return Boolean; |
538dbb56 AC |
3900 | -- If the iterator is part of a quantified expression, and the range is |
3901 | -- known to be statically empty, emit a warning and replace expression | |
4856cc2a | 3902 | -- with its static value. Returns True if the replacement occurs. |
538dbb56 | 3903 | |
0812b84e AC |
3904 | function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean; |
3905 | -- Determine whether if expression If_Expr lacks an else part or if it | |
3906 | -- has one, it evaluates to True. | |
3907 | ||
4856cc2a ES |
3908 | -------------------- |
3909 | -- Is_Empty_Range -- | |
3910 | -------------------- | |
3911 | ||
3912 | function Is_Empty_Range (Typ : Entity_Id) return Boolean is | |
3913 | Loc : constant Source_Ptr := Sloc (N); | |
538dbb56 AC |
3914 | |
3915 | begin | |
3916 | if Is_Array_Type (Typ) | |
4856cc2a ES |
3917 | and then Compile_Time_Known_Bounds (Typ) |
3918 | and then | |
9a6dc470 RD |
3919 | (Expr_Value (Type_Low_Bound (Etype (First_Index (Typ)))) > |
3920 | Expr_Value (Type_High_Bound (Etype (First_Index (Typ))))) | |
538dbb56 | 3921 | then |
4856cc2a ES |
3922 | Preanalyze_And_Resolve (Condition (N), Standard_Boolean); |
3923 | ||
538dbb56 | 3924 | if All_Present (N) then |
4856cc2a | 3925 | Error_Msg_N |
324ac540 | 3926 | ("??quantified expression with ALL " |
4856cc2a | 3927 | & "over a null range has value True", N); |
538dbb56 AC |
3928 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); |
3929 | ||
3930 | else | |
4856cc2a | 3931 | Error_Msg_N |
324ac540 | 3932 | ("??quantified expression with SOME " |
4856cc2a | 3933 | & "over a null range has value False", N); |
538dbb56 AC |
3934 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); |
3935 | end if; | |
3936 | ||
3937 | Analyze (N); | |
3938 | return True; | |
3939 | ||
3940 | else | |
3941 | return False; | |
3942 | end if; | |
3943 | end Is_Empty_Range; | |
3944 | ||
0812b84e AC |
3945 | ----------------------------- |
3946 | -- No_Else_Or_Trivial_True -- | |
3947 | ----------------------------- | |
3948 | ||
3949 | function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean is | |
3950 | Else_Expr : constant Node_Id := | |
3951 | Next (Next (First (Expressions (If_Expr)))); | |
3952 | begin | |
3953 | return | |
3954 | No (Else_Expr) | |
3955 | or else (Compile_Time_Known_Value (Else_Expr) | |
3956 | and then Is_True (Expr_Value (Else_Expr))); | |
3957 | end No_Else_Or_Trivial_True; | |
3958 | ||
3959 | -- Local variables | |
3960 | ||
3961 | Cond : constant Node_Id := Condition (N); | |
57081559 | 3962 | Loop_Id : Entity_Id; |
0812b84e AC |
3963 | QE_Scop : Entity_Id; |
3964 | ||
4856cc2a ES |
3965 | -- Start of processing for Analyze_Quantified_Expression |
3966 | ||
a961aa79 | 3967 | begin |
ce5ba43a | 3968 | Check_SPARK_05_Restriction ("quantified expression is not allowed", N); |
1d801f21 | 3969 | |
804670f1 AC |
3970 | -- Create a scope to emulate the loop-like behavior of the quantified |
3971 | -- expression. The scope is needed to provide proper visibility of the | |
3972 | -- loop variable. | |
b3e42de5 | 3973 | |
804670f1 AC |
3974 | QE_Scop := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L'); |
3975 | Set_Etype (QE_Scop, Standard_Void_Type); | |
3976 | Set_Scope (QE_Scop, Current_Scope); | |
3977 | Set_Parent (QE_Scop, N); | |
a961aa79 | 3978 | |
804670f1 | 3979 | Push_Scope (QE_Scop); |
c56a9ba4 | 3980 | |
804670f1 AC |
3981 | -- All constituents are preanalyzed and resolved to avoid untimely |
3982 | -- generation of various temporaries and types. Full analysis and | |
3983 | -- expansion is carried out when the quantified expression is | |
3984 | -- transformed into an expression with actions. | |
c56a9ba4 | 3985 | |
804670f1 AC |
3986 | if Present (Iterator_Specification (N)) then |
3987 | Preanalyze (Iterator_Specification (N)); | |
538dbb56 | 3988 | |
57081559 AC |
3989 | -- Do not proceed with the analysis when the range of iteration is |
3990 | -- empty. The appropriate error is issued by Is_Empty_Range. | |
3991 | ||
538dbb56 AC |
3992 | if Is_Entity_Name (Name (Iterator_Specification (N))) |
3993 | and then Is_Empty_Range (Etype (Name (Iterator_Specification (N)))) | |
3994 | then | |
3995 | return; | |
3996 | end if; | |
3997 | ||
57081559 | 3998 | else pragma Assert (Present (Loop_Parameter_Specification (N))); |
a736f6e6 AC |
3999 | declare |
4000 | Loop_Par : constant Node_Id := Loop_Parameter_Specification (N); | |
4001 | ||
4002 | begin | |
4003 | Preanalyze (Loop_Par); | |
4004 | ||
e4deba8e | 4005 | if Nkind (Discrete_Subtype_Definition (Loop_Par)) = N_Function_Call |
a736f6e6 AC |
4006 | and then Parent (Loop_Par) /= N |
4007 | then | |
4008 | -- The parser cannot distinguish between a loop specification | |
4009 | -- and an iterator specification. If after pre-analysis the | |
4010 | -- proper form has been recognized, rewrite the expression to | |
5f0c4d67 AC |
4011 | -- reflect the right kind. This is needed for proper ASIS |
4012 | -- navigation. If expansion is enabled, the transformation is | |
4013 | -- performed when the expression is rewritten as a loop. | |
a736f6e6 | 4014 | |
a736f6e6 AC |
4015 | Set_Iterator_Specification (N, |
4016 | New_Copy_Tree (Iterator_Specification (Parent (Loop_Par)))); | |
5f0c4d67 AC |
4017 | |
4018 | Set_Defining_Identifier (Iterator_Specification (N), | |
4019 | Relocate_Node (Defining_Identifier (Loop_Par))); | |
4020 | Set_Name (Iterator_Specification (N), | |
4021 | Relocate_Node (Discrete_Subtype_Definition (Loop_Par))); | |
4022 | Set_Comes_From_Source (Iterator_Specification (N), | |
4023 | Comes_From_Source (Loop_Parameter_Specification (N))); | |
4024 | Set_Loop_Parameter_Specification (N, Empty); | |
a736f6e6 AC |
4025 | end if; |
4026 | end; | |
ce6002ec AC |
4027 | end if; |
4028 | ||
0812b84e | 4029 | Preanalyze_And_Resolve (Cond, Standard_Boolean); |
804670f1 | 4030 | |
a961aa79 AC |
4031 | End_Scope; |
4032 | Set_Etype (N, Standard_Boolean); | |
0812b84e | 4033 | |
57081559 AC |
4034 | -- Verify that the loop variable is used within the condition of the |
4035 | -- quantified expression. | |
4036 | ||
4037 | if Present (Iterator_Specification (N)) then | |
4038 | Loop_Id := Defining_Identifier (Iterator_Specification (N)); | |
4039 | else | |
4040 | Loop_Id := Defining_Identifier (Loop_Parameter_Specification (N)); | |
4041 | end if; | |
4042 | ||
4043 | if Warn_On_Suspicious_Contract | |
4044 | and then not Referenced (Loop_Id, Cond) | |
4045 | then | |
124bed29 | 4046 | -- Generating C, this check causes spurious warnings on inlined |
519e9fdf | 4047 | -- postconditions; we can safely disable it because this check |
124bed29 | 4048 | -- was previously performed when analyzing the internally built |
519e9fdf AC |
4049 | -- postconditions procedure. |
4050 | ||
4051 | if Modify_Tree_For_C and then In_Inlined_Body then | |
4052 | null; | |
4053 | else | |
4054 | Error_Msg_N ("?T?unused variable &", Loop_Id); | |
4055 | end if; | |
57081559 AC |
4056 | end if; |
4057 | ||
e19fd0bd | 4058 | -- Diagnose a possible misuse of the SOME existential quantifier. When |
d1ec4768 RD |
4059 | -- we have a quantified expression of the form: |
4060 | ||
0812b84e | 4061 | -- for some X => (if P then Q [else True]) |
d1ec4768 | 4062 | |
e19fd0bd | 4063 | -- any value for X that makes P False results in the if expression being |
50ef946c | 4064 | -- trivially True, and so also results in the quantified expression |
e19fd0bd | 4065 | -- being trivially True. |
0812b84e | 4066 | |
e19fd0bd | 4067 | if Warn_On_Suspicious_Contract |
0812b84e AC |
4068 | and then not All_Present (N) |
4069 | and then Nkind (Cond) = N_If_Expression | |
4070 | and then No_Else_Or_Trivial_True (Cond) | |
4071 | then | |
e19fd0bd | 4072 | Error_Msg_N ("?T?suspicious expression", N); |
0812b84e AC |
4073 | Error_Msg_N ("\\did you mean (for all X ='> (if P then Q))", N); |
4074 | Error_Msg_N ("\\or (for some X ='> P and then Q) instead'?", N); | |
4075 | end if; | |
a961aa79 AC |
4076 | end Analyze_Quantified_Expression; |
4077 | ||
996ae0b0 RK |
4078 | ------------------- |
4079 | -- Analyze_Range -- | |
4080 | ------------------- | |
4081 | ||
4082 | procedure Analyze_Range (N : Node_Id) is | |
4083 | L : constant Node_Id := Low_Bound (N); | |
4084 | H : constant Node_Id := High_Bound (N); | |
4085 | I1, I2 : Interp_Index; | |
4086 | It1, It2 : Interp; | |
4087 | ||
4088 | procedure Check_Common_Type (T1, T2 : Entity_Id); | |
4089 | -- Verify the compatibility of two types, and choose the | |
4090 | -- non universal one if the other is universal. | |
4091 | ||
4092 | procedure Check_High_Bound (T : Entity_Id); | |
4093 | -- Test one interpretation of the low bound against all those | |
4094 | -- of the high bound. | |
4095 | ||
fbf5a39b | 4096 | procedure Check_Universal_Expression (N : Node_Id); |
a1092b48 AC |
4097 | -- In Ada 83, reject bounds of a universal range that are not literals |
4098 | -- or entity names. | |
fbf5a39b | 4099 | |
996ae0b0 RK |
4100 | ----------------------- |
4101 | -- Check_Common_Type -- | |
4102 | ----------------------- | |
4103 | ||
4104 | procedure Check_Common_Type (T1, T2 : Entity_Id) is | |
4105 | begin | |
b4592168 GD |
4106 | if Covers (T1 => T1, T2 => T2) |
4107 | or else | |
4108 | Covers (T1 => T2, T2 => T1) | |
4109 | then | |
996ae0b0 RK |
4110 | if T1 = Universal_Integer |
4111 | or else T1 = Universal_Real | |
4112 | or else T1 = Any_Character | |
4113 | then | |
4114 | Add_One_Interp (N, Base_Type (T2), Base_Type (T2)); | |
4115 | ||
fbf5a39b | 4116 | elsif T1 = T2 then |
996ae0b0 RK |
4117 | Add_One_Interp (N, T1, T1); |
4118 | ||
4119 | else | |
4120 | Add_One_Interp (N, Base_Type (T1), Base_Type (T1)); | |
4121 | end if; | |
4122 | end if; | |
4123 | end Check_Common_Type; | |
4124 | ||
4125 | ---------------------- | |
4126 | -- Check_High_Bound -- | |
4127 | ---------------------- | |
4128 | ||
4129 | procedure Check_High_Bound (T : Entity_Id) is | |
4130 | begin | |
4131 | if not Is_Overloaded (H) then | |
4132 | Check_Common_Type (T, Etype (H)); | |
4133 | else | |
4134 | Get_First_Interp (H, I2, It2); | |
996ae0b0 RK |
4135 | while Present (It2.Typ) loop |
4136 | Check_Common_Type (T, It2.Typ); | |
4137 | Get_Next_Interp (I2, It2); | |
4138 | end loop; | |
4139 | end if; | |
4140 | end Check_High_Bound; | |
4141 | ||
fbf5a39b AC |
4142 | ----------------------------- |
4143 | -- Is_Universal_Expression -- | |
4144 | ----------------------------- | |
4145 | ||
4146 | procedure Check_Universal_Expression (N : Node_Id) is | |
4147 | begin | |
4148 | if Etype (N) = Universal_Integer | |
4149 | and then Nkind (N) /= N_Integer_Literal | |
4150 | and then not Is_Entity_Name (N) | |
4151 | and then Nkind (N) /= N_Attribute_Reference | |
4152 | then | |
4153 | Error_Msg_N ("illegal bound in discrete range", N); | |
4154 | end if; | |
4155 | end Check_Universal_Expression; | |
4156 | ||
996ae0b0 RK |
4157 | -- Start of processing for Analyze_Range |
4158 | ||
4159 | begin | |
4160 | Set_Etype (N, Any_Type); | |
4161 | Analyze_Expression (L); | |
4162 | Analyze_Expression (H); | |
4163 | ||
4164 | if Etype (L) = Any_Type or else Etype (H) = Any_Type then | |
4165 | return; | |
4166 | ||
4167 | else | |
4168 | if not Is_Overloaded (L) then | |
4169 | Check_High_Bound (Etype (L)); | |
4170 | else | |
4171 | Get_First_Interp (L, I1, It1); | |
996ae0b0 RK |
4172 | while Present (It1.Typ) loop |
4173 | Check_High_Bound (It1.Typ); | |
4174 | Get_Next_Interp (I1, It1); | |
4175 | end loop; | |
4176 | end if; | |
4177 | ||
4178 | -- If result is Any_Type, then we did not find a compatible pair | |
4179 | ||
4180 | if Etype (N) = Any_Type then | |
4181 | Error_Msg_N ("incompatible types in range ", N); | |
4182 | end if; | |
4183 | end if; | |
fbf5a39b | 4184 | |
0ab80019 | 4185 | if Ada_Version = Ada_83 |
fbf5a39b AC |
4186 | and then |
4187 | (Nkind (Parent (N)) = N_Loop_Parameter_Specification | |
4c46b835 | 4188 | or else Nkind (Parent (N)) = N_Constrained_Array_Definition) |
fbf5a39b AC |
4189 | then |
4190 | Check_Universal_Expression (L); | |
4191 | Check_Universal_Expression (H); | |
4192 | end if; | |
d3820795 | 4193 | |
22e89283 | 4194 | Check_Function_Writable_Actuals (N); |
996ae0b0 RK |
4195 | end Analyze_Range; |
4196 | ||
4197 | ----------------------- | |
4198 | -- Analyze_Reference -- | |
4199 | ----------------------- | |
4200 | ||
4201 | procedure Analyze_Reference (N : Node_Id) is | |
4202 | P : constant Node_Id := Prefix (N); | |
b4592168 GD |
4203 | E : Entity_Id; |
4204 | T : Entity_Id; | |
996ae0b0 | 4205 | Acc_Type : Entity_Id; |
b4592168 | 4206 | |
996ae0b0 RK |
4207 | begin |
4208 | Analyze (P); | |
b4592168 | 4209 | |
c42e1b17 AC |
4210 | -- An interesting error check, if we take the 'Ref of an object for |
4211 | -- which a pragma Atomic or Volatile has been given, and the type of the | |
4212 | -- object is not Atomic or Volatile, then we are in trouble. The problem | |
4213 | -- is that no trace of the atomic/volatile status will remain for the | |
4214 | -- backend to respect when it deals with the resulting pointer, since | |
4215 | -- the pointer type will not be marked atomic (it is a pointer to the | |
4216 | -- base type of the object). | |
b4592168 GD |
4217 | |
4218 | -- It is not clear if that can ever occur, but in case it does, we will | |
4219 | -- generate an error message. Not clear if this message can ever be | |
4220 | -- generated, and pretty clear that it represents a bug if it is, still | |
d2f25cd1 AC |
4221 | -- seems worth checking, except in CodePeer mode where we do not really |
4222 | -- care and don't want to bother the user. | |
b4592168 GD |
4223 | |
4224 | T := Etype (P); | |
4225 | ||
4226 | if Is_Entity_Name (P) | |
4227 | and then Is_Object_Reference (P) | |
d2f25cd1 | 4228 | and then not CodePeer_Mode |
b4592168 GD |
4229 | then |
4230 | E := Entity (P); | |
4231 | T := Etype (P); | |
4232 | ||
4233 | if (Has_Atomic_Components (E) | |
c42e1b17 | 4234 | and then not Has_Atomic_Components (T)) |
b4592168 GD |
4235 | or else |
4236 | (Has_Volatile_Components (E) | |
c42e1b17 | 4237 | and then not Has_Volatile_Components (T)) |
b4592168 GD |
4238 | or else (Is_Atomic (E) and then not Is_Atomic (T)) |
4239 | or else (Is_Volatile (E) and then not Is_Volatile (T)) | |
4240 | then | |
4241 | Error_Msg_N ("cannot take reference to Atomic/Volatile object", N); | |
4242 | end if; | |
4243 | end if; | |
4244 | ||
4245 | -- Carry on with normal processing | |
4246 | ||
996ae0b0 | 4247 | Acc_Type := Create_Itype (E_Allocator_Type, N); |
b4592168 | 4248 | Set_Etype (Acc_Type, Acc_Type); |
996ae0b0 RK |
4249 | Set_Directly_Designated_Type (Acc_Type, Etype (P)); |
4250 | Set_Etype (N, Acc_Type); | |
4251 | end Analyze_Reference; | |
4252 | ||
4253 | -------------------------------- | |
4254 | -- Analyze_Selected_Component -- | |
4255 | -------------------------------- | |
4256 | ||
2383acbd AC |
4257 | -- Prefix is a record type or a task or protected type. In the latter case, |
4258 | -- the selector must denote a visible entry. | |
996ae0b0 RK |
4259 | |
4260 | procedure Analyze_Selected_Component (N : Node_Id) is | |
d469eabe HK |
4261 | Name : constant Node_Id := Prefix (N); |
4262 | Sel : constant Node_Id := Selector_Name (N); | |
4263 | Act_Decl : Node_Id; | |
4264 | Comp : Entity_Id; | |
4265 | Has_Candidate : Boolean := False; | |
4266 | In_Scope : Boolean; | |
4267 | Parent_N : Node_Id; | |
4268 | Pent : Entity_Id := Empty; | |
4269 | Prefix_Type : Entity_Id; | |
401093c1 ES |
4270 | |
4271 | Type_To_Use : Entity_Id; | |
4272 | -- In most cases this is the Prefix_Type, but if the Prefix_Type is | |
4273 | -- a class-wide type, we use its root type, whose components are | |
4274 | -- present in the class-wide type. | |
4275 | ||
2383acbd AC |
4276 | Is_Single_Concurrent_Object : Boolean; |
4277 | -- Set True if the prefix is a single task or a single protected object | |
4278 | ||
20261dc1 AC |
4279 | procedure Find_Component_In_Instance (Rec : Entity_Id); |
4280 | -- In an instance, a component of a private extension may not be visible | |
4281 | -- while it was visible in the generic. Search candidate scope for a | |
4282 | -- component with the proper identifier. This is only done if all other | |
f90d14ac AC |
4283 | -- searches have failed. If a match is found, the Etype of both N and |
4284 | -- Sel are set from this component, and the entity of Sel is set to | |
4285 | -- reference this component. If no match is found, Entity (Sel) remains | |
7d9880c9 AC |
4286 | -- unset. For a derived type that is an actual of the instance, the |
4287 | -- desired component may be found in any ancestor. | |
20261dc1 | 4288 | |
d469eabe HK |
4289 | function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean; |
4290 | -- It is known that the parent of N denotes a subprogram call. Comp | |
4291 | -- is an overloadable component of the concurrent type of the prefix. | |
4292 | -- Determine whether all formals of the parent of N and Comp are mode | |
b4592168 GD |
4293 | -- conformant. If the parent node is not analyzed yet it may be an |
4294 | -- indexed component rather than a function call. | |
d469eabe | 4295 | |
bd717ec9 | 4296 | function Has_Dereference (Nod : Node_Id) return Boolean; |
0f8b3e5d | 4297 | -- Check whether prefix includes a dereference at any level. |
bd717ec9 | 4298 | |
20261dc1 AC |
4299 | -------------------------------- |
4300 | -- Find_Component_In_Instance -- | |
4301 | -------------------------------- | |
4302 | ||
4303 | procedure Find_Component_In_Instance (Rec : Entity_Id) is | |
4304 | Comp : Entity_Id; | |
7d9880c9 | 4305 | Typ : Entity_Id; |
20261dc1 AC |
4306 | |
4307 | begin | |
7d9880c9 AC |
4308 | Typ := Rec; |
4309 | while Present (Typ) loop | |
4310 | Comp := First_Component (Typ); | |
4311 | while Present (Comp) loop | |
4312 | if Chars (Comp) = Chars (Sel) then | |
4313 | Set_Entity_With_Checks (Sel, Comp); | |
4314 | Set_Etype (Sel, Etype (Comp)); | |
4315 | Set_Etype (N, Etype (Comp)); | |
4316 | return; | |
4317 | end if; | |
4318 | ||
4319 | Next_Component (Comp); | |
4320 | end loop; | |
4321 | ||
4322 | -- If not found, the component may be declared in the parent | |
4323 | -- type or its full view, if any. | |
4324 | ||
4325 | if Is_Derived_Type (Typ) then | |
4326 | Typ := Etype (Typ); | |
4327 | ||
4328 | if Is_Private_Type (Typ) then | |
4329 | Typ := Full_View (Typ); | |
4330 | end if; | |
4331 | ||
4332 | else | |
20261dc1 AC |
4333 | return; |
4334 | end if; | |
20261dc1 AC |
4335 | end loop; |
4336 | ||
cf3e6845 AC |
4337 | -- If we fall through, no match, so no changes made |
4338 | ||
4339 | return; | |
20261dc1 AC |
4340 | end Find_Component_In_Instance; |
4341 | ||
d469eabe HK |
4342 | ------------------------------ |
4343 | -- Has_Mode_Conformant_Spec -- | |
4344 | ------------------------------ | |
4345 | ||
4346 | function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is | |
4347 | Comp_Param : Entity_Id; | |
4348 | Param : Node_Id; | |
4349 | Param_Typ : Entity_Id; | |
4350 | ||
4351 | begin | |
4352 | Comp_Param := First_Formal (Comp); | |
b4592168 GD |
4353 | |
4354 | if Nkind (Parent (N)) = N_Indexed_Component then | |
4355 | Param := First (Expressions (Parent (N))); | |
4356 | else | |
4357 | Param := First (Parameter_Associations (Parent (N))); | |
4358 | end if; | |
4359 | ||
d469eabe HK |
4360 | while Present (Comp_Param) |
4361 | and then Present (Param) | |
4362 | loop | |
4363 | Param_Typ := Find_Parameter_Type (Param); | |
4364 | ||
4365 | if Present (Param_Typ) | |
4366 | and then | |
4367 | not Conforming_Types | |
4368 | (Etype (Comp_Param), Param_Typ, Mode_Conformant) | |
4369 | then | |
4370 | return False; | |
4371 | end if; | |
4372 | ||
4373 | Next_Formal (Comp_Param); | |
4374 | Next (Param); | |
4375 | end loop; | |
4376 | ||
9e92ad49 AC |
4377 | -- One of the specs has additional formals; there is no match, unless |
4378 | -- this may be an indexing of a parameterless call. | |
f0e7963f AC |
4379 | |
4380 | -- Note that when expansion is disabled, the corresponding record | |
4381 | -- type of synchronized types is not constructed, so that there is | |
4382 | -- no point is attempting an interpretation as a prefixed call, as | |
4383 | -- this is bound to fail because the primitive operations will not | |
4384 | -- be properly located. | |
d469eabe HK |
4385 | |
4386 | if Present (Comp_Param) or else Present (Param) then | |
f0e7963f AC |
4387 | if Needs_No_Actuals (Comp) |
4388 | and then Is_Array_Type (Etype (Comp)) | |
4389 | and then not Expander_Active | |
4390 | then | |
4391 | return True; | |
f0e7963f AC |
4392 | else |
4393 | return False; | |
4394 | end if; | |
d469eabe HK |
4395 | end if; |
4396 | ||
4397 | return True; | |
4398 | end Has_Mode_Conformant_Spec; | |
996ae0b0 | 4399 | |
bd717ec9 AC |
4400 | --------------------- |
4401 | -- Has_Dereference -- | |
4402 | --------------------- | |
4403 | ||
4404 | function Has_Dereference (Nod : Node_Id) return Boolean is | |
4405 | begin | |
4406 | if Nkind (Nod) = N_Explicit_Dereference then | |
4407 | return True; | |
4408 | ||
a6363ed3 AC |
4409 | -- When expansion is disabled an explicit dereference may not have |
4410 | -- been inserted, but if this is an access type the indirection makes | |
4411 | -- the call safe. | |
4412 | ||
4413 | elsif Is_Access_Type (Etype (Nod)) then | |
4414 | return True; | |
4415 | ||
bd717ec9 AC |
4416 | elsif Nkind_In (Nod, N_Indexed_Component, N_Selected_Component) then |
4417 | return Has_Dereference (Prefix (Nod)); | |
4418 | ||
4419 | else | |
4420 | return False; | |
4421 | end if; | |
4422 | end Has_Dereference; | |
4423 | ||
996ae0b0 RK |
4424 | -- Start of processing for Analyze_Selected_Component |
4425 | ||
4426 | begin | |
4427 | Set_Etype (N, Any_Type); | |
4428 | ||
4429 | if Is_Overloaded (Name) then | |
4430 | Analyze_Overloaded_Selected_Component (N); | |
4431 | return; | |
4432 | ||
4433 | elsif Etype (Name) = Any_Type then | |
4434 | Set_Entity (Sel, Any_Id); | |
4435 | Set_Etype (Sel, Any_Type); | |
4436 | return; | |
4437 | ||
4438 | else | |
996ae0b0 RK |
4439 | Prefix_Type := Etype (Name); |
4440 | end if; | |
4441 | ||
4442 | if Is_Access_Type (Prefix_Type) then | |
07fc65c4 | 4443 | |
0d57c6f4 RD |
4444 | -- A RACW object can never be used as prefix of a selected component |
4445 | -- since that means it is dereferenced without being a controlling | |
4446 | -- operand of a dispatching operation (RM E.2.2(16/1)). Before | |
4447 | -- reporting an error, we must check whether this is actually a | |
4448 | -- dispatching call in prefix form. | |
07fc65c4 | 4449 | |
996ae0b0 RK |
4450 | if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type) |
4451 | and then Comes_From_Source (N) | |
4452 | then | |
b4592168 GD |
4453 | if Try_Object_Operation (N) then |
4454 | return; | |
4455 | else | |
4456 | Error_Msg_N | |
4457 | ("invalid dereference of a remote access-to-class-wide value", | |
4458 | N); | |
4459 | end if; | |
07fc65c4 GB |
4460 | |
4461 | -- Normal case of selected component applied to access type | |
4462 | ||
4463 | else | |
324ac540 | 4464 | Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N); |
da709d08 | 4465 | |
6e73e3ab AC |
4466 | if Is_Entity_Name (Name) then |
4467 | Pent := Entity (Name); | |
4468 | elsif Nkind (Name) = N_Selected_Component | |
4469 | and then Is_Entity_Name (Selector_Name (Name)) | |
4470 | then | |
4471 | Pent := Entity (Selector_Name (Name)); | |
4472 | end if; | |
da709d08 | 4473 | |
d469eabe | 4474 | Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name); |
996ae0b0 | 4475 | end if; |
b4592168 GD |
4476 | |
4477 | -- If we have an explicit dereference of a remote access-to-class-wide | |
4478 | -- value, then issue an error (see RM-E.2.2(16/1)). However we first | |
4479 | -- have to check for the case of a prefix that is a controlling operand | |
4480 | -- of a prefixed dispatching call, as the dereference is legal in that | |
4481 | -- case. Normally this condition is checked in Validate_Remote_Access_ | |
4482 | -- To_Class_Wide_Type, but we have to defer the checking for selected | |
4483 | -- component prefixes because of the prefixed dispatching call case. | |
4484 | -- Note that implicit dereferences are checked for this just above. | |
4485 | ||
4486 | elsif Nkind (Name) = N_Explicit_Dereference | |
4487 | and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name))) | |
4488 | and then Comes_From_Source (N) | |
4489 | then | |
4490 | if Try_Object_Operation (N) then | |
4491 | return; | |
4492 | else | |
4493 | Error_Msg_N | |
4494 | ("invalid dereference of a remote access-to-class-wide value", | |
4495 | N); | |
4496 | end if; | |
aab883ec | 4497 | end if; |
b67a385c | 4498 | |
aab883ec ES |
4499 | -- (Ada 2005): if the prefix is the limited view of a type, and |
4500 | -- the context already includes the full view, use the full view | |
4501 | -- in what follows, either to retrieve a component of to find | |
4502 | -- a primitive operation. If the prefix is an explicit dereference, | |
4503 | -- set the type of the prefix to reflect this transformation. | |
401093c1 ES |
4504 | -- If the non-limited view is itself an incomplete type, get the |
4505 | -- full view if available. | |
aab883ec | 4506 | |
47346923 AC |
4507 | if From_Limited_With (Prefix_Type) |
4508 | and then Has_Non_Limited_View (Prefix_Type) | |
aab883ec | 4509 | then |
401093c1 | 4510 | Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type)); |
aab883ec ES |
4511 | |
4512 | if Nkind (N) = N_Explicit_Dereference then | |
4513 | Set_Etype (Prefix (N), Prefix_Type); | |
4514 | end if; | |
996ae0b0 RK |
4515 | end if; |
4516 | ||
4517 | if Ekind (Prefix_Type) = E_Private_Subtype then | |
4518 | Prefix_Type := Base_Type (Prefix_Type); | |
4519 | end if; | |
4520 | ||
401093c1 | 4521 | Type_To_Use := Prefix_Type; |
996ae0b0 RK |
4522 | |
4523 | -- For class-wide types, use the entity list of the root type. This | |
4524 | -- indirection is specially important for private extensions because | |
4525 | -- only the root type get switched (not the class-wide type). | |
4526 | ||
4527 | if Is_Class_Wide_Type (Prefix_Type) then | |
401093c1 | 4528 | Type_To_Use := Root_Type (Prefix_Type); |
996ae0b0 RK |
4529 | end if; |
4530 | ||
2383acbd AC |
4531 | -- If the prefix is a single concurrent object, use its name in error |
4532 | -- messages, rather than that of its anonymous type. | |
4533 | ||
4534 | Is_Single_Concurrent_Object := | |
4535 | Is_Concurrent_Type (Prefix_Type) | |
4536 | and then Is_Internal_Name (Chars (Prefix_Type)) | |
4537 | and then not Is_Derived_Type (Prefix_Type) | |
4538 | and then Is_Entity_Name (Name); | |
4539 | ||
401093c1 | 4540 | Comp := First_Entity (Type_To_Use); |
996ae0b0 RK |
4541 | |
4542 | -- If the selector has an original discriminant, the node appears in | |
4543 | -- an instance. Replace the discriminant with the corresponding one | |
4544 | -- in the current discriminated type. For nested generics, this must | |
4545 | -- be done transitively, so note the new original discriminant. | |
4546 | ||
4547 | if Nkind (Sel) = N_Identifier | |
c0b11850 | 4548 | and then In_Instance |
996ae0b0 RK |
4549 | and then Present (Original_Discriminant (Sel)) |
4550 | then | |
4551 | Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type); | |
4552 | ||
4553 | -- Mark entity before rewriting, for completeness and because | |
4554 | -- subsequent semantic checks might examine the original node. | |
4555 | ||
4556 | Set_Entity (Sel, Comp); | |
ee2ba856 | 4557 | Rewrite (Selector_Name (N), New_Occurrence_Of (Comp, Sloc (N))); |
996ae0b0 RK |
4558 | Set_Original_Discriminant (Selector_Name (N), Comp); |
4559 | Set_Etype (N, Etype (Comp)); | |
44a10091 | 4560 | Check_Implicit_Dereference (N, Etype (Comp)); |
996ae0b0 RK |
4561 | |
4562 | if Is_Access_Type (Etype (Name)) then | |
4563 | Insert_Explicit_Dereference (Name); | |
324ac540 | 4564 | Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N); |
996ae0b0 RK |
4565 | end if; |
4566 | ||
4567 | elsif Is_Record_Type (Prefix_Type) then | |
4568 | ||
ee2ba856 AC |
4569 | -- Find component with given name. In an instance, if the node is |
4570 | -- known as a prefixed call, do not examine components whose | |
4571 | -- visibility may be accidental. | |
996ae0b0 | 4572 | |
4913e24c | 4573 | while Present (Comp) and then not Is_Prefixed_Call (N) loop |
996ae0b0 | 4574 | if Chars (Comp) = Chars (Sel) |
a53c5613 | 4575 | and then Is_Visible_Component (Comp, N) |
996ae0b0 | 4576 | then |
e7ba564f | 4577 | Set_Entity_With_Checks (Sel, Comp); |
996ae0b0 RK |
4578 | Set_Etype (Sel, Etype (Comp)); |
4579 | ||
4580 | if Ekind (Comp) = E_Discriminant then | |
5d09245e | 4581 | if Is_Unchecked_Union (Base_Type (Prefix_Type)) then |
996ae0b0 | 4582 | Error_Msg_N |
02f58834 | 4583 | ("cannot reference discriminant of unchecked union", |
996ae0b0 RK |
4584 | Sel); |
4585 | end if; | |
4586 | ||
4587 | if Is_Generic_Type (Prefix_Type) | |
4588 | or else | |
4589 | Is_Generic_Type (Root_Type (Prefix_Type)) | |
4590 | then | |
4591 | Set_Original_Discriminant (Sel, Comp); | |
4592 | end if; | |
4593 | end if; | |
4594 | ||
4595 | -- Resolve the prefix early otherwise it is not possible to | |
4596 | -- build the actual subtype of the component: it may need | |
4597 | -- to duplicate this prefix and duplication is only allowed | |
4598 | -- on fully resolved expressions. | |
4599 | ||
fbf5a39b | 4600 | Resolve (Name); |
996ae0b0 | 4601 | |
b67a385c ES |
4602 | -- Ada 2005 (AI-50217): Check wrong use of incomplete types or |
4603 | -- subtypes in a package specification. | |
28be29ce ES |
4604 | -- Example: |
4605 | ||
4606 | -- limited with Pkg; | |
4607 | -- package Pkg is | |
4608 | -- type Acc_Inc is access Pkg.T; | |
4609 | -- X : Acc_Inc; | |
b67a385c ES |
4610 | -- N : Natural := X.all.Comp; -- ERROR, limited view |
4611 | -- end Pkg; -- Comp is not visible | |
28be29ce ES |
4612 | |
4613 | if Nkind (Name) = N_Explicit_Dereference | |
7b56a91b | 4614 | and then From_Limited_With (Etype (Prefix (Name))) |
28be29ce | 4615 | and then not Is_Potentially_Use_Visible (Etype (Name)) |
b67a385c ES |
4616 | and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) = |
4617 | N_Package_Specification | |
28be29ce ES |
4618 | then |
4619 | Error_Msg_NE | |
4620 | ("premature usage of incomplete}", Prefix (Name), | |
4621 | Etype (Prefix (Name))); | |
4622 | end if; | |
4623 | ||
996ae0b0 RK |
4624 | -- We never need an actual subtype for the case of a selection |
4625 | -- for a indexed component of a non-packed array, since in | |
4626 | -- this case gigi generates all the checks and can find the | |
4627 | -- necessary bounds information. | |
4628 | ||
0d57c6f4 RD |
4629 | -- We also do not need an actual subtype for the case of a |
4630 | -- first, last, length, or range attribute applied to a | |
996ae0b0 RK |
4631 | -- non-packed array, since gigi can again get the bounds in |
4632 | -- these cases (gigi cannot handle the packed case, since it | |
4633 | -- has the bounds of the packed array type, not the original | |
4634 | -- bounds of the type). However, if the prefix is itself a | |
4635 | -- selected component, as in a.b.c (i), gigi may regard a.b.c | |
4636 | -- as a dynamic-sized temporary, so we do generate an actual | |
4637 | -- subtype for this case. | |
4638 | ||
4639 | Parent_N := Parent (N); | |
4640 | ||
4641 | if not Is_Packed (Etype (Comp)) | |
4642 | and then | |
4643 | ((Nkind (Parent_N) = N_Indexed_Component | |
d469eabe | 4644 | and then Nkind (Name) /= N_Selected_Component) |
996ae0b0 RK |
4645 | or else |
4646 | (Nkind (Parent_N) = N_Attribute_Reference | |
b69cd36a AC |
4647 | and then |
4648 | Nam_In (Attribute_Name (Parent_N), Name_First, | |
4649 | Name_Last, | |
4650 | Name_Length, | |
4651 | Name_Range))) | |
996ae0b0 RK |
4652 | then |
4653 | Set_Etype (N, Etype (Comp)); | |
4654 | ||
98123480 ES |
4655 | -- If full analysis is not enabled, we do not generate an |
4656 | -- actual subtype, because in the absence of expansion | |
4657 | -- reference to a formal of a protected type, for example, | |
4658 | -- will not be properly transformed, and will lead to | |
4659 | -- out-of-scope references in gigi. | |
4660 | ||
4661 | -- In all other cases, we currently build an actual subtype. | |
4662 | -- It seems likely that many of these cases can be avoided, | |
4663 | -- but right now, the front end makes direct references to the | |
fbf5a39b | 4664 | -- bounds (e.g. in generating a length check), and if we do |
996ae0b0 | 4665 | -- not make an actual subtype, we end up getting a direct |
98123480 | 4666 | -- reference to a discriminant, which will not do. |
996ae0b0 | 4667 | |
98123480 | 4668 | elsif Full_Analysis then |
996ae0b0 RK |
4669 | Act_Decl := |
4670 | Build_Actual_Subtype_Of_Component (Etype (Comp), N); | |
4671 | Insert_Action (N, Act_Decl); | |
4672 | ||
4673 | if No (Act_Decl) then | |
4674 | Set_Etype (N, Etype (Comp)); | |
4675 | ||
4676 | else | |
4677 | -- Component type depends on discriminants. Enter the | |
4678 | -- main attributes of the subtype. | |
4679 | ||
4680 | declare | |
fbf5a39b AC |
4681 | Subt : constant Entity_Id := |
4682 | Defining_Identifier (Act_Decl); | |
996ae0b0 RK |
4683 | |
4684 | begin | |
4685 | Set_Etype (Subt, Base_Type (Etype (Comp))); | |
4686 | Set_Ekind (Subt, Ekind (Etype (Comp))); | |
4687 | Set_Etype (N, Subt); | |
4688 | end; | |
4689 | end if; | |
98123480 ES |
4690 | |
4691 | -- If Full_Analysis not enabled, just set the Etype | |
4692 | ||
4693 | else | |
4694 | Set_Etype (N, Etype (Comp)); | |
996ae0b0 RK |
4695 | end if; |
4696 | ||
44a10091 | 4697 | Check_Implicit_Dereference (N, Etype (N)); |
996ae0b0 RK |
4698 | return; |
4699 | end if; | |
4700 | ||
aab883ec | 4701 | -- If the prefix is a private extension, check only the visible |
9c510803 | 4702 | -- components of the partial view. This must include the tag, |
f3d57416 | 4703 | -- which can appear in expanded code in a tag check. |
aab883ec | 4704 | |
9c510803 | 4705 | if Ekind (Type_To_Use) = E_Record_Type_With_Private |
df3e68b1 | 4706 | and then Chars (Selector_Name (N)) /= Name_uTag |
9c510803 | 4707 | then |
401093c1 | 4708 | exit when Comp = Last_Entity (Type_To_Use); |
aab883ec ES |
4709 | end if; |
4710 | ||
996ae0b0 RK |
4711 | Next_Entity (Comp); |
4712 | end loop; | |
4713 | ||
d469eabe HK |
4714 | -- Ada 2005 (AI-252): The selected component can be interpreted as |
4715 | -- a prefixed view of a subprogram. Depending on the context, this is | |
4716 | -- either a name that can appear in a renaming declaration, or part | |
4717 | -- of an enclosing call given in prefix form. | |
4718 | ||
4719 | -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the | |
4720 | -- selected component should resolve to a name. | |
35ae2ed8 | 4721 | |
0791fbe9 | 4722 | if Ada_Version >= Ada_2005 |
35ae2ed8 | 4723 | and then Is_Tagged_Type (Prefix_Type) |
d469eabe | 4724 | and then not Is_Concurrent_Type (Prefix_Type) |
35ae2ed8 | 4725 | then |
d469eabe HK |
4726 | if Nkind (Parent (N)) = N_Generic_Association |
4727 | or else Nkind (Parent (N)) = N_Requeue_Statement | |
4728 | or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration | |
4729 | then | |
4730 | if Find_Primitive_Operation (N) then | |
4731 | return; | |
4732 | end if; | |
4733 | ||
4734 | elsif Try_Object_Operation (N) then | |
4735 | return; | |
4736 | end if; | |
4c46b835 | 4737 | |
98123480 ES |
4738 | -- If the transformation fails, it will be necessary to redo the |
4739 | -- analysis with all errors enabled, to indicate candidate | |
4740 | -- interpretations and reasons for each failure ??? | |
4c46b835 | 4741 | |
35ae2ed8 AC |
4742 | end if; |
4743 | ||
996ae0b0 | 4744 | elsif Is_Private_Type (Prefix_Type) then |
d469eabe | 4745 | |
98123480 ES |
4746 | -- Allow access only to discriminants of the type. If the type has |
4747 | -- no full view, gigi uses the parent type for the components, so we | |
4748 | -- do the same here. | |
996ae0b0 RK |
4749 | |
4750 | if No (Full_View (Prefix_Type)) then | |
401093c1 ES |
4751 | Type_To_Use := Root_Type (Base_Type (Prefix_Type)); |
4752 | Comp := First_Entity (Type_To_Use); | |
996ae0b0 RK |
4753 | end if; |
4754 | ||
4755 | while Present (Comp) loop | |
996ae0b0 RK |
4756 | if Chars (Comp) = Chars (Sel) then |
4757 | if Ekind (Comp) = E_Discriminant then | |
e7ba564f | 4758 | Set_Entity_With_Checks (Sel, Comp); |
996ae0b0 RK |
4759 | Generate_Reference (Comp, Sel); |
4760 | ||
4761 | Set_Etype (Sel, Etype (Comp)); | |
4762 | Set_Etype (N, Etype (Comp)); | |
44a10091 | 4763 | Check_Implicit_Dereference (N, Etype (N)); |
996ae0b0 RK |
4764 | |
4765 | if Is_Generic_Type (Prefix_Type) | |
d469eabe | 4766 | or else Is_Generic_Type (Root_Type (Prefix_Type)) |
996ae0b0 RK |
4767 | then |
4768 | Set_Original_Discriminant (Sel, Comp); | |
4769 | end if; | |
4770 | ||
f3d57416 | 4771 | -- Before declaring an error, check whether this is tagged |
aab883ec ES |
4772 | -- private type and a call to a primitive operation. |
4773 | ||
0791fbe9 | 4774 | elsif Ada_Version >= Ada_2005 |
aab883ec ES |
4775 | and then Is_Tagged_Type (Prefix_Type) |
4776 | and then Try_Object_Operation (N) | |
4777 | then | |
4778 | return; | |
4779 | ||
996ae0b0 | 4780 | else |
2383acbd AC |
4781 | Error_Msg_Node_2 := First_Subtype (Prefix_Type); |
4782 | Error_Msg_NE ("invisible selector& for }", N, Sel); | |
996ae0b0 RK |
4783 | Set_Entity (Sel, Any_Id); |
4784 | Set_Etype (N, Any_Type); | |
4785 | end if; | |
4786 | ||
4787 | return; | |
4788 | end if; | |
4789 | ||
4790 | Next_Entity (Comp); | |
4791 | end loop; | |
4792 | ||
4793 | elsif Is_Concurrent_Type (Prefix_Type) then | |
4794 | ||
d469eabe HK |
4795 | -- Find visible operation with given name. For a protected type, |
4796 | -- the possible candidates are discriminants, entries or protected | |
4797 | -- procedures. For a task type, the set can only include entries or | |
4798 | -- discriminants if the task type is not an enclosing scope. If it | |
4799 | -- is an enclosing scope (e.g. in an inner task) then all entities | |
4800 | -- are visible, but the prefix must denote the enclosing scope, i.e. | |
4801 | -- can only be a direct name or an expanded name. | |
996ae0b0 | 4802 | |
d469eabe | 4803 | Set_Etype (Sel, Any_Type); |
996ae0b0 RK |
4804 | In_Scope := In_Open_Scopes (Prefix_Type); |
4805 | ||
4806 | while Present (Comp) loop | |
f31dcd99 | 4807 | |
86ec3bfb AC |
4808 | -- Do not examine private operations of the type if not within |
4809 | -- its scope. | |
4810 | ||
996ae0b0 | 4811 | if Chars (Comp) = Chars (Sel) then |
86ec3bfb AC |
4812 | if Is_Overloadable (Comp) |
4813 | and then (In_Scope | |
4814 | or else Comp /= First_Private_Entity (Type_To_Use)) | |
4815 | then | |
996ae0b0 RK |
4816 | Add_One_Interp (Sel, Comp, Etype (Comp)); |
4817 | ||
d469eabe HK |
4818 | -- If the prefix is tagged, the correct interpretation may |
4819 | -- lie in the primitive or class-wide operations of the | |
4820 | -- type. Perform a simple conformance check to determine | |
4821 | -- whether Try_Object_Operation should be invoked even if | |
4822 | -- a visible entity is found. | |
4823 | ||
4824 | if Is_Tagged_Type (Prefix_Type) | |
f31dcd99 HK |
4825 | and then Nkind_In (Parent (N), N_Function_Call, |
4826 | N_Indexed_Component, | |
4827 | N_Procedure_Call_Statement) | |
d469eabe HK |
4828 | and then Has_Mode_Conformant_Spec (Comp) |
4829 | then | |
4830 | Has_Candidate := True; | |
4831 | end if; | |
4832 | ||
2383acbd AC |
4833 | -- Note: a selected component may not denote a component of a |
4834 | -- protected type (4.1.3(7)). | |
4835 | ||
bce79204 | 4836 | elsif Ekind_In (Comp, E_Discriminant, E_Entry_Family) |
2383acbd AC |
4837 | or else (In_Scope |
4838 | and then not Is_Protected_Type (Prefix_Type) | |
4839 | and then Is_Entity_Name (Name)) | |
996ae0b0 | 4840 | then |
e7ba564f | 4841 | Set_Entity_With_Checks (Sel, Comp); |
996ae0b0 RK |
4842 | Generate_Reference (Comp, Sel); |
4843 | ||
65e78a74 AC |
4844 | -- The selector is not overloadable, so we have a candidate |
4845 | -- interpretation. | |
4846 | ||
4847 | Has_Candidate := True; | |
4848 | ||
996ae0b0 RK |
4849 | else |
4850 | goto Next_Comp; | |
4851 | end if; | |
4852 | ||
4853 | Set_Etype (Sel, Etype (Comp)); | |
4854 | Set_Etype (N, Etype (Comp)); | |
4855 | ||
4856 | if Ekind (Comp) = E_Discriminant then | |
4857 | Set_Original_Discriminant (Sel, Comp); | |
4858 | end if; | |
4859 | ||
09494c32 AC |
4860 | -- For access type case, introduce explicit dereference for |
4861 | -- more uniform treatment of entry calls. | |
996ae0b0 RK |
4862 | |
4863 | if Is_Access_Type (Etype (Name)) then | |
4864 | Insert_Explicit_Dereference (Name); | |
fbf5a39b | 4865 | Error_Msg_NW |
324ac540 | 4866 | (Warn_On_Dereference, "?d?implicit dereference", N); |
996ae0b0 RK |
4867 | end if; |
4868 | end if; | |
4869 | ||
4870 | <<Next_Comp>> | |
4871 | Next_Entity (Comp); | |
4872 | exit when not In_Scope | |
9bc856dd AC |
4873 | and then |
4874 | Comp = First_Private_Entity (Base_Type (Prefix_Type)); | |
996ae0b0 RK |
4875 | end loop; |
4876 | ||
b3083540 | 4877 | -- If the scope is a current instance, the prefix cannot be an |
0f6251c7 AC |
4878 | -- expression of the same type, unless the selector designates a |
4879 | -- public operation (otherwise that would represent an attempt to | |
4880 | -- reach an internal entity of another synchronized object). | |
b3083540 | 4881 | -- This is legal if prefix is an access to such type and there is |
0f6251c7 | 4882 | -- a dereference, or is a component with a dereferenced prefix. |
0f8b3e5d AC |
4883 | -- It is also legal if the prefix is a component of a task type, |
4884 | -- and the selector is one of the task operations. | |
b3083540 | 4885 | |
bd717ec9 AC |
4886 | if In_Scope |
4887 | and then not Is_Entity_Name (Name) | |
4888 | and then not Has_Dereference (Name) | |
4889 | then | |
0f8b3e5d AC |
4890 | if Is_Task_Type (Prefix_Type) |
4891 | and then Present (Entity (Sel)) | |
4892 | and then Ekind_In (Entity (Sel), E_Entry, E_Entry_Family) | |
4893 | then | |
4894 | null; | |
4895 | ||
4896 | else | |
4897 | Error_Msg_NE | |
4898 | ("invalid reference to internal operation of some object of " | |
4899 | & "type &", N, Type_To_Use); | |
4900 | Set_Entity (Sel, Any_Id); | |
4901 | Set_Etype (Sel, Any_Type); | |
4902 | return; | |
4903 | end if; | |
b3083540 AC |
4904 | end if; |
4905 | ||
d469eabe HK |
4906 | -- If there is no visible entity with the given name or none of the |
4907 | -- visible entities are plausible interpretations, check whether | |
4908 | -- there is some other primitive operation with that name. | |
aab883ec | 4909 | |
bc38dbb4 | 4910 | if Ada_Version >= Ada_2005 and then Is_Tagged_Type (Prefix_Type) then |
d469eabe HK |
4911 | if (Etype (N) = Any_Type |
4912 | or else not Has_Candidate) | |
0a36105d JM |
4913 | and then Try_Object_Operation (N) |
4914 | then | |
4915 | return; | |
4916 | ||
4917 | -- If the context is not syntactically a procedure call, it | |
4918 | -- may be a call to a primitive function declared outside of | |
4919 | -- the synchronized type. | |
4920 | ||
4921 | -- If the context is a procedure call, there might still be | |
4922 | -- an overloading between an entry and a primitive procedure | |
4923 | -- declared outside of the synchronized type, called in prefix | |
4924 | -- notation. This is harder to disambiguate because in one case | |
4925 | -- the controlling formal is implicit ??? | |
4926 | ||
4927 | elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement | |
b4592168 | 4928 | and then Nkind (Parent (N)) /= N_Indexed_Component |
0a36105d JM |
4929 | and then Try_Object_Operation (N) |
4930 | then | |
4931 | return; | |
4932 | end if; | |
8cf23b91 AC |
4933 | |
4934 | -- Ada 2012 (AI05-0090-1): If we found a candidate of a call to an | |
4935 | -- entry or procedure of a tagged concurrent type we must check | |
4936 | -- if there are class-wide subprograms covering the primitive. If | |
4937 | -- true then Try_Object_Operation reports the error. | |
4938 | ||
4939 | if Has_Candidate | |
4940 | and then Is_Concurrent_Type (Prefix_Type) | |
4941 | and then Nkind (Parent (N)) = N_Procedure_Call_Statement | |
bc38dbb4 | 4942 | then |
8cf23b91 AC |
4943 | -- Duplicate the call. This is required to avoid problems with |
4944 | -- the tree transformations performed by Try_Object_Operation. | |
d7a44b14 AC |
4945 | -- Set properly the parent of the copied call, because it is |
4946 | -- about to be reanalyzed. | |
8cf23b91 | 4947 | |
d7a44b14 AC |
4948 | declare |
4949 | Par : constant Node_Id := New_Copy_Tree (Parent (N)); | |
4950 | ||
4951 | begin | |
4952 | Set_Parent (Par, Parent (Parent (N))); | |
29ba9f52 | 4953 | |
d7a44b14 | 4954 | if Try_Object_Operation |
29ba9f52 | 4955 | (Sinfo.Name (Par), CW_Test_Only => True) |
d7a44b14 AC |
4956 | then |
4957 | return; | |
4958 | end if; | |
4959 | end; | |
8cf23b91 | 4960 | end if; |
aab883ec ES |
4961 | end if; |
4962 | ||
2383acbd | 4963 | if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then |
11bc76df | 4964 | |
2383acbd AC |
4965 | -- Case of a prefix of a protected type: selector might denote |
4966 | -- an invisible private component. | |
4967 | ||
4968 | Comp := First_Private_Entity (Base_Type (Prefix_Type)); | |
4969 | while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop | |
4970 | Next_Entity (Comp); | |
4971 | end loop; | |
4972 | ||
4973 | if Present (Comp) then | |
4974 | if Is_Single_Concurrent_Object then | |
4975 | Error_Msg_Node_2 := Entity (Name); | |
4976 | Error_Msg_NE ("invisible selector& for &", N, Sel); | |
4977 | ||
4978 | else | |
4979 | Error_Msg_Node_2 := First_Subtype (Prefix_Type); | |
4980 | Error_Msg_NE ("invisible selector& for }", N, Sel); | |
4981 | end if; | |
4982 | return; | |
4983 | end if; | |
4984 | end if; | |
4985 | ||
996ae0b0 RK |
4986 | Set_Is_Overloaded (N, Is_Overloaded (Sel)); |
4987 | ||
4988 | else | |
4989 | -- Invalid prefix | |
4990 | ||
4991 | Error_Msg_NE ("invalid prefix in selected component&", N, Sel); | |
4992 | end if; | |
4993 | ||
4c46b835 | 4994 | -- If N still has no type, the component is not defined in the prefix |
996ae0b0 RK |
4995 | |
4996 | if Etype (N) = Any_Type then | |
4997 | ||
2383acbd | 4998 | if Is_Single_Concurrent_Object then |
996ae0b0 RK |
4999 | Error_Msg_Node_2 := Entity (Name); |
5000 | Error_Msg_NE ("no selector& for&", N, Sel); | |
5001 | ||
401093c1 | 5002 | Check_Misspelled_Selector (Type_To_Use, Sel); |
996ae0b0 | 5003 | |
8b4230c8 AC |
5004 | -- If this is a derived formal type, the parent may have different |
5005 | -- visibility at this point. Try for an inherited component before | |
5006 | -- reporting an error. | |
5007 | ||
de76a39c GB |
5008 | elsif Is_Generic_Type (Prefix_Type) |
5009 | and then Ekind (Prefix_Type) = E_Record_Type_With_Private | |
07fc65c4 | 5010 | and then Prefix_Type /= Etype (Prefix_Type) |
de76a39c GB |
5011 | and then Is_Record_Type (Etype (Prefix_Type)) |
5012 | then | |
de76a39c GB |
5013 | Set_Etype (Prefix (N), Etype (Prefix_Type)); |
5014 | Analyze_Selected_Component (N); | |
5015 | return; | |
5016 | ||
b1d12996 AC |
5017 | -- Similarly, if this is the actual for a formal derived type, or |
5018 | -- a derived type thereof, the component inherited from the generic | |
5019 | -- parent may not be visible in the actual, but the selected | |
5020 | -- component is legal. Climb up the derivation chain of the generic | |
5021 | -- parent type until we find the proper ancestor type. | |
20261dc1 | 5022 | |
b1d12996 AC |
5023 | elsif In_Instance and then Is_Tagged_Type (Prefix_Type) then |
5024 | declare | |
5025 | Par : Entity_Id := Prefix_Type; | |
5026 | begin | |
5027 | -- Climb up derivation chain to generic actual subtype | |
5028 | ||
5029 | while not Is_Generic_Actual_Type (Par) loop | |
5030 | if Ekind (Par) = E_Record_Type then | |
5031 | Par := Parent_Subtype (Par); | |
5032 | exit when No (Par); | |
5033 | else | |
5034 | exit when Par = Etype (Par); | |
5035 | Par := Etype (Par); | |
5036 | end if; | |
5037 | end loop; | |
4c46b835 | 5038 | |
b1d12996 | 5039 | if Present (Par) and then Is_Generic_Actual_Type (Par) then |
73999267 | 5040 | |
b1d12996 | 5041 | -- Now look for component in ancestor types |
fbf5a39b | 5042 | |
b1d12996 AC |
5043 | Par := Generic_Parent_Type (Declaration_Node (Par)); |
5044 | loop | |
5045 | Find_Component_In_Instance (Par); | |
5046 | exit when Present (Entity (Sel)) | |
5047 | or else Par = Etype (Par); | |
5048 | Par := Etype (Par); | |
5049 | end loop; | |
73999267 | 5050 | |
7d9880c9 AC |
5051 | -- Another special case: the type is an extension of a private |
5052 | -- type T, is an actual in an instance, and we are in the body | |
5053 | -- of the instance, so the generic body had a full view of the | |
5054 | -- type declaration for T or of some ancestor that defines the | |
5055 | -- component in question. | |
5056 | ||
5057 | elsif Is_Derived_Type (Type_To_Use) | |
5058 | and then Used_As_Generic_Actual (Type_To_Use) | |
5059 | and then In_Instance_Body | |
5060 | then | |
5061 | Find_Component_In_Instance (Parent_Subtype (Type_To_Use)); | |
5062 | ||
73999267 AC |
5063 | -- In ASIS mode the generic parent type may be absent. Examine |
5064 | -- the parent type directly for a component that may have been | |
5065 | -- visible in a parent generic unit. | |
5066 | ||
5067 | elsif Is_Derived_Type (Prefix_Type) then | |
5068 | Par := Etype (Prefix_Type); | |
5069 | Find_Component_In_Instance (Par); | |
b1d12996 AC |
5070 | end if; |
5071 | end; | |
5072 | ||
5073 | -- The search above must have eventually succeeded, since the | |
5074 | -- selected component was legal in the generic. | |
5075 | ||
5076 | if No (Entity (Sel)) then | |
5077 | raise Program_Error; | |
5078 | end if; | |
73999267 | 5079 | |
20261dc1 | 5080 | return; |
fbf5a39b | 5081 | |
20261dc1 | 5082 | -- Component not found, specialize error message when appropriate |
fbf5a39b | 5083 | |
996ae0b0 RK |
5084 | else |
5085 | if Ekind (Prefix_Type) = E_Record_Subtype then | |
5086 | ||
f4b049db AC |
5087 | -- Check whether this is a component of the base type which |
5088 | -- is absent from a statically constrained subtype. This will | |
5089 | -- raise constraint error at run time, but is not a compile- | |
5090 | -- time error. When the selector is illegal for base type as | |
5091 | -- well fall through and generate a compilation error anyway. | |
996ae0b0 RK |
5092 | |
5093 | Comp := First_Component (Base_Type (Prefix_Type)); | |
996ae0b0 | 5094 | while Present (Comp) loop |
996ae0b0 RK |
5095 | if Chars (Comp) = Chars (Sel) |
5096 | and then Is_Visible_Component (Comp) | |
5097 | then | |
e7ba564f | 5098 | Set_Entity_With_Checks (Sel, Comp); |
996ae0b0 RK |
5099 | Generate_Reference (Comp, Sel); |
5100 | Set_Etype (Sel, Etype (Comp)); | |
5101 | Set_Etype (N, Etype (Comp)); | |
5102 | ||
637a41a5 AC |
5103 | -- Emit appropriate message. The node will be replaced |
5104 | -- by an appropriate raise statement. | |
996ae0b0 | 5105 | |
637a41a5 AC |
5106 | -- Note that in SPARK mode, as with all calls to apply a |
5107 | -- compile time constraint error, this will be made into | |
5108 | -- an error to simplify the processing of the formal | |
5109 | -- verification backend. | |
d7f41b2d | 5110 | |
4a28b181 | 5111 | Apply_Compile_Time_Constraint_Error |
637a41a5 | 5112 | (N, "component not present in }??", |
4a28b181 AC |
5113 | CE_Discriminant_Check_Failed, |
5114 | Ent => Prefix_Type, Rep => False); | |
d7f41b2d | 5115 | |
996ae0b0 RK |
5116 | Set_Raises_Constraint_Error (N); |
5117 | return; | |
5118 | end if; | |
5119 | ||
5120 | Next_Component (Comp); | |
5121 | end loop; | |
5122 | ||
5123 | end if; | |
5124 | ||
5125 | Error_Msg_Node_2 := First_Subtype (Prefix_Type); | |
5126 | Error_Msg_NE ("no selector& for}", N, Sel); | |
5127 | ||
94bbf008 | 5128 | -- Add information in the case of an incomplete prefix |
99d520ad ES |
5129 | |
5130 | if Is_Incomplete_Type (Type_To_Use) then | |
5131 | declare | |
5132 | Inc : constant Entity_Id := First_Subtype (Type_To_Use); | |
5133 | ||
5134 | begin | |
7b56a91b | 5135 | if From_Limited_With (Scope (Type_To_Use)) then |
99d520ad ES |
5136 | Error_Msg_NE |
5137 | ("\limited view of& has no components", N, Inc); | |
bd38b431 | 5138 | |
99d520ad ES |
5139 | else |
5140 | Error_Msg_NE | |
5141 | ("\premature usage of incomplete type&", N, Inc); | |
bd38b431 AC |
5142 | |
5143 | if Nkind (Parent (Inc)) = | |
5144 | N_Incomplete_Type_Declaration | |
99d520ad | 5145 | then |
94bbf008 AC |
5146 | -- Record location of premature use in entity so that |
5147 | -- a continuation message is generated when the | |
5148 | -- completion is seen. | |
5149 | ||
99d520ad ES |
5150 | Set_Premature_Use (Parent (Inc), N); |
5151 | end if; | |
5152 | end if; | |
5153 | end; | |
5154 | end if; | |
5155 | ||
401093c1 | 5156 | Check_Misspelled_Selector (Type_To_Use, Sel); |
996ae0b0 RK |
5157 | end if; |
5158 | ||
5159 | Set_Entity (Sel, Any_Id); | |
5160 | Set_Etype (Sel, Any_Type); | |
5161 | end if; | |
5162 | end Analyze_Selected_Component; | |
5163 | ||
5164 | --------------------------- | |
5165 | -- Analyze_Short_Circuit -- | |
5166 | --------------------------- | |
5167 | ||
5168 | procedure Analyze_Short_Circuit (N : Node_Id) is | |
5169 | L : constant Node_Id := Left_Opnd (N); | |
5170 | R : constant Node_Id := Right_Opnd (N); | |
5171 | Ind : Interp_Index; | |
5172 | It : Interp; | |
5173 | ||
5174 | begin | |
5175 | Analyze_Expression (L); | |
5176 | Analyze_Expression (R); | |
5177 | Set_Etype (N, Any_Type); | |
5178 | ||
5179 | if not Is_Overloaded (L) then | |
996ae0b0 RK |
5180 | if Root_Type (Etype (L)) = Standard_Boolean |
5181 | and then Has_Compatible_Type (R, Etype (L)) | |
5182 | then | |
5183 | Add_One_Interp (N, Etype (L), Etype (L)); | |
5184 | end if; | |
5185 | ||
5186 | else | |
5187 | Get_First_Interp (L, Ind, It); | |
996ae0b0 RK |
5188 | while Present (It.Typ) loop |
5189 | if Root_Type (It.Typ) = Standard_Boolean | |
5190 | and then Has_Compatible_Type (R, It.Typ) | |
5191 | then | |
5192 | Add_One_Interp (N, It.Typ, It.Typ); | |
5193 | end if; | |
5194 | ||
5195 | Get_Next_Interp (Ind, It); | |
5196 | end loop; | |
5197 | end if; | |
5198 | ||
d469eabe HK |
5199 | -- Here we have failed to find an interpretation. Clearly we know that |
5200 | -- it is not the case that both operands can have an interpretation of | |
5201 | -- Boolean, but this is by far the most likely intended interpretation. | |
5202 | -- So we simply resolve both operands as Booleans, and at least one of | |
5203 | -- these resolutions will generate an error message, and we do not need | |
5204 | -- to give another error message on the short circuit operation itself. | |
996ae0b0 RK |
5205 | |
5206 | if Etype (N) = Any_Type then | |
5207 | Resolve (L, Standard_Boolean); | |
5208 | Resolve (R, Standard_Boolean); | |
5209 | Set_Etype (N, Standard_Boolean); | |
5210 | end if; | |
5211 | end Analyze_Short_Circuit; | |
5212 | ||
5213 | ------------------- | |
5214 | -- Analyze_Slice -- | |
5215 | ------------------- | |
5216 | ||
5217 | procedure Analyze_Slice (N : Node_Id) is | |
996ae0b0 | 5218 | D : constant Node_Id := Discrete_Range (N); |
5f44f0d4 | 5219 | P : constant Node_Id := Prefix (N); |
996ae0b0 | 5220 | Array_Type : Entity_Id; |
5f44f0d4 | 5221 | Index_Type : Entity_Id; |
996ae0b0 RK |
5222 | |
5223 | procedure Analyze_Overloaded_Slice; | |
5224 | -- If the prefix is overloaded, select those interpretations that | |
5225 | -- yield a one-dimensional array type. | |
5226 | ||
4c46b835 AC |
5227 | ------------------------------ |
5228 | -- Analyze_Overloaded_Slice -- | |
5229 | ------------------------------ | |
5230 | ||
996ae0b0 RK |
5231 | procedure Analyze_Overloaded_Slice is |
5232 | I : Interp_Index; | |
5233 | It : Interp; | |
5234 | Typ : Entity_Id; | |
5235 | ||
5236 | begin | |
5237 | Set_Etype (N, Any_Type); | |
996ae0b0 | 5238 | |
4c46b835 | 5239 | Get_First_Interp (P, I, It); |
996ae0b0 RK |
5240 | while Present (It.Nam) loop |
5241 | Typ := It.Typ; | |
5242 | ||
5243 | if Is_Access_Type (Typ) then | |
5244 | Typ := Designated_Type (Typ); | |
324ac540 AC |
5245 | Error_Msg_NW |
5246 | (Warn_On_Dereference, "?d?implicit dereference", N); | |
996ae0b0 RK |
5247 | end if; |
5248 | ||
5249 | if Is_Array_Type (Typ) | |
5250 | and then Number_Dimensions (Typ) = 1 | |
5251 | and then Has_Compatible_Type (D, Etype (First_Index (Typ))) | |
5252 | then | |
5253 | Add_One_Interp (N, Typ, Typ); | |
5254 | end if; | |
5255 | ||
5256 | Get_Next_Interp (I, It); | |
5257 | end loop; | |
5258 | ||
5259 | if Etype (N) = Any_Type then | |
5260 | Error_Msg_N ("expect array type in prefix of slice", N); | |
5261 | end if; | |
5262 | end Analyze_Overloaded_Slice; | |
5263 | ||
5264 | -- Start of processing for Analyze_Slice | |
5265 | ||
5266 | begin | |
36b8f95f | 5267 | if Comes_From_Source (N) then |
ce5ba43a | 5268 | Check_SPARK_05_Restriction ("slice is not allowed", N); |
36b8f95f | 5269 | end if; |
1d801f21 | 5270 | |
523456db | 5271 | Analyze (P); |
996ae0b0 RK |
5272 | Analyze (D); |
5273 | ||
5274 | if Is_Overloaded (P) then | |
5275 | Analyze_Overloaded_Slice; | |
5276 | ||
5277 | else | |
5278 | Array_Type := Etype (P); | |
5279 | Set_Etype (N, Any_Type); | |
5280 | ||
5281 | if Is_Access_Type (Array_Type) then | |
5282 | Array_Type := Designated_Type (Array_Type); | |
324ac540 | 5283 | Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N); |
996ae0b0 RK |
5284 | end if; |
5285 | ||
5286 | if not Is_Array_Type (Array_Type) then | |
5287 | Wrong_Type (P, Any_Array); | |
5288 | ||
5289 | elsif Number_Dimensions (Array_Type) > 1 then | |
5290 | Error_Msg_N | |
5291 | ("type is not one-dimensional array in slice prefix", N); | |
5292 | ||
996ae0b0 | 5293 | else |
5f44f0d4 AC |
5294 | if Ekind (Array_Type) = E_String_Literal_Subtype then |
5295 | Index_Type := Etype (String_Literal_Low_Bound (Array_Type)); | |
5296 | else | |
5297 | Index_Type := Etype (First_Index (Array_Type)); | |
5298 | end if; | |
5299 | ||
5300 | if not Has_Compatible_Type (D, Index_Type) then | |
5301 | Wrong_Type (D, Index_Type); | |
5302 | else | |
5303 | Set_Etype (N, Array_Type); | |
5304 | end if; | |
996ae0b0 RK |
5305 | end if; |
5306 | end if; | |
5307 | end Analyze_Slice; | |
5308 | ||
5309 | ----------------------------- | |
5310 | -- Analyze_Type_Conversion -- | |
5311 | ----------------------------- | |
5312 | ||
5313 | procedure Analyze_Type_Conversion (N : Node_Id) is | |
5314 | Expr : constant Node_Id := Expression (N); | |
039538bc | 5315 | Typ : Entity_Id; |
996ae0b0 RK |
5316 | |
5317 | begin | |
039538bc AC |
5318 | -- If Conversion_OK is set, then the Etype is already set, and the only |
5319 | -- processing required is to analyze the expression. This is used to | |
5320 | -- construct certain "illegal" conversions which are not allowed by Ada | |
5321 | -- semantics, but can be handled by Gigi, see Sinfo for further details. | |
996ae0b0 RK |
5322 | |
5323 | if Conversion_OK (N) then | |
5324 | Analyze (Expr); | |
5325 | return; | |
5326 | end if; | |
5327 | ||
5328 | -- Otherwise full type analysis is required, as well as some semantic | |
5329 | -- checks to make sure the argument of the conversion is appropriate. | |
5330 | ||
5331 | Find_Type (Subtype_Mark (N)); | |
039538bc AC |
5332 | Typ := Entity (Subtype_Mark (N)); |
5333 | Set_Etype (N, Typ); | |
5334 | Check_Fully_Declared (Typ, N); | |
996ae0b0 RK |
5335 | Analyze_Expression (Expr); |
5336 | Validate_Remote_Type_Type_Conversion (N); | |
5e8c8e44 | 5337 | |
996ae0b0 RK |
5338 | -- Only remaining step is validity checks on the argument. These |
5339 | -- are skipped if the conversion does not come from the source. | |
5340 | ||
5341 | if not Comes_From_Source (N) then | |
5342 | return; | |
5343 | ||
b67a385c ES |
5344 | -- If there was an error in a generic unit, no need to replicate the |
5345 | -- error message. Conversely, constant-folding in the generic may | |
5346 | -- transform the argument of a conversion into a string literal, which | |
5347 | -- is legal. Therefore the following tests are not performed in an | |
36428cc4 | 5348 | -- instance. The same applies to an inlined body. |
b67a385c | 5349 | |
36428cc4 | 5350 | elsif In_Instance or In_Inlined_Body then |
b67a385c ES |
5351 | return; |
5352 | ||
996ae0b0 RK |
5353 | elsif Nkind (Expr) = N_Null then |
5354 | Error_Msg_N ("argument of conversion cannot be null", N); | |
ed2233dc | 5355 | Error_Msg_N ("\use qualified expression instead", N); |
996ae0b0 RK |
5356 | Set_Etype (N, Any_Type); |
5357 | ||
5358 | elsif Nkind (Expr) = N_Aggregate then | |
5359 | Error_Msg_N ("argument of conversion cannot be aggregate", N); | |
ed2233dc | 5360 | Error_Msg_N ("\use qualified expression instead", N); |
996ae0b0 RK |
5361 | |
5362 | elsif Nkind (Expr) = N_Allocator then | |
5363 | Error_Msg_N ("argument of conversion cannot be an allocator", N); | |
ed2233dc | 5364 | Error_Msg_N ("\use qualified expression instead", N); |
996ae0b0 RK |
5365 | |
5366 | elsif Nkind (Expr) = N_String_Literal then | |
5367 | Error_Msg_N ("argument of conversion cannot be string literal", N); | |
ed2233dc | 5368 | Error_Msg_N ("\use qualified expression instead", N); |
996ae0b0 RK |
5369 | |
5370 | elsif Nkind (Expr) = N_Character_Literal then | |
0ab80019 | 5371 | if Ada_Version = Ada_83 then |
039538bc | 5372 | Resolve (Expr, Typ); |
996ae0b0 RK |
5373 | else |
5374 | Error_Msg_N ("argument of conversion cannot be character literal", | |
5375 | N); | |
ed2233dc | 5376 | Error_Msg_N ("\use qualified expression instead", N); |
996ae0b0 RK |
5377 | end if; |
5378 | ||
5379 | elsif Nkind (Expr) = N_Attribute_Reference | |
039538bc AC |
5380 | and then Nam_In (Attribute_Name (Expr), Name_Access, |
5381 | Name_Unchecked_Access, | |
5382 | Name_Unrestricted_Access) | |
996ae0b0 RK |
5383 | then |
5384 | Error_Msg_N ("argument of conversion cannot be access", N); | |
ed2233dc | 5385 | Error_Msg_N ("\use qualified expression instead", N); |
996ae0b0 | 5386 | end if; |
039538bc AC |
5387 | |
5388 | -- A formal parameter of a specific tagged type whose related subprogram | |
5389 | -- is subject to pragma Extensions_Visible with value "False" cannot | |
070d862d HK |
5390 | -- appear in a class-wide conversion (SPARK RM 6.1.7(3)). Do not check |
5391 | -- internally generated expressions. | |
039538bc | 5392 | |
070d862d HK |
5393 | if Is_Class_Wide_Type (Typ) |
5394 | and then Comes_From_Source (Expr) | |
5395 | and then Is_EVF_Expression (Expr) | |
5396 | then | |
039538bc | 5397 | Error_Msg_N |
44900051 AC |
5398 | ("formal parameter cannot be converted to class-wide type when " |
5399 | & "Extensions_Visible is False", Expr); | |
039538bc | 5400 | end if; |
996ae0b0 RK |
5401 | end Analyze_Type_Conversion; |
5402 | ||
5403 | ---------------------- | |
5404 | -- Analyze_Unary_Op -- | |
5405 | ---------------------- | |
5406 | ||
5407 | procedure Analyze_Unary_Op (N : Node_Id) is | |
5408 | R : constant Node_Id := Right_Opnd (N); | |
5409 | Op_Id : Entity_Id := Entity (N); | |
5410 | ||
5411 | begin | |
5412 | Set_Etype (N, Any_Type); | |
5413 | Candidate_Type := Empty; | |
5414 | ||
5415 | Analyze_Expression (R); | |
5416 | ||
5417 | if Present (Op_Id) then | |
5418 | if Ekind (Op_Id) = E_Operator then | |
5419 | Find_Unary_Types (R, Op_Id, N); | |
5420 | else | |
5421 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
5422 | end if; | |
5423 | ||
5424 | else | |
5425 | Op_Id := Get_Name_Entity_Id (Chars (N)); | |
996ae0b0 | 5426 | while Present (Op_Id) loop |
996ae0b0 RK |
5427 | if Ekind (Op_Id) = E_Operator then |
5428 | if No (Next_Entity (First_Entity (Op_Id))) then | |
5429 | Find_Unary_Types (R, Op_Id, N); | |
5430 | end if; | |
5431 | ||
5432 | elsif Is_Overloadable (Op_Id) then | |
5433 | Analyze_User_Defined_Unary_Op (N, Op_Id); | |
5434 | end if; | |
5435 | ||
5436 | Op_Id := Homonym (Op_Id); | |
5437 | end loop; | |
5438 | end if; | |
5439 | ||
5440 | Operator_Check (N); | |
5441 | end Analyze_Unary_Op; | |
5442 | ||
5443 | ---------------------------------- | |
5444 | -- Analyze_Unchecked_Expression -- | |
5445 | ---------------------------------- | |
5446 | ||
5447 | procedure Analyze_Unchecked_Expression (N : Node_Id) is | |
5448 | begin | |
5449 | Analyze (Expression (N), Suppress => All_Checks); | |
5450 | Set_Etype (N, Etype (Expression (N))); | |
5451 | Save_Interps (Expression (N), N); | |
5452 | end Analyze_Unchecked_Expression; | |
5453 | ||
5454 | --------------------------------------- | |
5455 | -- Analyze_Unchecked_Type_Conversion -- | |
5456 | --------------------------------------- | |
5457 | ||
5458 | procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is | |
5459 | begin | |
5460 | Find_Type (Subtype_Mark (N)); | |
5461 | Analyze_Expression (Expression (N)); | |
5462 | Set_Etype (N, Entity (Subtype_Mark (N))); | |
5463 | end Analyze_Unchecked_Type_Conversion; | |
5464 | ||
5465 | ------------------------------------ | |
5466 | -- Analyze_User_Defined_Binary_Op -- | |
5467 | ------------------------------------ | |
5468 | ||
5469 | procedure Analyze_User_Defined_Binary_Op | |
5470 | (N : Node_Id; | |
5471 | Op_Id : Entity_Id) | |
5472 | is | |
5473 | begin | |
5474 | -- Only do analysis if the operator Comes_From_Source, since otherwise | |
5475 | -- the operator was generated by the expander, and all such operators | |
5476 | -- always refer to the operators in package Standard. | |
5477 | ||
5478 | if Comes_From_Source (N) then | |
5479 | declare | |
5480 | F1 : constant Entity_Id := First_Formal (Op_Id); | |
5481 | F2 : constant Entity_Id := Next_Formal (F1); | |
5482 | ||
5483 | begin | |
5484 | -- Verify that Op_Id is a visible binary function. Note that since | |
5485 | -- we know Op_Id is overloaded, potentially use visible means use | |
5486 | -- visible for sure (RM 9.4(11)). | |
5487 | ||
5488 | if Ekind (Op_Id) = E_Function | |
5489 | and then Present (F2) | |
5490 | and then (Is_Immediately_Visible (Op_Id) | |
5491 | or else Is_Potentially_Use_Visible (Op_Id)) | |
5492 | and then Has_Compatible_Type (Left_Opnd (N), Etype (F1)) | |
5493 | and then Has_Compatible_Type (Right_Opnd (N), Etype (F2)) | |
5494 | then | |
5495 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
5496 | ||
8b4230c8 AC |
5497 | -- If the left operand is overloaded, indicate that the current |
5498 | -- type is a viable candidate. This is redundant in most cases, | |
5499 | -- but for equality and comparison operators where the context | |
5500 | -- does not impose a type on the operands, setting the proper | |
5501 | -- type is necessary to avoid subsequent ambiguities during | |
5502 | -- resolution, when both user-defined and predefined operators | |
5503 | -- may be candidates. | |
7340e432 AC |
5504 | |
5505 | if Is_Overloaded (Left_Opnd (N)) then | |
5506 | Set_Etype (Left_Opnd (N), Etype (F1)); | |
5507 | end if; | |
5508 | ||
996ae0b0 RK |
5509 | if Debug_Flag_E then |
5510 | Write_Str ("user defined operator "); | |
5511 | Write_Name (Chars (Op_Id)); | |
5512 | Write_Str (" on node "); | |
5513 | Write_Int (Int (N)); | |
5514 | Write_Eol; | |
5515 | end if; | |
5516 | end if; | |
5517 | end; | |
5518 | end if; | |
5519 | end Analyze_User_Defined_Binary_Op; | |
5520 | ||
5521 | ----------------------------------- | |
5522 | -- Analyze_User_Defined_Unary_Op -- | |
5523 | ----------------------------------- | |
5524 | ||
5525 | procedure Analyze_User_Defined_Unary_Op | |
5526 | (N : Node_Id; | |
5527 | Op_Id : Entity_Id) | |
5528 | is | |
5529 | begin | |
5530 | -- Only do analysis if the operator Comes_From_Source, since otherwise | |
5531 | -- the operator was generated by the expander, and all such operators | |
5532 | -- always refer to the operators in package Standard. | |
5533 | ||
5534 | if Comes_From_Source (N) then | |
5535 | declare | |
5536 | F : constant Entity_Id := First_Formal (Op_Id); | |
5537 | ||
5538 | begin | |
5539 | -- Verify that Op_Id is a visible unary function. Note that since | |
5540 | -- we know Op_Id is overloaded, potentially use visible means use | |
5541 | -- visible for sure (RM 9.4(11)). | |
5542 | ||
5543 | if Ekind (Op_Id) = E_Function | |
5544 | and then No (Next_Formal (F)) | |
5545 | and then (Is_Immediately_Visible (Op_Id) | |
5546 | or else Is_Potentially_Use_Visible (Op_Id)) | |
5547 | and then Has_Compatible_Type (Right_Opnd (N), Etype (F)) | |
5548 | then | |
5549 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
5550 | end if; | |
5551 | end; | |
5552 | end if; | |
5553 | end Analyze_User_Defined_Unary_Op; | |
5554 | ||
5555 | --------------------------- | |
5556 | -- Check_Arithmetic_Pair -- | |
5557 | --------------------------- | |
5558 | ||
5559 | procedure Check_Arithmetic_Pair | |
5560 | (T1, T2 : Entity_Id; | |
5561 | Op_Id : Entity_Id; | |
5562 | N : Node_Id) | |
5563 | is | |
401093c1 | 5564 | Op_Name : constant Name_Id := Chars (Op_Id); |
996ae0b0 | 5565 | |
da709d08 AC |
5566 | function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean; |
5567 | -- Check whether the fixed-point type Typ has a user-defined operator | |
5568 | -- (multiplication or division) that should hide the corresponding | |
5569 | -- predefined operator. Used to implement Ada 2005 AI-264, to make | |
5570 | -- such operators more visible and therefore useful. | |
8b4230c8 | 5571 | -- |
50cff367 GD |
5572 | -- If the name of the operation is an expanded name with prefix |
5573 | -- Standard, the predefined universal fixed operator is available, | |
5574 | -- as specified by AI-420 (RM 4.5.5 (19.1/2)). | |
5575 | ||
996ae0b0 RK |
5576 | function Specific_Type (T1, T2 : Entity_Id) return Entity_Id; |
5577 | -- Get specific type (i.e. non-universal type if there is one) | |
5578 | ||
da709d08 AC |
5579 | ------------------ |
5580 | -- Has_Fixed_Op -- | |
5581 | ------------------ | |
5582 | ||
5583 | function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is | |
401093c1 | 5584 | Bas : constant Entity_Id := Base_Type (Typ); |
da709d08 AC |
5585 | Ent : Entity_Id; |
5586 | F1 : Entity_Id; | |
5587 | F2 : Entity_Id; | |
5588 | ||
5589 | begin | |
50cff367 GD |
5590 | -- If the universal_fixed operation is given explicitly the rule |
5591 | -- concerning primitive operations of the type do not apply. | |
5592 | ||
5593 | if Nkind (N) = N_Function_Call | |
5594 | and then Nkind (Name (N)) = N_Expanded_Name | |
5595 | and then Entity (Prefix (Name (N))) = Standard_Standard | |
5596 | then | |
5597 | return False; | |
5598 | end if; | |
5599 | ||
da709d08 AC |
5600 | -- The operation is treated as primitive if it is declared in the |
5601 | -- same scope as the type, and therefore on the same entity chain. | |
5602 | ||
5603 | Ent := Next_Entity (Typ); | |
5604 | while Present (Ent) loop | |
5605 | if Chars (Ent) = Chars (Op) then | |
5606 | F1 := First_Formal (Ent); | |
5607 | F2 := Next_Formal (F1); | |
5608 | ||
5609 | -- The operation counts as primitive if either operand or | |
401093c1 ES |
5610 | -- result are of the given base type, and both operands are |
5611 | -- fixed point types. | |
da709d08 | 5612 | |
401093c1 | 5613 | if (Base_Type (Etype (F1)) = Bas |
da709d08 AC |
5614 | and then Is_Fixed_Point_Type (Etype (F2))) |
5615 | ||
5616 | or else | |
401093c1 | 5617 | (Base_Type (Etype (F2)) = Bas |
da709d08 AC |
5618 | and then Is_Fixed_Point_Type (Etype (F1))) |
5619 | ||
5620 | or else | |
401093c1 | 5621 | (Base_Type (Etype (Ent)) = Bas |
da709d08 AC |
5622 | and then Is_Fixed_Point_Type (Etype (F1)) |
5623 | and then Is_Fixed_Point_Type (Etype (F2))) | |
5624 | then | |
5625 | return True; | |
5626 | end if; | |
5627 | end if; | |
5628 | ||
5629 | Next_Entity (Ent); | |
5630 | end loop; | |
5631 | ||
5632 | return False; | |
5633 | end Has_Fixed_Op; | |
5634 | ||
4c46b835 AC |
5635 | ------------------- |
5636 | -- Specific_Type -- | |
5637 | ------------------- | |
5638 | ||
996ae0b0 RK |
5639 | function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is |
5640 | begin | |
5641 | if T1 = Universal_Integer or else T1 = Universal_Real then | |
5642 | return Base_Type (T2); | |
5643 | else | |
5644 | return Base_Type (T1); | |
5645 | end if; | |
5646 | end Specific_Type; | |
5647 | ||
5648 | -- Start of processing for Check_Arithmetic_Pair | |
5649 | ||
5650 | begin | |
b69cd36a | 5651 | if Nam_In (Op_Name, Name_Op_Add, Name_Op_Subtract) then |
996ae0b0 RK |
5652 | if Is_Numeric_Type (T1) |
5653 | and then Is_Numeric_Type (T2) | |
b4592168 GD |
5654 | and then (Covers (T1 => T1, T2 => T2) |
5655 | or else | |
5656 | Covers (T1 => T2, T2 => T1)) | |
996ae0b0 RK |
5657 | then |
5658 | Add_One_Interp (N, Op_Id, Specific_Type (T1, T2)); | |
5659 | end if; | |
5660 | ||
b69cd36a | 5661 | elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide) then |
996ae0b0 | 5662 | if Is_Fixed_Point_Type (T1) |
b69cd36a | 5663 | and then (Is_Fixed_Point_Type (T2) or else T2 = Universal_Real) |
996ae0b0 RK |
5664 | then |
5665 | -- If Treat_Fixed_As_Integer is set then the Etype is already set | |
5666 | -- and no further processing is required (this is the case of an | |
5667 | -- operator constructed by Exp_Fixd for a fixed point operation) | |
5668 | -- Otherwise add one interpretation with universal fixed result | |
21d7ef70 | 5669 | -- If the operator is given in functional notation, it comes |
996ae0b0 RK |
5670 | -- from source and Fixed_As_Integer cannot apply. |
5671 | ||
da709d08 AC |
5672 | if (Nkind (N) not in N_Op |
5673 | or else not Treat_Fixed_As_Integer (N)) | |
5674 | and then | |
401093c1 | 5675 | (not Has_Fixed_Op (T1, Op_Id) |
da709d08 | 5676 | or else Nkind (Parent (N)) = N_Type_Conversion) |
fbf5a39b | 5677 | then |
996ae0b0 RK |
5678 | Add_One_Interp (N, Op_Id, Universal_Fixed); |
5679 | end if; | |
5680 | ||
5681 | elsif Is_Fixed_Point_Type (T2) | |
5682 | and then (Nkind (N) not in N_Op | |
5683 | or else not Treat_Fixed_As_Integer (N)) | |
5684 | and then T1 = Universal_Real | |
da709d08 | 5685 | and then |
401093c1 | 5686 | (not Has_Fixed_Op (T1, Op_Id) |
da709d08 | 5687 | or else Nkind (Parent (N)) = N_Type_Conversion) |
996ae0b0 RK |
5688 | then |
5689 | Add_One_Interp (N, Op_Id, Universal_Fixed); | |
5690 | ||
5691 | elsif Is_Numeric_Type (T1) | |
5692 | and then Is_Numeric_Type (T2) | |
b4592168 GD |
5693 | and then (Covers (T1 => T1, T2 => T2) |
5694 | or else | |
5695 | Covers (T1 => T2, T2 => T1)) | |
996ae0b0 RK |
5696 | then |
5697 | Add_One_Interp (N, Op_Id, Specific_Type (T1, T2)); | |
5698 | ||
5699 | elsif Is_Fixed_Point_Type (T1) | |
5700 | and then (Base_Type (T2) = Base_Type (Standard_Integer) | |
b69cd36a | 5701 | or else T2 = Universal_Integer) |
996ae0b0 RK |
5702 | then |
5703 | Add_One_Interp (N, Op_Id, T1); | |
5704 | ||
5705 | elsif T2 = Universal_Real | |
5706 | and then Base_Type (T1) = Base_Type (Standard_Integer) | |
5707 | and then Op_Name = Name_Op_Multiply | |
5708 | then | |
5709 | Add_One_Interp (N, Op_Id, Any_Fixed); | |
5710 | ||
5711 | elsif T1 = Universal_Real | |
5712 | and then Base_Type (T2) = Base_Type (Standard_Integer) | |
5713 | then | |
5714 | Add_One_Interp (N, Op_Id, Any_Fixed); | |
5715 | ||
5716 | elsif Is_Fixed_Point_Type (T2) | |
5717 | and then (Base_Type (T1) = Base_Type (Standard_Integer) | |
b69cd36a | 5718 | or else T1 = Universal_Integer) |
996ae0b0 RK |
5719 | and then Op_Name = Name_Op_Multiply |
5720 | then | |
5721 | Add_One_Interp (N, Op_Id, T2); | |
5722 | ||
5723 | elsif T1 = Universal_Real and then T2 = Universal_Integer then | |
5724 | Add_One_Interp (N, Op_Id, T1); | |
5725 | ||
5726 | elsif T2 = Universal_Real | |
5727 | and then T1 = Universal_Integer | |
5728 | and then Op_Name = Name_Op_Multiply | |
5729 | then | |
5730 | Add_One_Interp (N, Op_Id, T2); | |
5731 | end if; | |
5732 | ||
5733 | elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then | |
5734 | ||
5735 | -- Note: The fixed-point operands case with Treat_Fixed_As_Integer | |
5736 | -- set does not require any special processing, since the Etype is | |
5737 | -- already set (case of operation constructed by Exp_Fixed). | |
5738 | ||
5739 | if Is_Integer_Type (T1) | |
b4592168 GD |
5740 | and then (Covers (T1 => T1, T2 => T2) |
5741 | or else | |
5742 | Covers (T1 => T2, T2 => T1)) | |
996ae0b0 RK |
5743 | then |
5744 | Add_One_Interp (N, Op_Id, Specific_Type (T1, T2)); | |
5745 | end if; | |
5746 | ||
5747 | elsif Op_Name = Name_Op_Expon then | |
996ae0b0 RK |
5748 | if Is_Numeric_Type (T1) |
5749 | and then not Is_Fixed_Point_Type (T1) | |
5750 | and then (Base_Type (T2) = Base_Type (Standard_Integer) | |
5751 | or else T2 = Universal_Integer) | |
5752 | then | |
5753 | Add_One_Interp (N, Op_Id, Base_Type (T1)); | |
5754 | end if; | |
5755 | ||
5756 | else pragma Assert (Nkind (N) in N_Op_Shift); | |
5757 | ||
5758 | -- If not one of the predefined operators, the node may be one | |
5759 | -- of the intrinsic functions. Its kind is always specific, and | |
5760 | -- we can use it directly, rather than the name of the operation. | |
5761 | ||
5762 | if Is_Integer_Type (T1) | |
5763 | and then (Base_Type (T2) = Base_Type (Standard_Integer) | |
5764 | or else T2 = Universal_Integer) | |
5765 | then | |
5766 | Add_One_Interp (N, Op_Id, Base_Type (T1)); | |
5767 | end if; | |
5768 | end if; | |
5769 | end Check_Arithmetic_Pair; | |
5770 | ||
5771 | ------------------------------- | |
5772 | -- Check_Misspelled_Selector -- | |
5773 | ------------------------------- | |
5774 | ||
5775 | procedure Check_Misspelled_Selector | |
5776 | (Prefix : Entity_Id; | |
5777 | Sel : Node_Id) | |
5778 | is | |
5779 | Max_Suggestions : constant := 2; | |
5780 | Nr_Of_Suggestions : Natural := 0; | |
5781 | ||
5782 | Suggestion_1 : Entity_Id := Empty; | |
5783 | Suggestion_2 : Entity_Id := Empty; | |
5784 | ||
5785 | Comp : Entity_Id; | |
5786 | ||
5787 | begin | |
8b4230c8 AC |
5788 | -- All the components of the prefix of selector Sel are matched against |
5789 | -- Sel and a count is maintained of possible misspellings. When at | |
a90bd866 | 5790 | -- the end of the analysis there are one or two (not more) possible |
8b4230c8 AC |
5791 | -- misspellings, these misspellings will be suggested as possible |
5792 | -- correction. | |
996ae0b0 | 5793 | |
4c46b835 AC |
5794 | if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then |
5795 | ||
996ae0b0 | 5796 | -- Concurrent types should be handled as well ??? |
4c46b835 | 5797 | |
996ae0b0 RK |
5798 | return; |
5799 | end if; | |
5800 | ||
d469eabe HK |
5801 | Comp := First_Entity (Prefix); |
5802 | while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop | |
5803 | if Is_Visible_Component (Comp) then | |
5804 | if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then | |
5805 | Nr_Of_Suggestions := Nr_Of_Suggestions + 1; | |
5806 | ||
5807 | case Nr_Of_Suggestions is | |
5808 | when 1 => Suggestion_1 := Comp; | |
5809 | when 2 => Suggestion_2 := Comp; | |
e49de265 | 5810 | when others => null; |
d469eabe | 5811 | end case; |
996ae0b0 | 5812 | end if; |
d469eabe | 5813 | end if; |
996ae0b0 | 5814 | |
d469eabe HK |
5815 | Comp := Next_Entity (Comp); |
5816 | end loop; | |
996ae0b0 | 5817 | |
d469eabe | 5818 | -- Report at most two suggestions |
996ae0b0 | 5819 | |
d469eabe | 5820 | if Nr_Of_Suggestions = 1 then |
4e7a4f6e | 5821 | Error_Msg_NE -- CODEFIX |
d469eabe | 5822 | ("\possible misspelling of&", Sel, Suggestion_1); |
996ae0b0 | 5823 | |
d469eabe HK |
5824 | elsif Nr_Of_Suggestions = 2 then |
5825 | Error_Msg_Node_2 := Suggestion_2; | |
4e7a4f6e | 5826 | Error_Msg_NE -- CODEFIX |
d469eabe HK |
5827 | ("\possible misspelling of& or&", Sel, Suggestion_1); |
5828 | end if; | |
996ae0b0 RK |
5829 | end Check_Misspelled_Selector; |
5830 | ||
5831 | ---------------------- | |
5832 | -- Defined_In_Scope -- | |
5833 | ---------------------- | |
5834 | ||
5835 | function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean | |
5836 | is | |
5837 | S1 : constant Entity_Id := Scope (Base_Type (T)); | |
996ae0b0 RK |
5838 | begin |
5839 | return S1 = S | |
5840 | or else (S1 = System_Aux_Id and then S = Scope (S1)); | |
5841 | end Defined_In_Scope; | |
5842 | ||
5843 | ------------------- | |
5844 | -- Diagnose_Call -- | |
5845 | ------------------- | |
5846 | ||
5847 | procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is | |
fbf5a39b AC |
5848 | Actual : Node_Id; |
5849 | X : Interp_Index; | |
5850 | It : Interp; | |
fbf5a39b AC |
5851 | Err_Mode : Boolean; |
5852 | New_Nam : Node_Id; | |
5853 | Void_Interp_Seen : Boolean := False; | |
996ae0b0 | 5854 | |
24657705 HK |
5855 | Success : Boolean; |
5856 | pragma Warnings (Off, Boolean); | |
5857 | ||
996ae0b0 | 5858 | begin |
0791fbe9 | 5859 | if Ada_Version >= Ada_2005 then |
996ae0b0 | 5860 | Actual := First_Actual (N); |
996ae0b0 | 5861 | while Present (Actual) loop |
0ab80019 AC |
5862 | |
5863 | -- Ada 2005 (AI-50217): Post an error in case of premature | |
5864 | -- usage of an entity from the limited view. | |
19f0526a | 5865 | |
996ae0b0 | 5866 | if not Analyzed (Etype (Actual)) |
7b56a91b | 5867 | and then From_Limited_With (Etype (Actual)) |
996ae0b0 RK |
5868 | then |
5869 | Error_Msg_Qual_Level := 1; | |
ed2233dc | 5870 | Error_Msg_NE |
996ae0b0 RK |
5871 | ("missing with_clause for scope of imported type&", |
5872 | Actual, Etype (Actual)); | |
5873 | Error_Msg_Qual_Level := 0; | |
5874 | end if; | |
5875 | ||
5876 | Next_Actual (Actual); | |
5877 | end loop; | |
5878 | end if; | |
5879 | ||
fbf5a39b AC |
5880 | -- Analyze each candidate call again, with full error reporting |
5881 | -- for each. | |
5882 | ||
5883 | Error_Msg_N | |
5884 | ("no candidate interpretations match the actuals:!", Nam); | |
5885 | Err_Mode := All_Errors_Mode; | |
5886 | All_Errors_Mode := True; | |
5887 | ||
5888 | -- If this is a call to an operation of a concurrent type, | |
5889 | -- the failed interpretations have been removed from the | |
5890 | -- name. Recover them to provide full diagnostics. | |
5891 | ||
5892 | if Nkind (Parent (Nam)) = N_Selected_Component then | |
5893 | Set_Entity (Nam, Empty); | |
5894 | New_Nam := New_Copy_Tree (Parent (Nam)); | |
5895 | Set_Is_Overloaded (New_Nam, False); | |
5896 | Set_Is_Overloaded (Selector_Name (New_Nam), False); | |
5897 | Set_Parent (New_Nam, Parent (Parent (Nam))); | |
5898 | Analyze_Selected_Component (New_Nam); | |
5899 | Get_First_Interp (Selector_Name (New_Nam), X, It); | |
5900 | else | |
996ae0b0 | 5901 | Get_First_Interp (Nam, X, It); |
fbf5a39b | 5902 | end if; |
996ae0b0 | 5903 | |
fbf5a39b AC |
5904 | while Present (It.Nam) loop |
5905 | if Etype (It.Nam) = Standard_Void_Type then | |
5906 | Void_Interp_Seen := True; | |
996ae0b0 | 5907 | end if; |
fbf5a39b AC |
5908 | |
5909 | Analyze_One_Call (N, It.Nam, True, Success); | |
5910 | Get_Next_Interp (X, It); | |
5911 | end loop; | |
996ae0b0 RK |
5912 | |
5913 | if Nkind (N) = N_Function_Call then | |
5914 | Get_First_Interp (Nam, X, It); | |
996ae0b0 | 5915 | while Present (It.Nam) loop |
bce79204 | 5916 | if Ekind_In (It.Nam, E_Function, E_Operator) then |
996ae0b0 RK |
5917 | return; |
5918 | else | |
5919 | Get_Next_Interp (X, It); | |
5920 | end if; | |
5921 | end loop; | |
5922 | ||
5923 | -- If all interpretations are procedures, this deserves a | |
5924 | -- more precise message. Ditto if this appears as the prefix | |
5925 | -- of a selected component, which may be a lexical error. | |
5926 | ||
4c46b835 AC |
5927 | Error_Msg_N |
5928 | ("\context requires function call, found procedure name", Nam); | |
996ae0b0 RK |
5929 | |
5930 | if Nkind (Parent (N)) = N_Selected_Component | |
5931 | and then N = Prefix (Parent (N)) | |
5932 | then | |
4e7a4f6e AC |
5933 | Error_Msg_N -- CODEFIX |
5934 | ("\period should probably be semicolon", Parent (N)); | |
996ae0b0 | 5935 | end if; |
fbf5a39b AC |
5936 | |
5937 | elsif Nkind (N) = N_Procedure_Call_Statement | |
5938 | and then not Void_Interp_Seen | |
5939 | then | |
5940 | Error_Msg_N ( | |
5941 | "\function name found in procedure call", Nam); | |
996ae0b0 | 5942 | end if; |
fbf5a39b AC |
5943 | |
5944 | All_Errors_Mode := Err_Mode; | |
996ae0b0 RK |
5945 | end Diagnose_Call; |
5946 | ||
5947 | --------------------------- | |
5948 | -- Find_Arithmetic_Types -- | |
5949 | --------------------------- | |
5950 | ||
5951 | procedure Find_Arithmetic_Types | |
5952 | (L, R : Node_Id; | |
5953 | Op_Id : Entity_Id; | |
5954 | N : Node_Id) | |
5955 | is | |
4c46b835 AC |
5956 | Index1 : Interp_Index; |
5957 | Index2 : Interp_Index; | |
5958 | It1 : Interp; | |
5959 | It2 : Interp; | |
996ae0b0 RK |
5960 | |
5961 | procedure Check_Right_Argument (T : Entity_Id); | |
5962 | -- Check right operand of operator | |
5963 | ||
4c46b835 AC |
5964 | -------------------------- |
5965 | -- Check_Right_Argument -- | |
5966 | -------------------------- | |
5967 | ||
996ae0b0 RK |
5968 | procedure Check_Right_Argument (T : Entity_Id) is |
5969 | begin | |
5970 | if not Is_Overloaded (R) then | |
5971 | Check_Arithmetic_Pair (T, Etype (R), Op_Id, N); | |
5972 | else | |
5973 | Get_First_Interp (R, Index2, It2); | |
996ae0b0 RK |
5974 | while Present (It2.Typ) loop |
5975 | Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N); | |
5976 | Get_Next_Interp (Index2, It2); | |
5977 | end loop; | |
5978 | end if; | |
5979 | end Check_Right_Argument; | |
5980 | ||
d8221f45 | 5981 | -- Start of processing for Find_Arithmetic_Types |
996ae0b0 RK |
5982 | |
5983 | begin | |
5984 | if not Is_Overloaded (L) then | |
5985 | Check_Right_Argument (Etype (L)); | |
5986 | ||
5987 | else | |
5988 | Get_First_Interp (L, Index1, It1); | |
996ae0b0 RK |
5989 | while Present (It1.Typ) loop |
5990 | Check_Right_Argument (It1.Typ); | |
5991 | Get_Next_Interp (Index1, It1); | |
5992 | end loop; | |
5993 | end if; | |
5994 | ||
5995 | end Find_Arithmetic_Types; | |
5996 | ||
5997 | ------------------------ | |
5998 | -- Find_Boolean_Types -- | |
5999 | ------------------------ | |
6000 | ||
6001 | procedure Find_Boolean_Types | |
6002 | (L, R : Node_Id; | |
6003 | Op_Id : Entity_Id; | |
6004 | N : Node_Id) | |
6005 | is | |
6006 | Index : Interp_Index; | |
6007 | It : Interp; | |
6008 | ||
6009 | procedure Check_Numeric_Argument (T : Entity_Id); | |
6010 | -- Special case for logical operations one of whose operands is an | |
6011 | -- integer literal. If both are literal the result is any modular type. | |
6012 | ||
4c46b835 AC |
6013 | ---------------------------- |
6014 | -- Check_Numeric_Argument -- | |
6015 | ---------------------------- | |
6016 | ||
996ae0b0 RK |
6017 | procedure Check_Numeric_Argument (T : Entity_Id) is |
6018 | begin | |
6019 | if T = Universal_Integer then | |
6020 | Add_One_Interp (N, Op_Id, Any_Modular); | |
6021 | ||
6022 | elsif Is_Modular_Integer_Type (T) then | |
6023 | Add_One_Interp (N, Op_Id, T); | |
6024 | end if; | |
6025 | end Check_Numeric_Argument; | |
6026 | ||
6027 | -- Start of processing for Find_Boolean_Types | |
6028 | ||
6029 | begin | |
6030 | if not Is_Overloaded (L) then | |
996ae0b0 RK |
6031 | if Etype (L) = Universal_Integer |
6032 | or else Etype (L) = Any_Modular | |
6033 | then | |
6034 | if not Is_Overloaded (R) then | |
6035 | Check_Numeric_Argument (Etype (R)); | |
6036 | ||
6037 | else | |
6038 | Get_First_Interp (R, Index, It); | |
996ae0b0 RK |
6039 | while Present (It.Typ) loop |
6040 | Check_Numeric_Argument (It.Typ); | |
996ae0b0 RK |
6041 | Get_Next_Interp (Index, It); |
6042 | end loop; | |
6043 | end if; | |
6044 | ||
69e6a03e ES |
6045 | -- If operands are aggregates, we must assume that they may be |
6046 | -- boolean arrays, and leave disambiguation for the second pass. | |
6047 | -- If only one is an aggregate, verify that the other one has an | |
6048 | -- interpretation as a boolean array | |
6049 | ||
6050 | elsif Nkind (L) = N_Aggregate then | |
6051 | if Nkind (R) = N_Aggregate then | |
6052 | Add_One_Interp (N, Op_Id, Etype (L)); | |
6053 | ||
6054 | elsif not Is_Overloaded (R) then | |
6055 | if Valid_Boolean_Arg (Etype (R)) then | |
6056 | Add_One_Interp (N, Op_Id, Etype (R)); | |
6057 | end if; | |
6058 | ||
6059 | else | |
6060 | Get_First_Interp (R, Index, It); | |
6061 | while Present (It.Typ) loop | |
6062 | if Valid_Boolean_Arg (It.Typ) then | |
6063 | Add_One_Interp (N, Op_Id, It.Typ); | |
6064 | end if; | |
6065 | ||
6066 | Get_Next_Interp (Index, It); | |
6067 | end loop; | |
6068 | end if; | |
6069 | ||
996ae0b0 RK |
6070 | elsif Valid_Boolean_Arg (Etype (L)) |
6071 | and then Has_Compatible_Type (R, Etype (L)) | |
6072 | then | |
6073 | Add_One_Interp (N, Op_Id, Etype (L)); | |
6074 | end if; | |
6075 | ||
6076 | else | |
6077 | Get_First_Interp (L, Index, It); | |
996ae0b0 RK |
6078 | while Present (It.Typ) loop |
6079 | if Valid_Boolean_Arg (It.Typ) | |
6080 | and then Has_Compatible_Type (R, It.Typ) | |
6081 | then | |
6082 | Add_One_Interp (N, Op_Id, It.Typ); | |
6083 | end if; | |
6084 | ||
6085 | Get_Next_Interp (Index, It); | |
6086 | end loop; | |
6087 | end if; | |
6088 | end Find_Boolean_Types; | |
6089 | ||
6090 | --------------------------- | |
6091 | -- Find_Comparison_Types -- | |
6092 | --------------------------- | |
6093 | ||
6094 | procedure Find_Comparison_Types | |
6095 | (L, R : Node_Id; | |
6096 | Op_Id : Entity_Id; | |
6097 | N : Node_Id) | |
6098 | is | |
6099 | Index : Interp_Index; | |
6100 | It : Interp; | |
6101 | Found : Boolean := False; | |
6102 | I_F : Interp_Index; | |
6103 | T_F : Entity_Id; | |
6104 | Scop : Entity_Id := Empty; | |
6105 | ||
6106 | procedure Try_One_Interp (T1 : Entity_Id); | |
6107 | -- Routine to try one proposed interpretation. Note that the context | |
6108 | -- of the operator plays no role in resolving the arguments, so that | |
6109 | -- if there is more than one interpretation of the operands that is | |
6110 | -- compatible with comparison, the operation is ambiguous. | |
6111 | ||
4c46b835 AC |
6112 | -------------------- |
6113 | -- Try_One_Interp -- | |
6114 | -------------------- | |
6115 | ||
996ae0b0 RK |
6116 | procedure Try_One_Interp (T1 : Entity_Id) is |
6117 | begin | |
6118 | ||
6119 | -- If the operator is an expanded name, then the type of the operand | |
6120 | -- must be defined in the corresponding scope. If the type is | |
6121 | -- universal, the context will impose the correct type. | |
6122 | ||
6123 | if Present (Scop) | |
8b4230c8 AC |
6124 | and then not Defined_In_Scope (T1, Scop) |
6125 | and then T1 /= Universal_Integer | |
6126 | and then T1 /= Universal_Real | |
6127 | and then T1 /= Any_String | |
6128 | and then T1 /= Any_Composite | |
996ae0b0 RK |
6129 | then |
6130 | return; | |
6131 | end if; | |
6132 | ||
8b4230c8 AC |
6133 | if Valid_Comparison_Arg (T1) and then Has_Compatible_Type (R, T1) then |
6134 | if Found and then Base_Type (T1) /= Base_Type (T_F) then | |
996ae0b0 RK |
6135 | It := Disambiguate (L, I_F, Index, Any_Type); |
6136 | ||
6137 | if It = No_Interp then | |
6138 | Ambiguous_Operands (N); | |
6139 | Set_Etype (L, Any_Type); | |
6140 | return; | |
6141 | ||
6142 | else | |
6143 | T_F := It.Typ; | |
6144 | end if; | |
6145 | ||
6146 | else | |
6147 | Found := True; | |
6148 | T_F := T1; | |
6149 | I_F := Index; | |
6150 | end if; | |
6151 | ||
6152 | Set_Etype (L, T_F); | |
6153 | Find_Non_Universal_Interpretations (N, R, Op_Id, T1); | |
6154 | ||
6155 | end if; | |
6156 | end Try_One_Interp; | |
6157 | ||
d8221f45 | 6158 | -- Start of processing for Find_Comparison_Types |
996ae0b0 RK |
6159 | |
6160 | begin | |
fbf5a39b AC |
6161 | -- If left operand is aggregate, the right operand has to |
6162 | -- provide a usable type for it. | |
6163 | ||
8b4230c8 | 6164 | if Nkind (L) = N_Aggregate and then Nkind (R) /= N_Aggregate then |
b4592168 | 6165 | Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N); |
fbf5a39b AC |
6166 | return; |
6167 | end if; | |
996ae0b0 RK |
6168 | |
6169 | if Nkind (N) = N_Function_Call | |
6170 | and then Nkind (Name (N)) = N_Expanded_Name | |
6171 | then | |
6172 | Scop := Entity (Prefix (Name (N))); | |
6173 | ||
6174 | -- The prefix may be a package renaming, and the subsequent test | |
6175 | -- requires the original package. | |
6176 | ||
6177 | if Ekind (Scop) = E_Package | |
6178 | and then Present (Renamed_Entity (Scop)) | |
6179 | then | |
6180 | Scop := Renamed_Entity (Scop); | |
6181 | Set_Entity (Prefix (Name (N)), Scop); | |
6182 | end if; | |
6183 | end if; | |
6184 | ||
6185 | if not Is_Overloaded (L) then | |
6186 | Try_One_Interp (Etype (L)); | |
6187 | ||
6188 | else | |
6189 | Get_First_Interp (L, Index, It); | |
996ae0b0 RK |
6190 | while Present (It.Typ) loop |
6191 | Try_One_Interp (It.Typ); | |
6192 | Get_Next_Interp (Index, It); | |
6193 | end loop; | |
6194 | end if; | |
6195 | end Find_Comparison_Types; | |
6196 | ||
6197 | ---------------------------------------- | |
6198 | -- Find_Non_Universal_Interpretations -- | |
6199 | ---------------------------------------- | |
6200 | ||
6201 | procedure Find_Non_Universal_Interpretations | |
6202 | (N : Node_Id; | |
6203 | R : Node_Id; | |
6204 | Op_Id : Entity_Id; | |
6205 | T1 : Entity_Id) | |
6206 | is | |
6207 | Index : Interp_Index; | |
4c46b835 | 6208 | It : Interp; |
996ae0b0 RK |
6209 | |
6210 | begin | |
8b4230c8 | 6211 | if T1 = Universal_Integer or else T1 = Universal_Real |
b9daa96e AC |
6212 | |
6213 | -- If the left operand of an equality operator is null, the visibility | |
6214 | -- of the operator must be determined from the interpretation of the | |
6215 | -- right operand. This processing must be done for Any_Access, which | |
6216 | -- is the internal representation of the type of the literal null. | |
6217 | ||
be4c5193 | 6218 | or else T1 = Any_Access |
996ae0b0 RK |
6219 | then |
6220 | if not Is_Overloaded (R) then | |
8b4230c8 | 6221 | Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (Etype (R))); |
996ae0b0 RK |
6222 | else |
6223 | Get_First_Interp (R, Index, It); | |
996ae0b0 RK |
6224 | while Present (It.Typ) loop |
6225 | if Covers (It.Typ, T1) then | |
6226 | Add_One_Interp | |
6227 | (N, Op_Id, Standard_Boolean, Base_Type (It.Typ)); | |
6228 | end if; | |
6229 | ||
6230 | Get_Next_Interp (Index, It); | |
6231 | end loop; | |
6232 | end if; | |
6233 | else | |
6234 | Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1)); | |
6235 | end if; | |
6236 | end Find_Non_Universal_Interpretations; | |
6237 | ||
6238 | ------------------------------ | |
6239 | -- Find_Concatenation_Types -- | |
6240 | ------------------------------ | |
6241 | ||
6242 | procedure Find_Concatenation_Types | |
6243 | (L, R : Node_Id; | |
6244 | Op_Id : Entity_Id; | |
6245 | N : Node_Id) | |
6246 | is | |
6247 | Op_Type : constant Entity_Id := Etype (Op_Id); | |
6248 | ||
6249 | begin | |
6250 | if Is_Array_Type (Op_Type) | |
6251 | and then not Is_Limited_Type (Op_Type) | |
6252 | ||
6253 | and then (Has_Compatible_Type (L, Op_Type) | |
6254 | or else | |
6255 | Has_Compatible_Type (L, Component_Type (Op_Type))) | |
6256 | ||
6257 | and then (Has_Compatible_Type (R, Op_Type) | |
6258 | or else | |
6259 | Has_Compatible_Type (R, Component_Type (Op_Type))) | |
6260 | then | |
6261 | Add_One_Interp (N, Op_Id, Op_Type); | |
6262 | end if; | |
6263 | end Find_Concatenation_Types; | |
6264 | ||
6265 | ------------------------- | |
6266 | -- Find_Equality_Types -- | |
6267 | ------------------------- | |
6268 | ||
6269 | procedure Find_Equality_Types | |
6270 | (L, R : Node_Id; | |
6271 | Op_Id : Entity_Id; | |
6272 | N : Node_Id) | |
6273 | is | |
6274 | Index : Interp_Index; | |
6275 | It : Interp; | |
6276 | Found : Boolean := False; | |
6277 | I_F : Interp_Index; | |
6278 | T_F : Entity_Id; | |
6279 | Scop : Entity_Id := Empty; | |
6280 | ||
6281 | procedure Try_One_Interp (T1 : Entity_Id); | |
a8e65aa5 AC |
6282 | -- The context of the equality operator plays no role in resolving the |
6283 | -- arguments, so that if there is more than one interpretation of the | |
6284 | -- operands that is compatible with equality, the construct is ambiguous | |
6285 | -- and an error can be emitted now, after trying to disambiguate, i.e. | |
6286 | -- applying preference rules. | |
996ae0b0 | 6287 | |
4c46b835 AC |
6288 | -------------------- |
6289 | -- Try_One_Interp -- | |
6290 | -------------------- | |
6291 | ||
996ae0b0 | 6292 | procedure Try_One_Interp (T1 : Entity_Id) is |
a8e65aa5 AC |
6293 | Bas : constant Entity_Id := Base_Type (T1); |
6294 | ||
996ae0b0 | 6295 | begin |
996ae0b0 RK |
6296 | -- If the operator is an expanded name, then the type of the operand |
6297 | -- must be defined in the corresponding scope. If the type is | |
6298 | -- universal, the context will impose the correct type. An anonymous | |
6299 | -- type for a 'Access reference is also universal in this sense, as | |
6300 | -- the actual type is obtained from context. | |
8b4230c8 | 6301 | |
fe45e59e ES |
6302 | -- In Ada 2005, the equality operator for anonymous access types |
6303 | -- is declared in Standard, and preference rules apply to it. | |
996ae0b0 | 6304 | |
fe45e59e ES |
6305 | if Present (Scop) then |
6306 | if Defined_In_Scope (T1, Scop) | |
6307 | or else T1 = Universal_Integer | |
6308 | or else T1 = Universal_Real | |
6309 | or else T1 = Any_Access | |
6310 | or else T1 = Any_String | |
6311 | or else T1 = Any_Composite | |
6312 | or else (Ekind (T1) = E_Access_Subprogram_Type | |
a8e65aa5 | 6313 | and then not Comes_From_Source (T1)) |
fe45e59e ES |
6314 | then |
6315 | null; | |
6316 | ||
6317 | elsif Ekind (T1) = E_Anonymous_Access_Type | |
6318 | and then Scop = Standard_Standard | |
6319 | then | |
6320 | null; | |
6321 | ||
6322 | else | |
6323 | -- The scope does not contain an operator for the type | |
6324 | ||
6325 | return; | |
6326 | end if; | |
a8e65aa5 | 6327 | |
9aa04cc7 AC |
6328 | -- If we have infix notation, the operator must be usable. Within |
6329 | -- an instance, if the type is already established we know it is | |
6330 | -- correct. If an operand is universal it is compatible with any | |
6331 | -- numeric type. | |
31af8899 | 6332 | |
a8e65aa5 AC |
6333 | elsif In_Open_Scopes (Scope (Bas)) |
6334 | or else Is_Potentially_Use_Visible (Bas) | |
6335 | or else In_Use (Bas) | |
9aa04cc7 | 6336 | or else (In_Use (Scope (Bas)) and then not Is_Hidden (Bas)) |
a54ffd6c AC |
6337 | |
6338 | -- In an instance, the type may have been immediately visible. | |
6339 | -- Either the types are compatible, or one operand is universal | |
6340 | -- (numeric or null). | |
6341 | ||
a8e65aa5 | 6342 | or else (In_Instance |
9aa04cc7 AC |
6343 | and then |
6344 | (First_Subtype (T1) = First_Subtype (Etype (R)) | |
4f7c83ca | 6345 | or else Nkind (R) = N_Null |
9aa04cc7 AC |
6346 | or else |
6347 | (Is_Numeric_Type (T1) | |
6348 | and then Is_Universal_Numeric_Type (Etype (R))))) | |
a54ffd6c AC |
6349 | |
6350 | -- In Ada 2005, the equality on anonymous access types is declared | |
6351 | -- in Standard, and is always visible. | |
6352 | ||
a8e65aa5 AC |
6353 | or else Ekind (T1) = E_Anonymous_Access_Type |
6354 | then | |
6355 | null; | |
6356 | ||
6357 | else | |
308e6f3a | 6358 | -- Save candidate type for subsequent error message, if any |
a8e65aa5 AC |
6359 | |
6360 | if not Is_Limited_Type (T1) then | |
6361 | Candidate_Type := T1; | |
6362 | end if; | |
6363 | ||
6364 | return; | |
996ae0b0 RK |
6365 | end if; |
6366 | ||
0ab80019 AC |
6367 | -- Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95: |
6368 | -- Do not allow anonymous access types in equality operators. | |
6e937c1c | 6369 | |
0791fbe9 | 6370 | if Ada_Version < Ada_2005 |
6e937c1c AC |
6371 | and then Ekind (T1) = E_Anonymous_Access_Type |
6372 | then | |
6373 | return; | |
6374 | end if; | |
6375 | ||
c92e8586 AC |
6376 | -- If the right operand has a type compatible with T1, check for an |
6377 | -- acceptable interpretation, unless T1 is limited (no predefined | |
6378 | -- equality available), or this is use of a "/=" for a tagged type. | |
8b4230c8 AC |
6379 | -- In the latter case, possible interpretations of equality need |
6380 | -- to be considered, we don't want the default inequality declared | |
6381 | -- in Standard to be chosen, and the "/=" will be rewritten as a | |
c92e8586 | 6382 | -- negation of "=" (see the end of Analyze_Equality_Op). This ensures |
026c3cfd | 6383 | -- that rewriting happens during analysis rather than being |
c92e8586 AC |
6384 | -- delayed until expansion (this is needed for ASIS, which only sees |
6385 | -- the unexpanded tree). Note that if the node is N_Op_Ne, but Op_Id | |
6386 | -- is Name_Op_Eq then we still proceed with the interpretation, | |
6387 | -- because that indicates the potential rewriting case where the | |
6388 | -- interpretation to consider is actually "=" and the node may be | |
6389 | -- about to be rewritten by Analyze_Equality_Op. | |
6390 | ||
996ae0b0 | 6391 | if T1 /= Standard_Void_Type |
996ae0b0 | 6392 | and then Has_Compatible_Type (R, T1) |
c92e8586 | 6393 | |
9b62eb32 AC |
6394 | and then |
6395 | ((not Is_Limited_Type (T1) | |
6396 | and then not Is_Limited_Composite (T1)) | |
6397 | ||
6398 | or else | |
2fcc44fa | 6399 | (Is_Array_Type (T1) |
9b62eb32 AC |
6400 | and then not Is_Limited_Type (Component_Type (T1)) |
6401 | and then Available_Full_View_Of_Component (T1))) | |
c92e8586 AC |
6402 | |
6403 | and then | |
6404 | (Nkind (N) /= N_Op_Ne | |
6405 | or else not Is_Tagged_Type (T1) | |
6406 | or else Chars (Op_Id) = Name_Op_Eq) | |
996ae0b0 RK |
6407 | then |
6408 | if Found | |
6409 | and then Base_Type (T1) /= Base_Type (T_F) | |
6410 | then | |
6411 | It := Disambiguate (L, I_F, Index, Any_Type); | |
6412 | ||
6413 | if It = No_Interp then | |
6414 | Ambiguous_Operands (N); | |
6415 | Set_Etype (L, Any_Type); | |
6416 | return; | |
6417 | ||
6418 | else | |
6419 | T_F := It.Typ; | |
6420 | end if; | |
6421 | ||
6422 | else | |
6423 | Found := True; | |
6424 | T_F := T1; | |
6425 | I_F := Index; | |
6426 | end if; | |
6427 | ||
6428 | if not Analyzed (L) then | |
6429 | Set_Etype (L, T_F); | |
6430 | end if; | |
6431 | ||
6432 | Find_Non_Universal_Interpretations (N, R, Op_Id, T1); | |
6433 | ||
6e73e3ab | 6434 | -- Case of operator was not visible, Etype still set to Any_Type |
996ae0b0 | 6435 | |
6e73e3ab | 6436 | if Etype (N) = Any_Type then |
996ae0b0 RK |
6437 | Found := False; |
6438 | end if; | |
fe45e59e ES |
6439 | |
6440 | elsif Scop = Standard_Standard | |
6441 | and then Ekind (T1) = E_Anonymous_Access_Type | |
6442 | then | |
6443 | Found := True; | |
996ae0b0 RK |
6444 | end if; |
6445 | end Try_One_Interp; | |
6446 | ||
6447 | -- Start of processing for Find_Equality_Types | |
6448 | ||
6449 | begin | |
fbf5a39b AC |
6450 | -- If left operand is aggregate, the right operand has to |
6451 | -- provide a usable type for it. | |
6452 | ||
6453 | if Nkind (L) = N_Aggregate | |
6454 | and then Nkind (R) /= N_Aggregate | |
6455 | then | |
b4592168 | 6456 | Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N); |
fbf5a39b AC |
6457 | return; |
6458 | end if; | |
996ae0b0 RK |
6459 | |
6460 | if Nkind (N) = N_Function_Call | |
6461 | and then Nkind (Name (N)) = N_Expanded_Name | |
6462 | then | |
6463 | Scop := Entity (Prefix (Name (N))); | |
6464 | ||
6465 | -- The prefix may be a package renaming, and the subsequent test | |
6466 | -- requires the original package. | |
6467 | ||
6468 | if Ekind (Scop) = E_Package | |
6469 | and then Present (Renamed_Entity (Scop)) | |
6470 | then | |
6471 | Scop := Renamed_Entity (Scop); | |
6472 | Set_Entity (Prefix (Name (N)), Scop); | |
6473 | end if; | |
6474 | end if; | |
6475 | ||
6476 | if not Is_Overloaded (L) then | |
6477 | Try_One_Interp (Etype (L)); | |
996ae0b0 | 6478 | |
4c46b835 | 6479 | else |
996ae0b0 | 6480 | Get_First_Interp (L, Index, It); |
996ae0b0 RK |
6481 | while Present (It.Typ) loop |
6482 | Try_One_Interp (It.Typ); | |
6483 | Get_Next_Interp (Index, It); | |
6484 | end loop; | |
6485 | end if; | |
6486 | end Find_Equality_Types; | |
6487 | ||
6488 | ------------------------- | |
6489 | -- Find_Negation_Types -- | |
6490 | ------------------------- | |
6491 | ||
6492 | procedure Find_Negation_Types | |
6493 | (R : Node_Id; | |
6494 | Op_Id : Entity_Id; | |
6495 | N : Node_Id) | |
6496 | is | |
6497 | Index : Interp_Index; | |
6498 | It : Interp; | |
6499 | ||
6500 | begin | |
6501 | if not Is_Overloaded (R) then | |
996ae0b0 RK |
6502 | if Etype (R) = Universal_Integer then |
6503 | Add_One_Interp (N, Op_Id, Any_Modular); | |
996ae0b0 RK |
6504 | elsif Valid_Boolean_Arg (Etype (R)) then |
6505 | Add_One_Interp (N, Op_Id, Etype (R)); | |
6506 | end if; | |
6507 | ||
6508 | else | |
6509 | Get_First_Interp (R, Index, It); | |
996ae0b0 RK |
6510 | while Present (It.Typ) loop |
6511 | if Valid_Boolean_Arg (It.Typ) then | |
6512 | Add_One_Interp (N, Op_Id, It.Typ); | |
6513 | end if; | |
6514 | ||
6515 | Get_Next_Interp (Index, It); | |
6516 | end loop; | |
6517 | end if; | |
6518 | end Find_Negation_Types; | |
6519 | ||
d469eabe HK |
6520 | ------------------------------ |
6521 | -- Find_Primitive_Operation -- | |
6522 | ------------------------------ | |
6523 | ||
6524 | function Find_Primitive_Operation (N : Node_Id) return Boolean is | |
6525 | Obj : constant Node_Id := Prefix (N); | |
6526 | Op : constant Node_Id := Selector_Name (N); | |
6527 | ||
6528 | Prim : Elmt_Id; | |
6529 | Prims : Elist_Id; | |
6530 | Typ : Entity_Id; | |
6531 | ||
6532 | begin | |
6533 | Set_Etype (Op, Any_Type); | |
6534 | ||
6535 | if Is_Access_Type (Etype (Obj)) then | |
6536 | Typ := Designated_Type (Etype (Obj)); | |
6537 | else | |
6538 | Typ := Etype (Obj); | |
6539 | end if; | |
6540 | ||
6541 | if Is_Class_Wide_Type (Typ) then | |
6542 | Typ := Root_Type (Typ); | |
6543 | end if; | |
6544 | ||
6545 | Prims := Primitive_Operations (Typ); | |
6546 | ||
6547 | Prim := First_Elmt (Prims); | |
6548 | while Present (Prim) loop | |
6549 | if Chars (Node (Prim)) = Chars (Op) then | |
6550 | Add_One_Interp (Op, Node (Prim), Etype (Node (Prim))); | |
6551 | Set_Etype (N, Etype (Node (Prim))); | |
6552 | end if; | |
6553 | ||
6554 | Next_Elmt (Prim); | |
6555 | end loop; | |
6556 | ||
6557 | -- Now look for class-wide operations of the type or any of its | |
6558 | -- ancestors by iterating over the homonyms of the selector. | |
6559 | ||
6560 | declare | |
6561 | Cls_Type : constant Entity_Id := Class_Wide_Type (Typ); | |
6562 | Hom : Entity_Id; | |
6563 | ||
6564 | begin | |
6565 | Hom := Current_Entity (Op); | |
6566 | while Present (Hom) loop | |
6567 | if (Ekind (Hom) = E_Procedure | |
6568 | or else | |
6569 | Ekind (Hom) = E_Function) | |
6570 | and then Scope (Hom) = Scope (Typ) | |
6571 | and then Present (First_Formal (Hom)) | |
6572 | and then | |
6573 | (Base_Type (Etype (First_Formal (Hom))) = Cls_Type | |
6574 | or else | |
6575 | (Is_Access_Type (Etype (First_Formal (Hom))) | |
8b4230c8 AC |
6576 | and then |
6577 | Ekind (Etype (First_Formal (Hom))) = | |
6578 | E_Anonymous_Access_Type | |
6579 | and then | |
6580 | Base_Type | |
6581 | (Designated_Type (Etype (First_Formal (Hom)))) = | |
d469eabe HK |
6582 | Cls_Type)) |
6583 | then | |
6584 | Add_One_Interp (Op, Hom, Etype (Hom)); | |
6585 | Set_Etype (N, Etype (Hom)); | |
6586 | end if; | |
6587 | ||
6588 | Hom := Homonym (Hom); | |
6589 | end loop; | |
6590 | end; | |
6591 | ||
6592 | return Etype (Op) /= Any_Type; | |
6593 | end Find_Primitive_Operation; | |
6594 | ||
996ae0b0 RK |
6595 | ---------------------- |
6596 | -- Find_Unary_Types -- | |
6597 | ---------------------- | |
6598 | ||
6599 | procedure Find_Unary_Types | |
6600 | (R : Node_Id; | |
6601 | Op_Id : Entity_Id; | |
6602 | N : Node_Id) | |
6603 | is | |
6604 | Index : Interp_Index; | |
6605 | It : Interp; | |
6606 | ||
6607 | begin | |
6608 | if not Is_Overloaded (R) then | |
6609 | if Is_Numeric_Type (Etype (R)) then | |
65f7ed64 AC |
6610 | |
6611 | -- In an instance a generic actual may be a numeric type even if | |
6612 | -- the formal in the generic unit was not. In that case, the | |
6613 | -- predefined operator was not a possible interpretation in the | |
2e70d415 AC |
6614 | -- generic, and cannot be one in the instance, unless the operator |
6615 | -- is an actual of an instance. | |
65f7ed64 AC |
6616 | |
6617 | if In_Instance | |
6618 | and then | |
6619 | not Is_Numeric_Type (Corresponding_Generic_Type (Etype (R))) | |
6620 | then | |
6621 | null; | |
6622 | else | |
6623 | Add_One_Interp (N, Op_Id, Base_Type (Etype (R))); | |
6624 | end if; | |
996ae0b0 RK |
6625 | end if; |
6626 | ||
6627 | else | |
6628 | Get_First_Interp (R, Index, It); | |
996ae0b0 RK |
6629 | while Present (It.Typ) loop |
6630 | if Is_Numeric_Type (It.Typ) then | |
65f7ed64 AC |
6631 | if In_Instance |
6632 | and then | |
6633 | not Is_Numeric_Type | |
6634 | (Corresponding_Generic_Type (Etype (It.Typ))) | |
6635 | then | |
6636 | null; | |
6637 | ||
6638 | else | |
6639 | Add_One_Interp (N, Op_Id, Base_Type (It.Typ)); | |
6640 | end if; | |
996ae0b0 RK |
6641 | end if; |
6642 | ||
6643 | Get_Next_Interp (Index, It); | |
6644 | end loop; | |
6645 | end if; | |
6646 | end Find_Unary_Types; | |
6647 | ||
996ae0b0 RK |
6648 | ------------------ |
6649 | -- Junk_Operand -- | |
6650 | ------------------ | |
6651 | ||
6652 | function Junk_Operand (N : Node_Id) return Boolean is | |
6653 | Enode : Node_Id; | |
6654 | ||
6655 | begin | |
6656 | if Error_Posted (N) then | |
6657 | return False; | |
6658 | end if; | |
6659 | ||
6660 | -- Get entity to be tested | |
6661 | ||
6662 | if Is_Entity_Name (N) | |
6663 | and then Present (Entity (N)) | |
6664 | then | |
6665 | Enode := N; | |
6666 | ||
6667 | -- An odd case, a procedure name gets converted to a very peculiar | |
6668 | -- function call, and here is where we detect this happening. | |
6669 | ||
6670 | elsif Nkind (N) = N_Function_Call | |
6671 | and then Is_Entity_Name (Name (N)) | |
6672 | and then Present (Entity (Name (N))) | |
6673 | then | |
6674 | Enode := Name (N); | |
6675 | ||
6676 | -- Another odd case, there are at least some cases of selected | |
6677 | -- components where the selected component is not marked as having | |
6678 | -- an entity, even though the selector does have an entity | |
6679 | ||
6680 | elsif Nkind (N) = N_Selected_Component | |
6681 | and then Present (Entity (Selector_Name (N))) | |
6682 | then | |
6683 | Enode := Selector_Name (N); | |
6684 | ||
6685 | else | |
6686 | return False; | |
6687 | end if; | |
6688 | ||
9de61fcb | 6689 | -- Now test the entity we got to see if it is a bad case |
996ae0b0 RK |
6690 | |
6691 | case Ekind (Entity (Enode)) is | |
6692 | ||
6693 | when E_Package => | |
6694 | Error_Msg_N | |
6695 | ("package name cannot be used as operand", Enode); | |
6696 | ||
6697 | when Generic_Unit_Kind => | |
6698 | Error_Msg_N | |
6699 | ("generic unit name cannot be used as operand", Enode); | |
6700 | ||
6701 | when Type_Kind => | |
6702 | Error_Msg_N | |
6703 | ("subtype name cannot be used as operand", Enode); | |
6704 | ||
6705 | when Entry_Kind => | |
6706 | Error_Msg_N | |
6707 | ("entry name cannot be used as operand", Enode); | |
6708 | ||
6709 | when E_Procedure => | |
6710 | Error_Msg_N | |
6711 | ("procedure name cannot be used as operand", Enode); | |
6712 | ||
6713 | when E_Exception => | |
6714 | Error_Msg_N | |
6715 | ("exception name cannot be used as operand", Enode); | |
6716 | ||
6717 | when E_Block | E_Label | E_Loop => | |
6718 | Error_Msg_N | |
6719 | ("label name cannot be used as operand", Enode); | |
6720 | ||
6721 | when others => | |
6722 | return False; | |
6723 | ||
6724 | end case; | |
6725 | ||
6726 | return True; | |
6727 | end Junk_Operand; | |
6728 | ||
6729 | -------------------- | |
6730 | -- Operator_Check -- | |
6731 | -------------------- | |
6732 | ||
6733 | procedure Operator_Check (N : Node_Id) is | |
6734 | begin | |
30c20106 AC |
6735 | Remove_Abstract_Operations (N); |
6736 | ||
996ae0b0 RK |
6737 | -- Test for case of no interpretation found for operator |
6738 | ||
6739 | if Etype (N) = Any_Type then | |
6740 | declare | |
b67a385c ES |
6741 | L : Node_Id; |
6742 | R : Node_Id; | |
6743 | Op_Id : Entity_Id := Empty; | |
996ae0b0 RK |
6744 | |
6745 | begin | |
6746 | R := Right_Opnd (N); | |
6747 | ||
6748 | if Nkind (N) in N_Binary_Op then | |
6749 | L := Left_Opnd (N); | |
6750 | else | |
6751 | L := Empty; | |
6752 | end if; | |
6753 | ||
6754 | -- If either operand has no type, then don't complain further, | |
9de61fcb | 6755 | -- since this simply means that we have a propagated error. |
996ae0b0 RK |
6756 | |
6757 | if R = Error | |
6758 | or else Etype (R) = Any_Type | |
6759 | or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type) | |
6760 | then | |
7610fee8 AC |
6761 | -- For the rather unusual case where one of the operands is |
6762 | -- a Raise_Expression, whose initial type is Any_Type, use | |
6763 | -- the type of the other operand. | |
6764 | ||
6765 | if Nkind (L) = N_Raise_Expression then | |
6766 | Set_Etype (L, Etype (R)); | |
6767 | Set_Etype (N, Etype (R)); | |
6768 | ||
6769 | elsif Nkind (R) = N_Raise_Expression then | |
6770 | Set_Etype (R, Etype (L)); | |
6771 | Set_Etype (N, Etype (L)); | |
6772 | end if; | |
6773 | ||
996ae0b0 RK |
6774 | return; |
6775 | ||
4c46b835 AC |
6776 | -- We explicitly check for the case of concatenation of component |
6777 | -- with component to avoid reporting spurious matching array types | |
6778 | -- that might happen to be lurking in distant packages (such as | |
6779 | -- run-time packages). This also prevents inconsistencies in the | |
6780 | -- messages for certain ACVC B tests, which can vary depending on | |
6781 | -- types declared in run-time interfaces. Another improvement when | |
6782 | -- aggregates are present is to look for a well-typed operand. | |
996ae0b0 RK |
6783 | |
6784 | elsif Present (Candidate_Type) | |
6785 | and then (Nkind (N) /= N_Op_Concat | |
6786 | or else Is_Array_Type (Etype (L)) | |
6787 | or else Is_Array_Type (Etype (R))) | |
6788 | then | |
996ae0b0 RK |
6789 | if Nkind (N) = N_Op_Concat then |
6790 | if Etype (L) /= Any_Composite | |
6791 | and then Is_Array_Type (Etype (L)) | |
6792 | then | |
6793 | Candidate_Type := Etype (L); | |
6794 | ||
6795 | elsif Etype (R) /= Any_Composite | |
6796 | and then Is_Array_Type (Etype (R)) | |
6797 | then | |
6798 | Candidate_Type := Etype (R); | |
6799 | end if; | |
6800 | end if; | |
6801 | ||
19d846a0 | 6802 | Error_Msg_NE -- CODEFIX |
996ae0b0 RK |
6803 | ("operator for} is not directly visible!", |
6804 | N, First_Subtype (Candidate_Type)); | |
4561baf7 ES |
6805 | |
6806 | declare | |
6807 | U : constant Node_Id := | |
6808 | Cunit (Get_Source_Unit (Candidate_Type)); | |
4561baf7 ES |
6809 | begin |
6810 | if Unit_Is_Visible (U) then | |
6811 | Error_Msg_N -- CODEFIX | |
6812 | ("use clause would make operation legal!", N); | |
4561baf7 ES |
6813 | else |
6814 | Error_Msg_NE -- CODEFIX | |
6815 | ("add with_clause and use_clause for&!", | |
8b4230c8 | 6816 | N, Defining_Entity (Unit (U))); |
4561baf7 ES |
6817 | end if; |
6818 | end; | |
996ae0b0 RK |
6819 | return; |
6820 | ||
6821 | -- If either operand is a junk operand (e.g. package name), then | |
6822 | -- post appropriate error messages, but do not complain further. | |
6823 | ||
0e0eecec ES |
6824 | -- Note that the use of OR in this test instead of OR ELSE is |
6825 | -- quite deliberate, we may as well check both operands in the | |
6826 | -- binary operator case. | |
996ae0b0 RK |
6827 | |
6828 | elsif Junk_Operand (R) | |
9559eccf AC |
6829 | or -- really mean OR here and not OR ELSE, see above |
6830 | (Nkind (N) in N_Binary_Op and then Junk_Operand (L)) | |
996ae0b0 RK |
6831 | then |
6832 | return; | |
6833 | ||
6834 | -- If we have a logical operator, one of whose operands is | |
0e0eecec ES |
6835 | -- Boolean, then we know that the other operand cannot resolve to |
6836 | -- Boolean (since we got no interpretations), but in that case we | |
6837 | -- pretty much know that the other operand should be Boolean, so | |
070d862d | 6838 | -- resolve it that way (generating an error). |
996ae0b0 | 6839 | |
d469eabe | 6840 | elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then |
996ae0b0 RK |
6841 | if Etype (L) = Standard_Boolean then |
6842 | Resolve (R, Standard_Boolean); | |
6843 | return; | |
6844 | elsif Etype (R) = Standard_Boolean then | |
6845 | Resolve (L, Standard_Boolean); | |
6846 | return; | |
6847 | end if; | |
6848 | ||
6849 | -- For an arithmetic operator or comparison operator, if one | |
6850 | -- of the operands is numeric, then we know the other operand | |
6851 | -- is not the same numeric type. If it is a non-numeric type, | |
6852 | -- then probably it is intended to match the other operand. | |
6853 | ||
d469eabe HK |
6854 | elsif Nkind_In (N, N_Op_Add, |
6855 | N_Op_Divide, | |
6856 | N_Op_Ge, | |
6857 | N_Op_Gt, | |
6858 | N_Op_Le) | |
6859 | or else | |
6860 | Nkind_In (N, N_Op_Lt, | |
6861 | N_Op_Mod, | |
6862 | N_Op_Multiply, | |
6863 | N_Op_Rem, | |
6864 | N_Op_Subtract) | |
996ae0b0 | 6865 | then |
7dbd3de9 RD |
6866 | -- If Allow_Integer_Address is active, check whether the |
6867 | -- operation becomes legal after converting an operand. | |
6868 | ||
996ae0b0 RK |
6869 | if Is_Numeric_Type (Etype (L)) |
6870 | and then not Is_Numeric_Type (Etype (R)) | |
6871 | then | |
7dbd3de9 RD |
6872 | if Address_Integer_Convert_OK (Etype (R), Etype (L)) then |
6873 | Rewrite (R, | |
6874 | Unchecked_Convert_To (Etype (L), Relocate_Node (R))); | |
7dbd3de9 | 6875 | |
1e3689bd AC |
6876 | if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then |
6877 | Analyze_Comparison_Op (N); | |
6878 | else | |
6879 | Analyze_Arithmetic_Op (N); | |
6880 | end if; | |
7dbd3de9 RD |
6881 | else |
6882 | Resolve (R, Etype (L)); | |
6883 | end if; | |
1e3689bd | 6884 | |
996ae0b0 RK |
6885 | return; |
6886 | ||
6887 | elsif Is_Numeric_Type (Etype (R)) | |
6888 | and then not Is_Numeric_Type (Etype (L)) | |
6889 | then | |
7dbd3de9 RD |
6890 | if Address_Integer_Convert_OK (Etype (L), Etype (R)) then |
6891 | Rewrite (L, | |
6892 | Unchecked_Convert_To (Etype (R), Relocate_Node (L))); | |
1e3689bd AC |
6893 | |
6894 | if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then | |
6895 | Analyze_Comparison_Op (N); | |
6896 | else | |
6897 | Analyze_Arithmetic_Op (N); | |
6898 | end if; | |
6899 | ||
9559eccf | 6900 | return; |
7dbd3de9 RD |
6901 | |
6902 | else | |
6903 | Resolve (L, Etype (R)); | |
6904 | end if; | |
9559eccf | 6905 | |
996ae0b0 | 6906 | return; |
9559eccf AC |
6907 | |
6908 | elsif Allow_Integer_Address | |
d9d25d04 AC |
6909 | and then Is_Descendant_Of_Address (Etype (L)) |
6910 | and then Is_Descendant_Of_Address (Etype (R)) | |
9559eccf AC |
6911 | and then not Error_Posted (N) |
6912 | then | |
6913 | declare | |
6914 | Addr_Type : constant Entity_Id := Etype (L); | |
6915 | ||
6916 | begin | |
6917 | Rewrite (L, | |
6918 | Unchecked_Convert_To ( | |
6919 | Standard_Integer, Relocate_Node (L))); | |
6920 | Rewrite (R, | |
6921 | Unchecked_Convert_To ( | |
6922 | Standard_Integer, Relocate_Node (R))); | |
1e3689bd AC |
6923 | |
6924 | if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then | |
6925 | Analyze_Comparison_Op (N); | |
6926 | else | |
6927 | Analyze_Arithmetic_Op (N); | |
6928 | end if; | |
9559eccf AC |
6929 | |
6930 | -- If this is an operand in an enclosing arithmetic | |
6931 | -- operation, Convert the result as an address so that | |
6932 | -- arithmetic folding of address can continue. | |
6933 | ||
6934 | if Nkind (Parent (N)) in N_Op then | |
6935 | Rewrite (N, | |
6936 | Unchecked_Convert_To (Addr_Type, Relocate_Node (N))); | |
6937 | end if; | |
6938 | ||
6939 | return; | |
6940 | end; | |
a8a42b93 AC |
6941 | |
6942 | -- Under relaxed RM semantics silently replace occurrences of | |
6943 | -- null by System.Address_Null. | |
6944 | ||
6945 | elsif Null_To_Null_Address_Convert_OK (N) then | |
6946 | Replace_Null_By_Null_Address (N); | |
6947 | ||
6948 | if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then | |
6949 | Analyze_Comparison_Op (N); | |
6950 | else | |
6951 | Analyze_Arithmetic_Op (N); | |
6952 | end if; | |
6953 | ||
6954 | return; | |
996ae0b0 RK |
6955 | end if; |
6956 | ||
6957 | -- Comparisons on A'Access are common enough to deserve a | |
6958 | -- special message. | |
6959 | ||
d469eabe | 6960 | elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) |
996ae0b0 RK |
6961 | and then Ekind (Etype (L)) = E_Access_Attribute_Type |
6962 | and then Ekind (Etype (R)) = E_Access_Attribute_Type | |
6963 | then | |
6964 | Error_Msg_N | |
6965 | ("two access attributes cannot be compared directly", N); | |
6966 | Error_Msg_N | |
aab883ec | 6967 | ("\use qualified expression for one of the operands", |
996ae0b0 RK |
6968 | N); |
6969 | return; | |
6970 | ||
6971 | -- Another one for C programmers | |
6972 | ||
6973 | elsif Nkind (N) = N_Op_Concat | |
6974 | and then Valid_Boolean_Arg (Etype (L)) | |
6975 | and then Valid_Boolean_Arg (Etype (R)) | |
6976 | then | |
6977 | Error_Msg_N ("invalid operands for concatenation", N); | |
4e7a4f6e AC |
6978 | Error_Msg_N -- CODEFIX |
6979 | ("\maybe AND was meant", N); | |
996ae0b0 RK |
6980 | return; |
6981 | ||
6982 | -- A special case for comparison of access parameter with null | |
6983 | ||
6984 | elsif Nkind (N) = N_Op_Eq | |
6985 | and then Is_Entity_Name (L) | |
6986 | and then Nkind (Parent (Entity (L))) = N_Parameter_Specification | |
6987 | and then Nkind (Parameter_Type (Parent (Entity (L)))) = | |
6988 | N_Access_Definition | |
6989 | and then Nkind (R) = N_Null | |
6990 | then | |
6991 | Error_Msg_N ("access parameter is not allowed to be null", L); | |
6992 | Error_Msg_N ("\(call would raise Constraint_Error)", L); | |
6993 | return; | |
61bee0e3 AC |
6994 | |
6995 | -- Another special case for exponentiation, where the right | |
6996 | -- operand must be Natural, independently of the base. | |
6997 | ||
6998 | elsif Nkind (N) = N_Op_Expon | |
6999 | and then Is_Numeric_Type (Etype (L)) | |
7000 | and then not Is_Overloaded (R) | |
7001 | and then | |
7002 | First_Subtype (Base_Type (Etype (R))) /= Standard_Integer | |
7003 | and then Base_Type (Etype (R)) /= Universal_Integer | |
7004 | then | |
dec6faf1 | 7005 | if Ada_Version >= Ada_2012 |
15954beb | 7006 | and then Has_Dimension_System (Etype (L)) |
dec6faf1 AC |
7007 | then |
7008 | Error_Msg_NE | |
54c04d6c | 7009 | ("exponent for dimensioned type must be a rational" & |
dec6faf1 AC |
7010 | ", found}", R, Etype (R)); |
7011 | else | |
7012 | Error_Msg_NE | |
7013 | ("exponent must be of type Natural, found}", R, Etype (R)); | |
7014 | end if; | |
54c04d6c | 7015 | |
61bee0e3 | 7016 | return; |
11261647 AC |
7017 | |
7018 | elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then | |
7019 | if Address_Integer_Convert_OK (Etype (R), Etype (L)) then | |
7020 | Rewrite (R, | |
7021 | Unchecked_Convert_To (Etype (L), Relocate_Node (R))); | |
7022 | Analyze_Equality_Op (N); | |
7023 | return; | |
a8a42b93 AC |
7024 | |
7025 | -- Under relaxed RM semantics silently replace occurrences of | |
7026 | -- null by System.Address_Null. | |
7027 | ||
7028 | elsif Null_To_Null_Address_Convert_OK (N) then | |
7029 | Replace_Null_By_Null_Address (N); | |
7030 | Analyze_Equality_Op (N); | |
7031 | return; | |
11261647 | 7032 | end if; |
996ae0b0 RK |
7033 | end if; |
7034 | ||
0e0eecec ES |
7035 | -- If we fall through then just give general message. Note that in |
7036 | -- the following messages, if the operand is overloaded we choose | |
7037 | -- an arbitrary type to complain about, but that is probably more | |
7038 | -- useful than not giving a type at all. | |
996ae0b0 RK |
7039 | |
7040 | if Nkind (N) in N_Unary_Op then | |
7041 | Error_Msg_Node_2 := Etype (R); | |
7042 | Error_Msg_N ("operator& not defined for}", N); | |
7043 | return; | |
7044 | ||
7045 | else | |
fbf5a39b AC |
7046 | if Nkind (N) in N_Binary_Op then |
7047 | if not Is_Overloaded (L) | |
7048 | and then not Is_Overloaded (R) | |
7049 | and then Base_Type (Etype (L)) = Base_Type (Etype (R)) | |
7050 | then | |
7ffd9312 | 7051 | Error_Msg_Node_2 := First_Subtype (Etype (R)); |
fbf5a39b | 7052 | Error_Msg_N ("there is no applicable operator& for}", N); |
996ae0b0 | 7053 | |
fbf5a39b | 7054 | else |
b67a385c ES |
7055 | -- Another attempt to find a fix: one of the candidate |
7056 | -- interpretations may not be use-visible. This has | |
7057 | -- already been checked for predefined operators, so | |
7058 | -- we examine only user-defined functions. | |
7059 | ||
7060 | Op_Id := Get_Name_Entity_Id (Chars (N)); | |
7061 | ||
7062 | while Present (Op_Id) loop | |
7063 | if Ekind (Op_Id) /= E_Operator | |
7064 | and then Is_Overloadable (Op_Id) | |
7065 | then | |
7066 | if not Is_Immediately_Visible (Op_Id) | |
7067 | and then not In_Use (Scope (Op_Id)) | |
aab883ec | 7068 | and then not Is_Abstract_Subprogram (Op_Id) |
b67a385c ES |
7069 | and then not Is_Hidden (Op_Id) |
7070 | and then Ekind (Scope (Op_Id)) = E_Package | |
7071 | and then | |
7072 | Has_Compatible_Type | |
7073 | (L, Etype (First_Formal (Op_Id))) | |
7074 | and then Present | |
7075 | (Next_Formal (First_Formal (Op_Id))) | |
7076 | and then | |
7077 | Has_Compatible_Type | |
7078 | (R, | |
7079 | Etype (Next_Formal (First_Formal (Op_Id)))) | |
7080 | then | |
ed2233dc | 7081 | Error_Msg_N |
b67a385c | 7082 | ("No legal interpretation for operator&", N); |
ed2233dc | 7083 | Error_Msg_NE |
b67a385c | 7084 | ("\use clause on& would make operation legal", |
8b4230c8 | 7085 | N, Scope (Op_Id)); |
b67a385c ES |
7086 | exit; |
7087 | end if; | |
7088 | end if; | |
fbf5a39b | 7089 | |
b67a385c ES |
7090 | Op_Id := Homonym (Op_Id); |
7091 | end loop; | |
7092 | ||
7093 | if No (Op_Id) then | |
7094 | Error_Msg_N ("invalid operand types for operator&", N); | |
7095 | ||
7096 | if Nkind (N) /= N_Op_Concat then | |
7097 | Error_Msg_NE ("\left operand has}!", N, Etype (L)); | |
7098 | Error_Msg_NE ("\right operand has}!", N, Etype (R)); | |
2e70d415 AC |
7099 | |
7100 | -- For concatenation operators it is more difficult to | |
7101 | -- determine which is the wrong operand. It is worth | |
7102 | -- flagging explicitly an access type, for those who | |
7103 | -- might think that a dereference happens here. | |
7104 | ||
7105 | elsif Is_Access_Type (Etype (L)) then | |
7106 | Error_Msg_N ("\left operand is access type", N); | |
7107 | ||
7108 | elsif Is_Access_Type (Etype (R)) then | |
7109 | Error_Msg_N ("\right operand is access type", N); | |
b67a385c | 7110 | end if; |
fbf5a39b AC |
7111 | end if; |
7112 | end if; | |
996ae0b0 RK |
7113 | end if; |
7114 | end if; | |
7115 | end; | |
7116 | end if; | |
7117 | end Operator_Check; | |
7118 | ||
6e73e3ab AC |
7119 | ----------------------------------------- |
7120 | -- Process_Implicit_Dereference_Prefix -- | |
7121 | ----------------------------------------- | |
7122 | ||
d469eabe | 7123 | function Process_Implicit_Dereference_Prefix |
da709d08 | 7124 | (E : Entity_Id; |
d469eabe | 7125 | P : Entity_Id) return Entity_Id |
6e73e3ab AC |
7126 | is |
7127 | Ref : Node_Id; | |
d469eabe | 7128 | Typ : constant Entity_Id := Designated_Type (Etype (P)); |
da709d08 | 7129 | |
6e73e3ab | 7130 | begin |
1a8fae99 ES |
7131 | if Present (E) |
7132 | and then (Operating_Mode = Check_Semantics or else not Expander_Active) | |
7133 | then | |
8b4230c8 AC |
7134 | -- We create a dummy reference to E to ensure that the reference is |
7135 | -- not considered as part of an assignment (an implicit dereference | |
7136 | -- can never assign to its prefix). The Comes_From_Source attribute | |
7137 | -- needs to be propagated for accurate warnings. | |
6e73e3ab | 7138 | |
e4494292 | 7139 | Ref := New_Occurrence_Of (E, Sloc (P)); |
6e73e3ab AC |
7140 | Set_Comes_From_Source (Ref, Comes_From_Source (P)); |
7141 | Generate_Reference (E, Ref); | |
7142 | end if; | |
d469eabe | 7143 | |
8b4230c8 AC |
7144 | -- An implicit dereference is a legal occurrence of an incomplete type |
7145 | -- imported through a limited_with clause, if the full view is visible. | |
d469eabe | 7146 | |
7b56a91b AC |
7147 | if From_Limited_With (Typ) |
7148 | and then not From_Limited_With (Scope (Typ)) | |
d469eabe HK |
7149 | and then |
7150 | (Is_Immediately_Visible (Scope (Typ)) | |
7151 | or else | |
7152 | (Is_Child_Unit (Scope (Typ)) | |
8398e82e | 7153 | and then Is_Visible_Lib_Unit (Scope (Typ)))) |
d469eabe HK |
7154 | then |
7155 | return Available_View (Typ); | |
7156 | else | |
7157 | return Typ; | |
7158 | end if; | |
6e73e3ab AC |
7159 | end Process_Implicit_Dereference_Prefix; |
7160 | ||
30c20106 AC |
7161 | -------------------------------- |
7162 | -- Remove_Abstract_Operations -- | |
7163 | -------------------------------- | |
7164 | ||
7165 | procedure Remove_Abstract_Operations (N : Node_Id) is | |
e80f0cb0 | 7166 | Abstract_Op : Entity_Id := Empty; |
d9d25d04 | 7167 | Address_Descendant : Boolean := False; |
e80f0cb0 RD |
7168 | I : Interp_Index; |
7169 | It : Interp; | |
30c20106 | 7170 | |
0e0eecec ES |
7171 | -- AI-310: If overloaded, remove abstract non-dispatching operations. We |
7172 | -- activate this if either extensions are enabled, or if the abstract | |
7173 | -- operation in question comes from a predefined file. This latter test | |
7174 | -- allows us to use abstract to make operations invisible to users. In | |
7175 | -- particular, if type Address is non-private and abstract subprograms | |
7176 | -- are used to hide its operators, they will be truly hidden. | |
30c20106 | 7177 | |
5950a3ac | 7178 | type Operand_Position is (First_Op, Second_Op); |
8a36a0cc | 7179 | Univ_Type : constant Entity_Id := Universal_Interpretation (N); |
5950a3ac AC |
7180 | |
7181 | procedure Remove_Address_Interpretations (Op : Operand_Position); | |
0e0eecec ES |
7182 | -- Ambiguities may arise when the operands are literal and the address |
7183 | -- operations in s-auxdec are visible. In that case, remove the | |
8b4230c8 AC |
7184 | -- interpretation of a literal as Address, to retain the semantics |
7185 | -- of Address as a private type. | |
9f4fd324 AC |
7186 | |
7187 | ------------------------------------ | |
5950a3ac | 7188 | -- Remove_Address_Interpretations -- |
9f4fd324 AC |
7189 | ------------------------------------ |
7190 | ||
5950a3ac | 7191 | procedure Remove_Address_Interpretations (Op : Operand_Position) is |
9f4fd324 AC |
7192 | Formal : Entity_Id; |
7193 | ||
7194 | begin | |
7195 | if Is_Overloaded (N) then | |
7196 | Get_First_Interp (N, I, It); | |
7197 | while Present (It.Nam) loop | |
7198 | Formal := First_Entity (It.Nam); | |
7199 | ||
5950a3ac AC |
7200 | if Op = Second_Op then |
7201 | Formal := Next_Entity (Formal); | |
7202 | end if; | |
7203 | ||
d9d25d04 AC |
7204 | if Is_Descendant_Of_Address (Etype (Formal)) then |
7205 | Address_Descendant := True; | |
9f4fd324 AC |
7206 | Remove_Interp (I); |
7207 | end if; | |
7208 | ||
7209 | Get_Next_Interp (I, It); | |
7210 | end loop; | |
7211 | end if; | |
7212 | end Remove_Address_Interpretations; | |
7213 | ||
7214 | -- Start of processing for Remove_Abstract_Operations | |
7215 | ||
30c20106 | 7216 | begin |
d935a36e | 7217 | if Is_Overloaded (N) then |
ee1a7572 AC |
7218 | if Debug_Flag_V then |
7219 | Write_Str ("Remove_Abstract_Operations: "); | |
7220 | Write_Overloads (N); | |
7221 | end if; | |
7222 | ||
30c20106 | 7223 | Get_First_Interp (N, I, It); |
d935a36e | 7224 | |
30c20106 | 7225 | while Present (It.Nam) loop |
aab883ec ES |
7226 | if Is_Overloadable (It.Nam) |
7227 | and then Is_Abstract_Subprogram (It.Nam) | |
30c20106 AC |
7228 | and then not Is_Dispatching_Operation (It.Nam) |
7229 | then | |
af152989 | 7230 | Abstract_Op := It.Nam; |
fe45e59e | 7231 | |
d9d25d04 AC |
7232 | if Is_Descendant_Of_Address (It.Typ) then |
7233 | Address_Descendant := True; | |
401093c1 ES |
7234 | Remove_Interp (I); |
7235 | exit; | |
7236 | ||
76264f60 | 7237 | -- In Ada 2005, this operation does not participate in overload |
9c510803 | 7238 | -- resolution. If the operation is defined in a predefined |
fe45e59e ES |
7239 | -- unit, it is one of the operations declared abstract in some |
7240 | -- variants of System, and it must be removed as well. | |
7241 | ||
0791fbe9 | 7242 | elsif Ada_Version >= Ada_2005 |
401093c1 ES |
7243 | or else Is_Predefined_File_Name |
7244 | (Unit_File_Name (Get_Source_Unit (It.Nam))) | |
fe45e59e ES |
7245 | then |
7246 | Remove_Interp (I); | |
7247 | exit; | |
7248 | end if; | |
30c20106 AC |
7249 | end if; |
7250 | ||
7251 | Get_Next_Interp (I, It); | |
7252 | end loop; | |
7253 | ||
af152989 | 7254 | if No (Abstract_Op) then |
fe45e59e ES |
7255 | |
7256 | -- If some interpretation yields an integer type, it is still | |
7257 | -- possible that there are address interpretations. Remove them | |
7258 | -- if one operand is a literal, to avoid spurious ambiguities | |
7259 | -- on systems where Address is a visible integer type. | |
7260 | ||
7261 | if Is_Overloaded (N) | |
401093c1 | 7262 | and then Nkind (N) in N_Op |
fe45e59e ES |
7263 | and then Is_Integer_Type (Etype (N)) |
7264 | then | |
7265 | if Nkind (N) in N_Binary_Op then | |
7266 | if Nkind (Right_Opnd (N)) = N_Integer_Literal then | |
7267 | Remove_Address_Interpretations (Second_Op); | |
7268 | ||
7269 | elsif Nkind (Right_Opnd (N)) = N_Integer_Literal then | |
7270 | Remove_Address_Interpretations (First_Op); | |
7271 | end if; | |
7272 | end if; | |
7273 | end if; | |
3984e89a AC |
7274 | |
7275 | elsif Nkind (N) in N_Op then | |
4c46b835 | 7276 | |
fe45e59e ES |
7277 | -- Remove interpretations that treat literals as addresses. This |
7278 | -- is never appropriate, even when Address is defined as a visible | |
7279 | -- Integer type. The reason is that we would really prefer Address | |
7a5b62b0 AC |
7280 | -- to behave as a private type, even in this case. If Address is a |
7281 | -- visible integer type, we get lots of overload ambiguities. | |
30c20106 | 7282 | |
5950a3ac AC |
7283 | if Nkind (N) in N_Binary_Op then |
7284 | declare | |
7285 | U1 : constant Boolean := | |
8b4230c8 | 7286 | Present (Universal_Interpretation (Right_Opnd (N))); |
5950a3ac | 7287 | U2 : constant Boolean := |
8b4230c8 | 7288 | Present (Universal_Interpretation (Left_Opnd (N))); |
30c20106 | 7289 | |
5950a3ac | 7290 | begin |
0e0eecec | 7291 | if U1 then |
5950a3ac | 7292 | Remove_Address_Interpretations (Second_Op); |
0e0eecec | 7293 | end if; |
5950a3ac | 7294 | |
0e0eecec | 7295 | if U2 then |
5950a3ac | 7296 | Remove_Address_Interpretations (First_Op); |
30c20106 AC |
7297 | end if; |
7298 | ||
5950a3ac AC |
7299 | if not (U1 and U2) then |
7300 | ||
7301 | -- Remove corresponding predefined operator, which is | |
7302 | -- always added to the overload set. | |
7303 | ||
7304 | Get_First_Interp (N, I, It); | |
7305 | while Present (It.Nam) loop | |
0ab80019 AC |
7306 | if Scope (It.Nam) = Standard_Standard |
7307 | and then Base_Type (It.Typ) = | |
7308 | Base_Type (Etype (Abstract_Op)) | |
7309 | then | |
5950a3ac AC |
7310 | Remove_Interp (I); |
7311 | end if; | |
7312 | ||
8a36a0cc AC |
7313 | Get_Next_Interp (I, It); |
7314 | end loop; | |
7315 | ||
7316 | elsif Is_Overloaded (N) | |
7317 | and then Present (Univ_Type) | |
7318 | then | |
7319 | -- If both operands have a universal interpretation, | |
0e0eecec ES |
7320 | -- it is still necessary to remove interpretations that |
7321 | -- yield Address. Any remaining ambiguities will be | |
7322 | -- removed in Disambiguate. | |
8a36a0cc AC |
7323 | |
7324 | Get_First_Interp (N, I, It); | |
8a36a0cc | 7325 | while Present (It.Nam) loop |
d9d25d04 | 7326 | if Is_Descendant_Of_Address (It.Typ) then |
0e0eecec ES |
7327 | Remove_Interp (I); |
7328 | ||
7329 | elsif not Is_Type (It.Nam) then | |
8a36a0cc | 7330 | Set_Entity (N, It.Nam); |
8a36a0cc AC |
7331 | end if; |
7332 | ||
5950a3ac AC |
7333 | Get_Next_Interp (I, It); |
7334 | end loop; | |
7335 | end if; | |
7336 | end; | |
30c20106 | 7337 | end if; |
3984e89a AC |
7338 | |
7339 | elsif Nkind (N) = N_Function_Call | |
7340 | and then | |
7341 | (Nkind (Name (N)) = N_Operator_Symbol | |
7342 | or else | |
7343 | (Nkind (Name (N)) = N_Expanded_Name | |
7344 | and then | |
7345 | Nkind (Selector_Name (Name (N))) = N_Operator_Symbol)) | |
7346 | then | |
5950a3ac | 7347 | |
3984e89a AC |
7348 | declare |
7349 | Arg1 : constant Node_Id := First (Parameter_Associations (N)); | |
5950a3ac AC |
7350 | U1 : constant Boolean := |
7351 | Present (Universal_Interpretation (Arg1)); | |
7352 | U2 : constant Boolean := | |
7353 | Present (Next (Arg1)) and then | |
7354 | Present (Universal_Interpretation (Next (Arg1))); | |
3984e89a AC |
7355 | |
7356 | begin | |
0e0eecec | 7357 | if U1 then |
5950a3ac | 7358 | Remove_Address_Interpretations (First_Op); |
0e0eecec | 7359 | end if; |
3984e89a | 7360 | |
0e0eecec | 7361 | if U2 then |
5950a3ac AC |
7362 | Remove_Address_Interpretations (Second_Op); |
7363 | end if; | |
7364 | ||
7365 | if not (U1 and U2) then | |
3984e89a AC |
7366 | Get_First_Interp (N, I, It); |
7367 | while Present (It.Nam) loop | |
9f4fd324 AC |
7368 | if Scope (It.Nam) = Standard_Standard |
7369 | and then It.Typ = Base_Type (Etype (Abstract_Op)) | |
7370 | then | |
3984e89a AC |
7371 | Remove_Interp (I); |
7372 | end if; | |
7373 | ||
7374 | Get_Next_Interp (I, It); | |
7375 | end loop; | |
7376 | end if; | |
7377 | end; | |
30c20106 | 7378 | end if; |
af152989 | 7379 | |
401093c1 ES |
7380 | -- If the removal has left no valid interpretations, emit an error |
7381 | -- message now and label node as illegal. | |
af152989 AC |
7382 | |
7383 | if Present (Abstract_Op) then | |
7384 | Get_First_Interp (N, I, It); | |
7385 | ||
7386 | if No (It.Nam) then | |
7387 | ||
6e73e3ab | 7388 | -- Removal of abstract operation left no viable candidate |
af152989 AC |
7389 | |
7390 | Set_Etype (N, Any_Type); | |
7391 | Error_Msg_Sloc := Sloc (Abstract_Op); | |
7392 | Error_Msg_NE | |
7393 | ("cannot call abstract operation& declared#", N, Abstract_Op); | |
401093c1 ES |
7394 | |
7395 | -- In Ada 2005, an abstract operation may disable predefined | |
7396 | -- operators. Since the context is not yet known, we mark the | |
7397 | -- predefined operators as potentially hidden. Do not include | |
7398 | -- predefined operators when addresses are involved since this | |
7399 | -- case is handled separately. | |
7400 | ||
d9d25d04 | 7401 | elsif Ada_Version >= Ada_2005 and then not Address_Descendant then |
401093c1 ES |
7402 | while Present (It.Nam) loop |
7403 | if Is_Numeric_Type (It.Typ) | |
7404 | and then Scope (It.Typ) = Standard_Standard | |
7405 | then | |
7406 | Set_Abstract_Op (I, Abstract_Op); | |
7407 | end if; | |
7408 | ||
7409 | Get_Next_Interp (I, It); | |
7410 | end loop; | |
af152989 AC |
7411 | end if; |
7412 | end if; | |
ee1a7572 AC |
7413 | |
7414 | if Debug_Flag_V then | |
7415 | Write_Str ("Remove_Abstract_Operations done: "); | |
7416 | Write_Overloads (N); | |
7417 | end if; | |
30c20106 AC |
7418 | end if; |
7419 | end Remove_Abstract_Operations; | |
7420 | ||
d50f4827 AC |
7421 | ---------------------------- |
7422 | -- Try_Container_Indexing -- | |
7423 | ---------------------------- | |
7424 | ||
7425 | function Try_Container_Indexing | |
7426 | (N : Node_Id; | |
7427 | Prefix : Node_Id; | |
50878404 | 7428 | Exprs : List_Id) return Boolean |
d50f4827 | 7429 | is |
437244c7 AC |
7430 | Pref_Typ : constant Entity_Id := Etype (Prefix); |
7431 | ||
fa73fc3d AC |
7432 | function Constant_Indexing_OK return Boolean; |
7433 | -- Constant_Indexing is legal if there is no Variable_Indexing defined | |
7434 | -- for the type, or else node not a target of assignment, or an actual | |
7435 | -- for an IN OUT or OUT formal (RM 4.1.6 (11)). | |
7436 | ||
437244c7 AC |
7437 | function Find_Indexing_Operations |
7438 | (T : Entity_Id; | |
7439 | Nam : Name_Id; | |
7440 | Is_Constant : Boolean) return Node_Id; | |
7441 | -- Return a reference to the primitive operation of type T denoted by | |
7442 | -- name Nam. If the operation is overloaded, the reference carries all | |
7443 | -- interpretations. Flag Is_Constant should be set when the context is | |
7444 | -- constant indexing. | |
7445 | ||
fa73fc3d AC |
7446 | -------------------------- |
7447 | -- Constant_Indexing_OK -- | |
7448 | -------------------------- | |
7449 | ||
7450 | function Constant_Indexing_OK return Boolean is | |
7451 | Par : Node_Id; | |
7452 | ||
7453 | begin | |
437244c7 | 7454 | if No (Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing)) then |
fa73fc3d AC |
7455 | return True; |
7456 | ||
7457 | elsif not Is_Variable (Prefix) then | |
7458 | return True; | |
7459 | end if; | |
7460 | ||
7461 | Par := N; | |
7462 | while Present (Par) loop | |
7463 | if Nkind (Parent (Par)) = N_Assignment_Statement | |
7464 | and then Par = Name (Parent (Par)) | |
7465 | then | |
7466 | return False; | |
7467 | ||
7468 | -- The call may be overloaded, in which case we assume that its | |
7469 | -- resolution does not depend on the type of the parameter that | |
7470 | -- includes the indexing operation. | |
7471 | ||
7472 | elsif Nkind_In (Parent (Par), N_Function_Call, | |
7473 | N_Procedure_Call_Statement) | |
7474 | and then Is_Entity_Name (Name (Parent (Par))) | |
7475 | then | |
7476 | declare | |
7477 | Actual : Node_Id; | |
7478 | Formal : Entity_Id; | |
7479 | Proc : Entity_Id; | |
7480 | ||
7481 | begin | |
7482 | -- We should look for an interpretation with the proper | |
7483 | -- number of formals, and determine whether it is an | |
31101470 AC |
7484 | -- In_Parameter, but for now we examine the formal that |
7485 | -- corresponds to the indexing, and assume that variable | |
7486 | -- indexing is required if some interpretation has an | |
7487 | -- assignable formal at that position. Still does not | |
7488 | -- cover the most complex cases ??? | |
fa73fc3d AC |
7489 | |
7490 | if Is_Overloaded (Name (Parent (Par))) then | |
31101470 AC |
7491 | declare |
7492 | Proc : constant Node_Id := Name (Parent (Par)); | |
7493 | A : Node_Id; | |
7494 | F : Entity_Id; | |
7495 | I : Interp_Index; | |
7496 | It : Interp; | |
7497 | ||
7498 | begin | |
7499 | Get_First_Interp (Proc, I, It); | |
7500 | while Present (It.Nam) loop | |
7501 | F := First_Formal (It.Nam); | |
7502 | A := First (Parameter_Associations (Parent (Par))); | |
7503 | ||
7504 | while Present (F) and then Present (A) loop | |
7505 | if A = Par then | |
7506 | if Ekind (F) /= E_In_Parameter then | |
7507 | return False; | |
7508 | else | |
7509 | exit; -- interpretation is safe | |
7510 | end if; | |
7511 | end if; | |
7512 | ||
7513 | Next_Formal (F); | |
7514 | Next_Actual (A); | |
7515 | end loop; | |
7516 | ||
7517 | Get_Next_Interp (I, It); | |
7518 | end loop; | |
7519 | end; | |
7520 | ||
fa73fc3d AC |
7521 | return True; |
7522 | ||
7523 | else | |
7524 | Proc := Entity (Name (Parent (Par))); | |
7525 | ||
7526 | -- If this is an indirect call, get formals from | |
7527 | -- designated type. | |
7528 | ||
7529 | if Is_Access_Subprogram_Type (Etype (Proc)) then | |
7530 | Proc := Designated_Type (Etype (Proc)); | |
7531 | end if; | |
7532 | end if; | |
7533 | ||
7534 | Formal := First_Formal (Proc); | |
7535 | Actual := First_Actual (Parent (Par)); | |
7536 | ||
7537 | -- Find corresponding actual | |
7538 | ||
7539 | while Present (Actual) loop | |
7540 | exit when Actual = Par; | |
7541 | Next_Actual (Actual); | |
7542 | ||
7543 | if Present (Formal) then | |
7544 | Next_Formal (Formal); | |
7545 | ||
7546 | -- Otherwise this is a parameter mismatch, the error is | |
7547 | -- reported elsewhere. | |
7548 | ||
7549 | else | |
7550 | return False; | |
7551 | end if; | |
7552 | end loop; | |
7553 | ||
7554 | return Ekind (Formal) = E_In_Parameter; | |
7555 | end; | |
7556 | ||
7557 | elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then | |
7558 | return False; | |
7559 | ||
7560 | -- If the indexed component is a prefix it may be the first actual | |
7561 | -- of a prefixed call. Retrieve the called entity, if any, and | |
4e9ee595 AC |
7562 | -- check its first formal. Determine if the context is a procedure |
7563 | -- or function call. | |
fa73fc3d AC |
7564 | |
7565 | elsif Nkind (Parent (Par)) = N_Selected_Component then | |
7566 | declare | |
7567 | Sel : constant Node_Id := Selector_Name (Parent (Par)); | |
7568 | Nam : constant Entity_Id := Current_Entity (Sel); | |
7569 | ||
7570 | begin | |
bc38dbb4 AC |
7571 | if Present (Nam) and then Is_Overloadable (Nam) then |
7572 | if Nkind (Parent (Parent (Par))) = | |
7573 | N_Procedure_Call_Statement | |
4e9ee595 AC |
7574 | then |
7575 | return False; | |
7576 | ||
bc38dbb4 AC |
7577 | elsif Ekind (Nam) = E_Function |
7578 | and then Present (First_Formal (Nam)) | |
7579 | then | |
7580 | return Ekind (First_Formal (Nam)) = E_In_Parameter; | |
4e9ee595 | 7581 | end if; |
fa73fc3d AC |
7582 | end if; |
7583 | end; | |
7584 | ||
437244c7 | 7585 | elsif Nkind (Par) in N_Op then |
fa73fc3d AC |
7586 | return True; |
7587 | end if; | |
7588 | ||
7589 | Par := Parent (Par); | |
7590 | end loop; | |
7591 | ||
7592 | -- In all other cases, constant indexing is legal | |
7593 | ||
7594 | return True; | |
7595 | end Constant_Indexing_OK; | |
7596 | ||
437244c7 AC |
7597 | ------------------------------ |
7598 | -- Find_Indexing_Operations -- | |
7599 | ------------------------------ | |
7600 | ||
7601 | function Find_Indexing_Operations | |
7602 | (T : Entity_Id; | |
7603 | Nam : Name_Id; | |
7604 | Is_Constant : Boolean) return Node_Id | |
7605 | is | |
7606 | procedure Inspect_Declarations | |
7607 | (Typ : Entity_Id; | |
7608 | Ref : in out Node_Id); | |
7609 | -- Traverse the declarative list where type Typ resides and collect | |
7610 | -- all suitable interpretations in node Ref. | |
7611 | ||
7612 | procedure Inspect_Primitives | |
7613 | (Typ : Entity_Id; | |
7614 | Ref : in out Node_Id); | |
7615 | -- Traverse the list of primitive operations of type Typ and collect | |
7616 | -- all suitable interpretations in node Ref. | |
7617 | ||
7618 | function Is_OK_Candidate | |
7619 | (Subp_Id : Entity_Id; | |
7620 | Typ : Entity_Id) return Boolean; | |
7621 | -- Determine whether subprogram Subp_Id is a suitable indexing | |
7622 | -- operation for type Typ. To qualify as such, the subprogram must | |
7623 | -- be a function, have at least two parameters, and the type of the | |
7624 | -- first parameter must be either Typ, or Typ'Class, or access [to | |
7625 | -- constant] with designated type Typ or Typ'Class. | |
7626 | ||
7627 | procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id); | |
7628 | -- Store subprogram Subp_Id as an interpretation in node Ref | |
7629 | ||
7630 | -------------------------- | |
7631 | -- Inspect_Declarations -- | |
7632 | -------------------------- | |
7633 | ||
7634 | procedure Inspect_Declarations | |
7635 | (Typ : Entity_Id; | |
7636 | Ref : in out Node_Id) | |
7637 | is | |
7638 | Typ_Decl : constant Node_Id := Declaration_Node (Typ); | |
7639 | Decl : Node_Id; | |
7640 | Subp_Id : Entity_Id; | |
7641 | ||
7642 | begin | |
2cc2e964 | 7643 | -- Ensure that the routine is not called with itypes, which lack a |
437244c7 AC |
7644 | -- declarative node. |
7645 | ||
7646 | pragma Assert (Present (Typ_Decl)); | |
7647 | pragma Assert (Is_List_Member (Typ_Decl)); | |
7648 | ||
7649 | Decl := First (List_Containing (Typ_Decl)); | |
7650 | while Present (Decl) loop | |
7651 | if Nkind (Decl) = N_Subprogram_Declaration then | |
7652 | Subp_Id := Defining_Entity (Decl); | |
7653 | ||
7654 | if Is_OK_Candidate (Subp_Id, Typ) then | |
7655 | Record_Interp (Subp_Id, Ref); | |
7656 | end if; | |
7657 | end if; | |
7658 | ||
7659 | Next (Decl); | |
7660 | end loop; | |
7661 | end Inspect_Declarations; | |
7662 | ||
7663 | ------------------------ | |
7664 | -- Inspect_Primitives -- | |
7665 | ------------------------ | |
7666 | ||
7667 | procedure Inspect_Primitives | |
7668 | (Typ : Entity_Id; | |
7669 | Ref : in out Node_Id) | |
7670 | is | |
7671 | Prim_Elmt : Elmt_Id; | |
7672 | Prim_Id : Entity_Id; | |
7673 | ||
7674 | begin | |
7675 | Prim_Elmt := First_Elmt (Primitive_Operations (Typ)); | |
7676 | while Present (Prim_Elmt) loop | |
7677 | Prim_Id := Node (Prim_Elmt); | |
7678 | ||
7679 | if Is_OK_Candidate (Prim_Id, Typ) then | |
7680 | Record_Interp (Prim_Id, Ref); | |
7681 | end if; | |
7682 | ||
7683 | Next_Elmt (Prim_Elmt); | |
7684 | end loop; | |
7685 | end Inspect_Primitives; | |
7686 | ||
7687 | --------------------- | |
7688 | -- Is_OK_Candidate -- | |
7689 | --------------------- | |
7690 | ||
7691 | function Is_OK_Candidate | |
7692 | (Subp_Id : Entity_Id; | |
7693 | Typ : Entity_Id) return Boolean | |
7694 | is | |
7695 | Formal : Entity_Id; | |
7696 | Formal_Typ : Entity_Id; | |
7697 | Param_Typ : Node_Id; | |
7698 | ||
7699 | begin | |
2cc2e964 | 7700 | -- To classify as a suitable candidate, the subprogram must be a |
437244c7 AC |
7701 | -- function whose name matches the argument of aspect Constant or |
7702 | -- Variable_Indexing. | |
7703 | ||
7704 | if Ekind (Subp_Id) = E_Function and then Chars (Subp_Id) = Nam then | |
7705 | Formal := First_Formal (Subp_Id); | |
7706 | ||
7707 | -- The candidate requires at least two parameters | |
7708 | ||
7709 | if Present (Formal) and then Present (Next_Formal (Formal)) then | |
7710 | Formal_Typ := Empty; | |
7711 | Param_Typ := Parameter_Type (Parent (Formal)); | |
7712 | ||
7713 | -- Use the designated type when the first parameter is of an | |
7714 | -- access type. | |
7715 | ||
7716 | if Nkind (Param_Typ) = N_Access_Definition | |
7717 | and then Present (Subtype_Mark (Param_Typ)) | |
7718 | then | |
7719 | -- When the context is a constant indexing, the access | |
7720 | -- definition must be access-to-constant. This does not | |
7721 | -- apply to variable indexing. | |
7722 | ||
7723 | if not Is_Constant | |
7724 | or else Constant_Present (Param_Typ) | |
7725 | then | |
7726 | Formal_Typ := Etype (Subtype_Mark (Param_Typ)); | |
7727 | end if; | |
7728 | ||
7729 | -- Otherwise use the parameter type | |
7730 | ||
7731 | else | |
7732 | Formal_Typ := Etype (Param_Typ); | |
7733 | end if; | |
7734 | ||
7735 | if Present (Formal_Typ) then | |
7736 | ||
7737 | -- Use the specific type when the parameter type is | |
7738 | -- class-wide. | |
7739 | ||
7740 | if Is_Class_Wide_Type (Formal_Typ) then | |
7741 | Formal_Typ := Etype (Base_Type (Formal_Typ)); | |
7742 | end if; | |
7743 | ||
7744 | -- Use the full view when the parameter type is private | |
7745 | -- or incomplete. | |
7746 | ||
7747 | if Is_Incomplete_Or_Private_Type (Formal_Typ) | |
7748 | and then Present (Full_View (Formal_Typ)) | |
7749 | then | |
7750 | Formal_Typ := Full_View (Formal_Typ); | |
7751 | end if; | |
7752 | ||
7753 | -- The type of the first parameter must denote the type | |
7754 | -- of the container or acts as its ancestor type. | |
7755 | ||
7756 | return | |
7757 | Formal_Typ = Typ | |
7758 | or else Is_Ancestor (Formal_Typ, Typ); | |
7759 | end if; | |
7760 | end if; | |
7761 | end if; | |
7762 | ||
7763 | return False; | |
7764 | end Is_OK_Candidate; | |
7765 | ||
7766 | ------------------- | |
7767 | -- Record_Interp -- | |
7768 | ------------------- | |
7769 | ||
7770 | procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id) is | |
7771 | begin | |
7772 | if Present (Ref) then | |
7773 | Add_One_Interp (Ref, Subp_Id, Etype (Subp_Id)); | |
7774 | ||
7775 | -- Otherwise this is the first interpretation. Create a reference | |
7776 | -- where all remaining interpretations will be collected. | |
7777 | ||
7778 | else | |
7779 | Ref := New_Occurrence_Of (Subp_Id, Sloc (T)); | |
7780 | end if; | |
7781 | end Record_Interp; | |
7782 | ||
7783 | -- Local variables | |
7784 | ||
7785 | Ref : Node_Id; | |
7786 | Typ : Entity_Id; | |
7787 | ||
7788 | -- Start of processing for Find_Indexing_Operations | |
7789 | ||
7790 | begin | |
7791 | Typ := T; | |
7792 | ||
1e60643a AC |
7793 | -- Use the specific type when the parameter type is class-wide |
7794 | ||
437244c7 AC |
7795 | if Is_Class_Wide_Type (Typ) then |
7796 | Typ := Root_Type (Typ); | |
7797 | end if; | |
7798 | ||
7799 | Ref := Empty; | |
1e60643a | 7800 | Typ := Underlying_Type (Base_Type (Typ)); |
437244c7 AC |
7801 | |
7802 | Inspect_Primitives (Typ, Ref); | |
7803 | Inspect_Declarations (Typ, Ref); | |
7804 | ||
7805 | return Ref; | |
7806 | end Find_Indexing_Operations; | |
7807 | ||
fa73fc3d AC |
7808 | -- Local variables |
7809 | ||
d50f4827 | 7810 | Loc : constant Source_Ptr := Sloc (N); |
50878404 | 7811 | Assoc : List_Id; |
fa73fc3d | 7812 | C_Type : Entity_Id; |
d50f4827 AC |
7813 | Func : Entity_Id; |
7814 | Func_Name : Node_Id; | |
7815 | Indexing : Node_Id; | |
d50f4827 | 7816 | |
437244c7 AC |
7817 | Is_Constant_Indexing : Boolean := False; |
7818 | -- This flag reflects the nature of the container indexing. Note that | |
7819 | -- the context may be suited for constant indexing, but the type may | |
7820 | -- lack a Constant_Indexing annotation. | |
7821 | ||
fa73fc3d AC |
7822 | -- Start of processing for Try_Container_Indexing |
7823 | ||
d50f4827 | 7824 | begin |
fa73fc3d AC |
7825 | -- Node may have been analyzed already when testing for a prefixed |
7826 | -- call, in which case do not redo analysis. | |
7827 | ||
7828 | if Present (Generalized_Indexing (N)) then | |
7829 | return True; | |
7830 | end if; | |
7831 | ||
437244c7 | 7832 | C_Type := Pref_Typ; |
f3296dd3 | 7833 | |
fa73fc3d AC |
7834 | -- If indexing a class-wide container, obtain indexing primitive from |
7835 | -- specific type. | |
f3296dd3 AC |
7836 | |
7837 | if Is_Class_Wide_Type (C_Type) then | |
7838 | C_Type := Etype (Base_Type (C_Type)); | |
7839 | end if; | |
d50f4827 | 7840 | |
2cc2e964 | 7841 | -- Check whether the type has a specified indexing aspect |
d50f4827 AC |
7842 | |
7843 | Func_Name := Empty; | |
d50f4827 | 7844 | |
2cc2e964 AC |
7845 | -- The context is suitable for constant indexing, so obtain the name of |
7846 | -- the indexing function from aspect Constant_Indexing. | |
437244c7 | 7847 | |
fa73fc3d | 7848 | if Constant_Indexing_OK then |
d62520f3 | 7849 | Func_Name := |
437244c7 | 7850 | Find_Value_Of_Aspect (Pref_Typ, Aspect_Constant_Indexing); |
9ee76313 | 7851 | end if; |
dedac3eb | 7852 | |
437244c7 AC |
7853 | if Present (Func_Name) then |
7854 | Is_Constant_Indexing := True; | |
7855 | ||
7856 | -- Otherwise attempt variable indexing | |
7857 | ||
7858 | else | |
d62520f3 | 7859 | Func_Name := |
437244c7 | 7860 | Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing); |
9ee76313 | 7861 | end if; |
d50f4827 | 7862 | |
437244c7 AC |
7863 | -- The type is not subject to either form of indexing, therefore the |
7864 | -- indexed component does not denote container indexing. If this is a | |
7865 | -- true error, it is diagnosed by the caller. | |
d50f4827 AC |
7866 | |
7867 | if No (Func_Name) then | |
57a8057a | 7868 | |
437244c7 AC |
7869 | -- The prefix itself may be an indexing of a container. Rewrite it |
7870 | -- as such and retry. | |
57a8057a | 7871 | |
437244c7 AC |
7872 | if Has_Implicit_Dereference (Pref_Typ) then |
7873 | Build_Explicit_Dereference (Prefix, First_Discriminant (Pref_Typ)); | |
50878404 | 7874 | return Try_Container_Indexing (N, Prefix, Exprs); |
57a8057a | 7875 | |
437244c7 AC |
7876 | -- Otherwise this is definitely not container indexing |
7877 | ||
57a8057a AC |
7878 | else |
7879 | return False; | |
7880 | end if; | |
3f433bc0 | 7881 | |
6907542d AC |
7882 | -- If the container type is derived from another container type, the |
7883 | -- value of the inherited aspect is the Reference operation declared | |
7884 | -- for the parent type. | |
7885 | ||
fa73fc3d AC |
7886 | -- However, Reference is also a primitive operation of the type, and the |
7887 | -- inherited operation has a different signature. We retrieve the right | |
7888 | -- ones (the function may be overloaded) from the list of primitive | |
7889 | -- operations of the derived type. | |
3f433bc0 | 7890 | |
fa73fc3d AC |
7891 | -- Note that predefined containers are typically all derived from one of |
7892 | -- the Controlled types. The code below is motivated by containers that | |
7893 | -- are derived from other types with a Reference aspect. | |
6907542d | 7894 | |
f3296dd3 | 7895 | elsif Is_Derived_Type (C_Type) |
437244c7 | 7896 | and then Etype (First_Formal (Entity (Func_Name))) /= Pref_Typ |
6907542d | 7897 | then |
437244c7 AC |
7898 | Func_Name := |
7899 | Find_Indexing_Operations | |
7900 | (T => C_Type, | |
7901 | Nam => Chars (Func_Name), | |
7902 | Is_Constant => Is_Constant_Indexing); | |
d50f4827 AC |
7903 | end if; |
7904 | ||
50878404 AC |
7905 | Assoc := New_List (Relocate_Node (Prefix)); |
7906 | ||
5f50020a | 7907 | -- A generalized indexing may have nore than one index expression, so |
50878404 | 7908 | -- transfer all of them to the argument list to be used in the call. |
5f50020a ES |
7909 | -- Note that there may be named associations, in which case the node |
7910 | -- was rewritten earlier as a call, and has been transformed back into | |
7911 | -- an indexed expression to share the following processing. | |
e917e3b8 | 7912 | |
5f50020a ES |
7913 | -- The generalized indexing node is the one on which analysis and |
7914 | -- resolution take place. Before expansion the original node is replaced | |
fa73fc3d AC |
7915 | -- with the generalized indexing node, which is a call, possibly with a |
7916 | -- dereference operation. | |
50878404 | 7917 | |
e917e3b8 | 7918 | if Comes_From_Source (N) then |
c86cf714 | 7919 | Check_Compiler_Unit ("generalized indexing", N); |
e917e3b8 AC |
7920 | end if; |
7921 | ||
287aa0ed AC |
7922 | -- Create argument list for function call that represents generalized |
7923 | -- indexing. Note that indices (i.e. actuals) may themselves be | |
7924 | -- overloaded. | |
7925 | ||
50878404 | 7926 | declare |
287aa0ed AC |
7927 | Arg : Node_Id; |
7928 | New_Arg : Node_Id; | |
7929 | ||
50878404 AC |
7930 | begin |
7931 | Arg := First (Exprs); | |
7932 | while Present (Arg) loop | |
287aa0ed | 7933 | New_Arg := Relocate_Node (Arg); |
43151cfd ES |
7934 | |
7935 | -- The arguments can be parameter associations, in which case the | |
7936 | -- explicit actual parameter carries the overloadings. | |
7937 | ||
7938 | if Nkind (New_Arg) /= N_Parameter_Association then | |
7939 | Save_Interps (Arg, New_Arg); | |
7940 | end if; | |
7941 | ||
287aa0ed | 7942 | Append (New_Arg, Assoc); |
50878404 AC |
7943 | Next (Arg); |
7944 | end loop; | |
7945 | end; | |
7946 | ||
d50f4827 AC |
7947 | if not Is_Overloaded (Func_Name) then |
7948 | Func := Entity (Func_Name); | |
29ba9f52 RD |
7949 | Indexing := |
7950 | Make_Function_Call (Loc, | |
7951 | Name => New_Occurrence_Of (Func, Loc), | |
7952 | Parameter_Associations => Assoc); | |
5f50020a ES |
7953 | Set_Parent (Indexing, Parent (N)); |
7954 | Set_Generalized_Indexing (N, Indexing); | |
7955 | Analyze (Indexing); | |
7956 | Set_Etype (N, Etype (Indexing)); | |
d50f4827 | 7957 | |
76d49f49 ES |
7958 | -- If the return type of the indexing function is a reference type, |
7959 | -- add the dereference as a possible interpretation. Note that the | |
7960 | -- indexing aspect may be a function that returns the element type | |
5f50020a ES |
7961 | -- with no intervening implicit dereference, and that the reference |
7962 | -- discriminant is not the first discriminant. | |
76d49f49 ES |
7963 | |
7964 | if Has_Discriminants (Etype (Func)) then | |
71ff3d18 | 7965 | Check_Implicit_Dereference (N, Etype (Func)); |
76d49f49 | 7966 | end if; |
d50f4827 AC |
7967 | |
7968 | else | |
90b510e4 AC |
7969 | -- If there are multiple indexing functions, build a function call |
7970 | -- and analyze it for each of the possible interpretations. | |
7971 | ||
8b4230c8 AC |
7972 | Indexing := |
7973 | Make_Function_Call (Loc, | |
fa73fc3d AC |
7974 | Name => |
7975 | Make_Identifier (Loc, Chars (Func_Name)), | |
8b4230c8 | 7976 | Parameter_Associations => Assoc); |
d50f4827 | 7977 | |
5f50020a ES |
7978 | Set_Parent (Indexing, Parent (N)); |
7979 | Set_Generalized_Indexing (N, Indexing); | |
90b510e4 AC |
7980 | Set_Etype (N, Any_Type); |
7981 | Set_Etype (Name (Indexing), Any_Type); | |
d50f4827 AC |
7982 | |
7983 | declare | |
8b4230c8 AC |
7984 | I : Interp_Index; |
7985 | It : Interp; | |
d50f4827 AC |
7986 | Success : Boolean; |
7987 | ||
7988 | begin | |
7989 | Get_First_Interp (Func_Name, I, It); | |
5f50020a | 7990 | Set_Etype (Indexing, Any_Type); |
90b510e4 | 7991 | |
0310af44 AC |
7992 | -- Analyze eacn candidae function with the given actuals |
7993 | ||
d50f4827 | 7994 | while Present (It.Nam) loop |
5f50020a | 7995 | Analyze_One_Call (Indexing, It.Nam, False, Success); |
0310af44 AC |
7996 | Get_Next_Interp (I, It); |
7997 | end loop; | |
32bba3c9 | 7998 | |
0310af44 AC |
7999 | -- If there are several successful candidates, resolution will |
8000 | -- be by result. Mark the interpretations of the function name | |
8001 | -- itself. | |
d50f4827 | 8002 | |
0310af44 AC |
8003 | if Is_Overloaded (Indexing) then |
8004 | Get_First_Interp (Indexing, I, It); | |
90b510e4 | 8005 | |
0310af44 | 8006 | while Present (It.Nam) loop |
90b510e4 | 8007 | Add_One_Interp (Name (Indexing), It.Nam, It.Typ); |
0310af44 AC |
8008 | Get_Next_Interp (I, It); |
8009 | end loop; | |
8010 | ||
8011 | else | |
8012 | Set_Etype (Name (Indexing), Etype (Indexing)); | |
8013 | end if; | |
8014 | ||
8015 | -- Now add the candidate interpretations to the indexing node | |
8016 | -- itself, to be replaced later by the function call. | |
8017 | ||
8018 | if Is_Overloaded (Name (Indexing)) then | |
8019 | Get_First_Interp (Name (Indexing), I, It); | |
8020 | ||
8021 | while Present (It.Nam) loop | |
90b510e4 AC |
8022 | Add_One_Interp (N, It.Nam, It.Typ); |
8023 | ||
6c7f7b8c AC |
8024 | -- Add dereference interpretation if the result type has |
8025 | -- implicit reference discriminants. | |
d50f4827 | 8026 | |
76d49f49 | 8027 | if Has_Discriminants (Etype (It.Nam)) then |
71ff3d18 | 8028 | Check_Implicit_Dereference (N, Etype (It.Nam)); |
76d49f49 | 8029 | end if; |
32bba3c9 | 8030 | |
0310af44 AC |
8031 | Get_Next_Interp (I, It); |
8032 | end loop; | |
8033 | ||
8034 | else | |
8035 | Set_Etype (N, Etype (Name (Indexing))); | |
8036 | if Has_Discriminants (Etype (N)) then | |
8037 | Check_Implicit_Dereference (N, Etype (N)); | |
8038 | end if; | |
8039 | end if; | |
d50f4827 AC |
8040 | end; |
8041 | end if; | |
8042 | ||
5f50020a | 8043 | if Etype (Indexing) = Any_Type then |
29ba9f52 RD |
8044 | Error_Msg_NE |
8045 | ("container cannot be indexed with&", N, Etype (First (Exprs))); | |
9ee76313 | 8046 | Rewrite (N, New_Occurrence_Of (Any_Id, Loc)); |
9ee76313 AC |
8047 | end if; |
8048 | ||
d50f4827 AC |
8049 | return True; |
8050 | end Try_Container_Indexing; | |
8051 | ||
996ae0b0 RK |
8052 | ----------------------- |
8053 | -- Try_Indirect_Call -- | |
8054 | ----------------------- | |
8055 | ||
8056 | function Try_Indirect_Call | |
91b1417d AC |
8057 | (N : Node_Id; |
8058 | Nam : Entity_Id; | |
8059 | Typ : Entity_Id) return Boolean | |
996ae0b0 | 8060 | is |
24657705 HK |
8061 | Actual : Node_Id; |
8062 | Formal : Entity_Id; | |
8063 | ||
8a7988f5 | 8064 | Call_OK : Boolean; |
24657705 | 8065 | pragma Warnings (Off, Call_OK); |
996ae0b0 RK |
8066 | |
8067 | begin | |
8a7988f5 | 8068 | Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK); |
9de61fcb | 8069 | |
8a7988f5 | 8070 | Actual := First_Actual (N); |
fbf5a39b | 8071 | Formal := First_Formal (Designated_Type (Typ)); |
9de61fcb | 8072 | while Present (Actual) and then Present (Formal) loop |
996ae0b0 RK |
8073 | if not Has_Compatible_Type (Actual, Etype (Formal)) then |
8074 | return False; | |
8075 | end if; | |
8076 | ||
8077 | Next (Actual); | |
8078 | Next_Formal (Formal); | |
8079 | end loop; | |
8080 | ||
8081 | if No (Actual) and then No (Formal) then | |
8082 | Add_One_Interp (N, Nam, Etype (Designated_Type (Typ))); | |
8083 | ||
8084 | -- Nam is a candidate interpretation for the name in the call, | |
8085 | -- if it is not an indirect call. | |
8086 | ||
8087 | if not Is_Type (Nam) | |
8088 | and then Is_Entity_Name (Name (N)) | |
8089 | then | |
8090 | Set_Entity (Name (N), Nam); | |
8091 | end if; | |
8092 | ||
8093 | return True; | |
8b4230c8 | 8094 | |
996ae0b0 RK |
8095 | else |
8096 | return False; | |
8097 | end if; | |
8098 | end Try_Indirect_Call; | |
8099 | ||
8100 | ---------------------- | |
8101 | -- Try_Indexed_Call -- | |
8102 | ---------------------- | |
8103 | ||
8104 | function Try_Indexed_Call | |
aab883ec ES |
8105 | (N : Node_Id; |
8106 | Nam : Entity_Id; | |
8107 | Typ : Entity_Id; | |
8108 | Skip_First : Boolean) return Boolean | |
996ae0b0 | 8109 | is |
5ff22245 ES |
8110 | Loc : constant Source_Ptr := Sloc (N); |
8111 | Actuals : constant List_Id := Parameter_Associations (N); | |
8112 | Actual : Node_Id; | |
8113 | Index : Entity_Id; | |
996ae0b0 RK |
8114 | |
8115 | begin | |
fbf5a39b | 8116 | Actual := First (Actuals); |
aab883ec ES |
8117 | |
8118 | -- If the call was originally written in prefix form, skip the first | |
8119 | -- actual, which is obviously not defaulted. | |
8120 | ||
8121 | if Skip_First then | |
8122 | Next (Actual); | |
8123 | end if; | |
8124 | ||
fbf5a39b | 8125 | Index := First_Index (Typ); |
9de61fcb RD |
8126 | while Present (Actual) and then Present (Index) loop |
8127 | ||
996ae0b0 RK |
8128 | -- If the parameter list has a named association, the expression |
8129 | -- is definitely a call and not an indexed component. | |
8130 | ||
8131 | if Nkind (Actual) = N_Parameter_Association then | |
8132 | return False; | |
8133 | end if; | |
8134 | ||
5ff22245 ES |
8135 | if Is_Entity_Name (Actual) |
8136 | and then Is_Type (Entity (Actual)) | |
8137 | and then No (Next (Actual)) | |
8138 | then | |
1c218ac3 AC |
8139 | -- A single actual that is a type name indicates a slice if the |
8140 | -- type is discrete, and an error otherwise. | |
8141 | ||
8142 | if Is_Discrete_Type (Entity (Actual)) then | |
8143 | Rewrite (N, | |
8144 | Make_Slice (Loc, | |
22b77f68 RD |
8145 | Prefix => |
8146 | Make_Function_Call (Loc, | |
8147 | Name => Relocate_Node (Name (N))), | |
8148 | Discrete_Range => | |
1c218ac3 AC |
8149 | New_Occurrence_Of (Entity (Actual), Sloc (Actual)))); |
8150 | ||
8151 | Analyze (N); | |
8152 | ||
8153 | else | |
8154 | Error_Msg_N ("invalid use of type in expression", Actual); | |
8155 | Set_Etype (N, Any_Type); | |
8156 | end if; | |
5ff22245 | 8157 | |
5ff22245 ES |
8158 | return True; |
8159 | ||
8160 | elsif not Has_Compatible_Type (Actual, Etype (Index)) then | |
996ae0b0 RK |
8161 | return False; |
8162 | end if; | |
8163 | ||
8164 | Next (Actual); | |
8165 | Next_Index (Index); | |
8166 | end loop; | |
8167 | ||
8168 | if No (Actual) and then No (Index) then | |
8169 | Add_One_Interp (N, Nam, Component_Type (Typ)); | |
8170 | ||
8171 | -- Nam is a candidate interpretation for the name in the call, | |
8172 | -- if it is not an indirect call. | |
8173 | ||
8174 | if not Is_Type (Nam) | |
8175 | and then Is_Entity_Name (Name (N)) | |
8176 | then | |
8177 | Set_Entity (Name (N), Nam); | |
8178 | end if; | |
8179 | ||
8180 | return True; | |
8181 | else | |
8182 | return False; | |
8183 | end if; | |
996ae0b0 RK |
8184 | end Try_Indexed_Call; |
8185 | ||
35ae2ed8 AC |
8186 | -------------------------- |
8187 | -- Try_Object_Operation -- | |
8188 | -------------------------- | |
8189 | ||
8cf23b91 AC |
8190 | function Try_Object_Operation |
8191 | (N : Node_Id; CW_Test_Only : Boolean := False) return Boolean | |
8192 | is | |
b67a385c | 8193 | K : constant Node_Kind := Nkind (Parent (N)); |
d3b00ce3 | 8194 | Is_Subprg_Call : constant Boolean := K in N_Subprogram_Call; |
b67a385c | 8195 | Loc : constant Source_Ptr := Sloc (N); |
b67a385c | 8196 | Obj : constant Node_Id := Prefix (N); |
0d57c6f4 RD |
8197 | |
8198 | Subprog : constant Node_Id := | |
8199 | Make_Identifier (Sloc (Selector_Name (N)), | |
8200 | Chars => Chars (Selector_Name (N))); | |
401093c1 | 8201 | -- Identifier on which possible interpretations will be collected |
0a36105d | 8202 | |
b67a385c | 8203 | Report_Error : Boolean := False; |
8b4230c8 AC |
8204 | -- If no candidate interpretation matches the context, redo analysis |
8205 | -- with Report_Error True to provide additional information. | |
28d6470f JM |
8206 | |
8207 | Actual : Node_Id; | |
d469eabe | 8208 | Candidate : Entity_Id := Empty; |
b67a385c | 8209 | New_Call_Node : Node_Id := Empty; |
4c46b835 | 8210 | Node_To_Replace : Node_Id; |
28d6470f | 8211 | Obj_Type : Entity_Id := Etype (Obj); |
d469eabe | 8212 | Success : Boolean := False; |
4c46b835 | 8213 | |
0a36105d JM |
8214 | function Valid_Candidate |
8215 | (Success : Boolean; | |
8216 | Call : Node_Id; | |
8217 | Subp : Entity_Id) return Entity_Id; | |
8218 | -- If the subprogram is a valid interpretation, record it, and add | |
11fa950b | 8219 | -- to the list of interpretations of Subprog. Otherwise return Empty. |
0a36105d | 8220 | |
4c46b835 AC |
8221 | procedure Complete_Object_Operation |
8222 | (Call_Node : Node_Id; | |
0a36105d | 8223 | Node_To_Replace : Node_Id); |
ec6078e3 ES |
8224 | -- Make Subprog the name of Call_Node, replace Node_To_Replace with |
8225 | -- Call_Node, insert the object (or its dereference) as the first actual | |
8226 | -- in the call, and complete the analysis of the call. | |
4c46b835 | 8227 | |
0a36105d JM |
8228 | procedure Report_Ambiguity (Op : Entity_Id); |
8229 | -- If a prefixed procedure call is ambiguous, indicate whether the | |
8230 | -- call includes an implicit dereference or an implicit 'Access. | |
8231 | ||
4c46b835 AC |
8232 | procedure Transform_Object_Operation |
8233 | (Call_Node : out Node_Id; | |
0a36105d | 8234 | Node_To_Replace : out Node_Id); |
ec6078e3 | 8235 | -- Transform Obj.Operation (X, Y,,) into Operation (Obj, X, Y ..) |
d469eabe HK |
8236 | -- Call_Node is the resulting subprogram call, Node_To_Replace is |
8237 | -- either N or the parent of N, and Subprog is a reference to the | |
8238 | -- subprogram we are trying to match. | |
35ae2ed8 AC |
8239 | |
8240 | function Try_Class_Wide_Operation | |
4c46b835 AC |
8241 | (Call_Node : Node_Id; |
8242 | Node_To_Replace : Node_Id) return Boolean; | |
ec6078e3 ES |
8243 | -- Traverse all ancestor types looking for a class-wide subprogram |
8244 | -- for which the current operation is a valid non-dispatching call. | |
35ae2ed8 | 8245 | |
0a36105d JM |
8246 | procedure Try_One_Prefix_Interpretation (T : Entity_Id); |
8247 | -- If prefix is overloaded, its interpretation may include different | |
8248 | -- tagged types, and we must examine the primitive operations and | |
8249 | -- the class-wide operations of each in order to find candidate | |
8250 | -- interpretations for the call as a whole. | |
8251 | ||
4c46b835 AC |
8252 | function Try_Primitive_Operation |
8253 | (Call_Node : Node_Id; | |
8254 | Node_To_Replace : Node_Id) return Boolean; | |
ec6078e3 ES |
8255 | -- Traverse the list of primitive subprograms looking for a dispatching |
8256 | -- operation for which the current node is a valid call . | |
4c46b835 | 8257 | |
0a36105d JM |
8258 | --------------------- |
8259 | -- Valid_Candidate -- | |
8260 | --------------------- | |
8261 | ||
8262 | function Valid_Candidate | |
8263 | (Success : Boolean; | |
8264 | Call : Node_Id; | |
8265 | Subp : Entity_Id) return Entity_Id | |
8266 | is | |
ee9aa7b6 | 8267 | Arr_Type : Entity_Id; |
0a36105d JM |
8268 | Comp_Type : Entity_Id; |
8269 | ||
8270 | begin | |
8271 | -- If the subprogram is a valid interpretation, record it in global | |
8272 | -- variable Subprog, to collect all possible overloadings. | |
8273 | ||
8274 | if Success then | |
8275 | if Subp /= Entity (Subprog) then | |
8276 | Add_One_Interp (Subprog, Subp, Etype (Subp)); | |
8277 | end if; | |
8278 | end if; | |
8279 | ||
d469eabe HK |
8280 | -- If the call may be an indexed call, retrieve component type of |
8281 | -- resulting expression, and add possible interpretation. | |
0a36105d | 8282 | |
ee9aa7b6 | 8283 | Arr_Type := Empty; |
0a36105d JM |
8284 | Comp_Type := Empty; |
8285 | ||
8286 | if Nkind (Call) = N_Function_Call | |
d469eabe HK |
8287 | and then Nkind (Parent (N)) = N_Indexed_Component |
8288 | and then Needs_One_Actual (Subp) | |
0a36105d JM |
8289 | then |
8290 | if Is_Array_Type (Etype (Subp)) then | |
ee9aa7b6 | 8291 | Arr_Type := Etype (Subp); |
0a36105d JM |
8292 | |
8293 | elsif Is_Access_Type (Etype (Subp)) | |
8294 | and then Is_Array_Type (Designated_Type (Etype (Subp))) | |
8295 | then | |
ee9aa7b6 | 8296 | Arr_Type := Designated_Type (Etype (Subp)); |
0a36105d JM |
8297 | end if; |
8298 | end if; | |
8299 | ||
ee9aa7b6 AC |
8300 | if Present (Arr_Type) then |
8301 | ||
3b42c566 RD |
8302 | -- Verify that the actuals (excluding the object) match the types |
8303 | -- of the indexes. | |
ee9aa7b6 AC |
8304 | |
8305 | declare | |
8306 | Actual : Node_Id; | |
8307 | Index : Node_Id; | |
8308 | ||
8309 | begin | |
8310 | Actual := Next (First_Actual (Call)); | |
8311 | Index := First_Index (Arr_Type); | |
ee9aa7b6 AC |
8312 | while Present (Actual) and then Present (Index) loop |
8313 | if not Has_Compatible_Type (Actual, Etype (Index)) then | |
8314 | Arr_Type := Empty; | |
8315 | exit; | |
8316 | end if; | |
8317 | ||
8318 | Next_Actual (Actual); | |
8319 | Next_Index (Index); | |
8320 | end loop; | |
8321 | ||
8322 | if No (Actual) | |
8323 | and then No (Index) | |
8324 | and then Present (Arr_Type) | |
8325 | then | |
8326 | Comp_Type := Component_Type (Arr_Type); | |
8327 | end if; | |
8328 | end; | |
8329 | ||
8330 | if Present (Comp_Type) | |
8331 | and then Etype (Subprog) /= Comp_Type | |
8332 | then | |
8333 | Add_One_Interp (Subprog, Subp, Comp_Type); | |
8334 | end if; | |
0a36105d JM |
8335 | end if; |
8336 | ||
8337 | if Etype (Call) /= Any_Type then | |
8338 | return Subp; | |
8339 | else | |
8340 | return Empty; | |
8341 | end if; | |
8342 | end Valid_Candidate; | |
8343 | ||
4c46b835 AC |
8344 | ------------------------------- |
8345 | -- Complete_Object_Operation -- | |
8346 | ------------------------------- | |
8347 | ||
8348 | procedure Complete_Object_Operation | |
8349 | (Call_Node : Node_Id; | |
0a36105d | 8350 | Node_To_Replace : Node_Id) |
4c46b835 | 8351 | is |
b4592168 GD |
8352 | Control : constant Entity_Id := First_Formal (Entity (Subprog)); |
8353 | Formal_Type : constant Entity_Id := Etype (Control); | |
ec6078e3 ES |
8354 | First_Actual : Node_Id; |
8355 | ||
4c46b835 | 8356 | begin |
955871d3 AC |
8357 | -- Place the name of the operation, with its interpretations, |
8358 | -- on the rewritten call. | |
0a36105d | 8359 | |
ec6078e3 ES |
8360 | Set_Name (Call_Node, Subprog); |
8361 | ||
0a36105d JM |
8362 | First_Actual := First (Parameter_Associations (Call_Node)); |
8363 | ||
8b4230c8 AC |
8364 | -- For cross-reference purposes, treat the new node as being in the |
8365 | -- source if the original one is. Set entity and type, even though | |
8366 | -- they may be overwritten during resolution if overloaded. | |
b67a385c ES |
8367 | |
8368 | Set_Comes_From_Source (Subprog, Comes_From_Source (N)); | |
8369 | Set_Comes_From_Source (Call_Node, Comes_From_Source (N)); | |
8370 | ||
ec6078e3 | 8371 | if Nkind (N) = N_Selected_Component |
3d918396 | 8372 | and then not Inside_A_Generic |
ec6078e3 ES |
8373 | then |
8374 | Set_Entity (Selector_Name (N), Entity (Subprog)); | |
b2ab8c33 | 8375 | Set_Etype (Selector_Name (N), Etype (Entity (Subprog))); |
ec6078e3 ES |
8376 | end if; |
8377 | ||
8b4230c8 AC |
8378 | -- If need be, rewrite first actual as an explicit dereference. If |
8379 | -- the call is overloaded, the rewriting can only be done once the | |
8380 | -- primitive operation is identified. | |
0a36105d JM |
8381 | |
8382 | if Is_Overloaded (Subprog) then | |
ec6078e3 | 8383 | |
0a36105d JM |
8384 | -- The prefix itself may be overloaded, and its interpretations |
8385 | -- must be propagated to the new actual in the call. | |
8386 | ||
8387 | if Is_Overloaded (Obj) then | |
8388 | Save_Interps (Obj, First_Actual); | |
8389 | end if; | |
8390 | ||
8391 | Rewrite (First_Actual, Obj); | |
8392 | ||
8393 | elsif not Is_Access_Type (Formal_Type) | |
ec6078e3 ES |
8394 | and then Is_Access_Type (Etype (Obj)) |
8395 | then | |
8396 | Rewrite (First_Actual, | |
8397 | Make_Explicit_Dereference (Sloc (Obj), Obj)); | |
8398 | Analyze (First_Actual); | |
fe45e59e | 8399 | |
401093c1 ES |
8400 | -- If we need to introduce an explicit dereference, verify that |
8401 | -- the resulting actual is compatible with the mode of the formal. | |
8402 | ||
8403 | if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter | |
8404 | and then Is_Access_Constant (Etype (Obj)) | |
8405 | then | |
8406 | Error_Msg_NE | |
8407 | ("expect variable in call to&", Prefix (N), Entity (Subprog)); | |
8408 | end if; | |
8409 | ||
d469eabe HK |
8410 | -- Conversely, if the formal is an access parameter and the object |
8411 | -- is not, replace the actual with a 'Access reference. Its analysis | |
8412 | -- will check that the object is aliased. | |
fe45e59e ES |
8413 | |
8414 | elsif Is_Access_Type (Formal_Type) | |
8415 | and then not Is_Access_Type (Etype (Obj)) | |
8416 | then | |
b4592168 GD |
8417 | -- A special case: A.all'access is illegal if A is an access to a |
8418 | -- constant and the context requires an access to a variable. | |
8419 | ||
8420 | if not Is_Access_Constant (Formal_Type) then | |
8421 | if (Nkind (Obj) = N_Explicit_Dereference | |
8422 | and then Is_Access_Constant (Etype (Prefix (Obj)))) | |
8423 | or else not Is_Variable (Obj) | |
8424 | then | |
8425 | Error_Msg_NE | |
ad075b50 | 8426 | ("actual for & must be a variable", Obj, Control); |
b4592168 GD |
8427 | end if; |
8428 | end if; | |
8429 | ||
fe45e59e ES |
8430 | Rewrite (First_Actual, |
8431 | Make_Attribute_Reference (Loc, | |
8432 | Attribute_Name => Name_Access, | |
8433 | Prefix => Relocate_Node (Obj))); | |
0a36105d JM |
8434 | |
8435 | if not Is_Aliased_View (Obj) then | |
ed2233dc | 8436 | Error_Msg_NE |
ad075b50 | 8437 | ("object in prefixed call to & must be aliased " |
715e529d | 8438 | & "(RM 4.1.3 (13 1/2))", Prefix (First_Actual), Subprog); |
0a36105d JM |
8439 | end if; |
8440 | ||
fe45e59e ES |
8441 | Analyze (First_Actual); |
8442 | ||
ec6078e3 | 8443 | else |
0a36105d JM |
8444 | if Is_Overloaded (Obj) then |
8445 | Save_Interps (Obj, First_Actual); | |
8446 | end if; | |
ec6078e3 | 8447 | |
0a36105d | 8448 | Rewrite (First_Actual, Obj); |
aab883ec ES |
8449 | end if; |
8450 | ||
e699b76e AC |
8451 | -- The operation is obtained from the dispatch table and not by |
8452 | -- visibility, and may be declared in a unit that is not explicitly | |
8453 | -- referenced in the source, but is nevertheless required in the | |
8454 | -- context of the current unit. Indicate that operation and its scope | |
8455 | -- are referenced, to prevent spurious and misleading warnings. If | |
8456 | -- the operation is overloaded, all primitives are in the same scope | |
8457 | -- and we can use any of them. | |
8458 | ||
8459 | Set_Referenced (Entity (Subprog), True); | |
8460 | Set_Referenced (Scope (Entity (Subprog)), True); | |
8461 | ||
7ffd9312 | 8462 | Rewrite (Node_To_Replace, Call_Node); |
0a36105d JM |
8463 | |
8464 | -- Propagate the interpretations collected in subprog to the new | |
8465 | -- function call node, to be resolved from context. | |
8466 | ||
8467 | if Is_Overloaded (Subprog) then | |
8468 | Save_Interps (Subprog, Node_To_Replace); | |
7415029d | 8469 | |
0a36105d | 8470 | else |
28e18b4f AC |
8471 | -- The type of the subprogram may be a limited view obtained |
8472 | -- transitively from another unit. If full view is available, | |
8473 | -- use it to analyze call. | |
8474 | ||
8475 | declare | |
8476 | T : constant Entity_Id := Etype (Subprog); | |
8477 | begin | |
8478 | if From_Limited_With (T) then | |
8479 | Set_Etype (Entity (Subprog), Available_View (T)); | |
8480 | end if; | |
8481 | end; | |
8482 | ||
0a36105d | 8483 | Analyze (Node_To_Replace); |
438ff97c | 8484 | |
199c6a10 AC |
8485 | -- If the operation has been rewritten into a call, which may get |
8486 | -- subsequently an explicit dereference, preserve the type on the | |
8487 | -- original node (selected component or indexed component) for | |
8488 | -- subsequent legality tests, e.g. Is_Variable. which examines | |
8489 | -- the original node. | |
438ff97c ES |
8490 | |
8491 | if Nkind (Node_To_Replace) = N_Function_Call then | |
8492 | Set_Etype | |
8493 | (Original_Node (Node_To_Replace), Etype (Node_To_Replace)); | |
8494 | end if; | |
0a36105d | 8495 | end if; |
4c46b835 AC |
8496 | end Complete_Object_Operation; |
8497 | ||
0a36105d JM |
8498 | ---------------------- |
8499 | -- Report_Ambiguity -- | |
8500 | ---------------------- | |
8501 | ||
8502 | procedure Report_Ambiguity (Op : Entity_Id) is | |
0a36105d JM |
8503 | Access_Actual : constant Boolean := |
8504 | Is_Access_Type (Etype (Prefix (N))); | |
8cf23b91 | 8505 | Access_Formal : Boolean := False; |
0a36105d JM |
8506 | |
8507 | begin | |
8508 | Error_Msg_Sloc := Sloc (Op); | |
8509 | ||
8cf23b91 AC |
8510 | if Present (First_Formal (Op)) then |
8511 | Access_Formal := Is_Access_Type (Etype (First_Formal (Op))); | |
8512 | end if; | |
8513 | ||
0a36105d JM |
8514 | if Access_Formal and then not Access_Actual then |
8515 | if Nkind (Parent (Op)) = N_Full_Type_Declaration then | |
ed2233dc | 8516 | Error_Msg_N |
8b4230c8 AC |
8517 | ("\possible interpretation " |
8518 | & "(inherited, with implicit 'Access) #", N); | |
0a36105d | 8519 | else |
ed2233dc | 8520 | Error_Msg_N |
0a36105d JM |
8521 | ("\possible interpretation (with implicit 'Access) #", N); |
8522 | end if; | |
8523 | ||
8524 | elsif not Access_Formal and then Access_Actual then | |
8525 | if Nkind (Parent (Op)) = N_Full_Type_Declaration then | |
ed2233dc | 8526 | Error_Msg_N |
8b4230c8 | 8527 | ("\possible interpretation " |
28e18b4f | 8528 | & "(inherited, with implicit dereference) #", N); |
0a36105d | 8529 | else |
ed2233dc | 8530 | Error_Msg_N |
0a36105d JM |
8531 | ("\possible interpretation (with implicit dereference) #", N); |
8532 | end if; | |
8533 | ||
8534 | else | |
8535 | if Nkind (Parent (Op)) = N_Full_Type_Declaration then | |
ed2233dc | 8536 | Error_Msg_N ("\possible interpretation (inherited)#", N); |
0a36105d | 8537 | else |
4e7a4f6e AC |
8538 | Error_Msg_N -- CODEFIX |
8539 | ("\possible interpretation#", N); | |
0a36105d JM |
8540 | end if; |
8541 | end if; | |
8542 | end Report_Ambiguity; | |
8543 | ||
4c46b835 AC |
8544 | -------------------------------- |
8545 | -- Transform_Object_Operation -- | |
8546 | -------------------------------- | |
8547 | ||
8548 | procedure Transform_Object_Operation | |
8549 | (Call_Node : out Node_Id; | |
0a36105d | 8550 | Node_To_Replace : out Node_Id) |
35ae2ed8 | 8551 | is |
ec6078e3 ES |
8552 | Dummy : constant Node_Id := New_Copy (Obj); |
8553 | -- Placeholder used as a first parameter in the call, replaced | |
8554 | -- eventually by the proper object. | |
8555 | ||
d469eabe HK |
8556 | Parent_Node : constant Node_Id := Parent (N); |
8557 | ||
ec6078e3 | 8558 | Actual : Node_Id; |
d469eabe | 8559 | Actuals : List_Id; |
ec6078e3 | 8560 | |
35ae2ed8 | 8561 | begin |
ec6078e3 ES |
8562 | -- Common case covering 1) Call to a procedure and 2) Call to a |
8563 | -- function that has some additional actuals. | |
35ae2ed8 | 8564 | |
d3b00ce3 | 8565 | if Nkind (Parent_Node) in N_Subprogram_Call |
35ae2ed8 | 8566 | |
ec6078e3 ES |
8567 | -- N is a selected component node containing the name of the |
8568 | -- subprogram. If N is not the name of the parent node we must | |
8569 | -- not replace the parent node by the new construct. This case | |
8570 | -- occurs when N is a parameterless call to a subprogram that | |
8571 | -- is an actual parameter of a call to another subprogram. For | |
8572 | -- example: | |
8573 | -- Some_Subprogram (..., Obj.Operation, ...) | |
35ae2ed8 | 8574 | |
ec6078e3 | 8575 | and then Name (Parent_Node) = N |
4c46b835 AC |
8576 | then |
8577 | Node_To_Replace := Parent_Node; | |
35ae2ed8 | 8578 | |
ec6078e3 | 8579 | Actuals := Parameter_Associations (Parent_Node); |
d3e65aad | 8580 | |
ec6078e3 ES |
8581 | if Present (Actuals) then |
8582 | Prepend (Dummy, Actuals); | |
8583 | else | |
8584 | Actuals := New_List (Dummy); | |
8585 | end if; | |
4c46b835 AC |
8586 | |
8587 | if Nkind (Parent_Node) = N_Procedure_Call_Statement then | |
8588 | Call_Node := | |
8589 | Make_Procedure_Call_Statement (Loc, | |
0a36105d | 8590 | Name => New_Copy (Subprog), |
4c46b835 AC |
8591 | Parameter_Associations => Actuals); |
8592 | ||
8593 | else | |
4c46b835 AC |
8594 | Call_Node := |
8595 | Make_Function_Call (Loc, | |
8b4230c8 | 8596 | Name => New_Copy (Subprog), |
4c46b835 | 8597 | Parameter_Associations => Actuals); |
35ae2ed8 AC |
8598 | end if; |
8599 | ||
d469eabe | 8600 | -- Before analysis, a function call appears as an indexed component |
ec6078e3 | 8601 | -- if there are no named associations. |
758c442c | 8602 | |
c8307596 | 8603 | elsif Nkind (Parent_Node) = N_Indexed_Component |
ec6078e3 ES |
8604 | and then N = Prefix (Parent_Node) |
8605 | then | |
758c442c | 8606 | Node_To_Replace := Parent_Node; |
ec6078e3 ES |
8607 | Actuals := Expressions (Parent_Node); |
8608 | ||
8609 | Actual := First (Actuals); | |
8610 | while Present (Actual) loop | |
8611 | Analyze (Actual); | |
8612 | Next (Actual); | |
8613 | end loop; | |
8614 | ||
8615 | Prepend (Dummy, Actuals); | |
758c442c GD |
8616 | |
8617 | Call_Node := | |
8618 | Make_Function_Call (Loc, | |
8b4230c8 | 8619 | Name => New_Copy (Subprog), |
758c442c GD |
8620 | Parameter_Associations => Actuals); |
8621 | ||
d469eabe | 8622 | -- Parameterless call: Obj.F is rewritten as F (Obj) |
35ae2ed8 | 8623 | |
4c46b835 AC |
8624 | else |
8625 | Node_To_Replace := N; | |
8626 | ||
8627 | Call_Node := | |
8628 | Make_Function_Call (Loc, | |
8b4230c8 | 8629 | Name => New_Copy (Subprog), |
ec6078e3 | 8630 | Parameter_Associations => New_List (Dummy)); |
4c46b835 AC |
8631 | end if; |
8632 | end Transform_Object_Operation; | |
35ae2ed8 AC |
8633 | |
8634 | ------------------------------ | |
8635 | -- Try_Class_Wide_Operation -- | |
8636 | ------------------------------ | |
8637 | ||
8638 | function Try_Class_Wide_Operation | |
4c46b835 AC |
8639 | (Call_Node : Node_Id; |
8640 | Node_To_Replace : Node_Id) return Boolean | |
35ae2ed8 | 8641 | is |
0a36105d JM |
8642 | Anc_Type : Entity_Id; |
8643 | Matching_Op : Entity_Id := Empty; | |
8644 | Error : Boolean; | |
8645 | ||
8646 | procedure Traverse_Homonyms | |
8647 | (Anc_Type : Entity_Id; | |
8648 | Error : out Boolean); | |
8649 | -- Traverse the homonym chain of the subprogram searching for those | |
8650 | -- homonyms whose first formal has the Anc_Type's class-wide type, | |
d469eabe HK |
8651 | -- or an anonymous access type designating the class-wide type. If |
8652 | -- an ambiguity is detected, then Error is set to True. | |
0a36105d JM |
8653 | |
8654 | procedure Traverse_Interfaces | |
8655 | (Anc_Type : Entity_Id; | |
8656 | Error : out Boolean); | |
8657 | -- Traverse the list of interfaces, if any, associated with Anc_Type | |
8658 | -- and search for acceptable class-wide homonyms associated with each | |
8659 | -- interface. If an ambiguity is detected, then Error is set to True. | |
8660 | ||
8661 | ----------------------- | |
8662 | -- Traverse_Homonyms -- | |
8663 | ----------------------- | |
8664 | ||
8665 | procedure Traverse_Homonyms | |
8666 | (Anc_Type : Entity_Id; | |
8667 | Error : out Boolean) | |
8668 | is | |
8669 | Cls_Type : Entity_Id; | |
8670 | Hom : Entity_Id; | |
8671 | Hom_Ref : Node_Id; | |
8672 | Success : Boolean; | |
35ae2ed8 | 8673 | |
0a36105d JM |
8674 | begin |
8675 | Error := False; | |
ec6078e3 | 8676 | |
b67a385c ES |
8677 | Cls_Type := Class_Wide_Type (Anc_Type); |
8678 | ||
4c46b835 | 8679 | Hom := Current_Entity (Subprog); |
401093c1 | 8680 | |
383e179e AC |
8681 | -- Find a non-hidden operation whose first parameter is of the |
8682 | -- class-wide type, a subtype thereof, or an anonymous access | |
a68d415b | 8683 | -- to same. If in an instance, the operation can be considered |
8b4230c8 AC |
8684 | -- even if hidden (it may be hidden because the instantiation |
8685 | -- is expanded after the containing package has been analyzed). | |
401093c1 | 8686 | |
35ae2ed8 | 8687 | while Present (Hom) loop |
6a2e4f0b | 8688 | if Ekind_In (Hom, E_Procedure, E_Function) |
a68d415b | 8689 | and then (not Is_Hidden (Hom) or else In_Instance) |
b67a385c | 8690 | and then Scope (Hom) = Scope (Anc_Type) |
4c46b835 | 8691 | and then Present (First_Formal (Hom)) |
b67a385c | 8692 | and then |
401093c1 | 8693 | (Base_Type (Etype (First_Formal (Hom))) = Cls_Type |
b67a385c ES |
8694 | or else |
8695 | (Is_Access_Type (Etype (First_Formal (Hom))) | |
8b4230c8 AC |
8696 | and then |
8697 | Ekind (Etype (First_Formal (Hom))) = | |
8698 | E_Anonymous_Access_Type | |
8699 | and then | |
8700 | Base_Type | |
8701 | (Designated_Type (Etype (First_Formal (Hom)))) = | |
0a36105d | 8702 | Cls_Type)) |
35ae2ed8 | 8703 | then |
88f47280 AC |
8704 | -- If the context is a procedure call, ignore functions |
8705 | -- in the name of the call. | |
8706 | ||
8707 | if Ekind (Hom) = E_Function | |
8708 | and then Nkind (Parent (N)) = N_Procedure_Call_Statement | |
8709 | and then N = Name (Parent (N)) | |
8710 | then | |
8711 | goto Next_Hom; | |
11fa950b AC |
8712 | |
8713 | -- If the context is a function call, ignore procedures | |
8714 | -- in the name of the call. | |
8715 | ||
8716 | elsif Ekind (Hom) = E_Procedure | |
8717 | and then Nkind (Parent (N)) /= N_Procedure_Call_Statement | |
8718 | then | |
8719 | goto Next_Hom; | |
88f47280 AC |
8720 | end if; |
8721 | ||
ec6078e3 | 8722 | Set_Etype (Call_Node, Any_Type); |
0a36105d JM |
8723 | Set_Is_Overloaded (Call_Node, False); |
8724 | Success := False; | |
4c46b835 | 8725 | |
0a36105d | 8726 | if No (Matching_Op) then |
e4494292 | 8727 | Hom_Ref := New_Occurrence_Of (Hom, Sloc (Subprog)); |
0a36105d JM |
8728 | Set_Etype (Call_Node, Any_Type); |
8729 | Set_Parent (Call_Node, Parent (Node_To_Replace)); | |
4c46b835 | 8730 | |
0a36105d | 8731 | Set_Name (Call_Node, Hom_Ref); |
4c46b835 | 8732 | |
0a36105d JM |
8733 | Analyze_One_Call |
8734 | (N => Call_Node, | |
8735 | Nam => Hom, | |
8736 | Report => Report_Error, | |
8737 | Success => Success, | |
8738 | Skip_First => True); | |
4c46b835 | 8739 | |
0a36105d JM |
8740 | Matching_Op := |
8741 | Valid_Candidate (Success, Call_Node, Hom); | |
4c46b835 | 8742 | |
0a36105d JM |
8743 | else |
8744 | Analyze_One_Call | |
8745 | (N => Call_Node, | |
8746 | Nam => Hom, | |
8747 | Report => Report_Error, | |
8748 | Success => Success, | |
8749 | Skip_First => True); | |
8750 | ||
8751 | if Present (Valid_Candidate (Success, Call_Node, Hom)) | |
8752 | and then Nkind (Call_Node) /= N_Function_Call | |
8753 | then | |
ed2233dc | 8754 | Error_Msg_NE ("ambiguous call to&", N, Hom); |
0a36105d JM |
8755 | Report_Ambiguity (Matching_Op); |
8756 | Report_Ambiguity (Hom); | |
8757 | Error := True; | |
8758 | return; | |
8759 | end if; | |
35ae2ed8 AC |
8760 | end if; |
8761 | end if; | |
8762 | ||
88f47280 AC |
8763 | <<Next_Hom>> |
8764 | Hom := Homonym (Hom); | |
35ae2ed8 | 8765 | end loop; |
0a36105d JM |
8766 | end Traverse_Homonyms; |
8767 | ||
8768 | ------------------------- | |
8769 | -- Traverse_Interfaces -- | |
8770 | ------------------------- | |
35ae2ed8 | 8771 | |
0a36105d JM |
8772 | procedure Traverse_Interfaces |
8773 | (Anc_Type : Entity_Id; | |
8774 | Error : out Boolean) | |
8775 | is | |
0a36105d JM |
8776 | Intface_List : constant List_Id := |
8777 | Abstract_Interface_List (Anc_Type); | |
d469eabe | 8778 | Intface : Node_Id; |
0a36105d JM |
8779 | |
8780 | begin | |
8781 | Error := False; | |
8782 | ||
8783 | if Is_Non_Empty_List (Intface_List) then | |
8784 | Intface := First (Intface_List); | |
8785 | while Present (Intface) loop | |
8786 | ||
8787 | -- Look for acceptable class-wide homonyms associated with | |
8788 | -- the interface. | |
8789 | ||
8790 | Traverse_Homonyms (Etype (Intface), Error); | |
8791 | ||
8792 | if Error then | |
8793 | return; | |
8794 | end if; | |
8795 | ||
8796 | -- Continue the search by looking at each of the interface's | |
8797 | -- associated interface ancestors. | |
8798 | ||
8799 | Traverse_Interfaces (Etype (Intface), Error); | |
8800 | ||
8801 | if Error then | |
8802 | return; | |
8803 | end if; | |
8804 | ||
8805 | Next (Intface); | |
8806 | end loop; | |
8807 | end if; | |
8808 | end Traverse_Interfaces; | |
8809 | ||
8810 | -- Start of processing for Try_Class_Wide_Operation | |
8811 | ||
8812 | begin | |
8cf23b91 AC |
8813 | -- If we are searching only for conflicting class-wide subprograms |
8814 | -- then initialize directly Matching_Op with the target entity. | |
8815 | ||
8816 | if CW_Test_Only then | |
8817 | Matching_Op := Entity (Selector_Name (N)); | |
8818 | end if; | |
8819 | ||
d469eabe HK |
8820 | -- Loop through ancestor types (including interfaces), traversing |
8821 | -- the homonym chain of the subprogram, trying out those homonyms | |
8822 | -- whose first formal has the class-wide type of the ancestor, or | |
8823 | -- an anonymous access type designating the class-wide type. | |
0a36105d JM |
8824 | |
8825 | Anc_Type := Obj_Type; | |
8826 | loop | |
8827 | -- Look for a match among homonyms associated with the ancestor | |
8828 | ||
8829 | Traverse_Homonyms (Anc_Type, Error); | |
8830 | ||
8831 | if Error then | |
8832 | return True; | |
8833 | end if; | |
8834 | ||
8835 | -- Continue the search for matches among homonyms associated with | |
8836 | -- any interfaces implemented by the ancestor. | |
8837 | ||
8838 | Traverse_Interfaces (Anc_Type, Error); | |
8839 | ||
8840 | if Error then | |
8841 | return True; | |
8842 | end if; | |
35ae2ed8 | 8843 | |
4c46b835 AC |
8844 | exit when Etype (Anc_Type) = Anc_Type; |
8845 | Anc_Type := Etype (Anc_Type); | |
35ae2ed8 AC |
8846 | end loop; |
8847 | ||
0a36105d JM |
8848 | if Present (Matching_Op) then |
8849 | Set_Etype (Call_Node, Etype (Matching_Op)); | |
8850 | end if; | |
ec6078e3 | 8851 | |
0a36105d | 8852 | return Present (Matching_Op); |
35ae2ed8 AC |
8853 | end Try_Class_Wide_Operation; |
8854 | ||
0a36105d JM |
8855 | ----------------------------------- |
8856 | -- Try_One_Prefix_Interpretation -- | |
8857 | ----------------------------------- | |
8858 | ||
8859 | procedure Try_One_Prefix_Interpretation (T : Entity_Id) is | |
e3d9f448 AC |
8860 | |
8861 | -- If the interpretation does not have a valid candidate type, | |
8862 | -- preserve current value of Obj_Type for subsequent errors. | |
8863 | ||
8864 | Prev_Obj_Type : constant Entity_Id := Obj_Type; | |
8865 | ||
0a36105d JM |
8866 | begin |
8867 | Obj_Type := T; | |
8868 | ||
8869 | if Is_Access_Type (Obj_Type) then | |
8870 | Obj_Type := Designated_Type (Obj_Type); | |
8871 | end if; | |
8872 | ||
8873 | if Ekind (Obj_Type) = E_Private_Subtype then | |
8874 | Obj_Type := Base_Type (Obj_Type); | |
8875 | end if; | |
8876 | ||
8877 | if Is_Class_Wide_Type (Obj_Type) then | |
8878 | Obj_Type := Etype (Class_Wide_Type (Obj_Type)); | |
8879 | end if; | |
8880 | ||
8881 | -- The type may have be obtained through a limited_with clause, | |
8882 | -- in which case the primitive operations are available on its | |
401093c1 | 8883 | -- non-limited view. If still incomplete, retrieve full view. |
0a36105d JM |
8884 | |
8885 | if Ekind (Obj_Type) = E_Incomplete_Type | |
7b56a91b | 8886 | and then From_Limited_With (Obj_Type) |
47346923 | 8887 | and then Has_Non_Limited_View (Obj_Type) |
0a36105d | 8888 | then |
401093c1 | 8889 | Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type)); |
0a36105d JM |
8890 | end if; |
8891 | ||
8892 | -- If the object is not tagged, or the type is still an incomplete | |
8893 | -- type, this is not a prefixed call. | |
8894 | ||
8895 | if not Is_Tagged_Type (Obj_Type) | |
8896 | or else Is_Incomplete_Type (Obj_Type) | |
8897 | then | |
e3d9f448 | 8898 | |
00c93ba2 | 8899 | -- Restore previous type if current one is not legal candidate |
e3d9f448 AC |
8900 | |
8901 | Obj_Type := Prev_Obj_Type; | |
0a36105d JM |
8902 | return; |
8903 | end if; | |
8904 | ||
11fa950b AC |
8905 | declare |
8906 | Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node); | |
8907 | CW_Result : Boolean; | |
8908 | Prim_Result : Boolean; | |
8909 | pragma Unreferenced (CW_Result); | |
8910 | ||
8911 | begin | |
8cf23b91 AC |
8912 | if not CW_Test_Only then |
8913 | Prim_Result := | |
8914 | Try_Primitive_Operation | |
8915 | (Call_Node => New_Call_Node, | |
8916 | Node_To_Replace => Node_To_Replace); | |
8917 | end if; | |
11fa950b AC |
8918 | |
8919 | -- Check if there is a class-wide subprogram covering the | |
8920 | -- primitive. This check must be done even if a candidate | |
8921 | -- was found in order to report ambiguous calls. | |
8922 | ||
8923 | if not (Prim_Result) then | |
8924 | CW_Result := | |
8925 | Try_Class_Wide_Operation | |
8926 | (Call_Node => New_Call_Node, | |
8927 | Node_To_Replace => Node_To_Replace); | |
8928 | ||
8929 | -- If we found a primitive we search for class-wide subprograms | |
8930 | -- using a duplicate of the call node (done to avoid missing its | |
8931 | -- decoration if there is no ambiguity). | |
8932 | ||
8933 | else | |
8934 | CW_Result := | |
8935 | Try_Class_Wide_Operation | |
8936 | (Call_Node => Dup_Call_Node, | |
8937 | Node_To_Replace => Node_To_Replace); | |
8938 | end if; | |
8939 | end; | |
0a36105d JM |
8940 | end Try_One_Prefix_Interpretation; |
8941 | ||
4c46b835 AC |
8942 | ----------------------------- |
8943 | -- Try_Primitive_Operation -- | |
8944 | ----------------------------- | |
35ae2ed8 | 8945 | |
4c46b835 AC |
8946 | function Try_Primitive_Operation |
8947 | (Call_Node : Node_Id; | |
8948 | Node_To_Replace : Node_Id) return Boolean | |
35ae2ed8 | 8949 | is |
6e73e3ab AC |
8950 | Elmt : Elmt_Id; |
8951 | Prim_Op : Entity_Id; | |
0a36105d JM |
8952 | Matching_Op : Entity_Id := Empty; |
8953 | Prim_Op_Ref : Node_Id := Empty; | |
8954 | ||
8b4230c8 | 8955 | Corr_Type : Entity_Id := Empty; |
0a36105d JM |
8956 | -- If the prefix is a synchronized type, the controlling type of |
8957 | -- the primitive operation is the corresponding record type, else | |
8958 | -- this is the object type itself. | |
8959 | ||
8b4230c8 | 8960 | Success : Boolean := False; |
35ae2ed8 | 8961 | |
401093c1 ES |
8962 | function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id; |
8963 | -- For tagged types the candidate interpretations are found in | |
8964 | -- the list of primitive operations of the type and its ancestors. | |
8965 | -- For formal tagged types we have to find the operations declared | |
8966 | -- in the same scope as the type (including in the generic formal | |
8967 | -- part) because the type itself carries no primitive operations, | |
8968 | -- except for formal derived types that inherit the operations of | |
8969 | -- the parent and progenitors. | |
8b4230c8 | 8970 | -- |
d469eabe HK |
8971 | -- If the context is a generic subprogram body, the generic formals |
8972 | -- are visible by name, but are not in the entity list of the | |
8973 | -- subprogram because that list starts with the subprogram formals. | |
8974 | -- We retrieve the candidate operations from the generic declaration. | |
401093c1 | 8975 | |
84dad556 AC |
8976 | function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id; |
8977 | -- Prefix notation can also be used on operations that are not | |
8978 | -- primitives of the type, but are declared in the same immediate | |
8979 | -- declarative part, which can only mean the corresponding package | |
8980 | -- body (See RM 4.1.3 (9.2/3)). If we are in that body we extend the | |
8981 | -- list of primitives with body operations with the same name that | |
8982 | -- may be candidates, so that Try_Primitive_Operations can examine | |
8983 | -- them if no real primitive is found. | |
8984 | ||
dfcfdc0a AC |
8985 | function Is_Private_Overriding (Op : Entity_Id) return Boolean; |
8986 | -- An operation that overrides an inherited operation in the private | |
8987 | -- part of its package may be hidden, but if the inherited operation | |
8988 | -- is visible a direct call to it will dispatch to the private one, | |
8989 | -- which is therefore a valid candidate. | |
8990 | ||
42f11e4c AC |
8991 | function Names_Match |
8992 | (Obj_Type : Entity_Id; | |
8993 | Prim_Op : Entity_Id; | |
8994 | Subprog : Entity_Id) return Boolean; | |
8995 | -- Return True if the names of Prim_Op and Subprog match. If Obj_Type | |
8996 | -- is a protected type then compare also the original name of Prim_Op | |
8997 | -- with the name of Subprog (since the expander may have added a | |
8998 | -- prefix to its original name --see Exp_Ch9.Build_Selected_Name). | |
8999 | ||
ec6078e3 ES |
9000 | function Valid_First_Argument_Of (Op : Entity_Id) return Boolean; |
9001 | -- Verify that the prefix, dereferenced if need be, is a valid | |
9002 | -- controlling argument in a call to Op. The remaining actuals | |
9003 | -- are checked in the subsequent call to Analyze_One_Call. | |
35ae2ed8 | 9004 | |
401093c1 ES |
9005 | ------------------------------ |
9006 | -- Collect_Generic_Type_Ops -- | |
9007 | ------------------------------ | |
9008 | ||
9009 | function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is | |
9010 | Bas : constant Entity_Id := Base_Type (T); | |
9011 | Candidates : constant Elist_Id := New_Elmt_List; | |
9012 | Subp : Entity_Id; | |
9013 | Formal : Entity_Id; | |
9014 | ||
d469eabe HK |
9015 | procedure Check_Candidate; |
9016 | -- The operation is a candidate if its first parameter is a | |
9017 | -- controlling operand of the desired type. | |
9018 | ||
9019 | ----------------------- | |
9020 | -- Check_Candidate; -- | |
9021 | ----------------------- | |
9022 | ||
9023 | procedure Check_Candidate is | |
9024 | begin | |
9025 | Formal := First_Formal (Subp); | |
9026 | ||
9027 | if Present (Formal) | |
9028 | and then Is_Controlling_Formal (Formal) | |
9029 | and then | |
9030 | (Base_Type (Etype (Formal)) = Bas | |
9031 | or else | |
9032 | (Is_Access_Type (Etype (Formal)) | |
9033 | and then Designated_Type (Etype (Formal)) = Bas)) | |
9034 | then | |
9035 | Append_Elmt (Subp, Candidates); | |
9036 | end if; | |
9037 | end Check_Candidate; | |
9038 | ||
9039 | -- Start of processing for Collect_Generic_Type_Ops | |
9040 | ||
401093c1 ES |
9041 | begin |
9042 | if Is_Derived_Type (T) then | |
9043 | return Primitive_Operations (T); | |
9044 | ||
bce79204 AC |
9045 | elsif Ekind_In (Scope (T), E_Procedure, E_Function) then |
9046 | ||
d469eabe HK |
9047 | -- Scan the list of generic formals to find subprograms |
9048 | -- that may have a first controlling formal of the type. | |
9049 | ||
8b4230c8 AC |
9050 | if Nkind (Unit_Declaration_Node (Scope (T))) = |
9051 | N_Generic_Subprogram_Declaration | |
bb10b891 AC |
9052 | then |
9053 | declare | |
9054 | Decl : Node_Id; | |
9055 | ||
9056 | begin | |
9057 | Decl := | |
9058 | First (Generic_Formal_Declarations | |
9059 | (Unit_Declaration_Node (Scope (T)))); | |
9060 | while Present (Decl) loop | |
9061 | if Nkind (Decl) in N_Formal_Subprogram_Declaration then | |
9062 | Subp := Defining_Entity (Decl); | |
9063 | Check_Candidate; | |
9064 | end if; | |
d469eabe | 9065 | |
bb10b891 AC |
9066 | Next (Decl); |
9067 | end loop; | |
9068 | end; | |
9069 | end if; | |
d469eabe HK |
9070 | return Candidates; |
9071 | ||
401093c1 ES |
9072 | else |
9073 | -- Scan the list of entities declared in the same scope as | |
9074 | -- the type. In general this will be an open scope, given that | |
9075 | -- the call we are analyzing can only appear within a generic | |
9076 | -- declaration or body (either the one that declares T, or a | |
9077 | -- child unit). | |
9078 | ||
bb10b891 AC |
9079 | -- For a subtype representing a generic actual type, go to the |
9080 | -- base type. | |
9081 | ||
9082 | if Is_Generic_Actual_Type (T) then | |
9083 | Subp := First_Entity (Scope (Base_Type (T))); | |
9084 | else | |
9085 | Subp := First_Entity (Scope (T)); | |
9086 | end if; | |
9087 | ||
401093c1 ES |
9088 | while Present (Subp) loop |
9089 | if Is_Overloadable (Subp) then | |
d469eabe | 9090 | Check_Candidate; |
401093c1 ES |
9091 | end if; |
9092 | ||
9093 | Next_Entity (Subp); | |
9094 | end loop; | |
9095 | ||
9096 | return Candidates; | |
9097 | end if; | |
9098 | end Collect_Generic_Type_Ops; | |
9099 | ||
84dad556 AC |
9100 | ---------------------------- |
9101 | -- Extended_Primitive_Ops -- | |
9102 | ---------------------------- | |
9103 | ||
9104 | function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id is | |
9105 | Type_Scope : constant Entity_Id := Scope (T); | |
9106 | ||
9107 | Body_Decls : List_Id; | |
9108 | Op_Found : Boolean; | |
9109 | Op : Entity_Id; | |
9110 | Op_List : Elist_Id; | |
9111 | ||
9112 | begin | |
9113 | Op_List := Primitive_Operations (T); | |
9114 | ||
9115 | if Ekind (Type_Scope) = E_Package | |
9116 | and then In_Package_Body (Type_Scope) | |
9117 | and then In_Open_Scopes (Type_Scope) | |
9118 | then | |
9119 | -- Retrieve list of declarations of package body. | |
9120 | ||
9121 | Body_Decls := | |
9122 | Declarations | |
9123 | (Unit_Declaration_Node | |
9124 | (Corresponding_Body | |
9125 | (Unit_Declaration_Node (Type_Scope)))); | |
9126 | ||
9127 | Op := Current_Entity (Subprog); | |
9128 | Op_Found := False; | |
9129 | while Present (Op) loop | |
9130 | if Comes_From_Source (Op) | |
9131 | and then Is_Overloadable (Op) | |
e23e04db AC |
9132 | |
9133 | -- Exclude overriding primitive operations of a type | |
9134 | -- extension declared in the package body, to prevent | |
9135 | -- duplicates in extended list. | |
9136 | ||
9137 | and then not Is_Primitive (Op) | |
84dad556 AC |
9138 | and then Is_List_Member (Unit_Declaration_Node (Op)) |
9139 | and then List_Containing (Unit_Declaration_Node (Op)) = | |
9140 | Body_Decls | |
9141 | then | |
9142 | if not Op_Found then | |
9143 | ||
9144 | -- Copy list of primitives so it is not affected for | |
9145 | -- other uses. | |
9146 | ||
9147 | Op_List := New_Copy_Elist (Op_List); | |
9148 | Op_Found := True; | |
9149 | end if; | |
9150 | ||
9151 | Append_Elmt (Op, Op_List); | |
9152 | end if; | |
9153 | ||
9154 | Op := Homonym (Op); | |
9155 | end loop; | |
9156 | end if; | |
9157 | ||
9158 | return Op_List; | |
9159 | end Extended_Primitive_Ops; | |
9160 | ||
dfcfdc0a AC |
9161 | --------------------------- |
9162 | -- Is_Private_Overriding -- | |
9163 | --------------------------- | |
9164 | ||
9165 | function Is_Private_Overriding (Op : Entity_Id) return Boolean is | |
9166 | Visible_Op : constant Entity_Id := Homonym (Op); | |
9167 | ||
9168 | begin | |
9169 | return Present (Visible_Op) | |
6465b6a7 | 9170 | and then Scope (Op) = Scope (Visible_Op) |
dfcfdc0a AC |
9171 | and then not Comes_From_Source (Visible_Op) |
9172 | and then Alias (Visible_Op) = Op | |
9173 | and then not Is_Hidden (Visible_Op); | |
9174 | end Is_Private_Overriding; | |
9175 | ||
42f11e4c AC |
9176 | ----------------- |
9177 | -- Names_Match -- | |
9178 | ----------------- | |
9179 | ||
9180 | function Names_Match | |
9181 | (Obj_Type : Entity_Id; | |
9182 | Prim_Op : Entity_Id; | |
9183 | Subprog : Entity_Id) return Boolean is | |
9184 | begin | |
9185 | -- Common case: exact match | |
9186 | ||
9187 | if Chars (Prim_Op) = Chars (Subprog) then | |
9188 | return True; | |
9189 | ||
9190 | -- For protected type primitives the expander may have built the | |
9191 | -- name of the dispatching primitive prepending the type name to | |
9192 | -- avoid conflicts with the name of the protected subprogram (see | |
9193 | -- Exp_Ch9.Build_Selected_Name). | |
9194 | ||
9195 | elsif Is_Protected_Type (Obj_Type) then | |
bac5ba15 AC |
9196 | return |
9197 | Present (Original_Protected_Subprogram (Prim_Op)) | |
9198 | and then Chars (Original_Protected_Subprogram (Prim_Op)) = | |
9199 | Chars (Subprog); | |
42f11e4c AC |
9200 | end if; |
9201 | ||
9202 | return False; | |
9203 | end Names_Match; | |
9204 | ||
ec6078e3 ES |
9205 | ----------------------------- |
9206 | -- Valid_First_Argument_Of -- | |
9207 | ----------------------------- | |
35ae2ed8 | 9208 | |
ec6078e3 | 9209 | function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is |
9febb58f | 9210 | Typ : Entity_Id := Etype (First_Formal (Op)); |
35ae2ed8 | 9211 | |
ec6078e3 | 9212 | begin |
9febb58f JM |
9213 | if Is_Concurrent_Type (Typ) |
9214 | and then Present (Corresponding_Record_Type (Typ)) | |
9215 | then | |
9216 | Typ := Corresponding_Record_Type (Typ); | |
9217 | end if; | |
9218 | ||
d469eabe HK |
9219 | -- Simple case. Object may be a subtype of the tagged type or |
9220 | -- may be the corresponding record of a synchronized type. | |
5d09245e | 9221 | |
aab883ec | 9222 | return Obj_Type = Typ |
d469eabe | 9223 | or else Base_Type (Obj_Type) = Typ |
0a36105d JM |
9224 | or else Corr_Type = Typ |
9225 | ||
9226 | -- Prefix can be dereferenced | |
725e2a15 | 9227 | |
ec6078e3 | 9228 | or else |
0a36105d JM |
9229 | (Is_Access_Type (Corr_Type) |
9230 | and then Designated_Type (Corr_Type) = Typ) | |
5d09245e | 9231 | |
0a36105d JM |
9232 | -- Formal is an access parameter, for which the object |
9233 | -- can provide an access. | |
35ae2ed8 | 9234 | |
ec6078e3 ES |
9235 | or else |
9236 | (Ekind (Typ) = E_Anonymous_Access_Type | |
9fde638d RD |
9237 | and then |
9238 | Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type)); | |
ec6078e3 | 9239 | end Valid_First_Argument_Of; |
35ae2ed8 | 9240 | |
ec6078e3 | 9241 | -- Start of processing for Try_Primitive_Operation |
35ae2ed8 | 9242 | |
ec6078e3 | 9243 | begin |
d469eabe | 9244 | -- Look for subprograms in the list of primitive operations. The name |
0a36105d JM |
9245 | -- must be identical, and the kind of call indicates the expected |
9246 | -- kind of operation (function or procedure). If the type is a | |
d469eabe | 9247 | -- (tagged) synchronized type, the primitive ops are attached to the |
b4592168 | 9248 | -- corresponding record (base) type. |
aab883ec ES |
9249 | |
9250 | if Is_Concurrent_Type (Obj_Type) then | |
bb10b891 AC |
9251 | if Present (Corresponding_Record_Type (Obj_Type)) then |
9252 | Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type)); | |
9253 | Elmt := First_Elmt (Primitive_Operations (Corr_Type)); | |
9254 | else | |
9255 | Corr_Type := Obj_Type; | |
9256 | Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type)); | |
15e4986c JM |
9257 | end if; |
9258 | ||
401093c1 | 9259 | elsif not Is_Generic_Type (Obj_Type) then |
0a36105d | 9260 | Corr_Type := Obj_Type; |
84dad556 | 9261 | Elmt := First_Elmt (Extended_Primitive_Ops (Obj_Type)); |
401093c1 ES |
9262 | |
9263 | else | |
9264 | Corr_Type := Obj_Type; | |
9265 | Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type)); | |
aab883ec | 9266 | end if; |
35ae2ed8 | 9267 | |
ec6078e3 ES |
9268 | while Present (Elmt) loop |
9269 | Prim_Op := Node (Elmt); | |
9270 | ||
42f11e4c | 9271 | if Names_Match (Obj_Type, Prim_Op, Subprog) |
ec6078e3 ES |
9272 | and then Present (First_Formal (Prim_Op)) |
9273 | and then Valid_First_Argument_Of (Prim_Op) | |
fe45e59e | 9274 | and then |
7415029d | 9275 | (Nkind (Call_Node) = N_Function_Call) |
8b4230c8 AC |
9276 | = |
9277 | (Ekind (Prim_Op) = E_Function) | |
ec6078e3 | 9278 | then |
b67a385c | 9279 | -- Ada 2005 (AI-251): If this primitive operation corresponds |
8b4230c8 | 9280 | -- to an immediate ancestor interface there is no need to add |
b67a385c ES |
9281 | -- it to the list of interpretations; the corresponding aliased |
9282 | -- primitive is also in this list of primitive operations and | |
9283 | -- will be used instead. | |
fe45e59e | 9284 | |
ce2b6ba5 JM |
9285 | if (Present (Interface_Alias (Prim_Op)) |
9286 | and then Is_Ancestor (Find_Dispatching_Type | |
9287 | (Alias (Prim_Op)), Corr_Type)) | |
0a36105d | 9288 | |
dfcfdc0a AC |
9289 | -- Do not consider hidden primitives unless the type is in an |
9290 | -- open scope or we are within an instance, where visibility | |
9291 | -- is known to be correct, or else if this is an overriding | |
9292 | -- operation in the private part for an inherited operation. | |
0a36105d | 9293 | |
dfcfdc0a AC |
9294 | or else (Is_Hidden (Prim_Op) |
9295 | and then not Is_Immediately_Visible (Obj_Type) | |
9296 | and then not In_Instance | |
9297 | and then not Is_Private_Overriding (Prim_Op)) | |
fe45e59e ES |
9298 | then |
9299 | goto Continue; | |
9300 | end if; | |
9301 | ||
0a36105d JM |
9302 | Set_Etype (Call_Node, Any_Type); |
9303 | Set_Is_Overloaded (Call_Node, False); | |
9304 | ||
9305 | if No (Matching_Op) then | |
e4494292 | 9306 | Prim_Op_Ref := New_Occurrence_Of (Prim_Op, Sloc (Subprog)); |
b67a385c | 9307 | Candidate := Prim_Op; |
35ae2ed8 | 9308 | |
fe45e59e | 9309 | Set_Parent (Call_Node, Parent (Node_To_Replace)); |
35ae2ed8 | 9310 | |
fe45e59e | 9311 | Set_Name (Call_Node, Prim_Op_Ref); |
0a36105d | 9312 | Success := False; |
35ae2ed8 | 9313 | |
fe45e59e ES |
9314 | Analyze_One_Call |
9315 | (N => Call_Node, | |
9316 | Nam => Prim_Op, | |
b67a385c | 9317 | Report => Report_Error, |
fe45e59e ES |
9318 | Success => Success, |
9319 | Skip_First => True); | |
35ae2ed8 | 9320 | |
0a36105d | 9321 | Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op); |
fe45e59e | 9322 | |
d469eabe HK |
9323 | -- More than one interpretation, collect for subsequent |
9324 | -- disambiguation. If this is a procedure call and there | |
9325 | -- is another match, report ambiguity now. | |
0a36105d | 9326 | |
d469eabe | 9327 | else |
0a36105d JM |
9328 | Analyze_One_Call |
9329 | (N => Call_Node, | |
9330 | Nam => Prim_Op, | |
9331 | Report => Report_Error, | |
9332 | Success => Success, | |
9333 | Skip_First => True); | |
fe45e59e | 9334 | |
0a36105d JM |
9335 | if Present (Valid_Candidate (Success, Call_Node, Prim_Op)) |
9336 | and then Nkind (Call_Node) /= N_Function_Call | |
9337 | then | |
ed2233dc | 9338 | Error_Msg_NE ("ambiguous call to&", N, Prim_Op); |
0a36105d JM |
9339 | Report_Ambiguity (Matching_Op); |
9340 | Report_Ambiguity (Prim_Op); | |
9341 | return True; | |
9342 | end if; | |
4c46b835 AC |
9343 | end if; |
9344 | end if; | |
35ae2ed8 | 9345 | |
fe45e59e | 9346 | <<Continue>> |
4c46b835 AC |
9347 | Next_Elmt (Elmt); |
9348 | end loop; | |
35ae2ed8 | 9349 | |
0a36105d JM |
9350 | if Present (Matching_Op) then |
9351 | Set_Etype (Call_Node, Etype (Matching_Op)); | |
fe45e59e ES |
9352 | end if; |
9353 | ||
0a36105d | 9354 | return Present (Matching_Op); |
4c46b835 | 9355 | end Try_Primitive_Operation; |
35ae2ed8 | 9356 | |
4c46b835 | 9357 | -- Start of processing for Try_Object_Operation |
35ae2ed8 | 9358 | |
4c46b835 | 9359 | begin |
0a36105d | 9360 | Analyze_Expression (Obj); |
ec6078e3 | 9361 | |
0a36105d | 9362 | -- Analyze the actuals if node is known to be a subprogram call |
28d6470f JM |
9363 | |
9364 | if Is_Subprg_Call and then N = Name (Parent (N)) then | |
9365 | Actual := First (Parameter_Associations (Parent (N))); | |
9366 | while Present (Actual) loop | |
725e2a15 | 9367 | Analyze_Expression (Actual); |
28d6470f JM |
9368 | Next (Actual); |
9369 | end loop; | |
9370 | end if; | |
5d09245e | 9371 | |
ec6078e3 ES |
9372 | -- Build a subprogram call node, using a copy of Obj as its first |
9373 | -- actual. This is a placeholder, to be replaced by an explicit | |
9374 | -- dereference when needed. | |
4c46b835 | 9375 | |
ec6078e3 ES |
9376 | Transform_Object_Operation |
9377 | (Call_Node => New_Call_Node, | |
0a36105d | 9378 | Node_To_Replace => Node_To_Replace); |
4c46b835 | 9379 | |
ec6078e3 | 9380 | Set_Etype (New_Call_Node, Any_Type); |
0a36105d | 9381 | Set_Etype (Subprog, Any_Type); |
ec6078e3 | 9382 | Set_Parent (New_Call_Node, Parent (Node_To_Replace)); |
4c46b835 | 9383 | |
0a36105d JM |
9384 | if not Is_Overloaded (Obj) then |
9385 | Try_One_Prefix_Interpretation (Obj_Type); | |
ec6078e3 | 9386 | |
0a36105d JM |
9387 | else |
9388 | declare | |
9389 | I : Interp_Index; | |
9390 | It : Interp; | |
9391 | begin | |
9392 | Get_First_Interp (Obj, I, It); | |
9393 | while Present (It.Nam) loop | |
9394 | Try_One_Prefix_Interpretation (It.Typ); | |
9395 | Get_Next_Interp (I, It); | |
9396 | end loop; | |
9397 | end; | |
9398 | end if; | |
9399 | ||
9400 | if Etype (New_Call_Node) /= Any_Type then | |
8cf23b91 AC |
9401 | |
9402 | -- No need to complete the tree transformations if we are only | |
9403 | -- searching for conflicting class-wide subprograms | |
9404 | ||
9405 | if CW_Test_Only then | |
9406 | return False; | |
9407 | else | |
9408 | Complete_Object_Operation | |
9409 | (Call_Node => New_Call_Node, | |
9410 | Node_To_Replace => Node_To_Replace); | |
9411 | return True; | |
9412 | end if; | |
b67a385c ES |
9413 | |
9414 | elsif Present (Candidate) then | |
9415 | ||
9416 | -- The argument list is not type correct. Re-analyze with error | |
9417 | -- reporting enabled, and use one of the possible candidates. | |
d469eabe | 9418 | -- In All_Errors_Mode, re-analyze all failed interpretations. |
b67a385c ES |
9419 | |
9420 | if All_Errors_Mode then | |
9421 | Report_Error := True; | |
9422 | if Try_Primitive_Operation | |
8b4230c8 AC |
9423 | (Call_Node => New_Call_Node, |
9424 | Node_To_Replace => Node_To_Replace) | |
b67a385c ES |
9425 | |
9426 | or else | |
9427 | Try_Class_Wide_Operation | |
9428 | (Call_Node => New_Call_Node, | |
9429 | Node_To_Replace => Node_To_Replace) | |
9430 | then | |
9431 | null; | |
9432 | end if; | |
9433 | ||
9434 | else | |
9435 | Analyze_One_Call | |
9436 | (N => New_Call_Node, | |
9437 | Nam => Candidate, | |
9438 | Report => True, | |
9439 | Success => Success, | |
9440 | Skip_First => True); | |
9441 | end if; | |
9442 | ||
d469eabe HK |
9443 | -- No need for further errors |
9444 | ||
9445 | return True; | |
b67a385c ES |
9446 | |
9447 | else | |
9448 | -- There was no candidate operation, so report it as an error | |
9449 | -- in the caller: Analyze_Selected_Component. | |
9450 | ||
9451 | return False; | |
9452 | end if; | |
35ae2ed8 AC |
9453 | end Try_Object_Operation; |
9454 | ||
b4592168 GD |
9455 | --------- |
9456 | -- wpo -- | |
9457 | --------- | |
9458 | ||
9459 | procedure wpo (T : Entity_Id) is | |
9460 | Op : Entity_Id; | |
9461 | E : Elmt_Id; | |
9462 | ||
9463 | begin | |
9464 | if not Is_Tagged_Type (T) then | |
9465 | return; | |
9466 | end if; | |
9467 | ||
9468 | E := First_Elmt (Primitive_Operations (Base_Type (T))); | |
9469 | while Present (E) loop | |
9470 | Op := Node (E); | |
9471 | Write_Int (Int (Op)); | |
9472 | Write_Str (" === "); | |
9473 | Write_Name (Chars (Op)); | |
9474 | Write_Str (" in "); | |
9475 | Write_Name (Chars (Scope (Op))); | |
9476 | Next_Elmt (E); | |
9477 | Write_Eol; | |
9478 | end loop; | |
9479 | end wpo; | |
9480 | ||
996ae0b0 | 9481 | end Sem_Ch4; |