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