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