]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_ch4.adb
[Ada] Iterate with procedural versions of Next_... routines where possible
[thirdparty/gcc.git] / gcc / ada / sem_ch4.adb
CommitLineData
996ae0b0
RK
1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- S E M _ C H 4 --
6-- --
7-- B o d y --
8-- --
4b490c1e 9-- Copyright (C) 1992-2020, 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 26with Aspects; use Aspects;
996ae0b0
RK
27with Atree; use Atree;
28with Debug; use Debug;
29with Einfo; use Einfo;
35ae2ed8 30with Elists; use Elists;
996ae0b0
RK
31with Errout; use Errout;
32with Exp_Util; use Exp_Util;
996ae0b0 33with Itypes; use Itypes;
d935a36e 34with Lib; use Lib;
996ae0b0
RK
35with Lib.Xref; use Lib.Xref;
36with Namet; use Namet;
d469eabe 37with Namet.Sp; use Namet.Sp;
996ae0b0
RK
38with Nlists; use Nlists;
39with Nmake; use Nmake;
40with Opt; use Opt;
41with Output; use Output;
42with Restrict; use Restrict;
6e937c1c 43with Rident; use Rident;
996ae0b0 44with Sem; use Sem;
a4100e55 45with Sem_Aux; use Sem_Aux;
19d846a0 46with Sem_Case; use Sem_Case;
996ae0b0
RK
47with Sem_Cat; use Sem_Cat;
48with Sem_Ch3; use Sem_Ch3;
d469eabe 49with Sem_Ch6; use Sem_Ch6;
996ae0b0 50with Sem_Ch8; use Sem_Ch8;
dec6faf1 51with Sem_Dim; use Sem_Dim;
b67a385c 52with Sem_Disp; use Sem_Disp;
996ae0b0
RK
53with Sem_Dist; use Sem_Dist;
54with Sem_Eval; use Sem_Eval;
55with Sem_Res; use Sem_Res;
996ae0b0 56with Sem_Type; use Sem_Type;
19d846a0
RD
57with Sem_Util; use Sem_Util;
58with Sem_Warn; use Sem_Warn;
996ae0b0
RK
59with Stand; use Stand;
60with Sinfo; use Sinfo;
61with Snames; use Snames;
62with Tbuild; use Tbuild;
b727a82b 63with Uintp; use Uintp;
996ae0b0 64
996ae0b0
RK
65package 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 procedure Check_Misspelled_Selector
175 (Prefix : Entity_Id;
176 Sel : Node_Id);
80211802 177 -- Give possible misspelling message if Sel seems likely to be a mis-
8dbf3473
AC
178 -- spelling of one of the selectors of the Prefix. This is called by
179 -- Analyze_Selected_Component after producing an invalid selector error
180 -- message.
996ae0b0
RK
181
182 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean;
f3d57416 183 -- Verify that type T is declared in scope S. Used to find interpretations
996ae0b0
RK
184 -- for operators given by expanded names. This is abstracted as a separate
185 -- function to handle extensions to System, where S is System, but T is
186 -- declared in the extension.
187
188 procedure Find_Arithmetic_Types
189 (L, R : Node_Id;
190 Op_Id : Entity_Id;
191 N : Node_Id);
80211802
AC
192 -- L and R are the operands of an arithmetic operator. Find consistent
193 -- pairs of interpretations for L and R that have a numeric type consistent
194 -- with the semantics of the operator.
996ae0b0
RK
195
196 procedure Find_Comparison_Types
197 (L, R : Node_Id;
198 Op_Id : Entity_Id;
199 N : Node_Id);
80211802
AC
200 -- L and R are operands of a comparison operator. Find consistent pairs of
201 -- interpretations for L and R.
996ae0b0
RK
202
203 procedure Find_Concatenation_Types
204 (L, R : Node_Id;
205 Op_Id : Entity_Id;
206 N : Node_Id);
6e73e3ab 207 -- For the four varieties of concatenation
996ae0b0
RK
208
209 procedure Find_Equality_Types
210 (L, R : Node_Id;
211 Op_Id : Entity_Id;
212 N : Node_Id);
6e73e3ab 213 -- Ditto for equality operators
996ae0b0
RK
214
215 procedure Find_Boolean_Types
216 (L, R : Node_Id;
217 Op_Id : Entity_Id;
218 N : Node_Id);
6e73e3ab 219 -- Ditto for binary logical operations
996ae0b0
RK
220
221 procedure Find_Negation_Types
222 (R : Node_Id;
223 Op_Id : Entity_Id;
224 N : Node_Id);
6e73e3ab 225 -- Find consistent interpretation for operand of negation operator
996ae0b0
RK
226
227 procedure Find_Non_Universal_Interpretations
228 (N : Node_Id;
229 R : Node_Id;
230 Op_Id : Entity_Id;
231 T1 : Entity_Id);
5dc203d2
AC
232 -- For equality and comparison operators, the result is always boolean, and
233 -- the legality of the operation is determined from the visibility of the
234 -- operand types. If one of the operands has a universal interpretation,
235 -- the legality check uses some compatible non-universal interpretation of
236 -- the other operand. N can be an operator node, or a function call whose
237 -- name is an operator designator. Any_Access, which is the initial type of
238 -- the literal NULL, is a universal type for the purpose of this routine.
996ae0b0 239
d469eabe 240 function Find_Primitive_Operation (N : Node_Id) return Boolean;
5dc203d2
AC
241 -- Find candidate interpretations for the name Obj.Proc when it appears in
242 -- a subprogram renaming declaration.
d469eabe 243
996ae0b0
RK
244 procedure Find_Unary_Types
245 (R : Node_Id;
246 Op_Id : Entity_Id;
247 N : Node_Id);
6e73e3ab 248 -- Unary arithmetic types: plus, minus, abs
996ae0b0
RK
249
250 procedure Check_Arithmetic_Pair
251 (T1, T2 : Entity_Id;
252 Op_Id : Entity_Id;
253 N : Node_Id);
84dad556
AC
254 -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid types
255 -- for left and right operand. Determine whether they constitute a valid
256 -- pair for the given operator, and record the corresponding interpretation
257 -- of the operator node. The node N may be an operator node (the usual
258 -- case) or a function call whose prefix is an operator designator. In
259 -- both cases Op_Id is the operator name itself.
996ae0b0
RK
260
261 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
262 -- Give detailed information on overloaded call where none of the
263 -- interpretations match. N is the call node, Nam the designator for
264 -- the overloaded entity being called.
265
266 function Junk_Operand (N : Node_Id) return Boolean;
267 -- Test for an operand that is an inappropriate entity (e.g. a package
268 -- name or a label). If so, issue an error message and return True. If
269 -- the operand is not an inappropriate entity kind, return False.
270
271 procedure Operator_Check (N : Node_Id);
da709d08
AC
272 -- Verify that an operator has received some valid interpretation. If none
273 -- was found, determine whether a use clause would make the operation
274 -- legal. The variable Candidate_Type (defined in Sem_Type) is set for
275 -- every type compatible with the operator, even if the operator for the
276 -- type is not directly visible. The routine uses this type to emit a more
277 -- informative message.
996ae0b0 278
d469eabe 279 function Process_Implicit_Dereference_Prefix
da709d08 280 (E : Entity_Id;
d469eabe 281 P : Node_Id) return Entity_Id;
da709d08 282 -- Called when P is the prefix of an implicit dereference, denoting an
d469eabe
HK
283 -- object E. The function returns the designated type of the prefix, taking
284 -- into account that the designated type of an anonymous access type may be
a316b3fc 285 -- a limited view, when the nonlimited view is visible.
84dad556 286 --
d469eabe
HK
287 -- If in semantics only mode (-gnatc or generic), the function also records
288 -- that the prefix is a reference to E, if any. Normally, such a reference
289 -- is generated only when the implicit dereference is expanded into an
290 -- explicit one, but for consistency we must generate the reference when
291 -- expansion is disabled as well.
6e73e3ab 292
30c20106
AC
293 procedure Remove_Abstract_Operations (N : Node_Id);
294 -- Ada 2005: implementation of AI-310. An abstract non-dispatching
295 -- operation is not a candidate interpretation.
296
d50f4827
AC
297 function Try_Container_Indexing
298 (N : Node_Id;
299 Prefix : Node_Id;
50878404 300 Exprs : List_Id) return Boolean;
d50f4827
AC
301 -- AI05-0139: Generalized indexing to support iterators over containers
302
996ae0b0 303 function Try_Indexed_Call
aab883ec
ES
304 (N : Node_Id;
305 Nam : Entity_Id;
306 Typ : Entity_Id;
307 Skip_First : Boolean) return Boolean;
308 -- If a function has defaults for all its actuals, a call to it may in fact
309 -- be an indexing on the result of the call. Try_Indexed_Call attempts the
310 -- interpretation as an indexing, prior to analysis as a call. If both are
311 -- possible, the node is overloaded with both interpretations (same symbol
312 -- but two different types). If the call is written in prefix form, the
313 -- prefix becomes the first parameter in the call, and only the remaining
314 -- actuals must be checked for the presence of defaults.
996ae0b0
RK
315
316 function Try_Indirect_Call
91b1417d
AC
317 (N : Node_Id;
318 Nam : Entity_Id;
319 Typ : Entity_Id) return Boolean;
aab883ec
ES
320 -- Similarly, a function F that needs no actuals can return an access to a
321 -- subprogram, and the call F (X) interpreted as F.all (X). In this case
322 -- the call may be overloaded with both interpretations.
996ae0b0 323
b4592168
GD
324 procedure wpo (T : Entity_Id);
325 pragma Warnings (Off, wpo);
326 -- Used for debugging: obtain list of primitive operations even if
327 -- type is not frozen and dispatch table is not built yet.
328
996ae0b0
RK
329 ------------------------
330 -- Ambiguous_Operands --
331 ------------------------
332
333 procedure Ambiguous_Operands (N : Node_Id) is
fbf5a39b 334 procedure List_Operand_Interps (Opnd : Node_Id);
996ae0b0 335
4c46b835
AC
336 --------------------------
337 -- List_Operand_Interps --
338 --------------------------
339
fbf5a39b 340 procedure List_Operand_Interps (Opnd : Node_Id) is
dcd5fd67
PMR
341 Nam : Node_Id := Empty;
342 Err : Node_Id := N;
996ae0b0
RK
343
344 begin
345 if Is_Overloaded (Opnd) then
346 if Nkind (Opnd) in N_Op then
347 Nam := Opnd;
84dad556 348
996ae0b0
RK
349 elsif Nkind (Opnd) = N_Function_Call then
350 Nam := Name (Opnd);
84dad556 351
44a10091
AC
352 elsif Ada_Version >= Ada_2012 then
353 declare
354 It : Interp;
355 I : Interp_Index;
356
357 begin
358 Get_First_Interp (Opnd, I, It);
359 while Present (It.Nam) loop
360 if Has_Implicit_Dereference (It.Typ) then
361 Error_Msg_N
362 ("can be interpreted as implicit dereference", Opnd);
363 return;
364 end if;
365
366 Get_Next_Interp (I, It);
367 end loop;
368 end;
369
996ae0b0
RK
370 return;
371 end if;
372
373 else
374 return;
375 end if;
376
377 if Opnd = Left_Opnd (N) then
84dad556
AC
378 Error_Msg_N
379 ("\left operand has the following interpretations", N);
996ae0b0 380 else
ed2233dc 381 Error_Msg_N
996ae0b0
RK
382 ("\right operand has the following interpretations", N);
383 Err := Opnd;
384 end if;
385
fbf5a39b
AC
386 List_Interps (Nam, Err);
387 end List_Operand_Interps;
996ae0b0 388
4c46b835
AC
389 -- Start of processing for Ambiguous_Operands
390
996ae0b0 391 begin
b67a385c 392 if Nkind (N) in N_Membership_Test then
ed2233dc 393 Error_Msg_N ("ambiguous operands for membership", N);
996ae0b0 394
d469eabe 395 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
ed2233dc 396 Error_Msg_N ("ambiguous operands for equality", N);
996ae0b0
RK
397
398 else
ed2233dc 399 Error_Msg_N ("ambiguous operands for comparison", N);
996ae0b0
RK
400 end if;
401
402 if All_Errors_Mode then
fbf5a39b
AC
403 List_Operand_Interps (Left_Opnd (N));
404 List_Operand_Interps (Right_Opnd (N));
996ae0b0 405 else
555360a5 406 Error_Msg_N ("\use -gnatf switch for details", N);
996ae0b0
RK
407 end if;
408 end Ambiguous_Operands;
409
410 -----------------------
411 -- Analyze_Aggregate --
412 -----------------------
413
d63199d8
PMR
414 -- Most of the analysis of Aggregates requires that the type be known, and
415 -- is therefore put off until resolution of the context. Delta aggregates
416 -- have a base component that determines the enclosing aggregate type so
417 -- its type can be ascertained earlier. This also allows delta aggregates
418 -- to appear in the context of a record type with a private extension, as
419 -- per the latest update of AI12-0127.
996ae0b0
RK
420
421 procedure Analyze_Aggregate (N : Node_Id) is
422 begin
423 if No (Etype (N)) then
9ac3cbb3
PMR
424 if Nkind (N) = N_Delta_Aggregate then
425 declare
426 Base : constant Node_Id := Expression (N);
d63199d8 427
9ac3cbb3
PMR
428 I : Interp_Index;
429 It : Interp;
430
431 begin
432 Analyze (Base);
433
d63199d8
PMR
434 -- If the base is overloaded, propagate interpretations to the
435 -- enclosing aggregate.
9ac3cbb3
PMR
436
437 if Is_Overloaded (Base) then
438 Get_First_Interp (Base, I, It);
439 Set_Etype (N, Any_Type);
440
441 while Present (It.Nam) loop
442 Add_One_Interp (N, It.Typ, It.Typ);
443 Get_Next_Interp (I, It);
444 end loop;
445
446 else
447 Set_Etype (N, Etype (Base));
448 end if;
449 end;
450
451 else
452 Set_Etype (N, Any_Composite);
453 end if;
996ae0b0
RK
454 end if;
455 end Analyze_Aggregate;
456
457 -----------------------
458 -- Analyze_Allocator --
459 -----------------------
460
461 procedure Analyze_Allocator (N : Node_Id) is
462 Loc : constant Source_Ptr := Sloc (N);
07fc65c4 463 Sav_Errs : constant Nat := Serious_Errors_Detected;
b67a385c 464 E : Node_Id := Expression (N);
996ae0b0
RK
465 Acc_Type : Entity_Id;
466 Type_Id : Entity_Id;
87003b28
RD
467 P : Node_Id;
468 C : Node_Id;
b3b26ace 469 Onode : Node_Id;
996ae0b0
RK
470
471 begin
ce5ba43a 472 Check_SPARK_05_Restriction ("allocator is not allowed", N);
1d801f21 473
87003b28
RD
474 -- Deal with allocator restrictions
475
50cff367 476 -- In accordance with H.4(7), the No_Allocators restriction only applies
87003b28 477 -- to user-written allocators. The same consideration applies to the
d8941160 478 -- No_Standard_Allocators_Before_Elaboration restriction.
50cff367
GD
479
480 if Comes_From_Source (N) then
481 Check_Restriction (No_Allocators, N);
87003b28 482
57f4c288 483 -- Processing for No_Standard_Allocators_After_Elaboration, loop to
489c6e19 484 -- look at enclosing context, checking task/main subprogram case.
87003b28
RD
485
486 C := N;
487 P := Parent (C);
488 while Present (P) loop
489
b3b26ace
AC
490 -- For the task case we need a handled sequence of statements,
491 -- where the occurrence of the allocator is within the statements
492 -- and the parent is a task body
87003b28
RD
493
494 if Nkind (P) = N_Handled_Sequence_Of_Statements
495 and then Is_List_Member (C)
496 and then List_Containing (C) = Statements (P)
497 then
b3b26ace
AC
498 Onode := Original_Node (Parent (P));
499
87003b28 500 -- Check for allocator within task body, this is a definite
d8941160
RD
501 -- violation of No_Allocators_After_Elaboration we can detect
502 -- at compile time.
87003b28 503
b3b26ace 504 if Nkind (Onode) = N_Task_Body then
57f4c288
ES
505 Check_Restriction
506 (No_Standard_Allocators_After_Elaboration, N);
87003b28
RD
507 exit;
508 end if;
b3b26ace 509 end if;
87003b28 510
b3b26ace
AC
511 -- The other case is appearance in a subprogram body. This is
512 -- a violation if this is a library level subprogram with no
513 -- parameters. Note that this is now a static error even if the
514 -- subprogram is not the main program (this is a change, in an
515 -- earlier version only the main program was affected, and the
516 -- check had to be done in the binder.
87003b28 517
b3b26ace
AC
518 if Nkind (P) = N_Subprogram_Body
519 and then Nkind (Parent (P)) = N_Compilation_Unit
520 and then No (Parameter_Specifications (Specification (P)))
521 then
522 Check_Restriction
523 (No_Standard_Allocators_After_Elaboration, N);
87003b28
RD
524 end if;
525
526 C := P;
527 P := Parent (C);
528 end loop;
50cff367 529 end if;
996ae0b0 530
df170605
AC
531 -- Ada 2012 (AI05-0111-3): Analyze the subpool_specification, if
532 -- any. The expected type for the name is any type. A non-overloading
533 -- rule then requires it to be of a type descended from
f0f88eb6
RD
534 -- System.Storage_Pools.Subpools.Subpool_Handle.
535
536 -- This isn't exactly what the AI says, but it seems to be the right
537 -- rule. The AI should be fixed.???
df170605
AC
538
539 declare
540 Subpool : constant Node_Id := Subpool_Handle_Name (N);
f0f88eb6 541
df170605
AC
542 begin
543 if Present (Subpool) then
544 Analyze (Subpool);
f0f88eb6 545
df170605
AC
546 if Is_Overloaded (Subpool) then
547 Error_Msg_N ("ambiguous subpool handle", Subpool);
548 end if;
549
f0f88eb6 550 -- Check that Etype (Subpool) is descended from Subpool_Handle
df170605
AC
551
552 Resolve (Subpool);
553 end if;
554 end;
555
556 -- Analyze the qualified expression or subtype indication
87003b28 557
996ae0b0
RK
558 if Nkind (E) = N_Qualified_Expression then
559 Acc_Type := Create_Itype (E_Allocator_Type, N);
560 Set_Etype (Acc_Type, Acc_Type);
996ae0b0 561 Find_Type (Subtype_Mark (E));
45c8b94b
ES
562
563 -- Analyze the qualified expression, and apply the name resolution
f0f88eb6 564 -- rule given in 4.7(3).
45c8b94b
ES
565
566 Analyze (E);
567 Type_Id := Etype (E);
996ae0b0
RK
568 Set_Directly_Designated_Type (Acc_Type, Type_Id);
569
996ae0b0
RK
570 -- A qualified expression requires an exact match of the type,
571 -- class-wide matching is not allowed.
572
45c8b94b
ES
573 -- if Is_Class_Wide_Type (Type_Id)
574 -- and then Base_Type
575 -- (Etype (Expression (E))) /= Base_Type (Type_Id)
576 -- then
577 -- Wrong_Type (Expression (E), Type_Id);
578 -- end if;
996ae0b0 579
996ae0b0 580 -- We don't analyze the qualified expression itself because it's
f3691f46
ES
581 -- part of the allocator. It is fully analyzed and resolved when
582 -- the allocator is resolved with the context type.
996ae0b0
RK
583
584 Set_Etype (E, Type_Id);
585
aab883ec 586 -- Case where allocator has a subtype indication
4c46b835 587
996ae0b0
RK
588 else
589 declare
758c442c
GD
590 Def_Id : Entity_Id;
591 Base_Typ : Entity_Id;
996ae0b0
RK
592
593 begin
594 -- If the allocator includes a N_Subtype_Indication then a
595 -- constraint is present, otherwise the node is a subtype mark.
596 -- Introduce an explicit subtype declaration into the tree
597 -- defining some anonymous subtype and rewrite the allocator to
598 -- use this subtype rather than the subtype indication.
599
600 -- It is important to introduce the explicit subtype declaration
601 -- so that the bounds of the subtype indication are attached to
602 -- the tree in case the allocator is inside a generic unit.
603
3ba1a9eb
AC
604 -- Finally, if there is no subtype indication and the type is
605 -- a tagged unconstrained type with discriminants, the designated
606 -- object is constrained by their default values, and it is
607 -- simplest to introduce an explicit constraint now. In some cases
608 -- this is done during expansion, but freeze actions are certain
609 -- to be emitted in the proper order if constraint is explicit.
610
611 if Is_Entity_Name (E) and then Expander_Active then
612 Find_Type (E);
613 Type_Id := Entity (E);
614
615 if Is_Tagged_Type (Type_Id)
616 and then Has_Discriminants (Type_Id)
617 and then not Is_Constrained (Type_Id)
3702225c
AC
618 and then
619 Present
620 (Discriminant_Default_Value
621 (First_Discriminant (Type_Id)))
3ba1a9eb
AC
622 then
623 declare
3702225c 624 Constr : constant List_Id := New_List;
3ba1a9eb
AC
625 Loc : constant Source_Ptr := Sloc (E);
626 Discr : Entity_Id := First_Discriminant (Type_Id);
3ba1a9eb
AC
627
628 begin
629 if Present (Discriminant_Default_Value (Discr)) then
630 while Present (Discr) loop
631 Append (Discriminant_Default_Value (Discr), Constr);
632 Next_Discriminant (Discr);
633 end loop;
634
3702225c
AC
635 Rewrite (E,
636 Make_Subtype_Indication (Loc,
637 Subtype_Mark => New_Occurrence_Of (Type_Id, Loc),
638 Constraint =>
639 Make_Index_Or_Discriminant_Constraint (Loc,
640 Constraints => Constr)));
3ba1a9eb
AC
641 end if;
642 end;
643 end if;
644 end if;
645
996ae0b0
RK
646 if Nkind (E) = N_Subtype_Indication then
647
648 -- A constraint is only allowed for a composite type in Ada
649 -- 95. In Ada 83, a constraint is also allowed for an
650 -- access-to-composite type, but the constraint is ignored.
651
652 Find_Type (Subtype_Mark (E));
758c442c 653 Base_Typ := Entity (Subtype_Mark (E));
996ae0b0 654
758c442c 655 if Is_Elementary_Type (Base_Typ) then
0ab80019 656 if not (Ada_Version = Ada_83
758c442c 657 and then Is_Access_Type (Base_Typ))
996ae0b0
RK
658 then
659 Error_Msg_N ("constraint not allowed here", E);
660
24657705 661 if Nkind (Constraint (E)) =
3702225c 662 N_Index_Or_Discriminant_Constraint
996ae0b0 663 then
4e7a4f6e 664 Error_Msg_N -- CODEFIX
996ae0b0
RK
665 ("\if qualified expression was meant, " &
666 "use apostrophe", Constraint (E));
667 end if;
668 end if;
669
670 -- Get rid of the bogus constraint:
671
672 Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
673 Analyze_Allocator (N);
674 return;
675 end if;
676
60c14ec7
ES
677 -- In GNATprove mode we need to preserve the link between
678 -- the original subtype indication and the anonymous subtype,
16b54914 679 -- to extend proofs to constrained access types. We only do
ebad47fc
YM
680 -- that outside of spec expressions, otherwise the declaration
681 -- cannot be inserted and analyzed. In such a case, GNATprove
682 -- later rejects the allocator as it is not used here in
683 -- a non-interfering context (SPARK 4.8(2) and 7.1.3(12)).
684
685 if Expander_Active
686 or else (GNATprove_Mode and then not In_Spec_Expression)
687 then
092ef350 688 Def_Id := Make_Temporary (Loc, 'S');
996ae0b0
RK
689
690 Insert_Action (E,
691 Make_Subtype_Declaration (Loc,
692 Defining_Identifier => Def_Id,
693 Subtype_Indication => Relocate_Node (E)));
694
07fc65c4 695 if Sav_Errs /= Serious_Errors_Detected
d469eabe
HK
696 and then Nkind (Constraint (E)) =
697 N_Index_Or_Discriminant_Constraint
996ae0b0 698 then
4e7a4f6e 699 Error_Msg_N -- CODEFIX
a90bd866
RD
700 ("if qualified expression was meant, "
701 & "use apostrophe!", Constraint (E));
996ae0b0
RK
702 end if;
703
704 E := New_Occurrence_Of (Def_Id, Loc);
705 Rewrite (Expression (N), E);
706 end if;
707 end if;
708
709 Type_Id := Process_Subtype (E, N);
710 Acc_Type := Create_Itype (E_Allocator_Type, N);
84dad556 711 Set_Etype (Acc_Type, Acc_Type);
996ae0b0
RK
712 Set_Directly_Designated_Type (Acc_Type, Type_Id);
713 Check_Fully_Declared (Type_Id, N);
714
1baa4d2d 715 -- Ada 2005 (AI-231): If the designated type is itself an access
16b05213 716 -- type that excludes null, its default initialization will
75ad5042
ES
717 -- be a null object, and we can insert an unconditional raise
718 -- before the allocator.
2820d220 719
bfae1846 720 -- Ada 2012 (AI-104): A not null indication here is altogether
518ade91
AC
721 -- illegal.
722
2820d220 723 if Can_Never_Be_Null (Type_Id) then
75ad5042
ES
724 declare
725 Not_Null_Check : constant Node_Id :=
726 Make_Raise_Constraint_Error (Sloc (E),
727 Reason => CE_Null_Not_Allowed);
40b93859 728
75ad5042 729 begin
7b55fea6 730 if Expander_Active then
75ad5042
ES
731 Insert_Action (N, Not_Null_Check);
732 Analyze (Not_Null_Check);
40b93859 733
685bc70f
AC
734 elsif Warn_On_Ada_2012_Compatibility then
735 Error_Msg_N
736 ("null value not allowed here in Ada 2012?y?", E);
75ad5042
ES
737 end if;
738 end;
2820d220
AC
739 end if;
740
996ae0b0
RK
741 -- Check for missing initialization. Skip this check if we already
742 -- had errors on analyzing the allocator, since in that case these
24657705 743 -- are probably cascaded errors.
996ae0b0 744
83496138 745 if not Is_Definite_Subtype (Type_Id)
07fc65c4 746 and then Serious_Errors_Detected = Sav_Errs
996ae0b0 747 then
a4956515
AC
748 -- The build-in-place machinery may produce an allocator when
749 -- the designated type is indefinite but the underlying type is
750 -- not. In this case the unknown discriminants are meaningless
751 -- and should not trigger error messages. Check the parent node
752 -- because the allocator is marked as coming from source.
753
754 if Present (Underlying_Type (Type_Id))
83496138 755 and then Is_Definite_Subtype (Underlying_Type (Type_Id))
a4956515
AC
756 and then not Comes_From_Source (Parent (N))
757 then
758 null;
759
d43584ca
AC
760 -- An unusual case arises when the parent of a derived type is
761 -- a limited record extension with unknown discriminants, and
762 -- its full view has no discriminants.
763 --
764 -- A more general fix might be to create the proper underlying
765 -- type for such a derived type, but it is a record type with
766 -- no private attributes, so this required extending the
767 -- meaning of this attribute. ???
768
769 elsif Ekind (Etype (Type_Id)) = E_Record_Type_With_Private
770 and then Present (Underlying_Type (Etype (Type_Id)))
771 and then
772 not Has_Discriminants (Underlying_Type (Etype (Type_Id)))
773 and then not Comes_From_Source (Parent (N))
774 then
775 null;
776
a4956515 777 elsif Is_Class_Wide_Type (Type_Id) then
996ae0b0
RK
778 Error_Msg_N
779 ("initialization required in class-wide allocation", N);
a4956515 780
996ae0b0 781 else
0791fbe9 782 if Ada_Version < Ada_2005
24657705
HK
783 and then Is_Limited_Type (Type_Id)
784 then
785 Error_Msg_N ("unconstrained allocation not allowed", N);
786
787 if Is_Array_Type (Type_Id) then
788 Error_Msg_N
789 ("\constraint with array bounds required", N);
790
791 elsif Has_Unknown_Discriminants (Type_Id) then
792 null;
793
794 else pragma Assert (Has_Discriminants (Type_Id));
795 Error_Msg_N
796 ("\constraint with discriminant values required", N);
797 end if;
798
cf0e5ca7
BD
799 -- Limited Ada 2005 and general nonlimited case.
800 -- This is an error, except in the case of an
801 -- uninitialized allocator that is generated
802 -- for a build-in-place function return of a
803 -- discriminated but compile-time-known-size
804 -- type.
24657705
HK
805
806 else
cf0e5ca7
BD
807 if Original_Node (N) /= N
808 and then Nkind (Original_Node (N)) = N_Allocator
809 then
810 declare
811 Qual : constant Node_Id :=
812 Expression (Original_Node (N));
813 pragma Assert
814 (Nkind (Qual) = N_Qualified_Expression);
815 Call : constant Node_Id := Expression (Qual);
816 pragma Assert
817 (Is_Expanded_Build_In_Place_Call (Call));
818 begin
819 null;
820 end;
821
822 else
24657705 823 Error_Msg_N
cf0e5ca7
BD
824 ("uninitialized unconstrained allocation not "
825 & "allowed", N);
826
827 if Is_Array_Type (Type_Id) then
828 Error_Msg_N
829 ("\qualified expression or constraint with "
830 & "array bounds required", N);
831
832 elsif Has_Unknown_Discriminants (Type_Id) then
833 Error_Msg_N ("\qualified expression required", N);
834
835 else pragma Assert (Has_Discriminants (Type_Id));
836 Error_Msg_N
837 ("\qualified expression or constraint with "
838 & "discriminant values required", N);
839 end if;
24657705
HK
840 end if;
841 end if;
996ae0b0
RK
842 end if;
843 end if;
844 end;
845 end if;
846
aab883ec 847 if Is_Abstract_Type (Type_Id) then
996ae0b0
RK
848 Error_Msg_N ("cannot allocate abstract object", E);
849 end if;
850
851 if Has_Task (Designated_Type (Acc_Type)) then
6e937c1c 852 Check_Restriction (No_Tasking, N);
fbf5a39b 853 Check_Restriction (Max_Tasks, N);
996ae0b0 854 Check_Restriction (No_Task_Allocators, N);
70b3b953
GD
855 end if;
856
02bb0765
AC
857 -- Check restriction against dynamically allocated protected objects
858
859 if Has_Protected (Designated_Type (Acc_Type)) then
860 Check_Restriction (No_Protected_Type_Allocators, N);
861 end if;
862
646e2823
AC
863 -- AI05-0013-1: No_Nested_Finalization forbids allocators if the access
864 -- type is nested, and the designated type needs finalization. The rule
865 -- is conservative in that class-wide types need finalization.
866
867 if Needs_Finalization (Designated_Type (Acc_Type))
868 and then not Is_Library_Level_Entity (Acc_Type)
869 then
870 Check_Restriction (No_Nested_Finalization, N);
871 end if;
872
70b3b953
GD
873 -- Check that an allocator of a nested access type doesn't create a
874 -- protected object when restriction No_Local_Protected_Objects applies.
70b3b953 875
96e90ac1 876 if Has_Protected (Designated_Type (Acc_Type))
70b3b953
GD
877 and then not Is_Library_Level_Entity (Acc_Type)
878 then
879 Check_Restriction (No_Local_Protected_Objects, N);
996ae0b0
RK
880 end if;
881
4969efdf
AC
882 -- Likewise for No_Local_Timing_Events
883
884 if Has_Timing_Event (Designated_Type (Acc_Type))
885 and then not Is_Library_Level_Entity (Acc_Type)
886 then
887 Check_Restriction (No_Local_Timing_Events, N);
888 end if;
889
ffe9aba8
AC
890 -- If the No_Streams restriction is set, check that the type of the
891 -- object is not, and does not contain, any subtype derived from
892 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
893 -- Has_Stream just for efficiency reasons. There is no point in
894 -- spending time on a Has_Stream check if the restriction is not set.
895
7a963087 896 if Restriction_Check_Required (No_Streams) then
ffe9aba8
AC
897 if Has_Stream (Designated_Type (Acc_Type)) then
898 Check_Restriction (No_Streams, N);
899 end if;
900 end if;
901
996ae0b0
RK
902 Set_Etype (N, Acc_Type);
903
904 if not Is_Library_Level_Entity (Acc_Type) then
905 Check_Restriction (No_Local_Allocators, N);
906 end if;
2820d220 907
07fc65c4 908 if Serious_Errors_Detected > Sav_Errs then
996ae0b0
RK
909 Set_Error_Posted (N);
910 Set_Etype (N, Any_Type);
911 end if;
996ae0b0
RK
912 end Analyze_Allocator;
913
914 ---------------------------
915 -- Analyze_Arithmetic_Op --
916 ---------------------------
917
918 procedure Analyze_Arithmetic_Op (N : Node_Id) is
919 L : constant Node_Id := Left_Opnd (N);
920 R : constant Node_Id := Right_Opnd (N);
921 Op_Id : Entity_Id;
922
923 begin
924 Candidate_Type := Empty;
925 Analyze_Expression (L);
926 Analyze_Expression (R);
927
d469eabe
HK
928 -- If the entity is already set, the node is the instantiation of a
929 -- generic node with a non-local reference, or was manufactured by a
930 -- call to Make_Op_xxx. In either case the entity is known to be valid,
931 -- and we do not need to collect interpretations, instead we just get
932 -- the single possible interpretation.
996ae0b0
RK
933
934 Op_Id := Entity (N);
935
936 if Present (Op_Id) then
937 if Ekind (Op_Id) = E_Operator then
fa54f4da
EB
938 Set_Etype (N, Any_Type);
939 Find_Arithmetic_Types (L, R, Op_Id, N);
996ae0b0
RK
940 else
941 Set_Etype (N, Any_Type);
942 Add_One_Interp (N, Op_Id, Etype (Op_Id));
943 end if;
944
945 -- Entity is not already set, so we do need to collect interpretations
946
947 else
996ae0b0
RK
948 Set_Etype (N, Any_Type);
949
84dad556 950 Op_Id := Get_Name_Entity_Id (Chars (N));
996ae0b0
RK
951 while Present (Op_Id) loop
952 if Ekind (Op_Id) = E_Operator
953 and then Present (Next_Entity (First_Entity (Op_Id)))
954 then
955 Find_Arithmetic_Types (L, R, Op_Id, N);
956
957 -- The following may seem superfluous, because an operator cannot
958 -- be generic, but this ignores the cleverness of the author of
959 -- ACVC bc1013a.
960
961 elsif Is_Overloadable (Op_Id) then
962 Analyze_User_Defined_Binary_Op (N, Op_Id);
963 end if;
964
965 Op_Id := Homonym (Op_Id);
966 end loop;
967 end if;
968
969 Operator_Check (N);
22e89283 970 Check_Function_Writable_Actuals (N);
996ae0b0
RK
971 end Analyze_Arithmetic_Op;
972
973 ------------------
974 -- Analyze_Call --
975 ------------------
976
4c46b835
AC
977 -- Function, procedure, and entry calls are checked here. The Name in
978 -- the call may be overloaded. The actuals have been analyzed and may
979 -- themselves be overloaded. On exit from this procedure, the node N
980 -- may have zero, one or more interpretations. In the first case an
981 -- error message is produced. In the last case, the node is flagged
982 -- as overloaded and the interpretations are collected in All_Interp.
996ae0b0
RK
983
984 -- If the name is an Access_To_Subprogram, it cannot be overloaded, but
985 -- the type-checking is similar to that of other calls.
986
987 procedure Analyze_Call (N : Node_Id) is
4f324de2 988 Actuals : constant List_Id := Parameter_Associations (N);
f4ef7b06 989 Loc : constant Source_Ptr := Sloc (N);
63319f58 990 Nam : Node_Id;
996ae0b0
RK
991 X : Interp_Index;
992 It : Interp;
993 Nam_Ent : Entity_Id;
63319f58
RD
994 Success : Boolean := False;
995
996 Deref : Boolean := False;
1cb17b78
AC
997 -- Flag indicates whether an interpretation of the prefix is a
998 -- parameterless call that returns an access_to_subprogram.
996ae0b0 999
23685ae6
AC
1000 procedure Check_Mixed_Parameter_And_Named_Associations;
1001 -- Check that parameter and named associations are not mixed. This is
1002 -- a restriction in SPARK mode.
1003
288cbbbd
JM
1004 procedure Check_Writable_Actuals (N : Node_Id);
1005 -- If the call has out or in-out parameters then mark its outermost
1006 -- enclosing construct as a node on which the writable actuals check
1007 -- must be performed.
1008
996ae0b0 1009 function Name_Denotes_Function return Boolean;
5ff22245
ES
1010 -- If the type of the name is an access to subprogram, this may be the
1011 -- type of a name, or the return type of the function being called. If
1012 -- the name is not an entity then it can denote a protected function.
1013 -- Until we distinguish Etype from Return_Type, we must use this routine
1014 -- to resolve the meaning of the name in the call.
1015
1016 procedure No_Interpretation;
1017 -- Output error message when no valid interpretation exists
996ae0b0 1018
23685ae6
AC
1019 --------------------------------------------------
1020 -- Check_Mixed_Parameter_And_Named_Associations --
1021 --------------------------------------------------
1022
1023 procedure Check_Mixed_Parameter_And_Named_Associations is
1024 Actual : Node_Id;
1025 Named_Seen : Boolean;
f5afb270 1026
23685ae6 1027 begin
23685ae6 1028 Named_Seen := False;
f5afb270
AC
1029
1030 Actual := First (Actuals);
23685ae6
AC
1031 while Present (Actual) loop
1032 case Nkind (Actual) is
1033 when N_Parameter_Association =>
1034 if Named_Seen then
ce5ba43a 1035 Check_SPARK_05_Restriction
23685ae6
AC
1036 ("named association cannot follow positional one",
1037 Actual);
1038 exit;
1039 end if;
84dad556 1040
23685ae6
AC
1041 when others =>
1042 Named_Seen := True;
1043 end case;
1044
1045 Next (Actual);
1046 end loop;
1047 end Check_Mixed_Parameter_And_Named_Associations;
1048
288cbbbd
JM
1049 ----------------------------
1050 -- Check_Writable_Actuals --
1051 ----------------------------
1052
1053 -- The identification of conflicts in calls to functions with writable
551e1935 1054 -- actuals is performed in the analysis phase of the front end to ensure
288cbbbd
JM
1055 -- that it reports exactly the same errors compiling with and without
1056 -- expansion enabled. It is performed in two stages:
1057
551e1935
AC
1058 -- 1) When a call to a function with out-mode parameters is found,
1059 -- we climb to the outermost enclosing construct that can be
288cbbbd
JM
1060 -- evaluated in arbitrary order and we mark it with the flag
1061 -- Check_Actuals.
1062
551e1935
AC
1063 -- 2) When the analysis of the marked node is complete, we traverse
1064 -- its decorated subtree searching for conflicts (see function
1065 -- Sem_Util.Check_Function_Writable_Actuals).
288cbbbd 1066
551e1935
AC
1067 -- The unique exception to this general rule is for aggregates, since
1068 -- their analysis is performed by the front end in the resolution
1069 -- phase. For aggregates we do not climb to their enclosing construct:
288cbbbd
JM
1070 -- we restrict the analysis to the subexpressions initializing the
1071 -- aggregate components.
1072
1073 -- This implies that the analysis of expressions containing aggregates
551e1935 1074 -- is not complete, since there may be conflicts on writable actuals
288cbbbd
JM
1075 -- involving subexpressions of the enclosing logical or arithmetic
1076 -- expressions. However, we cannot wait and perform the analysis when
551e1935 1077 -- the whole subtree is resolved, since the subtrees may be transformed,
288cbbbd
JM
1078 -- thus adding extra complexity and computation cost to identify and
1079 -- report exactly the same errors compiling with and without expansion
1080 -- enabled.
1081
1082 procedure Check_Writable_Actuals (N : Node_Id) is
288cbbbd
JM
1083 begin
1084 if Comes_From_Source (N)
1085 and then Present (Get_Subprogram_Entity (N))
1086 and then Has_Out_Or_In_Out_Parameter (Get_Subprogram_Entity (N))
1087 then
1088 -- For procedures and entries there is no need to climb since
1089 -- we only need to check if the actuals of this call invoke
1090 -- functions whose out-mode parameters overlap.
1091
1092 if Nkind (N) /= N_Function_Call then
1093 Set_Check_Actuals (N);
1094
1095 -- For calls to functions we climb to the outermost enclosing
1096 -- construct where the out-mode actuals of this function may
1097 -- introduce conflicts.
1098
1099 else
1100 declare
5612989e 1101 Outermost : Node_Id := Empty; -- init to avoid warning
288cbbbd
JM
1102 P : Node_Id := N;
1103
1104 begin
1105 while Present (P) loop
551e1935 1106 -- For object declarations we can climb to the node from
288cbbbd
JM
1107 -- its object definition branch or from its initializing
1108 -- expression. We prefer to mark the child node as the
1109 -- outermost construct to avoid adding further complexity
551e1935 1110 -- to the routine that will later take care of
288cbbbd
JM
1111 -- performing the writable actuals check.
1112
22e89283
AC
1113 if Has_Arbitrary_Evaluation_Order (Nkind (P))
1114 and then not Nkind_In (P, N_Assignment_Statement,
1115 N_Object_Declaration)
288cbbbd
JM
1116 then
1117 Outermost := P;
1118 end if;
1119
5612989e 1120 -- Avoid climbing more than needed
288cbbbd 1121
22e89283 1122 exit when Stop_Subtree_Climbing (Nkind (P))
288cbbbd
JM
1123 or else (Nkind (P) = N_Range
1124 and then not
22e89283 1125 Nkind_In (Parent (P), N_In, N_Not_In));
288cbbbd
JM
1126
1127 P := Parent (P);
1128 end loop;
1129
1130 Set_Check_Actuals (Outermost);
1131 end;
1132 end if;
1133 end if;
1134 end Check_Writable_Actuals;
1135
996ae0b0
RK
1136 ---------------------------
1137 -- Name_Denotes_Function --
1138 ---------------------------
1139
1140 function Name_Denotes_Function return Boolean is
1141 begin
1142 if Is_Entity_Name (Nam) then
1143 return Ekind (Entity (Nam)) = E_Function;
996ae0b0
RK
1144 elsif Nkind (Nam) = N_Selected_Component then
1145 return Ekind (Entity (Selector_Name (Nam))) = E_Function;
996ae0b0
RK
1146 else
1147 return False;
1148 end if;
1149 end Name_Denotes_Function;
1150
5ff22245
ES
1151 -----------------------
1152 -- No_Interpretation --
1153 -----------------------
1154
1155 procedure No_Interpretation is
1156 L : constant Boolean := Is_List_Member (N);
1157 K : constant Node_Kind := Nkind (Parent (N));
1158
1159 begin
1160 -- If the node is in a list whose parent is not an expression then it
1161 -- must be an attempted procedure call.
1162
1163 if L and then K not in N_Subexpr then
1164 if Ekind (Entity (Nam)) = E_Generic_Procedure then
1165 Error_Msg_NE
1166 ("must instantiate generic procedure& before call",
1167 Nam, Entity (Nam));
1168 else
84dad556 1169 Error_Msg_N ("procedure or entry name expected", Nam);
5ff22245
ES
1170 end if;
1171
1172 -- Check for tasking cases where only an entry call will do
1173
1174 elsif not L
1175 and then Nkind_In (K, N_Entry_Call_Alternative,
1176 N_Triggering_Alternative)
1177 then
1178 Error_Msg_N ("entry name expected", Nam);
1179
1180 -- Otherwise give general error message
1181
1182 else
1183 Error_Msg_N ("invalid prefix in call", Nam);
1184 end if;
1185 end No_Interpretation;
1186
996ae0b0
RK
1187 -- Start of processing for Analyze_Call
1188
1189 begin
6480338a 1190 if Restriction_Check_Required (SPARK_05) then
23685ae6
AC
1191 Check_Mixed_Parameter_And_Named_Associations;
1192 end if;
1193
996ae0b0
RK
1194 -- Initialize the type of the result of the call to the error type,
1195 -- which will be reset if the type is successfully resolved.
1196
1197 Set_Etype (N, Any_Type);
1198
63319f58
RD
1199 Nam := Name (N);
1200
996ae0b0
RK
1201 if not Is_Overloaded (Nam) then
1202
1203 -- Only one interpretation to check
1204
1205 if Ekind (Etype (Nam)) = E_Subprogram_Type then
1206 Nam_Ent := Etype (Nam);
1207
758c442c
GD
1208 -- If the prefix is an access_to_subprogram, this may be an indirect
1209 -- call. This is the case if the name in the call is not an entity
1210 -- name, or if it is a function name in the context of a procedure
1211 -- call. In this latter case, we have a call to a parameterless
1212 -- function that returns a pointer_to_procedure which is the entity
5ff22245
ES
1213 -- being called. Finally, F (X) may be a call to a parameterless
1214 -- function that returns a pointer to a function with parameters.
80e59506 1215 -- Note that if F returns an access-to-subprogram whose designated
4bb9c7b9
AC
1216 -- type is an array, F (X) cannot be interpreted as an indirect call
1217 -- through the result of the call to F.
758c442c 1218
996ae0b0
RK
1219 elsif Is_Access_Type (Etype (Nam))
1220 and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type
758c442c
GD
1221 and then
1222 (not Name_Denotes_Function
b2834fbd
AC
1223 or else Nkind (N) = N_Procedure_Call_Statement
1224 or else
1225 (Nkind (Parent (N)) /= N_Explicit_Dereference
1226 and then Is_Entity_Name (Nam)
1227 and then No (First_Formal (Entity (Nam)))
1228 and then not
1229 Is_Array_Type (Etype (Designated_Type (Etype (Nam))))
1230 and then Present (Actuals)))
996ae0b0
RK
1231 then
1232 Nam_Ent := Designated_Type (Etype (Nam));
1233 Insert_Explicit_Dereference (Nam);
1234
1235 -- Selected component case. Simple entry or protected operation,
1236 -- where the entry name is given by the selector name.
1237
1238 elsif Nkind (Nam) = N_Selected_Component then
1239 Nam_Ent := Entity (Selector_Name (Nam));
1240
bce79204
AC
1241 if not Ekind_In (Nam_Ent, E_Entry,
1242 E_Entry_Family,
1243 E_Function,
1244 E_Procedure)
996ae0b0
RK
1245 then
1246 Error_Msg_N ("name in call is not a callable entity", Nam);
1247 Set_Etype (N, Any_Type);
1248 return;
1249 end if;
1250
1251 -- If the name is an Indexed component, it can be a call to a member
1252 -- of an entry family. The prefix must be a selected component whose
1253 -- selector is the entry. Analyze_Procedure_Call normalizes several
1254 -- kinds of call into this form.
1255
1256 elsif Nkind (Nam) = N_Indexed_Component then
996ae0b0
RK
1257 if Nkind (Prefix (Nam)) = N_Selected_Component then
1258 Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
996ae0b0
RK
1259 else
1260 Error_Msg_N ("name in call is not a callable entity", Nam);
1261 Set_Etype (N, Any_Type);
1262 return;
996ae0b0
RK
1263 end if;
1264
1265 elsif not Is_Entity_Name (Nam) then
1266 Error_Msg_N ("name in call is not a callable entity", Nam);
1267 Set_Etype (N, Any_Type);
1268 return;
1269
1270 else
1271 Nam_Ent := Entity (Nam);
1272
5f50020a
ES
1273 -- If not overloadable, this may be a generalized indexing
1274 -- operation with named associations. Rewrite again as an
1275 -- indexed component and analyze as container indexing.
996ae0b0
RK
1276
1277 if not Is_Overloadable (Nam_Ent) then
32bba3c9
AC
1278 if Present
1279 (Find_Value_Of_Aspect
1280 (Etype (Nam_Ent), Aspect_Constant_Indexing))
5f50020a
ES
1281 then
1282 Replace (N,
1283 Make_Indexed_Component (Sloc (N),
32bba3c9 1284 Prefix => Nam,
5f50020a
ES
1285 Expressions => Parameter_Associations (N)));
1286
1287 if Try_Container_Indexing (N, Nam, Expressions (N)) then
1288 return;
1289 else
1290 No_Interpretation;
1291 end if;
1292
1293 else
1294 No_Interpretation;
1295 end if;
32bba3c9 1296
5ff22245
ES
1297 return;
1298 end if;
1299 end if;
996ae0b0 1300
5ff22245
ES
1301 -- Operations generated for RACW stub types are called only through
1302 -- dispatching, and can never be the static interpretation of a call.
996ae0b0 1303
5ff22245
ES
1304 if Is_RACW_Stub_Type_Operation (Nam_Ent) then
1305 No_Interpretation;
1306 return;
996ae0b0
RK
1307 end if;
1308
1309 Analyze_One_Call (N, Nam_Ent, True, Success);
1310
ec6078e3
ES
1311 -- If this is an indirect call, the return type of the access_to
1312 -- subprogram may be an incomplete type. At the point of the call,
947430d5
AC
1313 -- use the full type if available, and at the same time update the
1314 -- return type of the access_to_subprogram.
ec6078e3
ES
1315
1316 if Success
d469eabe 1317 and then Nkind (Nam) = N_Explicit_Dereference
ec6078e3
ES
1318 and then Ekind (Etype (N)) = E_Incomplete_Type
1319 and then Present (Full_View (Etype (N)))
1320 then
1321 Set_Etype (N, Full_View (Etype (N)));
1322 Set_Etype (Nam_Ent, Etype (N));
1323 end if;
1324
a7e68e7f
HK
1325 -- Overloaded call
1326
996ae0b0 1327 else
5ff22245
ES
1328 -- An overloaded selected component must denote overloaded operations
1329 -- of a concurrent type. The interpretations are attached to the
1330 -- simple name of those operations.
996ae0b0
RK
1331
1332 if Nkind (Nam) = N_Selected_Component then
1333 Nam := Selector_Name (Nam);
1334 end if;
1335
1336 Get_First_Interp (Nam, X, It);
996ae0b0
RK
1337 while Present (It.Nam) loop
1338 Nam_Ent := It.Nam;
1cb17b78 1339 Deref := False;
996ae0b0
RK
1340
1341 -- Name may be call that returns an access to subprogram, or more
1342 -- generally an overloaded expression one of whose interpretations
947430d5
AC
1343 -- yields an access to subprogram. If the name is an entity, we do
1344 -- not dereference, because the node is a call that returns the
1345 -- access type: note difference between f(x), where the call may
1346 -- return an access subprogram type, and f(x)(y), where the type
1347 -- returned by the call to f is implicitly dereferenced to analyze
1348 -- the outer call.
996ae0b0
RK
1349
1350 if Is_Access_Type (Nam_Ent) then
1351 Nam_Ent := Designated_Type (Nam_Ent);
1352
1353 elsif Is_Access_Type (Etype (Nam_Ent))
1cb17b78
AC
1354 and then
1355 (not Is_Entity_Name (Nam)
1356 or else Nkind (N) = N_Procedure_Call_Statement)
996ae0b0
RK
1357 and then Ekind (Designated_Type (Etype (Nam_Ent)))
1358 = E_Subprogram_Type
1359 then
1360 Nam_Ent := Designated_Type (Etype (Nam_Ent));
1cb17b78
AC
1361
1362 if Is_Entity_Name (Nam) then
1363 Deref := True;
1364 end if;
996ae0b0
RK
1365 end if;
1366
7415029d
AC
1367 -- If the call has been rewritten from a prefixed call, the first
1368 -- parameter has been analyzed, but may need a subsequent
1369 -- dereference, so skip its analysis now.
1370
dc67cfea 1371 if Is_Rewrite_Substitution (N)
7415029d
AC
1372 and then Nkind (Original_Node (N)) = Nkind (N)
1373 and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N)))
1374 and then Present (Parameter_Associations (N))
1375 and then Present (Etype (First (Parameter_Associations (N))))
1376 then
1377 Analyze_One_Call
1378 (N, Nam_Ent, False, Success, Skip_First => True);
1379 else
1380 Analyze_One_Call (N, Nam_Ent, False, Success);
1381 end if;
996ae0b0
RK
1382
1383 -- If the interpretation succeeds, mark the proper type of the
1384 -- prefix (any valid candidate will do). If not, remove the
f4ef7b06
AC
1385 -- candidate interpretation. If this is a parameterless call
1386 -- on an anonymous access to subprogram, X is a variable with
1387 -- an access discriminant D, the entity in the interpretation is
1388 -- D, so rewrite X as X.D.all.
996ae0b0
RK
1389
1390 if Success then
1cb17b78
AC
1391 if Deref
1392 and then Nkind (Parent (N)) /= N_Explicit_Dereference
1393 then
f4ef7b06
AC
1394 if Ekind (It.Nam) = E_Discriminant
1395 and then Has_Implicit_Dereference (It.Nam)
1396 then
1397 Rewrite (Name (N),
1398 Make_Explicit_Dereference (Loc,
4f324de2
AC
1399 Prefix =>
1400 Make_Selected_Component (Loc,
1401 Prefix =>
1402 New_Occurrence_Of (Entity (Nam), Loc),
1403 Selector_Name =>
1404 New_Occurrence_Of (It.Nam, Loc))));
1405
f4ef7b06
AC
1406 Analyze (N);
1407 return;
1408
1409 else
1410 Set_Entity (Nam, It.Nam);
1411 Insert_Explicit_Dereference (Nam);
1412 Set_Etype (Nam, Nam_Ent);
1413 end if;
1cb17b78
AC
1414
1415 else
1416 Set_Etype (Nam, It.Typ);
1417 end if;
996ae0b0 1418
4f324de2 1419 elsif Nkind_In (Name (N), N_Function_Call, N_Selected_Component)
fbf5a39b 1420 then
996ae0b0
RK
1421 Remove_Interp (X);
1422 end if;
1423
1424 Get_Next_Interp (X, It);
1425 end loop;
1426
a7e68e7f
HK
1427 -- If the name is the result of a function call, it can only be a
1428 -- call to a function returning an access to subprogram. Insert
1429 -- explicit dereference.
996ae0b0
RK
1430
1431 if Nkind (Nam) = N_Function_Call then
1432 Insert_Explicit_Dereference (Nam);
1433 end if;
1434
1435 if Etype (N) = Any_Type then
1436
1437 -- None of the interpretations is compatible with the actuals
1438
1439 Diagnose_Call (N, Nam);
1440
1441 -- Special checks for uninstantiated put routines
1442
1443 if Nkind (N) = N_Procedure_Call_Statement
1444 and then Is_Entity_Name (Nam)
1445 and then Chars (Nam) = Name_Put
1446 and then List_Length (Actuals) = 1
1447 then
1448 declare
1449 Arg : constant Node_Id := First (Actuals);
1450 Typ : Entity_Id;
1451
1452 begin
1453 if Nkind (Arg) = N_Parameter_Association then
1454 Typ := Etype (Explicit_Actual_Parameter (Arg));
1455 else
1456 Typ := Etype (Arg);
1457 end if;
1458
1459 if Is_Signed_Integer_Type (Typ) then
1460 Error_Msg_N
a90bd866
RD
1461 ("possible missing instantiation of "
1462 & "'Text_'I'O.'Integer_'I'O!", Nam);
996ae0b0
RK
1463
1464 elsif Is_Modular_Integer_Type (Typ) then
1465 Error_Msg_N
a90bd866
RD
1466 ("possible missing instantiation of "
1467 & "'Text_'I'O.'Modular_'I'O!", Nam);
996ae0b0
RK
1468
1469 elsif Is_Floating_Point_Type (Typ) then
1470 Error_Msg_N
a90bd866
RD
1471 ("possible missing instantiation of "
1472 & "'Text_'I'O.'Float_'I'O!", Nam);
996ae0b0
RK
1473
1474 elsif Is_Ordinary_Fixed_Point_Type (Typ) then
1475 Error_Msg_N
a90bd866
RD
1476 ("possible missing instantiation of "
1477 & "'Text_'I'O.'Fixed_'I'O!", Nam);
996ae0b0
RK
1478
1479 elsif Is_Decimal_Fixed_Point_Type (Typ) then
1480 Error_Msg_N
a90bd866
RD
1481 ("possible missing instantiation of "
1482 & "'Text_'I'O.'Decimal_'I'O!", Nam);
996ae0b0
RK
1483
1484 elsif Is_Enumeration_Type (Typ) then
1485 Error_Msg_N
a90bd866
RD
1486 ("possible missing instantiation of "
1487 & "'Text_'I'O.'Enumeration_'I'O!", Nam);
996ae0b0
RK
1488 end if;
1489 end;
1490 end if;
1491
1492 elsif not Is_Overloaded (N)
1493 and then Is_Entity_Name (Nam)
1494 then
aab883ec
ES
1495 -- Resolution yields a single interpretation. Verify that the
1496 -- reference has capitalization consistent with the declaration.
996ae0b0 1497
e7ba564f 1498 Set_Entity_With_Checks (Nam, Entity (Nam));
996ae0b0
RK
1499 Generate_Reference (Entity (Nam), Nam);
1500
1501 Set_Etype (Nam, Etype (Entity (Nam)));
30c20106
AC
1502 else
1503 Remove_Abstract_Operations (N);
996ae0b0
RK
1504 end if;
1505
1506 End_Interp_List;
1507 end if;
288cbbbd
JM
1508
1509 if Ada_Version >= Ada_2012 then
1510
1511 -- Check if the call contains a function with writable actuals
1512
1513 Check_Writable_Actuals (N);
1514
551e1935
AC
1515 -- If found and the outermost construct that can be evaluated in
1516 -- an arbitrary order is precisely this call, then check all its
288cbbbd
JM
1517 -- actuals.
1518
22e89283 1519 Check_Function_Writable_Actuals (N);
b63d61f7
AC
1520
1521 -- The return type of the function may be incomplete. This can be
1522 -- the case if the type is a generic formal, or a limited view. It
1523 -- can also happen when the function declaration appears before the
1524 -- full view of the type (which is legal in Ada 2012) and the call
1525 -- appears in a different unit, in which case the incomplete view
a316b3fc 1526 -- must be replaced with the full view (or the nonlimited view)
d3271136
EB
1527 -- to prevent subsequent type errors. Note that the usual install/
1528 -- removal of limited_with clauses is not sufficient to handle this
a316b3fc 1529 -- case, because the limited view may have been captured in another
d3271136
EB
1530 -- compilation unit that defines the current function.
1531
1532 if Is_Incomplete_Type (Etype (N)) then
1533 if Present (Full_View (Etype (N))) then
1534 if Is_Entity_Name (Nam) then
1535 Set_Etype (Nam, Full_View (Etype (N)));
1536 Set_Etype (Entity (Nam), Full_View (Etype (N)));
1537 end if;
b63d61f7 1538
d3271136 1539 Set_Etype (N, Full_View (Etype (N)));
b63d61f7 1540
d3271136
EB
1541 elsif From_Limited_With (Etype (N))
1542 and then Present (Non_Limited_View (Etype (N)))
1543 then
1544 Set_Etype (N, Non_Limited_View (Etype (N)));
c312b9f2
PMR
1545
1546 -- If there is no completion for the type, this may be because
1547 -- there is only a limited view of it and there is nothing in
1548 -- the context of the current unit that has required a regular
1549 -- compilation of the unit containing the type. We recognize
604801a4 1550 -- this unusual case by the fact that unit is not analyzed.
c312b9f2
PMR
1551 -- Note that the call being analyzed is in a different unit from
1552 -- the function declaration, and nothing indicates that the type
1553 -- is a limited view.
1554
1555 elsif Ekind (Scope (Etype (N))) = E_Package
1556 and then Present (Limited_View (Scope (Etype (N))))
1557 and then not Analyzed (Unit_Declaration_Node (Scope (Etype (N))))
1558 then
c312b9f2 1559 Error_Msg_NE
d63199d8
PMR
1560 ("cannot call function that returns limited view of}",
1561 N, Etype (N));
1562
1563 Error_Msg_NE
1564 ("\there must be a regular with_clause for package & in the "
1565 & "current unit, or in some unit in its context",
1566 N, Scope (Etype (N)));
1567
c312b9f2 1568 Set_Etype (N, Any_Type);
d3271136 1569 end if;
b63d61f7 1570 end if;
288cbbbd 1571 end if;
996ae0b0
RK
1572 end Analyze_Call;
1573
19d846a0
RD
1574 -----------------------------
1575 -- Analyze_Case_Expression --
1576 -----------------------------
1577
1578 procedure Analyze_Case_Expression (N : Node_Id) is
19d846a0
RD
1579 procedure Non_Static_Choice_Error (Choice : Node_Id);
1580 -- Error routine invoked by the generic instantiation below when
1581 -- the case expression has a non static choice.
1582
15918371
AC
1583 package Case_Choices_Analysis is new
1584 Generic_Analyze_Choices
1585 (Process_Associated_Node => No_OP);
1586 use Case_Choices_Analysis;
1587
1588 package Case_Choices_Checking is new
1589 Generic_Check_Choices
1590 (Process_Empty_Choice => No_OP,
19d846a0
RD
1591 Process_Non_Static_Choice => Non_Static_Choice_Error,
1592 Process_Associated_Node => No_OP);
15918371 1593 use Case_Choices_Checking;
19d846a0 1594
19d846a0
RD
1595 -----------------------------
1596 -- Non_Static_Choice_Error --
1597 -----------------------------
1598
1599 procedure Non_Static_Choice_Error (Choice : Node_Id) is
1600 begin
1601 Flag_Non_Static_Expr
1602 ("choice given in case expression is not static!", Choice);
1603 end Non_Static_Choice_Error;
1604
752b81d9
AC
1605 -- Local variables
1606
1607 Expr : constant Node_Id := Expression (N);
752b81d9
AC
1608 Alt : Node_Id;
1609 Exp_Type : Entity_Id;
1610 Exp_Btype : Entity_Id;
1611
308aab0b
AC
1612 FirstX : Node_Id := Empty;
1613 -- First expression in the case for which there is some type information
1614 -- available, i.e. it is not Any_Type, which can happen because of some
1615 -- error, or from the use of e.g. raise Constraint_Error.
1616
752b81d9 1617 Others_Present : Boolean;
15918371 1618 -- Indicates if Others was present
752b81d9 1619
e49de265 1620 Wrong_Alt : Node_Id := Empty;
10671e7a
AC
1621 -- For error reporting
1622
19d846a0
RD
1623 -- Start of processing for Analyze_Case_Expression
1624
1625 begin
1626 if Comes_From_Source (N) then
c86cf714 1627 Check_Compiler_Unit ("case expression", N);
19d846a0
RD
1628 end if;
1629
1630 Analyze_And_Resolve (Expr, Any_Discrete);
1631 Check_Unset_Reference (Expr);
1632 Exp_Type := Etype (Expr);
1633 Exp_Btype := Base_Type (Exp_Type);
1634
1635 Alt := First (Alternatives (N));
1636 while Present (Alt) loop
b55993b3
AC
1637 if Error_Posted (Expression (Alt)) then
1638 return;
1639 end if;
1640
19d846a0 1641 Analyze (Expression (Alt));
308aab0b
AC
1642
1643 if No (FirstX) and then Etype (Expression (Alt)) /= Any_Type then
1644 FirstX := Expression (Alt);
1645 end if;
1646
19d846a0
RD
1647 Next (Alt);
1648 end loop;
1649
308aab0b
AC
1650 -- Get our initial type from the first expression for which we got some
1651 -- useful type information from the expression.
1652
a6354842
AC
1653 if No (FirstX) then
1654 return;
1655 end if;
1656
19d846a0
RD
1657 if not Is_Overloaded (FirstX) then
1658 Set_Etype (N, Etype (FirstX));
1659
1660 else
1661 declare
1662 I : Interp_Index;
1663 It : Interp;
1664
1665 begin
1666 Set_Etype (N, Any_Type);
1667
1668 Get_First_Interp (FirstX, I, It);
1669 while Present (It.Nam) loop
1670
308e6f3a
RW
1671 -- For each interpretation of the first expression, we only
1672 -- add the interpretation if every other expression in the
19d846a0
RD
1673 -- case expression alternatives has a compatible type.
1674
1675 Alt := Next (First (Alternatives (N)));
1676 while Present (Alt) loop
1677 exit when not Has_Compatible_Type (Expression (Alt), It.Typ);
1678 Next (Alt);
1679 end loop;
1680
1681 if No (Alt) then
1682 Add_One_Interp (N, It.Typ, It.Typ);
10671e7a
AC
1683 else
1684 Wrong_Alt := Alt;
19d846a0
RD
1685 end if;
1686
1687 Get_Next_Interp (I, It);
1688 end loop;
1689 end;
1690 end if;
1691
1692 Exp_Btype := Base_Type (Exp_Type);
1693
1694 -- The expression must be of a discrete type which must be determinable
1695 -- independently of the context in which the expression occurs, but
1696 -- using the fact that the expression must be of a discrete type.
1697 -- Moreover, the type this expression must not be a character literal
1698 -- (which is always ambiguous).
1699
1700 -- If error already reported by Resolve, nothing more to do
1701
15918371 1702 if Exp_Btype = Any_Discrete or else Exp_Btype = Any_Type then
19d846a0
RD
1703 return;
1704
bf0b0e5e
AC
1705 -- Special casee message for character literal
1706
19d846a0
RD
1707 elsif Exp_Btype = Any_Character then
1708 Error_Msg_N
1709 ("character literal as case expression is ambiguous", Expr);
1710 return;
1711 end if;
1712
10671e7a 1713 if Etype (N) = Any_Type and then Present (Wrong_Alt) then
bf0b0e5e
AC
1714 Error_Msg_N
1715 ("type incompatible with that of previous alternatives",
1716 Expression (Wrong_Alt));
10671e7a
AC
1717 return;
1718 end if;
1719
19d846a0
RD
1720 -- If the case expression is a formal object of mode in out, then
1721 -- treat it as having a nonstatic subtype by forcing use of the base
64ac53f4 1722 -- type (which has to get passed to Check_Case_Choices below). Also
19d846a0
RD
1723 -- use base type when the case expression is parenthesized.
1724
1725 if Paren_Count (Expr) > 0
1726 or else (Is_Entity_Name (Expr)
1727 and then Ekind (Entity (Expr)) = E_Generic_In_Out_Parameter)
1728 then
1729 Exp_Type := Exp_Btype;
1730 end if;
1731
752b81d9
AC
1732 -- The case expression alternatives cover the range of a static subtype
1733 -- subject to aspect Static_Predicate. Do not check the choices when the
1734 -- case expression has not been fully analyzed yet because this may lead
1735 -- to bogus errors.
1736
edab6088 1737 if Is_OK_Static_Subtype (Exp_Type)
ee4eee0a 1738 and then Has_Static_Predicate_Aspect (Exp_Type)
752b81d9
AC
1739 and then In_Spec_Expression
1740 then
1741 null;
1742
15918371 1743 -- Call Analyze_Choices and Check_Choices to do the rest of the work
19d846a0 1744
752b81d9 1745 else
15918371
AC
1746 Analyze_Choices (Alternatives (N), Exp_Type);
1747 Check_Choices (N, Alternatives (N), Exp_Type, Others_Present);
19d846a0 1748
dcd5fd67
PMR
1749 if Exp_Type = Universal_Integer and then not Others_Present then
1750 Error_Msg_N
1751 ("case on universal integer requires OTHERS choice", Expr);
1752 end if;
19d846a0
RD
1753 end if;
1754 end Analyze_Case_Expression;
1755
996ae0b0
RK
1756 ---------------------------
1757 -- Analyze_Comparison_Op --
1758 ---------------------------
1759
1760 procedure Analyze_Comparison_Op (N : Node_Id) is
1761 L : constant Node_Id := Left_Opnd (N);
1762 R : constant Node_Id := Right_Opnd (N);
1763 Op_Id : Entity_Id := Entity (N);
1764
1765 begin
1766 Set_Etype (N, Any_Type);
1767 Candidate_Type := Empty;
1768
1769 Analyze_Expression (L);
1770 Analyze_Expression (R);
1771
1772 if Present (Op_Id) then
996ae0b0
RK
1773 if Ekind (Op_Id) = E_Operator then
1774 Find_Comparison_Types (L, R, Op_Id, N);
1775 else
1776 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1777 end if;
1778
1779 if Is_Overloaded (L) then
1780 Set_Etype (L, Intersect_Types (L, R));
1781 end if;
1782
1783 else
1784 Op_Id := Get_Name_Entity_Id (Chars (N));
996ae0b0 1785 while Present (Op_Id) loop
996ae0b0
RK
1786 if Ekind (Op_Id) = E_Operator then
1787 Find_Comparison_Types (L, R, Op_Id, N);
1788 else
1789 Analyze_User_Defined_Binary_Op (N, Op_Id);
1790 end if;
1791
1792 Op_Id := Homonym (Op_Id);
1793 end loop;
1794 end if;
1795
1796 Operator_Check (N);
22e89283 1797 Check_Function_Writable_Actuals (N);
996ae0b0
RK
1798 end Analyze_Comparison_Op;
1799
1800 ---------------------------
1801 -- Analyze_Concatenation --
1802 ---------------------------
1803
fe39cf20
BD
1804 procedure Analyze_Concatenation (N : Node_Id) is
1805
1806 -- We wish to avoid deep recursion, because concatenations are often
1807 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
1808 -- operands nonrecursively until we find something that is not a
1809 -- concatenation (A in this case), or has already been analyzed. We
1810 -- analyze that, and then walk back up the tree following Parent
1811 -- pointers, calling Analyze_Concatenation_Rest to do the rest of the
1812 -- work at each level. The Parent pointers allow us to avoid recursion,
1813 -- and thus avoid running out of memory.
1814
1815 NN : Node_Id := N;
1816 L : Node_Id;
1817
1818 begin
1819 Candidate_Type := Empty;
1820
1821 -- The following code is equivalent to:
1822
1823 -- Set_Etype (N, Any_Type);
1824 -- Analyze_Expression (Left_Opnd (N));
1825 -- Analyze_Concatenation_Rest (N);
1826
1827 -- where the Analyze_Expression call recurses back here if the left
1828 -- operand is a concatenation.
1829
1830 -- Walk down left operands
1831
1832 loop
1833 Set_Etype (NN, Any_Type);
1834 L := Left_Opnd (NN);
1835 exit when Nkind (L) /= N_Op_Concat or else Analyzed (L);
1836 NN := L;
1837 end loop;
1838
1839 -- Now (given the above example) NN is A&B and L is A
1840
1841 -- First analyze L ...
1842
1843 Analyze_Expression (L);
1844
1845 -- ... then walk NN back up until we reach N (where we started), calling
1846 -- Analyze_Concatenation_Rest along the way.
1847
1848 loop
1849 Analyze_Concatenation_Rest (NN);
1850 exit when NN = N;
1851 NN := Parent (NN);
1852 end loop;
1853 end Analyze_Concatenation;
1854
1855 --------------------------------
1856 -- Analyze_Concatenation_Rest --
1857 --------------------------------
1858
996ae0b0
RK
1859 -- If the only one-dimensional array type in scope is String,
1860 -- this is the resulting type of the operation. Otherwise there
1861 -- will be a concatenation operation defined for each user-defined
1862 -- one-dimensional array.
1863
fe39cf20 1864 procedure Analyze_Concatenation_Rest (N : Node_Id) is
996ae0b0
RK
1865 L : constant Node_Id := Left_Opnd (N);
1866 R : constant Node_Id := Right_Opnd (N);
1867 Op_Id : Entity_Id := Entity (N);
1868 LT : Entity_Id;
1869 RT : Entity_Id;
1870
1871 begin
996ae0b0
RK
1872 Analyze_Expression (R);
1873
cd3cd5b1
AC
1874 -- If the entity is present, the node appears in an instance, and
1875 -- denotes a predefined concatenation operation. The resulting type is
1876 -- obtained from the arguments when possible. If the arguments are
1877 -- aggregates, the array type and the concatenation type must be
fbf5a39b 1878 -- visible.
996ae0b0
RK
1879
1880 if Present (Op_Id) then
1881 if Ekind (Op_Id) = E_Operator then
996ae0b0
RK
1882 LT := Base_Type (Etype (L));
1883 RT := Base_Type (Etype (R));
1884
1885 if Is_Array_Type (LT)
1886 and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
1887 then
1888 Add_One_Interp (N, Op_Id, LT);
1889
1890 elsif Is_Array_Type (RT)
1891 and then LT = Base_Type (Component_Type (RT))
1892 then
1893 Add_One_Interp (N, Op_Id, RT);
1894
fbf5a39b
AC
1895 -- If one operand is a string type or a user-defined array type,
1896 -- and the other is a literal, result is of the specific type.
1897
1898 elsif
1899 (Root_Type (LT) = Standard_String
1900 or else Scope (LT) /= Standard_Standard)
1901 and then Etype (R) = Any_String
1902 then
1903 Add_One_Interp (N, Op_Id, LT);
1904
1905 elsif
1906 (Root_Type (RT) = Standard_String
1907 or else Scope (RT) /= Standard_Standard)
1908 and then Etype (L) = Any_String
1909 then
1910 Add_One_Interp (N, Op_Id, RT);
1911
1912 elsif not Is_Generic_Type (Etype (Op_Id)) then
996ae0b0 1913 Add_One_Interp (N, Op_Id, Etype (Op_Id));
fbf5a39b
AC
1914
1915 else
4c46b835 1916 -- Type and its operations must be visible
fbf5a39b
AC
1917
1918 Set_Entity (N, Empty);
1919 Analyze_Concatenation (N);
996ae0b0
RK
1920 end if;
1921
1922 else
1923 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1924 end if;
1925
1926 else
1a8fae99 1927 Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
996ae0b0
RK
1928 while Present (Op_Id) loop
1929 if Ekind (Op_Id) = E_Operator then
1a8fae99 1930
155ed511
SL
1931 -- Do not consider operators declared in dead code, they
1932 -- cannot be part of the resolution.
1a8fae99
ES
1933
1934 if Is_Eliminated (Op_Id) then
1935 null;
1936 else
1937 Find_Concatenation_Types (L, R, Op_Id, N);
1938 end if;
1939
996ae0b0
RK
1940 else
1941 Analyze_User_Defined_Binary_Op (N, Op_Id);
1942 end if;
1943
1944 Op_Id := Homonym (Op_Id);
1945 end loop;
1946 end if;
1947
1948 Operator_Check (N);
fe39cf20 1949 end Analyze_Concatenation_Rest;
996ae0b0 1950
996ae0b0
RK
1951 -------------------------
1952 -- Analyze_Equality_Op --
1953 -------------------------
1954
1955 procedure Analyze_Equality_Op (N : Node_Id) is
4c46b835
AC
1956 Loc : constant Source_Ptr := Sloc (N);
1957 L : constant Node_Id := Left_Opnd (N);
1958 R : constant Node_Id := Right_Opnd (N);
1959 Op_Id : Entity_Id;
996ae0b0
RK
1960
1961 begin
1962 Set_Etype (N, Any_Type);
1963 Candidate_Type := Empty;
1964
1965 Analyze_Expression (L);
1966 Analyze_Expression (R);
1967
1968 -- If the entity is set, the node is a generic instance with a non-local
1969 -- reference to the predefined operator or to a user-defined function.
1970 -- It can also be an inequality that is expanded into the negation of a
1971 -- call to a user-defined equality operator.
1972
1973 -- For the predefined case, the result is Boolean, regardless of the
21d7ef70 1974 -- type of the operands. The operands may even be limited, if they are
996ae0b0
RK
1975 -- generic actuals. If they are overloaded, label the left argument with
1976 -- the common type that must be present, or with the type of the formal
1977 -- of the user-defined function.
1978
1979 if Present (Entity (N)) then
996ae0b0
RK
1980 Op_Id := Entity (N);
1981
1982 if Ekind (Op_Id) = E_Operator then
1983 Add_One_Interp (N, Op_Id, Standard_Boolean);
1984 else
1985 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1986 end if;
1987
1988 if Is_Overloaded (L) then
996ae0b0
RK
1989 if Ekind (Op_Id) = E_Operator then
1990 Set_Etype (L, Intersect_Types (L, R));
1991 else
1992 Set_Etype (L, Etype (First_Formal (Op_Id)));
1993 end if;
1994 end if;
1995
1996 else
1997 Op_Id := Get_Name_Entity_Id (Chars (N));
996ae0b0 1998 while Present (Op_Id) loop
996ae0b0
RK
1999 if Ekind (Op_Id) = E_Operator then
2000 Find_Equality_Types (L, R, Op_Id, N);
2001 else
2002 Analyze_User_Defined_Binary_Op (N, Op_Id);
2003 end if;
2004
2005 Op_Id := Homonym (Op_Id);
2006 end loop;
2007 end if;
2008
84dad556
AC
2009 -- If there was no match, and the operator is inequality, this may be
2010 -- a case where inequality has not been made explicit, as for tagged
2011 -- types. Analyze the node as the negation of an equality operation.
2012 -- This cannot be done earlier, because before analysis we cannot rule
2013 -- out the presence of an explicit inequality.
996ae0b0
RK
2014
2015 if Etype (N) = Any_Type
2016 and then Nkind (N) = N_Op_Ne
2017 then
2018 Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
996ae0b0 2019 while Present (Op_Id) loop
996ae0b0
RK
2020 if Ekind (Op_Id) = E_Operator then
2021 Find_Equality_Types (L, R, Op_Id, N);
2022 else
2023 Analyze_User_Defined_Binary_Op (N, Op_Id);
2024 end if;
2025
2026 Op_Id := Homonym (Op_Id);
2027 end loop;
2028
2029 if Etype (N) /= Any_Type then
2030 Op_Id := Entity (N);
2031
2032 Rewrite (N,
2033 Make_Op_Not (Loc,
2034 Right_Opnd =>
2035 Make_Op_Eq (Loc,
aab883ec
ES
2036 Left_Opnd => Left_Opnd (N),
2037 Right_Opnd => Right_Opnd (N))));
996ae0b0
RK
2038
2039 Set_Entity (Right_Opnd (N), Op_Id);
2040 Analyze (N);
2041 end if;
2042 end if;
2043
2044 Operator_Check (N);
22e89283 2045 Check_Function_Writable_Actuals (N);
996ae0b0
RK
2046 end Analyze_Equality_Op;
2047
2048 ----------------------------------
2049 -- Analyze_Explicit_Dereference --
2050 ----------------------------------
2051
2052 procedure Analyze_Explicit_Dereference (N : Node_Id) is
2053 Loc : constant Source_Ptr := Sloc (N);
2054 P : constant Node_Id := Prefix (N);
2055 T : Entity_Id;
2056 I : Interp_Index;
2057 It : Interp;
2058 New_N : Node_Id;
2059
2060 function Is_Function_Type return Boolean;
4c46b835
AC
2061 -- Check whether node may be interpreted as an implicit function call
2062
2063 ----------------------
2064 -- Is_Function_Type --
2065 ----------------------
996ae0b0
RK
2066
2067 function Is_Function_Type return Boolean is
4c46b835
AC
2068 I : Interp_Index;
2069 It : Interp;
996ae0b0
RK
2070
2071 begin
2072 if not Is_Overloaded (N) then
2073 return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
2074 and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
2075
2076 else
2077 Get_First_Interp (N, I, It);
996ae0b0
RK
2078 while Present (It.Nam) loop
2079 if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
2080 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
2081 then
2082 return False;
2083 end if;
2084
2085 Get_Next_Interp (I, It);
2086 end loop;
2087
2088 return True;
2089 end if;
2090 end Is_Function_Type;
2091
98123480 2092 -- Start of processing for Analyze_Explicit_Dereference
4c46b835 2093
996ae0b0 2094 begin
11bc76df
AC
2095 -- If source node, check SPARK restriction. We guard this with the
2096 -- source node check, because ???
2097
36b8f95f 2098 if Comes_From_Source (N) then
ce5ba43a 2099 Check_SPARK_05_Restriction ("explicit dereference is not allowed", N);
36b8f95f 2100 end if;
1d801f21 2101
226a7fa4
AC
2102 -- In formal verification mode, keep track of all reads and writes
2103 -- through explicit dereferences.
2104
f5da7a97 2105 if GNATprove_Mode then
06b599fd 2106 SPARK_Specific.Generate_Dereference (N);
226a7fa4
AC
2107 end if;
2108
996ae0b0
RK
2109 Analyze (P);
2110 Set_Etype (N, Any_Type);
2111
2112 -- Test for remote access to subprogram type, and if so return
2113 -- after rewriting the original tree.
2114
2115 if Remote_AST_E_Dereference (P) then
2116 return;
2117 end if;
2118
2119 -- Normal processing for other than remote access to subprogram type
2120
2121 if not Is_Overloaded (P) then
2122 if Is_Access_Type (Etype (P)) then
2123
ff9d220e 2124 -- Set the Etype
996ae0b0
RK
2125
2126 declare
ff9d220e 2127 DT : constant Entity_Id := Designated_Type (Etype (P));
996ae0b0
RK
2128
2129 begin
0a36105d 2130 -- An explicit dereference is a legal occurrence of an
0c6826a5
AC
2131 -- incomplete type imported through a limited_with clause, if
2132 -- the full view is visible, or if we are within an instance
2133 -- body, where the enclosing body has a regular with_clause
2134 -- on the unit.
0a36105d 2135
7b56a91b
AC
2136 if From_Limited_With (DT)
2137 and then not From_Limited_With (Scope (DT))
0a36105d
JM
2138 and then
2139 (Is_Immediately_Visible (Scope (DT))
2140 or else
2141 (Is_Child_Unit (Scope (DT))
bff469f7
AC
2142 and then Is_Visible_Lib_Unit (Scope (DT)))
2143 or else In_Instance_Body)
0a36105d
JM
2144 then
2145 Set_Etype (N, Available_View (DT));
2146
2147 else
2148 Set_Etype (N, DT);
2149 end if;
996ae0b0
RK
2150 end;
2151
2152 elsif Etype (P) /= Any_Type then
2153 Error_Msg_N ("prefix of dereference must be an access type", N);
2154 return;
2155 end if;
2156
2157 else
2158 Get_First_Interp (P, I, It);
996ae0b0
RK
2159 while Present (It.Nam) loop
2160 T := It.Typ;
2161
2162 if Is_Access_Type (T) then
2163 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
2164 end if;
2165
2166 Get_Next_Interp (I, It);
2167 end loop;
2168
6e73e3ab 2169 -- Error if no interpretation of the prefix has an access type
996ae0b0
RK
2170
2171 if Etype (N) = Any_Type then
2172 Error_Msg_N
2173 ("access type required in prefix of explicit dereference", P);
2174 Set_Etype (N, Any_Type);
2175 return;
2176 end if;
2177 end if;
2178
2179 if Is_Function_Type
2180 and then Nkind (Parent (N)) /= N_Indexed_Component
2181
2182 and then (Nkind (Parent (N)) /= N_Function_Call
2183 or else N /= Name (Parent (N)))
2184
2185 and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
2186 or else N /= Name (Parent (N)))
2187
2188 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
2189 and then (Nkind (Parent (N)) /= N_Attribute_Reference
2190 or else
2191 (Attribute_Name (Parent (N)) /= Name_Address
2192 and then
2193 Attribute_Name (Parent (N)) /= Name_Access))
2194 then
2195 -- Name is a function call with no actuals, in a context that
2196 -- requires deproceduring (including as an actual in an enclosing
98123480 2197 -- function or procedure call). There are some pathological cases
996ae0b0
RK
2198 -- where the prefix might include functions that return access to
2199 -- subprograms and others that return a regular type. Disambiguation
98123480 2200 -- of those has to take place in Resolve.
996ae0b0
RK
2201
2202 New_N :=
2203 Make_Function_Call (Loc,
b55993b3
AC
2204 Name => Make_Explicit_Dereference (Loc, P),
2205 Parameter_Associations => New_List);
996ae0b0
RK
2206
2207 -- If the prefix is overloaded, remove operations that have formals,
2208 -- we know that this is a parameterless call.
2209
2210 if Is_Overloaded (P) then
2211 Get_First_Interp (P, I, It);
996ae0b0
RK
2212 while Present (It.Nam) loop
2213 T := It.Typ;
2214
2215 if No (First_Formal (Base_Type (Designated_Type (T)))) then
2216 Set_Etype (P, T);
2217 else
2218 Remove_Interp (I);
2219 end if;
2220
2221 Get_Next_Interp (I, It);
2222 end loop;
2223 end if;
2224
2225 Rewrite (N, New_N);
2226 Analyze (N);
98123480
ES
2227
2228 elsif not Is_Function_Type
2229 and then Is_Overloaded (N)
2230 then
2231 -- The prefix may include access to subprograms and other access
1cb17b78 2232 -- types. If the context selects the interpretation that is a
56a7a3ab
TQ
2233 -- function call (not a procedure call) we cannot rewrite the node
2234 -- yet, but we include the result of the call interpretation.
98123480
ES
2235
2236 Get_First_Interp (N, I, It);
2237 while Present (It.Nam) loop
2238 if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
2239 and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
1cb17b78 2240 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
98123480
ES
2241 then
2242 Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
2243 end if;
2244
2245 Get_Next_Interp (I, It);
2246 end loop;
996ae0b0
RK
2247 end if;
2248
2249 -- A value of remote access-to-class-wide must not be dereferenced
2250 -- (RM E.2.2(16)).
2251
2252 Validate_Remote_Access_To_Class_Wide_Type (N);
996ae0b0
RK
2253 end Analyze_Explicit_Dereference;
2254
2255 ------------------------
2256 -- Analyze_Expression --
2257 ------------------------
2258
2259 procedure Analyze_Expression (N : Node_Id) is
2260 begin
5f50020a
ES
2261
2262 -- If the expression is an indexed component that will be rewritten
2263 -- as a container indexing, it has already been analyzed.
2264
2265 if Nkind (N) = N_Indexed_Component
2266 and then Present (Generalized_Indexing (N))
2267 then
2268 null;
2269
2270 else
2271 Analyze (N);
2272 Check_Parameterless_Call (N);
2273 end if;
996ae0b0
RK
2274 end Analyze_Expression;
2275
955871d3
AC
2276 -------------------------------------
2277 -- Analyze_Expression_With_Actions --
2278 -------------------------------------
2279
2280 procedure Analyze_Expression_With_Actions (N : Node_Id) is
2281 A : Node_Id;
2282
2283 begin
2284 A := First (Actions (N));
752b81d9 2285 while Present (A) loop
955871d3
AC
2286 Analyze (A);
2287 Next (A);
955871d3
AC
2288 end loop;
2289
2ffcbaa5
AC
2290 Analyze_Expression (Expression (N));
2291 Set_Etype (N, Etype (Expression (N)));
955871d3
AC
2292 end Analyze_Expression_With_Actions;
2293
9b16cb57
RD
2294 ---------------------------
2295 -- Analyze_If_Expression --
2296 ---------------------------
2297
2298 procedure Analyze_If_Expression (N : Node_Id) is
2299 Condition : constant Node_Id := First (Expressions (N));
a6354842 2300 Then_Expr : Node_Id;
9b16cb57
RD
2301 Else_Expr : Node_Id;
2302
2303 begin
2304 -- Defend against error of missing expressions from previous error
2305
a6354842
AC
2306 if No (Condition) then
2307 Check_Error_Detected;
2308 return;
2309 end if;
b55993b3 2310
a6354842
AC
2311 Then_Expr := Next (Condition);
2312
9b16cb57 2313 if No (Then_Expr) then
ee2ba856 2314 Check_Error_Detected;
9b16cb57
RD
2315 return;
2316 end if;
b55993b3 2317
a6354842 2318 Else_Expr := Next (Then_Expr);
9b16cb57 2319
08988ed9 2320 if Comes_From_Source (N) then
ce5ba43a 2321 Check_SPARK_05_Restriction ("if expression is not allowed", N);
08988ed9 2322 end if;
9b16cb57 2323
9b16cb57 2324 if Comes_From_Source (N) then
c86cf714 2325 Check_Compiler_Unit ("if expression", N);
9b16cb57
RD
2326 end if;
2327
ac072cb2
AC
2328 -- Analyze and resolve the condition. We need to resolve this now so
2329 -- that it gets folded to True/False if possible, before we analyze
2330 -- the THEN/ELSE branches, because when analyzing these branches, we
2331 -- may call Is_Statically_Unevaluated, which expects the condition of
2332 -- an enclosing IF to have been analyze/resolved/evaluated.
2333
9b16cb57 2334 Analyze_Expression (Condition);
ac072cb2
AC
2335 Resolve (Condition, Any_Boolean);
2336
2337 -- Analyze THEN expression and (if present) ELSE expression. For those
2338 -- we delay resolution in the normal manner, because of overloading etc.
2339
9b16cb57
RD
2340 Analyze_Expression (Then_Expr);
2341
2342 if Present (Else_Expr) then
2343 Analyze_Expression (Else_Expr);
2344 end if;
2345
2346 -- If then expression not overloaded, then that decides the type
2347
2348 if not Is_Overloaded (Then_Expr) then
2349 Set_Etype (N, Etype (Then_Expr));
2350
2351 -- Case where then expression is overloaded
2352
2353 else
2354 declare
2355 I : Interp_Index;
2356 It : Interp;
2357
2358 begin
2359 Set_Etype (N, Any_Type);
2360
bc795e3e 2361 -- Loop through interpretations of Then_Expr
9b16cb57
RD
2362
2363 Get_First_Interp (Then_Expr, I, It);
445e5888 2364 while Present (It.Nam) loop
9b16cb57 2365
bc795e3e 2366 -- Add possible interpretation of Then_Expr if no Else_Expr, or
0c6826a5 2367 -- Else_Expr is present and has a compatible type.
9b16cb57 2368
445e5888
AC
2369 if No (Else_Expr)
2370 or else Has_Compatible_Type (Else_Expr, It.Typ)
2371 then
2372 Add_One_Interp (N, It.Typ, It.Typ);
2373 end if;
9b16cb57 2374
445e5888
AC
2375 Get_Next_Interp (I, It);
2376 end loop;
7408c4a5 2377
bc38dbb4
AC
2378 -- If no valid interpretation has been found, then the type of the
2379 -- ELSE expression does not match any interpretation of the THEN
2380 -- expression.
7408c4a5
AC
2381
2382 if Etype (N) = Any_Type then
2383 Error_Msg_N
2384 ("type incompatible with that of `THEN` expression",
2385 Else_Expr);
2386 return;
2387 end if;
9b16cb57
RD
2388 end;
2389 end if;
2390 end Analyze_If_Expression;
2391
996ae0b0
RK
2392 ------------------------------------
2393 -- Analyze_Indexed_Component_Form --
2394 ------------------------------------
2395
2396 procedure Analyze_Indexed_Component_Form (N : Node_Id) is
fbf5a39b
AC
2397 P : constant Node_Id := Prefix (N);
2398 Exprs : constant List_Id := Expressions (N);
2399 Exp : Node_Id;
2400 P_T : Entity_Id;
2401 E : Node_Id;
2402 U_N : Entity_Id;
996ae0b0
RK
2403
2404 procedure Process_Function_Call;
0c6826a5
AC
2405 -- Prefix in indexed component form is an overloadable entity, so the
2406 -- node is a function call. Reformat it as such.
996ae0b0
RK
2407
2408 procedure Process_Indexed_Component;
2409 -- Prefix in indexed component form is actually an indexed component.
2410 -- This routine processes it, knowing that the prefix is already
2411 -- resolved.
2412
2413 procedure Process_Indexed_Component_Or_Slice;
2414 -- An indexed component with a single index may designate a slice if
2415 -- the index is a subtype mark. This routine disambiguates these two
2416 -- cases by resolving the prefix to see if it is a subtype mark.
2417
2418 procedure Process_Overloaded_Indexed_Component;
2419 -- If the prefix of an indexed component is overloaded, the proper
2420 -- interpretation is selected by the index types and the context.
2421
2422 ---------------------------
2423 -- Process_Function_Call --
2424 ---------------------------
2425
2426 procedure Process_Function_Call is
1725676d 2427 Loc : constant Source_Ptr := Sloc (N);
f5afb270
AC
2428 Actual : Node_Id;
2429
996ae0b0
RK
2430 begin
2431 Change_Node (N, N_Function_Call);
2432 Set_Name (N, P);
2433 Set_Parameter_Associations (N, Exprs);
996ae0b0 2434
401093c1 2435 -- Analyze actuals prior to analyzing the call itself
0a36105d 2436
4c46b835 2437 Actual := First (Parameter_Associations (N));
996ae0b0
RK
2438 while Present (Actual) loop
2439 Analyze (Actual);
2440 Check_Parameterless_Call (Actual);
0a36105d
JM
2441
2442 -- Move to next actual. Note that we use Next, not Next_Actual
2443 -- here. The reason for this is a bit subtle. If a function call
0c6826a5
AC
2444 -- includes named associations, the parser recognizes the node
2445 -- as a call, and it is analyzed as such. If all associations are
0a36105d
JM
2446 -- positional, the parser builds an indexed_component node, and
2447 -- it is only after analysis of the prefix that the construct
2448 -- is recognized as a call, in which case Process_Function_Call
2449 -- rewrites the node and analyzes the actuals. If the list of
2450 -- actuals is malformed, the parser may leave the node as an
2451 -- indexed component (despite the presence of named associations).
2452 -- The iterator Next_Actual is equivalent to Next if the list is
2453 -- positional, but follows the normalized chain of actuals when
2454 -- named associations are present. In this case normalization has
2455 -- not taken place, and actuals remain unanalyzed, which leads to
2456 -- subsequent crashes or loops if there is an attempt to continue
2457 -- analysis of the program.
2458
1725676d
AC
2459 -- IF there is a single actual and it is a type name, the node
2460 -- can only be interpreted as a slice of a parameterless call.
2461 -- Rebuild the node as such and analyze.
2462
2463 if No (Next (Actual))
2464 and then Is_Entity_Name (Actual)
2465 and then Is_Type (Entity (Actual))
2466 and then Is_Discrete_Type (Entity (Actual))
2467 then
2468 Replace (N,
adc876a8
AC
2469 Make_Slice (Loc,
2470 Prefix => P,
2471 Discrete_Range =>
2472 New_Occurrence_Of (Entity (Actual), Loc)));
1725676d
AC
2473 Analyze (N);
2474 return;
2475
2476 else
2477 Next (Actual);
2478 end if;
996ae0b0
RK
2479 end loop;
2480
2481 Analyze_Call (N);
2482 end Process_Function_Call;
2483
2484 -------------------------------
2485 -- Process_Indexed_Component --
2486 -------------------------------
2487
2488 procedure Process_Indexed_Component is
fe39cf20
BD
2489 Exp : Node_Id;
2490 Array_Type : Entity_Id;
2491 Index : Node_Id;
2492 Pent : Entity_Id := Empty;
996ae0b0
RK
2493
2494 begin
2495 Exp := First (Exprs);
2496
2497 if Is_Overloaded (P) then
2498 Process_Overloaded_Indexed_Component;
2499
2500 else
2501 Array_Type := Etype (P);
2502
6e73e3ab
AC
2503 if Is_Entity_Name (P) then
2504 Pent := Entity (P);
2505 elsif Nkind (P) = N_Selected_Component
2506 and then Is_Entity_Name (Selector_Name (P))
2507 then
2508 Pent := Entity (Selector_Name (P));
2509 end if;
2510
2511 -- Prefix must be appropriate for an array type, taking into
2512 -- account a possible implicit dereference.
996ae0b0
RK
2513
2514 if Is_Access_Type (Array_Type) then
324ac540
AC
2515 Error_Msg_NW
2516 (Warn_On_Dereference, "?d?implicit dereference", N);
d469eabe 2517 Array_Type := Process_Implicit_Dereference_Prefix (Pent, P);
996ae0b0
RK
2518 end if;
2519
2520 if Is_Array_Type (Array_Type) then
bfaf8a97
AC
2521
2522 -- In order to correctly access First_Index component later,
2523 -- replace string literal subtype by its parent type.
2524
2525 if Ekind (Array_Type) = E_String_Literal_Subtype then
2526 Array_Type := Etype (Array_Type);
2527 end if;
996ae0b0 2528
6e73e3ab 2529 elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
996ae0b0
RK
2530 Analyze (Exp);
2531 Set_Etype (N, Any_Type);
2532
dafe11cd 2533 if not Has_Compatible_Type (Exp, Entry_Index_Type (Pent)) then
996ae0b0
RK
2534 Error_Msg_N ("invalid index type in entry name", N);
2535
2536 elsif Present (Next (Exp)) then
2537 Error_Msg_N ("too many subscripts in entry reference", N);
2538
2539 else
2540 Set_Etype (N, Etype (P));
2541 end if;
2542
2543 return;
2544
2545 elsif Is_Record_Type (Array_Type)
2546 and then Remote_AST_I_Dereference (P)
2547 then
2548 return;
2549
50878404 2550 elsif Try_Container_Indexing (N, P, Exprs) then
d50f4827
AC
2551 return;
2552
996ae0b0
RK
2553 elsif Array_Type = Any_Type then
2554 Set_Etype (N, Any_Type);
6465b6a7
AC
2555
2556 -- In most cases the analysis of the prefix will have emitted
2557 -- an error already, but if the prefix may be interpreted as a
2558 -- call in prefixed notation, the report is left to the caller.
2559 -- To prevent cascaded errors, report only if no previous ones.
2560
2561 if Serious_Errors_Detected = 0 then
2562 Error_Msg_N ("invalid prefix in indexed component", P);
2563
2564 if Nkind (P) = N_Expanded_Name then
2565 Error_Msg_NE ("\& is not visible", P, Selector_Name (P));
2566 end if;
2567 end if;
2568
996ae0b0
RK
2569 return;
2570
2571 -- Here we definitely have a bad indexing
2572
2573 else
2574 if Nkind (Parent (N)) = N_Requeue_Statement
6e73e3ab 2575 and then Present (Pent) and then Ekind (Pent) = E_Entry
996ae0b0
RK
2576 then
2577 Error_Msg_N
2578 ("REQUEUE does not permit parameters", First (Exprs));
2579
2580 elsif Is_Entity_Name (P)
2581 and then Etype (P) = Standard_Void_Type
2582 then
0c6826a5 2583 Error_Msg_NE ("incorrect use of &", P, Entity (P));
996ae0b0
RK
2584
2585 else
2586 Error_Msg_N ("array type required in indexed component", P);
2587 end if;
2588
2589 Set_Etype (N, Any_Type);
2590 return;
2591 end if;
2592
2593 Index := First_Index (Array_Type);
996ae0b0
RK
2594 while Present (Index) and then Present (Exp) loop
2595 if not Has_Compatible_Type (Exp, Etype (Index)) then
2596 Wrong_Type (Exp, Etype (Index));
2597 Set_Etype (N, Any_Type);
2598 return;
2599 end if;
2600
2601 Next_Index (Index);
2602 Next (Exp);
2603 end loop;
2604
2605 Set_Etype (N, Component_Type (Array_Type));
44a10091 2606 Check_Implicit_Dereference (N, Etype (N));
996ae0b0
RK
2607
2608 if Present (Index) then
2609 Error_Msg_N
2610 ("too few subscripts in array reference", First (Exprs));
2611
2612 elsif Present (Exp) then
2613 Error_Msg_N ("too many subscripts in array reference", Exp);
2614 end if;
2615 end if;
996ae0b0
RK
2616 end Process_Indexed_Component;
2617
2618 ----------------------------------------
2619 -- Process_Indexed_Component_Or_Slice --
2620 ----------------------------------------
2621
2622 procedure Process_Indexed_Component_Or_Slice is
2623 begin
2624 Exp := First (Exprs);
996ae0b0
RK
2625 while Present (Exp) loop
2626 Analyze_Expression (Exp);
2627 Next (Exp);
2628 end loop;
2629
2630 Exp := First (Exprs);
2631
0c6826a5
AC
2632 -- If one index is present, and it is a subtype name, then the node
2633 -- denotes a slice (note that the case of an explicit range for a
2634 -- slice was already built as an N_Slice node in the first place,
2635 -- so that case is not handled here).
996ae0b0
RK
2636
2637 -- We use a replace rather than a rewrite here because this is one
2638 -- of the cases in which the tree built by the parser is plain wrong.
2639
2640 if No (Next (Exp))
2641 and then Is_Entity_Name (Exp)
2642 and then Is_Type (Entity (Exp))
2643 then
2644 Replace (N,
2645 Make_Slice (Sloc (N),
2646 Prefix => P,
2647 Discrete_Range => New_Copy (Exp)));
2648 Analyze (N);
2649
2650 -- Otherwise (more than one index present, or single index is not
2651 -- a subtype name), then we have the indexed component case.
2652
2653 else
2654 Process_Indexed_Component;
2655 end if;
2656 end Process_Indexed_Component_Or_Slice;
2657
2658 ------------------------------------------
2659 -- Process_Overloaded_Indexed_Component --
2660 ------------------------------------------
2661
2662 procedure Process_Overloaded_Indexed_Component is
2663 Exp : Node_Id;
2664 I : Interp_Index;
2665 It : Interp;
2666 Typ : Entity_Id;
2667 Index : Node_Id;
2668 Found : Boolean;
2669
2670 begin
2671 Set_Etype (N, Any_Type);
996ae0b0 2672
4c46b835 2673 Get_First_Interp (P, I, It);
996ae0b0
RK
2674 while Present (It.Nam) loop
2675 Typ := It.Typ;
2676
2677 if Is_Access_Type (Typ) then
2678 Typ := Designated_Type (Typ);
324ac540
AC
2679 Error_Msg_NW
2680 (Warn_On_Dereference, "?d?implicit dereference", N);
996ae0b0
RK
2681 end if;
2682
2683 if Is_Array_Type (Typ) then
2684
2685 -- Got a candidate: verify that index types are compatible
2686
2687 Index := First_Index (Typ);
2688 Found := True;
996ae0b0 2689 Exp := First (Exprs);
996ae0b0
RK
2690 while Present (Index) and then Present (Exp) loop
2691 if Has_Compatible_Type (Exp, Etype (Index)) then
2692 null;
2693 else
2694 Found := False;
2695 Remove_Interp (I);
2696 exit;
2697 end if;
2698
2699 Next_Index (Index);
2700 Next (Exp);
2701 end loop;
2702
2703 if Found and then No (Index) and then No (Exp) then
44a10091
AC
2704 declare
2705 CT : constant Entity_Id :=
2706 Base_Type (Component_Type (Typ));
2707 begin
2708 Add_One_Interp (N, CT, CT);
2709 Check_Implicit_Dereference (N, CT);
2710 end;
996ae0b0 2711 end if;
57a8057a 2712
50878404 2713 elsif Try_Container_Indexing (N, P, Exprs) then
57a8057a
AC
2714 return;
2715
996ae0b0
RK
2716 end if;
2717
2718 Get_Next_Interp (I, It);
2719 end loop;
2720
2721 if Etype (N) = Any_Type then
ad6b5b00 2722 Error_Msg_N ("no legal interpretation for indexed component", N);
996ae0b0
RK
2723 Set_Is_Overloaded (N, False);
2724 end if;
2725
2726 End_Interp_List;
2727 end Process_Overloaded_Indexed_Component;
2728
4c46b835 2729 -- Start of processing for Analyze_Indexed_Component_Form
996ae0b0
RK
2730
2731 begin
2732 -- Get name of array, function or type
2733
2734 Analyze (P);
d469eabe 2735
24778dbb
AC
2736 -- If P is an explicit dereference whose prefix is of a remote access-
2737 -- to-subprogram type, then N has already been rewritten as a subprogram
2738 -- call and analyzed.
2739
d3b00ce3 2740 if Nkind (N) in N_Subprogram_Call then
24778dbb 2741 return;
d469eabe 2742
24778dbb
AC
2743 -- When the prefix is attribute 'Loop_Entry and the sole expression of
2744 -- the indexed component denotes a loop name, the indexed form is turned
2745 -- into an attribute reference.
fbf5a39b 2746
24778dbb
AC
2747 elsif Nkind (N) = N_Attribute_Reference
2748 and then Attribute_Name (N) = Name_Loop_Entry
2749 then
fbf5a39b
AC
2750 return;
2751 end if;
2752
2753 pragma Assert (Nkind (N) = N_Indexed_Component);
2754
996ae0b0
RK
2755 P_T := Base_Type (Etype (P));
2756
878f708a 2757 if Is_Entity_Name (P) and then Present (Entity (P)) then
996ae0b0
RK
2758 U_N := Entity (P);
2759
aab883ec 2760 if Is_Type (U_N) then
996ae0b0 2761
4c46b835 2762 -- Reformat node as a type conversion
996ae0b0
RK
2763
2764 E := Remove_Head (Exprs);
2765
2766 if Present (First (Exprs)) then
2767 Error_Msg_N
2768 ("argument of type conversion must be single expression", N);
2769 end if;
2770
2771 Change_Node (N, N_Type_Conversion);
2772 Set_Subtype_Mark (N, P);
2773 Set_Etype (N, U_N);
2774 Set_Expression (N, E);
2775
2776 -- After changing the node, call for the specific Analysis
2777 -- routine directly, to avoid a double call to the expander.
2778
2779 Analyze_Type_Conversion (N);
2780 return;
2781 end if;
2782
2783 if Is_Overloadable (U_N) then
2784 Process_Function_Call;
2785
2786 elsif Ekind (Etype (P)) = E_Subprogram_Type
2787 or else (Is_Access_Type (Etype (P))
2788 and then
bce79204
AC
2789 Ekind (Designated_Type (Etype (P))) =
2790 E_Subprogram_Type)
996ae0b0
RK
2791 then
2792 -- Call to access_to-subprogram with possible implicit dereference
2793
2794 Process_Function_Call;
2795
fbf5a39b
AC
2796 elsif Is_Generic_Subprogram (U_N) then
2797
4c46b835 2798 -- A common beginner's (or C++ templates fan) error
996ae0b0
RK
2799
2800 Error_Msg_N ("generic subprogram cannot be called", N);
2801 Set_Etype (N, Any_Type);
2802 return;
2803
2804 else
2805 Process_Indexed_Component_Or_Slice;
2806 end if;
2807
2808 -- If not an entity name, prefix is an expression that may denote
2809 -- an array or an access-to-subprogram.
2810
2811 else
fbf5a39b 2812 if Ekind (P_T) = E_Subprogram_Type
996ae0b0
RK
2813 or else (Is_Access_Type (P_T)
2814 and then
bce79204 2815 Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
996ae0b0
RK
2816 then
2817 Process_Function_Call;
2818
2819 elsif Nkind (P) = N_Selected_Component
3d918396 2820 and then Present (Entity (Selector_Name (P)))
ffe9aba8 2821 and then Is_Overloadable (Entity (Selector_Name (P)))
996ae0b0
RK
2822 then
2823 Process_Function_Call;
996ae0b0
RK
2824 else
2825 -- Indexed component, slice, or a call to a member of a family
2826 -- entry, which will be converted to an entry call later.
fbf5a39b 2827
996ae0b0
RK
2828 Process_Indexed_Component_Or_Slice;
2829 end if;
2830 end if;
5f49133f
AC
2831
2832 Analyze_Dimension (N);
996ae0b0
RK
2833 end Analyze_Indexed_Component_Form;
2834
2835 ------------------------
2836 -- Analyze_Logical_Op --
2837 ------------------------
2838
2839 procedure Analyze_Logical_Op (N : Node_Id) is
2840 L : constant Node_Id := Left_Opnd (N);
2841 R : constant Node_Id := Right_Opnd (N);
2842 Op_Id : Entity_Id := Entity (N);
2843
2844 begin
2845 Set_Etype (N, Any_Type);
2846 Candidate_Type := Empty;
2847
2848 Analyze_Expression (L);
2849 Analyze_Expression (R);
2850
2851 if Present (Op_Id) then
2852
2853 if Ekind (Op_Id) = E_Operator then
2854 Find_Boolean_Types (L, R, Op_Id, N);
2855 else
2856 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2857 end if;
2858
2859 else
2860 Op_Id := Get_Name_Entity_Id (Chars (N));
996ae0b0
RK
2861 while Present (Op_Id) loop
2862 if Ekind (Op_Id) = E_Operator then
2863 Find_Boolean_Types (L, R, Op_Id, N);
2864 else
2865 Analyze_User_Defined_Binary_Op (N, Op_Id);
2866 end if;
2867
2868 Op_Id := Homonym (Op_Id);
2869 end loop;
2870 end if;
2871
2872 Operator_Check (N);
22e89283 2873 Check_Function_Writable_Actuals (N);
996ae0b0
RK
2874 end Analyze_Logical_Op;
2875
2876 ---------------------------
2877 -- Analyze_Membership_Op --
2878 ---------------------------
2879
2880 procedure Analyze_Membership_Op (N : Node_Id) is
66150d01 2881 Loc : constant Source_Ptr := Sloc (N);
f2acf80c
AC
2882 L : constant Node_Id := Left_Opnd (N);
2883 R : constant Node_Id := Right_Opnd (N);
996ae0b0
RK
2884
2885 Index : Interp_Index;
2886 It : Interp;
2887 Found : Boolean := False;
2888 I_F : Interp_Index;
2889 T_F : Entity_Id;
2890
2891 procedure Try_One_Interp (T1 : Entity_Id);
2892 -- Routine to try one proposed interpretation. Note that the context
2893 -- of the operation plays no role in resolving the arguments, so that
2894 -- if there is more than one interpretation of the operands that is
2895 -- compatible with a membership test, the operation is ambiguous.
2896
4c46b835
AC
2897 --------------------
2898 -- Try_One_Interp --
2899 --------------------
2900
996ae0b0
RK
2901 procedure Try_One_Interp (T1 : Entity_Id) is
2902 begin
2903 if Has_Compatible_Type (R, T1) then
2904 if Found
2905 and then Base_Type (T1) /= Base_Type (T_F)
2906 then
2907 It := Disambiguate (L, I_F, Index, Any_Type);
2908
2909 if It = No_Interp then
2910 Ambiguous_Operands (N);
2911 Set_Etype (L, Any_Type);
2912 return;
2913
2914 else
2915 T_F := It.Typ;
2916 end if;
2917
2918 else
2919 Found := True;
2920 T_F := T1;
2921 I_F := Index;
2922 end if;
2923
2924 Set_Etype (L, T_F);
2925 end if;
996ae0b0
RK
2926 end Try_One_Interp;
2927
197e4514
AC
2928 procedure Analyze_Set_Membership;
2929 -- If a set of alternatives is present, analyze each and find the
2930 -- common type to which they must all resolve.
2931
2932 ----------------------------
2933 -- Analyze_Set_Membership --
2934 ----------------------------
2935
2936 procedure Analyze_Set_Membership is
2937 Alt : Node_Id;
2938 Index : Interp_Index;
2939 It : Interp;
197e4514
AC
2940 Candidate_Interps : Node_Id;
2941 Common_Type : Entity_Id := Empty;
2942
2943 begin
e917e3b8 2944 if Comes_From_Source (N) then
c86cf714 2945 Check_Compiler_Unit ("set membership", N);
e917e3b8
AC
2946 end if;
2947
197e4514
AC
2948 Analyze (L);
2949 Candidate_Interps := L;
2950
2951 if not Is_Overloaded (L) then
2952 Common_Type := Etype (L);
2953
2954 Alt := First (Alternatives (N));
2955 while Present (Alt) loop
2956 Analyze (Alt);
2957
2958 if not Has_Compatible_Type (Alt, Common_Type) then
2959 Wrong_Type (Alt, Common_Type);
2960 end if;
2961
2962 Next (Alt);
2963 end loop;
2964
2965 else
2966 Alt := First (Alternatives (N));
2967 while Present (Alt) loop
2968 Analyze (Alt);
2969 if not Is_Overloaded (Alt) then
2970 Common_Type := Etype (Alt);
2971
2972 else
2973 Get_First_Interp (Alt, Index, It);
2974 while Present (It.Typ) loop
442c0581
RD
2975 if not
2976 Has_Compatible_Type (Candidate_Interps, It.Typ)
197e4514
AC
2977 then
2978 Remove_Interp (Index);
2979 end if;
442c0581 2980
197e4514
AC
2981 Get_Next_Interp (Index, It);
2982 end loop;
2983
2984 Get_First_Interp (Alt, Index, It);
442c0581 2985
197e4514
AC
2986 if No (It.Typ) then
2987 Error_Msg_N ("alternative has no legal type", Alt);
2988 return;
2989 end if;
2990
442c0581
RD
2991 -- If alternative is not overloaded, we have a unique type
2992 -- for all of them.
197e4514
AC
2993
2994 Set_Etype (Alt, It.Typ);
6376a3c6 2995
c23c86bb
AC
2996 -- If the alternative is an enumeration literal, use the one
2997 -- for this interpretation.
6376a3c6
AC
2998
2999 if Is_Entity_Name (Alt) then
3000 Set_Entity (Alt, It.Nam);
3001 end if;
3002
197e4514
AC
3003 Get_Next_Interp (Index, It);
3004
3005 if No (It.Typ) then
3006 Set_Is_Overloaded (Alt, False);
3007 Common_Type := Etype (Alt);
3008 end if;
3009
3010 Candidate_Interps := Alt;
3011 end if;
3012
3013 Next (Alt);
3014 end loop;
3015 end if;
3016
3017 Set_Etype (N, Standard_Boolean);
3018
3019 if Present (Common_Type) then
3020 Set_Etype (L, Common_Type);
cd1a470a
AC
3021
3022 -- The left operand may still be overloaded, to be resolved using
3023 -- the Common_Type.
197e4514
AC
3024
3025 else
3026 Error_Msg_N ("cannot resolve membership operation", N);
3027 end if;
3028 end Analyze_Set_Membership;
3029
996ae0b0
RK
3030 -- Start of processing for Analyze_Membership_Op
3031
3032 begin
3033 Analyze_Expression (L);
3034
0d8b6803 3035 if No (R) then
9f46106a
JM
3036 pragma Assert (Ada_Version >= Ada_2012);
3037 Analyze_Set_Membership;
3038 Check_Function_Writable_Actuals (N);
197e4514
AC
3039 return;
3040 end if;
3041
996ae0b0
RK
3042 if Nkind (R) = N_Range
3043 or else (Nkind (R) = N_Attribute_Reference
3044 and then Attribute_Name (R) = Name_Range)
3045 then
3046 Analyze (R);
3047
3048 if not Is_Overloaded (L) then
3049 Try_One_Interp (Etype (L));
3050
3051 else
3052 Get_First_Interp (L, Index, It);
996ae0b0
RK
3053 while Present (It.Typ) loop
3054 Try_One_Interp (It.Typ);
3055 Get_Next_Interp (Index, It);
3056 end loop;
3057 end if;
3058
f6b5dc8e 3059 -- If not a range, it can be a subtype mark, or else it is a degenerate
b0186f71
AC
3060 -- membership test with a singleton value, i.e. a test for equality,
3061 -- if the types are compatible.
996ae0b0
RK
3062
3063 else
66150d01 3064 Analyze (R);
7483c888 3065
66150d01
AC
3066 if Is_Entity_Name (R)
3067 and then Is_Type (Entity (R))
3068 then
3069 Find_Type (R);
996ae0b0 3070 Check_Fully_Declared (Entity (R), R);
66150d01 3071
b0186f71
AC
3072 elsif Ada_Version >= Ada_2012
3073 and then Has_Compatible_Type (R, Etype (L))
3074 then
66150d01
AC
3075 if Nkind (N) = N_In then
3076 Rewrite (N,
3077 Make_Op_Eq (Loc,
3078 Left_Opnd => L,
3079 Right_Opnd => R));
3080 else
3081 Rewrite (N,
3082 Make_Op_Ne (Loc,
3083 Left_Opnd => L,
3084 Right_Opnd => R));
3085 end if;
3086
3087 Analyze (N);
3088 return;
3089
3090 else
b0186f71
AC
3091 -- In all versions of the language, if we reach this point there
3092 -- is a previous error that will be diagnosed below.
66150d01
AC
3093
3094 Find_Type (R);
996ae0b0
RK
3095 end if;
3096 end if;
3097
3098 -- Compatibility between expression and subtype mark or range is
3099 -- checked during resolution. The result of the operation is Boolean
3100 -- in any case.
3101
3102 Set_Etype (N, Standard_Boolean);
fe45e59e
ES
3103
3104 if Comes_From_Source (N)
197e4514 3105 and then Present (Right_Opnd (N))
fe45e59e
ES
3106 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
3107 then
3108 Error_Msg_N ("membership test not applicable to cpp-class types", N);
3109 end if;
288cbbbd 3110
22e89283 3111 Check_Function_Writable_Actuals (N);
996ae0b0
RK
3112 end Analyze_Membership_Op;
3113
b727a82b
AC
3114 -----------------
3115 -- Analyze_Mod --
3116 -----------------
3117
3118 procedure Analyze_Mod (N : Node_Id) is
3119 begin
3120 -- A special warning check, if we have an expression of the form:
3121 -- expr mod 2 * literal
3122 -- where literal is 64 or less, then probably what was meant was
3123 -- expr mod 2 ** literal
3124 -- so issue an appropriate warning.
3125
3126 if Warn_On_Suspicious_Modulus_Value
3127 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
3128 and then Intval (Right_Opnd (N)) = Uint_2
3129 and then Nkind (Parent (N)) = N_Op_Multiply
3130 and then Nkind (Right_Opnd (Parent (N))) = N_Integer_Literal
3131 and then Intval (Right_Opnd (Parent (N))) <= Uint_64
3132 then
3133 Error_Msg_N
324ac540 3134 ("suspicious MOD value, was '*'* intended'??M?", Parent (N));
b727a82b
AC
3135 end if;
3136
3137 -- Remaining processing is same as for other arithmetic operators
3138
3139 Analyze_Arithmetic_Op (N);
3140 end Analyze_Mod;
3141
996ae0b0
RK
3142 ----------------------
3143 -- Analyze_Negation --
3144 ----------------------
3145
3146 procedure Analyze_Negation (N : Node_Id) is
3147 R : constant Node_Id := Right_Opnd (N);
3148 Op_Id : Entity_Id := Entity (N);
3149
3150 begin
3151 Set_Etype (N, Any_Type);
3152 Candidate_Type := Empty;
3153
3154 Analyze_Expression (R);
3155
3156 if Present (Op_Id) then
3157 if Ekind (Op_Id) = E_Operator then
3158 Find_Negation_Types (R, Op_Id, N);
3159 else
3160 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3161 end if;
3162
3163 else
3164 Op_Id := Get_Name_Entity_Id (Chars (N));
996ae0b0
RK
3165 while Present (Op_Id) loop
3166 if Ekind (Op_Id) = E_Operator then
3167 Find_Negation_Types (R, Op_Id, N);
3168 else
3169 Analyze_User_Defined_Unary_Op (N, Op_Id);
3170 end if;
3171
3172 Op_Id := Homonym (Op_Id);
3173 end loop;
3174 end if;
3175
3176 Operator_Check (N);
3177 end Analyze_Negation;
3178
15ce9ca2
AC
3179 ------------------
3180 -- Analyze_Null --
3181 ------------------
996ae0b0
RK
3182
3183 procedure Analyze_Null (N : Node_Id) is
3184 begin
ce5ba43a 3185 Check_SPARK_05_Restriction ("null is not allowed", N);
1d801f21 3186
996ae0b0
RK
3187 Set_Etype (N, Any_Access);
3188 end Analyze_Null;
3189
3190 ----------------------
3191 -- Analyze_One_Call --
3192 ----------------------
3193
3194 procedure Analyze_One_Call
ec6078e3
ES
3195 (N : Node_Id;
3196 Nam : Entity_Id;
3197 Report : Boolean;
3198 Success : out Boolean;
3199 Skip_First : Boolean := False)
996ae0b0 3200 is
d469eabe
HK
3201 Actuals : constant List_Id := Parameter_Associations (N);
3202 Prev_T : constant Entity_Id := Etype (N);
3203
12390626
ES
3204 -- Recognize cases of prefixed calls that have been rewritten in
3205 -- various ways. The simplest case is a rewritten selected component,
3206 -- but it can also be an already-examined indexed component, or a
3207 -- prefix that is itself a rewritten prefixed call that is in turn
3208 -- an indexed call (the syntactic ambiguity involving the indexing of
3209 -- a function with defaulted parameters that returns an array).
3210 -- A flag Maybe_Indexed_Call might be useful here ???
3211
aab883ec
ES
3212 Must_Skip : constant Boolean := Skip_First
3213 or else Nkind (Original_Node (N)) = N_Selected_Component
3214 or else
3215 (Nkind (Original_Node (N)) = N_Indexed_Component
e5fc0179
HK
3216 and then Nkind (Prefix (Original_Node (N))) =
3217 N_Selected_Component)
12390626
ES
3218 or else
3219 (Nkind (Parent (N)) = N_Function_Call
e5fc0179
HK
3220 and then Is_Array_Type (Etype (Name (N)))
3221 and then Etype (Original_Node (N)) =
3222 Component_Type (Etype (Name (N)))
3223 and then Nkind (Original_Node (Parent (N))) =
3224 N_Selected_Component);
12390626 3225
aab883ec
ES
3226 -- The first formal must be omitted from the match when trying to find
3227 -- a primitive operation that is a possible interpretation, and also
3228 -- after the call has been rewritten, because the corresponding actual
3229 -- is already known to be compatible, and because this may be an
3230 -- indexing of a call with default parameters.
3231
2f0a921f 3232 First_Form : Entity_Id;
53cf4600
ES
3233 Formal : Entity_Id;
3234 Actual : Node_Id;
3235 Is_Indexed : Boolean := False;
3236 Is_Indirect : Boolean := False;
3237 Subp_Type : constant Entity_Id := Etype (Nam);
3238 Norm_OK : Boolean;
996ae0b0 3239
1d2d8a8f
AC
3240 function Compatible_Types_In_Predicate
3241 (T1 : Entity_Id;
3242 T2 : Entity_Id) return Boolean;
3243 -- For an Ada 2012 predicate or invariant, a call may mention an
3244 -- incomplete type, while resolution of the corresponding predicate
3245 -- function may see the full view, as a consequence of the delayed
3246 -- resolution of the corresponding expressions. This may occur in
3247 -- the body of a predicate function, or in a call to such. Anomalies
3248 -- involving private and full views can also happen. In each case,
3249 -- rewrite node or add conversions to remove spurious type errors.
3250
3251 procedure Indicate_Name_And_Type;
3252 -- If candidate interpretation matches, indicate name and type of result
3253 -- on call node.
3254
157a9bf5
ES
3255 function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
3256 -- There may be a user-defined operator that hides the current
3257 -- interpretation. We must check for this independently of the
3258 -- analysis of the call with the user-defined operation, because
3259 -- the parameter names may be wrong and yet the hiding takes place.
3260 -- This fixes a problem with ACATS test B34014O.
3261 --
3262 -- When the type Address is a visible integer type, and the DEC
3263 -- system extension is visible, the predefined operator may be
3264 -- hidden as well, by one of the address operations in auxdec.
3265 -- Finally, The abstract operations on address do not hide the
3266 -- predefined operator (this is the purpose of making them abstract).
3267
1d2d8a8f
AC
3268 -----------------------------------
3269 -- Compatible_Types_In_Predicate --
3270 -----------------------------------
3271
3272 function Compatible_Types_In_Predicate
3273 (T1 : Entity_Id;
3274 T2 : Entity_Id) return Boolean
3275 is
3276 function Common_Type (T : Entity_Id) return Entity_Id;
3277 -- Find non-private full view if any, without going to ancestor type
3278 -- (as opposed to Underlying_Type).
3279
3280 -----------------
3281 -- Common_Type --
3282 -----------------
3283
3284 function Common_Type (T : Entity_Id) return Entity_Id is
3285 begin
3286 if Is_Private_Type (T) and then Present (Full_View (T)) then
3287 return Base_Type (Full_View (T));
3288 else
3289 return Base_Type (T);
3290 end if;
3291 end Common_Type;
3292
3293 -- Start of processing for Compatible_Types_In_Predicate
3294
3295 begin
3296 if (Ekind (Current_Scope) = E_Function
3297 and then Is_Predicate_Function (Current_Scope))
3298 or else
3299 (Ekind (Nam) = E_Function
3300 and then Is_Predicate_Function (Nam))
3301 then
3302 if Is_Incomplete_Type (T1)
3303 and then Present (Full_View (T1))
3304 and then Full_View (T1) = T2
3305 then
3306 Set_Etype (Formal, Etype (Actual));
3307 return True;
3308
3309 elsif Common_Type (T1) = Common_Type (T2) then
3310 Rewrite (Actual, Unchecked_Convert_To (Etype (Formal), Actual));
3311 return True;
3312
3313 else
3314 return False;
3315 end if;
3316
3317 else
3318 return False;
3319 end if;
3320 end Compatible_Types_In_Predicate;
996ae0b0 3321
fbf5a39b
AC
3322 ----------------------------
3323 -- Indicate_Name_And_Type --
3324 ----------------------------
996ae0b0 3325
fbf5a39b 3326 procedure Indicate_Name_And_Type is
996ae0b0
RK
3327 begin
3328 Add_One_Interp (N, Nam, Etype (Nam));
44a10091 3329 Check_Implicit_Dereference (N, Etype (Nam));
996ae0b0
RK
3330 Success := True;
3331
3332 -- If the prefix of the call is a name, indicate the entity
3333 -- being called. If it is not a name, it is an expression that
3334 -- denotes an access to subprogram or else an entry or family. In
3335 -- the latter case, the name is a selected component, and the entity
3336 -- being called is noted on the selector.
3337
3338 if not Is_Type (Nam) then
a3f2babd 3339 if Is_Entity_Name (Name (N)) then
996ae0b0 3340 Set_Entity (Name (N), Nam);
d9307840 3341 Set_Etype (Name (N), Etype (Nam));
996ae0b0
RK
3342
3343 elsif Nkind (Name (N)) = N_Selected_Component then
3344 Set_Entity (Selector_Name (Name (N)), Nam);
3345 end if;
3346 end if;
3347
3348 if Debug_Flag_E and not Report then
3349 Write_Str (" Overloaded call ");
3350 Write_Int (Int (N));
3351 Write_Str (" compatible with ");
3352 Write_Int (Int (Nam));
3353 Write_Eol;
3354 end if;
fbf5a39b 3355 end Indicate_Name_And_Type;
996ae0b0 3356
157a9bf5
ES
3357 ------------------------
3358 -- Operator_Hidden_By --
3359 ------------------------
3360
3361 function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
3362 Act1 : constant Node_Id := First_Actual (N);
3363 Act2 : constant Node_Id := Next_Actual (Act1);
3364 Form1 : constant Entity_Id := First_Formal (Fun);
3365 Form2 : constant Entity_Id := Next_Formal (Form1);
3366
3367 begin
e4deba8e 3368 if Ekind (Fun) /= E_Function or else Is_Abstract_Subprogram (Fun) then
157a9bf5
ES
3369 return False;
3370
3371 elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
3372 return False;
3373
3374 elsif Present (Form2) then
e4deba8e
RD
3375 if No (Act2)
3376 or else not Has_Compatible_Type (Act2, Etype (Form2))
157a9bf5
ES
3377 then
3378 return False;
3379 end if;
3380
3381 elsif Present (Act2) then
3382 return False;
3383 end if;
3384
3385 -- Now we know that the arity of the operator matches the function,
3386 -- and the function call is a valid interpretation. The function
3387 -- hides the operator if it has the right signature, or if one of
3388 -- its operands is a non-abstract operation on Address when this is
3389 -- a visible integer type.
3390
3391 return Hides_Op (Fun, Nam)
d9d25d04 3392 or else Is_Descendant_Of_Address (Etype (Form1))
157a9bf5
ES
3393 or else
3394 (Present (Form2)
d9d25d04 3395 and then Is_Descendant_Of_Address (Etype (Form2)));
157a9bf5
ES
3396 end Operator_Hidden_By;
3397
996ae0b0
RK
3398 -- Start of processing for Analyze_One_Call
3399
3400 begin
3401 Success := False;
3402
157a9bf5
ES
3403 -- If the subprogram has no formals or if all the formals have defaults,
3404 -- and the return type is an array type, the node may denote an indexing
3405 -- of the result of a parameterless call. In Ada 2005, the subprogram
3406 -- may have one non-defaulted formal, and the call may have been written
3407 -- in prefix notation, so that the rebuilt parameter list has more than
3408 -- one actual.
996ae0b0 3409
53cf4600
ES
3410 if not Is_Overloadable (Nam)
3411 and then Ekind (Nam) /= E_Subprogram_Type
3412 and then Ekind (Nam) /= E_Entry_Family
3413 then
3414 return;
3415 end if;
3416
80e59506 3417 -- An indexing requires at least one actual. The name of the call cannot
4bb9c7b9
AC
3418 -- be an implicit indirect call, so it cannot be a generated explicit
3419 -- dereference.
e1f3cb58
AC
3420
3421 if not Is_Empty_List (Actuals)
aab883ec
ES
3422 and then
3423 (Needs_No_Actuals (Nam)
3424 or else
3425 (Needs_One_Actual (Nam)
e4deba8e 3426 and then Present (Next_Actual (First (Actuals)))))
996ae0b0 3427 then
4bb9c7b9
AC
3428 if Is_Array_Type (Subp_Type)
3429 and then
3430 (Nkind (Name (N)) /= N_Explicit_Dereference
3431 or else Comes_From_Source (Name (N)))
3432 then
aab883ec 3433 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
996ae0b0
RK
3434
3435 elsif Is_Access_Type (Subp_Type)
3436 and then Is_Array_Type (Designated_Type (Subp_Type))
3437 then
3438 Is_Indexed :=
aab883ec
ES
3439 Try_Indexed_Call
3440 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
996ae0b0 3441
758c442c 3442 -- The prefix can also be a parameterless function that returns an
f3d57416 3443 -- access to subprogram, in which case this is an indirect call.
53cf4600
ES
3444 -- If this succeeds, an explicit dereference is added later on,
3445 -- in Analyze_Call or Resolve_Call.
758c442c 3446
996ae0b0 3447 elsif Is_Access_Type (Subp_Type)
401093c1 3448 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
996ae0b0 3449 then
53cf4600 3450 Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
996ae0b0
RK
3451 end if;
3452
3453 end if;
3454
5ff22245 3455 -- If the call has been transformed into a slice, it is of the form
30783513 3456 -- F (Subtype) where F is parameterless. The node has been rewritten in
5ff22245
ES
3457 -- Try_Indexed_Call and there is nothing else to do.
3458
3459 if Is_Indexed
21d7ef70 3460 and then Nkind (N) = N_Slice
5ff22245
ES
3461 then
3462 return;
3463 end if;
3464
53cf4600
ES
3465 Normalize_Actuals
3466 (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
996ae0b0
RK
3467
3468 if not Norm_OK then
3469
53cf4600 3470 -- If an indirect call is a possible interpretation, indicate
80e59506 3471 -- success to the caller. This may be an indexing of an explicit
4bb9c7b9 3472 -- dereference of a call that returns an access type (see above).
53cf4600 3473
4bb9c7b9
AC
3474 if Is_Indirect
3475 or else (Is_Indexed
3476 and then Nkind (Name (N)) = N_Explicit_Dereference
3477 and then Comes_From_Source (Name (N)))
3478 then
53cf4600
ES
3479 Success := True;
3480 return;
3481
996ae0b0
RK
3482 -- Mismatch in number or names of parameters
3483
53cf4600 3484 elsif Debug_Flag_E then
996ae0b0
RK
3485 Write_Str (" normalization fails in call ");
3486 Write_Int (Int (N));
3487 Write_Str (" with subprogram ");
3488 Write_Int (Int (Nam));
3489 Write_Eol;
3490 end if;
3491
3492 -- If the context expects a function call, discard any interpretation
3493 -- that is a procedure. If the node is not overloaded, leave as is for
3494 -- better error reporting when type mismatch is found.
3495
3496 elsif Nkind (N) = N_Function_Call
3497 and then Is_Overloaded (Name (N))
3498 and then Ekind (Nam) = E_Procedure
3499 then
3500 return;
3501
4c46b835 3502 -- Ditto for function calls in a procedure context
996ae0b0
RK
3503
3504 elsif Nkind (N) = N_Procedure_Call_Statement
3505 and then Is_Overloaded (Name (N))
3506 and then Etype (Nam) /= Standard_Void_Type
3507 then
3508 return;
3509
fe45e59e 3510 elsif No (Actuals) then
996ae0b0
RK
3511
3512 -- If Normalize succeeds, then there are default parameters for
3513 -- all formals.
3514
fbf5a39b 3515 Indicate_Name_And_Type;
996ae0b0
RK
3516
3517 elsif Ekind (Nam) = E_Operator then
996ae0b0
RK
3518 if Nkind (N) = N_Procedure_Call_Statement then
3519 return;
3520 end if;
3521
3522 -- This can occur when the prefix of the call is an operator
3523 -- name or an expanded name whose selector is an operator name.
3524
3525 Analyze_Operator_Call (N, Nam);
3526
3527 if Etype (N) /= Prev_T then
3528
157a9bf5 3529 -- Check that operator is not hidden by a function interpretation
996ae0b0
RK
3530
3531 if Is_Overloaded (Name (N)) then
3532 declare
3533 I : Interp_Index;
3534 It : Interp;
3535
3536 begin
3537 Get_First_Interp (Name (N), I, It);
996ae0b0 3538 while Present (It.Nam) loop
157a9bf5 3539 if Operator_Hidden_By (It.Nam) then
996ae0b0
RK
3540 Set_Etype (N, Prev_T);
3541 return;
3542 end if;
3543
3544 Get_Next_Interp (I, It);
3545 end loop;
3546 end;
3547 end if;
3548
3549 -- If operator matches formals, record its name on the call.
3550 -- If the operator is overloaded, Resolve will select the
3551 -- correct one from the list of interpretations. The call
3552 -- node itself carries the first candidate.
3553
3554 Set_Entity (Name (N), Nam);
3555 Success := True;
3556
3557 elsif Report and then Etype (N) = Any_Type then
3558 Error_Msg_N ("incompatible arguments for operator", N);
3559 end if;
3560
3561 else
3562 -- Normalize_Actuals has chained the named associations in the
3563 -- correct order of the formals.
3564
2f0a921f
JS
3565 Actual := First_Actual (N);
3566 Formal := First_Formal (Nam);
3567 First_Form := Formal;
ec6078e3 3568
df3e68b1
HK
3569 -- If we are analyzing a call rewritten from object notation, skip
3570 -- first actual, which may be rewritten later as an explicit
3571 -- dereference.
ec6078e3 3572
aab883ec 3573 if Must_Skip then
ec6078e3
ES
3574 Next_Actual (Actual);
3575 Next_Formal (Formal);
3576 end if;
3577
996ae0b0 3578 while Present (Actual) and then Present (Formal) loop
fbf5a39b
AC
3579 if Nkind (Parent (Actual)) /= N_Parameter_Association
3580 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
996ae0b0 3581 then
9c510803
ES
3582 -- The actual can be compatible with the formal, but we must
3583 -- also check that the context is not an address type that is
7a5b62b0 3584 -- visibly an integer type. In this case the use of literals is
d9d25d04 3585 -- illegal, except in the body of descendants of system, where
7a5b62b0 3586 -- arithmetic operations on address are of course used.
9c510803
ES
3587
3588 if Has_Compatible_Type (Actual, Etype (Formal))
3589 and then
3590 (Etype (Actual) /= Universal_Integer
d9d25d04 3591 or else not Is_Descendant_Of_Address (Etype (Formal))
8ab31c0c 3592 or else In_Predefined_Unit (N))
9c510803 3593 then
996ae0b0
RK
3594 Next_Actual (Actual);
3595 Next_Formal (Formal);
3596
061828e3
AC
3597 -- In Allow_Integer_Address mode, we allow an actual integer to
3598 -- match a formal address type and vice versa. We only do this
3599 -- if we are certain that an error will otherwise be issued
3600
3601 elsif Address_Integer_Convert_OK
3602 (Etype (Actual), Etype (Formal))
3603 and then (Report and not Is_Indexed and not Is_Indirect)
3604 then
3605 -- Handle this case by introducing an unchecked conversion
3606
3607 Rewrite (Actual,
3608 Unchecked_Convert_To (Etype (Formal),
3609 Relocate_Node (Actual)));
3610 Analyze_And_Resolve (Actual, Etype (Formal));
3611 Next_Actual (Actual);
3612 Next_Formal (Formal);
3613
a8a42b93
AC
3614 -- Under relaxed RM semantics silently replace occurrences of
3615 -- null by System.Address_Null. We only do this if we know that
3616 -- an error will otherwise be issued.
3617
3618 elsif Null_To_Null_Address_Convert_OK (Actual, Etype (Formal))
3619 and then (Report and not Is_Indexed and not Is_Indirect)
3620 then
3621 Replace_Null_By_Null_Address (Actual);
3622 Analyze_And_Resolve (Actual, Etype (Formal));
3623 Next_Actual (Actual);
3624 Next_Formal (Formal);
3625
1d2d8a8f
AC
3626 elsif Compatible_Types_In_Predicate
3627 (Etype (Formal), Etype (Actual))
a921e83c 3628 then
a921e83c
AC
3629 Next_Actual (Actual);
3630 Next_Formal (Formal);
3631
bb072d1c
AC
3632 -- Handle failed type check
3633
996ae0b0
RK
3634 else
3635 if Debug_Flag_E then
3636 Write_Str (" type checking fails in call ");
3637 Write_Int (Int (N));
3638 Write_Str (" with formal ");
3639 Write_Int (Int (Formal));
3640 Write_Str (" in subprogram ");
3641 Write_Int (Int (Nam));
3642 Write_Eol;
3643 end if;
3644
061828e3
AC
3645 -- Comment needed on the following test???
3646
53cf4600 3647 if Report and not Is_Indexed and not Is_Indirect then
758c442c
GD
3648
3649 -- Ada 2005 (AI-251): Complete the error notification
8f2eeab7 3650 -- to help new Ada 2005 users.
758c442c
GD
3651
3652 if Is_Class_Wide_Type (Etype (Formal))
3653 and then Is_Interface (Etype (Etype (Formal)))
3654 and then not Interface_Present_In_Ancestor
3655 (Typ => Etype (Actual),
3656 Iface => Etype (Etype (Formal)))
3657 then
758c442c 3658 Error_Msg_NE
ec6078e3 3659 ("(Ada 2005) does not implement interface }",
758c442c
GD
3660 Actual, Etype (Etype (Formal)));
3661 end if;
3662
996ae0b0
RK
3663 Wrong_Type (Actual, Etype (Formal));
3664
3665 if Nkind (Actual) = N_Op_Eq
3666 and then Nkind (Left_Opnd (Actual)) = N_Identifier
3667 then
3668 Formal := First_Formal (Nam);
996ae0b0 3669 while Present (Formal) loop
996ae0b0 3670 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
4e7a4f6e 3671 Error_Msg_N -- CODEFIX
fbf5a39b 3672 ("possible misspelling of `='>`!", Actual);
996ae0b0
RK
3673 exit;
3674 end if;
3675
3676 Next_Formal (Formal);
3677 end loop;
3678 end if;
3679
3680 if All_Errors_Mode then
3681 Error_Msg_Sloc := Sloc (Nam);
3682
3b42c566
RD
3683 if Etype (Formal) = Any_Type then
3684 Error_Msg_N
3685 ("there is no legal actual parameter", Actual);
3686 end if;
3687
996ae0b0
RK
3688 if Is_Overloadable (Nam)
3689 and then Present (Alias (Nam))
3690 and then not Comes_From_Source (Nam)
3691 then
3692 Error_Msg_NE
401093c1
ES
3693 ("\\ =='> in call to inherited operation & #!",
3694 Actual, Nam);
7324bf49
AC
3695
3696 elsif Ekind (Nam) = E_Subprogram_Type then
3697 declare
3698 Access_To_Subprogram_Typ :
3699 constant Entity_Id :=
3700 Defining_Identifier
3701 (Associated_Node_For_Itype (Nam));
3702 begin
a90bd866
RD
3703 Error_Msg_NE
3704 ("\\ =='> in call to dereference of &#!",
3705 Actual, Access_To_Subprogram_Typ);
7324bf49
AC
3706 end;
3707
996ae0b0 3708 else
401093c1
ES
3709 Error_Msg_NE
3710 ("\\ =='> in call to &#!", Actual, Nam);
7324bf49 3711
996ae0b0
RK
3712 end if;
3713 end if;
3714 end if;
3715
3716 return;
3717 end if;
3718
3719 else
3720 -- Normalize_Actuals has verified that a default value exists
3721 -- for this formal. Current actual names a subsequent formal.
3722
3723 Next_Formal (Formal);
3724 end if;
3725 end loop;
3726
2f0a921f
JS
3727 -- Due to our current model of controlled type expansion we may
3728 -- have resolved a user call to a non-visible controlled primitive
3729 -- since these inherited subprograms may be generated in the current
4cdd4a33 3730 -- scope. This is a side effect of the need for the expander to be
2f0a921f
JS
3731 -- able to resolve internally generated calls.
3732
3733 -- Specifically, the issue appears when predefined controlled
3734 -- operations get called on a type extension whose parent is a
3735 -- private extension completed with a controlled extension - see
3736 -- below:
3737
3738 -- package X is
3739 -- type Par_Typ is tagged private;
3740 -- private
3741 -- type Par_Typ is new Controlled with null record;
3742 -- end;
3743 -- ...
3744 -- procedure Main is
3745 -- type Ext_Typ is new Par_Typ with null record;
3746 -- Obj : Ext_Typ;
3747 -- begin
3748 -- Finalize (Obj); -- Will improperly resolve
3749 -- end;
3750
3751 -- To avoid breaking privacy, Is_Hidden gets set elsewhere on such
3752 -- primitives, but we still need to verify that Nam is indeed a
3753 -- controlled subprogram. So, we do that here and issue the
3754 -- appropriate error.
3755
3756 if Is_Hidden (Nam)
3757 and then not In_Instance
3758 and then not Comes_From_Source (Nam)
3759 and then Comes_From_Source (N)
3760
3761 -- Verify Nam is a controlled primitive
3762
3763 and then Nam_In (Chars (Nam), Name_Adjust,
3764 Name_Finalize,
3765 Name_Initialize)
3766 and then Ekind (Nam) = E_Procedure
3767 and then Is_Controlled (Etype (First_Form))
3768 and then No (Next_Formal (First_Form))
3769 then
3770 Error_Msg_Node_2 := Etype (First_Form);
3771 Error_Msg_NE ("call to non-visible controlled primitive & on type"
3772 & " &", N, Nam);
3773 end if;
3774
4c46b835 3775 -- On exit, all actuals match
996ae0b0 3776
fbf5a39b 3777 Indicate_Name_And_Type;
996ae0b0
RK
3778 end if;
3779 end Analyze_One_Call;
3780
15ce9ca2
AC
3781 ---------------------------
3782 -- Analyze_Operator_Call --
3783 ---------------------------
996ae0b0
RK
3784
3785 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
3786 Op_Name : constant Name_Id := Chars (Op_Id);
3787 Act1 : constant Node_Id := First_Actual (N);
3788 Act2 : constant Node_Id := Next_Actual (Act1);
3789
3790 begin
4c46b835
AC
3791 -- Binary operator case
3792
996ae0b0
RK
3793 if Present (Act2) then
3794
4c46b835 3795 -- If more than two operands, then not binary operator after all
996ae0b0
RK
3796
3797 if Present (Next_Actual (Act2)) then
996ae0b0 3798 return;
b7539c3b 3799 end if;
996ae0b0 3800
b7539c3b 3801 -- Otherwise action depends on operator
996ae0b0 3802
b7539c3b 3803 case Op_Name is
d8f43ee6
HK
3804 when Name_Op_Add
3805 | Name_Op_Divide
3806 | Name_Op_Expon
3807 | Name_Op_Mod
3808 | Name_Op_Multiply
3809 | Name_Op_Rem
3810 | Name_Op_Subtract
3811 =>
b7539c3b 3812 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
996ae0b0 3813
d8f43ee6
HK
3814 when Name_Op_And
3815 | Name_Op_Or
3816 | Name_Op_Xor
3817 =>
b7539c3b 3818 Find_Boolean_Types (Act1, Act2, Op_Id, N);
996ae0b0 3819
d8f43ee6
HK
3820 when Name_Op_Ge
3821 | Name_Op_Gt
3822 | Name_Op_Le
3823 | Name_Op_Lt
3824 =>
b7539c3b 3825 Find_Comparison_Types (Act1, Act2, Op_Id, N);
996ae0b0 3826
d8f43ee6
HK
3827 when Name_Op_Eq
3828 | Name_Op_Ne
3829 =>
b7539c3b 3830 Find_Equality_Types (Act1, Act2, Op_Id, N);
996ae0b0 3831
d8f43ee6 3832 when Name_Op_Concat =>
b7539c3b 3833 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
996ae0b0 3834
b7539c3b
AC
3835 -- Is this when others, or should it be an abort???
3836
d8f43ee6 3837 when others =>
b7539c3b
AC
3838 null;
3839 end case;
996ae0b0 3840
4c46b835 3841 -- Unary operator case
996ae0b0 3842
4c46b835 3843 else
b7539c3b 3844 case Op_Name is
d8f43ee6
HK
3845 when Name_Op_Abs
3846 | Name_Op_Add
3847 | Name_Op_Subtract
3848 =>
b7539c3b 3849 Find_Unary_Types (Act1, Op_Id, N);
996ae0b0 3850
d8f43ee6 3851 when Name_Op_Not =>
b7539c3b 3852 Find_Negation_Types (Act1, Op_Id, N);
996ae0b0 3853
b7539c3b 3854 -- Is this when others correct, or should it be an abort???
996ae0b0 3855
d8f43ee6 3856 when others =>
b7539c3b
AC
3857 null;
3858 end case;
996ae0b0
RK
3859 end if;
3860 end Analyze_Operator_Call;
3861
3862 -------------------------------------------
3863 -- Analyze_Overloaded_Selected_Component --
3864 -------------------------------------------
3865
3866 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
fbf5a39b
AC
3867 Nam : constant Node_Id := Prefix (N);
3868 Sel : constant Node_Id := Selector_Name (N);
996ae0b0 3869 Comp : Entity_Id;
996ae0b0
RK
3870 I : Interp_Index;
3871 It : Interp;
3872 T : Entity_Id;
3873
3874 begin
4c46b835 3875 Set_Etype (Sel, Any_Type);
996ae0b0 3876
4c46b835 3877 Get_First_Interp (Nam, I, It);
996ae0b0
RK
3878 while Present (It.Typ) loop
3879 if Is_Access_Type (It.Typ) then
3880 T := Designated_Type (It.Typ);
324ac540 3881 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
996ae0b0
RK
3882 else
3883 T := It.Typ;
3884 end if;
3885
95eb8b69
AC
3886 -- Locate the component. For a private prefix the selector can denote
3887 -- a discriminant.
3888
3889 if Is_Record_Type (T) or else Is_Private_Type (T) then
d469eabe
HK
3890
3891 -- If the prefix is a class-wide type, the visible components are
3892 -- those of the base type.
3893
3894 if Is_Class_Wide_Type (T) then
3895 T := Etype (T);
3896 end if;
3897
996ae0b0 3898 Comp := First_Entity (T);
996ae0b0 3899 while Present (Comp) loop
996ae0b0 3900 if Chars (Comp) = Chars (Sel)
24e95966 3901 and then Is_Visible_Component (Comp, Sel)
996ae0b0 3902 then
996ae0b0 3903
f16d05d9
AC
3904 -- AI05-105: if the context is an object renaming with
3905 -- an anonymous access type, the expected type of the
3906 -- object must be anonymous. This is a name resolution rule.
996ae0b0 3907
f16d05d9
AC
3908 if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
3909 or else No (Access_Definition (Parent (N)))
3910 or else Ekind (Etype (Comp)) = E_Anonymous_Access_Type
3911 or else
3912 Ekind (Etype (Comp)) = E_Anonymous_Access_Subprogram_Type
3913 then
3914 Set_Entity (Sel, Comp);
3915 Set_Etype (Sel, Etype (Comp));
3916 Add_One_Interp (N, Etype (Comp), Etype (Comp));
44a10091 3917 Check_Implicit_Dereference (N, Etype (Comp));
f16d05d9
AC
3918
3919 -- This also specifies a candidate to resolve the name.
3920 -- Further overloading will be resolved from context.
3921 -- The selector name itself does not carry overloading
3922 -- information.
3923
3924 Set_Etype (Nam, It.Typ);
3925
3926 else
b61ee1aa 3927 -- Named access type in the context of a renaming
f16d05d9
AC
3928 -- declaration with an access definition. Remove
3929 -- inapplicable candidate.
3930
3931 Remove_Interp (I);
3932 end if;
996ae0b0
RK
3933 end if;
3934
3935 Next_Entity (Comp);
3936 end loop;
3937
3938 elsif Is_Concurrent_Type (T) then
3939 Comp := First_Entity (T);
996ae0b0
RK
3940 while Present (Comp)
3941 and then Comp /= First_Private_Entity (T)
3942 loop
3943 if Chars (Comp) = Chars (Sel) then
3944 if Is_Overloadable (Comp) then
3945 Add_One_Interp (Sel, Comp, Etype (Comp));
3946 else
e7ba564f 3947 Set_Entity_With_Checks (Sel, Comp);
996ae0b0
RK
3948 Generate_Reference (Comp, Sel);
3949 end if;
3950
3951 Set_Etype (Sel, Etype (Comp));
3952 Set_Etype (N, Etype (Comp));
3953 Set_Etype (Nam, It.Typ);
3954
09494c32
AC
3955 -- For access type case, introduce explicit dereference for
3956 -- more uniform treatment of entry calls. Do this only once
3957 -- if several interpretations yield an access type.
996ae0b0 3958
d469eabe
HK
3959 if Is_Access_Type (Etype (Nam))
3960 and then Nkind (Nam) /= N_Explicit_Dereference
3961 then
996ae0b0 3962 Insert_Explicit_Dereference (Nam);
fbf5a39b 3963 Error_Msg_NW
324ac540 3964 (Warn_On_Dereference, "?d?implicit dereference", N);
996ae0b0
RK
3965 end if;
3966 end if;
3967
3968 Next_Entity (Comp);
3969 end loop;
3970
3971 Set_Is_Overloaded (N, Is_Overloaded (Sel));
996ae0b0
RK
3972 end if;
3973
3974 Get_Next_Interp (I, It);
3975 end loop;
3976
0a36105d
JM
3977 if Etype (N) = Any_Type
3978 and then not Try_Object_Operation (N)
3979 then
996ae0b0
RK
3980 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
3981 Set_Entity (Sel, Any_Id);
3982 Set_Etype (Sel, Any_Type);
3983 end if;
996ae0b0
RK
3984 end Analyze_Overloaded_Selected_Component;
3985
3986 ----------------------------------
3987 -- Analyze_Qualified_Expression --
3988 ----------------------------------
3989
3990 procedure Analyze_Qualified_Expression (N : Node_Id) is
3991 Mark : constant Entity_Id := Subtype_Mark (N);
45c8b94b
ES
3992 Expr : constant Node_Id := Expression (N);
3993 I : Interp_Index;
3994 It : Interp;
996ae0b0
RK
3995 T : Entity_Id;
3996
3997 begin
45c8b94b
ES
3998 Analyze_Expression (Expr);
3999
996ae0b0
RK
4000 Set_Etype (N, Any_Type);
4001 Find_Type (Mark);
4002 T := Entity (Mark);
845af9e6 4003
aa11d1dd
PMR
4004 if Nkind_In (Enclosing_Declaration (N), N_Formal_Type_Declaration,
4005 N_Full_Type_Declaration,
4006 N_Incomplete_Type_Declaration,
4007 N_Protected_Type_Declaration,
4008 N_Private_Extension_Declaration,
4009 N_Private_Type_Declaration,
4010 N_Subtype_Declaration,
4011 N_Task_Type_Declaration)
845af9e6
PMR
4012 and then T = Defining_Identifier (Enclosing_Declaration (N))
4013 then
4014 Error_Msg_N ("current instance not allowed", Mark);
4015 T := Any_Type;
4016 end if;
4017
45c8b94b 4018 Set_Etype (N, T);
996ae0b0
RK
4019
4020 if T = Any_Type then
4021 return;
4022 end if;
996ae0b0 4023
4c46b835 4024 Check_Fully_Declared (T, N);
45c8b94b
ES
4025
4026 -- If expected type is class-wide, check for exact match before
4027 -- expansion, because if the expression is a dispatching call it
4028 -- may be rewritten as explicit dereference with class-wide result.
4029 -- If expression is overloaded, retain only interpretations that
4030 -- will yield exact matches.
4031
4032 if Is_Class_Wide_Type (T) then
4033 if not Is_Overloaded (Expr) then
32794080
JM
4034 if Base_Type (Etype (Expr)) /= Base_Type (T)
4035 and then Etype (Expr) /= Raise_Type
4036 then
45c8b94b
ES
4037 if Nkind (Expr) = N_Aggregate then
4038 Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
4039 else
4040 Wrong_Type (Expr, T);
4041 end if;
4042 end if;
4043
4044 else
4045 Get_First_Interp (Expr, I, It);
4046
4047 while Present (It.Nam) loop
4048 if Base_Type (It.Typ) /= Base_Type (T) then
4049 Remove_Interp (I);
4050 end if;
4051
4052 Get_Next_Interp (I, It);
4053 end loop;
4054 end if;
4055 end if;
4056
996ae0b0
RK
4057 Set_Etype (N, T);
4058 end Analyze_Qualified_Expression;
4059
a961aa79
AC
4060 -----------------------------------
4061 -- Analyze_Quantified_Expression --
4062 -----------------------------------
4063
4064 procedure Analyze_Quantified_Expression (N : Node_Id) is
4856cc2a 4065 function Is_Empty_Range (Typ : Entity_Id) return Boolean;
538dbb56
AC
4066 -- If the iterator is part of a quantified expression, and the range is
4067 -- known to be statically empty, emit a warning and replace expression
4856cc2a 4068 -- with its static value. Returns True if the replacement occurs.
538dbb56 4069
0812b84e
AC
4070 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean;
4071 -- Determine whether if expression If_Expr lacks an else part or if it
4072 -- has one, it evaluates to True.
4073
4856cc2a
ES
4074 --------------------
4075 -- Is_Empty_Range --
4076 --------------------
4077
4078 function Is_Empty_Range (Typ : Entity_Id) return Boolean is
4079 Loc : constant Source_Ptr := Sloc (N);
538dbb56
AC
4080
4081 begin
4082 if Is_Array_Type (Typ)
4856cc2a
ES
4083 and then Compile_Time_Known_Bounds (Typ)
4084 and then
9a6dc470
RD
4085 (Expr_Value (Type_Low_Bound (Etype (First_Index (Typ)))) >
4086 Expr_Value (Type_High_Bound (Etype (First_Index (Typ)))))
538dbb56 4087 then
4856cc2a
ES
4088 Preanalyze_And_Resolve (Condition (N), Standard_Boolean);
4089
538dbb56 4090 if All_Present (N) then
4856cc2a 4091 Error_Msg_N
324ac540 4092 ("??quantified expression with ALL "
4856cc2a 4093 & "over a null range has value True", N);
538dbb56
AC
4094 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
4095
4096 else
4856cc2a 4097 Error_Msg_N
324ac540 4098 ("??quantified expression with SOME "
4856cc2a 4099 & "over a null range has value False", N);
538dbb56
AC
4100 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4101 end if;
4102
4103 Analyze (N);
4104 return True;
4105
4106 else
4107 return False;
4108 end if;
4109 end Is_Empty_Range;
4110
0812b84e
AC
4111 -----------------------------
4112 -- No_Else_Or_Trivial_True --
4113 -----------------------------
4114
4115 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean is
4116 Else_Expr : constant Node_Id :=
4117 Next (Next (First (Expressions (If_Expr))));
4118 begin
4119 return
4120 No (Else_Expr)
4121 or else (Compile_Time_Known_Value (Else_Expr)
4122 and then Is_True (Expr_Value (Else_Expr)));
4123 end No_Else_Or_Trivial_True;
4124
4125 -- Local variables
4126
4127 Cond : constant Node_Id := Condition (N);
57081559 4128 Loop_Id : Entity_Id;
0812b84e
AC
4129 QE_Scop : Entity_Id;
4130
4856cc2a
ES
4131 -- Start of processing for Analyze_Quantified_Expression
4132
a961aa79 4133 begin
ce5ba43a 4134 Check_SPARK_05_Restriction ("quantified expression is not allowed", N);
1d801f21 4135
804670f1
AC
4136 -- Create a scope to emulate the loop-like behavior of the quantified
4137 -- expression. The scope is needed to provide proper visibility of the
4138 -- loop variable.
b3e42de5 4139
804670f1
AC
4140 QE_Scop := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L');
4141 Set_Etype (QE_Scop, Standard_Void_Type);
4142 Set_Scope (QE_Scop, Current_Scope);
4143 Set_Parent (QE_Scop, N);
a961aa79 4144
804670f1 4145 Push_Scope (QE_Scop);
c56a9ba4 4146
804670f1
AC
4147 -- All constituents are preanalyzed and resolved to avoid untimely
4148 -- generation of various temporaries and types. Full analysis and
4149 -- expansion is carried out when the quantified expression is
4150 -- transformed into an expression with actions.
c56a9ba4 4151
804670f1
AC
4152 if Present (Iterator_Specification (N)) then
4153 Preanalyze (Iterator_Specification (N));
538dbb56 4154
57081559
AC
4155 -- Do not proceed with the analysis when the range of iteration is
4156 -- empty. The appropriate error is issued by Is_Empty_Range.
4157
538dbb56
AC
4158 if Is_Entity_Name (Name (Iterator_Specification (N)))
4159 and then Is_Empty_Range (Etype (Name (Iterator_Specification (N))))
4160 then
4161 return;
4162 end if;
4163
57081559 4164 else pragma Assert (Present (Loop_Parameter_Specification (N)));
a736f6e6
AC
4165 declare
4166 Loop_Par : constant Node_Id := Loop_Parameter_Specification (N);
4167
4168 begin
4169 Preanalyze (Loop_Par);
4170
e4deba8e 4171 if Nkind (Discrete_Subtype_Definition (Loop_Par)) = N_Function_Call
a736f6e6
AC
4172 and then Parent (Loop_Par) /= N
4173 then
4174 -- The parser cannot distinguish between a loop specification
5b85ad7d 4175 -- and an iterator specification. If after preanalysis the
a736f6e6 4176 -- proper form has been recognized, rewrite the expression to
5f0c4d67
AC
4177 -- reflect the right kind. This is needed for proper ASIS
4178 -- navigation. If expansion is enabled, the transformation is
4179 -- performed when the expression is rewritten as a loop.
65f1ca2e 4180 -- Is this still needed???
a736f6e6 4181
a736f6e6
AC
4182 Set_Iterator_Specification (N,
4183 New_Copy_Tree (Iterator_Specification (Parent (Loop_Par))));
5f0c4d67
AC
4184
4185 Set_Defining_Identifier (Iterator_Specification (N),
4186 Relocate_Node (Defining_Identifier (Loop_Par)));
4187 Set_Name (Iterator_Specification (N),
4188 Relocate_Node (Discrete_Subtype_Definition (Loop_Par)));
4189 Set_Comes_From_Source (Iterator_Specification (N),
4190 Comes_From_Source (Loop_Parameter_Specification (N)));
4191 Set_Loop_Parameter_Specification (N, Empty);
a736f6e6
AC
4192 end if;
4193 end;
ce6002ec
AC
4194 end if;
4195
0812b84e 4196 Preanalyze_And_Resolve (Cond, Standard_Boolean);
804670f1 4197
a961aa79
AC
4198 End_Scope;
4199 Set_Etype (N, Standard_Boolean);
0812b84e 4200
57081559
AC
4201 -- Verify that the loop variable is used within the condition of the
4202 -- quantified expression.
4203
4204 if Present (Iterator_Specification (N)) then
4205 Loop_Id := Defining_Identifier (Iterator_Specification (N));
4206 else
4207 Loop_Id := Defining_Identifier (Loop_Parameter_Specification (N));
4208 end if;
4209
4210 if Warn_On_Suspicious_Contract
4211 and then not Referenced (Loop_Id, Cond)
4212 then
124bed29 4213 -- Generating C, this check causes spurious warnings on inlined
519e9fdf 4214 -- postconditions; we can safely disable it because this check
124bed29 4215 -- was previously performed when analyzing the internally built
519e9fdf
AC
4216 -- postconditions procedure.
4217
4218 if Modify_Tree_For_C and then In_Inlined_Body then
4219 null;
4220 else
4221 Error_Msg_N ("?T?unused variable &", Loop_Id);
4222 end if;
57081559
AC
4223 end if;
4224
e19fd0bd 4225 -- Diagnose a possible misuse of the SOME existential quantifier. When
d1ec4768
RD
4226 -- we have a quantified expression of the form:
4227
0812b84e 4228 -- for some X => (if P then Q [else True])
d1ec4768 4229
e19fd0bd 4230 -- any value for X that makes P False results in the if expression being
50ef946c 4231 -- trivially True, and so also results in the quantified expression
e19fd0bd 4232 -- being trivially True.
0812b84e 4233
e19fd0bd 4234 if Warn_On_Suspicious_Contract
0812b84e
AC
4235 and then not All_Present (N)
4236 and then Nkind (Cond) = N_If_Expression
4237 and then No_Else_Or_Trivial_True (Cond)
4238 then
e19fd0bd 4239 Error_Msg_N ("?T?suspicious expression", N);
0812b84e
AC
4240 Error_Msg_N ("\\did you mean (for all X ='> (if P then Q))", N);
4241 Error_Msg_N ("\\or (for some X ='> P and then Q) instead'?", N);
4242 end if;
a961aa79
AC
4243 end Analyze_Quantified_Expression;
4244
996ae0b0
RK
4245 -------------------
4246 -- Analyze_Range --
4247 -------------------
4248
4249 procedure Analyze_Range (N : Node_Id) is
4250 L : constant Node_Id := Low_Bound (N);
4251 H : constant Node_Id := High_Bound (N);
4252 I1, I2 : Interp_Index;
4253 It1, It2 : Interp;
4254
4255 procedure Check_Common_Type (T1, T2 : Entity_Id);
4256 -- Verify the compatibility of two types, and choose the
4257 -- non universal one if the other is universal.
4258
4259 procedure Check_High_Bound (T : Entity_Id);
4260 -- Test one interpretation of the low bound against all those
4261 -- of the high bound.
4262
fbf5a39b 4263 procedure Check_Universal_Expression (N : Node_Id);
a1092b48
AC
4264 -- In Ada 83, reject bounds of a universal range that are not literals
4265 -- or entity names.
fbf5a39b 4266
996ae0b0
RK
4267 -----------------------
4268 -- Check_Common_Type --
4269 -----------------------
4270
4271 procedure Check_Common_Type (T1, T2 : Entity_Id) is
4272 begin
b4592168
GD
4273 if Covers (T1 => T1, T2 => T2)
4274 or else
4275 Covers (T1 => T2, T2 => T1)
4276 then
996ae0b0
RK
4277 if T1 = Universal_Integer
4278 or else T1 = Universal_Real
4279 or else T1 = Any_Character
4280 then
4281 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
4282
fbf5a39b 4283 elsif T1 = T2 then
996ae0b0
RK
4284 Add_One_Interp (N, T1, T1);
4285
4286 else
4287 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
4288 end if;
4289 end if;
4290 end Check_Common_Type;
4291
4292 ----------------------
4293 -- Check_High_Bound --
4294 ----------------------
4295
4296 procedure Check_High_Bound (T : Entity_Id) is
4297 begin
4298 if not Is_Overloaded (H) then
4299 Check_Common_Type (T, Etype (H));
4300 else
4301 Get_First_Interp (H, I2, It2);
996ae0b0
RK
4302 while Present (It2.Typ) loop
4303 Check_Common_Type (T, It2.Typ);
4304 Get_Next_Interp (I2, It2);
4305 end loop;
4306 end if;
4307 end Check_High_Bound;
4308
8016e567
PT
4309 --------------------------------
4310 -- Check_Universal_Expression --
4311 --------------------------------
fbf5a39b
AC
4312
4313 procedure Check_Universal_Expression (N : Node_Id) is
4314 begin
4315 if Etype (N) = Universal_Integer
4316 and then Nkind (N) /= N_Integer_Literal
4317 and then not Is_Entity_Name (N)
4318 and then Nkind (N) /= N_Attribute_Reference
4319 then
4320 Error_Msg_N ("illegal bound in discrete range", N);
4321 end if;
4322 end Check_Universal_Expression;
4323
996ae0b0
RK
4324 -- Start of processing for Analyze_Range
4325
4326 begin
4327 Set_Etype (N, Any_Type);
4328 Analyze_Expression (L);
4329 Analyze_Expression (H);
4330
4331 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
4332 return;
4333
4334 else
4335 if not Is_Overloaded (L) then
4336 Check_High_Bound (Etype (L));
4337 else
4338 Get_First_Interp (L, I1, It1);
996ae0b0
RK
4339 while Present (It1.Typ) loop
4340 Check_High_Bound (It1.Typ);
4341 Get_Next_Interp (I1, It1);
4342 end loop;
4343 end if;
4344
4345 -- If result is Any_Type, then we did not find a compatible pair
4346
4347 if Etype (N) = Any_Type then
4348 Error_Msg_N ("incompatible types in range ", N);
4349 end if;
4350 end if;
fbf5a39b 4351
0ab80019 4352 if Ada_Version = Ada_83
fbf5a39b
AC
4353 and then
4354 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
4c46b835 4355 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
fbf5a39b
AC
4356 then
4357 Check_Universal_Expression (L);
4358 Check_Universal_Expression (H);
4359 end if;
d3820795 4360
22e89283 4361 Check_Function_Writable_Actuals (N);
996ae0b0
RK
4362 end Analyze_Range;
4363
4364 -----------------------
4365 -- Analyze_Reference --
4366 -----------------------
4367
4368 procedure Analyze_Reference (N : Node_Id) is
4369 P : constant Node_Id := Prefix (N);
b4592168
GD
4370 E : Entity_Id;
4371 T : Entity_Id;
996ae0b0 4372 Acc_Type : Entity_Id;
b4592168 4373
996ae0b0
RK
4374 begin
4375 Analyze (P);
b4592168 4376
c42e1b17
AC
4377 -- An interesting error check, if we take the 'Ref of an object for
4378 -- which a pragma Atomic or Volatile has been given, and the type of the
4379 -- object is not Atomic or Volatile, then we are in trouble. The problem
4380 -- is that no trace of the atomic/volatile status will remain for the
4381 -- backend to respect when it deals with the resulting pointer, since
4382 -- the pointer type will not be marked atomic (it is a pointer to the
4383 -- base type of the object).
b4592168
GD
4384
4385 -- It is not clear if that can ever occur, but in case it does, we will
4386 -- generate an error message. Not clear if this message can ever be
4387 -- generated, and pretty clear that it represents a bug if it is, still
d2f25cd1
AC
4388 -- seems worth checking, except in CodePeer mode where we do not really
4389 -- care and don't want to bother the user.
b4592168
GD
4390
4391 T := Etype (P);
4392
4393 if Is_Entity_Name (P)
4394 and then Is_Object_Reference (P)
d2f25cd1 4395 and then not CodePeer_Mode
b4592168
GD
4396 then
4397 E := Entity (P);
4398 T := Etype (P);
4399
4400 if (Has_Atomic_Components (E)
c42e1b17 4401 and then not Has_Atomic_Components (T))
b4592168
GD
4402 or else
4403 (Has_Volatile_Components (E)
c42e1b17 4404 and then not Has_Volatile_Components (T))
b4592168
GD
4405 or else (Is_Atomic (E) and then not Is_Atomic (T))
4406 or else (Is_Volatile (E) and then not Is_Volatile (T))
4407 then
4408 Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
4409 end if;
4410 end if;
4411
4412 -- Carry on with normal processing
4413
996ae0b0 4414 Acc_Type := Create_Itype (E_Allocator_Type, N);
b4592168 4415 Set_Etype (Acc_Type, Acc_Type);
996ae0b0
RK
4416 Set_Directly_Designated_Type (Acc_Type, Etype (P));
4417 Set_Etype (N, Acc_Type);
4418 end Analyze_Reference;
4419
4420 --------------------------------
4421 -- Analyze_Selected_Component --
4422 --------------------------------
4423
2383acbd
AC
4424 -- Prefix is a record type or a task or protected type. In the latter case,
4425 -- the selector must denote a visible entry.
996ae0b0
RK
4426
4427 procedure Analyze_Selected_Component (N : Node_Id) is
d469eabe
HK
4428 Name : constant Node_Id := Prefix (N);
4429 Sel : constant Node_Id := Selector_Name (N);
4430 Act_Decl : Node_Id;
4431 Comp : Entity_Id;
4432 Has_Candidate : Boolean := False;
c0e938d0 4433 Hidden_Comp : Entity_Id;
d469eabe 4434 In_Scope : Boolean;
d1eb8a82 4435 Is_Private_Op : Boolean;
d469eabe
HK
4436 Parent_N : Node_Id;
4437 Pent : Entity_Id := Empty;
4438 Prefix_Type : Entity_Id;
401093c1
ES
4439
4440 Type_To_Use : Entity_Id;
4441 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
4442 -- a class-wide type, we use its root type, whose components are
4443 -- present in the class-wide type.
4444
2383acbd
AC
4445 Is_Single_Concurrent_Object : Boolean;
4446 -- Set True if the prefix is a single task or a single protected object
4447
20261dc1
AC
4448 procedure Find_Component_In_Instance (Rec : Entity_Id);
4449 -- In an instance, a component of a private extension may not be visible
4450 -- while it was visible in the generic. Search candidate scope for a
4451 -- component with the proper identifier. This is only done if all other
f90d14ac
AC
4452 -- searches have failed. If a match is found, the Etype of both N and
4453 -- Sel are set from this component, and the entity of Sel is set to
4454 -- reference this component. If no match is found, Entity (Sel) remains
7d9880c9
AC
4455 -- unset. For a derived type that is an actual of the instance, the
4456 -- desired component may be found in any ancestor.
20261dc1 4457
d469eabe
HK
4458 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
4459 -- It is known that the parent of N denotes a subprogram call. Comp
4460 -- is an overloadable component of the concurrent type of the prefix.
4461 -- Determine whether all formals of the parent of N and Comp are mode
b4592168
GD
4462 -- conformant. If the parent node is not analyzed yet it may be an
4463 -- indexed component rather than a function call.
d469eabe 4464
bd717ec9 4465 function Has_Dereference (Nod : Node_Id) return Boolean;
0f8b3e5d 4466 -- Check whether prefix includes a dereference at any level.
bd717ec9 4467
20261dc1
AC
4468 --------------------------------
4469 -- Find_Component_In_Instance --
4470 --------------------------------
4471
4472 procedure Find_Component_In_Instance (Rec : Entity_Id) is
4473 Comp : Entity_Id;
7d9880c9 4474 Typ : Entity_Id;
20261dc1
AC
4475
4476 begin
7d9880c9
AC
4477 Typ := Rec;
4478 while Present (Typ) loop
4479 Comp := First_Component (Typ);
4480 while Present (Comp) loop
4481 if Chars (Comp) = Chars (Sel) then
4482 Set_Entity_With_Checks (Sel, Comp);
4483 Set_Etype (Sel, Etype (Comp));
4484 Set_Etype (N, Etype (Comp));
4485 return;
4486 end if;
4487
4488 Next_Component (Comp);
4489 end loop;
4490
4491 -- If not found, the component may be declared in the parent
4492 -- type or its full view, if any.
4493
4494 if Is_Derived_Type (Typ) then
4495 Typ := Etype (Typ);
4496
4497 if Is_Private_Type (Typ) then
4498 Typ := Full_View (Typ);
4499 end if;
4500
4501 else
20261dc1
AC
4502 return;
4503 end if;
20261dc1
AC
4504 end loop;
4505
cf3e6845
AC
4506 -- If we fall through, no match, so no changes made
4507
4508 return;
20261dc1
AC
4509 end Find_Component_In_Instance;
4510
d469eabe
HK
4511 ------------------------------
4512 -- Has_Mode_Conformant_Spec --
4513 ------------------------------
4514
4515 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
4516 Comp_Param : Entity_Id;
4517 Param : Node_Id;
4518 Param_Typ : Entity_Id;
4519
4520 begin
4521 Comp_Param := First_Formal (Comp);
b4592168
GD
4522
4523 if Nkind (Parent (N)) = N_Indexed_Component then
4524 Param := First (Expressions (Parent (N)));
4525 else
4526 Param := First (Parameter_Associations (Parent (N)));
4527 end if;
4528
d469eabe
HK
4529 while Present (Comp_Param)
4530 and then Present (Param)
4531 loop
4532 Param_Typ := Find_Parameter_Type (Param);
4533
4534 if Present (Param_Typ)
4535 and then
4536 not Conforming_Types
4537 (Etype (Comp_Param), Param_Typ, Mode_Conformant)
4538 then
4539 return False;
4540 end if;
4541
4542 Next_Formal (Comp_Param);
4543 Next (Param);
4544 end loop;
4545
9e92ad49
AC
4546 -- One of the specs has additional formals; there is no match, unless
4547 -- this may be an indexing of a parameterless call.
f0e7963f
AC
4548
4549 -- Note that when expansion is disabled, the corresponding record
4550 -- type of synchronized types is not constructed, so that there is
4551 -- no point is attempting an interpretation as a prefixed call, as
4552 -- this is bound to fail because the primitive operations will not
4553 -- be properly located.
d469eabe
HK
4554
4555 if Present (Comp_Param) or else Present (Param) then
f0e7963f
AC
4556 if Needs_No_Actuals (Comp)
4557 and then Is_Array_Type (Etype (Comp))
4558 and then not Expander_Active
4559 then
4560 return True;
f0e7963f
AC
4561 else
4562 return False;
4563 end if;
d469eabe
HK
4564 end if;
4565
4566 return True;
4567 end Has_Mode_Conformant_Spec;
996ae0b0 4568
bd717ec9
AC
4569 ---------------------
4570 -- Has_Dereference --
4571 ---------------------
4572
4573 function Has_Dereference (Nod : Node_Id) return Boolean is
4574 begin
4575 if Nkind (Nod) = N_Explicit_Dereference then
4576 return True;
4577
a6363ed3
AC
4578 -- When expansion is disabled an explicit dereference may not have
4579 -- been inserted, but if this is an access type the indirection makes
4580 -- the call safe.
4581
4582 elsif Is_Access_Type (Etype (Nod)) then
4583 return True;
4584
bd717ec9
AC
4585 elsif Nkind_In (Nod, N_Indexed_Component, N_Selected_Component) then
4586 return Has_Dereference (Prefix (Nod));
4587
4588 else
4589 return False;
4590 end if;
4591 end Has_Dereference;
4592
996ae0b0
RK
4593 -- Start of processing for Analyze_Selected_Component
4594
4595 begin
4596 Set_Etype (N, Any_Type);
4597
4598 if Is_Overloaded (Name) then
4599 Analyze_Overloaded_Selected_Component (N);
4600 return;
4601
4602 elsif Etype (Name) = Any_Type then
4603 Set_Entity (Sel, Any_Id);
4604 Set_Etype (Sel, Any_Type);
4605 return;
4606
4607 else
996ae0b0
RK
4608 Prefix_Type := Etype (Name);
4609 end if;
4610
4611 if Is_Access_Type (Prefix_Type) then
07fc65c4 4612
0d57c6f4
RD
4613 -- A RACW object can never be used as prefix of a selected component
4614 -- since that means it is dereferenced without being a controlling
4615 -- operand of a dispatching operation (RM E.2.2(16/1)). Before
4616 -- reporting an error, we must check whether this is actually a
4617 -- dispatching call in prefix form.
07fc65c4 4618
996ae0b0
RK
4619 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
4620 and then Comes_From_Source (N)
4621 then
b4592168
GD
4622 if Try_Object_Operation (N) then
4623 return;
4624 else
4625 Error_Msg_N
4626 ("invalid dereference of a remote access-to-class-wide value",
4627 N);
4628 end if;
07fc65c4
GB
4629
4630 -- Normal case of selected component applied to access type
4631
4632 else
324ac540 4633 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
da709d08 4634
6e73e3ab
AC
4635 if Is_Entity_Name (Name) then
4636 Pent := Entity (Name);
4637 elsif Nkind (Name) = N_Selected_Component
4638 and then Is_Entity_Name (Selector_Name (Name))
4639 then
4640 Pent := Entity (Selector_Name (Name));
4641 end if;
da709d08 4642
d469eabe 4643 Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name);
996ae0b0 4644 end if;
b4592168
GD
4645
4646 -- If we have an explicit dereference of a remote access-to-class-wide
4647 -- value, then issue an error (see RM-E.2.2(16/1)). However we first
4648 -- have to check for the case of a prefix that is a controlling operand
4649 -- of a prefixed dispatching call, as the dereference is legal in that
4650 -- case. Normally this condition is checked in Validate_Remote_Access_
4651 -- To_Class_Wide_Type, but we have to defer the checking for selected
4652 -- component prefixes because of the prefixed dispatching call case.
4653 -- Note that implicit dereferences are checked for this just above.
4654
4655 elsif Nkind (Name) = N_Explicit_Dereference
4656 and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
4657 and then Comes_From_Source (N)
4658 then
4659 if Try_Object_Operation (N) then
4660 return;
4661 else
4662 Error_Msg_N
4663 ("invalid dereference of a remote access-to-class-wide value",
4664 N);
4665 end if;
aab883ec 4666 end if;
b67a385c 4667
aab883ec
ES
4668 -- (Ada 2005): if the prefix is the limited view of a type, and
4669 -- the context already includes the full view, use the full view
4670 -- in what follows, either to retrieve a component of to find
4671 -- a primitive operation. If the prefix is an explicit dereference,
4672 -- set the type of the prefix to reflect this transformation.
a316b3fc 4673 -- If the nonlimited view is itself an incomplete type, get the
401093c1 4674 -- full view if available.
aab883ec 4675
47346923
AC
4676 if From_Limited_With (Prefix_Type)
4677 and then Has_Non_Limited_View (Prefix_Type)
aab883ec 4678 then
401093c1 4679 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
aab883ec
ES
4680
4681 if Nkind (N) = N_Explicit_Dereference then
4682 Set_Etype (Prefix (N), Prefix_Type);
4683 end if;
996ae0b0
RK
4684 end if;
4685
4686 if Ekind (Prefix_Type) = E_Private_Subtype then
4687 Prefix_Type := Base_Type (Prefix_Type);
4688 end if;
4689
401093c1 4690 Type_To_Use := Prefix_Type;
996ae0b0
RK
4691
4692 -- For class-wide types, use the entity list of the root type. This
4693 -- indirection is specially important for private extensions because
4694 -- only the root type get switched (not the class-wide type).
4695
4696 if Is_Class_Wide_Type (Prefix_Type) then
401093c1 4697 Type_To_Use := Root_Type (Prefix_Type);
996ae0b0
RK
4698 end if;
4699
2383acbd
AC
4700 -- If the prefix is a single concurrent object, use its name in error
4701 -- messages, rather than that of its anonymous type.
4702
4703 Is_Single_Concurrent_Object :=
4704 Is_Concurrent_Type (Prefix_Type)
4705 and then Is_Internal_Name (Chars (Prefix_Type))
4706 and then not Is_Derived_Type (Prefix_Type)
4707 and then Is_Entity_Name (Name);
4708
401093c1 4709 Comp := First_Entity (Type_To_Use);
996ae0b0
RK
4710
4711 -- If the selector has an original discriminant, the node appears in
4712 -- an instance. Replace the discriminant with the corresponding one
4713 -- in the current discriminated type. For nested generics, this must
4714 -- be done transitively, so note the new original discriminant.
4715
4716 if Nkind (Sel) = N_Identifier
c0b11850 4717 and then In_Instance
996ae0b0
RK
4718 and then Present (Original_Discriminant (Sel))
4719 then
4720 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
4721
4722 -- Mark entity before rewriting, for completeness and because
4723 -- subsequent semantic checks might examine the original node.
4724
4725 Set_Entity (Sel, Comp);
ee2ba856 4726 Rewrite (Selector_Name (N), New_Occurrence_Of (Comp, Sloc (N)));
996ae0b0
RK
4727 Set_Original_Discriminant (Selector_Name (N), Comp);
4728 Set_Etype (N, Etype (Comp));
44a10091 4729 Check_Implicit_Dereference (N, Etype (Comp));
996ae0b0
RK
4730
4731 if Is_Access_Type (Etype (Name)) then
4732 Insert_Explicit_Dereference (Name);
324ac540 4733 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
996ae0b0
RK
4734 end if;
4735
4736 elsif Is_Record_Type (Prefix_Type) then
4737
ee2ba856
AC
4738 -- Find component with given name. In an instance, if the node is
4739 -- known as a prefixed call, do not examine components whose
4740 -- visibility may be accidental.
996ae0b0 4741
4913e24c 4742 while Present (Comp) and then not Is_Prefixed_Call (N) loop
996ae0b0 4743 if Chars (Comp) = Chars (Sel)
a53c5613 4744 and then Is_Visible_Component (Comp, N)
996ae0b0 4745 then
e7ba564f 4746 Set_Entity_With_Checks (Sel, Comp);
996ae0b0
RK
4747 Set_Etype (Sel, Etype (Comp));
4748
4749 if Ekind (Comp) = E_Discriminant then
5d09245e 4750 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
996ae0b0 4751 Error_Msg_N
02f58834 4752 ("cannot reference discriminant of unchecked union",
996ae0b0
RK
4753 Sel);
4754 end if;
4755
4756 if Is_Generic_Type (Prefix_Type)
4757 or else
4758 Is_Generic_Type (Root_Type (Prefix_Type))
4759 then
4760 Set_Original_Discriminant (Sel, Comp);
4761 end if;
4762 end if;
4763
4764 -- Resolve the prefix early otherwise it is not possible to
4765 -- build the actual subtype of the component: it may need
4766 -- to duplicate this prefix and duplication is only allowed
4767 -- on fully resolved expressions.
4768
fbf5a39b 4769 Resolve (Name);
996ae0b0 4770
b67a385c
ES
4771 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
4772 -- subtypes in a package specification.
28be29ce
ES
4773 -- Example:
4774
4775 -- limited with Pkg;
4776 -- package Pkg is
4777 -- type Acc_Inc is access Pkg.T;
4778 -- X : Acc_Inc;
b67a385c
ES
4779 -- N : Natural := X.all.Comp; -- ERROR, limited view
4780 -- end Pkg; -- Comp is not visible
28be29ce
ES
4781
4782 if Nkind (Name) = N_Explicit_Dereference
7b56a91b 4783 and then From_Limited_With (Etype (Prefix (Name)))
28be29ce 4784 and then not Is_Potentially_Use_Visible (Etype (Name))
b67a385c
ES
4785 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
4786 N_Package_Specification
28be29ce
ES
4787 then
4788 Error_Msg_NE
4789 ("premature usage of incomplete}", Prefix (Name),
4790 Etype (Prefix (Name)));
4791 end if;
4792
996ae0b0
RK
4793 -- We never need an actual subtype for the case of a selection
4794 -- for a indexed component of a non-packed array, since in
4795 -- this case gigi generates all the checks and can find the
4796 -- necessary bounds information.
4797
0d57c6f4
RD
4798 -- We also do not need an actual subtype for the case of a
4799 -- first, last, length, or range attribute applied to a
996ae0b0
RK
4800 -- non-packed array, since gigi can again get the bounds in
4801 -- these cases (gigi cannot handle the packed case, since it
4802 -- has the bounds of the packed array type, not the original
4803 -- bounds of the type). However, if the prefix is itself a
4804 -- selected component, as in a.b.c (i), gigi may regard a.b.c
4805 -- as a dynamic-sized temporary, so we do generate an actual
4806 -- subtype for this case.
4807
4808 Parent_N := Parent (N);
4809
4810 if not Is_Packed (Etype (Comp))
4811 and then
4812 ((Nkind (Parent_N) = N_Indexed_Component
d469eabe 4813 and then Nkind (Name) /= N_Selected_Component)
996ae0b0
RK
4814 or else
4815 (Nkind (Parent_N) = N_Attribute_Reference
b69cd36a
AC
4816 and then
4817 Nam_In (Attribute_Name (Parent_N), Name_First,
4818 Name_Last,
4819 Name_Length,
4820 Name_Range)))
996ae0b0
RK
4821 then
4822 Set_Etype (N, Etype (Comp));
4823
98123480
ES
4824 -- If full analysis is not enabled, we do not generate an
4825 -- actual subtype, because in the absence of expansion
4826 -- reference to a formal of a protected type, for example,
4827 -- will not be properly transformed, and will lead to
4828 -- out-of-scope references in gigi.
4829
4830 -- In all other cases, we currently build an actual subtype.
4831 -- It seems likely that many of these cases can be avoided,
4832 -- but right now, the front end makes direct references to the
fbf5a39b 4833 -- bounds (e.g. in generating a length check), and if we do
996ae0b0 4834 -- not make an actual subtype, we end up getting a direct
98123480 4835 -- reference to a discriminant, which will not do.
996ae0b0 4836
98123480 4837 elsif Full_Analysis then
996ae0b0
RK
4838 Act_Decl :=
4839 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
4840 Insert_Action (N, Act_Decl);
4841
4842 if No (Act_Decl) then
4843 Set_Etype (N, Etype (Comp));
4844
4845 else
605d8166
ES
4846 -- If discriminants were present in the component
4847 -- declaration, they have been replaced by the
4848 -- actual values in the prefix object.
996ae0b0
RK
4849
4850 declare
fbf5a39b
AC
4851 Subt : constant Entity_Id :=
4852 Defining_Identifier (Act_Decl);
996ae0b0
RK
4853 begin
4854 Set_Etype (Subt, Base_Type (Etype (Comp)));
996ae0b0
RK
4855 Set_Etype (N, Subt);
4856 end;
4857 end if;
98123480
ES
4858
4859 -- If Full_Analysis not enabled, just set the Etype
4860
4861 else
4862 Set_Etype (N, Etype (Comp));
996ae0b0
RK
4863 end if;
4864
44a10091 4865 Check_Implicit_Dereference (N, Etype (N));
996ae0b0
RK
4866 return;
4867 end if;
4868
aab883ec 4869 -- If the prefix is a private extension, check only the visible
9c510803 4870 -- components of the partial view. This must include the tag,
f3d57416 4871 -- which can appear in expanded code in a tag check.
aab883ec 4872
9c510803 4873 if Ekind (Type_To_Use) = E_Record_Type_With_Private
df3e68b1 4874 and then Chars (Selector_Name (N)) /= Name_uTag
9c510803 4875 then
401093c1 4876 exit when Comp = Last_Entity (Type_To_Use);
aab883ec
ES
4877 end if;
4878
996ae0b0
RK
4879 Next_Entity (Comp);
4880 end loop;
4881
d469eabe
HK
4882 -- Ada 2005 (AI-252): The selected component can be interpreted as
4883 -- a prefixed view of a subprogram. Depending on the context, this is
4884 -- either a name that can appear in a renaming declaration, or part
4885 -- of an enclosing call given in prefix form.
4886
4887 -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the
4888 -- selected component should resolve to a name.
35ae2ed8 4889
0791fbe9 4890 if Ada_Version >= Ada_2005
35ae2ed8 4891 and then Is_Tagged_Type (Prefix_Type)
d469eabe 4892 and then not Is_Concurrent_Type (Prefix_Type)
35ae2ed8 4893 then
d469eabe
HK
4894 if Nkind (Parent (N)) = N_Generic_Association
4895 or else Nkind (Parent (N)) = N_Requeue_Statement
4896 or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
4897 then
4898 if Find_Primitive_Operation (N) then
4899 return;
4900 end if;
4901
4902 elsif Try_Object_Operation (N) then
4903 return;
4904 end if;
4c46b835 4905
98123480
ES
4906 -- If the transformation fails, it will be necessary to redo the
4907 -- analysis with all errors enabled, to indicate candidate
4908 -- interpretations and reasons for each failure ???
4c46b835 4909
35ae2ed8
AC
4910 end if;
4911
996ae0b0 4912 elsif Is_Private_Type (Prefix_Type) then
d469eabe 4913
98123480
ES
4914 -- Allow access only to discriminants of the type. If the type has
4915 -- no full view, gigi uses the parent type for the components, so we
4916 -- do the same here.
996ae0b0
RK
4917
4918 if No (Full_View (Prefix_Type)) then
401093c1
ES
4919 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
4920 Comp := First_Entity (Type_To_Use);
996ae0b0
RK
4921 end if;
4922
4923 while Present (Comp) loop
996ae0b0
RK
4924 if Chars (Comp) = Chars (Sel) then
4925 if Ekind (Comp) = E_Discriminant then
e7ba564f 4926 Set_Entity_With_Checks (Sel, Comp);
996ae0b0
RK
4927 Generate_Reference (Comp, Sel);
4928
4929 Set_Etype (Sel, Etype (Comp));
4930 Set_Etype (N, Etype (Comp));
44a10091 4931 Check_Implicit_Dereference (N, Etype (N));
996ae0b0
RK
4932
4933 if Is_Generic_Type (Prefix_Type)
d469eabe 4934 or else Is_Generic_Type (Root_Type (Prefix_Type))
996ae0b0
RK
4935 then
4936 Set_Original_Discriminant (Sel, Comp);
4937 end if;
4938
f3d57416 4939 -- Before declaring an error, check whether this is tagged
aab883ec
ES
4940 -- private type and a call to a primitive operation.
4941
0791fbe9 4942 elsif Ada_Version >= Ada_2005
aab883ec
ES
4943 and then Is_Tagged_Type (Prefix_Type)
4944 and then Try_Object_Operation (N)
4945 then
4946 return;
4947
996ae0b0 4948 else
2383acbd
AC
4949 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4950 Error_Msg_NE ("invisible selector& for }", N, Sel);
996ae0b0
RK
4951 Set_Entity (Sel, Any_Id);
4952 Set_Etype (N, Any_Type);
4953 end if;
4954
4955 return;
4956 end if;
4957
4958 Next_Entity (Comp);
4959 end loop;
4960
4961 elsif Is_Concurrent_Type (Prefix_Type) then
4962
d469eabe
HK
4963 -- Find visible operation with given name. For a protected type,
4964 -- the possible candidates are discriminants, entries or protected
d1eb8a82 4965 -- subprograms. For a task type, the set can only include entries or
d469eabe
HK
4966 -- discriminants if the task type is not an enclosing scope. If it
4967 -- is an enclosing scope (e.g. in an inner task) then all entities
4968 -- are visible, but the prefix must denote the enclosing scope, i.e.
4969 -- can only be a direct name or an expanded name.
996ae0b0 4970
d469eabe 4971 Set_Etype (Sel, Any_Type);
c0e938d0 4972 Hidden_Comp := Empty;
996ae0b0 4973 In_Scope := In_Open_Scopes (Prefix_Type);
d1eb8a82 4974 Is_Private_Op := False;
996ae0b0
RK
4975
4976 while Present (Comp) loop
f31dcd99 4977
86ec3bfb
AC
4978 -- Do not examine private operations of the type if not within
4979 -- its scope.
4980
996ae0b0 4981 if Chars (Comp) = Chars (Sel) then
86ec3bfb
AC
4982 if Is_Overloadable (Comp)
4983 and then (In_Scope
4984 or else Comp /= First_Private_Entity (Type_To_Use))
4985 then
996ae0b0 4986 Add_One_Interp (Sel, Comp, Etype (Comp));
d1eb8a82
AC
4987 if Comp = First_Private_Entity (Type_To_Use) then
4988 Is_Private_Op := True;
4989 end if;
996ae0b0 4990
d469eabe
HK
4991 -- If the prefix is tagged, the correct interpretation may
4992 -- lie in the primitive or class-wide operations of the
4993 -- type. Perform a simple conformance check to determine
4994 -- whether Try_Object_Operation should be invoked even if
4995 -- a visible entity is found.
4996
4997 if Is_Tagged_Type (Prefix_Type)
f31dcd99
HK
4998 and then Nkind_In (Parent (N), N_Function_Call,
4999 N_Indexed_Component,
5000 N_Procedure_Call_Statement)
d469eabe
HK
5001 and then Has_Mode_Conformant_Spec (Comp)
5002 then
5003 Has_Candidate := True;
5004 end if;
5005
2383acbd
AC
5006 -- Note: a selected component may not denote a component of a
5007 -- protected type (4.1.3(7)).
5008
bce79204 5009 elsif Ekind_In (Comp, E_Discriminant, E_Entry_Family)
2383acbd
AC
5010 or else (In_Scope
5011 and then not Is_Protected_Type (Prefix_Type)
5012 and then Is_Entity_Name (Name))
996ae0b0 5013 then
e7ba564f 5014 Set_Entity_With_Checks (Sel, Comp);
996ae0b0
RK
5015 Generate_Reference (Comp, Sel);
5016
65e78a74
AC
5017 -- The selector is not overloadable, so we have a candidate
5018 -- interpretation.
5019
5020 Has_Candidate := True;
5021
996ae0b0 5022 else
c0e938d0
AC
5023 if Ekind (Comp) = E_Component then
5024 Hidden_Comp := Comp;
5025 end if;
5026
996ae0b0
RK
5027 goto Next_Comp;
5028 end if;
5029
5030 Set_Etype (Sel, Etype (Comp));
5031 Set_Etype (N, Etype (Comp));
5032
5033 if Ekind (Comp) = E_Discriminant then
5034 Set_Original_Discriminant (Sel, Comp);
5035 end if;
5036
09494c32
AC
5037 -- For access type case, introduce explicit dereference for
5038 -- more uniform treatment of entry calls.
996ae0b0
RK
5039
5040 if Is_Access_Type (Etype (Name)) then
5041 Insert_Explicit_Dereference (Name);
fbf5a39b 5042 Error_Msg_NW
324ac540 5043 (Warn_On_Dereference, "?d?implicit dereference", N);
996ae0b0
RK
5044 end if;
5045 end if;
5046
5047 <<Next_Comp>>
c0e938d0
AC
5048 if Comp = First_Private_Entity (Type_To_Use) then
5049 if Etype (Sel) /= Any_Type then
5050
27de857e
GD
5051 -- If the first private entity's name matches, then treat
5052 -- it as a private op: needed for the error check for
5053 -- illegal selection of private entities further below.
5054
5055 if Chars (Comp) = Chars (Sel) then
5056 Is_Private_Op := True;
5057 end if;
5058
5059 -- We have a candidate, so exit the loop
c5b4738f 5060
c0e938d0
AC
5061 exit;
5062
5063 else
5064 -- Indicate that subsequent operations are private,
5065 -- for better error reporting.
5066
5067 Is_Private_Op := True;
5068 end if;
5069 end if;
5070
614bc51c
PMR
5071 -- Do not examine private operations if not within scope of
5072 -- the synchronized type.
5073
996ae0b0 5074 exit when not In_Scope
9bc856dd
AC
5075 and then
5076 Comp = First_Private_Entity (Base_Type (Prefix_Type));
614bc51c 5077 Next_Entity (Comp);
996ae0b0
RK
5078 end loop;
5079
b3083540 5080 -- If the scope is a current instance, the prefix cannot be an
0f6251c7
AC
5081 -- expression of the same type, unless the selector designates a
5082 -- public operation (otherwise that would represent an attempt to
5083 -- reach an internal entity of another synchronized object).
be3416c6 5084
b3083540 5085 -- This is legal if prefix is an access to such type and there is
0f6251c7 5086 -- a dereference, or is a component with a dereferenced prefix.
0f8b3e5d
AC
5087 -- It is also legal if the prefix is a component of a task type,
5088 -- and the selector is one of the task operations.
b3083540 5089
bd717ec9
AC
5090 if In_Scope
5091 and then not Is_Entity_Name (Name)
5092 and then not Has_Dereference (Name)
5093 then
0f8b3e5d
AC
5094 if Is_Task_Type (Prefix_Type)
5095 and then Present (Entity (Sel))
5096 and then Ekind_In (Entity (Sel), E_Entry, E_Entry_Family)
5097 then
5098 null;
5099
d1eb8a82
AC
5100 elsif Is_Protected_Type (Prefix_Type)
5101 and then Is_Overloadable (Entity (Sel))
5102 and then not Is_Private_Op
5103 then
5104 null;
5105
0f8b3e5d
AC
5106 else
5107 Error_Msg_NE
5108 ("invalid reference to internal operation of some object of "
5109 & "type &", N, Type_To_Use);
5110 Set_Entity (Sel, Any_Id);
5111 Set_Etype (Sel, Any_Type);
5112 return;
5113 end if;
be3416c6
AC
5114
5115 -- Another special case: the prefix may denote an object of the type
5116 -- (but not a type) in which case this is an external call and the
5117 -- operation must be public.
5118
5119 elsif In_Scope
5120 and then Is_Object_Reference (Original_Node (Prefix (N)))
c0e938d0 5121 and then Comes_From_Source (N)
be3416c6
AC
5122 and then Is_Private_Op
5123 then
c0e938d0
AC
5124 if Present (Hidden_Comp) then
5125 Error_Msg_NE
c5b4738f
AC
5126 ("invalid reference to private component of object of type "
5127 & "&", N, Type_To_Use);
c0e938d0
AC
5128
5129 else
5130 Error_Msg_NE
5131 ("invalid reference to private operation of some object of "
5132 & "type &", N, Type_To_Use);
5133 end if;
5134
be3416c6
AC
5135 Set_Entity (Sel, Any_Id);
5136 Set_Etype (Sel, Any_Type);
5137 return;
b3083540
AC
5138 end if;
5139
d469eabe
HK
5140 -- If there is no visible entity with the given name or none of the
5141 -- visible entities are plausible interpretations, check whether
5142 -- there is some other primitive operation with that name.
aab883ec 5143
bc38dbb4 5144 if Ada_Version >= Ada_2005 and then Is_Tagged_Type (Prefix_Type) then
d469eabe
HK
5145 if (Etype (N) = Any_Type
5146 or else not Has_Candidate)
0a36105d
JM
5147 and then Try_Object_Operation (N)
5148 then
5149 return;
5150
5151 -- If the context is not syntactically a procedure call, it
5152 -- may be a call to a primitive function declared outside of
5153 -- the synchronized type.
5154
5155 -- If the context is a procedure call, there might still be
5156 -- an overloading between an entry and a primitive procedure
5157 -- declared outside of the synchronized type, called in prefix
5158 -- notation. This is harder to disambiguate because in one case
5159 -- the controlling formal is implicit ???
5160
5161 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
b4592168 5162 and then Nkind (Parent (N)) /= N_Indexed_Component
0a36105d
JM
5163 and then Try_Object_Operation (N)
5164 then
5165 return;
5166 end if;
8cf23b91
AC
5167
5168 -- Ada 2012 (AI05-0090-1): If we found a candidate of a call to an
5169 -- entry or procedure of a tagged concurrent type we must check
5170 -- if there are class-wide subprograms covering the primitive. If
5171 -- true then Try_Object_Operation reports the error.
5172
5173 if Has_Candidate
5174 and then Is_Concurrent_Type (Prefix_Type)
5175 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
bc38dbb4 5176 then
8cf23b91
AC
5177 -- Duplicate the call. This is required to avoid problems with
5178 -- the tree transformations performed by Try_Object_Operation.
d7a44b14
AC
5179 -- Set properly the parent of the copied call, because it is
5180 -- about to be reanalyzed.
8cf23b91 5181
d7a44b14
AC
5182 declare
5183 Par : constant Node_Id := New_Copy_Tree (Parent (N));
5184
5185 begin
5186 Set_Parent (Par, Parent (Parent (N)));
29ba9f52 5187
d7a44b14 5188 if Try_Object_Operation
29ba9f52 5189 (Sinfo.Name (Par), CW_Test_Only => True)
d7a44b14
AC
5190 then
5191 return;
5192 end if;
5193 end;
8cf23b91 5194 end if;
aab883ec
ES
5195 end if;
5196
2383acbd 5197 if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
11bc76df 5198
2383acbd
AC
5199 -- Case of a prefix of a protected type: selector might denote
5200 -- an invisible private component.
5201
5202 Comp := First_Private_Entity (Base_Type (Prefix_Type));
5203 while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
5204 Next_Entity (Comp);
5205 end loop;
5206
5207 if Present (Comp) then
5208 if Is_Single_Concurrent_Object then
5209 Error_Msg_Node_2 := Entity (Name);
5210 Error_Msg_NE ("invisible selector& for &", N, Sel);
5211
5212 else
5213 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5214 Error_Msg_NE ("invisible selector& for }", N, Sel);
5215 end if;
5216 return;
5217 end if;
5218 end if;
5219
996ae0b0
RK
5220 Set_Is_Overloaded (N, Is_Overloaded (Sel));
5221
5222 else
5223 -- Invalid prefix
5224
5225 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
5226 end if;
5227
4c46b835 5228 -- If N still has no type, the component is not defined in the prefix
996ae0b0
RK
5229
5230 if Etype (N) = Any_Type then
5231
2383acbd 5232 if Is_Single_Concurrent_Object then
996ae0b0
RK
5233 Error_Msg_Node_2 := Entity (Name);
5234 Error_Msg_NE ("no selector& for&", N, Sel);
5235
401093c1 5236 Check_Misspelled_Selector (Type_To_Use, Sel);
996ae0b0 5237
8b4230c8
AC
5238 -- If this is a derived formal type, the parent may have different
5239 -- visibility at this point. Try for an inherited component before
5240 -- reporting an error.
5241
de76a39c
GB
5242 elsif Is_Generic_Type (Prefix_Type)
5243 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
07fc65c4 5244 and then Prefix_Type /= Etype (Prefix_Type)
de76a39c
GB
5245 and then Is_Record_Type (Etype (Prefix_Type))
5246 then
de76a39c
GB
5247 Set_Etype (Prefix (N), Etype (Prefix_Type));
5248 Analyze_Selected_Component (N);
5249 return;
5250
b1d12996
AC
5251 -- Similarly, if this is the actual for a formal derived type, or
5252 -- a derived type thereof, the component inherited from the generic
5253 -- parent may not be visible in the actual, but the selected
5254 -- component is legal. Climb up the derivation chain of the generic
5255 -- parent type until we find the proper ancestor type.
20261dc1 5256
b1d12996
AC
5257 elsif In_Instance and then Is_Tagged_Type (Prefix_Type) then
5258 declare
5259 Par : Entity_Id := Prefix_Type;
5260 begin
5261 -- Climb up derivation chain to generic actual subtype
5262
5263 while not Is_Generic_Actual_Type (Par) loop
5264 if Ekind (Par) = E_Record_Type then
5265 Par := Parent_Subtype (Par);
5266 exit when No (Par);
5267 else
5268 exit when Par = Etype (Par);
5269 Par := Etype (Par);
5270 end if;
5271 end loop;
4c46b835 5272
b1d12996 5273 if Present (Par) and then Is_Generic_Actual_Type (Par) then
73999267 5274
b1d12996 5275 -- Now look for component in ancestor types
fbf5a39b 5276
b1d12996
AC
5277 Par := Generic_Parent_Type (Declaration_Node (Par));
5278 loop
5279 Find_Component_In_Instance (Par);
5280 exit when Present (Entity (Sel))
5281 or else Par = Etype (Par);
5282 Par := Etype (Par);
5283 end loop;
73999267 5284
7d9880c9
AC
5285 -- Another special case: the type is an extension of a private
5286 -- type T, is an actual in an instance, and we are in the body
5287 -- of the instance, so the generic body had a full view of the
5288 -- type declaration for T or of some ancestor that defines the
5289 -- component in question.
5290
5291 elsif Is_Derived_Type (Type_To_Use)
5292 and then Used_As_Generic_Actual (Type_To_Use)
5293 and then In_Instance_Body
5294 then
5295 Find_Component_In_Instance (Parent_Subtype (Type_To_Use));
5296
73999267
AC
5297 -- In ASIS mode the generic parent type may be absent. Examine
5298 -- the parent type directly for a component that may have been
5299 -- visible in a parent generic unit.
65f1ca2e 5300 -- ??? Revisit now that ASIS mode is gone
73999267
AC
5301
5302 elsif Is_Derived_Type (Prefix_Type) then
5303 Par := Etype (Prefix_Type);
5304 Find_Component_In_Instance (Par);
b1d12996
AC
5305 end if;
5306 end;
5307
5308 -- The search above must have eventually succeeded, since the
5309 -- selected component was legal in the generic.
5310
5311 if No (Entity (Sel)) then
5312 raise Program_Error;
5313 end if;
73999267 5314
20261dc1 5315 return;
fbf5a39b 5316
20261dc1 5317 -- Component not found, specialize error message when appropriate
fbf5a39b 5318
996ae0b0
RK
5319 else
5320 if Ekind (Prefix_Type) = E_Record_Subtype then
5321
f4b049db
AC
5322 -- Check whether this is a component of the base type which
5323 -- is absent from a statically constrained subtype. This will
5324 -- raise constraint error at run time, but is not a compile-
5325 -- time error. When the selector is illegal for base type as
5326 -- well fall through and generate a compilation error anyway.
996ae0b0
RK
5327
5328 Comp := First_Component (Base_Type (Prefix_Type));
996ae0b0 5329 while Present (Comp) loop
996ae0b0 5330 if Chars (Comp) = Chars (Sel)
24e95966 5331 and then Is_Visible_Component (Comp, Sel)
996ae0b0 5332 then
e7ba564f 5333 Set_Entity_With_Checks (Sel, Comp);
996ae0b0
RK
5334 Generate_Reference (Comp, Sel);
5335 Set_Etype (Sel, Etype (Comp));
5336 Set_Etype (N, Etype (Comp));
5337
637a41a5
AC
5338 -- Emit appropriate message. The node will be replaced
5339 -- by an appropriate raise statement.
996ae0b0 5340
637a41a5
AC
5341 -- Note that in SPARK mode, as with all calls to apply a
5342 -- compile time constraint error, this will be made into
5343 -- an error to simplify the processing of the formal
5344 -- verification backend.
d7f41b2d 5345
4a28b181 5346 Apply_Compile_Time_Constraint_Error
637a41a5 5347 (N, "component not present in }??",
4a28b181
AC
5348 CE_Discriminant_Check_Failed,
5349 Ent => Prefix_Type, Rep => False);
d7f41b2d 5350
996ae0b0
RK
5351 Set_Raises_Constraint_Error (N);
5352 return;
5353 end if;
5354
5355 Next_Component (Comp);
5356 end loop;
5357
5358 end if;
5359
5360 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5361 Error_Msg_NE ("no selector& for}", N, Sel);
5362
94bbf008 5363 -- Add information in the case of an incomplete prefix
99d520ad
ES
5364
5365 if Is_Incomplete_Type (Type_To_Use) then
5366 declare
5367 Inc : constant Entity_Id := First_Subtype (Type_To_Use);
5368
5369 begin
7b56a91b 5370 if From_Limited_With (Scope (Type_To_Use)) then
99d520ad
ES
5371 Error_Msg_NE
5372 ("\limited view of& has no components", N, Inc);
bd38b431 5373
99d520ad
ES
5374 else
5375 Error_Msg_NE
5376 ("\premature usage of incomplete type&", N, Inc);
bd38b431
AC
5377
5378 if Nkind (Parent (Inc)) =
5379 N_Incomplete_Type_Declaration
99d520ad 5380 then
94bbf008
AC
5381 -- Record location of premature use in entity so that
5382 -- a continuation message is generated when the
5383 -- completion is seen.
5384
99d520ad
ES
5385 Set_Premature_Use (Parent (Inc), N);
5386 end if;
5387 end if;
5388 end;
5389 end if;
5390
401093c1 5391 Check_Misspelled_Selector (Type_To_Use, Sel);
996ae0b0
RK
5392 end if;
5393
5394 Set_Entity (Sel, Any_Id);
5395 Set_Etype (Sel, Any_Type);
5396 end if;
5397 end Analyze_Selected_Component;
5398
5399 ---------------------------
5400 -- Analyze_Short_Circuit --
5401 ---------------------------
5402
5403 procedure Analyze_Short_Circuit (N : Node_Id) is
5404 L : constant Node_Id := Left_Opnd (N);
5405 R : constant Node_Id := Right_Opnd (N);
5406 Ind : Interp_Index;
5407 It : Interp;
5408
5409 begin
5410 Analyze_Expression (L);
5411 Analyze_Expression (R);
5412 Set_Etype (N, Any_Type);
5413
5414 if not Is_Overloaded (L) then
996ae0b0
RK
5415 if Root_Type (Etype (L)) = Standard_Boolean
5416 and then Has_Compatible_Type (R, Etype (L))
5417 then
5418 Add_One_Interp (N, Etype (L), Etype (L));
5419 end if;
5420
5421 else
5422 Get_First_Interp (L, Ind, It);
996ae0b0
RK
5423 while Present (It.Typ) loop
5424 if Root_Type (It.Typ) = Standard_Boolean
5425 and then Has_Compatible_Type (R, It.Typ)
5426 then
5427 Add_One_Interp (N, It.Typ, It.Typ);
5428 end if;
5429
5430 Get_Next_Interp (Ind, It);
5431 end loop;
5432 end if;
5433
d469eabe
HK
5434 -- Here we have failed to find an interpretation. Clearly we know that
5435 -- it is not the case that both operands can have an interpretation of
5436 -- Boolean, but this is by far the most likely intended interpretation.
5437 -- So we simply resolve both operands as Booleans, and at least one of
5438 -- these resolutions will generate an error message, and we do not need
5439 -- to give another error message on the short circuit operation itself.
996ae0b0
RK
5440
5441 if Etype (N) = Any_Type then
5442 Resolve (L, Standard_Boolean);
5443 Resolve (R, Standard_Boolean);
5444 Set_Etype (N, Standard_Boolean);
5445 end if;
5446 end Analyze_Short_Circuit;
5447
5448 -------------------
5449 -- Analyze_Slice --
5450 -------------------
5451
5452 procedure Analyze_Slice (N : Node_Id) is
996ae0b0 5453 D : constant Node_Id := Discrete_Range (N);
5f44f0d4 5454 P : constant Node_Id := Prefix (N);
996ae0b0 5455 Array_Type : Entity_Id;
5f44f0d4 5456 Index_Type : Entity_Id;
996ae0b0
RK
5457
5458 procedure Analyze_Overloaded_Slice;
5459 -- If the prefix is overloaded, select those interpretations that
5460 -- yield a one-dimensional array type.
5461
4c46b835
AC
5462 ------------------------------
5463 -- Analyze_Overloaded_Slice --
5464 ------------------------------
5465
996ae0b0
RK
5466 procedure Analyze_Overloaded_Slice is
5467 I : Interp_Index;
5468 It : Interp;
5469 Typ : Entity_Id;
5470
5471 begin
5472 Set_Etype (N, Any_Type);
996ae0b0 5473
4c46b835 5474 Get_First_Interp (P, I, It);
996ae0b0
RK
5475 while Present (It.Nam) loop
5476 Typ := It.Typ;
5477
5478 if Is_Access_Type (Typ) then
5479 Typ := Designated_Type (Typ);
324ac540
AC
5480 Error_Msg_NW
5481 (Warn_On_Dereference, "?d?implicit dereference", N);
996ae0b0
RK
5482 end if;
5483
5484 if Is_Array_Type (Typ)
5485 and then Number_Dimensions (Typ) = 1
5486 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
5487 then
5488 Add_One_Interp (N, Typ, Typ);
5489 end if;
5490
5491 Get_Next_Interp (I, It);
5492 end loop;
5493
5494 if Etype (N) = Any_Type then
5495 Error_Msg_N ("expect array type in prefix of slice", N);
5496 end if;
5497 end Analyze_Overloaded_Slice;
5498
5499 -- Start of processing for Analyze_Slice
5500
5501 begin
36b8f95f 5502 if Comes_From_Source (N) then
ce5ba43a 5503 Check_SPARK_05_Restriction ("slice is not allowed", N);
36b8f95f 5504 end if;
1d801f21 5505
523456db 5506 Analyze (P);
996ae0b0
RK
5507 Analyze (D);
5508
5509 if Is_Overloaded (P) then
5510 Analyze_Overloaded_Slice;
5511
5512 else
5513 Array_Type := Etype (P);
5514 Set_Etype (N, Any_Type);
5515
5516 if Is_Access_Type (Array_Type) then
5517 Array_Type := Designated_Type (Array_Type);
324ac540 5518 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
996ae0b0
RK
5519 end if;
5520
5521 if not Is_Array_Type (Array_Type) then
5522 Wrong_Type (P, Any_Array);
5523
5524 elsif Number_Dimensions (Array_Type) > 1 then
5525 Error_Msg_N
5526 ("type is not one-dimensional array in slice prefix", N);
5527
996ae0b0 5528 else
5f44f0d4
AC
5529 if Ekind (Array_Type) = E_String_Literal_Subtype then
5530 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
5531 else
5532 Index_Type := Etype (First_Index (Array_Type));
5533 end if;
5534
5535 if not Has_Compatible_Type (D, Index_Type) then
5536 Wrong_Type (D, Index_Type);
5537 else
5538 Set_Etype (N, Array_Type);
5539 end if;
996ae0b0
RK
5540 end if;
5541 end if;
5542 end Analyze_Slice;
5543
5544 -----------------------------
5545 -- Analyze_Type_Conversion --
5546 -----------------------------
5547
5548 procedure Analyze_Type_Conversion (N : Node_Id) is
5549 Expr : constant Node_Id := Expression (N);
039538bc 5550 Typ : Entity_Id;
996ae0b0
RK
5551
5552 begin
039538bc
AC
5553 -- If Conversion_OK is set, then the Etype is already set, and the only
5554 -- processing required is to analyze the expression. This is used to
5555 -- construct certain "illegal" conversions which are not allowed by Ada
5556 -- semantics, but can be handled by Gigi, see Sinfo for further details.
996ae0b0
RK
5557
5558 if Conversion_OK (N) then
5559 Analyze (Expr);
5560 return;
5561 end if;
5562
5563 -- Otherwise full type analysis is required, as well as some semantic
5564 -- checks to make sure the argument of the conversion is appropriate.
5565
5566 Find_Type (Subtype_Mark (N));
039538bc
AC
5567 Typ := Entity (Subtype_Mark (N));
5568 Set_Etype (N, Typ);
5569 Check_Fully_Declared (Typ, N);
996ae0b0
RK
5570 Analyze_Expression (Expr);
5571 Validate_Remote_Type_Type_Conversion (N);
5e8c8e44 5572
996ae0b0
RK
5573 -- Only remaining step is validity checks on the argument. These
5574 -- are skipped if the conversion does not come from the source.
5575
5576 if not Comes_From_Source (N) then
5577 return;
5578
b67a385c
ES
5579 -- If there was an error in a generic unit, no need to replicate the
5580 -- error message. Conversely, constant-folding in the generic may
5581 -- transform the argument of a conversion into a string literal, which
5582 -- is legal. Therefore the following tests are not performed in an
36428cc4 5583 -- instance. The same applies to an inlined body.
b67a385c 5584
36428cc4 5585 elsif In_Instance or In_Inlined_Body then
b67a385c
ES
5586 return;
5587
996ae0b0
RK
5588 elsif Nkind (Expr) = N_Null then
5589 Error_Msg_N ("argument of conversion cannot be null", N);
ed2233dc 5590 Error_Msg_N ("\use qualified expression instead", N);
996ae0b0
RK
5591 Set_Etype (N, Any_Type);
5592
5593 elsif Nkind (Expr) = N_Aggregate then
5594 Error_Msg_N ("argument of conversion cannot be aggregate", N);
ed2233dc 5595 Error_Msg_N ("\use qualified expression instead", N);
996ae0b0
RK
5596
5597 elsif Nkind (Expr) = N_Allocator then
5598 Error_Msg_N ("argument of conversion cannot be an allocator", N);
ed2233dc 5599 Error_Msg_N ("\use qualified expression instead", N);
996ae0b0
RK
5600
5601 elsif Nkind (Expr) = N_String_Literal then
5602 Error_Msg_N ("argument of conversion cannot be string literal", N);
ed2233dc 5603 Error_Msg_N ("\use qualified expression instead", N);
996ae0b0
RK
5604
5605 elsif Nkind (Expr) = N_Character_Literal then
0ab80019 5606 if Ada_Version = Ada_83 then
039538bc 5607 Resolve (Expr, Typ);
996ae0b0
RK
5608 else
5609 Error_Msg_N ("argument of conversion cannot be character literal",
5610 N);
ed2233dc 5611 Error_Msg_N ("\use qualified expression instead", N);
996ae0b0
RK
5612 end if;
5613
5614 elsif Nkind (Expr) = N_Attribute_Reference
039538bc
AC
5615 and then Nam_In (Attribute_Name (Expr), Name_Access,
5616 Name_Unchecked_Access,
5617 Name_Unrestricted_Access)
996ae0b0
RK
5618 then
5619 Error_Msg_N ("argument of conversion cannot be access", N);
ed2233dc 5620 Error_Msg_N ("\use qualified expression instead", N);
996ae0b0 5621 end if;
039538bc
AC
5622
5623 -- A formal parameter of a specific tagged type whose related subprogram
5624 -- is subject to pragma Extensions_Visible with value "False" cannot
070d862d
HK
5625 -- appear in a class-wide conversion (SPARK RM 6.1.7(3)). Do not check
5626 -- internally generated expressions.
039538bc 5627
070d862d
HK
5628 if Is_Class_Wide_Type (Typ)
5629 and then Comes_From_Source (Expr)
5630 and then Is_EVF_Expression (Expr)
5631 then
039538bc 5632 Error_Msg_N
44900051
AC
5633 ("formal parameter cannot be converted to class-wide type when "
5634 & "Extensions_Visible is False", Expr);
039538bc 5635 end if;
996ae0b0
RK
5636 end Analyze_Type_Conversion;
5637
5638 ----------------------
5639 -- Analyze_Unary_Op --
5640 ----------------------
5641
5642 procedure Analyze_Unary_Op (N : Node_Id) is
5643 R : constant Node_Id := Right_Opnd (N);
5644 Op_Id : Entity_Id := Entity (N);
5645
5646 begin
5647 Set_Etype (N, Any_Type);
5648 Candidate_Type := Empty;
5649
5650 Analyze_Expression (R);
5651
5652 if Present (Op_Id) then
5653 if Ekind (Op_Id) = E_Operator then
5654 Find_Unary_Types (R, Op_Id, N);
5655 else
5656 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5657 end if;
5658
5659 else
5660 Op_Id := Get_Name_Entity_Id (Chars (N));
996ae0b0 5661 while Present (Op_Id) loop
996ae0b0
RK
5662 if Ekind (Op_Id) = E_Operator then
5663 if No (Next_Entity (First_Entity (Op_Id))) then
5664 Find_Unary_Types (R, Op_Id, N);
5665 end if;
5666
5667 elsif Is_Overloadable (Op_Id) then
5668 Analyze_User_Defined_Unary_Op (N, Op_Id);
5669 end if;
5670
5671 Op_Id := Homonym (Op_Id);
5672 end loop;
5673 end if;
5674
5675 Operator_Check (N);
5676 end Analyze_Unary_Op;
5677
5678 ----------------------------------
5679 -- Analyze_Unchecked_Expression --
5680 ----------------------------------
5681
5682 procedure Analyze_Unchecked_Expression (N : Node_Id) is
5683 begin
5684 Analyze (Expression (N), Suppress => All_Checks);
5685 Set_Etype (N, Etype (Expression (N)));
5686 Save_Interps (Expression (N), N);
5687 end Analyze_Unchecked_Expression;
5688
5689 ---------------------------------------
5690 -- Analyze_Unchecked_Type_Conversion --
5691 ---------------------------------------
5692
5693 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
5694 begin
5695 Find_Type (Subtype_Mark (N));
5696 Analyze_Expression (Expression (N));
5697 Set_Etype (N, Entity (Subtype_Mark (N)));
5698 end Analyze_Unchecked_Type_Conversion;
5699
5700 ------------------------------------
5701 -- Analyze_User_Defined_Binary_Op --
5702 ------------------------------------
5703
5704 procedure Analyze_User_Defined_Binary_Op
5705 (N : Node_Id;
5706 Op_Id : Entity_Id)
5707 is
5708 begin
5709 -- Only do analysis if the operator Comes_From_Source, since otherwise
5710 -- the operator was generated by the expander, and all such operators
5711 -- always refer to the operators in package Standard.
5712
5713 if Comes_From_Source (N) then
5714 declare
5715 F1 : constant Entity_Id := First_Formal (Op_Id);
5716 F2 : constant Entity_Id := Next_Formal (F1);
5717
5718 begin
5719 -- Verify that Op_Id is a visible binary function. Note that since
5720 -- we know Op_Id is overloaded, potentially use visible means use
5721 -- visible for sure (RM 9.4(11)).
5722
5723 if Ekind (Op_Id) = E_Function
5724 and then Present (F2)
5725 and then (Is_Immediately_Visible (Op_Id)
5726 or else Is_Potentially_Use_Visible (Op_Id))
5727 and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
5728 and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
5729 then
5730 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5731
8b4230c8
AC
5732 -- If the left operand is overloaded, indicate that the current
5733 -- type is a viable candidate. This is redundant in most cases,
5734 -- but for equality and comparison operators where the context
5735 -- does not impose a type on the operands, setting the proper
5736 -- type is necessary to avoid subsequent ambiguities during
5737 -- resolution, when both user-defined and predefined operators
5738 -- may be candidates.
7340e432
AC
5739
5740 if Is_Overloaded (Left_Opnd (N)) then
5741 Set_Etype (Left_Opnd (N), Etype (F1));
5742 end if;
5743
996ae0b0
RK
5744 if Debug_Flag_E then
5745 Write_Str ("user defined operator ");
5746 Write_Name (Chars (Op_Id));
5747 Write_Str (" on node ");
5748 Write_Int (Int (N));
5749 Write_Eol;
5750 end if;
5751 end if;
5752 end;
5753 end if;
5754 end Analyze_User_Defined_Binary_Op;
5755
5756 -----------------------------------
5757 -- Analyze_User_Defined_Unary_Op --
5758 -----------------------------------
5759
5760 procedure Analyze_User_Defined_Unary_Op
5761 (N : Node_Id;
5762 Op_Id : Entity_Id)
5763 is
5764 begin
5765 -- Only do analysis if the operator Comes_From_Source, since otherwise
5766 -- the operator was generated by the expander, and all such operators
5767 -- always refer to the operators in package Standard.
5768
5769 if Comes_From_Source (N) then
5770 declare
5771 F : constant Entity_Id := First_Formal (Op_Id);
5772
5773 begin
5774 -- Verify that Op_Id is a visible unary function. Note that since
5775 -- we know Op_Id is overloaded, potentially use visible means use
5776 -- visible for sure (RM 9.4(11)).
5777
5778 if Ekind (Op_Id) = E_Function
5779 and then No (Next_Formal (F))
5780 and then (Is_Immediately_Visible (Op_Id)
5781 or else Is_Potentially_Use_Visible (Op_Id))
5782 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
5783 then
5784 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5785 end if;
5786 end;
5787 end if;
5788 end Analyze_User_Defined_Unary_Op;
5789
5790 ---------------------------
5791 -- Check_Arithmetic_Pair --
5792 ---------------------------
5793
5794 procedure Check_Arithmetic_Pair
5795 (T1, T2 : Entity_Id;
5796 Op_Id : Entity_Id;
5797 N : Node_Id)
5798 is
401093c1 5799 Op_Name : constant Name_Id := Chars (Op_Id);
996ae0b0 5800
da709d08
AC
5801 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
5802 -- Check whether the fixed-point type Typ has a user-defined operator
5803 -- (multiplication or division) that should hide the corresponding
5804 -- predefined operator. Used to implement Ada 2005 AI-264, to make
5805 -- such operators more visible and therefore useful.
8b4230c8 5806 --
50cff367
GD
5807 -- If the name of the operation is an expanded name with prefix
5808 -- Standard, the predefined universal fixed operator is available,
5809 -- as specified by AI-420 (RM 4.5.5 (19.1/2)).
5810
996ae0b0
RK
5811 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
5812 -- Get specific type (i.e. non-universal type if there is one)
5813
da709d08
AC
5814 ------------------
5815 -- Has_Fixed_Op --
5816 ------------------
5817
5818 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
401093c1 5819 Bas : constant Entity_Id := Base_Type (Typ);
da709d08
AC
5820 Ent : Entity_Id;
5821 F1 : Entity_Id;
5822 F2 : Entity_Id;
5823
5824 begin
50cff367
GD
5825 -- If the universal_fixed operation is given explicitly the rule
5826 -- concerning primitive operations of the type do not apply.
5827
5828 if Nkind (N) = N_Function_Call
5829 and then Nkind (Name (N)) = N_Expanded_Name
5830 and then Entity (Prefix (Name (N))) = Standard_Standard
5831 then
5832 return False;
5833 end if;
5834
da709d08
AC
5835 -- The operation is treated as primitive if it is declared in the
5836 -- same scope as the type, and therefore on the same entity chain.
5837
5838 Ent := Next_Entity (Typ);
5839 while Present (Ent) loop
5840 if Chars (Ent) = Chars (Op) then
5841 F1 := First_Formal (Ent);
5842 F2 := Next_Formal (F1);
5843
5844 -- The operation counts as primitive if either operand or
401093c1
ES
5845 -- result are of the given base type, and both operands are
5846 -- fixed point types.
da709d08 5847
401093c1 5848 if (Base_Type (Etype (F1)) = Bas
da709d08
AC
5849 and then Is_Fixed_Point_Type (Etype (F2)))
5850
5851 or else
401093c1 5852 (Base_Type (Etype (F2)) = Bas
da709d08
AC
5853 and then Is_Fixed_Point_Type (Etype (F1)))
5854
5855 or else
401093c1 5856 (Base_Type (Etype (Ent)) = Bas
da709d08
AC
5857 and then Is_Fixed_Point_Type (Etype (F1))
5858 and then Is_Fixed_Point_Type (Etype (F2)))
5859 then
5860 return True;
5861 end if;
5862 end if;
5863
5864 Next_Entity (Ent);
5865 end loop;
5866
5867 return False;
5868 end Has_Fixed_Op;
5869
4c46b835
AC
5870 -------------------
5871 -- Specific_Type --
5872 -------------------
5873
996ae0b0
RK
5874 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
5875 begin
5876 if T1 = Universal_Integer or else T1 = Universal_Real then
5877 return Base_Type (T2);
5878 else
5879 return Base_Type (T1);
5880 end if;
5881 end Specific_Type;
5882
5883 -- Start of processing for Check_Arithmetic_Pair
5884
5885 begin
b69cd36a 5886 if Nam_In (Op_Name, Name_Op_Add, Name_Op_Subtract) then
996ae0b0
RK
5887 if Is_Numeric_Type (T1)
5888 and then Is_Numeric_Type (T2)
b4592168
GD
5889 and then (Covers (T1 => T1, T2 => T2)
5890 or else
5891 Covers (T1 => T2, T2 => T1))
996ae0b0
RK
5892 then
5893 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5894 end if;
5895
b69cd36a 5896 elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide) then
996ae0b0 5897 if Is_Fixed_Point_Type (T1)
b69cd36a 5898 and then (Is_Fixed_Point_Type (T2) or else T2 = Universal_Real)
996ae0b0 5899 then
fa54f4da
EB
5900 -- Add one interpretation with universal fixed result
5901
5902 if not Has_Fixed_Op (T1, Op_Id)
5903 or else Nkind (Parent (N)) = N_Type_Conversion
fbf5a39b 5904 then
996ae0b0
RK
5905 Add_One_Interp (N, Op_Id, Universal_Fixed);
5906 end if;
5907
5908 elsif Is_Fixed_Point_Type (T2)
996ae0b0 5909 and then T1 = Universal_Real
da709d08 5910 and then
401093c1 5911 (not Has_Fixed_Op (T1, Op_Id)
da709d08 5912 or else Nkind (Parent (N)) = N_Type_Conversion)
996ae0b0
RK
5913 then
5914 Add_One_Interp (N, Op_Id, Universal_Fixed);
5915
5916 elsif Is_Numeric_Type (T1)
5917 and then Is_Numeric_Type (T2)
b4592168
GD
5918 and then (Covers (T1 => T1, T2 => T2)
5919 or else
5920 Covers (T1 => T2, T2 => T1))
996ae0b0
RK
5921 then
5922 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5923
5924 elsif Is_Fixed_Point_Type (T1)
5925 and then (Base_Type (T2) = Base_Type (Standard_Integer)
b69cd36a 5926 or else T2 = Universal_Integer)
996ae0b0
RK
5927 then
5928 Add_One_Interp (N, Op_Id, T1);
5929
5930 elsif T2 = Universal_Real
5931 and then Base_Type (T1) = Base_Type (Standard_Integer)
5932 and then Op_Name = Name_Op_Multiply
5933 then
5934 Add_One_Interp (N, Op_Id, Any_Fixed);
5935
5936 elsif T1 = Universal_Real
5937 and then Base_Type (T2) = Base_Type (Standard_Integer)
5938 then
5939 Add_One_Interp (N, Op_Id, Any_Fixed);
5940
5941 elsif Is_Fixed_Point_Type (T2)
5942 and then (Base_Type (T1) = Base_Type (Standard_Integer)
b69cd36a 5943 or else T1 = Universal_Integer)
996ae0b0
RK
5944 and then Op_Name = Name_Op_Multiply
5945 then
5946 Add_One_Interp (N, Op_Id, T2);
5947
5948 elsif T1 = Universal_Real and then T2 = Universal_Integer then
5949 Add_One_Interp (N, Op_Id, T1);
5950
5951 elsif T2 = Universal_Real
5952 and then T1 = Universal_Integer
5953 and then Op_Name = Name_Op_Multiply
5954 then
5955 Add_One_Interp (N, Op_Id, T2);
5956 end if;
5957
5958 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
5959
996ae0b0 5960 if Is_Integer_Type (T1)
b4592168
GD
5961 and then (Covers (T1 => T1, T2 => T2)
5962 or else
5963 Covers (T1 => T2, T2 => T1))
996ae0b0
RK
5964 then
5965 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5966 end if;
5967
5968 elsif Op_Name = Name_Op_Expon then
996ae0b0
RK
5969 if Is_Numeric_Type (T1)
5970 and then not Is_Fixed_Point_Type (T1)
5971 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5972 or else T2 = Universal_Integer)
5973 then
5974 Add_One_Interp (N, Op_Id, Base_Type (T1));
5975 end if;
5976
5977 else pragma Assert (Nkind (N) in N_Op_Shift);
5978
5979 -- If not one of the predefined operators, the node may be one
5980 -- of the intrinsic functions. Its kind is always specific, and
5981 -- we can use it directly, rather than the name of the operation.
5982
5983 if Is_Integer_Type (T1)
5984 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5985 or else T2 = Universal_Integer)
5986 then
5987 Add_One_Interp (N, Op_Id, Base_Type (T1));
5988 end if;
5989 end if;
5990 end Check_Arithmetic_Pair;
5991
5992 -------------------------------
5993 -- Check_Misspelled_Selector --
5994 -------------------------------
5995
5996 procedure Check_Misspelled_Selector
5997 (Prefix : Entity_Id;
5998 Sel : Node_Id)
5999 is
6000 Max_Suggestions : constant := 2;
6001 Nr_Of_Suggestions : Natural := 0;
6002
6003 Suggestion_1 : Entity_Id := Empty;
6004 Suggestion_2 : Entity_Id := Empty;
6005
6006 Comp : Entity_Id;
6007
6008 begin
8b4230c8
AC
6009 -- All the components of the prefix of selector Sel are matched against
6010 -- Sel and a count is maintained of possible misspellings. When at
a90bd866 6011 -- the end of the analysis there are one or two (not more) possible
8b4230c8
AC
6012 -- misspellings, these misspellings will be suggested as possible
6013 -- correction.
996ae0b0 6014
4c46b835
AC
6015 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
6016
996ae0b0 6017 -- Concurrent types should be handled as well ???
4c46b835 6018
996ae0b0
RK
6019 return;
6020 end if;
6021
99859ea7 6022 Comp := First_Entity (Prefix);
d469eabe 6023 while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
24e95966 6024 if Is_Visible_Component (Comp, Sel) then
d469eabe
HK
6025 if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
6026 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
6027
6028 case Nr_Of_Suggestions is
6029 when 1 => Suggestion_1 := Comp;
6030 when 2 => Suggestion_2 := Comp;
e49de265 6031 when others => null;
d469eabe 6032 end case;
996ae0b0 6033 end if;
d469eabe 6034 end if;
996ae0b0 6035
99859ea7 6036 Next_Entity (Comp);
d469eabe 6037 end loop;
996ae0b0 6038
d469eabe 6039 -- Report at most two suggestions
996ae0b0 6040
d469eabe 6041 if Nr_Of_Suggestions = 1 then
4e7a4f6e 6042 Error_Msg_NE -- CODEFIX
d469eabe 6043 ("\possible misspelling of&", Sel, Suggestion_1);
996ae0b0 6044
d469eabe
HK
6045 elsif Nr_Of_Suggestions = 2 then
6046 Error_Msg_Node_2 := Suggestion_2;
4e7a4f6e 6047 Error_Msg_NE -- CODEFIX
d469eabe
HK
6048 ("\possible misspelling of& or&", Sel, Suggestion_1);
6049 end if;
996ae0b0
RK
6050 end Check_Misspelled_Selector;
6051
6052 ----------------------
6053 -- Defined_In_Scope --
6054 ----------------------
6055
6056 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
6057 is
6058 S1 : constant Entity_Id := Scope (Base_Type (T));
996ae0b0
RK
6059 begin
6060 return S1 = S
6061 or else (S1 = System_Aux_Id and then S = Scope (S1));
6062 end Defined_In_Scope;
6063
6064 -------------------
6065 -- Diagnose_Call --
6066 -------------------
6067
6068 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
fbf5a39b
AC
6069 Actual : Node_Id;
6070 X : Interp_Index;
6071 It : Interp;
fbf5a39b
AC
6072 Err_Mode : Boolean;
6073 New_Nam : Node_Id;
6074 Void_Interp_Seen : Boolean := False;
996ae0b0 6075
24657705
HK
6076 Success : Boolean;
6077 pragma Warnings (Off, Boolean);
6078
996ae0b0 6079 begin
0791fbe9 6080 if Ada_Version >= Ada_2005 then
996ae0b0 6081 Actual := First_Actual (N);
996ae0b0 6082 while Present (Actual) loop
0ab80019
AC
6083
6084 -- Ada 2005 (AI-50217): Post an error in case of premature
6085 -- usage of an entity from the limited view.
19f0526a 6086
996ae0b0 6087 if not Analyzed (Etype (Actual))
7b56a91b 6088 and then From_Limited_With (Etype (Actual))
996ae0b0
RK
6089 then
6090 Error_Msg_Qual_Level := 1;
ed2233dc 6091 Error_Msg_NE
996ae0b0
RK
6092 ("missing with_clause for scope of imported type&",
6093 Actual, Etype (Actual));
6094 Error_Msg_Qual_Level := 0;
6095 end if;
6096
6097 Next_Actual (Actual);
6098 end loop;
6099 end if;
6100
c63a2ad6
AC
6101 -- Before listing the possible candidates, check whether this is
6102 -- a prefix of a selected component that has been rewritten as a
6103 -- parameterless function call because there is a callable candidate
6104 -- interpretation. If there is a hidden package in the list of homonyms
6105 -- of the function name (bad programming style in any case) suggest that
6106 -- this is the intended entity.
40bf00b1
AC
6107
6108 if No (Parameter_Associations (N))
6109 and then Nkind (Parent (N)) = N_Selected_Component
6110 and then Nkind (Parent (Parent (N))) in N_Declaration
6111 and then Is_Overloaded (Nam)
6112 then
6113 declare
6114 Ent : Entity_Id;
6115
6116 begin
6117 Ent := Current_Entity (Nam);
6118 while Present (Ent) loop
6119 if Ekind (Ent) = E_Package then
6120 Error_Msg_N
6121 ("no legal interpretations as function call,!", Nam);
6122 Error_Msg_NE ("\package& is not visible", N, Ent);
c63a2ad6 6123
40bf00b1
AC
6124 Rewrite (Parent (N),
6125 New_Occurrence_Of (Any_Type, Sloc (N)));
6126 return;
6127 end if;
6128
6129 Ent := Homonym (Ent);
6130 end loop;
6131 end;
6132 end if;
6133
c63a2ad6
AC
6134 -- Analyze each candidate call again, with full error reporting for
6135 -- each.
fbf5a39b
AC
6136
6137 Error_Msg_N
6138 ("no candidate interpretations match the actuals:!", Nam);
6139 Err_Mode := All_Errors_Mode;
6140 All_Errors_Mode := True;
6141
6142 -- If this is a call to an operation of a concurrent type,
6143 -- the failed interpretations have been removed from the
6144 -- name. Recover them to provide full diagnostics.
6145
6146 if Nkind (Parent (Nam)) = N_Selected_Component then
6147 Set_Entity (Nam, Empty);
6148 New_Nam := New_Copy_Tree (Parent (Nam));
6149 Set_Is_Overloaded (New_Nam, False);
6150 Set_Is_Overloaded (Selector_Name (New_Nam), False);
6151 Set_Parent (New_Nam, Parent (Parent (Nam)));
6152 Analyze_Selected_Component (New_Nam);
6153 Get_First_Interp (Selector_Name (New_Nam), X, It);
6154 else
996ae0b0 6155 Get_First_Interp (Nam, X, It);
fbf5a39b 6156 end if;
996ae0b0 6157
fbf5a39b
AC
6158 while Present (It.Nam) loop
6159 if Etype (It.Nam) = Standard_Void_Type then
6160 Void_Interp_Seen := True;
996ae0b0 6161 end if;
fbf5a39b
AC
6162
6163 Analyze_One_Call (N, It.Nam, True, Success);
6164 Get_Next_Interp (X, It);
6165 end loop;
996ae0b0
RK
6166
6167 if Nkind (N) = N_Function_Call then
6168 Get_First_Interp (Nam, X, It);
996ae0b0 6169
a9e47028
ES
6170 if No (It.Typ)
6171 and then Ekind (Entity (Name (N))) = E_Function
6172 and then Present (Homonym (Entity (Name (N))))
6173 then
7f8c1cd3
HK
6174 -- A name may appear overloaded if it has a homonym, even if that
6175 -- homonym is non-overloadable, in which case the overload list is
6176 -- in fact empty. This specialized case deserves a special message
6177 -- if the homonym is a child package.
996ae0b0 6178
a9e47028
ES
6179 declare
6180 Nam : constant Node_Id := Name (N);
6181 H : constant Entity_Id := Homonym (Entity (Nam));
6182
6183 begin
7f8c1cd3 6184 if Ekind (H) = E_Package and then Is_Child_Unit (H) then
a9e47028
ES
6185 Error_Msg_Qual_Level := 2;
6186 Error_Msg_NE ("if an entity in package& is meant, ", Nam, H);
6187 Error_Msg_NE ("\use a fully qualified name", Nam, H);
6188 Error_Msg_Qual_Level := 0;
6189 end if;
6190 end;
6191
6192 else
6193 while Present (It.Nam) loop
6194 if Ekind_In (It.Nam, E_Function, E_Operator) then
6195 return;
6196 else
6197 Get_Next_Interp (X, It);
6198 end if;
6199 end loop;
6200
7f8c1cd3
HK
6201 -- If all interpretations are procedures, this deserves a more
6202 -- precise message. Ditto if this appears as the prefix of a
6203 -- selected component, which may be a lexical error.
a9e47028
ES
6204
6205 Error_Msg_N
6206 ("\context requires function call, found procedure name", Nam);
6207
6208 if Nkind (Parent (N)) = N_Selected_Component
6209 and then N = Prefix (Parent (N))
6210 then
6211 Error_Msg_N -- CODEFIX
6212 ("\period should probably be semicolon", Parent (N));
6213 end if;
996ae0b0 6214 end if;
fbf5a39b
AC
6215
6216 elsif Nkind (N) = N_Procedure_Call_Statement
6217 and then not Void_Interp_Seen
6218 then
7f8c1cd3 6219 Error_Msg_N ("\function name found in procedure call", Nam);
996ae0b0 6220 end if;
fbf5a39b
AC
6221
6222 All_Errors_Mode := Err_Mode;
996ae0b0
RK
6223 end Diagnose_Call;
6224
6225 ---------------------------
6226 -- Find_Arithmetic_Types --
6227 ---------------------------
6228
6229 procedure Find_Arithmetic_Types
6230 (L, R : Node_Id;
6231 Op_Id : Entity_Id;
6232 N : Node_Id)
6233 is
4c46b835
AC
6234 Index1 : Interp_Index;
6235 Index2 : Interp_Index;
6236 It1 : Interp;
6237 It2 : Interp;
996ae0b0
RK
6238
6239 procedure Check_Right_Argument (T : Entity_Id);
6240 -- Check right operand of operator
6241
4c46b835
AC
6242 --------------------------
6243 -- Check_Right_Argument --
6244 --------------------------
6245
996ae0b0
RK
6246 procedure Check_Right_Argument (T : Entity_Id) is
6247 begin
6248 if not Is_Overloaded (R) then
6249 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
6250 else
6251 Get_First_Interp (R, Index2, It2);
996ae0b0
RK
6252 while Present (It2.Typ) loop
6253 Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
6254 Get_Next_Interp (Index2, It2);
6255 end loop;
6256 end if;
6257 end Check_Right_Argument;
6258
d8221f45 6259 -- Start of processing for Find_Arithmetic_Types
996ae0b0
RK
6260
6261 begin
6262 if not Is_Overloaded (L) then
6263 Check_Right_Argument (Etype (L));
6264
6265 else
6266 Get_First_Interp (L, Index1, It1);
996ae0b0
RK
6267 while Present (It1.Typ) loop
6268 Check_Right_Argument (It1.Typ);
6269 Get_Next_Interp (Index1, It1);
6270 end loop;
6271 end if;
6272
6273 end Find_Arithmetic_Types;
6274
6275 ------------------------
6276 -- Find_Boolean_Types --
6277 ------------------------
6278
6279 procedure Find_Boolean_Types
6280 (L, R : Node_Id;
6281 Op_Id : Entity_Id;
6282 N : Node_Id)
6283 is
6284 Index : Interp_Index;
6285 It : Interp;
6286
6287 procedure Check_Numeric_Argument (T : Entity_Id);
6288 -- Special case for logical operations one of whose operands is an
6289 -- integer literal. If both are literal the result is any modular type.
6290
4c46b835
AC
6291 ----------------------------
6292 -- Check_Numeric_Argument --
6293 ----------------------------
6294
996ae0b0
RK
6295 procedure Check_Numeric_Argument (T : Entity_Id) is
6296 begin
6297 if T = Universal_Integer then
6298 Add_One_Interp (N, Op_Id, Any_Modular);
6299
6300 elsif Is_Modular_Integer_Type (T) then
6301 Add_One_Interp (N, Op_Id, T);
6302 end if;
6303 end Check_Numeric_Argument;
6304
6305 -- Start of processing for Find_Boolean_Types
6306
6307 begin
6308 if not Is_Overloaded (L) then
996ae0b0
RK
6309 if Etype (L) = Universal_Integer
6310 or else Etype (L) = Any_Modular
6311 then
6312 if not Is_Overloaded (R) then
6313 Check_Numeric_Argument (Etype (R));
6314
6315 else
6316 Get_First_Interp (R, Index, It);
996ae0b0
RK
6317 while Present (It.Typ) loop
6318 Check_Numeric_Argument (It.Typ);
996ae0b0
RK
6319 Get_Next_Interp (Index, It);
6320 end loop;
6321 end if;
6322
69e6a03e
ES
6323 -- If operands are aggregates, we must assume that they may be
6324 -- boolean arrays, and leave disambiguation for the second pass.
6325 -- If only one is an aggregate, verify that the other one has an
6326 -- interpretation as a boolean array
6327
6328 elsif Nkind (L) = N_Aggregate then
6329 if Nkind (R) = N_Aggregate then
6330 Add_One_Interp (N, Op_Id, Etype (L));
6331
6332 elsif not Is_Overloaded (R) then
6333 if Valid_Boolean_Arg (Etype (R)) then
6334 Add_One_Interp (N, Op_Id, Etype (R));
6335 end if;
6336
6337 else
6338 Get_First_Interp (R, Index, It);
6339 while Present (It.Typ) loop
6340 if Valid_Boolean_Arg (It.Typ) then
6341 Add_One_Interp (N, Op_Id, It.Typ);
6342 end if;
6343
6344 Get_Next_Interp (Index, It);
6345 end loop;
6346 end if;
6347
996ae0b0
RK
6348 elsif Valid_Boolean_Arg (Etype (L))
6349 and then Has_Compatible_Type (R, Etype (L))
6350 then
6351 Add_One_Interp (N, Op_Id, Etype (L));
6352 end if;
6353
6354 else
6355 Get_First_Interp (L, Index, It);
996ae0b0
RK
6356 while Present (It.Typ) loop
6357 if Valid_Boolean_Arg (It.Typ)
6358 and then Has_Compatible_Type (R, It.Typ)
6359 then
6360 Add_One_Interp (N, Op_Id, It.Typ);
6361 end if;
6362
6363 Get_Next_Interp (Index, It);
6364 end loop;
6365 end if;
6366 end Find_Boolean_Types;
6367
6368 ---------------------------
6369 -- Find_Comparison_Types --
6370 ---------------------------
6371
6372 procedure Find_Comparison_Types
6373 (L, R : Node_Id;
6374 Op_Id : Entity_Id;
6375 N : Node_Id)
6376 is
6377 Index : Interp_Index;
6378 It : Interp;
6379 Found : Boolean := False;
6380 I_F : Interp_Index;
6381 T_F : Entity_Id;
6382 Scop : Entity_Id := Empty;
6383
6384 procedure Try_One_Interp (T1 : Entity_Id);
6385 -- Routine to try one proposed interpretation. Note that the context
6386 -- of the operator plays no role in resolving the arguments, so that
6387 -- if there is more than one interpretation of the operands that is
6388 -- compatible with comparison, the operation is ambiguous.
6389
4c46b835
AC
6390 --------------------
6391 -- Try_One_Interp --
6392 --------------------
6393
996ae0b0
RK
6394 procedure Try_One_Interp (T1 : Entity_Id) is
6395 begin
996ae0b0
RK
6396 -- If the operator is an expanded name, then the type of the operand
6397 -- must be defined in the corresponding scope. If the type is
c468e1fb
AC
6398 -- universal, the context will impose the correct type. Note that we
6399 -- also avoid returning if we are currently within a generic instance
6400 -- due to the fact that the generic package declaration has already
6401 -- been successfully analyzed and Defined_In_Scope expects the base
6402 -- type to be defined within the instance which will never be the
6403 -- case.
996ae0b0
RK
6404
6405 if Present (Scop)
8b4230c8 6406 and then not Defined_In_Scope (T1, Scop)
c468e1fb 6407 and then not In_Instance
8b4230c8
AC
6408 and then T1 /= Universal_Integer
6409 and then T1 /= Universal_Real
6410 and then T1 /= Any_String
6411 and then T1 /= Any_Composite
996ae0b0
RK
6412 then
6413 return;
6414 end if;
6415
8b4230c8
AC
6416 if Valid_Comparison_Arg (T1) and then Has_Compatible_Type (R, T1) then
6417 if Found and then Base_Type (T1) /= Base_Type (T_F) then
996ae0b0
RK
6418 It := Disambiguate (L, I_F, Index, Any_Type);
6419
6420 if It = No_Interp then
6421 Ambiguous_Operands (N);
6422 Set_Etype (L, Any_Type);
6423 return;
6424
6425 else
6426 T_F := It.Typ;
6427 end if;
996ae0b0
RK
6428 else
6429 Found := True;
6430 T_F := T1;
6431 I_F := Index;
6432 end if;
6433
6434 Set_Etype (L, T_F);
6435 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
996ae0b0
RK
6436 end if;
6437 end Try_One_Interp;
6438
d8221f45 6439 -- Start of processing for Find_Comparison_Types
996ae0b0
RK
6440
6441 begin
fbf5a39b
AC
6442 -- If left operand is aggregate, the right operand has to
6443 -- provide a usable type for it.
6444
8b4230c8 6445 if Nkind (L) = N_Aggregate and then Nkind (R) /= N_Aggregate then
b4592168 6446 Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N);
fbf5a39b
AC
6447 return;
6448 end if;
996ae0b0
RK
6449
6450 if Nkind (N) = N_Function_Call
6451 and then Nkind (Name (N)) = N_Expanded_Name
6452 then
6453 Scop := Entity (Prefix (Name (N)));
6454
6455 -- The prefix may be a package renaming, and the subsequent test
6456 -- requires the original package.
6457
6458 if Ekind (Scop) = E_Package
6459 and then Present (Renamed_Entity (Scop))
6460 then
6461 Scop := Renamed_Entity (Scop);
6462 Set_Entity (Prefix (Name (N)), Scop);
6463 end if;
6464 end if;
6465
6466 if not Is_Overloaded (L) then
6467 Try_One_Interp (Etype (L));
6468
6469 else
6470 Get_First_Interp (L, Index, It);
996ae0b0
RK
6471 while Present (It.Typ) loop
6472 Try_One_Interp (It.Typ);
6473 Get_Next_Interp (Index, It);
6474 end loop;
6475 end if;
6476 end Find_Comparison_Types;
6477
6478 ----------------------------------------
6479 -- Find_Non_Universal_Interpretations --
6480 ----------------------------------------
6481
6482 procedure Find_Non_Universal_Interpretations
6483 (N : Node_Id;
6484 R : Node_Id;
6485 Op_Id : Entity_Id;
6486 T1 : Entity_Id)
6487 is
6488 Index : Interp_Index;
4c46b835 6489 It : Interp;
996ae0b0
RK
6490
6491 begin
8b4230c8 6492 if T1 = Universal_Integer or else T1 = Universal_Real
b9daa96e
AC
6493
6494 -- If the left operand of an equality operator is null, the visibility
6495 -- of the operator must be determined from the interpretation of the
6496 -- right operand. This processing must be done for Any_Access, which
6497 -- is the internal representation of the type of the literal null.
6498
be4c5193 6499 or else T1 = Any_Access
996ae0b0
RK
6500 then
6501 if not Is_Overloaded (R) then
8b4230c8 6502 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
996ae0b0
RK
6503 else
6504 Get_First_Interp (R, Index, It);
996ae0b0
RK
6505 while Present (It.Typ) loop
6506 if Covers (It.Typ, T1) then
6507 Add_One_Interp
6508 (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
6509 end if;
6510
6511 Get_Next_Interp (Index, It);
6512 end loop;
6513 end if;
6514 else
6515 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
6516 end if;
6517 end Find_Non_Universal_Interpretations;
6518
6519 ------------------------------
6520 -- Find_Concatenation_Types --
6521 ------------------------------
6522
6523 procedure Find_Concatenation_Types
6524 (L, R : Node_Id;
6525 Op_Id : Entity_Id;
6526 N : Node_Id)
6527 is
e201023c
PMR
6528 Is_String : constant Boolean := Nkind (L) = N_String_Literal
6529 or else
6530 Nkind (R) = N_String_Literal;
6531 Op_Type : constant Entity_Id := Etype (Op_Id);
996ae0b0
RK
6532
6533 begin
6534 if Is_Array_Type (Op_Type)
e201023c
PMR
6535
6536 -- Small but very effective optimization: if at least one operand is a
6537 -- string literal, then the type of the operator must be either array
6538 -- of characters or array of strings.
6539
6540 and then (not Is_String
6541 or else
6542 Is_Character_Type (Component_Type (Op_Type))
6543 or else
6544 Is_String_Type (Component_Type (Op_Type)))
6545
996ae0b0
RK
6546 and then not Is_Limited_Type (Op_Type)
6547
6548 and then (Has_Compatible_Type (L, Op_Type)
6549 or else
6550 Has_Compatible_Type (L, Component_Type (Op_Type)))
6551
6552 and then (Has_Compatible_Type (R, Op_Type)
6553 or else
6554 Has_Compatible_Type (R, Component_Type (Op_Type)))
6555 then
6556 Add_One_Interp (N, Op_Id, Op_Type);
6557 end if;
6558 end Find_Concatenation_Types;
6559
6560 -------------------------
6561 -- Find_Equality_Types --
6562 -------------------------
6563
6564 procedure Find_Equality_Types
6565 (L, R : Node_Id;
6566 Op_Id : Entity_Id;
6567 N : Node_Id)
6568 is
6569 Index : Interp_Index;
6570 It : Interp;
6571 Found : Boolean := False;
6572 I_F : Interp_Index;
6573 T_F : Entity_Id;
6574 Scop : Entity_Id := Empty;
6575
6576 procedure Try_One_Interp (T1 : Entity_Id);
a8e65aa5
AC
6577 -- The context of the equality operator plays no role in resolving the
6578 -- arguments, so that if there is more than one interpretation of the
6579 -- operands that is compatible with equality, the construct is ambiguous
6580 -- and an error can be emitted now, after trying to disambiguate, i.e.
6581 -- applying preference rules.
996ae0b0 6582
4c46b835
AC
6583 --------------------
6584 -- Try_One_Interp --
6585 --------------------
6586
996ae0b0 6587 procedure Try_One_Interp (T1 : Entity_Id) is
851e9f19 6588 Bas : Entity_Id;
a8e65aa5 6589
996ae0b0 6590 begin
851e9f19
PMR
6591 -- Perform a sanity check in case of previous errors
6592
6593 if No (T1) then
6594 return;
6595 end if;
6596
6597 Bas := Base_Type (T1);
6598
996ae0b0
RK
6599 -- If the operator is an expanded name, then the type of the operand
6600 -- must be defined in the corresponding scope. If the type is
6601 -- universal, the context will impose the correct type. An anonymous
6602 -- type for a 'Access reference is also universal in this sense, as
6603 -- the actual type is obtained from context.
8b4230c8 6604
fe45e59e
ES
6605 -- In Ada 2005, the equality operator for anonymous access types
6606 -- is declared in Standard, and preference rules apply to it.
996ae0b0 6607
fe45e59e 6608 if Present (Scop) then
c468e1fb
AC
6609
6610 -- Note that we avoid returning if we are currently within a
6611 -- generic instance due to the fact that the generic package
6612 -- declaration has already been successfully analyzed and
4ac62786
AC
6613 -- Defined_In_Scope expects the base type to be defined within
6614 -- the instance which will never be the case.
c468e1fb 6615
fe45e59e 6616 if Defined_In_Scope (T1, Scop)
c468e1fb 6617 or else In_Instance
fe45e59e
ES
6618 or else T1 = Universal_Integer
6619 or else T1 = Universal_Real
6620 or else T1 = Any_Access
6621 or else T1 = Any_String
6622 or else T1 = Any_Composite
6623 or else (Ekind (T1) = E_Access_Subprogram_Type
a8e65aa5 6624 and then not Comes_From_Source (T1))
fe45e59e
ES
6625 then
6626 null;
6627
6628 elsif Ekind (T1) = E_Anonymous_Access_Type
6629 and then Scop = Standard_Standard
6630 then
6631 null;
6632
6633 else
6634 -- The scope does not contain an operator for the type
6635
6636 return;
6637 end if;
a8e65aa5 6638
9aa04cc7
AC
6639 -- If we have infix notation, the operator must be usable. Within
6640 -- an instance, if the type is already established we know it is
6641 -- correct. If an operand is universal it is compatible with any
6642 -- numeric type.
31af8899 6643
a8e65aa5
AC
6644 elsif In_Open_Scopes (Scope (Bas))
6645 or else Is_Potentially_Use_Visible (Bas)
6646 or else In_Use (Bas)
9aa04cc7 6647 or else (In_Use (Scope (Bas)) and then not Is_Hidden (Bas))
a54ffd6c
AC
6648
6649 -- In an instance, the type may have been immediately visible.
6650 -- Either the types are compatible, or one operand is universal
6651 -- (numeric or null).
6652
c5b4738f
AC
6653 or else
6654 ((In_Instance or else In_Inlined_Body)
6655 and then
6656 (First_Subtype (T1) = First_Subtype (Etype (R))
6657 or else Nkind (R) = N_Null
6658 or else
6659 (Is_Numeric_Type (T1)
6660 and then Is_Universal_Numeric_Type (Etype (R)))))
a54ffd6c
AC
6661
6662 -- In Ada 2005, the equality on anonymous access types is declared
6663 -- in Standard, and is always visible.
6664
a8e65aa5
AC
6665 or else Ekind (T1) = E_Anonymous_Access_Type
6666 then
6667 null;
6668
6669 else
308e6f3a 6670 -- Save candidate type for subsequent error message, if any
a8e65aa5
AC
6671
6672 if not Is_Limited_Type (T1) then
6673 Candidate_Type := T1;
6674 end if;
6675
6676 return;
996ae0b0
RK
6677 end if;
6678
0ab80019
AC
6679 -- Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
6680 -- Do not allow anonymous access types in equality operators.
6e937c1c 6681
0791fbe9 6682 if Ada_Version < Ada_2005
6e937c1c
AC
6683 and then Ekind (T1) = E_Anonymous_Access_Type
6684 then
6685 return;
6686 end if;
6687
c92e8586
AC
6688 -- If the right operand has a type compatible with T1, check for an
6689 -- acceptable interpretation, unless T1 is limited (no predefined
6690 -- equality available), or this is use of a "/=" for a tagged type.
8b4230c8
AC
6691 -- In the latter case, possible interpretations of equality need
6692 -- to be considered, we don't want the default inequality declared
6693 -- in Standard to be chosen, and the "/=" will be rewritten as a
c92e8586 6694 -- negation of "=" (see the end of Analyze_Equality_Op). This ensures
026c3cfd 6695 -- that rewriting happens during analysis rather than being
65f1ca2e
AC
6696 -- delayed until expansion (is this still needed now that ASIS mode
6697 -- is gone???). Note that if the node is N_Op_Ne, but Op_Id
c92e8586
AC
6698 -- is Name_Op_Eq then we still proceed with the interpretation,
6699 -- because that indicates the potential rewriting case where the
6700 -- interpretation to consider is actually "=" and the node may be
6701 -- about to be rewritten by Analyze_Equality_Op.
6702
996ae0b0 6703 if T1 /= Standard_Void_Type
996ae0b0 6704 and then Has_Compatible_Type (R, T1)
c92e8586 6705
9b62eb32
AC
6706 and then
6707 ((not Is_Limited_Type (T1)
6708 and then not Is_Limited_Composite (T1))
6709
6710 or else
2fcc44fa 6711 (Is_Array_Type (T1)
9b62eb32
AC
6712 and then not Is_Limited_Type (Component_Type (T1))
6713 and then Available_Full_View_Of_Component (T1)))
c92e8586
AC
6714
6715 and then
6716 (Nkind (N) /= N_Op_Ne
6717 or else not Is_Tagged_Type (T1)
6718 or else Chars (Op_Id) = Name_Op_Eq)
996ae0b0
RK
6719 then
6720 if Found
6721 and then Base_Type (T1) /= Base_Type (T_F)
6722 then
6723 It := Disambiguate (L, I_F, Index, Any_Type);
6724
6725 if It = No_Interp then
6726 Ambiguous_Operands (N);
6727 Set_Etype (L, Any_Type);
6728 return;
6729
6730 else
6731 T_F := It.Typ;
6732 end if;
6733
6734 else
6735 Found := True;
6736 T_F := T1;
6737 I_F := Index;
6738 end if;
6739
6740 if not Analyzed (L) then
6741 Set_Etype (L, T_F);
6742 end if;
6743
6744 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
6745
6e73e3ab 6746 -- Case of operator was not visible, Etype still set to Any_Type
996ae0b0 6747
6e73e3ab 6748 if Etype (N) = Any_Type then
996ae0b0
RK
6749 Found := False;
6750 end if;
fe45e59e
ES
6751
6752 elsif Scop = Standard_Standard
6753 and then Ekind (T1) = E_Anonymous_Access_Type
6754 then
6755 Found := True;
996ae0b0
RK
6756 end if;
6757 end Try_One_Interp;
6758
6759 -- Start of processing for Find_Equality_Types
6760
6761 begin
fbf5a39b
AC
6762 -- If left operand is aggregate, the right operand has to
6763 -- provide a usable type for it.
6764
6765 if Nkind (L) = N_Aggregate
6766 and then Nkind (R) /= N_Aggregate
6767 then
b4592168 6768 Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N);
fbf5a39b
AC
6769 return;
6770 end if;
996ae0b0
RK
6771
6772 if Nkind (N) = N_Function_Call
6773 and then Nkind (Name (N)) = N_Expanded_Name
6774 then
6775 Scop := Entity (Prefix (Name (N)));
6776
6777 -- The prefix may be a package renaming, and the subsequent test
6778 -- requires the original package.
6779
6780 if Ekind (Scop) = E_Package
6781 and then Present (Renamed_Entity (Scop))
6782 then
6783 Scop := Renamed_Entity (Scop);
6784 Set_Entity (Prefix (Name (N)), Scop);
6785 end if;
6786 end if;
6787
6788 if not Is_Overloaded (L) then
6789 Try_One_Interp (Etype (L));
996ae0b0 6790
4c46b835 6791 else
996ae0b0 6792 Get_First_Interp (L, Index, It);
996ae0b0
RK
6793 while Present (It.Typ) loop
6794 Try_One_Interp (It.Typ);
6795 Get_Next_Interp (Index, It);
6796 end loop;
6797 end if;
6798 end Find_Equality_Types;
6799
6800 -------------------------
6801 -- Find_Negation_Types --
6802 -------------------------
6803
6804 procedure Find_Negation_Types
6805 (R : Node_Id;
6806 Op_Id : Entity_Id;
6807 N : Node_Id)
6808 is
6809 Index : Interp_Index;
6810 It : Interp;
6811
6812 begin
6813 if not Is_Overloaded (R) then
996ae0b0
RK
6814 if Etype (R) = Universal_Integer then
6815 Add_One_Interp (N, Op_Id, Any_Modular);
996ae0b0
RK
6816 elsif Valid_Boolean_Arg (Etype (R)) then
6817 Add_One_Interp (N, Op_Id, Etype (R));
6818 end if;
6819
6820 else
6821 Get_First_Interp (R, Index, It);
996ae0b0
RK
6822 while Present (It.Typ) loop
6823 if Valid_Boolean_Arg (It.Typ) then
6824 Add_One_Interp (N, Op_Id, It.Typ);
6825 end if;
6826
6827 Get_Next_Interp (Index, It);
6828 end loop;
6829 end if;
6830 end Find_Negation_Types;
6831
d469eabe
HK
6832 ------------------------------
6833 -- Find_Primitive_Operation --
6834 ------------------------------
6835
6836 function Find_Primitive_Operation (N : Node_Id) return Boolean is
6837 Obj : constant Node_Id := Prefix (N);
6838 Op : constant Node_Id := Selector_Name (N);
6839
6840 Prim : Elmt_Id;
6841 Prims : Elist_Id;
6842 Typ : Entity_Id;
6843
6844 begin
6845 Set_Etype (Op, Any_Type);
6846
6847 if Is_Access_Type (Etype (Obj)) then
6848 Typ := Designated_Type (Etype (Obj));
6849 else
6850 Typ := Etype (Obj);
6851 end if;
6852
6853 if Is_Class_Wide_Type (Typ) then
6854 Typ := Root_Type (Typ);
6855 end if;
6856
6857 Prims := Primitive_Operations (Typ);
6858
6859 Prim := First_Elmt (Prims);
6860 while Present (Prim) loop
6861 if Chars (Node (Prim)) = Chars (Op) then
6862 Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
6863 Set_Etype (N, Etype (Node (Prim)));
6864 end if;
6865
6866 Next_Elmt (Prim);
6867 end loop;
6868
6869 -- Now look for class-wide operations of the type or any of its
6870 -- ancestors by iterating over the homonyms of the selector.
6871
6872 declare
6873 Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
6874 Hom : Entity_Id;
6875
6876 begin
6877 Hom := Current_Entity (Op);
6878 while Present (Hom) loop
6879 if (Ekind (Hom) = E_Procedure
6880 or else
6881 Ekind (Hom) = E_Function)
6882 and then Scope (Hom) = Scope (Typ)
6883 and then Present (First_Formal (Hom))
6884 and then
6885 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
6886 or else
6887 (Is_Access_Type (Etype (First_Formal (Hom)))
8b4230c8
AC
6888 and then
6889 Ekind (Etype (First_Formal (Hom))) =
6890 E_Anonymous_Access_Type
6891 and then
6892 Base_Type
6893 (Designated_Type (Etype (First_Formal (Hom)))) =
d469eabe
HK
6894 Cls_Type))
6895 then
6896 Add_One_Interp (Op, Hom, Etype (Hom));
6897 Set_Etype (N, Etype (Hom));
6898 end if;
6899
6900 Hom := Homonym (Hom);
6901 end loop;
6902 end;
6903
6904 return Etype (Op) /= Any_Type;
6905 end Find_Primitive_Operation;
6906
996ae0b0
RK
6907 ----------------------
6908 -- Find_Unary_Types --
6909 ----------------------
6910
6911 procedure Find_Unary_Types
6912 (R : Node_Id;
6913 Op_Id : Entity_Id;
6914 N : Node_Id)
6915 is
6916 Index : Interp_Index;
6917 It : Interp;
6918
6919 begin
6920 if not Is_Overloaded (R) then
6921 if Is_Numeric_Type (Etype (R)) then
65f7ed64
AC
6922
6923 -- In an instance a generic actual may be a numeric type even if
6924 -- the formal in the generic unit was not. In that case, the
6925 -- predefined operator was not a possible interpretation in the
2e70d415
AC
6926 -- generic, and cannot be one in the instance, unless the operator
6927 -- is an actual of an instance.
65f7ed64
AC
6928
6929 if In_Instance
6930 and then
6931 not Is_Numeric_Type (Corresponding_Generic_Type (Etype (R)))
6932 then
6933 null;
6934 else
6935 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
6936 end if;
996ae0b0
RK
6937 end if;
6938
6939 else
6940 Get_First_Interp (R, Index, It);
996ae0b0
RK
6941 while Present (It.Typ) loop
6942 if Is_Numeric_Type (It.Typ) then
65f7ed64
AC
6943 if In_Instance
6944 and then
6945 not Is_Numeric_Type
6946 (Corresponding_Generic_Type (Etype (It.Typ)))
6947 then
6948 null;
6949
6950 else
6951 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
6952 end if;
996ae0b0
RK
6953 end if;
6954
6955 Get_Next_Interp (Index, It);
6956 end loop;
6957 end if;
6958 end Find_Unary_Types;
6959
996ae0b0
RK
6960 ------------------
6961 -- Junk_Operand --
6962 ------------------
6963
6964 function Junk_Operand (N : Node_Id) return Boolean is
6965 Enode : Node_Id;
6966
6967 begin
6968 if Error_Posted (N) then
6969 return False;
6970 end if;
6971
6972 -- Get entity to be tested
6973
6974 if Is_Entity_Name (N)
6975 and then Present (Entity (N))
6976 then
6977 Enode := N;
6978
6979 -- An odd case, a procedure name gets converted to a very peculiar
6980 -- function call, and here is where we detect this happening.
6981
6982 elsif Nkind (N) = N_Function_Call
6983 and then Is_Entity_Name (Name (N))
6984 and then Present (Entity (Name (N)))
6985 then
6986 Enode := Name (N);
6987
6988 -- Another odd case, there are at least some cases of selected
6989 -- components where the selected component is not marked as having
6990 -- an entity, even though the selector does have an entity
6991
6992 elsif Nkind (N) = N_Selected_Component
6993 and then Present (Entity (Selector_Name (N)))
6994 then
6995 Enode := Selector_Name (N);
6996
6997 else
6998 return False;
6999 end if;
7000
9de61fcb 7001 -- Now test the entity we got to see if it is a bad case
996ae0b0
RK
7002
7003 case Ekind (Entity (Enode)) is
996ae0b0
RK
7004 when E_Package =>
7005 Error_Msg_N
7006 ("package name cannot be used as operand", Enode);
7007
7008 when Generic_Unit_Kind =>
7009 Error_Msg_N
7010 ("generic unit name cannot be used as operand", Enode);
7011
7012 when Type_Kind =>
7013 Error_Msg_N
7014 ("subtype name cannot be used as operand", Enode);
7015
7016 when Entry_Kind =>
7017 Error_Msg_N
7018 ("entry name cannot be used as operand", Enode);
7019
7020 when E_Procedure =>
7021 Error_Msg_N
7022 ("procedure name cannot be used as operand", Enode);
7023
7024 when E_Exception =>
7025 Error_Msg_N
7026 ("exception name cannot be used as operand", Enode);
7027
d8f43ee6
HK
7028 when E_Block
7029 | E_Label
7030 | E_Loop
7031 =>
996ae0b0
RK
7032 Error_Msg_N
7033 ("label name cannot be used as operand", Enode);
7034
7035 when others =>
7036 return False;
996ae0b0
RK
7037 end case;
7038
7039 return True;
7040 end Junk_Operand;
7041
7042 --------------------
7043 -- Operator_Check --
7044 --------------------
7045
7046 procedure Operator_Check (N : Node_Id) is
7047 begin
30c20106
AC
7048 Remove_Abstract_Operations (N);
7049
996ae0b0
RK
7050 -- Test for case of no interpretation found for operator
7051
7052 if Etype (N) = Any_Type then
7053 declare
b67a385c
ES
7054 L : Node_Id;
7055 R : Node_Id;
7056 Op_Id : Entity_Id := Empty;
996ae0b0
RK
7057
7058 begin
7059 R := Right_Opnd (N);
7060
7061 if Nkind (N) in N_Binary_Op then
7062 L := Left_Opnd (N);
7063 else
7064 L := Empty;
7065 end if;
7066
7067 -- If either operand has no type, then don't complain further,
9de61fcb 7068 -- since this simply means that we have a propagated error.
996ae0b0
RK
7069
7070 if R = Error
7071 or else Etype (R) = Any_Type
7072 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
7073 then
7610fee8
AC
7074 -- For the rather unusual case where one of the operands is
7075 -- a Raise_Expression, whose initial type is Any_Type, use
7076 -- the type of the other operand.
7077
7078 if Nkind (L) = N_Raise_Expression then
7079 Set_Etype (L, Etype (R));
7080 Set_Etype (N, Etype (R));
7081
7082 elsif Nkind (R) = N_Raise_Expression then
7083 Set_Etype (R, Etype (L));
7084 Set_Etype (N, Etype (L));
7085 end if;
7086
996ae0b0
RK
7087 return;
7088
4c46b835
AC
7089 -- We explicitly check for the case of concatenation of component
7090 -- with component to avoid reporting spurious matching array types
7091 -- that might happen to be lurking in distant packages (such as
7092 -- run-time packages). This also prevents inconsistencies in the
7093 -- messages for certain ACVC B tests, which can vary depending on
7094 -- types declared in run-time interfaces. Another improvement when
7095 -- aggregates are present is to look for a well-typed operand.
996ae0b0
RK
7096
7097 elsif Present (Candidate_Type)
7098 and then (Nkind (N) /= N_Op_Concat
7099 or else Is_Array_Type (Etype (L))
7100 or else Is_Array_Type (Etype (R)))
7101 then
996ae0b0
RK
7102 if Nkind (N) = N_Op_Concat then
7103 if Etype (L) /= Any_Composite
7104 and then Is_Array_Type (Etype (L))
7105 then
7106 Candidate_Type := Etype (L);
7107
7108 elsif Etype (R) /= Any_Composite
7109 and then Is_Array_Type (Etype (R))
7110 then
7111 Candidate_Type := Etype (R);
7112 end if;
7113 end if;
7114
19d846a0 7115 Error_Msg_NE -- CODEFIX
996ae0b0
RK
7116 ("operator for} is not directly visible!",
7117 N, First_Subtype (Candidate_Type));
4561baf7
ES
7118
7119 declare
7120 U : constant Node_Id :=
7121 Cunit (Get_Source_Unit (Candidate_Type));
4561baf7
ES
7122 begin
7123 if Unit_Is_Visible (U) then
7124 Error_Msg_N -- CODEFIX
7125 ("use clause would make operation legal!", N);
4561baf7
ES
7126 else
7127 Error_Msg_NE -- CODEFIX
7128 ("add with_clause and use_clause for&!",
8b4230c8 7129 N, Defining_Entity (Unit (U)));
4561baf7
ES
7130 end if;
7131 end;
996ae0b0
RK
7132 return;
7133
7134 -- If either operand is a junk operand (e.g. package name), then
7135 -- post appropriate error messages, but do not complain further.
7136
0e0eecec
ES
7137 -- Note that the use of OR in this test instead of OR ELSE is
7138 -- quite deliberate, we may as well check both operands in the
7139 -- binary operator case.
996ae0b0
RK
7140
7141 elsif Junk_Operand (R)
9559eccf
AC
7142 or -- really mean OR here and not OR ELSE, see above
7143 (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
996ae0b0
RK
7144 then
7145 return;
7146
7147 -- If we have a logical operator, one of whose operands is
0e0eecec
ES
7148 -- Boolean, then we know that the other operand cannot resolve to
7149 -- Boolean (since we got no interpretations), but in that case we
7150 -- pretty much know that the other operand should be Boolean, so
070d862d 7151 -- resolve it that way (generating an error).
996ae0b0 7152
d469eabe 7153 elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then
996ae0b0
RK
7154 if Etype (L) = Standard_Boolean then
7155 Resolve (R, Standard_Boolean);
7156 return;
7157 elsif Etype (R) = Standard_Boolean then
7158 Resolve (L, Standard_Boolean);
7159 return;
7160 end if;
7161
7162 -- For an arithmetic operator or comparison operator, if one
7163 -- of the operands is numeric, then we know the other operand
7164 -- is not the same numeric type. If it is a non-numeric type,
7165 -- then probably it is intended to match the other operand.
7166
d469eabe
HK
7167 elsif Nkind_In (N, N_Op_Add,
7168 N_Op_Divide,
7169 N_Op_Ge,
7170 N_Op_Gt,
7171 N_Op_Le)
7172 or else
7173 Nkind_In (N, N_Op_Lt,
7174 N_Op_Mod,
7175 N_Op_Multiply,
7176 N_Op_Rem,
7177 N_Op_Subtract)
996ae0b0 7178 then
7dbd3de9
RD
7179 -- If Allow_Integer_Address is active, check whether the
7180 -- operation becomes legal after converting an operand.
7181
996ae0b0
RK
7182 if Is_Numeric_Type (Etype (L))
7183 and then not Is_Numeric_Type (Etype (R))
7184 then
7dbd3de9
RD
7185 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
7186 Rewrite (R,
7187 Unchecked_Convert_To (Etype (L), Relocate_Node (R)));
7dbd3de9 7188
1e3689bd
AC
7189 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7190 Analyze_Comparison_Op (N);
7191 else
7192 Analyze_Arithmetic_Op (N);
7193 end if;
7dbd3de9
RD
7194 else
7195 Resolve (R, Etype (L));
7196 end if;
1e3689bd 7197
996ae0b0
RK
7198 return;
7199
7200 elsif Is_Numeric_Type (Etype (R))
7201 and then not Is_Numeric_Type (Etype (L))
7202 then
7dbd3de9
RD
7203 if Address_Integer_Convert_OK (Etype (L), Etype (R)) then
7204 Rewrite (L,
7205 Unchecked_Convert_To (Etype (R), Relocate_Node (L)));
1e3689bd
AC
7206
7207 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7208 Analyze_Comparison_Op (N);
7209 else
7210 Analyze_Arithmetic_Op (N);
7211 end if;
7212
9559eccf 7213 return;
7dbd3de9
RD
7214
7215 else
7216 Resolve (L, Etype (R));
7217 end if;
9559eccf 7218
996ae0b0 7219 return;
9559eccf
AC
7220
7221 elsif Allow_Integer_Address
d9d25d04
AC
7222 and then Is_Descendant_Of_Address (Etype (L))
7223 and then Is_Descendant_Of_Address (Etype (R))
9559eccf
AC
7224 and then not Error_Posted (N)
7225 then
7226 declare
7227 Addr_Type : constant Entity_Id := Etype (L);
7228
7229 begin
7230 Rewrite (L,
7231 Unchecked_Convert_To (
7232 Standard_Integer, Relocate_Node (L)));
7233 Rewrite (R,
7234 Unchecked_Convert_To (
7235 Standard_Integer, Relocate_Node (R)));
1e3689bd
AC
7236
7237 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7238 Analyze_Comparison_Op (N);
7239 else
7240 Analyze_Arithmetic_Op (N);
7241 end if;
9559eccf
AC
7242
7243 -- If this is an operand in an enclosing arithmetic
7244 -- operation, Convert the result as an address so that
7245 -- arithmetic folding of address can continue.
7246
7247 if Nkind (Parent (N)) in N_Op then
7248 Rewrite (N,
7249 Unchecked_Convert_To (Addr_Type, Relocate_Node (N)));
7250 end if;
7251
7252 return;
7253 end;
a8a42b93
AC
7254
7255 -- Under relaxed RM semantics silently replace occurrences of
7256 -- null by System.Address_Null.
7257
7258 elsif Null_To_Null_Address_Convert_OK (N) then
7259 Replace_Null_By_Null_Address (N);
7260
7261 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7262 Analyze_Comparison_Op (N);
7263 else
7264 Analyze_Arithmetic_Op (N);
7265 end if;
7266
7267 return;
996ae0b0
RK
7268 end if;
7269
7270 -- Comparisons on A'Access are common enough to deserve a
7271 -- special message.
7272
d469eabe 7273 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne)
996ae0b0
RK
7274 and then Ekind (Etype (L)) = E_Access_Attribute_Type
7275 and then Ekind (Etype (R)) = E_Access_Attribute_Type
7276 then
7277 Error_Msg_N
7278 ("two access attributes cannot be compared directly", N);
7279 Error_Msg_N
aab883ec 7280 ("\use qualified expression for one of the operands",
996ae0b0
RK
7281 N);
7282 return;
7283
7284 -- Another one for C programmers
7285
7286 elsif Nkind (N) = N_Op_Concat
7287 and then Valid_Boolean_Arg (Etype (L))
7288 and then Valid_Boolean_Arg (Etype (R))
7289 then
7290 Error_Msg_N ("invalid operands for concatenation", N);
4e7a4f6e
AC
7291 Error_Msg_N -- CODEFIX
7292 ("\maybe AND was meant", N);
996ae0b0
RK
7293 return;
7294
7295 -- A special case for comparison of access parameter with null
7296
7297 elsif Nkind (N) = N_Op_Eq
7298 and then Is_Entity_Name (L)
7299 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
7300 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
7301 N_Access_Definition
7302 and then Nkind (R) = N_Null
7303 then
7304 Error_Msg_N ("access parameter is not allowed to be null", L);
7305 Error_Msg_N ("\(call would raise Constraint_Error)", L);
7306 return;
61bee0e3
AC
7307
7308 -- Another special case for exponentiation, where the right
7309 -- operand must be Natural, independently of the base.
7310
7311 elsif Nkind (N) = N_Op_Expon
7312 and then Is_Numeric_Type (Etype (L))
7313 and then not Is_Overloaded (R)
7314 and then
7315 First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
7316 and then Base_Type (Etype (R)) /= Universal_Integer
7317 then
dec6faf1 7318 if Ada_Version >= Ada_2012
15954beb 7319 and then Has_Dimension_System (Etype (L))
dec6faf1
AC
7320 then
7321 Error_Msg_NE
54c04d6c 7322 ("exponent for dimensioned type must be a rational" &
dec6faf1
AC
7323 ", found}", R, Etype (R));
7324 else
7325 Error_Msg_NE
7326 ("exponent must be of type Natural, found}", R, Etype (R));
7327 end if;
54c04d6c 7328
61bee0e3 7329 return;
11261647
AC
7330
7331 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
7332 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
7333 Rewrite (R,
7334 Unchecked_Convert_To (Etype (L), Relocate_Node (R)));
7335 Analyze_Equality_Op (N);
7336 return;
a8a42b93
AC
7337
7338 -- Under relaxed RM semantics silently replace occurrences of
7339 -- null by System.Address_Null.
7340
7341 elsif Null_To_Null_Address_Convert_OK (N) then
7342 Replace_Null_By_Null_Address (N);
7343 Analyze_Equality_Op (N);
7344 return;
11261647 7345 end if;
996ae0b0
RK
7346 end if;
7347
0e0eecec
ES
7348 -- If we fall through then just give general message. Note that in
7349 -- the following messages, if the operand is overloaded we choose
7350 -- an arbitrary type to complain about, but that is probably more
7351 -- useful than not giving a type at all.
996ae0b0
RK
7352
7353 if Nkind (N) in N_Unary_Op then
7354 Error_Msg_Node_2 := Etype (R);
7355 Error_Msg_N ("operator& not defined for}", N);
7356 return;
7357
7358 else
fbf5a39b
AC
7359 if Nkind (N) in N_Binary_Op then
7360 if not Is_Overloaded (L)
7361 and then not Is_Overloaded (R)
7362 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
7363 then
7ffd9312 7364 Error_Msg_Node_2 := First_Subtype (Etype (R));
fbf5a39b 7365 Error_Msg_N ("there is no applicable operator& for}", N);
996ae0b0 7366
fbf5a39b 7367 else
b67a385c
ES
7368 -- Another attempt to find a fix: one of the candidate
7369 -- interpretations may not be use-visible. This has
7370 -- already been checked for predefined operators, so
7371 -- we examine only user-defined functions.
7372
7373 Op_Id := Get_Name_Entity_Id (Chars (N));
7374
7375 while Present (Op_Id) loop
7376 if Ekind (Op_Id) /= E_Operator
7377 and then Is_Overloadable (Op_Id)
7378 then
7379 if not Is_Immediately_Visible (Op_Id)
7380 and then not In_Use (Scope (Op_Id))
aab883ec 7381 and then not Is_Abstract_Subprogram (Op_Id)
b67a385c
ES
7382 and then not Is_Hidden (Op_Id)
7383 and then Ekind (Scope (Op_Id)) = E_Package
7384 and then
7385 Has_Compatible_Type
7386 (L, Etype (First_Formal (Op_Id)))
7387 and then Present
7388 (Next_Formal (First_Formal (Op_Id)))
7389 and then
7390 Has_Compatible_Type
7391 (R,
7392 Etype (Next_Formal (First_Formal (Op_Id))))
7393 then
ed2233dc 7394 Error_Msg_N
1d0b1439 7395 ("no legal interpretation for operator&", N);
ed2233dc 7396 Error_Msg_NE
b67a385c 7397 ("\use clause on& would make operation legal",
8b4230c8 7398 N, Scope (Op_Id));
b67a385c
ES
7399 exit;
7400 end if;
7401 end if;
fbf5a39b 7402
b67a385c
ES
7403 Op_Id := Homonym (Op_Id);
7404 end loop;
7405
7406 if No (Op_Id) then
7407 Error_Msg_N ("invalid operand types for operator&", N);
7408
7409 if Nkind (N) /= N_Op_Concat then
7410 Error_Msg_NE ("\left operand has}!", N, Etype (L));
7411 Error_Msg_NE ("\right operand has}!", N, Etype (R));
2e70d415 7412
1d0b1439
YM
7413 -- For multiplication and division operators with
7414 -- a fixed-point operand and an integer operand,
7415 -- indicate that the integer operand should be of
7416 -- type Integer.
7417
7418 if Nkind_In (N, N_Op_Multiply, N_Op_Divide)
7419 and then Is_Fixed_Point_Type (Etype (L))
7420 and then Is_Integer_Type (Etype (R))
7421 then
6578a6bf
HK
7422 Error_Msg_N
7423 ("\convert right operand to `Integer`", N);
1d0b1439
YM
7424
7425 elsif Nkind (N) = N_Op_Multiply
7426 and then Is_Fixed_Point_Type (Etype (R))
7427 and then Is_Integer_Type (Etype (L))
7428 then
6578a6bf
HK
7429 Error_Msg_N
7430 ("\convert left operand to `Integer`", N);
1d0b1439
YM
7431 end if;
7432
2e70d415
AC
7433 -- For concatenation operators it is more difficult to
7434 -- determine which is the wrong operand. It is worth
7435 -- flagging explicitly an access type, for those who
7436 -- might think that a dereference happens here.
7437
7438 elsif Is_Access_Type (Etype (L)) then
7439 Error_Msg_N ("\left operand is access type", N);
7440
7441 elsif Is_Access_Type (Etype (R)) then
7442 Error_Msg_N ("\right operand is access type", N);
b67a385c 7443 end if;
fbf5a39b
AC
7444 end if;
7445 end if;
996ae0b0
RK
7446 end if;
7447 end if;
7448 end;
7449 end if;
7450 end Operator_Check;
7451
6e73e3ab
AC
7452 -----------------------------------------
7453 -- Process_Implicit_Dereference_Prefix --
7454 -----------------------------------------
7455
d469eabe 7456 function Process_Implicit_Dereference_Prefix
da709d08 7457 (E : Entity_Id;
d469eabe 7458 P : Entity_Id) return Entity_Id
6e73e3ab
AC
7459 is
7460 Ref : Node_Id;
d469eabe 7461 Typ : constant Entity_Id := Designated_Type (Etype (P));
da709d08 7462
6e73e3ab 7463 begin
1a8fae99
ES
7464 if Present (E)
7465 and then (Operating_Mode = Check_Semantics or else not Expander_Active)
7466 then
8b4230c8
AC
7467 -- We create a dummy reference to E to ensure that the reference is
7468 -- not considered as part of an assignment (an implicit dereference
7469 -- can never assign to its prefix). The Comes_From_Source attribute
7470 -- needs to be propagated for accurate warnings.
6e73e3ab 7471
e4494292 7472 Ref := New_Occurrence_Of (E, Sloc (P));
6e73e3ab
AC
7473 Set_Comes_From_Source (Ref, Comes_From_Source (P));
7474 Generate_Reference (E, Ref);
7475 end if;
d469eabe 7476
8b4230c8
AC
7477 -- An implicit dereference is a legal occurrence of an incomplete type
7478 -- imported through a limited_with clause, if the full view is visible.
d469eabe 7479
7b56a91b
AC
7480 if From_Limited_With (Typ)
7481 and then not From_Limited_With (Scope (Typ))
d469eabe
HK
7482 and then
7483 (Is_Immediately_Visible (Scope (Typ))
7484 or else
7485 (Is_Child_Unit (Scope (Typ))
8398e82e 7486 and then Is_Visible_Lib_Unit (Scope (Typ))))
d469eabe
HK
7487 then
7488 return Available_View (Typ);
7489 else
7490 return Typ;
7491 end if;
6e73e3ab
AC
7492 end Process_Implicit_Dereference_Prefix;
7493
30c20106
AC
7494 --------------------------------
7495 -- Remove_Abstract_Operations --
7496 --------------------------------
7497
7498 procedure Remove_Abstract_Operations (N : Node_Id) is
e80f0cb0 7499 Abstract_Op : Entity_Id := Empty;
d9d25d04 7500 Address_Descendant : Boolean := False;
e80f0cb0
RD
7501 I : Interp_Index;
7502 It : Interp;
30c20106 7503
0e0eecec
ES
7504 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
7505 -- activate this if either extensions are enabled, or if the abstract
7506 -- operation in question comes from a predefined file. This latter test
7507 -- allows us to use abstract to make operations invisible to users. In
7508 -- particular, if type Address is non-private and abstract subprograms
7509 -- are used to hide its operators, they will be truly hidden.
30c20106 7510
5950a3ac 7511 type Operand_Position is (First_Op, Second_Op);
8a36a0cc 7512 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
5950a3ac
AC
7513
7514 procedure Remove_Address_Interpretations (Op : Operand_Position);
0e0eecec
ES
7515 -- Ambiguities may arise when the operands are literal and the address
7516 -- operations in s-auxdec are visible. In that case, remove the
8b4230c8
AC
7517 -- interpretation of a literal as Address, to retain the semantics
7518 -- of Address as a private type.
9f4fd324
AC
7519
7520 ------------------------------------
5950a3ac 7521 -- Remove_Address_Interpretations --
9f4fd324
AC
7522 ------------------------------------
7523
5950a3ac 7524 procedure Remove_Address_Interpretations (Op : Operand_Position) is
9f4fd324
AC
7525 Formal : Entity_Id;
7526
7527 begin
7528 if Is_Overloaded (N) then
7529 Get_First_Interp (N, I, It);
7530 while Present (It.Nam) loop
7531 Formal := First_Entity (It.Nam);
7532
5950a3ac 7533 if Op = Second_Op then
99859ea7 7534 Next_Entity (Formal);
5950a3ac
AC
7535 end if;
7536
d9d25d04
AC
7537 if Is_Descendant_Of_Address (Etype (Formal)) then
7538 Address_Descendant := True;
9f4fd324
AC
7539 Remove_Interp (I);
7540 end if;
7541
7542 Get_Next_Interp (I, It);
7543 end loop;
7544 end if;
7545 end Remove_Address_Interpretations;
7546
7547 -- Start of processing for Remove_Abstract_Operations
7548
30c20106 7549 begin
d935a36e 7550 if Is_Overloaded (N) then
ee1a7572 7551 if Debug_Flag_V then
ba301a3b 7552 Write_Line ("Remove_Abstract_Operations: ");
ee1a7572
AC
7553 Write_Overloads (N);
7554 end if;
7555
30c20106 7556 Get_First_Interp (N, I, It);
d935a36e 7557
30c20106 7558 while Present (It.Nam) loop
aab883ec
ES
7559 if Is_Overloadable (It.Nam)
7560 and then Is_Abstract_Subprogram (It.Nam)
30c20106
AC
7561 and then not Is_Dispatching_Operation (It.Nam)
7562 then
af152989 7563 Abstract_Op := It.Nam;
fe45e59e 7564
d9d25d04
AC
7565 if Is_Descendant_Of_Address (It.Typ) then
7566 Address_Descendant := True;
401093c1
ES
7567 Remove_Interp (I);
7568 exit;
7569
76264f60 7570 -- In Ada 2005, this operation does not participate in overload
9c510803 7571 -- resolution. If the operation is defined in a predefined
fe45e59e
ES
7572 -- unit, it is one of the operations declared abstract in some
7573 -- variants of System, and it must be removed as well.
7574
0791fbe9 7575 elsif Ada_Version >= Ada_2005
8ab31c0c 7576 or else In_Predefined_Unit (It.Nam)
fe45e59e
ES
7577 then
7578 Remove_Interp (I);
7579 exit;
7580 end if;
30c20106
AC
7581 end if;
7582
7583 Get_Next_Interp (I, It);
7584 end loop;
7585
af152989 7586 if No (Abstract_Op) then
fe45e59e
ES
7587
7588 -- If some interpretation yields an integer type, it is still
7589 -- possible that there are address interpretations. Remove them
7590 -- if one operand is a literal, to avoid spurious ambiguities
7591 -- on systems where Address is a visible integer type.
7592
7593 if Is_Overloaded (N)
401093c1 7594 and then Nkind (N) in N_Op
fe45e59e
ES
7595 and then Is_Integer_Type (Etype (N))
7596 then
7597 if Nkind (N) in N_Binary_Op then
7598 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
7599 Remove_Address_Interpretations (Second_Op);
7600
e6326de5 7601 elsif Nkind (Left_Opnd (N)) = N_Integer_Literal then
fe45e59e
ES
7602 Remove_Address_Interpretations (First_Op);
7603 end if;
7604 end if;
7605 end if;
3984e89a
AC
7606
7607 elsif Nkind (N) in N_Op then
4c46b835 7608
fe45e59e
ES
7609 -- Remove interpretations that treat literals as addresses. This
7610 -- is never appropriate, even when Address is defined as a visible
7611 -- Integer type. The reason is that we would really prefer Address
7a5b62b0
AC
7612 -- to behave as a private type, even in this case. If Address is a
7613 -- visible integer type, we get lots of overload ambiguities.
30c20106 7614
5950a3ac
AC
7615 if Nkind (N) in N_Binary_Op then
7616 declare
7617 U1 : constant Boolean :=
8b4230c8 7618 Present (Universal_Interpretation (Right_Opnd (N)));
5950a3ac 7619 U2 : constant Boolean :=
8b4230c8 7620 Present (Universal_Interpretation (Left_Opnd (N)));
30c20106 7621
5950a3ac 7622 begin
0e0eecec 7623 if U1 then
5950a3ac 7624 Remove_Address_Interpretations (Second_Op);
0e0eecec 7625 end if;
5950a3ac 7626
0e0eecec 7627 if U2 then
5950a3ac 7628 Remove_Address_Interpretations (First_Op);
30c20106
AC
7629 end if;
7630
5950a3ac
AC
7631 if not (U1 and U2) then
7632
7633 -- Remove corresponding predefined operator, which is
7634 -- always added to the overload set.
7635
7636 Get_First_Interp (N, I, It);
7637 while Present (It.Nam) loop
0ab80019
AC
7638 if Scope (It.Nam) = Standard_Standard
7639 and then Base_Type (It.Typ) =
7640 Base_Type (Etype (Abstract_Op))
7641 then
5950a3ac
AC
7642 Remove_Interp (I);
7643 end if;
7644
8a36a0cc
AC
7645 Get_Next_Interp (I, It);
7646 end loop;
7647
7648 elsif Is_Overloaded (N)
7649 and then Present (Univ_Type)
7650 then
7651 -- If both operands have a universal interpretation,
0e0eecec
ES
7652 -- it is still necessary to remove interpretations that
7653 -- yield Address. Any remaining ambiguities will be
7654 -- removed in Disambiguate.
8a36a0cc
AC
7655
7656 Get_First_Interp (N, I, It);
8a36a0cc 7657 while Present (It.Nam) loop
d9d25d04 7658 if Is_Descendant_Of_Address (It.Typ) then
0e0eecec
ES
7659 Remove_Interp (I);
7660
7661 elsif not Is_Type (It.Nam) then
8a36a0cc 7662 Set_Entity (N, It.Nam);
8a36a0cc
AC
7663 end if;
7664
5950a3ac
AC
7665 Get_Next_Interp (I, It);
7666 end loop;
7667 end if;
7668 end;
30c20106 7669 end if;
3984e89a
AC
7670
7671 elsif Nkind (N) = N_Function_Call
7672 and then
7673 (Nkind (Name (N)) = N_Operator_Symbol
7674 or else
7675 (Nkind (Name (N)) = N_Expanded_Name
7676 and then
7677 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
7678 then
5950a3ac 7679
3984e89a
AC
7680 declare
7681 Arg1 : constant Node_Id := First (Parameter_Associations (N));
5950a3ac
AC
7682 U1 : constant Boolean :=
7683 Present (Universal_Interpretation (Arg1));
7684 U2 : constant Boolean :=
7685 Present (Next (Arg1)) and then
7686 Present (Universal_Interpretation (Next (Arg1)));
3984e89a
AC
7687
7688 begin
0e0eecec 7689 if U1 then
5950a3ac 7690 Remove_Address_Interpretations (First_Op);
0e0eecec 7691 end if;
3984e89a 7692
0e0eecec 7693 if U2 then
5950a3ac
AC
7694 Remove_Address_Interpretations (Second_Op);
7695 end if;
7696
7697 if not (U1 and U2) then
3984e89a
AC
7698 Get_First_Interp (N, I, It);
7699 while Present (It.Nam) loop
9f4fd324
AC
7700 if Scope (It.Nam) = Standard_Standard
7701 and then It.Typ = Base_Type (Etype (Abstract_Op))
7702 then
3984e89a
AC
7703 Remove_Interp (I);
7704 end if;
7705
7706 Get_Next_Interp (I, It);
7707 end loop;
7708 end if;
7709 end;
30c20106 7710 end if;
af152989 7711
401093c1
ES
7712 -- If the removal has left no valid interpretations, emit an error
7713 -- message now and label node as illegal.
af152989
AC
7714
7715 if Present (Abstract_Op) then
7716 Get_First_Interp (N, I, It);
7717
7718 if No (It.Nam) then
7719
6e73e3ab 7720 -- Removal of abstract operation left no viable candidate
af152989
AC
7721
7722 Set_Etype (N, Any_Type);
7723 Error_Msg_Sloc := Sloc (Abstract_Op);
7724 Error_Msg_NE
7725 ("cannot call abstract operation& declared#", N, Abstract_Op);
401093c1
ES
7726
7727 -- In Ada 2005, an abstract operation may disable predefined
7728 -- operators. Since the context is not yet known, we mark the
7729 -- predefined operators as potentially hidden. Do not include
7730 -- predefined operators when addresses are involved since this
7731 -- case is handled separately.
7732
d9d25d04 7733 elsif Ada_Version >= Ada_2005 and then not Address_Descendant then
401093c1
ES
7734 while Present (It.Nam) loop
7735 if Is_Numeric_Type (It.Typ)
7736 and then Scope (It.Typ) = Standard_Standard
7737 then
7738 Set_Abstract_Op (I, Abstract_Op);
7739 end if;
7740
7741 Get_Next_Interp (I, It);
7742 end loop;
af152989
AC
7743 end if;
7744 end if;
ee1a7572
AC
7745
7746 if Debug_Flag_V then
ba301a3b 7747 Write_Line ("Remove_Abstract_Operations done: ");
ee1a7572
AC
7748 Write_Overloads (N);
7749 end if;
30c20106
AC
7750 end if;
7751 end Remove_Abstract_Operations;
7752
d50f4827
AC
7753 ----------------------------
7754 -- Try_Container_Indexing --
7755 ----------------------------
7756
7757 function Try_Container_Indexing
7758 (N : Node_Id;
7759 Prefix : Node_Id;
50878404 7760 Exprs : List_Id) return Boolean
d50f4827 7761 is
437244c7
AC
7762 Pref_Typ : constant Entity_Id := Etype (Prefix);
7763
0c3ef0cc
GD
7764 function Constant_Indexing_OK return Boolean;
7765 -- Constant_Indexing is legal if there is no Variable_Indexing defined
7766 -- for the type, or else node not a target of assignment, or an actual
7767 -- for an IN OUT or OUT formal (RM 4.1.6 (11)).
7768
211e7410
AC
7769 function Expr_Matches_In_Formal
7770 (Subp : Entity_Id;
7771 Par : Node_Id) return Boolean;
7772 -- Find formal corresponding to given indexed component that is an
7773 -- actual in a call. Note that the enclosing subprogram call has not
0c3ef0cc 7774 -- been analyzed yet, and the parameter list is not normalized, so
211e7410
AC
7775 -- that if the argument is a parameter association we must match it
7776 -- by name and not by position.
7777
437244c7
AC
7778 function Find_Indexing_Operations
7779 (T : Entity_Id;
7780 Nam : Name_Id;
7781 Is_Constant : Boolean) return Node_Id;
7782 -- Return a reference to the primitive operation of type T denoted by
7783 -- name Nam. If the operation is overloaded, the reference carries all
7784 -- interpretations. Flag Is_Constant should be set when the context is
7785 -- constant indexing.
7786
fa73fc3d
AC
7787 --------------------------
7788 -- Constant_Indexing_OK --
7789 --------------------------
7790
7791 function Constant_Indexing_OK return Boolean is
7792 Par : Node_Id;
7793
7794 begin
437244c7 7795 if No (Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing)) then
fa73fc3d
AC
7796 return True;
7797
7798 elsif not Is_Variable (Prefix) then
7799 return True;
7800 end if;
7801
7802 Par := N;
7803 while Present (Par) loop
7804 if Nkind (Parent (Par)) = N_Assignment_Statement
7805 and then Par = Name (Parent (Par))
7806 then
7807 return False;
7808
7809 -- The call may be overloaded, in which case we assume that its
7810 -- resolution does not depend on the type of the parameter that
7811 -- includes the indexing operation.
7812
7813 elsif Nkind_In (Parent (Par), N_Function_Call,
7814 N_Procedure_Call_Statement)
7815 and then Is_Entity_Name (Name (Parent (Par)))
7816 then
7817 declare
fa73fc3d
AC
7818 Proc : Entity_Id;
7819
7820 begin
7821 -- We should look for an interpretation with the proper
7822 -- number of formals, and determine whether it is an
31101470
AC
7823 -- In_Parameter, but for now we examine the formal that
7824 -- corresponds to the indexing, and assume that variable
7825 -- indexing is required if some interpretation has an
64ac53f4 7826 -- assignable formal at that position. Still does not
31101470 7827 -- cover the most complex cases ???
fa73fc3d
AC
7828
7829 if Is_Overloaded (Name (Parent (Par))) then
31101470
AC
7830 declare
7831 Proc : constant Node_Id := Name (Parent (Par));
31101470
AC
7832 I : Interp_Index;
7833 It : Interp;
7834
7835 begin
7836 Get_First_Interp (Proc, I, It);
7837 while Present (It.Nam) loop
211e7410
AC
7838 if not Expr_Matches_In_Formal (It.Nam, Par) then
7839 return False;
7840 end if;
31101470
AC
7841
7842 Get_Next_Interp (I, It);
7843 end loop;
7844 end;
7845
0c3ef0cc 7846 -- All interpretations have a matching in-mode formal
211e7410 7847
fa73fc3d
AC
7848 return True;
7849
7850 else
7851 Proc := Entity (Name (Parent (Par)));
7852
7853 -- If this is an indirect call, get formals from
7854 -- designated type.
7855
7856 if Is_Access_Subprogram_Type (Etype (Proc)) then
7857 Proc := Designated_Type (Etype (Proc));
7858 end if;
7859 end if;
7860
211e7410 7861 return Expr_Matches_In_Formal (Proc, Par);
fa73fc3d
AC
7862 end;
7863
7864 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
7865 return False;
7866
7867 -- If the indexed component is a prefix it may be the first actual
7868 -- of a prefixed call. Retrieve the called entity, if any, and
4e9ee595
AC
7869 -- check its first formal. Determine if the context is a procedure
7870 -- or function call.
fa73fc3d
AC
7871
7872 elsif Nkind (Parent (Par)) = N_Selected_Component then
7873 declare
7874 Sel : constant Node_Id := Selector_Name (Parent (Par));
7875 Nam : constant Entity_Id := Current_Entity (Sel);
7876
7877 begin
bc38dbb4
AC
7878 if Present (Nam) and then Is_Overloadable (Nam) then
7879 if Nkind (Parent (Parent (Par))) =
7880 N_Procedure_Call_Statement
4e9ee595
AC
7881 then
7882 return False;
7883
bc38dbb4
AC
7884 elsif Ekind (Nam) = E_Function
7885 and then Present (First_Formal (Nam))
7886 then
7887 return Ekind (First_Formal (Nam)) = E_In_Parameter;
4e9ee595 7888 end if;
fa73fc3d
AC
7889 end if;
7890 end;
7891
437244c7 7892 elsif Nkind (Par) in N_Op then
fa73fc3d
AC
7893 return True;
7894 end if;
7895
7896 Par := Parent (Par);
7897 end loop;
7898
7899 -- In all other cases, constant indexing is legal
7900
7901 return True;
7902 end Constant_Indexing_OK;
7903
f32eb591
AC
7904 ----------------------------
7905 -- Expr_Matches_In_Formal --
7906 ----------------------------
0c3ef0cc
GD
7907
7908 function Expr_Matches_In_Formal
7909 (Subp : Entity_Id;
7910 Par : Node_Id) return Boolean
7911 is
7912 Actual : Node_Id;
7913 Formal : Node_Id;
7914
7915 begin
7916 Formal := First_Formal (Subp);
7917 Actual := First (Parameter_Associations ((Parent (Par))));
7918
7919 if Nkind (Par) /= N_Parameter_Association then
7920
7921 -- Match by position
7922
7923 while Present (Actual) and then Present (Formal) loop
7924 exit when Actual = Par;
7925 Next (Actual);
7926
7927 if Present (Formal) then
7928 Next_Formal (Formal);
7929
7930 -- Otherwise this is a parameter mismatch, the error is
7931 -- reported elsewhere, or else variable indexing is implied.
7932
7933 else
7934 return False;
7935 end if;
7936 end loop;
7937
7938 else
7939 -- Match by name
7940
7941 while Present (Formal) loop
7942 exit when Chars (Formal) = Chars (Selector_Name (Par));
7943 Next_Formal (Formal);
7944
7945 if No (Formal) then
7946 return False;
7947 end if;
7948 end loop;
7949 end if;
7950
7951 return Present (Formal) and then Ekind (Formal) = E_In_Parameter;
7952 end Expr_Matches_In_Formal;
7953
437244c7
AC
7954 ------------------------------
7955 -- Find_Indexing_Operations --
7956 ------------------------------
7957
7958 function Find_Indexing_Operations
7959 (T : Entity_Id;
7960 Nam : Name_Id;
7961 Is_Constant : Boolean) return Node_Id
7962 is
7963 procedure Inspect_Declarations
7964 (Typ : Entity_Id;
7965 Ref : in out Node_Id);
7966 -- Traverse the declarative list where type Typ resides and collect
7967 -- all suitable interpretations in node Ref.
7968
7969 procedure Inspect_Primitives
7970 (Typ : Entity_Id;
7971 Ref : in out Node_Id);
7972 -- Traverse the list of primitive operations of type Typ and collect
7973 -- all suitable interpretations in node Ref.
7974
7975 function Is_OK_Candidate
7976 (Subp_Id : Entity_Id;
7977 Typ : Entity_Id) return Boolean;
7978 -- Determine whether subprogram Subp_Id is a suitable indexing
7979 -- operation for type Typ. To qualify as such, the subprogram must
7980 -- be a function, have at least two parameters, and the type of the
7981 -- first parameter must be either Typ, or Typ'Class, or access [to
7982 -- constant] with designated type Typ or Typ'Class.
7983
7984 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id);
7985 -- Store subprogram Subp_Id as an interpretation in node Ref
7986
7987 --------------------------
7988 -- Inspect_Declarations --
7989 --------------------------
7990
7991 procedure Inspect_Declarations
7992 (Typ : Entity_Id;
7993 Ref : in out Node_Id)
7994 is
7995 Typ_Decl : constant Node_Id := Declaration_Node (Typ);
7996 Decl : Node_Id;
7997 Subp_Id : Entity_Id;
7998
7999 begin
2cc2e964 8000 -- Ensure that the routine is not called with itypes, which lack a
437244c7
AC
8001 -- declarative node.
8002
8003 pragma Assert (Present (Typ_Decl));
8004 pragma Assert (Is_List_Member (Typ_Decl));
8005
8006 Decl := First (List_Containing (Typ_Decl));
8007 while Present (Decl) loop
8008 if Nkind (Decl) = N_Subprogram_Declaration then
8009 Subp_Id := Defining_Entity (Decl);
8010
8011 if Is_OK_Candidate (Subp_Id, Typ) then
8012 Record_Interp (Subp_Id, Ref);
8013 end if;
8014 end if;
8015
8016 Next (Decl);
8017 end loop;
8018 end Inspect_Declarations;
8019
8020 ------------------------
8021 -- Inspect_Primitives --
8022 ------------------------
8023
8024 procedure Inspect_Primitives
8025 (Typ : Entity_Id;
8026 Ref : in out Node_Id)
8027 is
8028 Prim_Elmt : Elmt_Id;
8029 Prim_Id : Entity_Id;
8030
8031 begin
8032 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
8033 while Present (Prim_Elmt) loop
8034 Prim_Id := Node (Prim_Elmt);
8035
8036 if Is_OK_Candidate (Prim_Id, Typ) then
8037 Record_Interp (Prim_Id, Ref);
8038 end if;
8039
8040 Next_Elmt (Prim_Elmt);
8041 end loop;
8042 end Inspect_Primitives;
8043
8044 ---------------------
8045 -- Is_OK_Candidate --
8046 ---------------------
8047
8048 function Is_OK_Candidate
8049 (Subp_Id : Entity_Id;
8050 Typ : Entity_Id) return Boolean
8051 is
8052 Formal : Entity_Id;
8053 Formal_Typ : Entity_Id;
8054 Param_Typ : Node_Id;
8055
8056 begin
2cc2e964 8057 -- To classify as a suitable candidate, the subprogram must be a
437244c7
AC
8058 -- function whose name matches the argument of aspect Constant or
8059 -- Variable_Indexing.
8060
8061 if Ekind (Subp_Id) = E_Function and then Chars (Subp_Id) = Nam then
8062 Formal := First_Formal (Subp_Id);
8063
8064 -- The candidate requires at least two parameters
8065
8066 if Present (Formal) and then Present (Next_Formal (Formal)) then
8067 Formal_Typ := Empty;
8068 Param_Typ := Parameter_Type (Parent (Formal));
8069
8070 -- Use the designated type when the first parameter is of an
8071 -- access type.
8072
8073 if Nkind (Param_Typ) = N_Access_Definition
8074 and then Present (Subtype_Mark (Param_Typ))
8075 then
8076 -- When the context is a constant indexing, the access
8077 -- definition must be access-to-constant. This does not
8078 -- apply to variable indexing.
8079
8080 if not Is_Constant
8081 or else Constant_Present (Param_Typ)
8082 then
8083 Formal_Typ := Etype (Subtype_Mark (Param_Typ));
8084 end if;
8085
8086 -- Otherwise use the parameter type
8087
8088 else
8089 Formal_Typ := Etype (Param_Typ);
8090 end if;
8091
8092 if Present (Formal_Typ) then
8093
8094 -- Use the specific type when the parameter type is
8095 -- class-wide.
8096
8097 if Is_Class_Wide_Type (Formal_Typ) then
8098 Formal_Typ := Etype (Base_Type (Formal_Typ));
8099 end if;
8100
8101 -- Use the full view when the parameter type is private
8102 -- or incomplete.
8103
8104 if Is_Incomplete_Or_Private_Type (Formal_Typ)
8105 and then Present (Full_View (Formal_Typ))
8106 then
8107 Formal_Typ := Full_View (Formal_Typ);
8108 end if;
8109
8110 -- The type of the first parameter must denote the type
8111 -- of the container or acts as its ancestor type.
8112
8113 return
8114 Formal_Typ = Typ
8115 or else Is_Ancestor (Formal_Typ, Typ);
8116 end if;
8117 end if;
8118 end if;
8119
8120 return False;
8121 end Is_OK_Candidate;
8122
8123 -------------------
8124 -- Record_Interp --
8125 -------------------
8126
8127 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id) is
8128 begin
8129 if Present (Ref) then
8130 Add_One_Interp (Ref, Subp_Id, Etype (Subp_Id));
8131
8132 -- Otherwise this is the first interpretation. Create a reference
8133 -- where all remaining interpretations will be collected.
8134
8135 else
8136 Ref := New_Occurrence_Of (Subp_Id, Sloc (T));
8137 end if;
8138 end Record_Interp;
8139
8140 -- Local variables
8141
8142 Ref : Node_Id;
8143 Typ : Entity_Id;
8144
8145 -- Start of processing for Find_Indexing_Operations
8146
8147 begin
8148 Typ := T;
8149
1e60643a
AC
8150 -- Use the specific type when the parameter type is class-wide
8151
437244c7
AC
8152 if Is_Class_Wide_Type (Typ) then
8153 Typ := Root_Type (Typ);
8154 end if;
8155
8156 Ref := Empty;
1e60643a 8157 Typ := Underlying_Type (Base_Type (Typ));
437244c7 8158
e11b776b
AC
8159 Inspect_Primitives (Typ, Ref);
8160
8161 -- Now look for explicit declarations of an indexing operation.
8162 -- If the type is private the operation may be declared in the
8163 -- visible part that contains the partial view.
8164
8165 if Is_Private_Type (T) then
8166 Inspect_Declarations (T, Ref);
8167 end if;
8168
437244c7
AC
8169 Inspect_Declarations (Typ, Ref);
8170
8171 return Ref;
8172 end Find_Indexing_Operations;
8173
fa73fc3d
AC
8174 -- Local variables
8175
d50f4827 8176 Loc : constant Source_Ptr := Sloc (N);
50878404 8177 Assoc : List_Id;
fa73fc3d 8178 C_Type : Entity_Id;
d50f4827
AC
8179 Func : Entity_Id;
8180 Func_Name : Node_Id;
8181 Indexing : Node_Id;
d50f4827 8182
437244c7
AC
8183 Is_Constant_Indexing : Boolean := False;
8184 -- This flag reflects the nature of the container indexing. Note that
8185 -- the context may be suited for constant indexing, but the type may
8186 -- lack a Constant_Indexing annotation.
8187
fa73fc3d
AC
8188 -- Start of processing for Try_Container_Indexing
8189
d50f4827 8190 begin
fa73fc3d
AC
8191 -- Node may have been analyzed already when testing for a prefixed
8192 -- call, in which case do not redo analysis.
8193
8194 if Present (Generalized_Indexing (N)) then
8195 return True;
8196 end if;
8197
437244c7 8198 C_Type := Pref_Typ;
f3296dd3 8199
fa73fc3d
AC
8200 -- If indexing a class-wide container, obtain indexing primitive from
8201 -- specific type.
f3296dd3
AC
8202
8203 if Is_Class_Wide_Type (C_Type) then
8204 C_Type := Etype (Base_Type (C_Type));
8205 end if;
d50f4827 8206
2cc2e964 8207 -- Check whether the type has a specified indexing aspect
d50f4827
AC
8208
8209 Func_Name := Empty;
d50f4827 8210
2cc2e964
AC
8211 -- The context is suitable for constant indexing, so obtain the name of
8212 -- the indexing function from aspect Constant_Indexing.
437244c7 8213
fa73fc3d 8214 if Constant_Indexing_OK then
d62520f3 8215 Func_Name :=
437244c7 8216 Find_Value_Of_Aspect (Pref_Typ, Aspect_Constant_Indexing);
9ee76313 8217 end if;
dedac3eb 8218
437244c7
AC
8219 if Present (Func_Name) then
8220 Is_Constant_Indexing := True;
8221
8222 -- Otherwise attempt variable indexing
8223
8224 else
d62520f3 8225 Func_Name :=
437244c7 8226 Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing);
9ee76313 8227 end if;
d50f4827 8228
437244c7
AC
8229 -- The type is not subject to either form of indexing, therefore the
8230 -- indexed component does not denote container indexing. If this is a
8231 -- true error, it is diagnosed by the caller.
d50f4827
AC
8232
8233 if No (Func_Name) then
57a8057a 8234
437244c7
AC
8235 -- The prefix itself may be an indexing of a container. Rewrite it
8236 -- as such and retry.
57a8057a 8237
437244c7
AC
8238 if Has_Implicit_Dereference (Pref_Typ) then
8239 Build_Explicit_Dereference (Prefix, First_Discriminant (Pref_Typ));
50878404 8240 return Try_Container_Indexing (N, Prefix, Exprs);
57a8057a 8241
437244c7
AC
8242 -- Otherwise this is definitely not container indexing
8243
57a8057a
AC
8244 else
8245 return False;
8246 end if;
3f433bc0 8247
6907542d
AC
8248 -- If the container type is derived from another container type, the
8249 -- value of the inherited aspect is the Reference operation declared
8250 -- for the parent type.
8251
fa73fc3d
AC
8252 -- However, Reference is also a primitive operation of the type, and the
8253 -- inherited operation has a different signature. We retrieve the right
8254 -- ones (the function may be overloaded) from the list of primitive
8255 -- operations of the derived type.
3f433bc0 8256
fa73fc3d
AC
8257 -- Note that predefined containers are typically all derived from one of
8258 -- the Controlled types. The code below is motivated by containers that
8259 -- are derived from other types with a Reference aspect.
c85dda72 8260 -- Note as well that we need to examine the base type, given that
dab8e608
GD
8261 -- the container object may be a constrained subtype or itype that
8262 -- does not have an explicit declaration.
6907542d 8263
f3296dd3 8264 elsif Is_Derived_Type (C_Type)
437244c7 8265 and then Etype (First_Formal (Entity (Func_Name))) /= Pref_Typ
6907542d 8266 then
437244c7
AC
8267 Func_Name :=
8268 Find_Indexing_Operations
c85dda72 8269 (T => Base_Type (C_Type),
437244c7
AC
8270 Nam => Chars (Func_Name),
8271 Is_Constant => Is_Constant_Indexing);
d50f4827
AC
8272 end if;
8273
50878404
AC
8274 Assoc := New_List (Relocate_Node (Prefix));
8275
5f50020a 8276 -- A generalized indexing may have nore than one index expression, so
50878404 8277 -- transfer all of them to the argument list to be used in the call.
5f50020a
ES
8278 -- Note that there may be named associations, in which case the node
8279 -- was rewritten earlier as a call, and has been transformed back into
8280 -- an indexed expression to share the following processing.
e917e3b8 8281
5f50020a
ES
8282 -- The generalized indexing node is the one on which analysis and
8283 -- resolution take place. Before expansion the original node is replaced
fa73fc3d
AC
8284 -- with the generalized indexing node, which is a call, possibly with a
8285 -- dereference operation.
50878404 8286
e917e3b8 8287 if Comes_From_Source (N) then
c86cf714 8288 Check_Compiler_Unit ("generalized indexing", N);
e917e3b8
AC
8289 end if;
8290
287aa0ed
AC
8291 -- Create argument list for function call that represents generalized
8292 -- indexing. Note that indices (i.e. actuals) may themselves be
8293 -- overloaded.
8294
50878404 8295 declare
287aa0ed
AC
8296 Arg : Node_Id;
8297 New_Arg : Node_Id;
8298
50878404
AC
8299 begin
8300 Arg := First (Exprs);
8301 while Present (Arg) loop
287aa0ed 8302 New_Arg := Relocate_Node (Arg);
43151cfd
ES
8303
8304 -- The arguments can be parameter associations, in which case the
8305 -- explicit actual parameter carries the overloadings.
8306
8307 if Nkind (New_Arg) /= N_Parameter_Association then
8308 Save_Interps (Arg, New_Arg);
8309 end if;
8310
287aa0ed 8311 Append (New_Arg, Assoc);
50878404
AC
8312 Next (Arg);
8313 end loop;
8314 end;
8315
d50f4827
AC
8316 if not Is_Overloaded (Func_Name) then
8317 Func := Entity (Func_Name);
f4ef7b06 8318
29ba9f52
RD
8319 Indexing :=
8320 Make_Function_Call (Loc,
8321 Name => New_Occurrence_Of (Func, Loc),
8322 Parameter_Associations => Assoc);
f4ef7b06 8323
5f50020a
ES
8324 Set_Parent (Indexing, Parent (N));
8325 Set_Generalized_Indexing (N, Indexing);
8326 Analyze (Indexing);
8327 Set_Etype (N, Etype (Indexing));
d50f4827 8328
76d49f49
ES
8329 -- If the return type of the indexing function is a reference type,
8330 -- add the dereference as a possible interpretation. Note that the
8331 -- indexing aspect may be a function that returns the element type
5f50020a
ES
8332 -- with no intervening implicit dereference, and that the reference
8333 -- discriminant is not the first discriminant.
76d49f49
ES
8334
8335 if Has_Discriminants (Etype (Func)) then
71ff3d18 8336 Check_Implicit_Dereference (N, Etype (Func));
76d49f49 8337 end if;
d50f4827
AC
8338
8339 else
90b510e4
AC
8340 -- If there are multiple indexing functions, build a function call
8341 -- and analyze it for each of the possible interpretations.
8342
8b4230c8
AC
8343 Indexing :=
8344 Make_Function_Call (Loc,
fa73fc3d
AC
8345 Name =>
8346 Make_Identifier (Loc, Chars (Func_Name)),
8b4230c8 8347 Parameter_Associations => Assoc);
5f50020a
ES
8348 Set_Parent (Indexing, Parent (N));
8349 Set_Generalized_Indexing (N, Indexing);
90b510e4
AC
8350 Set_Etype (N, Any_Type);
8351 Set_Etype (Name (Indexing), Any_Type);
d50f4827
AC
8352
8353 declare
8b4230c8
AC
8354 I : Interp_Index;
8355 It : Interp;
d50f4827
AC
8356 Success : Boolean;
8357
8358 begin
8359 Get_First_Interp (Func_Name, I, It);
5f50020a 8360 Set_Etype (Indexing, Any_Type);
90b510e4 8361
f4ef7b06 8362 -- Analyze each candidate function with the given actuals
0310af44 8363
d50f4827 8364 while Present (It.Nam) loop
5f50020a 8365 Analyze_One_Call (Indexing, It.Nam, False, Success);
0310af44
AC
8366 Get_Next_Interp (I, It);
8367 end loop;
32bba3c9 8368
0310af44
AC
8369 -- If there are several successful candidates, resolution will
8370 -- be by result. Mark the interpretations of the function name
8371 -- itself.
d50f4827 8372
0310af44
AC
8373 if Is_Overloaded (Indexing) then
8374 Get_First_Interp (Indexing, I, It);
90b510e4 8375
0310af44 8376 while Present (It.Nam) loop
90b510e4 8377 Add_One_Interp (Name (Indexing), It.Nam, It.Typ);
0310af44
AC
8378 Get_Next_Interp (I, It);
8379 end loop;
8380
8381 else
8382 Set_Etype (Name (Indexing), Etype (Indexing));
8383 end if;
8384
8385 -- Now add the candidate interpretations to the indexing node
8386 -- itself, to be replaced later by the function call.
8387
8388 if Is_Overloaded (Name (Indexing)) then
8389 Get_First_Interp (Name (Indexing), I, It);
8390
8391 while Present (It.Nam) loop
90b510e4
AC
8392 Add_One_Interp (N, It.Nam, It.Typ);
8393
6c7f7b8c
AC
8394 -- Add dereference interpretation if the result type has
8395 -- implicit reference discriminants.
d50f4827 8396
76d49f49 8397 if Has_Discriminants (Etype (It.Nam)) then
71ff3d18 8398 Check_Implicit_Dereference (N, Etype (It.Nam));
76d49f49 8399 end if;
32bba3c9 8400
0310af44
AC
8401 Get_Next_Interp (I, It);
8402 end loop;
8403
8404 else
8405 Set_Etype (N, Etype (Name (Indexing)));
8406 if Has_Discriminants (Etype (N)) then
8407 Check_Implicit_Dereference (N, Etype (N));
8408 end if;
8409 end if;
d50f4827
AC
8410 end;
8411 end if;
8412
5f50020a 8413 if Etype (Indexing) = Any_Type then
29ba9f52
RD
8414 Error_Msg_NE
8415 ("container cannot be indexed with&", N, Etype (First (Exprs)));
9ee76313 8416 Rewrite (N, New_Occurrence_Of (Any_Id, Loc));
9ee76313
AC
8417 end if;
8418
d50f4827
AC
8419 return True;
8420 end Try_Container_Indexing;
8421
996ae0b0
RK
8422 -----------------------
8423 -- Try_Indirect_Call --
8424 -----------------------
8425
8426 function Try_Indirect_Call
91b1417d
AC
8427 (N : Node_Id;
8428 Nam : Entity_Id;
8429 Typ : Entity_Id) return Boolean
996ae0b0 8430 is
24657705
HK
8431 Actual : Node_Id;
8432 Formal : Entity_Id;
8433
8a7988f5 8434 Call_OK : Boolean;
24657705 8435 pragma Warnings (Off, Call_OK);
996ae0b0
RK
8436
8437 begin
8a7988f5 8438 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
9de61fcb 8439
8a7988f5 8440 Actual := First_Actual (N);
fbf5a39b 8441 Formal := First_Formal (Designated_Type (Typ));
9de61fcb 8442 while Present (Actual) and then Present (Formal) loop
996ae0b0
RK
8443 if not Has_Compatible_Type (Actual, Etype (Formal)) then
8444 return False;
8445 end if;
8446
8447 Next (Actual);
8448 Next_Formal (Formal);
8449 end loop;
8450
8451 if No (Actual) and then No (Formal) then
8452 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
8453
8454 -- Nam is a candidate interpretation for the name in the call,
8455 -- if it is not an indirect call.
8456
8457 if not Is_Type (Nam)
8458 and then Is_Entity_Name (Name (N))
8459 then
8460 Set_Entity (Name (N), Nam);
8461 end if;
8462
8463 return True;
8b4230c8 8464
996ae0b0
RK
8465 else
8466 return False;
8467 end if;
8468 end Try_Indirect_Call;
8469
8470 ----------------------
8471 -- Try_Indexed_Call --
8472 ----------------------
8473
8474 function Try_Indexed_Call
aab883ec
ES
8475 (N : Node_Id;
8476 Nam : Entity_Id;
8477 Typ : Entity_Id;
8478 Skip_First : Boolean) return Boolean
996ae0b0 8479 is
5ff22245
ES
8480 Loc : constant Source_Ptr := Sloc (N);
8481 Actuals : constant List_Id := Parameter_Associations (N);
8482 Actual : Node_Id;
8483 Index : Entity_Id;
996ae0b0
RK
8484
8485 begin
fbf5a39b 8486 Actual := First (Actuals);
aab883ec
ES
8487
8488 -- If the call was originally written in prefix form, skip the first
8489 -- actual, which is obviously not defaulted.
8490
8491 if Skip_First then
8492 Next (Actual);
8493 end if;
8494
fbf5a39b 8495 Index := First_Index (Typ);
9de61fcb
RD
8496 while Present (Actual) and then Present (Index) loop
8497
996ae0b0
RK
8498 -- If the parameter list has a named association, the expression
8499 -- is definitely a call and not an indexed component.
8500
8501 if Nkind (Actual) = N_Parameter_Association then
8502 return False;
8503 end if;
8504
5ff22245
ES
8505 if Is_Entity_Name (Actual)
8506 and then Is_Type (Entity (Actual))
8507 and then No (Next (Actual))
8508 then
1c218ac3
AC
8509 -- A single actual that is a type name indicates a slice if the
8510 -- type is discrete, and an error otherwise.
8511
8512 if Is_Discrete_Type (Entity (Actual)) then
8513 Rewrite (N,
8514 Make_Slice (Loc,
22b77f68
RD
8515 Prefix =>
8516 Make_Function_Call (Loc,
8517 Name => Relocate_Node (Name (N))),
8518 Discrete_Range =>
1c218ac3
AC
8519 New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
8520
8521 Analyze (N);
8522
8523 else
8524 Error_Msg_N ("invalid use of type in expression", Actual);
8525 Set_Etype (N, Any_Type);
8526 end if;
5ff22245 8527
5ff22245
ES
8528 return True;
8529
8530 elsif not Has_Compatible_Type (Actual, Etype (Index)) then
996ae0b0
RK
8531 return False;
8532 end if;
8533
8534 Next (Actual);
8535 Next_Index (Index);
8536 end loop;
8537
8538 if No (Actual) and then No (Index) then
8539 Add_One_Interp (N, Nam, Component_Type (Typ));
8540
8541 -- Nam is a candidate interpretation for the name in the call,
8542 -- if it is not an indirect call.
8543
8544 if not Is_Type (Nam)
8545 and then Is_Entity_Name (Name (N))
8546 then
8547 Set_Entity (Name (N), Nam);
8548 end if;
8549
8550 return True;
8551 else
8552 return False;
8553 end if;
996ae0b0
RK
8554 end Try_Indexed_Call;
8555
35ae2ed8
AC
8556 --------------------------
8557 -- Try_Object_Operation --
8558 --------------------------
8559
8cf23b91
AC
8560 function Try_Object_Operation
8561 (N : Node_Id; CW_Test_Only : Boolean := False) return Boolean
8562 is
b67a385c 8563 K : constant Node_Kind := Nkind (Parent (N));
d3b00ce3 8564 Is_Subprg_Call : constant Boolean := K in N_Subprogram_Call;
b67a385c 8565 Loc : constant Source_Ptr := Sloc (N);
b67a385c 8566 Obj : constant Node_Id := Prefix (N);
0d57c6f4 8567
48c8c473 8568 Subprog : constant Node_Id :=
0d57c6f4
RD
8569 Make_Identifier (Sloc (Selector_Name (N)),
8570 Chars => Chars (Selector_Name (N)));
401093c1 8571 -- Identifier on which possible interpretations will be collected
0a36105d 8572
b67a385c 8573 Report_Error : Boolean := False;
8b4230c8
AC
8574 -- If no candidate interpretation matches the context, redo analysis
8575 -- with Report_Error True to provide additional information.
28d6470f
JM
8576
8577 Actual : Node_Id;
d469eabe 8578 Candidate : Entity_Id := Empty;
48c8c473 8579 New_Call_Node : Node_Id := Empty;
4c46b835 8580 Node_To_Replace : Node_Id;
28d6470f 8581 Obj_Type : Entity_Id := Etype (Obj);
48c8c473 8582 Success : Boolean := False;
0a36105d 8583
4c46b835
AC
8584 procedure Complete_Object_Operation
8585 (Call_Node : Node_Id;
0a36105d 8586 Node_To_Replace : Node_Id);
ec6078e3
ES
8587 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
8588 -- Call_Node, insert the object (or its dereference) as the first actual
8589 -- in the call, and complete the analysis of the call.
4c46b835 8590
0a36105d 8591 procedure Report_Ambiguity (Op : Entity_Id);
48c8c473
AC
8592 -- If a prefixed procedure call is ambiguous, indicate whether the call
8593 -- includes an implicit dereference or an implicit 'Access.
0a36105d 8594
4c46b835
AC
8595 procedure Transform_Object_Operation
8596 (Call_Node : out Node_Id;
0a36105d 8597 Node_To_Replace : out Node_Id);
0e3a687f 8598 -- Transform Obj.Operation (X, Y, ...) into Operation (Obj, X, Y ...).
d469eabe
HK
8599 -- Call_Node is the resulting subprogram call, Node_To_Replace is
8600 -- either N or the parent of N, and Subprog is a reference to the
8601 -- subprogram we are trying to match.
35ae2ed8
AC
8602
8603 function Try_Class_Wide_Operation
4c46b835
AC
8604 (Call_Node : Node_Id;
8605 Node_To_Replace : Node_Id) return Boolean;
48c8c473
AC
8606 -- Traverse all ancestor types looking for a class-wide subprogram for
8607 -- which the current operation is a valid non-dispatching call.
35ae2ed8 8608
0a36105d
JM
8609 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
8610 -- If prefix is overloaded, its interpretation may include different
48c8c473
AC
8611 -- tagged types, and we must examine the primitive operations and the
8612 -- class-wide operations of each in order to find candidate
0a36105d
JM
8613 -- interpretations for the call as a whole.
8614
4c46b835
AC
8615 function Try_Primitive_Operation
8616 (Call_Node : Node_Id;
8617 Node_To_Replace : Node_Id) return Boolean;
ec6078e3 8618 -- Traverse the list of primitive subprograms looking for a dispatching
48c8c473 8619 -- operation for which the current node is a valid call.
0a36105d
JM
8620
8621 function Valid_Candidate
8622 (Success : Boolean;
8623 Call : Node_Id;
48c8c473
AC
8624 Subp : Entity_Id) return Entity_Id;
8625 -- If the subprogram is a valid interpretation, record it, and add to
8626 -- the list of interpretations of Subprog. Otherwise return Empty.
0a36105d 8627
4c46b835
AC
8628 -------------------------------
8629 -- Complete_Object_Operation --
8630 -------------------------------
8631
8632 procedure Complete_Object_Operation
8633 (Call_Node : Node_Id;
0a36105d 8634 Node_To_Replace : Node_Id)
4c46b835 8635 is
b4592168
GD
8636 Control : constant Entity_Id := First_Formal (Entity (Subprog));
8637 Formal_Type : constant Entity_Id := Etype (Control);
ec6078e3
ES
8638 First_Actual : Node_Id;
8639
4c46b835 8640 begin
955871d3
AC
8641 -- Place the name of the operation, with its interpretations,
8642 -- on the rewritten call.
0a36105d 8643
ec6078e3
ES
8644 Set_Name (Call_Node, Subprog);
8645
0a36105d
JM
8646 First_Actual := First (Parameter_Associations (Call_Node));
8647
8b4230c8
AC
8648 -- For cross-reference purposes, treat the new node as being in the
8649 -- source if the original one is. Set entity and type, even though
8650 -- they may be overwritten during resolution if overloaded.
b67a385c
ES
8651
8652 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
8653 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
8654
ec6078e3 8655 if Nkind (N) = N_Selected_Component
3d918396 8656 and then not Inside_A_Generic
ec6078e3
ES
8657 then
8658 Set_Entity (Selector_Name (N), Entity (Subprog));
b2ab8c33 8659 Set_Etype (Selector_Name (N), Etype (Entity (Subprog)));
ec6078e3
ES
8660 end if;
8661
8b4230c8
AC
8662 -- If need be, rewrite first actual as an explicit dereference. If
8663 -- the call is overloaded, the rewriting can only be done once the
8664 -- primitive operation is identified.
0a36105d
JM
8665
8666 if Is_Overloaded (Subprog) then
ec6078e3 8667
0a36105d
JM
8668 -- The prefix itself may be overloaded, and its interpretations
8669 -- must be propagated to the new actual in the call.
8670
8671 if Is_Overloaded (Obj) then
8672 Save_Interps (Obj, First_Actual);
8673 end if;
8674
8675 Rewrite (First_Actual, Obj);
8676
8677 elsif not Is_Access_Type (Formal_Type)
ec6078e3
ES
8678 and then Is_Access_Type (Etype (Obj))
8679 then
8680 Rewrite (First_Actual,
8681 Make_Explicit_Dereference (Sloc (Obj), Obj));
8682 Analyze (First_Actual);
fe45e59e 8683
401093c1
ES
8684 -- If we need to introduce an explicit dereference, verify that
8685 -- the resulting actual is compatible with the mode of the formal.
8686
8687 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
8688 and then Is_Access_Constant (Etype (Obj))
8689 then
8690 Error_Msg_NE
8691 ("expect variable in call to&", Prefix (N), Entity (Subprog));
8692 end if;
8693
2eda24e9
PMR
8694 -- Conversely, if the formal is an access parameter and the object is
8695 -- not an access type or a reference type (i.e. a type with the
8696 -- Implicit_Dereference aspect specified), replace the actual with a
8697 -- 'Access reference. Its analysis will check that the object is
8698 -- aliased.
fe45e59e
ES
8699
8700 elsif Is_Access_Type (Formal_Type)
8701 and then not Is_Access_Type (Etype (Obj))
aa11d1dd
PMR
8702 and then
8703 (not Has_Implicit_Dereference (Etype (Obj))
8704 or else
8705 not Is_Access_Type (Designated_Type (Etype
8706 (Get_Reference_Discriminant (Etype (Obj))))))
fe45e59e 8707 then
2eda24e9 8708 -- A special case: A.all'Access is illegal if A is an access to a
b4592168
GD
8709 -- constant and the context requires an access to a variable.
8710
8711 if not Is_Access_Constant (Formal_Type) then
8712 if (Nkind (Obj) = N_Explicit_Dereference
8713 and then Is_Access_Constant (Etype (Prefix (Obj))))
8714 or else not Is_Variable (Obj)
8715 then
8716 Error_Msg_NE
ad075b50 8717 ("actual for & must be a variable", Obj, Control);
b4592168
GD
8718 end if;
8719 end if;
8720
fe45e59e
ES
8721 Rewrite (First_Actual,
8722 Make_Attribute_Reference (Loc,
8723 Attribute_Name => Name_Access,
8724 Prefix => Relocate_Node (Obj)));
0a36105d 8725
8f34c90b
AC
8726 -- If the object is not overloaded verify that taking access of
8727 -- it is legal. Otherwise check is made during resolution.
8728
8729 if not Is_Overloaded (Obj)
8730 and then not Is_Aliased_View (Obj)
8731 then
ed2233dc 8732 Error_Msg_NE
ad075b50 8733 ("object in prefixed call to & must be aliased "
715e529d 8734 & "(RM 4.1.3 (13 1/2))", Prefix (First_Actual), Subprog);
0a36105d
JM
8735 end if;
8736
fe45e59e
ES
8737 Analyze (First_Actual);
8738
ec6078e3 8739 else
0a36105d
JM
8740 if Is_Overloaded (Obj) then
8741 Save_Interps (Obj, First_Actual);
8742 end if;
ec6078e3 8743
0a36105d 8744 Rewrite (First_Actual, Obj);
aab883ec
ES
8745 end if;
8746
e699b76e
AC
8747 -- The operation is obtained from the dispatch table and not by
8748 -- visibility, and may be declared in a unit that is not explicitly
8749 -- referenced in the source, but is nevertheless required in the
8750 -- context of the current unit. Indicate that operation and its scope
8751 -- are referenced, to prevent spurious and misleading warnings. If
8752 -- the operation is overloaded, all primitives are in the same scope
8753 -- and we can use any of them.
8754
8755 Set_Referenced (Entity (Subprog), True);
8756 Set_Referenced (Scope (Entity (Subprog)), True);
8757
7ffd9312 8758 Rewrite (Node_To_Replace, Call_Node);
0a36105d
JM
8759
8760 -- Propagate the interpretations collected in subprog to the new
8761 -- function call node, to be resolved from context.
8762
8763 if Is_Overloaded (Subprog) then
8764 Save_Interps (Subprog, Node_To_Replace);
7415029d 8765
0a36105d 8766 else
28e18b4f
AC
8767 -- The type of the subprogram may be a limited view obtained
8768 -- transitively from another unit. If full view is available,
c312b9f2
PMR
8769 -- use it to analyze call. If there is no nonlimited view, then
8770 -- this is diagnosed when analyzing the rewritten call.
28e18b4f
AC
8771
8772 declare
8773 T : constant Entity_Id := Etype (Subprog);
8774 begin
8775 if From_Limited_With (T) then
8776 Set_Etype (Entity (Subprog), Available_View (T));
8777 end if;
8778 end;
8779
0a36105d 8780 Analyze (Node_To_Replace);
438ff97c 8781
199c6a10
AC
8782 -- If the operation has been rewritten into a call, which may get
8783 -- subsequently an explicit dereference, preserve the type on the
8784 -- original node (selected component or indexed component) for
8785 -- subsequent legality tests, e.g. Is_Variable. which examines
8786 -- the original node.
438ff97c
ES
8787
8788 if Nkind (Node_To_Replace) = N_Function_Call then
8789 Set_Etype
8790 (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
8791 end if;
0a36105d 8792 end if;
4c46b835
AC
8793 end Complete_Object_Operation;
8794
0a36105d
JM
8795 ----------------------
8796 -- Report_Ambiguity --
8797 ----------------------
8798
8799 procedure Report_Ambiguity (Op : Entity_Id) is
0a36105d
JM
8800 Access_Actual : constant Boolean :=
8801 Is_Access_Type (Etype (Prefix (N)));
8cf23b91 8802 Access_Formal : Boolean := False;
0a36105d
JM
8803
8804 begin
8805 Error_Msg_Sloc := Sloc (Op);
8806
8cf23b91
AC
8807 if Present (First_Formal (Op)) then
8808 Access_Formal := Is_Access_Type (Etype (First_Formal (Op)));
8809 end if;
8810
0a36105d
JM
8811 if Access_Formal and then not Access_Actual then
8812 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
ed2233dc 8813 Error_Msg_N
8b4230c8
AC
8814 ("\possible interpretation "
8815 & "(inherited, with implicit 'Access) #", N);
0a36105d 8816 else
ed2233dc 8817 Error_Msg_N
0a36105d
JM
8818 ("\possible interpretation (with implicit 'Access) #", N);
8819 end if;
8820
8821 elsif not Access_Formal and then Access_Actual then
8822 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
ed2233dc 8823 Error_Msg_N
8b4230c8 8824 ("\possible interpretation "
28e18b4f 8825 & "(inherited, with implicit dereference) #", N);
0a36105d 8826 else
ed2233dc 8827 Error_Msg_N
0a36105d
JM
8828 ("\possible interpretation (with implicit dereference) #", N);
8829 end if;
8830
8831 else
8832 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
ed2233dc 8833 Error_Msg_N ("\possible interpretation (inherited)#", N);
0a36105d 8834 else
4e7a4f6e
AC
8835 Error_Msg_N -- CODEFIX
8836 ("\possible interpretation#", N);
0a36105d
JM
8837 end if;
8838 end if;
8839 end Report_Ambiguity;
8840
4c46b835
AC
8841 --------------------------------
8842 -- Transform_Object_Operation --
8843 --------------------------------
8844
8845 procedure Transform_Object_Operation
8846 (Call_Node : out Node_Id;
0a36105d 8847 Node_To_Replace : out Node_Id)
35ae2ed8 8848 is
ec6078e3
ES
8849 Dummy : constant Node_Id := New_Copy (Obj);
8850 -- Placeholder used as a first parameter in the call, replaced
8851 -- eventually by the proper object.
8852
d469eabe
HK
8853 Parent_Node : constant Node_Id := Parent (N);
8854
ec6078e3 8855 Actual : Node_Id;
d469eabe 8856 Actuals : List_Id;
ec6078e3 8857
35ae2ed8 8858 begin
ec6078e3
ES
8859 -- Common case covering 1) Call to a procedure and 2) Call to a
8860 -- function that has some additional actuals.
35ae2ed8 8861
d3b00ce3 8862 if Nkind (Parent_Node) in N_Subprogram_Call
35ae2ed8 8863
ec6078e3
ES
8864 -- N is a selected component node containing the name of the
8865 -- subprogram. If N is not the name of the parent node we must
8866 -- not replace the parent node by the new construct. This case
8867 -- occurs when N is a parameterless call to a subprogram that
8868 -- is an actual parameter of a call to another subprogram. For
8869 -- example:
8870 -- Some_Subprogram (..., Obj.Operation, ...)
35ae2ed8 8871
ec6078e3 8872 and then Name (Parent_Node) = N
4c46b835
AC
8873 then
8874 Node_To_Replace := Parent_Node;
35ae2ed8 8875
ec6078e3 8876 Actuals := Parameter_Associations (Parent_Node);
d3e65aad 8877
ec6078e3
ES
8878 if Present (Actuals) then
8879 Prepend (Dummy, Actuals);
8880 else
8881 Actuals := New_List (Dummy);
8882 end if;
4c46b835
AC
8883
8884 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
8885 Call_Node :=
8886 Make_Procedure_Call_Statement (Loc,
48c8c473 8887 Name => New_Copy (Subprog),
4c46b835
AC
8888 Parameter_Associations => Actuals);
8889
8890 else
4c46b835
AC
8891 Call_Node :=
8892 Make_Function_Call (Loc,
8b4230c8 8893 Name => New_Copy (Subprog),
4c46b835 8894 Parameter_Associations => Actuals);
35ae2ed8
AC
8895 end if;
8896
d469eabe 8897 -- Before analysis, a function call appears as an indexed component
ec6078e3 8898 -- if there are no named associations.
758c442c 8899
c8307596 8900 elsif Nkind (Parent_Node) = N_Indexed_Component
ec6078e3
ES
8901 and then N = Prefix (Parent_Node)
8902 then
758c442c 8903 Node_To_Replace := Parent_Node;
ec6078e3
ES
8904 Actuals := Expressions (Parent_Node);
8905
8906 Actual := First (Actuals);
8907 while Present (Actual) loop
8908 Analyze (Actual);
8909 Next (Actual);
8910 end loop;
8911
8912 Prepend (Dummy, Actuals);
758c442c
GD
8913
8914 Call_Node :=
8915 Make_Function_Call (Loc,
8b4230c8 8916 Name => New_Copy (Subprog),
758c442c
GD
8917 Parameter_Associations => Actuals);
8918
d469eabe 8919 -- Parameterless call: Obj.F is rewritten as F (Obj)
35ae2ed8 8920
4c46b835
AC
8921 else
8922 Node_To_Replace := N;
8923
8924 Call_Node :=
8925 Make_Function_Call (Loc,
8b4230c8 8926 Name => New_Copy (Subprog),
ec6078e3 8927 Parameter_Associations => New_List (Dummy));
4c46b835
AC
8928 end if;
8929 end Transform_Object_Operation;
35ae2ed8
AC
8930
8931 ------------------------------
8932 -- Try_Class_Wide_Operation --
8933 ------------------------------
8934
8935 function Try_Class_Wide_Operation
4c46b835
AC
8936 (Call_Node : Node_Id;
8937 Node_To_Replace : Node_Id) return Boolean
35ae2ed8 8938 is
0a36105d
JM
8939 Anc_Type : Entity_Id;
8940 Matching_Op : Entity_Id := Empty;
8941 Error : Boolean;
8942
8943 procedure Traverse_Homonyms
8944 (Anc_Type : Entity_Id;
8945 Error : out Boolean);
8946 -- Traverse the homonym chain of the subprogram searching for those
8947 -- homonyms whose first formal has the Anc_Type's class-wide type,
d469eabe
HK
8948 -- or an anonymous access type designating the class-wide type. If
8949 -- an ambiguity is detected, then Error is set to True.
0a36105d
JM
8950
8951 procedure Traverse_Interfaces
8952 (Anc_Type : Entity_Id;
8953 Error : out Boolean);
8954 -- Traverse the list of interfaces, if any, associated with Anc_Type
8955 -- and search for acceptable class-wide homonyms associated with each
8956 -- interface. If an ambiguity is detected, then Error is set to True.
8957
8958 -----------------------
8959 -- Traverse_Homonyms --
8960 -----------------------
8961
8962 procedure Traverse_Homonyms
8963 (Anc_Type : Entity_Id;
8964 Error : out Boolean)
8965 is
118f2d8b 8966 function First_Formal_Match
61770974
HK
8967 (Subp_Id : Entity_Id;
8968 Typ : Entity_Id) return Boolean;
8969 -- Predicate to verify that the first foramal of class-wide
8970 -- subprogram Subp_Id matches type Typ of the prefix.
118f2d8b
ES
8971
8972 ------------------------
8973 -- First_Formal_Match --
8974 ------------------------
8975
8976 function First_Formal_Match
61770974
HK
8977 (Subp_Id : Entity_Id;
8978 Typ : Entity_Id) return Boolean
118f2d8b 8979 is
61770974
HK
8980 Ctrl : constant Entity_Id := First_Formal (Subp_Id);
8981
118f2d8b 8982 begin
61770974
HK
8983 return
8984 Present (Ctrl)
8985 and then
8986 (Base_Type (Etype (Ctrl)) = Typ
8987 or else
8988 (Ekind (Etype (Ctrl)) = E_Anonymous_Access_Type
8989 and then
8990 Base_Type (Designated_Type (Etype (Ctrl))) =
8991 Typ));
118f2d8b
ES
8992 end First_Formal_Match;
8993
61770974 8994 -- Local variables
ec6078e3 8995
61770974 8996 CW_Typ : constant Entity_Id := Class_Wide_Type (Anc_Type);
b67a385c 8997
61770974
HK
8998 Candidate : Entity_Id;
8999 -- If homonym is a renaming, examine the renamed program
9000
9001 Hom : Entity_Id;
9002 Hom_Ref : Node_Id;
9003 Success : Boolean;
9004
9005 -- Start of processing for Traverse_Homonyms
9006
9007 begin
9008 Error := False;
401093c1 9009
383e179e
AC
9010 -- Find a non-hidden operation whose first parameter is of the
9011 -- class-wide type, a subtype thereof, or an anonymous access
a68d415b 9012 -- to same. If in an instance, the operation can be considered
8b4230c8
AC
9013 -- even if hidden (it may be hidden because the instantiation
9014 -- is expanded after the containing package has been analyzed).
3bb9bd7d
ES
9015 -- If the subprogram is a generic actual in an enclosing instance,
9016 -- it appears as a renaming that is a candidate interpretation as
9017 -- well.
401093c1 9018
61770974 9019 Hom := Current_Entity (Subprog);
35ae2ed8 9020 while Present (Hom) loop
6a2e4f0b 9021 if Ekind_In (Hom, E_Procedure, E_Function)
118f2d8b
ES
9022 and then Present (Renamed_Entity (Hom))
9023 and then Is_Generic_Actual_Subprogram (Hom)
3bb9bd7d 9024 and then In_Open_Scopes (Scope (Hom))
118f2d8b
ES
9025 then
9026 Candidate := Renamed_Entity (Hom);
9027 else
9028 Candidate := Hom;
9029 end if;
9030
61770974 9031 if Ekind_In (Candidate, E_Function, E_Procedure)
118f2d8b
ES
9032 and then (not Is_Hidden (Candidate) or else In_Instance)
9033 and then Scope (Candidate) = Scope (Base_Type (Anc_Type))
61770974 9034 and then First_Formal_Match (Candidate, CW_Typ)
35ae2ed8 9035 then
88f47280
AC
9036 -- If the context is a procedure call, ignore functions
9037 -- in the name of the call.
9038
118f2d8b 9039 if Ekind (Candidate) = E_Function
88f47280
AC
9040 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
9041 and then N = Name (Parent (N))
9042 then
9043 goto Next_Hom;
11fa950b
AC
9044
9045 -- If the context is a function call, ignore procedures
9046 -- in the name of the call.
9047
118f2d8b 9048 elsif Ekind (Candidate) = E_Procedure
11fa950b
AC
9049 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
9050 then
9051 goto Next_Hom;
88f47280
AC
9052 end if;
9053
61770974 9054 Set_Etype (Call_Node, Any_Type);
0a36105d
JM
9055 Set_Is_Overloaded (Call_Node, False);
9056 Success := False;
4c46b835 9057
0a36105d 9058 if No (Matching_Op) then
118f2d8b 9059 Hom_Ref := New_Occurrence_Of (Candidate, Sloc (Subprog));
4c46b835 9060
61770974
HK
9061 Set_Etype (Call_Node, Any_Type);
9062 Set_Name (Call_Node, Hom_Ref);
9063 Set_Parent (Call_Node, Parent (Node_To_Replace));
4c46b835 9064
0a36105d
JM
9065 Analyze_One_Call
9066 (N => Call_Node,
118f2d8b 9067 Nam => Candidate,
0a36105d
JM
9068 Report => Report_Error,
9069 Success => Success,
9070 Skip_First => True);
4c46b835 9071
0a36105d 9072 Matching_Op :=
118f2d8b 9073 Valid_Candidate (Success, Call_Node, Candidate);
4c46b835 9074
0a36105d
JM
9075 else
9076 Analyze_One_Call
9077 (N => Call_Node,
118f2d8b 9078 Nam => Candidate,
0a36105d
JM
9079 Report => Report_Error,
9080 Success => Success,
9081 Skip_First => True);
9082
15529d0a
PMR
9083 -- The same operation may be encountered on two homonym
9084 -- traversals, before and after looking at interfaces.
9085 -- Check for this case before reporting a real ambiguity.
9086
118f2d8b 9087 if Present
61770974 9088 (Valid_Candidate (Success, Call_Node, Candidate))
0a36105d 9089 and then Nkind (Call_Node) /= N_Function_Call
118f2d8b 9090 and then Candidate /= Matching_Op
0a36105d 9091 then
ed2233dc 9092 Error_Msg_NE ("ambiguous call to&", N, Hom);
0a36105d
JM
9093 Report_Ambiguity (Matching_Op);
9094 Report_Ambiguity (Hom);
9095 Error := True;
9096 return;
9097 end if;
35ae2ed8
AC
9098 end if;
9099 end if;
9100
88f47280
AC
9101 <<Next_Hom>>
9102 Hom := Homonym (Hom);
35ae2ed8 9103 end loop;
0a36105d
JM
9104 end Traverse_Homonyms;
9105
9106 -------------------------
9107 -- Traverse_Interfaces --
9108 -------------------------
35ae2ed8 9109
0a36105d
JM
9110 procedure Traverse_Interfaces
9111 (Anc_Type : Entity_Id;
9112 Error : out Boolean)
9113 is
0a36105d
JM
9114 Intface_List : constant List_Id :=
9115 Abstract_Interface_List (Anc_Type);
d469eabe 9116 Intface : Node_Id;
0a36105d
JM
9117
9118 begin
9119 Error := False;
9120
9121 if Is_Non_Empty_List (Intface_List) then
9122 Intface := First (Intface_List);
9123 while Present (Intface) loop
9124
9125 -- Look for acceptable class-wide homonyms associated with
9126 -- the interface.
9127
9128 Traverse_Homonyms (Etype (Intface), Error);
9129
9130 if Error then
9131 return;
9132 end if;
9133
9134 -- Continue the search by looking at each of the interface's
9135 -- associated interface ancestors.
9136
9137 Traverse_Interfaces (Etype (Intface), Error);
9138
9139 if Error then
9140 return;
9141 end if;
9142
9143 Next (Intface);
9144 end loop;
9145 end if;
9146 end Traverse_Interfaces;
9147
9148 -- Start of processing for Try_Class_Wide_Operation
9149
9150 begin
8cf23b91
AC
9151 -- If we are searching only for conflicting class-wide subprograms
9152 -- then initialize directly Matching_Op with the target entity.
9153
9154 if CW_Test_Only then
9155 Matching_Op := Entity (Selector_Name (N));
9156 end if;
9157
d469eabe
HK
9158 -- Loop through ancestor types (including interfaces), traversing
9159 -- the homonym chain of the subprogram, trying out those homonyms
9160 -- whose first formal has the class-wide type of the ancestor, or
9161 -- an anonymous access type designating the class-wide type.
0a36105d
JM
9162
9163 Anc_Type := Obj_Type;
9164 loop
9165 -- Look for a match among homonyms associated with the ancestor
9166
9167 Traverse_Homonyms (Anc_Type, Error);
9168
9169 if Error then
9170 return True;
9171 end if;
9172
9173 -- Continue the search for matches among homonyms associated with
9174 -- any interfaces implemented by the ancestor.
9175
9176 Traverse_Interfaces (Anc_Type, Error);
9177
9178 if Error then
9179 return True;
9180 end if;
35ae2ed8 9181
4c46b835
AC
9182 exit when Etype (Anc_Type) = Anc_Type;
9183 Anc_Type := Etype (Anc_Type);
35ae2ed8
AC
9184 end loop;
9185
0a36105d
JM
9186 if Present (Matching_Op) then
9187 Set_Etype (Call_Node, Etype (Matching_Op));
9188 end if;
ec6078e3 9189
0a36105d 9190 return Present (Matching_Op);
35ae2ed8
AC
9191 end Try_Class_Wide_Operation;
9192
0a36105d
JM
9193 -----------------------------------
9194 -- Try_One_Prefix_Interpretation --
9195 -----------------------------------
9196
9197 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
48c8c473 9198 Prev_Obj_Type : constant Entity_Id := Obj_Type;
e3d9f448
AC
9199 -- If the interpretation does not have a valid candidate type,
9200 -- preserve current value of Obj_Type for subsequent errors.
9201
0a36105d
JM
9202 begin
9203 Obj_Type := T;
9204
9205 if Is_Access_Type (Obj_Type) then
9206 Obj_Type := Designated_Type (Obj_Type);
9207 end if;
9208
48c8c473
AC
9209 if Ekind_In (Obj_Type, E_Private_Subtype,
9210 E_Record_Subtype_With_Private)
9211 then
0a36105d
JM
9212 Obj_Type := Base_Type (Obj_Type);
9213 end if;
9214
9215 if Is_Class_Wide_Type (Obj_Type) then
9216 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
9217 end if;
9218
9219 -- The type may have be obtained through a limited_with clause,
9220 -- in which case the primitive operations are available on its
a316b3fc 9221 -- nonlimited view. If still incomplete, retrieve full view.
0a36105d
JM
9222
9223 if Ekind (Obj_Type) = E_Incomplete_Type
7b56a91b 9224 and then From_Limited_With (Obj_Type)
47346923 9225 and then Has_Non_Limited_View (Obj_Type)
0a36105d 9226 then
401093c1 9227 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
0a36105d
JM
9228 end if;
9229
9230 -- If the object is not tagged, or the type is still an incomplete
48c8c473
AC
9231 -- type, this is not a prefixed call. Restore the previous type as
9232 -- the current one is not a legal candidate.
0a36105d
JM
9233
9234 if not Is_Tagged_Type (Obj_Type)
9235 or else Is_Incomplete_Type (Obj_Type)
9236 then
e3d9f448 9237 Obj_Type := Prev_Obj_Type;
0a36105d
JM
9238 return;
9239 end if;
9240
11fa950b
AC
9241 declare
9242 Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node);
5612989e
PMR
9243 Ignore : Boolean;
9244 Prim_Result : Boolean := False;
11fa950b
AC
9245
9246 begin
8cf23b91
AC
9247 if not CW_Test_Only then
9248 Prim_Result :=
9249 Try_Primitive_Operation
9250 (Call_Node => New_Call_Node,
9251 Node_To_Replace => Node_To_Replace);
9252 end if;
11fa950b
AC
9253
9254 -- Check if there is a class-wide subprogram covering the
9255 -- primitive. This check must be done even if a candidate
9256 -- was found in order to report ambiguous calls.
9257
48c8c473 9258 if not Prim_Result then
5612989e 9259 Ignore :=
11fa950b
AC
9260 Try_Class_Wide_Operation
9261 (Call_Node => New_Call_Node,
9262 Node_To_Replace => Node_To_Replace);
9263
9264 -- If we found a primitive we search for class-wide subprograms
9265 -- using a duplicate of the call node (done to avoid missing its
9266 -- decoration if there is no ambiguity).
9267
9268 else
5612989e 9269 Ignore :=
11fa950b
AC
9270 Try_Class_Wide_Operation
9271 (Call_Node => Dup_Call_Node,
9272 Node_To_Replace => Node_To_Replace);
9273 end if;
9274 end;
0a36105d
JM
9275 end Try_One_Prefix_Interpretation;
9276
4c46b835
AC
9277 -----------------------------
9278 -- Try_Primitive_Operation --
9279 -----------------------------
35ae2ed8 9280
4c46b835
AC
9281 function Try_Primitive_Operation
9282 (Call_Node : Node_Id;
9283 Node_To_Replace : Node_Id) return Boolean
35ae2ed8 9284 is
6e73e3ab
AC
9285 Elmt : Elmt_Id;
9286 Prim_Op : Entity_Id;
0a36105d
JM
9287 Matching_Op : Entity_Id := Empty;
9288 Prim_Op_Ref : Node_Id := Empty;
9289
8b4230c8 9290 Corr_Type : Entity_Id := Empty;
0a36105d
JM
9291 -- If the prefix is a synchronized type, the controlling type of
9292 -- the primitive operation is the corresponding record type, else
9293 -- this is the object type itself.
9294
8b4230c8 9295 Success : Boolean := False;
35ae2ed8 9296
401093c1
ES
9297 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
9298 -- For tagged types the candidate interpretations are found in
9299 -- the list of primitive operations of the type and its ancestors.
9300 -- For formal tagged types we have to find the operations declared
9301 -- in the same scope as the type (including in the generic formal
9302 -- part) because the type itself carries no primitive operations,
9303 -- except for formal derived types that inherit the operations of
9304 -- the parent and progenitors.
8b4230c8 9305 --
d469eabe
HK
9306 -- If the context is a generic subprogram body, the generic formals
9307 -- are visible by name, but are not in the entity list of the
9308 -- subprogram because that list starts with the subprogram formals.
9309 -- We retrieve the candidate operations from the generic declaration.
401093c1 9310
84dad556
AC
9311 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id;
9312 -- Prefix notation can also be used on operations that are not
9313 -- primitives of the type, but are declared in the same immediate
9314 -- declarative part, which can only mean the corresponding package
0e3a687f 9315 -- body (see RM 4.1.3 (9.2/3)). If we are in that body we extend the
84dad556
AC
9316 -- list of primitives with body operations with the same name that
9317 -- may be candidates, so that Try_Primitive_Operations can examine
9318 -- them if no real primitive is found.
9319
dfcfdc0a
AC
9320 function Is_Private_Overriding (Op : Entity_Id) return Boolean;
9321 -- An operation that overrides an inherited operation in the private
9322 -- part of its package may be hidden, but if the inherited operation
9323 -- is visible a direct call to it will dispatch to the private one,
9324 -- which is therefore a valid candidate.
9325
42f11e4c
AC
9326 function Names_Match
9327 (Obj_Type : Entity_Id;
9328 Prim_Op : Entity_Id;
9329 Subprog : Entity_Id) return Boolean;
9330 -- Return True if the names of Prim_Op and Subprog match. If Obj_Type
9331 -- is a protected type then compare also the original name of Prim_Op
9332 -- with the name of Subprog (since the expander may have added a
9333 -- prefix to its original name --see Exp_Ch9.Build_Selected_Name).
9334
ec6078e3
ES
9335 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
9336 -- Verify that the prefix, dereferenced if need be, is a valid
9337 -- controlling argument in a call to Op. The remaining actuals
9338 -- are checked in the subsequent call to Analyze_One_Call.
35ae2ed8 9339
401093c1
ES
9340 ------------------------------
9341 -- Collect_Generic_Type_Ops --
9342 ------------------------------
9343
9344 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
9345 Bas : constant Entity_Id := Base_Type (T);
9346 Candidates : constant Elist_Id := New_Elmt_List;
9347 Subp : Entity_Id;
9348 Formal : Entity_Id;
9349
d469eabe
HK
9350 procedure Check_Candidate;
9351 -- The operation is a candidate if its first parameter is a
9352 -- controlling operand of the desired type.
9353
9354 -----------------------
9355 -- Check_Candidate; --
9356 -----------------------
9357
9358 procedure Check_Candidate is
9359 begin
9360 Formal := First_Formal (Subp);
9361
9362 if Present (Formal)
9363 and then Is_Controlling_Formal (Formal)
9364 and then
9365 (Base_Type (Etype (Formal)) = Bas
9366 or else
9367 (Is_Access_Type (Etype (Formal))
9368 and then Designated_Type (Etype (Formal)) = Bas))
9369 then
9370 Append_Elmt (Subp, Candidates);
9371 end if;
9372 end Check_Candidate;
9373
9374 -- Start of processing for Collect_Generic_Type_Ops
9375
401093c1
ES
9376 begin
9377 if Is_Derived_Type (T) then
9378 return Primitive_Operations (T);
9379
bce79204
AC
9380 elsif Ekind_In (Scope (T), E_Procedure, E_Function) then
9381
d469eabe
HK
9382 -- Scan the list of generic formals to find subprograms
9383 -- that may have a first controlling formal of the type.
9384
8b4230c8
AC
9385 if Nkind (Unit_Declaration_Node (Scope (T))) =
9386 N_Generic_Subprogram_Declaration
bb10b891
AC
9387 then
9388 declare
9389 Decl : Node_Id;
9390
9391 begin
9392 Decl :=
9393 First (Generic_Formal_Declarations
9394 (Unit_Declaration_Node (Scope (T))));
9395 while Present (Decl) loop
9396 if Nkind (Decl) in N_Formal_Subprogram_Declaration then
9397 Subp := Defining_Entity (Decl);
9398 Check_Candidate;
9399 end if;
d469eabe 9400
bb10b891
AC
9401 Next (Decl);
9402 end loop;
9403 end;
9404 end if;
d469eabe
HK
9405 return Candidates;
9406
401093c1
ES
9407 else
9408 -- Scan the list of entities declared in the same scope as
9409 -- the type. In general this will be an open scope, given that
9410 -- the call we are analyzing can only appear within a generic
9411 -- declaration or body (either the one that declares T, or a
9412 -- child unit).
9413
bb10b891
AC
9414 -- For a subtype representing a generic actual type, go to the
9415 -- base type.
9416
9417 if Is_Generic_Actual_Type (T) then
9418 Subp := First_Entity (Scope (Base_Type (T)));
9419 else
9420 Subp := First_Entity (Scope (T));
9421 end if;
9422
401093c1
ES
9423 while Present (Subp) loop
9424 if Is_Overloadable (Subp) then
d469eabe 9425 Check_Candidate;
401093c1
ES
9426 end if;
9427
9428 Next_Entity (Subp);
9429 end loop;
9430
9431 return Candidates;
9432 end if;
9433 end Collect_Generic_Type_Ops;
9434
84dad556
AC
9435 ----------------------------
9436 -- Extended_Primitive_Ops --
9437 ----------------------------
9438
9439 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id is
9440 Type_Scope : constant Entity_Id := Scope (T);
0e3a687f 9441 Op_List : Elist_Id := Primitive_Operations (T);
84dad556 9442 begin
a92db262 9443 if Is_Package_Or_Generic_Package (Type_Scope)
0e3a687f
BD
9444 and then ((In_Package_Body (Type_Scope)
9445 and then In_Open_Scopes (Type_Scope)) or else In_Instance_Body)
84dad556 9446 then
0e3a687f 9447 -- Retrieve list of declarations of package body if possible
84dad556 9448
0e3a687f
BD
9449 declare
9450 The_Body : constant Node_Id :=
9451 Corresponding_Body (Unit_Declaration_Node (Type_Scope));
9452 begin
9453 if Present (The_Body) then
9454 declare
9455 Body_Decls : constant List_Id :=
9456 Declarations (Unit_Declaration_Node (The_Body));
9457 Op_Found : Boolean := False;
9458 Op : Entity_Id := Current_Entity (Subprog);
9459 begin
9460 while Present (Op) loop
9461 if Comes_From_Source (Op)
9462 and then Is_Overloadable (Op)
9463
9464 -- Exclude overriding primitive operations of a
9465 -- type extension declared in the package body,
9466 -- to prevent duplicates in extended list.
9467
9468 and then not Is_Primitive (Op)
9469 and then Is_List_Member
9470 (Unit_Declaration_Node (Op))
9471 and then List_Containing
9472 (Unit_Declaration_Node (Op)) = Body_Decls
9473 then
9474 if not Op_Found then
9475 -- Copy list of primitives so it is not
9476 -- affected for other uses.
84dad556 9477
0e3a687f
BD
9478 Op_List := New_Copy_Elist (Op_List);
9479 Op_Found := True;
9480 end if;
84dad556 9481
0e3a687f
BD
9482 Append_Elmt (Op, Op_List);
9483 end if;
84dad556 9484
0e3a687f
BD
9485 Op := Homonym (Op);
9486 end loop;
9487 end;
9488 end if;
9489 end;
84dad556
AC
9490 end if;
9491
9492 return Op_List;
9493 end Extended_Primitive_Ops;
9494
dfcfdc0a
AC
9495 ---------------------------
9496 -- Is_Private_Overriding --
9497 ---------------------------
9498
9499 function Is_Private_Overriding (Op : Entity_Id) return Boolean is
40c21e91 9500 Visible_Op : Entity_Id;
dfcfdc0a
AC
9501
9502 begin
40c21e91
PMR
9503 -- The subprogram may be overloaded with both visible and private
9504 -- entities with the same name. We have to scan the chain of
9505 -- homonyms to determine whether there is a previous implicit
9506 -- declaration in the same scope that is overridden by the
9507 -- private candidate.
9508
9509 Visible_Op := Homonym (Op);
9510 while Present (Visible_Op) loop
9511 if Scope (Op) /= Scope (Visible_Op) then
9512 return False;
9513
9514 elsif not Comes_From_Source (Visible_Op)
9515 and then Alias (Visible_Op) = Op
9516 and then not Is_Hidden (Visible_Op)
9517 then
9518 return True;
9519 end if;
9520
9521 Visible_Op := Homonym (Visible_Op);
9522 end loop;
9523
9524 return False;
dfcfdc0a
AC
9525 end Is_Private_Overriding;
9526
42f11e4c
AC
9527 -----------------
9528 -- Names_Match --
9529 -----------------
9530
9531 function Names_Match
9532 (Obj_Type : Entity_Id;
9533 Prim_Op : Entity_Id;
9534 Subprog : Entity_Id) return Boolean is
9535 begin
9536 -- Common case: exact match
9537
9538 if Chars (Prim_Op) = Chars (Subprog) then
9539 return True;
9540
9541 -- For protected type primitives the expander may have built the
9542 -- name of the dispatching primitive prepending the type name to
9543 -- avoid conflicts with the name of the protected subprogram (see
9544 -- Exp_Ch9.Build_Selected_Name).
9545
9546 elsif Is_Protected_Type (Obj_Type) then
bac5ba15
AC
9547 return
9548 Present (Original_Protected_Subprogram (Prim_Op))
9549 and then Chars (Original_Protected_Subprogram (Prim_Op)) =
9550 Chars (Subprog);
118f2d8b
ES
9551
9552 -- In an instance, the selector name may be a generic actual that
9553 -- renames a primitive operation of the type of the prefix.
9554
9555 elsif In_Instance and then Present (Current_Entity (Subprog)) then
9556 declare
9557 Subp : constant Entity_Id := Current_Entity (Subprog);
9558 begin
9559 if Present (Subp)
9560 and then Is_Subprogram (Subp)
9561 and then Present (Renamed_Entity (Subp))
9562 and then Is_Generic_Actual_Subprogram (Subp)
9563 and then Chars (Renamed_Entity (Subp)) = Chars (Prim_Op)
9564 then
9565 return True;
9566 end if;
9567 end;
42f11e4c
AC
9568 end if;
9569
9570 return False;
9571 end Names_Match;
9572
ec6078e3
ES
9573 -----------------------------
9574 -- Valid_First_Argument_Of --
9575 -----------------------------
35ae2ed8 9576
ec6078e3 9577 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
9febb58f 9578 Typ : Entity_Id := Etype (First_Formal (Op));
35ae2ed8 9579
ec6078e3 9580 begin
9febb58f
JM
9581 if Is_Concurrent_Type (Typ)
9582 and then Present (Corresponding_Record_Type (Typ))
9583 then
9584 Typ := Corresponding_Record_Type (Typ);
9585 end if;
9586
9313a26a
AC
9587 -- Simple case. Object may be a subtype of the tagged type or may
9588 -- be the corresponding record of a synchronized type.
5d09245e 9589
aab883ec 9590 return Obj_Type = Typ
d469eabe 9591 or else Base_Type (Obj_Type) = Typ
0a36105d
JM
9592 or else Corr_Type = Typ
9593
913e4b36 9594 -- Object may be of a derived type whose parent has unknown
9313a26a
AC
9595 -- discriminants, in which case the type matches the underlying
9596 -- record view of its base.
913e4b36 9597
9313a26a
AC
9598 or else
9599 (Has_Unknown_Discriminants (Typ)
9600 and then Typ = Underlying_Record_View (Base_Type (Obj_Type)))
913e4b36 9601
0a36105d 9602 -- Prefix can be dereferenced
725e2a15 9603
ec6078e3 9604 or else
0a36105d
JM
9605 (Is_Access_Type (Corr_Type)
9606 and then Designated_Type (Corr_Type) = Typ)
5d09245e 9607
9313a26a
AC
9608 -- Formal is an access parameter, for which the object can
9609 -- provide an access.
35ae2ed8 9610
ec6078e3
ES
9611 or else
9612 (Ekind (Typ) = E_Anonymous_Access_Type
9fde638d
RD
9613 and then
9614 Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
ec6078e3 9615 end Valid_First_Argument_Of;
35ae2ed8 9616
ec6078e3 9617 -- Start of processing for Try_Primitive_Operation
35ae2ed8 9618
ec6078e3 9619 begin
d469eabe 9620 -- Look for subprograms in the list of primitive operations. The name
0a36105d
JM
9621 -- must be identical, and the kind of call indicates the expected
9622 -- kind of operation (function or procedure). If the type is a
d469eabe 9623 -- (tagged) synchronized type, the primitive ops are attached to the
b4592168 9624 -- corresponding record (base) type.
aab883ec
ES
9625
9626 if Is_Concurrent_Type (Obj_Type) then
bb10b891
AC
9627 if Present (Corresponding_Record_Type (Obj_Type)) then
9628 Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
48c8c473 9629 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
bb10b891
AC
9630 else
9631 Corr_Type := Obj_Type;
48c8c473 9632 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
15e4986c
JM
9633 end if;
9634
401093c1 9635 elsif not Is_Generic_Type (Obj_Type) then
0a36105d 9636 Corr_Type := Obj_Type;
48c8c473 9637 Elmt := First_Elmt (Extended_Primitive_Ops (Obj_Type));
401093c1
ES
9638
9639 else
9640 Corr_Type := Obj_Type;
48c8c473 9641 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
aab883ec 9642 end if;
35ae2ed8 9643
ec6078e3
ES
9644 while Present (Elmt) loop
9645 Prim_Op := Node (Elmt);
9646
42f11e4c 9647 if Names_Match (Obj_Type, Prim_Op, Subprog)
ec6078e3
ES
9648 and then Present (First_Formal (Prim_Op))
9649 and then Valid_First_Argument_Of (Prim_Op)
fe45e59e 9650 and then
7415029d 9651 (Nkind (Call_Node) = N_Function_Call)
48c8c473 9652 =
8b4230c8 9653 (Ekind (Prim_Op) = E_Function)
ec6078e3 9654 then
b67a385c 9655 -- Ada 2005 (AI-251): If this primitive operation corresponds
8b4230c8 9656 -- to an immediate ancestor interface there is no need to add
b67a385c
ES
9657 -- it to the list of interpretations; the corresponding aliased
9658 -- primitive is also in this list of primitive operations and
9659 -- will be used instead.
fe45e59e 9660
ce2b6ba5
JM
9661 if (Present (Interface_Alias (Prim_Op))
9662 and then Is_Ancestor (Find_Dispatching_Type
9663 (Alias (Prim_Op)), Corr_Type))
0a36105d 9664
dfcfdc0a
AC
9665 -- Do not consider hidden primitives unless the type is in an
9666 -- open scope or we are within an instance, where visibility
9667 -- is known to be correct, or else if this is an overriding
9668 -- operation in the private part for an inherited operation.
0a36105d 9669
dfcfdc0a
AC
9670 or else (Is_Hidden (Prim_Op)
9671 and then not Is_Immediately_Visible (Obj_Type)
9672 and then not In_Instance
9673 and then not Is_Private_Overriding (Prim_Op))
fe45e59e
ES
9674 then
9675 goto Continue;
9676 end if;
9677
0a36105d
JM
9678 Set_Etype (Call_Node, Any_Type);
9679 Set_Is_Overloaded (Call_Node, False);
9680
9681 if No (Matching_Op) then
e4494292 9682 Prim_Op_Ref := New_Occurrence_Of (Prim_Op, Sloc (Subprog));
b67a385c 9683 Candidate := Prim_Op;
35ae2ed8 9684
fe45e59e 9685 Set_Parent (Call_Node, Parent (Node_To_Replace));
35ae2ed8 9686
fe45e59e 9687 Set_Name (Call_Node, Prim_Op_Ref);
0a36105d 9688 Success := False;
35ae2ed8 9689
fe45e59e
ES
9690 Analyze_One_Call
9691 (N => Call_Node,
9692 Nam => Prim_Op,
b67a385c 9693 Report => Report_Error,
fe45e59e
ES
9694 Success => Success,
9695 Skip_First => True);
35ae2ed8 9696
0a36105d 9697 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
fe45e59e 9698
d469eabe
HK
9699 -- More than one interpretation, collect for subsequent
9700 -- disambiguation. If this is a procedure call and there
9701 -- is another match, report ambiguity now.
0a36105d 9702
d469eabe 9703 else
0a36105d
JM
9704 Analyze_One_Call
9705 (N => Call_Node,
9706 Nam => Prim_Op,
9707 Report => Report_Error,
9708 Success => Success,
9709 Skip_First => True);
fe45e59e 9710
0a36105d
JM
9711 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
9712 and then Nkind (Call_Node) /= N_Function_Call
9713 then
ed2233dc 9714 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
0a36105d
JM
9715 Report_Ambiguity (Matching_Op);
9716 Report_Ambiguity (Prim_Op);
9717 return True;
9718 end if;
4c46b835
AC
9719 end if;
9720 end if;
35ae2ed8 9721
fe45e59e 9722 <<Continue>>
4c46b835
AC
9723 Next_Elmt (Elmt);
9724 end loop;
35ae2ed8 9725
0a36105d
JM
9726 if Present (Matching_Op) then
9727 Set_Etype (Call_Node, Etype (Matching_Op));
fe45e59e
ES
9728 end if;
9729
0a36105d 9730 return Present (Matching_Op);
4c46b835 9731 end Try_Primitive_Operation;
35ae2ed8 9732
48c8c473
AC
9733 ---------------------
9734 -- Valid_Candidate --
9735 ---------------------
9736
9737 function Valid_Candidate
9738 (Success : Boolean;
9739 Call : Node_Id;
9740 Subp : Entity_Id) return Entity_Id
9741 is
9742 Arr_Type : Entity_Id;
9743 Comp_Type : Entity_Id;
9744
9745 begin
9746 -- If the subprogram is a valid interpretation, record it in global
9747 -- variable Subprog, to collect all possible overloadings.
9748
9749 if Success then
9750 if Subp /= Entity (Subprog) then
9751 Add_One_Interp (Subprog, Subp, Etype (Subp));
9752 end if;
9753 end if;
9754
9755 -- If the call may be an indexed call, retrieve component type of
9756 -- resulting expression, and add possible interpretation.
9757
9758 Arr_Type := Empty;
9759 Comp_Type := Empty;
9760
9761 if Nkind (Call) = N_Function_Call
9762 and then Nkind (Parent (N)) = N_Indexed_Component
9763 and then Needs_One_Actual (Subp)
9764 then
9765 if Is_Array_Type (Etype (Subp)) then
9766 Arr_Type := Etype (Subp);
9767
9768 elsif Is_Access_Type (Etype (Subp))
9769 and then Is_Array_Type (Designated_Type (Etype (Subp)))
9770 then
9771 Arr_Type := Designated_Type (Etype (Subp));
9772 end if;
9773 end if;
9774
9775 if Present (Arr_Type) then
9776
9777 -- Verify that the actuals (excluding the object) match the types
9778 -- of the indexes.
9779
9780 declare
9781 Actual : Node_Id;
9782 Index : Node_Id;
9783
9784 begin
9785 Actual := Next (First_Actual (Call));
9786 Index := First_Index (Arr_Type);
9787 while Present (Actual) and then Present (Index) loop
9788 if not Has_Compatible_Type (Actual, Etype (Index)) then
9789 Arr_Type := Empty;
9790 exit;
9791 end if;
9792
9793 Next_Actual (Actual);
9794 Next_Index (Index);
9795 end loop;
9796
9797 if No (Actual)
9798 and then No (Index)
9799 and then Present (Arr_Type)
9800 then
9801 Comp_Type := Component_Type (Arr_Type);
9802 end if;
9803 end;
9804
9805 if Present (Comp_Type)
9806 and then Etype (Subprog) /= Comp_Type
9807 then
9808 Add_One_Interp (Subprog, Subp, Comp_Type);
9809 end if;
9810 end if;
9811
9812 if Etype (Call) /= Any_Type then
9813 return Subp;
9814 else
9815 return Empty;
9816 end if;
9817 end Valid_Candidate;
9818
4c46b835 9819 -- Start of processing for Try_Object_Operation
35ae2ed8 9820
4c46b835 9821 begin
0a36105d 9822 Analyze_Expression (Obj);
ec6078e3 9823
0a36105d 9824 -- Analyze the actuals if node is known to be a subprogram call
28d6470f
JM
9825
9826 if Is_Subprg_Call and then N = Name (Parent (N)) then
9827 Actual := First (Parameter_Associations (Parent (N)));
9828 while Present (Actual) loop
725e2a15 9829 Analyze_Expression (Actual);
28d6470f
JM
9830 Next (Actual);
9831 end loop;
9832 end if;
5d09245e 9833
ec6078e3
ES
9834 -- Build a subprogram call node, using a copy of Obj as its first
9835 -- actual. This is a placeholder, to be replaced by an explicit
9836 -- dereference when needed.
4c46b835 9837
ec6078e3
ES
9838 Transform_Object_Operation
9839 (Call_Node => New_Call_Node,
0a36105d 9840 Node_To_Replace => Node_To_Replace);
4c46b835 9841
ec6078e3 9842 Set_Etype (New_Call_Node, Any_Type);
0a36105d 9843 Set_Etype (Subprog, Any_Type);
ec6078e3 9844 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
4c46b835 9845
0a36105d
JM
9846 if not Is_Overloaded (Obj) then
9847 Try_One_Prefix_Interpretation (Obj_Type);
ec6078e3 9848
0a36105d
JM
9849 else
9850 declare
9851 I : Interp_Index;
9852 It : Interp;
9853 begin
9854 Get_First_Interp (Obj, I, It);
9855 while Present (It.Nam) loop
9856 Try_One_Prefix_Interpretation (It.Typ);
9857 Get_Next_Interp (I, It);
9858 end loop;
9859 end;
9860 end if;
9861
9862 if Etype (New_Call_Node) /= Any_Type then
8cf23b91
AC
9863
9864 -- No need to complete the tree transformations if we are only
9865 -- searching for conflicting class-wide subprograms
9866
9867 if CW_Test_Only then
9868 return False;
9869 else
9870 Complete_Object_Operation
9871 (Call_Node => New_Call_Node,
9872 Node_To_Replace => Node_To_Replace);
9873 return True;
9874 end if;
b67a385c
ES
9875
9876 elsif Present (Candidate) then
9877
9878 -- The argument list is not type correct. Re-analyze with error
9879 -- reporting enabled, and use one of the possible candidates.
d469eabe 9880 -- In All_Errors_Mode, re-analyze all failed interpretations.
b67a385c
ES
9881
9882 if All_Errors_Mode then
9883 Report_Error := True;
9884 if Try_Primitive_Operation
8b4230c8
AC
9885 (Call_Node => New_Call_Node,
9886 Node_To_Replace => Node_To_Replace)
b67a385c
ES
9887
9888 or else
9889 Try_Class_Wide_Operation
9890 (Call_Node => New_Call_Node,
9891 Node_To_Replace => Node_To_Replace)
9892 then
9893 null;
9894 end if;
9895
9896 else
9897 Analyze_One_Call
9898 (N => New_Call_Node,
9899 Nam => Candidate,
9900 Report => True,
9901 Success => Success,
9902 Skip_First => True);
9903 end if;
9904
d469eabe
HK
9905 -- No need for further errors
9906
9907 return True;
b67a385c
ES
9908
9909 else
9910 -- There was no candidate operation, so report it as an error
9911 -- in the caller: Analyze_Selected_Component.
9912
9913 return False;
9914 end if;
35ae2ed8
AC
9915 end Try_Object_Operation;
9916
b4592168
GD
9917 ---------
9918 -- wpo --
9919 ---------
9920
9921 procedure wpo (T : Entity_Id) is
9922 Op : Entity_Id;
9923 E : Elmt_Id;
9924
9925 begin
9926 if not Is_Tagged_Type (T) then
9927 return;
9928 end if;
9929
9930 E := First_Elmt (Primitive_Operations (Base_Type (T)));
9931 while Present (E) loop
9932 Op := Node (E);
9933 Write_Int (Int (Op));
9934 Write_Str (" === ");
9935 Write_Name (Chars (Op));
9936 Write_Str (" in ");
9937 Write_Name (Chars (Scope (Op)));
9938 Next_Elmt (E);
9939 Write_Eol;
9940 end loop;
9941 end wpo;
9942
996ae0b0 9943end Sem_Ch4;