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