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