]>
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 | -- -- | |
211e7410 | 9 | -- Copyright (C) 1992-2017, 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; | |
996ae0b0 | 33 | with Itypes; use Itypes; |
d935a36e | 34 | with Lib; use Lib; |
996ae0b0 RK |
35 | with Lib.Xref; use Lib.Xref; |
36 | with Namet; use Namet; | |
d469eabe | 37 | with Namet.Sp; use Namet.Sp; |
996ae0b0 RK |
38 | with Nlists; use Nlists; |
39 | with Nmake; use Nmake; | |
40 | with Opt; use Opt; | |
41 | with Output; use Output; | |
42 | with Restrict; use Restrict; | |
6e937c1c | 43 | with Rident; use Rident; |
996ae0b0 | 44 | with Sem; use Sem; |
a4100e55 | 45 | with Sem_Aux; use Sem_Aux; |
19d846a0 | 46 | with Sem_Case; use Sem_Case; |
996ae0b0 RK |
47 | with Sem_Cat; use Sem_Cat; |
48 | with Sem_Ch3; use Sem_Ch3; | |
d469eabe | 49 | with Sem_Ch6; use Sem_Ch6; |
996ae0b0 | 50 | with Sem_Ch8; use Sem_Ch8; |
dec6faf1 | 51 | with Sem_Dim; use Sem_Dim; |
b67a385c | 52 | with Sem_Disp; use Sem_Disp; |
996ae0b0 RK |
53 | with Sem_Dist; use Sem_Dist; |
54 | with Sem_Eval; use Sem_Eval; | |
55 | with Sem_Res; use Sem_Res; | |
996ae0b0 | 56 | with Sem_Type; use Sem_Type; |
19d846a0 RD |
57 | with Sem_Util; use Sem_Util; |
58 | with Sem_Warn; use Sem_Warn; | |
996ae0b0 RK |
59 | with Stand; use Stand; |
60 | with Sinfo; use Sinfo; | |
61 | with Snames; use Snames; | |
62 | with Tbuild; use Tbuild; | |
b727a82b | 63 | with Uintp; use Uintp; |
996ae0b0 | 64 | |
996ae0b0 RK |
65 | package body Sem_Ch4 is |
66 | ||
22e89283 AC |
67 | -- Tables which speed up the identification of dangerous calls to Ada 2012 |
68 | -- functions with writable actuals (AI05-0144). | |
69 | ||
70 | -- The following table enumerates the Ada constructs which may evaluate in | |
71 | -- arbitrary order. It does not cover all the language constructs which can | |
72 | -- be evaluated in arbitrary order but the subset needed for AI05-0144. | |
73 | ||
74 | Has_Arbitrary_Evaluation_Order : constant array (Node_Kind) of Boolean := | |
75 | (N_Aggregate => True, | |
76 | N_Assignment_Statement => True, | |
77 | N_Entry_Call_Statement => True, | |
78 | N_Extension_Aggregate => True, | |
79 | N_Full_Type_Declaration => True, | |
80 | N_Indexed_Component => True, | |
81 | N_Object_Declaration => True, | |
82 | N_Pragma => True, | |
83 | N_Range => True, | |
84 | N_Slice => True, | |
213999c2 AC |
85 | N_Array_Type_Definition => True, |
86 | N_Membership_Test => True, | |
87 | N_Binary_Op => True, | |
88 | N_Subprogram_Call => True, | |
22e89283 AC |
89 | others => False); |
90 | ||
91 | -- The following table enumerates the nodes on which we stop climbing when | |
92 | -- locating the outermost Ada construct that can be evaluated in arbitrary | |
93 | -- order. | |
94 | ||
95 | Stop_Subtree_Climbing : constant array (Node_Kind) of Boolean := | |
96 | (N_Aggregate => True, | |
97 | N_Assignment_Statement => True, | |
98 | N_Entry_Call_Statement => True, | |
99 | N_Extended_Return_Statement => True, | |
100 | N_Extension_Aggregate => True, | |
101 | N_Full_Type_Declaration => True, | |
102 | N_Object_Declaration => True, | |
103 | N_Object_Renaming_Declaration => True, | |
104 | N_Package_Specification => True, | |
105 | N_Pragma => True, | |
106 | N_Procedure_Call_Statement => True, | |
107 | N_Simple_Return_Statement => True, | |
213999c2 | 108 | N_Has_Condition => True, |
22e89283 AC |
109 | others => False); |
110 | ||
996ae0b0 RK |
111 | ----------------------- |
112 | -- Local Subprograms -- | |
113 | ----------------------- | |
114 | ||
fe39cf20 BD |
115 | procedure Analyze_Concatenation_Rest (N : Node_Id); |
116 | -- Does the "rest" of the work of Analyze_Concatenation, after the left | |
117 | -- operand has been analyzed. See Analyze_Concatenation for details. | |
118 | ||
996ae0b0 | 119 | procedure Analyze_Expression (N : Node_Id); |
80211802 AC |
120 | -- For expressions that are not names, this is just a call to analyze. If |
121 | -- the expression is a name, it may be a call to a parameterless function, | |
122 | -- and if so must be converted into an explicit call node and analyzed as | |
123 | -- such. This deproceduring must be done during the first pass of overload | |
124 | -- resolution, because otherwise a procedure call with overloaded actuals | |
125 | -- may fail to resolve. | |
996ae0b0 RK |
126 | |
127 | procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id); | |
80211802 AC |
128 | -- Analyze a call of the form "+"(x, y), etc. The prefix of the call is an |
129 | -- operator name or an expanded name whose selector is an operator name, | |
130 | -- and one possible interpretation is as a predefined operator. | |
996ae0b0 RK |
131 | |
132 | procedure Analyze_Overloaded_Selected_Component (N : Node_Id); | |
133 | -- If the prefix of a selected_component is overloaded, the proper | |
134 | -- interpretation that yields a record type with the proper selector | |
135 | -- name must be selected. | |
136 | ||
137 | procedure Analyze_User_Defined_Binary_Op (N : Node_Id; Op_Id : Entity_Id); | |
138 | -- Procedure to analyze a user defined binary operator, which is resolved | |
139 | -- like a function, but instead of a list of actuals it is presented | |
140 | -- with the left and right operands of an operator node. | |
141 | ||
142 | procedure Analyze_User_Defined_Unary_Op (N : Node_Id; Op_Id : Entity_Id); | |
143 | -- Procedure to analyze a user defined unary operator, which is resolved | |
144 | -- like a function, but instead of a list of actuals, it is presented with | |
145 | -- the operand of the operator node. | |
146 | ||
147 | procedure Ambiguous_Operands (N : Node_Id); | |
0877856b | 148 | -- For equality, membership, and comparison operators with overloaded |
996ae0b0 RK |
149 | -- arguments, list possible interpretations. |
150 | ||
996ae0b0 | 151 | procedure Analyze_One_Call |
ec6078e3 ES |
152 | (N : Node_Id; |
153 | Nam : Entity_Id; | |
154 | Report : Boolean; | |
155 | Success : out Boolean; | |
156 | Skip_First : Boolean := False); | |
996ae0b0 RK |
157 | -- Check one interpretation of an overloaded subprogram name for |
158 | -- compatibility with the types of the actuals in a call. If there is a | |
159 | -- single interpretation which does not match, post error if Report is | |
160 | -- set to True. | |
161 | -- | |
162 | -- Nam is the entity that provides the formals against which the actuals | |
163 | -- are checked. Nam is either the name of a subprogram, or the internal | |
164 | -- subprogram type constructed for an access_to_subprogram. If the actuals | |
165 | -- are compatible with Nam, then Nam is added to the list of candidate | |
166 | -- interpretations for N, and Success is set to True. | |
ec6078e3 ES |
167 | -- |
168 | -- The flag Skip_First is used when analyzing a call that was rewritten | |
169 | -- from object notation. In this case the first actual may have to receive | |
170 | -- an explicit dereference, depending on the first formal of the operation | |
171 | -- being called. The caller will have verified that the object is legal | |
172 | -- for the call. If the remaining parameters match, the first parameter | |
173 | -- will rewritten as a dereference if needed, prior to completing analysis. | |
996ae0b0 RK |
174 | |
175 | procedure Check_Misspelled_Selector | |
176 | (Prefix : Entity_Id; | |
177 | Sel : Node_Id); | |
80211802 | 178 | -- Give possible misspelling message if Sel seems likely to be a mis- |
8dbf3473 AC |
179 | -- spelling of one of the selectors of the Prefix. This is called by |
180 | -- Analyze_Selected_Component after producing an invalid selector error | |
181 | -- message. | |
996ae0b0 RK |
182 | |
183 | function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean; | |
f3d57416 | 184 | -- Verify that type T is declared in scope S. Used to find interpretations |
996ae0b0 RK |
185 | -- for operators given by expanded names. This is abstracted as a separate |
186 | -- function to handle extensions to System, where S is System, but T is | |
187 | -- declared in the extension. | |
188 | ||
189 | procedure Find_Arithmetic_Types | |
190 | (L, R : Node_Id; | |
191 | Op_Id : Entity_Id; | |
192 | N : Node_Id); | |
80211802 AC |
193 | -- L and R are the operands of an arithmetic operator. Find consistent |
194 | -- pairs of interpretations for L and R that have a numeric type consistent | |
195 | -- with the semantics of the operator. | |
996ae0b0 RK |
196 | |
197 | procedure Find_Comparison_Types | |
198 | (L, R : Node_Id; | |
199 | Op_Id : Entity_Id; | |
200 | N : Node_Id); | |
80211802 AC |
201 | -- L and R are operands of a comparison operator. Find consistent pairs of |
202 | -- interpretations for L and R. | |
996ae0b0 RK |
203 | |
204 | procedure Find_Concatenation_Types | |
205 | (L, R : Node_Id; | |
206 | Op_Id : Entity_Id; | |
207 | N : Node_Id); | |
6e73e3ab | 208 | -- For the four varieties of concatenation |
996ae0b0 RK |
209 | |
210 | procedure Find_Equality_Types | |
211 | (L, R : Node_Id; | |
212 | Op_Id : Entity_Id; | |
213 | N : Node_Id); | |
6e73e3ab | 214 | -- Ditto for equality operators |
996ae0b0 RK |
215 | |
216 | procedure Find_Boolean_Types | |
217 | (L, R : Node_Id; | |
218 | Op_Id : Entity_Id; | |
219 | N : Node_Id); | |
6e73e3ab | 220 | -- Ditto for binary logical operations |
996ae0b0 RK |
221 | |
222 | procedure Find_Negation_Types | |
223 | (R : Node_Id; | |
224 | Op_Id : Entity_Id; | |
225 | N : Node_Id); | |
6e73e3ab | 226 | -- Find consistent interpretation for operand of negation operator |
996ae0b0 RK |
227 | |
228 | procedure Find_Non_Universal_Interpretations | |
229 | (N : Node_Id; | |
230 | R : Node_Id; | |
231 | Op_Id : Entity_Id; | |
232 | T1 : Entity_Id); | |
5dc203d2 AC |
233 | -- For equality and comparison operators, the result is always boolean, and |
234 | -- the legality of the operation is determined from the visibility of the | |
235 | -- operand types. If one of the operands has a universal interpretation, | |
236 | -- the legality check uses some compatible non-universal interpretation of | |
237 | -- the other operand. N can be an operator node, or a function call whose | |
238 | -- name is an operator designator. Any_Access, which is the initial type of | |
239 | -- the literal NULL, is a universal type for the purpose of this routine. | |
996ae0b0 | 240 | |
d469eabe | 241 | function Find_Primitive_Operation (N : Node_Id) return Boolean; |
5dc203d2 AC |
242 | -- Find candidate interpretations for the name Obj.Proc when it appears in |
243 | -- a subprogram renaming declaration. | |
d469eabe | 244 | |
996ae0b0 RK |
245 | procedure Find_Unary_Types |
246 | (R : Node_Id; | |
247 | Op_Id : Entity_Id; | |
248 | N : Node_Id); | |
6e73e3ab | 249 | -- Unary arithmetic types: plus, minus, abs |
996ae0b0 RK |
250 | |
251 | procedure Check_Arithmetic_Pair | |
252 | (T1, T2 : Entity_Id; | |
253 | Op_Id : Entity_Id; | |
254 | N : Node_Id); | |
84dad556 AC |
255 | -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid types |
256 | -- for left and right operand. Determine whether they constitute a valid | |
257 | -- pair for the given operator, and record the corresponding interpretation | |
258 | -- of the operator node. The node N may be an operator node (the usual | |
259 | -- case) or a function call whose prefix is an operator designator. In | |
260 | -- both cases Op_Id is the operator name itself. | |
996ae0b0 RK |
261 | |
262 | procedure Diagnose_Call (N : Node_Id; Nam : Node_Id); | |
263 | -- Give detailed information on overloaded call where none of the | |
264 | -- interpretations match. N is the call node, Nam the designator for | |
265 | -- the overloaded entity being called. | |
266 | ||
267 | function Junk_Operand (N : Node_Id) return Boolean; | |
268 | -- Test for an operand that is an inappropriate entity (e.g. a package | |
269 | -- name or a label). If so, issue an error message and return True. If | |
270 | -- the operand is not an inappropriate entity kind, return False. | |
271 | ||
272 | procedure Operator_Check (N : Node_Id); | |
da709d08 AC |
273 | -- Verify that an operator has received some valid interpretation. If none |
274 | -- was found, determine whether a use clause would make the operation | |
275 | -- legal. The variable Candidate_Type (defined in Sem_Type) is set for | |
276 | -- every type compatible with the operator, even if the operator for the | |
277 | -- type is not directly visible. The routine uses this type to emit a more | |
278 | -- informative message. | |
996ae0b0 | 279 | |
d469eabe | 280 | function Process_Implicit_Dereference_Prefix |
da709d08 | 281 | (E : Entity_Id; |
d469eabe | 282 | P : Node_Id) return Entity_Id; |
da709d08 | 283 | -- Called when P is the prefix of an implicit dereference, denoting an |
d469eabe HK |
284 | -- object E. The function returns the designated type of the prefix, taking |
285 | -- into account that the designated type of an anonymous access type may be | |
286 | -- a limited view, when the non-limited view is visible. | |
84dad556 | 287 | -- |
d469eabe HK |
288 | -- If in semantics only mode (-gnatc or generic), the function also records |
289 | -- that the prefix is a reference to E, if any. Normally, such a reference | |
290 | -- is generated only when the implicit dereference is expanded into an | |
291 | -- explicit one, but for consistency we must generate the reference when | |
292 | -- expansion is disabled as well. | |
6e73e3ab | 293 | |
30c20106 AC |
294 | procedure Remove_Abstract_Operations (N : Node_Id); |
295 | -- Ada 2005: implementation of AI-310. An abstract non-dispatching | |
296 | -- operation is not a candidate interpretation. | |
297 | ||
d50f4827 AC |
298 | function Try_Container_Indexing |
299 | (N : Node_Id; | |
300 | Prefix : Node_Id; | |
50878404 | 301 | Exprs : List_Id) return Boolean; |
d50f4827 AC |
302 | -- AI05-0139: Generalized indexing to support iterators over containers |
303 | ||
996ae0b0 | 304 | function Try_Indexed_Call |
aab883ec ES |
305 | (N : Node_Id; |
306 | Nam : Entity_Id; | |
307 | Typ : Entity_Id; | |
308 | Skip_First : Boolean) return Boolean; | |
309 | -- If a function has defaults for all its actuals, a call to it may in fact | |
310 | -- be an indexing on the result of the call. Try_Indexed_Call attempts the | |
311 | -- interpretation as an indexing, prior to analysis as a call. If both are | |
312 | -- possible, the node is overloaded with both interpretations (same symbol | |
313 | -- but two different types). If the call is written in prefix form, the | |
314 | -- prefix becomes the first parameter in the call, and only the remaining | |
315 | -- actuals must be checked for the presence of defaults. | |
996ae0b0 RK |
316 | |
317 | function Try_Indirect_Call | |
91b1417d AC |
318 | (N : Node_Id; |
319 | Nam : Entity_Id; | |
320 | Typ : Entity_Id) return Boolean; | |
aab883ec ES |
321 | -- Similarly, a function F that needs no actuals can return an access to a |
322 | -- subprogram, and the call F (X) interpreted as F.all (X). In this case | |
323 | -- the call may be overloaded with both interpretations. | |
996ae0b0 | 324 | |
b4592168 GD |
325 | procedure wpo (T : Entity_Id); |
326 | pragma Warnings (Off, wpo); | |
327 | -- Used for debugging: obtain list of primitive operations even if | |
328 | -- type is not frozen and dispatch table is not built yet. | |
329 | ||
996ae0b0 RK |
330 | ------------------------ |
331 | -- Ambiguous_Operands -- | |
332 | ------------------------ | |
333 | ||
334 | procedure Ambiguous_Operands (N : Node_Id) is | |
fbf5a39b | 335 | procedure List_Operand_Interps (Opnd : Node_Id); |
996ae0b0 | 336 | |
4c46b835 AC |
337 | -------------------------- |
338 | -- List_Operand_Interps -- | |
339 | -------------------------- | |
340 | ||
fbf5a39b | 341 | procedure List_Operand_Interps (Opnd : Node_Id) is |
996ae0b0 | 342 | Nam : Node_Id; |
85be939e | 343 | pragma Warnings (Off, Nam); |
996ae0b0 RK |
344 | Err : Node_Id := N; |
345 | ||
346 | begin | |
347 | if Is_Overloaded (Opnd) then | |
348 | if Nkind (Opnd) in N_Op then | |
349 | Nam := Opnd; | |
84dad556 | 350 | |
996ae0b0 RK |
351 | elsif Nkind (Opnd) = N_Function_Call then |
352 | Nam := Name (Opnd); | |
84dad556 | 353 | |
44a10091 AC |
354 | elsif Ada_Version >= Ada_2012 then |
355 | declare | |
356 | It : Interp; | |
357 | I : Interp_Index; | |
358 | ||
359 | begin | |
360 | Get_First_Interp (Opnd, I, It); | |
361 | while Present (It.Nam) loop | |
362 | if Has_Implicit_Dereference (It.Typ) then | |
363 | Error_Msg_N | |
364 | ("can be interpreted as implicit dereference", Opnd); | |
365 | return; | |
366 | end if; | |
367 | ||
368 | Get_Next_Interp (I, It); | |
369 | end loop; | |
370 | end; | |
371 | ||
996ae0b0 RK |
372 | return; |
373 | end if; | |
374 | ||
375 | else | |
376 | return; | |
377 | end if; | |
378 | ||
379 | if Opnd = Left_Opnd (N) then | |
84dad556 AC |
380 | Error_Msg_N |
381 | ("\left operand has the following interpretations", N); | |
996ae0b0 | 382 | else |
ed2233dc | 383 | Error_Msg_N |
996ae0b0 RK |
384 | ("\right operand has the following interpretations", N); |
385 | Err := Opnd; | |
386 | end if; | |
387 | ||
fbf5a39b AC |
388 | List_Interps (Nam, Err); |
389 | end List_Operand_Interps; | |
996ae0b0 | 390 | |
4c46b835 AC |
391 | -- Start of processing for Ambiguous_Operands |
392 | ||
996ae0b0 | 393 | begin |
b67a385c | 394 | if Nkind (N) in N_Membership_Test then |
ed2233dc | 395 | Error_Msg_N ("ambiguous operands for membership", N); |
996ae0b0 | 396 | |
d469eabe | 397 | elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then |
ed2233dc | 398 | Error_Msg_N ("ambiguous operands for equality", N); |
996ae0b0 RK |
399 | |
400 | else | |
ed2233dc | 401 | Error_Msg_N ("ambiguous operands for comparison", N); |
996ae0b0 RK |
402 | end if; |
403 | ||
404 | if All_Errors_Mode then | |
fbf5a39b AC |
405 | List_Operand_Interps (Left_Opnd (N)); |
406 | List_Operand_Interps (Right_Opnd (N)); | |
996ae0b0 | 407 | else |
555360a5 | 408 | Error_Msg_N ("\use -gnatf switch for details", N); |
996ae0b0 RK |
409 | end if; |
410 | end Ambiguous_Operands; | |
411 | ||
412 | ----------------------- | |
413 | -- Analyze_Aggregate -- | |
414 | ----------------------- | |
415 | ||
416 | -- Most of the analysis of Aggregates requires that the type be known, | |
417 | -- and is therefore put off until resolution. | |
418 | ||
419 | procedure Analyze_Aggregate (N : Node_Id) is | |
420 | begin | |
421 | if No (Etype (N)) then | |
422 | Set_Etype (N, Any_Composite); | |
423 | end if; | |
424 | end Analyze_Aggregate; | |
425 | ||
426 | ----------------------- | |
427 | -- Analyze_Allocator -- | |
428 | ----------------------- | |
429 | ||
430 | procedure Analyze_Allocator (N : Node_Id) is | |
431 | Loc : constant Source_Ptr := Sloc (N); | |
07fc65c4 | 432 | Sav_Errs : constant Nat := Serious_Errors_Detected; |
b67a385c | 433 | E : Node_Id := Expression (N); |
996ae0b0 RK |
434 | Acc_Type : Entity_Id; |
435 | Type_Id : Entity_Id; | |
87003b28 RD |
436 | P : Node_Id; |
437 | C : Node_Id; | |
b3b26ace | 438 | Onode : Node_Id; |
996ae0b0 RK |
439 | |
440 | begin | |
ce5ba43a | 441 | Check_SPARK_05_Restriction ("allocator is not allowed", N); |
1d801f21 | 442 | |
87003b28 RD |
443 | -- Deal with allocator restrictions |
444 | ||
50cff367 | 445 | -- In accordance with H.4(7), the No_Allocators restriction only applies |
87003b28 | 446 | -- to user-written allocators. The same consideration applies to the |
d8941160 | 447 | -- No_Standard_Allocators_Before_Elaboration restriction. |
50cff367 GD |
448 | |
449 | if Comes_From_Source (N) then | |
450 | Check_Restriction (No_Allocators, N); | |
87003b28 | 451 | |
57f4c288 | 452 | -- Processing for No_Standard_Allocators_After_Elaboration, loop to |
489c6e19 | 453 | -- look at enclosing context, checking task/main subprogram case. |
87003b28 RD |
454 | |
455 | C := N; | |
456 | P := Parent (C); | |
457 | while Present (P) loop | |
458 | ||
b3b26ace AC |
459 | -- For the task case we need a handled sequence of statements, |
460 | -- where the occurrence of the allocator is within the statements | |
461 | -- and the parent is a task body | |
87003b28 RD |
462 | |
463 | if Nkind (P) = N_Handled_Sequence_Of_Statements | |
464 | and then Is_List_Member (C) | |
465 | and then List_Containing (C) = Statements (P) | |
466 | then | |
b3b26ace AC |
467 | Onode := Original_Node (Parent (P)); |
468 | ||
87003b28 | 469 | -- Check for allocator within task body, this is a definite |
d8941160 RD |
470 | -- violation of No_Allocators_After_Elaboration we can detect |
471 | -- at compile time. | |
87003b28 | 472 | |
b3b26ace | 473 | if Nkind (Onode) = N_Task_Body then |
57f4c288 ES |
474 | Check_Restriction |
475 | (No_Standard_Allocators_After_Elaboration, N); | |
87003b28 RD |
476 | exit; |
477 | end if; | |
b3b26ace | 478 | end if; |
87003b28 | 479 | |
b3b26ace AC |
480 | -- The other case is appearance in a subprogram body. This is |
481 | -- a violation if this is a library level subprogram with no | |
482 | -- parameters. Note that this is now a static error even if the | |
483 | -- subprogram is not the main program (this is a change, in an | |
484 | -- earlier version only the main program was affected, and the | |
485 | -- check had to be done in the binder. | |
87003b28 | 486 | |
b3b26ace AC |
487 | if Nkind (P) = N_Subprogram_Body |
488 | and then Nkind (Parent (P)) = N_Compilation_Unit | |
489 | and then No (Parameter_Specifications (Specification (P))) | |
490 | then | |
491 | Check_Restriction | |
492 | (No_Standard_Allocators_After_Elaboration, N); | |
87003b28 RD |
493 | end if; |
494 | ||
495 | C := P; | |
496 | P := Parent (C); | |
497 | end loop; | |
50cff367 | 498 | end if; |
996ae0b0 | 499 | |
df170605 AC |
500 | -- Ada 2012 (AI05-0111-3): Analyze the subpool_specification, if |
501 | -- any. The expected type for the name is any type. A non-overloading | |
502 | -- rule then requires it to be of a type descended from | |
f0f88eb6 RD |
503 | -- System.Storage_Pools.Subpools.Subpool_Handle. |
504 | ||
505 | -- This isn't exactly what the AI says, but it seems to be the right | |
506 | -- rule. The AI should be fixed.??? | |
df170605 AC |
507 | |
508 | declare | |
509 | Subpool : constant Node_Id := Subpool_Handle_Name (N); | |
f0f88eb6 | 510 | |
df170605 AC |
511 | begin |
512 | if Present (Subpool) then | |
513 | Analyze (Subpool); | |
f0f88eb6 | 514 | |
df170605 AC |
515 | if Is_Overloaded (Subpool) then |
516 | Error_Msg_N ("ambiguous subpool handle", Subpool); | |
517 | end if; | |
518 | ||
f0f88eb6 | 519 | -- Check that Etype (Subpool) is descended from Subpool_Handle |
df170605 AC |
520 | |
521 | Resolve (Subpool); | |
522 | end if; | |
523 | end; | |
524 | ||
525 | -- Analyze the qualified expression or subtype indication | |
87003b28 | 526 | |
996ae0b0 RK |
527 | if Nkind (E) = N_Qualified_Expression then |
528 | Acc_Type := Create_Itype (E_Allocator_Type, N); | |
529 | Set_Etype (Acc_Type, Acc_Type); | |
996ae0b0 | 530 | Find_Type (Subtype_Mark (E)); |
45c8b94b ES |
531 | |
532 | -- Analyze the qualified expression, and apply the name resolution | |
f0f88eb6 | 533 | -- rule given in 4.7(3). |
45c8b94b ES |
534 | |
535 | Analyze (E); | |
536 | Type_Id := Etype (E); | |
996ae0b0 RK |
537 | Set_Directly_Designated_Type (Acc_Type, Type_Id); |
538 | ||
996ae0b0 RK |
539 | -- A qualified expression requires an exact match of the type, |
540 | -- class-wide matching is not allowed. | |
541 | ||
45c8b94b ES |
542 | -- if Is_Class_Wide_Type (Type_Id) |
543 | -- and then Base_Type | |
544 | -- (Etype (Expression (E))) /= Base_Type (Type_Id) | |
545 | -- then | |
546 | -- Wrong_Type (Expression (E), Type_Id); | |
547 | -- end if; | |
996ae0b0 | 548 | |
996ae0b0 | 549 | -- We don't analyze the qualified expression itself because it's |
f3691f46 ES |
550 | -- part of the allocator. It is fully analyzed and resolved when |
551 | -- the allocator is resolved with the context type. | |
996ae0b0 RK |
552 | |
553 | Set_Etype (E, Type_Id); | |
554 | ||
aab883ec | 555 | -- Case where allocator has a subtype indication |
4c46b835 | 556 | |
996ae0b0 RK |
557 | else |
558 | declare | |
758c442c GD |
559 | Def_Id : Entity_Id; |
560 | Base_Typ : Entity_Id; | |
996ae0b0 RK |
561 | |
562 | begin | |
563 | -- If the allocator includes a N_Subtype_Indication then a | |
564 | -- constraint is present, otherwise the node is a subtype mark. | |
565 | -- Introduce an explicit subtype declaration into the tree | |
566 | -- defining some anonymous subtype and rewrite the allocator to | |
567 | -- use this subtype rather than the subtype indication. | |
568 | ||
569 | -- It is important to introduce the explicit subtype declaration | |
570 | -- so that the bounds of the subtype indication are attached to | |
571 | -- the tree in case the allocator is inside a generic unit. | |
572 | ||
3ba1a9eb AC |
573 | -- Finally, if there is no subtype indication and the type is |
574 | -- a tagged unconstrained type with discriminants, the designated | |
575 | -- object is constrained by their default values, and it is | |
576 | -- simplest to introduce an explicit constraint now. In some cases | |
577 | -- this is done during expansion, but freeze actions are certain | |
578 | -- to be emitted in the proper order if constraint is explicit. | |
579 | ||
580 | if Is_Entity_Name (E) and then Expander_Active then | |
581 | Find_Type (E); | |
582 | Type_Id := Entity (E); | |
583 | ||
584 | if Is_Tagged_Type (Type_Id) | |
585 | and then Has_Discriminants (Type_Id) | |
586 | and then not Is_Constrained (Type_Id) | |
3702225c AC |
587 | and then |
588 | Present | |
589 | (Discriminant_Default_Value | |
590 | (First_Discriminant (Type_Id))) | |
3ba1a9eb AC |
591 | then |
592 | declare | |
3702225c | 593 | Constr : constant List_Id := New_List; |
3ba1a9eb AC |
594 | Loc : constant Source_Ptr := Sloc (E); |
595 | Discr : Entity_Id := First_Discriminant (Type_Id); | |
3ba1a9eb AC |
596 | |
597 | begin | |
598 | if Present (Discriminant_Default_Value (Discr)) then | |
599 | while Present (Discr) loop | |
600 | Append (Discriminant_Default_Value (Discr), Constr); | |
601 | Next_Discriminant (Discr); | |
602 | end loop; | |
603 | ||
3702225c AC |
604 | Rewrite (E, |
605 | Make_Subtype_Indication (Loc, | |
606 | Subtype_Mark => New_Occurrence_Of (Type_Id, Loc), | |
607 | Constraint => | |
608 | Make_Index_Or_Discriminant_Constraint (Loc, | |
609 | Constraints => Constr))); | |
3ba1a9eb AC |
610 | end if; |
611 | end; | |
612 | end if; | |
613 | end if; | |
614 | ||
996ae0b0 RK |
615 | if Nkind (E) = N_Subtype_Indication then |
616 | ||
617 | -- A constraint is only allowed for a composite type in Ada | |
618 | -- 95. In Ada 83, a constraint is also allowed for an | |
619 | -- access-to-composite type, but the constraint is ignored. | |
620 | ||
621 | Find_Type (Subtype_Mark (E)); | |
758c442c | 622 | Base_Typ := Entity (Subtype_Mark (E)); |
996ae0b0 | 623 | |
758c442c | 624 | if Is_Elementary_Type (Base_Typ) then |
0ab80019 | 625 | if not (Ada_Version = Ada_83 |
758c442c | 626 | and then Is_Access_Type (Base_Typ)) |
996ae0b0 RK |
627 | then |
628 | Error_Msg_N ("constraint not allowed here", E); | |
629 | ||
24657705 | 630 | if Nkind (Constraint (E)) = |
3702225c | 631 | N_Index_Or_Discriminant_Constraint |
996ae0b0 | 632 | then |
4e7a4f6e | 633 | Error_Msg_N -- CODEFIX |
996ae0b0 RK |
634 | ("\if qualified expression was meant, " & |
635 | "use apostrophe", Constraint (E)); | |
636 | end if; | |
637 | end if; | |
638 | ||
639 | -- Get rid of the bogus constraint: | |
640 | ||
641 | Rewrite (E, New_Copy_Tree (Subtype_Mark (E))); | |
642 | Analyze_Allocator (N); | |
643 | return; | |
644 | end if; | |
645 | ||
646 | if Expander_Active then | |
092ef350 | 647 | Def_Id := Make_Temporary (Loc, 'S'); |
996ae0b0 RK |
648 | |
649 | Insert_Action (E, | |
650 | Make_Subtype_Declaration (Loc, | |
651 | Defining_Identifier => Def_Id, | |
652 | Subtype_Indication => Relocate_Node (E))); | |
653 | ||
07fc65c4 | 654 | if Sav_Errs /= Serious_Errors_Detected |
d469eabe HK |
655 | and then Nkind (Constraint (E)) = |
656 | N_Index_Or_Discriminant_Constraint | |
996ae0b0 | 657 | then |
4e7a4f6e | 658 | Error_Msg_N -- CODEFIX |
a90bd866 RD |
659 | ("if qualified expression was meant, " |
660 | & "use apostrophe!", Constraint (E)); | |
996ae0b0 RK |
661 | end if; |
662 | ||
663 | E := New_Occurrence_Of (Def_Id, Loc); | |
664 | Rewrite (Expression (N), E); | |
665 | end if; | |
666 | end if; | |
667 | ||
668 | Type_Id := Process_Subtype (E, N); | |
669 | Acc_Type := Create_Itype (E_Allocator_Type, N); | |
84dad556 | 670 | Set_Etype (Acc_Type, Acc_Type); |
996ae0b0 RK |
671 | Set_Directly_Designated_Type (Acc_Type, Type_Id); |
672 | Check_Fully_Declared (Type_Id, N); | |
673 | ||
1baa4d2d | 674 | -- Ada 2005 (AI-231): If the designated type is itself an access |
16b05213 | 675 | -- type that excludes null, its default initialization will |
75ad5042 ES |
676 | -- be a null object, and we can insert an unconditional raise |
677 | -- before the allocator. | |
2820d220 | 678 | |
bfae1846 | 679 | -- Ada 2012 (AI-104): A not null indication here is altogether |
518ade91 AC |
680 | -- illegal. |
681 | ||
2820d220 | 682 | if Can_Never_Be_Null (Type_Id) then |
75ad5042 ES |
683 | declare |
684 | Not_Null_Check : constant Node_Id := | |
685 | Make_Raise_Constraint_Error (Sloc (E), | |
686 | Reason => CE_Null_Not_Allowed); | |
40b93859 | 687 | |
75ad5042 | 688 | begin |
7b55fea6 | 689 | if Expander_Active then |
75ad5042 ES |
690 | Insert_Action (N, Not_Null_Check); |
691 | Analyze (Not_Null_Check); | |
40b93859 | 692 | |
685bc70f AC |
693 | elsif Warn_On_Ada_2012_Compatibility then |
694 | Error_Msg_N | |
695 | ("null value not allowed here in Ada 2012?y?", E); | |
75ad5042 ES |
696 | end if; |
697 | end; | |
2820d220 AC |
698 | end if; |
699 | ||
996ae0b0 RK |
700 | -- Check for missing initialization. Skip this check if we already |
701 | -- had errors on analyzing the allocator, since in that case these | |
24657705 | 702 | -- are probably cascaded errors. |
996ae0b0 | 703 | |
83496138 | 704 | if not Is_Definite_Subtype (Type_Id) |
07fc65c4 | 705 | and then Serious_Errors_Detected = Sav_Errs |
996ae0b0 | 706 | then |
a4956515 AC |
707 | -- The build-in-place machinery may produce an allocator when |
708 | -- the designated type is indefinite but the underlying type is | |
709 | -- not. In this case the unknown discriminants are meaningless | |
710 | -- and should not trigger error messages. Check the parent node | |
711 | -- because the allocator is marked as coming from source. | |
712 | ||
713 | if Present (Underlying_Type (Type_Id)) | |
83496138 | 714 | and then Is_Definite_Subtype (Underlying_Type (Type_Id)) |
a4956515 AC |
715 | and then not Comes_From_Source (Parent (N)) |
716 | then | |
717 | null; | |
718 | ||
d43584ca AC |
719 | -- An unusual case arises when the parent of a derived type is |
720 | -- a limited record extension with unknown discriminants, and | |
721 | -- its full view has no discriminants. | |
722 | -- | |
723 | -- A more general fix might be to create the proper underlying | |
724 | -- type for such a derived type, but it is a record type with | |
725 | -- no private attributes, so this required extending the | |
726 | -- meaning of this attribute. ??? | |
727 | ||
728 | elsif Ekind (Etype (Type_Id)) = E_Record_Type_With_Private | |
729 | and then Present (Underlying_Type (Etype (Type_Id))) | |
730 | and then | |
731 | not Has_Discriminants (Underlying_Type (Etype (Type_Id))) | |
732 | and then not Comes_From_Source (Parent (N)) | |
733 | then | |
734 | null; | |
735 | ||
a4956515 | 736 | elsif Is_Class_Wide_Type (Type_Id) then |
996ae0b0 RK |
737 | Error_Msg_N |
738 | ("initialization required in class-wide allocation", N); | |
a4956515 | 739 | |
996ae0b0 | 740 | else |
0791fbe9 | 741 | if Ada_Version < Ada_2005 |
24657705 HK |
742 | and then Is_Limited_Type (Type_Id) |
743 | then | |
744 | Error_Msg_N ("unconstrained allocation not allowed", N); | |
745 | ||
746 | if Is_Array_Type (Type_Id) then | |
747 | Error_Msg_N | |
748 | ("\constraint with array bounds required", N); | |
749 | ||
750 | elsif Has_Unknown_Discriminants (Type_Id) then | |
751 | null; | |
752 | ||
753 | else pragma Assert (Has_Discriminants (Type_Id)); | |
754 | Error_Msg_N | |
755 | ("\constraint with discriminant values required", N); | |
756 | end if; | |
757 | ||
758 | -- Limited Ada 2005 and general non-limited case | |
759 | ||
760 | else | |
761 | Error_Msg_N | |
84dad556 AC |
762 | ("uninitialized unconstrained allocation not " |
763 | & "allowed", N); | |
24657705 HK |
764 | |
765 | if Is_Array_Type (Type_Id) then | |
766 | Error_Msg_N | |
84dad556 AC |
767 | ("\qualified expression or constraint with " |
768 | & "array bounds required", N); | |
24657705 HK |
769 | |
770 | elsif Has_Unknown_Discriminants (Type_Id) then | |
771 | Error_Msg_N ("\qualified expression required", N); | |
772 | ||
773 | else pragma Assert (Has_Discriminants (Type_Id)); | |
774 | Error_Msg_N | |
84dad556 AC |
775 | ("\qualified expression or constraint with " |
776 | & "discriminant values required", N); | |
24657705 HK |
777 | end if; |
778 | end if; | |
996ae0b0 RK |
779 | end if; |
780 | end if; | |
781 | end; | |
782 | end if; | |
783 | ||
aab883ec | 784 | if Is_Abstract_Type (Type_Id) then |
996ae0b0 RK |
785 | Error_Msg_N ("cannot allocate abstract object", E); |
786 | end if; | |
787 | ||
788 | if Has_Task (Designated_Type (Acc_Type)) then | |
6e937c1c | 789 | Check_Restriction (No_Tasking, N); |
fbf5a39b | 790 | Check_Restriction (Max_Tasks, N); |
996ae0b0 | 791 | Check_Restriction (No_Task_Allocators, N); |
70b3b953 GD |
792 | end if; |
793 | ||
02bb0765 AC |
794 | -- Check restriction against dynamically allocated protected objects |
795 | ||
796 | if Has_Protected (Designated_Type (Acc_Type)) then | |
797 | Check_Restriction (No_Protected_Type_Allocators, N); | |
798 | end if; | |
799 | ||
646e2823 AC |
800 | -- AI05-0013-1: No_Nested_Finalization forbids allocators if the access |
801 | -- type is nested, and the designated type needs finalization. The rule | |
802 | -- is conservative in that class-wide types need finalization. | |
803 | ||
804 | if Needs_Finalization (Designated_Type (Acc_Type)) | |
805 | and then not Is_Library_Level_Entity (Acc_Type) | |
806 | then | |
807 | Check_Restriction (No_Nested_Finalization, N); | |
808 | end if; | |
809 | ||
70b3b953 GD |
810 | -- Check that an allocator of a nested access type doesn't create a |
811 | -- protected object when restriction No_Local_Protected_Objects applies. | |
70b3b953 | 812 | |
96e90ac1 | 813 | if Has_Protected (Designated_Type (Acc_Type)) |
70b3b953 GD |
814 | and then not Is_Library_Level_Entity (Acc_Type) |
815 | then | |
816 | Check_Restriction (No_Local_Protected_Objects, N); | |
996ae0b0 RK |
817 | end if; |
818 | ||
4969efdf AC |
819 | -- Likewise for No_Local_Timing_Events |
820 | ||
821 | if Has_Timing_Event (Designated_Type (Acc_Type)) | |
822 | and then not Is_Library_Level_Entity (Acc_Type) | |
823 | then | |
824 | Check_Restriction (No_Local_Timing_Events, N); | |
825 | end if; | |
826 | ||
ffe9aba8 AC |
827 | -- If the No_Streams restriction is set, check that the type of the |
828 | -- object is not, and does not contain, any subtype derived from | |
829 | -- Ada.Streams.Root_Stream_Type. Note that we guard the call to | |
830 | -- Has_Stream just for efficiency reasons. There is no point in | |
831 | -- spending time on a Has_Stream check if the restriction is not set. | |
832 | ||
7a963087 | 833 | if Restriction_Check_Required (No_Streams) then |
ffe9aba8 AC |
834 | if Has_Stream (Designated_Type (Acc_Type)) then |
835 | Check_Restriction (No_Streams, N); | |
836 | end if; | |
837 | end if; | |
838 | ||
996ae0b0 RK |
839 | Set_Etype (N, Acc_Type); |
840 | ||
841 | if not Is_Library_Level_Entity (Acc_Type) then | |
842 | Check_Restriction (No_Local_Allocators, N); | |
843 | end if; | |
2820d220 | 844 | |
07fc65c4 | 845 | if Serious_Errors_Detected > Sav_Errs then |
996ae0b0 RK |
846 | Set_Error_Posted (N); |
847 | Set_Etype (N, Any_Type); | |
848 | end if; | |
996ae0b0 RK |
849 | end Analyze_Allocator; |
850 | ||
851 | --------------------------- | |
852 | -- Analyze_Arithmetic_Op -- | |
853 | --------------------------- | |
854 | ||
855 | procedure Analyze_Arithmetic_Op (N : Node_Id) is | |
856 | L : constant Node_Id := Left_Opnd (N); | |
857 | R : constant Node_Id := Right_Opnd (N); | |
858 | Op_Id : Entity_Id; | |
859 | ||
860 | begin | |
861 | Candidate_Type := Empty; | |
862 | Analyze_Expression (L); | |
863 | Analyze_Expression (R); | |
864 | ||
d469eabe HK |
865 | -- If the entity is already set, the node is the instantiation of a |
866 | -- generic node with a non-local reference, or was manufactured by a | |
867 | -- call to Make_Op_xxx. In either case the entity is known to be valid, | |
868 | -- and we do not need to collect interpretations, instead we just get | |
869 | -- the single possible interpretation. | |
996ae0b0 RK |
870 | |
871 | Op_Id := Entity (N); | |
872 | ||
873 | if Present (Op_Id) then | |
874 | if Ekind (Op_Id) = E_Operator then | |
875 | ||
d469eabe | 876 | if Nkind_In (N, N_Op_Divide, N_Op_Mod, N_Op_Multiply, N_Op_Rem) |
996ae0b0 RK |
877 | and then Treat_Fixed_As_Integer (N) |
878 | then | |
879 | null; | |
880 | else | |
881 | Set_Etype (N, Any_Type); | |
882 | Find_Arithmetic_Types (L, R, Op_Id, N); | |
883 | end if; | |
884 | ||
885 | else | |
886 | Set_Etype (N, Any_Type); | |
887 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
888 | end if; | |
889 | ||
890 | -- Entity is not already set, so we do need to collect interpretations | |
891 | ||
892 | else | |
996ae0b0 RK |
893 | Set_Etype (N, Any_Type); |
894 | ||
84dad556 | 895 | Op_Id := Get_Name_Entity_Id (Chars (N)); |
996ae0b0 RK |
896 | while Present (Op_Id) loop |
897 | if Ekind (Op_Id) = E_Operator | |
898 | and then Present (Next_Entity (First_Entity (Op_Id))) | |
899 | then | |
900 | Find_Arithmetic_Types (L, R, Op_Id, N); | |
901 | ||
902 | -- The following may seem superfluous, because an operator cannot | |
903 | -- be generic, but this ignores the cleverness of the author of | |
904 | -- ACVC bc1013a. | |
905 | ||
906 | elsif Is_Overloadable (Op_Id) then | |
907 | Analyze_User_Defined_Binary_Op (N, Op_Id); | |
908 | end if; | |
909 | ||
910 | Op_Id := Homonym (Op_Id); | |
911 | end loop; | |
912 | end if; | |
913 | ||
914 | Operator_Check (N); | |
22e89283 | 915 | Check_Function_Writable_Actuals (N); |
996ae0b0 RK |
916 | end Analyze_Arithmetic_Op; |
917 | ||
918 | ------------------ | |
919 | -- Analyze_Call -- | |
920 | ------------------ | |
921 | ||
4c46b835 AC |
922 | -- Function, procedure, and entry calls are checked here. The Name in |
923 | -- the call may be overloaded. The actuals have been analyzed and may | |
924 | -- themselves be overloaded. On exit from this procedure, the node N | |
925 | -- may have zero, one or more interpretations. In the first case an | |
926 | -- error message is produced. In the last case, the node is flagged | |
927 | -- as overloaded and the interpretations are collected in All_Interp. | |
996ae0b0 RK |
928 | |
929 | -- If the name is an Access_To_Subprogram, it cannot be overloaded, but | |
930 | -- the type-checking is similar to that of other calls. | |
931 | ||
932 | procedure Analyze_Call (N : Node_Id) is | |
4f324de2 | 933 | Actuals : constant List_Id := Parameter_Associations (N); |
f4ef7b06 | 934 | Loc : constant Source_Ptr := Sloc (N); |
63319f58 | 935 | Nam : Node_Id; |
996ae0b0 RK |
936 | X : Interp_Index; |
937 | It : Interp; | |
938 | Nam_Ent : Entity_Id; | |
63319f58 RD |
939 | Success : Boolean := False; |
940 | ||
941 | Deref : Boolean := False; | |
1cb17b78 AC |
942 | -- Flag indicates whether an interpretation of the prefix is a |
943 | -- parameterless call that returns an access_to_subprogram. | |
996ae0b0 | 944 | |
23685ae6 AC |
945 | procedure Check_Mixed_Parameter_And_Named_Associations; |
946 | -- Check that parameter and named associations are not mixed. This is | |
947 | -- a restriction in SPARK mode. | |
948 | ||
288cbbbd JM |
949 | procedure Check_Writable_Actuals (N : Node_Id); |
950 | -- If the call has out or in-out parameters then mark its outermost | |
951 | -- enclosing construct as a node on which the writable actuals check | |
952 | -- must be performed. | |
953 | ||
996ae0b0 | 954 | function Name_Denotes_Function return Boolean; |
5ff22245 ES |
955 | -- If the type of the name is an access to subprogram, this may be the |
956 | -- type of a name, or the return type of the function being called. If | |
957 | -- the name is not an entity then it can denote a protected function. | |
958 | -- Until we distinguish Etype from Return_Type, we must use this routine | |
959 | -- to resolve the meaning of the name in the call. | |
960 | ||
961 | procedure No_Interpretation; | |
962 | -- Output error message when no valid interpretation exists | |
996ae0b0 | 963 | |
23685ae6 AC |
964 | -------------------------------------------------- |
965 | -- Check_Mixed_Parameter_And_Named_Associations -- | |
966 | -------------------------------------------------- | |
967 | ||
968 | procedure Check_Mixed_Parameter_And_Named_Associations is | |
969 | Actual : Node_Id; | |
970 | Named_Seen : Boolean; | |
f5afb270 | 971 | |
23685ae6 | 972 | begin |
23685ae6 | 973 | Named_Seen := False; |
f5afb270 AC |
974 | |
975 | Actual := First (Actuals); | |
23685ae6 AC |
976 | while Present (Actual) loop |
977 | case Nkind (Actual) is | |
978 | when N_Parameter_Association => | |
979 | if Named_Seen then | |
ce5ba43a | 980 | Check_SPARK_05_Restriction |
23685ae6 AC |
981 | ("named association cannot follow positional one", |
982 | Actual); | |
983 | exit; | |
984 | end if; | |
84dad556 | 985 | |
23685ae6 AC |
986 | when others => |
987 | Named_Seen := True; | |
988 | end case; | |
989 | ||
990 | Next (Actual); | |
991 | end loop; | |
992 | end Check_Mixed_Parameter_And_Named_Associations; | |
993 | ||
288cbbbd JM |
994 | ---------------------------- |
995 | -- Check_Writable_Actuals -- | |
996 | ---------------------------- | |
997 | ||
998 | -- The identification of conflicts in calls to functions with writable | |
551e1935 | 999 | -- actuals is performed in the analysis phase of the front end to ensure |
288cbbbd JM |
1000 | -- that it reports exactly the same errors compiling with and without |
1001 | -- expansion enabled. It is performed in two stages: | |
1002 | ||
551e1935 AC |
1003 | -- 1) When a call to a function with out-mode parameters is found, |
1004 | -- we climb to the outermost enclosing construct that can be | |
288cbbbd JM |
1005 | -- evaluated in arbitrary order and we mark it with the flag |
1006 | -- Check_Actuals. | |
1007 | ||
551e1935 AC |
1008 | -- 2) When the analysis of the marked node is complete, we traverse |
1009 | -- its decorated subtree searching for conflicts (see function | |
1010 | -- Sem_Util.Check_Function_Writable_Actuals). | |
288cbbbd | 1011 | |
551e1935 AC |
1012 | -- The unique exception to this general rule is for aggregates, since |
1013 | -- their analysis is performed by the front end in the resolution | |
1014 | -- phase. For aggregates we do not climb to their enclosing construct: | |
288cbbbd JM |
1015 | -- we restrict the analysis to the subexpressions initializing the |
1016 | -- aggregate components. | |
1017 | ||
1018 | -- This implies that the analysis of expressions containing aggregates | |
551e1935 | 1019 | -- is not complete, since there may be conflicts on writable actuals |
288cbbbd JM |
1020 | -- involving subexpressions of the enclosing logical or arithmetic |
1021 | -- expressions. However, we cannot wait and perform the analysis when | |
551e1935 | 1022 | -- the whole subtree is resolved, since the subtrees may be transformed, |
288cbbbd JM |
1023 | -- thus adding extra complexity and computation cost to identify and |
1024 | -- report exactly the same errors compiling with and without expansion | |
1025 | -- enabled. | |
1026 | ||
1027 | procedure Check_Writable_Actuals (N : Node_Id) is | |
288cbbbd JM |
1028 | begin |
1029 | if Comes_From_Source (N) | |
1030 | and then Present (Get_Subprogram_Entity (N)) | |
1031 | and then Has_Out_Or_In_Out_Parameter (Get_Subprogram_Entity (N)) | |
1032 | then | |
1033 | -- For procedures and entries there is no need to climb since | |
1034 | -- we only need to check if the actuals of this call invoke | |
1035 | -- functions whose out-mode parameters overlap. | |
1036 | ||
1037 | if Nkind (N) /= N_Function_Call then | |
1038 | Set_Check_Actuals (N); | |
1039 | ||
1040 | -- For calls to functions we climb to the outermost enclosing | |
1041 | -- construct where the out-mode actuals of this function may | |
1042 | -- introduce conflicts. | |
1043 | ||
1044 | else | |
1045 | declare | |
1046 | Outermost : Node_Id; | |
1047 | P : Node_Id := N; | |
1048 | ||
1049 | begin | |
1050 | while Present (P) loop | |
1051 | ||
551e1935 | 1052 | -- For object declarations we can climb to the node from |
288cbbbd JM |
1053 | -- its object definition branch or from its initializing |
1054 | -- expression. We prefer to mark the child node as the | |
1055 | -- outermost construct to avoid adding further complexity | |
551e1935 | 1056 | -- to the routine that will later take care of |
288cbbbd JM |
1057 | -- performing the writable actuals check. |
1058 | ||
22e89283 AC |
1059 | if Has_Arbitrary_Evaluation_Order (Nkind (P)) |
1060 | and then not Nkind_In (P, N_Assignment_Statement, | |
1061 | N_Object_Declaration) | |
288cbbbd JM |
1062 | then |
1063 | Outermost := P; | |
1064 | end if; | |
1065 | ||
1066 | -- Avoid climbing more than needed! | |
1067 | ||
22e89283 | 1068 | exit when Stop_Subtree_Climbing (Nkind (P)) |
288cbbbd JM |
1069 | or else (Nkind (P) = N_Range |
1070 | and then not | |
22e89283 | 1071 | Nkind_In (Parent (P), N_In, N_Not_In)); |
288cbbbd JM |
1072 | |
1073 | P := Parent (P); | |
1074 | end loop; | |
1075 | ||
1076 | Set_Check_Actuals (Outermost); | |
1077 | end; | |
1078 | end if; | |
1079 | end if; | |
1080 | end Check_Writable_Actuals; | |
1081 | ||
996ae0b0 RK |
1082 | --------------------------- |
1083 | -- Name_Denotes_Function -- | |
1084 | --------------------------- | |
1085 | ||
1086 | function Name_Denotes_Function return Boolean is | |
1087 | begin | |
1088 | if Is_Entity_Name (Nam) then | |
1089 | return Ekind (Entity (Nam)) = E_Function; | |
996ae0b0 RK |
1090 | elsif Nkind (Nam) = N_Selected_Component then |
1091 | return Ekind (Entity (Selector_Name (Nam))) = E_Function; | |
996ae0b0 RK |
1092 | else |
1093 | return False; | |
1094 | end if; | |
1095 | end Name_Denotes_Function; | |
1096 | ||
5ff22245 ES |
1097 | ----------------------- |
1098 | -- No_Interpretation -- | |
1099 | ----------------------- | |
1100 | ||
1101 | procedure No_Interpretation is | |
1102 | L : constant Boolean := Is_List_Member (N); | |
1103 | K : constant Node_Kind := Nkind (Parent (N)); | |
1104 | ||
1105 | begin | |
1106 | -- If the node is in a list whose parent is not an expression then it | |
1107 | -- must be an attempted procedure call. | |
1108 | ||
1109 | if L and then K not in N_Subexpr then | |
1110 | if Ekind (Entity (Nam)) = E_Generic_Procedure then | |
1111 | Error_Msg_NE | |
1112 | ("must instantiate generic procedure& before call", | |
1113 | Nam, Entity (Nam)); | |
1114 | else | |
84dad556 | 1115 | Error_Msg_N ("procedure or entry name expected", Nam); |
5ff22245 ES |
1116 | end if; |
1117 | ||
1118 | -- Check for tasking cases where only an entry call will do | |
1119 | ||
1120 | elsif not L | |
1121 | and then Nkind_In (K, N_Entry_Call_Alternative, | |
1122 | N_Triggering_Alternative) | |
1123 | then | |
1124 | Error_Msg_N ("entry name expected", Nam); | |
1125 | ||
1126 | -- Otherwise give general error message | |
1127 | ||
1128 | else | |
1129 | Error_Msg_N ("invalid prefix in call", Nam); | |
1130 | end if; | |
1131 | end No_Interpretation; | |
1132 | ||
996ae0b0 RK |
1133 | -- Start of processing for Analyze_Call |
1134 | ||
1135 | begin | |
6480338a | 1136 | if Restriction_Check_Required (SPARK_05) then |
23685ae6 AC |
1137 | Check_Mixed_Parameter_And_Named_Associations; |
1138 | end if; | |
1139 | ||
996ae0b0 RK |
1140 | -- Initialize the type of the result of the call to the error type, |
1141 | -- which will be reset if the type is successfully resolved. | |
1142 | ||
1143 | Set_Etype (N, Any_Type); | |
1144 | ||
63319f58 RD |
1145 | Nam := Name (N); |
1146 | ||
996ae0b0 RK |
1147 | if not Is_Overloaded (Nam) then |
1148 | ||
1149 | -- Only one interpretation to check | |
1150 | ||
1151 | if Ekind (Etype (Nam)) = E_Subprogram_Type then | |
1152 | Nam_Ent := Etype (Nam); | |
1153 | ||
758c442c GD |
1154 | -- If the prefix is an access_to_subprogram, this may be an indirect |
1155 | -- call. This is the case if the name in the call is not an entity | |
1156 | -- name, or if it is a function name in the context of a procedure | |
1157 | -- call. In this latter case, we have a call to a parameterless | |
1158 | -- function that returns a pointer_to_procedure which is the entity | |
5ff22245 ES |
1159 | -- being called. Finally, F (X) may be a call to a parameterless |
1160 | -- function that returns a pointer to a function with parameters. | |
80e59506 | 1161 | -- Note that if F returns an access-to-subprogram whose designated |
4bb9c7b9 AC |
1162 | -- type is an array, F (X) cannot be interpreted as an indirect call |
1163 | -- through the result of the call to F. | |
758c442c | 1164 | |
996ae0b0 RK |
1165 | elsif Is_Access_Type (Etype (Nam)) |
1166 | and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type | |
758c442c GD |
1167 | and then |
1168 | (not Name_Denotes_Function | |
b2834fbd AC |
1169 | or else Nkind (N) = N_Procedure_Call_Statement |
1170 | or else | |
1171 | (Nkind (Parent (N)) /= N_Explicit_Dereference | |
1172 | and then Is_Entity_Name (Nam) | |
1173 | and then No (First_Formal (Entity (Nam))) | |
1174 | and then not | |
1175 | Is_Array_Type (Etype (Designated_Type (Etype (Nam)))) | |
1176 | and then Present (Actuals))) | |
996ae0b0 RK |
1177 | then |
1178 | Nam_Ent := Designated_Type (Etype (Nam)); | |
1179 | Insert_Explicit_Dereference (Nam); | |
1180 | ||
1181 | -- Selected component case. Simple entry or protected operation, | |
1182 | -- where the entry name is given by the selector name. | |
1183 | ||
1184 | elsif Nkind (Nam) = N_Selected_Component then | |
1185 | Nam_Ent := Entity (Selector_Name (Nam)); | |
1186 | ||
bce79204 AC |
1187 | if not Ekind_In (Nam_Ent, E_Entry, |
1188 | E_Entry_Family, | |
1189 | E_Function, | |
1190 | E_Procedure) | |
996ae0b0 RK |
1191 | then |
1192 | Error_Msg_N ("name in call is not a callable entity", Nam); | |
1193 | Set_Etype (N, Any_Type); | |
1194 | return; | |
1195 | end if; | |
1196 | ||
1197 | -- If the name is an Indexed component, it can be a call to a member | |
1198 | -- of an entry family. The prefix must be a selected component whose | |
1199 | -- selector is the entry. Analyze_Procedure_Call normalizes several | |
1200 | -- kinds of call into this form. | |
1201 | ||
1202 | elsif Nkind (Nam) = N_Indexed_Component then | |
996ae0b0 RK |
1203 | if Nkind (Prefix (Nam)) = N_Selected_Component then |
1204 | Nam_Ent := Entity (Selector_Name (Prefix (Nam))); | |
996ae0b0 RK |
1205 | else |
1206 | Error_Msg_N ("name in call is not a callable entity", Nam); | |
1207 | Set_Etype (N, Any_Type); | |
1208 | return; | |
996ae0b0 RK |
1209 | end if; |
1210 | ||
1211 | elsif not Is_Entity_Name (Nam) then | |
1212 | Error_Msg_N ("name in call is not a callable entity", Nam); | |
1213 | Set_Etype (N, Any_Type); | |
1214 | return; | |
1215 | ||
1216 | else | |
1217 | Nam_Ent := Entity (Nam); | |
1218 | ||
5f50020a ES |
1219 | -- If not overloadable, this may be a generalized indexing |
1220 | -- operation with named associations. Rewrite again as an | |
1221 | -- indexed component and analyze as container indexing. | |
996ae0b0 RK |
1222 | |
1223 | if not Is_Overloadable (Nam_Ent) then | |
32bba3c9 AC |
1224 | if Present |
1225 | (Find_Value_Of_Aspect | |
1226 | (Etype (Nam_Ent), Aspect_Constant_Indexing)) | |
5f50020a ES |
1227 | then |
1228 | Replace (N, | |
1229 | Make_Indexed_Component (Sloc (N), | |
32bba3c9 | 1230 | Prefix => Nam, |
5f50020a ES |
1231 | Expressions => Parameter_Associations (N))); |
1232 | ||
1233 | if Try_Container_Indexing (N, Nam, Expressions (N)) then | |
1234 | return; | |
1235 | else | |
1236 | No_Interpretation; | |
1237 | end if; | |
1238 | ||
1239 | else | |
1240 | No_Interpretation; | |
1241 | end if; | |
32bba3c9 | 1242 | |
5ff22245 ES |
1243 | return; |
1244 | end if; | |
1245 | end if; | |
996ae0b0 | 1246 | |
5ff22245 ES |
1247 | -- Operations generated for RACW stub types are called only through |
1248 | -- dispatching, and can never be the static interpretation of a call. | |
996ae0b0 | 1249 | |
5ff22245 ES |
1250 | if Is_RACW_Stub_Type_Operation (Nam_Ent) then |
1251 | No_Interpretation; | |
1252 | return; | |
996ae0b0 RK |
1253 | end if; |
1254 | ||
1255 | Analyze_One_Call (N, Nam_Ent, True, Success); | |
1256 | ||
ec6078e3 ES |
1257 | -- If this is an indirect call, the return type of the access_to |
1258 | -- subprogram may be an incomplete type. At the point of the call, | |
947430d5 AC |
1259 | -- use the full type if available, and at the same time update the |
1260 | -- return type of the access_to_subprogram. | |
ec6078e3 ES |
1261 | |
1262 | if Success | |
d469eabe | 1263 | and then Nkind (Nam) = N_Explicit_Dereference |
ec6078e3 ES |
1264 | and then Ekind (Etype (N)) = E_Incomplete_Type |
1265 | and then Present (Full_View (Etype (N))) | |
1266 | then | |
1267 | Set_Etype (N, Full_View (Etype (N))); | |
1268 | Set_Etype (Nam_Ent, Etype (N)); | |
1269 | end if; | |
1270 | ||
a7e68e7f HK |
1271 | -- Overloaded call |
1272 | ||
996ae0b0 | 1273 | else |
5ff22245 ES |
1274 | -- An overloaded selected component must denote overloaded operations |
1275 | -- of a concurrent type. The interpretations are attached to the | |
1276 | -- simple name of those operations. | |
996ae0b0 RK |
1277 | |
1278 | if Nkind (Nam) = N_Selected_Component then | |
1279 | Nam := Selector_Name (Nam); | |
1280 | end if; | |
1281 | ||
1282 | Get_First_Interp (Nam, X, It); | |
996ae0b0 RK |
1283 | while Present (It.Nam) loop |
1284 | Nam_Ent := It.Nam; | |
1cb17b78 | 1285 | Deref := False; |
996ae0b0 RK |
1286 | |
1287 | -- Name may be call that returns an access to subprogram, or more | |
1288 | -- generally an overloaded expression one of whose interpretations | |
947430d5 AC |
1289 | -- yields an access to subprogram. If the name is an entity, we do |
1290 | -- not dereference, because the node is a call that returns the | |
1291 | -- access type: note difference between f(x), where the call may | |
1292 | -- return an access subprogram type, and f(x)(y), where the type | |
1293 | -- returned by the call to f is implicitly dereferenced to analyze | |
1294 | -- the outer call. | |
996ae0b0 RK |
1295 | |
1296 | if Is_Access_Type (Nam_Ent) then | |
1297 | Nam_Ent := Designated_Type (Nam_Ent); | |
1298 | ||
1299 | elsif Is_Access_Type (Etype (Nam_Ent)) | |
1cb17b78 AC |
1300 | and then |
1301 | (not Is_Entity_Name (Nam) | |
1302 | or else Nkind (N) = N_Procedure_Call_Statement) | |
996ae0b0 RK |
1303 | and then Ekind (Designated_Type (Etype (Nam_Ent))) |
1304 | = E_Subprogram_Type | |
1305 | then | |
1306 | Nam_Ent := Designated_Type (Etype (Nam_Ent)); | |
1cb17b78 AC |
1307 | |
1308 | if Is_Entity_Name (Nam) then | |
1309 | Deref := True; | |
1310 | end if; | |
996ae0b0 RK |
1311 | end if; |
1312 | ||
7415029d AC |
1313 | -- If the call has been rewritten from a prefixed call, the first |
1314 | -- parameter has been analyzed, but may need a subsequent | |
1315 | -- dereference, so skip its analysis now. | |
1316 | ||
1317 | if N /= Original_Node (N) | |
1318 | and then Nkind (Original_Node (N)) = Nkind (N) | |
1319 | and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N))) | |
1320 | and then Present (Parameter_Associations (N)) | |
1321 | and then Present (Etype (First (Parameter_Associations (N)))) | |
1322 | then | |
1323 | Analyze_One_Call | |
1324 | (N, Nam_Ent, False, Success, Skip_First => True); | |
1325 | else | |
1326 | Analyze_One_Call (N, Nam_Ent, False, Success); | |
1327 | end if; | |
996ae0b0 RK |
1328 | |
1329 | -- If the interpretation succeeds, mark the proper type of the | |
1330 | -- prefix (any valid candidate will do). If not, remove the | |
f4ef7b06 AC |
1331 | -- candidate interpretation. If this is a parameterless call |
1332 | -- on an anonymous access to subprogram, X is a variable with | |
1333 | -- an access discriminant D, the entity in the interpretation is | |
1334 | -- D, so rewrite X as X.D.all. | |
996ae0b0 RK |
1335 | |
1336 | if Success then | |
1cb17b78 AC |
1337 | if Deref |
1338 | and then Nkind (Parent (N)) /= N_Explicit_Dereference | |
1339 | then | |
f4ef7b06 AC |
1340 | if Ekind (It.Nam) = E_Discriminant |
1341 | and then Has_Implicit_Dereference (It.Nam) | |
1342 | then | |
1343 | Rewrite (Name (N), | |
1344 | Make_Explicit_Dereference (Loc, | |
4f324de2 AC |
1345 | Prefix => |
1346 | Make_Selected_Component (Loc, | |
1347 | Prefix => | |
1348 | New_Occurrence_Of (Entity (Nam), Loc), | |
1349 | Selector_Name => | |
1350 | New_Occurrence_Of (It.Nam, Loc)))); | |
1351 | ||
f4ef7b06 AC |
1352 | Analyze (N); |
1353 | return; | |
1354 | ||
1355 | else | |
1356 | Set_Entity (Nam, It.Nam); | |
1357 | Insert_Explicit_Dereference (Nam); | |
1358 | Set_Etype (Nam, Nam_Ent); | |
1359 | end if; | |
1cb17b78 AC |
1360 | |
1361 | else | |
1362 | Set_Etype (Nam, It.Typ); | |
1363 | end if; | |
996ae0b0 | 1364 | |
4f324de2 | 1365 | elsif Nkind_In (Name (N), N_Function_Call, N_Selected_Component) |
fbf5a39b | 1366 | then |
996ae0b0 RK |
1367 | Remove_Interp (X); |
1368 | end if; | |
1369 | ||
1370 | Get_Next_Interp (X, It); | |
1371 | end loop; | |
1372 | ||
a7e68e7f HK |
1373 | -- If the name is the result of a function call, it can only be a |
1374 | -- call to a function returning an access to subprogram. Insert | |
1375 | -- explicit dereference. | |
996ae0b0 RK |
1376 | |
1377 | if Nkind (Nam) = N_Function_Call then | |
1378 | Insert_Explicit_Dereference (Nam); | |
1379 | end if; | |
1380 | ||
1381 | if Etype (N) = Any_Type then | |
1382 | ||
1383 | -- None of the interpretations is compatible with the actuals | |
1384 | ||
1385 | Diagnose_Call (N, Nam); | |
1386 | ||
1387 | -- Special checks for uninstantiated put routines | |
1388 | ||
1389 | if Nkind (N) = N_Procedure_Call_Statement | |
1390 | and then Is_Entity_Name (Nam) | |
1391 | and then Chars (Nam) = Name_Put | |
1392 | and then List_Length (Actuals) = 1 | |
1393 | then | |
1394 | declare | |
1395 | Arg : constant Node_Id := First (Actuals); | |
1396 | Typ : Entity_Id; | |
1397 | ||
1398 | begin | |
1399 | if Nkind (Arg) = N_Parameter_Association then | |
1400 | Typ := Etype (Explicit_Actual_Parameter (Arg)); | |
1401 | else | |
1402 | Typ := Etype (Arg); | |
1403 | end if; | |
1404 | ||
1405 | if Is_Signed_Integer_Type (Typ) then | |
1406 | Error_Msg_N | |
a90bd866 RD |
1407 | ("possible missing instantiation of " |
1408 | & "'Text_'I'O.'Integer_'I'O!", Nam); | |
996ae0b0 RK |
1409 | |
1410 | elsif Is_Modular_Integer_Type (Typ) then | |
1411 | Error_Msg_N | |
a90bd866 RD |
1412 | ("possible missing instantiation of " |
1413 | & "'Text_'I'O.'Modular_'I'O!", Nam); | |
996ae0b0 RK |
1414 | |
1415 | elsif Is_Floating_Point_Type (Typ) then | |
1416 | Error_Msg_N | |
a90bd866 RD |
1417 | ("possible missing instantiation of " |
1418 | & "'Text_'I'O.'Float_'I'O!", Nam); | |
996ae0b0 RK |
1419 | |
1420 | elsif Is_Ordinary_Fixed_Point_Type (Typ) then | |
1421 | Error_Msg_N | |
a90bd866 RD |
1422 | ("possible missing instantiation of " |
1423 | & "'Text_'I'O.'Fixed_'I'O!", Nam); | |
996ae0b0 RK |
1424 | |
1425 | elsif Is_Decimal_Fixed_Point_Type (Typ) then | |
1426 | Error_Msg_N | |
a90bd866 RD |
1427 | ("possible missing instantiation of " |
1428 | & "'Text_'I'O.'Decimal_'I'O!", Nam); | |
996ae0b0 RK |
1429 | |
1430 | elsif Is_Enumeration_Type (Typ) then | |
1431 | Error_Msg_N | |
a90bd866 RD |
1432 | ("possible missing instantiation of " |
1433 | & "'Text_'I'O.'Enumeration_'I'O!", Nam); | |
996ae0b0 RK |
1434 | end if; |
1435 | end; | |
1436 | end if; | |
1437 | ||
1438 | elsif not Is_Overloaded (N) | |
1439 | and then Is_Entity_Name (Nam) | |
1440 | then | |
aab883ec ES |
1441 | -- Resolution yields a single interpretation. Verify that the |
1442 | -- reference has capitalization consistent with the declaration. | |
996ae0b0 | 1443 | |
e7ba564f | 1444 | Set_Entity_With_Checks (Nam, Entity (Nam)); |
996ae0b0 RK |
1445 | Generate_Reference (Entity (Nam), Nam); |
1446 | ||
1447 | Set_Etype (Nam, Etype (Entity (Nam))); | |
30c20106 AC |
1448 | else |
1449 | Remove_Abstract_Operations (N); | |
996ae0b0 RK |
1450 | end if; |
1451 | ||
1452 | End_Interp_List; | |
1453 | end if; | |
288cbbbd JM |
1454 | |
1455 | if Ada_Version >= Ada_2012 then | |
1456 | ||
1457 | -- Check if the call contains a function with writable actuals | |
1458 | ||
1459 | Check_Writable_Actuals (N); | |
1460 | ||
551e1935 AC |
1461 | -- If found and the outermost construct that can be evaluated in |
1462 | -- an arbitrary order is precisely this call, then check all its | |
288cbbbd JM |
1463 | -- actuals. |
1464 | ||
22e89283 | 1465 | Check_Function_Writable_Actuals (N); |
b63d61f7 AC |
1466 | |
1467 | -- The return type of the function may be incomplete. This can be | |
1468 | -- the case if the type is a generic formal, or a limited view. It | |
1469 | -- can also happen when the function declaration appears before the | |
1470 | -- full view of the type (which is legal in Ada 2012) and the call | |
1471 | -- appears in a different unit, in which case the incomplete view | |
1472 | -- must be replaced with the full view to prevent subsequent type | |
1473 | -- errors. | |
1474 | ||
1475 | if Is_Incomplete_Type (Etype (N)) | |
1476 | and then Present (Full_View (Etype (N))) | |
1477 | then | |
1478 | if Is_Entity_Name (Nam) then | |
1479 | Set_Etype (Nam, Full_View (Etype (N))); | |
1480 | Set_Etype (Entity (Nam), Full_View (Etype (N))); | |
1481 | end if; | |
1482 | ||
1483 | Set_Etype (N, Full_View (Etype (N))); | |
1484 | end if; | |
288cbbbd | 1485 | end if; |
996ae0b0 RK |
1486 | end Analyze_Call; |
1487 | ||
19d846a0 RD |
1488 | ----------------------------- |
1489 | -- Analyze_Case_Expression -- | |
1490 | ----------------------------- | |
1491 | ||
1492 | procedure Analyze_Case_Expression (N : Node_Id) is | |
19d846a0 RD |
1493 | procedure Non_Static_Choice_Error (Choice : Node_Id); |
1494 | -- Error routine invoked by the generic instantiation below when | |
1495 | -- the case expression has a non static choice. | |
1496 | ||
15918371 AC |
1497 | package Case_Choices_Analysis is new |
1498 | Generic_Analyze_Choices | |
1499 | (Process_Associated_Node => No_OP); | |
1500 | use Case_Choices_Analysis; | |
1501 | ||
1502 | package Case_Choices_Checking is new | |
1503 | Generic_Check_Choices | |
1504 | (Process_Empty_Choice => No_OP, | |
19d846a0 RD |
1505 | Process_Non_Static_Choice => Non_Static_Choice_Error, |
1506 | Process_Associated_Node => No_OP); | |
15918371 | 1507 | use Case_Choices_Checking; |
19d846a0 | 1508 | |
19d846a0 RD |
1509 | ----------------------------- |
1510 | -- Non_Static_Choice_Error -- | |
1511 | ----------------------------- | |
1512 | ||
1513 | procedure Non_Static_Choice_Error (Choice : Node_Id) is | |
1514 | begin | |
1515 | Flag_Non_Static_Expr | |
1516 | ("choice given in case expression is not static!", Choice); | |
1517 | end Non_Static_Choice_Error; | |
1518 | ||
752b81d9 AC |
1519 | -- Local variables |
1520 | ||
1521 | Expr : constant Node_Id := Expression (N); | |
752b81d9 AC |
1522 | Alt : Node_Id; |
1523 | Exp_Type : Entity_Id; | |
1524 | Exp_Btype : Entity_Id; | |
1525 | ||
308aab0b AC |
1526 | FirstX : Node_Id := Empty; |
1527 | -- First expression in the case for which there is some type information | |
1528 | -- available, i.e. it is not Any_Type, which can happen because of some | |
1529 | -- error, or from the use of e.g. raise Constraint_Error. | |
1530 | ||
752b81d9 | 1531 | Others_Present : Boolean; |
15918371 | 1532 | -- Indicates if Others was present |
752b81d9 | 1533 | |
e49de265 | 1534 | Wrong_Alt : Node_Id := Empty; |
10671e7a AC |
1535 | -- For error reporting |
1536 | ||
19d846a0 RD |
1537 | -- Start of processing for Analyze_Case_Expression |
1538 | ||
1539 | begin | |
1540 | if Comes_From_Source (N) then | |
c86cf714 | 1541 | Check_Compiler_Unit ("case expression", N); |
19d846a0 RD |
1542 | end if; |
1543 | ||
1544 | Analyze_And_Resolve (Expr, Any_Discrete); | |
1545 | Check_Unset_Reference (Expr); | |
1546 | Exp_Type := Etype (Expr); | |
1547 | Exp_Btype := Base_Type (Exp_Type); | |
1548 | ||
1549 | Alt := First (Alternatives (N)); | |
1550 | while Present (Alt) loop | |
1551 | Analyze (Expression (Alt)); | |
308aab0b AC |
1552 | |
1553 | if No (FirstX) and then Etype (Expression (Alt)) /= Any_Type then | |
1554 | FirstX := Expression (Alt); | |
1555 | end if; | |
1556 | ||
19d846a0 RD |
1557 | Next (Alt); |
1558 | end loop; | |
1559 | ||
308aab0b AC |
1560 | -- Get our initial type from the first expression for which we got some |
1561 | -- useful type information from the expression. | |
1562 | ||
19d846a0 RD |
1563 | if not Is_Overloaded (FirstX) then |
1564 | Set_Etype (N, Etype (FirstX)); | |
1565 | ||
1566 | else | |
1567 | declare | |
1568 | I : Interp_Index; | |
1569 | It : Interp; | |
1570 | ||
1571 | begin | |
1572 | Set_Etype (N, Any_Type); | |
1573 | ||
1574 | Get_First_Interp (FirstX, I, It); | |
1575 | while Present (It.Nam) loop | |
1576 | ||
308e6f3a RW |
1577 | -- For each interpretation of the first expression, we only |
1578 | -- add the interpretation if every other expression in the | |
19d846a0 RD |
1579 | -- case expression alternatives has a compatible type. |
1580 | ||
1581 | Alt := Next (First (Alternatives (N))); | |
1582 | while Present (Alt) loop | |
1583 | exit when not Has_Compatible_Type (Expression (Alt), It.Typ); | |
1584 | Next (Alt); | |
1585 | end loop; | |
1586 | ||
1587 | if No (Alt) then | |
1588 | Add_One_Interp (N, It.Typ, It.Typ); | |
10671e7a AC |
1589 | else |
1590 | Wrong_Alt := Alt; | |
19d846a0 RD |
1591 | end if; |
1592 | ||
1593 | Get_Next_Interp (I, It); | |
1594 | end loop; | |
1595 | end; | |
1596 | end if; | |
1597 | ||
1598 | Exp_Btype := Base_Type (Exp_Type); | |
1599 | ||
1600 | -- The expression must be of a discrete type which must be determinable | |
1601 | -- independently of the context in which the expression occurs, but | |
1602 | -- using the fact that the expression must be of a discrete type. | |
1603 | -- Moreover, the type this expression must not be a character literal | |
1604 | -- (which is always ambiguous). | |
1605 | ||
1606 | -- If error already reported by Resolve, nothing more to do | |
1607 | ||
15918371 | 1608 | if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then |
19d846a0 RD |
1609 | return; |
1610 | ||
bf0b0e5e AC |
1611 | -- Special casee message for character literal |
1612 | ||
19d846a0 RD |
1613 | elsif Exp_Btype = Any_Character then |
1614 | Error_Msg_N | |
1615 | ("character literal as case expression is ambiguous", Expr); | |
1616 | return; | |
1617 | end if; | |
1618 | ||
10671e7a | 1619 | if Etype (N) = Any_Type and then Present (Wrong_Alt) then |
bf0b0e5e AC |
1620 | Error_Msg_N |
1621 | ("type incompatible with that of previous alternatives", | |
1622 | Expression (Wrong_Alt)); | |
10671e7a AC |
1623 | return; |
1624 | end if; | |
1625 | ||
19d846a0 RD |
1626 | -- If the case expression is a formal object of mode in out, then |
1627 | -- treat it as having a nonstatic subtype by forcing use of the base | |
1628 | -- type (which has to get passed to Check_Case_Choices below). Also | |
1629 | -- use base type when the case expression is parenthesized. | |
1630 | ||
1631 | if Paren_Count (Expr) > 0 | |
1632 | or else (Is_Entity_Name (Expr) | |
1633 | and then Ekind (Entity (Expr)) = E_Generic_In_Out_Parameter) | |
1634 | then | |
1635 | Exp_Type := Exp_Btype; | |
1636 | end if; | |
1637 | ||
752b81d9 AC |
1638 | -- The case expression alternatives cover the range of a static subtype |
1639 | -- subject to aspect Static_Predicate. Do not check the choices when the | |
1640 | -- case expression has not been fully analyzed yet because this may lead | |
1641 | -- to bogus errors. | |
1642 | ||
edab6088 | 1643 | if Is_OK_Static_Subtype (Exp_Type) |
ee4eee0a | 1644 | and then Has_Static_Predicate_Aspect (Exp_Type) |
752b81d9 AC |
1645 | and then In_Spec_Expression |
1646 | then | |
1647 | null; | |
1648 | ||
15918371 | 1649 | -- Call Analyze_Choices and Check_Choices to do the rest of the work |
19d846a0 | 1650 | |
752b81d9 | 1651 | else |
15918371 AC |
1652 | Analyze_Choices (Alternatives (N), Exp_Type); |
1653 | Check_Choices (N, Alternatives (N), Exp_Type, Others_Present); | |
752b81d9 | 1654 | end if; |
19d846a0 RD |
1655 | |
1656 | if Exp_Type = Universal_Integer and then not Others_Present then | |
1657 | Error_Msg_N | |
1658 | ("case on universal integer requires OTHERS choice", Expr); | |
1659 | end if; | |
1660 | end Analyze_Case_Expression; | |
1661 | ||
996ae0b0 RK |
1662 | --------------------------- |
1663 | -- Analyze_Comparison_Op -- | |
1664 | --------------------------- | |
1665 | ||
1666 | procedure Analyze_Comparison_Op (N : Node_Id) is | |
1667 | L : constant Node_Id := Left_Opnd (N); | |
1668 | R : constant Node_Id := Right_Opnd (N); | |
1669 | Op_Id : Entity_Id := Entity (N); | |
1670 | ||
1671 | begin | |
1672 | Set_Etype (N, Any_Type); | |
1673 | Candidate_Type := Empty; | |
1674 | ||
1675 | Analyze_Expression (L); | |
1676 | Analyze_Expression (R); | |
1677 | ||
1678 | if Present (Op_Id) then | |
996ae0b0 RK |
1679 | if Ekind (Op_Id) = E_Operator then |
1680 | Find_Comparison_Types (L, R, Op_Id, N); | |
1681 | else | |
1682 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
1683 | end if; | |
1684 | ||
1685 | if Is_Overloaded (L) then | |
1686 | Set_Etype (L, Intersect_Types (L, R)); | |
1687 | end if; | |
1688 | ||
1689 | else | |
1690 | Op_Id := Get_Name_Entity_Id (Chars (N)); | |
996ae0b0 | 1691 | while Present (Op_Id) loop |
996ae0b0 RK |
1692 | if Ekind (Op_Id) = E_Operator then |
1693 | Find_Comparison_Types (L, R, Op_Id, N); | |
1694 | else | |
1695 | Analyze_User_Defined_Binary_Op (N, Op_Id); | |
1696 | end if; | |
1697 | ||
1698 | Op_Id := Homonym (Op_Id); | |
1699 | end loop; | |
1700 | end if; | |
1701 | ||
1702 | Operator_Check (N); | |
22e89283 | 1703 | Check_Function_Writable_Actuals (N); |
996ae0b0 RK |
1704 | end Analyze_Comparison_Op; |
1705 | ||
1706 | --------------------------- | |
1707 | -- Analyze_Concatenation -- | |
1708 | --------------------------- | |
1709 | ||
fe39cf20 BD |
1710 | procedure Analyze_Concatenation (N : Node_Id) is |
1711 | ||
1712 | -- We wish to avoid deep recursion, because concatenations are often | |
1713 | -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left | |
1714 | -- operands nonrecursively until we find something that is not a | |
1715 | -- concatenation (A in this case), or has already been analyzed. We | |
1716 | -- analyze that, and then walk back up the tree following Parent | |
1717 | -- pointers, calling Analyze_Concatenation_Rest to do the rest of the | |
1718 | -- work at each level. The Parent pointers allow us to avoid recursion, | |
1719 | -- and thus avoid running out of memory. | |
1720 | ||
1721 | NN : Node_Id := N; | |
1722 | L : Node_Id; | |
1723 | ||
1724 | begin | |
1725 | Candidate_Type := Empty; | |
1726 | ||
1727 | -- The following code is equivalent to: | |
1728 | ||
1729 | -- Set_Etype (N, Any_Type); | |
1730 | -- Analyze_Expression (Left_Opnd (N)); | |
1731 | -- Analyze_Concatenation_Rest (N); | |
1732 | ||
1733 | -- where the Analyze_Expression call recurses back here if the left | |
1734 | -- operand is a concatenation. | |
1735 | ||
1736 | -- Walk down left operands | |
1737 | ||
1738 | loop | |
1739 | Set_Etype (NN, Any_Type); | |
1740 | L := Left_Opnd (NN); | |
1741 | exit when Nkind (L) /= N_Op_Concat or else Analyzed (L); | |
1742 | NN := L; | |
1743 | end loop; | |
1744 | ||
1745 | -- Now (given the above example) NN is A&B and L is A | |
1746 | ||
1747 | -- First analyze L ... | |
1748 | ||
1749 | Analyze_Expression (L); | |
1750 | ||
1751 | -- ... then walk NN back up until we reach N (where we started), calling | |
1752 | -- Analyze_Concatenation_Rest along the way. | |
1753 | ||
1754 | loop | |
1755 | Analyze_Concatenation_Rest (NN); | |
1756 | exit when NN = N; | |
1757 | NN := Parent (NN); | |
1758 | end loop; | |
1759 | end Analyze_Concatenation; | |
1760 | ||
1761 | -------------------------------- | |
1762 | -- Analyze_Concatenation_Rest -- | |
1763 | -------------------------------- | |
1764 | ||
996ae0b0 RK |
1765 | -- If the only one-dimensional array type in scope is String, |
1766 | -- this is the resulting type of the operation. Otherwise there | |
1767 | -- will be a concatenation operation defined for each user-defined | |
1768 | -- one-dimensional array. | |
1769 | ||
fe39cf20 | 1770 | procedure Analyze_Concatenation_Rest (N : Node_Id) is |
996ae0b0 RK |
1771 | L : constant Node_Id := Left_Opnd (N); |
1772 | R : constant Node_Id := Right_Opnd (N); | |
1773 | Op_Id : Entity_Id := Entity (N); | |
1774 | LT : Entity_Id; | |
1775 | RT : Entity_Id; | |
1776 | ||
1777 | begin | |
996ae0b0 RK |
1778 | Analyze_Expression (R); |
1779 | ||
cd3cd5b1 AC |
1780 | -- If the entity is present, the node appears in an instance, and |
1781 | -- denotes a predefined concatenation operation. The resulting type is | |
1782 | -- obtained from the arguments when possible. If the arguments are | |
1783 | -- aggregates, the array type and the concatenation type must be | |
fbf5a39b | 1784 | -- visible. |
996ae0b0 RK |
1785 | |
1786 | if Present (Op_Id) then | |
1787 | if Ekind (Op_Id) = E_Operator then | |
996ae0b0 RK |
1788 | LT := Base_Type (Etype (L)); |
1789 | RT := Base_Type (Etype (R)); | |
1790 | ||
1791 | if Is_Array_Type (LT) | |
1792 | and then (RT = LT or else RT = Base_Type (Component_Type (LT))) | |
1793 | then | |
1794 | Add_One_Interp (N, Op_Id, LT); | |
1795 | ||
1796 | elsif Is_Array_Type (RT) | |
1797 | and then LT = Base_Type (Component_Type (RT)) | |
1798 | then | |
1799 | Add_One_Interp (N, Op_Id, RT); | |
1800 | ||
fbf5a39b AC |
1801 | -- If one operand is a string type or a user-defined array type, |
1802 | -- and the other is a literal, result is of the specific type. | |
1803 | ||
1804 | elsif | |
1805 | (Root_Type (LT) = Standard_String | |
1806 | or else Scope (LT) /= Standard_Standard) | |
1807 | and then Etype (R) = Any_String | |
1808 | then | |
1809 | Add_One_Interp (N, Op_Id, LT); | |
1810 | ||
1811 | elsif | |
1812 | (Root_Type (RT) = Standard_String | |
1813 | or else Scope (RT) /= Standard_Standard) | |
1814 | and then Etype (L) = Any_String | |
1815 | then | |
1816 | Add_One_Interp (N, Op_Id, RT); | |
1817 | ||
1818 | elsif not Is_Generic_Type (Etype (Op_Id)) then | |
996ae0b0 | 1819 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); |
fbf5a39b AC |
1820 | |
1821 | else | |
4c46b835 | 1822 | -- Type and its operations must be visible |
fbf5a39b AC |
1823 | |
1824 | Set_Entity (N, Empty); | |
1825 | Analyze_Concatenation (N); | |
996ae0b0 RK |
1826 | end if; |
1827 | ||
1828 | else | |
1829 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
1830 | end if; | |
1831 | ||
1832 | else | |
1a8fae99 | 1833 | Op_Id := Get_Name_Entity_Id (Name_Op_Concat); |
996ae0b0 RK |
1834 | while Present (Op_Id) loop |
1835 | if Ekind (Op_Id) = E_Operator then | |
1a8fae99 ES |
1836 | |
1837 | -- Do not consider operators declared in dead code, they can | |
1838 | -- not be part of the resolution. | |
1839 | ||
1840 | if Is_Eliminated (Op_Id) then | |
1841 | null; | |
1842 | else | |
1843 | Find_Concatenation_Types (L, R, Op_Id, N); | |
1844 | end if; | |
1845 | ||
996ae0b0 RK |
1846 | else |
1847 | Analyze_User_Defined_Binary_Op (N, Op_Id); | |
1848 | end if; | |
1849 | ||
1850 | Op_Id := Homonym (Op_Id); | |
1851 | end loop; | |
1852 | end if; | |
1853 | ||
1854 | Operator_Check (N); | |
fe39cf20 | 1855 | end Analyze_Concatenation_Rest; |
996ae0b0 | 1856 | |
996ae0b0 RK |
1857 | ------------------------- |
1858 | -- Analyze_Equality_Op -- | |
1859 | ------------------------- | |
1860 | ||
1861 | procedure Analyze_Equality_Op (N : Node_Id) is | |
4c46b835 AC |
1862 | Loc : constant Source_Ptr := Sloc (N); |
1863 | L : constant Node_Id := Left_Opnd (N); | |
1864 | R : constant Node_Id := Right_Opnd (N); | |
1865 | Op_Id : Entity_Id; | |
996ae0b0 RK |
1866 | |
1867 | begin | |
1868 | Set_Etype (N, Any_Type); | |
1869 | Candidate_Type := Empty; | |
1870 | ||
1871 | Analyze_Expression (L); | |
1872 | Analyze_Expression (R); | |
1873 | ||
1874 | -- If the entity is set, the node is a generic instance with a non-local | |
1875 | -- reference to the predefined operator or to a user-defined function. | |
1876 | -- It can also be an inequality that is expanded into the negation of a | |
1877 | -- call to a user-defined equality operator. | |
1878 | ||
1879 | -- For the predefined case, the result is Boolean, regardless of the | |
21d7ef70 | 1880 | -- type of the operands. The operands may even be limited, if they are |
996ae0b0 RK |
1881 | -- generic actuals. If they are overloaded, label the left argument with |
1882 | -- the common type that must be present, or with the type of the formal | |
1883 | -- of the user-defined function. | |
1884 | ||
1885 | if Present (Entity (N)) then | |
996ae0b0 RK |
1886 | Op_Id := Entity (N); |
1887 | ||
1888 | if Ekind (Op_Id) = E_Operator then | |
1889 | Add_One_Interp (N, Op_Id, Standard_Boolean); | |
1890 | else | |
1891 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
1892 | end if; | |
1893 | ||
1894 | if Is_Overloaded (L) then | |
996ae0b0 RK |
1895 | if Ekind (Op_Id) = E_Operator then |
1896 | Set_Etype (L, Intersect_Types (L, R)); | |
1897 | else | |
1898 | Set_Etype (L, Etype (First_Formal (Op_Id))); | |
1899 | end if; | |
1900 | end if; | |
1901 | ||
1902 | else | |
1903 | Op_Id := Get_Name_Entity_Id (Chars (N)); | |
996ae0b0 | 1904 | while Present (Op_Id) loop |
996ae0b0 RK |
1905 | if Ekind (Op_Id) = E_Operator then |
1906 | Find_Equality_Types (L, R, Op_Id, N); | |
1907 | else | |
1908 | Analyze_User_Defined_Binary_Op (N, Op_Id); | |
1909 | end if; | |
1910 | ||
1911 | Op_Id := Homonym (Op_Id); | |
1912 | end loop; | |
1913 | end if; | |
1914 | ||
84dad556 AC |
1915 | -- If there was no match, and the operator is inequality, this may be |
1916 | -- a case where inequality has not been made explicit, as for tagged | |
1917 | -- types. Analyze the node as the negation of an equality operation. | |
1918 | -- This cannot be done earlier, because before analysis we cannot rule | |
1919 | -- out the presence of an explicit inequality. | |
996ae0b0 RK |
1920 | |
1921 | if Etype (N) = Any_Type | |
1922 | and then Nkind (N) = N_Op_Ne | |
1923 | then | |
1924 | Op_Id := Get_Name_Entity_Id (Name_Op_Eq); | |
996ae0b0 | 1925 | while Present (Op_Id) loop |
996ae0b0 RK |
1926 | if Ekind (Op_Id) = E_Operator then |
1927 | Find_Equality_Types (L, R, Op_Id, N); | |
1928 | else | |
1929 | Analyze_User_Defined_Binary_Op (N, Op_Id); | |
1930 | end if; | |
1931 | ||
1932 | Op_Id := Homonym (Op_Id); | |
1933 | end loop; | |
1934 | ||
1935 | if Etype (N) /= Any_Type then | |
1936 | Op_Id := Entity (N); | |
1937 | ||
1938 | Rewrite (N, | |
1939 | Make_Op_Not (Loc, | |
1940 | Right_Opnd => | |
1941 | Make_Op_Eq (Loc, | |
aab883ec ES |
1942 | Left_Opnd => Left_Opnd (N), |
1943 | Right_Opnd => Right_Opnd (N)))); | |
996ae0b0 RK |
1944 | |
1945 | Set_Entity (Right_Opnd (N), Op_Id); | |
1946 | Analyze (N); | |
1947 | end if; | |
1948 | end if; | |
1949 | ||
1950 | Operator_Check (N); | |
22e89283 | 1951 | Check_Function_Writable_Actuals (N); |
996ae0b0 RK |
1952 | end Analyze_Equality_Op; |
1953 | ||
1954 | ---------------------------------- | |
1955 | -- Analyze_Explicit_Dereference -- | |
1956 | ---------------------------------- | |
1957 | ||
1958 | procedure Analyze_Explicit_Dereference (N : Node_Id) is | |
1959 | Loc : constant Source_Ptr := Sloc (N); | |
1960 | P : constant Node_Id := Prefix (N); | |
1961 | T : Entity_Id; | |
1962 | I : Interp_Index; | |
1963 | It : Interp; | |
1964 | New_N : Node_Id; | |
1965 | ||
1966 | function Is_Function_Type return Boolean; | |
4c46b835 AC |
1967 | -- Check whether node may be interpreted as an implicit function call |
1968 | ||
1969 | ---------------------- | |
1970 | -- Is_Function_Type -- | |
1971 | ---------------------- | |
996ae0b0 RK |
1972 | |
1973 | function Is_Function_Type return Boolean is | |
4c46b835 AC |
1974 | I : Interp_Index; |
1975 | It : Interp; | |
996ae0b0 RK |
1976 | |
1977 | begin | |
1978 | if not Is_Overloaded (N) then | |
1979 | return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type | |
1980 | and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type; | |
1981 | ||
1982 | else | |
1983 | Get_First_Interp (N, I, It); | |
996ae0b0 RK |
1984 | while Present (It.Nam) loop |
1985 | if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type | |
1986 | or else Etype (Base_Type (It.Typ)) = Standard_Void_Type | |
1987 | then | |
1988 | return False; | |
1989 | end if; | |
1990 | ||
1991 | Get_Next_Interp (I, It); | |
1992 | end loop; | |
1993 | ||
1994 | return True; | |
1995 | end if; | |
1996 | end Is_Function_Type; | |
1997 | ||
98123480 | 1998 | -- Start of processing for Analyze_Explicit_Dereference |
4c46b835 | 1999 | |
996ae0b0 | 2000 | begin |
11bc76df AC |
2001 | -- If source node, check SPARK restriction. We guard this with the |
2002 | -- source node check, because ??? | |
2003 | ||
36b8f95f | 2004 | if Comes_From_Source (N) then |
ce5ba43a | 2005 | Check_SPARK_05_Restriction ("explicit dereference is not allowed", N); |
36b8f95f | 2006 | end if; |
1d801f21 | 2007 | |
226a7fa4 AC |
2008 | -- In formal verification mode, keep track of all reads and writes |
2009 | -- through explicit dereferences. | |
2010 | ||
f5da7a97 | 2011 | if GNATprove_Mode then |
06b599fd | 2012 | SPARK_Specific.Generate_Dereference (N); |
226a7fa4 AC |
2013 | end if; |
2014 | ||
996ae0b0 RK |
2015 | Analyze (P); |
2016 | Set_Etype (N, Any_Type); | |
2017 | ||
2018 | -- Test for remote access to subprogram type, and if so return | |
2019 | -- after rewriting the original tree. | |
2020 | ||
2021 | if Remote_AST_E_Dereference (P) then | |
2022 | return; | |
2023 | end if; | |
2024 | ||
2025 | -- Normal processing for other than remote access to subprogram type | |
2026 | ||
2027 | if not Is_Overloaded (P) then | |
2028 | if Is_Access_Type (Etype (P)) then | |
2029 | ||
f3d57416 | 2030 | -- Set the Etype. We need to go through Is_For_Access_Subtypes to |
0a36105d JM |
2031 | -- avoid other problems caused by the Private_Subtype and it is |
2032 | -- safe to go to the Base_Type because this is the same as | |
2033 | -- converting the access value to its Base_Type. | |
996ae0b0 RK |
2034 | |
2035 | declare | |
2036 | DT : Entity_Id := Designated_Type (Etype (P)); | |
2037 | ||
2038 | begin | |
2039 | if Ekind (DT) = E_Private_Subtype | |
2040 | and then Is_For_Access_Subtype (DT) | |
2041 | then | |
2042 | DT := Base_Type (DT); | |
2043 | end if; | |
2044 | ||
0a36105d | 2045 | -- An explicit dereference is a legal occurrence of an |
0c6826a5 AC |
2046 | -- incomplete type imported through a limited_with clause, if |
2047 | -- the full view is visible, or if we are within an instance | |
2048 | -- body, where the enclosing body has a regular with_clause | |
2049 | -- on the unit. | |
0a36105d | 2050 | |
7b56a91b AC |
2051 | if From_Limited_With (DT) |
2052 | and then not From_Limited_With (Scope (DT)) | |
0a36105d JM |
2053 | and then |
2054 | (Is_Immediately_Visible (Scope (DT)) | |
2055 | or else | |
2056 | (Is_Child_Unit (Scope (DT)) | |
bff469f7 AC |
2057 | and then Is_Visible_Lib_Unit (Scope (DT))) |
2058 | or else In_Instance_Body) | |
0a36105d JM |
2059 | then |
2060 | Set_Etype (N, Available_View (DT)); | |
2061 | ||
2062 | else | |
2063 | Set_Etype (N, DT); | |
2064 | end if; | |
996ae0b0 RK |
2065 | end; |
2066 | ||
2067 | elsif Etype (P) /= Any_Type then | |
2068 | Error_Msg_N ("prefix of dereference must be an access type", N); | |
2069 | return; | |
2070 | end if; | |
2071 | ||
2072 | else | |
2073 | Get_First_Interp (P, I, It); | |
996ae0b0 RK |
2074 | while Present (It.Nam) loop |
2075 | T := It.Typ; | |
2076 | ||
2077 | if Is_Access_Type (T) then | |
2078 | Add_One_Interp (N, Designated_Type (T), Designated_Type (T)); | |
2079 | end if; | |
2080 | ||
2081 | Get_Next_Interp (I, It); | |
2082 | end loop; | |
2083 | ||
6e73e3ab | 2084 | -- Error if no interpretation of the prefix has an access type |
996ae0b0 RK |
2085 | |
2086 | if Etype (N) = Any_Type then | |
2087 | Error_Msg_N | |
2088 | ("access type required in prefix of explicit dereference", P); | |
2089 | Set_Etype (N, Any_Type); | |
2090 | return; | |
2091 | end if; | |
2092 | end if; | |
2093 | ||
2094 | if Is_Function_Type | |
2095 | and then Nkind (Parent (N)) /= N_Indexed_Component | |
2096 | ||
2097 | and then (Nkind (Parent (N)) /= N_Function_Call | |
2098 | or else N /= Name (Parent (N))) | |
2099 | ||
2100 | and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement | |
2101 | or else N /= Name (Parent (N))) | |
2102 | ||
2103 | and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration | |
2104 | and then (Nkind (Parent (N)) /= N_Attribute_Reference | |
2105 | or else | |
2106 | (Attribute_Name (Parent (N)) /= Name_Address | |
2107 | and then | |
2108 | Attribute_Name (Parent (N)) /= Name_Access)) | |
2109 | then | |
2110 | -- Name is a function call with no actuals, in a context that | |
2111 | -- requires deproceduring (including as an actual in an enclosing | |
98123480 | 2112 | -- function or procedure call). There are some pathological cases |
996ae0b0 RK |
2113 | -- where the prefix might include functions that return access to |
2114 | -- subprograms and others that return a regular type. Disambiguation | |
98123480 | 2115 | -- of those has to take place in Resolve. |
996ae0b0 RK |
2116 | |
2117 | New_N := | |
2118 | Make_Function_Call (Loc, | |
2119 | Name => Make_Explicit_Dereference (Loc, P), | |
2120 | Parameter_Associations => New_List); | |
2121 | ||
2122 | -- If the prefix is overloaded, remove operations that have formals, | |
2123 | -- we know that this is a parameterless call. | |
2124 | ||
2125 | if Is_Overloaded (P) then | |
2126 | Get_First_Interp (P, I, It); | |
996ae0b0 RK |
2127 | while Present (It.Nam) loop |
2128 | T := It.Typ; | |
2129 | ||
2130 | if No (First_Formal (Base_Type (Designated_Type (T)))) then | |
2131 | Set_Etype (P, T); | |
2132 | else | |
2133 | Remove_Interp (I); | |
2134 | end if; | |
2135 | ||
2136 | Get_Next_Interp (I, It); | |
2137 | end loop; | |
2138 | end if; | |
2139 | ||
2140 | Rewrite (N, New_N); | |
2141 | Analyze (N); | |
98123480 ES |
2142 | |
2143 | elsif not Is_Function_Type | |
2144 | and then Is_Overloaded (N) | |
2145 | then | |
2146 | -- The prefix may include access to subprograms and other access | |
1cb17b78 | 2147 | -- types. If the context selects the interpretation that is a |
56a7a3ab TQ |
2148 | -- function call (not a procedure call) we cannot rewrite the node |
2149 | -- yet, but we include the result of the call interpretation. | |
98123480 ES |
2150 | |
2151 | Get_First_Interp (N, I, It); | |
2152 | while Present (It.Nam) loop | |
2153 | if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type | |
2154 | and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type | |
1cb17b78 | 2155 | and then Nkind (Parent (N)) /= N_Procedure_Call_Statement |
98123480 ES |
2156 | then |
2157 | Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ)); | |
2158 | end if; | |
2159 | ||
2160 | Get_Next_Interp (I, It); | |
2161 | end loop; | |
996ae0b0 RK |
2162 | end if; |
2163 | ||
2164 | -- A value of remote access-to-class-wide must not be dereferenced | |
2165 | -- (RM E.2.2(16)). | |
2166 | ||
2167 | Validate_Remote_Access_To_Class_Wide_Type (N); | |
996ae0b0 RK |
2168 | end Analyze_Explicit_Dereference; |
2169 | ||
2170 | ------------------------ | |
2171 | -- Analyze_Expression -- | |
2172 | ------------------------ | |
2173 | ||
2174 | procedure Analyze_Expression (N : Node_Id) is | |
2175 | begin | |
5f50020a ES |
2176 | |
2177 | -- If the expression is an indexed component that will be rewritten | |
2178 | -- as a container indexing, it has already been analyzed. | |
2179 | ||
2180 | if Nkind (N) = N_Indexed_Component | |
2181 | and then Present (Generalized_Indexing (N)) | |
2182 | then | |
2183 | null; | |
2184 | ||
2185 | else | |
2186 | Analyze (N); | |
2187 | Check_Parameterless_Call (N); | |
2188 | end if; | |
996ae0b0 RK |
2189 | end Analyze_Expression; |
2190 | ||
955871d3 AC |
2191 | ------------------------------------- |
2192 | -- Analyze_Expression_With_Actions -- | |
2193 | ------------------------------------- | |
2194 | ||
2195 | procedure Analyze_Expression_With_Actions (N : Node_Id) is | |
2196 | A : Node_Id; | |
2197 | ||
2198 | begin | |
2199 | A := First (Actions (N)); | |
752b81d9 | 2200 | while Present (A) loop |
955871d3 AC |
2201 | Analyze (A); |
2202 | Next (A); | |
955871d3 AC |
2203 | end loop; |
2204 | ||
2ffcbaa5 AC |
2205 | Analyze_Expression (Expression (N)); |
2206 | Set_Etype (N, Etype (Expression (N))); | |
955871d3 AC |
2207 | end Analyze_Expression_With_Actions; |
2208 | ||
9b16cb57 RD |
2209 | --------------------------- |
2210 | -- Analyze_If_Expression -- | |
2211 | --------------------------- | |
2212 | ||
2213 | procedure Analyze_If_Expression (N : Node_Id) is | |
2214 | Condition : constant Node_Id := First (Expressions (N)); | |
2215 | Then_Expr : constant Node_Id := Next (Condition); | |
2216 | Else_Expr : Node_Id; | |
2217 | ||
2218 | begin | |
2219 | -- Defend against error of missing expressions from previous error | |
2220 | ||
2221 | if No (Then_Expr) then | |
ee2ba856 | 2222 | Check_Error_Detected; |
9b16cb57 RD |
2223 | return; |
2224 | end if; | |
2225 | ||
08988ed9 | 2226 | if Comes_From_Source (N) then |
ce5ba43a | 2227 | Check_SPARK_05_Restriction ("if expression is not allowed", N); |
08988ed9 | 2228 | end if; |
9b16cb57 RD |
2229 | |
2230 | Else_Expr := Next (Then_Expr); | |
2231 | ||
2232 | if Comes_From_Source (N) then | |
c86cf714 | 2233 | Check_Compiler_Unit ("if expression", N); |
9b16cb57 RD |
2234 | end if; |
2235 | ||
ac072cb2 AC |
2236 | -- Analyze and resolve the condition. We need to resolve this now so |
2237 | -- that it gets folded to True/False if possible, before we analyze | |
2238 | -- the THEN/ELSE branches, because when analyzing these branches, we | |
2239 | -- may call Is_Statically_Unevaluated, which expects the condition of | |
2240 | -- an enclosing IF to have been analyze/resolved/evaluated. | |
2241 | ||
9b16cb57 | 2242 | Analyze_Expression (Condition); |
ac072cb2 AC |
2243 | Resolve (Condition, Any_Boolean); |
2244 | ||
2245 | -- Analyze THEN expression and (if present) ELSE expression. For those | |
2246 | -- we delay resolution in the normal manner, because of overloading etc. | |
2247 | ||
9b16cb57 RD |
2248 | Analyze_Expression (Then_Expr); |
2249 | ||
2250 | if Present (Else_Expr) then | |
2251 | Analyze_Expression (Else_Expr); | |
2252 | end if; | |
2253 | ||
2254 | -- If then expression not overloaded, then that decides the type | |
2255 | ||
2256 | if not Is_Overloaded (Then_Expr) then | |
2257 | Set_Etype (N, Etype (Then_Expr)); | |
2258 | ||
2259 | -- Case where then expression is overloaded | |
2260 | ||
2261 | else | |
2262 | declare | |
2263 | I : Interp_Index; | |
2264 | It : Interp; | |
2265 | ||
2266 | begin | |
2267 | Set_Etype (N, Any_Type); | |
2268 | ||
bc795e3e | 2269 | -- Loop through interpretations of Then_Expr |
9b16cb57 RD |
2270 | |
2271 | Get_First_Interp (Then_Expr, I, It); | |
445e5888 | 2272 | while Present (It.Nam) loop |
9b16cb57 | 2273 | |
bc795e3e | 2274 | -- Add possible interpretation of Then_Expr if no Else_Expr, or |
0c6826a5 | 2275 | -- Else_Expr is present and has a compatible type. |
9b16cb57 | 2276 | |
445e5888 AC |
2277 | if No (Else_Expr) |
2278 | or else Has_Compatible_Type (Else_Expr, It.Typ) | |
2279 | then | |
2280 | Add_One_Interp (N, It.Typ, It.Typ); | |
2281 | end if; | |
9b16cb57 | 2282 | |
445e5888 AC |
2283 | Get_Next_Interp (I, It); |
2284 | end loop; | |
7408c4a5 | 2285 | |
bc38dbb4 AC |
2286 | -- If no valid interpretation has been found, then the type of the |
2287 | -- ELSE expression does not match any interpretation of the THEN | |
2288 | -- expression. | |
7408c4a5 AC |
2289 | |
2290 | if Etype (N) = Any_Type then | |
2291 | Error_Msg_N | |
2292 | ("type incompatible with that of `THEN` expression", | |
2293 | Else_Expr); | |
2294 | return; | |
2295 | end if; | |
9b16cb57 RD |
2296 | end; |
2297 | end if; | |
2298 | end Analyze_If_Expression; | |
2299 | ||
996ae0b0 RK |
2300 | ------------------------------------ |
2301 | -- Analyze_Indexed_Component_Form -- | |
2302 | ------------------------------------ | |
2303 | ||
2304 | procedure Analyze_Indexed_Component_Form (N : Node_Id) is | |
fbf5a39b AC |
2305 | P : constant Node_Id := Prefix (N); |
2306 | Exprs : constant List_Id := Expressions (N); | |
2307 | Exp : Node_Id; | |
2308 | P_T : Entity_Id; | |
2309 | E : Node_Id; | |
2310 | U_N : Entity_Id; | |
996ae0b0 RK |
2311 | |
2312 | procedure Process_Function_Call; | |
0c6826a5 AC |
2313 | -- Prefix in indexed component form is an overloadable entity, so the |
2314 | -- node is a function call. Reformat it as such. | |
996ae0b0 RK |
2315 | |
2316 | procedure Process_Indexed_Component; | |
2317 | -- Prefix in indexed component form is actually an indexed component. | |
2318 | -- This routine processes it, knowing that the prefix is already | |
2319 | -- resolved. | |
2320 | ||
2321 | procedure Process_Indexed_Component_Or_Slice; | |
2322 | -- An indexed component with a single index may designate a slice if | |
2323 | -- the index is a subtype mark. This routine disambiguates these two | |
2324 | -- cases by resolving the prefix to see if it is a subtype mark. | |
2325 | ||
2326 | procedure Process_Overloaded_Indexed_Component; | |
2327 | -- If the prefix of an indexed component is overloaded, the proper | |
2328 | -- interpretation is selected by the index types and the context. | |
2329 | ||
2330 | --------------------------- | |
2331 | -- Process_Function_Call -- | |
2332 | --------------------------- | |
2333 | ||
2334 | procedure Process_Function_Call is | |
1725676d | 2335 | Loc : constant Source_Ptr := Sloc (N); |
f5afb270 AC |
2336 | Actual : Node_Id; |
2337 | ||
996ae0b0 RK |
2338 | begin |
2339 | Change_Node (N, N_Function_Call); | |
2340 | Set_Name (N, P); | |
2341 | Set_Parameter_Associations (N, Exprs); | |
996ae0b0 | 2342 | |
401093c1 | 2343 | -- Analyze actuals prior to analyzing the call itself |
0a36105d | 2344 | |
4c46b835 | 2345 | Actual := First (Parameter_Associations (N)); |
996ae0b0 RK |
2346 | while Present (Actual) loop |
2347 | Analyze (Actual); | |
2348 | Check_Parameterless_Call (Actual); | |
0a36105d JM |
2349 | |
2350 | -- Move to next actual. Note that we use Next, not Next_Actual | |
2351 | -- here. The reason for this is a bit subtle. If a function call | |
0c6826a5 AC |
2352 | -- includes named associations, the parser recognizes the node |
2353 | -- as a call, and it is analyzed as such. If all associations are | |
0a36105d JM |
2354 | -- positional, the parser builds an indexed_component node, and |
2355 | -- it is only after analysis of the prefix that the construct | |
2356 | -- is recognized as a call, in which case Process_Function_Call | |
2357 | -- rewrites the node and analyzes the actuals. If the list of | |
2358 | -- actuals is malformed, the parser may leave the node as an | |
2359 | -- indexed component (despite the presence of named associations). | |
2360 | -- The iterator Next_Actual is equivalent to Next if the list is | |
2361 | -- positional, but follows the normalized chain of actuals when | |
2362 | -- named associations are present. In this case normalization has | |
2363 | -- not taken place, and actuals remain unanalyzed, which leads to | |
2364 | -- subsequent crashes or loops if there is an attempt to continue | |
2365 | -- analysis of the program. | |
2366 | ||
1725676d AC |
2367 | -- IF there is a single actual and it is a type name, the node |
2368 | -- can only be interpreted as a slice of a parameterless call. | |
2369 | -- Rebuild the node as such and analyze. | |
2370 | ||
2371 | if No (Next (Actual)) | |
2372 | and then Is_Entity_Name (Actual) | |
2373 | and then Is_Type (Entity (Actual)) | |
2374 | and then Is_Discrete_Type (Entity (Actual)) | |
2375 | then | |
2376 | Replace (N, | |
adc876a8 AC |
2377 | Make_Slice (Loc, |
2378 | Prefix => P, | |
2379 | Discrete_Range => | |
2380 | New_Occurrence_Of (Entity (Actual), Loc))); | |
1725676d AC |
2381 | Analyze (N); |
2382 | return; | |
2383 | ||
2384 | else | |
2385 | Next (Actual); | |
2386 | end if; | |
996ae0b0 RK |
2387 | end loop; |
2388 | ||
2389 | Analyze_Call (N); | |
2390 | end Process_Function_Call; | |
2391 | ||
2392 | ------------------------------- | |
2393 | -- Process_Indexed_Component -- | |
2394 | ------------------------------- | |
2395 | ||
2396 | procedure Process_Indexed_Component is | |
fe39cf20 BD |
2397 | Exp : Node_Id; |
2398 | Array_Type : Entity_Id; | |
2399 | Index : Node_Id; | |
2400 | Pent : Entity_Id := Empty; | |
996ae0b0 RK |
2401 | |
2402 | begin | |
2403 | Exp := First (Exprs); | |
2404 | ||
2405 | if Is_Overloaded (P) then | |
2406 | Process_Overloaded_Indexed_Component; | |
2407 | ||
2408 | else | |
2409 | Array_Type := Etype (P); | |
2410 | ||
6e73e3ab AC |
2411 | if Is_Entity_Name (P) then |
2412 | Pent := Entity (P); | |
2413 | elsif Nkind (P) = N_Selected_Component | |
2414 | and then Is_Entity_Name (Selector_Name (P)) | |
2415 | then | |
2416 | Pent := Entity (Selector_Name (P)); | |
2417 | end if; | |
2418 | ||
2419 | -- Prefix must be appropriate for an array type, taking into | |
2420 | -- account a possible implicit dereference. | |
996ae0b0 RK |
2421 | |
2422 | if Is_Access_Type (Array_Type) then | |
324ac540 AC |
2423 | Error_Msg_NW |
2424 | (Warn_On_Dereference, "?d?implicit dereference", N); | |
d469eabe | 2425 | Array_Type := Process_Implicit_Dereference_Prefix (Pent, P); |
996ae0b0 RK |
2426 | end if; |
2427 | ||
2428 | if Is_Array_Type (Array_Type) then | |
bfaf8a97 AC |
2429 | |
2430 | -- In order to correctly access First_Index component later, | |
2431 | -- replace string literal subtype by its parent type. | |
2432 | ||
2433 | if Ekind (Array_Type) = E_String_Literal_Subtype then | |
2434 | Array_Type := Etype (Array_Type); | |
2435 | end if; | |
996ae0b0 | 2436 | |
6e73e3ab | 2437 | elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then |
996ae0b0 RK |
2438 | Analyze (Exp); |
2439 | Set_Etype (N, Any_Type); | |
2440 | ||
dafe11cd | 2441 | if not Has_Compatible_Type (Exp, Entry_Index_Type (Pent)) then |
996ae0b0 RK |
2442 | Error_Msg_N ("invalid index type in entry name", N); |
2443 | ||
2444 | elsif Present (Next (Exp)) then | |
2445 | Error_Msg_N ("too many subscripts in entry reference", N); | |
2446 | ||
2447 | else | |
2448 | Set_Etype (N, Etype (P)); | |
2449 | end if; | |
2450 | ||
2451 | return; | |
2452 | ||
2453 | elsif Is_Record_Type (Array_Type) | |
2454 | and then Remote_AST_I_Dereference (P) | |
2455 | then | |
2456 | return; | |
2457 | ||
50878404 | 2458 | elsif Try_Container_Indexing (N, P, Exprs) then |
d50f4827 AC |
2459 | return; |
2460 | ||
996ae0b0 RK |
2461 | elsif Array_Type = Any_Type then |
2462 | Set_Etype (N, Any_Type); | |
6465b6a7 AC |
2463 | |
2464 | -- In most cases the analysis of the prefix will have emitted | |
2465 | -- an error already, but if the prefix may be interpreted as a | |
2466 | -- call in prefixed notation, the report is left to the caller. | |
2467 | -- To prevent cascaded errors, report only if no previous ones. | |
2468 | ||
2469 | if Serious_Errors_Detected = 0 then | |
2470 | Error_Msg_N ("invalid prefix in indexed component", P); | |
2471 | ||
2472 | if Nkind (P) = N_Expanded_Name then | |
2473 | Error_Msg_NE ("\& is not visible", P, Selector_Name (P)); | |
2474 | end if; | |
2475 | end if; | |
2476 | ||
996ae0b0 RK |
2477 | return; |
2478 | ||
2479 | -- Here we definitely have a bad indexing | |
2480 | ||
2481 | else | |
2482 | if Nkind (Parent (N)) = N_Requeue_Statement | |
6e73e3ab | 2483 | and then Present (Pent) and then Ekind (Pent) = E_Entry |
996ae0b0 RK |
2484 | then |
2485 | Error_Msg_N | |
2486 | ("REQUEUE does not permit parameters", First (Exprs)); | |
2487 | ||
2488 | elsif Is_Entity_Name (P) | |
2489 | and then Etype (P) = Standard_Void_Type | |
2490 | then | |
0c6826a5 | 2491 | Error_Msg_NE ("incorrect use of &", P, Entity (P)); |
996ae0b0 RK |
2492 | |
2493 | else | |
2494 | Error_Msg_N ("array type required in indexed component", P); | |
2495 | end if; | |
2496 | ||
2497 | Set_Etype (N, Any_Type); | |
2498 | return; | |
2499 | end if; | |
2500 | ||
2501 | Index := First_Index (Array_Type); | |
996ae0b0 RK |
2502 | while Present (Index) and then Present (Exp) loop |
2503 | if not Has_Compatible_Type (Exp, Etype (Index)) then | |
2504 | Wrong_Type (Exp, Etype (Index)); | |
2505 | Set_Etype (N, Any_Type); | |
2506 | return; | |
2507 | end if; | |
2508 | ||
2509 | Next_Index (Index); | |
2510 | Next (Exp); | |
2511 | end loop; | |
2512 | ||
2513 | Set_Etype (N, Component_Type (Array_Type)); | |
44a10091 | 2514 | Check_Implicit_Dereference (N, Etype (N)); |
996ae0b0 RK |
2515 | |
2516 | if Present (Index) then | |
2517 | Error_Msg_N | |
2518 | ("too few subscripts in array reference", First (Exprs)); | |
2519 | ||
2520 | elsif Present (Exp) then | |
2521 | Error_Msg_N ("too many subscripts in array reference", Exp); | |
2522 | end if; | |
2523 | end if; | |
996ae0b0 RK |
2524 | end Process_Indexed_Component; |
2525 | ||
2526 | ---------------------------------------- | |
2527 | -- Process_Indexed_Component_Or_Slice -- | |
2528 | ---------------------------------------- | |
2529 | ||
2530 | procedure Process_Indexed_Component_Or_Slice is | |
2531 | begin | |
2532 | Exp := First (Exprs); | |
996ae0b0 RK |
2533 | while Present (Exp) loop |
2534 | Analyze_Expression (Exp); | |
2535 | Next (Exp); | |
2536 | end loop; | |
2537 | ||
2538 | Exp := First (Exprs); | |
2539 | ||
0c6826a5 AC |
2540 | -- If one index is present, and it is a subtype name, then the node |
2541 | -- denotes a slice (note that the case of an explicit range for a | |
2542 | -- slice was already built as an N_Slice node in the first place, | |
2543 | -- so that case is not handled here). | |
996ae0b0 RK |
2544 | |
2545 | -- We use a replace rather than a rewrite here because this is one | |
2546 | -- of the cases in which the tree built by the parser is plain wrong. | |
2547 | ||
2548 | if No (Next (Exp)) | |
2549 | and then Is_Entity_Name (Exp) | |
2550 | and then Is_Type (Entity (Exp)) | |
2551 | then | |
2552 | Replace (N, | |
2553 | Make_Slice (Sloc (N), | |
2554 | Prefix => P, | |
2555 | Discrete_Range => New_Copy (Exp))); | |
2556 | Analyze (N); | |
2557 | ||
2558 | -- Otherwise (more than one index present, or single index is not | |
2559 | -- a subtype name), then we have the indexed component case. | |
2560 | ||
2561 | else | |
2562 | Process_Indexed_Component; | |
2563 | end if; | |
2564 | end Process_Indexed_Component_Or_Slice; | |
2565 | ||
2566 | ------------------------------------------ | |
2567 | -- Process_Overloaded_Indexed_Component -- | |
2568 | ------------------------------------------ | |
2569 | ||
2570 | procedure Process_Overloaded_Indexed_Component is | |
2571 | Exp : Node_Id; | |
2572 | I : Interp_Index; | |
2573 | It : Interp; | |
2574 | Typ : Entity_Id; | |
2575 | Index : Node_Id; | |
2576 | Found : Boolean; | |
2577 | ||
2578 | begin | |
2579 | Set_Etype (N, Any_Type); | |
996ae0b0 | 2580 | |
4c46b835 | 2581 | Get_First_Interp (P, I, It); |
996ae0b0 RK |
2582 | while Present (It.Nam) loop |
2583 | Typ := It.Typ; | |
2584 | ||
2585 | if Is_Access_Type (Typ) then | |
2586 | Typ := Designated_Type (Typ); | |
324ac540 AC |
2587 | Error_Msg_NW |
2588 | (Warn_On_Dereference, "?d?implicit dereference", N); | |
996ae0b0 RK |
2589 | end if; |
2590 | ||
2591 | if Is_Array_Type (Typ) then | |
2592 | ||
2593 | -- Got a candidate: verify that index types are compatible | |
2594 | ||
2595 | Index := First_Index (Typ); | |
2596 | Found := True; | |
996ae0b0 | 2597 | Exp := First (Exprs); |
996ae0b0 RK |
2598 | while Present (Index) and then Present (Exp) loop |
2599 | if Has_Compatible_Type (Exp, Etype (Index)) then | |
2600 | null; | |
2601 | else | |
2602 | Found := False; | |
2603 | Remove_Interp (I); | |
2604 | exit; | |
2605 | end if; | |
2606 | ||
2607 | Next_Index (Index); | |
2608 | Next (Exp); | |
2609 | end loop; | |
2610 | ||
2611 | if Found and then No (Index) and then No (Exp) then | |
44a10091 AC |
2612 | declare |
2613 | CT : constant Entity_Id := | |
2614 | Base_Type (Component_Type (Typ)); | |
2615 | begin | |
2616 | Add_One_Interp (N, CT, CT); | |
2617 | Check_Implicit_Dereference (N, CT); | |
2618 | end; | |
996ae0b0 | 2619 | end if; |
57a8057a | 2620 | |
50878404 | 2621 | elsif Try_Container_Indexing (N, P, Exprs) then |
57a8057a AC |
2622 | return; |
2623 | ||
996ae0b0 RK |
2624 | end if; |
2625 | ||
2626 | Get_Next_Interp (I, It); | |
2627 | end loop; | |
2628 | ||
2629 | if Etype (N) = Any_Type then | |
ad6b5b00 | 2630 | Error_Msg_N ("no legal interpretation for indexed component", N); |
996ae0b0 RK |
2631 | Set_Is_Overloaded (N, False); |
2632 | end if; | |
2633 | ||
2634 | End_Interp_List; | |
2635 | end Process_Overloaded_Indexed_Component; | |
2636 | ||
4c46b835 | 2637 | -- Start of processing for Analyze_Indexed_Component_Form |
996ae0b0 RK |
2638 | |
2639 | begin | |
2640 | -- Get name of array, function or type | |
2641 | ||
2642 | Analyze (P); | |
d469eabe | 2643 | |
24778dbb AC |
2644 | -- If P is an explicit dereference whose prefix is of a remote access- |
2645 | -- to-subprogram type, then N has already been rewritten as a subprogram | |
2646 | -- call and analyzed. | |
2647 | ||
d3b00ce3 | 2648 | if Nkind (N) in N_Subprogram_Call then |
24778dbb | 2649 | return; |
d469eabe | 2650 | |
24778dbb AC |
2651 | -- When the prefix is attribute 'Loop_Entry and the sole expression of |
2652 | -- the indexed component denotes a loop name, the indexed form is turned | |
2653 | -- into an attribute reference. | |
fbf5a39b | 2654 | |
24778dbb AC |
2655 | elsif Nkind (N) = N_Attribute_Reference |
2656 | and then Attribute_Name (N) = Name_Loop_Entry | |
2657 | then | |
fbf5a39b AC |
2658 | return; |
2659 | end if; | |
2660 | ||
2661 | pragma Assert (Nkind (N) = N_Indexed_Component); | |
2662 | ||
996ae0b0 RK |
2663 | P_T := Base_Type (Etype (P)); |
2664 | ||
878f708a | 2665 | if Is_Entity_Name (P) and then Present (Entity (P)) then |
996ae0b0 RK |
2666 | U_N := Entity (P); |
2667 | ||
aab883ec | 2668 | if Is_Type (U_N) then |
996ae0b0 | 2669 | |
4c46b835 | 2670 | -- Reformat node as a type conversion |
996ae0b0 RK |
2671 | |
2672 | E := Remove_Head (Exprs); | |
2673 | ||
2674 | if Present (First (Exprs)) then | |
2675 | Error_Msg_N | |
2676 | ("argument of type conversion must be single expression", N); | |
2677 | end if; | |
2678 | ||
2679 | Change_Node (N, N_Type_Conversion); | |
2680 | Set_Subtype_Mark (N, P); | |
2681 | Set_Etype (N, U_N); | |
2682 | Set_Expression (N, E); | |
2683 | ||
2684 | -- After changing the node, call for the specific Analysis | |
2685 | -- routine directly, to avoid a double call to the expander. | |
2686 | ||
2687 | Analyze_Type_Conversion (N); | |
2688 | return; | |
2689 | end if; | |
2690 | ||
2691 | if Is_Overloadable (U_N) then | |
2692 | Process_Function_Call; | |
2693 | ||
2694 | elsif Ekind (Etype (P)) = E_Subprogram_Type | |
2695 | or else (Is_Access_Type (Etype (P)) | |
2696 | and then | |
bce79204 AC |
2697 | Ekind (Designated_Type (Etype (P))) = |
2698 | E_Subprogram_Type) | |
996ae0b0 RK |
2699 | then |
2700 | -- Call to access_to-subprogram with possible implicit dereference | |
2701 | ||
2702 | Process_Function_Call; | |
2703 | ||
fbf5a39b AC |
2704 | elsif Is_Generic_Subprogram (U_N) then |
2705 | ||
4c46b835 | 2706 | -- A common beginner's (or C++ templates fan) error |
996ae0b0 RK |
2707 | |
2708 | Error_Msg_N ("generic subprogram cannot be called", N); | |
2709 | Set_Etype (N, Any_Type); | |
2710 | return; | |
2711 | ||
2712 | else | |
2713 | Process_Indexed_Component_Or_Slice; | |
2714 | end if; | |
2715 | ||
2716 | -- If not an entity name, prefix is an expression that may denote | |
2717 | -- an array or an access-to-subprogram. | |
2718 | ||
2719 | else | |
fbf5a39b | 2720 | if Ekind (P_T) = E_Subprogram_Type |
996ae0b0 RK |
2721 | or else (Is_Access_Type (P_T) |
2722 | and then | |
bce79204 | 2723 | Ekind (Designated_Type (P_T)) = E_Subprogram_Type) |
996ae0b0 RK |
2724 | then |
2725 | Process_Function_Call; | |
2726 | ||
2727 | elsif Nkind (P) = N_Selected_Component | |
3d918396 | 2728 | and then Present (Entity (Selector_Name (P))) |
ffe9aba8 | 2729 | and then Is_Overloadable (Entity (Selector_Name (P))) |
996ae0b0 RK |
2730 | then |
2731 | Process_Function_Call; | |
2732 | ||
3d918396 AC |
2733 | -- In ASIS mode within a generic, a prefixed call is analyzed and |
2734 | -- partially rewritten but the original indexed component has not | |
2735 | -- yet been rewritten as a call. Perform the replacement now. | |
2736 | ||
2737 | elsif Nkind (P) = N_Selected_Component | |
2738 | and then Nkind (Parent (P)) = N_Function_Call | |
2739 | and then ASIS_Mode | |
2740 | then | |
2741 | Rewrite (N, Parent (P)); | |
2742 | Analyze (N); | |
2743 | ||
996ae0b0 RK |
2744 | else |
2745 | -- Indexed component, slice, or a call to a member of a family | |
2746 | -- entry, which will be converted to an entry call later. | |
fbf5a39b | 2747 | |
996ae0b0 RK |
2748 | Process_Indexed_Component_Or_Slice; |
2749 | end if; | |
2750 | end if; | |
5f49133f AC |
2751 | |
2752 | Analyze_Dimension (N); | |
996ae0b0 RK |
2753 | end Analyze_Indexed_Component_Form; |
2754 | ||
2755 | ------------------------ | |
2756 | -- Analyze_Logical_Op -- | |
2757 | ------------------------ | |
2758 | ||
2759 | procedure Analyze_Logical_Op (N : Node_Id) is | |
2760 | L : constant Node_Id := Left_Opnd (N); | |
2761 | R : constant Node_Id := Right_Opnd (N); | |
2762 | Op_Id : Entity_Id := Entity (N); | |
2763 | ||
2764 | begin | |
2765 | Set_Etype (N, Any_Type); | |
2766 | Candidate_Type := Empty; | |
2767 | ||
2768 | Analyze_Expression (L); | |
2769 | Analyze_Expression (R); | |
2770 | ||
2771 | if Present (Op_Id) then | |
2772 | ||
2773 | if Ekind (Op_Id) = E_Operator then | |
2774 | Find_Boolean_Types (L, R, Op_Id, N); | |
2775 | else | |
2776 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
2777 | end if; | |
2778 | ||
2779 | else | |
2780 | Op_Id := Get_Name_Entity_Id (Chars (N)); | |
996ae0b0 RK |
2781 | while Present (Op_Id) loop |
2782 | if Ekind (Op_Id) = E_Operator then | |
2783 | Find_Boolean_Types (L, R, Op_Id, N); | |
2784 | else | |
2785 | Analyze_User_Defined_Binary_Op (N, Op_Id); | |
2786 | end if; | |
2787 | ||
2788 | Op_Id := Homonym (Op_Id); | |
2789 | end loop; | |
2790 | end if; | |
2791 | ||
2792 | Operator_Check (N); | |
22e89283 | 2793 | Check_Function_Writable_Actuals (N); |
996ae0b0 RK |
2794 | end Analyze_Logical_Op; |
2795 | ||
2796 | --------------------------- | |
2797 | -- Analyze_Membership_Op -- | |
2798 | --------------------------- | |
2799 | ||
2800 | procedure Analyze_Membership_Op (N : Node_Id) is | |
66150d01 | 2801 | Loc : constant Source_Ptr := Sloc (N); |
f2acf80c AC |
2802 | L : constant Node_Id := Left_Opnd (N); |
2803 | R : constant Node_Id := Right_Opnd (N); | |
996ae0b0 RK |
2804 | |
2805 | Index : Interp_Index; | |
2806 | It : Interp; | |
2807 | Found : Boolean := False; | |
2808 | I_F : Interp_Index; | |
2809 | T_F : Entity_Id; | |
2810 | ||
2811 | procedure Try_One_Interp (T1 : Entity_Id); | |
2812 | -- Routine to try one proposed interpretation. Note that the context | |
2813 | -- of the operation plays no role in resolving the arguments, so that | |
2814 | -- if there is more than one interpretation of the operands that is | |
2815 | -- compatible with a membership test, the operation is ambiguous. | |
2816 | ||
4c46b835 AC |
2817 | -------------------- |
2818 | -- Try_One_Interp -- | |
2819 | -------------------- | |
2820 | ||
996ae0b0 RK |
2821 | procedure Try_One_Interp (T1 : Entity_Id) is |
2822 | begin | |
2823 | if Has_Compatible_Type (R, T1) then | |
2824 | if Found | |
2825 | and then Base_Type (T1) /= Base_Type (T_F) | |
2826 | then | |
2827 | It := Disambiguate (L, I_F, Index, Any_Type); | |
2828 | ||
2829 | if It = No_Interp then | |
2830 | Ambiguous_Operands (N); | |
2831 | Set_Etype (L, Any_Type); | |
2832 | return; | |
2833 | ||
2834 | else | |
2835 | T_F := It.Typ; | |
2836 | end if; | |
2837 | ||
2838 | else | |
2839 | Found := True; | |
2840 | T_F := T1; | |
2841 | I_F := Index; | |
2842 | end if; | |
2843 | ||
2844 | Set_Etype (L, T_F); | |
2845 | end if; | |
996ae0b0 RK |
2846 | end Try_One_Interp; |
2847 | ||
197e4514 AC |
2848 | procedure Analyze_Set_Membership; |
2849 | -- If a set of alternatives is present, analyze each and find the | |
2850 | -- common type to which they must all resolve. | |
2851 | ||
2852 | ---------------------------- | |
2853 | -- Analyze_Set_Membership -- | |
2854 | ---------------------------- | |
2855 | ||
2856 | procedure Analyze_Set_Membership is | |
2857 | Alt : Node_Id; | |
2858 | Index : Interp_Index; | |
2859 | It : Interp; | |
197e4514 AC |
2860 | Candidate_Interps : Node_Id; |
2861 | Common_Type : Entity_Id := Empty; | |
2862 | ||
2863 | begin | |
e917e3b8 | 2864 | if Comes_From_Source (N) then |
c86cf714 | 2865 | Check_Compiler_Unit ("set membership", N); |
e917e3b8 AC |
2866 | end if; |
2867 | ||
197e4514 AC |
2868 | Analyze (L); |
2869 | Candidate_Interps := L; | |
2870 | ||
2871 | if not Is_Overloaded (L) then | |
2872 | Common_Type := Etype (L); | |
2873 | ||
2874 | Alt := First (Alternatives (N)); | |
2875 | while Present (Alt) loop | |
2876 | Analyze (Alt); | |
2877 | ||
2878 | if not Has_Compatible_Type (Alt, Common_Type) then | |
2879 | Wrong_Type (Alt, Common_Type); | |
2880 | end if; | |
2881 | ||
2882 | Next (Alt); | |
2883 | end loop; | |
2884 | ||
2885 | else | |
2886 | Alt := First (Alternatives (N)); | |
2887 | while Present (Alt) loop | |
2888 | Analyze (Alt); | |
2889 | if not Is_Overloaded (Alt) then | |
2890 | Common_Type := Etype (Alt); | |
2891 | ||
2892 | else | |
2893 | Get_First_Interp (Alt, Index, It); | |
2894 | while Present (It.Typ) loop | |
442c0581 RD |
2895 | if not |
2896 | Has_Compatible_Type (Candidate_Interps, It.Typ) | |
197e4514 AC |
2897 | then |
2898 | Remove_Interp (Index); | |
2899 | end if; | |
442c0581 | 2900 | |
197e4514 AC |
2901 | Get_Next_Interp (Index, It); |
2902 | end loop; | |
2903 | ||
2904 | Get_First_Interp (Alt, Index, It); | |
442c0581 | 2905 | |
197e4514 AC |
2906 | if No (It.Typ) then |
2907 | Error_Msg_N ("alternative has no legal type", Alt); | |
2908 | return; | |
2909 | end if; | |
2910 | ||
442c0581 RD |
2911 | -- If alternative is not overloaded, we have a unique type |
2912 | -- for all of them. | |
197e4514 AC |
2913 | |
2914 | Set_Etype (Alt, It.Typ); | |
2915 | Get_Next_Interp (Index, It); | |
2916 | ||
2917 | if No (It.Typ) then | |
2918 | Set_Is_Overloaded (Alt, False); | |
2919 | Common_Type := Etype (Alt); | |
2920 | end if; | |
2921 | ||
2922 | Candidate_Interps := Alt; | |
2923 | end if; | |
2924 | ||
2925 | Next (Alt); | |
2926 | end loop; | |
2927 | end if; | |
2928 | ||
2929 | Set_Etype (N, Standard_Boolean); | |
2930 | ||
2931 | if Present (Common_Type) then | |
2932 | Set_Etype (L, Common_Type); | |
cd1a470a AC |
2933 | |
2934 | -- The left operand may still be overloaded, to be resolved using | |
2935 | -- the Common_Type. | |
197e4514 AC |
2936 | |
2937 | else | |
2938 | Error_Msg_N ("cannot resolve membership operation", N); | |
2939 | end if; | |
2940 | end Analyze_Set_Membership; | |
2941 | ||
996ae0b0 RK |
2942 | -- Start of processing for Analyze_Membership_Op |
2943 | ||
2944 | begin | |
2945 | Analyze_Expression (L); | |
2946 | ||
e917e3b8 | 2947 | if No (R) and then Ada_Version >= Ada_2012 then |
197e4514 | 2948 | Analyze_Set_Membership; |
22e89283 | 2949 | Check_Function_Writable_Actuals (N); |
288cbbbd | 2950 | |
197e4514 AC |
2951 | return; |
2952 | end if; | |
2953 | ||
996ae0b0 RK |
2954 | if Nkind (R) = N_Range |
2955 | or else (Nkind (R) = N_Attribute_Reference | |
2956 | and then Attribute_Name (R) = Name_Range) | |
2957 | then | |
2958 | Analyze (R); | |
2959 | ||
2960 | if not Is_Overloaded (L) then | |
2961 | Try_One_Interp (Etype (L)); | |
2962 | ||
2963 | else | |
2964 | Get_First_Interp (L, Index, It); | |
996ae0b0 RK |
2965 | while Present (It.Typ) loop |
2966 | Try_One_Interp (It.Typ); | |
2967 | Get_Next_Interp (Index, It); | |
2968 | end loop; | |
2969 | end if; | |
2970 | ||
f6b5dc8e | 2971 | -- If not a range, it can be a subtype mark, or else it is a degenerate |
b0186f71 AC |
2972 | -- membership test with a singleton value, i.e. a test for equality, |
2973 | -- if the types are compatible. | |
996ae0b0 RK |
2974 | |
2975 | else | |
66150d01 | 2976 | Analyze (R); |
7483c888 | 2977 | |
66150d01 AC |
2978 | if Is_Entity_Name (R) |
2979 | and then Is_Type (Entity (R)) | |
2980 | then | |
2981 | Find_Type (R); | |
996ae0b0 | 2982 | Check_Fully_Declared (Entity (R), R); |
66150d01 | 2983 | |
b0186f71 AC |
2984 | elsif Ada_Version >= Ada_2012 |
2985 | and then Has_Compatible_Type (R, Etype (L)) | |
2986 | then | |
66150d01 AC |
2987 | if Nkind (N) = N_In then |
2988 | Rewrite (N, | |
2989 | Make_Op_Eq (Loc, | |
2990 | Left_Opnd => L, | |
2991 | Right_Opnd => R)); | |
2992 | else | |
2993 | Rewrite (N, | |
2994 | Make_Op_Ne (Loc, | |
2995 | Left_Opnd => L, | |
2996 | Right_Opnd => R)); | |
2997 | end if; | |
2998 | ||
2999 | Analyze (N); | |
3000 | return; | |
3001 | ||
3002 | else | |
b0186f71 AC |
3003 | -- In all versions of the language, if we reach this point there |
3004 | -- is a previous error that will be diagnosed below. | |
66150d01 AC |
3005 | |
3006 | Find_Type (R); | |
996ae0b0 RK |
3007 | end if; |
3008 | end if; | |
3009 | ||
3010 | -- Compatibility between expression and subtype mark or range is | |
3011 | -- checked during resolution. The result of the operation is Boolean | |
3012 | -- in any case. | |
3013 | ||
3014 | Set_Etype (N, Standard_Boolean); | |
fe45e59e ES |
3015 | |
3016 | if Comes_From_Source (N) | |
197e4514 | 3017 | and then Present (Right_Opnd (N)) |
fe45e59e ES |
3018 | and then Is_CPP_Class (Etype (Etype (Right_Opnd (N)))) |
3019 | then | |
3020 | Error_Msg_N ("membership test not applicable to cpp-class types", N); | |
3021 | end if; | |
288cbbbd | 3022 | |
22e89283 | 3023 | Check_Function_Writable_Actuals (N); |
996ae0b0 RK |
3024 | end Analyze_Membership_Op; |
3025 | ||
b727a82b AC |
3026 | ----------------- |
3027 | -- Analyze_Mod -- | |
3028 | ----------------- | |
3029 | ||
3030 | procedure Analyze_Mod (N : Node_Id) is | |
3031 | begin | |
3032 | -- A special warning check, if we have an expression of the form: | |
3033 | -- expr mod 2 * literal | |
3034 | -- where literal is 64 or less, then probably what was meant was | |
3035 | -- expr mod 2 ** literal | |
3036 | -- so issue an appropriate warning. | |
3037 | ||
3038 | if Warn_On_Suspicious_Modulus_Value | |
3039 | and then Nkind (Right_Opnd (N)) = N_Integer_Literal | |
3040 | and then Intval (Right_Opnd (N)) = Uint_2 | |
3041 | and then Nkind (Parent (N)) = N_Op_Multiply | |
3042 | and then Nkind (Right_Opnd (Parent (N))) = N_Integer_Literal | |
3043 | and then Intval (Right_Opnd (Parent (N))) <= Uint_64 | |
3044 | then | |
3045 | Error_Msg_N | |
324ac540 | 3046 | ("suspicious MOD value, was '*'* intended'??M?", Parent (N)); |
b727a82b AC |
3047 | end if; |
3048 | ||
3049 | -- Remaining processing is same as for other arithmetic operators | |
3050 | ||
3051 | Analyze_Arithmetic_Op (N); | |
3052 | end Analyze_Mod; | |
3053 | ||
996ae0b0 RK |
3054 | ---------------------- |
3055 | -- Analyze_Negation -- | |
3056 | ---------------------- | |
3057 | ||
3058 | procedure Analyze_Negation (N : Node_Id) is | |
3059 | R : constant Node_Id := Right_Opnd (N); | |
3060 | Op_Id : Entity_Id := Entity (N); | |
3061 | ||
3062 | begin | |
3063 | Set_Etype (N, Any_Type); | |
3064 | Candidate_Type := Empty; | |
3065 | ||
3066 | Analyze_Expression (R); | |
3067 | ||
3068 | if Present (Op_Id) then | |
3069 | if Ekind (Op_Id) = E_Operator then | |
3070 | Find_Negation_Types (R, Op_Id, N); | |
3071 | else | |
3072 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
3073 | end if; | |
3074 | ||
3075 | else | |
3076 | Op_Id := Get_Name_Entity_Id (Chars (N)); | |
996ae0b0 RK |
3077 | while Present (Op_Id) loop |
3078 | if Ekind (Op_Id) = E_Operator then | |
3079 | Find_Negation_Types (R, Op_Id, N); | |
3080 | else | |
3081 | Analyze_User_Defined_Unary_Op (N, Op_Id); | |
3082 | end if; | |
3083 | ||
3084 | Op_Id := Homonym (Op_Id); | |
3085 | end loop; | |
3086 | end if; | |
3087 | ||
3088 | Operator_Check (N); | |
3089 | end Analyze_Negation; | |
3090 | ||
15ce9ca2 AC |
3091 | ------------------ |
3092 | -- Analyze_Null -- | |
3093 | ------------------ | |
996ae0b0 RK |
3094 | |
3095 | procedure Analyze_Null (N : Node_Id) is | |
3096 | begin | |
ce5ba43a | 3097 | Check_SPARK_05_Restriction ("null is not allowed", N); |
1d801f21 | 3098 | |
996ae0b0 RK |
3099 | Set_Etype (N, Any_Access); |
3100 | end Analyze_Null; | |
3101 | ||
3102 | ---------------------- | |
3103 | -- Analyze_One_Call -- | |
3104 | ---------------------- | |
3105 | ||
3106 | procedure Analyze_One_Call | |
ec6078e3 ES |
3107 | (N : Node_Id; |
3108 | Nam : Entity_Id; | |
3109 | Report : Boolean; | |
3110 | Success : out Boolean; | |
3111 | Skip_First : Boolean := False) | |
996ae0b0 | 3112 | is |
d469eabe HK |
3113 | Actuals : constant List_Id := Parameter_Associations (N); |
3114 | Prev_T : constant Entity_Id := Etype (N); | |
3115 | ||
aab883ec ES |
3116 | Must_Skip : constant Boolean := Skip_First |
3117 | or else Nkind (Original_Node (N)) = N_Selected_Component | |
3118 | or else | |
3119 | (Nkind (Original_Node (N)) = N_Indexed_Component | |
3120 | and then Nkind (Prefix (Original_Node (N))) | |
3121 | = N_Selected_Component); | |
3122 | -- The first formal must be omitted from the match when trying to find | |
3123 | -- a primitive operation that is a possible interpretation, and also | |
3124 | -- after the call has been rewritten, because the corresponding actual | |
3125 | -- is already known to be compatible, and because this may be an | |
3126 | -- indexing of a call with default parameters. | |
3127 | ||
53cf4600 ES |
3128 | Formal : Entity_Id; |
3129 | Actual : Node_Id; | |
3130 | Is_Indexed : Boolean := False; | |
3131 | Is_Indirect : Boolean := False; | |
3132 | Subp_Type : constant Entity_Id := Etype (Nam); | |
3133 | Norm_OK : Boolean; | |
996ae0b0 | 3134 | |
1d2d8a8f AC |
3135 | function Compatible_Types_In_Predicate |
3136 | (T1 : Entity_Id; | |
3137 | T2 : Entity_Id) return Boolean; | |
3138 | -- For an Ada 2012 predicate or invariant, a call may mention an | |
3139 | -- incomplete type, while resolution of the corresponding predicate | |
3140 | -- function may see the full view, as a consequence of the delayed | |
3141 | -- resolution of the corresponding expressions. This may occur in | |
3142 | -- the body of a predicate function, or in a call to such. Anomalies | |
3143 | -- involving private and full views can also happen. In each case, | |
3144 | -- rewrite node or add conversions to remove spurious type errors. | |
3145 | ||
3146 | procedure Indicate_Name_And_Type; | |
3147 | -- If candidate interpretation matches, indicate name and type of result | |
3148 | -- on call node. | |
3149 | ||
157a9bf5 ES |
3150 | function Operator_Hidden_By (Fun : Entity_Id) return Boolean; |
3151 | -- There may be a user-defined operator that hides the current | |
3152 | -- interpretation. We must check for this independently of the | |
3153 | -- analysis of the call with the user-defined operation, because | |
3154 | -- the parameter names may be wrong and yet the hiding takes place. | |
3155 | -- This fixes a problem with ACATS test B34014O. | |
3156 | -- | |
3157 | -- When the type Address is a visible integer type, and the DEC | |
3158 | -- system extension is visible, the predefined operator may be | |
3159 | -- hidden as well, by one of the address operations in auxdec. | |
3160 | -- Finally, The abstract operations on address do not hide the | |
3161 | -- predefined operator (this is the purpose of making them abstract). | |
3162 | ||
1d2d8a8f AC |
3163 | ----------------------------------- |
3164 | -- Compatible_Types_In_Predicate -- | |
3165 | ----------------------------------- | |
3166 | ||
3167 | function Compatible_Types_In_Predicate | |
3168 | (T1 : Entity_Id; | |
3169 | T2 : Entity_Id) return Boolean | |
3170 | is | |
3171 | function Common_Type (T : Entity_Id) return Entity_Id; | |
3172 | -- Find non-private full view if any, without going to ancestor type | |
3173 | -- (as opposed to Underlying_Type). | |
3174 | ||
3175 | ----------------- | |
3176 | -- Common_Type -- | |
3177 | ----------------- | |
3178 | ||
3179 | function Common_Type (T : Entity_Id) return Entity_Id is | |
3180 | begin | |
3181 | if Is_Private_Type (T) and then Present (Full_View (T)) then | |
3182 | return Base_Type (Full_View (T)); | |
3183 | else | |
3184 | return Base_Type (T); | |
3185 | end if; | |
3186 | end Common_Type; | |
3187 | ||
3188 | -- Start of processing for Compatible_Types_In_Predicate | |
3189 | ||
3190 | begin | |
3191 | if (Ekind (Current_Scope) = E_Function | |
3192 | and then Is_Predicate_Function (Current_Scope)) | |
3193 | or else | |
3194 | (Ekind (Nam) = E_Function | |
3195 | and then Is_Predicate_Function (Nam)) | |
3196 | then | |
3197 | if Is_Incomplete_Type (T1) | |
3198 | and then Present (Full_View (T1)) | |
3199 | and then Full_View (T1) = T2 | |
3200 | then | |
3201 | Set_Etype (Formal, Etype (Actual)); | |
3202 | return True; | |
3203 | ||
3204 | elsif Common_Type (T1) = Common_Type (T2) then | |
3205 | Rewrite (Actual, Unchecked_Convert_To (Etype (Formal), Actual)); | |
3206 | return True; | |
3207 | ||
3208 | else | |
3209 | return False; | |
3210 | end if; | |
3211 | ||
3212 | else | |
3213 | return False; | |
3214 | end if; | |
3215 | end Compatible_Types_In_Predicate; | |
996ae0b0 | 3216 | |
fbf5a39b AC |
3217 | ---------------------------- |
3218 | -- Indicate_Name_And_Type -- | |
3219 | ---------------------------- | |
996ae0b0 | 3220 | |
fbf5a39b | 3221 | procedure Indicate_Name_And_Type is |
996ae0b0 RK |
3222 | begin |
3223 | Add_One_Interp (N, Nam, Etype (Nam)); | |
44a10091 | 3224 | Check_Implicit_Dereference (N, Etype (Nam)); |
996ae0b0 RK |
3225 | Success := True; |
3226 | ||
3227 | -- If the prefix of the call is a name, indicate the entity | |
3228 | -- being called. If it is not a name, it is an expression that | |
3229 | -- denotes an access to subprogram or else an entry or family. In | |
3230 | -- the latter case, the name is a selected component, and the entity | |
3231 | -- being called is noted on the selector. | |
3232 | ||
3233 | if not Is_Type (Nam) then | |
a3f2babd | 3234 | if Is_Entity_Name (Name (N)) then |
996ae0b0 | 3235 | Set_Entity (Name (N), Nam); |
d9307840 | 3236 | Set_Etype (Name (N), Etype (Nam)); |
996ae0b0 RK |
3237 | |
3238 | elsif Nkind (Name (N)) = N_Selected_Component then | |
3239 | Set_Entity (Selector_Name (Name (N)), Nam); | |
3240 | end if; | |
3241 | end if; | |
3242 | ||
3243 | if Debug_Flag_E and not Report then | |
3244 | Write_Str (" Overloaded call "); | |
3245 | Write_Int (Int (N)); | |
3246 | Write_Str (" compatible with "); | |
3247 | Write_Int (Int (Nam)); | |
3248 | Write_Eol; | |
3249 | end if; | |
fbf5a39b | 3250 | end Indicate_Name_And_Type; |
996ae0b0 | 3251 | |
157a9bf5 ES |
3252 | ------------------------ |
3253 | -- Operator_Hidden_By -- | |
3254 | ------------------------ | |
3255 | ||
3256 | function Operator_Hidden_By (Fun : Entity_Id) return Boolean is | |
3257 | Act1 : constant Node_Id := First_Actual (N); | |
3258 | Act2 : constant Node_Id := Next_Actual (Act1); | |
3259 | Form1 : constant Entity_Id := First_Formal (Fun); | |
3260 | Form2 : constant Entity_Id := Next_Formal (Form1); | |
3261 | ||
3262 | begin | |
e4deba8e | 3263 | if Ekind (Fun) /= E_Function or else Is_Abstract_Subprogram (Fun) then |
157a9bf5 ES |
3264 | return False; |
3265 | ||
3266 | elsif not Has_Compatible_Type (Act1, Etype (Form1)) then | |
3267 | return False; | |
3268 | ||
3269 | elsif Present (Form2) then | |
e4deba8e RD |
3270 | if No (Act2) |
3271 | or else not Has_Compatible_Type (Act2, Etype (Form2)) | |
157a9bf5 ES |
3272 | then |
3273 | return False; | |
3274 | end if; | |
3275 | ||
3276 | elsif Present (Act2) then | |
3277 | return False; | |
3278 | end if; | |
3279 | ||
3280 | -- Now we know that the arity of the operator matches the function, | |
3281 | -- and the function call is a valid interpretation. The function | |
3282 | -- hides the operator if it has the right signature, or if one of | |
3283 | -- its operands is a non-abstract operation on Address when this is | |
3284 | -- a visible integer type. | |
3285 | ||
3286 | return Hides_Op (Fun, Nam) | |
d9d25d04 | 3287 | or else Is_Descendant_Of_Address (Etype (Form1)) |
157a9bf5 ES |
3288 | or else |
3289 | (Present (Form2) | |
d9d25d04 | 3290 | and then Is_Descendant_Of_Address (Etype (Form2))); |
157a9bf5 ES |
3291 | end Operator_Hidden_By; |
3292 | ||
996ae0b0 RK |
3293 | -- Start of processing for Analyze_One_Call |
3294 | ||
3295 | begin | |
3296 | Success := False; | |
3297 | ||
157a9bf5 ES |
3298 | -- If the subprogram has no formals or if all the formals have defaults, |
3299 | -- and the return type is an array type, the node may denote an indexing | |
3300 | -- of the result of a parameterless call. In Ada 2005, the subprogram | |
3301 | -- may have one non-defaulted formal, and the call may have been written | |
3302 | -- in prefix notation, so that the rebuilt parameter list has more than | |
3303 | -- one actual. | |
996ae0b0 | 3304 | |
53cf4600 ES |
3305 | if not Is_Overloadable (Nam) |
3306 | and then Ekind (Nam) /= E_Subprogram_Type | |
3307 | and then Ekind (Nam) /= E_Entry_Family | |
3308 | then | |
3309 | return; | |
3310 | end if; | |
3311 | ||
80e59506 | 3312 | -- An indexing requires at least one actual. The name of the call cannot |
4bb9c7b9 AC |
3313 | -- be an implicit indirect call, so it cannot be a generated explicit |
3314 | -- dereference. | |
e1f3cb58 AC |
3315 | |
3316 | if not Is_Empty_List (Actuals) | |
aab883ec ES |
3317 | and then |
3318 | (Needs_No_Actuals (Nam) | |
3319 | or else | |
3320 | (Needs_One_Actual (Nam) | |
e4deba8e | 3321 | and then Present (Next_Actual (First (Actuals))))) |
996ae0b0 | 3322 | then |
4bb9c7b9 AC |
3323 | if Is_Array_Type (Subp_Type) |
3324 | and then | |
3325 | (Nkind (Name (N)) /= N_Explicit_Dereference | |
3326 | or else Comes_From_Source (Name (N))) | |
3327 | then | |
aab883ec | 3328 | Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip); |
996ae0b0 RK |
3329 | |
3330 | elsif Is_Access_Type (Subp_Type) | |
3331 | and then Is_Array_Type (Designated_Type (Subp_Type)) | |
3332 | then | |
3333 | Is_Indexed := | |
aab883ec ES |
3334 | Try_Indexed_Call |
3335 | (N, Nam, Designated_Type (Subp_Type), Must_Skip); | |
996ae0b0 | 3336 | |
758c442c | 3337 | -- The prefix can also be a parameterless function that returns an |
f3d57416 | 3338 | -- access to subprogram, in which case this is an indirect call. |
53cf4600 ES |
3339 | -- If this succeeds, an explicit dereference is added later on, |
3340 | -- in Analyze_Call or Resolve_Call. | |
758c442c | 3341 | |
996ae0b0 | 3342 | elsif Is_Access_Type (Subp_Type) |
401093c1 | 3343 | and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type |
996ae0b0 | 3344 | then |
53cf4600 | 3345 | Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type); |
996ae0b0 RK |
3346 | end if; |
3347 | ||
3348 | end if; | |
3349 | ||
5ff22245 | 3350 | -- If the call has been transformed into a slice, it is of the form |
30783513 | 3351 | -- F (Subtype) where F is parameterless. The node has been rewritten in |
5ff22245 ES |
3352 | -- Try_Indexed_Call and there is nothing else to do. |
3353 | ||
3354 | if Is_Indexed | |
21d7ef70 | 3355 | and then Nkind (N) = N_Slice |
5ff22245 ES |
3356 | then |
3357 | return; | |
3358 | end if; | |
3359 | ||
53cf4600 ES |
3360 | Normalize_Actuals |
3361 | (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK); | |
996ae0b0 RK |
3362 | |
3363 | if not Norm_OK then | |
3364 | ||
53cf4600 | 3365 | -- If an indirect call is a possible interpretation, indicate |
80e59506 | 3366 | -- success to the caller. This may be an indexing of an explicit |
4bb9c7b9 | 3367 | -- dereference of a call that returns an access type (see above). |
53cf4600 | 3368 | |
4bb9c7b9 AC |
3369 | if Is_Indirect |
3370 | or else (Is_Indexed | |
3371 | and then Nkind (Name (N)) = N_Explicit_Dereference | |
3372 | and then Comes_From_Source (Name (N))) | |
3373 | then | |
53cf4600 ES |
3374 | Success := True; |
3375 | return; | |
3376 | ||
996ae0b0 RK |
3377 | -- Mismatch in number or names of parameters |
3378 | ||
53cf4600 | 3379 | elsif Debug_Flag_E then |
996ae0b0 RK |
3380 | Write_Str (" normalization fails in call "); |
3381 | Write_Int (Int (N)); | |
3382 | Write_Str (" with subprogram "); | |
3383 | Write_Int (Int (Nam)); | |
3384 | Write_Eol; | |
3385 | end if; | |
3386 | ||
3387 | -- If the context expects a function call, discard any interpretation | |
3388 | -- that is a procedure. If the node is not overloaded, leave as is for | |
3389 | -- better error reporting when type mismatch is found. | |
3390 | ||
3391 | elsif Nkind (N) = N_Function_Call | |
3392 | and then Is_Overloaded (Name (N)) | |
3393 | and then Ekind (Nam) = E_Procedure | |
3394 | then | |
3395 | return; | |
3396 | ||
4c46b835 | 3397 | -- Ditto for function calls in a procedure context |
996ae0b0 RK |
3398 | |
3399 | elsif Nkind (N) = N_Procedure_Call_Statement | |
3400 | and then Is_Overloaded (Name (N)) | |
3401 | and then Etype (Nam) /= Standard_Void_Type | |
3402 | then | |
3403 | return; | |
3404 | ||
fe45e59e | 3405 | elsif No (Actuals) then |
996ae0b0 RK |
3406 | |
3407 | -- If Normalize succeeds, then there are default parameters for | |
3408 | -- all formals. | |
3409 | ||
fbf5a39b | 3410 | Indicate_Name_And_Type; |
996ae0b0 RK |
3411 | |
3412 | elsif Ekind (Nam) = E_Operator then | |
996ae0b0 RK |
3413 | if Nkind (N) = N_Procedure_Call_Statement then |
3414 | return; | |
3415 | end if; | |
3416 | ||
3417 | -- This can occur when the prefix of the call is an operator | |
3418 | -- name or an expanded name whose selector is an operator name. | |
3419 | ||
3420 | Analyze_Operator_Call (N, Nam); | |
3421 | ||
3422 | if Etype (N) /= Prev_T then | |
3423 | ||
157a9bf5 | 3424 | -- Check that operator is not hidden by a function interpretation |
996ae0b0 RK |
3425 | |
3426 | if Is_Overloaded (Name (N)) then | |
3427 | declare | |
3428 | I : Interp_Index; | |
3429 | It : Interp; | |
3430 | ||
3431 | begin | |
3432 | Get_First_Interp (Name (N), I, It); | |
996ae0b0 | 3433 | while Present (It.Nam) loop |
157a9bf5 | 3434 | if Operator_Hidden_By (It.Nam) then |
996ae0b0 RK |
3435 | Set_Etype (N, Prev_T); |
3436 | return; | |
3437 | end if; | |
3438 | ||
3439 | Get_Next_Interp (I, It); | |
3440 | end loop; | |
3441 | end; | |
3442 | end if; | |
3443 | ||
3444 | -- If operator matches formals, record its name on the call. | |
3445 | -- If the operator is overloaded, Resolve will select the | |
3446 | -- correct one from the list of interpretations. The call | |
3447 | -- node itself carries the first candidate. | |
3448 | ||
3449 | Set_Entity (Name (N), Nam); | |
3450 | Success := True; | |
3451 | ||
3452 | elsif Report and then Etype (N) = Any_Type then | |
3453 | Error_Msg_N ("incompatible arguments for operator", N); | |
3454 | end if; | |
3455 | ||
3456 | else | |
3457 | -- Normalize_Actuals has chained the named associations in the | |
3458 | -- correct order of the formals. | |
3459 | ||
3460 | Actual := First_Actual (N); | |
3461 | Formal := First_Formal (Nam); | |
ec6078e3 | 3462 | |
df3e68b1 HK |
3463 | -- If we are analyzing a call rewritten from object notation, skip |
3464 | -- first actual, which may be rewritten later as an explicit | |
3465 | -- dereference. | |
ec6078e3 | 3466 | |
aab883ec | 3467 | if Must_Skip then |
ec6078e3 ES |
3468 | Next_Actual (Actual); |
3469 | Next_Formal (Formal); | |
3470 | end if; | |
3471 | ||
996ae0b0 | 3472 | while Present (Actual) and then Present (Formal) loop |
fbf5a39b AC |
3473 | if Nkind (Parent (Actual)) /= N_Parameter_Association |
3474 | or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal) | |
996ae0b0 | 3475 | then |
9c510803 ES |
3476 | -- The actual can be compatible with the formal, but we must |
3477 | -- also check that the context is not an address type that is | |
7a5b62b0 | 3478 | -- visibly an integer type. In this case the use of literals is |
d9d25d04 | 3479 | -- illegal, except in the body of descendants of system, where |
7a5b62b0 | 3480 | -- arithmetic operations on address are of course used. |
9c510803 ES |
3481 | |
3482 | if Has_Compatible_Type (Actual, Etype (Formal)) | |
3483 | and then | |
3484 | (Etype (Actual) /= Universal_Integer | |
d9d25d04 | 3485 | or else not Is_Descendant_Of_Address (Etype (Formal)) |
8ab31c0c | 3486 | or else In_Predefined_Unit (N)) |
9c510803 | 3487 | then |
996ae0b0 RK |
3488 | Next_Actual (Actual); |
3489 | Next_Formal (Formal); | |
3490 | ||
061828e3 AC |
3491 | -- In Allow_Integer_Address mode, we allow an actual integer to |
3492 | -- match a formal address type and vice versa. We only do this | |
3493 | -- if we are certain that an error will otherwise be issued | |
3494 | ||
3495 | elsif Address_Integer_Convert_OK | |
3496 | (Etype (Actual), Etype (Formal)) | |
3497 | and then (Report and not Is_Indexed and not Is_Indirect) | |
3498 | then | |
3499 | -- Handle this case by introducing an unchecked conversion | |
3500 | ||
3501 | Rewrite (Actual, | |
3502 | Unchecked_Convert_To (Etype (Formal), | |
3503 | Relocate_Node (Actual))); | |
3504 | Analyze_And_Resolve (Actual, Etype (Formal)); | |
3505 | Next_Actual (Actual); | |
3506 | Next_Formal (Formal); | |
3507 | ||
a8a42b93 AC |
3508 | -- Under relaxed RM semantics silently replace occurrences of |
3509 | -- null by System.Address_Null. We only do this if we know that | |
3510 | -- an error will otherwise be issued. | |
3511 | ||
3512 | elsif Null_To_Null_Address_Convert_OK (Actual, Etype (Formal)) | |
3513 | and then (Report and not Is_Indexed and not Is_Indirect) | |
3514 | then | |
3515 | Replace_Null_By_Null_Address (Actual); | |
3516 | Analyze_And_Resolve (Actual, Etype (Formal)); | |
3517 | Next_Actual (Actual); | |
3518 | Next_Formal (Formal); | |
3519 | ||
1d2d8a8f AC |
3520 | elsif Compatible_Types_In_Predicate |
3521 | (Etype (Formal), Etype (Actual)) | |
a921e83c | 3522 | then |
a921e83c AC |
3523 | Next_Actual (Actual); |
3524 | Next_Formal (Formal); | |
3525 | ||
bb072d1c AC |
3526 | -- In a complex case where an enclosing generic and a nested |
3527 | -- generic package, both declared with partially parameterized | |
3528 | -- formal subprograms with the same names, are instantiated | |
3529 | -- with the same type, the types of the actual parameter and | |
3530 | -- that of the formal may appear incompatible at first sight. | |
3531 | ||
3532 | -- generic | |
3533 | -- type Outer_T is private; | |
3534 | -- with function Func (Formal : Outer_T) | |
3535 | -- return ... is <>; | |
3536 | ||
3537 | -- package Outer_Gen is | |
3538 | -- generic | |
3539 | -- type Inner_T is private; | |
3540 | -- with function Func (Formal : Inner_T) -- (1) | |
d030f3a4 | 3541 | -- return ... is <>; |
bb072d1c AC |
3542 | |
3543 | -- package Inner_Gen is | |
3544 | -- function Inner_Func (Formal : Inner_T) -- (2) | |
d030f3a4 | 3545 | -- return ... is (Func (Formal)); |
bb072d1c AC |
3546 | -- end Inner_Gen; |
3547 | -- end Outer_Generic; | |
3548 | ||
3549 | -- package Outer_Inst is new Outer_Gen (Actual_T); | |
3550 | -- package Inner_Inst is new Outer_Inst.Inner_Gen (Actual_T); | |
3551 | ||
3552 | -- In the example above, the type of parameter | |
3553 | -- Inner_Func.Formal at (2) is incompatible with the type of | |
3554 | -- Func.Formal at (1) in the context of instantiations | |
d030f3a4 AC |
3555 | -- Outer_Inst and Inner_Inst. In reality both types are generic |
3556 | -- actual subtypes renaming base type Actual_T as part of the | |
3557 | -- generic prologues for the instantiations. | |
3558 | ||
3559 | -- Recognize this case and add a type conversion to allow this | |
3560 | -- kind of generic actual subtype conformance. Note that this | |
3561 | -- is done only when the call is non-overloaded because the | |
3562 | -- resolution mechanism already has the means to disambiguate | |
3563 | -- similar cases. | |
bb072d1c AC |
3564 | |
3565 | elsif not Is_Overloaded (Name (N)) | |
3566 | and then Is_Type (Etype (Actual)) | |
3567 | and then Is_Type (Etype (Formal)) | |
3568 | and then Is_Generic_Actual_Type (Etype (Actual)) | |
3569 | and then Is_Generic_Actual_Type (Etype (Formal)) | |
3570 | and then Base_Type (Etype (Actual)) = | |
3571 | Base_Type (Etype (Formal)) | |
3572 | then | |
3573 | Rewrite (Actual, | |
3574 | Convert_To (Etype (Formal), Relocate_Node (Actual))); | |
3575 | Analyze_And_Resolve (Actual, Etype (Formal)); | |
3576 | Next_Actual (Actual); | |
3577 | Next_Formal (Formal); | |
3578 | ||
3579 | -- Handle failed type check | |
3580 | ||
996ae0b0 RK |
3581 | else |
3582 | if Debug_Flag_E then | |
3583 | Write_Str (" type checking fails in call "); | |
3584 | Write_Int (Int (N)); | |
3585 | Write_Str (" with formal "); | |
3586 | Write_Int (Int (Formal)); | |
3587 | Write_Str (" in subprogram "); | |
3588 | Write_Int (Int (Nam)); | |
3589 | Write_Eol; | |
3590 | end if; | |
3591 | ||
061828e3 AC |
3592 | -- Comment needed on the following test??? |
3593 | ||
53cf4600 | 3594 | if Report and not Is_Indexed and not Is_Indirect then |
758c442c GD |
3595 | |
3596 | -- Ada 2005 (AI-251): Complete the error notification | |
8f2eeab7 | 3597 | -- to help new Ada 2005 users. |
758c442c GD |
3598 | |
3599 | if Is_Class_Wide_Type (Etype (Formal)) | |
3600 | and then Is_Interface (Etype (Etype (Formal))) | |
3601 | and then not Interface_Present_In_Ancestor | |
3602 | (Typ => Etype (Actual), | |
3603 | Iface => Etype (Etype (Formal))) | |
3604 | then | |
758c442c | 3605 | Error_Msg_NE |
ec6078e3 | 3606 | ("(Ada 2005) does not implement interface }", |
758c442c GD |
3607 | Actual, Etype (Etype (Formal))); |
3608 | end if; | |
3609 | ||
996ae0b0 RK |
3610 | Wrong_Type (Actual, Etype (Formal)); |
3611 | ||
3612 | if Nkind (Actual) = N_Op_Eq | |
3613 | and then Nkind (Left_Opnd (Actual)) = N_Identifier | |
3614 | then | |
3615 | Formal := First_Formal (Nam); | |
996ae0b0 | 3616 | while Present (Formal) loop |
996ae0b0 | 3617 | if Chars (Left_Opnd (Actual)) = Chars (Formal) then |
4e7a4f6e | 3618 | Error_Msg_N -- CODEFIX |
fbf5a39b | 3619 | ("possible misspelling of `='>`!", Actual); |
996ae0b0 RK |
3620 | exit; |
3621 | end if; | |
3622 | ||
3623 | Next_Formal (Formal); | |
3624 | end loop; | |
3625 | end if; | |
3626 | ||
3627 | if All_Errors_Mode then | |
3628 | Error_Msg_Sloc := Sloc (Nam); | |
3629 | ||
3b42c566 RD |
3630 | if Etype (Formal) = Any_Type then |
3631 | Error_Msg_N | |
3632 | ("there is no legal actual parameter", Actual); | |
3633 | end if; | |
3634 | ||
996ae0b0 RK |
3635 | if Is_Overloadable (Nam) |
3636 | and then Present (Alias (Nam)) | |
3637 | and then not Comes_From_Source (Nam) | |
3638 | then | |
3639 | Error_Msg_NE | |
401093c1 ES |
3640 | ("\\ =='> in call to inherited operation & #!", |
3641 | Actual, Nam); | |
7324bf49 AC |
3642 | |
3643 | elsif Ekind (Nam) = E_Subprogram_Type then | |
3644 | declare | |
3645 | Access_To_Subprogram_Typ : | |
3646 | constant Entity_Id := | |
3647 | Defining_Identifier | |
3648 | (Associated_Node_For_Itype (Nam)); | |
3649 | begin | |
a90bd866 RD |
3650 | Error_Msg_NE |
3651 | ("\\ =='> in call to dereference of &#!", | |
3652 | Actual, Access_To_Subprogram_Typ); | |
7324bf49 AC |
3653 | end; |
3654 | ||
996ae0b0 | 3655 | else |
401093c1 ES |
3656 | Error_Msg_NE |
3657 | ("\\ =='> in call to &#!", Actual, Nam); | |
7324bf49 | 3658 | |
996ae0b0 RK |
3659 | end if; |
3660 | end if; | |
3661 | end if; | |
3662 | ||
3663 | return; | |
3664 | end if; | |
3665 | ||
3666 | else | |
3667 | -- Normalize_Actuals has verified that a default value exists | |
3668 | -- for this formal. Current actual names a subsequent formal. | |
3669 | ||
3670 | Next_Formal (Formal); | |
3671 | end if; | |
3672 | end loop; | |
3673 | ||
4c46b835 | 3674 | -- On exit, all actuals match |
996ae0b0 | 3675 | |
fbf5a39b | 3676 | Indicate_Name_And_Type; |
996ae0b0 RK |
3677 | end if; |
3678 | end Analyze_One_Call; | |
3679 | ||
15ce9ca2 AC |
3680 | --------------------------- |
3681 | -- Analyze_Operator_Call -- | |
3682 | --------------------------- | |
996ae0b0 RK |
3683 | |
3684 | procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is | |
3685 | Op_Name : constant Name_Id := Chars (Op_Id); | |
3686 | Act1 : constant Node_Id := First_Actual (N); | |
3687 | Act2 : constant Node_Id := Next_Actual (Act1); | |
3688 | ||
3689 | begin | |
4c46b835 AC |
3690 | -- Binary operator case |
3691 | ||
996ae0b0 RK |
3692 | if Present (Act2) then |
3693 | ||
4c46b835 | 3694 | -- If more than two operands, then not binary operator after all |
996ae0b0 RK |
3695 | |
3696 | if Present (Next_Actual (Act2)) then | |
996ae0b0 | 3697 | return; |
b7539c3b | 3698 | end if; |
996ae0b0 | 3699 | |
b7539c3b | 3700 | -- Otherwise action depends on operator |
996ae0b0 | 3701 | |
b7539c3b | 3702 | case Op_Name is |
d8f43ee6 HK |
3703 | when Name_Op_Add |
3704 | | Name_Op_Divide | |
3705 | | Name_Op_Expon | |
3706 | | Name_Op_Mod | |
3707 | | Name_Op_Multiply | |
3708 | | Name_Op_Rem | |
3709 | | Name_Op_Subtract | |
3710 | => | |
b7539c3b | 3711 | Find_Arithmetic_Types (Act1, Act2, Op_Id, N); |
996ae0b0 | 3712 | |
d8f43ee6 HK |
3713 | when Name_Op_And |
3714 | | Name_Op_Or | |
3715 | | Name_Op_Xor | |
3716 | => | |
b7539c3b | 3717 | Find_Boolean_Types (Act1, Act2, Op_Id, N); |
996ae0b0 | 3718 | |
d8f43ee6 HK |
3719 | when Name_Op_Ge |
3720 | | Name_Op_Gt | |
3721 | | Name_Op_Le | |
3722 | | Name_Op_Lt | |
3723 | => | |
b7539c3b | 3724 | Find_Comparison_Types (Act1, Act2, Op_Id, N); |
996ae0b0 | 3725 | |
d8f43ee6 HK |
3726 | when Name_Op_Eq |
3727 | | Name_Op_Ne | |
3728 | => | |
b7539c3b | 3729 | Find_Equality_Types (Act1, Act2, Op_Id, N); |
996ae0b0 | 3730 | |
d8f43ee6 | 3731 | when Name_Op_Concat => |
b7539c3b | 3732 | Find_Concatenation_Types (Act1, Act2, Op_Id, N); |
996ae0b0 | 3733 | |
b7539c3b AC |
3734 | -- Is this when others, or should it be an abort??? |
3735 | ||
d8f43ee6 | 3736 | when others => |
b7539c3b AC |
3737 | null; |
3738 | end case; | |
996ae0b0 | 3739 | |
4c46b835 | 3740 | -- Unary operator case |
996ae0b0 | 3741 | |
4c46b835 | 3742 | else |
b7539c3b | 3743 | case Op_Name is |
d8f43ee6 HK |
3744 | when Name_Op_Abs |
3745 | | Name_Op_Add | |
3746 | | Name_Op_Subtract | |
3747 | => | |
b7539c3b | 3748 | Find_Unary_Types (Act1, Op_Id, N); |
996ae0b0 | 3749 | |
d8f43ee6 | 3750 | when Name_Op_Not => |
b7539c3b | 3751 | Find_Negation_Types (Act1, Op_Id, N); |
996ae0b0 | 3752 | |
b7539c3b | 3753 | -- Is this when others correct, or should it be an abort??? |
996ae0b0 | 3754 | |
d8f43ee6 | 3755 | when others => |
b7539c3b AC |
3756 | null; |
3757 | end case; | |
996ae0b0 RK |
3758 | end if; |
3759 | end Analyze_Operator_Call; | |
3760 | ||
3761 | ------------------------------------------- | |
3762 | -- Analyze_Overloaded_Selected_Component -- | |
3763 | ------------------------------------------- | |
3764 | ||
3765 | procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is | |
fbf5a39b AC |
3766 | Nam : constant Node_Id := Prefix (N); |
3767 | Sel : constant Node_Id := Selector_Name (N); | |
996ae0b0 | 3768 | Comp : Entity_Id; |
996ae0b0 RK |
3769 | I : Interp_Index; |
3770 | It : Interp; | |
3771 | T : Entity_Id; | |
3772 | ||
3773 | begin | |
4c46b835 | 3774 | Set_Etype (Sel, Any_Type); |
996ae0b0 | 3775 | |
4c46b835 | 3776 | Get_First_Interp (Nam, I, It); |
996ae0b0 RK |
3777 | while Present (It.Typ) loop |
3778 | if Is_Access_Type (It.Typ) then | |
3779 | T := Designated_Type (It.Typ); | |
324ac540 | 3780 | Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N); |
996ae0b0 RK |
3781 | else |
3782 | T := It.Typ; | |
3783 | end if; | |
3784 | ||
95eb8b69 AC |
3785 | -- Locate the component. For a private prefix the selector can denote |
3786 | -- a discriminant. | |
3787 | ||
3788 | if Is_Record_Type (T) or else Is_Private_Type (T) then | |
d469eabe HK |
3789 | |
3790 | -- If the prefix is a class-wide type, the visible components are | |
3791 | -- those of the base type. | |
3792 | ||
3793 | if Is_Class_Wide_Type (T) then | |
3794 | T := Etype (T); | |
3795 | end if; | |
3796 | ||
996ae0b0 | 3797 | Comp := First_Entity (T); |
996ae0b0 | 3798 | while Present (Comp) loop |
996ae0b0 RK |
3799 | if Chars (Comp) = Chars (Sel) |
3800 | and then Is_Visible_Component (Comp) | |
3801 | then | |
996ae0b0 | 3802 | |
f16d05d9 AC |
3803 | -- AI05-105: if the context is an object renaming with |
3804 | -- an anonymous access type, the expected type of the | |
3805 | -- object must be anonymous. This is a name resolution rule. | |
996ae0b0 | 3806 | |
f16d05d9 AC |
3807 | if Nkind (Parent (N)) /= N_Object_Renaming_Declaration |
3808 | or else No (Access_Definition (Parent (N))) | |
3809 | or else Ekind (Etype (Comp)) = E_Anonymous_Access_Type | |
3810 | or else | |
3811 | Ekind (Etype (Comp)) = E_Anonymous_Access_Subprogram_Type | |
3812 | then | |
3813 | Set_Entity (Sel, Comp); | |
3814 | Set_Etype (Sel, Etype (Comp)); | |
3815 | Add_One_Interp (N, Etype (Comp), Etype (Comp)); | |
44a10091 | 3816 | Check_Implicit_Dereference (N, Etype (Comp)); |
f16d05d9 AC |
3817 | |
3818 | -- This also specifies a candidate to resolve the name. | |
3819 | -- Further overloading will be resolved from context. | |
3820 | -- The selector name itself does not carry overloading | |
3821 | -- information. | |
3822 | ||
3823 | Set_Etype (Nam, It.Typ); | |
3824 | ||
3825 | else | |
b61ee1aa | 3826 | -- Named access type in the context of a renaming |
f16d05d9 AC |
3827 | -- declaration with an access definition. Remove |
3828 | -- inapplicable candidate. | |
3829 | ||
3830 | Remove_Interp (I); | |
3831 | end if; | |
996ae0b0 RK |
3832 | end if; |
3833 | ||
3834 | Next_Entity (Comp); | |
3835 | end loop; | |
3836 | ||
3837 | elsif Is_Concurrent_Type (T) then | |
3838 | Comp := First_Entity (T); | |
996ae0b0 RK |
3839 | while Present (Comp) |
3840 | and then Comp /= First_Private_Entity (T) | |
3841 | loop | |
3842 | if Chars (Comp) = Chars (Sel) then | |
3843 | if Is_Overloadable (Comp) then | |
3844 | Add_One_Interp (Sel, Comp, Etype (Comp)); | |
3845 | else | |
e7ba564f | 3846 | Set_Entity_With_Checks (Sel, Comp); |
996ae0b0 RK |
3847 | Generate_Reference (Comp, Sel); |
3848 | end if; | |
3849 | ||
3850 | Set_Etype (Sel, Etype (Comp)); | |
3851 | Set_Etype (N, Etype (Comp)); | |
3852 | Set_Etype (Nam, It.Typ); | |
3853 | ||
09494c32 AC |
3854 | -- For access type case, introduce explicit dereference for |
3855 | -- more uniform treatment of entry calls. Do this only once | |
3856 | -- if several interpretations yield an access type. | |
996ae0b0 | 3857 | |
d469eabe HK |
3858 | if Is_Access_Type (Etype (Nam)) |
3859 | and then Nkind (Nam) /= N_Explicit_Dereference | |
3860 | then | |
996ae0b0 | 3861 | Insert_Explicit_Dereference (Nam); |
fbf5a39b | 3862 | Error_Msg_NW |
324ac540 | 3863 | (Warn_On_Dereference, "?d?implicit dereference", N); |
996ae0b0 RK |
3864 | end if; |
3865 | end if; | |
3866 | ||
3867 | Next_Entity (Comp); | |
3868 | end loop; | |
3869 | ||
3870 | Set_Is_Overloaded (N, Is_Overloaded (Sel)); | |
996ae0b0 RK |
3871 | end if; |
3872 | ||
3873 | Get_Next_Interp (I, It); | |
3874 | end loop; | |
3875 | ||
0a36105d JM |
3876 | if Etype (N) = Any_Type |
3877 | and then not Try_Object_Operation (N) | |
3878 | then | |
996ae0b0 RK |
3879 | Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel); |
3880 | Set_Entity (Sel, Any_Id); | |
3881 | Set_Etype (Sel, Any_Type); | |
3882 | end if; | |
996ae0b0 RK |
3883 | end Analyze_Overloaded_Selected_Component; |
3884 | ||
3885 | ---------------------------------- | |
3886 | -- Analyze_Qualified_Expression -- | |
3887 | ---------------------------------- | |
3888 | ||
3889 | procedure Analyze_Qualified_Expression (N : Node_Id) is | |
3890 | Mark : constant Entity_Id := Subtype_Mark (N); | |
45c8b94b ES |
3891 | Expr : constant Node_Id := Expression (N); |
3892 | I : Interp_Index; | |
3893 | It : Interp; | |
996ae0b0 RK |
3894 | T : Entity_Id; |
3895 | ||
3896 | begin | |
45c8b94b ES |
3897 | Analyze_Expression (Expr); |
3898 | ||
996ae0b0 RK |
3899 | Set_Etype (N, Any_Type); |
3900 | Find_Type (Mark); | |
3901 | T := Entity (Mark); | |
45c8b94b | 3902 | Set_Etype (N, T); |
996ae0b0 RK |
3903 | |
3904 | if T = Any_Type then | |
3905 | return; | |
3906 | end if; | |
996ae0b0 | 3907 | |
4c46b835 | 3908 | Check_Fully_Declared (T, N); |
45c8b94b ES |
3909 | |
3910 | -- If expected type is class-wide, check for exact match before | |
3911 | -- expansion, because if the expression is a dispatching call it | |
3912 | -- may be rewritten as explicit dereference with class-wide result. | |
3913 | -- If expression is overloaded, retain only interpretations that | |
3914 | -- will yield exact matches. | |
3915 | ||
3916 | if Is_Class_Wide_Type (T) then | |
3917 | if not Is_Overloaded (Expr) then | |
9fe696a3 | 3918 | if Base_Type (Etype (Expr)) /= Base_Type (T) then |
45c8b94b ES |
3919 | if Nkind (Expr) = N_Aggregate then |
3920 | Error_Msg_N ("type of aggregate cannot be class-wide", Expr); | |
3921 | else | |
3922 | Wrong_Type (Expr, T); | |
3923 | end if; | |
3924 | end if; | |
3925 | ||
3926 | else | |
3927 | Get_First_Interp (Expr, I, It); | |
3928 | ||
3929 | while Present (It.Nam) loop | |
3930 | if Base_Type (It.Typ) /= Base_Type (T) then | |
3931 | Remove_Interp (I); | |
3932 | end if; | |
3933 | ||
3934 | Get_Next_Interp (I, It); | |
3935 | end loop; | |
3936 | end if; | |
3937 | end if; | |
3938 | ||
996ae0b0 RK |
3939 | Set_Etype (N, T); |
3940 | end Analyze_Qualified_Expression; | |
3941 | ||
a961aa79 AC |
3942 | ----------------------------------- |
3943 | -- Analyze_Quantified_Expression -- | |
3944 | ----------------------------------- | |
3945 | ||
3946 | procedure Analyze_Quantified_Expression (N : Node_Id) is | |
4856cc2a | 3947 | function Is_Empty_Range (Typ : Entity_Id) return Boolean; |
538dbb56 AC |
3948 | -- If the iterator is part of a quantified expression, and the range is |
3949 | -- known to be statically empty, emit a warning and replace expression | |
4856cc2a | 3950 | -- with its static value. Returns True if the replacement occurs. |
538dbb56 | 3951 | |
0812b84e AC |
3952 | function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean; |
3953 | -- Determine whether if expression If_Expr lacks an else part or if it | |
3954 | -- has one, it evaluates to True. | |
3955 | ||
4856cc2a ES |
3956 | -------------------- |
3957 | -- Is_Empty_Range -- | |
3958 | -------------------- | |
3959 | ||
3960 | function Is_Empty_Range (Typ : Entity_Id) return Boolean is | |
3961 | Loc : constant Source_Ptr := Sloc (N); | |
538dbb56 AC |
3962 | |
3963 | begin | |
3964 | if Is_Array_Type (Typ) | |
4856cc2a ES |
3965 | and then Compile_Time_Known_Bounds (Typ) |
3966 | and then | |
9a6dc470 RD |
3967 | (Expr_Value (Type_Low_Bound (Etype (First_Index (Typ)))) > |
3968 | Expr_Value (Type_High_Bound (Etype (First_Index (Typ))))) | |
538dbb56 | 3969 | then |
4856cc2a ES |
3970 | Preanalyze_And_Resolve (Condition (N), Standard_Boolean); |
3971 | ||
538dbb56 | 3972 | if All_Present (N) then |
4856cc2a | 3973 | Error_Msg_N |
324ac540 | 3974 | ("??quantified expression with ALL " |
4856cc2a | 3975 | & "over a null range has value True", N); |
538dbb56 AC |
3976 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); |
3977 | ||
3978 | else | |
4856cc2a | 3979 | Error_Msg_N |
324ac540 | 3980 | ("??quantified expression with SOME " |
4856cc2a | 3981 | & "over a null range has value False", N); |
538dbb56 AC |
3982 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); |
3983 | end if; | |
3984 | ||
3985 | Analyze (N); | |
3986 | return True; | |
3987 | ||
3988 | else | |
3989 | return False; | |
3990 | end if; | |
3991 | end Is_Empty_Range; | |
3992 | ||
0812b84e AC |
3993 | ----------------------------- |
3994 | -- No_Else_Or_Trivial_True -- | |
3995 | ----------------------------- | |
3996 | ||
3997 | function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean is | |
3998 | Else_Expr : constant Node_Id := | |
3999 | Next (Next (First (Expressions (If_Expr)))); | |
4000 | begin | |
4001 | return | |
4002 | No (Else_Expr) | |
4003 | or else (Compile_Time_Known_Value (Else_Expr) | |
4004 | and then Is_True (Expr_Value (Else_Expr))); | |
4005 | end No_Else_Or_Trivial_True; | |
4006 | ||
4007 | -- Local variables | |
4008 | ||
4009 | Cond : constant Node_Id := Condition (N); | |
57081559 | 4010 | Loop_Id : Entity_Id; |
0812b84e AC |
4011 | QE_Scop : Entity_Id; |
4012 | ||
4856cc2a ES |
4013 | -- Start of processing for Analyze_Quantified_Expression |
4014 | ||
a961aa79 | 4015 | begin |
ce5ba43a | 4016 | Check_SPARK_05_Restriction ("quantified expression is not allowed", N); |
1d801f21 | 4017 | |
804670f1 AC |
4018 | -- Create a scope to emulate the loop-like behavior of the quantified |
4019 | -- expression. The scope is needed to provide proper visibility of the | |
4020 | -- loop variable. | |
b3e42de5 | 4021 | |
804670f1 AC |
4022 | QE_Scop := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L'); |
4023 | Set_Etype (QE_Scop, Standard_Void_Type); | |
4024 | Set_Scope (QE_Scop, Current_Scope); | |
4025 | Set_Parent (QE_Scop, N); | |
a961aa79 | 4026 | |
804670f1 | 4027 | Push_Scope (QE_Scop); |
c56a9ba4 | 4028 | |
804670f1 AC |
4029 | -- All constituents are preanalyzed and resolved to avoid untimely |
4030 | -- generation of various temporaries and types. Full analysis and | |
4031 | -- expansion is carried out when the quantified expression is | |
4032 | -- transformed into an expression with actions. | |
c56a9ba4 | 4033 | |
804670f1 AC |
4034 | if Present (Iterator_Specification (N)) then |
4035 | Preanalyze (Iterator_Specification (N)); | |
538dbb56 | 4036 | |
57081559 AC |
4037 | -- Do not proceed with the analysis when the range of iteration is |
4038 | -- empty. The appropriate error is issued by Is_Empty_Range. | |
4039 | ||
538dbb56 AC |
4040 | if Is_Entity_Name (Name (Iterator_Specification (N))) |
4041 | and then Is_Empty_Range (Etype (Name (Iterator_Specification (N)))) | |
4042 | then | |
4043 | return; | |
4044 | end if; | |
4045 | ||
57081559 | 4046 | else pragma Assert (Present (Loop_Parameter_Specification (N))); |
a736f6e6 AC |
4047 | declare |
4048 | Loop_Par : constant Node_Id := Loop_Parameter_Specification (N); | |
4049 | ||
4050 | begin | |
4051 | Preanalyze (Loop_Par); | |
4052 | ||
e4deba8e | 4053 | if Nkind (Discrete_Subtype_Definition (Loop_Par)) = N_Function_Call |
a736f6e6 AC |
4054 | and then Parent (Loop_Par) /= N |
4055 | then | |
4056 | -- The parser cannot distinguish between a loop specification | |
4057 | -- and an iterator specification. If after pre-analysis the | |
4058 | -- proper form has been recognized, rewrite the expression to | |
5f0c4d67 AC |
4059 | -- reflect the right kind. This is needed for proper ASIS |
4060 | -- navigation. If expansion is enabled, the transformation is | |
4061 | -- performed when the expression is rewritten as a loop. | |
a736f6e6 | 4062 | |
a736f6e6 AC |
4063 | Set_Iterator_Specification (N, |
4064 | New_Copy_Tree (Iterator_Specification (Parent (Loop_Par)))); | |
5f0c4d67 AC |
4065 | |
4066 | Set_Defining_Identifier (Iterator_Specification (N), | |
4067 | Relocate_Node (Defining_Identifier (Loop_Par))); | |
4068 | Set_Name (Iterator_Specification (N), | |
4069 | Relocate_Node (Discrete_Subtype_Definition (Loop_Par))); | |
4070 | Set_Comes_From_Source (Iterator_Specification (N), | |
4071 | Comes_From_Source (Loop_Parameter_Specification (N))); | |
4072 | Set_Loop_Parameter_Specification (N, Empty); | |
a736f6e6 AC |
4073 | end if; |
4074 | end; | |
ce6002ec AC |
4075 | end if; |
4076 | ||
0812b84e | 4077 | Preanalyze_And_Resolve (Cond, Standard_Boolean); |
804670f1 | 4078 | |
a961aa79 AC |
4079 | End_Scope; |
4080 | Set_Etype (N, Standard_Boolean); | |
0812b84e | 4081 | |
57081559 AC |
4082 | -- Verify that the loop variable is used within the condition of the |
4083 | -- quantified expression. | |
4084 | ||
4085 | if Present (Iterator_Specification (N)) then | |
4086 | Loop_Id := Defining_Identifier (Iterator_Specification (N)); | |
4087 | else | |
4088 | Loop_Id := Defining_Identifier (Loop_Parameter_Specification (N)); | |
4089 | end if; | |
4090 | ||
4091 | if Warn_On_Suspicious_Contract | |
4092 | and then not Referenced (Loop_Id, Cond) | |
4093 | then | |
124bed29 | 4094 | -- Generating C, this check causes spurious warnings on inlined |
519e9fdf | 4095 | -- postconditions; we can safely disable it because this check |
124bed29 | 4096 | -- was previously performed when analyzing the internally built |
519e9fdf AC |
4097 | -- postconditions procedure. |
4098 | ||
4099 | if Modify_Tree_For_C and then In_Inlined_Body then | |
4100 | null; | |
4101 | else | |
4102 | Error_Msg_N ("?T?unused variable &", Loop_Id); | |
4103 | end if; | |
57081559 AC |
4104 | end if; |
4105 | ||
e19fd0bd | 4106 | -- Diagnose a possible misuse of the SOME existential quantifier. When |
d1ec4768 RD |
4107 | -- we have a quantified expression of the form: |
4108 | ||
0812b84e | 4109 | -- for some X => (if P then Q [else True]) |
d1ec4768 | 4110 | |
e19fd0bd | 4111 | -- any value for X that makes P False results in the if expression being |
50ef946c | 4112 | -- trivially True, and so also results in the quantified expression |
e19fd0bd | 4113 | -- being trivially True. |
0812b84e | 4114 | |
e19fd0bd | 4115 | if Warn_On_Suspicious_Contract |
0812b84e AC |
4116 | and then not All_Present (N) |
4117 | and then Nkind (Cond) = N_If_Expression | |
4118 | and then No_Else_Or_Trivial_True (Cond) | |
4119 | then | |
e19fd0bd | 4120 | Error_Msg_N ("?T?suspicious expression", N); |
0812b84e AC |
4121 | Error_Msg_N ("\\did you mean (for all X ='> (if P then Q))", N); |
4122 | Error_Msg_N ("\\or (for some X ='> P and then Q) instead'?", N); | |
4123 | end if; | |
a961aa79 AC |
4124 | end Analyze_Quantified_Expression; |
4125 | ||
996ae0b0 RK |
4126 | ------------------- |
4127 | -- Analyze_Range -- | |
4128 | ------------------- | |
4129 | ||
4130 | procedure Analyze_Range (N : Node_Id) is | |
4131 | L : constant Node_Id := Low_Bound (N); | |
4132 | H : constant Node_Id := High_Bound (N); | |
4133 | I1, I2 : Interp_Index; | |
4134 | It1, It2 : Interp; | |
4135 | ||
4136 | procedure Check_Common_Type (T1, T2 : Entity_Id); | |
4137 | -- Verify the compatibility of two types, and choose the | |
4138 | -- non universal one if the other is universal. | |
4139 | ||
4140 | procedure Check_High_Bound (T : Entity_Id); | |
4141 | -- Test one interpretation of the low bound against all those | |
4142 | -- of the high bound. | |
4143 | ||
fbf5a39b | 4144 | procedure Check_Universal_Expression (N : Node_Id); |
a1092b48 AC |
4145 | -- In Ada 83, reject bounds of a universal range that are not literals |
4146 | -- or entity names. | |
fbf5a39b | 4147 | |
996ae0b0 RK |
4148 | ----------------------- |
4149 | -- Check_Common_Type -- | |
4150 | ----------------------- | |
4151 | ||
4152 | procedure Check_Common_Type (T1, T2 : Entity_Id) is | |
4153 | begin | |
b4592168 GD |
4154 | if Covers (T1 => T1, T2 => T2) |
4155 | or else | |
4156 | Covers (T1 => T2, T2 => T1) | |
4157 | then | |
996ae0b0 RK |
4158 | if T1 = Universal_Integer |
4159 | or else T1 = Universal_Real | |
4160 | or else T1 = Any_Character | |
4161 | then | |
4162 | Add_One_Interp (N, Base_Type (T2), Base_Type (T2)); | |
4163 | ||
fbf5a39b | 4164 | elsif T1 = T2 then |
996ae0b0 RK |
4165 | Add_One_Interp (N, T1, T1); |
4166 | ||
4167 | else | |
4168 | Add_One_Interp (N, Base_Type (T1), Base_Type (T1)); | |
4169 | end if; | |
4170 | end if; | |
4171 | end Check_Common_Type; | |
4172 | ||
4173 | ---------------------- | |
4174 | -- Check_High_Bound -- | |
4175 | ---------------------- | |
4176 | ||
4177 | procedure Check_High_Bound (T : Entity_Id) is | |
4178 | begin | |
4179 | if not Is_Overloaded (H) then | |
4180 | Check_Common_Type (T, Etype (H)); | |
4181 | else | |
4182 | Get_First_Interp (H, I2, It2); | |
996ae0b0 RK |
4183 | while Present (It2.Typ) loop |
4184 | Check_Common_Type (T, It2.Typ); | |
4185 | Get_Next_Interp (I2, It2); | |
4186 | end loop; | |
4187 | end if; | |
4188 | end Check_High_Bound; | |
4189 | ||
fbf5a39b AC |
4190 | ----------------------------- |
4191 | -- Is_Universal_Expression -- | |
4192 | ----------------------------- | |
4193 | ||
4194 | procedure Check_Universal_Expression (N : Node_Id) is | |
4195 | begin | |
4196 | if Etype (N) = Universal_Integer | |
4197 | and then Nkind (N) /= N_Integer_Literal | |
4198 | and then not Is_Entity_Name (N) | |
4199 | and then Nkind (N) /= N_Attribute_Reference | |
4200 | then | |
4201 | Error_Msg_N ("illegal bound in discrete range", N); | |
4202 | end if; | |
4203 | end Check_Universal_Expression; | |
4204 | ||
996ae0b0 RK |
4205 | -- Start of processing for Analyze_Range |
4206 | ||
4207 | begin | |
4208 | Set_Etype (N, Any_Type); | |
4209 | Analyze_Expression (L); | |
4210 | Analyze_Expression (H); | |
4211 | ||
4212 | if Etype (L) = Any_Type or else Etype (H) = Any_Type then | |
4213 | return; | |
4214 | ||
4215 | else | |
4216 | if not Is_Overloaded (L) then | |
4217 | Check_High_Bound (Etype (L)); | |
4218 | else | |
4219 | Get_First_Interp (L, I1, It1); | |
996ae0b0 RK |
4220 | while Present (It1.Typ) loop |
4221 | Check_High_Bound (It1.Typ); | |
4222 | Get_Next_Interp (I1, It1); | |
4223 | end loop; | |
4224 | end if; | |
4225 | ||
4226 | -- If result is Any_Type, then we did not find a compatible pair | |
4227 | ||
4228 | if Etype (N) = Any_Type then | |
4229 | Error_Msg_N ("incompatible types in range ", N); | |
4230 | end if; | |
4231 | end if; | |
fbf5a39b | 4232 | |
0ab80019 | 4233 | if Ada_Version = Ada_83 |
fbf5a39b AC |
4234 | and then |
4235 | (Nkind (Parent (N)) = N_Loop_Parameter_Specification | |
4c46b835 | 4236 | or else Nkind (Parent (N)) = N_Constrained_Array_Definition) |
fbf5a39b AC |
4237 | then |
4238 | Check_Universal_Expression (L); | |
4239 | Check_Universal_Expression (H); | |
4240 | end if; | |
d3820795 | 4241 | |
22e89283 | 4242 | Check_Function_Writable_Actuals (N); |
996ae0b0 RK |
4243 | end Analyze_Range; |
4244 | ||
4245 | ----------------------- | |
4246 | -- Analyze_Reference -- | |
4247 | ----------------------- | |
4248 | ||
4249 | procedure Analyze_Reference (N : Node_Id) is | |
4250 | P : constant Node_Id := Prefix (N); | |
b4592168 GD |
4251 | E : Entity_Id; |
4252 | T : Entity_Id; | |
996ae0b0 | 4253 | Acc_Type : Entity_Id; |
b4592168 | 4254 | |
996ae0b0 RK |
4255 | begin |
4256 | Analyze (P); | |
b4592168 | 4257 | |
c42e1b17 AC |
4258 | -- An interesting error check, if we take the 'Ref of an object for |
4259 | -- which a pragma Atomic or Volatile has been given, and the type of the | |
4260 | -- object is not Atomic or Volatile, then we are in trouble. The problem | |
4261 | -- is that no trace of the atomic/volatile status will remain for the | |
4262 | -- backend to respect when it deals with the resulting pointer, since | |
4263 | -- the pointer type will not be marked atomic (it is a pointer to the | |
4264 | -- base type of the object). | |
b4592168 GD |
4265 | |
4266 | -- It is not clear if that can ever occur, but in case it does, we will | |
4267 | -- generate an error message. Not clear if this message can ever be | |
4268 | -- generated, and pretty clear that it represents a bug if it is, still | |
d2f25cd1 AC |
4269 | -- seems worth checking, except in CodePeer mode where we do not really |
4270 | -- care and don't want to bother the user. | |
b4592168 GD |
4271 | |
4272 | T := Etype (P); | |
4273 | ||
4274 | if Is_Entity_Name (P) | |
4275 | and then Is_Object_Reference (P) | |
d2f25cd1 | 4276 | and then not CodePeer_Mode |
b4592168 GD |
4277 | then |
4278 | E := Entity (P); | |
4279 | T := Etype (P); | |
4280 | ||
4281 | if (Has_Atomic_Components (E) | |
c42e1b17 | 4282 | and then not Has_Atomic_Components (T)) |
b4592168 GD |
4283 | or else |
4284 | (Has_Volatile_Components (E) | |
c42e1b17 | 4285 | and then not Has_Volatile_Components (T)) |
b4592168 GD |
4286 | or else (Is_Atomic (E) and then not Is_Atomic (T)) |
4287 | or else (Is_Volatile (E) and then not Is_Volatile (T)) | |
4288 | then | |
4289 | Error_Msg_N ("cannot take reference to Atomic/Volatile object", N); | |
4290 | end if; | |
4291 | end if; | |
4292 | ||
4293 | -- Carry on with normal processing | |
4294 | ||
996ae0b0 | 4295 | Acc_Type := Create_Itype (E_Allocator_Type, N); |
b4592168 | 4296 | Set_Etype (Acc_Type, Acc_Type); |
996ae0b0 RK |
4297 | Set_Directly_Designated_Type (Acc_Type, Etype (P)); |
4298 | Set_Etype (N, Acc_Type); | |
4299 | end Analyze_Reference; | |
4300 | ||
4301 | -------------------------------- | |
4302 | -- Analyze_Selected_Component -- | |
4303 | -------------------------------- | |
4304 | ||
2383acbd AC |
4305 | -- Prefix is a record type or a task or protected type. In the latter case, |
4306 | -- the selector must denote a visible entry. | |
996ae0b0 RK |
4307 | |
4308 | procedure Analyze_Selected_Component (N : Node_Id) is | |
d469eabe HK |
4309 | Name : constant Node_Id := Prefix (N); |
4310 | Sel : constant Node_Id := Selector_Name (N); | |
4311 | Act_Decl : Node_Id; | |
4312 | Comp : Entity_Id; | |
4313 | Has_Candidate : Boolean := False; | |
c0e938d0 | 4314 | Hidden_Comp : Entity_Id; |
d469eabe | 4315 | In_Scope : Boolean; |
d1eb8a82 | 4316 | Is_Private_Op : Boolean; |
d469eabe HK |
4317 | Parent_N : Node_Id; |
4318 | Pent : Entity_Id := Empty; | |
4319 | Prefix_Type : Entity_Id; | |
401093c1 ES |
4320 | |
4321 | Type_To_Use : Entity_Id; | |
4322 | -- In most cases this is the Prefix_Type, but if the Prefix_Type is | |
4323 | -- a class-wide type, we use its root type, whose components are | |
4324 | -- present in the class-wide type. | |
4325 | ||
2383acbd AC |
4326 | Is_Single_Concurrent_Object : Boolean; |
4327 | -- Set True if the prefix is a single task or a single protected object | |
4328 | ||
20261dc1 AC |
4329 | procedure Find_Component_In_Instance (Rec : Entity_Id); |
4330 | -- In an instance, a component of a private extension may not be visible | |
4331 | -- while it was visible in the generic. Search candidate scope for a | |
4332 | -- component with the proper identifier. This is only done if all other | |
f90d14ac AC |
4333 | -- searches have failed. If a match is found, the Etype of both N and |
4334 | -- Sel are set from this component, and the entity of Sel is set to | |
4335 | -- reference this component. If no match is found, Entity (Sel) remains | |
7d9880c9 AC |
4336 | -- unset. For a derived type that is an actual of the instance, the |
4337 | -- desired component may be found in any ancestor. | |
20261dc1 | 4338 | |
d469eabe HK |
4339 | function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean; |
4340 | -- It is known that the parent of N denotes a subprogram call. Comp | |
4341 | -- is an overloadable component of the concurrent type of the prefix. | |
4342 | -- Determine whether all formals of the parent of N and Comp are mode | |
b4592168 GD |
4343 | -- conformant. If the parent node is not analyzed yet it may be an |
4344 | -- indexed component rather than a function call. | |
d469eabe | 4345 | |
bd717ec9 | 4346 | function Has_Dereference (Nod : Node_Id) return Boolean; |
0f8b3e5d | 4347 | -- Check whether prefix includes a dereference at any level. |
bd717ec9 | 4348 | |
20261dc1 AC |
4349 | -------------------------------- |
4350 | -- Find_Component_In_Instance -- | |
4351 | -------------------------------- | |
4352 | ||
4353 | procedure Find_Component_In_Instance (Rec : Entity_Id) is | |
4354 | Comp : Entity_Id; | |
7d9880c9 | 4355 | Typ : Entity_Id; |
20261dc1 AC |
4356 | |
4357 | begin | |
7d9880c9 AC |
4358 | Typ := Rec; |
4359 | while Present (Typ) loop | |
4360 | Comp := First_Component (Typ); | |
4361 | while Present (Comp) loop | |
4362 | if Chars (Comp) = Chars (Sel) then | |
4363 | Set_Entity_With_Checks (Sel, Comp); | |
4364 | Set_Etype (Sel, Etype (Comp)); | |
4365 | Set_Etype (N, Etype (Comp)); | |
4366 | return; | |
4367 | end if; | |
4368 | ||
4369 | Next_Component (Comp); | |
4370 | end loop; | |
4371 | ||
4372 | -- If not found, the component may be declared in the parent | |
4373 | -- type or its full view, if any. | |
4374 | ||
4375 | if Is_Derived_Type (Typ) then | |
4376 | Typ := Etype (Typ); | |
4377 | ||
4378 | if Is_Private_Type (Typ) then | |
4379 | Typ := Full_View (Typ); | |
4380 | end if; | |
4381 | ||
4382 | else | |
20261dc1 AC |
4383 | return; |
4384 | end if; | |
20261dc1 AC |
4385 | end loop; |
4386 | ||
cf3e6845 AC |
4387 | -- If we fall through, no match, so no changes made |
4388 | ||
4389 | return; | |
20261dc1 AC |
4390 | end Find_Component_In_Instance; |
4391 | ||
d469eabe HK |
4392 | ------------------------------ |
4393 | -- Has_Mode_Conformant_Spec -- | |
4394 | ------------------------------ | |
4395 | ||
4396 | function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is | |
4397 | Comp_Param : Entity_Id; | |
4398 | Param : Node_Id; | |
4399 | Param_Typ : Entity_Id; | |
4400 | ||
4401 | begin | |
4402 | Comp_Param := First_Formal (Comp); | |
b4592168 GD |
4403 | |
4404 | if Nkind (Parent (N)) = N_Indexed_Component then | |
4405 | Param := First (Expressions (Parent (N))); | |
4406 | else | |
4407 | Param := First (Parameter_Associations (Parent (N))); | |
4408 | end if; | |
4409 | ||
d469eabe HK |
4410 | while Present (Comp_Param) |
4411 | and then Present (Param) | |
4412 | loop | |
4413 | Param_Typ := Find_Parameter_Type (Param); | |
4414 | ||
4415 | if Present (Param_Typ) | |
4416 | and then | |
4417 | not Conforming_Types | |
4418 | (Etype (Comp_Param), Param_Typ, Mode_Conformant) | |
4419 | then | |
4420 | return False; | |
4421 | end if; | |
4422 | ||
4423 | Next_Formal (Comp_Param); | |
4424 | Next (Param); | |
4425 | end loop; | |
4426 | ||
9e92ad49 AC |
4427 | -- One of the specs has additional formals; there is no match, unless |
4428 | -- this may be an indexing of a parameterless call. | |
f0e7963f AC |
4429 | |
4430 | -- Note that when expansion is disabled, the corresponding record | |
4431 | -- type of synchronized types is not constructed, so that there is | |
4432 | -- no point is attempting an interpretation as a prefixed call, as | |
4433 | -- this is bound to fail because the primitive operations will not | |
4434 | -- be properly located. | |
d469eabe HK |
4435 | |
4436 | if Present (Comp_Param) or else Present (Param) then | |
f0e7963f AC |
4437 | if Needs_No_Actuals (Comp) |
4438 | and then Is_Array_Type (Etype (Comp)) | |
4439 | and then not Expander_Active | |
4440 | then | |
4441 | return True; | |
f0e7963f AC |
4442 | else |
4443 | return False; | |
4444 | end if; | |
d469eabe HK |
4445 | end if; |
4446 | ||
4447 | return True; | |
4448 | end Has_Mode_Conformant_Spec; | |
996ae0b0 | 4449 | |
bd717ec9 AC |
4450 | --------------------- |
4451 | -- Has_Dereference -- | |
4452 | --------------------- | |
4453 | ||
4454 | function Has_Dereference (Nod : Node_Id) return Boolean is | |
4455 | begin | |
4456 | if Nkind (Nod) = N_Explicit_Dereference then | |
4457 | return True; | |
4458 | ||
a6363ed3 AC |
4459 | -- When expansion is disabled an explicit dereference may not have |
4460 | -- been inserted, but if this is an access type the indirection makes | |
4461 | -- the call safe. | |
4462 | ||
4463 | elsif Is_Access_Type (Etype (Nod)) then | |
4464 | return True; | |
4465 | ||
bd717ec9 AC |
4466 | elsif Nkind_In (Nod, N_Indexed_Component, N_Selected_Component) then |
4467 | return Has_Dereference (Prefix (Nod)); | |
4468 | ||
4469 | else | |
4470 | return False; | |
4471 | end if; | |
4472 | end Has_Dereference; | |
4473 | ||
996ae0b0 RK |
4474 | -- Start of processing for Analyze_Selected_Component |
4475 | ||
4476 | begin | |
4477 | Set_Etype (N, Any_Type); | |
4478 | ||
4479 | if Is_Overloaded (Name) then | |
4480 | Analyze_Overloaded_Selected_Component (N); | |
4481 | return; | |
4482 | ||
4483 | elsif Etype (Name) = Any_Type then | |
4484 | Set_Entity (Sel, Any_Id); | |
4485 | Set_Etype (Sel, Any_Type); | |
4486 | return; | |
4487 | ||
4488 | else | |
996ae0b0 RK |
4489 | Prefix_Type := Etype (Name); |
4490 | end if; | |
4491 | ||
4492 | if Is_Access_Type (Prefix_Type) then | |
07fc65c4 | 4493 | |
0d57c6f4 RD |
4494 | -- A RACW object can never be used as prefix of a selected component |
4495 | -- since that means it is dereferenced without being a controlling | |
4496 | -- operand of a dispatching operation (RM E.2.2(16/1)). Before | |
4497 | -- reporting an error, we must check whether this is actually a | |
4498 | -- dispatching call in prefix form. | |
07fc65c4 | 4499 | |
996ae0b0 RK |
4500 | if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type) |
4501 | and then Comes_From_Source (N) | |
4502 | then | |
b4592168 GD |
4503 | if Try_Object_Operation (N) then |
4504 | return; | |
4505 | else | |
4506 | Error_Msg_N | |
4507 | ("invalid dereference of a remote access-to-class-wide value", | |
4508 | N); | |
4509 | end if; | |
07fc65c4 GB |
4510 | |
4511 | -- Normal case of selected component applied to access type | |
4512 | ||
4513 | else | |
324ac540 | 4514 | Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N); |
da709d08 | 4515 | |
6e73e3ab AC |
4516 | if Is_Entity_Name (Name) then |
4517 | Pent := Entity (Name); | |
4518 | elsif Nkind (Name) = N_Selected_Component | |
4519 | and then Is_Entity_Name (Selector_Name (Name)) | |
4520 | then | |
4521 | Pent := Entity (Selector_Name (Name)); | |
4522 | end if; | |
da709d08 | 4523 | |
d469eabe | 4524 | Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name); |
996ae0b0 | 4525 | end if; |
b4592168 GD |
4526 | |
4527 | -- If we have an explicit dereference of a remote access-to-class-wide | |
4528 | -- value, then issue an error (see RM-E.2.2(16/1)). However we first | |
4529 | -- have to check for the case of a prefix that is a controlling operand | |
4530 | -- of a prefixed dispatching call, as the dereference is legal in that | |
4531 | -- case. Normally this condition is checked in Validate_Remote_Access_ | |
4532 | -- To_Class_Wide_Type, but we have to defer the checking for selected | |
4533 | -- component prefixes because of the prefixed dispatching call case. | |
4534 | -- Note that implicit dereferences are checked for this just above. | |
4535 | ||
4536 | elsif Nkind (Name) = N_Explicit_Dereference | |
4537 | and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name))) | |
4538 | and then Comes_From_Source (N) | |
4539 | then | |
4540 | if Try_Object_Operation (N) then | |
4541 | return; | |
4542 | else | |
4543 | Error_Msg_N | |
4544 | ("invalid dereference of a remote access-to-class-wide value", | |
4545 | N); | |
4546 | end if; | |
aab883ec | 4547 | end if; |
b67a385c | 4548 | |
aab883ec ES |
4549 | -- (Ada 2005): if the prefix is the limited view of a type, and |
4550 | -- the context already includes the full view, use the full view | |
4551 | -- in what follows, either to retrieve a component of to find | |
4552 | -- a primitive operation. If the prefix is an explicit dereference, | |
4553 | -- set the type of the prefix to reflect this transformation. | |
401093c1 ES |
4554 | -- If the non-limited view is itself an incomplete type, get the |
4555 | -- full view if available. | |
aab883ec | 4556 | |
47346923 AC |
4557 | if From_Limited_With (Prefix_Type) |
4558 | and then Has_Non_Limited_View (Prefix_Type) | |
aab883ec | 4559 | then |
401093c1 | 4560 | Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type)); |
aab883ec ES |
4561 | |
4562 | if Nkind (N) = N_Explicit_Dereference then | |
4563 | Set_Etype (Prefix (N), Prefix_Type); | |
4564 | end if; | |
996ae0b0 RK |
4565 | end if; |
4566 | ||
4567 | if Ekind (Prefix_Type) = E_Private_Subtype then | |
4568 | Prefix_Type := Base_Type (Prefix_Type); | |
4569 | end if; | |
4570 | ||
401093c1 | 4571 | Type_To_Use := Prefix_Type; |
996ae0b0 RK |
4572 | |
4573 | -- For class-wide types, use the entity list of the root type. This | |
4574 | -- indirection is specially important for private extensions because | |
4575 | -- only the root type get switched (not the class-wide type). | |
4576 | ||
4577 | if Is_Class_Wide_Type (Prefix_Type) then | |
401093c1 | 4578 | Type_To_Use := Root_Type (Prefix_Type); |
996ae0b0 RK |
4579 | end if; |
4580 | ||
2383acbd AC |
4581 | -- If the prefix is a single concurrent object, use its name in error |
4582 | -- messages, rather than that of its anonymous type. | |
4583 | ||
4584 | Is_Single_Concurrent_Object := | |
4585 | Is_Concurrent_Type (Prefix_Type) | |
4586 | and then Is_Internal_Name (Chars (Prefix_Type)) | |
4587 | and then not Is_Derived_Type (Prefix_Type) | |
4588 | and then Is_Entity_Name (Name); | |
4589 | ||
401093c1 | 4590 | Comp := First_Entity (Type_To_Use); |
996ae0b0 RK |
4591 | |
4592 | -- If the selector has an original discriminant, the node appears in | |
4593 | -- an instance. Replace the discriminant with the corresponding one | |
4594 | -- in the current discriminated type. For nested generics, this must | |
4595 | -- be done transitively, so note the new original discriminant. | |
4596 | ||
4597 | if Nkind (Sel) = N_Identifier | |
c0b11850 | 4598 | and then In_Instance |
996ae0b0 RK |
4599 | and then Present (Original_Discriminant (Sel)) |
4600 | then | |
4601 | Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type); | |
4602 | ||
4603 | -- Mark entity before rewriting, for completeness and because | |
4604 | -- subsequent semantic checks might examine the original node. | |
4605 | ||
4606 | Set_Entity (Sel, Comp); | |
ee2ba856 | 4607 | Rewrite (Selector_Name (N), New_Occurrence_Of (Comp, Sloc (N))); |
996ae0b0 RK |
4608 | Set_Original_Discriminant (Selector_Name (N), Comp); |
4609 | Set_Etype (N, Etype (Comp)); | |
44a10091 | 4610 | Check_Implicit_Dereference (N, Etype (Comp)); |
996ae0b0 RK |
4611 | |
4612 | if Is_Access_Type (Etype (Name)) then | |
4613 | Insert_Explicit_Dereference (Name); | |
324ac540 | 4614 | Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N); |
996ae0b0 RK |
4615 | end if; |
4616 | ||
4617 | elsif Is_Record_Type (Prefix_Type) then | |
4618 | ||
ee2ba856 AC |
4619 | -- Find component with given name. In an instance, if the node is |
4620 | -- known as a prefixed call, do not examine components whose | |
4621 | -- visibility may be accidental. | |
996ae0b0 | 4622 | |
4913e24c | 4623 | while Present (Comp) and then not Is_Prefixed_Call (N) loop |
996ae0b0 | 4624 | if Chars (Comp) = Chars (Sel) |
a53c5613 | 4625 | and then Is_Visible_Component (Comp, N) |
996ae0b0 | 4626 | then |
e7ba564f | 4627 | Set_Entity_With_Checks (Sel, Comp); |
996ae0b0 RK |
4628 | Set_Etype (Sel, Etype (Comp)); |
4629 | ||
4630 | if Ekind (Comp) = E_Discriminant then | |
5d09245e | 4631 | if Is_Unchecked_Union (Base_Type (Prefix_Type)) then |
996ae0b0 | 4632 | Error_Msg_N |
02f58834 | 4633 | ("cannot reference discriminant of unchecked union", |
996ae0b0 RK |
4634 | Sel); |
4635 | end if; | |
4636 | ||
4637 | if Is_Generic_Type (Prefix_Type) | |
4638 | or else | |
4639 | Is_Generic_Type (Root_Type (Prefix_Type)) | |
4640 | then | |
4641 | Set_Original_Discriminant (Sel, Comp); | |
4642 | end if; | |
4643 | end if; | |
4644 | ||
4645 | -- Resolve the prefix early otherwise it is not possible to | |
4646 | -- build the actual subtype of the component: it may need | |
4647 | -- to duplicate this prefix and duplication is only allowed | |
4648 | -- on fully resolved expressions. | |
4649 | ||
fbf5a39b | 4650 | Resolve (Name); |
996ae0b0 | 4651 | |
b67a385c ES |
4652 | -- Ada 2005 (AI-50217): Check wrong use of incomplete types or |
4653 | -- subtypes in a package specification. | |
28be29ce ES |
4654 | -- Example: |
4655 | ||
4656 | -- limited with Pkg; | |
4657 | -- package Pkg is | |
4658 | -- type Acc_Inc is access Pkg.T; | |
4659 | -- X : Acc_Inc; | |
b67a385c ES |
4660 | -- N : Natural := X.all.Comp; -- ERROR, limited view |
4661 | -- end Pkg; -- Comp is not visible | |
28be29ce ES |
4662 | |
4663 | if Nkind (Name) = N_Explicit_Dereference | |
7b56a91b | 4664 | and then From_Limited_With (Etype (Prefix (Name))) |
28be29ce | 4665 | and then not Is_Potentially_Use_Visible (Etype (Name)) |
b67a385c ES |
4666 | and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) = |
4667 | N_Package_Specification | |
28be29ce ES |
4668 | then |
4669 | Error_Msg_NE | |
4670 | ("premature usage of incomplete}", Prefix (Name), | |
4671 | Etype (Prefix (Name))); | |
4672 | end if; | |
4673 | ||
996ae0b0 RK |
4674 | -- We never need an actual subtype for the case of a selection |
4675 | -- for a indexed component of a non-packed array, since in | |
4676 | -- this case gigi generates all the checks and can find the | |
4677 | -- necessary bounds information. | |
4678 | ||
0d57c6f4 RD |
4679 | -- We also do not need an actual subtype for the case of a |
4680 | -- first, last, length, or range attribute applied to a | |
996ae0b0 RK |
4681 | -- non-packed array, since gigi can again get the bounds in |
4682 | -- these cases (gigi cannot handle the packed case, since it | |
4683 | -- has the bounds of the packed array type, not the original | |
4684 | -- bounds of the type). However, if the prefix is itself a | |
4685 | -- selected component, as in a.b.c (i), gigi may regard a.b.c | |
4686 | -- as a dynamic-sized temporary, so we do generate an actual | |
4687 | -- subtype for this case. | |
4688 | ||
4689 | Parent_N := Parent (N); | |
4690 | ||
4691 | if not Is_Packed (Etype (Comp)) | |
4692 | and then | |
4693 | ((Nkind (Parent_N) = N_Indexed_Component | |
d469eabe | 4694 | and then Nkind (Name) /= N_Selected_Component) |
996ae0b0 RK |
4695 | or else |
4696 | (Nkind (Parent_N) = N_Attribute_Reference | |
b69cd36a AC |
4697 | and then |
4698 | Nam_In (Attribute_Name (Parent_N), Name_First, | |
4699 | Name_Last, | |
4700 | Name_Length, | |
4701 | Name_Range))) | |
996ae0b0 RK |
4702 | then |
4703 | Set_Etype (N, Etype (Comp)); | |
4704 | ||
98123480 ES |
4705 | -- If full analysis is not enabled, we do not generate an |
4706 | -- actual subtype, because in the absence of expansion | |
4707 | -- reference to a formal of a protected type, for example, | |
4708 | -- will not be properly transformed, and will lead to | |
4709 | -- out-of-scope references in gigi. | |
4710 | ||
4711 | -- In all other cases, we currently build an actual subtype. | |
4712 | -- It seems likely that many of these cases can be avoided, | |
4713 | -- but right now, the front end makes direct references to the | |
fbf5a39b | 4714 | -- bounds (e.g. in generating a length check), and if we do |
996ae0b0 | 4715 | -- not make an actual subtype, we end up getting a direct |
98123480 | 4716 | -- reference to a discriminant, which will not do. |
996ae0b0 | 4717 | |
98123480 | 4718 | elsif Full_Analysis then |
996ae0b0 RK |
4719 | Act_Decl := |
4720 | Build_Actual_Subtype_Of_Component (Etype (Comp), N); | |
4721 | Insert_Action (N, Act_Decl); | |
4722 | ||
4723 | if No (Act_Decl) then | |
4724 | Set_Etype (N, Etype (Comp)); | |
4725 | ||
4726 | else | |
4727 | -- Component type depends on discriminants. Enter the | |
4728 | -- main attributes of the subtype. | |
4729 | ||
4730 | declare | |
fbf5a39b AC |
4731 | Subt : constant Entity_Id := |
4732 | Defining_Identifier (Act_Decl); | |
996ae0b0 RK |
4733 | |
4734 | begin | |
4735 | Set_Etype (Subt, Base_Type (Etype (Comp))); | |
4736 | Set_Ekind (Subt, Ekind (Etype (Comp))); | |
4737 | Set_Etype (N, Subt); | |
4738 | end; | |
4739 | end if; | |
98123480 ES |
4740 | |
4741 | -- If Full_Analysis not enabled, just set the Etype | |
4742 | ||
4743 | else | |
4744 | Set_Etype (N, Etype (Comp)); | |
996ae0b0 RK |
4745 | end if; |
4746 | ||
44a10091 | 4747 | Check_Implicit_Dereference (N, Etype (N)); |
996ae0b0 RK |
4748 | return; |
4749 | end if; | |
4750 | ||
aab883ec | 4751 | -- If the prefix is a private extension, check only the visible |
9c510803 | 4752 | -- components of the partial view. This must include the tag, |
f3d57416 | 4753 | -- which can appear in expanded code in a tag check. |
aab883ec | 4754 | |
9c510803 | 4755 | if Ekind (Type_To_Use) = E_Record_Type_With_Private |
df3e68b1 | 4756 | and then Chars (Selector_Name (N)) /= Name_uTag |
9c510803 | 4757 | then |
401093c1 | 4758 | exit when Comp = Last_Entity (Type_To_Use); |
aab883ec ES |
4759 | end if; |
4760 | ||
996ae0b0 RK |
4761 | Next_Entity (Comp); |
4762 | end loop; | |
4763 | ||
d469eabe HK |
4764 | -- Ada 2005 (AI-252): The selected component can be interpreted as |
4765 | -- a prefixed view of a subprogram. Depending on the context, this is | |
4766 | -- either a name that can appear in a renaming declaration, or part | |
4767 | -- of an enclosing call given in prefix form. | |
4768 | ||
4769 | -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the | |
4770 | -- selected component should resolve to a name. | |
35ae2ed8 | 4771 | |
0791fbe9 | 4772 | if Ada_Version >= Ada_2005 |
35ae2ed8 | 4773 | and then Is_Tagged_Type (Prefix_Type) |
d469eabe | 4774 | and then not Is_Concurrent_Type (Prefix_Type) |
35ae2ed8 | 4775 | then |
d469eabe HK |
4776 | if Nkind (Parent (N)) = N_Generic_Association |
4777 | or else Nkind (Parent (N)) = N_Requeue_Statement | |
4778 | or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration | |
4779 | then | |
4780 | if Find_Primitive_Operation (N) then | |
4781 | return; | |
4782 | end if; | |
4783 | ||
4784 | elsif Try_Object_Operation (N) then | |
4785 | return; | |
4786 | end if; | |
4c46b835 | 4787 | |
98123480 ES |
4788 | -- If the transformation fails, it will be necessary to redo the |
4789 | -- analysis with all errors enabled, to indicate candidate | |
4790 | -- interpretations and reasons for each failure ??? | |
4c46b835 | 4791 | |
35ae2ed8 AC |
4792 | end if; |
4793 | ||
996ae0b0 | 4794 | elsif Is_Private_Type (Prefix_Type) then |
d469eabe | 4795 | |
98123480 ES |
4796 | -- Allow access only to discriminants of the type. If the type has |
4797 | -- no full view, gigi uses the parent type for the components, so we | |
4798 | -- do the same here. | |
996ae0b0 RK |
4799 | |
4800 | if No (Full_View (Prefix_Type)) then | |
401093c1 ES |
4801 | Type_To_Use := Root_Type (Base_Type (Prefix_Type)); |
4802 | Comp := First_Entity (Type_To_Use); | |
996ae0b0 RK |
4803 | end if; |
4804 | ||
4805 | while Present (Comp) loop | |
996ae0b0 RK |
4806 | if Chars (Comp) = Chars (Sel) then |
4807 | if Ekind (Comp) = E_Discriminant then | |
e7ba564f | 4808 | Set_Entity_With_Checks (Sel, Comp); |
996ae0b0 RK |
4809 | Generate_Reference (Comp, Sel); |
4810 | ||
4811 | Set_Etype (Sel, Etype (Comp)); | |
4812 | Set_Etype (N, Etype (Comp)); | |
44a10091 | 4813 | Check_Implicit_Dereference (N, Etype (N)); |
996ae0b0 RK |
4814 | |
4815 | if Is_Generic_Type (Prefix_Type) | |
d469eabe | 4816 | or else Is_Generic_Type (Root_Type (Prefix_Type)) |
996ae0b0 RK |
4817 | then |
4818 | Set_Original_Discriminant (Sel, Comp); | |
4819 | end if; | |
4820 | ||
f3d57416 | 4821 | -- Before declaring an error, check whether this is tagged |
aab883ec ES |
4822 | -- private type and a call to a primitive operation. |
4823 | ||
0791fbe9 | 4824 | elsif Ada_Version >= Ada_2005 |
aab883ec ES |
4825 | and then Is_Tagged_Type (Prefix_Type) |
4826 | and then Try_Object_Operation (N) | |
4827 | then | |
4828 | return; | |
4829 | ||
996ae0b0 | 4830 | else |
2383acbd AC |
4831 | Error_Msg_Node_2 := First_Subtype (Prefix_Type); |
4832 | Error_Msg_NE ("invisible selector& for }", N, Sel); | |
996ae0b0 RK |
4833 | Set_Entity (Sel, Any_Id); |
4834 | Set_Etype (N, Any_Type); | |
4835 | end if; | |
4836 | ||
4837 | return; | |
4838 | end if; | |
4839 | ||
4840 | Next_Entity (Comp); | |
4841 | end loop; | |
4842 | ||
4843 | elsif Is_Concurrent_Type (Prefix_Type) then | |
4844 | ||
d469eabe HK |
4845 | -- Find visible operation with given name. For a protected type, |
4846 | -- the possible candidates are discriminants, entries or protected | |
d1eb8a82 | 4847 | -- subprograms. For a task type, the set can only include entries or |
d469eabe HK |
4848 | -- discriminants if the task type is not an enclosing scope. If it |
4849 | -- is an enclosing scope (e.g. in an inner task) then all entities | |
4850 | -- are visible, but the prefix must denote the enclosing scope, i.e. | |
4851 | -- can only be a direct name or an expanded name. | |
996ae0b0 | 4852 | |
d469eabe | 4853 | Set_Etype (Sel, Any_Type); |
c0e938d0 | 4854 | Hidden_Comp := Empty; |
996ae0b0 | 4855 | In_Scope := In_Open_Scopes (Prefix_Type); |
d1eb8a82 | 4856 | Is_Private_Op := False; |
996ae0b0 RK |
4857 | |
4858 | while Present (Comp) loop | |
f31dcd99 | 4859 | |
86ec3bfb AC |
4860 | -- Do not examine private operations of the type if not within |
4861 | -- its scope. | |
4862 | ||
996ae0b0 | 4863 | if Chars (Comp) = Chars (Sel) then |
86ec3bfb AC |
4864 | if Is_Overloadable (Comp) |
4865 | and then (In_Scope | |
4866 | or else Comp /= First_Private_Entity (Type_To_Use)) | |
4867 | then | |
996ae0b0 | 4868 | Add_One_Interp (Sel, Comp, Etype (Comp)); |
d1eb8a82 AC |
4869 | if Comp = First_Private_Entity (Type_To_Use) then |
4870 | Is_Private_Op := True; | |
4871 | end if; | |
996ae0b0 | 4872 | |
d469eabe HK |
4873 | -- If the prefix is tagged, the correct interpretation may |
4874 | -- lie in the primitive or class-wide operations of the | |
4875 | -- type. Perform a simple conformance check to determine | |
4876 | -- whether Try_Object_Operation should be invoked even if | |
4877 | -- a visible entity is found. | |
4878 | ||
4879 | if Is_Tagged_Type (Prefix_Type) | |
f31dcd99 HK |
4880 | and then Nkind_In (Parent (N), N_Function_Call, |
4881 | N_Indexed_Component, | |
4882 | N_Procedure_Call_Statement) | |
d469eabe HK |
4883 | and then Has_Mode_Conformant_Spec (Comp) |
4884 | then | |
4885 | Has_Candidate := True; | |
4886 | end if; | |
4887 | ||
2383acbd AC |
4888 | -- Note: a selected component may not denote a component of a |
4889 | -- protected type (4.1.3(7)). | |
4890 | ||
bce79204 | 4891 | elsif Ekind_In (Comp, E_Discriminant, E_Entry_Family) |
2383acbd AC |
4892 | or else (In_Scope |
4893 | and then not Is_Protected_Type (Prefix_Type) | |
4894 | and then Is_Entity_Name (Name)) | |
996ae0b0 | 4895 | then |
e7ba564f | 4896 | Set_Entity_With_Checks (Sel, Comp); |
996ae0b0 RK |
4897 | Generate_Reference (Comp, Sel); |
4898 | ||
65e78a74 AC |
4899 | -- The selector is not overloadable, so we have a candidate |
4900 | -- interpretation. | |
4901 | ||
4902 | Has_Candidate := True; | |
4903 | ||
996ae0b0 | 4904 | else |
c0e938d0 AC |
4905 | if Ekind (Comp) = E_Component then |
4906 | Hidden_Comp := Comp; | |
4907 | end if; | |
4908 | ||
996ae0b0 RK |
4909 | goto Next_Comp; |
4910 | end if; | |
4911 | ||
4912 | Set_Etype (Sel, Etype (Comp)); | |
4913 | Set_Etype (N, Etype (Comp)); | |
4914 | ||
4915 | if Ekind (Comp) = E_Discriminant then | |
4916 | Set_Original_Discriminant (Sel, Comp); | |
4917 | end if; | |
4918 | ||
09494c32 AC |
4919 | -- For access type case, introduce explicit dereference for |
4920 | -- more uniform treatment of entry calls. | |
996ae0b0 RK |
4921 | |
4922 | if Is_Access_Type (Etype (Name)) then | |
4923 | Insert_Explicit_Dereference (Name); | |
fbf5a39b | 4924 | Error_Msg_NW |
324ac540 | 4925 | (Warn_On_Dereference, "?d?implicit dereference", N); |
996ae0b0 RK |
4926 | end if; |
4927 | end if; | |
4928 | ||
4929 | <<Next_Comp>> | |
c0e938d0 AC |
4930 | if Comp = First_Private_Entity (Type_To_Use) then |
4931 | if Etype (Sel) /= Any_Type then | |
4932 | ||
c5b4738f AC |
4933 | -- We have a candiate |
4934 | ||
c0e938d0 AC |
4935 | exit; |
4936 | ||
4937 | else | |
4938 | -- Indicate that subsequent operations are private, | |
4939 | -- for better error reporting. | |
4940 | ||
4941 | Is_Private_Op := True; | |
4942 | end if; | |
4943 | end if; | |
4944 | ||
996ae0b0 RK |
4945 | Next_Entity (Comp); |
4946 | exit when not In_Scope | |
9bc856dd AC |
4947 | and then |
4948 | Comp = First_Private_Entity (Base_Type (Prefix_Type)); | |
996ae0b0 RK |
4949 | end loop; |
4950 | ||
b3083540 | 4951 | -- If the scope is a current instance, the prefix cannot be an |
0f6251c7 AC |
4952 | -- expression of the same type, unless the selector designates a |
4953 | -- public operation (otherwise that would represent an attempt to | |
4954 | -- reach an internal entity of another synchronized object). | |
be3416c6 | 4955 | |
b3083540 | 4956 | -- This is legal if prefix is an access to such type and there is |
0f6251c7 | 4957 | -- a dereference, or is a component with a dereferenced prefix. |
0f8b3e5d AC |
4958 | -- It is also legal if the prefix is a component of a task type, |
4959 | -- and the selector is one of the task operations. | |
b3083540 | 4960 | |
bd717ec9 AC |
4961 | if In_Scope |
4962 | and then not Is_Entity_Name (Name) | |
4963 | and then not Has_Dereference (Name) | |
4964 | then | |
0f8b3e5d AC |
4965 | if Is_Task_Type (Prefix_Type) |
4966 | and then Present (Entity (Sel)) | |
4967 | and then Ekind_In (Entity (Sel), E_Entry, E_Entry_Family) | |
4968 | then | |
4969 | null; | |
4970 | ||
d1eb8a82 AC |
4971 | elsif Is_Protected_Type (Prefix_Type) |
4972 | and then Is_Overloadable (Entity (Sel)) | |
4973 | and then not Is_Private_Op | |
4974 | then | |
4975 | null; | |
4976 | ||
0f8b3e5d AC |
4977 | else |
4978 | Error_Msg_NE | |
4979 | ("invalid reference to internal operation of some object of " | |
4980 | & "type &", N, Type_To_Use); | |
4981 | Set_Entity (Sel, Any_Id); | |
4982 | Set_Etype (Sel, Any_Type); | |
4983 | return; | |
4984 | end if; | |
be3416c6 AC |
4985 | |
4986 | -- Another special case: the prefix may denote an object of the type | |
4987 | -- (but not a type) in which case this is an external call and the | |
4988 | -- operation must be public. | |
4989 | ||
4990 | elsif In_Scope | |
4991 | and then Is_Object_Reference (Original_Node (Prefix (N))) | |
c0e938d0 | 4992 | and then Comes_From_Source (N) |
be3416c6 AC |
4993 | and then Is_Private_Op |
4994 | then | |
c0e938d0 AC |
4995 | if Present (Hidden_Comp) then |
4996 | Error_Msg_NE | |
c5b4738f AC |
4997 | ("invalid reference to private component of object of type " |
4998 | & "&", N, Type_To_Use); | |
c0e938d0 AC |
4999 | |
5000 | else | |
5001 | Error_Msg_NE | |
5002 | ("invalid reference to private operation of some object of " | |
5003 | & "type &", N, Type_To_Use); | |
5004 | end if; | |
5005 | ||
be3416c6 AC |
5006 | Set_Entity (Sel, Any_Id); |
5007 | Set_Etype (Sel, Any_Type); | |
5008 | return; | |
b3083540 AC |
5009 | end if; |
5010 | ||
d469eabe HK |
5011 | -- If there is no visible entity with the given name or none of the |
5012 | -- visible entities are plausible interpretations, check whether | |
5013 | -- there is some other primitive operation with that name. | |
aab883ec | 5014 | |
bc38dbb4 | 5015 | if Ada_Version >= Ada_2005 and then Is_Tagged_Type (Prefix_Type) then |
d469eabe HK |
5016 | if (Etype (N) = Any_Type |
5017 | or else not Has_Candidate) | |
0a36105d JM |
5018 | and then Try_Object_Operation (N) |
5019 | then | |
5020 | return; | |
5021 | ||
5022 | -- If the context is not syntactically a procedure call, it | |
5023 | -- may be a call to a primitive function declared outside of | |
5024 | -- the synchronized type. | |
5025 | ||
5026 | -- If the context is a procedure call, there might still be | |
5027 | -- an overloading between an entry and a primitive procedure | |
5028 | -- declared outside of the synchronized type, called in prefix | |
5029 | -- notation. This is harder to disambiguate because in one case | |
5030 | -- the controlling formal is implicit ??? | |
5031 | ||
5032 | elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement | |
b4592168 | 5033 | and then Nkind (Parent (N)) /= N_Indexed_Component |
0a36105d JM |
5034 | and then Try_Object_Operation (N) |
5035 | then | |
5036 | return; | |
5037 | end if; | |
8cf23b91 AC |
5038 | |
5039 | -- Ada 2012 (AI05-0090-1): If we found a candidate of a call to an | |
5040 | -- entry or procedure of a tagged concurrent type we must check | |
5041 | -- if there are class-wide subprograms covering the primitive. If | |
5042 | -- true then Try_Object_Operation reports the error. | |
5043 | ||
5044 | if Has_Candidate | |
5045 | and then Is_Concurrent_Type (Prefix_Type) | |
5046 | and then Nkind (Parent (N)) = N_Procedure_Call_Statement | |
bc38dbb4 | 5047 | then |
8cf23b91 AC |
5048 | -- Duplicate the call. This is required to avoid problems with |
5049 | -- the tree transformations performed by Try_Object_Operation. | |
d7a44b14 AC |
5050 | -- Set properly the parent of the copied call, because it is |
5051 | -- about to be reanalyzed. | |
8cf23b91 | 5052 | |
d7a44b14 AC |
5053 | declare |
5054 | Par : constant Node_Id := New_Copy_Tree (Parent (N)); | |
5055 | ||
5056 | begin | |
5057 | Set_Parent (Par, Parent (Parent (N))); | |
29ba9f52 | 5058 | |
d7a44b14 | 5059 | if Try_Object_Operation |
29ba9f52 | 5060 | (Sinfo.Name (Par), CW_Test_Only => True) |
d7a44b14 AC |
5061 | then |
5062 | return; | |
5063 | end if; | |
5064 | end; | |
8cf23b91 | 5065 | end if; |
aab883ec ES |
5066 | end if; |
5067 | ||
2383acbd | 5068 | if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then |
11bc76df | 5069 | |
2383acbd AC |
5070 | -- Case of a prefix of a protected type: selector might denote |
5071 | -- an invisible private component. | |
5072 | ||
5073 | Comp := First_Private_Entity (Base_Type (Prefix_Type)); | |
5074 | while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop | |
5075 | Next_Entity (Comp); | |
5076 | end loop; | |
5077 | ||
5078 | if Present (Comp) then | |
5079 | if Is_Single_Concurrent_Object then | |
5080 | Error_Msg_Node_2 := Entity (Name); | |
5081 | Error_Msg_NE ("invisible selector& for &", N, Sel); | |
5082 | ||
5083 | else | |
5084 | Error_Msg_Node_2 := First_Subtype (Prefix_Type); | |
5085 | Error_Msg_NE ("invisible selector& for }", N, Sel); | |
5086 | end if; | |
5087 | return; | |
5088 | end if; | |
5089 | end if; | |
5090 | ||
996ae0b0 RK |
5091 | Set_Is_Overloaded (N, Is_Overloaded (Sel)); |
5092 | ||
5093 | else | |
5094 | -- Invalid prefix | |
5095 | ||
5096 | Error_Msg_NE ("invalid prefix in selected component&", N, Sel); | |
5097 | end if; | |
5098 | ||
4c46b835 | 5099 | -- If N still has no type, the component is not defined in the prefix |
996ae0b0 RK |
5100 | |
5101 | if Etype (N) = Any_Type then | |
5102 | ||
2383acbd | 5103 | if Is_Single_Concurrent_Object then |
996ae0b0 RK |
5104 | Error_Msg_Node_2 := Entity (Name); |
5105 | Error_Msg_NE ("no selector& for&", N, Sel); | |
5106 | ||
401093c1 | 5107 | Check_Misspelled_Selector (Type_To_Use, Sel); |
996ae0b0 | 5108 | |
8b4230c8 AC |
5109 | -- If this is a derived formal type, the parent may have different |
5110 | -- visibility at this point. Try for an inherited component before | |
5111 | -- reporting an error. | |
5112 | ||
de76a39c GB |
5113 | elsif Is_Generic_Type (Prefix_Type) |
5114 | and then Ekind (Prefix_Type) = E_Record_Type_With_Private | |
07fc65c4 | 5115 | and then Prefix_Type /= Etype (Prefix_Type) |
de76a39c GB |
5116 | and then Is_Record_Type (Etype (Prefix_Type)) |
5117 | then | |
de76a39c GB |
5118 | Set_Etype (Prefix (N), Etype (Prefix_Type)); |
5119 | Analyze_Selected_Component (N); | |
5120 | return; | |
5121 | ||
b1d12996 AC |
5122 | -- Similarly, if this is the actual for a formal derived type, or |
5123 | -- a derived type thereof, the component inherited from the generic | |
5124 | -- parent may not be visible in the actual, but the selected | |
5125 | -- component is legal. Climb up the derivation chain of the generic | |
5126 | -- parent type until we find the proper ancestor type. | |
20261dc1 | 5127 | |
b1d12996 AC |
5128 | elsif In_Instance and then Is_Tagged_Type (Prefix_Type) then |
5129 | declare | |
5130 | Par : Entity_Id := Prefix_Type; | |
5131 | begin | |
5132 | -- Climb up derivation chain to generic actual subtype | |
5133 | ||
5134 | while not Is_Generic_Actual_Type (Par) loop | |
5135 | if Ekind (Par) = E_Record_Type then | |
5136 | Par := Parent_Subtype (Par); | |
5137 | exit when No (Par); | |
5138 | else | |
5139 | exit when Par = Etype (Par); | |
5140 | Par := Etype (Par); | |
5141 | end if; | |
5142 | end loop; | |
4c46b835 | 5143 | |
b1d12996 | 5144 | if Present (Par) and then Is_Generic_Actual_Type (Par) then |
73999267 | 5145 | |
b1d12996 | 5146 | -- Now look for component in ancestor types |
fbf5a39b | 5147 | |
b1d12996 AC |
5148 | Par := Generic_Parent_Type (Declaration_Node (Par)); |
5149 | loop | |
5150 | Find_Component_In_Instance (Par); | |
5151 | exit when Present (Entity (Sel)) | |
5152 | or else Par = Etype (Par); | |
5153 | Par := Etype (Par); | |
5154 | end loop; | |
73999267 | 5155 | |
7d9880c9 AC |
5156 | -- Another special case: the type is an extension of a private |
5157 | -- type T, is an actual in an instance, and we are in the body | |
5158 | -- of the instance, so the generic body had a full view of the | |
5159 | -- type declaration for T or of some ancestor that defines the | |
5160 | -- component in question. | |
5161 | ||
5162 | elsif Is_Derived_Type (Type_To_Use) | |
5163 | and then Used_As_Generic_Actual (Type_To_Use) | |
5164 | and then In_Instance_Body | |
5165 | then | |
5166 | Find_Component_In_Instance (Parent_Subtype (Type_To_Use)); | |
5167 | ||
73999267 AC |
5168 | -- In ASIS mode the generic parent type may be absent. Examine |
5169 | -- the parent type directly for a component that may have been | |
5170 | -- visible in a parent generic unit. | |
5171 | ||
5172 | elsif Is_Derived_Type (Prefix_Type) then | |
5173 | Par := Etype (Prefix_Type); | |
5174 | Find_Component_In_Instance (Par); | |
b1d12996 AC |
5175 | end if; |
5176 | end; | |
5177 | ||
5178 | -- The search above must have eventually succeeded, since the | |
5179 | -- selected component was legal in the generic. | |
5180 | ||
5181 | if No (Entity (Sel)) then | |
5182 | raise Program_Error; | |
5183 | end if; | |
73999267 | 5184 | |
20261dc1 | 5185 | return; |
fbf5a39b | 5186 | |
20261dc1 | 5187 | -- Component not found, specialize error message when appropriate |
fbf5a39b | 5188 | |
996ae0b0 RK |
5189 | else |
5190 | if Ekind (Prefix_Type) = E_Record_Subtype then | |
5191 | ||
f4b049db AC |
5192 | -- Check whether this is a component of the base type which |
5193 | -- is absent from a statically constrained subtype. This will | |
5194 | -- raise constraint error at run time, but is not a compile- | |
5195 | -- time error. When the selector is illegal for base type as | |
5196 | -- well fall through and generate a compilation error anyway. | |
996ae0b0 RK |
5197 | |
5198 | Comp := First_Component (Base_Type (Prefix_Type)); | |
996ae0b0 | 5199 | while Present (Comp) loop |
996ae0b0 RK |
5200 | if Chars (Comp) = Chars (Sel) |
5201 | and then Is_Visible_Component (Comp) | |
5202 | then | |
e7ba564f | 5203 | Set_Entity_With_Checks (Sel, Comp); |
996ae0b0 RK |
5204 | Generate_Reference (Comp, Sel); |
5205 | Set_Etype (Sel, Etype (Comp)); | |
5206 | Set_Etype (N, Etype (Comp)); | |
5207 | ||
637a41a5 AC |
5208 | -- Emit appropriate message. The node will be replaced |
5209 | -- by an appropriate raise statement. | |
996ae0b0 | 5210 | |
637a41a5 AC |
5211 | -- Note that in SPARK mode, as with all calls to apply a |
5212 | -- compile time constraint error, this will be made into | |
5213 | -- an error to simplify the processing of the formal | |
5214 | -- verification backend. | |
d7f41b2d | 5215 | |
4a28b181 | 5216 | Apply_Compile_Time_Constraint_Error |
637a41a5 | 5217 | (N, "component not present in }??", |
4a28b181 AC |
5218 | CE_Discriminant_Check_Failed, |
5219 | Ent => Prefix_Type, Rep => False); | |
d7f41b2d | 5220 | |
996ae0b0 RK |
5221 | Set_Raises_Constraint_Error (N); |
5222 | return; | |
5223 | end if; | |
5224 | ||
5225 | Next_Component (Comp); | |
5226 | end loop; | |
5227 | ||
5228 | end if; | |
5229 | ||
5230 | Error_Msg_Node_2 := First_Subtype (Prefix_Type); | |
5231 | Error_Msg_NE ("no selector& for}", N, Sel); | |
5232 | ||
94bbf008 | 5233 | -- Add information in the case of an incomplete prefix |
99d520ad ES |
5234 | |
5235 | if Is_Incomplete_Type (Type_To_Use) then | |
5236 | declare | |
5237 | Inc : constant Entity_Id := First_Subtype (Type_To_Use); | |
5238 | ||
5239 | begin | |
7b56a91b | 5240 | if From_Limited_With (Scope (Type_To_Use)) then |
99d520ad ES |
5241 | Error_Msg_NE |
5242 | ("\limited view of& has no components", N, Inc); | |
bd38b431 | 5243 | |
99d520ad ES |
5244 | else |
5245 | Error_Msg_NE | |
5246 | ("\premature usage of incomplete type&", N, Inc); | |
bd38b431 AC |
5247 | |
5248 | if Nkind (Parent (Inc)) = | |
5249 | N_Incomplete_Type_Declaration | |
99d520ad | 5250 | then |
94bbf008 AC |
5251 | -- Record location of premature use in entity so that |
5252 | -- a continuation message is generated when the | |
5253 | -- completion is seen. | |
5254 | ||
99d520ad ES |
5255 | Set_Premature_Use (Parent (Inc), N); |
5256 | end if; | |
5257 | end if; | |
5258 | end; | |
5259 | end if; | |
5260 | ||
401093c1 | 5261 | Check_Misspelled_Selector (Type_To_Use, Sel); |
996ae0b0 RK |
5262 | end if; |
5263 | ||
5264 | Set_Entity (Sel, Any_Id); | |
5265 | Set_Etype (Sel, Any_Type); | |
5266 | end if; | |
5267 | end Analyze_Selected_Component; | |
5268 | ||
5269 | --------------------------- | |
5270 | -- Analyze_Short_Circuit -- | |
5271 | --------------------------- | |
5272 | ||
5273 | procedure Analyze_Short_Circuit (N : Node_Id) is | |
5274 | L : constant Node_Id := Left_Opnd (N); | |
5275 | R : constant Node_Id := Right_Opnd (N); | |
5276 | Ind : Interp_Index; | |
5277 | It : Interp; | |
5278 | ||
5279 | begin | |
5280 | Analyze_Expression (L); | |
5281 | Analyze_Expression (R); | |
5282 | Set_Etype (N, Any_Type); | |
5283 | ||
5284 | if not Is_Overloaded (L) then | |
996ae0b0 RK |
5285 | if Root_Type (Etype (L)) = Standard_Boolean |
5286 | and then Has_Compatible_Type (R, Etype (L)) | |
5287 | then | |
5288 | Add_One_Interp (N, Etype (L), Etype (L)); | |
5289 | end if; | |
5290 | ||
5291 | else | |
5292 | Get_First_Interp (L, Ind, It); | |
996ae0b0 RK |
5293 | while Present (It.Typ) loop |
5294 | if Root_Type (It.Typ) = Standard_Boolean | |
5295 | and then Has_Compatible_Type (R, It.Typ) | |
5296 | then | |
5297 | Add_One_Interp (N, It.Typ, It.Typ); | |
5298 | end if; | |
5299 | ||
5300 | Get_Next_Interp (Ind, It); | |
5301 | end loop; | |
5302 | end if; | |
5303 | ||
d469eabe HK |
5304 | -- Here we have failed to find an interpretation. Clearly we know that |
5305 | -- it is not the case that both operands can have an interpretation of | |
5306 | -- Boolean, but this is by far the most likely intended interpretation. | |
5307 | -- So we simply resolve both operands as Booleans, and at least one of | |
5308 | -- these resolutions will generate an error message, and we do not need | |
5309 | -- to give another error message on the short circuit operation itself. | |
996ae0b0 RK |
5310 | |
5311 | if Etype (N) = Any_Type then | |
5312 | Resolve (L, Standard_Boolean); | |
5313 | Resolve (R, Standard_Boolean); | |
5314 | Set_Etype (N, Standard_Boolean); | |
5315 | end if; | |
5316 | end Analyze_Short_Circuit; | |
5317 | ||
5318 | ------------------- | |
5319 | -- Analyze_Slice -- | |
5320 | ------------------- | |
5321 | ||
5322 | procedure Analyze_Slice (N : Node_Id) is | |
996ae0b0 | 5323 | D : constant Node_Id := Discrete_Range (N); |
5f44f0d4 | 5324 | P : constant Node_Id := Prefix (N); |
996ae0b0 | 5325 | Array_Type : Entity_Id; |
5f44f0d4 | 5326 | Index_Type : Entity_Id; |
996ae0b0 RK |
5327 | |
5328 | procedure Analyze_Overloaded_Slice; | |
5329 | -- If the prefix is overloaded, select those interpretations that | |
5330 | -- yield a one-dimensional array type. | |
5331 | ||
4c46b835 AC |
5332 | ------------------------------ |
5333 | -- Analyze_Overloaded_Slice -- | |
5334 | ------------------------------ | |
5335 | ||
996ae0b0 RK |
5336 | procedure Analyze_Overloaded_Slice is |
5337 | I : Interp_Index; | |
5338 | It : Interp; | |
5339 | Typ : Entity_Id; | |
5340 | ||
5341 | begin | |
5342 | Set_Etype (N, Any_Type); | |
996ae0b0 | 5343 | |
4c46b835 | 5344 | Get_First_Interp (P, I, It); |
996ae0b0 RK |
5345 | while Present (It.Nam) loop |
5346 | Typ := It.Typ; | |
5347 | ||
5348 | if Is_Access_Type (Typ) then | |
5349 | Typ := Designated_Type (Typ); | |
324ac540 AC |
5350 | Error_Msg_NW |
5351 | (Warn_On_Dereference, "?d?implicit dereference", N); | |
996ae0b0 RK |
5352 | end if; |
5353 | ||
5354 | if Is_Array_Type (Typ) | |
5355 | and then Number_Dimensions (Typ) = 1 | |
5356 | and then Has_Compatible_Type (D, Etype (First_Index (Typ))) | |
5357 | then | |
5358 | Add_One_Interp (N, Typ, Typ); | |
5359 | end if; | |
5360 | ||
5361 | Get_Next_Interp (I, It); | |
5362 | end loop; | |
5363 | ||
5364 | if Etype (N) = Any_Type then | |
5365 | Error_Msg_N ("expect array type in prefix of slice", N); | |
5366 | end if; | |
5367 | end Analyze_Overloaded_Slice; | |
5368 | ||
5369 | -- Start of processing for Analyze_Slice | |
5370 | ||
5371 | begin | |
36b8f95f | 5372 | if Comes_From_Source (N) then |
ce5ba43a | 5373 | Check_SPARK_05_Restriction ("slice is not allowed", N); |
36b8f95f | 5374 | end if; |
1d801f21 | 5375 | |
523456db | 5376 | Analyze (P); |
996ae0b0 RK |
5377 | Analyze (D); |
5378 | ||
5379 | if Is_Overloaded (P) then | |
5380 | Analyze_Overloaded_Slice; | |
5381 | ||
5382 | else | |
5383 | Array_Type := Etype (P); | |
5384 | Set_Etype (N, Any_Type); | |
5385 | ||
5386 | if Is_Access_Type (Array_Type) then | |
5387 | Array_Type := Designated_Type (Array_Type); | |
324ac540 | 5388 | Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N); |
996ae0b0 RK |
5389 | end if; |
5390 | ||
5391 | if not Is_Array_Type (Array_Type) then | |
5392 | Wrong_Type (P, Any_Array); | |
5393 | ||
5394 | elsif Number_Dimensions (Array_Type) > 1 then | |
5395 | Error_Msg_N | |
5396 | ("type is not one-dimensional array in slice prefix", N); | |
5397 | ||
996ae0b0 | 5398 | else |
5f44f0d4 AC |
5399 | if Ekind (Array_Type) = E_String_Literal_Subtype then |
5400 | Index_Type := Etype (String_Literal_Low_Bound (Array_Type)); | |
5401 | else | |
5402 | Index_Type := Etype (First_Index (Array_Type)); | |
5403 | end if; | |
5404 | ||
5405 | if not Has_Compatible_Type (D, Index_Type) then | |
5406 | Wrong_Type (D, Index_Type); | |
5407 | else | |
5408 | Set_Etype (N, Array_Type); | |
5409 | end if; | |
996ae0b0 RK |
5410 | end if; |
5411 | end if; | |
5412 | end Analyze_Slice; | |
5413 | ||
5414 | ----------------------------- | |
5415 | -- Analyze_Type_Conversion -- | |
5416 | ----------------------------- | |
5417 | ||
5418 | procedure Analyze_Type_Conversion (N : Node_Id) is | |
5419 | Expr : constant Node_Id := Expression (N); | |
039538bc | 5420 | Typ : Entity_Id; |
996ae0b0 RK |
5421 | |
5422 | begin | |
039538bc AC |
5423 | -- If Conversion_OK is set, then the Etype is already set, and the only |
5424 | -- processing required is to analyze the expression. This is used to | |
5425 | -- construct certain "illegal" conversions which are not allowed by Ada | |
5426 | -- semantics, but can be handled by Gigi, see Sinfo for further details. | |
996ae0b0 RK |
5427 | |
5428 | if Conversion_OK (N) then | |
5429 | Analyze (Expr); | |
5430 | return; | |
5431 | end if; | |
5432 | ||
5433 | -- Otherwise full type analysis is required, as well as some semantic | |
5434 | -- checks to make sure the argument of the conversion is appropriate. | |
5435 | ||
5436 | Find_Type (Subtype_Mark (N)); | |
039538bc AC |
5437 | Typ := Entity (Subtype_Mark (N)); |
5438 | Set_Etype (N, Typ); | |
5439 | Check_Fully_Declared (Typ, N); | |
996ae0b0 RK |
5440 | Analyze_Expression (Expr); |
5441 | Validate_Remote_Type_Type_Conversion (N); | |
5e8c8e44 | 5442 | |
996ae0b0 RK |
5443 | -- Only remaining step is validity checks on the argument. These |
5444 | -- are skipped if the conversion does not come from the source. | |
5445 | ||
5446 | if not Comes_From_Source (N) then | |
5447 | return; | |
5448 | ||
b67a385c ES |
5449 | -- If there was an error in a generic unit, no need to replicate the |
5450 | -- error message. Conversely, constant-folding in the generic may | |
5451 | -- transform the argument of a conversion into a string literal, which | |
5452 | -- is legal. Therefore the following tests are not performed in an | |
36428cc4 | 5453 | -- instance. The same applies to an inlined body. |
b67a385c | 5454 | |
36428cc4 | 5455 | elsif In_Instance or In_Inlined_Body then |
b67a385c ES |
5456 | return; |
5457 | ||
996ae0b0 RK |
5458 | elsif Nkind (Expr) = N_Null then |
5459 | Error_Msg_N ("argument of conversion cannot be null", N); | |
ed2233dc | 5460 | Error_Msg_N ("\use qualified expression instead", N); |
996ae0b0 RK |
5461 | Set_Etype (N, Any_Type); |
5462 | ||
5463 | elsif Nkind (Expr) = N_Aggregate then | |
5464 | Error_Msg_N ("argument of conversion cannot be aggregate", N); | |
ed2233dc | 5465 | Error_Msg_N ("\use qualified expression instead", N); |
996ae0b0 RK |
5466 | |
5467 | elsif Nkind (Expr) = N_Allocator then | |
5468 | Error_Msg_N ("argument of conversion cannot be an allocator", N); | |
ed2233dc | 5469 | Error_Msg_N ("\use qualified expression instead", N); |
996ae0b0 RK |
5470 | |
5471 | elsif Nkind (Expr) = N_String_Literal then | |
5472 | Error_Msg_N ("argument of conversion cannot be string literal", N); | |
ed2233dc | 5473 | Error_Msg_N ("\use qualified expression instead", N); |
996ae0b0 RK |
5474 | |
5475 | elsif Nkind (Expr) = N_Character_Literal then | |
0ab80019 | 5476 | if Ada_Version = Ada_83 then |
039538bc | 5477 | Resolve (Expr, Typ); |
996ae0b0 RK |
5478 | else |
5479 | Error_Msg_N ("argument of conversion cannot be character literal", | |
5480 | N); | |
ed2233dc | 5481 | Error_Msg_N ("\use qualified expression instead", N); |
996ae0b0 RK |
5482 | end if; |
5483 | ||
5484 | elsif Nkind (Expr) = N_Attribute_Reference | |
039538bc AC |
5485 | and then Nam_In (Attribute_Name (Expr), Name_Access, |
5486 | Name_Unchecked_Access, | |
5487 | Name_Unrestricted_Access) | |
996ae0b0 RK |
5488 | then |
5489 | Error_Msg_N ("argument of conversion cannot be access", N); | |
ed2233dc | 5490 | Error_Msg_N ("\use qualified expression instead", N); |
996ae0b0 | 5491 | end if; |
039538bc AC |
5492 | |
5493 | -- A formal parameter of a specific tagged type whose related subprogram | |
5494 | -- is subject to pragma Extensions_Visible with value "False" cannot | |
070d862d HK |
5495 | -- appear in a class-wide conversion (SPARK RM 6.1.7(3)). Do not check |
5496 | -- internally generated expressions. | |
039538bc | 5497 | |
070d862d HK |
5498 | if Is_Class_Wide_Type (Typ) |
5499 | and then Comes_From_Source (Expr) | |
5500 | and then Is_EVF_Expression (Expr) | |
5501 | then | |
039538bc | 5502 | Error_Msg_N |
44900051 AC |
5503 | ("formal parameter cannot be converted to class-wide type when " |
5504 | & "Extensions_Visible is False", Expr); | |
039538bc | 5505 | end if; |
996ae0b0 RK |
5506 | end Analyze_Type_Conversion; |
5507 | ||
5508 | ---------------------- | |
5509 | -- Analyze_Unary_Op -- | |
5510 | ---------------------- | |
5511 | ||
5512 | procedure Analyze_Unary_Op (N : Node_Id) is | |
5513 | R : constant Node_Id := Right_Opnd (N); | |
5514 | Op_Id : Entity_Id := Entity (N); | |
5515 | ||
5516 | begin | |
5517 | Set_Etype (N, Any_Type); | |
5518 | Candidate_Type := Empty; | |
5519 | ||
5520 | Analyze_Expression (R); | |
5521 | ||
5522 | if Present (Op_Id) then | |
5523 | if Ekind (Op_Id) = E_Operator then | |
5524 | Find_Unary_Types (R, Op_Id, N); | |
5525 | else | |
5526 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
5527 | end if; | |
5528 | ||
5529 | else | |
5530 | Op_Id := Get_Name_Entity_Id (Chars (N)); | |
996ae0b0 | 5531 | while Present (Op_Id) loop |
996ae0b0 RK |
5532 | if Ekind (Op_Id) = E_Operator then |
5533 | if No (Next_Entity (First_Entity (Op_Id))) then | |
5534 | Find_Unary_Types (R, Op_Id, N); | |
5535 | end if; | |
5536 | ||
5537 | elsif Is_Overloadable (Op_Id) then | |
5538 | Analyze_User_Defined_Unary_Op (N, Op_Id); | |
5539 | end if; | |
5540 | ||
5541 | Op_Id := Homonym (Op_Id); | |
5542 | end loop; | |
5543 | end if; | |
5544 | ||
5545 | Operator_Check (N); | |
5546 | end Analyze_Unary_Op; | |
5547 | ||
5548 | ---------------------------------- | |
5549 | -- Analyze_Unchecked_Expression -- | |
5550 | ---------------------------------- | |
5551 | ||
5552 | procedure Analyze_Unchecked_Expression (N : Node_Id) is | |
5553 | begin | |
5554 | Analyze (Expression (N), Suppress => All_Checks); | |
5555 | Set_Etype (N, Etype (Expression (N))); | |
5556 | Save_Interps (Expression (N), N); | |
5557 | end Analyze_Unchecked_Expression; | |
5558 | ||
5559 | --------------------------------------- | |
5560 | -- Analyze_Unchecked_Type_Conversion -- | |
5561 | --------------------------------------- | |
5562 | ||
5563 | procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is | |
5564 | begin | |
5565 | Find_Type (Subtype_Mark (N)); | |
5566 | Analyze_Expression (Expression (N)); | |
5567 | Set_Etype (N, Entity (Subtype_Mark (N))); | |
5568 | end Analyze_Unchecked_Type_Conversion; | |
5569 | ||
5570 | ------------------------------------ | |
5571 | -- Analyze_User_Defined_Binary_Op -- | |
5572 | ------------------------------------ | |
5573 | ||
5574 | procedure Analyze_User_Defined_Binary_Op | |
5575 | (N : Node_Id; | |
5576 | Op_Id : Entity_Id) | |
5577 | is | |
5578 | begin | |
5579 | -- Only do analysis if the operator Comes_From_Source, since otherwise | |
5580 | -- the operator was generated by the expander, and all such operators | |
5581 | -- always refer to the operators in package Standard. | |
5582 | ||
5583 | if Comes_From_Source (N) then | |
5584 | declare | |
5585 | F1 : constant Entity_Id := First_Formal (Op_Id); | |
5586 | F2 : constant Entity_Id := Next_Formal (F1); | |
5587 | ||
5588 | begin | |
5589 | -- Verify that Op_Id is a visible binary function. Note that since | |
5590 | -- we know Op_Id is overloaded, potentially use visible means use | |
5591 | -- visible for sure (RM 9.4(11)). | |
5592 | ||
5593 | if Ekind (Op_Id) = E_Function | |
5594 | and then Present (F2) | |
5595 | and then (Is_Immediately_Visible (Op_Id) | |
5596 | or else Is_Potentially_Use_Visible (Op_Id)) | |
5597 | and then Has_Compatible_Type (Left_Opnd (N), Etype (F1)) | |
5598 | and then Has_Compatible_Type (Right_Opnd (N), Etype (F2)) | |
5599 | then | |
5600 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
5601 | ||
8b4230c8 AC |
5602 | -- If the left operand is overloaded, indicate that the current |
5603 | -- type is a viable candidate. This is redundant in most cases, | |
5604 | -- but for equality and comparison operators where the context | |
5605 | -- does not impose a type on the operands, setting the proper | |
5606 | -- type is necessary to avoid subsequent ambiguities during | |
5607 | -- resolution, when both user-defined and predefined operators | |
5608 | -- may be candidates. | |
7340e432 AC |
5609 | |
5610 | if Is_Overloaded (Left_Opnd (N)) then | |
5611 | Set_Etype (Left_Opnd (N), Etype (F1)); | |
5612 | end if; | |
5613 | ||
996ae0b0 RK |
5614 | if Debug_Flag_E then |
5615 | Write_Str ("user defined operator "); | |
5616 | Write_Name (Chars (Op_Id)); | |
5617 | Write_Str (" on node "); | |
5618 | Write_Int (Int (N)); | |
5619 | Write_Eol; | |
5620 | end if; | |
5621 | end if; | |
5622 | end; | |
5623 | end if; | |
5624 | end Analyze_User_Defined_Binary_Op; | |
5625 | ||
5626 | ----------------------------------- | |
5627 | -- Analyze_User_Defined_Unary_Op -- | |
5628 | ----------------------------------- | |
5629 | ||
5630 | procedure Analyze_User_Defined_Unary_Op | |
5631 | (N : Node_Id; | |
5632 | Op_Id : Entity_Id) | |
5633 | is | |
5634 | begin | |
5635 | -- Only do analysis if the operator Comes_From_Source, since otherwise | |
5636 | -- the operator was generated by the expander, and all such operators | |
5637 | -- always refer to the operators in package Standard. | |
5638 | ||
5639 | if Comes_From_Source (N) then | |
5640 | declare | |
5641 | F : constant Entity_Id := First_Formal (Op_Id); | |
5642 | ||
5643 | begin | |
5644 | -- Verify that Op_Id is a visible unary function. Note that since | |
5645 | -- we know Op_Id is overloaded, potentially use visible means use | |
5646 | -- visible for sure (RM 9.4(11)). | |
5647 | ||
5648 | if Ekind (Op_Id) = E_Function | |
5649 | and then No (Next_Formal (F)) | |
5650 | and then (Is_Immediately_Visible (Op_Id) | |
5651 | or else Is_Potentially_Use_Visible (Op_Id)) | |
5652 | and then Has_Compatible_Type (Right_Opnd (N), Etype (F)) | |
5653 | then | |
5654 | Add_One_Interp (N, Op_Id, Etype (Op_Id)); | |
5655 | end if; | |
5656 | end; | |
5657 | end if; | |
5658 | end Analyze_User_Defined_Unary_Op; | |
5659 | ||
5660 | --------------------------- | |
5661 | -- Check_Arithmetic_Pair -- | |
5662 | --------------------------- | |
5663 | ||
5664 | procedure Check_Arithmetic_Pair | |
5665 | (T1, T2 : Entity_Id; | |
5666 | Op_Id : Entity_Id; | |
5667 | N : Node_Id) | |
5668 | is | |
401093c1 | 5669 | Op_Name : constant Name_Id := Chars (Op_Id); |
996ae0b0 | 5670 | |
da709d08 AC |
5671 | function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean; |
5672 | -- Check whether the fixed-point type Typ has a user-defined operator | |
5673 | -- (multiplication or division) that should hide the corresponding | |
5674 | -- predefined operator. Used to implement Ada 2005 AI-264, to make | |
5675 | -- such operators more visible and therefore useful. | |
8b4230c8 | 5676 | -- |
50cff367 GD |
5677 | -- If the name of the operation is an expanded name with prefix |
5678 | -- Standard, the predefined universal fixed operator is available, | |
5679 | -- as specified by AI-420 (RM 4.5.5 (19.1/2)). | |
5680 | ||
996ae0b0 RK |
5681 | function Specific_Type (T1, T2 : Entity_Id) return Entity_Id; |
5682 | -- Get specific type (i.e. non-universal type if there is one) | |
5683 | ||
da709d08 AC |
5684 | ------------------ |
5685 | -- Has_Fixed_Op -- | |
5686 | ------------------ | |
5687 | ||
5688 | function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is | |
401093c1 | 5689 | Bas : constant Entity_Id := Base_Type (Typ); |
da709d08 AC |
5690 | Ent : Entity_Id; |
5691 | F1 : Entity_Id; | |
5692 | F2 : Entity_Id; | |
5693 | ||
5694 | begin | |
50cff367 GD |
5695 | -- If the universal_fixed operation is given explicitly the rule |
5696 | -- concerning primitive operations of the type do not apply. | |
5697 | ||
5698 | if Nkind (N) = N_Function_Call | |
5699 | and then Nkind (Name (N)) = N_Expanded_Name | |
5700 | and then Entity (Prefix (Name (N))) = Standard_Standard | |
5701 | then | |
5702 | return False; | |
5703 | end if; | |
5704 | ||
da709d08 AC |
5705 | -- The operation is treated as primitive if it is declared in the |
5706 | -- same scope as the type, and therefore on the same entity chain. | |
5707 | ||
5708 | Ent := Next_Entity (Typ); | |
5709 | while Present (Ent) loop | |
5710 | if Chars (Ent) = Chars (Op) then | |
5711 | F1 := First_Formal (Ent); | |
5712 | F2 := Next_Formal (F1); | |
5713 | ||
5714 | -- The operation counts as primitive if either operand or | |
401093c1 ES |
5715 | -- result are of the given base type, and both operands are |
5716 | -- fixed point types. | |
da709d08 | 5717 | |
401093c1 | 5718 | if (Base_Type (Etype (F1)) = Bas |
da709d08 AC |
5719 | and then Is_Fixed_Point_Type (Etype (F2))) |
5720 | ||
5721 | or else | |
401093c1 | 5722 | (Base_Type (Etype (F2)) = Bas |
da709d08 AC |
5723 | and then Is_Fixed_Point_Type (Etype (F1))) |
5724 | ||
5725 | or else | |
401093c1 | 5726 | (Base_Type (Etype (Ent)) = Bas |
da709d08 AC |
5727 | and then Is_Fixed_Point_Type (Etype (F1)) |
5728 | and then Is_Fixed_Point_Type (Etype (F2))) | |
5729 | then | |
5730 | return True; | |
5731 | end if; | |
5732 | end if; | |
5733 | ||
5734 | Next_Entity (Ent); | |
5735 | end loop; | |
5736 | ||
5737 | return False; | |
5738 | end Has_Fixed_Op; | |
5739 | ||
4c46b835 AC |
5740 | ------------------- |
5741 | -- Specific_Type -- | |
5742 | ------------------- | |
5743 | ||
996ae0b0 RK |
5744 | function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is |
5745 | begin | |
5746 | if T1 = Universal_Integer or else T1 = Universal_Real then | |
5747 | return Base_Type (T2); | |
5748 | else | |
5749 | return Base_Type (T1); | |
5750 | end if; | |
5751 | end Specific_Type; | |
5752 | ||
5753 | -- Start of processing for Check_Arithmetic_Pair | |
5754 | ||
5755 | begin | |
b69cd36a | 5756 | if Nam_In (Op_Name, Name_Op_Add, Name_Op_Subtract) then |
996ae0b0 RK |
5757 | if Is_Numeric_Type (T1) |
5758 | and then Is_Numeric_Type (T2) | |
b4592168 GD |
5759 | and then (Covers (T1 => T1, T2 => T2) |
5760 | or else | |
5761 | Covers (T1 => T2, T2 => T1)) | |
996ae0b0 RK |
5762 | then |
5763 | Add_One_Interp (N, Op_Id, Specific_Type (T1, T2)); | |
5764 | end if; | |
5765 | ||
b69cd36a | 5766 | elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide) then |
996ae0b0 | 5767 | if Is_Fixed_Point_Type (T1) |
b69cd36a | 5768 | and then (Is_Fixed_Point_Type (T2) or else T2 = Universal_Real) |
996ae0b0 RK |
5769 | then |
5770 | -- If Treat_Fixed_As_Integer is set then the Etype is already set | |
5771 | -- and no further processing is required (this is the case of an | |
5772 | -- operator constructed by Exp_Fixd for a fixed point operation) | |
5773 | -- Otherwise add one interpretation with universal fixed result | |
21d7ef70 | 5774 | -- If the operator is given in functional notation, it comes |
996ae0b0 RK |
5775 | -- from source and Fixed_As_Integer cannot apply. |
5776 | ||
da709d08 AC |
5777 | if (Nkind (N) not in N_Op |
5778 | or else not Treat_Fixed_As_Integer (N)) | |
5779 | and then | |
401093c1 | 5780 | (not Has_Fixed_Op (T1, Op_Id) |
da709d08 | 5781 | or else Nkind (Parent (N)) = N_Type_Conversion) |
fbf5a39b | 5782 | then |
996ae0b0 RK |
5783 | Add_One_Interp (N, Op_Id, Universal_Fixed); |
5784 | end if; | |
5785 | ||
5786 | elsif Is_Fixed_Point_Type (T2) | |
5787 | and then (Nkind (N) not in N_Op | |
5788 | or else not Treat_Fixed_As_Integer (N)) | |
5789 | and then T1 = Universal_Real | |
da709d08 | 5790 | and then |
401093c1 | 5791 | (not Has_Fixed_Op (T1, Op_Id) |
da709d08 | 5792 | or else Nkind (Parent (N)) = N_Type_Conversion) |
996ae0b0 RK |
5793 | then |
5794 | Add_One_Interp (N, Op_Id, Universal_Fixed); | |
5795 | ||
5796 | elsif Is_Numeric_Type (T1) | |
5797 | and then Is_Numeric_Type (T2) | |
b4592168 GD |
5798 | and then (Covers (T1 => T1, T2 => T2) |
5799 | or else | |
5800 | Covers (T1 => T2, T2 => T1)) | |
996ae0b0 RK |
5801 | then |
5802 | Add_One_Interp (N, Op_Id, Specific_Type (T1, T2)); | |
5803 | ||
5804 | elsif Is_Fixed_Point_Type (T1) | |
5805 | and then (Base_Type (T2) = Base_Type (Standard_Integer) | |
b69cd36a | 5806 | or else T2 = Universal_Integer) |
996ae0b0 RK |
5807 | then |
5808 | Add_One_Interp (N, Op_Id, T1); | |
5809 | ||
5810 | elsif T2 = Universal_Real | |
5811 | and then Base_Type (T1) = Base_Type (Standard_Integer) | |
5812 | and then Op_Name = Name_Op_Multiply | |
5813 | then | |
5814 | Add_One_Interp (N, Op_Id, Any_Fixed); | |
5815 | ||
5816 | elsif T1 = Universal_Real | |
5817 | and then Base_Type (T2) = Base_Type (Standard_Integer) | |
5818 | then | |
5819 | Add_One_Interp (N, Op_Id, Any_Fixed); | |
5820 | ||
5821 | elsif Is_Fixed_Point_Type (T2) | |
5822 | and then (Base_Type (T1) = Base_Type (Standard_Integer) | |
b69cd36a | 5823 | or else T1 = Universal_Integer) |
996ae0b0 RK |
5824 | and then Op_Name = Name_Op_Multiply |
5825 | then | |
5826 | Add_One_Interp (N, Op_Id, T2); | |
5827 | ||
5828 | elsif T1 = Universal_Real and then T2 = Universal_Integer then | |
5829 | Add_One_Interp (N, Op_Id, T1); | |
5830 | ||
5831 | elsif T2 = Universal_Real | |
5832 | and then T1 = Universal_Integer | |
5833 | and then Op_Name = Name_Op_Multiply | |
5834 | then | |
5835 | Add_One_Interp (N, Op_Id, T2); | |
5836 | end if; | |
5837 | ||
5838 | elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then | |
5839 | ||
5840 | -- Note: The fixed-point operands case with Treat_Fixed_As_Integer | |
5841 | -- set does not require any special processing, since the Etype is | |
5842 | -- already set (case of operation constructed by Exp_Fixed). | |
5843 | ||
5844 | if Is_Integer_Type (T1) | |
b4592168 GD |
5845 | and then (Covers (T1 => T1, T2 => T2) |
5846 | or else | |
5847 | Covers (T1 => T2, T2 => T1)) | |
996ae0b0 RK |
5848 | then |
5849 | Add_One_Interp (N, Op_Id, Specific_Type (T1, T2)); | |
5850 | end if; | |
5851 | ||
5852 | elsif Op_Name = Name_Op_Expon then | |
996ae0b0 RK |
5853 | if Is_Numeric_Type (T1) |
5854 | and then not Is_Fixed_Point_Type (T1) | |
5855 | and then (Base_Type (T2) = Base_Type (Standard_Integer) | |
5856 | or else T2 = Universal_Integer) | |
5857 | then | |
5858 | Add_One_Interp (N, Op_Id, Base_Type (T1)); | |
5859 | end if; | |
5860 | ||
5861 | else pragma Assert (Nkind (N) in N_Op_Shift); | |
5862 | ||
5863 | -- If not one of the predefined operators, the node may be one | |
5864 | -- of the intrinsic functions. Its kind is always specific, and | |
5865 | -- we can use it directly, rather than the name of the operation. | |
5866 | ||
5867 | if Is_Integer_Type (T1) | |
5868 | and then (Base_Type (T2) = Base_Type (Standard_Integer) | |
5869 | or else T2 = Universal_Integer) | |
5870 | then | |
5871 | Add_One_Interp (N, Op_Id, Base_Type (T1)); | |
5872 | end if; | |
5873 | end if; | |
5874 | end Check_Arithmetic_Pair; | |
5875 | ||
5876 | ------------------------------- | |
5877 | -- Check_Misspelled_Selector -- | |
5878 | ------------------------------- | |
5879 | ||
5880 | procedure Check_Misspelled_Selector | |
5881 | (Prefix : Entity_Id; | |
5882 | Sel : Node_Id) | |
5883 | is | |
5884 | Max_Suggestions : constant := 2; | |
5885 | Nr_Of_Suggestions : Natural := 0; | |
5886 | ||
5887 | Suggestion_1 : Entity_Id := Empty; | |
5888 | Suggestion_2 : Entity_Id := Empty; | |
5889 | ||
5890 | Comp : Entity_Id; | |
5891 | ||
5892 | begin | |
8b4230c8 AC |
5893 | -- All the components of the prefix of selector Sel are matched against |
5894 | -- Sel and a count is maintained of possible misspellings. When at | |
a90bd866 | 5895 | -- the end of the analysis there are one or two (not more) possible |
8b4230c8 AC |
5896 | -- misspellings, these misspellings will be suggested as possible |
5897 | -- correction. | |
996ae0b0 | 5898 | |
4c46b835 AC |
5899 | if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then |
5900 | ||
996ae0b0 | 5901 | -- Concurrent types should be handled as well ??? |
4c46b835 | 5902 | |
996ae0b0 RK |
5903 | return; |
5904 | end if; | |
5905 | ||
d469eabe HK |
5906 | Comp := First_Entity (Prefix); |
5907 | while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop | |
5908 | if Is_Visible_Component (Comp) then | |
5909 | if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then | |
5910 | Nr_Of_Suggestions := Nr_Of_Suggestions + 1; | |
5911 | ||
5912 | case Nr_Of_Suggestions is | |
5913 | when 1 => Suggestion_1 := Comp; | |
5914 | when 2 => Suggestion_2 := Comp; | |
e49de265 | 5915 | when others => null; |
d469eabe | 5916 | end case; |
996ae0b0 | 5917 | end if; |
d469eabe | 5918 | end if; |
996ae0b0 | 5919 | |
d469eabe HK |
5920 | Comp := Next_Entity (Comp); |
5921 | end loop; | |
996ae0b0 | 5922 | |
d469eabe | 5923 | -- Report at most two suggestions |
996ae0b0 | 5924 | |
d469eabe | 5925 | if Nr_Of_Suggestions = 1 then |
4e7a4f6e | 5926 | Error_Msg_NE -- CODEFIX |
d469eabe | 5927 | ("\possible misspelling of&", Sel, Suggestion_1); |
996ae0b0 | 5928 | |
d469eabe HK |
5929 | elsif Nr_Of_Suggestions = 2 then |
5930 | Error_Msg_Node_2 := Suggestion_2; | |
4e7a4f6e | 5931 | Error_Msg_NE -- CODEFIX |
d469eabe HK |
5932 | ("\possible misspelling of& or&", Sel, Suggestion_1); |
5933 | end if; | |
996ae0b0 RK |
5934 | end Check_Misspelled_Selector; |
5935 | ||
5936 | ---------------------- | |
5937 | -- Defined_In_Scope -- | |
5938 | ---------------------- | |
5939 | ||
5940 | function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean | |
5941 | is | |
5942 | S1 : constant Entity_Id := Scope (Base_Type (T)); | |
996ae0b0 RK |
5943 | begin |
5944 | return S1 = S | |
5945 | or else (S1 = System_Aux_Id and then S = Scope (S1)); | |
5946 | end Defined_In_Scope; | |
5947 | ||
5948 | ------------------- | |
5949 | -- Diagnose_Call -- | |
5950 | ------------------- | |
5951 | ||
5952 | procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is | |
fbf5a39b AC |
5953 | Actual : Node_Id; |
5954 | X : Interp_Index; | |
5955 | It : Interp; | |
fbf5a39b AC |
5956 | Err_Mode : Boolean; |
5957 | New_Nam : Node_Id; | |
5958 | Void_Interp_Seen : Boolean := False; | |
996ae0b0 | 5959 | |
24657705 HK |
5960 | Success : Boolean; |
5961 | pragma Warnings (Off, Boolean); | |
5962 | ||
996ae0b0 | 5963 | begin |
0791fbe9 | 5964 | if Ada_Version >= Ada_2005 then |
996ae0b0 | 5965 | Actual := First_Actual (N); |
996ae0b0 | 5966 | while Present (Actual) loop |
0ab80019 AC |
5967 | |
5968 | -- Ada 2005 (AI-50217): Post an error in case of premature | |
5969 | -- usage of an entity from the limited view. | |
19f0526a | 5970 | |
996ae0b0 | 5971 | if not Analyzed (Etype (Actual)) |
7b56a91b | 5972 | and then From_Limited_With (Etype (Actual)) |
996ae0b0 RK |
5973 | then |
5974 | Error_Msg_Qual_Level := 1; | |
ed2233dc | 5975 | Error_Msg_NE |
996ae0b0 RK |
5976 | ("missing with_clause for scope of imported type&", |
5977 | Actual, Etype (Actual)); | |
5978 | Error_Msg_Qual_Level := 0; | |
5979 | end if; | |
5980 | ||
5981 | Next_Actual (Actual); | |
5982 | end loop; | |
5983 | end if; | |
5984 | ||
c63a2ad6 AC |
5985 | -- Before listing the possible candidates, check whether this is |
5986 | -- a prefix of a selected component that has been rewritten as a | |
5987 | -- parameterless function call because there is a callable candidate | |
5988 | -- interpretation. If there is a hidden package in the list of homonyms | |
5989 | -- of the function name (bad programming style in any case) suggest that | |
5990 | -- this is the intended entity. | |
40bf00b1 AC |
5991 | |
5992 | if No (Parameter_Associations (N)) | |
5993 | and then Nkind (Parent (N)) = N_Selected_Component | |
5994 | and then Nkind (Parent (Parent (N))) in N_Declaration | |
5995 | and then Is_Overloaded (Nam) | |
5996 | then | |
5997 | declare | |
5998 | Ent : Entity_Id; | |
5999 | ||
6000 | begin | |
6001 | Ent := Current_Entity (Nam); | |
6002 | while Present (Ent) loop | |
6003 | if Ekind (Ent) = E_Package then | |
6004 | Error_Msg_N | |
6005 | ("no legal interpretations as function call,!", Nam); | |
6006 | Error_Msg_NE ("\package& is not visible", N, Ent); | |
c63a2ad6 | 6007 | |
40bf00b1 AC |
6008 | Rewrite (Parent (N), |
6009 | New_Occurrence_Of (Any_Type, Sloc (N))); | |
6010 | return; | |
6011 | end if; | |
6012 | ||
6013 | Ent := Homonym (Ent); | |
6014 | end loop; | |
6015 | end; | |
6016 | end if; | |
6017 | ||
c63a2ad6 AC |
6018 | -- Analyze each candidate call again, with full error reporting for |
6019 | -- each. | |
fbf5a39b AC |
6020 | |
6021 | Error_Msg_N | |
6022 | ("no candidate interpretations match the actuals:!", Nam); | |
6023 | Err_Mode := All_Errors_Mode; | |
6024 | All_Errors_Mode := True; | |
6025 | ||
6026 | -- If this is a call to an operation of a concurrent type, | |
6027 | -- the failed interpretations have been removed from the | |
6028 | -- name. Recover them to provide full diagnostics. | |
6029 | ||
6030 | if Nkind (Parent (Nam)) = N_Selected_Component then | |
6031 | Set_Entity (Nam, Empty); | |
6032 | New_Nam := New_Copy_Tree (Parent (Nam)); | |
6033 | Set_Is_Overloaded (New_Nam, False); | |
6034 | Set_Is_Overloaded (Selector_Name (New_Nam), False); | |
6035 | Set_Parent (New_Nam, Parent (Parent (Nam))); | |
6036 | Analyze_Selected_Component (New_Nam); | |
6037 | Get_First_Interp (Selector_Name (New_Nam), X, It); | |
6038 | else | |
996ae0b0 | 6039 | Get_First_Interp (Nam, X, It); |
fbf5a39b | 6040 | end if; |
996ae0b0 | 6041 | |
fbf5a39b AC |
6042 | while Present (It.Nam) loop |
6043 | if Etype (It.Nam) = Standard_Void_Type then | |
6044 | Void_Interp_Seen := True; | |
996ae0b0 | 6045 | end if; |
fbf5a39b AC |
6046 | |
6047 | Analyze_One_Call (N, It.Nam, True, Success); | |
6048 | Get_Next_Interp (X, It); | |
6049 | end loop; | |
996ae0b0 RK |
6050 | |
6051 | if Nkind (N) = N_Function_Call then | |
6052 | Get_First_Interp (Nam, X, It); | |
996ae0b0 | 6053 | while Present (It.Nam) loop |
bce79204 | 6054 | if Ekind_In (It.Nam, E_Function, E_Operator) then |
996ae0b0 RK |
6055 | return; |
6056 | else | |
6057 | Get_Next_Interp (X, It); | |
6058 | end if; | |
6059 | end loop; | |
6060 | ||
6061 | -- If all interpretations are procedures, this deserves a | |
6062 | -- more precise message. Ditto if this appears as the prefix | |
6063 | -- of a selected component, which may be a lexical error. | |
6064 | ||
4c46b835 AC |
6065 | Error_Msg_N |
6066 | ("\context requires function call, found procedure name", Nam); | |
996ae0b0 RK |
6067 | |
6068 | if Nkind (Parent (N)) = N_Selected_Component | |
6069 | and then N = Prefix (Parent (N)) | |
6070 | then | |
4e7a4f6e AC |
6071 | Error_Msg_N -- CODEFIX |
6072 | ("\period should probably be semicolon", Parent (N)); | |
996ae0b0 | 6073 | end if; |
fbf5a39b AC |
6074 | |
6075 | elsif Nkind (N) = N_Procedure_Call_Statement | |
6076 | and then not Void_Interp_Seen | |
6077 | then | |
6078 | Error_Msg_N ( | |
6079 | "\function name found in procedure call", Nam); | |
996ae0b0 | 6080 | end if; |
fbf5a39b AC |
6081 | |
6082 | All_Errors_Mode := Err_Mode; | |
996ae0b0 RK |
6083 | end Diagnose_Call; |
6084 | ||
6085 | --------------------------- | |
6086 | -- Find_Arithmetic_Types -- | |
6087 | --------------------------- | |
6088 | ||
6089 | procedure Find_Arithmetic_Types | |
6090 | (L, R : Node_Id; | |
6091 | Op_Id : Entity_Id; | |
6092 | N : Node_Id) | |
6093 | is | |
4c46b835 AC |
6094 | Index1 : Interp_Index; |
6095 | Index2 : Interp_Index; | |
6096 | It1 : Interp; | |
6097 | It2 : Interp; | |
996ae0b0 RK |
6098 | |
6099 | procedure Check_Right_Argument (T : Entity_Id); | |
6100 | -- Check right operand of operator | |
6101 | ||
4c46b835 AC |
6102 | -------------------------- |
6103 | -- Check_Right_Argument -- | |
6104 | -------------------------- | |
6105 | ||
996ae0b0 RK |
6106 | procedure Check_Right_Argument (T : Entity_Id) is |
6107 | begin | |
6108 | if not Is_Overloaded (R) then | |
6109 | Check_Arithmetic_Pair (T, Etype (R), Op_Id, N); | |
6110 | else | |
6111 | Get_First_Interp (R, Index2, It2); | |
996ae0b0 RK |
6112 | while Present (It2.Typ) loop |
6113 | Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N); | |
6114 | Get_Next_Interp (Index2, It2); | |
6115 | end loop; | |
6116 | end if; | |
6117 | end Check_Right_Argument; | |
6118 | ||
d8221f45 | 6119 | -- Start of processing for Find_Arithmetic_Types |
996ae0b0 RK |
6120 | |
6121 | begin | |
6122 | if not Is_Overloaded (L) then | |
6123 | Check_Right_Argument (Etype (L)); | |
6124 | ||
6125 | else | |
6126 | Get_First_Interp (L, Index1, It1); | |
996ae0b0 RK |
6127 | while Present (It1.Typ) loop |
6128 | Check_Right_Argument (It1.Typ); | |
6129 | Get_Next_Interp (Index1, It1); | |
6130 | end loop; | |
6131 | end if; | |
6132 | ||
6133 | end Find_Arithmetic_Types; | |
6134 | ||
6135 | ------------------------ | |
6136 | -- Find_Boolean_Types -- | |
6137 | ------------------------ | |
6138 | ||
6139 | procedure Find_Boolean_Types | |
6140 | (L, R : Node_Id; | |
6141 | Op_Id : Entity_Id; | |
6142 | N : Node_Id) | |
6143 | is | |
6144 | Index : Interp_Index; | |
6145 | It : Interp; | |
6146 | ||
6147 | procedure Check_Numeric_Argument (T : Entity_Id); | |
6148 | -- Special case for logical operations one of whose operands is an | |
6149 | -- integer literal. If both are literal the result is any modular type. | |
6150 | ||
4c46b835 AC |
6151 | ---------------------------- |
6152 | -- Check_Numeric_Argument -- | |
6153 | ---------------------------- | |
6154 | ||
996ae0b0 RK |
6155 | procedure Check_Numeric_Argument (T : Entity_Id) is |
6156 | begin | |
6157 | if T = Universal_Integer then | |
6158 | Add_One_Interp (N, Op_Id, Any_Modular); | |
6159 | ||
6160 | elsif Is_Modular_Integer_Type (T) then | |
6161 | Add_One_Interp (N, Op_Id, T); | |
6162 | end if; | |
6163 | end Check_Numeric_Argument; | |
6164 | ||
6165 | -- Start of processing for Find_Boolean_Types | |
6166 | ||
6167 | begin | |
6168 | if not Is_Overloaded (L) then | |
996ae0b0 RK |
6169 | if Etype (L) = Universal_Integer |
6170 | or else Etype (L) = Any_Modular | |
6171 | then | |
6172 | if not Is_Overloaded (R) then | |
6173 | Check_Numeric_Argument (Etype (R)); | |
6174 | ||
6175 | else | |
6176 | Get_First_Interp (R, Index, It); | |
996ae0b0 RK |
6177 | while Present (It.Typ) loop |
6178 | Check_Numeric_Argument (It.Typ); | |
996ae0b0 RK |
6179 | Get_Next_Interp (Index, It); |
6180 | end loop; | |
6181 | end if; | |
6182 | ||
69e6a03e ES |
6183 | -- If operands are aggregates, we must assume that they may be |
6184 | -- boolean arrays, and leave disambiguation for the second pass. | |
6185 | -- If only one is an aggregate, verify that the other one has an | |
6186 | -- interpretation as a boolean array | |
6187 | ||
6188 | elsif Nkind (L) = N_Aggregate then | |
6189 | if Nkind (R) = N_Aggregate then | |
6190 | Add_One_Interp (N, Op_Id, Etype (L)); | |
6191 | ||
6192 | elsif not Is_Overloaded (R) then | |
6193 | if Valid_Boolean_Arg (Etype (R)) then | |
6194 | Add_One_Interp (N, Op_Id, Etype (R)); | |
6195 | end if; | |
6196 | ||
6197 | else | |
6198 | Get_First_Interp (R, Index, It); | |
6199 | while Present (It.Typ) loop | |
6200 | if Valid_Boolean_Arg (It.Typ) then | |
6201 | Add_One_Interp (N, Op_Id, It.Typ); | |
6202 | end if; | |
6203 | ||
6204 | Get_Next_Interp (Index, It); | |
6205 | end loop; | |
6206 | end if; | |
6207 | ||
996ae0b0 RK |
6208 | elsif Valid_Boolean_Arg (Etype (L)) |
6209 | and then Has_Compatible_Type (R, Etype (L)) | |
6210 | then | |
6211 | Add_One_Interp (N, Op_Id, Etype (L)); | |
6212 | end if; | |
6213 | ||
6214 | else | |
6215 | Get_First_Interp (L, Index, It); | |
996ae0b0 RK |
6216 | while Present (It.Typ) loop |
6217 | if Valid_Boolean_Arg (It.Typ) | |
6218 | and then Has_Compatible_Type (R, It.Typ) | |
6219 | then | |
6220 | Add_One_Interp (N, Op_Id, It.Typ); | |
6221 | end if; | |
6222 | ||
6223 | Get_Next_Interp (Index, It); | |
6224 | end loop; | |
6225 | end if; | |
6226 | end Find_Boolean_Types; | |
6227 | ||
6228 | --------------------------- | |
6229 | -- Find_Comparison_Types -- | |
6230 | --------------------------- | |
6231 | ||
6232 | procedure Find_Comparison_Types | |
6233 | (L, R : Node_Id; | |
6234 | Op_Id : Entity_Id; | |
6235 | N : Node_Id) | |
6236 | is | |
6237 | Index : Interp_Index; | |
6238 | It : Interp; | |
6239 | Found : Boolean := False; | |
6240 | I_F : Interp_Index; | |
6241 | T_F : Entity_Id; | |
6242 | Scop : Entity_Id := Empty; | |
6243 | ||
6244 | procedure Try_One_Interp (T1 : Entity_Id); | |
6245 | -- Routine to try one proposed interpretation. Note that the context | |
6246 | -- of the operator plays no role in resolving the arguments, so that | |
6247 | -- if there is more than one interpretation of the operands that is | |
6248 | -- compatible with comparison, the operation is ambiguous. | |
6249 | ||
4c46b835 AC |
6250 | -------------------- |
6251 | -- Try_One_Interp -- | |
6252 | -------------------- | |
6253 | ||
996ae0b0 RK |
6254 | procedure Try_One_Interp (T1 : Entity_Id) is |
6255 | begin | |
6256 | ||
6257 | -- If the operator is an expanded name, then the type of the operand | |
6258 | -- must be defined in the corresponding scope. If the type is | |
6259 | -- universal, the context will impose the correct type. | |
6260 | ||
6261 | if Present (Scop) | |
8b4230c8 AC |
6262 | and then not Defined_In_Scope (T1, Scop) |
6263 | and then T1 /= Universal_Integer | |
6264 | and then T1 /= Universal_Real | |
6265 | and then T1 /= Any_String | |
6266 | and then T1 /= Any_Composite | |
996ae0b0 RK |
6267 | then |
6268 | return; | |
6269 | end if; | |
6270 | ||
8b4230c8 AC |
6271 | if Valid_Comparison_Arg (T1) and then Has_Compatible_Type (R, T1) then |
6272 | if Found and then Base_Type (T1) /= Base_Type (T_F) then | |
996ae0b0 RK |
6273 | It := Disambiguate (L, I_F, Index, Any_Type); |
6274 | ||
6275 | if It = No_Interp then | |
6276 | Ambiguous_Operands (N); | |
6277 | Set_Etype (L, Any_Type); | |
6278 | return; | |
6279 | ||
6280 | else | |
6281 | T_F := It.Typ; | |
6282 | end if; | |
6283 | ||
6284 | else | |
6285 | Found := True; | |
6286 | T_F := T1; | |
6287 | I_F := Index; | |
6288 | end if; | |
6289 | ||
6290 | Set_Etype (L, T_F); | |
6291 | Find_Non_Universal_Interpretations (N, R, Op_Id, T1); | |
6292 | ||
6293 | end if; | |
6294 | end Try_One_Interp; | |
6295 | ||
d8221f45 | 6296 | -- Start of processing for Find_Comparison_Types |
996ae0b0 RK |
6297 | |
6298 | begin | |
fbf5a39b AC |
6299 | -- If left operand is aggregate, the right operand has to |
6300 | -- provide a usable type for it. | |
6301 | ||
8b4230c8 | 6302 | if Nkind (L) = N_Aggregate and then Nkind (R) /= N_Aggregate then |
b4592168 | 6303 | Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N); |
fbf5a39b AC |
6304 | return; |
6305 | end if; | |
996ae0b0 RK |
6306 | |
6307 | if Nkind (N) = N_Function_Call | |
6308 | and then Nkind (Name (N)) = N_Expanded_Name | |
6309 | then | |
6310 | Scop := Entity (Prefix (Name (N))); | |
6311 | ||
6312 | -- The prefix may be a package renaming, and the subsequent test | |
6313 | -- requires the original package. | |
6314 | ||
6315 | if Ekind (Scop) = E_Package | |
6316 | and then Present (Renamed_Entity (Scop)) | |
6317 | then | |
6318 | Scop := Renamed_Entity (Scop); | |
6319 | Set_Entity (Prefix (Name (N)), Scop); | |
6320 | end if; | |
6321 | end if; | |
6322 | ||
6323 | if not Is_Overloaded (L) then | |
6324 | Try_One_Interp (Etype (L)); | |
6325 | ||
6326 | else | |
6327 | Get_First_Interp (L, Index, It); | |
996ae0b0 RK |
6328 | while Present (It.Typ) loop |
6329 | Try_One_Interp (It.Typ); | |
6330 | Get_Next_Interp (Index, It); | |
6331 | end loop; | |
6332 | end if; | |
6333 | end Find_Comparison_Types; | |
6334 | ||
6335 | ---------------------------------------- | |
6336 | -- Find_Non_Universal_Interpretations -- | |
6337 | ---------------------------------------- | |
6338 | ||
6339 | procedure Find_Non_Universal_Interpretations | |
6340 | (N : Node_Id; | |
6341 | R : Node_Id; | |
6342 | Op_Id : Entity_Id; | |
6343 | T1 : Entity_Id) | |
6344 | is | |
6345 | Index : Interp_Index; | |
4c46b835 | 6346 | It : Interp; |
996ae0b0 RK |
6347 | |
6348 | begin | |
8b4230c8 | 6349 | if T1 = Universal_Integer or else T1 = Universal_Real |
b9daa96e AC |
6350 | |
6351 | -- If the left operand of an equality operator is null, the visibility | |
6352 | -- of the operator must be determined from the interpretation of the | |
6353 | -- right operand. This processing must be done for Any_Access, which | |
6354 | -- is the internal representation of the type of the literal null. | |
6355 | ||
be4c5193 | 6356 | or else T1 = Any_Access |
996ae0b0 RK |
6357 | then |
6358 | if not Is_Overloaded (R) then | |
8b4230c8 | 6359 | Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (Etype (R))); |
996ae0b0 RK |
6360 | else |
6361 | Get_First_Interp (R, Index, It); | |
996ae0b0 RK |
6362 | while Present (It.Typ) loop |
6363 | if Covers (It.Typ, T1) then | |
6364 | Add_One_Interp | |
6365 | (N, Op_Id, Standard_Boolean, Base_Type (It.Typ)); | |
6366 | end if; | |
6367 | ||
6368 | Get_Next_Interp (Index, It); | |
6369 | end loop; | |
6370 | end if; | |
6371 | else | |
6372 | Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1)); | |
6373 | end if; | |
6374 | end Find_Non_Universal_Interpretations; | |
6375 | ||
6376 | ------------------------------ | |
6377 | -- Find_Concatenation_Types -- | |
6378 | ------------------------------ | |
6379 | ||
6380 | procedure Find_Concatenation_Types | |
6381 | (L, R : Node_Id; | |
6382 | Op_Id : Entity_Id; | |
6383 | N : Node_Id) | |
6384 | is | |
6385 | Op_Type : constant Entity_Id := Etype (Op_Id); | |
6386 | ||
6387 | begin | |
6388 | if Is_Array_Type (Op_Type) | |
6389 | and then not Is_Limited_Type (Op_Type) | |
6390 | ||
6391 | and then (Has_Compatible_Type (L, Op_Type) | |
6392 | or else | |
6393 | Has_Compatible_Type (L, Component_Type (Op_Type))) | |
6394 | ||
6395 | and then (Has_Compatible_Type (R, Op_Type) | |
6396 | or else | |
6397 | Has_Compatible_Type (R, Component_Type (Op_Type))) | |
6398 | then | |
6399 | Add_One_Interp (N, Op_Id, Op_Type); | |
6400 | end if; | |
6401 | end Find_Concatenation_Types; | |
6402 | ||
6403 | ------------------------- | |
6404 | -- Find_Equality_Types -- | |
6405 | ------------------------- | |
6406 | ||
6407 | procedure Find_Equality_Types | |
6408 | (L, R : Node_Id; | |
6409 | Op_Id : Entity_Id; | |
6410 | N : Node_Id) | |
6411 | is | |
6412 | Index : Interp_Index; | |
6413 | It : Interp; | |
6414 | Found : Boolean := False; | |
6415 | I_F : Interp_Index; | |
6416 | T_F : Entity_Id; | |
6417 | Scop : Entity_Id := Empty; | |
6418 | ||
6419 | procedure Try_One_Interp (T1 : Entity_Id); | |
a8e65aa5 AC |
6420 | -- The context of the equality operator plays no role in resolving the |
6421 | -- arguments, so that if there is more than one interpretation of the | |
6422 | -- operands that is compatible with equality, the construct is ambiguous | |
6423 | -- and an error can be emitted now, after trying to disambiguate, i.e. | |
6424 | -- applying preference rules. | |
996ae0b0 | 6425 | |
4c46b835 AC |
6426 | -------------------- |
6427 | -- Try_One_Interp -- | |
6428 | -------------------- | |
6429 | ||
996ae0b0 | 6430 | procedure Try_One_Interp (T1 : Entity_Id) is |
a8e65aa5 AC |
6431 | Bas : constant Entity_Id := Base_Type (T1); |
6432 | ||
996ae0b0 | 6433 | begin |
996ae0b0 RK |
6434 | -- If the operator is an expanded name, then the type of the operand |
6435 | -- must be defined in the corresponding scope. If the type is | |
6436 | -- universal, the context will impose the correct type. An anonymous | |
6437 | -- type for a 'Access reference is also universal in this sense, as | |
6438 | -- the actual type is obtained from context. | |
8b4230c8 | 6439 | |
fe45e59e ES |
6440 | -- In Ada 2005, the equality operator for anonymous access types |
6441 | -- is declared in Standard, and preference rules apply to it. | |
996ae0b0 | 6442 | |
fe45e59e ES |
6443 | if Present (Scop) then |
6444 | if Defined_In_Scope (T1, Scop) | |
6445 | or else T1 = Universal_Integer | |
6446 | or else T1 = Universal_Real | |
6447 | or else T1 = Any_Access | |
6448 | or else T1 = Any_String | |
6449 | or else T1 = Any_Composite | |
6450 | or else (Ekind (T1) = E_Access_Subprogram_Type | |
a8e65aa5 | 6451 | and then not Comes_From_Source (T1)) |
fe45e59e ES |
6452 | then |
6453 | null; | |
6454 | ||
6455 | elsif Ekind (T1) = E_Anonymous_Access_Type | |
6456 | and then Scop = Standard_Standard | |
6457 | then | |
6458 | null; | |
6459 | ||
6460 | else | |
6461 | -- The scope does not contain an operator for the type | |
6462 | ||
6463 | return; | |
6464 | end if; | |
a8e65aa5 | 6465 | |
9aa04cc7 AC |
6466 | -- If we have infix notation, the operator must be usable. Within |
6467 | -- an instance, if the type is already established we know it is | |
6468 | -- correct. If an operand is universal it is compatible with any | |
6469 | -- numeric type. | |
31af8899 | 6470 | |
a8e65aa5 AC |
6471 | elsif In_Open_Scopes (Scope (Bas)) |
6472 | or else Is_Potentially_Use_Visible (Bas) | |
6473 | or else In_Use (Bas) | |
9aa04cc7 | 6474 | or else (In_Use (Scope (Bas)) and then not Is_Hidden (Bas)) |
a54ffd6c AC |
6475 | |
6476 | -- In an instance, the type may have been immediately visible. | |
6477 | -- Either the types are compatible, or one operand is universal | |
6478 | -- (numeric or null). | |
6479 | ||
c5b4738f AC |
6480 | or else |
6481 | ((In_Instance or else In_Inlined_Body) | |
6482 | and then | |
6483 | (First_Subtype (T1) = First_Subtype (Etype (R)) | |
6484 | or else Nkind (R) = N_Null | |
6485 | or else | |
6486 | (Is_Numeric_Type (T1) | |
6487 | and then Is_Universal_Numeric_Type (Etype (R))))) | |
a54ffd6c AC |
6488 | |
6489 | -- In Ada 2005, the equality on anonymous access types is declared | |
6490 | -- in Standard, and is always visible. | |
6491 | ||
a8e65aa5 AC |
6492 | or else Ekind (T1) = E_Anonymous_Access_Type |
6493 | then | |
6494 | null; | |
6495 | ||
6496 | else | |
308e6f3a | 6497 | -- Save candidate type for subsequent error message, if any |
a8e65aa5 AC |
6498 | |
6499 | if not Is_Limited_Type (T1) then | |
6500 | Candidate_Type := T1; | |
6501 | end if; | |
6502 | ||
6503 | return; | |
996ae0b0 RK |
6504 | end if; |
6505 | ||
0ab80019 AC |
6506 | -- Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95: |
6507 | -- Do not allow anonymous access types in equality operators. | |
6e937c1c | 6508 | |
0791fbe9 | 6509 | if Ada_Version < Ada_2005 |
6e937c1c AC |
6510 | and then Ekind (T1) = E_Anonymous_Access_Type |
6511 | then | |
6512 | return; | |
6513 | end if; | |
6514 | ||
c92e8586 AC |
6515 | -- If the right operand has a type compatible with T1, check for an |
6516 | -- acceptable interpretation, unless T1 is limited (no predefined | |
6517 | -- equality available), or this is use of a "/=" for a tagged type. | |
8b4230c8 AC |
6518 | -- In the latter case, possible interpretations of equality need |
6519 | -- to be considered, we don't want the default inequality declared | |
6520 | -- in Standard to be chosen, and the "/=" will be rewritten as a | |
c92e8586 | 6521 | -- negation of "=" (see the end of Analyze_Equality_Op). This ensures |
026c3cfd | 6522 | -- that rewriting happens during analysis rather than being |
c92e8586 AC |
6523 | -- delayed until expansion (this is needed for ASIS, which only sees |
6524 | -- the unexpanded tree). Note that if the node is N_Op_Ne, but Op_Id | |
6525 | -- is Name_Op_Eq then we still proceed with the interpretation, | |
6526 | -- because that indicates the potential rewriting case where the | |
6527 | -- interpretation to consider is actually "=" and the node may be | |
6528 | -- about to be rewritten by Analyze_Equality_Op. | |
6529 | ||
996ae0b0 | 6530 | if T1 /= Standard_Void_Type |
996ae0b0 | 6531 | and then Has_Compatible_Type (R, T1) |
c92e8586 | 6532 | |
9b62eb32 AC |
6533 | and then |
6534 | ((not Is_Limited_Type (T1) | |
6535 | and then not Is_Limited_Composite (T1)) | |
6536 | ||
6537 | or else | |
2fcc44fa | 6538 | (Is_Array_Type (T1) |
9b62eb32 AC |
6539 | and then not Is_Limited_Type (Component_Type (T1)) |
6540 | and then Available_Full_View_Of_Component (T1))) | |
c92e8586 AC |
6541 | |
6542 | and then | |
6543 | (Nkind (N) /= N_Op_Ne | |
6544 | or else not Is_Tagged_Type (T1) | |
6545 | or else Chars (Op_Id) = Name_Op_Eq) | |
996ae0b0 RK |
6546 | then |
6547 | if Found | |
6548 | and then Base_Type (T1) /= Base_Type (T_F) | |
6549 | then | |
6550 | It := Disambiguate (L, I_F, Index, Any_Type); | |
6551 | ||
6552 | if It = No_Interp then | |
6553 | Ambiguous_Operands (N); | |
6554 | Set_Etype (L, Any_Type); | |
6555 | return; | |
6556 | ||
6557 | else | |
6558 | T_F := It.Typ; | |
6559 | end if; | |
6560 | ||
6561 | else | |
6562 | Found := True; | |
6563 | T_F := T1; | |
6564 | I_F := Index; | |
6565 | end if; | |
6566 | ||
6567 | if not Analyzed (L) then | |
6568 | Set_Etype (L, T_F); | |
6569 | end if; | |
6570 | ||
6571 | Find_Non_Universal_Interpretations (N, R, Op_Id, T1); | |
6572 | ||
6e73e3ab | 6573 | -- Case of operator was not visible, Etype still set to Any_Type |
996ae0b0 | 6574 | |
6e73e3ab | 6575 | if Etype (N) = Any_Type then |
996ae0b0 RK |
6576 | Found := False; |
6577 | end if; | |
fe45e59e ES |
6578 | |
6579 | elsif Scop = Standard_Standard | |
6580 | and then Ekind (T1) = E_Anonymous_Access_Type | |
6581 | then | |
6582 | Found := True; | |
996ae0b0 RK |
6583 | end if; |
6584 | end Try_One_Interp; | |
6585 | ||
6586 | -- Start of processing for Find_Equality_Types | |
6587 | ||
6588 | begin | |
fbf5a39b AC |
6589 | -- If left operand is aggregate, the right operand has to |
6590 | -- provide a usable type for it. | |
6591 | ||
6592 | if Nkind (L) = N_Aggregate | |
6593 | and then Nkind (R) /= N_Aggregate | |
6594 | then | |
b4592168 | 6595 | Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N); |
fbf5a39b AC |
6596 | return; |
6597 | end if; | |
996ae0b0 RK |
6598 | |
6599 | if Nkind (N) = N_Function_Call | |
6600 | and then Nkind (Name (N)) = N_Expanded_Name | |
6601 | then | |
6602 | Scop := Entity (Prefix (Name (N))); | |
6603 | ||
6604 | -- The prefix may be a package renaming, and the subsequent test | |
6605 | -- requires the original package. | |
6606 | ||
6607 | if Ekind (Scop) = E_Package | |
6608 | and then Present (Renamed_Entity (Scop)) | |
6609 | then | |
6610 | Scop := Renamed_Entity (Scop); | |
6611 | Set_Entity (Prefix (Name (N)), Scop); | |
6612 | end if; | |
6613 | end if; | |
6614 | ||
6615 | if not Is_Overloaded (L) then | |
6616 | Try_One_Interp (Etype (L)); | |
996ae0b0 | 6617 | |
4c46b835 | 6618 | else |
996ae0b0 | 6619 | Get_First_Interp (L, Index, It); |
996ae0b0 RK |
6620 | while Present (It.Typ) loop |
6621 | Try_One_Interp (It.Typ); | |
6622 | Get_Next_Interp (Index, It); | |
6623 | end loop; | |
6624 | end if; | |
6625 | end Find_Equality_Types; | |
6626 | ||
6627 | ------------------------- | |
6628 | -- Find_Negation_Types -- | |
6629 | ------------------------- | |
6630 | ||
6631 | procedure Find_Negation_Types | |
6632 | (R : Node_Id; | |
6633 | Op_Id : Entity_Id; | |
6634 | N : Node_Id) | |
6635 | is | |
6636 | Index : Interp_Index; | |
6637 | It : Interp; | |
6638 | ||
6639 | begin | |
6640 | if not Is_Overloaded (R) then | |
996ae0b0 RK |
6641 | if Etype (R) = Universal_Integer then |
6642 | Add_One_Interp (N, Op_Id, Any_Modular); | |
996ae0b0 RK |
6643 | elsif Valid_Boolean_Arg (Etype (R)) then |
6644 | Add_One_Interp (N, Op_Id, Etype (R)); | |
6645 | end if; | |
6646 | ||
6647 | else | |
6648 | Get_First_Interp (R, Index, It); | |
996ae0b0 RK |
6649 | while Present (It.Typ) loop |
6650 | if Valid_Boolean_Arg (It.Typ) then | |
6651 | Add_One_Interp (N, Op_Id, It.Typ); | |
6652 | end if; | |
6653 | ||
6654 | Get_Next_Interp (Index, It); | |
6655 | end loop; | |
6656 | end if; | |
6657 | end Find_Negation_Types; | |
6658 | ||
d469eabe HK |
6659 | ------------------------------ |
6660 | -- Find_Primitive_Operation -- | |
6661 | ------------------------------ | |
6662 | ||
6663 | function Find_Primitive_Operation (N : Node_Id) return Boolean is | |
6664 | Obj : constant Node_Id := Prefix (N); | |
6665 | Op : constant Node_Id := Selector_Name (N); | |
6666 | ||
6667 | Prim : Elmt_Id; | |
6668 | Prims : Elist_Id; | |
6669 | Typ : Entity_Id; | |
6670 | ||
6671 | begin | |
6672 | Set_Etype (Op, Any_Type); | |
6673 | ||
6674 | if Is_Access_Type (Etype (Obj)) then | |
6675 | Typ := Designated_Type (Etype (Obj)); | |
6676 | else | |
6677 | Typ := Etype (Obj); | |
6678 | end if; | |
6679 | ||
6680 | if Is_Class_Wide_Type (Typ) then | |
6681 | Typ := Root_Type (Typ); | |
6682 | end if; | |
6683 | ||
6684 | Prims := Primitive_Operations (Typ); | |
6685 | ||
6686 | Prim := First_Elmt (Prims); | |
6687 | while Present (Prim) loop | |
6688 | if Chars (Node (Prim)) = Chars (Op) then | |
6689 | Add_One_Interp (Op, Node (Prim), Etype (Node (Prim))); | |
6690 | Set_Etype (N, Etype (Node (Prim))); | |
6691 | end if; | |
6692 | ||
6693 | Next_Elmt (Prim); | |
6694 | end loop; | |
6695 | ||
6696 | -- Now look for class-wide operations of the type or any of its | |
6697 | -- ancestors by iterating over the homonyms of the selector. | |
6698 | ||
6699 | declare | |
6700 | Cls_Type : constant Entity_Id := Class_Wide_Type (Typ); | |
6701 | Hom : Entity_Id; | |
6702 | ||
6703 | begin | |
6704 | Hom := Current_Entity (Op); | |
6705 | while Present (Hom) loop | |
6706 | if (Ekind (Hom) = E_Procedure | |
6707 | or else | |
6708 | Ekind (Hom) = E_Function) | |
6709 | and then Scope (Hom) = Scope (Typ) | |
6710 | and then Present (First_Formal (Hom)) | |
6711 | and then | |
6712 | (Base_Type (Etype (First_Formal (Hom))) = Cls_Type | |
6713 | or else | |
6714 | (Is_Access_Type (Etype (First_Formal (Hom))) | |
8b4230c8 AC |
6715 | and then |
6716 | Ekind (Etype (First_Formal (Hom))) = | |
6717 | E_Anonymous_Access_Type | |
6718 | and then | |
6719 | Base_Type | |
6720 | (Designated_Type (Etype (First_Formal (Hom)))) = | |
d469eabe HK |
6721 | Cls_Type)) |
6722 | then | |
6723 | Add_One_Interp (Op, Hom, Etype (Hom)); | |
6724 | Set_Etype (N, Etype (Hom)); | |
6725 | end if; | |
6726 | ||
6727 | Hom := Homonym (Hom); | |
6728 | end loop; | |
6729 | end; | |
6730 | ||
6731 | return Etype (Op) /= Any_Type; | |
6732 | end Find_Primitive_Operation; | |
6733 | ||
996ae0b0 RK |
6734 | ---------------------- |
6735 | -- Find_Unary_Types -- | |
6736 | ---------------------- | |
6737 | ||
6738 | procedure Find_Unary_Types | |
6739 | (R : Node_Id; | |
6740 | Op_Id : Entity_Id; | |
6741 | N : Node_Id) | |
6742 | is | |
6743 | Index : Interp_Index; | |
6744 | It : Interp; | |
6745 | ||
6746 | begin | |
6747 | if not Is_Overloaded (R) then | |
6748 | if Is_Numeric_Type (Etype (R)) then | |
65f7ed64 AC |
6749 | |
6750 | -- In an instance a generic actual may be a numeric type even if | |
6751 | -- the formal in the generic unit was not. In that case, the | |
6752 | -- predefined operator was not a possible interpretation in the | |
2e70d415 AC |
6753 | -- generic, and cannot be one in the instance, unless the operator |
6754 | -- is an actual of an instance. | |
65f7ed64 AC |
6755 | |
6756 | if In_Instance | |
6757 | and then | |
6758 | not Is_Numeric_Type (Corresponding_Generic_Type (Etype (R))) | |
6759 | then | |
6760 | null; | |
6761 | else | |
6762 | Add_One_Interp (N, Op_Id, Base_Type (Etype (R))); | |
6763 | end if; | |
996ae0b0 RK |
6764 | end if; |
6765 | ||
6766 | else | |
6767 | Get_First_Interp (R, Index, It); | |
996ae0b0 RK |
6768 | while Present (It.Typ) loop |
6769 | if Is_Numeric_Type (It.Typ) then | |
65f7ed64 AC |
6770 | if In_Instance |
6771 | and then | |
6772 | not Is_Numeric_Type | |
6773 | (Corresponding_Generic_Type (Etype (It.Typ))) | |
6774 | then | |
6775 | null; | |
6776 | ||
6777 | else | |
6778 | Add_One_Interp (N, Op_Id, Base_Type (It.Typ)); | |
6779 | end if; | |
996ae0b0 RK |
6780 | end if; |
6781 | ||
6782 | Get_Next_Interp (Index, It); | |
6783 | end loop; | |
6784 | end if; | |
6785 | end Find_Unary_Types; | |
6786 | ||
996ae0b0 RK |
6787 | ------------------ |
6788 | -- Junk_Operand -- | |
6789 | ------------------ | |
6790 | ||
6791 | function Junk_Operand (N : Node_Id) return Boolean is | |
6792 | Enode : Node_Id; | |
6793 | ||
6794 | begin | |
6795 | if Error_Posted (N) then | |
6796 | return False; | |
6797 | end if; | |
6798 | ||
6799 | -- Get entity to be tested | |
6800 | ||
6801 | if Is_Entity_Name (N) | |
6802 | and then Present (Entity (N)) | |
6803 | then | |
6804 | Enode := N; | |
6805 | ||
6806 | -- An odd case, a procedure name gets converted to a very peculiar | |
6807 | -- function call, and here is where we detect this happening. | |
6808 | ||
6809 | elsif Nkind (N) = N_Function_Call | |
6810 | and then Is_Entity_Name (Name (N)) | |
6811 | and then Present (Entity (Name (N))) | |
6812 | then | |
6813 | Enode := Name (N); | |
6814 | ||
6815 | -- Another odd case, there are at least some cases of selected | |
6816 | -- components where the selected component is not marked as having | |
6817 | -- an entity, even though the selector does have an entity | |
6818 | ||
6819 | elsif Nkind (N) = N_Selected_Component | |
6820 | and then Present (Entity (Selector_Name (N))) | |
6821 | then | |
6822 | Enode := Selector_Name (N); | |
6823 | ||
6824 | else | |
6825 | return False; | |
6826 | end if; | |
6827 | ||
9de61fcb | 6828 | -- Now test the entity we got to see if it is a bad case |
996ae0b0 RK |
6829 | |
6830 | case Ekind (Entity (Enode)) is | |
996ae0b0 RK |
6831 | when E_Package => |
6832 | Error_Msg_N | |
6833 | ("package name cannot be used as operand", Enode); | |
6834 | ||
6835 | when Generic_Unit_Kind => | |
6836 | Error_Msg_N | |
6837 | ("generic unit name cannot be used as operand", Enode); | |
6838 | ||
6839 | when Type_Kind => | |
6840 | Error_Msg_N | |
6841 | ("subtype name cannot be used as operand", Enode); | |
6842 | ||
6843 | when Entry_Kind => | |
6844 | Error_Msg_N | |
6845 | ("entry name cannot be used as operand", Enode); | |
6846 | ||
6847 | when E_Procedure => | |
6848 | Error_Msg_N | |
6849 | ("procedure name cannot be used as operand", Enode); | |
6850 | ||
6851 | when E_Exception => | |
6852 | Error_Msg_N | |
6853 | ("exception name cannot be used as operand", Enode); | |
6854 | ||
d8f43ee6 HK |
6855 | when E_Block |
6856 | | E_Label | |
6857 | | E_Loop | |
6858 | => | |
996ae0b0 RK |
6859 | Error_Msg_N |
6860 | ("label name cannot be used as operand", Enode); | |
6861 | ||
6862 | when others => | |
6863 | return False; | |
996ae0b0 RK |
6864 | end case; |
6865 | ||
6866 | return True; | |
6867 | end Junk_Operand; | |
6868 | ||
6869 | -------------------- | |
6870 | -- Operator_Check -- | |
6871 | -------------------- | |
6872 | ||
6873 | procedure Operator_Check (N : Node_Id) is | |
6874 | begin | |
30c20106 AC |
6875 | Remove_Abstract_Operations (N); |
6876 | ||
996ae0b0 RK |
6877 | -- Test for case of no interpretation found for operator |
6878 | ||
6879 | if Etype (N) = Any_Type then | |
6880 | declare | |
b67a385c ES |
6881 | L : Node_Id; |
6882 | R : Node_Id; | |
6883 | Op_Id : Entity_Id := Empty; | |
996ae0b0 RK |
6884 | |
6885 | begin | |
6886 | R := Right_Opnd (N); | |
6887 | ||
6888 | if Nkind (N) in N_Binary_Op then | |
6889 | L := Left_Opnd (N); | |
6890 | else | |
6891 | L := Empty; | |
6892 | end if; | |
6893 | ||
6894 | -- If either operand has no type, then don't complain further, | |
9de61fcb | 6895 | -- since this simply means that we have a propagated error. |
996ae0b0 RK |
6896 | |
6897 | if R = Error | |
6898 | or else Etype (R) = Any_Type | |
6899 | or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type) | |
6900 | then | |
7610fee8 AC |
6901 | -- For the rather unusual case where one of the operands is |
6902 | -- a Raise_Expression, whose initial type is Any_Type, use | |
6903 | -- the type of the other operand. | |
6904 | ||
6905 | if Nkind (L) = N_Raise_Expression then | |
6906 | Set_Etype (L, Etype (R)); | |
6907 | Set_Etype (N, Etype (R)); | |
6908 | ||
6909 | elsif Nkind (R) = N_Raise_Expression then | |
6910 | Set_Etype (R, Etype (L)); | |
6911 | Set_Etype (N, Etype (L)); | |
6912 | end if; | |
6913 | ||
996ae0b0 RK |
6914 | return; |
6915 | ||
4c46b835 AC |
6916 | -- We explicitly check for the case of concatenation of component |
6917 | -- with component to avoid reporting spurious matching array types | |
6918 | -- that might happen to be lurking in distant packages (such as | |
6919 | -- run-time packages). This also prevents inconsistencies in the | |
6920 | -- messages for certain ACVC B tests, which can vary depending on | |
6921 | -- types declared in run-time interfaces. Another improvement when | |
6922 | -- aggregates are present is to look for a well-typed operand. | |
996ae0b0 RK |
6923 | |
6924 | elsif Present (Candidate_Type) | |
6925 | and then (Nkind (N) /= N_Op_Concat | |
6926 | or else Is_Array_Type (Etype (L)) | |
6927 | or else Is_Array_Type (Etype (R))) | |
6928 | then | |
996ae0b0 RK |
6929 | if Nkind (N) = N_Op_Concat then |
6930 | if Etype (L) /= Any_Composite | |
6931 | and then Is_Array_Type (Etype (L)) | |
6932 | then | |
6933 | Candidate_Type := Etype (L); | |
6934 | ||
6935 | elsif Etype (R) /= Any_Composite | |
6936 | and then Is_Array_Type (Etype (R)) | |
6937 | then | |
6938 | Candidate_Type := Etype (R); | |
6939 | end if; | |
6940 | end if; | |
6941 | ||
19d846a0 | 6942 | Error_Msg_NE -- CODEFIX |
996ae0b0 RK |
6943 | ("operator for} is not directly visible!", |
6944 | N, First_Subtype (Candidate_Type)); | |
4561baf7 ES |
6945 | |
6946 | declare | |
6947 | U : constant Node_Id := | |
6948 | Cunit (Get_Source_Unit (Candidate_Type)); | |
4561baf7 ES |
6949 | begin |
6950 | if Unit_Is_Visible (U) then | |
6951 | Error_Msg_N -- CODEFIX | |
6952 | ("use clause would make operation legal!", N); | |
4561baf7 ES |
6953 | else |
6954 | Error_Msg_NE -- CODEFIX | |
6955 | ("add with_clause and use_clause for&!", | |
8b4230c8 | 6956 | N, Defining_Entity (Unit (U))); |
4561baf7 ES |
6957 | end if; |
6958 | end; | |
996ae0b0 RK |
6959 | return; |
6960 | ||
6961 | -- If either operand is a junk operand (e.g. package name), then | |
6962 | -- post appropriate error messages, but do not complain further. | |
6963 | ||
0e0eecec ES |
6964 | -- Note that the use of OR in this test instead of OR ELSE is |
6965 | -- quite deliberate, we may as well check both operands in the | |
6966 | -- binary operator case. | |
996ae0b0 RK |
6967 | |
6968 | elsif Junk_Operand (R) | |
9559eccf AC |
6969 | or -- really mean OR here and not OR ELSE, see above |
6970 | (Nkind (N) in N_Binary_Op and then Junk_Operand (L)) | |
996ae0b0 RK |
6971 | then |
6972 | return; | |
6973 | ||
6974 | -- If we have a logical operator, one of whose operands is | |
0e0eecec ES |
6975 | -- Boolean, then we know that the other operand cannot resolve to |
6976 | -- Boolean (since we got no interpretations), but in that case we | |
6977 | -- pretty much know that the other operand should be Boolean, so | |
070d862d | 6978 | -- resolve it that way (generating an error). |
996ae0b0 | 6979 | |
d469eabe | 6980 | elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then |
996ae0b0 RK |
6981 | if Etype (L) = Standard_Boolean then |
6982 | Resolve (R, Standard_Boolean); | |
6983 | return; | |
6984 | elsif Etype (R) = Standard_Boolean then | |
6985 | Resolve (L, Standard_Boolean); | |
6986 | return; | |
6987 | end if; | |
6988 | ||
6989 | -- For an arithmetic operator or comparison operator, if one | |
6990 | -- of the operands is numeric, then we know the other operand | |
6991 | -- is not the same numeric type. If it is a non-numeric type, | |
6992 | -- then probably it is intended to match the other operand. | |
6993 | ||
d469eabe HK |
6994 | elsif Nkind_In (N, N_Op_Add, |
6995 | N_Op_Divide, | |
6996 | N_Op_Ge, | |
6997 | N_Op_Gt, | |
6998 | N_Op_Le) | |
6999 | or else | |
7000 | Nkind_In (N, N_Op_Lt, | |
7001 | N_Op_Mod, | |
7002 | N_Op_Multiply, | |
7003 | N_Op_Rem, | |
7004 | N_Op_Subtract) | |
996ae0b0 | 7005 | then |
7dbd3de9 RD |
7006 | -- If Allow_Integer_Address is active, check whether the |
7007 | -- operation becomes legal after converting an operand. | |
7008 | ||
996ae0b0 RK |
7009 | if Is_Numeric_Type (Etype (L)) |
7010 | and then not Is_Numeric_Type (Etype (R)) | |
7011 | then | |
7dbd3de9 RD |
7012 | if Address_Integer_Convert_OK (Etype (R), Etype (L)) then |
7013 | Rewrite (R, | |
7014 | Unchecked_Convert_To (Etype (L), Relocate_Node (R))); | |
7dbd3de9 | 7015 | |
1e3689bd AC |
7016 | if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then |
7017 | Analyze_Comparison_Op (N); | |
7018 | else | |
7019 | Analyze_Arithmetic_Op (N); | |
7020 | end if; | |
7dbd3de9 RD |
7021 | else |
7022 | Resolve (R, Etype (L)); | |
7023 | end if; | |
1e3689bd | 7024 | |
996ae0b0 RK |
7025 | return; |
7026 | ||
7027 | elsif Is_Numeric_Type (Etype (R)) | |
7028 | and then not Is_Numeric_Type (Etype (L)) | |
7029 | then | |
7dbd3de9 RD |
7030 | if Address_Integer_Convert_OK (Etype (L), Etype (R)) then |
7031 | Rewrite (L, | |
7032 | Unchecked_Convert_To (Etype (R), Relocate_Node (L))); | |
1e3689bd AC |
7033 | |
7034 | if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then | |
7035 | Analyze_Comparison_Op (N); | |
7036 | else | |
7037 | Analyze_Arithmetic_Op (N); | |
7038 | end if; | |
7039 | ||
9559eccf | 7040 | return; |
7dbd3de9 RD |
7041 | |
7042 | else | |
7043 | Resolve (L, Etype (R)); | |
7044 | end if; | |
9559eccf | 7045 | |
996ae0b0 | 7046 | return; |
9559eccf AC |
7047 | |
7048 | elsif Allow_Integer_Address | |
d9d25d04 AC |
7049 | and then Is_Descendant_Of_Address (Etype (L)) |
7050 | and then Is_Descendant_Of_Address (Etype (R)) | |
9559eccf AC |
7051 | and then not Error_Posted (N) |
7052 | then | |
7053 | declare | |
7054 | Addr_Type : constant Entity_Id := Etype (L); | |
7055 | ||
7056 | begin | |
7057 | Rewrite (L, | |
7058 | Unchecked_Convert_To ( | |
7059 | Standard_Integer, Relocate_Node (L))); | |
7060 | Rewrite (R, | |
7061 | Unchecked_Convert_To ( | |
7062 | Standard_Integer, Relocate_Node (R))); | |
1e3689bd AC |
7063 | |
7064 | if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then | |
7065 | Analyze_Comparison_Op (N); | |
7066 | else | |
7067 | Analyze_Arithmetic_Op (N); | |
7068 | end if; | |
9559eccf AC |
7069 | |
7070 | -- If this is an operand in an enclosing arithmetic | |
7071 | -- operation, Convert the result as an address so that | |
7072 | -- arithmetic folding of address can continue. | |
7073 | ||
7074 | if Nkind (Parent (N)) in N_Op then | |
7075 | Rewrite (N, | |
7076 | Unchecked_Convert_To (Addr_Type, Relocate_Node (N))); | |
7077 | end if; | |
7078 | ||
7079 | return; | |
7080 | end; | |
a8a42b93 AC |
7081 | |
7082 | -- Under relaxed RM semantics silently replace occurrences of | |
7083 | -- null by System.Address_Null. | |
7084 | ||
7085 | elsif Null_To_Null_Address_Convert_OK (N) then | |
7086 | Replace_Null_By_Null_Address (N); | |
7087 | ||
7088 | if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then | |
7089 | Analyze_Comparison_Op (N); | |
7090 | else | |
7091 | Analyze_Arithmetic_Op (N); | |
7092 | end if; | |
7093 | ||
7094 | return; | |
996ae0b0 RK |
7095 | end if; |
7096 | ||
7097 | -- Comparisons on A'Access are common enough to deserve a | |
7098 | -- special message. | |
7099 | ||
d469eabe | 7100 | elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) |
996ae0b0 RK |
7101 | and then Ekind (Etype (L)) = E_Access_Attribute_Type |
7102 | and then Ekind (Etype (R)) = E_Access_Attribute_Type | |
7103 | then | |
7104 | Error_Msg_N | |
7105 | ("two access attributes cannot be compared directly", N); | |
7106 | Error_Msg_N | |
aab883ec | 7107 | ("\use qualified expression for one of the operands", |
996ae0b0 RK |
7108 | N); |
7109 | return; | |
7110 | ||
7111 | -- Another one for C programmers | |
7112 | ||
7113 | elsif Nkind (N) = N_Op_Concat | |
7114 | and then Valid_Boolean_Arg (Etype (L)) | |
7115 | and then Valid_Boolean_Arg (Etype (R)) | |
7116 | then | |
7117 | Error_Msg_N ("invalid operands for concatenation", N); | |
4e7a4f6e AC |
7118 | Error_Msg_N -- CODEFIX |
7119 | ("\maybe AND was meant", N); | |
996ae0b0 RK |
7120 | return; |
7121 | ||
7122 | -- A special case for comparison of access parameter with null | |
7123 | ||
7124 | elsif Nkind (N) = N_Op_Eq | |
7125 | and then Is_Entity_Name (L) | |
7126 | and then Nkind (Parent (Entity (L))) = N_Parameter_Specification | |
7127 | and then Nkind (Parameter_Type (Parent (Entity (L)))) = | |
7128 | N_Access_Definition | |
7129 | and then Nkind (R) = N_Null | |
7130 | then | |
7131 | Error_Msg_N ("access parameter is not allowed to be null", L); | |
7132 | Error_Msg_N ("\(call would raise Constraint_Error)", L); | |
7133 | return; | |
61bee0e3 AC |
7134 | |
7135 | -- Another special case for exponentiation, where the right | |
7136 | -- operand must be Natural, independently of the base. | |
7137 | ||
7138 | elsif Nkind (N) = N_Op_Expon | |
7139 | and then Is_Numeric_Type (Etype (L)) | |
7140 | and then not Is_Overloaded (R) | |
7141 | and then | |
7142 | First_Subtype (Base_Type (Etype (R))) /= Standard_Integer | |
7143 | and then Base_Type (Etype (R)) /= Universal_Integer | |
7144 | then | |
dec6faf1 | 7145 | if Ada_Version >= Ada_2012 |
15954beb | 7146 | and then Has_Dimension_System (Etype (L)) |
dec6faf1 AC |
7147 | then |
7148 | Error_Msg_NE | |
54c04d6c | 7149 | ("exponent for dimensioned type must be a rational" & |
dec6faf1 AC |
7150 | ", found}", R, Etype (R)); |
7151 | else | |
7152 | Error_Msg_NE | |
7153 | ("exponent must be of type Natural, found}", R, Etype (R)); | |
7154 | end if; | |
54c04d6c | 7155 | |
61bee0e3 | 7156 | return; |
11261647 AC |
7157 | |
7158 | elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then | |
7159 | if Address_Integer_Convert_OK (Etype (R), Etype (L)) then | |
7160 | Rewrite (R, | |
7161 | Unchecked_Convert_To (Etype (L), Relocate_Node (R))); | |
7162 | Analyze_Equality_Op (N); | |
7163 | return; | |
a8a42b93 AC |
7164 | |
7165 | -- Under relaxed RM semantics silently replace occurrences of | |
7166 | -- null by System.Address_Null. | |
7167 | ||
7168 | elsif Null_To_Null_Address_Convert_OK (N) then | |
7169 | Replace_Null_By_Null_Address (N); | |
7170 | Analyze_Equality_Op (N); | |
7171 | return; | |
11261647 | 7172 | end if; |
996ae0b0 RK |
7173 | end if; |
7174 | ||
0e0eecec ES |
7175 | -- If we fall through then just give general message. Note that in |
7176 | -- the following messages, if the operand is overloaded we choose | |
7177 | -- an arbitrary type to complain about, but that is probably more | |
7178 | -- useful than not giving a type at all. | |
996ae0b0 RK |
7179 | |
7180 | if Nkind (N) in N_Unary_Op then | |
7181 | Error_Msg_Node_2 := Etype (R); | |
7182 | Error_Msg_N ("operator& not defined for}", N); | |
7183 | return; | |
7184 | ||
7185 | else | |
fbf5a39b AC |
7186 | if Nkind (N) in N_Binary_Op then |
7187 | if not Is_Overloaded (L) | |
7188 | and then not Is_Overloaded (R) | |
7189 | and then Base_Type (Etype (L)) = Base_Type (Etype (R)) | |
7190 | then | |
7ffd9312 | 7191 | Error_Msg_Node_2 := First_Subtype (Etype (R)); |
fbf5a39b | 7192 | Error_Msg_N ("there is no applicable operator& for}", N); |
996ae0b0 | 7193 | |
fbf5a39b | 7194 | else |
b67a385c ES |
7195 | -- Another attempt to find a fix: one of the candidate |
7196 | -- interpretations may not be use-visible. This has | |
7197 | -- already been checked for predefined operators, so | |
7198 | -- we examine only user-defined functions. | |
7199 | ||
7200 | Op_Id := Get_Name_Entity_Id (Chars (N)); | |
7201 | ||
7202 | while Present (Op_Id) loop | |
7203 | if Ekind (Op_Id) /= E_Operator | |
7204 | and then Is_Overloadable (Op_Id) | |
7205 | then | |
7206 | if not Is_Immediately_Visible (Op_Id) | |
7207 | and then not In_Use (Scope (Op_Id)) | |
aab883ec | 7208 | and then not Is_Abstract_Subprogram (Op_Id) |
b67a385c ES |
7209 | and then not Is_Hidden (Op_Id) |
7210 | and then Ekind (Scope (Op_Id)) = E_Package | |
7211 | and then | |
7212 | Has_Compatible_Type | |
7213 | (L, Etype (First_Formal (Op_Id))) | |
7214 | and then Present | |
7215 | (Next_Formal (First_Formal (Op_Id))) | |
7216 | and then | |
7217 | Has_Compatible_Type | |
7218 | (R, | |
7219 | Etype (Next_Formal (First_Formal (Op_Id)))) | |
7220 | then | |
ed2233dc | 7221 | Error_Msg_N |
b67a385c | 7222 | ("No legal interpretation for operator&", N); |
ed2233dc | 7223 | Error_Msg_NE |
b67a385c | 7224 | ("\use clause on& would make operation legal", |
8b4230c8 | 7225 | N, Scope (Op_Id)); |
b67a385c ES |
7226 | exit; |
7227 | end if; | |
7228 | end if; | |
fbf5a39b | 7229 | |
b67a385c ES |
7230 | Op_Id := Homonym (Op_Id); |
7231 | end loop; | |
7232 | ||
7233 | if No (Op_Id) then | |
7234 | Error_Msg_N ("invalid operand types for operator&", N); | |
7235 | ||
7236 | if Nkind (N) /= N_Op_Concat then | |
7237 | Error_Msg_NE ("\left operand has}!", N, Etype (L)); | |
7238 | Error_Msg_NE ("\right operand has}!", N, Etype (R)); | |
2e70d415 AC |
7239 | |
7240 | -- For concatenation operators it is more difficult to | |
7241 | -- determine which is the wrong operand. It is worth | |
7242 | -- flagging explicitly an access type, for those who | |
7243 | -- might think that a dereference happens here. | |
7244 | ||
7245 | elsif Is_Access_Type (Etype (L)) then | |
7246 | Error_Msg_N ("\left operand is access type", N); | |
7247 | ||
7248 | elsif Is_Access_Type (Etype (R)) then | |
7249 | Error_Msg_N ("\right operand is access type", N); | |
b67a385c | 7250 | end if; |
fbf5a39b AC |
7251 | end if; |
7252 | end if; | |
996ae0b0 RK |
7253 | end if; |
7254 | end if; | |
7255 | end; | |
7256 | end if; | |
7257 | end Operator_Check; | |
7258 | ||
6e73e3ab AC |
7259 | ----------------------------------------- |
7260 | -- Process_Implicit_Dereference_Prefix -- | |
7261 | ----------------------------------------- | |
7262 | ||
d469eabe | 7263 | function Process_Implicit_Dereference_Prefix |
da709d08 | 7264 | (E : Entity_Id; |
d469eabe | 7265 | P : Entity_Id) return Entity_Id |
6e73e3ab AC |
7266 | is |
7267 | Ref : Node_Id; | |
d469eabe | 7268 | Typ : constant Entity_Id := Designated_Type (Etype (P)); |
da709d08 | 7269 | |
6e73e3ab | 7270 | begin |
1a8fae99 ES |
7271 | if Present (E) |
7272 | and then (Operating_Mode = Check_Semantics or else not Expander_Active) | |
7273 | then | |
8b4230c8 AC |
7274 | -- We create a dummy reference to E to ensure that the reference is |
7275 | -- not considered as part of an assignment (an implicit dereference | |
7276 | -- can never assign to its prefix). The Comes_From_Source attribute | |
7277 | -- needs to be propagated for accurate warnings. | |
6e73e3ab | 7278 | |
e4494292 | 7279 | Ref := New_Occurrence_Of (E, Sloc (P)); |
6e73e3ab AC |
7280 | Set_Comes_From_Source (Ref, Comes_From_Source (P)); |
7281 | Generate_Reference (E, Ref); | |
7282 | end if; | |
d469eabe | 7283 | |
8b4230c8 AC |
7284 | -- An implicit dereference is a legal occurrence of an incomplete type |
7285 | -- imported through a limited_with clause, if the full view is visible. | |
d469eabe | 7286 | |
7b56a91b AC |
7287 | if From_Limited_With (Typ) |
7288 | and then not From_Limited_With (Scope (Typ)) | |
d469eabe HK |
7289 | and then |
7290 | (Is_Immediately_Visible (Scope (Typ)) | |
7291 | or else | |
7292 | (Is_Child_Unit (Scope (Typ)) | |
8398e82e | 7293 | and then Is_Visible_Lib_Unit (Scope (Typ)))) |
d469eabe HK |
7294 | then |
7295 | return Available_View (Typ); | |
7296 | else | |
7297 | return Typ; | |
7298 | end if; | |
6e73e3ab AC |
7299 | end Process_Implicit_Dereference_Prefix; |
7300 | ||
30c20106 AC |
7301 | -------------------------------- |
7302 | -- Remove_Abstract_Operations -- | |
7303 | -------------------------------- | |
7304 | ||
7305 | procedure Remove_Abstract_Operations (N : Node_Id) is | |
e80f0cb0 | 7306 | Abstract_Op : Entity_Id := Empty; |
d9d25d04 | 7307 | Address_Descendant : Boolean := False; |
e80f0cb0 RD |
7308 | I : Interp_Index; |
7309 | It : Interp; | |
30c20106 | 7310 | |
0e0eecec ES |
7311 | -- AI-310: If overloaded, remove abstract non-dispatching operations. We |
7312 | -- activate this if either extensions are enabled, or if the abstract | |
7313 | -- operation in question comes from a predefined file. This latter test | |
7314 | -- allows us to use abstract to make operations invisible to users. In | |
7315 | -- particular, if type Address is non-private and abstract subprograms | |
7316 | -- are used to hide its operators, they will be truly hidden. | |
30c20106 | 7317 | |
5950a3ac | 7318 | type Operand_Position is (First_Op, Second_Op); |
8a36a0cc | 7319 | Univ_Type : constant Entity_Id := Universal_Interpretation (N); |
5950a3ac AC |
7320 | |
7321 | procedure Remove_Address_Interpretations (Op : Operand_Position); | |
0e0eecec ES |
7322 | -- Ambiguities may arise when the operands are literal and the address |
7323 | -- operations in s-auxdec are visible. In that case, remove the | |
8b4230c8 AC |
7324 | -- interpretation of a literal as Address, to retain the semantics |
7325 | -- of Address as a private type. | |
9f4fd324 AC |
7326 | |
7327 | ------------------------------------ | |
5950a3ac | 7328 | -- Remove_Address_Interpretations -- |
9f4fd324 AC |
7329 | ------------------------------------ |
7330 | ||
5950a3ac | 7331 | procedure Remove_Address_Interpretations (Op : Operand_Position) is |
9f4fd324 AC |
7332 | Formal : Entity_Id; |
7333 | ||
7334 | begin | |
7335 | if Is_Overloaded (N) then | |
7336 | Get_First_Interp (N, I, It); | |
7337 | while Present (It.Nam) loop | |
7338 | Formal := First_Entity (It.Nam); | |
7339 | ||
5950a3ac AC |
7340 | if Op = Second_Op then |
7341 | Formal := Next_Entity (Formal); | |
7342 | end if; | |
7343 | ||
d9d25d04 AC |
7344 | if Is_Descendant_Of_Address (Etype (Formal)) then |
7345 | Address_Descendant := True; | |
9f4fd324 AC |
7346 | Remove_Interp (I); |
7347 | end if; | |
7348 | ||
7349 | Get_Next_Interp (I, It); | |
7350 | end loop; | |
7351 | end if; | |
7352 | end Remove_Address_Interpretations; | |
7353 | ||
7354 | -- Start of processing for Remove_Abstract_Operations | |
7355 | ||
30c20106 | 7356 | begin |
d935a36e | 7357 | if Is_Overloaded (N) then |
ee1a7572 AC |
7358 | if Debug_Flag_V then |
7359 | Write_Str ("Remove_Abstract_Operations: "); | |
7360 | Write_Overloads (N); | |
7361 | end if; | |
7362 | ||
30c20106 | 7363 | Get_First_Interp (N, I, It); |
d935a36e | 7364 | |
30c20106 | 7365 | while Present (It.Nam) loop |
aab883ec ES |
7366 | if Is_Overloadable (It.Nam) |
7367 | and then Is_Abstract_Subprogram (It.Nam) | |
30c20106 AC |
7368 | and then not Is_Dispatching_Operation (It.Nam) |
7369 | then | |
af152989 | 7370 | Abstract_Op := It.Nam; |
fe45e59e | 7371 | |
d9d25d04 AC |
7372 | if Is_Descendant_Of_Address (It.Typ) then |
7373 | Address_Descendant := True; | |
401093c1 ES |
7374 | Remove_Interp (I); |
7375 | exit; | |
7376 | ||
76264f60 | 7377 | -- In Ada 2005, this operation does not participate in overload |
9c510803 | 7378 | -- resolution. If the operation is defined in a predefined |
fe45e59e ES |
7379 | -- unit, it is one of the operations declared abstract in some |
7380 | -- variants of System, and it must be removed as well. | |
7381 | ||
0791fbe9 | 7382 | elsif Ada_Version >= Ada_2005 |
8ab31c0c | 7383 | or else In_Predefined_Unit (It.Nam) |
fe45e59e ES |
7384 | then |
7385 | Remove_Interp (I); | |
7386 | exit; | |
7387 | end if; | |
30c20106 AC |
7388 | end if; |
7389 | ||
7390 | Get_Next_Interp (I, It); | |
7391 | end loop; | |
7392 | ||
af152989 | 7393 | if No (Abstract_Op) then |
fe45e59e ES |
7394 | |
7395 | -- If some interpretation yields an integer type, it is still | |
7396 | -- possible that there are address interpretations. Remove them | |
7397 | -- if one operand is a literal, to avoid spurious ambiguities | |
7398 | -- on systems where Address is a visible integer type. | |
7399 | ||
7400 | if Is_Overloaded (N) | |
401093c1 | 7401 | and then Nkind (N) in N_Op |
fe45e59e ES |
7402 | and then Is_Integer_Type (Etype (N)) |
7403 | then | |
7404 | if Nkind (N) in N_Binary_Op then | |
7405 | if Nkind (Right_Opnd (N)) = N_Integer_Literal then | |
7406 | Remove_Address_Interpretations (Second_Op); | |
7407 | ||
e6326de5 | 7408 | elsif Nkind (Left_Opnd (N)) = N_Integer_Literal then |
fe45e59e ES |
7409 | Remove_Address_Interpretations (First_Op); |
7410 | end if; | |
7411 | end if; | |
7412 | end if; | |
3984e89a AC |
7413 | |
7414 | elsif Nkind (N) in N_Op then | |
4c46b835 | 7415 | |
fe45e59e ES |
7416 | -- Remove interpretations that treat literals as addresses. This |
7417 | -- is never appropriate, even when Address is defined as a visible | |
7418 | -- Integer type. The reason is that we would really prefer Address | |
7a5b62b0 AC |
7419 | -- to behave as a private type, even in this case. If Address is a |
7420 | -- visible integer type, we get lots of overload ambiguities. | |
30c20106 | 7421 | |
5950a3ac AC |
7422 | if Nkind (N) in N_Binary_Op then |
7423 | declare | |
7424 | U1 : constant Boolean := | |
8b4230c8 | 7425 | Present (Universal_Interpretation (Right_Opnd (N))); |
5950a3ac | 7426 | U2 : constant Boolean := |
8b4230c8 | 7427 | Present (Universal_Interpretation (Left_Opnd (N))); |
30c20106 | 7428 | |
5950a3ac | 7429 | begin |
0e0eecec | 7430 | if U1 then |
5950a3ac | 7431 | Remove_Address_Interpretations (Second_Op); |
0e0eecec | 7432 | end if; |
5950a3ac | 7433 | |
0e0eecec | 7434 | if U2 then |
5950a3ac | 7435 | Remove_Address_Interpretations (First_Op); |
30c20106 AC |
7436 | end if; |
7437 | ||
5950a3ac AC |
7438 | if not (U1 and U2) then |
7439 | ||
7440 | -- Remove corresponding predefined operator, which is | |
7441 | -- always added to the overload set. | |
7442 | ||
7443 | Get_First_Interp (N, I, It); | |
7444 | while Present (It.Nam) loop | |
0ab80019 AC |
7445 | if Scope (It.Nam) = Standard_Standard |
7446 | and then Base_Type (It.Typ) = | |
7447 | Base_Type (Etype (Abstract_Op)) | |
7448 | then | |
5950a3ac AC |
7449 | Remove_Interp (I); |
7450 | end if; | |
7451 | ||
8a36a0cc AC |
7452 | Get_Next_Interp (I, It); |
7453 | end loop; | |
7454 | ||
7455 | elsif Is_Overloaded (N) | |
7456 | and then Present (Univ_Type) | |
7457 | then | |
7458 | -- If both operands have a universal interpretation, | |
0e0eecec ES |
7459 | -- it is still necessary to remove interpretations that |
7460 | -- yield Address. Any remaining ambiguities will be | |
7461 | -- removed in Disambiguate. | |
8a36a0cc AC |
7462 | |
7463 | Get_First_Interp (N, I, It); | |
8a36a0cc | 7464 | while Present (It.Nam) loop |
d9d25d04 | 7465 | if Is_Descendant_Of_Address (It.Typ) then |
0e0eecec ES |
7466 | Remove_Interp (I); |
7467 | ||
7468 | elsif not Is_Type (It.Nam) then | |
8a36a0cc | 7469 | Set_Entity (N, It.Nam); |
8a36a0cc AC |
7470 | end if; |
7471 | ||
5950a3ac AC |
7472 | Get_Next_Interp (I, It); |
7473 | end loop; | |
7474 | end if; | |
7475 | end; | |
30c20106 | 7476 | end if; |
3984e89a AC |
7477 | |
7478 | elsif Nkind (N) = N_Function_Call | |
7479 | and then | |
7480 | (Nkind (Name (N)) = N_Operator_Symbol | |
7481 | or else | |
7482 | (Nkind (Name (N)) = N_Expanded_Name | |
7483 | and then | |
7484 | Nkind (Selector_Name (Name (N))) = N_Operator_Symbol)) | |
7485 | then | |
5950a3ac | 7486 | |
3984e89a AC |
7487 | declare |
7488 | Arg1 : constant Node_Id := First (Parameter_Associations (N)); | |
5950a3ac AC |
7489 | U1 : constant Boolean := |
7490 | Present (Universal_Interpretation (Arg1)); | |
7491 | U2 : constant Boolean := | |
7492 | Present (Next (Arg1)) and then | |
7493 | Present (Universal_Interpretation (Next (Arg1))); | |
3984e89a AC |
7494 | |
7495 | begin | |
0e0eecec | 7496 | if U1 then |
5950a3ac | 7497 | Remove_Address_Interpretations (First_Op); |
0e0eecec | 7498 | end if; |
3984e89a | 7499 | |
0e0eecec | 7500 | if U2 then |
5950a3ac AC |
7501 | Remove_Address_Interpretations (Second_Op); |
7502 | end if; | |
7503 | ||
7504 | if not (U1 and U2) then | |
3984e89a AC |
7505 | Get_First_Interp (N, I, It); |
7506 | while Present (It.Nam) loop | |
9f4fd324 AC |
7507 | if Scope (It.Nam) = Standard_Standard |
7508 | and then It.Typ = Base_Type (Etype (Abstract_Op)) | |
7509 | then | |
3984e89a AC |
7510 | Remove_Interp (I); |
7511 | end if; | |
7512 | ||
7513 | Get_Next_Interp (I, It); | |
7514 | end loop; | |
7515 | end if; | |
7516 | end; | |
30c20106 | 7517 | end if; |
af152989 | 7518 | |
401093c1 ES |
7519 | -- If the removal has left no valid interpretations, emit an error |
7520 | -- message now and label node as illegal. | |
af152989 AC |
7521 | |
7522 | if Present (Abstract_Op) then | |
7523 | Get_First_Interp (N, I, It); | |
7524 | ||
7525 | if No (It.Nam) then | |
7526 | ||
6e73e3ab | 7527 | -- Removal of abstract operation left no viable candidate |
af152989 AC |
7528 | |
7529 | Set_Etype (N, Any_Type); | |
7530 | Error_Msg_Sloc := Sloc (Abstract_Op); | |
7531 | Error_Msg_NE | |
7532 | ("cannot call abstract operation& declared#", N, Abstract_Op); | |
401093c1 ES |
7533 | |
7534 | -- In Ada 2005, an abstract operation may disable predefined | |
7535 | -- operators. Since the context is not yet known, we mark the | |
7536 | -- predefined operators as potentially hidden. Do not include | |
7537 | -- predefined operators when addresses are involved since this | |
7538 | -- case is handled separately. | |
7539 | ||
d9d25d04 | 7540 | elsif Ada_Version >= Ada_2005 and then not Address_Descendant then |
401093c1 ES |
7541 | while Present (It.Nam) loop |
7542 | if Is_Numeric_Type (It.Typ) | |
7543 | and then Scope (It.Typ) = Standard_Standard | |
7544 | then | |
7545 | Set_Abstract_Op (I, Abstract_Op); | |
7546 | end if; | |
7547 | ||
7548 | Get_Next_Interp (I, It); | |
7549 | end loop; | |
af152989 AC |
7550 | end if; |
7551 | end if; | |
ee1a7572 AC |
7552 | |
7553 | if Debug_Flag_V then | |
7554 | Write_Str ("Remove_Abstract_Operations done: "); | |
7555 | Write_Overloads (N); | |
7556 | end if; | |
30c20106 AC |
7557 | end if; |
7558 | end Remove_Abstract_Operations; | |
7559 | ||
d50f4827 AC |
7560 | ---------------------------- |
7561 | -- Try_Container_Indexing -- | |
7562 | ---------------------------- | |
7563 | ||
7564 | function Try_Container_Indexing | |
7565 | (N : Node_Id; | |
7566 | Prefix : Node_Id; | |
50878404 | 7567 | Exprs : List_Id) return Boolean |
d50f4827 | 7568 | is |
437244c7 AC |
7569 | Pref_Typ : constant Entity_Id := Etype (Prefix); |
7570 | ||
0c3ef0cc GD |
7571 | function Constant_Indexing_OK return Boolean; |
7572 | -- Constant_Indexing is legal if there is no Variable_Indexing defined | |
7573 | -- for the type, or else node not a target of assignment, or an actual | |
7574 | -- for an IN OUT or OUT formal (RM 4.1.6 (11)). | |
7575 | ||
211e7410 AC |
7576 | function Expr_Matches_In_Formal |
7577 | (Subp : Entity_Id; | |
7578 | Par : Node_Id) return Boolean; | |
7579 | -- Find formal corresponding to given indexed component that is an | |
7580 | -- actual in a call. Note that the enclosing subprogram call has not | |
0c3ef0cc | 7581 | -- been analyzed yet, and the parameter list is not normalized, so |
211e7410 AC |
7582 | -- that if the argument is a parameter association we must match it |
7583 | -- by name and not by position. | |
7584 | ||
437244c7 AC |
7585 | function Find_Indexing_Operations |
7586 | (T : Entity_Id; | |
7587 | Nam : Name_Id; | |
7588 | Is_Constant : Boolean) return Node_Id; | |
7589 | -- Return a reference to the primitive operation of type T denoted by | |
7590 | -- name Nam. If the operation is overloaded, the reference carries all | |
7591 | -- interpretations. Flag Is_Constant should be set when the context is | |
7592 | -- constant indexing. | |
7593 | ||
fa73fc3d AC |
7594 | -------------------------- |
7595 | -- Constant_Indexing_OK -- | |
7596 | -------------------------- | |
7597 | ||
7598 | function Constant_Indexing_OK return Boolean is | |
7599 | Par : Node_Id; | |
7600 | ||
7601 | begin | |
437244c7 | 7602 | if No (Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing)) then |
fa73fc3d AC |
7603 | return True; |
7604 | ||
7605 | elsif not Is_Variable (Prefix) then | |
7606 | return True; | |
7607 | end if; | |
7608 | ||
7609 | Par := N; | |
7610 | while Present (Par) loop | |
7611 | if Nkind (Parent (Par)) = N_Assignment_Statement | |
7612 | and then Par = Name (Parent (Par)) | |
7613 | then | |
7614 | return False; | |
7615 | ||
7616 | -- The call may be overloaded, in which case we assume that its | |
7617 | -- resolution does not depend on the type of the parameter that | |
7618 | -- includes the indexing operation. | |
7619 | ||
7620 | elsif Nkind_In (Parent (Par), N_Function_Call, | |
7621 | N_Procedure_Call_Statement) | |
7622 | and then Is_Entity_Name (Name (Parent (Par))) | |
7623 | then | |
7624 | declare | |
fa73fc3d AC |
7625 | Proc : Entity_Id; |
7626 | ||
7627 | begin | |
7628 | -- We should look for an interpretation with the proper | |
7629 | -- number of formals, and determine whether it is an | |
31101470 AC |
7630 | -- In_Parameter, but for now we examine the formal that |
7631 | -- corresponds to the indexing, and assume that variable | |
7632 | -- indexing is required if some interpretation has an | |
7633 | -- assignable formal at that position. Still does not | |
7634 | -- cover the most complex cases ??? | |
fa73fc3d AC |
7635 | |
7636 | if Is_Overloaded (Name (Parent (Par))) then | |
31101470 AC |
7637 | declare |
7638 | Proc : constant Node_Id := Name (Parent (Par)); | |
31101470 AC |
7639 | I : Interp_Index; |
7640 | It : Interp; | |
7641 | ||
7642 | begin | |
7643 | Get_First_Interp (Proc, I, It); | |
7644 | while Present (It.Nam) loop | |
211e7410 AC |
7645 | if not Expr_Matches_In_Formal (It.Nam, Par) then |
7646 | return False; | |
7647 | end if; | |
31101470 AC |
7648 | |
7649 | Get_Next_Interp (I, It); | |
7650 | end loop; | |
7651 | end; | |
7652 | ||
0c3ef0cc | 7653 | -- All interpretations have a matching in-mode formal |
211e7410 | 7654 | |
fa73fc3d AC |
7655 | return True; |
7656 | ||
7657 | else | |
7658 | Proc := Entity (Name (Parent (Par))); | |
7659 | ||
7660 | -- If this is an indirect call, get formals from | |
7661 | -- designated type. | |
7662 | ||
7663 | if Is_Access_Subprogram_Type (Etype (Proc)) then | |
7664 | Proc := Designated_Type (Etype (Proc)); | |
7665 | end if; | |
7666 | end if; | |
7667 | ||
211e7410 | 7668 | return Expr_Matches_In_Formal (Proc, Par); |
fa73fc3d AC |
7669 | end; |
7670 | ||
7671 | elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then | |
7672 | return False; | |
7673 | ||
7674 | -- If the indexed component is a prefix it may be the first actual | |
7675 | -- of a prefixed call. Retrieve the called entity, if any, and | |
4e9ee595 AC |
7676 | -- check its first formal. Determine if the context is a procedure |
7677 | -- or function call. | |
fa73fc3d AC |
7678 | |
7679 | elsif Nkind (Parent (Par)) = N_Selected_Component then | |
7680 | declare | |
7681 | Sel : constant Node_Id := Selector_Name (Parent (Par)); | |
7682 | Nam : constant Entity_Id := Current_Entity (Sel); | |
7683 | ||
7684 | begin | |
bc38dbb4 AC |
7685 | if Present (Nam) and then Is_Overloadable (Nam) then |
7686 | if Nkind (Parent (Parent (Par))) = | |
7687 | N_Procedure_Call_Statement | |
4e9ee595 AC |
7688 | then |
7689 | return False; | |
7690 | ||
bc38dbb4 AC |
7691 | elsif Ekind (Nam) = E_Function |
7692 | and then Present (First_Formal (Nam)) | |
7693 | then | |
7694 | return Ekind (First_Formal (Nam)) = E_In_Parameter; | |
4e9ee595 | 7695 | end if; |
fa73fc3d AC |
7696 | end if; |
7697 | end; | |
7698 | ||
437244c7 | 7699 | elsif Nkind (Par) in N_Op then |
fa73fc3d AC |
7700 | return True; |
7701 | end if; | |
7702 | ||
7703 | Par := Parent (Par); | |
7704 | end loop; | |
7705 | ||
7706 | -- In all other cases, constant indexing is legal | |
7707 | ||
7708 | return True; | |
7709 | end Constant_Indexing_OK; | |
7710 | ||
f32eb591 AC |
7711 | ---------------------------- |
7712 | -- Expr_Matches_In_Formal -- | |
7713 | ---------------------------- | |
0c3ef0cc GD |
7714 | |
7715 | function Expr_Matches_In_Formal | |
7716 | (Subp : Entity_Id; | |
7717 | Par : Node_Id) return Boolean | |
7718 | is | |
7719 | Actual : Node_Id; | |
7720 | Formal : Node_Id; | |
7721 | ||
7722 | begin | |
7723 | Formal := First_Formal (Subp); | |
7724 | Actual := First (Parameter_Associations ((Parent (Par)))); | |
7725 | ||
7726 | if Nkind (Par) /= N_Parameter_Association then | |
7727 | ||
7728 | -- Match by position | |
7729 | ||
7730 | while Present (Actual) and then Present (Formal) loop | |
7731 | exit when Actual = Par; | |
7732 | Next (Actual); | |
7733 | ||
7734 | if Present (Formal) then | |
7735 | Next_Formal (Formal); | |
7736 | ||
7737 | -- Otherwise this is a parameter mismatch, the error is | |
7738 | -- reported elsewhere, or else variable indexing is implied. | |
7739 | ||
7740 | else | |
7741 | return False; | |
7742 | end if; | |
7743 | end loop; | |
7744 | ||
7745 | else | |
7746 | -- Match by name | |
7747 | ||
7748 | while Present (Formal) loop | |
7749 | exit when Chars (Formal) = Chars (Selector_Name (Par)); | |
7750 | Next_Formal (Formal); | |
7751 | ||
7752 | if No (Formal) then | |
7753 | return False; | |
7754 | end if; | |
7755 | end loop; | |
7756 | end if; | |
7757 | ||
7758 | return Present (Formal) and then Ekind (Formal) = E_In_Parameter; | |
7759 | end Expr_Matches_In_Formal; | |
7760 | ||
437244c7 AC |
7761 | ------------------------------ |
7762 | -- Find_Indexing_Operations -- | |
7763 | ------------------------------ | |
7764 | ||
7765 | function Find_Indexing_Operations | |
7766 | (T : Entity_Id; | |
7767 | Nam : Name_Id; | |
7768 | Is_Constant : Boolean) return Node_Id | |
7769 | is | |
7770 | procedure Inspect_Declarations | |
7771 | (Typ : Entity_Id; | |
7772 | Ref : in out Node_Id); | |
7773 | -- Traverse the declarative list where type Typ resides and collect | |
7774 | -- all suitable interpretations in node Ref. | |
7775 | ||
7776 | procedure Inspect_Primitives | |
7777 | (Typ : Entity_Id; | |
7778 | Ref : in out Node_Id); | |
7779 | -- Traverse the list of primitive operations of type Typ and collect | |
7780 | -- all suitable interpretations in node Ref. | |
7781 | ||
7782 | function Is_OK_Candidate | |
7783 | (Subp_Id : Entity_Id; | |
7784 | Typ : Entity_Id) return Boolean; | |
7785 | -- Determine whether subprogram Subp_Id is a suitable indexing | |
7786 | -- operation for type Typ. To qualify as such, the subprogram must | |
7787 | -- be a function, have at least two parameters, and the type of the | |
7788 | -- first parameter must be either Typ, or Typ'Class, or access [to | |
7789 | -- constant] with designated type Typ or Typ'Class. | |
7790 | ||
7791 | procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id); | |
7792 | -- Store subprogram Subp_Id as an interpretation in node Ref | |
7793 | ||
7794 | -------------------------- | |
7795 | -- Inspect_Declarations -- | |
7796 | -------------------------- | |
7797 | ||
7798 | procedure Inspect_Declarations | |
7799 | (Typ : Entity_Id; | |
7800 | Ref : in out Node_Id) | |
7801 | is | |
7802 | Typ_Decl : constant Node_Id := Declaration_Node (Typ); | |
7803 | Decl : Node_Id; | |
7804 | Subp_Id : Entity_Id; | |
7805 | ||
7806 | begin | |
2cc2e964 | 7807 | -- Ensure that the routine is not called with itypes, which lack a |
437244c7 AC |
7808 | -- declarative node. |
7809 | ||
7810 | pragma Assert (Present (Typ_Decl)); | |
7811 | pragma Assert (Is_List_Member (Typ_Decl)); | |
7812 | ||
7813 | Decl := First (List_Containing (Typ_Decl)); | |
7814 | while Present (Decl) loop | |
7815 | if Nkind (Decl) = N_Subprogram_Declaration then | |
7816 | Subp_Id := Defining_Entity (Decl); | |
7817 | ||
7818 | if Is_OK_Candidate (Subp_Id, Typ) then | |
7819 | Record_Interp (Subp_Id, Ref); | |
7820 | end if; | |
7821 | end if; | |
7822 | ||
7823 | Next (Decl); | |
7824 | end loop; | |
7825 | end Inspect_Declarations; | |
7826 | ||
7827 | ------------------------ | |
7828 | -- Inspect_Primitives -- | |
7829 | ------------------------ | |
7830 | ||
7831 | procedure Inspect_Primitives | |
7832 | (Typ : Entity_Id; | |
7833 | Ref : in out Node_Id) | |
7834 | is | |
7835 | Prim_Elmt : Elmt_Id; | |
7836 | Prim_Id : Entity_Id; | |
7837 | ||
7838 | begin | |
7839 | Prim_Elmt := First_Elmt (Primitive_Operations (Typ)); | |
7840 | while Present (Prim_Elmt) loop | |
7841 | Prim_Id := Node (Prim_Elmt); | |
7842 | ||
7843 | if Is_OK_Candidate (Prim_Id, Typ) then | |
7844 | Record_Interp (Prim_Id, Ref); | |
7845 | end if; | |
7846 | ||
7847 | Next_Elmt (Prim_Elmt); | |
7848 | end loop; | |
7849 | end Inspect_Primitives; | |
7850 | ||
7851 | --------------------- | |
7852 | -- Is_OK_Candidate -- | |
7853 | --------------------- | |
7854 | ||
7855 | function Is_OK_Candidate | |
7856 | (Subp_Id : Entity_Id; | |
7857 | Typ : Entity_Id) return Boolean | |
7858 | is | |
7859 | Formal : Entity_Id; | |
7860 | Formal_Typ : Entity_Id; | |
7861 | Param_Typ : Node_Id; | |
7862 | ||
7863 | begin | |
2cc2e964 | 7864 | -- To classify as a suitable candidate, the subprogram must be a |
437244c7 AC |
7865 | -- function whose name matches the argument of aspect Constant or |
7866 | -- Variable_Indexing. | |
7867 | ||
7868 | if Ekind (Subp_Id) = E_Function and then Chars (Subp_Id) = Nam then | |
7869 | Formal := First_Formal (Subp_Id); | |
7870 | ||
7871 | -- The candidate requires at least two parameters | |
7872 | ||
7873 | if Present (Formal) and then Present (Next_Formal (Formal)) then | |
7874 | Formal_Typ := Empty; | |
7875 | Param_Typ := Parameter_Type (Parent (Formal)); | |
7876 | ||
7877 | -- Use the designated type when the first parameter is of an | |
7878 | -- access type. | |
7879 | ||
7880 | if Nkind (Param_Typ) = N_Access_Definition | |
7881 | and then Present (Subtype_Mark (Param_Typ)) | |
7882 | then | |
7883 | -- When the context is a constant indexing, the access | |
7884 | -- definition must be access-to-constant. This does not | |
7885 | -- apply to variable indexing. | |
7886 | ||
7887 | if not Is_Constant | |
7888 | or else Constant_Present (Param_Typ) | |
7889 | then | |
7890 | Formal_Typ := Etype (Subtype_Mark (Param_Typ)); | |
7891 | end if; | |
7892 | ||
7893 | -- Otherwise use the parameter type | |
7894 | ||
7895 | else | |
7896 | Formal_Typ := Etype (Param_Typ); | |
7897 | end if; | |
7898 | ||
7899 | if Present (Formal_Typ) then | |
7900 | ||
7901 | -- Use the specific type when the parameter type is | |
7902 | -- class-wide. | |
7903 | ||
7904 | if Is_Class_Wide_Type (Formal_Typ) then | |
7905 | Formal_Typ := Etype (Base_Type (Formal_Typ)); | |
7906 | end if; | |
7907 | ||
7908 | -- Use the full view when the parameter type is private | |
7909 | -- or incomplete. | |
7910 | ||
7911 | if Is_Incomplete_Or_Private_Type (Formal_Typ) | |
7912 | and then Present (Full_View (Formal_Typ)) | |
7913 | then | |
7914 | Formal_Typ := Full_View (Formal_Typ); | |
7915 | end if; | |
7916 | ||
7917 | -- The type of the first parameter must denote the type | |
7918 | -- of the container or acts as its ancestor type. | |
7919 | ||
7920 | return | |
7921 | Formal_Typ = Typ | |
7922 | or else Is_Ancestor (Formal_Typ, Typ); | |
7923 | end if; | |
7924 | end if; | |
7925 | end if; | |
7926 | ||
7927 | return False; | |
7928 | end Is_OK_Candidate; | |
7929 | ||
7930 | ------------------- | |
7931 | -- Record_Interp -- | |
7932 | ------------------- | |
7933 | ||
7934 | procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id) is | |
7935 | begin | |
7936 | if Present (Ref) then | |
7937 | Add_One_Interp (Ref, Subp_Id, Etype (Subp_Id)); | |
7938 | ||
7939 | -- Otherwise this is the first interpretation. Create a reference | |
7940 | -- where all remaining interpretations will be collected. | |
7941 | ||
7942 | else | |
7943 | Ref := New_Occurrence_Of (Subp_Id, Sloc (T)); | |
7944 | end if; | |
7945 | end Record_Interp; | |
7946 | ||
7947 | -- Local variables | |
7948 | ||
7949 | Ref : Node_Id; | |
7950 | Typ : Entity_Id; | |
7951 | ||
7952 | -- Start of processing for Find_Indexing_Operations | |
7953 | ||
7954 | begin | |
7955 | Typ := T; | |
7956 | ||
1e60643a AC |
7957 | -- Use the specific type when the parameter type is class-wide |
7958 | ||
437244c7 AC |
7959 | if Is_Class_Wide_Type (Typ) then |
7960 | Typ := Root_Type (Typ); | |
7961 | end if; | |
7962 | ||
7963 | Ref := Empty; | |
1e60643a | 7964 | Typ := Underlying_Type (Base_Type (Typ)); |
437244c7 | 7965 | |
e11b776b AC |
7966 | Inspect_Primitives (Typ, Ref); |
7967 | ||
7968 | -- Now look for explicit declarations of an indexing operation. | |
7969 | -- If the type is private the operation may be declared in the | |
7970 | -- visible part that contains the partial view. | |
7971 | ||
7972 | if Is_Private_Type (T) then | |
7973 | Inspect_Declarations (T, Ref); | |
7974 | end if; | |
7975 | ||
437244c7 AC |
7976 | Inspect_Declarations (Typ, Ref); |
7977 | ||
7978 | return Ref; | |
7979 | end Find_Indexing_Operations; | |
7980 | ||
fa73fc3d AC |
7981 | -- Local variables |
7982 | ||
d50f4827 | 7983 | Loc : constant Source_Ptr := Sloc (N); |
50878404 | 7984 | Assoc : List_Id; |
fa73fc3d | 7985 | C_Type : Entity_Id; |
d50f4827 AC |
7986 | Func : Entity_Id; |
7987 | Func_Name : Node_Id; | |
7988 | Indexing : Node_Id; | |
d50f4827 | 7989 | |
437244c7 AC |
7990 | Is_Constant_Indexing : Boolean := False; |
7991 | -- This flag reflects the nature of the container indexing. Note that | |
7992 | -- the context may be suited for constant indexing, but the type may | |
7993 | -- lack a Constant_Indexing annotation. | |
7994 | ||
fa73fc3d AC |
7995 | -- Start of processing for Try_Container_Indexing |
7996 | ||
d50f4827 | 7997 | begin |
fa73fc3d AC |
7998 | -- Node may have been analyzed already when testing for a prefixed |
7999 | -- call, in which case do not redo analysis. | |
8000 | ||
8001 | if Present (Generalized_Indexing (N)) then | |
8002 | return True; | |
8003 | end if; | |
8004 | ||
437244c7 | 8005 | C_Type := Pref_Typ; |
f3296dd3 | 8006 | |
fa73fc3d AC |
8007 | -- If indexing a class-wide container, obtain indexing primitive from |
8008 | -- specific type. | |
f3296dd3 AC |
8009 | |
8010 | if Is_Class_Wide_Type (C_Type) then | |
8011 | C_Type := Etype (Base_Type (C_Type)); | |
8012 | end if; | |
d50f4827 | 8013 | |
2cc2e964 | 8014 | -- Check whether the type has a specified indexing aspect |
d50f4827 AC |
8015 | |
8016 | Func_Name := Empty; | |
d50f4827 | 8017 | |
2cc2e964 AC |
8018 | -- The context is suitable for constant indexing, so obtain the name of |
8019 | -- the indexing function from aspect Constant_Indexing. | |
437244c7 | 8020 | |
fa73fc3d | 8021 | if Constant_Indexing_OK then |
d62520f3 | 8022 | Func_Name := |
437244c7 | 8023 | Find_Value_Of_Aspect (Pref_Typ, Aspect_Constant_Indexing); |
9ee76313 | 8024 | end if; |
dedac3eb | 8025 | |
437244c7 AC |
8026 | if Present (Func_Name) then |
8027 | Is_Constant_Indexing := True; | |
8028 | ||
8029 | -- Otherwise attempt variable indexing | |
8030 | ||
8031 | else | |
d62520f3 | 8032 | Func_Name := |
437244c7 | 8033 | Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing); |
9ee76313 | 8034 | end if; |
d50f4827 | 8035 | |
437244c7 AC |
8036 | -- The type is not subject to either form of indexing, therefore the |
8037 | -- indexed component does not denote container indexing. If this is a | |
8038 | -- true error, it is diagnosed by the caller. | |
d50f4827 AC |
8039 | |
8040 | if No (Func_Name) then | |
57a8057a | 8041 | |
437244c7 AC |
8042 | -- The prefix itself may be an indexing of a container. Rewrite it |
8043 | -- as such and retry. | |
57a8057a | 8044 | |
437244c7 AC |
8045 | if Has_Implicit_Dereference (Pref_Typ) then |
8046 | Build_Explicit_Dereference (Prefix, First_Discriminant (Pref_Typ)); | |
50878404 | 8047 | return Try_Container_Indexing (N, Prefix, Exprs); |
57a8057a | 8048 | |
437244c7 AC |
8049 | -- Otherwise this is definitely not container indexing |
8050 | ||
57a8057a AC |
8051 | else |
8052 | return False; | |
8053 | end if; | |
3f433bc0 | 8054 | |
6907542d AC |
8055 | -- If the container type is derived from another container type, the |
8056 | -- value of the inherited aspect is the Reference operation declared | |
8057 | -- for the parent type. | |
8058 | ||
fa73fc3d AC |
8059 | -- However, Reference is also a primitive operation of the type, and the |
8060 | -- inherited operation has a different signature. We retrieve the right | |
8061 | -- ones (the function may be overloaded) from the list of primitive | |
8062 | -- operations of the derived type. | |
3f433bc0 | 8063 | |
fa73fc3d AC |
8064 | -- Note that predefined containers are typically all derived from one of |
8065 | -- the Controlled types. The code below is motivated by containers that | |
8066 | -- are derived from other types with a Reference aspect. | |
6907542d | 8067 | |
f3296dd3 | 8068 | elsif Is_Derived_Type (C_Type) |
437244c7 | 8069 | and then Etype (First_Formal (Entity (Func_Name))) /= Pref_Typ |
6907542d | 8070 | then |
437244c7 AC |
8071 | Func_Name := |
8072 | Find_Indexing_Operations | |
8073 | (T => C_Type, | |
8074 | Nam => Chars (Func_Name), | |
8075 | Is_Constant => Is_Constant_Indexing); | |
d50f4827 AC |
8076 | end if; |
8077 | ||
50878404 AC |
8078 | Assoc := New_List (Relocate_Node (Prefix)); |
8079 | ||
5f50020a | 8080 | -- A generalized indexing may have nore than one index expression, so |
50878404 | 8081 | -- transfer all of them to the argument list to be used in the call. |
5f50020a ES |
8082 | -- Note that there may be named associations, in which case the node |
8083 | -- was rewritten earlier as a call, and has been transformed back into | |
8084 | -- an indexed expression to share the following processing. | |
e917e3b8 | 8085 | |
5f50020a ES |
8086 | -- The generalized indexing node is the one on which analysis and |
8087 | -- resolution take place. Before expansion the original node is replaced | |
fa73fc3d AC |
8088 | -- with the generalized indexing node, which is a call, possibly with a |
8089 | -- dereference operation. | |
50878404 | 8090 | |
e917e3b8 | 8091 | if Comes_From_Source (N) then |
c86cf714 | 8092 | Check_Compiler_Unit ("generalized indexing", N); |
e917e3b8 AC |
8093 | end if; |
8094 | ||
287aa0ed AC |
8095 | -- Create argument list for function call that represents generalized |
8096 | -- indexing. Note that indices (i.e. actuals) may themselves be | |
8097 | -- overloaded. | |
8098 | ||
50878404 | 8099 | declare |
287aa0ed AC |
8100 | Arg : Node_Id; |
8101 | New_Arg : Node_Id; | |
8102 | ||
50878404 AC |
8103 | begin |
8104 | Arg := First (Exprs); | |
8105 | while Present (Arg) loop | |
287aa0ed | 8106 | New_Arg := Relocate_Node (Arg); |
43151cfd ES |
8107 | |
8108 | -- The arguments can be parameter associations, in which case the | |
8109 | -- explicit actual parameter carries the overloadings. | |
8110 | ||
8111 | if Nkind (New_Arg) /= N_Parameter_Association then | |
8112 | Save_Interps (Arg, New_Arg); | |
8113 | end if; | |
8114 | ||
287aa0ed | 8115 | Append (New_Arg, Assoc); |
50878404 AC |
8116 | Next (Arg); |
8117 | end loop; | |
8118 | end; | |
8119 | ||
d50f4827 AC |
8120 | if not Is_Overloaded (Func_Name) then |
8121 | Func := Entity (Func_Name); | |
f4ef7b06 | 8122 | |
29ba9f52 RD |
8123 | Indexing := |
8124 | Make_Function_Call (Loc, | |
8125 | Name => New_Occurrence_Of (Func, Loc), | |
8126 | Parameter_Associations => Assoc); | |
f4ef7b06 | 8127 | |
5f50020a ES |
8128 | Set_Parent (Indexing, Parent (N)); |
8129 | Set_Generalized_Indexing (N, Indexing); | |
8130 | Analyze (Indexing); | |
8131 | Set_Etype (N, Etype (Indexing)); | |
d50f4827 | 8132 | |
76d49f49 ES |
8133 | -- If the return type of the indexing function is a reference type, |
8134 | -- add the dereference as a possible interpretation. Note that the | |
8135 | -- indexing aspect may be a function that returns the element type | |
5f50020a ES |
8136 | -- with no intervening implicit dereference, and that the reference |
8137 | -- discriminant is not the first discriminant. | |
76d49f49 ES |
8138 | |
8139 | if Has_Discriminants (Etype (Func)) then | |
71ff3d18 | 8140 | Check_Implicit_Dereference (N, Etype (Func)); |
76d49f49 | 8141 | end if; |
d50f4827 AC |
8142 | |
8143 | else | |
90b510e4 AC |
8144 | -- If there are multiple indexing functions, build a function call |
8145 | -- and analyze it for each of the possible interpretations. | |
8146 | ||
8b4230c8 AC |
8147 | Indexing := |
8148 | Make_Function_Call (Loc, | |
fa73fc3d AC |
8149 | Name => |
8150 | Make_Identifier (Loc, Chars (Func_Name)), | |
8b4230c8 | 8151 | Parameter_Associations => Assoc); |
5f50020a ES |
8152 | Set_Parent (Indexing, Parent (N)); |
8153 | Set_Generalized_Indexing (N, Indexing); | |
90b510e4 AC |
8154 | Set_Etype (N, Any_Type); |
8155 | Set_Etype (Name (Indexing), Any_Type); | |
d50f4827 AC |
8156 | |
8157 | declare | |
8b4230c8 AC |
8158 | I : Interp_Index; |
8159 | It : Interp; | |
d50f4827 AC |
8160 | Success : Boolean; |
8161 | ||
8162 | begin | |
8163 | Get_First_Interp (Func_Name, I, It); | |
5f50020a | 8164 | Set_Etype (Indexing, Any_Type); |
90b510e4 | 8165 | |
f4ef7b06 | 8166 | -- Analyze each candidate function with the given actuals |
0310af44 | 8167 | |
d50f4827 | 8168 | while Present (It.Nam) loop |
5f50020a | 8169 | Analyze_One_Call (Indexing, It.Nam, False, Success); |
0310af44 AC |
8170 | Get_Next_Interp (I, It); |
8171 | end loop; | |
32bba3c9 | 8172 | |
0310af44 AC |
8173 | -- If there are several successful candidates, resolution will |
8174 | -- be by result. Mark the interpretations of the function name | |
8175 | -- itself. | |
d50f4827 | 8176 | |
0310af44 AC |
8177 | if Is_Overloaded (Indexing) then |
8178 | Get_First_Interp (Indexing, I, It); | |
90b510e4 | 8179 | |
0310af44 | 8180 | while Present (It.Nam) loop |
90b510e4 | 8181 | Add_One_Interp (Name (Indexing), It.Nam, It.Typ); |
0310af44 AC |
8182 | Get_Next_Interp (I, It); |
8183 | end loop; | |
8184 | ||
8185 | else | |
8186 | Set_Etype (Name (Indexing), Etype (Indexing)); | |
8187 | end if; | |
8188 | ||
8189 | -- Now add the candidate interpretations to the indexing node | |
8190 | -- itself, to be replaced later by the function call. | |
8191 | ||
8192 | if Is_Overloaded (Name (Indexing)) then | |
8193 | Get_First_Interp (Name (Indexing), I, It); | |
8194 | ||
8195 | while Present (It.Nam) loop | |
90b510e4 AC |
8196 | Add_One_Interp (N, It.Nam, It.Typ); |
8197 | ||
6c7f7b8c AC |
8198 | -- Add dereference interpretation if the result type has |
8199 | -- implicit reference discriminants. | |
d50f4827 | 8200 | |
76d49f49 | 8201 | if Has_Discriminants (Etype (It.Nam)) then |
71ff3d18 | 8202 | Check_Implicit_Dereference (N, Etype (It.Nam)); |
76d49f49 | 8203 | end if; |
32bba3c9 | 8204 | |
0310af44 AC |
8205 | Get_Next_Interp (I, It); |
8206 | end loop; | |
8207 | ||
8208 | else | |
8209 | Set_Etype (N, Etype (Name (Indexing))); | |
8210 | if Has_Discriminants (Etype (N)) then | |
8211 | Check_Implicit_Dereference (N, Etype (N)); | |
8212 | end if; | |
8213 | end if; | |
d50f4827 AC |
8214 | end; |
8215 | end if; | |
8216 | ||
5f50020a | 8217 | if Etype (Indexing) = Any_Type then |
29ba9f52 RD |
8218 | Error_Msg_NE |
8219 | ("container cannot be indexed with&", N, Etype (First (Exprs))); | |
9ee76313 | 8220 | Rewrite (N, New_Occurrence_Of (Any_Id, Loc)); |
9ee76313 AC |
8221 | end if; |
8222 | ||
d50f4827 AC |
8223 | return True; |
8224 | end Try_Container_Indexing; | |
8225 | ||
996ae0b0 RK |
8226 | ----------------------- |
8227 | -- Try_Indirect_Call -- | |
8228 | ----------------------- | |
8229 | ||
8230 | function Try_Indirect_Call | |
91b1417d AC |
8231 | (N : Node_Id; |
8232 | Nam : Entity_Id; | |
8233 | Typ : Entity_Id) return Boolean | |
996ae0b0 | 8234 | is |
24657705 HK |
8235 | Actual : Node_Id; |
8236 | Formal : Entity_Id; | |
8237 | ||
8a7988f5 | 8238 | Call_OK : Boolean; |
24657705 | 8239 | pragma Warnings (Off, Call_OK); |
996ae0b0 RK |
8240 | |
8241 | begin | |
8a7988f5 | 8242 | Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK); |
9de61fcb | 8243 | |
8a7988f5 | 8244 | Actual := First_Actual (N); |
fbf5a39b | 8245 | Formal := First_Formal (Designated_Type (Typ)); |
9de61fcb | 8246 | while Present (Actual) and then Present (Formal) loop |
996ae0b0 RK |
8247 | if not Has_Compatible_Type (Actual, Etype (Formal)) then |
8248 | return False; | |
8249 | end if; | |
8250 | ||
8251 | Next (Actual); | |
8252 | Next_Formal (Formal); | |
8253 | end loop; | |
8254 | ||
8255 | if No (Actual) and then No (Formal) then | |
8256 | Add_One_Interp (N, Nam, Etype (Designated_Type (Typ))); | |
8257 | ||
8258 | -- Nam is a candidate interpretation for the name in the call, | |
8259 | -- if it is not an indirect call. | |
8260 | ||
8261 | if not Is_Type (Nam) | |
8262 | and then Is_Entity_Name (Name (N)) | |
8263 | then | |
8264 | Set_Entity (Name (N), Nam); | |
8265 | end if; | |
8266 | ||
8267 | return True; | |
8b4230c8 | 8268 | |
996ae0b0 RK |
8269 | else |
8270 | return False; | |
8271 | end if; | |
8272 | end Try_Indirect_Call; | |
8273 | ||
8274 | ---------------------- | |
8275 | -- Try_Indexed_Call -- | |
8276 | ---------------------- | |
8277 | ||
8278 | function Try_Indexed_Call | |
aab883ec ES |
8279 | (N : Node_Id; |
8280 | Nam : Entity_Id; | |
8281 | Typ : Entity_Id; | |
8282 | Skip_First : Boolean) return Boolean | |
996ae0b0 | 8283 | is |
5ff22245 ES |
8284 | Loc : constant Source_Ptr := Sloc (N); |
8285 | Actuals : constant List_Id := Parameter_Associations (N); | |
8286 | Actual : Node_Id; | |
8287 | Index : Entity_Id; | |
996ae0b0 RK |
8288 | |
8289 | begin | |
fbf5a39b | 8290 | Actual := First (Actuals); |
aab883ec ES |
8291 | |
8292 | -- If the call was originally written in prefix form, skip the first | |
8293 | -- actual, which is obviously not defaulted. | |
8294 | ||
8295 | if Skip_First then | |
8296 | Next (Actual); | |
8297 | end if; | |
8298 | ||
fbf5a39b | 8299 | Index := First_Index (Typ); |
9de61fcb RD |
8300 | while Present (Actual) and then Present (Index) loop |
8301 | ||
996ae0b0 RK |
8302 | -- If the parameter list has a named association, the expression |
8303 | -- is definitely a call and not an indexed component. | |
8304 | ||
8305 | if Nkind (Actual) = N_Parameter_Association then | |
8306 | return False; | |
8307 | end if; | |
8308 | ||
5ff22245 ES |
8309 | if Is_Entity_Name (Actual) |
8310 | and then Is_Type (Entity (Actual)) | |
8311 | and then No (Next (Actual)) | |
8312 | then | |
1c218ac3 AC |
8313 | -- A single actual that is a type name indicates a slice if the |
8314 | -- type is discrete, and an error otherwise. | |
8315 | ||
8316 | if Is_Discrete_Type (Entity (Actual)) then | |
8317 | Rewrite (N, | |
8318 | Make_Slice (Loc, | |
22b77f68 RD |
8319 | Prefix => |
8320 | Make_Function_Call (Loc, | |
8321 | Name => Relocate_Node (Name (N))), | |
8322 | Discrete_Range => | |
1c218ac3 AC |
8323 | New_Occurrence_Of (Entity (Actual), Sloc (Actual)))); |
8324 | ||
8325 | Analyze (N); | |
8326 | ||
8327 | else | |
8328 | Error_Msg_N ("invalid use of type in expression", Actual); | |
8329 | Set_Etype (N, Any_Type); | |
8330 | end if; | |
5ff22245 | 8331 | |
5ff22245 ES |
8332 | return True; |
8333 | ||
8334 | elsif not Has_Compatible_Type (Actual, Etype (Index)) then | |
996ae0b0 RK |
8335 | return False; |
8336 | end if; | |
8337 | ||
8338 | Next (Actual); | |
8339 | Next_Index (Index); | |
8340 | end loop; | |
8341 | ||
8342 | if No (Actual) and then No (Index) then | |
8343 | Add_One_Interp (N, Nam, Component_Type (Typ)); | |
8344 | ||
8345 | -- Nam is a candidate interpretation for the name in the call, | |
8346 | -- if it is not an indirect call. | |
8347 | ||
8348 | if not Is_Type (Nam) | |
8349 | and then Is_Entity_Name (Name (N)) | |
8350 | then | |
8351 | Set_Entity (Name (N), Nam); | |
8352 | end if; | |
8353 | ||
8354 | return True; | |
8355 | else | |
8356 | return False; | |
8357 | end if; | |
996ae0b0 RK |
8358 | end Try_Indexed_Call; |
8359 | ||
35ae2ed8 AC |
8360 | -------------------------- |
8361 | -- Try_Object_Operation -- | |
8362 | -------------------------- | |
8363 | ||
8cf23b91 AC |
8364 | function Try_Object_Operation |
8365 | (N : Node_Id; CW_Test_Only : Boolean := False) return Boolean | |
8366 | is | |
b67a385c | 8367 | K : constant Node_Kind := Nkind (Parent (N)); |
d3b00ce3 | 8368 | Is_Subprg_Call : constant Boolean := K in N_Subprogram_Call; |
b67a385c | 8369 | Loc : constant Source_Ptr := Sloc (N); |
b67a385c | 8370 | Obj : constant Node_Id := Prefix (N); |
0d57c6f4 | 8371 | |
48c8c473 | 8372 | Subprog : constant Node_Id := |
0d57c6f4 RD |
8373 | Make_Identifier (Sloc (Selector_Name (N)), |
8374 | Chars => Chars (Selector_Name (N))); | |
401093c1 | 8375 | -- Identifier on which possible interpretations will be collected |
0a36105d | 8376 | |
b67a385c | 8377 | Report_Error : Boolean := False; |
8b4230c8 AC |
8378 | -- If no candidate interpretation matches the context, redo analysis |
8379 | -- with Report_Error True to provide additional information. | |
28d6470f JM |
8380 | |
8381 | Actual : Node_Id; | |
d469eabe | 8382 | Candidate : Entity_Id := Empty; |
48c8c473 | 8383 | New_Call_Node : Node_Id := Empty; |
4c46b835 | 8384 | Node_To_Replace : Node_Id; |
28d6470f | 8385 | Obj_Type : Entity_Id := Etype (Obj); |
48c8c473 | 8386 | Success : Boolean := False; |
0a36105d | 8387 | |
4c46b835 AC |
8388 | procedure Complete_Object_Operation |
8389 | (Call_Node : Node_Id; | |
0a36105d | 8390 | Node_To_Replace : Node_Id); |
ec6078e3 ES |
8391 | -- Make Subprog the name of Call_Node, replace Node_To_Replace with |
8392 | -- Call_Node, insert the object (or its dereference) as the first actual | |
8393 | -- in the call, and complete the analysis of the call. | |
4c46b835 | 8394 | |
0a36105d | 8395 | procedure Report_Ambiguity (Op : Entity_Id); |
48c8c473 AC |
8396 | -- If a prefixed procedure call is ambiguous, indicate whether the call |
8397 | -- includes an implicit dereference or an implicit 'Access. | |
0a36105d | 8398 | |
4c46b835 AC |
8399 | procedure Transform_Object_Operation |
8400 | (Call_Node : out Node_Id; | |
0a36105d | 8401 | Node_To_Replace : out Node_Id); |
ec6078e3 | 8402 | -- Transform Obj.Operation (X, Y,,) into Operation (Obj, X, Y ..) |
d469eabe HK |
8403 | -- Call_Node is the resulting subprogram call, Node_To_Replace is |
8404 | -- either N or the parent of N, and Subprog is a reference to the | |
8405 | -- subprogram we are trying to match. | |
35ae2ed8 AC |
8406 | |
8407 | function Try_Class_Wide_Operation | |
4c46b835 AC |
8408 | (Call_Node : Node_Id; |
8409 | Node_To_Replace : Node_Id) return Boolean; | |
48c8c473 AC |
8410 | -- Traverse all ancestor types looking for a class-wide subprogram for |
8411 | -- which the current operation is a valid non-dispatching call. | |
35ae2ed8 | 8412 | |
0a36105d JM |
8413 | procedure Try_One_Prefix_Interpretation (T : Entity_Id); |
8414 | -- If prefix is overloaded, its interpretation may include different | |
48c8c473 AC |
8415 | -- tagged types, and we must examine the primitive operations and the |
8416 | -- class-wide operations of each in order to find candidate | |
0a36105d JM |
8417 | -- interpretations for the call as a whole. |
8418 | ||
4c46b835 AC |
8419 | function Try_Primitive_Operation |
8420 | (Call_Node : Node_Id; | |
8421 | Node_To_Replace : Node_Id) return Boolean; | |
ec6078e3 | 8422 | -- Traverse the list of primitive subprograms looking for a dispatching |
48c8c473 | 8423 | -- operation for which the current node is a valid call. |
0a36105d JM |
8424 | |
8425 | function Valid_Candidate | |
8426 | (Success : Boolean; | |
8427 | Call : Node_Id; | |
48c8c473 AC |
8428 | Subp : Entity_Id) return Entity_Id; |
8429 | -- If the subprogram is a valid interpretation, record it, and add to | |
8430 | -- the list of interpretations of Subprog. Otherwise return Empty. | |
0a36105d | 8431 | |
4c46b835 AC |
8432 | ------------------------------- |
8433 | -- Complete_Object_Operation -- | |
8434 | ------------------------------- | |
8435 | ||
8436 | procedure Complete_Object_Operation | |
8437 | (Call_Node : Node_Id; | |
0a36105d | 8438 | Node_To_Replace : Node_Id) |
4c46b835 | 8439 | is |
b4592168 GD |
8440 | Control : constant Entity_Id := First_Formal (Entity (Subprog)); |
8441 | Formal_Type : constant Entity_Id := Etype (Control); | |
ec6078e3 ES |
8442 | First_Actual : Node_Id; |
8443 | ||
4c46b835 | 8444 | begin |
955871d3 AC |
8445 | -- Place the name of the operation, with its interpretations, |
8446 | -- on the rewritten call. | |
0a36105d | 8447 | |
ec6078e3 ES |
8448 | Set_Name (Call_Node, Subprog); |
8449 | ||
0a36105d JM |
8450 | First_Actual := First (Parameter_Associations (Call_Node)); |
8451 | ||
8b4230c8 AC |
8452 | -- For cross-reference purposes, treat the new node as being in the |
8453 | -- source if the original one is. Set entity and type, even though | |
8454 | -- they may be overwritten during resolution if overloaded. | |
b67a385c ES |
8455 | |
8456 | Set_Comes_From_Source (Subprog, Comes_From_Source (N)); | |
8457 | Set_Comes_From_Source (Call_Node, Comes_From_Source (N)); | |
8458 | ||
ec6078e3 | 8459 | if Nkind (N) = N_Selected_Component |
3d918396 | 8460 | and then not Inside_A_Generic |
ec6078e3 ES |
8461 | then |
8462 | Set_Entity (Selector_Name (N), Entity (Subprog)); | |
b2ab8c33 | 8463 | Set_Etype (Selector_Name (N), Etype (Entity (Subprog))); |
ec6078e3 ES |
8464 | end if; |
8465 | ||
8b4230c8 AC |
8466 | -- If need be, rewrite first actual as an explicit dereference. If |
8467 | -- the call is overloaded, the rewriting can only be done once the | |
8468 | -- primitive operation is identified. | |
0a36105d JM |
8469 | |
8470 | if Is_Overloaded (Subprog) then | |
ec6078e3 | 8471 | |
0a36105d JM |
8472 | -- The prefix itself may be overloaded, and its interpretations |
8473 | -- must be propagated to the new actual in the call. | |
8474 | ||
8475 | if Is_Overloaded (Obj) then | |
8476 | Save_Interps (Obj, First_Actual); | |
8477 | end if; | |
8478 | ||
8479 | Rewrite (First_Actual, Obj); | |
8480 | ||
8481 | elsif not Is_Access_Type (Formal_Type) | |
ec6078e3 ES |
8482 | and then Is_Access_Type (Etype (Obj)) |
8483 | then | |
8484 | Rewrite (First_Actual, | |
8485 | Make_Explicit_Dereference (Sloc (Obj), Obj)); | |
8486 | Analyze (First_Actual); | |
fe45e59e | 8487 | |
401093c1 ES |
8488 | -- If we need to introduce an explicit dereference, verify that |
8489 | -- the resulting actual is compatible with the mode of the formal. | |
8490 | ||
8491 | if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter | |
8492 | and then Is_Access_Constant (Etype (Obj)) | |
8493 | then | |
8494 | Error_Msg_NE | |
8495 | ("expect variable in call to&", Prefix (N), Entity (Subprog)); | |
8496 | end if; | |
8497 | ||
d469eabe HK |
8498 | -- Conversely, if the formal is an access parameter and the object |
8499 | -- is not, replace the actual with a 'Access reference. Its analysis | |
8500 | -- will check that the object is aliased. | |
fe45e59e ES |
8501 | |
8502 | elsif Is_Access_Type (Formal_Type) | |
8503 | and then not Is_Access_Type (Etype (Obj)) | |
8504 | then | |
b4592168 GD |
8505 | -- A special case: A.all'access is illegal if A is an access to a |
8506 | -- constant and the context requires an access to a variable. | |
8507 | ||
8508 | if not Is_Access_Constant (Formal_Type) then | |
8509 | if (Nkind (Obj) = N_Explicit_Dereference | |
8510 | and then Is_Access_Constant (Etype (Prefix (Obj)))) | |
8511 | or else not Is_Variable (Obj) | |
8512 | then | |
8513 | Error_Msg_NE | |
ad075b50 | 8514 | ("actual for & must be a variable", Obj, Control); |
b4592168 GD |
8515 | end if; |
8516 | end if; | |
8517 | ||
fe45e59e ES |
8518 | Rewrite (First_Actual, |
8519 | Make_Attribute_Reference (Loc, | |
8520 | Attribute_Name => Name_Access, | |
8521 | Prefix => Relocate_Node (Obj))); | |
0a36105d | 8522 | |
8f34c90b AC |
8523 | -- If the object is not overloaded verify that taking access of |
8524 | -- it is legal. Otherwise check is made during resolution. | |
8525 | ||
8526 | if not Is_Overloaded (Obj) | |
8527 | and then not Is_Aliased_View (Obj) | |
8528 | then | |
ed2233dc | 8529 | Error_Msg_NE |
ad075b50 | 8530 | ("object in prefixed call to & must be aliased " |
715e529d | 8531 | & "(RM 4.1.3 (13 1/2))", Prefix (First_Actual), Subprog); |
0a36105d JM |
8532 | end if; |
8533 | ||
fe45e59e ES |
8534 | Analyze (First_Actual); |
8535 | ||
ec6078e3 | 8536 | else |
0a36105d JM |
8537 | if Is_Overloaded (Obj) then |
8538 | Save_Interps (Obj, First_Actual); | |
8539 | end if; | |
ec6078e3 | 8540 | |
0a36105d | 8541 | Rewrite (First_Actual, Obj); |
aab883ec ES |
8542 | end if; |
8543 | ||
e699b76e AC |
8544 | -- The operation is obtained from the dispatch table and not by |
8545 | -- visibility, and may be declared in a unit that is not explicitly | |
8546 | -- referenced in the source, but is nevertheless required in the | |
8547 | -- context of the current unit. Indicate that operation and its scope | |
8548 | -- are referenced, to prevent spurious and misleading warnings. If | |
8549 | -- the operation is overloaded, all primitives are in the same scope | |
8550 | -- and we can use any of them. | |
8551 | ||
8552 | Set_Referenced (Entity (Subprog), True); | |
8553 | Set_Referenced (Scope (Entity (Subprog)), True); | |
8554 | ||
7ffd9312 | 8555 | Rewrite (Node_To_Replace, Call_Node); |
0a36105d JM |
8556 | |
8557 | -- Propagate the interpretations collected in subprog to the new | |
8558 | -- function call node, to be resolved from context. | |
8559 | ||
8560 | if Is_Overloaded (Subprog) then | |
8561 | Save_Interps (Subprog, Node_To_Replace); | |
7415029d | 8562 | |
0a36105d | 8563 | else |
28e18b4f AC |
8564 | -- The type of the subprogram may be a limited view obtained |
8565 | -- transitively from another unit. If full view is available, | |
8566 | -- use it to analyze call. | |
8567 | ||
8568 | declare | |
8569 | T : constant Entity_Id := Etype (Subprog); | |
8570 | begin | |
8571 | if From_Limited_With (T) then | |
8572 | Set_Etype (Entity (Subprog), Available_View (T)); | |
8573 | end if; | |
8574 | end; | |
8575 | ||
0a36105d | 8576 | Analyze (Node_To_Replace); |
438ff97c | 8577 | |
199c6a10 AC |
8578 | -- If the operation has been rewritten into a call, which may get |
8579 | -- subsequently an explicit dereference, preserve the type on the | |
8580 | -- original node (selected component or indexed component) for | |
8581 | -- subsequent legality tests, e.g. Is_Variable. which examines | |
8582 | -- the original node. | |
438ff97c ES |
8583 | |
8584 | if Nkind (Node_To_Replace) = N_Function_Call then | |
8585 | Set_Etype | |
8586 | (Original_Node (Node_To_Replace), Etype (Node_To_Replace)); | |
8587 | end if; | |
0a36105d | 8588 | end if; |
4c46b835 AC |
8589 | end Complete_Object_Operation; |
8590 | ||
0a36105d JM |
8591 | ---------------------- |
8592 | -- Report_Ambiguity -- | |
8593 | ---------------------- | |
8594 | ||
8595 | procedure Report_Ambiguity (Op : Entity_Id) is | |
0a36105d JM |
8596 | Access_Actual : constant Boolean := |
8597 | Is_Access_Type (Etype (Prefix (N))); | |
8cf23b91 | 8598 | Access_Formal : Boolean := False; |
0a36105d JM |
8599 | |
8600 | begin | |
8601 | Error_Msg_Sloc := Sloc (Op); | |
8602 | ||
8cf23b91 AC |
8603 | if Present (First_Formal (Op)) then |
8604 | Access_Formal := Is_Access_Type (Etype (First_Formal (Op))); | |
8605 | end if; | |
8606 | ||
0a36105d JM |
8607 | if Access_Formal and then not Access_Actual then |
8608 | if Nkind (Parent (Op)) = N_Full_Type_Declaration then | |
ed2233dc | 8609 | Error_Msg_N |
8b4230c8 AC |
8610 | ("\possible interpretation " |
8611 | & "(inherited, with implicit 'Access) #", N); | |
0a36105d | 8612 | else |
ed2233dc | 8613 | Error_Msg_N |
0a36105d JM |
8614 | ("\possible interpretation (with implicit 'Access) #", N); |
8615 | end if; | |
8616 | ||
8617 | elsif not Access_Formal and then Access_Actual then | |
8618 | if Nkind (Parent (Op)) = N_Full_Type_Declaration then | |
ed2233dc | 8619 | Error_Msg_N |
8b4230c8 | 8620 | ("\possible interpretation " |
28e18b4f | 8621 | & "(inherited, with implicit dereference) #", N); |
0a36105d | 8622 | else |
ed2233dc | 8623 | Error_Msg_N |
0a36105d JM |
8624 | ("\possible interpretation (with implicit dereference) #", N); |
8625 | end if; | |
8626 | ||
8627 | else | |
8628 | if Nkind (Parent (Op)) = N_Full_Type_Declaration then | |
ed2233dc | 8629 | Error_Msg_N ("\possible interpretation (inherited)#", N); |
0a36105d | 8630 | else |
4e7a4f6e AC |
8631 | Error_Msg_N -- CODEFIX |
8632 | ("\possible interpretation#", N); | |
0a36105d JM |
8633 | end if; |
8634 | end if; | |
8635 | end Report_Ambiguity; | |
8636 | ||
4c46b835 AC |
8637 | -------------------------------- |
8638 | -- Transform_Object_Operation -- | |
8639 | -------------------------------- | |
8640 | ||
8641 | procedure Transform_Object_Operation | |
8642 | (Call_Node : out Node_Id; | |
0a36105d | 8643 | Node_To_Replace : out Node_Id) |
35ae2ed8 | 8644 | is |
ec6078e3 ES |
8645 | Dummy : constant Node_Id := New_Copy (Obj); |
8646 | -- Placeholder used as a first parameter in the call, replaced | |
8647 | -- eventually by the proper object. | |
8648 | ||
d469eabe HK |
8649 | Parent_Node : constant Node_Id := Parent (N); |
8650 | ||
ec6078e3 | 8651 | Actual : Node_Id; |
d469eabe | 8652 | Actuals : List_Id; |
ec6078e3 | 8653 | |
35ae2ed8 | 8654 | begin |
40b4bc2d AC |
8655 | -- Obj may already have been rewritten if it involves an implicit |
8656 | -- dereference (e.g. if it is an access to a limited view). Preserve | |
8657 | -- a link to the original node for ASIS use. | |
8658 | ||
8659 | if not Comes_From_Source (Obj) then | |
8660 | Set_Original_Node (Dummy, Original_Node (Obj)); | |
8661 | end if; | |
8662 | ||
ec6078e3 ES |
8663 | -- Common case covering 1) Call to a procedure and 2) Call to a |
8664 | -- function that has some additional actuals. | |
35ae2ed8 | 8665 | |
d3b00ce3 | 8666 | if Nkind (Parent_Node) in N_Subprogram_Call |
35ae2ed8 | 8667 | |
ec6078e3 ES |
8668 | -- N is a selected component node containing the name of the |
8669 | -- subprogram. If N is not the name of the parent node we must | |
8670 | -- not replace the parent node by the new construct. This case | |
8671 | -- occurs when N is a parameterless call to a subprogram that | |
8672 | -- is an actual parameter of a call to another subprogram. For | |
8673 | -- example: | |
8674 | -- Some_Subprogram (..., Obj.Operation, ...) | |
35ae2ed8 | 8675 | |
ec6078e3 | 8676 | and then Name (Parent_Node) = N |
4c46b835 AC |
8677 | then |
8678 | Node_To_Replace := Parent_Node; | |
35ae2ed8 | 8679 | |
ec6078e3 | 8680 | Actuals := Parameter_Associations (Parent_Node); |
d3e65aad | 8681 | |
ec6078e3 ES |
8682 | if Present (Actuals) then |
8683 | Prepend (Dummy, Actuals); | |
8684 | else | |
8685 | Actuals := New_List (Dummy); | |
8686 | end if; | |
4c46b835 AC |
8687 | |
8688 | if Nkind (Parent_Node) = N_Procedure_Call_Statement then | |
8689 | Call_Node := | |
8690 | Make_Procedure_Call_Statement (Loc, | |
48c8c473 | 8691 | Name => New_Copy (Subprog), |
4c46b835 AC |
8692 | Parameter_Associations => Actuals); |
8693 | ||
8694 | else | |
4c46b835 AC |
8695 | Call_Node := |
8696 | Make_Function_Call (Loc, | |
8b4230c8 | 8697 | Name => New_Copy (Subprog), |
4c46b835 | 8698 | Parameter_Associations => Actuals); |
35ae2ed8 AC |
8699 | end if; |
8700 | ||
d469eabe | 8701 | -- Before analysis, a function call appears as an indexed component |
ec6078e3 | 8702 | -- if there are no named associations. |
758c442c | 8703 | |
c8307596 | 8704 | elsif Nkind (Parent_Node) = N_Indexed_Component |
ec6078e3 ES |
8705 | and then N = Prefix (Parent_Node) |
8706 | then | |
758c442c | 8707 | Node_To_Replace := Parent_Node; |
ec6078e3 ES |
8708 | Actuals := Expressions (Parent_Node); |
8709 | ||
8710 | Actual := First (Actuals); | |
8711 | while Present (Actual) loop | |
8712 | Analyze (Actual); | |
8713 | Next (Actual); | |
8714 | end loop; | |
8715 | ||
8716 | Prepend (Dummy, Actuals); | |
758c442c GD |
8717 | |
8718 | Call_Node := | |
8719 | Make_Function_Call (Loc, | |
8b4230c8 | 8720 | Name => New_Copy (Subprog), |
758c442c GD |
8721 | Parameter_Associations => Actuals); |
8722 | ||
d469eabe | 8723 | -- Parameterless call: Obj.F is rewritten as F (Obj) |
35ae2ed8 | 8724 | |
4c46b835 AC |
8725 | else |
8726 | Node_To_Replace := N; | |
8727 | ||
8728 | Call_Node := | |
8729 | Make_Function_Call (Loc, | |
8b4230c8 | 8730 | Name => New_Copy (Subprog), |
ec6078e3 | 8731 | Parameter_Associations => New_List (Dummy)); |
4c46b835 AC |
8732 | end if; |
8733 | end Transform_Object_Operation; | |
35ae2ed8 AC |
8734 | |
8735 | ------------------------------ | |
8736 | -- Try_Class_Wide_Operation -- | |
8737 | ------------------------------ | |
8738 | ||
8739 | function Try_Class_Wide_Operation | |
4c46b835 AC |
8740 | (Call_Node : Node_Id; |
8741 | Node_To_Replace : Node_Id) return Boolean | |
35ae2ed8 | 8742 | is |
0a36105d JM |
8743 | Anc_Type : Entity_Id; |
8744 | Matching_Op : Entity_Id := Empty; | |
8745 | Error : Boolean; | |
8746 | ||
8747 | procedure Traverse_Homonyms | |
8748 | (Anc_Type : Entity_Id; | |
8749 | Error : out Boolean); | |
8750 | -- Traverse the homonym chain of the subprogram searching for those | |
8751 | -- homonyms whose first formal has the Anc_Type's class-wide type, | |
d469eabe HK |
8752 | -- or an anonymous access type designating the class-wide type. If |
8753 | -- an ambiguity is detected, then Error is set to True. | |
0a36105d JM |
8754 | |
8755 | procedure Traverse_Interfaces | |
8756 | (Anc_Type : Entity_Id; | |
8757 | Error : out Boolean); | |
8758 | -- Traverse the list of interfaces, if any, associated with Anc_Type | |
8759 | -- and search for acceptable class-wide homonyms associated with each | |
8760 | -- interface. If an ambiguity is detected, then Error is set to True. | |
8761 | ||
8762 | ----------------------- | |
8763 | -- Traverse_Homonyms -- | |
8764 | ----------------------- | |
8765 | ||
8766 | procedure Traverse_Homonyms | |
8767 | (Anc_Type : Entity_Id; | |
8768 | Error : out Boolean) | |
8769 | is | |
8770 | Cls_Type : Entity_Id; | |
8771 | Hom : Entity_Id; | |
8772 | Hom_Ref : Node_Id; | |
8773 | Success : Boolean; | |
35ae2ed8 | 8774 | |
0a36105d JM |
8775 | begin |
8776 | Error := False; | |
ec6078e3 | 8777 | |
b67a385c ES |
8778 | Cls_Type := Class_Wide_Type (Anc_Type); |
8779 | ||
4c46b835 | 8780 | Hom := Current_Entity (Subprog); |
401093c1 | 8781 | |
383e179e AC |
8782 | -- Find a non-hidden operation whose first parameter is of the |
8783 | -- class-wide type, a subtype thereof, or an anonymous access | |
a68d415b | 8784 | -- to same. If in an instance, the operation can be considered |
8b4230c8 AC |
8785 | -- even if hidden (it may be hidden because the instantiation |
8786 | -- is expanded after the containing package has been analyzed). | |
401093c1 | 8787 | |
35ae2ed8 | 8788 | while Present (Hom) loop |
6a2e4f0b | 8789 | if Ekind_In (Hom, E_Procedure, E_Function) |
a68d415b | 8790 | and then (not Is_Hidden (Hom) or else In_Instance) |
b67a385c | 8791 | and then Scope (Hom) = Scope (Anc_Type) |
4c46b835 | 8792 | and then Present (First_Formal (Hom)) |
b67a385c | 8793 | and then |
401093c1 | 8794 | (Base_Type (Etype (First_Formal (Hom))) = Cls_Type |
b67a385c ES |
8795 | or else |
8796 | (Is_Access_Type (Etype (First_Formal (Hom))) | |
8b4230c8 AC |
8797 | and then |
8798 | Ekind (Etype (First_Formal (Hom))) = | |
8799 | E_Anonymous_Access_Type | |
8800 | and then | |
8801 | Base_Type | |
8802 | (Designated_Type (Etype (First_Formal (Hom)))) = | |
0a36105d | 8803 | Cls_Type)) |
35ae2ed8 | 8804 | then |
88f47280 AC |
8805 | -- If the context is a procedure call, ignore functions |
8806 | -- in the name of the call. | |
8807 | ||
8808 | if Ekind (Hom) = E_Function | |
8809 | and then Nkind (Parent (N)) = N_Procedure_Call_Statement | |
8810 | and then N = Name (Parent (N)) | |
8811 | then | |
8812 | goto Next_Hom; | |
11fa950b AC |
8813 | |
8814 | -- If the context is a function call, ignore procedures | |
8815 | -- in the name of the call. | |
8816 | ||
8817 | elsif Ekind (Hom) = E_Procedure | |
8818 | and then Nkind (Parent (N)) /= N_Procedure_Call_Statement | |
8819 | then | |
8820 | goto Next_Hom; | |
88f47280 AC |
8821 | end if; |
8822 | ||
ec6078e3 | 8823 | Set_Etype (Call_Node, Any_Type); |
0a36105d JM |
8824 | Set_Is_Overloaded (Call_Node, False); |
8825 | Success := False; | |
4c46b835 | 8826 | |
0a36105d | 8827 | if No (Matching_Op) then |
e4494292 | 8828 | Hom_Ref := New_Occurrence_Of (Hom, Sloc (Subprog)); |
0a36105d JM |
8829 | Set_Etype (Call_Node, Any_Type); |
8830 | Set_Parent (Call_Node, Parent (Node_To_Replace)); | |
4c46b835 | 8831 | |
0a36105d | 8832 | Set_Name (Call_Node, Hom_Ref); |
4c46b835 | 8833 | |
0a36105d JM |
8834 | Analyze_One_Call |
8835 | (N => Call_Node, | |
8836 | Nam => Hom, | |
8837 | Report => Report_Error, | |
8838 | Success => Success, | |
8839 | Skip_First => True); | |
4c46b835 | 8840 | |
0a36105d JM |
8841 | Matching_Op := |
8842 | Valid_Candidate (Success, Call_Node, Hom); | |
4c46b835 | 8843 | |
0a36105d JM |
8844 | else |
8845 | Analyze_One_Call | |
8846 | (N => Call_Node, | |
8847 | Nam => Hom, | |
8848 | Report => Report_Error, | |
8849 | Success => Success, | |
8850 | Skip_First => True); | |
8851 | ||
8852 | if Present (Valid_Candidate (Success, Call_Node, Hom)) | |
8853 | and then Nkind (Call_Node) /= N_Function_Call | |
8854 | then | |
ed2233dc | 8855 | Error_Msg_NE ("ambiguous call to&", N, Hom); |
0a36105d JM |
8856 | Report_Ambiguity (Matching_Op); |
8857 | Report_Ambiguity (Hom); | |
8858 | Error := True; | |
8859 | return; | |
8860 | end if; | |
35ae2ed8 AC |
8861 | end if; |
8862 | end if; | |
8863 | ||
88f47280 AC |
8864 | <<Next_Hom>> |
8865 | Hom := Homonym (Hom); | |
35ae2ed8 | 8866 | end loop; |
0a36105d JM |
8867 | end Traverse_Homonyms; |
8868 | ||
8869 | ------------------------- | |
8870 | -- Traverse_Interfaces -- | |
8871 | ------------------------- | |
35ae2ed8 | 8872 | |
0a36105d JM |
8873 | procedure Traverse_Interfaces |
8874 | (Anc_Type : Entity_Id; | |
8875 | Error : out Boolean) | |
8876 | is | |
0a36105d JM |
8877 | Intface_List : constant List_Id := |
8878 | Abstract_Interface_List (Anc_Type); | |
d469eabe | 8879 | Intface : Node_Id; |
0a36105d JM |
8880 | |
8881 | begin | |
8882 | Error := False; | |
8883 | ||
8884 | if Is_Non_Empty_List (Intface_List) then | |
8885 | Intface := First (Intface_List); | |
8886 | while Present (Intface) loop | |
8887 | ||
8888 | -- Look for acceptable class-wide homonyms associated with | |
8889 | -- the interface. | |
8890 | ||
8891 | Traverse_Homonyms (Etype (Intface), Error); | |
8892 | ||
8893 | if Error then | |
8894 | return; | |
8895 | end if; | |
8896 | ||
8897 | -- Continue the search by looking at each of the interface's | |
8898 | -- associated interface ancestors. | |
8899 | ||
8900 | Traverse_Interfaces (Etype (Intface), Error); | |
8901 | ||
8902 | if Error then | |
8903 | return; | |
8904 | end if; | |
8905 | ||
8906 | Next (Intface); | |
8907 | end loop; | |
8908 | end if; | |
8909 | end Traverse_Interfaces; | |
8910 | ||
8911 | -- Start of processing for Try_Class_Wide_Operation | |
8912 | ||
8913 | begin | |
8cf23b91 AC |
8914 | -- If we are searching only for conflicting class-wide subprograms |
8915 | -- then initialize directly Matching_Op with the target entity. | |
8916 | ||
8917 | if CW_Test_Only then | |
8918 | Matching_Op := Entity (Selector_Name (N)); | |
8919 | end if; | |
8920 | ||
d469eabe HK |
8921 | -- Loop through ancestor types (including interfaces), traversing |
8922 | -- the homonym chain of the subprogram, trying out those homonyms | |
8923 | -- whose first formal has the class-wide type of the ancestor, or | |
8924 | -- an anonymous access type designating the class-wide type. | |
0a36105d JM |
8925 | |
8926 | Anc_Type := Obj_Type; | |
8927 | loop | |
8928 | -- Look for a match among homonyms associated with the ancestor | |
8929 | ||
8930 | Traverse_Homonyms (Anc_Type, Error); | |
8931 | ||
8932 | if Error then | |
8933 | return True; | |
8934 | end if; | |
8935 | ||
8936 | -- Continue the search for matches among homonyms associated with | |
8937 | -- any interfaces implemented by the ancestor. | |
8938 | ||
8939 | Traverse_Interfaces (Anc_Type, Error); | |
8940 | ||
8941 | if Error then | |
8942 | return True; | |
8943 | end if; | |
35ae2ed8 | 8944 | |
4c46b835 AC |
8945 | exit when Etype (Anc_Type) = Anc_Type; |
8946 | Anc_Type := Etype (Anc_Type); | |
35ae2ed8 AC |
8947 | end loop; |
8948 | ||
0a36105d JM |
8949 | if Present (Matching_Op) then |
8950 | Set_Etype (Call_Node, Etype (Matching_Op)); | |
8951 | end if; | |
ec6078e3 | 8952 | |
0a36105d | 8953 | return Present (Matching_Op); |
35ae2ed8 AC |
8954 | end Try_Class_Wide_Operation; |
8955 | ||
0a36105d JM |
8956 | ----------------------------------- |
8957 | -- Try_One_Prefix_Interpretation -- | |
8958 | ----------------------------------- | |
8959 | ||
8960 | procedure Try_One_Prefix_Interpretation (T : Entity_Id) is | |
48c8c473 | 8961 | Prev_Obj_Type : constant Entity_Id := Obj_Type; |
e3d9f448 AC |
8962 | -- If the interpretation does not have a valid candidate type, |
8963 | -- preserve current value of Obj_Type for subsequent errors. | |
8964 | ||
0a36105d JM |
8965 | begin |
8966 | Obj_Type := T; | |
8967 | ||
8968 | if Is_Access_Type (Obj_Type) then | |
8969 | Obj_Type := Designated_Type (Obj_Type); | |
8970 | end if; | |
8971 | ||
48c8c473 AC |
8972 | if Ekind_In (Obj_Type, E_Private_Subtype, |
8973 | E_Record_Subtype_With_Private) | |
8974 | then | |
0a36105d JM |
8975 | Obj_Type := Base_Type (Obj_Type); |
8976 | end if; | |
8977 | ||
8978 | if Is_Class_Wide_Type (Obj_Type) then | |
8979 | Obj_Type := Etype (Class_Wide_Type (Obj_Type)); | |
8980 | end if; | |
8981 | ||
8982 | -- The type may have be obtained through a limited_with clause, | |
8983 | -- in which case the primitive operations are available on its | |
401093c1 | 8984 | -- non-limited view. If still incomplete, retrieve full view. |
0a36105d JM |
8985 | |
8986 | if Ekind (Obj_Type) = E_Incomplete_Type | |
7b56a91b | 8987 | and then From_Limited_With (Obj_Type) |
47346923 | 8988 | and then Has_Non_Limited_View (Obj_Type) |
0a36105d | 8989 | then |
401093c1 | 8990 | Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type)); |
0a36105d JM |
8991 | end if; |
8992 | ||
8993 | -- If the object is not tagged, or the type is still an incomplete | |
48c8c473 AC |
8994 | -- type, this is not a prefixed call. Restore the previous type as |
8995 | -- the current one is not a legal candidate. | |
0a36105d JM |
8996 | |
8997 | if not Is_Tagged_Type (Obj_Type) | |
8998 | or else Is_Incomplete_Type (Obj_Type) | |
8999 | then | |
e3d9f448 | 9000 | Obj_Type := Prev_Obj_Type; |
0a36105d JM |
9001 | return; |
9002 | end if; | |
9003 | ||
11fa950b AC |
9004 | declare |
9005 | Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node); | |
9006 | CW_Result : Boolean; | |
9007 | Prim_Result : Boolean; | |
9008 | pragma Unreferenced (CW_Result); | |
9009 | ||
9010 | begin | |
8cf23b91 AC |
9011 | if not CW_Test_Only then |
9012 | Prim_Result := | |
9013 | Try_Primitive_Operation | |
9014 | (Call_Node => New_Call_Node, | |
9015 | Node_To_Replace => Node_To_Replace); | |
9016 | end if; | |
11fa950b AC |
9017 | |
9018 | -- Check if there is a class-wide subprogram covering the | |
9019 | -- primitive. This check must be done even if a candidate | |
9020 | -- was found in order to report ambiguous calls. | |
9021 | ||
48c8c473 | 9022 | if not Prim_Result then |
11fa950b AC |
9023 | CW_Result := |
9024 | Try_Class_Wide_Operation | |
9025 | (Call_Node => New_Call_Node, | |
9026 | Node_To_Replace => Node_To_Replace); | |
9027 | ||
9028 | -- If we found a primitive we search for class-wide subprograms | |
9029 | -- using a duplicate of the call node (done to avoid missing its | |
9030 | -- decoration if there is no ambiguity). | |
9031 | ||
9032 | else | |
9033 | CW_Result := | |
9034 | Try_Class_Wide_Operation | |
9035 | (Call_Node => Dup_Call_Node, | |
9036 | Node_To_Replace => Node_To_Replace); | |
9037 | end if; | |
9038 | end; | |
0a36105d JM |
9039 | end Try_One_Prefix_Interpretation; |
9040 | ||
4c46b835 AC |
9041 | ----------------------------- |
9042 | -- Try_Primitive_Operation -- | |
9043 | ----------------------------- | |
35ae2ed8 | 9044 | |
4c46b835 AC |
9045 | function Try_Primitive_Operation |
9046 | (Call_Node : Node_Id; | |
9047 | Node_To_Replace : Node_Id) return Boolean | |
35ae2ed8 | 9048 | is |
6e73e3ab AC |
9049 | Elmt : Elmt_Id; |
9050 | Prim_Op : Entity_Id; | |
0a36105d JM |
9051 | Matching_Op : Entity_Id := Empty; |
9052 | Prim_Op_Ref : Node_Id := Empty; | |
9053 | ||
8b4230c8 | 9054 | Corr_Type : Entity_Id := Empty; |
0a36105d JM |
9055 | -- If the prefix is a synchronized type, the controlling type of |
9056 | -- the primitive operation is the corresponding record type, else | |
9057 | -- this is the object type itself. | |
9058 | ||
8b4230c8 | 9059 | Success : Boolean := False; |
35ae2ed8 | 9060 | |
401093c1 ES |
9061 | function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id; |
9062 | -- For tagged types the candidate interpretations are found in | |
9063 | -- the list of primitive operations of the type and its ancestors. | |
9064 | -- For formal tagged types we have to find the operations declared | |
9065 | -- in the same scope as the type (including in the generic formal | |
9066 | -- part) because the type itself carries no primitive operations, | |
9067 | -- except for formal derived types that inherit the operations of | |
9068 | -- the parent and progenitors. | |
8b4230c8 | 9069 | -- |
d469eabe HK |
9070 | -- If the context is a generic subprogram body, the generic formals |
9071 | -- are visible by name, but are not in the entity list of the | |
9072 | -- subprogram because that list starts with the subprogram formals. | |
9073 | -- We retrieve the candidate operations from the generic declaration. | |
401093c1 | 9074 | |
84dad556 AC |
9075 | function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id; |
9076 | -- Prefix notation can also be used on operations that are not | |
9077 | -- primitives of the type, but are declared in the same immediate | |
9078 | -- declarative part, which can only mean the corresponding package | |
9079 | -- body (See RM 4.1.3 (9.2/3)). If we are in that body we extend the | |
9080 | -- list of primitives with body operations with the same name that | |
9081 | -- may be candidates, so that Try_Primitive_Operations can examine | |
9082 | -- them if no real primitive is found. | |
9083 | ||
dfcfdc0a AC |
9084 | function Is_Private_Overriding (Op : Entity_Id) return Boolean; |
9085 | -- An operation that overrides an inherited operation in the private | |
9086 | -- part of its package may be hidden, but if the inherited operation | |
9087 | -- is visible a direct call to it will dispatch to the private one, | |
9088 | -- which is therefore a valid candidate. | |
9089 | ||
42f11e4c AC |
9090 | function Names_Match |
9091 | (Obj_Type : Entity_Id; | |
9092 | Prim_Op : Entity_Id; | |
9093 | Subprog : Entity_Id) return Boolean; | |
9094 | -- Return True if the names of Prim_Op and Subprog match. If Obj_Type | |
9095 | -- is a protected type then compare also the original name of Prim_Op | |
9096 | -- with the name of Subprog (since the expander may have added a | |
9097 | -- prefix to its original name --see Exp_Ch9.Build_Selected_Name). | |
9098 | ||
ec6078e3 ES |
9099 | function Valid_First_Argument_Of (Op : Entity_Id) return Boolean; |
9100 | -- Verify that the prefix, dereferenced if need be, is a valid | |
9101 | -- controlling argument in a call to Op. The remaining actuals | |
9102 | -- are checked in the subsequent call to Analyze_One_Call. | |
35ae2ed8 | 9103 | |
401093c1 ES |
9104 | ------------------------------ |
9105 | -- Collect_Generic_Type_Ops -- | |
9106 | ------------------------------ | |
9107 | ||
9108 | function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is | |
9109 | Bas : constant Entity_Id := Base_Type (T); | |
9110 | Candidates : constant Elist_Id := New_Elmt_List; | |
9111 | Subp : Entity_Id; | |
9112 | Formal : Entity_Id; | |
9113 | ||
d469eabe HK |
9114 | procedure Check_Candidate; |
9115 | -- The operation is a candidate if its first parameter is a | |
9116 | -- controlling operand of the desired type. | |
9117 | ||
9118 | ----------------------- | |
9119 | -- Check_Candidate; -- | |
9120 | ----------------------- | |
9121 | ||
9122 | procedure Check_Candidate is | |
9123 | begin | |
9124 | Formal := First_Formal (Subp); | |
9125 | ||
9126 | if Present (Formal) | |
9127 | and then Is_Controlling_Formal (Formal) | |
9128 | and then | |
9129 | (Base_Type (Etype (Formal)) = Bas | |
9130 | or else | |
9131 | (Is_Access_Type (Etype (Formal)) | |
9132 | and then Designated_Type (Etype (Formal)) = Bas)) | |
9133 | then | |
9134 | Append_Elmt (Subp, Candidates); | |
9135 | end if; | |
9136 | end Check_Candidate; | |
9137 | ||
9138 | -- Start of processing for Collect_Generic_Type_Ops | |
9139 | ||
401093c1 ES |
9140 | begin |
9141 | if Is_Derived_Type (T) then | |
9142 | return Primitive_Operations (T); | |
9143 | ||
bce79204 AC |
9144 | elsif Ekind_In (Scope (T), E_Procedure, E_Function) then |
9145 | ||
d469eabe HK |
9146 | -- Scan the list of generic formals to find subprograms |
9147 | -- that may have a first controlling formal of the type. | |
9148 | ||
8b4230c8 AC |
9149 | if Nkind (Unit_Declaration_Node (Scope (T))) = |
9150 | N_Generic_Subprogram_Declaration | |
bb10b891 AC |
9151 | then |
9152 | declare | |
9153 | Decl : Node_Id; | |
9154 | ||
9155 | begin | |
9156 | Decl := | |
9157 | First (Generic_Formal_Declarations | |
9158 | (Unit_Declaration_Node (Scope (T)))); | |
9159 | while Present (Decl) loop | |
9160 | if Nkind (Decl) in N_Formal_Subprogram_Declaration then | |
9161 | Subp := Defining_Entity (Decl); | |
9162 | Check_Candidate; | |
9163 | end if; | |
d469eabe | 9164 | |
bb10b891 AC |
9165 | Next (Decl); |
9166 | end loop; | |
9167 | end; | |
9168 | end if; | |
d469eabe HK |
9169 | return Candidates; |
9170 | ||
401093c1 ES |
9171 | else |
9172 | -- Scan the list of entities declared in the same scope as | |
9173 | -- the type. In general this will be an open scope, given that | |
9174 | -- the call we are analyzing can only appear within a generic | |
9175 | -- declaration or body (either the one that declares T, or a | |
9176 | -- child unit). | |
9177 | ||
bb10b891 AC |
9178 | -- For a subtype representing a generic actual type, go to the |
9179 | -- base type. | |
9180 | ||
9181 | if Is_Generic_Actual_Type (T) then | |
9182 | Subp := First_Entity (Scope (Base_Type (T))); | |
9183 | else | |
9184 | Subp := First_Entity (Scope (T)); | |
9185 | end if; | |
9186 | ||
401093c1 ES |
9187 | while Present (Subp) loop |
9188 | if Is_Overloadable (Subp) then | |
d469eabe | 9189 | Check_Candidate; |
401093c1 ES |
9190 | end if; |
9191 | ||
9192 | Next_Entity (Subp); | |
9193 | end loop; | |
9194 | ||
9195 | return Candidates; | |
9196 | end if; | |
9197 | end Collect_Generic_Type_Ops; | |
9198 | ||
84dad556 AC |
9199 | ---------------------------- |
9200 | -- Extended_Primitive_Ops -- | |
9201 | ---------------------------- | |
9202 | ||
9203 | function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id is | |
9204 | Type_Scope : constant Entity_Id := Scope (T); | |
9205 | ||
9206 | Body_Decls : List_Id; | |
9207 | Op_Found : Boolean; | |
9208 | Op : Entity_Id; | |
9209 | Op_List : Elist_Id; | |
9210 | ||
9211 | begin | |
9212 | Op_List := Primitive_Operations (T); | |
9213 | ||
9214 | if Ekind (Type_Scope) = E_Package | |
9215 | and then In_Package_Body (Type_Scope) | |
9216 | and then In_Open_Scopes (Type_Scope) | |
9217 | then | |
9218 | -- Retrieve list of declarations of package body. | |
9219 | ||
9220 | Body_Decls := | |
9221 | Declarations | |
9222 | (Unit_Declaration_Node | |
9223 | (Corresponding_Body | |
9224 | (Unit_Declaration_Node (Type_Scope)))); | |
9225 | ||
9226 | Op := Current_Entity (Subprog); | |
9227 | Op_Found := False; | |
9228 | while Present (Op) loop | |
9229 | if Comes_From_Source (Op) | |
9230 | and then Is_Overloadable (Op) | |
e23e04db AC |
9231 | |
9232 | -- Exclude overriding primitive operations of a type | |
9233 | -- extension declared in the package body, to prevent | |
9234 | -- duplicates in extended list. | |
9235 | ||
9236 | and then not Is_Primitive (Op) | |
84dad556 AC |
9237 | and then Is_List_Member (Unit_Declaration_Node (Op)) |
9238 | and then List_Containing (Unit_Declaration_Node (Op)) = | |
9239 | Body_Decls | |
9240 | then | |
9241 | if not Op_Found then | |
9242 | ||
9243 | -- Copy list of primitives so it is not affected for | |
9244 | -- other uses. | |
9245 | ||
9246 | Op_List := New_Copy_Elist (Op_List); | |
9247 | Op_Found := True; | |
9248 | end if; | |
9249 | ||
9250 | Append_Elmt (Op, Op_List); | |
9251 | end if; | |
9252 | ||
9253 | Op := Homonym (Op); | |
9254 | end loop; | |
9255 | end if; | |
9256 | ||
9257 | return Op_List; | |
9258 | end Extended_Primitive_Ops; | |
9259 | ||
dfcfdc0a AC |
9260 | --------------------------- |
9261 | -- Is_Private_Overriding -- | |
9262 | --------------------------- | |
9263 | ||
9264 | function Is_Private_Overriding (Op : Entity_Id) return Boolean is | |
9265 | Visible_Op : constant Entity_Id := Homonym (Op); | |
9266 | ||
9267 | begin | |
9268 | return Present (Visible_Op) | |
6465b6a7 | 9269 | and then Scope (Op) = Scope (Visible_Op) |
dfcfdc0a AC |
9270 | and then not Comes_From_Source (Visible_Op) |
9271 | and then Alias (Visible_Op) = Op | |
9272 | and then not Is_Hidden (Visible_Op); | |
9273 | end Is_Private_Overriding; | |
9274 | ||
42f11e4c AC |
9275 | ----------------- |
9276 | -- Names_Match -- | |
9277 | ----------------- | |
9278 | ||
9279 | function Names_Match | |
9280 | (Obj_Type : Entity_Id; | |
9281 | Prim_Op : Entity_Id; | |
9282 | Subprog : Entity_Id) return Boolean is | |
9283 | begin | |
9284 | -- Common case: exact match | |
9285 | ||
9286 | if Chars (Prim_Op) = Chars (Subprog) then | |
9287 | return True; | |
9288 | ||
9289 | -- For protected type primitives the expander may have built the | |
9290 | -- name of the dispatching primitive prepending the type name to | |
9291 | -- avoid conflicts with the name of the protected subprogram (see | |
9292 | -- Exp_Ch9.Build_Selected_Name). | |
9293 | ||
9294 | elsif Is_Protected_Type (Obj_Type) then | |
bac5ba15 AC |
9295 | return |
9296 | Present (Original_Protected_Subprogram (Prim_Op)) | |
9297 | and then Chars (Original_Protected_Subprogram (Prim_Op)) = | |
9298 | Chars (Subprog); | |
42f11e4c AC |
9299 | end if; |
9300 | ||
9301 | return False; | |
9302 | end Names_Match; | |
9303 | ||
ec6078e3 ES |
9304 | ----------------------------- |
9305 | -- Valid_First_Argument_Of -- | |
9306 | ----------------------------- | |
35ae2ed8 | 9307 | |
ec6078e3 | 9308 | function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is |
9febb58f | 9309 | Typ : Entity_Id := Etype (First_Formal (Op)); |
35ae2ed8 | 9310 | |
ec6078e3 | 9311 | begin |
9febb58f JM |
9312 | if Is_Concurrent_Type (Typ) |
9313 | and then Present (Corresponding_Record_Type (Typ)) | |
9314 | then | |
9315 | Typ := Corresponding_Record_Type (Typ); | |
9316 | end if; | |
9317 | ||
9313a26a AC |
9318 | -- Simple case. Object may be a subtype of the tagged type or may |
9319 | -- be the corresponding record of a synchronized type. | |
5d09245e | 9320 | |
aab883ec | 9321 | return Obj_Type = Typ |
d469eabe | 9322 | or else Base_Type (Obj_Type) = Typ |
0a36105d JM |
9323 | or else Corr_Type = Typ |
9324 | ||
913e4b36 | 9325 | -- Object may be of a derived type whose parent has unknown |
9313a26a AC |
9326 | -- discriminants, in which case the type matches the underlying |
9327 | -- record view of its base. | |
913e4b36 | 9328 | |
9313a26a AC |
9329 | or else |
9330 | (Has_Unknown_Discriminants (Typ) | |
9331 | and then Typ = Underlying_Record_View (Base_Type (Obj_Type))) | |
913e4b36 | 9332 | |
0a36105d | 9333 | -- Prefix can be dereferenced |
725e2a15 | 9334 | |
ec6078e3 | 9335 | or else |
0a36105d JM |
9336 | (Is_Access_Type (Corr_Type) |
9337 | and then Designated_Type (Corr_Type) = Typ) | |
5d09245e | 9338 | |
9313a26a AC |
9339 | -- Formal is an access parameter, for which the object can |
9340 | -- provide an access. | |
35ae2ed8 | 9341 | |
ec6078e3 ES |
9342 | or else |
9343 | (Ekind (Typ) = E_Anonymous_Access_Type | |
9fde638d RD |
9344 | and then |
9345 | Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type)); | |
ec6078e3 | 9346 | end Valid_First_Argument_Of; |
35ae2ed8 | 9347 | |
ec6078e3 | 9348 | -- Start of processing for Try_Primitive_Operation |
35ae2ed8 | 9349 | |
ec6078e3 | 9350 | begin |
d469eabe | 9351 | -- Look for subprograms in the list of primitive operations. The name |
0a36105d JM |
9352 | -- must be identical, and the kind of call indicates the expected |
9353 | -- kind of operation (function or procedure). If the type is a | |
d469eabe | 9354 | -- (tagged) synchronized type, the primitive ops are attached to the |
b4592168 | 9355 | -- corresponding record (base) type. |
aab883ec ES |
9356 | |
9357 | if Is_Concurrent_Type (Obj_Type) then | |
bb10b891 AC |
9358 | if Present (Corresponding_Record_Type (Obj_Type)) then |
9359 | Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type)); | |
48c8c473 | 9360 | Elmt := First_Elmt (Primitive_Operations (Corr_Type)); |
bb10b891 AC |
9361 | else |
9362 | Corr_Type := Obj_Type; | |
48c8c473 | 9363 | Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type)); |
15e4986c JM |
9364 | end if; |
9365 | ||
401093c1 | 9366 | elsif not Is_Generic_Type (Obj_Type) then |
0a36105d | 9367 | Corr_Type := Obj_Type; |
48c8c473 | 9368 | Elmt := First_Elmt (Extended_Primitive_Ops (Obj_Type)); |
401093c1 ES |
9369 | |
9370 | else | |
9371 | Corr_Type := Obj_Type; | |
48c8c473 | 9372 | Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type)); |
aab883ec | 9373 | end if; |
35ae2ed8 | 9374 | |
ec6078e3 ES |
9375 | while Present (Elmt) loop |
9376 | Prim_Op := Node (Elmt); | |
9377 | ||
42f11e4c | 9378 | if Names_Match (Obj_Type, Prim_Op, Subprog) |
ec6078e3 ES |
9379 | and then Present (First_Formal (Prim_Op)) |
9380 | and then Valid_First_Argument_Of (Prim_Op) | |
fe45e59e | 9381 | and then |
7415029d | 9382 | (Nkind (Call_Node) = N_Function_Call) |
48c8c473 | 9383 | = |
8b4230c8 | 9384 | (Ekind (Prim_Op) = E_Function) |
ec6078e3 | 9385 | then |
b67a385c | 9386 | -- Ada 2005 (AI-251): If this primitive operation corresponds |
8b4230c8 | 9387 | -- to an immediate ancestor interface there is no need to add |
b67a385c ES |
9388 | -- it to the list of interpretations; the corresponding aliased |
9389 | -- primitive is also in this list of primitive operations and | |
9390 | -- will be used instead. | |
fe45e59e | 9391 | |
ce2b6ba5 JM |
9392 | if (Present (Interface_Alias (Prim_Op)) |
9393 | and then Is_Ancestor (Find_Dispatching_Type | |
9394 | (Alias (Prim_Op)), Corr_Type)) | |
0a36105d | 9395 | |
dfcfdc0a AC |
9396 | -- Do not consider hidden primitives unless the type is in an |
9397 | -- open scope or we are within an instance, where visibility | |
9398 | -- is known to be correct, or else if this is an overriding | |
9399 | -- operation in the private part for an inherited operation. | |
0a36105d | 9400 | |
dfcfdc0a AC |
9401 | or else (Is_Hidden (Prim_Op) |
9402 | and then not Is_Immediately_Visible (Obj_Type) | |
9403 | and then not In_Instance | |
9404 | and then not Is_Private_Overriding (Prim_Op)) | |
fe45e59e ES |
9405 | then |
9406 | goto Continue; | |
9407 | end if; | |
9408 | ||
0a36105d JM |
9409 | Set_Etype (Call_Node, Any_Type); |
9410 | Set_Is_Overloaded (Call_Node, False); | |
9411 | ||
9412 | if No (Matching_Op) then | |
e4494292 | 9413 | Prim_Op_Ref := New_Occurrence_Of (Prim_Op, Sloc (Subprog)); |
b67a385c | 9414 | Candidate := Prim_Op; |
35ae2ed8 | 9415 | |
fe45e59e | 9416 | Set_Parent (Call_Node, Parent (Node_To_Replace)); |
35ae2ed8 | 9417 | |
fe45e59e | 9418 | Set_Name (Call_Node, Prim_Op_Ref); |
0a36105d | 9419 | Success := False; |
35ae2ed8 | 9420 | |
fe45e59e ES |
9421 | Analyze_One_Call |
9422 | (N => Call_Node, | |
9423 | Nam => Prim_Op, | |
b67a385c | 9424 | Report => Report_Error, |
fe45e59e ES |
9425 | Success => Success, |
9426 | Skip_First => True); | |
35ae2ed8 | 9427 | |
0a36105d | 9428 | Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op); |
fe45e59e | 9429 | |
d469eabe HK |
9430 | -- More than one interpretation, collect for subsequent |
9431 | -- disambiguation. If this is a procedure call and there | |
9432 | -- is another match, report ambiguity now. | |
0a36105d | 9433 | |
d469eabe | 9434 | else |
0a36105d JM |
9435 | Analyze_One_Call |
9436 | (N => Call_Node, | |
9437 | Nam => Prim_Op, | |
9438 | Report => Report_Error, | |
9439 | Success => Success, | |
9440 | Skip_First => True); | |
fe45e59e | 9441 | |
0a36105d JM |
9442 | if Present (Valid_Candidate (Success, Call_Node, Prim_Op)) |
9443 | and then Nkind (Call_Node) /= N_Function_Call | |
9444 | then | |
ed2233dc | 9445 | Error_Msg_NE ("ambiguous call to&", N, Prim_Op); |
0a36105d JM |
9446 | Report_Ambiguity (Matching_Op); |
9447 | Report_Ambiguity (Prim_Op); | |
9448 | return True; | |
9449 | end if; | |
4c46b835 AC |
9450 | end if; |
9451 | end if; | |
35ae2ed8 | 9452 | |
fe45e59e | 9453 | <<Continue>> |
4c46b835 AC |
9454 | Next_Elmt (Elmt); |
9455 | end loop; | |
35ae2ed8 | 9456 | |
0a36105d JM |
9457 | if Present (Matching_Op) then |
9458 | Set_Etype (Call_Node, Etype (Matching_Op)); | |
fe45e59e ES |
9459 | end if; |
9460 | ||
0a36105d | 9461 | return Present (Matching_Op); |
4c46b835 | 9462 | end Try_Primitive_Operation; |
35ae2ed8 | 9463 | |
48c8c473 AC |
9464 | --------------------- |
9465 | -- Valid_Candidate -- | |
9466 | --------------------- | |
9467 | ||
9468 | function Valid_Candidate | |
9469 | (Success : Boolean; | |
9470 | Call : Node_Id; | |
9471 | Subp : Entity_Id) return Entity_Id | |
9472 | is | |
9473 | Arr_Type : Entity_Id; | |
9474 | Comp_Type : Entity_Id; | |
9475 | ||
9476 | begin | |
9477 | -- If the subprogram is a valid interpretation, record it in global | |
9478 | -- variable Subprog, to collect all possible overloadings. | |
9479 | ||
9480 | if Success then | |
9481 | if Subp /= Entity (Subprog) then | |
9482 | Add_One_Interp (Subprog, Subp, Etype (Subp)); | |
9483 | end if; | |
9484 | end if; | |
9485 | ||
9486 | -- If the call may be an indexed call, retrieve component type of | |
9487 | -- resulting expression, and add possible interpretation. | |
9488 | ||
9489 | Arr_Type := Empty; | |
9490 | Comp_Type := Empty; | |
9491 | ||
9492 | if Nkind (Call) = N_Function_Call | |
9493 | and then Nkind (Parent (N)) = N_Indexed_Component | |
9494 | and then Needs_One_Actual (Subp) | |
9495 | then | |
9496 | if Is_Array_Type (Etype (Subp)) then | |
9497 | Arr_Type := Etype (Subp); | |
9498 | ||
9499 | elsif Is_Access_Type (Etype (Subp)) | |
9500 | and then Is_Array_Type (Designated_Type (Etype (Subp))) | |
9501 | then | |
9502 | Arr_Type := Designated_Type (Etype (Subp)); | |
9503 | end if; | |
9504 | end if; | |
9505 | ||
9506 | if Present (Arr_Type) then | |
9507 | ||
9508 | -- Verify that the actuals (excluding the object) match the types | |
9509 | -- of the indexes. | |
9510 | ||
9511 | declare | |
9512 | Actual : Node_Id; | |
9513 | Index : Node_Id; | |
9514 | ||
9515 | begin | |
9516 | Actual := Next (First_Actual (Call)); | |
9517 | Index := First_Index (Arr_Type); | |
9518 | while Present (Actual) and then Present (Index) loop | |
9519 | if not Has_Compatible_Type (Actual, Etype (Index)) then | |
9520 | Arr_Type := Empty; | |
9521 | exit; | |
9522 | end if; | |
9523 | ||
9524 | Next_Actual (Actual); | |
9525 | Next_Index (Index); | |
9526 | end loop; | |
9527 | ||
9528 | if No (Actual) | |
9529 | and then No (Index) | |
9530 | and then Present (Arr_Type) | |
9531 | then | |
9532 | Comp_Type := Component_Type (Arr_Type); | |
9533 | end if; | |
9534 | end; | |
9535 | ||
9536 | if Present (Comp_Type) | |
9537 | and then Etype (Subprog) /= Comp_Type | |
9538 | then | |
9539 | Add_One_Interp (Subprog, Subp, Comp_Type); | |
9540 | end if; | |
9541 | end if; | |
9542 | ||
9543 | if Etype (Call) /= Any_Type then | |
9544 | return Subp; | |
9545 | else | |
9546 | return Empty; | |
9547 | end if; | |
9548 | end Valid_Candidate; | |
9549 | ||
4c46b835 | 9550 | -- Start of processing for Try_Object_Operation |
35ae2ed8 | 9551 | |
4c46b835 | 9552 | begin |
0a36105d | 9553 | Analyze_Expression (Obj); |
ec6078e3 | 9554 | |
0a36105d | 9555 | -- Analyze the actuals if node is known to be a subprogram call |
28d6470f JM |
9556 | |
9557 | if Is_Subprg_Call and then N = Name (Parent (N)) then | |
9558 | Actual := First (Parameter_Associations (Parent (N))); | |
9559 | while Present (Actual) loop | |
725e2a15 | 9560 | Analyze_Expression (Actual); |
28d6470f JM |
9561 | Next (Actual); |
9562 | end loop; | |
9563 | end if; | |
5d09245e | 9564 | |
ec6078e3 ES |
9565 | -- Build a subprogram call node, using a copy of Obj as its first |
9566 | -- actual. This is a placeholder, to be replaced by an explicit | |
9567 | -- dereference when needed. | |
4c46b835 | 9568 | |
ec6078e3 ES |
9569 | Transform_Object_Operation |
9570 | (Call_Node => New_Call_Node, | |
0a36105d | 9571 | Node_To_Replace => Node_To_Replace); |
4c46b835 | 9572 | |
ec6078e3 | 9573 | Set_Etype (New_Call_Node, Any_Type); |
0a36105d | 9574 | Set_Etype (Subprog, Any_Type); |
ec6078e3 | 9575 | Set_Parent (New_Call_Node, Parent (Node_To_Replace)); |
4c46b835 | 9576 | |
0a36105d JM |
9577 | if not Is_Overloaded (Obj) then |
9578 | Try_One_Prefix_Interpretation (Obj_Type); | |
ec6078e3 | 9579 | |
0a36105d JM |
9580 | else |
9581 | declare | |
9582 | I : Interp_Index; | |
9583 | It : Interp; | |
9584 | begin | |
9585 | Get_First_Interp (Obj, I, It); | |
9586 | while Present (It.Nam) loop | |
9587 | Try_One_Prefix_Interpretation (It.Typ); | |
9588 | Get_Next_Interp (I, It); | |
9589 | end loop; | |
9590 | end; | |
9591 | end if; | |
9592 | ||
9593 | if Etype (New_Call_Node) /= Any_Type then | |
8cf23b91 AC |
9594 | |
9595 | -- No need to complete the tree transformations if we are only | |
9596 | -- searching for conflicting class-wide subprograms | |
9597 | ||
9598 | if CW_Test_Only then | |
9599 | return False; | |
9600 | else | |
9601 | Complete_Object_Operation | |
9602 | (Call_Node => New_Call_Node, | |
9603 | Node_To_Replace => Node_To_Replace); | |
9604 | return True; | |
9605 | end if; | |
b67a385c ES |
9606 | |
9607 | elsif Present (Candidate) then | |
9608 | ||
9609 | -- The argument list is not type correct. Re-analyze with error | |
9610 | -- reporting enabled, and use one of the possible candidates. | |
d469eabe | 9611 | -- In All_Errors_Mode, re-analyze all failed interpretations. |
b67a385c ES |
9612 | |
9613 | if All_Errors_Mode then | |
9614 | Report_Error := True; | |
9615 | if Try_Primitive_Operation | |
8b4230c8 AC |
9616 | (Call_Node => New_Call_Node, |
9617 | Node_To_Replace => Node_To_Replace) | |
b67a385c ES |
9618 | |
9619 | or else | |
9620 | Try_Class_Wide_Operation | |
9621 | (Call_Node => New_Call_Node, | |
9622 | Node_To_Replace => Node_To_Replace) | |
9623 | then | |
9624 | null; | |
9625 | end if; | |
9626 | ||
9627 | else | |
9628 | Analyze_One_Call | |
9629 | (N => New_Call_Node, | |
9630 | Nam => Candidate, | |
9631 | Report => True, | |
9632 | Success => Success, | |
9633 | Skip_First => True); | |
9634 | end if; | |
9635 | ||
d469eabe HK |
9636 | -- No need for further errors |
9637 | ||
9638 | return True; | |
b67a385c ES |
9639 | |
9640 | else | |
9641 | -- There was no candidate operation, so report it as an error | |
9642 | -- in the caller: Analyze_Selected_Component. | |
9643 | ||
9644 | return False; | |
9645 | end if; | |
35ae2ed8 AC |
9646 | end Try_Object_Operation; |
9647 | ||
b4592168 GD |
9648 | --------- |
9649 | -- wpo -- | |
9650 | --------- | |
9651 | ||
9652 | procedure wpo (T : Entity_Id) is | |
9653 | Op : Entity_Id; | |
9654 | E : Elmt_Id; | |
9655 | ||
9656 | begin | |
9657 | if not Is_Tagged_Type (T) then | |
9658 | return; | |
9659 | end if; | |
9660 | ||
9661 | E := First_Elmt (Primitive_Operations (Base_Type (T))); | |
9662 | while Present (E) loop | |
9663 | Op := Node (E); | |
9664 | Write_Int (Int (Op)); | |
9665 | Write_Str (" === "); | |
9666 | Write_Name (Chars (Op)); | |
9667 | Write_Str (" in "); | |
9668 | Write_Name (Chars (Scope (Op))); | |
9669 | Next_Elmt (E); | |
9670 | Write_Eol; | |
9671 | end loop; | |
9672 | end wpo; | |
9673 | ||
996ae0b0 | 9674 | end Sem_Ch4; |