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