]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_ch4.adb
[Ada] Remove degenerate Treat_Fixed_As_Integer mechanism
[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-- --
1d005acc 9-- Copyright (C) 1992-2019, 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;
2824
3d918396
AC
2825 -- In ASIS mode within a generic, a prefixed call is analyzed and
2826 -- partially rewritten but the original indexed component has not
2827 -- yet been rewritten as a call. Perform the replacement now.
2828
2829 elsif Nkind (P) = N_Selected_Component
2830 and then Nkind (Parent (P)) = N_Function_Call
2831 and then ASIS_Mode
2832 then
2833 Rewrite (N, Parent (P));
2834 Analyze (N);
2835
996ae0b0
RK
2836 else
2837 -- Indexed component, slice, or a call to a member of a family
2838 -- entry, which will be converted to an entry call later.
fbf5a39b 2839
996ae0b0
RK
2840 Process_Indexed_Component_Or_Slice;
2841 end if;
2842 end if;
5f49133f
AC
2843
2844 Analyze_Dimension (N);
996ae0b0
RK
2845 end Analyze_Indexed_Component_Form;
2846
2847 ------------------------
2848 -- Analyze_Logical_Op --
2849 ------------------------
2850
2851 procedure Analyze_Logical_Op (N : Node_Id) is
2852 L : constant Node_Id := Left_Opnd (N);
2853 R : constant Node_Id := Right_Opnd (N);
2854 Op_Id : Entity_Id := Entity (N);
2855
2856 begin
2857 Set_Etype (N, Any_Type);
2858 Candidate_Type := Empty;
2859
2860 Analyze_Expression (L);
2861 Analyze_Expression (R);
2862
2863 if Present (Op_Id) then
2864
2865 if Ekind (Op_Id) = E_Operator then
2866 Find_Boolean_Types (L, R, Op_Id, N);
2867 else
2868 Add_One_Interp (N, Op_Id, Etype (Op_Id));
2869 end if;
2870
2871 else
2872 Op_Id := Get_Name_Entity_Id (Chars (N));
996ae0b0
RK
2873 while Present (Op_Id) loop
2874 if Ekind (Op_Id) = E_Operator then
2875 Find_Boolean_Types (L, R, Op_Id, N);
2876 else
2877 Analyze_User_Defined_Binary_Op (N, Op_Id);
2878 end if;
2879
2880 Op_Id := Homonym (Op_Id);
2881 end loop;
2882 end if;
2883
2884 Operator_Check (N);
22e89283 2885 Check_Function_Writable_Actuals (N);
996ae0b0
RK
2886 end Analyze_Logical_Op;
2887
2888 ---------------------------
2889 -- Analyze_Membership_Op --
2890 ---------------------------
2891
2892 procedure Analyze_Membership_Op (N : Node_Id) is
66150d01 2893 Loc : constant Source_Ptr := Sloc (N);
f2acf80c
AC
2894 L : constant Node_Id := Left_Opnd (N);
2895 R : constant Node_Id := Right_Opnd (N);
996ae0b0
RK
2896
2897 Index : Interp_Index;
2898 It : Interp;
2899 Found : Boolean := False;
2900 I_F : Interp_Index;
2901 T_F : Entity_Id;
2902
2903 procedure Try_One_Interp (T1 : Entity_Id);
2904 -- Routine to try one proposed interpretation. Note that the context
2905 -- of the operation plays no role in resolving the arguments, so that
2906 -- if there is more than one interpretation of the operands that is
2907 -- compatible with a membership test, the operation is ambiguous.
2908
4c46b835
AC
2909 --------------------
2910 -- Try_One_Interp --
2911 --------------------
2912
996ae0b0
RK
2913 procedure Try_One_Interp (T1 : Entity_Id) is
2914 begin
2915 if Has_Compatible_Type (R, T1) then
2916 if Found
2917 and then Base_Type (T1) /= Base_Type (T_F)
2918 then
2919 It := Disambiguate (L, I_F, Index, Any_Type);
2920
2921 if It = No_Interp then
2922 Ambiguous_Operands (N);
2923 Set_Etype (L, Any_Type);
2924 return;
2925
2926 else
2927 T_F := It.Typ;
2928 end if;
2929
2930 else
2931 Found := True;
2932 T_F := T1;
2933 I_F := Index;
2934 end if;
2935
2936 Set_Etype (L, T_F);
2937 end if;
996ae0b0
RK
2938 end Try_One_Interp;
2939
197e4514
AC
2940 procedure Analyze_Set_Membership;
2941 -- If a set of alternatives is present, analyze each and find the
2942 -- common type to which they must all resolve.
2943
2944 ----------------------------
2945 -- Analyze_Set_Membership --
2946 ----------------------------
2947
2948 procedure Analyze_Set_Membership is
2949 Alt : Node_Id;
2950 Index : Interp_Index;
2951 It : Interp;
197e4514
AC
2952 Candidate_Interps : Node_Id;
2953 Common_Type : Entity_Id := Empty;
2954
2955 begin
e917e3b8 2956 if Comes_From_Source (N) then
c86cf714 2957 Check_Compiler_Unit ("set membership", N);
e917e3b8
AC
2958 end if;
2959
197e4514
AC
2960 Analyze (L);
2961 Candidate_Interps := L;
2962
2963 if not Is_Overloaded (L) then
2964 Common_Type := Etype (L);
2965
2966 Alt := First (Alternatives (N));
2967 while Present (Alt) loop
2968 Analyze (Alt);
2969
2970 if not Has_Compatible_Type (Alt, Common_Type) then
2971 Wrong_Type (Alt, Common_Type);
2972 end if;
2973
2974 Next (Alt);
2975 end loop;
2976
2977 else
2978 Alt := First (Alternatives (N));
2979 while Present (Alt) loop
2980 Analyze (Alt);
2981 if not Is_Overloaded (Alt) then
2982 Common_Type := Etype (Alt);
2983
2984 else
2985 Get_First_Interp (Alt, Index, It);
2986 while Present (It.Typ) loop
442c0581
RD
2987 if not
2988 Has_Compatible_Type (Candidate_Interps, It.Typ)
197e4514
AC
2989 then
2990 Remove_Interp (Index);
2991 end if;
442c0581 2992
197e4514
AC
2993 Get_Next_Interp (Index, It);
2994 end loop;
2995
2996 Get_First_Interp (Alt, Index, It);
442c0581 2997
197e4514
AC
2998 if No (It.Typ) then
2999 Error_Msg_N ("alternative has no legal type", Alt);
3000 return;
3001 end if;
3002
442c0581
RD
3003 -- If alternative is not overloaded, we have a unique type
3004 -- for all of them.
197e4514
AC
3005
3006 Set_Etype (Alt, It.Typ);
6376a3c6 3007
c23c86bb
AC
3008 -- If the alternative is an enumeration literal, use the one
3009 -- for this interpretation.
6376a3c6
AC
3010
3011 if Is_Entity_Name (Alt) then
3012 Set_Entity (Alt, It.Nam);
3013 end if;
3014
197e4514
AC
3015 Get_Next_Interp (Index, It);
3016
3017 if No (It.Typ) then
3018 Set_Is_Overloaded (Alt, False);
3019 Common_Type := Etype (Alt);
3020 end if;
3021
3022 Candidate_Interps := Alt;
3023 end if;
3024
3025 Next (Alt);
3026 end loop;
3027 end if;
3028
3029 Set_Etype (N, Standard_Boolean);
3030
3031 if Present (Common_Type) then
3032 Set_Etype (L, Common_Type);
cd1a470a
AC
3033
3034 -- The left operand may still be overloaded, to be resolved using
3035 -- the Common_Type.
197e4514
AC
3036
3037 else
3038 Error_Msg_N ("cannot resolve membership operation", N);
3039 end if;
3040 end Analyze_Set_Membership;
3041
996ae0b0
RK
3042 -- Start of processing for Analyze_Membership_Op
3043
3044 begin
3045 Analyze_Expression (L);
3046
0d8b6803 3047 if No (R) then
9f46106a
JM
3048 pragma Assert (Ada_Version >= Ada_2012);
3049 Analyze_Set_Membership;
3050 Check_Function_Writable_Actuals (N);
197e4514
AC
3051 return;
3052 end if;
3053
996ae0b0
RK
3054 if Nkind (R) = N_Range
3055 or else (Nkind (R) = N_Attribute_Reference
3056 and then Attribute_Name (R) = Name_Range)
3057 then
3058 Analyze (R);
3059
3060 if not Is_Overloaded (L) then
3061 Try_One_Interp (Etype (L));
3062
3063 else
3064 Get_First_Interp (L, Index, It);
996ae0b0
RK
3065 while Present (It.Typ) loop
3066 Try_One_Interp (It.Typ);
3067 Get_Next_Interp (Index, It);
3068 end loop;
3069 end if;
3070
f6b5dc8e 3071 -- If not a range, it can be a subtype mark, or else it is a degenerate
b0186f71
AC
3072 -- membership test with a singleton value, i.e. a test for equality,
3073 -- if the types are compatible.
996ae0b0
RK
3074
3075 else
66150d01 3076 Analyze (R);
7483c888 3077
66150d01
AC
3078 if Is_Entity_Name (R)
3079 and then Is_Type (Entity (R))
3080 then
3081 Find_Type (R);
996ae0b0 3082 Check_Fully_Declared (Entity (R), R);
66150d01 3083
b0186f71
AC
3084 elsif Ada_Version >= Ada_2012
3085 and then Has_Compatible_Type (R, Etype (L))
3086 then
66150d01
AC
3087 if Nkind (N) = N_In then
3088 Rewrite (N,
3089 Make_Op_Eq (Loc,
3090 Left_Opnd => L,
3091 Right_Opnd => R));
3092 else
3093 Rewrite (N,
3094 Make_Op_Ne (Loc,
3095 Left_Opnd => L,
3096 Right_Opnd => R));
3097 end if;
3098
3099 Analyze (N);
3100 return;
3101
3102 else
b0186f71
AC
3103 -- In all versions of the language, if we reach this point there
3104 -- is a previous error that will be diagnosed below.
66150d01
AC
3105
3106 Find_Type (R);
996ae0b0
RK
3107 end if;
3108 end if;
3109
3110 -- Compatibility between expression and subtype mark or range is
3111 -- checked during resolution. The result of the operation is Boolean
3112 -- in any case.
3113
3114 Set_Etype (N, Standard_Boolean);
fe45e59e
ES
3115
3116 if Comes_From_Source (N)
197e4514 3117 and then Present (Right_Opnd (N))
fe45e59e
ES
3118 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
3119 then
3120 Error_Msg_N ("membership test not applicable to cpp-class types", N);
3121 end if;
288cbbbd 3122
22e89283 3123 Check_Function_Writable_Actuals (N);
996ae0b0
RK
3124 end Analyze_Membership_Op;
3125
b727a82b
AC
3126 -----------------
3127 -- Analyze_Mod --
3128 -----------------
3129
3130 procedure Analyze_Mod (N : Node_Id) is
3131 begin
3132 -- A special warning check, if we have an expression of the form:
3133 -- expr mod 2 * literal
3134 -- where literal is 64 or less, then probably what was meant was
3135 -- expr mod 2 ** literal
3136 -- so issue an appropriate warning.
3137
3138 if Warn_On_Suspicious_Modulus_Value
3139 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
3140 and then Intval (Right_Opnd (N)) = Uint_2
3141 and then Nkind (Parent (N)) = N_Op_Multiply
3142 and then Nkind (Right_Opnd (Parent (N))) = N_Integer_Literal
3143 and then Intval (Right_Opnd (Parent (N))) <= Uint_64
3144 then
3145 Error_Msg_N
324ac540 3146 ("suspicious MOD value, was '*'* intended'??M?", Parent (N));
b727a82b
AC
3147 end if;
3148
3149 -- Remaining processing is same as for other arithmetic operators
3150
3151 Analyze_Arithmetic_Op (N);
3152 end Analyze_Mod;
3153
996ae0b0
RK
3154 ----------------------
3155 -- Analyze_Negation --
3156 ----------------------
3157
3158 procedure Analyze_Negation (N : Node_Id) is
3159 R : constant Node_Id := Right_Opnd (N);
3160 Op_Id : Entity_Id := Entity (N);
3161
3162 begin
3163 Set_Etype (N, Any_Type);
3164 Candidate_Type := Empty;
3165
3166 Analyze_Expression (R);
3167
3168 if Present (Op_Id) then
3169 if Ekind (Op_Id) = E_Operator then
3170 Find_Negation_Types (R, Op_Id, N);
3171 else
3172 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3173 end if;
3174
3175 else
3176 Op_Id := Get_Name_Entity_Id (Chars (N));
996ae0b0
RK
3177 while Present (Op_Id) loop
3178 if Ekind (Op_Id) = E_Operator then
3179 Find_Negation_Types (R, Op_Id, N);
3180 else
3181 Analyze_User_Defined_Unary_Op (N, Op_Id);
3182 end if;
3183
3184 Op_Id := Homonym (Op_Id);
3185 end loop;
3186 end if;
3187
3188 Operator_Check (N);
3189 end Analyze_Negation;
3190
15ce9ca2
AC
3191 ------------------
3192 -- Analyze_Null --
3193 ------------------
996ae0b0
RK
3194
3195 procedure Analyze_Null (N : Node_Id) is
3196 begin
ce5ba43a 3197 Check_SPARK_05_Restriction ("null is not allowed", N);
1d801f21 3198
996ae0b0
RK
3199 Set_Etype (N, Any_Access);
3200 end Analyze_Null;
3201
3202 ----------------------
3203 -- Analyze_One_Call --
3204 ----------------------
3205
3206 procedure Analyze_One_Call
ec6078e3
ES
3207 (N : Node_Id;
3208 Nam : Entity_Id;
3209 Report : Boolean;
3210 Success : out Boolean;
3211 Skip_First : Boolean := False)
996ae0b0 3212 is
d469eabe
HK
3213 Actuals : constant List_Id := Parameter_Associations (N);
3214 Prev_T : constant Entity_Id := Etype (N);
3215
12390626
ES
3216 -- Recognize cases of prefixed calls that have been rewritten in
3217 -- various ways. The simplest case is a rewritten selected component,
3218 -- but it can also be an already-examined indexed component, or a
3219 -- prefix that is itself a rewritten prefixed call that is in turn
3220 -- an indexed call (the syntactic ambiguity involving the indexing of
3221 -- a function with defaulted parameters that returns an array).
3222 -- A flag Maybe_Indexed_Call might be useful here ???
3223
aab883ec
ES
3224 Must_Skip : constant Boolean := Skip_First
3225 or else Nkind (Original_Node (N)) = N_Selected_Component
3226 or else
3227 (Nkind (Original_Node (N)) = N_Indexed_Component
e5fc0179
HK
3228 and then Nkind (Prefix (Original_Node (N))) =
3229 N_Selected_Component)
12390626
ES
3230 or else
3231 (Nkind (Parent (N)) = N_Function_Call
e5fc0179
HK
3232 and then Is_Array_Type (Etype (Name (N)))
3233 and then Etype (Original_Node (N)) =
3234 Component_Type (Etype (Name (N)))
3235 and then Nkind (Original_Node (Parent (N))) =
3236 N_Selected_Component);
12390626 3237
aab883ec
ES
3238 -- The first formal must be omitted from the match when trying to find
3239 -- a primitive operation that is a possible interpretation, and also
3240 -- after the call has been rewritten, because the corresponding actual
3241 -- is already known to be compatible, and because this may be an
3242 -- indexing of a call with default parameters.
3243
2f0a921f 3244 First_Form : Entity_Id;
53cf4600
ES
3245 Formal : Entity_Id;
3246 Actual : Node_Id;
3247 Is_Indexed : Boolean := False;
3248 Is_Indirect : Boolean := False;
3249 Subp_Type : constant Entity_Id := Etype (Nam);
3250 Norm_OK : Boolean;
996ae0b0 3251
1d2d8a8f
AC
3252 function Compatible_Types_In_Predicate
3253 (T1 : Entity_Id;
3254 T2 : Entity_Id) return Boolean;
3255 -- For an Ada 2012 predicate or invariant, a call may mention an
3256 -- incomplete type, while resolution of the corresponding predicate
3257 -- function may see the full view, as a consequence of the delayed
3258 -- resolution of the corresponding expressions. This may occur in
3259 -- the body of a predicate function, or in a call to such. Anomalies
3260 -- involving private and full views can also happen. In each case,
3261 -- rewrite node or add conversions to remove spurious type errors.
3262
3263 procedure Indicate_Name_And_Type;
3264 -- If candidate interpretation matches, indicate name and type of result
3265 -- on call node.
3266
157a9bf5
ES
3267 function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
3268 -- There may be a user-defined operator that hides the current
3269 -- interpretation. We must check for this independently of the
3270 -- analysis of the call with the user-defined operation, because
3271 -- the parameter names may be wrong and yet the hiding takes place.
3272 -- This fixes a problem with ACATS test B34014O.
3273 --
3274 -- When the type Address is a visible integer type, and the DEC
3275 -- system extension is visible, the predefined operator may be
3276 -- hidden as well, by one of the address operations in auxdec.
3277 -- Finally, The abstract operations on address do not hide the
3278 -- predefined operator (this is the purpose of making them abstract).
3279
1d2d8a8f
AC
3280 -----------------------------------
3281 -- Compatible_Types_In_Predicate --
3282 -----------------------------------
3283
3284 function Compatible_Types_In_Predicate
3285 (T1 : Entity_Id;
3286 T2 : Entity_Id) return Boolean
3287 is
3288 function Common_Type (T : Entity_Id) return Entity_Id;
3289 -- Find non-private full view if any, without going to ancestor type
3290 -- (as opposed to Underlying_Type).
3291
3292 -----------------
3293 -- Common_Type --
3294 -----------------
3295
3296 function Common_Type (T : Entity_Id) return Entity_Id is
3297 begin
3298 if Is_Private_Type (T) and then Present (Full_View (T)) then
3299 return Base_Type (Full_View (T));
3300 else
3301 return Base_Type (T);
3302 end if;
3303 end Common_Type;
3304
3305 -- Start of processing for Compatible_Types_In_Predicate
3306
3307 begin
3308 if (Ekind (Current_Scope) = E_Function
3309 and then Is_Predicate_Function (Current_Scope))
3310 or else
3311 (Ekind (Nam) = E_Function
3312 and then Is_Predicate_Function (Nam))
3313 then
3314 if Is_Incomplete_Type (T1)
3315 and then Present (Full_View (T1))
3316 and then Full_View (T1) = T2
3317 then
3318 Set_Etype (Formal, Etype (Actual));
3319 return True;
3320
3321 elsif Common_Type (T1) = Common_Type (T2) then
3322 Rewrite (Actual, Unchecked_Convert_To (Etype (Formal), Actual));
3323 return True;
3324
3325 else
3326 return False;
3327 end if;
3328
3329 else
3330 return False;
3331 end if;
3332 end Compatible_Types_In_Predicate;
996ae0b0 3333
fbf5a39b
AC
3334 ----------------------------
3335 -- Indicate_Name_And_Type --
3336 ----------------------------
996ae0b0 3337
fbf5a39b 3338 procedure Indicate_Name_And_Type is
996ae0b0
RK
3339 begin
3340 Add_One_Interp (N, Nam, Etype (Nam));
44a10091 3341 Check_Implicit_Dereference (N, Etype (Nam));
996ae0b0
RK
3342 Success := True;
3343
3344 -- If the prefix of the call is a name, indicate the entity
3345 -- being called. If it is not a name, it is an expression that
3346 -- denotes an access to subprogram or else an entry or family. In
3347 -- the latter case, the name is a selected component, and the entity
3348 -- being called is noted on the selector.
3349
3350 if not Is_Type (Nam) then
a3f2babd 3351 if Is_Entity_Name (Name (N)) then
996ae0b0 3352 Set_Entity (Name (N), Nam);
d9307840 3353 Set_Etype (Name (N), Etype (Nam));
996ae0b0
RK
3354
3355 elsif Nkind (Name (N)) = N_Selected_Component then
3356 Set_Entity (Selector_Name (Name (N)), Nam);
3357 end if;
3358 end if;
3359
3360 if Debug_Flag_E and not Report then
3361 Write_Str (" Overloaded call ");
3362 Write_Int (Int (N));
3363 Write_Str (" compatible with ");
3364 Write_Int (Int (Nam));
3365 Write_Eol;
3366 end if;
fbf5a39b 3367 end Indicate_Name_And_Type;
996ae0b0 3368
157a9bf5
ES
3369 ------------------------
3370 -- Operator_Hidden_By --
3371 ------------------------
3372
3373 function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
3374 Act1 : constant Node_Id := First_Actual (N);
3375 Act2 : constant Node_Id := Next_Actual (Act1);
3376 Form1 : constant Entity_Id := First_Formal (Fun);
3377 Form2 : constant Entity_Id := Next_Formal (Form1);
3378
3379 begin
e4deba8e 3380 if Ekind (Fun) /= E_Function or else Is_Abstract_Subprogram (Fun) then
157a9bf5
ES
3381 return False;
3382
3383 elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
3384 return False;
3385
3386 elsif Present (Form2) then
e4deba8e
RD
3387 if No (Act2)
3388 or else not Has_Compatible_Type (Act2, Etype (Form2))
157a9bf5
ES
3389 then
3390 return False;
3391 end if;
3392
3393 elsif Present (Act2) then
3394 return False;
3395 end if;
3396
3397 -- Now we know that the arity of the operator matches the function,
3398 -- and the function call is a valid interpretation. The function
3399 -- hides the operator if it has the right signature, or if one of
3400 -- its operands is a non-abstract operation on Address when this is
3401 -- a visible integer type.
3402
3403 return Hides_Op (Fun, Nam)
d9d25d04 3404 or else Is_Descendant_Of_Address (Etype (Form1))
157a9bf5
ES
3405 or else
3406 (Present (Form2)
d9d25d04 3407 and then Is_Descendant_Of_Address (Etype (Form2)));
157a9bf5
ES
3408 end Operator_Hidden_By;
3409
996ae0b0
RK
3410 -- Start of processing for Analyze_One_Call
3411
3412 begin
3413 Success := False;
3414
157a9bf5
ES
3415 -- If the subprogram has no formals or if all the formals have defaults,
3416 -- and the return type is an array type, the node may denote an indexing
3417 -- of the result of a parameterless call. In Ada 2005, the subprogram
3418 -- may have one non-defaulted formal, and the call may have been written
3419 -- in prefix notation, so that the rebuilt parameter list has more than
3420 -- one actual.
996ae0b0 3421
53cf4600
ES
3422 if not Is_Overloadable (Nam)
3423 and then Ekind (Nam) /= E_Subprogram_Type
3424 and then Ekind (Nam) /= E_Entry_Family
3425 then
3426 return;
3427 end if;
3428
80e59506 3429 -- An indexing requires at least one actual. The name of the call cannot
4bb9c7b9
AC
3430 -- be an implicit indirect call, so it cannot be a generated explicit
3431 -- dereference.
e1f3cb58
AC
3432
3433 if not Is_Empty_List (Actuals)
aab883ec
ES
3434 and then
3435 (Needs_No_Actuals (Nam)
3436 or else
3437 (Needs_One_Actual (Nam)
e4deba8e 3438 and then Present (Next_Actual (First (Actuals)))))
996ae0b0 3439 then
4bb9c7b9
AC
3440 if Is_Array_Type (Subp_Type)
3441 and then
3442 (Nkind (Name (N)) /= N_Explicit_Dereference
3443 or else Comes_From_Source (Name (N)))
3444 then
aab883ec 3445 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
996ae0b0
RK
3446
3447 elsif Is_Access_Type (Subp_Type)
3448 and then Is_Array_Type (Designated_Type (Subp_Type))
3449 then
3450 Is_Indexed :=
aab883ec
ES
3451 Try_Indexed_Call
3452 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
996ae0b0 3453
758c442c 3454 -- The prefix can also be a parameterless function that returns an
f3d57416 3455 -- access to subprogram, in which case this is an indirect call.
53cf4600
ES
3456 -- If this succeeds, an explicit dereference is added later on,
3457 -- in Analyze_Call or Resolve_Call.
758c442c 3458
996ae0b0 3459 elsif Is_Access_Type (Subp_Type)
401093c1 3460 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
996ae0b0 3461 then
53cf4600 3462 Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
996ae0b0
RK
3463 end if;
3464
3465 end if;
3466
5ff22245 3467 -- If the call has been transformed into a slice, it is of the form
30783513 3468 -- F (Subtype) where F is parameterless. The node has been rewritten in
5ff22245
ES
3469 -- Try_Indexed_Call and there is nothing else to do.
3470
3471 if Is_Indexed
21d7ef70 3472 and then Nkind (N) = N_Slice
5ff22245
ES
3473 then
3474 return;
3475 end if;
3476
53cf4600
ES
3477 Normalize_Actuals
3478 (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
996ae0b0
RK
3479
3480 if not Norm_OK then
3481
53cf4600 3482 -- If an indirect call is a possible interpretation, indicate
80e59506 3483 -- success to the caller. This may be an indexing of an explicit
4bb9c7b9 3484 -- dereference of a call that returns an access type (see above).
53cf4600 3485
4bb9c7b9
AC
3486 if Is_Indirect
3487 or else (Is_Indexed
3488 and then Nkind (Name (N)) = N_Explicit_Dereference
3489 and then Comes_From_Source (Name (N)))
3490 then
53cf4600
ES
3491 Success := True;
3492 return;
3493
996ae0b0
RK
3494 -- Mismatch in number or names of parameters
3495
53cf4600 3496 elsif Debug_Flag_E then
996ae0b0
RK
3497 Write_Str (" normalization fails in call ");
3498 Write_Int (Int (N));
3499 Write_Str (" with subprogram ");
3500 Write_Int (Int (Nam));
3501 Write_Eol;
3502 end if;
3503
3504 -- If the context expects a function call, discard any interpretation
3505 -- that is a procedure. If the node is not overloaded, leave as is for
3506 -- better error reporting when type mismatch is found.
3507
3508 elsif Nkind (N) = N_Function_Call
3509 and then Is_Overloaded (Name (N))
3510 and then Ekind (Nam) = E_Procedure
3511 then
3512 return;
3513
4c46b835 3514 -- Ditto for function calls in a procedure context
996ae0b0
RK
3515
3516 elsif Nkind (N) = N_Procedure_Call_Statement
3517 and then Is_Overloaded (Name (N))
3518 and then Etype (Nam) /= Standard_Void_Type
3519 then
3520 return;
3521
fe45e59e 3522 elsif No (Actuals) then
996ae0b0
RK
3523
3524 -- If Normalize succeeds, then there are default parameters for
3525 -- all formals.
3526
fbf5a39b 3527 Indicate_Name_And_Type;
996ae0b0
RK
3528
3529 elsif Ekind (Nam) = E_Operator then
996ae0b0
RK
3530 if Nkind (N) = N_Procedure_Call_Statement then
3531 return;
3532 end if;
3533
3534 -- This can occur when the prefix of the call is an operator
3535 -- name or an expanded name whose selector is an operator name.
3536
3537 Analyze_Operator_Call (N, Nam);
3538
3539 if Etype (N) /= Prev_T then
3540
157a9bf5 3541 -- Check that operator is not hidden by a function interpretation
996ae0b0
RK
3542
3543 if Is_Overloaded (Name (N)) then
3544 declare
3545 I : Interp_Index;
3546 It : Interp;
3547
3548 begin
3549 Get_First_Interp (Name (N), I, It);
996ae0b0 3550 while Present (It.Nam) loop
157a9bf5 3551 if Operator_Hidden_By (It.Nam) then
996ae0b0
RK
3552 Set_Etype (N, Prev_T);
3553 return;
3554 end if;
3555
3556 Get_Next_Interp (I, It);
3557 end loop;
3558 end;
3559 end if;
3560
3561 -- If operator matches formals, record its name on the call.
3562 -- If the operator is overloaded, Resolve will select the
3563 -- correct one from the list of interpretations. The call
3564 -- node itself carries the first candidate.
3565
3566 Set_Entity (Name (N), Nam);
3567 Success := True;
3568
3569 elsif Report and then Etype (N) = Any_Type then
3570 Error_Msg_N ("incompatible arguments for operator", N);
3571 end if;
3572
3573 else
3574 -- Normalize_Actuals has chained the named associations in the
3575 -- correct order of the formals.
3576
2f0a921f
JS
3577 Actual := First_Actual (N);
3578 Formal := First_Formal (Nam);
3579 First_Form := Formal;
ec6078e3 3580
df3e68b1
HK
3581 -- If we are analyzing a call rewritten from object notation, skip
3582 -- first actual, which may be rewritten later as an explicit
3583 -- dereference.
ec6078e3 3584
aab883ec 3585 if Must_Skip then
ec6078e3
ES
3586 Next_Actual (Actual);
3587 Next_Formal (Formal);
3588 end if;
3589
996ae0b0 3590 while Present (Actual) and then Present (Formal) loop
fbf5a39b
AC
3591 if Nkind (Parent (Actual)) /= N_Parameter_Association
3592 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
996ae0b0 3593 then
9c510803
ES
3594 -- The actual can be compatible with the formal, but we must
3595 -- also check that the context is not an address type that is
7a5b62b0 3596 -- visibly an integer type. In this case the use of literals is
d9d25d04 3597 -- illegal, except in the body of descendants of system, where
7a5b62b0 3598 -- arithmetic operations on address are of course used.
9c510803
ES
3599
3600 if Has_Compatible_Type (Actual, Etype (Formal))
3601 and then
3602 (Etype (Actual) /= Universal_Integer
d9d25d04 3603 or else not Is_Descendant_Of_Address (Etype (Formal))
8ab31c0c 3604 or else In_Predefined_Unit (N))
9c510803 3605 then
996ae0b0
RK
3606 Next_Actual (Actual);
3607 Next_Formal (Formal);
3608
061828e3
AC
3609 -- In Allow_Integer_Address mode, we allow an actual integer to
3610 -- match a formal address type and vice versa. We only do this
3611 -- if we are certain that an error will otherwise be issued
3612
3613 elsif Address_Integer_Convert_OK
3614 (Etype (Actual), Etype (Formal))
3615 and then (Report and not Is_Indexed and not Is_Indirect)
3616 then
3617 -- Handle this case by introducing an unchecked conversion
3618
3619 Rewrite (Actual,
3620 Unchecked_Convert_To (Etype (Formal),
3621 Relocate_Node (Actual)));
3622 Analyze_And_Resolve (Actual, Etype (Formal));
3623 Next_Actual (Actual);
3624 Next_Formal (Formal);
3625
a8a42b93
AC
3626 -- Under relaxed RM semantics silently replace occurrences of
3627 -- null by System.Address_Null. We only do this if we know that
3628 -- an error will otherwise be issued.
3629
3630 elsif Null_To_Null_Address_Convert_OK (Actual, Etype (Formal))
3631 and then (Report and not Is_Indexed and not Is_Indirect)
3632 then
3633 Replace_Null_By_Null_Address (Actual);
3634 Analyze_And_Resolve (Actual, Etype (Formal));
3635 Next_Actual (Actual);
3636 Next_Formal (Formal);
3637
1d2d8a8f
AC
3638 elsif Compatible_Types_In_Predicate
3639 (Etype (Formal), Etype (Actual))
a921e83c 3640 then
a921e83c
AC
3641 Next_Actual (Actual);
3642 Next_Formal (Formal);
3643
bb072d1c
AC
3644 -- Handle failed type check
3645
996ae0b0
RK
3646 else
3647 if Debug_Flag_E then
3648 Write_Str (" type checking fails in call ");
3649 Write_Int (Int (N));
3650 Write_Str (" with formal ");
3651 Write_Int (Int (Formal));
3652 Write_Str (" in subprogram ");
3653 Write_Int (Int (Nam));
3654 Write_Eol;
3655 end if;
3656
061828e3
AC
3657 -- Comment needed on the following test???
3658
53cf4600 3659 if Report and not Is_Indexed and not Is_Indirect then
758c442c
GD
3660
3661 -- Ada 2005 (AI-251): Complete the error notification
8f2eeab7 3662 -- to help new Ada 2005 users.
758c442c
GD
3663
3664 if Is_Class_Wide_Type (Etype (Formal))
3665 and then Is_Interface (Etype (Etype (Formal)))
3666 and then not Interface_Present_In_Ancestor
3667 (Typ => Etype (Actual),
3668 Iface => Etype (Etype (Formal)))
3669 then
758c442c 3670 Error_Msg_NE
ec6078e3 3671 ("(Ada 2005) does not implement interface }",
758c442c
GD
3672 Actual, Etype (Etype (Formal)));
3673 end if;
3674
996ae0b0
RK
3675 Wrong_Type (Actual, Etype (Formal));
3676
3677 if Nkind (Actual) = N_Op_Eq
3678 and then Nkind (Left_Opnd (Actual)) = N_Identifier
3679 then
3680 Formal := First_Formal (Nam);
996ae0b0 3681 while Present (Formal) loop
996ae0b0 3682 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
4e7a4f6e 3683 Error_Msg_N -- CODEFIX
fbf5a39b 3684 ("possible misspelling of `='>`!", Actual);
996ae0b0
RK
3685 exit;
3686 end if;
3687
3688 Next_Formal (Formal);
3689 end loop;
3690 end if;
3691
3692 if All_Errors_Mode then
3693 Error_Msg_Sloc := Sloc (Nam);
3694
3b42c566
RD
3695 if Etype (Formal) = Any_Type then
3696 Error_Msg_N
3697 ("there is no legal actual parameter", Actual);
3698 end if;
3699
996ae0b0
RK
3700 if Is_Overloadable (Nam)
3701 and then Present (Alias (Nam))
3702 and then not Comes_From_Source (Nam)
3703 then
3704 Error_Msg_NE
401093c1
ES
3705 ("\\ =='> in call to inherited operation & #!",
3706 Actual, Nam);
7324bf49
AC
3707
3708 elsif Ekind (Nam) = E_Subprogram_Type then
3709 declare
3710 Access_To_Subprogram_Typ :
3711 constant Entity_Id :=
3712 Defining_Identifier
3713 (Associated_Node_For_Itype (Nam));
3714 begin
a90bd866
RD
3715 Error_Msg_NE
3716 ("\\ =='> in call to dereference of &#!",
3717 Actual, Access_To_Subprogram_Typ);
7324bf49
AC
3718 end;
3719
996ae0b0 3720 else
401093c1
ES
3721 Error_Msg_NE
3722 ("\\ =='> in call to &#!", Actual, Nam);
7324bf49 3723
996ae0b0
RK
3724 end if;
3725 end if;
3726 end if;
3727
3728 return;
3729 end if;
3730
3731 else
3732 -- Normalize_Actuals has verified that a default value exists
3733 -- for this formal. Current actual names a subsequent formal.
3734
3735 Next_Formal (Formal);
3736 end if;
3737 end loop;
3738
2f0a921f
JS
3739 -- Due to our current model of controlled type expansion we may
3740 -- have resolved a user call to a non-visible controlled primitive
3741 -- since these inherited subprograms may be generated in the current
4cdd4a33 3742 -- scope. This is a side effect of the need for the expander to be
2f0a921f
JS
3743 -- able to resolve internally generated calls.
3744
3745 -- Specifically, the issue appears when predefined controlled
3746 -- operations get called on a type extension whose parent is a
3747 -- private extension completed with a controlled extension - see
3748 -- below:
3749
3750 -- package X is
3751 -- type Par_Typ is tagged private;
3752 -- private
3753 -- type Par_Typ is new Controlled with null record;
3754 -- end;
3755 -- ...
3756 -- procedure Main is
3757 -- type Ext_Typ is new Par_Typ with null record;
3758 -- Obj : Ext_Typ;
3759 -- begin
3760 -- Finalize (Obj); -- Will improperly resolve
3761 -- end;
3762
3763 -- To avoid breaking privacy, Is_Hidden gets set elsewhere on such
3764 -- primitives, but we still need to verify that Nam is indeed a
3765 -- controlled subprogram. So, we do that here and issue the
3766 -- appropriate error.
3767
3768 if Is_Hidden (Nam)
3769 and then not In_Instance
3770 and then not Comes_From_Source (Nam)
3771 and then Comes_From_Source (N)
3772
3773 -- Verify Nam is a controlled primitive
3774
3775 and then Nam_In (Chars (Nam), Name_Adjust,
3776 Name_Finalize,
3777 Name_Initialize)
3778 and then Ekind (Nam) = E_Procedure
3779 and then Is_Controlled (Etype (First_Form))
3780 and then No (Next_Formal (First_Form))
3781 then
3782 Error_Msg_Node_2 := Etype (First_Form);
3783 Error_Msg_NE ("call to non-visible controlled primitive & on type"
3784 & " &", N, Nam);
3785 end if;
3786
4c46b835 3787 -- On exit, all actuals match
996ae0b0 3788
fbf5a39b 3789 Indicate_Name_And_Type;
996ae0b0
RK
3790 end if;
3791 end Analyze_One_Call;
3792
15ce9ca2
AC
3793 ---------------------------
3794 -- Analyze_Operator_Call --
3795 ---------------------------
996ae0b0
RK
3796
3797 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
3798 Op_Name : constant Name_Id := Chars (Op_Id);
3799 Act1 : constant Node_Id := First_Actual (N);
3800 Act2 : constant Node_Id := Next_Actual (Act1);
3801
3802 begin
4c46b835
AC
3803 -- Binary operator case
3804
996ae0b0
RK
3805 if Present (Act2) then
3806
4c46b835 3807 -- If more than two operands, then not binary operator after all
996ae0b0
RK
3808
3809 if Present (Next_Actual (Act2)) then
996ae0b0 3810 return;
b7539c3b 3811 end if;
996ae0b0 3812
b7539c3b 3813 -- Otherwise action depends on operator
996ae0b0 3814
b7539c3b 3815 case Op_Name is
d8f43ee6
HK
3816 when Name_Op_Add
3817 | Name_Op_Divide
3818 | Name_Op_Expon
3819 | Name_Op_Mod
3820 | Name_Op_Multiply
3821 | Name_Op_Rem
3822 | Name_Op_Subtract
3823 =>
b7539c3b 3824 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
996ae0b0 3825
d8f43ee6
HK
3826 when Name_Op_And
3827 | Name_Op_Or
3828 | Name_Op_Xor
3829 =>
b7539c3b 3830 Find_Boolean_Types (Act1, Act2, Op_Id, N);
996ae0b0 3831
d8f43ee6
HK
3832 when Name_Op_Ge
3833 | Name_Op_Gt
3834 | Name_Op_Le
3835 | Name_Op_Lt
3836 =>
b7539c3b 3837 Find_Comparison_Types (Act1, Act2, Op_Id, N);
996ae0b0 3838
d8f43ee6
HK
3839 when Name_Op_Eq
3840 | Name_Op_Ne
3841 =>
b7539c3b 3842 Find_Equality_Types (Act1, Act2, Op_Id, N);
996ae0b0 3843
d8f43ee6 3844 when Name_Op_Concat =>
b7539c3b 3845 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
996ae0b0 3846
b7539c3b
AC
3847 -- Is this when others, or should it be an abort???
3848
d8f43ee6 3849 when others =>
b7539c3b
AC
3850 null;
3851 end case;
996ae0b0 3852
4c46b835 3853 -- Unary operator case
996ae0b0 3854
4c46b835 3855 else
b7539c3b 3856 case Op_Name is
d8f43ee6
HK
3857 when Name_Op_Abs
3858 | Name_Op_Add
3859 | Name_Op_Subtract
3860 =>
b7539c3b 3861 Find_Unary_Types (Act1, Op_Id, N);
996ae0b0 3862
d8f43ee6 3863 when Name_Op_Not =>
b7539c3b 3864 Find_Negation_Types (Act1, Op_Id, N);
996ae0b0 3865
b7539c3b 3866 -- Is this when others correct, or should it be an abort???
996ae0b0 3867
d8f43ee6 3868 when others =>
b7539c3b
AC
3869 null;
3870 end case;
996ae0b0
RK
3871 end if;
3872 end Analyze_Operator_Call;
3873
3874 -------------------------------------------
3875 -- Analyze_Overloaded_Selected_Component --
3876 -------------------------------------------
3877
3878 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
fbf5a39b
AC
3879 Nam : constant Node_Id := Prefix (N);
3880 Sel : constant Node_Id := Selector_Name (N);
996ae0b0 3881 Comp : Entity_Id;
996ae0b0
RK
3882 I : Interp_Index;
3883 It : Interp;
3884 T : Entity_Id;
3885
3886 begin
4c46b835 3887 Set_Etype (Sel, Any_Type);
996ae0b0 3888
4c46b835 3889 Get_First_Interp (Nam, I, It);
996ae0b0
RK
3890 while Present (It.Typ) loop
3891 if Is_Access_Type (It.Typ) then
3892 T := Designated_Type (It.Typ);
324ac540 3893 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
996ae0b0
RK
3894 else
3895 T := It.Typ;
3896 end if;
3897
95eb8b69
AC
3898 -- Locate the component. For a private prefix the selector can denote
3899 -- a discriminant.
3900
3901 if Is_Record_Type (T) or else Is_Private_Type (T) then
d469eabe
HK
3902
3903 -- If the prefix is a class-wide type, the visible components are
3904 -- those of the base type.
3905
3906 if Is_Class_Wide_Type (T) then
3907 T := Etype (T);
3908 end if;
3909
996ae0b0 3910 Comp := First_Entity (T);
996ae0b0 3911 while Present (Comp) loop
996ae0b0 3912 if Chars (Comp) = Chars (Sel)
24e95966 3913 and then Is_Visible_Component (Comp, Sel)
996ae0b0 3914 then
996ae0b0 3915
f16d05d9
AC
3916 -- AI05-105: if the context is an object renaming with
3917 -- an anonymous access type, the expected type of the
3918 -- object must be anonymous. This is a name resolution rule.
996ae0b0 3919
f16d05d9
AC
3920 if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
3921 or else No (Access_Definition (Parent (N)))
3922 or else Ekind (Etype (Comp)) = E_Anonymous_Access_Type
3923 or else
3924 Ekind (Etype (Comp)) = E_Anonymous_Access_Subprogram_Type
3925 then
3926 Set_Entity (Sel, Comp);
3927 Set_Etype (Sel, Etype (Comp));
3928 Add_One_Interp (N, Etype (Comp), Etype (Comp));
44a10091 3929 Check_Implicit_Dereference (N, Etype (Comp));
f16d05d9
AC
3930
3931 -- This also specifies a candidate to resolve the name.
3932 -- Further overloading will be resolved from context.
3933 -- The selector name itself does not carry overloading
3934 -- information.
3935
3936 Set_Etype (Nam, It.Typ);
3937
3938 else
b61ee1aa 3939 -- Named access type in the context of a renaming
f16d05d9
AC
3940 -- declaration with an access definition. Remove
3941 -- inapplicable candidate.
3942
3943 Remove_Interp (I);
3944 end if;
996ae0b0
RK
3945 end if;
3946
3947 Next_Entity (Comp);
3948 end loop;
3949
3950 elsif Is_Concurrent_Type (T) then
3951 Comp := First_Entity (T);
996ae0b0
RK
3952 while Present (Comp)
3953 and then Comp /= First_Private_Entity (T)
3954 loop
3955 if Chars (Comp) = Chars (Sel) then
3956 if Is_Overloadable (Comp) then
3957 Add_One_Interp (Sel, Comp, Etype (Comp));
3958 else
e7ba564f 3959 Set_Entity_With_Checks (Sel, Comp);
996ae0b0
RK
3960 Generate_Reference (Comp, Sel);
3961 end if;
3962
3963 Set_Etype (Sel, Etype (Comp));
3964 Set_Etype (N, Etype (Comp));
3965 Set_Etype (Nam, It.Typ);
3966
09494c32
AC
3967 -- For access type case, introduce explicit dereference for
3968 -- more uniform treatment of entry calls. Do this only once
3969 -- if several interpretations yield an access type.
996ae0b0 3970
d469eabe
HK
3971 if Is_Access_Type (Etype (Nam))
3972 and then Nkind (Nam) /= N_Explicit_Dereference
3973 then
996ae0b0 3974 Insert_Explicit_Dereference (Nam);
fbf5a39b 3975 Error_Msg_NW
324ac540 3976 (Warn_On_Dereference, "?d?implicit dereference", N);
996ae0b0
RK
3977 end if;
3978 end if;
3979
3980 Next_Entity (Comp);
3981 end loop;
3982
3983 Set_Is_Overloaded (N, Is_Overloaded (Sel));
996ae0b0
RK
3984 end if;
3985
3986 Get_Next_Interp (I, It);
3987 end loop;
3988
0a36105d
JM
3989 if Etype (N) = Any_Type
3990 and then not Try_Object_Operation (N)
3991 then
996ae0b0
RK
3992 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
3993 Set_Entity (Sel, Any_Id);
3994 Set_Etype (Sel, Any_Type);
3995 end if;
996ae0b0
RK
3996 end Analyze_Overloaded_Selected_Component;
3997
3998 ----------------------------------
3999 -- Analyze_Qualified_Expression --
4000 ----------------------------------
4001
4002 procedure Analyze_Qualified_Expression (N : Node_Id) is
4003 Mark : constant Entity_Id := Subtype_Mark (N);
45c8b94b
ES
4004 Expr : constant Node_Id := Expression (N);
4005 I : Interp_Index;
4006 It : Interp;
996ae0b0
RK
4007 T : Entity_Id;
4008
4009 begin
45c8b94b
ES
4010 Analyze_Expression (Expr);
4011
996ae0b0
RK
4012 Set_Etype (N, Any_Type);
4013 Find_Type (Mark);
4014 T := Entity (Mark);
845af9e6 4015
aa11d1dd
PMR
4016 if Nkind_In (Enclosing_Declaration (N), N_Formal_Type_Declaration,
4017 N_Full_Type_Declaration,
4018 N_Incomplete_Type_Declaration,
4019 N_Protected_Type_Declaration,
4020 N_Private_Extension_Declaration,
4021 N_Private_Type_Declaration,
4022 N_Subtype_Declaration,
4023 N_Task_Type_Declaration)
845af9e6
PMR
4024 and then T = Defining_Identifier (Enclosing_Declaration (N))
4025 then
4026 Error_Msg_N ("current instance not allowed", Mark);
4027 T := Any_Type;
4028 end if;
4029
45c8b94b 4030 Set_Etype (N, T);
996ae0b0
RK
4031
4032 if T = Any_Type then
4033 return;
4034 end if;
996ae0b0 4035
4c46b835 4036 Check_Fully_Declared (T, N);
45c8b94b
ES
4037
4038 -- If expected type is class-wide, check for exact match before
4039 -- expansion, because if the expression is a dispatching call it
4040 -- may be rewritten as explicit dereference with class-wide result.
4041 -- If expression is overloaded, retain only interpretations that
4042 -- will yield exact matches.
4043
4044 if Is_Class_Wide_Type (T) then
4045 if not Is_Overloaded (Expr) then
32794080
JM
4046 if Base_Type (Etype (Expr)) /= Base_Type (T)
4047 and then Etype (Expr) /= Raise_Type
4048 then
45c8b94b
ES
4049 if Nkind (Expr) = N_Aggregate then
4050 Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
4051 else
4052 Wrong_Type (Expr, T);
4053 end if;
4054 end if;
4055
4056 else
4057 Get_First_Interp (Expr, I, It);
4058
4059 while Present (It.Nam) loop
4060 if Base_Type (It.Typ) /= Base_Type (T) then
4061 Remove_Interp (I);
4062 end if;
4063
4064 Get_Next_Interp (I, It);
4065 end loop;
4066 end if;
4067 end if;
4068
996ae0b0
RK
4069 Set_Etype (N, T);
4070 end Analyze_Qualified_Expression;
4071
a961aa79
AC
4072 -----------------------------------
4073 -- Analyze_Quantified_Expression --
4074 -----------------------------------
4075
4076 procedure Analyze_Quantified_Expression (N : Node_Id) is
4856cc2a 4077 function Is_Empty_Range (Typ : Entity_Id) return Boolean;
538dbb56
AC
4078 -- If the iterator is part of a quantified expression, and the range is
4079 -- known to be statically empty, emit a warning and replace expression
4856cc2a 4080 -- with its static value. Returns True if the replacement occurs.
538dbb56 4081
0812b84e
AC
4082 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean;
4083 -- Determine whether if expression If_Expr lacks an else part or if it
4084 -- has one, it evaluates to True.
4085
4856cc2a
ES
4086 --------------------
4087 -- Is_Empty_Range --
4088 --------------------
4089
4090 function Is_Empty_Range (Typ : Entity_Id) return Boolean is
4091 Loc : constant Source_Ptr := Sloc (N);
538dbb56
AC
4092
4093 begin
4094 if Is_Array_Type (Typ)
4856cc2a
ES
4095 and then Compile_Time_Known_Bounds (Typ)
4096 and then
9a6dc470
RD
4097 (Expr_Value (Type_Low_Bound (Etype (First_Index (Typ)))) >
4098 Expr_Value (Type_High_Bound (Etype (First_Index (Typ)))))
538dbb56 4099 then
4856cc2a
ES
4100 Preanalyze_And_Resolve (Condition (N), Standard_Boolean);
4101
538dbb56 4102 if All_Present (N) then
4856cc2a 4103 Error_Msg_N
324ac540 4104 ("??quantified expression with ALL "
4856cc2a 4105 & "over a null range has value True", N);
538dbb56
AC
4106 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
4107
4108 else
4856cc2a 4109 Error_Msg_N
324ac540 4110 ("??quantified expression with SOME "
4856cc2a 4111 & "over a null range has value False", N);
538dbb56
AC
4112 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4113 end if;
4114
4115 Analyze (N);
4116 return True;
4117
4118 else
4119 return False;
4120 end if;
4121 end Is_Empty_Range;
4122
0812b84e
AC
4123 -----------------------------
4124 -- No_Else_Or_Trivial_True --
4125 -----------------------------
4126
4127 function No_Else_Or_Trivial_True (If_Expr : Node_Id) return Boolean is
4128 Else_Expr : constant Node_Id :=
4129 Next (Next (First (Expressions (If_Expr))));
4130 begin
4131 return
4132 No (Else_Expr)
4133 or else (Compile_Time_Known_Value (Else_Expr)
4134 and then Is_True (Expr_Value (Else_Expr)));
4135 end No_Else_Or_Trivial_True;
4136
4137 -- Local variables
4138
4139 Cond : constant Node_Id := Condition (N);
57081559 4140 Loop_Id : Entity_Id;
0812b84e
AC
4141 QE_Scop : Entity_Id;
4142
4856cc2a
ES
4143 -- Start of processing for Analyze_Quantified_Expression
4144
a961aa79 4145 begin
ce5ba43a 4146 Check_SPARK_05_Restriction ("quantified expression is not allowed", N);
1d801f21 4147
804670f1
AC
4148 -- Create a scope to emulate the loop-like behavior of the quantified
4149 -- expression. The scope is needed to provide proper visibility of the
4150 -- loop variable.
b3e42de5 4151
804670f1
AC
4152 QE_Scop := New_Internal_Entity (E_Loop, Current_Scope, Sloc (N), 'L');
4153 Set_Etype (QE_Scop, Standard_Void_Type);
4154 Set_Scope (QE_Scop, Current_Scope);
4155 Set_Parent (QE_Scop, N);
a961aa79 4156
804670f1 4157 Push_Scope (QE_Scop);
c56a9ba4 4158
804670f1
AC
4159 -- All constituents are preanalyzed and resolved to avoid untimely
4160 -- generation of various temporaries and types. Full analysis and
4161 -- expansion is carried out when the quantified expression is
4162 -- transformed into an expression with actions.
c56a9ba4 4163
804670f1
AC
4164 if Present (Iterator_Specification (N)) then
4165 Preanalyze (Iterator_Specification (N));
538dbb56 4166
57081559
AC
4167 -- Do not proceed with the analysis when the range of iteration is
4168 -- empty. The appropriate error is issued by Is_Empty_Range.
4169
538dbb56
AC
4170 if Is_Entity_Name (Name (Iterator_Specification (N)))
4171 and then Is_Empty_Range (Etype (Name (Iterator_Specification (N))))
4172 then
4173 return;
4174 end if;
4175
57081559 4176 else pragma Assert (Present (Loop_Parameter_Specification (N)));
a736f6e6
AC
4177 declare
4178 Loop_Par : constant Node_Id := Loop_Parameter_Specification (N);
4179
4180 begin
4181 Preanalyze (Loop_Par);
4182
e4deba8e 4183 if Nkind (Discrete_Subtype_Definition (Loop_Par)) = N_Function_Call
a736f6e6
AC
4184 and then Parent (Loop_Par) /= N
4185 then
4186 -- The parser cannot distinguish between a loop specification
5b85ad7d 4187 -- and an iterator specification. If after preanalysis the
a736f6e6 4188 -- proper form has been recognized, rewrite the expression to
5f0c4d67
AC
4189 -- reflect the right kind. This is needed for proper ASIS
4190 -- navigation. If expansion is enabled, the transformation is
4191 -- performed when the expression is rewritten as a loop.
a736f6e6 4192
a736f6e6
AC
4193 Set_Iterator_Specification (N,
4194 New_Copy_Tree (Iterator_Specification (Parent (Loop_Par))));
5f0c4d67
AC
4195
4196 Set_Defining_Identifier (Iterator_Specification (N),
4197 Relocate_Node (Defining_Identifier (Loop_Par)));
4198 Set_Name (Iterator_Specification (N),
4199 Relocate_Node (Discrete_Subtype_Definition (Loop_Par)));
4200 Set_Comes_From_Source (Iterator_Specification (N),
4201 Comes_From_Source (Loop_Parameter_Specification (N)));
4202 Set_Loop_Parameter_Specification (N, Empty);
a736f6e6
AC
4203 end if;
4204 end;
ce6002ec
AC
4205 end if;
4206
0812b84e 4207 Preanalyze_And_Resolve (Cond, Standard_Boolean);
804670f1 4208
a961aa79
AC
4209 End_Scope;
4210 Set_Etype (N, Standard_Boolean);
0812b84e 4211
57081559
AC
4212 -- Verify that the loop variable is used within the condition of the
4213 -- quantified expression.
4214
4215 if Present (Iterator_Specification (N)) then
4216 Loop_Id := Defining_Identifier (Iterator_Specification (N));
4217 else
4218 Loop_Id := Defining_Identifier (Loop_Parameter_Specification (N));
4219 end if;
4220
4221 if Warn_On_Suspicious_Contract
4222 and then not Referenced (Loop_Id, Cond)
4223 then
124bed29 4224 -- Generating C, this check causes spurious warnings on inlined
519e9fdf 4225 -- postconditions; we can safely disable it because this check
124bed29 4226 -- was previously performed when analyzing the internally built
519e9fdf
AC
4227 -- postconditions procedure.
4228
4229 if Modify_Tree_For_C and then In_Inlined_Body then
4230 null;
4231 else
4232 Error_Msg_N ("?T?unused variable &", Loop_Id);
4233 end if;
57081559
AC
4234 end if;
4235
e19fd0bd 4236 -- Diagnose a possible misuse of the SOME existential quantifier. When
d1ec4768
RD
4237 -- we have a quantified expression of the form:
4238
0812b84e 4239 -- for some X => (if P then Q [else True])
d1ec4768 4240
e19fd0bd 4241 -- any value for X that makes P False results in the if expression being
50ef946c 4242 -- trivially True, and so also results in the quantified expression
e19fd0bd 4243 -- being trivially True.
0812b84e 4244
e19fd0bd 4245 if Warn_On_Suspicious_Contract
0812b84e
AC
4246 and then not All_Present (N)
4247 and then Nkind (Cond) = N_If_Expression
4248 and then No_Else_Or_Trivial_True (Cond)
4249 then
e19fd0bd 4250 Error_Msg_N ("?T?suspicious expression", N);
0812b84e
AC
4251 Error_Msg_N ("\\did you mean (for all X ='> (if P then Q))", N);
4252 Error_Msg_N ("\\or (for some X ='> P and then Q) instead'?", N);
4253 end if;
a961aa79
AC
4254 end Analyze_Quantified_Expression;
4255
996ae0b0
RK
4256 -------------------
4257 -- Analyze_Range --
4258 -------------------
4259
4260 procedure Analyze_Range (N : Node_Id) is
4261 L : constant Node_Id := Low_Bound (N);
4262 H : constant Node_Id := High_Bound (N);
4263 I1, I2 : Interp_Index;
4264 It1, It2 : Interp;
4265
4266 procedure Check_Common_Type (T1, T2 : Entity_Id);
4267 -- Verify the compatibility of two types, and choose the
4268 -- non universal one if the other is universal.
4269
4270 procedure Check_High_Bound (T : Entity_Id);
4271 -- Test one interpretation of the low bound against all those
4272 -- of the high bound.
4273
fbf5a39b 4274 procedure Check_Universal_Expression (N : Node_Id);
a1092b48
AC
4275 -- In Ada 83, reject bounds of a universal range that are not literals
4276 -- or entity names.
fbf5a39b 4277
996ae0b0
RK
4278 -----------------------
4279 -- Check_Common_Type --
4280 -----------------------
4281
4282 procedure Check_Common_Type (T1, T2 : Entity_Id) is
4283 begin
b4592168
GD
4284 if Covers (T1 => T1, T2 => T2)
4285 or else
4286 Covers (T1 => T2, T2 => T1)
4287 then
996ae0b0
RK
4288 if T1 = Universal_Integer
4289 or else T1 = Universal_Real
4290 or else T1 = Any_Character
4291 then
4292 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
4293
fbf5a39b 4294 elsif T1 = T2 then
996ae0b0
RK
4295 Add_One_Interp (N, T1, T1);
4296
4297 else
4298 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
4299 end if;
4300 end if;
4301 end Check_Common_Type;
4302
4303 ----------------------
4304 -- Check_High_Bound --
4305 ----------------------
4306
4307 procedure Check_High_Bound (T : Entity_Id) is
4308 begin
4309 if not Is_Overloaded (H) then
4310 Check_Common_Type (T, Etype (H));
4311 else
4312 Get_First_Interp (H, I2, It2);
996ae0b0
RK
4313 while Present (It2.Typ) loop
4314 Check_Common_Type (T, It2.Typ);
4315 Get_Next_Interp (I2, It2);
4316 end loop;
4317 end if;
4318 end Check_High_Bound;
4319
8016e567
PT
4320 --------------------------------
4321 -- Check_Universal_Expression --
4322 --------------------------------
fbf5a39b
AC
4323
4324 procedure Check_Universal_Expression (N : Node_Id) is
4325 begin
4326 if Etype (N) = Universal_Integer
4327 and then Nkind (N) /= N_Integer_Literal
4328 and then not Is_Entity_Name (N)
4329 and then Nkind (N) /= N_Attribute_Reference
4330 then
4331 Error_Msg_N ("illegal bound in discrete range", N);
4332 end if;
4333 end Check_Universal_Expression;
4334
996ae0b0
RK
4335 -- Start of processing for Analyze_Range
4336
4337 begin
4338 Set_Etype (N, Any_Type);
4339 Analyze_Expression (L);
4340 Analyze_Expression (H);
4341
4342 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
4343 return;
4344
4345 else
4346 if not Is_Overloaded (L) then
4347 Check_High_Bound (Etype (L));
4348 else
4349 Get_First_Interp (L, I1, It1);
996ae0b0
RK
4350 while Present (It1.Typ) loop
4351 Check_High_Bound (It1.Typ);
4352 Get_Next_Interp (I1, It1);
4353 end loop;
4354 end if;
4355
4356 -- If result is Any_Type, then we did not find a compatible pair
4357
4358 if Etype (N) = Any_Type then
4359 Error_Msg_N ("incompatible types in range ", N);
4360 end if;
4361 end if;
fbf5a39b 4362
0ab80019 4363 if Ada_Version = Ada_83
fbf5a39b
AC
4364 and then
4365 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
4c46b835 4366 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
fbf5a39b
AC
4367 then
4368 Check_Universal_Expression (L);
4369 Check_Universal_Expression (H);
4370 end if;
d3820795 4371
22e89283 4372 Check_Function_Writable_Actuals (N);
996ae0b0
RK
4373 end Analyze_Range;
4374
4375 -----------------------
4376 -- Analyze_Reference --
4377 -----------------------
4378
4379 procedure Analyze_Reference (N : Node_Id) is
4380 P : constant Node_Id := Prefix (N);
b4592168
GD
4381 E : Entity_Id;
4382 T : Entity_Id;
996ae0b0 4383 Acc_Type : Entity_Id;
b4592168 4384
996ae0b0
RK
4385 begin
4386 Analyze (P);
b4592168 4387
c42e1b17
AC
4388 -- An interesting error check, if we take the 'Ref of an object for
4389 -- which a pragma Atomic or Volatile has been given, and the type of the
4390 -- object is not Atomic or Volatile, then we are in trouble. The problem
4391 -- is that no trace of the atomic/volatile status will remain for the
4392 -- backend to respect when it deals with the resulting pointer, since
4393 -- the pointer type will not be marked atomic (it is a pointer to the
4394 -- base type of the object).
b4592168
GD
4395
4396 -- It is not clear if that can ever occur, but in case it does, we will
4397 -- generate an error message. Not clear if this message can ever be
4398 -- generated, and pretty clear that it represents a bug if it is, still
d2f25cd1
AC
4399 -- seems worth checking, except in CodePeer mode where we do not really
4400 -- care and don't want to bother the user.
b4592168
GD
4401
4402 T := Etype (P);
4403
4404 if Is_Entity_Name (P)
4405 and then Is_Object_Reference (P)
d2f25cd1 4406 and then not CodePeer_Mode
b4592168
GD
4407 then
4408 E := Entity (P);
4409 T := Etype (P);
4410
4411 if (Has_Atomic_Components (E)
c42e1b17 4412 and then not Has_Atomic_Components (T))
b4592168
GD
4413 or else
4414 (Has_Volatile_Components (E)
c42e1b17 4415 and then not Has_Volatile_Components (T))
b4592168
GD
4416 or else (Is_Atomic (E) and then not Is_Atomic (T))
4417 or else (Is_Volatile (E) and then not Is_Volatile (T))
4418 then
4419 Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
4420 end if;
4421 end if;
4422
4423 -- Carry on with normal processing
4424
996ae0b0 4425 Acc_Type := Create_Itype (E_Allocator_Type, N);
b4592168 4426 Set_Etype (Acc_Type, Acc_Type);
996ae0b0
RK
4427 Set_Directly_Designated_Type (Acc_Type, Etype (P));
4428 Set_Etype (N, Acc_Type);
4429 end Analyze_Reference;
4430
4431 --------------------------------
4432 -- Analyze_Selected_Component --
4433 --------------------------------
4434
2383acbd
AC
4435 -- Prefix is a record type or a task or protected type. In the latter case,
4436 -- the selector must denote a visible entry.
996ae0b0
RK
4437
4438 procedure Analyze_Selected_Component (N : Node_Id) is
d469eabe
HK
4439 Name : constant Node_Id := Prefix (N);
4440 Sel : constant Node_Id := Selector_Name (N);
4441 Act_Decl : Node_Id;
4442 Comp : Entity_Id;
4443 Has_Candidate : Boolean := False;
c0e938d0 4444 Hidden_Comp : Entity_Id;
d469eabe 4445 In_Scope : Boolean;
d1eb8a82 4446 Is_Private_Op : Boolean;
d469eabe
HK
4447 Parent_N : Node_Id;
4448 Pent : Entity_Id := Empty;
4449 Prefix_Type : Entity_Id;
401093c1
ES
4450
4451 Type_To_Use : Entity_Id;
4452 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
4453 -- a class-wide type, we use its root type, whose components are
4454 -- present in the class-wide type.
4455
2383acbd
AC
4456 Is_Single_Concurrent_Object : Boolean;
4457 -- Set True if the prefix is a single task or a single protected object
4458
20261dc1
AC
4459 procedure Find_Component_In_Instance (Rec : Entity_Id);
4460 -- In an instance, a component of a private extension may not be visible
4461 -- while it was visible in the generic. Search candidate scope for a
4462 -- component with the proper identifier. This is only done if all other
f90d14ac
AC
4463 -- searches have failed. If a match is found, the Etype of both N and
4464 -- Sel are set from this component, and the entity of Sel is set to
4465 -- reference this component. If no match is found, Entity (Sel) remains
7d9880c9
AC
4466 -- unset. For a derived type that is an actual of the instance, the
4467 -- desired component may be found in any ancestor.
20261dc1 4468
d469eabe
HK
4469 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
4470 -- It is known that the parent of N denotes a subprogram call. Comp
4471 -- is an overloadable component of the concurrent type of the prefix.
4472 -- Determine whether all formals of the parent of N and Comp are mode
b4592168
GD
4473 -- conformant. If the parent node is not analyzed yet it may be an
4474 -- indexed component rather than a function call.
d469eabe 4475
bd717ec9 4476 function Has_Dereference (Nod : Node_Id) return Boolean;
0f8b3e5d 4477 -- Check whether prefix includes a dereference at any level.
bd717ec9 4478
20261dc1
AC
4479 --------------------------------
4480 -- Find_Component_In_Instance --
4481 --------------------------------
4482
4483 procedure Find_Component_In_Instance (Rec : Entity_Id) is
4484 Comp : Entity_Id;
7d9880c9 4485 Typ : Entity_Id;
20261dc1
AC
4486
4487 begin
7d9880c9
AC
4488 Typ := Rec;
4489 while Present (Typ) loop
4490 Comp := First_Component (Typ);
4491 while Present (Comp) loop
4492 if Chars (Comp) = Chars (Sel) then
4493 Set_Entity_With_Checks (Sel, Comp);
4494 Set_Etype (Sel, Etype (Comp));
4495 Set_Etype (N, Etype (Comp));
4496 return;
4497 end if;
4498
4499 Next_Component (Comp);
4500 end loop;
4501
4502 -- If not found, the component may be declared in the parent
4503 -- type or its full view, if any.
4504
4505 if Is_Derived_Type (Typ) then
4506 Typ := Etype (Typ);
4507
4508 if Is_Private_Type (Typ) then
4509 Typ := Full_View (Typ);
4510 end if;
4511
4512 else
20261dc1
AC
4513 return;
4514 end if;
20261dc1
AC
4515 end loop;
4516
cf3e6845
AC
4517 -- If we fall through, no match, so no changes made
4518
4519 return;
20261dc1
AC
4520 end Find_Component_In_Instance;
4521
d469eabe
HK
4522 ------------------------------
4523 -- Has_Mode_Conformant_Spec --
4524 ------------------------------
4525
4526 function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
4527 Comp_Param : Entity_Id;
4528 Param : Node_Id;
4529 Param_Typ : Entity_Id;
4530
4531 begin
4532 Comp_Param := First_Formal (Comp);
b4592168
GD
4533
4534 if Nkind (Parent (N)) = N_Indexed_Component then
4535 Param := First (Expressions (Parent (N)));
4536 else
4537 Param := First (Parameter_Associations (Parent (N)));
4538 end if;
4539
d469eabe
HK
4540 while Present (Comp_Param)
4541 and then Present (Param)
4542 loop
4543 Param_Typ := Find_Parameter_Type (Param);
4544
4545 if Present (Param_Typ)
4546 and then
4547 not Conforming_Types
4548 (Etype (Comp_Param), Param_Typ, Mode_Conformant)
4549 then
4550 return False;
4551 end if;
4552
4553 Next_Formal (Comp_Param);
4554 Next (Param);
4555 end loop;
4556
9e92ad49
AC
4557 -- One of the specs has additional formals; there is no match, unless
4558 -- this may be an indexing of a parameterless call.
f0e7963f
AC
4559
4560 -- Note that when expansion is disabled, the corresponding record
4561 -- type of synchronized types is not constructed, so that there is
4562 -- no point is attempting an interpretation as a prefixed call, as
4563 -- this is bound to fail because the primitive operations will not
4564 -- be properly located.
d469eabe
HK
4565
4566 if Present (Comp_Param) or else Present (Param) then
f0e7963f
AC
4567 if Needs_No_Actuals (Comp)
4568 and then Is_Array_Type (Etype (Comp))
4569 and then not Expander_Active
4570 then
4571 return True;
f0e7963f
AC
4572 else
4573 return False;
4574 end if;
d469eabe
HK
4575 end if;
4576
4577 return True;
4578 end Has_Mode_Conformant_Spec;
996ae0b0 4579
bd717ec9
AC
4580 ---------------------
4581 -- Has_Dereference --
4582 ---------------------
4583
4584 function Has_Dereference (Nod : Node_Id) return Boolean is
4585 begin
4586 if Nkind (Nod) = N_Explicit_Dereference then
4587 return True;
4588
a6363ed3
AC
4589 -- When expansion is disabled an explicit dereference may not have
4590 -- been inserted, but if this is an access type the indirection makes
4591 -- the call safe.
4592
4593 elsif Is_Access_Type (Etype (Nod)) then
4594 return True;
4595
bd717ec9
AC
4596 elsif Nkind_In (Nod, N_Indexed_Component, N_Selected_Component) then
4597 return Has_Dereference (Prefix (Nod));
4598
4599 else
4600 return False;
4601 end if;
4602 end Has_Dereference;
4603
996ae0b0
RK
4604 -- Start of processing for Analyze_Selected_Component
4605
4606 begin
4607 Set_Etype (N, Any_Type);
4608
4609 if Is_Overloaded (Name) then
4610 Analyze_Overloaded_Selected_Component (N);
4611 return;
4612
4613 elsif Etype (Name) = Any_Type then
4614 Set_Entity (Sel, Any_Id);
4615 Set_Etype (Sel, Any_Type);
4616 return;
4617
4618 else
996ae0b0
RK
4619 Prefix_Type := Etype (Name);
4620 end if;
4621
4622 if Is_Access_Type (Prefix_Type) then
07fc65c4 4623
0d57c6f4
RD
4624 -- A RACW object can never be used as prefix of a selected component
4625 -- since that means it is dereferenced without being a controlling
4626 -- operand of a dispatching operation (RM E.2.2(16/1)). Before
4627 -- reporting an error, we must check whether this is actually a
4628 -- dispatching call in prefix form.
07fc65c4 4629
996ae0b0
RK
4630 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
4631 and then Comes_From_Source (N)
4632 then
b4592168
GD
4633 if Try_Object_Operation (N) then
4634 return;
4635 else
4636 Error_Msg_N
4637 ("invalid dereference of a remote access-to-class-wide value",
4638 N);
4639 end if;
07fc65c4
GB
4640
4641 -- Normal case of selected component applied to access type
4642
4643 else
324ac540 4644 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
da709d08 4645
6e73e3ab
AC
4646 if Is_Entity_Name (Name) then
4647 Pent := Entity (Name);
4648 elsif Nkind (Name) = N_Selected_Component
4649 and then Is_Entity_Name (Selector_Name (Name))
4650 then
4651 Pent := Entity (Selector_Name (Name));
4652 end if;
da709d08 4653
d469eabe 4654 Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name);
996ae0b0 4655 end if;
b4592168
GD
4656
4657 -- If we have an explicit dereference of a remote access-to-class-wide
4658 -- value, then issue an error (see RM-E.2.2(16/1)). However we first
4659 -- have to check for the case of a prefix that is a controlling operand
4660 -- of a prefixed dispatching call, as the dereference is legal in that
4661 -- case. Normally this condition is checked in Validate_Remote_Access_
4662 -- To_Class_Wide_Type, but we have to defer the checking for selected
4663 -- component prefixes because of the prefixed dispatching call case.
4664 -- Note that implicit dereferences are checked for this just above.
4665
4666 elsif Nkind (Name) = N_Explicit_Dereference
4667 and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
4668 and then Comes_From_Source (N)
4669 then
4670 if Try_Object_Operation (N) then
4671 return;
4672 else
4673 Error_Msg_N
4674 ("invalid dereference of a remote access-to-class-wide value",
4675 N);
4676 end if;
aab883ec 4677 end if;
b67a385c 4678
aab883ec
ES
4679 -- (Ada 2005): if the prefix is the limited view of a type, and
4680 -- the context already includes the full view, use the full view
4681 -- in what follows, either to retrieve a component of to find
4682 -- a primitive operation. If the prefix is an explicit dereference,
4683 -- set the type of the prefix to reflect this transformation.
a316b3fc 4684 -- If the nonlimited view is itself an incomplete type, get the
401093c1 4685 -- full view if available.
aab883ec 4686
47346923
AC
4687 if From_Limited_With (Prefix_Type)
4688 and then Has_Non_Limited_View (Prefix_Type)
aab883ec 4689 then
401093c1 4690 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
aab883ec
ES
4691
4692 if Nkind (N) = N_Explicit_Dereference then
4693 Set_Etype (Prefix (N), Prefix_Type);
4694 end if;
996ae0b0
RK
4695 end if;
4696
4697 if Ekind (Prefix_Type) = E_Private_Subtype then
4698 Prefix_Type := Base_Type (Prefix_Type);
4699 end if;
4700
401093c1 4701 Type_To_Use := Prefix_Type;
996ae0b0
RK
4702
4703 -- For class-wide types, use the entity list of the root type. This
4704 -- indirection is specially important for private extensions because
4705 -- only the root type get switched (not the class-wide type).
4706
4707 if Is_Class_Wide_Type (Prefix_Type) then
401093c1 4708 Type_To_Use := Root_Type (Prefix_Type);
996ae0b0
RK
4709 end if;
4710
2383acbd
AC
4711 -- If the prefix is a single concurrent object, use its name in error
4712 -- messages, rather than that of its anonymous type.
4713
4714 Is_Single_Concurrent_Object :=
4715 Is_Concurrent_Type (Prefix_Type)
4716 and then Is_Internal_Name (Chars (Prefix_Type))
4717 and then not Is_Derived_Type (Prefix_Type)
4718 and then Is_Entity_Name (Name);
4719
401093c1 4720 Comp := First_Entity (Type_To_Use);
996ae0b0
RK
4721
4722 -- If the selector has an original discriminant, the node appears in
4723 -- an instance. Replace the discriminant with the corresponding one
4724 -- in the current discriminated type. For nested generics, this must
4725 -- be done transitively, so note the new original discriminant.
4726
4727 if Nkind (Sel) = N_Identifier
c0b11850 4728 and then In_Instance
996ae0b0
RK
4729 and then Present (Original_Discriminant (Sel))
4730 then
4731 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
4732
4733 -- Mark entity before rewriting, for completeness and because
4734 -- subsequent semantic checks might examine the original node.
4735
4736 Set_Entity (Sel, Comp);
ee2ba856 4737 Rewrite (Selector_Name (N), New_Occurrence_Of (Comp, Sloc (N)));
996ae0b0
RK
4738 Set_Original_Discriminant (Selector_Name (N), Comp);
4739 Set_Etype (N, Etype (Comp));
44a10091 4740 Check_Implicit_Dereference (N, Etype (Comp));
996ae0b0
RK
4741
4742 if Is_Access_Type (Etype (Name)) then
4743 Insert_Explicit_Dereference (Name);
324ac540 4744 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
996ae0b0
RK
4745 end if;
4746
4747 elsif Is_Record_Type (Prefix_Type) then
4748
ee2ba856
AC
4749 -- Find component with given name. In an instance, if the node is
4750 -- known as a prefixed call, do not examine components whose
4751 -- visibility may be accidental.
996ae0b0 4752
4913e24c 4753 while Present (Comp) and then not Is_Prefixed_Call (N) loop
996ae0b0 4754 if Chars (Comp) = Chars (Sel)
a53c5613 4755 and then Is_Visible_Component (Comp, N)
996ae0b0 4756 then
e7ba564f 4757 Set_Entity_With_Checks (Sel, Comp);
996ae0b0
RK
4758 Set_Etype (Sel, Etype (Comp));
4759
4760 if Ekind (Comp) = E_Discriminant then
5d09245e 4761 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
996ae0b0 4762 Error_Msg_N
02f58834 4763 ("cannot reference discriminant of unchecked union",
996ae0b0
RK
4764 Sel);
4765 end if;
4766
4767 if Is_Generic_Type (Prefix_Type)
4768 or else
4769 Is_Generic_Type (Root_Type (Prefix_Type))
4770 then
4771 Set_Original_Discriminant (Sel, Comp);
4772 end if;
4773 end if;
4774
4775 -- Resolve the prefix early otherwise it is not possible to
4776 -- build the actual subtype of the component: it may need
4777 -- to duplicate this prefix and duplication is only allowed
4778 -- on fully resolved expressions.
4779
fbf5a39b 4780 Resolve (Name);
996ae0b0 4781
b67a385c
ES
4782 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
4783 -- subtypes in a package specification.
28be29ce
ES
4784 -- Example:
4785
4786 -- limited with Pkg;
4787 -- package Pkg is
4788 -- type Acc_Inc is access Pkg.T;
4789 -- X : Acc_Inc;
b67a385c
ES
4790 -- N : Natural := X.all.Comp; -- ERROR, limited view
4791 -- end Pkg; -- Comp is not visible
28be29ce
ES
4792
4793 if Nkind (Name) = N_Explicit_Dereference
7b56a91b 4794 and then From_Limited_With (Etype (Prefix (Name)))
28be29ce 4795 and then not Is_Potentially_Use_Visible (Etype (Name))
b67a385c
ES
4796 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
4797 N_Package_Specification
28be29ce
ES
4798 then
4799 Error_Msg_NE
4800 ("premature usage of incomplete}", Prefix (Name),
4801 Etype (Prefix (Name)));
4802 end if;
4803
996ae0b0
RK
4804 -- We never need an actual subtype for the case of a selection
4805 -- for a indexed component of a non-packed array, since in
4806 -- this case gigi generates all the checks and can find the
4807 -- necessary bounds information.
4808
0d57c6f4
RD
4809 -- We also do not need an actual subtype for the case of a
4810 -- first, last, length, or range attribute applied to a
996ae0b0
RK
4811 -- non-packed array, since gigi can again get the bounds in
4812 -- these cases (gigi cannot handle the packed case, since it
4813 -- has the bounds of the packed array type, not the original
4814 -- bounds of the type). However, if the prefix is itself a
4815 -- selected component, as in a.b.c (i), gigi may regard a.b.c
4816 -- as a dynamic-sized temporary, so we do generate an actual
4817 -- subtype for this case.
4818
4819 Parent_N := Parent (N);
4820
4821 if not Is_Packed (Etype (Comp))
4822 and then
4823 ((Nkind (Parent_N) = N_Indexed_Component
d469eabe 4824 and then Nkind (Name) /= N_Selected_Component)
996ae0b0
RK
4825 or else
4826 (Nkind (Parent_N) = N_Attribute_Reference
b69cd36a
AC
4827 and then
4828 Nam_In (Attribute_Name (Parent_N), Name_First,
4829 Name_Last,
4830 Name_Length,
4831 Name_Range)))
996ae0b0
RK
4832 then
4833 Set_Etype (N, Etype (Comp));
4834
98123480
ES
4835 -- If full analysis is not enabled, we do not generate an
4836 -- actual subtype, because in the absence of expansion
4837 -- reference to a formal of a protected type, for example,
4838 -- will not be properly transformed, and will lead to
4839 -- out-of-scope references in gigi.
4840
4841 -- In all other cases, we currently build an actual subtype.
4842 -- It seems likely that many of these cases can be avoided,
4843 -- but right now, the front end makes direct references to the
fbf5a39b 4844 -- bounds (e.g. in generating a length check), and if we do
996ae0b0 4845 -- not make an actual subtype, we end up getting a direct
98123480 4846 -- reference to a discriminant, which will not do.
996ae0b0 4847
98123480 4848 elsif Full_Analysis then
996ae0b0
RK
4849 Act_Decl :=
4850 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
4851 Insert_Action (N, Act_Decl);
4852
4853 if No (Act_Decl) then
4854 Set_Etype (N, Etype (Comp));
4855
4856 else
605d8166
ES
4857 -- If discriminants were present in the component
4858 -- declaration, they have been replaced by the
4859 -- actual values in the prefix object.
996ae0b0
RK
4860
4861 declare
fbf5a39b
AC
4862 Subt : constant Entity_Id :=
4863 Defining_Identifier (Act_Decl);
996ae0b0
RK
4864 begin
4865 Set_Etype (Subt, Base_Type (Etype (Comp)));
996ae0b0
RK
4866 Set_Etype (N, Subt);
4867 end;
4868 end if;
98123480
ES
4869
4870 -- If Full_Analysis not enabled, just set the Etype
4871
4872 else
4873 Set_Etype (N, Etype (Comp));
996ae0b0
RK
4874 end if;
4875
44a10091 4876 Check_Implicit_Dereference (N, Etype (N));
996ae0b0
RK
4877 return;
4878 end if;
4879
aab883ec 4880 -- If the prefix is a private extension, check only the visible
9c510803 4881 -- components of the partial view. This must include the tag,
f3d57416 4882 -- which can appear in expanded code in a tag check.
aab883ec 4883
9c510803 4884 if Ekind (Type_To_Use) = E_Record_Type_With_Private
df3e68b1 4885 and then Chars (Selector_Name (N)) /= Name_uTag
9c510803 4886 then
401093c1 4887 exit when Comp = Last_Entity (Type_To_Use);
aab883ec
ES
4888 end if;
4889
996ae0b0
RK
4890 Next_Entity (Comp);
4891 end loop;
4892
d469eabe
HK
4893 -- Ada 2005 (AI-252): The selected component can be interpreted as
4894 -- a prefixed view of a subprogram. Depending on the context, this is
4895 -- either a name that can appear in a renaming declaration, or part
4896 -- of an enclosing call given in prefix form.
4897
4898 -- Ada 2005 (AI05-0030): In the case of dispatching requeue, the
4899 -- selected component should resolve to a name.
35ae2ed8 4900
0791fbe9 4901 if Ada_Version >= Ada_2005
35ae2ed8 4902 and then Is_Tagged_Type (Prefix_Type)
d469eabe 4903 and then not Is_Concurrent_Type (Prefix_Type)
35ae2ed8 4904 then
d469eabe
HK
4905 if Nkind (Parent (N)) = N_Generic_Association
4906 or else Nkind (Parent (N)) = N_Requeue_Statement
4907 or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
4908 then
4909 if Find_Primitive_Operation (N) then
4910 return;
4911 end if;
4912
4913 elsif Try_Object_Operation (N) then
4914 return;
4915 end if;
4c46b835 4916
98123480
ES
4917 -- If the transformation fails, it will be necessary to redo the
4918 -- analysis with all errors enabled, to indicate candidate
4919 -- interpretations and reasons for each failure ???
4c46b835 4920
35ae2ed8
AC
4921 end if;
4922
996ae0b0 4923 elsif Is_Private_Type (Prefix_Type) then
d469eabe 4924
98123480
ES
4925 -- Allow access only to discriminants of the type. If the type has
4926 -- no full view, gigi uses the parent type for the components, so we
4927 -- do the same here.
996ae0b0
RK
4928
4929 if No (Full_View (Prefix_Type)) then
401093c1
ES
4930 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
4931 Comp := First_Entity (Type_To_Use);
996ae0b0
RK
4932 end if;
4933
4934 while Present (Comp) loop
996ae0b0
RK
4935 if Chars (Comp) = Chars (Sel) then
4936 if Ekind (Comp) = E_Discriminant then
e7ba564f 4937 Set_Entity_With_Checks (Sel, Comp);
996ae0b0
RK
4938 Generate_Reference (Comp, Sel);
4939
4940 Set_Etype (Sel, Etype (Comp));
4941 Set_Etype (N, Etype (Comp));
44a10091 4942 Check_Implicit_Dereference (N, Etype (N));
996ae0b0
RK
4943
4944 if Is_Generic_Type (Prefix_Type)
d469eabe 4945 or else Is_Generic_Type (Root_Type (Prefix_Type))
996ae0b0
RK
4946 then
4947 Set_Original_Discriminant (Sel, Comp);
4948 end if;
4949
f3d57416 4950 -- Before declaring an error, check whether this is tagged
aab883ec
ES
4951 -- private type and a call to a primitive operation.
4952
0791fbe9 4953 elsif Ada_Version >= Ada_2005
aab883ec
ES
4954 and then Is_Tagged_Type (Prefix_Type)
4955 and then Try_Object_Operation (N)
4956 then
4957 return;
4958
996ae0b0 4959 else
2383acbd
AC
4960 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4961 Error_Msg_NE ("invisible selector& for }", N, Sel);
996ae0b0
RK
4962 Set_Entity (Sel, Any_Id);
4963 Set_Etype (N, Any_Type);
4964 end if;
4965
4966 return;
4967 end if;
4968
4969 Next_Entity (Comp);
4970 end loop;
4971
4972 elsif Is_Concurrent_Type (Prefix_Type) then
4973
d469eabe
HK
4974 -- Find visible operation with given name. For a protected type,
4975 -- the possible candidates are discriminants, entries or protected
d1eb8a82 4976 -- subprograms. For a task type, the set can only include entries or
d469eabe
HK
4977 -- discriminants if the task type is not an enclosing scope. If it
4978 -- is an enclosing scope (e.g. in an inner task) then all entities
4979 -- are visible, but the prefix must denote the enclosing scope, i.e.
4980 -- can only be a direct name or an expanded name.
996ae0b0 4981
d469eabe 4982 Set_Etype (Sel, Any_Type);
c0e938d0 4983 Hidden_Comp := Empty;
996ae0b0 4984 In_Scope := In_Open_Scopes (Prefix_Type);
d1eb8a82 4985 Is_Private_Op := False;
996ae0b0
RK
4986
4987 while Present (Comp) loop
f31dcd99 4988
86ec3bfb
AC
4989 -- Do not examine private operations of the type if not within
4990 -- its scope.
4991
996ae0b0 4992 if Chars (Comp) = Chars (Sel) then
86ec3bfb
AC
4993 if Is_Overloadable (Comp)
4994 and then (In_Scope
4995 or else Comp /= First_Private_Entity (Type_To_Use))
4996 then
996ae0b0 4997 Add_One_Interp (Sel, Comp, Etype (Comp));
d1eb8a82
AC
4998 if Comp = First_Private_Entity (Type_To_Use) then
4999 Is_Private_Op := True;
5000 end if;
996ae0b0 5001
d469eabe
HK
5002 -- If the prefix is tagged, the correct interpretation may
5003 -- lie in the primitive or class-wide operations of the
5004 -- type. Perform a simple conformance check to determine
5005 -- whether Try_Object_Operation should be invoked even if
5006 -- a visible entity is found.
5007
5008 if Is_Tagged_Type (Prefix_Type)
f31dcd99
HK
5009 and then Nkind_In (Parent (N), N_Function_Call,
5010 N_Indexed_Component,
5011 N_Procedure_Call_Statement)
d469eabe
HK
5012 and then Has_Mode_Conformant_Spec (Comp)
5013 then
5014 Has_Candidate := True;
5015 end if;
5016
2383acbd
AC
5017 -- Note: a selected component may not denote a component of a
5018 -- protected type (4.1.3(7)).
5019
bce79204 5020 elsif Ekind_In (Comp, E_Discriminant, E_Entry_Family)
2383acbd
AC
5021 or else (In_Scope
5022 and then not Is_Protected_Type (Prefix_Type)
5023 and then Is_Entity_Name (Name))
996ae0b0 5024 then
e7ba564f 5025 Set_Entity_With_Checks (Sel, Comp);
996ae0b0
RK
5026 Generate_Reference (Comp, Sel);
5027
65e78a74
AC
5028 -- The selector is not overloadable, so we have a candidate
5029 -- interpretation.
5030
5031 Has_Candidate := True;
5032
996ae0b0 5033 else
c0e938d0
AC
5034 if Ekind (Comp) = E_Component then
5035 Hidden_Comp := Comp;
5036 end if;
5037
996ae0b0
RK
5038 goto Next_Comp;
5039 end if;
5040
5041 Set_Etype (Sel, Etype (Comp));
5042 Set_Etype (N, Etype (Comp));
5043
5044 if Ekind (Comp) = E_Discriminant then
5045 Set_Original_Discriminant (Sel, Comp);
5046 end if;
5047
09494c32
AC
5048 -- For access type case, introduce explicit dereference for
5049 -- more uniform treatment of entry calls.
996ae0b0
RK
5050
5051 if Is_Access_Type (Etype (Name)) then
5052 Insert_Explicit_Dereference (Name);
fbf5a39b 5053 Error_Msg_NW
324ac540 5054 (Warn_On_Dereference, "?d?implicit dereference", N);
996ae0b0
RK
5055 end if;
5056 end if;
5057
5058 <<Next_Comp>>
c0e938d0
AC
5059 if Comp = First_Private_Entity (Type_To_Use) then
5060 if Etype (Sel) /= Any_Type then
5061
27de857e
GD
5062 -- If the first private entity's name matches, then treat
5063 -- it as a private op: needed for the error check for
5064 -- illegal selection of private entities further below.
5065
5066 if Chars (Comp) = Chars (Sel) then
5067 Is_Private_Op := True;
5068 end if;
5069
5070 -- We have a candidate, so exit the loop
c5b4738f 5071
c0e938d0
AC
5072 exit;
5073
5074 else
5075 -- Indicate that subsequent operations are private,
5076 -- for better error reporting.
5077
5078 Is_Private_Op := True;
5079 end if;
5080 end if;
5081
614bc51c
PMR
5082 -- Do not examine private operations if not within scope of
5083 -- the synchronized type.
5084
996ae0b0 5085 exit when not In_Scope
9bc856dd
AC
5086 and then
5087 Comp = First_Private_Entity (Base_Type (Prefix_Type));
614bc51c 5088 Next_Entity (Comp);
996ae0b0
RK
5089 end loop;
5090
b3083540 5091 -- If the scope is a current instance, the prefix cannot be an
0f6251c7
AC
5092 -- expression of the same type, unless the selector designates a
5093 -- public operation (otherwise that would represent an attempt to
5094 -- reach an internal entity of another synchronized object).
be3416c6 5095
b3083540 5096 -- This is legal if prefix is an access to such type and there is
0f6251c7 5097 -- a dereference, or is a component with a dereferenced prefix.
0f8b3e5d
AC
5098 -- It is also legal if the prefix is a component of a task type,
5099 -- and the selector is one of the task operations.
b3083540 5100
bd717ec9
AC
5101 if In_Scope
5102 and then not Is_Entity_Name (Name)
5103 and then not Has_Dereference (Name)
5104 then
0f8b3e5d
AC
5105 if Is_Task_Type (Prefix_Type)
5106 and then Present (Entity (Sel))
5107 and then Ekind_In (Entity (Sel), E_Entry, E_Entry_Family)
5108 then
5109 null;
5110
d1eb8a82
AC
5111 elsif Is_Protected_Type (Prefix_Type)
5112 and then Is_Overloadable (Entity (Sel))
5113 and then not Is_Private_Op
5114 then
5115 null;
5116
0f8b3e5d
AC
5117 else
5118 Error_Msg_NE
5119 ("invalid reference to internal operation of some object of "
5120 & "type &", N, Type_To_Use);
5121 Set_Entity (Sel, Any_Id);
5122 Set_Etype (Sel, Any_Type);
5123 return;
5124 end if;
be3416c6
AC
5125
5126 -- Another special case: the prefix may denote an object of the type
5127 -- (but not a type) in which case this is an external call and the
5128 -- operation must be public.
5129
5130 elsif In_Scope
5131 and then Is_Object_Reference (Original_Node (Prefix (N)))
c0e938d0 5132 and then Comes_From_Source (N)
be3416c6
AC
5133 and then Is_Private_Op
5134 then
c0e938d0
AC
5135 if Present (Hidden_Comp) then
5136 Error_Msg_NE
c5b4738f
AC
5137 ("invalid reference to private component of object of type "
5138 & "&", N, Type_To_Use);
c0e938d0
AC
5139
5140 else
5141 Error_Msg_NE
5142 ("invalid reference to private operation of some object of "
5143 & "type &", N, Type_To_Use);
5144 end if;
5145
be3416c6
AC
5146 Set_Entity (Sel, Any_Id);
5147 Set_Etype (Sel, Any_Type);
5148 return;
b3083540
AC
5149 end if;
5150
d469eabe
HK
5151 -- If there is no visible entity with the given name or none of the
5152 -- visible entities are plausible interpretations, check whether
5153 -- there is some other primitive operation with that name.
aab883ec 5154
bc38dbb4 5155 if Ada_Version >= Ada_2005 and then Is_Tagged_Type (Prefix_Type) then
d469eabe
HK
5156 if (Etype (N) = Any_Type
5157 or else not Has_Candidate)
0a36105d
JM
5158 and then Try_Object_Operation (N)
5159 then
5160 return;
5161
5162 -- If the context is not syntactically a procedure call, it
5163 -- may be a call to a primitive function declared outside of
5164 -- the synchronized type.
5165
5166 -- If the context is a procedure call, there might still be
5167 -- an overloading between an entry and a primitive procedure
5168 -- declared outside of the synchronized type, called in prefix
5169 -- notation. This is harder to disambiguate because in one case
5170 -- the controlling formal is implicit ???
5171
5172 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
b4592168 5173 and then Nkind (Parent (N)) /= N_Indexed_Component
0a36105d
JM
5174 and then Try_Object_Operation (N)
5175 then
5176 return;
5177 end if;
8cf23b91
AC
5178
5179 -- Ada 2012 (AI05-0090-1): If we found a candidate of a call to an
5180 -- entry or procedure of a tagged concurrent type we must check
5181 -- if there are class-wide subprograms covering the primitive. If
5182 -- true then Try_Object_Operation reports the error.
5183
5184 if Has_Candidate
5185 and then Is_Concurrent_Type (Prefix_Type)
5186 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
bc38dbb4 5187 then
8cf23b91
AC
5188 -- Duplicate the call. This is required to avoid problems with
5189 -- the tree transformations performed by Try_Object_Operation.
d7a44b14
AC
5190 -- Set properly the parent of the copied call, because it is
5191 -- about to be reanalyzed.
8cf23b91 5192
d7a44b14
AC
5193 declare
5194 Par : constant Node_Id := New_Copy_Tree (Parent (N));
5195
5196 begin
5197 Set_Parent (Par, Parent (Parent (N)));
29ba9f52 5198
d7a44b14 5199 if Try_Object_Operation
29ba9f52 5200 (Sinfo.Name (Par), CW_Test_Only => True)
d7a44b14
AC
5201 then
5202 return;
5203 end if;
5204 end;
8cf23b91 5205 end if;
aab883ec
ES
5206 end if;
5207
2383acbd 5208 if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
11bc76df 5209
2383acbd
AC
5210 -- Case of a prefix of a protected type: selector might denote
5211 -- an invisible private component.
5212
5213 Comp := First_Private_Entity (Base_Type (Prefix_Type));
5214 while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
5215 Next_Entity (Comp);
5216 end loop;
5217
5218 if Present (Comp) then
5219 if Is_Single_Concurrent_Object then
5220 Error_Msg_Node_2 := Entity (Name);
5221 Error_Msg_NE ("invisible selector& for &", N, Sel);
5222
5223 else
5224 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5225 Error_Msg_NE ("invisible selector& for }", N, Sel);
5226 end if;
5227 return;
5228 end if;
5229 end if;
5230
996ae0b0
RK
5231 Set_Is_Overloaded (N, Is_Overloaded (Sel));
5232
5233 else
5234 -- Invalid prefix
5235
5236 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
5237 end if;
5238
4c46b835 5239 -- If N still has no type, the component is not defined in the prefix
996ae0b0
RK
5240
5241 if Etype (N) = Any_Type then
5242
2383acbd 5243 if Is_Single_Concurrent_Object then
996ae0b0
RK
5244 Error_Msg_Node_2 := Entity (Name);
5245 Error_Msg_NE ("no selector& for&", N, Sel);
5246
401093c1 5247 Check_Misspelled_Selector (Type_To_Use, Sel);
996ae0b0 5248
8b4230c8
AC
5249 -- If this is a derived formal type, the parent may have different
5250 -- visibility at this point. Try for an inherited component before
5251 -- reporting an error.
5252
de76a39c
GB
5253 elsif Is_Generic_Type (Prefix_Type)
5254 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
07fc65c4 5255 and then Prefix_Type /= Etype (Prefix_Type)
de76a39c
GB
5256 and then Is_Record_Type (Etype (Prefix_Type))
5257 then
de76a39c
GB
5258 Set_Etype (Prefix (N), Etype (Prefix_Type));
5259 Analyze_Selected_Component (N);
5260 return;
5261
b1d12996
AC
5262 -- Similarly, if this is the actual for a formal derived type, or
5263 -- a derived type thereof, the component inherited from the generic
5264 -- parent may not be visible in the actual, but the selected
5265 -- component is legal. Climb up the derivation chain of the generic
5266 -- parent type until we find the proper ancestor type.
20261dc1 5267
b1d12996
AC
5268 elsif In_Instance and then Is_Tagged_Type (Prefix_Type) then
5269 declare
5270 Par : Entity_Id := Prefix_Type;
5271 begin
5272 -- Climb up derivation chain to generic actual subtype
5273
5274 while not Is_Generic_Actual_Type (Par) loop
5275 if Ekind (Par) = E_Record_Type then
5276 Par := Parent_Subtype (Par);
5277 exit when No (Par);
5278 else
5279 exit when Par = Etype (Par);
5280 Par := Etype (Par);
5281 end if;
5282 end loop;
4c46b835 5283
b1d12996 5284 if Present (Par) and then Is_Generic_Actual_Type (Par) then
73999267 5285
b1d12996 5286 -- Now look for component in ancestor types
fbf5a39b 5287
b1d12996
AC
5288 Par := Generic_Parent_Type (Declaration_Node (Par));
5289 loop
5290 Find_Component_In_Instance (Par);
5291 exit when Present (Entity (Sel))
5292 or else Par = Etype (Par);
5293 Par := Etype (Par);
5294 end loop;
73999267 5295
7d9880c9
AC
5296 -- Another special case: the type is an extension of a private
5297 -- type T, is an actual in an instance, and we are in the body
5298 -- of the instance, so the generic body had a full view of the
5299 -- type declaration for T or of some ancestor that defines the
5300 -- component in question.
5301
5302 elsif Is_Derived_Type (Type_To_Use)
5303 and then Used_As_Generic_Actual (Type_To_Use)
5304 and then In_Instance_Body
5305 then
5306 Find_Component_In_Instance (Parent_Subtype (Type_To_Use));
5307
73999267
AC
5308 -- In ASIS mode the generic parent type may be absent. Examine
5309 -- the parent type directly for a component that may have been
5310 -- visible in a parent generic unit.
5311
5312 elsif Is_Derived_Type (Prefix_Type) then
5313 Par := Etype (Prefix_Type);
5314 Find_Component_In_Instance (Par);
b1d12996
AC
5315 end if;
5316 end;
5317
5318 -- The search above must have eventually succeeded, since the
5319 -- selected component was legal in the generic.
5320
5321 if No (Entity (Sel)) then
5322 raise Program_Error;
5323 end if;
73999267 5324
20261dc1 5325 return;
fbf5a39b 5326
20261dc1 5327 -- Component not found, specialize error message when appropriate
fbf5a39b 5328
996ae0b0
RK
5329 else
5330 if Ekind (Prefix_Type) = E_Record_Subtype then
5331
f4b049db
AC
5332 -- Check whether this is a component of the base type which
5333 -- is absent from a statically constrained subtype. This will
5334 -- raise constraint error at run time, but is not a compile-
5335 -- time error. When the selector is illegal for base type as
5336 -- well fall through and generate a compilation error anyway.
996ae0b0
RK
5337
5338 Comp := First_Component (Base_Type (Prefix_Type));
996ae0b0 5339 while Present (Comp) loop
996ae0b0 5340 if Chars (Comp) = Chars (Sel)
24e95966 5341 and then Is_Visible_Component (Comp, Sel)
996ae0b0 5342 then
e7ba564f 5343 Set_Entity_With_Checks (Sel, Comp);
996ae0b0
RK
5344 Generate_Reference (Comp, Sel);
5345 Set_Etype (Sel, Etype (Comp));
5346 Set_Etype (N, Etype (Comp));
5347
637a41a5
AC
5348 -- Emit appropriate message. The node will be replaced
5349 -- by an appropriate raise statement.
996ae0b0 5350
637a41a5
AC
5351 -- Note that in SPARK mode, as with all calls to apply a
5352 -- compile time constraint error, this will be made into
5353 -- an error to simplify the processing of the formal
5354 -- verification backend.
d7f41b2d 5355
4a28b181 5356 Apply_Compile_Time_Constraint_Error
637a41a5 5357 (N, "component not present in }??",
4a28b181
AC
5358 CE_Discriminant_Check_Failed,
5359 Ent => Prefix_Type, Rep => False);
d7f41b2d 5360
996ae0b0
RK
5361 Set_Raises_Constraint_Error (N);
5362 return;
5363 end if;
5364
5365 Next_Component (Comp);
5366 end loop;
5367
5368 end if;
5369
5370 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
5371 Error_Msg_NE ("no selector& for}", N, Sel);
5372
94bbf008 5373 -- Add information in the case of an incomplete prefix
99d520ad
ES
5374
5375 if Is_Incomplete_Type (Type_To_Use) then
5376 declare
5377 Inc : constant Entity_Id := First_Subtype (Type_To_Use);
5378
5379 begin
7b56a91b 5380 if From_Limited_With (Scope (Type_To_Use)) then
99d520ad
ES
5381 Error_Msg_NE
5382 ("\limited view of& has no components", N, Inc);
bd38b431 5383
99d520ad
ES
5384 else
5385 Error_Msg_NE
5386 ("\premature usage of incomplete type&", N, Inc);
bd38b431
AC
5387
5388 if Nkind (Parent (Inc)) =
5389 N_Incomplete_Type_Declaration
99d520ad 5390 then
94bbf008
AC
5391 -- Record location of premature use in entity so that
5392 -- a continuation message is generated when the
5393 -- completion is seen.
5394
99d520ad
ES
5395 Set_Premature_Use (Parent (Inc), N);
5396 end if;
5397 end if;
5398 end;
5399 end if;
5400
401093c1 5401 Check_Misspelled_Selector (Type_To_Use, Sel);
996ae0b0
RK
5402 end if;
5403
5404 Set_Entity (Sel, Any_Id);
5405 Set_Etype (Sel, Any_Type);
5406 end if;
5407 end Analyze_Selected_Component;
5408
5409 ---------------------------
5410 -- Analyze_Short_Circuit --
5411 ---------------------------
5412
5413 procedure Analyze_Short_Circuit (N : Node_Id) is
5414 L : constant Node_Id := Left_Opnd (N);
5415 R : constant Node_Id := Right_Opnd (N);
5416 Ind : Interp_Index;
5417 It : Interp;
5418
5419 begin
5420 Analyze_Expression (L);
5421 Analyze_Expression (R);
5422 Set_Etype (N, Any_Type);
5423
5424 if not Is_Overloaded (L) then
996ae0b0
RK
5425 if Root_Type (Etype (L)) = Standard_Boolean
5426 and then Has_Compatible_Type (R, Etype (L))
5427 then
5428 Add_One_Interp (N, Etype (L), Etype (L));
5429 end if;
5430
5431 else
5432 Get_First_Interp (L, Ind, It);
996ae0b0
RK
5433 while Present (It.Typ) loop
5434 if Root_Type (It.Typ) = Standard_Boolean
5435 and then Has_Compatible_Type (R, It.Typ)
5436 then
5437 Add_One_Interp (N, It.Typ, It.Typ);
5438 end if;
5439
5440 Get_Next_Interp (Ind, It);
5441 end loop;
5442 end if;
5443
d469eabe
HK
5444 -- Here we have failed to find an interpretation. Clearly we know that
5445 -- it is not the case that both operands can have an interpretation of
5446 -- Boolean, but this is by far the most likely intended interpretation.
5447 -- So we simply resolve both operands as Booleans, and at least one of
5448 -- these resolutions will generate an error message, and we do not need
5449 -- to give another error message on the short circuit operation itself.
996ae0b0
RK
5450
5451 if Etype (N) = Any_Type then
5452 Resolve (L, Standard_Boolean);
5453 Resolve (R, Standard_Boolean);
5454 Set_Etype (N, Standard_Boolean);
5455 end if;
5456 end Analyze_Short_Circuit;
5457
5458 -------------------
5459 -- Analyze_Slice --
5460 -------------------
5461
5462 procedure Analyze_Slice (N : Node_Id) is
996ae0b0 5463 D : constant Node_Id := Discrete_Range (N);
5f44f0d4 5464 P : constant Node_Id := Prefix (N);
996ae0b0 5465 Array_Type : Entity_Id;
5f44f0d4 5466 Index_Type : Entity_Id;
996ae0b0
RK
5467
5468 procedure Analyze_Overloaded_Slice;
5469 -- If the prefix is overloaded, select those interpretations that
5470 -- yield a one-dimensional array type.
5471
4c46b835
AC
5472 ------------------------------
5473 -- Analyze_Overloaded_Slice --
5474 ------------------------------
5475
996ae0b0
RK
5476 procedure Analyze_Overloaded_Slice is
5477 I : Interp_Index;
5478 It : Interp;
5479 Typ : Entity_Id;
5480
5481 begin
5482 Set_Etype (N, Any_Type);
996ae0b0 5483
4c46b835 5484 Get_First_Interp (P, I, It);
996ae0b0
RK
5485 while Present (It.Nam) loop
5486 Typ := It.Typ;
5487
5488 if Is_Access_Type (Typ) then
5489 Typ := Designated_Type (Typ);
324ac540
AC
5490 Error_Msg_NW
5491 (Warn_On_Dereference, "?d?implicit dereference", N);
996ae0b0
RK
5492 end if;
5493
5494 if Is_Array_Type (Typ)
5495 and then Number_Dimensions (Typ) = 1
5496 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
5497 then
5498 Add_One_Interp (N, Typ, Typ);
5499 end if;
5500
5501 Get_Next_Interp (I, It);
5502 end loop;
5503
5504 if Etype (N) = Any_Type then
5505 Error_Msg_N ("expect array type in prefix of slice", N);
5506 end if;
5507 end Analyze_Overloaded_Slice;
5508
5509 -- Start of processing for Analyze_Slice
5510
5511 begin
36b8f95f 5512 if Comes_From_Source (N) then
ce5ba43a 5513 Check_SPARK_05_Restriction ("slice is not allowed", N);
36b8f95f 5514 end if;
1d801f21 5515
523456db 5516 Analyze (P);
996ae0b0
RK
5517 Analyze (D);
5518
5519 if Is_Overloaded (P) then
5520 Analyze_Overloaded_Slice;
5521
5522 else
5523 Array_Type := Etype (P);
5524 Set_Etype (N, Any_Type);
5525
5526 if Is_Access_Type (Array_Type) then
5527 Array_Type := Designated_Type (Array_Type);
324ac540 5528 Error_Msg_NW (Warn_On_Dereference, "?d?implicit dereference", N);
996ae0b0
RK
5529 end if;
5530
5531 if not Is_Array_Type (Array_Type) then
5532 Wrong_Type (P, Any_Array);
5533
5534 elsif Number_Dimensions (Array_Type) > 1 then
5535 Error_Msg_N
5536 ("type is not one-dimensional array in slice prefix", N);
5537
996ae0b0 5538 else
5f44f0d4
AC
5539 if Ekind (Array_Type) = E_String_Literal_Subtype then
5540 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
5541 else
5542 Index_Type := Etype (First_Index (Array_Type));
5543 end if;
5544
5545 if not Has_Compatible_Type (D, Index_Type) then
5546 Wrong_Type (D, Index_Type);
5547 else
5548 Set_Etype (N, Array_Type);
5549 end if;
996ae0b0
RK
5550 end if;
5551 end if;
5552 end Analyze_Slice;
5553
5554 -----------------------------
5555 -- Analyze_Type_Conversion --
5556 -----------------------------
5557
5558 procedure Analyze_Type_Conversion (N : Node_Id) is
5559 Expr : constant Node_Id := Expression (N);
039538bc 5560 Typ : Entity_Id;
996ae0b0
RK
5561
5562 begin
039538bc
AC
5563 -- If Conversion_OK is set, then the Etype is already set, and the only
5564 -- processing required is to analyze the expression. This is used to
5565 -- construct certain "illegal" conversions which are not allowed by Ada
5566 -- semantics, but can be handled by Gigi, see Sinfo for further details.
996ae0b0
RK
5567
5568 if Conversion_OK (N) then
5569 Analyze (Expr);
5570 return;
5571 end if;
5572
5573 -- Otherwise full type analysis is required, as well as some semantic
5574 -- checks to make sure the argument of the conversion is appropriate.
5575
5576 Find_Type (Subtype_Mark (N));
039538bc
AC
5577 Typ := Entity (Subtype_Mark (N));
5578 Set_Etype (N, Typ);
5579 Check_Fully_Declared (Typ, N);
996ae0b0
RK
5580 Analyze_Expression (Expr);
5581 Validate_Remote_Type_Type_Conversion (N);
5e8c8e44 5582
996ae0b0
RK
5583 -- Only remaining step is validity checks on the argument. These
5584 -- are skipped if the conversion does not come from the source.
5585
5586 if not Comes_From_Source (N) then
5587 return;
5588
b67a385c
ES
5589 -- If there was an error in a generic unit, no need to replicate the
5590 -- error message. Conversely, constant-folding in the generic may
5591 -- transform the argument of a conversion into a string literal, which
5592 -- is legal. Therefore the following tests are not performed in an
36428cc4 5593 -- instance. The same applies to an inlined body.
b67a385c 5594
36428cc4 5595 elsif In_Instance or In_Inlined_Body then
b67a385c
ES
5596 return;
5597
996ae0b0
RK
5598 elsif Nkind (Expr) = N_Null then
5599 Error_Msg_N ("argument of conversion cannot be null", N);
ed2233dc 5600 Error_Msg_N ("\use qualified expression instead", N);
996ae0b0
RK
5601 Set_Etype (N, Any_Type);
5602
5603 elsif Nkind (Expr) = N_Aggregate then
5604 Error_Msg_N ("argument of conversion cannot be aggregate", N);
ed2233dc 5605 Error_Msg_N ("\use qualified expression instead", N);
996ae0b0
RK
5606
5607 elsif Nkind (Expr) = N_Allocator then
5608 Error_Msg_N ("argument of conversion cannot be an allocator", N);
ed2233dc 5609 Error_Msg_N ("\use qualified expression instead", N);
996ae0b0
RK
5610
5611 elsif Nkind (Expr) = N_String_Literal then
5612 Error_Msg_N ("argument of conversion cannot be string literal", N);
ed2233dc 5613 Error_Msg_N ("\use qualified expression instead", N);
996ae0b0
RK
5614
5615 elsif Nkind (Expr) = N_Character_Literal then
0ab80019 5616 if Ada_Version = Ada_83 then
039538bc 5617 Resolve (Expr, Typ);
996ae0b0
RK
5618 else
5619 Error_Msg_N ("argument of conversion cannot be character literal",
5620 N);
ed2233dc 5621 Error_Msg_N ("\use qualified expression instead", N);
996ae0b0
RK
5622 end if;
5623
5624 elsif Nkind (Expr) = N_Attribute_Reference
039538bc
AC
5625 and then Nam_In (Attribute_Name (Expr), Name_Access,
5626 Name_Unchecked_Access,
5627 Name_Unrestricted_Access)
996ae0b0
RK
5628 then
5629 Error_Msg_N ("argument of conversion cannot be access", N);
ed2233dc 5630 Error_Msg_N ("\use qualified expression instead", N);
996ae0b0 5631 end if;
039538bc
AC
5632
5633 -- A formal parameter of a specific tagged type whose related subprogram
5634 -- is subject to pragma Extensions_Visible with value "False" cannot
070d862d
HK
5635 -- appear in a class-wide conversion (SPARK RM 6.1.7(3)). Do not check
5636 -- internally generated expressions.
039538bc 5637
070d862d
HK
5638 if Is_Class_Wide_Type (Typ)
5639 and then Comes_From_Source (Expr)
5640 and then Is_EVF_Expression (Expr)
5641 then
039538bc 5642 Error_Msg_N
44900051
AC
5643 ("formal parameter cannot be converted to class-wide type when "
5644 & "Extensions_Visible is False", Expr);
039538bc 5645 end if;
996ae0b0
RK
5646 end Analyze_Type_Conversion;
5647
5648 ----------------------
5649 -- Analyze_Unary_Op --
5650 ----------------------
5651
5652 procedure Analyze_Unary_Op (N : Node_Id) is
5653 R : constant Node_Id := Right_Opnd (N);
5654 Op_Id : Entity_Id := Entity (N);
5655
5656 begin
5657 Set_Etype (N, Any_Type);
5658 Candidate_Type := Empty;
5659
5660 Analyze_Expression (R);
5661
5662 if Present (Op_Id) then
5663 if Ekind (Op_Id) = E_Operator then
5664 Find_Unary_Types (R, Op_Id, N);
5665 else
5666 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5667 end if;
5668
5669 else
5670 Op_Id := Get_Name_Entity_Id (Chars (N));
996ae0b0 5671 while Present (Op_Id) loop
996ae0b0
RK
5672 if Ekind (Op_Id) = E_Operator then
5673 if No (Next_Entity (First_Entity (Op_Id))) then
5674 Find_Unary_Types (R, Op_Id, N);
5675 end if;
5676
5677 elsif Is_Overloadable (Op_Id) then
5678 Analyze_User_Defined_Unary_Op (N, Op_Id);
5679 end if;
5680
5681 Op_Id := Homonym (Op_Id);
5682 end loop;
5683 end if;
5684
5685 Operator_Check (N);
5686 end Analyze_Unary_Op;
5687
5688 ----------------------------------
5689 -- Analyze_Unchecked_Expression --
5690 ----------------------------------
5691
5692 procedure Analyze_Unchecked_Expression (N : Node_Id) is
5693 begin
5694 Analyze (Expression (N), Suppress => All_Checks);
5695 Set_Etype (N, Etype (Expression (N)));
5696 Save_Interps (Expression (N), N);
5697 end Analyze_Unchecked_Expression;
5698
5699 ---------------------------------------
5700 -- Analyze_Unchecked_Type_Conversion --
5701 ---------------------------------------
5702
5703 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
5704 begin
5705 Find_Type (Subtype_Mark (N));
5706 Analyze_Expression (Expression (N));
5707 Set_Etype (N, Entity (Subtype_Mark (N)));
5708 end Analyze_Unchecked_Type_Conversion;
5709
5710 ------------------------------------
5711 -- Analyze_User_Defined_Binary_Op --
5712 ------------------------------------
5713
5714 procedure Analyze_User_Defined_Binary_Op
5715 (N : Node_Id;
5716 Op_Id : Entity_Id)
5717 is
5718 begin
5719 -- Only do analysis if the operator Comes_From_Source, since otherwise
5720 -- the operator was generated by the expander, and all such operators
5721 -- always refer to the operators in package Standard.
5722
5723 if Comes_From_Source (N) then
5724 declare
5725 F1 : constant Entity_Id := First_Formal (Op_Id);
5726 F2 : constant Entity_Id := Next_Formal (F1);
5727
5728 begin
5729 -- Verify that Op_Id is a visible binary function. Note that since
5730 -- we know Op_Id is overloaded, potentially use visible means use
5731 -- visible for sure (RM 9.4(11)).
5732
5733 if Ekind (Op_Id) = E_Function
5734 and then Present (F2)
5735 and then (Is_Immediately_Visible (Op_Id)
5736 or else Is_Potentially_Use_Visible (Op_Id))
5737 and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
5738 and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
5739 then
5740 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5741
8b4230c8
AC
5742 -- If the left operand is overloaded, indicate that the current
5743 -- type is a viable candidate. This is redundant in most cases,
5744 -- but for equality and comparison operators where the context
5745 -- does not impose a type on the operands, setting the proper
5746 -- type is necessary to avoid subsequent ambiguities during
5747 -- resolution, when both user-defined and predefined operators
5748 -- may be candidates.
7340e432
AC
5749
5750 if Is_Overloaded (Left_Opnd (N)) then
5751 Set_Etype (Left_Opnd (N), Etype (F1));
5752 end if;
5753
996ae0b0
RK
5754 if Debug_Flag_E then
5755 Write_Str ("user defined operator ");
5756 Write_Name (Chars (Op_Id));
5757 Write_Str (" on node ");
5758 Write_Int (Int (N));
5759 Write_Eol;
5760 end if;
5761 end if;
5762 end;
5763 end if;
5764 end Analyze_User_Defined_Binary_Op;
5765
5766 -----------------------------------
5767 -- Analyze_User_Defined_Unary_Op --
5768 -----------------------------------
5769
5770 procedure Analyze_User_Defined_Unary_Op
5771 (N : Node_Id;
5772 Op_Id : Entity_Id)
5773 is
5774 begin
5775 -- Only do analysis if the operator Comes_From_Source, since otherwise
5776 -- the operator was generated by the expander, and all such operators
5777 -- always refer to the operators in package Standard.
5778
5779 if Comes_From_Source (N) then
5780 declare
5781 F : constant Entity_Id := First_Formal (Op_Id);
5782
5783 begin
5784 -- Verify that Op_Id is a visible unary function. Note that since
5785 -- we know Op_Id is overloaded, potentially use visible means use
5786 -- visible for sure (RM 9.4(11)).
5787
5788 if Ekind (Op_Id) = E_Function
5789 and then No (Next_Formal (F))
5790 and then (Is_Immediately_Visible (Op_Id)
5791 or else Is_Potentially_Use_Visible (Op_Id))
5792 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
5793 then
5794 Add_One_Interp (N, Op_Id, Etype (Op_Id));
5795 end if;
5796 end;
5797 end if;
5798 end Analyze_User_Defined_Unary_Op;
5799
5800 ---------------------------
5801 -- Check_Arithmetic_Pair --
5802 ---------------------------
5803
5804 procedure Check_Arithmetic_Pair
5805 (T1, T2 : Entity_Id;
5806 Op_Id : Entity_Id;
5807 N : Node_Id)
5808 is
401093c1 5809 Op_Name : constant Name_Id := Chars (Op_Id);
996ae0b0 5810
da709d08
AC
5811 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
5812 -- Check whether the fixed-point type Typ has a user-defined operator
5813 -- (multiplication or division) that should hide the corresponding
5814 -- predefined operator. Used to implement Ada 2005 AI-264, to make
5815 -- such operators more visible and therefore useful.
8b4230c8 5816 --
50cff367
GD
5817 -- If the name of the operation is an expanded name with prefix
5818 -- Standard, the predefined universal fixed operator is available,
5819 -- as specified by AI-420 (RM 4.5.5 (19.1/2)).
5820
996ae0b0
RK
5821 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
5822 -- Get specific type (i.e. non-universal type if there is one)
5823
da709d08
AC
5824 ------------------
5825 -- Has_Fixed_Op --
5826 ------------------
5827
5828 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
401093c1 5829 Bas : constant Entity_Id := Base_Type (Typ);
da709d08
AC
5830 Ent : Entity_Id;
5831 F1 : Entity_Id;
5832 F2 : Entity_Id;
5833
5834 begin
50cff367
GD
5835 -- If the universal_fixed operation is given explicitly the rule
5836 -- concerning primitive operations of the type do not apply.
5837
5838 if Nkind (N) = N_Function_Call
5839 and then Nkind (Name (N)) = N_Expanded_Name
5840 and then Entity (Prefix (Name (N))) = Standard_Standard
5841 then
5842 return False;
5843 end if;
5844
da709d08
AC
5845 -- The operation is treated as primitive if it is declared in the
5846 -- same scope as the type, and therefore on the same entity chain.
5847
5848 Ent := Next_Entity (Typ);
5849 while Present (Ent) loop
5850 if Chars (Ent) = Chars (Op) then
5851 F1 := First_Formal (Ent);
5852 F2 := Next_Formal (F1);
5853
5854 -- The operation counts as primitive if either operand or
401093c1
ES
5855 -- result are of the given base type, and both operands are
5856 -- fixed point types.
da709d08 5857
401093c1 5858 if (Base_Type (Etype (F1)) = Bas
da709d08
AC
5859 and then Is_Fixed_Point_Type (Etype (F2)))
5860
5861 or else
401093c1 5862 (Base_Type (Etype (F2)) = Bas
da709d08
AC
5863 and then Is_Fixed_Point_Type (Etype (F1)))
5864
5865 or else
401093c1 5866 (Base_Type (Etype (Ent)) = Bas
da709d08
AC
5867 and then Is_Fixed_Point_Type (Etype (F1))
5868 and then Is_Fixed_Point_Type (Etype (F2)))
5869 then
5870 return True;
5871 end if;
5872 end if;
5873
5874 Next_Entity (Ent);
5875 end loop;
5876
5877 return False;
5878 end Has_Fixed_Op;
5879
4c46b835
AC
5880 -------------------
5881 -- Specific_Type --
5882 -------------------
5883
996ae0b0
RK
5884 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
5885 begin
5886 if T1 = Universal_Integer or else T1 = Universal_Real then
5887 return Base_Type (T2);
5888 else
5889 return Base_Type (T1);
5890 end if;
5891 end Specific_Type;
5892
5893 -- Start of processing for Check_Arithmetic_Pair
5894
5895 begin
b69cd36a 5896 if Nam_In (Op_Name, Name_Op_Add, Name_Op_Subtract) then
996ae0b0
RK
5897 if Is_Numeric_Type (T1)
5898 and then Is_Numeric_Type (T2)
b4592168
GD
5899 and then (Covers (T1 => T1, T2 => T2)
5900 or else
5901 Covers (T1 => T2, T2 => T1))
996ae0b0
RK
5902 then
5903 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5904 end if;
5905
b69cd36a 5906 elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide) then
996ae0b0 5907 if Is_Fixed_Point_Type (T1)
b69cd36a 5908 and then (Is_Fixed_Point_Type (T2) or else T2 = Universal_Real)
996ae0b0 5909 then
fa54f4da
EB
5910 -- Add one interpretation with universal fixed result
5911
5912 if not Has_Fixed_Op (T1, Op_Id)
5913 or else Nkind (Parent (N)) = N_Type_Conversion
fbf5a39b 5914 then
996ae0b0
RK
5915 Add_One_Interp (N, Op_Id, Universal_Fixed);
5916 end if;
5917
5918 elsif Is_Fixed_Point_Type (T2)
996ae0b0 5919 and then T1 = Universal_Real
da709d08 5920 and then
401093c1 5921 (not Has_Fixed_Op (T1, Op_Id)
da709d08 5922 or else Nkind (Parent (N)) = N_Type_Conversion)
996ae0b0
RK
5923 then
5924 Add_One_Interp (N, Op_Id, Universal_Fixed);
5925
5926 elsif Is_Numeric_Type (T1)
5927 and then Is_Numeric_Type (T2)
b4592168
GD
5928 and then (Covers (T1 => T1, T2 => T2)
5929 or else
5930 Covers (T1 => T2, T2 => T1))
996ae0b0
RK
5931 then
5932 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5933
5934 elsif Is_Fixed_Point_Type (T1)
5935 and then (Base_Type (T2) = Base_Type (Standard_Integer)
b69cd36a 5936 or else T2 = Universal_Integer)
996ae0b0
RK
5937 then
5938 Add_One_Interp (N, Op_Id, T1);
5939
5940 elsif T2 = Universal_Real
5941 and then Base_Type (T1) = Base_Type (Standard_Integer)
5942 and then Op_Name = Name_Op_Multiply
5943 then
5944 Add_One_Interp (N, Op_Id, Any_Fixed);
5945
5946 elsif T1 = Universal_Real
5947 and then Base_Type (T2) = Base_Type (Standard_Integer)
5948 then
5949 Add_One_Interp (N, Op_Id, Any_Fixed);
5950
5951 elsif Is_Fixed_Point_Type (T2)
5952 and then (Base_Type (T1) = Base_Type (Standard_Integer)
b69cd36a 5953 or else T1 = Universal_Integer)
996ae0b0
RK
5954 and then Op_Name = Name_Op_Multiply
5955 then
5956 Add_One_Interp (N, Op_Id, T2);
5957
5958 elsif T1 = Universal_Real and then T2 = Universal_Integer then
5959 Add_One_Interp (N, Op_Id, T1);
5960
5961 elsif T2 = Universal_Real
5962 and then T1 = Universal_Integer
5963 and then Op_Name = Name_Op_Multiply
5964 then
5965 Add_One_Interp (N, Op_Id, T2);
5966 end if;
5967
5968 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
5969
996ae0b0 5970 if Is_Integer_Type (T1)
b4592168
GD
5971 and then (Covers (T1 => T1, T2 => T2)
5972 or else
5973 Covers (T1 => T2, T2 => T1))
996ae0b0
RK
5974 then
5975 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
5976 end if;
5977
5978 elsif Op_Name = Name_Op_Expon then
996ae0b0
RK
5979 if Is_Numeric_Type (T1)
5980 and then not Is_Fixed_Point_Type (T1)
5981 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5982 or else T2 = Universal_Integer)
5983 then
5984 Add_One_Interp (N, Op_Id, Base_Type (T1));
5985 end if;
5986
5987 else pragma Assert (Nkind (N) in N_Op_Shift);
5988
5989 -- If not one of the predefined operators, the node may be one
5990 -- of the intrinsic functions. Its kind is always specific, and
5991 -- we can use it directly, rather than the name of the operation.
5992
5993 if Is_Integer_Type (T1)
5994 and then (Base_Type (T2) = Base_Type (Standard_Integer)
5995 or else T2 = Universal_Integer)
5996 then
5997 Add_One_Interp (N, Op_Id, Base_Type (T1));
5998 end if;
5999 end if;
6000 end Check_Arithmetic_Pair;
6001
6002 -------------------------------
6003 -- Check_Misspelled_Selector --
6004 -------------------------------
6005
6006 procedure Check_Misspelled_Selector
6007 (Prefix : Entity_Id;
6008 Sel : Node_Id)
6009 is
6010 Max_Suggestions : constant := 2;
6011 Nr_Of_Suggestions : Natural := 0;
6012
6013 Suggestion_1 : Entity_Id := Empty;
6014 Suggestion_2 : Entity_Id := Empty;
6015
6016 Comp : Entity_Id;
6017
6018 begin
8b4230c8
AC
6019 -- All the components of the prefix of selector Sel are matched against
6020 -- Sel and a count is maintained of possible misspellings. When at
a90bd866 6021 -- the end of the analysis there are one or two (not more) possible
8b4230c8
AC
6022 -- misspellings, these misspellings will be suggested as possible
6023 -- correction.
996ae0b0 6024
4c46b835
AC
6025 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
6026
996ae0b0 6027 -- Concurrent types should be handled as well ???
4c46b835 6028
996ae0b0
RK
6029 return;
6030 end if;
6031
d469eabe
HK
6032 Comp := First_Entity (Prefix);
6033 while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
24e95966 6034 if Is_Visible_Component (Comp, Sel) then
d469eabe
HK
6035 if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
6036 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
6037
6038 case Nr_Of_Suggestions is
6039 when 1 => Suggestion_1 := Comp;
6040 when 2 => Suggestion_2 := Comp;
e49de265 6041 when others => null;
d469eabe 6042 end case;
996ae0b0 6043 end if;
d469eabe 6044 end if;
996ae0b0 6045
d469eabe
HK
6046 Comp := Next_Entity (Comp);
6047 end loop;
996ae0b0 6048
d469eabe 6049 -- Report at most two suggestions
996ae0b0 6050
d469eabe 6051 if Nr_Of_Suggestions = 1 then
4e7a4f6e 6052 Error_Msg_NE -- CODEFIX
d469eabe 6053 ("\possible misspelling of&", Sel, Suggestion_1);
996ae0b0 6054
d469eabe
HK
6055 elsif Nr_Of_Suggestions = 2 then
6056 Error_Msg_Node_2 := Suggestion_2;
4e7a4f6e 6057 Error_Msg_NE -- CODEFIX
d469eabe
HK
6058 ("\possible misspelling of& or&", Sel, Suggestion_1);
6059 end if;
996ae0b0
RK
6060 end Check_Misspelled_Selector;
6061
6062 ----------------------
6063 -- Defined_In_Scope --
6064 ----------------------
6065
6066 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
6067 is
6068 S1 : constant Entity_Id := Scope (Base_Type (T));
996ae0b0
RK
6069 begin
6070 return S1 = S
6071 or else (S1 = System_Aux_Id and then S = Scope (S1));
6072 end Defined_In_Scope;
6073
6074 -------------------
6075 -- Diagnose_Call --
6076 -------------------
6077
6078 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
fbf5a39b
AC
6079 Actual : Node_Id;
6080 X : Interp_Index;
6081 It : Interp;
fbf5a39b
AC
6082 Err_Mode : Boolean;
6083 New_Nam : Node_Id;
6084 Void_Interp_Seen : Boolean := False;
996ae0b0 6085
24657705
HK
6086 Success : Boolean;
6087 pragma Warnings (Off, Boolean);
6088
996ae0b0 6089 begin
0791fbe9 6090 if Ada_Version >= Ada_2005 then
996ae0b0 6091 Actual := First_Actual (N);
996ae0b0 6092 while Present (Actual) loop
0ab80019
AC
6093
6094 -- Ada 2005 (AI-50217): Post an error in case of premature
6095 -- usage of an entity from the limited view.
19f0526a 6096
996ae0b0 6097 if not Analyzed (Etype (Actual))
7b56a91b 6098 and then From_Limited_With (Etype (Actual))
996ae0b0
RK
6099 then
6100 Error_Msg_Qual_Level := 1;
ed2233dc 6101 Error_Msg_NE
996ae0b0
RK
6102 ("missing with_clause for scope of imported type&",
6103 Actual, Etype (Actual));
6104 Error_Msg_Qual_Level := 0;
6105 end if;
6106
6107 Next_Actual (Actual);
6108 end loop;
6109 end if;
6110
c63a2ad6
AC
6111 -- Before listing the possible candidates, check whether this is
6112 -- a prefix of a selected component that has been rewritten as a
6113 -- parameterless function call because there is a callable candidate
6114 -- interpretation. If there is a hidden package in the list of homonyms
6115 -- of the function name (bad programming style in any case) suggest that
6116 -- this is the intended entity.
40bf00b1
AC
6117
6118 if No (Parameter_Associations (N))
6119 and then Nkind (Parent (N)) = N_Selected_Component
6120 and then Nkind (Parent (Parent (N))) in N_Declaration
6121 and then Is_Overloaded (Nam)
6122 then
6123 declare
6124 Ent : Entity_Id;
6125
6126 begin
6127 Ent := Current_Entity (Nam);
6128 while Present (Ent) loop
6129 if Ekind (Ent) = E_Package then
6130 Error_Msg_N
6131 ("no legal interpretations as function call,!", Nam);
6132 Error_Msg_NE ("\package& is not visible", N, Ent);
c63a2ad6 6133
40bf00b1
AC
6134 Rewrite (Parent (N),
6135 New_Occurrence_Of (Any_Type, Sloc (N)));
6136 return;
6137 end if;
6138
6139 Ent := Homonym (Ent);
6140 end loop;
6141 end;
6142 end if;
6143
c63a2ad6
AC
6144 -- Analyze each candidate call again, with full error reporting for
6145 -- each.
fbf5a39b
AC
6146
6147 Error_Msg_N
6148 ("no candidate interpretations match the actuals:!", Nam);
6149 Err_Mode := All_Errors_Mode;
6150 All_Errors_Mode := True;
6151
6152 -- If this is a call to an operation of a concurrent type,
6153 -- the failed interpretations have been removed from the
6154 -- name. Recover them to provide full diagnostics.
6155
6156 if Nkind (Parent (Nam)) = N_Selected_Component then
6157 Set_Entity (Nam, Empty);
6158 New_Nam := New_Copy_Tree (Parent (Nam));
6159 Set_Is_Overloaded (New_Nam, False);
6160 Set_Is_Overloaded (Selector_Name (New_Nam), False);
6161 Set_Parent (New_Nam, Parent (Parent (Nam)));
6162 Analyze_Selected_Component (New_Nam);
6163 Get_First_Interp (Selector_Name (New_Nam), X, It);
6164 else
996ae0b0 6165 Get_First_Interp (Nam, X, It);
fbf5a39b 6166 end if;
996ae0b0 6167
fbf5a39b
AC
6168 while Present (It.Nam) loop
6169 if Etype (It.Nam) = Standard_Void_Type then
6170 Void_Interp_Seen := True;
996ae0b0 6171 end if;
fbf5a39b
AC
6172
6173 Analyze_One_Call (N, It.Nam, True, Success);
6174 Get_Next_Interp (X, It);
6175 end loop;
996ae0b0
RK
6176
6177 if Nkind (N) = N_Function_Call then
6178 Get_First_Interp (Nam, X, It);
996ae0b0 6179
a9e47028
ES
6180 if No (It.Typ)
6181 and then Ekind (Entity (Name (N))) = E_Function
6182 and then Present (Homonym (Entity (Name (N))))
6183 then
7f8c1cd3
HK
6184 -- A name may appear overloaded if it has a homonym, even if that
6185 -- homonym is non-overloadable, in which case the overload list is
6186 -- in fact empty. This specialized case deserves a special message
6187 -- if the homonym is a child package.
996ae0b0 6188
a9e47028
ES
6189 declare
6190 Nam : constant Node_Id := Name (N);
6191 H : constant Entity_Id := Homonym (Entity (Nam));
6192
6193 begin
7f8c1cd3 6194 if Ekind (H) = E_Package and then Is_Child_Unit (H) then
a9e47028
ES
6195 Error_Msg_Qual_Level := 2;
6196 Error_Msg_NE ("if an entity in package& is meant, ", Nam, H);
6197 Error_Msg_NE ("\use a fully qualified name", Nam, H);
6198 Error_Msg_Qual_Level := 0;
6199 end if;
6200 end;
6201
6202 else
6203 while Present (It.Nam) loop
6204 if Ekind_In (It.Nam, E_Function, E_Operator) then
6205 return;
6206 else
6207 Get_Next_Interp (X, It);
6208 end if;
6209 end loop;
6210
7f8c1cd3
HK
6211 -- If all interpretations are procedures, this deserves a more
6212 -- precise message. Ditto if this appears as the prefix of a
6213 -- selected component, which may be a lexical error.
a9e47028
ES
6214
6215 Error_Msg_N
6216 ("\context requires function call, found procedure name", Nam);
6217
6218 if Nkind (Parent (N)) = N_Selected_Component
6219 and then N = Prefix (Parent (N))
6220 then
6221 Error_Msg_N -- CODEFIX
6222 ("\period should probably be semicolon", Parent (N));
6223 end if;
996ae0b0 6224 end if;
fbf5a39b
AC
6225
6226 elsif Nkind (N) = N_Procedure_Call_Statement
6227 and then not Void_Interp_Seen
6228 then
7f8c1cd3 6229 Error_Msg_N ("\function name found in procedure call", Nam);
996ae0b0 6230 end if;
fbf5a39b
AC
6231
6232 All_Errors_Mode := Err_Mode;
996ae0b0
RK
6233 end Diagnose_Call;
6234
6235 ---------------------------
6236 -- Find_Arithmetic_Types --
6237 ---------------------------
6238
6239 procedure Find_Arithmetic_Types
6240 (L, R : Node_Id;
6241 Op_Id : Entity_Id;
6242 N : Node_Id)
6243 is
4c46b835
AC
6244 Index1 : Interp_Index;
6245 Index2 : Interp_Index;
6246 It1 : Interp;
6247 It2 : Interp;
996ae0b0
RK
6248
6249 procedure Check_Right_Argument (T : Entity_Id);
6250 -- Check right operand of operator
6251
4c46b835
AC
6252 --------------------------
6253 -- Check_Right_Argument --
6254 --------------------------
6255
996ae0b0
RK
6256 procedure Check_Right_Argument (T : Entity_Id) is
6257 begin
6258 if not Is_Overloaded (R) then
6259 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
6260 else
6261 Get_First_Interp (R, Index2, It2);
996ae0b0
RK
6262 while Present (It2.Typ) loop
6263 Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
6264 Get_Next_Interp (Index2, It2);
6265 end loop;
6266 end if;
6267 end Check_Right_Argument;
6268
d8221f45 6269 -- Start of processing for Find_Arithmetic_Types
996ae0b0
RK
6270
6271 begin
6272 if not Is_Overloaded (L) then
6273 Check_Right_Argument (Etype (L));
6274
6275 else
6276 Get_First_Interp (L, Index1, It1);
996ae0b0
RK
6277 while Present (It1.Typ) loop
6278 Check_Right_Argument (It1.Typ);
6279 Get_Next_Interp (Index1, It1);
6280 end loop;
6281 end if;
6282
6283 end Find_Arithmetic_Types;
6284
6285 ------------------------
6286 -- Find_Boolean_Types --
6287 ------------------------
6288
6289 procedure Find_Boolean_Types
6290 (L, R : Node_Id;
6291 Op_Id : Entity_Id;
6292 N : Node_Id)
6293 is
6294 Index : Interp_Index;
6295 It : Interp;
6296
6297 procedure Check_Numeric_Argument (T : Entity_Id);
6298 -- Special case for logical operations one of whose operands is an
6299 -- integer literal. If both are literal the result is any modular type.
6300
4c46b835
AC
6301 ----------------------------
6302 -- Check_Numeric_Argument --
6303 ----------------------------
6304
996ae0b0
RK
6305 procedure Check_Numeric_Argument (T : Entity_Id) is
6306 begin
6307 if T = Universal_Integer then
6308 Add_One_Interp (N, Op_Id, Any_Modular);
6309
6310 elsif Is_Modular_Integer_Type (T) then
6311 Add_One_Interp (N, Op_Id, T);
6312 end if;
6313 end Check_Numeric_Argument;
6314
6315 -- Start of processing for Find_Boolean_Types
6316
6317 begin
6318 if not Is_Overloaded (L) then
996ae0b0
RK
6319 if Etype (L) = Universal_Integer
6320 or else Etype (L) = Any_Modular
6321 then
6322 if not Is_Overloaded (R) then
6323 Check_Numeric_Argument (Etype (R));
6324
6325 else
6326 Get_First_Interp (R, Index, It);
996ae0b0
RK
6327 while Present (It.Typ) loop
6328 Check_Numeric_Argument (It.Typ);
996ae0b0
RK
6329 Get_Next_Interp (Index, It);
6330 end loop;
6331 end if;
6332
69e6a03e
ES
6333 -- If operands are aggregates, we must assume that they may be
6334 -- boolean arrays, and leave disambiguation for the second pass.
6335 -- If only one is an aggregate, verify that the other one has an
6336 -- interpretation as a boolean array
6337
6338 elsif Nkind (L) = N_Aggregate then
6339 if Nkind (R) = N_Aggregate then
6340 Add_One_Interp (N, Op_Id, Etype (L));
6341
6342 elsif not Is_Overloaded (R) then
6343 if Valid_Boolean_Arg (Etype (R)) then
6344 Add_One_Interp (N, Op_Id, Etype (R));
6345 end if;
6346
6347 else
6348 Get_First_Interp (R, Index, It);
6349 while Present (It.Typ) loop
6350 if Valid_Boolean_Arg (It.Typ) then
6351 Add_One_Interp (N, Op_Id, It.Typ);
6352 end if;
6353
6354 Get_Next_Interp (Index, It);
6355 end loop;
6356 end if;
6357
996ae0b0
RK
6358 elsif Valid_Boolean_Arg (Etype (L))
6359 and then Has_Compatible_Type (R, Etype (L))
6360 then
6361 Add_One_Interp (N, Op_Id, Etype (L));
6362 end if;
6363
6364 else
6365 Get_First_Interp (L, Index, It);
996ae0b0
RK
6366 while Present (It.Typ) loop
6367 if Valid_Boolean_Arg (It.Typ)
6368 and then Has_Compatible_Type (R, It.Typ)
6369 then
6370 Add_One_Interp (N, Op_Id, It.Typ);
6371 end if;
6372
6373 Get_Next_Interp (Index, It);
6374 end loop;
6375 end if;
6376 end Find_Boolean_Types;
6377
6378 ---------------------------
6379 -- Find_Comparison_Types --
6380 ---------------------------
6381
6382 procedure Find_Comparison_Types
6383 (L, R : Node_Id;
6384 Op_Id : Entity_Id;
6385 N : Node_Id)
6386 is
6387 Index : Interp_Index;
6388 It : Interp;
6389 Found : Boolean := False;
6390 I_F : Interp_Index;
6391 T_F : Entity_Id;
6392 Scop : Entity_Id := Empty;
6393
6394 procedure Try_One_Interp (T1 : Entity_Id);
6395 -- Routine to try one proposed interpretation. Note that the context
6396 -- of the operator plays no role in resolving the arguments, so that
6397 -- if there is more than one interpretation of the operands that is
6398 -- compatible with comparison, the operation is ambiguous.
6399
4c46b835
AC
6400 --------------------
6401 -- Try_One_Interp --
6402 --------------------
6403
996ae0b0
RK
6404 procedure Try_One_Interp (T1 : Entity_Id) is
6405 begin
996ae0b0
RK
6406 -- If the operator is an expanded name, then the type of the operand
6407 -- must be defined in the corresponding scope. If the type is
c468e1fb
AC
6408 -- universal, the context will impose the correct type. Note that we
6409 -- also avoid returning if we are currently within a generic instance
6410 -- due to the fact that the generic package declaration has already
6411 -- been successfully analyzed and Defined_In_Scope expects the base
6412 -- type to be defined within the instance which will never be the
6413 -- case.
996ae0b0
RK
6414
6415 if Present (Scop)
8b4230c8 6416 and then not Defined_In_Scope (T1, Scop)
c468e1fb 6417 and then not In_Instance
8b4230c8
AC
6418 and then T1 /= Universal_Integer
6419 and then T1 /= Universal_Real
6420 and then T1 /= Any_String
6421 and then T1 /= Any_Composite
996ae0b0
RK
6422 then
6423 return;
6424 end if;
6425
8b4230c8
AC
6426 if Valid_Comparison_Arg (T1) and then Has_Compatible_Type (R, T1) then
6427 if Found and then Base_Type (T1) /= Base_Type (T_F) then
996ae0b0
RK
6428 It := Disambiguate (L, I_F, Index, Any_Type);
6429
6430 if It = No_Interp then
6431 Ambiguous_Operands (N);
6432 Set_Etype (L, Any_Type);
6433 return;
6434
6435 else
6436 T_F := It.Typ;
6437 end if;
996ae0b0
RK
6438 else
6439 Found := True;
6440 T_F := T1;
6441 I_F := Index;
6442 end if;
6443
6444 Set_Etype (L, T_F);
6445 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
996ae0b0
RK
6446 end if;
6447 end Try_One_Interp;
6448
d8221f45 6449 -- Start of processing for Find_Comparison_Types
996ae0b0
RK
6450
6451 begin
fbf5a39b
AC
6452 -- If left operand is aggregate, the right operand has to
6453 -- provide a usable type for it.
6454
8b4230c8 6455 if Nkind (L) = N_Aggregate and then Nkind (R) /= N_Aggregate then
b4592168 6456 Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N);
fbf5a39b
AC
6457 return;
6458 end if;
996ae0b0
RK
6459
6460 if Nkind (N) = N_Function_Call
6461 and then Nkind (Name (N)) = N_Expanded_Name
6462 then
6463 Scop := Entity (Prefix (Name (N)));
6464
6465 -- The prefix may be a package renaming, and the subsequent test
6466 -- requires the original package.
6467
6468 if Ekind (Scop) = E_Package
6469 and then Present (Renamed_Entity (Scop))
6470 then
6471 Scop := Renamed_Entity (Scop);
6472 Set_Entity (Prefix (Name (N)), Scop);
6473 end if;
6474 end if;
6475
6476 if not Is_Overloaded (L) then
6477 Try_One_Interp (Etype (L));
6478
6479 else
6480 Get_First_Interp (L, Index, It);
996ae0b0
RK
6481 while Present (It.Typ) loop
6482 Try_One_Interp (It.Typ);
6483 Get_Next_Interp (Index, It);
6484 end loop;
6485 end if;
6486 end Find_Comparison_Types;
6487
6488 ----------------------------------------
6489 -- Find_Non_Universal_Interpretations --
6490 ----------------------------------------
6491
6492 procedure Find_Non_Universal_Interpretations
6493 (N : Node_Id;
6494 R : Node_Id;
6495 Op_Id : Entity_Id;
6496 T1 : Entity_Id)
6497 is
6498 Index : Interp_Index;
4c46b835 6499 It : Interp;
996ae0b0
RK
6500
6501 begin
8b4230c8 6502 if T1 = Universal_Integer or else T1 = Universal_Real
b9daa96e
AC
6503
6504 -- If the left operand of an equality operator is null, the visibility
6505 -- of the operator must be determined from the interpretation of the
6506 -- right operand. This processing must be done for Any_Access, which
6507 -- is the internal representation of the type of the literal null.
6508
be4c5193 6509 or else T1 = Any_Access
996ae0b0
RK
6510 then
6511 if not Is_Overloaded (R) then
8b4230c8 6512 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
996ae0b0
RK
6513 else
6514 Get_First_Interp (R, Index, It);
996ae0b0
RK
6515 while Present (It.Typ) loop
6516 if Covers (It.Typ, T1) then
6517 Add_One_Interp
6518 (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
6519 end if;
6520
6521 Get_Next_Interp (Index, It);
6522 end loop;
6523 end if;
6524 else
6525 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
6526 end if;
6527 end Find_Non_Universal_Interpretations;
6528
6529 ------------------------------
6530 -- Find_Concatenation_Types --
6531 ------------------------------
6532
6533 procedure Find_Concatenation_Types
6534 (L, R : Node_Id;
6535 Op_Id : Entity_Id;
6536 N : Node_Id)
6537 is
e201023c
PMR
6538 Is_String : constant Boolean := Nkind (L) = N_String_Literal
6539 or else
6540 Nkind (R) = N_String_Literal;
6541 Op_Type : constant Entity_Id := Etype (Op_Id);
996ae0b0
RK
6542
6543 begin
6544 if Is_Array_Type (Op_Type)
e201023c
PMR
6545
6546 -- Small but very effective optimization: if at least one operand is a
6547 -- string literal, then the type of the operator must be either array
6548 -- of characters or array of strings.
6549
6550 and then (not Is_String
6551 or else
6552 Is_Character_Type (Component_Type (Op_Type))
6553 or else
6554 Is_String_Type (Component_Type (Op_Type)))
6555
996ae0b0
RK
6556 and then not Is_Limited_Type (Op_Type)
6557
6558 and then (Has_Compatible_Type (L, Op_Type)
6559 or else
6560 Has_Compatible_Type (L, Component_Type (Op_Type)))
6561
6562 and then (Has_Compatible_Type (R, Op_Type)
6563 or else
6564 Has_Compatible_Type (R, Component_Type (Op_Type)))
6565 then
6566 Add_One_Interp (N, Op_Id, Op_Type);
6567 end if;
6568 end Find_Concatenation_Types;
6569
6570 -------------------------
6571 -- Find_Equality_Types --
6572 -------------------------
6573
6574 procedure Find_Equality_Types
6575 (L, R : Node_Id;
6576 Op_Id : Entity_Id;
6577 N : Node_Id)
6578 is
6579 Index : Interp_Index;
6580 It : Interp;
6581 Found : Boolean := False;
6582 I_F : Interp_Index;
6583 T_F : Entity_Id;
6584 Scop : Entity_Id := Empty;
6585
6586 procedure Try_One_Interp (T1 : Entity_Id);
a8e65aa5
AC
6587 -- The context of the equality operator plays no role in resolving the
6588 -- arguments, so that if there is more than one interpretation of the
6589 -- operands that is compatible with equality, the construct is ambiguous
6590 -- and an error can be emitted now, after trying to disambiguate, i.e.
6591 -- applying preference rules.
996ae0b0 6592
4c46b835
AC
6593 --------------------
6594 -- Try_One_Interp --
6595 --------------------
6596
996ae0b0 6597 procedure Try_One_Interp (T1 : Entity_Id) is
851e9f19 6598 Bas : Entity_Id;
a8e65aa5 6599
996ae0b0 6600 begin
851e9f19
PMR
6601 -- Perform a sanity check in case of previous errors
6602
6603 if No (T1) then
6604 return;
6605 end if;
6606
6607 Bas := Base_Type (T1);
6608
996ae0b0
RK
6609 -- If the operator is an expanded name, then the type of the operand
6610 -- must be defined in the corresponding scope. If the type is
6611 -- universal, the context will impose the correct type. An anonymous
6612 -- type for a 'Access reference is also universal in this sense, as
6613 -- the actual type is obtained from context.
8b4230c8 6614
fe45e59e
ES
6615 -- In Ada 2005, the equality operator for anonymous access types
6616 -- is declared in Standard, and preference rules apply to it.
996ae0b0 6617
fe45e59e 6618 if Present (Scop) then
c468e1fb
AC
6619
6620 -- Note that we avoid returning if we are currently within a
6621 -- generic instance due to the fact that the generic package
6622 -- declaration has already been successfully analyzed and
4ac62786
AC
6623 -- Defined_In_Scope expects the base type to be defined within
6624 -- the instance which will never be the case.
c468e1fb 6625
fe45e59e 6626 if Defined_In_Scope (T1, Scop)
c468e1fb 6627 or else In_Instance
fe45e59e
ES
6628 or else T1 = Universal_Integer
6629 or else T1 = Universal_Real
6630 or else T1 = Any_Access
6631 or else T1 = Any_String
6632 or else T1 = Any_Composite
6633 or else (Ekind (T1) = E_Access_Subprogram_Type
a8e65aa5 6634 and then not Comes_From_Source (T1))
fe45e59e
ES
6635 then
6636 null;
6637
6638 elsif Ekind (T1) = E_Anonymous_Access_Type
6639 and then Scop = Standard_Standard
6640 then
6641 null;
6642
6643 else
6644 -- The scope does not contain an operator for the type
6645
6646 return;
6647 end if;
a8e65aa5 6648
9aa04cc7
AC
6649 -- If we have infix notation, the operator must be usable. Within
6650 -- an instance, if the type is already established we know it is
6651 -- correct. If an operand is universal it is compatible with any
6652 -- numeric type.
31af8899 6653
a8e65aa5
AC
6654 elsif In_Open_Scopes (Scope (Bas))
6655 or else Is_Potentially_Use_Visible (Bas)
6656 or else In_Use (Bas)
9aa04cc7 6657 or else (In_Use (Scope (Bas)) and then not Is_Hidden (Bas))
a54ffd6c
AC
6658
6659 -- In an instance, the type may have been immediately visible.
6660 -- Either the types are compatible, or one operand is universal
6661 -- (numeric or null).
6662
c5b4738f
AC
6663 or else
6664 ((In_Instance or else In_Inlined_Body)
6665 and then
6666 (First_Subtype (T1) = First_Subtype (Etype (R))
6667 or else Nkind (R) = N_Null
6668 or else
6669 (Is_Numeric_Type (T1)
6670 and then Is_Universal_Numeric_Type (Etype (R)))))
a54ffd6c
AC
6671
6672 -- In Ada 2005, the equality on anonymous access types is declared
6673 -- in Standard, and is always visible.
6674
a8e65aa5
AC
6675 or else Ekind (T1) = E_Anonymous_Access_Type
6676 then
6677 null;
6678
6679 else
308e6f3a 6680 -- Save candidate type for subsequent error message, if any
a8e65aa5
AC
6681
6682 if not Is_Limited_Type (T1) then
6683 Candidate_Type := T1;
6684 end if;
6685
6686 return;
996ae0b0
RK
6687 end if;
6688
0ab80019
AC
6689 -- Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
6690 -- Do not allow anonymous access types in equality operators.
6e937c1c 6691
0791fbe9 6692 if Ada_Version < Ada_2005
6e937c1c
AC
6693 and then Ekind (T1) = E_Anonymous_Access_Type
6694 then
6695 return;
6696 end if;
6697
c92e8586
AC
6698 -- If the right operand has a type compatible with T1, check for an
6699 -- acceptable interpretation, unless T1 is limited (no predefined
6700 -- equality available), or this is use of a "/=" for a tagged type.
8b4230c8
AC
6701 -- In the latter case, possible interpretations of equality need
6702 -- to be considered, we don't want the default inequality declared
6703 -- in Standard to be chosen, and the "/=" will be rewritten as a
c92e8586 6704 -- negation of "=" (see the end of Analyze_Equality_Op). This ensures
026c3cfd 6705 -- that rewriting happens during analysis rather than being
c92e8586
AC
6706 -- delayed until expansion (this is needed for ASIS, which only sees
6707 -- the unexpanded tree). Note that if the node is N_Op_Ne, but Op_Id
6708 -- is Name_Op_Eq then we still proceed with the interpretation,
6709 -- because that indicates the potential rewriting case where the
6710 -- interpretation to consider is actually "=" and the node may be
6711 -- about to be rewritten by Analyze_Equality_Op.
6712
996ae0b0 6713 if T1 /= Standard_Void_Type
996ae0b0 6714 and then Has_Compatible_Type (R, T1)
c92e8586 6715
9b62eb32
AC
6716 and then
6717 ((not Is_Limited_Type (T1)
6718 and then not Is_Limited_Composite (T1))
6719
6720 or else
2fcc44fa 6721 (Is_Array_Type (T1)
9b62eb32
AC
6722 and then not Is_Limited_Type (Component_Type (T1))
6723 and then Available_Full_View_Of_Component (T1)))
c92e8586
AC
6724
6725 and then
6726 (Nkind (N) /= N_Op_Ne
6727 or else not Is_Tagged_Type (T1)
6728 or else Chars (Op_Id) = Name_Op_Eq)
996ae0b0
RK
6729 then
6730 if Found
6731 and then Base_Type (T1) /= Base_Type (T_F)
6732 then
6733 It := Disambiguate (L, I_F, Index, Any_Type);
6734
6735 if It = No_Interp then
6736 Ambiguous_Operands (N);
6737 Set_Etype (L, Any_Type);
6738 return;
6739
6740 else
6741 T_F := It.Typ;
6742 end if;
6743
6744 else
6745 Found := True;
6746 T_F := T1;
6747 I_F := Index;
6748 end if;
6749
6750 if not Analyzed (L) then
6751 Set_Etype (L, T_F);
6752 end if;
6753
6754 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
6755
6e73e3ab 6756 -- Case of operator was not visible, Etype still set to Any_Type
996ae0b0 6757
6e73e3ab 6758 if Etype (N) = Any_Type then
996ae0b0
RK
6759 Found := False;
6760 end if;
fe45e59e
ES
6761
6762 elsif Scop = Standard_Standard
6763 and then Ekind (T1) = E_Anonymous_Access_Type
6764 then
6765 Found := True;
996ae0b0
RK
6766 end if;
6767 end Try_One_Interp;
6768
6769 -- Start of processing for Find_Equality_Types
6770
6771 begin
fbf5a39b
AC
6772 -- If left operand is aggregate, the right operand has to
6773 -- provide a usable type for it.
6774
6775 if Nkind (L) = N_Aggregate
6776 and then Nkind (R) /= N_Aggregate
6777 then
b4592168 6778 Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N);
fbf5a39b
AC
6779 return;
6780 end if;
996ae0b0
RK
6781
6782 if Nkind (N) = N_Function_Call
6783 and then Nkind (Name (N)) = N_Expanded_Name
6784 then
6785 Scop := Entity (Prefix (Name (N)));
6786
6787 -- The prefix may be a package renaming, and the subsequent test
6788 -- requires the original package.
6789
6790 if Ekind (Scop) = E_Package
6791 and then Present (Renamed_Entity (Scop))
6792 then
6793 Scop := Renamed_Entity (Scop);
6794 Set_Entity (Prefix (Name (N)), Scop);
6795 end if;
6796 end if;
6797
6798 if not Is_Overloaded (L) then
6799 Try_One_Interp (Etype (L));
996ae0b0 6800
4c46b835 6801 else
996ae0b0 6802 Get_First_Interp (L, Index, It);
996ae0b0
RK
6803 while Present (It.Typ) loop
6804 Try_One_Interp (It.Typ);
6805 Get_Next_Interp (Index, It);
6806 end loop;
6807 end if;
6808 end Find_Equality_Types;
6809
6810 -------------------------
6811 -- Find_Negation_Types --
6812 -------------------------
6813
6814 procedure Find_Negation_Types
6815 (R : Node_Id;
6816 Op_Id : Entity_Id;
6817 N : Node_Id)
6818 is
6819 Index : Interp_Index;
6820 It : Interp;
6821
6822 begin
6823 if not Is_Overloaded (R) then
996ae0b0
RK
6824 if Etype (R) = Universal_Integer then
6825 Add_One_Interp (N, Op_Id, Any_Modular);
996ae0b0
RK
6826 elsif Valid_Boolean_Arg (Etype (R)) then
6827 Add_One_Interp (N, Op_Id, Etype (R));
6828 end if;
6829
6830 else
6831 Get_First_Interp (R, Index, It);
996ae0b0
RK
6832 while Present (It.Typ) loop
6833 if Valid_Boolean_Arg (It.Typ) then
6834 Add_One_Interp (N, Op_Id, It.Typ);
6835 end if;
6836
6837 Get_Next_Interp (Index, It);
6838 end loop;
6839 end if;
6840 end Find_Negation_Types;
6841
d469eabe
HK
6842 ------------------------------
6843 -- Find_Primitive_Operation --
6844 ------------------------------
6845
6846 function Find_Primitive_Operation (N : Node_Id) return Boolean is
6847 Obj : constant Node_Id := Prefix (N);
6848 Op : constant Node_Id := Selector_Name (N);
6849
6850 Prim : Elmt_Id;
6851 Prims : Elist_Id;
6852 Typ : Entity_Id;
6853
6854 begin
6855 Set_Etype (Op, Any_Type);
6856
6857 if Is_Access_Type (Etype (Obj)) then
6858 Typ := Designated_Type (Etype (Obj));
6859 else
6860 Typ := Etype (Obj);
6861 end if;
6862
6863 if Is_Class_Wide_Type (Typ) then
6864 Typ := Root_Type (Typ);
6865 end if;
6866
6867 Prims := Primitive_Operations (Typ);
6868
6869 Prim := First_Elmt (Prims);
6870 while Present (Prim) loop
6871 if Chars (Node (Prim)) = Chars (Op) then
6872 Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
6873 Set_Etype (N, Etype (Node (Prim)));
6874 end if;
6875
6876 Next_Elmt (Prim);
6877 end loop;
6878
6879 -- Now look for class-wide operations of the type or any of its
6880 -- ancestors by iterating over the homonyms of the selector.
6881
6882 declare
6883 Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
6884 Hom : Entity_Id;
6885
6886 begin
6887 Hom := Current_Entity (Op);
6888 while Present (Hom) loop
6889 if (Ekind (Hom) = E_Procedure
6890 or else
6891 Ekind (Hom) = E_Function)
6892 and then Scope (Hom) = Scope (Typ)
6893 and then Present (First_Formal (Hom))
6894 and then
6895 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
6896 or else
6897 (Is_Access_Type (Etype (First_Formal (Hom)))
8b4230c8
AC
6898 and then
6899 Ekind (Etype (First_Formal (Hom))) =
6900 E_Anonymous_Access_Type
6901 and then
6902 Base_Type
6903 (Designated_Type (Etype (First_Formal (Hom)))) =
d469eabe
HK
6904 Cls_Type))
6905 then
6906 Add_One_Interp (Op, Hom, Etype (Hom));
6907 Set_Etype (N, Etype (Hom));
6908 end if;
6909
6910 Hom := Homonym (Hom);
6911 end loop;
6912 end;
6913
6914 return Etype (Op) /= Any_Type;
6915 end Find_Primitive_Operation;
6916
996ae0b0
RK
6917 ----------------------
6918 -- Find_Unary_Types --
6919 ----------------------
6920
6921 procedure Find_Unary_Types
6922 (R : Node_Id;
6923 Op_Id : Entity_Id;
6924 N : Node_Id)
6925 is
6926 Index : Interp_Index;
6927 It : Interp;
6928
6929 begin
6930 if not Is_Overloaded (R) then
6931 if Is_Numeric_Type (Etype (R)) then
65f7ed64
AC
6932
6933 -- In an instance a generic actual may be a numeric type even if
6934 -- the formal in the generic unit was not. In that case, the
6935 -- predefined operator was not a possible interpretation in the
2e70d415
AC
6936 -- generic, and cannot be one in the instance, unless the operator
6937 -- is an actual of an instance.
65f7ed64
AC
6938
6939 if In_Instance
6940 and then
6941 not Is_Numeric_Type (Corresponding_Generic_Type (Etype (R)))
6942 then
6943 null;
6944 else
6945 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
6946 end if;
996ae0b0
RK
6947 end if;
6948
6949 else
6950 Get_First_Interp (R, Index, It);
996ae0b0
RK
6951 while Present (It.Typ) loop
6952 if Is_Numeric_Type (It.Typ) then
65f7ed64
AC
6953 if In_Instance
6954 and then
6955 not Is_Numeric_Type
6956 (Corresponding_Generic_Type (Etype (It.Typ)))
6957 then
6958 null;
6959
6960 else
6961 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
6962 end if;
996ae0b0
RK
6963 end if;
6964
6965 Get_Next_Interp (Index, It);
6966 end loop;
6967 end if;
6968 end Find_Unary_Types;
6969
996ae0b0
RK
6970 ------------------
6971 -- Junk_Operand --
6972 ------------------
6973
6974 function Junk_Operand (N : Node_Id) return Boolean is
6975 Enode : Node_Id;
6976
6977 begin
6978 if Error_Posted (N) then
6979 return False;
6980 end if;
6981
6982 -- Get entity to be tested
6983
6984 if Is_Entity_Name (N)
6985 and then Present (Entity (N))
6986 then
6987 Enode := N;
6988
6989 -- An odd case, a procedure name gets converted to a very peculiar
6990 -- function call, and here is where we detect this happening.
6991
6992 elsif Nkind (N) = N_Function_Call
6993 and then Is_Entity_Name (Name (N))
6994 and then Present (Entity (Name (N)))
6995 then
6996 Enode := Name (N);
6997
6998 -- Another odd case, there are at least some cases of selected
6999 -- components where the selected component is not marked as having
7000 -- an entity, even though the selector does have an entity
7001
7002 elsif Nkind (N) = N_Selected_Component
7003 and then Present (Entity (Selector_Name (N)))
7004 then
7005 Enode := Selector_Name (N);
7006
7007 else
7008 return False;
7009 end if;
7010
9de61fcb 7011 -- Now test the entity we got to see if it is a bad case
996ae0b0
RK
7012
7013 case Ekind (Entity (Enode)) is
996ae0b0
RK
7014 when E_Package =>
7015 Error_Msg_N
7016 ("package name cannot be used as operand", Enode);
7017
7018 when Generic_Unit_Kind =>
7019 Error_Msg_N
7020 ("generic unit name cannot be used as operand", Enode);
7021
7022 when Type_Kind =>
7023 Error_Msg_N
7024 ("subtype name cannot be used as operand", Enode);
7025
7026 when Entry_Kind =>
7027 Error_Msg_N
7028 ("entry name cannot be used as operand", Enode);
7029
7030 when E_Procedure =>
7031 Error_Msg_N
7032 ("procedure name cannot be used as operand", Enode);
7033
7034 when E_Exception =>
7035 Error_Msg_N
7036 ("exception name cannot be used as operand", Enode);
7037
d8f43ee6
HK
7038 when E_Block
7039 | E_Label
7040 | E_Loop
7041 =>
996ae0b0
RK
7042 Error_Msg_N
7043 ("label name cannot be used as operand", Enode);
7044
7045 when others =>
7046 return False;
996ae0b0
RK
7047 end case;
7048
7049 return True;
7050 end Junk_Operand;
7051
7052 --------------------
7053 -- Operator_Check --
7054 --------------------
7055
7056 procedure Operator_Check (N : Node_Id) is
7057 begin
30c20106
AC
7058 Remove_Abstract_Operations (N);
7059
996ae0b0
RK
7060 -- Test for case of no interpretation found for operator
7061
7062 if Etype (N) = Any_Type then
7063 declare
b67a385c
ES
7064 L : Node_Id;
7065 R : Node_Id;
7066 Op_Id : Entity_Id := Empty;
996ae0b0
RK
7067
7068 begin
7069 R := Right_Opnd (N);
7070
7071 if Nkind (N) in N_Binary_Op then
7072 L := Left_Opnd (N);
7073 else
7074 L := Empty;
7075 end if;
7076
7077 -- If either operand has no type, then don't complain further,
9de61fcb 7078 -- since this simply means that we have a propagated error.
996ae0b0
RK
7079
7080 if R = Error
7081 or else Etype (R) = Any_Type
7082 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
7083 then
7610fee8
AC
7084 -- For the rather unusual case where one of the operands is
7085 -- a Raise_Expression, whose initial type is Any_Type, use
7086 -- the type of the other operand.
7087
7088 if Nkind (L) = N_Raise_Expression then
7089 Set_Etype (L, Etype (R));
7090 Set_Etype (N, Etype (R));
7091
7092 elsif Nkind (R) = N_Raise_Expression then
7093 Set_Etype (R, Etype (L));
7094 Set_Etype (N, Etype (L));
7095 end if;
7096
996ae0b0
RK
7097 return;
7098
4c46b835
AC
7099 -- We explicitly check for the case of concatenation of component
7100 -- with component to avoid reporting spurious matching array types
7101 -- that might happen to be lurking in distant packages (such as
7102 -- run-time packages). This also prevents inconsistencies in the
7103 -- messages for certain ACVC B tests, which can vary depending on
7104 -- types declared in run-time interfaces. Another improvement when
7105 -- aggregates are present is to look for a well-typed operand.
996ae0b0
RK
7106
7107 elsif Present (Candidate_Type)
7108 and then (Nkind (N) /= N_Op_Concat
7109 or else Is_Array_Type (Etype (L))
7110 or else Is_Array_Type (Etype (R)))
7111 then
996ae0b0
RK
7112 if Nkind (N) = N_Op_Concat then
7113 if Etype (L) /= Any_Composite
7114 and then Is_Array_Type (Etype (L))
7115 then
7116 Candidate_Type := Etype (L);
7117
7118 elsif Etype (R) /= Any_Composite
7119 and then Is_Array_Type (Etype (R))
7120 then
7121 Candidate_Type := Etype (R);
7122 end if;
7123 end if;
7124
19d846a0 7125 Error_Msg_NE -- CODEFIX
996ae0b0
RK
7126 ("operator for} is not directly visible!",
7127 N, First_Subtype (Candidate_Type));
4561baf7
ES
7128
7129 declare
7130 U : constant Node_Id :=
7131 Cunit (Get_Source_Unit (Candidate_Type));
4561baf7
ES
7132 begin
7133 if Unit_Is_Visible (U) then
7134 Error_Msg_N -- CODEFIX
7135 ("use clause would make operation legal!", N);
4561baf7
ES
7136 else
7137 Error_Msg_NE -- CODEFIX
7138 ("add with_clause and use_clause for&!",
8b4230c8 7139 N, Defining_Entity (Unit (U)));
4561baf7
ES
7140 end if;
7141 end;
996ae0b0
RK
7142 return;
7143
7144 -- If either operand is a junk operand (e.g. package name), then
7145 -- post appropriate error messages, but do not complain further.
7146
0e0eecec
ES
7147 -- Note that the use of OR in this test instead of OR ELSE is
7148 -- quite deliberate, we may as well check both operands in the
7149 -- binary operator case.
996ae0b0
RK
7150
7151 elsif Junk_Operand (R)
9559eccf
AC
7152 or -- really mean OR here and not OR ELSE, see above
7153 (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
996ae0b0
RK
7154 then
7155 return;
7156
7157 -- If we have a logical operator, one of whose operands is
0e0eecec
ES
7158 -- Boolean, then we know that the other operand cannot resolve to
7159 -- Boolean (since we got no interpretations), but in that case we
7160 -- pretty much know that the other operand should be Boolean, so
070d862d 7161 -- resolve it that way (generating an error).
996ae0b0 7162
d469eabe 7163 elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then
996ae0b0
RK
7164 if Etype (L) = Standard_Boolean then
7165 Resolve (R, Standard_Boolean);
7166 return;
7167 elsif Etype (R) = Standard_Boolean then
7168 Resolve (L, Standard_Boolean);
7169 return;
7170 end if;
7171
7172 -- For an arithmetic operator or comparison operator, if one
7173 -- of the operands is numeric, then we know the other operand
7174 -- is not the same numeric type. If it is a non-numeric type,
7175 -- then probably it is intended to match the other operand.
7176
d469eabe
HK
7177 elsif Nkind_In (N, N_Op_Add,
7178 N_Op_Divide,
7179 N_Op_Ge,
7180 N_Op_Gt,
7181 N_Op_Le)
7182 or else
7183 Nkind_In (N, N_Op_Lt,
7184 N_Op_Mod,
7185 N_Op_Multiply,
7186 N_Op_Rem,
7187 N_Op_Subtract)
996ae0b0 7188 then
7dbd3de9
RD
7189 -- If Allow_Integer_Address is active, check whether the
7190 -- operation becomes legal after converting an operand.
7191
996ae0b0
RK
7192 if Is_Numeric_Type (Etype (L))
7193 and then not Is_Numeric_Type (Etype (R))
7194 then
7dbd3de9
RD
7195 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
7196 Rewrite (R,
7197 Unchecked_Convert_To (Etype (L), Relocate_Node (R)));
7dbd3de9 7198
1e3689bd
AC
7199 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7200 Analyze_Comparison_Op (N);
7201 else
7202 Analyze_Arithmetic_Op (N);
7203 end if;
7dbd3de9
RD
7204 else
7205 Resolve (R, Etype (L));
7206 end if;
1e3689bd 7207
996ae0b0
RK
7208 return;
7209
7210 elsif Is_Numeric_Type (Etype (R))
7211 and then not Is_Numeric_Type (Etype (L))
7212 then
7dbd3de9
RD
7213 if Address_Integer_Convert_OK (Etype (L), Etype (R)) then
7214 Rewrite (L,
7215 Unchecked_Convert_To (Etype (R), Relocate_Node (L)));
1e3689bd
AC
7216
7217 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7218 Analyze_Comparison_Op (N);
7219 else
7220 Analyze_Arithmetic_Op (N);
7221 end if;
7222
9559eccf 7223 return;
7dbd3de9
RD
7224
7225 else
7226 Resolve (L, Etype (R));
7227 end if;
9559eccf 7228
996ae0b0 7229 return;
9559eccf
AC
7230
7231 elsif Allow_Integer_Address
d9d25d04
AC
7232 and then Is_Descendant_Of_Address (Etype (L))
7233 and then Is_Descendant_Of_Address (Etype (R))
9559eccf
AC
7234 and then not Error_Posted (N)
7235 then
7236 declare
7237 Addr_Type : constant Entity_Id := Etype (L);
7238
7239 begin
7240 Rewrite (L,
7241 Unchecked_Convert_To (
7242 Standard_Integer, Relocate_Node (L)));
7243 Rewrite (R,
7244 Unchecked_Convert_To (
7245 Standard_Integer, Relocate_Node (R)));
1e3689bd
AC
7246
7247 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7248 Analyze_Comparison_Op (N);
7249 else
7250 Analyze_Arithmetic_Op (N);
7251 end if;
9559eccf
AC
7252
7253 -- If this is an operand in an enclosing arithmetic
7254 -- operation, Convert the result as an address so that
7255 -- arithmetic folding of address can continue.
7256
7257 if Nkind (Parent (N)) in N_Op then
7258 Rewrite (N,
7259 Unchecked_Convert_To (Addr_Type, Relocate_Node (N)));
7260 end if;
7261
7262 return;
7263 end;
a8a42b93
AC
7264
7265 -- Under relaxed RM semantics silently replace occurrences of
7266 -- null by System.Address_Null.
7267
7268 elsif Null_To_Null_Address_Convert_OK (N) then
7269 Replace_Null_By_Null_Address (N);
7270
7271 if Nkind_In (N, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt) then
7272 Analyze_Comparison_Op (N);
7273 else
7274 Analyze_Arithmetic_Op (N);
7275 end if;
7276
7277 return;
996ae0b0
RK
7278 end if;
7279
7280 -- Comparisons on A'Access are common enough to deserve a
7281 -- special message.
7282
d469eabe 7283 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne)
996ae0b0
RK
7284 and then Ekind (Etype (L)) = E_Access_Attribute_Type
7285 and then Ekind (Etype (R)) = E_Access_Attribute_Type
7286 then
7287 Error_Msg_N
7288 ("two access attributes cannot be compared directly", N);
7289 Error_Msg_N
aab883ec 7290 ("\use qualified expression for one of the operands",
996ae0b0
RK
7291 N);
7292 return;
7293
7294 -- Another one for C programmers
7295
7296 elsif Nkind (N) = N_Op_Concat
7297 and then Valid_Boolean_Arg (Etype (L))
7298 and then Valid_Boolean_Arg (Etype (R))
7299 then
7300 Error_Msg_N ("invalid operands for concatenation", N);
4e7a4f6e
AC
7301 Error_Msg_N -- CODEFIX
7302 ("\maybe AND was meant", N);
996ae0b0
RK
7303 return;
7304
7305 -- A special case for comparison of access parameter with null
7306
7307 elsif Nkind (N) = N_Op_Eq
7308 and then Is_Entity_Name (L)
7309 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
7310 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
7311 N_Access_Definition
7312 and then Nkind (R) = N_Null
7313 then
7314 Error_Msg_N ("access parameter is not allowed to be null", L);
7315 Error_Msg_N ("\(call would raise Constraint_Error)", L);
7316 return;
61bee0e3
AC
7317
7318 -- Another special case for exponentiation, where the right
7319 -- operand must be Natural, independently of the base.
7320
7321 elsif Nkind (N) = N_Op_Expon
7322 and then Is_Numeric_Type (Etype (L))
7323 and then not Is_Overloaded (R)
7324 and then
7325 First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
7326 and then Base_Type (Etype (R)) /= Universal_Integer
7327 then
dec6faf1 7328 if Ada_Version >= Ada_2012
15954beb 7329 and then Has_Dimension_System (Etype (L))
dec6faf1
AC
7330 then
7331 Error_Msg_NE
54c04d6c 7332 ("exponent for dimensioned type must be a rational" &
dec6faf1
AC
7333 ", found}", R, Etype (R));
7334 else
7335 Error_Msg_NE
7336 ("exponent must be of type Natural, found}", R, Etype (R));
7337 end if;
54c04d6c 7338
61bee0e3 7339 return;
11261647
AC
7340
7341 elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
7342 if Address_Integer_Convert_OK (Etype (R), Etype (L)) then
7343 Rewrite (R,
7344 Unchecked_Convert_To (Etype (L), Relocate_Node (R)));
7345 Analyze_Equality_Op (N);
7346 return;
a8a42b93
AC
7347
7348 -- Under relaxed RM semantics silently replace occurrences of
7349 -- null by System.Address_Null.
7350
7351 elsif Null_To_Null_Address_Convert_OK (N) then
7352 Replace_Null_By_Null_Address (N);
7353 Analyze_Equality_Op (N);
7354 return;
11261647 7355 end if;
996ae0b0
RK
7356 end if;
7357
0e0eecec
ES
7358 -- If we fall through then just give general message. Note that in
7359 -- the following messages, if the operand is overloaded we choose
7360 -- an arbitrary type to complain about, but that is probably more
7361 -- useful than not giving a type at all.
996ae0b0
RK
7362
7363 if Nkind (N) in N_Unary_Op then
7364 Error_Msg_Node_2 := Etype (R);
7365 Error_Msg_N ("operator& not defined for}", N);
7366 return;
7367
7368 else
fbf5a39b
AC
7369 if Nkind (N) in N_Binary_Op then
7370 if not Is_Overloaded (L)
7371 and then not Is_Overloaded (R)
7372 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
7373 then
7ffd9312 7374 Error_Msg_Node_2 := First_Subtype (Etype (R));
fbf5a39b 7375 Error_Msg_N ("there is no applicable operator& for}", N);
996ae0b0 7376
fbf5a39b 7377 else
b67a385c
ES
7378 -- Another attempt to find a fix: one of the candidate
7379 -- interpretations may not be use-visible. This has
7380 -- already been checked for predefined operators, so
7381 -- we examine only user-defined functions.
7382
7383 Op_Id := Get_Name_Entity_Id (Chars (N));
7384
7385 while Present (Op_Id) loop
7386 if Ekind (Op_Id) /= E_Operator
7387 and then Is_Overloadable (Op_Id)
7388 then
7389 if not Is_Immediately_Visible (Op_Id)
7390 and then not In_Use (Scope (Op_Id))
aab883ec 7391 and then not Is_Abstract_Subprogram (Op_Id)
b67a385c
ES
7392 and then not Is_Hidden (Op_Id)
7393 and then Ekind (Scope (Op_Id)) = E_Package
7394 and then
7395 Has_Compatible_Type
7396 (L, Etype (First_Formal (Op_Id)))
7397 and then Present
7398 (Next_Formal (First_Formal (Op_Id)))
7399 and then
7400 Has_Compatible_Type
7401 (R,
7402 Etype (Next_Formal (First_Formal (Op_Id))))
7403 then
ed2233dc 7404 Error_Msg_N
1d0b1439 7405 ("no legal interpretation for operator&", N);
ed2233dc 7406 Error_Msg_NE
b67a385c 7407 ("\use clause on& would make operation legal",
8b4230c8 7408 N, Scope (Op_Id));
b67a385c
ES
7409 exit;
7410 end if;
7411 end if;
fbf5a39b 7412
b67a385c
ES
7413 Op_Id := Homonym (Op_Id);
7414 end loop;
7415
7416 if No (Op_Id) then
7417 Error_Msg_N ("invalid operand types for operator&", N);
7418
7419 if Nkind (N) /= N_Op_Concat then
7420 Error_Msg_NE ("\left operand has}!", N, Etype (L));
7421 Error_Msg_NE ("\right operand has}!", N, Etype (R));
2e70d415 7422
1d0b1439
YM
7423 -- For multiplication and division operators with
7424 -- a fixed-point operand and an integer operand,
7425 -- indicate that the integer operand should be of
7426 -- type Integer.
7427
7428 if Nkind_In (N, N_Op_Multiply, N_Op_Divide)
7429 and then Is_Fixed_Point_Type (Etype (L))
7430 and then Is_Integer_Type (Etype (R))
7431 then
6578a6bf
HK
7432 Error_Msg_N
7433 ("\convert right operand to `Integer`", N);
1d0b1439
YM
7434
7435 elsif Nkind (N) = N_Op_Multiply
7436 and then Is_Fixed_Point_Type (Etype (R))
7437 and then Is_Integer_Type (Etype (L))
7438 then
6578a6bf
HK
7439 Error_Msg_N
7440 ("\convert left operand to `Integer`", N);
1d0b1439
YM
7441 end if;
7442
2e70d415
AC
7443 -- For concatenation operators it is more difficult to
7444 -- determine which is the wrong operand. It is worth
7445 -- flagging explicitly an access type, for those who
7446 -- might think that a dereference happens here.
7447
7448 elsif Is_Access_Type (Etype (L)) then
7449 Error_Msg_N ("\left operand is access type", N);
7450
7451 elsif Is_Access_Type (Etype (R)) then
7452 Error_Msg_N ("\right operand is access type", N);
b67a385c 7453 end if;
fbf5a39b
AC
7454 end if;
7455 end if;
996ae0b0
RK
7456 end if;
7457 end if;
7458 end;
7459 end if;
7460 end Operator_Check;
7461
6e73e3ab
AC
7462 -----------------------------------------
7463 -- Process_Implicit_Dereference_Prefix --
7464 -----------------------------------------
7465
d469eabe 7466 function Process_Implicit_Dereference_Prefix
da709d08 7467 (E : Entity_Id;
d469eabe 7468 P : Entity_Id) return Entity_Id
6e73e3ab
AC
7469 is
7470 Ref : Node_Id;
d469eabe 7471 Typ : constant Entity_Id := Designated_Type (Etype (P));
da709d08 7472
6e73e3ab 7473 begin
1a8fae99
ES
7474 if Present (E)
7475 and then (Operating_Mode = Check_Semantics or else not Expander_Active)
7476 then
8b4230c8
AC
7477 -- We create a dummy reference to E to ensure that the reference is
7478 -- not considered as part of an assignment (an implicit dereference
7479 -- can never assign to its prefix). The Comes_From_Source attribute
7480 -- needs to be propagated for accurate warnings.
6e73e3ab 7481
e4494292 7482 Ref := New_Occurrence_Of (E, Sloc (P));
6e73e3ab
AC
7483 Set_Comes_From_Source (Ref, Comes_From_Source (P));
7484 Generate_Reference (E, Ref);
7485 end if;
d469eabe 7486
8b4230c8
AC
7487 -- An implicit dereference is a legal occurrence of an incomplete type
7488 -- imported through a limited_with clause, if the full view is visible.
d469eabe 7489
7b56a91b
AC
7490 if From_Limited_With (Typ)
7491 and then not From_Limited_With (Scope (Typ))
d469eabe
HK
7492 and then
7493 (Is_Immediately_Visible (Scope (Typ))
7494 or else
7495 (Is_Child_Unit (Scope (Typ))
8398e82e 7496 and then Is_Visible_Lib_Unit (Scope (Typ))))
d469eabe
HK
7497 then
7498 return Available_View (Typ);
7499 else
7500 return Typ;
7501 end if;
6e73e3ab
AC
7502 end Process_Implicit_Dereference_Prefix;
7503
30c20106
AC
7504 --------------------------------
7505 -- Remove_Abstract_Operations --
7506 --------------------------------
7507
7508 procedure Remove_Abstract_Operations (N : Node_Id) is
e80f0cb0 7509 Abstract_Op : Entity_Id := Empty;
d9d25d04 7510 Address_Descendant : Boolean := False;
e80f0cb0
RD
7511 I : Interp_Index;
7512 It : Interp;
30c20106 7513
0e0eecec
ES
7514 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
7515 -- activate this if either extensions are enabled, or if the abstract
7516 -- operation in question comes from a predefined file. This latter test
7517 -- allows us to use abstract to make operations invisible to users. In
7518 -- particular, if type Address is non-private and abstract subprograms
7519 -- are used to hide its operators, they will be truly hidden.
30c20106 7520
5950a3ac 7521 type Operand_Position is (First_Op, Second_Op);
8a36a0cc 7522 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
5950a3ac
AC
7523
7524 procedure Remove_Address_Interpretations (Op : Operand_Position);
0e0eecec
ES
7525 -- Ambiguities may arise when the operands are literal and the address
7526 -- operations in s-auxdec are visible. In that case, remove the
8b4230c8
AC
7527 -- interpretation of a literal as Address, to retain the semantics
7528 -- of Address as a private type.
9f4fd324
AC
7529
7530 ------------------------------------
5950a3ac 7531 -- Remove_Address_Interpretations --
9f4fd324
AC
7532 ------------------------------------
7533
5950a3ac 7534 procedure Remove_Address_Interpretations (Op : Operand_Position) is
9f4fd324
AC
7535 Formal : Entity_Id;
7536
7537 begin
7538 if Is_Overloaded (N) then
7539 Get_First_Interp (N, I, It);
7540 while Present (It.Nam) loop
7541 Formal := First_Entity (It.Nam);
7542
5950a3ac
AC
7543 if Op = Second_Op then
7544 Formal := Next_Entity (Formal);
7545 end if;
7546
d9d25d04
AC
7547 if Is_Descendant_Of_Address (Etype (Formal)) then
7548 Address_Descendant := True;
9f4fd324
AC
7549 Remove_Interp (I);
7550 end if;
7551
7552 Get_Next_Interp (I, It);
7553 end loop;
7554 end if;
7555 end Remove_Address_Interpretations;
7556
7557 -- Start of processing for Remove_Abstract_Operations
7558
30c20106 7559 begin
d935a36e 7560 if Is_Overloaded (N) then
ee1a7572 7561 if Debug_Flag_V then
ba301a3b 7562 Write_Line ("Remove_Abstract_Operations: ");
ee1a7572
AC
7563 Write_Overloads (N);
7564 end if;
7565
30c20106 7566 Get_First_Interp (N, I, It);
d935a36e 7567
30c20106 7568 while Present (It.Nam) loop
aab883ec
ES
7569 if Is_Overloadable (It.Nam)
7570 and then Is_Abstract_Subprogram (It.Nam)
30c20106
AC
7571 and then not Is_Dispatching_Operation (It.Nam)
7572 then
af152989 7573 Abstract_Op := It.Nam;
fe45e59e 7574
d9d25d04
AC
7575 if Is_Descendant_Of_Address (It.Typ) then
7576 Address_Descendant := True;
401093c1
ES
7577 Remove_Interp (I);
7578 exit;
7579
76264f60 7580 -- In Ada 2005, this operation does not participate in overload
9c510803 7581 -- resolution. If the operation is defined in a predefined
fe45e59e
ES
7582 -- unit, it is one of the operations declared abstract in some
7583 -- variants of System, and it must be removed as well.
7584
0791fbe9 7585 elsif Ada_Version >= Ada_2005
8ab31c0c 7586 or else In_Predefined_Unit (It.Nam)
fe45e59e
ES
7587 then
7588 Remove_Interp (I);
7589 exit;
7590 end if;
30c20106
AC
7591 end if;
7592
7593 Get_Next_Interp (I, It);
7594 end loop;
7595
af152989 7596 if No (Abstract_Op) then
fe45e59e
ES
7597
7598 -- If some interpretation yields an integer type, it is still
7599 -- possible that there are address interpretations. Remove them
7600 -- if one operand is a literal, to avoid spurious ambiguities
7601 -- on systems where Address is a visible integer type.
7602
7603 if Is_Overloaded (N)
401093c1 7604 and then Nkind (N) in N_Op
fe45e59e
ES
7605 and then Is_Integer_Type (Etype (N))
7606 then
7607 if Nkind (N) in N_Binary_Op then
7608 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
7609 Remove_Address_Interpretations (Second_Op);
7610
e6326de5 7611 elsif Nkind (Left_Opnd (N)) = N_Integer_Literal then
fe45e59e
ES
7612 Remove_Address_Interpretations (First_Op);
7613 end if;
7614 end if;
7615 end if;
3984e89a
AC
7616
7617 elsif Nkind (N) in N_Op then
4c46b835 7618
fe45e59e
ES
7619 -- Remove interpretations that treat literals as addresses. This
7620 -- is never appropriate, even when Address is defined as a visible
7621 -- Integer type. The reason is that we would really prefer Address
7a5b62b0
AC
7622 -- to behave as a private type, even in this case. If Address is a
7623 -- visible integer type, we get lots of overload ambiguities.
30c20106 7624
5950a3ac
AC
7625 if Nkind (N) in N_Binary_Op then
7626 declare
7627 U1 : constant Boolean :=
8b4230c8 7628 Present (Universal_Interpretation (Right_Opnd (N)));
5950a3ac 7629 U2 : constant Boolean :=
8b4230c8 7630 Present (Universal_Interpretation (Left_Opnd (N)));
30c20106 7631
5950a3ac 7632 begin
0e0eecec 7633 if U1 then
5950a3ac 7634 Remove_Address_Interpretations (Second_Op);
0e0eecec 7635 end if;
5950a3ac 7636
0e0eecec 7637 if U2 then
5950a3ac 7638 Remove_Address_Interpretations (First_Op);
30c20106
AC
7639 end if;
7640
5950a3ac
AC
7641 if not (U1 and U2) then
7642
7643 -- Remove corresponding predefined operator, which is
7644 -- always added to the overload set.
7645
7646 Get_First_Interp (N, I, It);
7647 while Present (It.Nam) loop
0ab80019
AC
7648 if Scope (It.Nam) = Standard_Standard
7649 and then Base_Type (It.Typ) =
7650 Base_Type (Etype (Abstract_Op))
7651 then
5950a3ac
AC
7652 Remove_Interp (I);
7653 end if;
7654
8a36a0cc
AC
7655 Get_Next_Interp (I, It);
7656 end loop;
7657
7658 elsif Is_Overloaded (N)
7659 and then Present (Univ_Type)
7660 then
7661 -- If both operands have a universal interpretation,
0e0eecec
ES
7662 -- it is still necessary to remove interpretations that
7663 -- yield Address. Any remaining ambiguities will be
7664 -- removed in Disambiguate.
8a36a0cc
AC
7665
7666 Get_First_Interp (N, I, It);
8a36a0cc 7667 while Present (It.Nam) loop
d9d25d04 7668 if Is_Descendant_Of_Address (It.Typ) then
0e0eecec
ES
7669 Remove_Interp (I);
7670
7671 elsif not Is_Type (It.Nam) then
8a36a0cc 7672 Set_Entity (N, It.Nam);
8a36a0cc
AC
7673 end if;
7674
5950a3ac
AC
7675 Get_Next_Interp (I, It);
7676 end loop;
7677 end if;
7678 end;
30c20106 7679 end if;
3984e89a
AC
7680
7681 elsif Nkind (N) = N_Function_Call
7682 and then
7683 (Nkind (Name (N)) = N_Operator_Symbol
7684 or else
7685 (Nkind (Name (N)) = N_Expanded_Name
7686 and then
7687 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
7688 then
5950a3ac 7689
3984e89a
AC
7690 declare
7691 Arg1 : constant Node_Id := First (Parameter_Associations (N));
5950a3ac
AC
7692 U1 : constant Boolean :=
7693 Present (Universal_Interpretation (Arg1));
7694 U2 : constant Boolean :=
7695 Present (Next (Arg1)) and then
7696 Present (Universal_Interpretation (Next (Arg1)));
3984e89a
AC
7697
7698 begin
0e0eecec 7699 if U1 then
5950a3ac 7700 Remove_Address_Interpretations (First_Op);
0e0eecec 7701 end if;
3984e89a 7702
0e0eecec 7703 if U2 then
5950a3ac
AC
7704 Remove_Address_Interpretations (Second_Op);
7705 end if;
7706
7707 if not (U1 and U2) then
3984e89a
AC
7708 Get_First_Interp (N, I, It);
7709 while Present (It.Nam) loop
9f4fd324
AC
7710 if Scope (It.Nam) = Standard_Standard
7711 and then It.Typ = Base_Type (Etype (Abstract_Op))
7712 then
3984e89a
AC
7713 Remove_Interp (I);
7714 end if;
7715
7716 Get_Next_Interp (I, It);
7717 end loop;
7718 end if;
7719 end;
30c20106 7720 end if;
af152989 7721
401093c1
ES
7722 -- If the removal has left no valid interpretations, emit an error
7723 -- message now and label node as illegal.
af152989
AC
7724
7725 if Present (Abstract_Op) then
7726 Get_First_Interp (N, I, It);
7727
7728 if No (It.Nam) then
7729
6e73e3ab 7730 -- Removal of abstract operation left no viable candidate
af152989
AC
7731
7732 Set_Etype (N, Any_Type);
7733 Error_Msg_Sloc := Sloc (Abstract_Op);
7734 Error_Msg_NE
7735 ("cannot call abstract operation& declared#", N, Abstract_Op);
401093c1
ES
7736
7737 -- In Ada 2005, an abstract operation may disable predefined
7738 -- operators. Since the context is not yet known, we mark the
7739 -- predefined operators as potentially hidden. Do not include
7740 -- predefined operators when addresses are involved since this
7741 -- case is handled separately.
7742
d9d25d04 7743 elsif Ada_Version >= Ada_2005 and then not Address_Descendant then
401093c1
ES
7744 while Present (It.Nam) loop
7745 if Is_Numeric_Type (It.Typ)
7746 and then Scope (It.Typ) = Standard_Standard
7747 then
7748 Set_Abstract_Op (I, Abstract_Op);
7749 end if;
7750
7751 Get_Next_Interp (I, It);
7752 end loop;
af152989
AC
7753 end if;
7754 end if;
ee1a7572
AC
7755
7756 if Debug_Flag_V then
ba301a3b 7757 Write_Line ("Remove_Abstract_Operations done: ");
ee1a7572
AC
7758 Write_Overloads (N);
7759 end if;
30c20106
AC
7760 end if;
7761 end Remove_Abstract_Operations;
7762
d50f4827
AC
7763 ----------------------------
7764 -- Try_Container_Indexing --
7765 ----------------------------
7766
7767 function Try_Container_Indexing
7768 (N : Node_Id;
7769 Prefix : Node_Id;
50878404 7770 Exprs : List_Id) return Boolean
d50f4827 7771 is
437244c7
AC
7772 Pref_Typ : constant Entity_Id := Etype (Prefix);
7773
0c3ef0cc
GD
7774 function Constant_Indexing_OK return Boolean;
7775 -- Constant_Indexing is legal if there is no Variable_Indexing defined
7776 -- for the type, or else node not a target of assignment, or an actual
7777 -- for an IN OUT or OUT formal (RM 4.1.6 (11)).
7778
211e7410
AC
7779 function Expr_Matches_In_Formal
7780 (Subp : Entity_Id;
7781 Par : Node_Id) return Boolean;
7782 -- Find formal corresponding to given indexed component that is an
7783 -- actual in a call. Note that the enclosing subprogram call has not
0c3ef0cc 7784 -- been analyzed yet, and the parameter list is not normalized, so
211e7410
AC
7785 -- that if the argument is a parameter association we must match it
7786 -- by name and not by position.
7787
437244c7
AC
7788 function Find_Indexing_Operations
7789 (T : Entity_Id;
7790 Nam : Name_Id;
7791 Is_Constant : Boolean) return Node_Id;
7792 -- Return a reference to the primitive operation of type T denoted by
7793 -- name Nam. If the operation is overloaded, the reference carries all
7794 -- interpretations. Flag Is_Constant should be set when the context is
7795 -- constant indexing.
7796
fa73fc3d
AC
7797 --------------------------
7798 -- Constant_Indexing_OK --
7799 --------------------------
7800
7801 function Constant_Indexing_OK return Boolean is
7802 Par : Node_Id;
7803
7804 begin
437244c7 7805 if No (Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing)) then
fa73fc3d
AC
7806 return True;
7807
7808 elsif not Is_Variable (Prefix) then
7809 return True;
7810 end if;
7811
7812 Par := N;
7813 while Present (Par) loop
7814 if Nkind (Parent (Par)) = N_Assignment_Statement
7815 and then Par = Name (Parent (Par))
7816 then
7817 return False;
7818
7819 -- The call may be overloaded, in which case we assume that its
7820 -- resolution does not depend on the type of the parameter that
7821 -- includes the indexing operation.
7822
7823 elsif Nkind_In (Parent (Par), N_Function_Call,
7824 N_Procedure_Call_Statement)
7825 and then Is_Entity_Name (Name (Parent (Par)))
7826 then
7827 declare
fa73fc3d
AC
7828 Proc : Entity_Id;
7829
7830 begin
7831 -- We should look for an interpretation with the proper
7832 -- number of formals, and determine whether it is an
31101470
AC
7833 -- In_Parameter, but for now we examine the formal that
7834 -- corresponds to the indexing, and assume that variable
7835 -- indexing is required if some interpretation has an
64ac53f4 7836 -- assignable formal at that position. Still does not
31101470 7837 -- cover the most complex cases ???
fa73fc3d
AC
7838
7839 if Is_Overloaded (Name (Parent (Par))) then
31101470
AC
7840 declare
7841 Proc : constant Node_Id := Name (Parent (Par));
31101470
AC
7842 I : Interp_Index;
7843 It : Interp;
7844
7845 begin
7846 Get_First_Interp (Proc, I, It);
7847 while Present (It.Nam) loop
211e7410
AC
7848 if not Expr_Matches_In_Formal (It.Nam, Par) then
7849 return False;
7850 end if;
31101470
AC
7851
7852 Get_Next_Interp (I, It);
7853 end loop;
7854 end;
7855
0c3ef0cc 7856 -- All interpretations have a matching in-mode formal
211e7410 7857
fa73fc3d
AC
7858 return True;
7859
7860 else
7861 Proc := Entity (Name (Parent (Par)));
7862
7863 -- If this is an indirect call, get formals from
7864 -- designated type.
7865
7866 if Is_Access_Subprogram_Type (Etype (Proc)) then
7867 Proc := Designated_Type (Etype (Proc));
7868 end if;
7869 end if;
7870
211e7410 7871 return Expr_Matches_In_Formal (Proc, Par);
fa73fc3d
AC
7872 end;
7873
7874 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
7875 return False;
7876
7877 -- If the indexed component is a prefix it may be the first actual
7878 -- of a prefixed call. Retrieve the called entity, if any, and
4e9ee595
AC
7879 -- check its first formal. Determine if the context is a procedure
7880 -- or function call.
fa73fc3d
AC
7881
7882 elsif Nkind (Parent (Par)) = N_Selected_Component then
7883 declare
7884 Sel : constant Node_Id := Selector_Name (Parent (Par));
7885 Nam : constant Entity_Id := Current_Entity (Sel);
7886
7887 begin
bc38dbb4
AC
7888 if Present (Nam) and then Is_Overloadable (Nam) then
7889 if Nkind (Parent (Parent (Par))) =
7890 N_Procedure_Call_Statement
4e9ee595
AC
7891 then
7892 return False;
7893
bc38dbb4
AC
7894 elsif Ekind (Nam) = E_Function
7895 and then Present (First_Formal (Nam))
7896 then
7897 return Ekind (First_Formal (Nam)) = E_In_Parameter;
4e9ee595 7898 end if;
fa73fc3d
AC
7899 end if;
7900 end;
7901
437244c7 7902 elsif Nkind (Par) in N_Op then
fa73fc3d
AC
7903 return True;
7904 end if;
7905
7906 Par := Parent (Par);
7907 end loop;
7908
7909 -- In all other cases, constant indexing is legal
7910
7911 return True;
7912 end Constant_Indexing_OK;
7913
f32eb591
AC
7914 ----------------------------
7915 -- Expr_Matches_In_Formal --
7916 ----------------------------
0c3ef0cc
GD
7917
7918 function Expr_Matches_In_Formal
7919 (Subp : Entity_Id;
7920 Par : Node_Id) return Boolean
7921 is
7922 Actual : Node_Id;
7923 Formal : Node_Id;
7924
7925 begin
7926 Formal := First_Formal (Subp);
7927 Actual := First (Parameter_Associations ((Parent (Par))));
7928
7929 if Nkind (Par) /= N_Parameter_Association then
7930
7931 -- Match by position
7932
7933 while Present (Actual) and then Present (Formal) loop
7934 exit when Actual = Par;
7935 Next (Actual);
7936
7937 if Present (Formal) then
7938 Next_Formal (Formal);
7939
7940 -- Otherwise this is a parameter mismatch, the error is
7941 -- reported elsewhere, or else variable indexing is implied.
7942
7943 else
7944 return False;
7945 end if;
7946 end loop;
7947
7948 else
7949 -- Match by name
7950
7951 while Present (Formal) loop
7952 exit when Chars (Formal) = Chars (Selector_Name (Par));
7953 Next_Formal (Formal);
7954
7955 if No (Formal) then
7956 return False;
7957 end if;
7958 end loop;
7959 end if;
7960
7961 return Present (Formal) and then Ekind (Formal) = E_In_Parameter;
7962 end Expr_Matches_In_Formal;
7963
437244c7
AC
7964 ------------------------------
7965 -- Find_Indexing_Operations --
7966 ------------------------------
7967
7968 function Find_Indexing_Operations
7969 (T : Entity_Id;
7970 Nam : Name_Id;
7971 Is_Constant : Boolean) return Node_Id
7972 is
7973 procedure Inspect_Declarations
7974 (Typ : Entity_Id;
7975 Ref : in out Node_Id);
7976 -- Traverse the declarative list where type Typ resides and collect
7977 -- all suitable interpretations in node Ref.
7978
7979 procedure Inspect_Primitives
7980 (Typ : Entity_Id;
7981 Ref : in out Node_Id);
7982 -- Traverse the list of primitive operations of type Typ and collect
7983 -- all suitable interpretations in node Ref.
7984
7985 function Is_OK_Candidate
7986 (Subp_Id : Entity_Id;
7987 Typ : Entity_Id) return Boolean;
7988 -- Determine whether subprogram Subp_Id is a suitable indexing
7989 -- operation for type Typ. To qualify as such, the subprogram must
7990 -- be a function, have at least two parameters, and the type of the
7991 -- first parameter must be either Typ, or Typ'Class, or access [to
7992 -- constant] with designated type Typ or Typ'Class.
7993
7994 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id);
7995 -- Store subprogram Subp_Id as an interpretation in node Ref
7996
7997 --------------------------
7998 -- Inspect_Declarations --
7999 --------------------------
8000
8001 procedure Inspect_Declarations
8002 (Typ : Entity_Id;
8003 Ref : in out Node_Id)
8004 is
8005 Typ_Decl : constant Node_Id := Declaration_Node (Typ);
8006 Decl : Node_Id;
8007 Subp_Id : Entity_Id;
8008
8009 begin
2cc2e964 8010 -- Ensure that the routine is not called with itypes, which lack a
437244c7
AC
8011 -- declarative node.
8012
8013 pragma Assert (Present (Typ_Decl));
8014 pragma Assert (Is_List_Member (Typ_Decl));
8015
8016 Decl := First (List_Containing (Typ_Decl));
8017 while Present (Decl) loop
8018 if Nkind (Decl) = N_Subprogram_Declaration then
8019 Subp_Id := Defining_Entity (Decl);
8020
8021 if Is_OK_Candidate (Subp_Id, Typ) then
8022 Record_Interp (Subp_Id, Ref);
8023 end if;
8024 end if;
8025
8026 Next (Decl);
8027 end loop;
8028 end Inspect_Declarations;
8029
8030 ------------------------
8031 -- Inspect_Primitives --
8032 ------------------------
8033
8034 procedure Inspect_Primitives
8035 (Typ : Entity_Id;
8036 Ref : in out Node_Id)
8037 is
8038 Prim_Elmt : Elmt_Id;
8039 Prim_Id : Entity_Id;
8040
8041 begin
8042 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
8043 while Present (Prim_Elmt) loop
8044 Prim_Id := Node (Prim_Elmt);
8045
8046 if Is_OK_Candidate (Prim_Id, Typ) then
8047 Record_Interp (Prim_Id, Ref);
8048 end if;
8049
8050 Next_Elmt (Prim_Elmt);
8051 end loop;
8052 end Inspect_Primitives;
8053
8054 ---------------------
8055 -- Is_OK_Candidate --
8056 ---------------------
8057
8058 function Is_OK_Candidate
8059 (Subp_Id : Entity_Id;
8060 Typ : Entity_Id) return Boolean
8061 is
8062 Formal : Entity_Id;
8063 Formal_Typ : Entity_Id;
8064 Param_Typ : Node_Id;
8065
8066 begin
2cc2e964 8067 -- To classify as a suitable candidate, the subprogram must be a
437244c7
AC
8068 -- function whose name matches the argument of aspect Constant or
8069 -- Variable_Indexing.
8070
8071 if Ekind (Subp_Id) = E_Function and then Chars (Subp_Id) = Nam then
8072 Formal := First_Formal (Subp_Id);
8073
8074 -- The candidate requires at least two parameters
8075
8076 if Present (Formal) and then Present (Next_Formal (Formal)) then
8077 Formal_Typ := Empty;
8078 Param_Typ := Parameter_Type (Parent (Formal));
8079
8080 -- Use the designated type when the first parameter is of an
8081 -- access type.
8082
8083 if Nkind (Param_Typ) = N_Access_Definition
8084 and then Present (Subtype_Mark (Param_Typ))
8085 then
8086 -- When the context is a constant indexing, the access
8087 -- definition must be access-to-constant. This does not
8088 -- apply to variable indexing.
8089
8090 if not Is_Constant
8091 or else Constant_Present (Param_Typ)
8092 then
8093 Formal_Typ := Etype (Subtype_Mark (Param_Typ));
8094 end if;
8095
8096 -- Otherwise use the parameter type
8097
8098 else
8099 Formal_Typ := Etype (Param_Typ);
8100 end if;
8101
8102 if Present (Formal_Typ) then
8103
8104 -- Use the specific type when the parameter type is
8105 -- class-wide.
8106
8107 if Is_Class_Wide_Type (Formal_Typ) then
8108 Formal_Typ := Etype (Base_Type (Formal_Typ));
8109 end if;
8110
8111 -- Use the full view when the parameter type is private
8112 -- or incomplete.
8113
8114 if Is_Incomplete_Or_Private_Type (Formal_Typ)
8115 and then Present (Full_View (Formal_Typ))
8116 then
8117 Formal_Typ := Full_View (Formal_Typ);
8118 end if;
8119
8120 -- The type of the first parameter must denote the type
8121 -- of the container or acts as its ancestor type.
8122
8123 return
8124 Formal_Typ = Typ
8125 or else Is_Ancestor (Formal_Typ, Typ);
8126 end if;
8127 end if;
8128 end if;
8129
8130 return False;
8131 end Is_OK_Candidate;
8132
8133 -------------------
8134 -- Record_Interp --
8135 -------------------
8136
8137 procedure Record_Interp (Subp_Id : Entity_Id; Ref : in out Node_Id) is
8138 begin
8139 if Present (Ref) then
8140 Add_One_Interp (Ref, Subp_Id, Etype (Subp_Id));
8141
8142 -- Otherwise this is the first interpretation. Create a reference
8143 -- where all remaining interpretations will be collected.
8144
8145 else
8146 Ref := New_Occurrence_Of (Subp_Id, Sloc (T));
8147 end if;
8148 end Record_Interp;
8149
8150 -- Local variables
8151
8152 Ref : Node_Id;
8153 Typ : Entity_Id;
8154
8155 -- Start of processing for Find_Indexing_Operations
8156
8157 begin
8158 Typ := T;
8159
1e60643a
AC
8160 -- Use the specific type when the parameter type is class-wide
8161
437244c7
AC
8162 if Is_Class_Wide_Type (Typ) then
8163 Typ := Root_Type (Typ);
8164 end if;
8165
8166 Ref := Empty;
1e60643a 8167 Typ := Underlying_Type (Base_Type (Typ));
437244c7 8168
e11b776b
AC
8169 Inspect_Primitives (Typ, Ref);
8170
8171 -- Now look for explicit declarations of an indexing operation.
8172 -- If the type is private the operation may be declared in the
8173 -- visible part that contains the partial view.
8174
8175 if Is_Private_Type (T) then
8176 Inspect_Declarations (T, Ref);
8177 end if;
8178
437244c7
AC
8179 Inspect_Declarations (Typ, Ref);
8180
8181 return Ref;
8182 end Find_Indexing_Operations;
8183
fa73fc3d
AC
8184 -- Local variables
8185
d50f4827 8186 Loc : constant Source_Ptr := Sloc (N);
50878404 8187 Assoc : List_Id;
fa73fc3d 8188 C_Type : Entity_Id;
d50f4827
AC
8189 Func : Entity_Id;
8190 Func_Name : Node_Id;
8191 Indexing : Node_Id;
d50f4827 8192
437244c7
AC
8193 Is_Constant_Indexing : Boolean := False;
8194 -- This flag reflects the nature of the container indexing. Note that
8195 -- the context may be suited for constant indexing, but the type may
8196 -- lack a Constant_Indexing annotation.
8197
fa73fc3d
AC
8198 -- Start of processing for Try_Container_Indexing
8199
d50f4827 8200 begin
fa73fc3d
AC
8201 -- Node may have been analyzed already when testing for a prefixed
8202 -- call, in which case do not redo analysis.
8203
8204 if Present (Generalized_Indexing (N)) then
8205 return True;
8206 end if;
8207
437244c7 8208 C_Type := Pref_Typ;
f3296dd3 8209
fa73fc3d
AC
8210 -- If indexing a class-wide container, obtain indexing primitive from
8211 -- specific type.
f3296dd3
AC
8212
8213 if Is_Class_Wide_Type (C_Type) then
8214 C_Type := Etype (Base_Type (C_Type));
8215 end if;
d50f4827 8216
2cc2e964 8217 -- Check whether the type has a specified indexing aspect
d50f4827
AC
8218
8219 Func_Name := Empty;
d50f4827 8220
2cc2e964
AC
8221 -- The context is suitable for constant indexing, so obtain the name of
8222 -- the indexing function from aspect Constant_Indexing.
437244c7 8223
fa73fc3d 8224 if Constant_Indexing_OK then
d62520f3 8225 Func_Name :=
437244c7 8226 Find_Value_Of_Aspect (Pref_Typ, Aspect_Constant_Indexing);
9ee76313 8227 end if;
dedac3eb 8228
437244c7
AC
8229 if Present (Func_Name) then
8230 Is_Constant_Indexing := True;
8231
8232 -- Otherwise attempt variable indexing
8233
8234 else
d62520f3 8235 Func_Name :=
437244c7 8236 Find_Value_Of_Aspect (Pref_Typ, Aspect_Variable_Indexing);
9ee76313 8237 end if;
d50f4827 8238
437244c7
AC
8239 -- The type is not subject to either form of indexing, therefore the
8240 -- indexed component does not denote container indexing. If this is a
8241 -- true error, it is diagnosed by the caller.
d50f4827
AC
8242
8243 if No (Func_Name) then
57a8057a 8244
437244c7
AC
8245 -- The prefix itself may be an indexing of a container. Rewrite it
8246 -- as such and retry.
57a8057a 8247
437244c7
AC
8248 if Has_Implicit_Dereference (Pref_Typ) then
8249 Build_Explicit_Dereference (Prefix, First_Discriminant (Pref_Typ));
50878404 8250 return Try_Container_Indexing (N, Prefix, Exprs);
57a8057a 8251
437244c7
AC
8252 -- Otherwise this is definitely not container indexing
8253
57a8057a
AC
8254 else
8255 return False;
8256 end if;
3f433bc0 8257
6907542d
AC
8258 -- If the container type is derived from another container type, the
8259 -- value of the inherited aspect is the Reference operation declared
8260 -- for the parent type.
8261
fa73fc3d
AC
8262 -- However, Reference is also a primitive operation of the type, and the
8263 -- inherited operation has a different signature. We retrieve the right
8264 -- ones (the function may be overloaded) from the list of primitive
8265 -- operations of the derived type.
3f433bc0 8266
fa73fc3d
AC
8267 -- Note that predefined containers are typically all derived from one of
8268 -- the Controlled types. The code below is motivated by containers that
8269 -- are derived from other types with a Reference aspect.
c85dda72 8270 -- Note as well that we need to examine the base type, given that
dab8e608
GD
8271 -- the container object may be a constrained subtype or itype that
8272 -- does not have an explicit declaration.
6907542d 8273
f3296dd3 8274 elsif Is_Derived_Type (C_Type)
437244c7 8275 and then Etype (First_Formal (Entity (Func_Name))) /= Pref_Typ
6907542d 8276 then
437244c7
AC
8277 Func_Name :=
8278 Find_Indexing_Operations
c85dda72 8279 (T => Base_Type (C_Type),
437244c7
AC
8280 Nam => Chars (Func_Name),
8281 Is_Constant => Is_Constant_Indexing);
d50f4827
AC
8282 end if;
8283
50878404
AC
8284 Assoc := New_List (Relocate_Node (Prefix));
8285
5f50020a 8286 -- A generalized indexing may have nore than one index expression, so
50878404 8287 -- transfer all of them to the argument list to be used in the call.
5f50020a
ES
8288 -- Note that there may be named associations, in which case the node
8289 -- was rewritten earlier as a call, and has been transformed back into
8290 -- an indexed expression to share the following processing.
e917e3b8 8291
5f50020a
ES
8292 -- The generalized indexing node is the one on which analysis and
8293 -- resolution take place. Before expansion the original node is replaced
fa73fc3d
AC
8294 -- with the generalized indexing node, which is a call, possibly with a
8295 -- dereference operation.
50878404 8296
e917e3b8 8297 if Comes_From_Source (N) then
c86cf714 8298 Check_Compiler_Unit ("generalized indexing", N);
e917e3b8
AC
8299 end if;
8300
287aa0ed
AC
8301 -- Create argument list for function call that represents generalized
8302 -- indexing. Note that indices (i.e. actuals) may themselves be
8303 -- overloaded.
8304
50878404 8305 declare
287aa0ed
AC
8306 Arg : Node_Id;
8307 New_Arg : Node_Id;
8308
50878404
AC
8309 begin
8310 Arg := First (Exprs);
8311 while Present (Arg) loop
287aa0ed 8312 New_Arg := Relocate_Node (Arg);
43151cfd
ES
8313
8314 -- The arguments can be parameter associations, in which case the
8315 -- explicit actual parameter carries the overloadings.
8316
8317 if Nkind (New_Arg) /= N_Parameter_Association then
8318 Save_Interps (Arg, New_Arg);
8319 end if;
8320
287aa0ed 8321 Append (New_Arg, Assoc);
50878404
AC
8322 Next (Arg);
8323 end loop;
8324 end;
8325
d50f4827
AC
8326 if not Is_Overloaded (Func_Name) then
8327 Func := Entity (Func_Name);
f4ef7b06 8328
29ba9f52
RD
8329 Indexing :=
8330 Make_Function_Call (Loc,
8331 Name => New_Occurrence_Of (Func, Loc),
8332 Parameter_Associations => Assoc);
f4ef7b06 8333
5f50020a
ES
8334 Set_Parent (Indexing, Parent (N));
8335 Set_Generalized_Indexing (N, Indexing);
8336 Analyze (Indexing);
8337 Set_Etype (N, Etype (Indexing));
d50f4827 8338
76d49f49
ES
8339 -- If the return type of the indexing function is a reference type,
8340 -- add the dereference as a possible interpretation. Note that the
8341 -- indexing aspect may be a function that returns the element type
5f50020a
ES
8342 -- with no intervening implicit dereference, and that the reference
8343 -- discriminant is not the first discriminant.
76d49f49
ES
8344
8345 if Has_Discriminants (Etype (Func)) then
71ff3d18 8346 Check_Implicit_Dereference (N, Etype (Func));
76d49f49 8347 end if;
d50f4827
AC
8348
8349 else
90b510e4
AC
8350 -- If there are multiple indexing functions, build a function call
8351 -- and analyze it for each of the possible interpretations.
8352
8b4230c8
AC
8353 Indexing :=
8354 Make_Function_Call (Loc,
fa73fc3d
AC
8355 Name =>
8356 Make_Identifier (Loc, Chars (Func_Name)),
8b4230c8 8357 Parameter_Associations => Assoc);
5f50020a
ES
8358 Set_Parent (Indexing, Parent (N));
8359 Set_Generalized_Indexing (N, Indexing);
90b510e4
AC
8360 Set_Etype (N, Any_Type);
8361 Set_Etype (Name (Indexing), Any_Type);
d50f4827
AC
8362
8363 declare
8b4230c8
AC
8364 I : Interp_Index;
8365 It : Interp;
d50f4827
AC
8366 Success : Boolean;
8367
8368 begin
8369 Get_First_Interp (Func_Name, I, It);
5f50020a 8370 Set_Etype (Indexing, Any_Type);
90b510e4 8371
f4ef7b06 8372 -- Analyze each candidate function with the given actuals
0310af44 8373
d50f4827 8374 while Present (It.Nam) loop
5f50020a 8375 Analyze_One_Call (Indexing, It.Nam, False, Success);
0310af44
AC
8376 Get_Next_Interp (I, It);
8377 end loop;
32bba3c9 8378
0310af44
AC
8379 -- If there are several successful candidates, resolution will
8380 -- be by result. Mark the interpretations of the function name
8381 -- itself.
d50f4827 8382
0310af44
AC
8383 if Is_Overloaded (Indexing) then
8384 Get_First_Interp (Indexing, I, It);
90b510e4 8385
0310af44 8386 while Present (It.Nam) loop
90b510e4 8387 Add_One_Interp (Name (Indexing), It.Nam, It.Typ);
0310af44
AC
8388 Get_Next_Interp (I, It);
8389 end loop;
8390
8391 else
8392 Set_Etype (Name (Indexing), Etype (Indexing));
8393 end if;
8394
8395 -- Now add the candidate interpretations to the indexing node
8396 -- itself, to be replaced later by the function call.
8397
8398 if Is_Overloaded (Name (Indexing)) then
8399 Get_First_Interp (Name (Indexing), I, It);
8400
8401 while Present (It.Nam) loop
90b510e4
AC
8402 Add_One_Interp (N, It.Nam, It.Typ);
8403
6c7f7b8c
AC
8404 -- Add dereference interpretation if the result type has
8405 -- implicit reference discriminants.
d50f4827 8406
76d49f49 8407 if Has_Discriminants (Etype (It.Nam)) then
71ff3d18 8408 Check_Implicit_Dereference (N, Etype (It.Nam));
76d49f49 8409 end if;
32bba3c9 8410
0310af44
AC
8411 Get_Next_Interp (I, It);
8412 end loop;
8413
8414 else
8415 Set_Etype (N, Etype (Name (Indexing)));
8416 if Has_Discriminants (Etype (N)) then
8417 Check_Implicit_Dereference (N, Etype (N));
8418 end if;
8419 end if;
d50f4827
AC
8420 end;
8421 end if;
8422
5f50020a 8423 if Etype (Indexing) = Any_Type then
29ba9f52
RD
8424 Error_Msg_NE
8425 ("container cannot be indexed with&", N, Etype (First (Exprs)));
9ee76313 8426 Rewrite (N, New_Occurrence_Of (Any_Id, Loc));
9ee76313
AC
8427 end if;
8428
d50f4827
AC
8429 return True;
8430 end Try_Container_Indexing;
8431
996ae0b0
RK
8432 -----------------------
8433 -- Try_Indirect_Call --
8434 -----------------------
8435
8436 function Try_Indirect_Call
91b1417d
AC
8437 (N : Node_Id;
8438 Nam : Entity_Id;
8439 Typ : Entity_Id) return Boolean
996ae0b0 8440 is
24657705
HK
8441 Actual : Node_Id;
8442 Formal : Entity_Id;
8443
8a7988f5 8444 Call_OK : Boolean;
24657705 8445 pragma Warnings (Off, Call_OK);
996ae0b0
RK
8446
8447 begin
8a7988f5 8448 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
9de61fcb 8449
8a7988f5 8450 Actual := First_Actual (N);
fbf5a39b 8451 Formal := First_Formal (Designated_Type (Typ));
9de61fcb 8452 while Present (Actual) and then Present (Formal) loop
996ae0b0
RK
8453 if not Has_Compatible_Type (Actual, Etype (Formal)) then
8454 return False;
8455 end if;
8456
8457 Next (Actual);
8458 Next_Formal (Formal);
8459 end loop;
8460
8461 if No (Actual) and then No (Formal) then
8462 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
8463
8464 -- Nam is a candidate interpretation for the name in the call,
8465 -- if it is not an indirect call.
8466
8467 if not Is_Type (Nam)
8468 and then Is_Entity_Name (Name (N))
8469 then
8470 Set_Entity (Name (N), Nam);
8471 end if;
8472
8473 return True;
8b4230c8 8474
996ae0b0
RK
8475 else
8476 return False;
8477 end if;
8478 end Try_Indirect_Call;
8479
8480 ----------------------
8481 -- Try_Indexed_Call --
8482 ----------------------
8483
8484 function Try_Indexed_Call
aab883ec
ES
8485 (N : Node_Id;
8486 Nam : Entity_Id;
8487 Typ : Entity_Id;
8488 Skip_First : Boolean) return Boolean
996ae0b0 8489 is
5ff22245
ES
8490 Loc : constant Source_Ptr := Sloc (N);
8491 Actuals : constant List_Id := Parameter_Associations (N);
8492 Actual : Node_Id;
8493 Index : Entity_Id;
996ae0b0
RK
8494
8495 begin
fbf5a39b 8496 Actual := First (Actuals);
aab883ec
ES
8497
8498 -- If the call was originally written in prefix form, skip the first
8499 -- actual, which is obviously not defaulted.
8500
8501 if Skip_First then
8502 Next (Actual);
8503 end if;
8504
fbf5a39b 8505 Index := First_Index (Typ);
9de61fcb
RD
8506 while Present (Actual) and then Present (Index) loop
8507
996ae0b0
RK
8508 -- If the parameter list has a named association, the expression
8509 -- is definitely a call and not an indexed component.
8510
8511 if Nkind (Actual) = N_Parameter_Association then
8512 return False;
8513 end if;
8514
5ff22245
ES
8515 if Is_Entity_Name (Actual)
8516 and then Is_Type (Entity (Actual))
8517 and then No (Next (Actual))
8518 then
1c218ac3
AC
8519 -- A single actual that is a type name indicates a slice if the
8520 -- type is discrete, and an error otherwise.
8521
8522 if Is_Discrete_Type (Entity (Actual)) then
8523 Rewrite (N,
8524 Make_Slice (Loc,
22b77f68
RD
8525 Prefix =>
8526 Make_Function_Call (Loc,
8527 Name => Relocate_Node (Name (N))),
8528 Discrete_Range =>
1c218ac3
AC
8529 New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
8530
8531 Analyze (N);
8532
8533 else
8534 Error_Msg_N ("invalid use of type in expression", Actual);
8535 Set_Etype (N, Any_Type);
8536 end if;
5ff22245 8537
5ff22245
ES
8538 return True;
8539
8540 elsif not Has_Compatible_Type (Actual, Etype (Index)) then
996ae0b0
RK
8541 return False;
8542 end if;
8543
8544 Next (Actual);
8545 Next_Index (Index);
8546 end loop;
8547
8548 if No (Actual) and then No (Index) then
8549 Add_One_Interp (N, Nam, Component_Type (Typ));
8550
8551 -- Nam is a candidate interpretation for the name in the call,
8552 -- if it is not an indirect call.
8553
8554 if not Is_Type (Nam)
8555 and then Is_Entity_Name (Name (N))
8556 then
8557 Set_Entity (Name (N), Nam);
8558 end if;
8559
8560 return True;
8561 else
8562 return False;
8563 end if;
996ae0b0
RK
8564 end Try_Indexed_Call;
8565
35ae2ed8
AC
8566 --------------------------
8567 -- Try_Object_Operation --
8568 --------------------------
8569
8cf23b91
AC
8570 function Try_Object_Operation
8571 (N : Node_Id; CW_Test_Only : Boolean := False) return Boolean
8572 is
b67a385c 8573 K : constant Node_Kind := Nkind (Parent (N));
d3b00ce3 8574 Is_Subprg_Call : constant Boolean := K in N_Subprogram_Call;
b67a385c 8575 Loc : constant Source_Ptr := Sloc (N);
b67a385c 8576 Obj : constant Node_Id := Prefix (N);
0d57c6f4 8577
48c8c473 8578 Subprog : constant Node_Id :=
0d57c6f4
RD
8579 Make_Identifier (Sloc (Selector_Name (N)),
8580 Chars => Chars (Selector_Name (N)));
401093c1 8581 -- Identifier on which possible interpretations will be collected
0a36105d 8582
b67a385c 8583 Report_Error : Boolean := False;
8b4230c8
AC
8584 -- If no candidate interpretation matches the context, redo analysis
8585 -- with Report_Error True to provide additional information.
28d6470f
JM
8586
8587 Actual : Node_Id;
d469eabe 8588 Candidate : Entity_Id := Empty;
48c8c473 8589 New_Call_Node : Node_Id := Empty;
4c46b835 8590 Node_To_Replace : Node_Id;
28d6470f 8591 Obj_Type : Entity_Id := Etype (Obj);
48c8c473 8592 Success : Boolean := False;
0a36105d 8593
4c46b835
AC
8594 procedure Complete_Object_Operation
8595 (Call_Node : Node_Id;
0a36105d 8596 Node_To_Replace : Node_Id);
ec6078e3
ES
8597 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
8598 -- Call_Node, insert the object (or its dereference) as the first actual
8599 -- in the call, and complete the analysis of the call.
4c46b835 8600
0a36105d 8601 procedure Report_Ambiguity (Op : Entity_Id);
48c8c473
AC
8602 -- If a prefixed procedure call is ambiguous, indicate whether the call
8603 -- includes an implicit dereference or an implicit 'Access.
0a36105d 8604
4c46b835
AC
8605 procedure Transform_Object_Operation
8606 (Call_Node : out Node_Id;
0a36105d 8607 Node_To_Replace : out Node_Id);
0e3a687f 8608 -- Transform Obj.Operation (X, Y, ...) into Operation (Obj, X, Y ...).
d469eabe
HK
8609 -- Call_Node is the resulting subprogram call, Node_To_Replace is
8610 -- either N or the parent of N, and Subprog is a reference to the
8611 -- subprogram we are trying to match.
35ae2ed8
AC
8612
8613 function Try_Class_Wide_Operation
4c46b835
AC
8614 (Call_Node : Node_Id;
8615 Node_To_Replace : Node_Id) return Boolean;
48c8c473
AC
8616 -- Traverse all ancestor types looking for a class-wide subprogram for
8617 -- which the current operation is a valid non-dispatching call.
35ae2ed8 8618
0a36105d
JM
8619 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
8620 -- If prefix is overloaded, its interpretation may include different
48c8c473
AC
8621 -- tagged types, and we must examine the primitive operations and the
8622 -- class-wide operations of each in order to find candidate
0a36105d
JM
8623 -- interpretations for the call as a whole.
8624
4c46b835
AC
8625 function Try_Primitive_Operation
8626 (Call_Node : Node_Id;
8627 Node_To_Replace : Node_Id) return Boolean;
ec6078e3 8628 -- Traverse the list of primitive subprograms looking for a dispatching
48c8c473 8629 -- operation for which the current node is a valid call.
0a36105d
JM
8630
8631 function Valid_Candidate
8632 (Success : Boolean;
8633 Call : Node_Id;
48c8c473
AC
8634 Subp : Entity_Id) return Entity_Id;
8635 -- If the subprogram is a valid interpretation, record it, and add to
8636 -- the list of interpretations of Subprog. Otherwise return Empty.
0a36105d 8637
4c46b835
AC
8638 -------------------------------
8639 -- Complete_Object_Operation --
8640 -------------------------------
8641
8642 procedure Complete_Object_Operation
8643 (Call_Node : Node_Id;
0a36105d 8644 Node_To_Replace : Node_Id)
4c46b835 8645 is
b4592168
GD
8646 Control : constant Entity_Id := First_Formal (Entity (Subprog));
8647 Formal_Type : constant Entity_Id := Etype (Control);
ec6078e3
ES
8648 First_Actual : Node_Id;
8649
4c46b835 8650 begin
955871d3
AC
8651 -- Place the name of the operation, with its interpretations,
8652 -- on the rewritten call.
0a36105d 8653
ec6078e3
ES
8654 Set_Name (Call_Node, Subprog);
8655
0a36105d
JM
8656 First_Actual := First (Parameter_Associations (Call_Node));
8657
8b4230c8
AC
8658 -- For cross-reference purposes, treat the new node as being in the
8659 -- source if the original one is. Set entity and type, even though
8660 -- they may be overwritten during resolution if overloaded.
b67a385c
ES
8661
8662 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
8663 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
8664
ec6078e3 8665 if Nkind (N) = N_Selected_Component
3d918396 8666 and then not Inside_A_Generic
ec6078e3
ES
8667 then
8668 Set_Entity (Selector_Name (N), Entity (Subprog));
b2ab8c33 8669 Set_Etype (Selector_Name (N), Etype (Entity (Subprog)));
ec6078e3
ES
8670 end if;
8671
8b4230c8
AC
8672 -- If need be, rewrite first actual as an explicit dereference. If
8673 -- the call is overloaded, the rewriting can only be done once the
8674 -- primitive operation is identified.
0a36105d
JM
8675
8676 if Is_Overloaded (Subprog) then
ec6078e3 8677
0a36105d
JM
8678 -- The prefix itself may be overloaded, and its interpretations
8679 -- must be propagated to the new actual in the call.
8680
8681 if Is_Overloaded (Obj) then
8682 Save_Interps (Obj, First_Actual);
8683 end if;
8684
8685 Rewrite (First_Actual, Obj);
8686
8687 elsif not Is_Access_Type (Formal_Type)
ec6078e3
ES
8688 and then Is_Access_Type (Etype (Obj))
8689 then
8690 Rewrite (First_Actual,
8691 Make_Explicit_Dereference (Sloc (Obj), Obj));
8692 Analyze (First_Actual);
fe45e59e 8693
401093c1
ES
8694 -- If we need to introduce an explicit dereference, verify that
8695 -- the resulting actual is compatible with the mode of the formal.
8696
8697 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
8698 and then Is_Access_Constant (Etype (Obj))
8699 then
8700 Error_Msg_NE
8701 ("expect variable in call to&", Prefix (N), Entity (Subprog));
8702 end if;
8703
2eda24e9
PMR
8704 -- Conversely, if the formal is an access parameter and the object is
8705 -- not an access type or a reference type (i.e. a type with the
8706 -- Implicit_Dereference aspect specified), replace the actual with a
8707 -- 'Access reference. Its analysis will check that the object is
8708 -- aliased.
fe45e59e
ES
8709
8710 elsif Is_Access_Type (Formal_Type)
8711 and then not Is_Access_Type (Etype (Obj))
aa11d1dd
PMR
8712 and then
8713 (not Has_Implicit_Dereference (Etype (Obj))
8714 or else
8715 not Is_Access_Type (Designated_Type (Etype
8716 (Get_Reference_Discriminant (Etype (Obj))))))
fe45e59e 8717 then
2eda24e9 8718 -- A special case: A.all'Access is illegal if A is an access to a
b4592168
GD
8719 -- constant and the context requires an access to a variable.
8720
8721 if not Is_Access_Constant (Formal_Type) then
8722 if (Nkind (Obj) = N_Explicit_Dereference
8723 and then Is_Access_Constant (Etype (Prefix (Obj))))
8724 or else not Is_Variable (Obj)
8725 then
8726 Error_Msg_NE
ad075b50 8727 ("actual for & must be a variable", Obj, Control);
b4592168
GD
8728 end if;
8729 end if;
8730
fe45e59e
ES
8731 Rewrite (First_Actual,
8732 Make_Attribute_Reference (Loc,
8733 Attribute_Name => Name_Access,
8734 Prefix => Relocate_Node (Obj)));
0a36105d 8735
8f34c90b
AC
8736 -- If the object is not overloaded verify that taking access of
8737 -- it is legal. Otherwise check is made during resolution.
8738
8739 if not Is_Overloaded (Obj)
8740 and then not Is_Aliased_View (Obj)
8741 then
ed2233dc 8742 Error_Msg_NE
ad075b50 8743 ("object in prefixed call to & must be aliased "
715e529d 8744 & "(RM 4.1.3 (13 1/2))", Prefix (First_Actual), Subprog);
0a36105d
JM
8745 end if;
8746
fe45e59e
ES
8747 Analyze (First_Actual);
8748
ec6078e3 8749 else
0a36105d
JM
8750 if Is_Overloaded (Obj) then
8751 Save_Interps (Obj, First_Actual);
8752 end if;
ec6078e3 8753
0a36105d 8754 Rewrite (First_Actual, Obj);
aab883ec
ES
8755 end if;
8756
e699b76e
AC
8757 -- The operation is obtained from the dispatch table and not by
8758 -- visibility, and may be declared in a unit that is not explicitly
8759 -- referenced in the source, but is nevertheless required in the
8760 -- context of the current unit. Indicate that operation and its scope
8761 -- are referenced, to prevent spurious and misleading warnings. If
8762 -- the operation is overloaded, all primitives are in the same scope
8763 -- and we can use any of them.
8764
8765 Set_Referenced (Entity (Subprog), True);
8766 Set_Referenced (Scope (Entity (Subprog)), True);
8767
7ffd9312 8768 Rewrite (Node_To_Replace, Call_Node);
0a36105d
JM
8769
8770 -- Propagate the interpretations collected in subprog to the new
8771 -- function call node, to be resolved from context.
8772
8773 if Is_Overloaded (Subprog) then
8774 Save_Interps (Subprog, Node_To_Replace);
7415029d 8775
0a36105d 8776 else
28e18b4f
AC
8777 -- The type of the subprogram may be a limited view obtained
8778 -- transitively from another unit. If full view is available,
c312b9f2
PMR
8779 -- use it to analyze call. If there is no nonlimited view, then
8780 -- this is diagnosed when analyzing the rewritten call.
28e18b4f
AC
8781
8782 declare
8783 T : constant Entity_Id := Etype (Subprog);
8784 begin
8785 if From_Limited_With (T) then
8786 Set_Etype (Entity (Subprog), Available_View (T));
8787 end if;
8788 end;
8789
0a36105d 8790 Analyze (Node_To_Replace);
438ff97c 8791
199c6a10
AC
8792 -- If the operation has been rewritten into a call, which may get
8793 -- subsequently an explicit dereference, preserve the type on the
8794 -- original node (selected component or indexed component) for
8795 -- subsequent legality tests, e.g. Is_Variable. which examines
8796 -- the original node.
438ff97c
ES
8797
8798 if Nkind (Node_To_Replace) = N_Function_Call then
8799 Set_Etype
8800 (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
8801 end if;
0a36105d 8802 end if;
4c46b835
AC
8803 end Complete_Object_Operation;
8804
0a36105d
JM
8805 ----------------------
8806 -- Report_Ambiguity --
8807 ----------------------
8808
8809 procedure Report_Ambiguity (Op : Entity_Id) is
0a36105d
JM
8810 Access_Actual : constant Boolean :=
8811 Is_Access_Type (Etype (Prefix (N)));
8cf23b91 8812 Access_Formal : Boolean := False;
0a36105d
JM
8813
8814 begin
8815 Error_Msg_Sloc := Sloc (Op);
8816
8cf23b91
AC
8817 if Present (First_Formal (Op)) then
8818 Access_Formal := Is_Access_Type (Etype (First_Formal (Op)));
8819 end if;
8820
0a36105d
JM
8821 if Access_Formal and then not Access_Actual then
8822 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
ed2233dc 8823 Error_Msg_N
8b4230c8
AC
8824 ("\possible interpretation "
8825 & "(inherited, with implicit 'Access) #", N);
0a36105d 8826 else
ed2233dc 8827 Error_Msg_N
0a36105d
JM
8828 ("\possible interpretation (with implicit 'Access) #", N);
8829 end if;
8830
8831 elsif not Access_Formal and then Access_Actual then
8832 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
ed2233dc 8833 Error_Msg_N
8b4230c8 8834 ("\possible interpretation "
28e18b4f 8835 & "(inherited, with implicit dereference) #", N);
0a36105d 8836 else
ed2233dc 8837 Error_Msg_N
0a36105d
JM
8838 ("\possible interpretation (with implicit dereference) #", N);
8839 end if;
8840
8841 else
8842 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
ed2233dc 8843 Error_Msg_N ("\possible interpretation (inherited)#", N);
0a36105d 8844 else
4e7a4f6e
AC
8845 Error_Msg_N -- CODEFIX
8846 ("\possible interpretation#", N);
0a36105d
JM
8847 end if;
8848 end if;
8849 end Report_Ambiguity;
8850
4c46b835
AC
8851 --------------------------------
8852 -- Transform_Object_Operation --
8853 --------------------------------
8854
8855 procedure Transform_Object_Operation
8856 (Call_Node : out Node_Id;
0a36105d 8857 Node_To_Replace : out Node_Id)
35ae2ed8 8858 is
ec6078e3
ES
8859 Dummy : constant Node_Id := New_Copy (Obj);
8860 -- Placeholder used as a first parameter in the call, replaced
8861 -- eventually by the proper object.
8862
d469eabe
HK
8863 Parent_Node : constant Node_Id := Parent (N);
8864
ec6078e3 8865 Actual : Node_Id;
d469eabe 8866 Actuals : List_Id;
ec6078e3 8867
35ae2ed8 8868 begin
40b4bc2d
AC
8869 -- Obj may already have been rewritten if it involves an implicit
8870 -- dereference (e.g. if it is an access to a limited view). Preserve
8871 -- a link to the original node for ASIS use.
8872
8873 if not Comes_From_Source (Obj) then
8874 Set_Original_Node (Dummy, Original_Node (Obj));
8875 end if;
8876
ec6078e3
ES
8877 -- Common case covering 1) Call to a procedure and 2) Call to a
8878 -- function that has some additional actuals.
35ae2ed8 8879
d3b00ce3 8880 if Nkind (Parent_Node) in N_Subprogram_Call
35ae2ed8 8881
ec6078e3
ES
8882 -- N is a selected component node containing the name of the
8883 -- subprogram. If N is not the name of the parent node we must
8884 -- not replace the parent node by the new construct. This case
8885 -- occurs when N is a parameterless call to a subprogram that
8886 -- is an actual parameter of a call to another subprogram. For
8887 -- example:
8888 -- Some_Subprogram (..., Obj.Operation, ...)
35ae2ed8 8889
ec6078e3 8890 and then Name (Parent_Node) = N
4c46b835
AC
8891 then
8892 Node_To_Replace := Parent_Node;
35ae2ed8 8893
ec6078e3 8894 Actuals := Parameter_Associations (Parent_Node);
d3e65aad 8895
ec6078e3
ES
8896 if Present (Actuals) then
8897 Prepend (Dummy, Actuals);
8898 else
8899 Actuals := New_List (Dummy);
8900 end if;
4c46b835
AC
8901
8902 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
8903 Call_Node :=
8904 Make_Procedure_Call_Statement (Loc,
48c8c473 8905 Name => New_Copy (Subprog),
4c46b835
AC
8906 Parameter_Associations => Actuals);
8907
8908 else
4c46b835
AC
8909 Call_Node :=
8910 Make_Function_Call (Loc,
8b4230c8 8911 Name => New_Copy (Subprog),
4c46b835 8912 Parameter_Associations => Actuals);
35ae2ed8
AC
8913 end if;
8914
d469eabe 8915 -- Before analysis, a function call appears as an indexed component
ec6078e3 8916 -- if there are no named associations.
758c442c 8917
c8307596 8918 elsif Nkind (Parent_Node) = N_Indexed_Component
ec6078e3
ES
8919 and then N = Prefix (Parent_Node)
8920 then
758c442c 8921 Node_To_Replace := Parent_Node;
ec6078e3
ES
8922 Actuals := Expressions (Parent_Node);
8923
8924 Actual := First (Actuals);
8925 while Present (Actual) loop
8926 Analyze (Actual);
8927 Next (Actual);
8928 end loop;
8929
8930 Prepend (Dummy, Actuals);
758c442c
GD
8931
8932 Call_Node :=
8933 Make_Function_Call (Loc,
8b4230c8 8934 Name => New_Copy (Subprog),
758c442c
GD
8935 Parameter_Associations => Actuals);
8936
d469eabe 8937 -- Parameterless call: Obj.F is rewritten as F (Obj)
35ae2ed8 8938
4c46b835
AC
8939 else
8940 Node_To_Replace := N;
8941
8942 Call_Node :=
8943 Make_Function_Call (Loc,
8b4230c8 8944 Name => New_Copy (Subprog),
ec6078e3 8945 Parameter_Associations => New_List (Dummy));
4c46b835
AC
8946 end if;
8947 end Transform_Object_Operation;
35ae2ed8
AC
8948
8949 ------------------------------
8950 -- Try_Class_Wide_Operation --
8951 ------------------------------
8952
8953 function Try_Class_Wide_Operation
4c46b835
AC
8954 (Call_Node : Node_Id;
8955 Node_To_Replace : Node_Id) return Boolean
35ae2ed8 8956 is
0a36105d
JM
8957 Anc_Type : Entity_Id;
8958 Matching_Op : Entity_Id := Empty;
8959 Error : Boolean;
8960
8961 procedure Traverse_Homonyms
8962 (Anc_Type : Entity_Id;
8963 Error : out Boolean);
8964 -- Traverse the homonym chain of the subprogram searching for those
8965 -- homonyms whose first formal has the Anc_Type's class-wide type,
d469eabe
HK
8966 -- or an anonymous access type designating the class-wide type. If
8967 -- an ambiguity is detected, then Error is set to True.
0a36105d
JM
8968
8969 procedure Traverse_Interfaces
8970 (Anc_Type : Entity_Id;
8971 Error : out Boolean);
8972 -- Traverse the list of interfaces, if any, associated with Anc_Type
8973 -- and search for acceptable class-wide homonyms associated with each
8974 -- interface. If an ambiguity is detected, then Error is set to True.
8975
8976 -----------------------
8977 -- Traverse_Homonyms --
8978 -----------------------
8979
8980 procedure Traverse_Homonyms
8981 (Anc_Type : Entity_Id;
8982 Error : out Boolean)
8983 is
118f2d8b 8984 function First_Formal_Match
61770974
HK
8985 (Subp_Id : Entity_Id;
8986 Typ : Entity_Id) return Boolean;
8987 -- Predicate to verify that the first foramal of class-wide
8988 -- subprogram Subp_Id matches type Typ of the prefix.
118f2d8b
ES
8989
8990 ------------------------
8991 -- First_Formal_Match --
8992 ------------------------
8993
8994 function First_Formal_Match
61770974
HK
8995 (Subp_Id : Entity_Id;
8996 Typ : Entity_Id) return Boolean
118f2d8b 8997 is
61770974
HK
8998 Ctrl : constant Entity_Id := First_Formal (Subp_Id);
8999
118f2d8b 9000 begin
61770974
HK
9001 return
9002 Present (Ctrl)
9003 and then
9004 (Base_Type (Etype (Ctrl)) = Typ
9005 or else
9006 (Ekind (Etype (Ctrl)) = E_Anonymous_Access_Type
9007 and then
9008 Base_Type (Designated_Type (Etype (Ctrl))) =
9009 Typ));
118f2d8b
ES
9010 end First_Formal_Match;
9011
61770974 9012 -- Local variables
ec6078e3 9013
61770974 9014 CW_Typ : constant Entity_Id := Class_Wide_Type (Anc_Type);
b67a385c 9015
61770974
HK
9016 Candidate : Entity_Id;
9017 -- If homonym is a renaming, examine the renamed program
9018
9019 Hom : Entity_Id;
9020 Hom_Ref : Node_Id;
9021 Success : Boolean;
9022
9023 -- Start of processing for Traverse_Homonyms
9024
9025 begin
9026 Error := False;
401093c1 9027
383e179e
AC
9028 -- Find a non-hidden operation whose first parameter is of the
9029 -- class-wide type, a subtype thereof, or an anonymous access
a68d415b 9030 -- to same. If in an instance, the operation can be considered
8b4230c8
AC
9031 -- even if hidden (it may be hidden because the instantiation
9032 -- is expanded after the containing package has been analyzed).
3bb9bd7d
ES
9033 -- If the subprogram is a generic actual in an enclosing instance,
9034 -- it appears as a renaming that is a candidate interpretation as
9035 -- well.
401093c1 9036
61770974 9037 Hom := Current_Entity (Subprog);
35ae2ed8 9038 while Present (Hom) loop
6a2e4f0b 9039 if Ekind_In (Hom, E_Procedure, E_Function)
118f2d8b
ES
9040 and then Present (Renamed_Entity (Hom))
9041 and then Is_Generic_Actual_Subprogram (Hom)
3bb9bd7d 9042 and then In_Open_Scopes (Scope (Hom))
118f2d8b
ES
9043 then
9044 Candidate := Renamed_Entity (Hom);
9045 else
9046 Candidate := Hom;
9047 end if;
9048
61770974 9049 if Ekind_In (Candidate, E_Function, E_Procedure)
118f2d8b
ES
9050 and then (not Is_Hidden (Candidate) or else In_Instance)
9051 and then Scope (Candidate) = Scope (Base_Type (Anc_Type))
61770974 9052 and then First_Formal_Match (Candidate, CW_Typ)
35ae2ed8 9053 then
88f47280
AC
9054 -- If the context is a procedure call, ignore functions
9055 -- in the name of the call.
9056
118f2d8b 9057 if Ekind (Candidate) = E_Function
88f47280
AC
9058 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
9059 and then N = Name (Parent (N))
9060 then
9061 goto Next_Hom;
11fa950b
AC
9062
9063 -- If the context is a function call, ignore procedures
9064 -- in the name of the call.
9065
118f2d8b 9066 elsif Ekind (Candidate) = E_Procedure
11fa950b
AC
9067 and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
9068 then
9069 goto Next_Hom;
88f47280
AC
9070 end if;
9071
61770974 9072 Set_Etype (Call_Node, Any_Type);
0a36105d
JM
9073 Set_Is_Overloaded (Call_Node, False);
9074 Success := False;
4c46b835 9075
0a36105d 9076 if No (Matching_Op) then
118f2d8b 9077 Hom_Ref := New_Occurrence_Of (Candidate, Sloc (Subprog));
4c46b835 9078
61770974
HK
9079 Set_Etype (Call_Node, Any_Type);
9080 Set_Name (Call_Node, Hom_Ref);
9081 Set_Parent (Call_Node, Parent (Node_To_Replace));
4c46b835 9082
0a36105d
JM
9083 Analyze_One_Call
9084 (N => Call_Node,
118f2d8b 9085 Nam => Candidate,
0a36105d
JM
9086 Report => Report_Error,
9087 Success => Success,
9088 Skip_First => True);
4c46b835 9089
0a36105d 9090 Matching_Op :=
118f2d8b 9091 Valid_Candidate (Success, Call_Node, Candidate);
4c46b835 9092
0a36105d
JM
9093 else
9094 Analyze_One_Call
9095 (N => Call_Node,
118f2d8b 9096 Nam => Candidate,
0a36105d
JM
9097 Report => Report_Error,
9098 Success => Success,
9099 Skip_First => True);
9100
15529d0a
PMR
9101 -- The same operation may be encountered on two homonym
9102 -- traversals, before and after looking at interfaces.
9103 -- Check for this case before reporting a real ambiguity.
9104
118f2d8b 9105 if Present
61770974 9106 (Valid_Candidate (Success, Call_Node, Candidate))
0a36105d 9107 and then Nkind (Call_Node) /= N_Function_Call
118f2d8b 9108 and then Candidate /= Matching_Op
0a36105d 9109 then
ed2233dc 9110 Error_Msg_NE ("ambiguous call to&", N, Hom);
0a36105d
JM
9111 Report_Ambiguity (Matching_Op);
9112 Report_Ambiguity (Hom);
9113 Error := True;
9114 return;
9115 end if;
35ae2ed8
AC
9116 end if;
9117 end if;
9118
88f47280
AC
9119 <<Next_Hom>>
9120 Hom := Homonym (Hom);
35ae2ed8 9121 end loop;
0a36105d
JM
9122 end Traverse_Homonyms;
9123
9124 -------------------------
9125 -- Traverse_Interfaces --
9126 -------------------------
35ae2ed8 9127
0a36105d
JM
9128 procedure Traverse_Interfaces
9129 (Anc_Type : Entity_Id;
9130 Error : out Boolean)
9131 is
0a36105d
JM
9132 Intface_List : constant List_Id :=
9133 Abstract_Interface_List (Anc_Type);
d469eabe 9134 Intface : Node_Id;
0a36105d
JM
9135
9136 begin
9137 Error := False;
9138
9139 if Is_Non_Empty_List (Intface_List) then
9140 Intface := First (Intface_List);
9141 while Present (Intface) loop
9142
9143 -- Look for acceptable class-wide homonyms associated with
9144 -- the interface.
9145
9146 Traverse_Homonyms (Etype (Intface), Error);
9147
9148 if Error then
9149 return;
9150 end if;
9151
9152 -- Continue the search by looking at each of the interface's
9153 -- associated interface ancestors.
9154
9155 Traverse_Interfaces (Etype (Intface), Error);
9156
9157 if Error then
9158 return;
9159 end if;
9160
9161 Next (Intface);
9162 end loop;
9163 end if;
9164 end Traverse_Interfaces;
9165
9166 -- Start of processing for Try_Class_Wide_Operation
9167
9168 begin
8cf23b91
AC
9169 -- If we are searching only for conflicting class-wide subprograms
9170 -- then initialize directly Matching_Op with the target entity.
9171
9172 if CW_Test_Only then
9173 Matching_Op := Entity (Selector_Name (N));
9174 end if;
9175
d469eabe
HK
9176 -- Loop through ancestor types (including interfaces), traversing
9177 -- the homonym chain of the subprogram, trying out those homonyms
9178 -- whose first formal has the class-wide type of the ancestor, or
9179 -- an anonymous access type designating the class-wide type.
0a36105d
JM
9180
9181 Anc_Type := Obj_Type;
9182 loop
9183 -- Look for a match among homonyms associated with the ancestor
9184
9185 Traverse_Homonyms (Anc_Type, Error);
9186
9187 if Error then
9188 return True;
9189 end if;
9190
9191 -- Continue the search for matches among homonyms associated with
9192 -- any interfaces implemented by the ancestor.
9193
9194 Traverse_Interfaces (Anc_Type, Error);
9195
9196 if Error then
9197 return True;
9198 end if;
35ae2ed8 9199
4c46b835
AC
9200 exit when Etype (Anc_Type) = Anc_Type;
9201 Anc_Type := Etype (Anc_Type);
35ae2ed8
AC
9202 end loop;
9203
0a36105d
JM
9204 if Present (Matching_Op) then
9205 Set_Etype (Call_Node, Etype (Matching_Op));
9206 end if;
ec6078e3 9207
0a36105d 9208 return Present (Matching_Op);
35ae2ed8
AC
9209 end Try_Class_Wide_Operation;
9210
0a36105d
JM
9211 -----------------------------------
9212 -- Try_One_Prefix_Interpretation --
9213 -----------------------------------
9214
9215 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
48c8c473 9216 Prev_Obj_Type : constant Entity_Id := Obj_Type;
e3d9f448
AC
9217 -- If the interpretation does not have a valid candidate type,
9218 -- preserve current value of Obj_Type for subsequent errors.
9219
0a36105d
JM
9220 begin
9221 Obj_Type := T;
9222
9223 if Is_Access_Type (Obj_Type) then
9224 Obj_Type := Designated_Type (Obj_Type);
9225 end if;
9226
48c8c473
AC
9227 if Ekind_In (Obj_Type, E_Private_Subtype,
9228 E_Record_Subtype_With_Private)
9229 then
0a36105d
JM
9230 Obj_Type := Base_Type (Obj_Type);
9231 end if;
9232
9233 if Is_Class_Wide_Type (Obj_Type) then
9234 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
9235 end if;
9236
9237 -- The type may have be obtained through a limited_with clause,
9238 -- in which case the primitive operations are available on its
a316b3fc 9239 -- nonlimited view. If still incomplete, retrieve full view.
0a36105d
JM
9240
9241 if Ekind (Obj_Type) = E_Incomplete_Type
7b56a91b 9242 and then From_Limited_With (Obj_Type)
47346923 9243 and then Has_Non_Limited_View (Obj_Type)
0a36105d 9244 then
401093c1 9245 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
0a36105d
JM
9246 end if;
9247
9248 -- If the object is not tagged, or the type is still an incomplete
48c8c473
AC
9249 -- type, this is not a prefixed call. Restore the previous type as
9250 -- the current one is not a legal candidate.
0a36105d
JM
9251
9252 if not Is_Tagged_Type (Obj_Type)
9253 or else Is_Incomplete_Type (Obj_Type)
9254 then
e3d9f448 9255 Obj_Type := Prev_Obj_Type;
0a36105d
JM
9256 return;
9257 end if;
9258
11fa950b
AC
9259 declare
9260 Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node);
5612989e
PMR
9261 Ignore : Boolean;
9262 Prim_Result : Boolean := False;
11fa950b
AC
9263
9264 begin
8cf23b91
AC
9265 if not CW_Test_Only then
9266 Prim_Result :=
9267 Try_Primitive_Operation
9268 (Call_Node => New_Call_Node,
9269 Node_To_Replace => Node_To_Replace);
9270 end if;
11fa950b
AC
9271
9272 -- Check if there is a class-wide subprogram covering the
9273 -- primitive. This check must be done even if a candidate
9274 -- was found in order to report ambiguous calls.
9275
48c8c473 9276 if not Prim_Result then
5612989e 9277 Ignore :=
11fa950b
AC
9278 Try_Class_Wide_Operation
9279 (Call_Node => New_Call_Node,
9280 Node_To_Replace => Node_To_Replace);
9281
9282 -- If we found a primitive we search for class-wide subprograms
9283 -- using a duplicate of the call node (done to avoid missing its
9284 -- decoration if there is no ambiguity).
9285
9286 else
5612989e 9287 Ignore :=
11fa950b
AC
9288 Try_Class_Wide_Operation
9289 (Call_Node => Dup_Call_Node,
9290 Node_To_Replace => Node_To_Replace);
9291 end if;
9292 end;
0a36105d
JM
9293 end Try_One_Prefix_Interpretation;
9294
4c46b835
AC
9295 -----------------------------
9296 -- Try_Primitive_Operation --
9297 -----------------------------
35ae2ed8 9298
4c46b835
AC
9299 function Try_Primitive_Operation
9300 (Call_Node : Node_Id;
9301 Node_To_Replace : Node_Id) return Boolean
35ae2ed8 9302 is
6e73e3ab
AC
9303 Elmt : Elmt_Id;
9304 Prim_Op : Entity_Id;
0a36105d
JM
9305 Matching_Op : Entity_Id := Empty;
9306 Prim_Op_Ref : Node_Id := Empty;
9307
8b4230c8 9308 Corr_Type : Entity_Id := Empty;
0a36105d
JM
9309 -- If the prefix is a synchronized type, the controlling type of
9310 -- the primitive operation is the corresponding record type, else
9311 -- this is the object type itself.
9312
8b4230c8 9313 Success : Boolean := False;
35ae2ed8 9314
401093c1
ES
9315 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
9316 -- For tagged types the candidate interpretations are found in
9317 -- the list of primitive operations of the type and its ancestors.
9318 -- For formal tagged types we have to find the operations declared
9319 -- in the same scope as the type (including in the generic formal
9320 -- part) because the type itself carries no primitive operations,
9321 -- except for formal derived types that inherit the operations of
9322 -- the parent and progenitors.
8b4230c8 9323 --
d469eabe
HK
9324 -- If the context is a generic subprogram body, the generic formals
9325 -- are visible by name, but are not in the entity list of the
9326 -- subprogram because that list starts with the subprogram formals.
9327 -- We retrieve the candidate operations from the generic declaration.
401093c1 9328
84dad556
AC
9329 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id;
9330 -- Prefix notation can also be used on operations that are not
9331 -- primitives of the type, but are declared in the same immediate
9332 -- declarative part, which can only mean the corresponding package
0e3a687f 9333 -- body (see RM 4.1.3 (9.2/3)). If we are in that body we extend the
84dad556
AC
9334 -- list of primitives with body operations with the same name that
9335 -- may be candidates, so that Try_Primitive_Operations can examine
9336 -- them if no real primitive is found.
9337
dfcfdc0a
AC
9338 function Is_Private_Overriding (Op : Entity_Id) return Boolean;
9339 -- An operation that overrides an inherited operation in the private
9340 -- part of its package may be hidden, but if the inherited operation
9341 -- is visible a direct call to it will dispatch to the private one,
9342 -- which is therefore a valid candidate.
9343
42f11e4c
AC
9344 function Names_Match
9345 (Obj_Type : Entity_Id;
9346 Prim_Op : Entity_Id;
9347 Subprog : Entity_Id) return Boolean;
9348 -- Return True if the names of Prim_Op and Subprog match. If Obj_Type
9349 -- is a protected type then compare also the original name of Prim_Op
9350 -- with the name of Subprog (since the expander may have added a
9351 -- prefix to its original name --see Exp_Ch9.Build_Selected_Name).
9352
ec6078e3
ES
9353 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
9354 -- Verify that the prefix, dereferenced if need be, is a valid
9355 -- controlling argument in a call to Op. The remaining actuals
9356 -- are checked in the subsequent call to Analyze_One_Call.
35ae2ed8 9357
401093c1
ES
9358 ------------------------------
9359 -- Collect_Generic_Type_Ops --
9360 ------------------------------
9361
9362 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
9363 Bas : constant Entity_Id := Base_Type (T);
9364 Candidates : constant Elist_Id := New_Elmt_List;
9365 Subp : Entity_Id;
9366 Formal : Entity_Id;
9367
d469eabe
HK
9368 procedure Check_Candidate;
9369 -- The operation is a candidate if its first parameter is a
9370 -- controlling operand of the desired type.
9371
9372 -----------------------
9373 -- Check_Candidate; --
9374 -----------------------
9375
9376 procedure Check_Candidate is
9377 begin
9378 Formal := First_Formal (Subp);
9379
9380 if Present (Formal)
9381 and then Is_Controlling_Formal (Formal)
9382 and then
9383 (Base_Type (Etype (Formal)) = Bas
9384 or else
9385 (Is_Access_Type (Etype (Formal))
9386 and then Designated_Type (Etype (Formal)) = Bas))
9387 then
9388 Append_Elmt (Subp, Candidates);
9389 end if;
9390 end Check_Candidate;
9391
9392 -- Start of processing for Collect_Generic_Type_Ops
9393
401093c1
ES
9394 begin
9395 if Is_Derived_Type (T) then
9396 return Primitive_Operations (T);
9397
bce79204
AC
9398 elsif Ekind_In (Scope (T), E_Procedure, E_Function) then
9399
d469eabe
HK
9400 -- Scan the list of generic formals to find subprograms
9401 -- that may have a first controlling formal of the type.
9402
8b4230c8
AC
9403 if Nkind (Unit_Declaration_Node (Scope (T))) =
9404 N_Generic_Subprogram_Declaration
bb10b891
AC
9405 then
9406 declare
9407 Decl : Node_Id;
9408
9409 begin
9410 Decl :=
9411 First (Generic_Formal_Declarations
9412 (Unit_Declaration_Node (Scope (T))));
9413 while Present (Decl) loop
9414 if Nkind (Decl) in N_Formal_Subprogram_Declaration then
9415 Subp := Defining_Entity (Decl);
9416 Check_Candidate;
9417 end if;
d469eabe 9418
bb10b891
AC
9419 Next (Decl);
9420 end loop;
9421 end;
9422 end if;
d469eabe
HK
9423 return Candidates;
9424
401093c1
ES
9425 else
9426 -- Scan the list of entities declared in the same scope as
9427 -- the type. In general this will be an open scope, given that
9428 -- the call we are analyzing can only appear within a generic
9429 -- declaration or body (either the one that declares T, or a
9430 -- child unit).
9431
bb10b891
AC
9432 -- For a subtype representing a generic actual type, go to the
9433 -- base type.
9434
9435 if Is_Generic_Actual_Type (T) then
9436 Subp := First_Entity (Scope (Base_Type (T)));
9437 else
9438 Subp := First_Entity (Scope (T));
9439 end if;
9440
401093c1
ES
9441 while Present (Subp) loop
9442 if Is_Overloadable (Subp) then
d469eabe 9443 Check_Candidate;
401093c1
ES
9444 end if;
9445
9446 Next_Entity (Subp);
9447 end loop;
9448
9449 return Candidates;
9450 end if;
9451 end Collect_Generic_Type_Ops;
9452
84dad556
AC
9453 ----------------------------
9454 -- Extended_Primitive_Ops --
9455 ----------------------------
9456
9457 function Extended_Primitive_Ops (T : Entity_Id) return Elist_Id is
9458 Type_Scope : constant Entity_Id := Scope (T);
0e3a687f 9459 Op_List : Elist_Id := Primitive_Operations (T);
84dad556 9460 begin
a92db262 9461 if Is_Package_Or_Generic_Package (Type_Scope)
0e3a687f
BD
9462 and then ((In_Package_Body (Type_Scope)
9463 and then In_Open_Scopes (Type_Scope)) or else In_Instance_Body)
84dad556 9464 then
0e3a687f 9465 -- Retrieve list of declarations of package body if possible
84dad556 9466
0e3a687f
BD
9467 declare
9468 The_Body : constant Node_Id :=
9469 Corresponding_Body (Unit_Declaration_Node (Type_Scope));
9470 begin
9471 if Present (The_Body) then
9472 declare
9473 Body_Decls : constant List_Id :=
9474 Declarations (Unit_Declaration_Node (The_Body));
9475 Op_Found : Boolean := False;
9476 Op : Entity_Id := Current_Entity (Subprog);
9477 begin
9478 while Present (Op) loop
9479 if Comes_From_Source (Op)
9480 and then Is_Overloadable (Op)
9481
9482 -- Exclude overriding primitive operations of a
9483 -- type extension declared in the package body,
9484 -- to prevent duplicates in extended list.
9485
9486 and then not Is_Primitive (Op)
9487 and then Is_List_Member
9488 (Unit_Declaration_Node (Op))
9489 and then List_Containing
9490 (Unit_Declaration_Node (Op)) = Body_Decls
9491 then
9492 if not Op_Found then
9493 -- Copy list of primitives so it is not
9494 -- affected for other uses.
84dad556 9495
0e3a687f
BD
9496 Op_List := New_Copy_Elist (Op_List);
9497 Op_Found := True;
9498 end if;
84dad556 9499
0e3a687f
BD
9500 Append_Elmt (Op, Op_List);
9501 end if;
84dad556 9502
0e3a687f
BD
9503 Op := Homonym (Op);
9504 end loop;
9505 end;
9506 end if;
9507 end;
84dad556
AC
9508 end if;
9509
9510 return Op_List;
9511 end Extended_Primitive_Ops;
9512
dfcfdc0a
AC
9513 ---------------------------
9514 -- Is_Private_Overriding --
9515 ---------------------------
9516
9517 function Is_Private_Overriding (Op : Entity_Id) return Boolean is
40c21e91 9518 Visible_Op : Entity_Id;
dfcfdc0a
AC
9519
9520 begin
40c21e91
PMR
9521 -- The subprogram may be overloaded with both visible and private
9522 -- entities with the same name. We have to scan the chain of
9523 -- homonyms to determine whether there is a previous implicit
9524 -- declaration in the same scope that is overridden by the
9525 -- private candidate.
9526
9527 Visible_Op := Homonym (Op);
9528 while Present (Visible_Op) loop
9529 if Scope (Op) /= Scope (Visible_Op) then
9530 return False;
9531
9532 elsif not Comes_From_Source (Visible_Op)
9533 and then Alias (Visible_Op) = Op
9534 and then not Is_Hidden (Visible_Op)
9535 then
9536 return True;
9537 end if;
9538
9539 Visible_Op := Homonym (Visible_Op);
9540 end loop;
9541
9542 return False;
dfcfdc0a
AC
9543 end Is_Private_Overriding;
9544
42f11e4c
AC
9545 -----------------
9546 -- Names_Match --
9547 -----------------
9548
9549 function Names_Match
9550 (Obj_Type : Entity_Id;
9551 Prim_Op : Entity_Id;
9552 Subprog : Entity_Id) return Boolean is
9553 begin
9554 -- Common case: exact match
9555
9556 if Chars (Prim_Op) = Chars (Subprog) then
9557 return True;
9558
9559 -- For protected type primitives the expander may have built the
9560 -- name of the dispatching primitive prepending the type name to
9561 -- avoid conflicts with the name of the protected subprogram (see
9562 -- Exp_Ch9.Build_Selected_Name).
9563
9564 elsif Is_Protected_Type (Obj_Type) then
bac5ba15
AC
9565 return
9566 Present (Original_Protected_Subprogram (Prim_Op))
9567 and then Chars (Original_Protected_Subprogram (Prim_Op)) =
9568 Chars (Subprog);
118f2d8b
ES
9569
9570 -- In an instance, the selector name may be a generic actual that
9571 -- renames a primitive operation of the type of the prefix.
9572
9573 elsif In_Instance and then Present (Current_Entity (Subprog)) then
9574 declare
9575 Subp : constant Entity_Id := Current_Entity (Subprog);
9576 begin
9577 if Present (Subp)
9578 and then Is_Subprogram (Subp)
9579 and then Present (Renamed_Entity (Subp))
9580 and then Is_Generic_Actual_Subprogram (Subp)
9581 and then Chars (Renamed_Entity (Subp)) = Chars (Prim_Op)
9582 then
9583 return True;
9584 end if;
9585 end;
42f11e4c
AC
9586 end if;
9587
9588 return False;
9589 end Names_Match;
9590
ec6078e3
ES
9591 -----------------------------
9592 -- Valid_First_Argument_Of --
9593 -----------------------------
35ae2ed8 9594
ec6078e3 9595 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
9febb58f 9596 Typ : Entity_Id := Etype (First_Formal (Op));
35ae2ed8 9597
ec6078e3 9598 begin
9febb58f
JM
9599 if Is_Concurrent_Type (Typ)
9600 and then Present (Corresponding_Record_Type (Typ))
9601 then
9602 Typ := Corresponding_Record_Type (Typ);
9603 end if;
9604
9313a26a
AC
9605 -- Simple case. Object may be a subtype of the tagged type or may
9606 -- be the corresponding record of a synchronized type.
5d09245e 9607
aab883ec 9608 return Obj_Type = Typ
d469eabe 9609 or else Base_Type (Obj_Type) = Typ
0a36105d
JM
9610 or else Corr_Type = Typ
9611
913e4b36 9612 -- Object may be of a derived type whose parent has unknown
9313a26a
AC
9613 -- discriminants, in which case the type matches the underlying
9614 -- record view of its base.
913e4b36 9615
9313a26a
AC
9616 or else
9617 (Has_Unknown_Discriminants (Typ)
9618 and then Typ = Underlying_Record_View (Base_Type (Obj_Type)))
913e4b36 9619
0a36105d 9620 -- Prefix can be dereferenced
725e2a15 9621
ec6078e3 9622 or else
0a36105d
JM
9623 (Is_Access_Type (Corr_Type)
9624 and then Designated_Type (Corr_Type) = Typ)
5d09245e 9625
9313a26a
AC
9626 -- Formal is an access parameter, for which the object can
9627 -- provide an access.
35ae2ed8 9628
ec6078e3
ES
9629 or else
9630 (Ekind (Typ) = E_Anonymous_Access_Type
9fde638d
RD
9631 and then
9632 Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
ec6078e3 9633 end Valid_First_Argument_Of;
35ae2ed8 9634
ec6078e3 9635 -- Start of processing for Try_Primitive_Operation
35ae2ed8 9636
ec6078e3 9637 begin
d469eabe 9638 -- Look for subprograms in the list of primitive operations. The name
0a36105d
JM
9639 -- must be identical, and the kind of call indicates the expected
9640 -- kind of operation (function or procedure). If the type is a
d469eabe 9641 -- (tagged) synchronized type, the primitive ops are attached to the
b4592168 9642 -- corresponding record (base) type.
aab883ec
ES
9643
9644 if Is_Concurrent_Type (Obj_Type) then
bb10b891
AC
9645 if Present (Corresponding_Record_Type (Obj_Type)) then
9646 Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
48c8c473 9647 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
bb10b891
AC
9648 else
9649 Corr_Type := Obj_Type;
48c8c473 9650 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
15e4986c
JM
9651 end if;
9652
401093c1 9653 elsif not Is_Generic_Type (Obj_Type) then
0a36105d 9654 Corr_Type := Obj_Type;
48c8c473 9655 Elmt := First_Elmt (Extended_Primitive_Ops (Obj_Type));
401093c1
ES
9656
9657 else
9658 Corr_Type := Obj_Type;
48c8c473 9659 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
aab883ec 9660 end if;
35ae2ed8 9661
ec6078e3
ES
9662 while Present (Elmt) loop
9663 Prim_Op := Node (Elmt);
9664
42f11e4c 9665 if Names_Match (Obj_Type, Prim_Op, Subprog)
ec6078e3
ES
9666 and then Present (First_Formal (Prim_Op))
9667 and then Valid_First_Argument_Of (Prim_Op)
fe45e59e 9668 and then
7415029d 9669 (Nkind (Call_Node) = N_Function_Call)
48c8c473 9670 =
8b4230c8 9671 (Ekind (Prim_Op) = E_Function)
ec6078e3 9672 then
b67a385c 9673 -- Ada 2005 (AI-251): If this primitive operation corresponds
8b4230c8 9674 -- to an immediate ancestor interface there is no need to add
b67a385c
ES
9675 -- it to the list of interpretations; the corresponding aliased
9676 -- primitive is also in this list of primitive operations and
9677 -- will be used instead.
fe45e59e 9678
ce2b6ba5
JM
9679 if (Present (Interface_Alias (Prim_Op))
9680 and then Is_Ancestor (Find_Dispatching_Type
9681 (Alias (Prim_Op)), Corr_Type))
0a36105d 9682
dfcfdc0a
AC
9683 -- Do not consider hidden primitives unless the type is in an
9684 -- open scope or we are within an instance, where visibility
9685 -- is known to be correct, or else if this is an overriding
9686 -- operation in the private part for an inherited operation.
0a36105d 9687
dfcfdc0a
AC
9688 or else (Is_Hidden (Prim_Op)
9689 and then not Is_Immediately_Visible (Obj_Type)
9690 and then not In_Instance
9691 and then not Is_Private_Overriding (Prim_Op))
fe45e59e
ES
9692 then
9693 goto Continue;
9694 end if;
9695
0a36105d
JM
9696 Set_Etype (Call_Node, Any_Type);
9697 Set_Is_Overloaded (Call_Node, False);
9698
9699 if No (Matching_Op) then
e4494292 9700 Prim_Op_Ref := New_Occurrence_Of (Prim_Op, Sloc (Subprog));
b67a385c 9701 Candidate := Prim_Op;
35ae2ed8 9702
fe45e59e 9703 Set_Parent (Call_Node, Parent (Node_To_Replace));
35ae2ed8 9704
fe45e59e 9705 Set_Name (Call_Node, Prim_Op_Ref);
0a36105d 9706 Success := False;
35ae2ed8 9707
fe45e59e
ES
9708 Analyze_One_Call
9709 (N => Call_Node,
9710 Nam => Prim_Op,
b67a385c 9711 Report => Report_Error,
fe45e59e
ES
9712 Success => Success,
9713 Skip_First => True);
35ae2ed8 9714
0a36105d 9715 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
fe45e59e 9716
d469eabe
HK
9717 -- More than one interpretation, collect for subsequent
9718 -- disambiguation. If this is a procedure call and there
9719 -- is another match, report ambiguity now.
0a36105d 9720
d469eabe 9721 else
0a36105d
JM
9722 Analyze_One_Call
9723 (N => Call_Node,
9724 Nam => Prim_Op,
9725 Report => Report_Error,
9726 Success => Success,
9727 Skip_First => True);
fe45e59e 9728
0a36105d
JM
9729 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
9730 and then Nkind (Call_Node) /= N_Function_Call
9731 then
ed2233dc 9732 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
0a36105d
JM
9733 Report_Ambiguity (Matching_Op);
9734 Report_Ambiguity (Prim_Op);
9735 return True;
9736 end if;
4c46b835
AC
9737 end if;
9738 end if;
35ae2ed8 9739
fe45e59e 9740 <<Continue>>
4c46b835
AC
9741 Next_Elmt (Elmt);
9742 end loop;
35ae2ed8 9743
0a36105d
JM
9744 if Present (Matching_Op) then
9745 Set_Etype (Call_Node, Etype (Matching_Op));
fe45e59e
ES
9746 end if;
9747
0a36105d 9748 return Present (Matching_Op);
4c46b835 9749 end Try_Primitive_Operation;
35ae2ed8 9750
48c8c473
AC
9751 ---------------------
9752 -- Valid_Candidate --
9753 ---------------------
9754
9755 function Valid_Candidate
9756 (Success : Boolean;
9757 Call : Node_Id;
9758 Subp : Entity_Id) return Entity_Id
9759 is
9760 Arr_Type : Entity_Id;
9761 Comp_Type : Entity_Id;
9762
9763 begin
9764 -- If the subprogram is a valid interpretation, record it in global
9765 -- variable Subprog, to collect all possible overloadings.
9766
9767 if Success then
9768 if Subp /= Entity (Subprog) then
9769 Add_One_Interp (Subprog, Subp, Etype (Subp));
9770 end if;
9771 end if;
9772
9773 -- If the call may be an indexed call, retrieve component type of
9774 -- resulting expression, and add possible interpretation.
9775
9776 Arr_Type := Empty;
9777 Comp_Type := Empty;
9778
9779 if Nkind (Call) = N_Function_Call
9780 and then Nkind (Parent (N)) = N_Indexed_Component
9781 and then Needs_One_Actual (Subp)
9782 then
9783 if Is_Array_Type (Etype (Subp)) then
9784 Arr_Type := Etype (Subp);
9785
9786 elsif Is_Access_Type (Etype (Subp))
9787 and then Is_Array_Type (Designated_Type (Etype (Subp)))
9788 then
9789 Arr_Type := Designated_Type (Etype (Subp));
9790 end if;
9791 end if;
9792
9793 if Present (Arr_Type) then
9794
9795 -- Verify that the actuals (excluding the object) match the types
9796 -- of the indexes.
9797
9798 declare
9799 Actual : Node_Id;
9800 Index : Node_Id;
9801
9802 begin
9803 Actual := Next (First_Actual (Call));
9804 Index := First_Index (Arr_Type);
9805 while Present (Actual) and then Present (Index) loop
9806 if not Has_Compatible_Type (Actual, Etype (Index)) then
9807 Arr_Type := Empty;
9808 exit;
9809 end if;
9810
9811 Next_Actual (Actual);
9812 Next_Index (Index);
9813 end loop;
9814
9815 if No (Actual)
9816 and then No (Index)
9817 and then Present (Arr_Type)
9818 then
9819 Comp_Type := Component_Type (Arr_Type);
9820 end if;
9821 end;
9822
9823 if Present (Comp_Type)
9824 and then Etype (Subprog) /= Comp_Type
9825 then
9826 Add_One_Interp (Subprog, Subp, Comp_Type);
9827 end if;
9828 end if;
9829
9830 if Etype (Call) /= Any_Type then
9831 return Subp;
9832 else
9833 return Empty;
9834 end if;
9835 end Valid_Candidate;
9836
4c46b835 9837 -- Start of processing for Try_Object_Operation
35ae2ed8 9838
4c46b835 9839 begin
0a36105d 9840 Analyze_Expression (Obj);
ec6078e3 9841
0a36105d 9842 -- Analyze the actuals if node is known to be a subprogram call
28d6470f
JM
9843
9844 if Is_Subprg_Call and then N = Name (Parent (N)) then
9845 Actual := First (Parameter_Associations (Parent (N)));
9846 while Present (Actual) loop
725e2a15 9847 Analyze_Expression (Actual);
28d6470f
JM
9848 Next (Actual);
9849 end loop;
9850 end if;
5d09245e 9851
ec6078e3
ES
9852 -- Build a subprogram call node, using a copy of Obj as its first
9853 -- actual. This is a placeholder, to be replaced by an explicit
9854 -- dereference when needed.
4c46b835 9855
ec6078e3
ES
9856 Transform_Object_Operation
9857 (Call_Node => New_Call_Node,
0a36105d 9858 Node_To_Replace => Node_To_Replace);
4c46b835 9859
ec6078e3 9860 Set_Etype (New_Call_Node, Any_Type);
0a36105d 9861 Set_Etype (Subprog, Any_Type);
ec6078e3 9862 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
4c46b835 9863
0a36105d
JM
9864 if not Is_Overloaded (Obj) then
9865 Try_One_Prefix_Interpretation (Obj_Type);
ec6078e3 9866
0a36105d
JM
9867 else
9868 declare
9869 I : Interp_Index;
9870 It : Interp;
9871 begin
9872 Get_First_Interp (Obj, I, It);
9873 while Present (It.Nam) loop
9874 Try_One_Prefix_Interpretation (It.Typ);
9875 Get_Next_Interp (I, It);
9876 end loop;
9877 end;
9878 end if;
9879
9880 if Etype (New_Call_Node) /= Any_Type then
8cf23b91
AC
9881
9882 -- No need to complete the tree transformations if we are only
9883 -- searching for conflicting class-wide subprograms
9884
9885 if CW_Test_Only then
9886 return False;
9887 else
9888 Complete_Object_Operation
9889 (Call_Node => New_Call_Node,
9890 Node_To_Replace => Node_To_Replace);
9891 return True;
9892 end if;
b67a385c
ES
9893
9894 elsif Present (Candidate) then
9895
9896 -- The argument list is not type correct. Re-analyze with error
9897 -- reporting enabled, and use one of the possible candidates.
d469eabe 9898 -- In All_Errors_Mode, re-analyze all failed interpretations.
b67a385c
ES
9899
9900 if All_Errors_Mode then
9901 Report_Error := True;
9902 if Try_Primitive_Operation
8b4230c8
AC
9903 (Call_Node => New_Call_Node,
9904 Node_To_Replace => Node_To_Replace)
b67a385c
ES
9905
9906 or else
9907 Try_Class_Wide_Operation
9908 (Call_Node => New_Call_Node,
9909 Node_To_Replace => Node_To_Replace)
9910 then
9911 null;
9912 end if;
9913
9914 else
9915 Analyze_One_Call
9916 (N => New_Call_Node,
9917 Nam => Candidate,
9918 Report => True,
9919 Success => Success,
9920 Skip_First => True);
9921 end if;
9922
d469eabe
HK
9923 -- No need for further errors
9924
9925 return True;
b67a385c
ES
9926
9927 else
9928 -- There was no candidate operation, so report it as an error
9929 -- in the caller: Analyze_Selected_Component.
9930
9931 return False;
9932 end if;
35ae2ed8
AC
9933 end Try_Object_Operation;
9934
b4592168
GD
9935 ---------
9936 -- wpo --
9937 ---------
9938
9939 procedure wpo (T : Entity_Id) is
9940 Op : Entity_Id;
9941 E : Elmt_Id;
9942
9943 begin
9944 if not Is_Tagged_Type (T) then
9945 return;
9946 end if;
9947
9948 E := First_Elmt (Primitive_Operations (Base_Type (T)));
9949 while Present (E) loop
9950 Op := Node (E);
9951 Write_Int (Int (Op));
9952 Write_Str (" === ");
9953 Write_Name (Chars (Op));
9954 Write_Str (" in ");
9955 Write_Name (Chars (Scope (Op)));
9956 Next_Elmt (E);
9957 Write_Eol;
9958 end loop;
9959 end wpo;
9960
996ae0b0 9961end Sem_Ch4;