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