]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_ch4.adb
vms_conv.adb (Process_Argument): Ensure that project related options are not put...
[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-- --
0a36105d 9-- Copyright (C) 1992-2007, 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- --
13-- ware Foundation; either version 2, or (at your option) any later ver- --
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 --
18-- Public License distributed with GNAT; see file COPYING. If not, write --
cb5fee25
KC
19-- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20-- Boston, MA 02110-1301, USA. --
996ae0b0
RK
21-- --
22-- GNAT was originally developed by the GNAT team at New York University. --
71ff80dc 23-- Extensive contributions were provided by Ada Core Technologies Inc. --
996ae0b0
RK
24-- --
25------------------------------------------------------------------------------
26
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;
38with Nlists; use Nlists;
39with Nmake; use Nmake;
40with Opt; use Opt;
41with Output; use Output;
42with Restrict; use Restrict;
6e937c1c 43with Rident; use Rident;
996ae0b0
RK
44with Sem; use Sem;
45with Sem_Cat; use Sem_Cat;
46with Sem_Ch3; use Sem_Ch3;
47with Sem_Ch8; use Sem_Ch8;
b67a385c 48with Sem_Disp; use Sem_Disp;
996ae0b0
RK
49with Sem_Dist; use Sem_Dist;
50with Sem_Eval; use Sem_Eval;
51with Sem_Res; use Sem_Res;
52with Sem_Util; use Sem_Util;
53with Sem_Type; use Sem_Type;
54with Stand; use Stand;
55with Sinfo; use Sinfo;
56with Snames; use Snames;
57with Tbuild; use Tbuild;
58
59with GNAT.Spelling_Checker; use GNAT.Spelling_Checker;
60
61package body Sem_Ch4 is
62
63 -----------------------
64 -- Local Subprograms --
65 -----------------------
66
67 procedure Analyze_Expression (N : Node_Id);
68 -- For expressions that are not names, this is just a call to analyze.
69 -- If the expression is a name, it may be a call to a parameterless
70 -- function, and if so must be converted into an explicit call node
71 -- and analyzed as such. This deproceduring must be done during the first
72 -- pass of overload resolution, because otherwise a procedure call with
73 -- overloaded actuals may fail to resolve. See 4327-001 for an example.
74
75 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id);
76 -- Analyze a call of the form "+"(x, y), etc. The prefix of the call
77 -- is an operator name or an expanded name whose selector is an operator
78 -- name, and one possible interpretation is as a predefined operator.
79
80 procedure Analyze_Overloaded_Selected_Component (N : Node_Id);
81 -- If the prefix of a selected_component is overloaded, the proper
82 -- interpretation that yields a record type with the proper selector
83 -- name must be selected.
84
85 procedure Analyze_User_Defined_Binary_Op (N : Node_Id; Op_Id : Entity_Id);
86 -- Procedure to analyze a user defined binary operator, which is resolved
87 -- like a function, but instead of a list of actuals it is presented
88 -- with the left and right operands of an operator node.
89
90 procedure Analyze_User_Defined_Unary_Op (N : Node_Id; Op_Id : Entity_Id);
91 -- Procedure to analyze a user defined unary operator, which is resolved
92 -- like a function, but instead of a list of actuals, it is presented with
93 -- the operand of the operator node.
94
95 procedure Ambiguous_Operands (N : Node_Id);
96 -- for equality, membership, and comparison operators with overloaded
97 -- arguments, list possible interpretations.
98
996ae0b0 99 procedure Analyze_One_Call
ec6078e3
ES
100 (N : Node_Id;
101 Nam : Entity_Id;
102 Report : Boolean;
103 Success : out Boolean;
104 Skip_First : Boolean := False);
996ae0b0
RK
105 -- Check one interpretation of an overloaded subprogram name for
106 -- compatibility with the types of the actuals in a call. If there is a
107 -- single interpretation which does not match, post error if Report is
108 -- set to True.
109 --
110 -- Nam is the entity that provides the formals against which the actuals
111 -- are checked. Nam is either the name of a subprogram, or the internal
112 -- subprogram type constructed for an access_to_subprogram. If the actuals
113 -- are compatible with Nam, then Nam is added to the list of candidate
114 -- interpretations for N, and Success is set to True.
ec6078e3
ES
115 --
116 -- The flag Skip_First is used when analyzing a call that was rewritten
117 -- from object notation. In this case the first actual may have to receive
118 -- an explicit dereference, depending on the first formal of the operation
119 -- being called. The caller will have verified that the object is legal
120 -- for the call. If the remaining parameters match, the first parameter
121 -- will rewritten as a dereference if needed, prior to completing analysis.
996ae0b0
RK
122
123 procedure Check_Misspelled_Selector
124 (Prefix : Entity_Id;
125 Sel : Node_Id);
126 -- Give possible misspelling diagnostic if Sel is likely to be
127 -- a misspelling of one of the selectors of the Prefix.
128 -- This is called by Analyze_Selected_Component after producing
129 -- an invalid selector error message.
130
131 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean;
132 -- Verify that type T is declared in scope S. Used to find intepretations
133 -- for operators given by expanded names. This is abstracted as a separate
134 -- function to handle extensions to System, where S is System, but T is
135 -- declared in the extension.
136
137 procedure Find_Arithmetic_Types
138 (L, R : Node_Id;
139 Op_Id : Entity_Id;
140 N : Node_Id);
141 -- L and R are the operands of an arithmetic operator. Find
142 -- consistent pairs of interpretations for L and R that have a
143 -- numeric type consistent with the semantics of the operator.
144
145 procedure Find_Comparison_Types
146 (L, R : Node_Id;
147 Op_Id : Entity_Id;
148 N : Node_Id);
149 -- L and R are operands of a comparison operator. Find consistent
150 -- pairs of interpretations for L and R.
151
152 procedure Find_Concatenation_Types
153 (L, R : Node_Id;
154 Op_Id : Entity_Id;
155 N : Node_Id);
6e73e3ab 156 -- For the four varieties of concatenation
996ae0b0
RK
157
158 procedure Find_Equality_Types
159 (L, R : Node_Id;
160 Op_Id : Entity_Id;
161 N : Node_Id);
6e73e3ab 162 -- Ditto for equality operators
996ae0b0
RK
163
164 procedure Find_Boolean_Types
165 (L, R : Node_Id;
166 Op_Id : Entity_Id;
167 N : Node_Id);
6e73e3ab 168 -- Ditto for binary logical operations
996ae0b0
RK
169
170 procedure Find_Negation_Types
171 (R : Node_Id;
172 Op_Id : Entity_Id;
173 N : Node_Id);
6e73e3ab 174 -- Find consistent interpretation for operand of negation operator
996ae0b0
RK
175
176 procedure Find_Non_Universal_Interpretations
177 (N : Node_Id;
178 R : Node_Id;
179 Op_Id : Entity_Id;
180 T1 : Entity_Id);
181 -- For equality and comparison operators, the result is always boolean,
182 -- and the legality of the operation is determined from the visibility
183 -- of the operand types. If one of the operands has a universal interpre-
184 -- tation, the legality check uses some compatible non-universal
185 -- interpretation of the other operand. N can be an operator node, or
186 -- a function call whose name is an operator designator.
187
188 procedure Find_Unary_Types
189 (R : Node_Id;
190 Op_Id : Entity_Id;
191 N : Node_Id);
6e73e3ab 192 -- Unary arithmetic types: plus, minus, abs
996ae0b0
RK
193
194 procedure Check_Arithmetic_Pair
195 (T1, T2 : Entity_Id;
196 Op_Id : Entity_Id;
197 N : Node_Id);
198 -- Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid
199 -- types for left and right operand. Determine whether they constitute
200 -- a valid pair for the given operator, and record the corresponding
201 -- interpretation of the operator node. The node N may be an operator
202 -- node (the usual case) or a function call whose prefix is an operator
401093c1 203 -- designator. In both cases Op_Id is the operator name itself.
996ae0b0
RK
204
205 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
206 -- Give detailed information on overloaded call where none of the
207 -- interpretations match. N is the call node, Nam the designator for
208 -- the overloaded entity being called.
209
210 function Junk_Operand (N : Node_Id) return Boolean;
211 -- Test for an operand that is an inappropriate entity (e.g. a package
212 -- name or a label). If so, issue an error message and return True. If
213 -- the operand is not an inappropriate entity kind, return False.
214
215 procedure Operator_Check (N : Node_Id);
da709d08
AC
216 -- Verify that an operator has received some valid interpretation. If none
217 -- was found, determine whether a use clause would make the operation
218 -- legal. The variable Candidate_Type (defined in Sem_Type) is set for
219 -- every type compatible with the operator, even if the operator for the
220 -- type is not directly visible. The routine uses this type to emit a more
221 -- informative message.
996ae0b0 222
6e73e3ab 223 procedure Process_Implicit_Dereference_Prefix
da709d08
AC
224 (E : Entity_Id;
225 P : Node_Id);
226 -- Called when P is the prefix of an implicit dereference, denoting an
1a8fae99
ES
227 -- object E. If in semantics only mode (-gnatc or generic), record that is
228 -- a reference to E. Normally, such a reference is generated only when the
da709d08
AC
229 -- implicit dereference is expanded into an explicit one. E may be empty,
230 -- in which case this procedure does nothing.
6e73e3ab 231
30c20106
AC
232 procedure Remove_Abstract_Operations (N : Node_Id);
233 -- Ada 2005: implementation of AI-310. An abstract non-dispatching
234 -- operation is not a candidate interpretation.
235
996ae0b0 236 function Try_Indexed_Call
aab883ec
ES
237 (N : Node_Id;
238 Nam : Entity_Id;
239 Typ : Entity_Id;
240 Skip_First : Boolean) return Boolean;
241 -- If a function has defaults for all its actuals, a call to it may in fact
242 -- be an indexing on the result of the call. Try_Indexed_Call attempts the
243 -- interpretation as an indexing, prior to analysis as a call. If both are
244 -- possible, the node is overloaded with both interpretations (same symbol
245 -- but two different types). If the call is written in prefix form, the
246 -- prefix becomes the first parameter in the call, and only the remaining
247 -- actuals must be checked for the presence of defaults.
996ae0b0
RK
248
249 function Try_Indirect_Call
91b1417d
AC
250 (N : Node_Id;
251 Nam : Entity_Id;
252 Typ : Entity_Id) return Boolean;
aab883ec
ES
253 -- Similarly, a function F that needs no actuals can return an access to a
254 -- subprogram, and the call F (X) interpreted as F.all (X). In this case
255 -- the call may be overloaded with both interpretations.
996ae0b0 256
35ae2ed8 257 function Try_Object_Operation (N : Node_Id) return Boolean;
aab883ec 258 -- Ada 2005 (AI-252): Support the object.operation notation
35ae2ed8 259
996ae0b0
RK
260 ------------------------
261 -- Ambiguous_Operands --
262 ------------------------
263
264 procedure Ambiguous_Operands (N : Node_Id) is
fbf5a39b 265 procedure List_Operand_Interps (Opnd : Node_Id);
996ae0b0 266
4c46b835
AC
267 --------------------------
268 -- List_Operand_Interps --
269 --------------------------
270
fbf5a39b 271 procedure List_Operand_Interps (Opnd : Node_Id) is
996ae0b0
RK
272 Nam : Node_Id;
273 Err : Node_Id := N;
274
275 begin
276 if Is_Overloaded (Opnd) then
277 if Nkind (Opnd) in N_Op then
278 Nam := Opnd;
996ae0b0
RK
279 elsif Nkind (Opnd) = N_Function_Call then
280 Nam := Name (Opnd);
996ae0b0
RK
281 else
282 return;
283 end if;
284
285 else
286 return;
287 end if;
288
289 if Opnd = Left_Opnd (N) then
290 Error_Msg_N
291 ("\left operand has the following interpretations", N);
292 else
293 Error_Msg_N
294 ("\right operand has the following interpretations", N);
295 Err := Opnd;
296 end if;
297
fbf5a39b
AC
298 List_Interps (Nam, Err);
299 end List_Operand_Interps;
996ae0b0 300
4c46b835
AC
301 -- Start of processing for Ambiguous_Operands
302
996ae0b0 303 begin
b67a385c 304 if Nkind (N) in N_Membership_Test then
996ae0b0
RK
305 Error_Msg_N ("ambiguous operands for membership", N);
306
307 elsif Nkind (N) = N_Op_Eq
308 or else Nkind (N) = N_Op_Ne
309 then
310 Error_Msg_N ("ambiguous operands for equality", N);
311
312 else
313 Error_Msg_N ("ambiguous operands for comparison", N);
314 end if;
315
316 if All_Errors_Mode then
fbf5a39b
AC
317 List_Operand_Interps (Left_Opnd (N));
318 List_Operand_Interps (Right_Opnd (N));
996ae0b0 319 else
555360a5 320 Error_Msg_N ("\use -gnatf switch for details", N);
996ae0b0
RK
321 end if;
322 end Ambiguous_Operands;
323
324 -----------------------
325 -- Analyze_Aggregate --
326 -----------------------
327
328 -- Most of the analysis of Aggregates requires that the type be known,
329 -- and is therefore put off until resolution.
330
331 procedure Analyze_Aggregate (N : Node_Id) is
332 begin
333 if No (Etype (N)) then
334 Set_Etype (N, Any_Composite);
335 end if;
336 end Analyze_Aggregate;
337
338 -----------------------
339 -- Analyze_Allocator --
340 -----------------------
341
342 procedure Analyze_Allocator (N : Node_Id) is
343 Loc : constant Source_Ptr := Sloc (N);
07fc65c4 344 Sav_Errs : constant Nat := Serious_Errors_Detected;
b67a385c 345 E : Node_Id := Expression (N);
996ae0b0
RK
346 Acc_Type : Entity_Id;
347 Type_Id : Entity_Id;
348
349 begin
350 Check_Restriction (No_Allocators, N);
351
352 if Nkind (E) = N_Qualified_Expression then
aab883ec 353
996ae0b0
RK
354 Acc_Type := Create_Itype (E_Allocator_Type, N);
355 Set_Etype (Acc_Type, Acc_Type);
356 Init_Size_Align (Acc_Type);
357 Find_Type (Subtype_Mark (E));
358 Type_Id := Entity (Subtype_Mark (E));
359 Check_Fully_Declared (Type_Id, N);
360 Set_Directly_Designated_Type (Acc_Type, Type_Id);
361
b67a385c
ES
362 Analyze_And_Resolve (Expression (E), Type_Id);
363
d05ef0ab 364 if Is_Limited_Type (Type_Id)
996ae0b0
RK
365 and then Comes_From_Source (N)
366 and then not In_Instance_Body
367 then
b67a385c 368 if not OK_For_Limited_Init (Expression (E)) then
d05ef0ab
AC
369 Error_Msg_N ("initialization not allowed for limited types", N);
370 Explain_Limited_Type (Type_Id, N);
371 end if;
996ae0b0
RK
372 end if;
373
996ae0b0
RK
374 -- A qualified expression requires an exact match of the type,
375 -- class-wide matching is not allowed.
376
377 if Is_Class_Wide_Type (Type_Id)
378 and then Base_Type (Etype (Expression (E))) /= Base_Type (Type_Id)
379 then
380 Wrong_Type (Expression (E), Type_Id);
381 end if;
382
383 Check_Non_Static_Context (Expression (E));
384
385 -- We don't analyze the qualified expression itself because it's
386 -- part of the allocator
387
388 Set_Etype (E, Type_Id);
389
aab883ec 390 -- Case where allocator has a subtype indication
4c46b835 391
996ae0b0
RK
392 else
393 declare
758c442c
GD
394 Def_Id : Entity_Id;
395 Base_Typ : Entity_Id;
996ae0b0
RK
396
397 begin
398 -- If the allocator includes a N_Subtype_Indication then a
399 -- constraint is present, otherwise the node is a subtype mark.
400 -- Introduce an explicit subtype declaration into the tree
401 -- defining some anonymous subtype and rewrite the allocator to
402 -- use this subtype rather than the subtype indication.
403
404 -- It is important to introduce the explicit subtype declaration
405 -- so that the bounds of the subtype indication are attached to
406 -- the tree in case the allocator is inside a generic unit.
407
408 if Nkind (E) = N_Subtype_Indication then
409
410 -- A constraint is only allowed for a composite type in Ada
411 -- 95. In Ada 83, a constraint is also allowed for an
412 -- access-to-composite type, but the constraint is ignored.
413
414 Find_Type (Subtype_Mark (E));
758c442c 415 Base_Typ := Entity (Subtype_Mark (E));
996ae0b0 416
758c442c 417 if Is_Elementary_Type (Base_Typ) then
0ab80019 418 if not (Ada_Version = Ada_83
758c442c 419 and then Is_Access_Type (Base_Typ))
996ae0b0
RK
420 then
421 Error_Msg_N ("constraint not allowed here", E);
422
423 if Nkind (Constraint (E))
424 = N_Index_Or_Discriminant_Constraint
425 then
426 Error_Msg_N
427 ("\if qualified expression was meant, " &
428 "use apostrophe", Constraint (E));
429 end if;
430 end if;
431
432 -- Get rid of the bogus constraint:
433
434 Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
435 Analyze_Allocator (N);
436 return;
758c442c
GD
437
438 -- Ada 2005, AI-363: if the designated type has a constrained
439 -- partial view, it cannot receive a discriminant constraint,
440 -- and the allocated object is unconstrained.
441
442 elsif Ada_Version >= Ada_05
443 and then Has_Constrained_Partial_View (Base_Typ)
444 then
445 Error_Msg_N
446 ("constraint no allowed when type " &
447 "has a constrained partial view", Constraint (E));
996ae0b0
RK
448 end if;
449
450 if Expander_Active then
451 Def_Id :=
452 Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
453
454 Insert_Action (E,
455 Make_Subtype_Declaration (Loc,
456 Defining_Identifier => Def_Id,
457 Subtype_Indication => Relocate_Node (E)));
458
07fc65c4 459 if Sav_Errs /= Serious_Errors_Detected
996ae0b0
RK
460 and then Nkind (Constraint (E))
461 = N_Index_Or_Discriminant_Constraint
462 then
463 Error_Msg_N
464 ("if qualified expression was meant, " &
465 "use apostrophe!", Constraint (E));
466 end if;
467
468 E := New_Occurrence_Of (Def_Id, Loc);
469 Rewrite (Expression (N), E);
470 end if;
471 end if;
472
473 Type_Id := Process_Subtype (E, N);
474 Acc_Type := Create_Itype (E_Allocator_Type, N);
475 Set_Etype (Acc_Type, Acc_Type);
476 Init_Size_Align (Acc_Type);
477 Set_Directly_Designated_Type (Acc_Type, Type_Id);
478 Check_Fully_Declared (Type_Id, N);
479
0ab80019 480 -- Ada 2005 (AI-231)
2820d220
AC
481
482 if Can_Never_Be_Null (Type_Id) then
0ab80019 483 Error_Msg_N ("(Ada 2005) qualified expression required",
2820d220
AC
484 Expression (N));
485 end if;
486
91b1417d
AC
487 -- Check restriction against dynamically allocated protected
488 -- objects. Note that when limited aggregates are supported,
489 -- a similar test should be applied to an allocator with a
490 -- qualified expression ???
491
492 if Is_Protected_Type (Type_Id) then
493 Check_Restriction (No_Protected_Type_Allocators, N);
494 end if;
495
996ae0b0
RK
496 -- Check for missing initialization. Skip this check if we already
497 -- had errors on analyzing the allocator, since in that case these
498 -- are probably cascaded errors
499
500 if Is_Indefinite_Subtype (Type_Id)
07fc65c4 501 and then Serious_Errors_Detected = Sav_Errs
996ae0b0
RK
502 then
503 if Is_Class_Wide_Type (Type_Id) then
504 Error_Msg_N
505 ("initialization required in class-wide allocation", N);
506 else
507 Error_Msg_N
508 ("initialization required in unconstrained allocation", N);
509 end if;
510 end if;
511 end;
512 end if;
513
aab883ec 514 if Is_Abstract_Type (Type_Id) then
996ae0b0
RK
515 Error_Msg_N ("cannot allocate abstract object", E);
516 end if;
517
518 if Has_Task (Designated_Type (Acc_Type)) then
6e937c1c 519 Check_Restriction (No_Tasking, N);
fbf5a39b 520 Check_Restriction (Max_Tasks, N);
996ae0b0
RK
521 Check_Restriction (No_Task_Allocators, N);
522 end if;
523
ffe9aba8
AC
524 -- If the No_Streams restriction is set, check that the type of the
525 -- object is not, and does not contain, any subtype derived from
526 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
527 -- Has_Stream just for efficiency reasons. There is no point in
528 -- spending time on a Has_Stream check if the restriction is not set.
529
530 if Restrictions.Set (No_Streams) then
531 if Has_Stream (Designated_Type (Acc_Type)) then
532 Check_Restriction (No_Streams, N);
533 end if;
534 end if;
535
996ae0b0
RK
536 Set_Etype (N, Acc_Type);
537
538 if not Is_Library_Level_Entity (Acc_Type) then
539 Check_Restriction (No_Local_Allocators, N);
540 end if;
2820d220 541
07fc65c4 542 if Serious_Errors_Detected > Sav_Errs then
996ae0b0
RK
543 Set_Error_Posted (N);
544 Set_Etype (N, Any_Type);
545 end if;
996ae0b0
RK
546 end Analyze_Allocator;
547
548 ---------------------------
549 -- Analyze_Arithmetic_Op --
550 ---------------------------
551
552 procedure Analyze_Arithmetic_Op (N : Node_Id) is
553 L : constant Node_Id := Left_Opnd (N);
554 R : constant Node_Id := Right_Opnd (N);
555 Op_Id : Entity_Id;
556
557 begin
558 Candidate_Type := Empty;
559 Analyze_Expression (L);
560 Analyze_Expression (R);
561
562 -- If the entity is already set, the node is the instantiation of
563 -- a generic node with a non-local reference, or was manufactured
564 -- by a call to Make_Op_xxx. In either case the entity is known to
565 -- be valid, and we do not need to collect interpretations, instead
566 -- we just get the single possible interpretation.
567
568 Op_Id := Entity (N);
569
570 if Present (Op_Id) then
571 if Ekind (Op_Id) = E_Operator then
572
573 if (Nkind (N) = N_Op_Divide or else
574 Nkind (N) = N_Op_Mod or else
575 Nkind (N) = N_Op_Multiply or else
576 Nkind (N) = N_Op_Rem)
577 and then Treat_Fixed_As_Integer (N)
578 then
579 null;
580 else
581 Set_Etype (N, Any_Type);
582 Find_Arithmetic_Types (L, R, Op_Id, N);
583 end if;
584
585 else
586 Set_Etype (N, Any_Type);
587 Add_One_Interp (N, Op_Id, Etype (Op_Id));
588 end if;
589
590 -- Entity is not already set, so we do need to collect interpretations
591
592 else
593 Op_Id := Get_Name_Entity_Id (Chars (N));
594 Set_Etype (N, Any_Type);
595
596 while Present (Op_Id) loop
597 if Ekind (Op_Id) = E_Operator
598 and then Present (Next_Entity (First_Entity (Op_Id)))
599 then
600 Find_Arithmetic_Types (L, R, Op_Id, N);
601
602 -- The following may seem superfluous, because an operator cannot
603 -- be generic, but this ignores the cleverness of the author of
604 -- ACVC bc1013a.
605
606 elsif Is_Overloadable (Op_Id) then
607 Analyze_User_Defined_Binary_Op (N, Op_Id);
608 end if;
609
610 Op_Id := Homonym (Op_Id);
611 end loop;
612 end if;
613
614 Operator_Check (N);
615 end Analyze_Arithmetic_Op;
616
617 ------------------
618 -- Analyze_Call --
619 ------------------
620
4c46b835
AC
621 -- Function, procedure, and entry calls are checked here. The Name in
622 -- the call may be overloaded. The actuals have been analyzed and may
623 -- themselves be overloaded. On exit from this procedure, the node N
624 -- may have zero, one or more interpretations. In the first case an
625 -- error message is produced. In the last case, the node is flagged
626 -- as overloaded and the interpretations are collected in All_Interp.
996ae0b0
RK
627
628 -- If the name is an Access_To_Subprogram, it cannot be overloaded, but
629 -- the type-checking is similar to that of other calls.
630
631 procedure Analyze_Call (N : Node_Id) is
632 Actuals : constant List_Id := Parameter_Associations (N);
633 Nam : Node_Id := Name (N);
634 X : Interp_Index;
635 It : Interp;
636 Nam_Ent : Entity_Id;
637 Success : Boolean := False;
638
639 function Name_Denotes_Function return Boolean;
640 -- If the type of the name is an access to subprogram, this may be
641 -- the type of a name, or the return type of the function being called.
642 -- If the name is not an entity then it can denote a protected function.
643 -- Until we distinguish Etype from Return_Type, we must use this
644 -- routine to resolve the meaning of the name in the call.
645
646 ---------------------------
647 -- Name_Denotes_Function --
648 ---------------------------
649
650 function Name_Denotes_Function return Boolean is
651 begin
652 if Is_Entity_Name (Nam) then
653 return Ekind (Entity (Nam)) = E_Function;
654
655 elsif Nkind (Nam) = N_Selected_Component then
656 return Ekind (Entity (Selector_Name (Nam))) = E_Function;
657
658 else
659 return False;
660 end if;
661 end Name_Denotes_Function;
662
663 -- Start of processing for Analyze_Call
664
665 begin
666 -- Initialize the type of the result of the call to the error type,
667 -- which will be reset if the type is successfully resolved.
668
669 Set_Etype (N, Any_Type);
670
671 if not Is_Overloaded (Nam) then
672
673 -- Only one interpretation to check
674
675 if Ekind (Etype (Nam)) = E_Subprogram_Type then
676 Nam_Ent := Etype (Nam);
677
758c442c
GD
678 -- If the prefix is an access_to_subprogram, this may be an indirect
679 -- call. This is the case if the name in the call is not an entity
680 -- name, or if it is a function name in the context of a procedure
681 -- call. In this latter case, we have a call to a parameterless
682 -- function that returns a pointer_to_procedure which is the entity
683 -- being called.
684
996ae0b0
RK
685 elsif Is_Access_Type (Etype (Nam))
686 and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type
758c442c
GD
687 and then
688 (not Name_Denotes_Function
689 or else Nkind (N) = N_Procedure_Call_Statement)
996ae0b0
RK
690 then
691 Nam_Ent := Designated_Type (Etype (Nam));
692 Insert_Explicit_Dereference (Nam);
693
694 -- Selected component case. Simple entry or protected operation,
695 -- where the entry name is given by the selector name.
696
697 elsif Nkind (Nam) = N_Selected_Component then
698 Nam_Ent := Entity (Selector_Name (Nam));
699
700 if Ekind (Nam_Ent) /= E_Entry
701 and then Ekind (Nam_Ent) /= E_Entry_Family
702 and then Ekind (Nam_Ent) /= E_Function
703 and then Ekind (Nam_Ent) /= E_Procedure
704 then
705 Error_Msg_N ("name in call is not a callable entity", Nam);
706 Set_Etype (N, Any_Type);
707 return;
708 end if;
709
710 -- If the name is an Indexed component, it can be a call to a member
711 -- of an entry family. The prefix must be a selected component whose
712 -- selector is the entry. Analyze_Procedure_Call normalizes several
713 -- kinds of call into this form.
714
715 elsif Nkind (Nam) = N_Indexed_Component then
716
717 if Nkind (Prefix (Nam)) = N_Selected_Component then
718 Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
996ae0b0
RK
719 else
720 Error_Msg_N ("name in call is not a callable entity", Nam);
721 Set_Etype (N, Any_Type);
722 return;
996ae0b0
RK
723 end if;
724
725 elsif not Is_Entity_Name (Nam) then
726 Error_Msg_N ("name in call is not a callable entity", Nam);
727 Set_Etype (N, Any_Type);
728 return;
729
730 else
731 Nam_Ent := Entity (Nam);
732
733 -- If no interpretations, give error message
734
735 if not Is_Overloadable (Nam_Ent) then
736 declare
737 L : constant Boolean := Is_List_Member (N);
738 K : constant Node_Kind := Nkind (Parent (N));
739
740 begin
741 -- If the node is in a list whose parent is not an
742 -- expression then it must be an attempted procedure call.
743
744 if L and then K not in N_Subexpr then
745 if Ekind (Entity (Nam)) = E_Generic_Procedure then
746 Error_Msg_NE
747 ("must instantiate generic procedure& before call",
748 Nam, Entity (Nam));
749 else
750 Error_Msg_N
751 ("procedure or entry name expected", Nam);
752 end if;
753
754 -- Check for tasking cases where only an entry call will do
755
756 elsif not L
757 and then (K = N_Entry_Call_Alternative
758 or else K = N_Triggering_Alternative)
759 then
760 Error_Msg_N ("entry name expected", Nam);
761
762 -- Otherwise give general error message
763
764 else
765 Error_Msg_N ("invalid prefix in call", Nam);
766 end if;
767
768 return;
769 end;
770 end if;
771 end if;
772
773 Analyze_One_Call (N, Nam_Ent, True, Success);
774
ec6078e3
ES
775 -- If this is an indirect call, the return type of the access_to
776 -- subprogram may be an incomplete type. At the point of the call,
777 -- use the full type if available, and at the same time update
778 -- the return type of the access_to_subprogram.
779
780 if Success
781 and then Nkind (Nam) = N_Explicit_Dereference
782 and then Ekind (Etype (N)) = E_Incomplete_Type
783 and then Present (Full_View (Etype (N)))
784 then
785 Set_Etype (N, Full_View (Etype (N)));
786 Set_Etype (Nam_Ent, Etype (N));
787 end if;
788
996ae0b0
RK
789 else
790 -- An overloaded selected component must denote overloaded
791 -- operations of a concurrent type. The interpretations are
792 -- attached to the simple name of those operations.
793
794 if Nkind (Nam) = N_Selected_Component then
795 Nam := Selector_Name (Nam);
796 end if;
797
798 Get_First_Interp (Nam, X, It);
799
800 while Present (It.Nam) loop
801 Nam_Ent := It.Nam;
802
803 -- Name may be call that returns an access to subprogram, or more
804 -- generally an overloaded expression one of whose interpretations
805 -- yields an access to subprogram. If the name is an entity, we
806 -- do not dereference, because the node is a call that returns
807 -- the access type: note difference between f(x), where the call
808 -- may return an access subprogram type, and f(x)(y), where the
809 -- type returned by the call to f is implicitly dereferenced to
810 -- analyze the outer call.
811
812 if Is_Access_Type (Nam_Ent) then
813 Nam_Ent := Designated_Type (Nam_Ent);
814
815 elsif Is_Access_Type (Etype (Nam_Ent))
816 and then not Is_Entity_Name (Nam)
817 and then Ekind (Designated_Type (Etype (Nam_Ent)))
818 = E_Subprogram_Type
819 then
820 Nam_Ent := Designated_Type (Etype (Nam_Ent));
821 end if;
822
823 Analyze_One_Call (N, Nam_Ent, False, Success);
824
825 -- If the interpretation succeeds, mark the proper type of the
826 -- prefix (any valid candidate will do). If not, remove the
827 -- candidate interpretation. This only needs to be done for
828 -- overloaded protected operations, for other entities disambi-
829 -- guation is done directly in Resolve.
830
831 if Success then
832 Set_Etype (Nam, It.Typ);
833
fbf5a39b
AC
834 elsif Nkind (Name (N)) = N_Selected_Component
835 or else Nkind (Name (N)) = N_Function_Call
836 then
996ae0b0
RK
837 Remove_Interp (X);
838 end if;
839
840 Get_Next_Interp (X, It);
841 end loop;
842
843 -- If the name is the result of a function call, it can only
844 -- be a call to a function returning an access to subprogram.
845 -- Insert explicit dereference.
846
847 if Nkind (Nam) = N_Function_Call then
848 Insert_Explicit_Dereference (Nam);
849 end if;
850
851 if Etype (N) = Any_Type then
852
853 -- None of the interpretations is compatible with the actuals
854
855 Diagnose_Call (N, Nam);
856
857 -- Special checks for uninstantiated put routines
858
859 if Nkind (N) = N_Procedure_Call_Statement
860 and then Is_Entity_Name (Nam)
861 and then Chars (Nam) = Name_Put
862 and then List_Length (Actuals) = 1
863 then
864 declare
865 Arg : constant Node_Id := First (Actuals);
866 Typ : Entity_Id;
867
868 begin
869 if Nkind (Arg) = N_Parameter_Association then
870 Typ := Etype (Explicit_Actual_Parameter (Arg));
871 else
872 Typ := Etype (Arg);
873 end if;
874
875 if Is_Signed_Integer_Type (Typ) then
876 Error_Msg_N
877 ("possible missing instantiation of " &
878 "'Text_'I'O.'Integer_'I'O!", Nam);
879
880 elsif Is_Modular_Integer_Type (Typ) then
881 Error_Msg_N
882 ("possible missing instantiation of " &
883 "'Text_'I'O.'Modular_'I'O!", Nam);
884
885 elsif Is_Floating_Point_Type (Typ) then
886 Error_Msg_N
887 ("possible missing instantiation of " &
888 "'Text_'I'O.'Float_'I'O!", Nam);
889
890 elsif Is_Ordinary_Fixed_Point_Type (Typ) then
891 Error_Msg_N
892 ("possible missing instantiation of " &
893 "'Text_'I'O.'Fixed_'I'O!", Nam);
894
895 elsif Is_Decimal_Fixed_Point_Type (Typ) then
896 Error_Msg_N
897 ("possible missing instantiation of " &
898 "'Text_'I'O.'Decimal_'I'O!", Nam);
899
900 elsif Is_Enumeration_Type (Typ) then
901 Error_Msg_N
902 ("possible missing instantiation of " &
903 "'Text_'I'O.'Enumeration_'I'O!", Nam);
904 end if;
905 end;
906 end if;
907
908 elsif not Is_Overloaded (N)
909 and then Is_Entity_Name (Nam)
910 then
aab883ec
ES
911 -- Resolution yields a single interpretation. Verify that the
912 -- reference has capitalization consistent with the declaration.
996ae0b0
RK
913
914 Set_Entity_With_Style_Check (Nam, Entity (Nam));
915 Generate_Reference (Entity (Nam), Nam);
916
917 Set_Etype (Nam, Etype (Entity (Nam)));
30c20106
AC
918 else
919 Remove_Abstract_Operations (N);
996ae0b0
RK
920 end if;
921
922 End_Interp_List;
923 end if;
b67a385c 924
aab883ec
ES
925 -- Check for not-yet-implemented cases of AI-318. We only need to check
926 -- for inherently limited types, because other limited types will be
927 -- returned by copy, which works just fine.
b67a385c
ES
928
929 if Ada_Version >= Ada_05
930 and then not Debug_Flag_Dot_L
931 and then Is_Inherently_Limited_Type (Etype (N))
932 and then (Nkind (Parent (N)) = N_Selected_Component
aab883ec
ES
933 or else Nkind (Parent (N)) = N_Indexed_Component
934 or else Nkind (Parent (N)) = N_Slice
935 or else Nkind (Parent (N)) = N_Attribute_Reference)
b67a385c
ES
936 then
937 Error_Msg_N ("(Ada 2005) limited function call in this context" &
938 " is not yet implemented", N);
939 end if;
996ae0b0
RK
940 end Analyze_Call;
941
942 ---------------------------
943 -- Analyze_Comparison_Op --
944 ---------------------------
945
946 procedure Analyze_Comparison_Op (N : Node_Id) is
947 L : constant Node_Id := Left_Opnd (N);
948 R : constant Node_Id := Right_Opnd (N);
949 Op_Id : Entity_Id := Entity (N);
950
951 begin
952 Set_Etype (N, Any_Type);
953 Candidate_Type := Empty;
954
955 Analyze_Expression (L);
956 Analyze_Expression (R);
957
958 if Present (Op_Id) then
996ae0b0
RK
959 if Ekind (Op_Id) = E_Operator then
960 Find_Comparison_Types (L, R, Op_Id, N);
961 else
962 Add_One_Interp (N, Op_Id, Etype (Op_Id));
963 end if;
964
965 if Is_Overloaded (L) then
966 Set_Etype (L, Intersect_Types (L, R));
967 end if;
968
969 else
970 Op_Id := Get_Name_Entity_Id (Chars (N));
996ae0b0 971 while Present (Op_Id) loop
996ae0b0
RK
972 if Ekind (Op_Id) = E_Operator then
973 Find_Comparison_Types (L, R, Op_Id, N);
974 else
975 Analyze_User_Defined_Binary_Op (N, Op_Id);
976 end if;
977
978 Op_Id := Homonym (Op_Id);
979 end loop;
980 end if;
981
982 Operator_Check (N);
983 end Analyze_Comparison_Op;
984
985 ---------------------------
986 -- Analyze_Concatenation --
987 ---------------------------
988
989 -- If the only one-dimensional array type in scope is String,
990 -- this is the resulting type of the operation. Otherwise there
991 -- will be a concatenation operation defined for each user-defined
992 -- one-dimensional array.
993
994 procedure Analyze_Concatenation (N : Node_Id) is
995 L : constant Node_Id := Left_Opnd (N);
996 R : constant Node_Id := Right_Opnd (N);
997 Op_Id : Entity_Id := Entity (N);
998 LT : Entity_Id;
999 RT : Entity_Id;
1000
1001 begin
1002 Set_Etype (N, Any_Type);
1003 Candidate_Type := Empty;
1004
1005 Analyze_Expression (L);
1006 Analyze_Expression (R);
1007
1008 -- If the entity is present, the node appears in an instance,
1009 -- and denotes a predefined concatenation operation. The resulting
fbf5a39b
AC
1010 -- type is obtained from the arguments when possible. If the arguments
1011 -- are aggregates, the array type and the concatenation type must be
1012 -- visible.
996ae0b0
RK
1013
1014 if Present (Op_Id) then
1015 if Ekind (Op_Id) = E_Operator then
1016
1017 LT := Base_Type (Etype (L));
1018 RT := Base_Type (Etype (R));
1019
1020 if Is_Array_Type (LT)
1021 and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
1022 then
1023 Add_One_Interp (N, Op_Id, LT);
1024
1025 elsif Is_Array_Type (RT)
1026 and then LT = Base_Type (Component_Type (RT))
1027 then
1028 Add_One_Interp (N, Op_Id, RT);
1029
fbf5a39b
AC
1030 -- If one operand is a string type or a user-defined array type,
1031 -- and the other is a literal, result is of the specific type.
1032
1033 elsif
1034 (Root_Type (LT) = Standard_String
1035 or else Scope (LT) /= Standard_Standard)
1036 and then Etype (R) = Any_String
1037 then
1038 Add_One_Interp (N, Op_Id, LT);
1039
1040 elsif
1041 (Root_Type (RT) = Standard_String
1042 or else Scope (RT) /= Standard_Standard)
1043 and then Etype (L) = Any_String
1044 then
1045 Add_One_Interp (N, Op_Id, RT);
1046
1047 elsif not Is_Generic_Type (Etype (Op_Id)) then
996ae0b0 1048 Add_One_Interp (N, Op_Id, Etype (Op_Id));
fbf5a39b
AC
1049
1050 else
4c46b835 1051 -- Type and its operations must be visible
fbf5a39b
AC
1052
1053 Set_Entity (N, Empty);
1054 Analyze_Concatenation (N);
996ae0b0
RK
1055 end if;
1056
1057 else
1058 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1059 end if;
1060
1061 else
1a8fae99 1062 Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
996ae0b0
RK
1063 while Present (Op_Id) loop
1064 if Ekind (Op_Id) = E_Operator then
1a8fae99
ES
1065
1066 -- Do not consider operators declared in dead code, they can
1067 -- not be part of the resolution.
1068
1069 if Is_Eliminated (Op_Id) then
1070 null;
1071 else
1072 Find_Concatenation_Types (L, R, Op_Id, N);
1073 end if;
1074
996ae0b0
RK
1075 else
1076 Analyze_User_Defined_Binary_Op (N, Op_Id);
1077 end if;
1078
1079 Op_Id := Homonym (Op_Id);
1080 end loop;
1081 end if;
1082
1083 Operator_Check (N);
1084 end Analyze_Concatenation;
1085
1086 ------------------------------------
1087 -- Analyze_Conditional_Expression --
1088 ------------------------------------
1089
1090 procedure Analyze_Conditional_Expression (N : Node_Id) is
1091 Condition : constant Node_Id := First (Expressions (N));
1092 Then_Expr : constant Node_Id := Next (Condition);
1093 Else_Expr : constant Node_Id := Next (Then_Expr);
996ae0b0
RK
1094 begin
1095 Analyze_Expression (Condition);
1096 Analyze_Expression (Then_Expr);
1097 Analyze_Expression (Else_Expr);
1098 Set_Etype (N, Etype (Then_Expr));
1099 end Analyze_Conditional_Expression;
1100
1101 -------------------------
1102 -- Analyze_Equality_Op --
1103 -------------------------
1104
1105 procedure Analyze_Equality_Op (N : Node_Id) is
4c46b835
AC
1106 Loc : constant Source_Ptr := Sloc (N);
1107 L : constant Node_Id := Left_Opnd (N);
1108 R : constant Node_Id := Right_Opnd (N);
1109 Op_Id : Entity_Id;
996ae0b0
RK
1110
1111 begin
1112 Set_Etype (N, Any_Type);
1113 Candidate_Type := Empty;
1114
1115 Analyze_Expression (L);
1116 Analyze_Expression (R);
1117
1118 -- If the entity is set, the node is a generic instance with a non-local
1119 -- reference to the predefined operator or to a user-defined function.
1120 -- It can also be an inequality that is expanded into the negation of a
1121 -- call to a user-defined equality operator.
1122
1123 -- For the predefined case, the result is Boolean, regardless of the
1124 -- type of the operands. The operands may even be limited, if they are
1125 -- generic actuals. If they are overloaded, label the left argument with
1126 -- the common type that must be present, or with the type of the formal
1127 -- of the user-defined function.
1128
1129 if Present (Entity (N)) then
996ae0b0
RK
1130 Op_Id := Entity (N);
1131
1132 if Ekind (Op_Id) = E_Operator then
1133 Add_One_Interp (N, Op_Id, Standard_Boolean);
1134 else
1135 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1136 end if;
1137
1138 if Is_Overloaded (L) then
996ae0b0
RK
1139 if Ekind (Op_Id) = E_Operator then
1140 Set_Etype (L, Intersect_Types (L, R));
1141 else
1142 Set_Etype (L, Etype (First_Formal (Op_Id)));
1143 end if;
1144 end if;
1145
1146 else
1147 Op_Id := Get_Name_Entity_Id (Chars (N));
996ae0b0 1148 while Present (Op_Id) loop
996ae0b0
RK
1149 if Ekind (Op_Id) = E_Operator then
1150 Find_Equality_Types (L, R, Op_Id, N);
1151 else
1152 Analyze_User_Defined_Binary_Op (N, Op_Id);
1153 end if;
1154
1155 Op_Id := Homonym (Op_Id);
1156 end loop;
1157 end if;
1158
1159 -- If there was no match, and the operator is inequality, this may
1160 -- be a case where inequality has not been made explicit, as for
1161 -- tagged types. Analyze the node as the negation of an equality
1162 -- operation. This cannot be done earlier, because before analysis
1163 -- we cannot rule out the presence of an explicit inequality.
1164
1165 if Etype (N) = Any_Type
1166 and then Nkind (N) = N_Op_Ne
1167 then
1168 Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
996ae0b0 1169 while Present (Op_Id) loop
996ae0b0
RK
1170 if Ekind (Op_Id) = E_Operator then
1171 Find_Equality_Types (L, R, Op_Id, N);
1172 else
1173 Analyze_User_Defined_Binary_Op (N, Op_Id);
1174 end if;
1175
1176 Op_Id := Homonym (Op_Id);
1177 end loop;
1178
1179 if Etype (N) /= Any_Type then
1180 Op_Id := Entity (N);
1181
1182 Rewrite (N,
1183 Make_Op_Not (Loc,
1184 Right_Opnd =>
1185 Make_Op_Eq (Loc,
aab883ec
ES
1186 Left_Opnd => Left_Opnd (N),
1187 Right_Opnd => Right_Opnd (N))));
996ae0b0
RK
1188
1189 Set_Entity (Right_Opnd (N), Op_Id);
1190 Analyze (N);
1191 end if;
1192 end if;
1193
1194 Operator_Check (N);
1195 end Analyze_Equality_Op;
1196
1197 ----------------------------------
1198 -- Analyze_Explicit_Dereference --
1199 ----------------------------------
1200
1201 procedure Analyze_Explicit_Dereference (N : Node_Id) is
1202 Loc : constant Source_Ptr := Sloc (N);
1203 P : constant Node_Id := Prefix (N);
1204 T : Entity_Id;
1205 I : Interp_Index;
1206 It : Interp;
1207 New_N : Node_Id;
1208
1209 function Is_Function_Type return Boolean;
4c46b835
AC
1210 -- Check whether node may be interpreted as an implicit function call
1211
1212 ----------------------
1213 -- Is_Function_Type --
1214 ----------------------
996ae0b0
RK
1215
1216 function Is_Function_Type return Boolean is
4c46b835
AC
1217 I : Interp_Index;
1218 It : Interp;
996ae0b0
RK
1219
1220 begin
1221 if not Is_Overloaded (N) then
1222 return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
1223 and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
1224
1225 else
1226 Get_First_Interp (N, I, It);
996ae0b0
RK
1227 while Present (It.Nam) loop
1228 if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
1229 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
1230 then
1231 return False;
1232 end if;
1233
1234 Get_Next_Interp (I, It);
1235 end loop;
1236
1237 return True;
1238 end if;
1239 end Is_Function_Type;
1240
98123480 1241 -- Start of processing for Analyze_Explicit_Dereference
4c46b835 1242
996ae0b0
RK
1243 begin
1244 Analyze (P);
1245 Set_Etype (N, Any_Type);
1246
1247 -- Test for remote access to subprogram type, and if so return
1248 -- after rewriting the original tree.
1249
1250 if Remote_AST_E_Dereference (P) then
1251 return;
1252 end if;
1253
1254 -- Normal processing for other than remote access to subprogram type
1255
1256 if not Is_Overloaded (P) then
1257 if Is_Access_Type (Etype (P)) then
1258
0a36105d
JM
1259 -- Set the Etype. We need to go thru Is_For_Access_Subtypes to
1260 -- avoid other problems caused by the Private_Subtype and it is
1261 -- safe to go to the Base_Type because this is the same as
1262 -- converting the access value to its Base_Type.
996ae0b0
RK
1263
1264 declare
1265 DT : Entity_Id := Designated_Type (Etype (P));
1266
1267 begin
1268 if Ekind (DT) = E_Private_Subtype
1269 and then Is_For_Access_Subtype (DT)
1270 then
1271 DT := Base_Type (DT);
1272 end if;
1273
0a36105d
JM
1274 -- An explicit dereference is a legal occurrence of an
1275 -- incomplete type imported through a limited_with clause,
1276 -- if the full view is visible.
1277
1278 if From_With_Type (DT)
1279 and then not From_With_Type (Scope (DT))
1280 and then
1281 (Is_Immediately_Visible (Scope (DT))
1282 or else
1283 (Is_Child_Unit (Scope (DT))
1284 and then Is_Visible_Child_Unit (Scope (DT))))
1285 then
1286 Set_Etype (N, Available_View (DT));
1287
1288 else
1289 Set_Etype (N, DT);
1290 end if;
996ae0b0
RK
1291 end;
1292
1293 elsif Etype (P) /= Any_Type then
1294 Error_Msg_N ("prefix of dereference must be an access type", N);
1295 return;
1296 end if;
1297
1298 else
1299 Get_First_Interp (P, I, It);
996ae0b0
RK
1300 while Present (It.Nam) loop
1301 T := It.Typ;
1302
1303 if Is_Access_Type (T) then
1304 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
1305 end if;
1306
1307 Get_Next_Interp (I, It);
1308 end loop;
1309
6e73e3ab 1310 -- Error if no interpretation of the prefix has an access type
996ae0b0
RK
1311
1312 if Etype (N) = Any_Type then
1313 Error_Msg_N
1314 ("access type required in prefix of explicit dereference", P);
1315 Set_Etype (N, Any_Type);
1316 return;
1317 end if;
1318 end if;
1319
1320 if Is_Function_Type
1321 and then Nkind (Parent (N)) /= N_Indexed_Component
1322
1323 and then (Nkind (Parent (N)) /= N_Function_Call
1324 or else N /= Name (Parent (N)))
1325
1326 and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
1327 or else N /= Name (Parent (N)))
1328
1329 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1330 and then (Nkind (Parent (N)) /= N_Attribute_Reference
1331 or else
1332 (Attribute_Name (Parent (N)) /= Name_Address
1333 and then
1334 Attribute_Name (Parent (N)) /= Name_Access))
1335 then
1336 -- Name is a function call with no actuals, in a context that
1337 -- requires deproceduring (including as an actual in an enclosing
98123480 1338 -- function or procedure call). There are some pathological cases
996ae0b0
RK
1339 -- where the prefix might include functions that return access to
1340 -- subprograms and others that return a regular type. Disambiguation
98123480
ES
1341 -- of those has to take place in Resolve.
1342 -- See e.g. 7117-014 and E317-001.
996ae0b0
RK
1343
1344 New_N :=
1345 Make_Function_Call (Loc,
1346 Name => Make_Explicit_Dereference (Loc, P),
1347 Parameter_Associations => New_List);
1348
1349 -- If the prefix is overloaded, remove operations that have formals,
1350 -- we know that this is a parameterless call.
1351
1352 if Is_Overloaded (P) then
1353 Get_First_Interp (P, I, It);
996ae0b0
RK
1354 while Present (It.Nam) loop
1355 T := It.Typ;
1356
1357 if No (First_Formal (Base_Type (Designated_Type (T)))) then
1358 Set_Etype (P, T);
1359 else
1360 Remove_Interp (I);
1361 end if;
1362
1363 Get_Next_Interp (I, It);
1364 end loop;
1365 end if;
1366
1367 Rewrite (N, New_N);
1368 Analyze (N);
98123480
ES
1369
1370 elsif not Is_Function_Type
1371 and then Is_Overloaded (N)
1372 then
1373 -- The prefix may include access to subprograms and other access
1374 -- types. If the context selects the interpretation that is a call,
1375 -- we cannot rewrite the node yet, but we include the result of
1376 -- the call interpretation.
1377
1378 Get_First_Interp (N, I, It);
1379 while Present (It.Nam) loop
1380 if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
1381 and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
1382 then
1383 Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
1384 end if;
1385
1386 Get_Next_Interp (I, It);
1387 end loop;
996ae0b0
RK
1388 end if;
1389
1390 -- A value of remote access-to-class-wide must not be dereferenced
1391 -- (RM E.2.2(16)).
1392
1393 Validate_Remote_Access_To_Class_Wide_Type (N);
996ae0b0
RK
1394 end Analyze_Explicit_Dereference;
1395
1396 ------------------------
1397 -- Analyze_Expression --
1398 ------------------------
1399
1400 procedure Analyze_Expression (N : Node_Id) is
1401 begin
1402 Analyze (N);
1403 Check_Parameterless_Call (N);
1404 end Analyze_Expression;
1405
1406 ------------------------------------
1407 -- Analyze_Indexed_Component_Form --
1408 ------------------------------------
1409
1410 procedure Analyze_Indexed_Component_Form (N : Node_Id) is
fbf5a39b
AC
1411 P : constant Node_Id := Prefix (N);
1412 Exprs : constant List_Id := Expressions (N);
1413 Exp : Node_Id;
1414 P_T : Entity_Id;
1415 E : Node_Id;
1416 U_N : Entity_Id;
996ae0b0
RK
1417
1418 procedure Process_Function_Call;
1419 -- Prefix in indexed component form is an overloadable entity,
1420 -- so the node is a function call. Reformat it as such.
1421
1422 procedure Process_Indexed_Component;
1423 -- Prefix in indexed component form is actually an indexed component.
1424 -- This routine processes it, knowing that the prefix is already
1425 -- resolved.
1426
1427 procedure Process_Indexed_Component_Or_Slice;
1428 -- An indexed component with a single index may designate a slice if
1429 -- the index is a subtype mark. This routine disambiguates these two
1430 -- cases by resolving the prefix to see if it is a subtype mark.
1431
1432 procedure Process_Overloaded_Indexed_Component;
1433 -- If the prefix of an indexed component is overloaded, the proper
1434 -- interpretation is selected by the index types and the context.
1435
1436 ---------------------------
1437 -- Process_Function_Call --
1438 ---------------------------
1439
1440 procedure Process_Function_Call is
1441 Actual : Node_Id;
1442
1443 begin
1444 Change_Node (N, N_Function_Call);
1445 Set_Name (N, P);
1446 Set_Parameter_Associations (N, Exprs);
996ae0b0 1447
401093c1 1448 -- Analyze actuals prior to analyzing the call itself
0a36105d 1449
4c46b835 1450 Actual := First (Parameter_Associations (N));
996ae0b0
RK
1451 while Present (Actual) loop
1452 Analyze (Actual);
1453 Check_Parameterless_Call (Actual);
0a36105d
JM
1454
1455 -- Move to next actual. Note that we use Next, not Next_Actual
1456 -- here. The reason for this is a bit subtle. If a function call
1457 -- includes named associations, the parser recognizes the node as
1458 -- a call, and it is analyzed as such. If all associations are
1459 -- positional, the parser builds an indexed_component node, and
1460 -- it is only after analysis of the prefix that the construct
1461 -- is recognized as a call, in which case Process_Function_Call
1462 -- rewrites the node and analyzes the actuals. If the list of
1463 -- actuals is malformed, the parser may leave the node as an
1464 -- indexed component (despite the presence of named associations).
1465 -- The iterator Next_Actual is equivalent to Next if the list is
1466 -- positional, but follows the normalized chain of actuals when
1467 -- named associations are present. In this case normalization has
1468 -- not taken place, and actuals remain unanalyzed, which leads to
1469 -- subsequent crashes or loops if there is an attempt to continue
1470 -- analysis of the program.
1471
1472 Next (Actual);
996ae0b0
RK
1473 end loop;
1474
1475 Analyze_Call (N);
1476 end Process_Function_Call;
1477
1478 -------------------------------
1479 -- Process_Indexed_Component --
1480 -------------------------------
1481
1482 procedure Process_Indexed_Component is
1483 Exp : Node_Id;
1484 Array_Type : Entity_Id;
1485 Index : Node_Id;
6e73e3ab 1486 Pent : Entity_Id := Empty;
996ae0b0
RK
1487
1488 begin
1489 Exp := First (Exprs);
1490
1491 if Is_Overloaded (P) then
1492 Process_Overloaded_Indexed_Component;
1493
1494 else
1495 Array_Type := Etype (P);
1496
6e73e3ab
AC
1497 if Is_Entity_Name (P) then
1498 Pent := Entity (P);
1499 elsif Nkind (P) = N_Selected_Component
1500 and then Is_Entity_Name (Selector_Name (P))
1501 then
1502 Pent := Entity (Selector_Name (P));
1503 end if;
1504
1505 -- Prefix must be appropriate for an array type, taking into
1506 -- account a possible implicit dereference.
996ae0b0
RK
1507
1508 if Is_Access_Type (Array_Type) then
1509 Array_Type := Designated_Type (Array_Type);
fbf5a39b 1510 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
6e73e3ab 1511 Process_Implicit_Dereference_Prefix (Pent, P);
996ae0b0
RK
1512 end if;
1513
1514 if Is_Array_Type (Array_Type) then
1515 null;
1516
6e73e3ab 1517 elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
996ae0b0
RK
1518 Analyze (Exp);
1519 Set_Etype (N, Any_Type);
1520
1521 if not Has_Compatible_Type
6e73e3ab 1522 (Exp, Entry_Index_Type (Pent))
996ae0b0
RK
1523 then
1524 Error_Msg_N ("invalid index type in entry name", N);
1525
1526 elsif Present (Next (Exp)) then
1527 Error_Msg_N ("too many subscripts in entry reference", N);
1528
1529 else
1530 Set_Etype (N, Etype (P));
1531 end if;
1532
1533 return;
1534
1535 elsif Is_Record_Type (Array_Type)
1536 and then Remote_AST_I_Dereference (P)
1537 then
1538 return;
1539
1540 elsif Array_Type = Any_Type then
1541 Set_Etype (N, Any_Type);
1542 return;
1543
1544 -- Here we definitely have a bad indexing
1545
1546 else
1547 if Nkind (Parent (N)) = N_Requeue_Statement
6e73e3ab 1548 and then Present (Pent) and then Ekind (Pent) = E_Entry
996ae0b0
RK
1549 then
1550 Error_Msg_N
1551 ("REQUEUE does not permit parameters", First (Exprs));
1552
1553 elsif Is_Entity_Name (P)
1554 and then Etype (P) = Standard_Void_Type
1555 then
1556 Error_Msg_NE ("incorrect use of&", P, Entity (P));
1557
1558 else
1559 Error_Msg_N ("array type required in indexed component", P);
1560 end if;
1561
1562 Set_Etype (N, Any_Type);
1563 return;
1564 end if;
1565
1566 Index := First_Index (Array_Type);
996ae0b0
RK
1567 while Present (Index) and then Present (Exp) loop
1568 if not Has_Compatible_Type (Exp, Etype (Index)) then
1569 Wrong_Type (Exp, Etype (Index));
1570 Set_Etype (N, Any_Type);
1571 return;
1572 end if;
1573
1574 Next_Index (Index);
1575 Next (Exp);
1576 end loop;
1577
1578 Set_Etype (N, Component_Type (Array_Type));
1579
1580 if Present (Index) then
1581 Error_Msg_N
1582 ("too few subscripts in array reference", First (Exprs));
1583
1584 elsif Present (Exp) then
1585 Error_Msg_N ("too many subscripts in array reference", Exp);
1586 end if;
1587 end if;
996ae0b0
RK
1588 end Process_Indexed_Component;
1589
1590 ----------------------------------------
1591 -- Process_Indexed_Component_Or_Slice --
1592 ----------------------------------------
1593
1594 procedure Process_Indexed_Component_Or_Slice is
1595 begin
1596 Exp := First (Exprs);
996ae0b0
RK
1597 while Present (Exp) loop
1598 Analyze_Expression (Exp);
1599 Next (Exp);
1600 end loop;
1601
1602 Exp := First (Exprs);
1603
1604 -- If one index is present, and it is a subtype name, then the
1605 -- node denotes a slice (note that the case of an explicit range
1606 -- for a slice was already built as an N_Slice node in the first
1607 -- place, so that case is not handled here).
1608
1609 -- We use a replace rather than a rewrite here because this is one
1610 -- of the cases in which the tree built by the parser is plain wrong.
1611
1612 if No (Next (Exp))
1613 and then Is_Entity_Name (Exp)
1614 and then Is_Type (Entity (Exp))
1615 then
1616 Replace (N,
1617 Make_Slice (Sloc (N),
1618 Prefix => P,
1619 Discrete_Range => New_Copy (Exp)));
1620 Analyze (N);
1621
1622 -- Otherwise (more than one index present, or single index is not
1623 -- a subtype name), then we have the indexed component case.
1624
1625 else
1626 Process_Indexed_Component;
1627 end if;
1628 end Process_Indexed_Component_Or_Slice;
1629
1630 ------------------------------------------
1631 -- Process_Overloaded_Indexed_Component --
1632 ------------------------------------------
1633
1634 procedure Process_Overloaded_Indexed_Component is
1635 Exp : Node_Id;
1636 I : Interp_Index;
1637 It : Interp;
1638 Typ : Entity_Id;
1639 Index : Node_Id;
1640 Found : Boolean;
1641
1642 begin
1643 Set_Etype (N, Any_Type);
996ae0b0 1644
4c46b835 1645 Get_First_Interp (P, I, It);
996ae0b0
RK
1646 while Present (It.Nam) loop
1647 Typ := It.Typ;
1648
1649 if Is_Access_Type (Typ) then
1650 Typ := Designated_Type (Typ);
fbf5a39b 1651 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
996ae0b0
RK
1652 end if;
1653
1654 if Is_Array_Type (Typ) then
1655
1656 -- Got a candidate: verify that index types are compatible
1657
1658 Index := First_Index (Typ);
1659 Found := True;
996ae0b0 1660 Exp := First (Exprs);
996ae0b0
RK
1661 while Present (Index) and then Present (Exp) loop
1662 if Has_Compatible_Type (Exp, Etype (Index)) then
1663 null;
1664 else
1665 Found := False;
1666 Remove_Interp (I);
1667 exit;
1668 end if;
1669
1670 Next_Index (Index);
1671 Next (Exp);
1672 end loop;
1673
1674 if Found and then No (Index) and then No (Exp) then
1675 Add_One_Interp (N,
1676 Etype (Component_Type (Typ)),
1677 Etype (Component_Type (Typ)));
1678 end if;
1679 end if;
1680
1681 Get_Next_Interp (I, It);
1682 end loop;
1683
1684 if Etype (N) = Any_Type then
1685 Error_Msg_N ("no legal interpetation for indexed component", N);
1686 Set_Is_Overloaded (N, False);
1687 end if;
1688
1689 End_Interp_List;
1690 end Process_Overloaded_Indexed_Component;
1691
4c46b835 1692 -- Start of processing for Analyze_Indexed_Component_Form
996ae0b0
RK
1693
1694 begin
1695 -- Get name of array, function or type
1696
1697 Analyze (P);
fbf5a39b
AC
1698 if Nkind (N) = N_Function_Call
1699 or else Nkind (N) = N_Procedure_Call_Statement
1700 then
1701 -- If P is an explicit dereference whose prefix is of a
1702 -- remote access-to-subprogram type, then N has already
1703 -- been rewritten as a subprogram call and analyzed.
1704
1705 return;
1706 end if;
1707
1708 pragma Assert (Nkind (N) = N_Indexed_Component);
1709
996ae0b0
RK
1710 P_T := Base_Type (Etype (P));
1711
1712 if Is_Entity_Name (P)
1713 or else Nkind (P) = N_Operator_Symbol
1714 then
1715 U_N := Entity (P);
1716
aab883ec 1717 if Is_Type (U_N) then
996ae0b0 1718
4c46b835 1719 -- Reformat node as a type conversion
996ae0b0
RK
1720
1721 E := Remove_Head (Exprs);
1722
1723 if Present (First (Exprs)) then
1724 Error_Msg_N
1725 ("argument of type conversion must be single expression", N);
1726 end if;
1727
1728 Change_Node (N, N_Type_Conversion);
1729 Set_Subtype_Mark (N, P);
1730 Set_Etype (N, U_N);
1731 Set_Expression (N, E);
1732
1733 -- After changing the node, call for the specific Analysis
1734 -- routine directly, to avoid a double call to the expander.
1735
1736 Analyze_Type_Conversion (N);
1737 return;
1738 end if;
1739
1740 if Is_Overloadable (U_N) then
1741 Process_Function_Call;
1742
1743 elsif Ekind (Etype (P)) = E_Subprogram_Type
1744 or else (Is_Access_Type (Etype (P))
1745 and then
1746 Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type)
1747 then
1748 -- Call to access_to-subprogram with possible implicit dereference
1749
1750 Process_Function_Call;
1751
fbf5a39b
AC
1752 elsif Is_Generic_Subprogram (U_N) then
1753
4c46b835 1754 -- A common beginner's (or C++ templates fan) error
996ae0b0
RK
1755
1756 Error_Msg_N ("generic subprogram cannot be called", N);
1757 Set_Etype (N, Any_Type);
1758 return;
1759
1760 else
1761 Process_Indexed_Component_Or_Slice;
1762 end if;
1763
1764 -- If not an entity name, prefix is an expression that may denote
1765 -- an array or an access-to-subprogram.
1766
1767 else
fbf5a39b 1768 if Ekind (P_T) = E_Subprogram_Type
996ae0b0
RK
1769 or else (Is_Access_Type (P_T)
1770 and then
1771 Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
1772 then
1773 Process_Function_Call;
1774
1775 elsif Nkind (P) = N_Selected_Component
ffe9aba8 1776 and then Is_Overloadable (Entity (Selector_Name (P)))
996ae0b0
RK
1777 then
1778 Process_Function_Call;
1779
1780 else
1781 -- Indexed component, slice, or a call to a member of a family
1782 -- entry, which will be converted to an entry call later.
fbf5a39b 1783
996ae0b0
RK
1784 Process_Indexed_Component_Or_Slice;
1785 end if;
1786 end if;
1787 end Analyze_Indexed_Component_Form;
1788
1789 ------------------------
1790 -- Analyze_Logical_Op --
1791 ------------------------
1792
1793 procedure Analyze_Logical_Op (N : Node_Id) is
1794 L : constant Node_Id := Left_Opnd (N);
1795 R : constant Node_Id := Right_Opnd (N);
1796 Op_Id : Entity_Id := Entity (N);
1797
1798 begin
1799 Set_Etype (N, Any_Type);
1800 Candidate_Type := Empty;
1801
1802 Analyze_Expression (L);
1803 Analyze_Expression (R);
1804
1805 if Present (Op_Id) then
1806
1807 if Ekind (Op_Id) = E_Operator then
1808 Find_Boolean_Types (L, R, Op_Id, N);
1809 else
1810 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1811 end if;
1812
1813 else
1814 Op_Id := Get_Name_Entity_Id (Chars (N));
996ae0b0
RK
1815 while Present (Op_Id) loop
1816 if Ekind (Op_Id) = E_Operator then
1817 Find_Boolean_Types (L, R, Op_Id, N);
1818 else
1819 Analyze_User_Defined_Binary_Op (N, Op_Id);
1820 end if;
1821
1822 Op_Id := Homonym (Op_Id);
1823 end loop;
1824 end if;
1825
1826 Operator_Check (N);
1827 end Analyze_Logical_Op;
1828
1829 ---------------------------
1830 -- Analyze_Membership_Op --
1831 ---------------------------
1832
1833 procedure Analyze_Membership_Op (N : Node_Id) is
1834 L : constant Node_Id := Left_Opnd (N);
1835 R : constant Node_Id := Right_Opnd (N);
1836
1837 Index : Interp_Index;
1838 It : Interp;
1839 Found : Boolean := False;
1840 I_F : Interp_Index;
1841 T_F : Entity_Id;
1842
1843 procedure Try_One_Interp (T1 : Entity_Id);
1844 -- Routine to try one proposed interpretation. Note that the context
1845 -- of the operation plays no role in resolving the arguments, so that
1846 -- if there is more than one interpretation of the operands that is
1847 -- compatible with a membership test, the operation is ambiguous.
1848
4c46b835
AC
1849 --------------------
1850 -- Try_One_Interp --
1851 --------------------
1852
996ae0b0
RK
1853 procedure Try_One_Interp (T1 : Entity_Id) is
1854 begin
1855 if Has_Compatible_Type (R, T1) then
1856 if Found
1857 and then Base_Type (T1) /= Base_Type (T_F)
1858 then
1859 It := Disambiguate (L, I_F, Index, Any_Type);
1860
1861 if It = No_Interp then
1862 Ambiguous_Operands (N);
1863 Set_Etype (L, Any_Type);
1864 return;
1865
1866 else
1867 T_F := It.Typ;
1868 end if;
1869
1870 else
1871 Found := True;
1872 T_F := T1;
1873 I_F := Index;
1874 end if;
1875
1876 Set_Etype (L, T_F);
1877 end if;
1878
1879 end Try_One_Interp;
1880
1881 -- Start of processing for Analyze_Membership_Op
1882
1883 begin
1884 Analyze_Expression (L);
1885
1886 if Nkind (R) = N_Range
1887 or else (Nkind (R) = N_Attribute_Reference
1888 and then Attribute_Name (R) = Name_Range)
1889 then
1890 Analyze (R);
1891
1892 if not Is_Overloaded (L) then
1893 Try_One_Interp (Etype (L));
1894
1895 else
1896 Get_First_Interp (L, Index, It);
996ae0b0
RK
1897 while Present (It.Typ) loop
1898 Try_One_Interp (It.Typ);
1899 Get_Next_Interp (Index, It);
1900 end loop;
1901 end if;
1902
1903 -- If not a range, it can only be a subtype mark, or else there
1904 -- is a more basic error, to be diagnosed in Find_Type.
1905
1906 else
1907 Find_Type (R);
1908
1909 if Is_Entity_Name (R) then
1910 Check_Fully_Declared (Entity (R), R);
1911 end if;
1912 end if;
1913
1914 -- Compatibility between expression and subtype mark or range is
1915 -- checked during resolution. The result of the operation is Boolean
1916 -- in any case.
1917
1918 Set_Etype (N, Standard_Boolean);
fe45e59e
ES
1919
1920 if Comes_From_Source (N)
1921 and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
1922 then
1923 Error_Msg_N ("membership test not applicable to cpp-class types", N);
1924 end if;
996ae0b0
RK
1925 end Analyze_Membership_Op;
1926
1927 ----------------------
1928 -- Analyze_Negation --
1929 ----------------------
1930
1931 procedure Analyze_Negation (N : Node_Id) is
1932 R : constant Node_Id := Right_Opnd (N);
1933 Op_Id : Entity_Id := Entity (N);
1934
1935 begin
1936 Set_Etype (N, Any_Type);
1937 Candidate_Type := Empty;
1938
1939 Analyze_Expression (R);
1940
1941 if Present (Op_Id) then
1942 if Ekind (Op_Id) = E_Operator then
1943 Find_Negation_Types (R, Op_Id, N);
1944 else
1945 Add_One_Interp (N, Op_Id, Etype (Op_Id));
1946 end if;
1947
1948 else
1949 Op_Id := Get_Name_Entity_Id (Chars (N));
996ae0b0
RK
1950 while Present (Op_Id) loop
1951 if Ekind (Op_Id) = E_Operator then
1952 Find_Negation_Types (R, Op_Id, N);
1953 else
1954 Analyze_User_Defined_Unary_Op (N, Op_Id);
1955 end if;
1956
1957 Op_Id := Homonym (Op_Id);
1958 end loop;
1959 end if;
1960
1961 Operator_Check (N);
1962 end Analyze_Negation;
1963
15ce9ca2
AC
1964 ------------------
1965 -- Analyze_Null --
1966 ------------------
996ae0b0
RK
1967
1968 procedure Analyze_Null (N : Node_Id) is
1969 begin
1970 Set_Etype (N, Any_Access);
1971 end Analyze_Null;
1972
1973 ----------------------
1974 -- Analyze_One_Call --
1975 ----------------------
1976
1977 procedure Analyze_One_Call
ec6078e3
ES
1978 (N : Node_Id;
1979 Nam : Entity_Id;
1980 Report : Boolean;
1981 Success : out Boolean;
1982 Skip_First : Boolean := False)
996ae0b0
RK
1983 is
1984 Actuals : constant List_Id := Parameter_Associations (N);
1985 Prev_T : constant Entity_Id := Etype (N);
aab883ec
ES
1986 Must_Skip : constant Boolean := Skip_First
1987 or else Nkind (Original_Node (N)) = N_Selected_Component
1988 or else
1989 (Nkind (Original_Node (N)) = N_Indexed_Component
1990 and then Nkind (Prefix (Original_Node (N)))
1991 = N_Selected_Component);
1992 -- The first formal must be omitted from the match when trying to find
1993 -- a primitive operation that is a possible interpretation, and also
1994 -- after the call has been rewritten, because the corresponding actual
1995 -- is already known to be compatible, and because this may be an
1996 -- indexing of a call with default parameters.
1997
996ae0b0
RK
1998 Formal : Entity_Id;
1999 Actual : Node_Id;
2000 Is_Indexed : Boolean := False;
2001 Subp_Type : constant Entity_Id := Etype (Nam);
2002 Norm_OK : Boolean;
2003
fbf5a39b 2004 procedure Indicate_Name_And_Type;
996ae0b0
RK
2005 -- If candidate interpretation matches, indicate name and type of
2006 -- result on call node.
2007
fbf5a39b
AC
2008 ----------------------------
2009 -- Indicate_Name_And_Type --
2010 ----------------------------
996ae0b0 2011
fbf5a39b 2012 procedure Indicate_Name_And_Type is
996ae0b0
RK
2013 begin
2014 Add_One_Interp (N, Nam, Etype (Nam));
2015 Success := True;
2016
2017 -- If the prefix of the call is a name, indicate the entity
2018 -- being called. If it is not a name, it is an expression that
2019 -- denotes an access to subprogram or else an entry or family. In
2020 -- the latter case, the name is a selected component, and the entity
2021 -- being called is noted on the selector.
2022
2023 if not Is_Type (Nam) then
2024 if Is_Entity_Name (Name (N))
2025 or else Nkind (Name (N)) = N_Operator_Symbol
2026 then
2027 Set_Entity (Name (N), Nam);
2028
2029 elsif Nkind (Name (N)) = N_Selected_Component then
2030 Set_Entity (Selector_Name (Name (N)), Nam);
2031 end if;
2032 end if;
2033
2034 if Debug_Flag_E and not Report then
2035 Write_Str (" Overloaded call ");
2036 Write_Int (Int (N));
2037 Write_Str (" compatible with ");
2038 Write_Int (Int (Nam));
2039 Write_Eol;
2040 end if;
fbf5a39b 2041 end Indicate_Name_And_Type;
996ae0b0
RK
2042
2043 -- Start of processing for Analyze_One_Call
2044
2045 begin
2046 Success := False;
2047
2048 -- If the subprogram has no formals, or if all the formals have
2049 -- defaults, and the return type is an array type, the node may
2050 -- denote an indexing of the result of a parameterless call.
aab883ec
ES
2051 -- In Ada 2005, the subprogram may have one non-defaulted formal,
2052 -- and the call may have been written in prefix notation, so that
2053 -- the rebuilt parameter list has more than one actual.
996ae0b0 2054
aab883ec
ES
2055 if Present (Actuals)
2056 and then
2057 (Needs_No_Actuals (Nam)
2058 or else
2059 (Needs_One_Actual (Nam)
2060 and then Present (Next_Actual (First (Actuals)))))
996ae0b0
RK
2061 then
2062 if Is_Array_Type (Subp_Type) then
aab883ec 2063 Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
996ae0b0
RK
2064
2065 elsif Is_Access_Type (Subp_Type)
2066 and then Is_Array_Type (Designated_Type (Subp_Type))
2067 then
2068 Is_Indexed :=
aab883ec
ES
2069 Try_Indexed_Call
2070 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
996ae0b0 2071
758c442c
GD
2072 -- The prefix can also be a parameterless function that returns an
2073 -- access to subprogram. in which case this is an indirect call.
2074
996ae0b0 2075 elsif Is_Access_Type (Subp_Type)
401093c1 2076 and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
996ae0b0
RK
2077 then
2078 Is_Indexed := Try_Indirect_Call (N, Nam, Subp_Type);
2079 end if;
2080
2081 end if;
2082
2083 Normalize_Actuals (N, Nam, (Report and not Is_Indexed), Norm_OK);
2084
2085 if not Norm_OK then
2086
2087 -- Mismatch in number or names of parameters
2088
2089 if Debug_Flag_E then
2090 Write_Str (" normalization fails in call ");
2091 Write_Int (Int (N));
2092 Write_Str (" with subprogram ");
2093 Write_Int (Int (Nam));
2094 Write_Eol;
2095 end if;
2096
2097 -- If the context expects a function call, discard any interpretation
2098 -- that is a procedure. If the node is not overloaded, leave as is for
2099 -- better error reporting when type mismatch is found.
2100
2101 elsif Nkind (N) = N_Function_Call
2102 and then Is_Overloaded (Name (N))
2103 and then Ekind (Nam) = E_Procedure
2104 then
2105 return;
2106
4c46b835 2107 -- Ditto for function calls in a procedure context
996ae0b0
RK
2108
2109 elsif Nkind (N) = N_Procedure_Call_Statement
2110 and then Is_Overloaded (Name (N))
2111 and then Etype (Nam) /= Standard_Void_Type
2112 then
2113 return;
2114
fe45e59e 2115 elsif No (Actuals) then
996ae0b0
RK
2116
2117 -- If Normalize succeeds, then there are default parameters for
2118 -- all formals.
2119
fbf5a39b 2120 Indicate_Name_And_Type;
996ae0b0
RK
2121
2122 elsif Ekind (Nam) = E_Operator then
996ae0b0
RK
2123 if Nkind (N) = N_Procedure_Call_Statement then
2124 return;
2125 end if;
2126
2127 -- This can occur when the prefix of the call is an operator
2128 -- name or an expanded name whose selector is an operator name.
2129
2130 Analyze_Operator_Call (N, Nam);
2131
2132 if Etype (N) /= Prev_T then
2133
2134 -- There may be a user-defined operator that hides the
2135 -- current interpretation. We must check for this independently
2136 -- of the analysis of the call with the user-defined operation,
2137 -- because the parameter names may be wrong and yet the hiding
2138 -- takes place. Fixes b34014o.
2139
2140 if Is_Overloaded (Name (N)) then
2141 declare
2142 I : Interp_Index;
2143 It : Interp;
2144
2145 begin
2146 Get_First_Interp (Name (N), I, It);
996ae0b0 2147 while Present (It.Nam) loop
996ae0b0
RK
2148 if Ekind (It.Nam) /= E_Operator
2149 and then Hides_Op (It.Nam, Nam)
2150 and then
2151 Has_Compatible_Type
2152 (First_Actual (N), Etype (First_Formal (It.Nam)))
2153 and then (No (Next_Actual (First_Actual (N)))
2154 or else Has_Compatible_Type
2155 (Next_Actual (First_Actual (N)),
2156 Etype (Next_Formal (First_Formal (It.Nam)))))
2157 then
2158 Set_Etype (N, Prev_T);
2159 return;
2160 end if;
2161
2162 Get_Next_Interp (I, It);
2163 end loop;
2164 end;
2165 end if;
2166
2167 -- If operator matches formals, record its name on the call.
2168 -- If the operator is overloaded, Resolve will select the
2169 -- correct one from the list of interpretations. The call
2170 -- node itself carries the first candidate.
2171
2172 Set_Entity (Name (N), Nam);
2173 Success := True;
2174
2175 elsif Report and then Etype (N) = Any_Type then
2176 Error_Msg_N ("incompatible arguments for operator", N);
2177 end if;
2178
2179 else
2180 -- Normalize_Actuals has chained the named associations in the
2181 -- correct order of the formals.
2182
2183 Actual := First_Actual (N);
2184 Formal := First_Formal (Nam);
ec6078e3
ES
2185
2186 -- If we are analyzing a call rewritten from object notation,
2187 -- skip first actual, which may be rewritten later as an
2188 -- explicit dereference.
2189
aab883ec 2190 if Must_Skip then
ec6078e3
ES
2191 Next_Actual (Actual);
2192 Next_Formal (Formal);
2193 end if;
2194
996ae0b0 2195 while Present (Actual) and then Present (Formal) loop
fbf5a39b
AC
2196 if Nkind (Parent (Actual)) /= N_Parameter_Association
2197 or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
996ae0b0
RK
2198 then
2199 if Has_Compatible_Type (Actual, Etype (Formal)) then
2200 Next_Actual (Actual);
2201 Next_Formal (Formal);
2202
2203 else
2204 if Debug_Flag_E then
2205 Write_Str (" type checking fails in call ");
2206 Write_Int (Int (N));
2207 Write_Str (" with formal ");
2208 Write_Int (Int (Formal));
2209 Write_Str (" in subprogram ");
2210 Write_Int (Int (Nam));
2211 Write_Eol;
2212 end if;
2213
2214 if Report and not Is_Indexed then
758c442c
GD
2215
2216 -- Ada 2005 (AI-251): Complete the error notification
2217 -- to help new Ada 2005 users
2218
2219 if Is_Class_Wide_Type (Etype (Formal))
2220 and then Is_Interface (Etype (Etype (Formal)))
2221 and then not Interface_Present_In_Ancestor
2222 (Typ => Etype (Actual),
2223 Iface => Etype (Etype (Formal)))
2224 then
758c442c 2225 Error_Msg_NE
ec6078e3 2226 ("(Ada 2005) does not implement interface }",
758c442c
GD
2227 Actual, Etype (Etype (Formal)));
2228 end if;
2229
996ae0b0
RK
2230 Wrong_Type (Actual, Etype (Formal));
2231
2232 if Nkind (Actual) = N_Op_Eq
2233 and then Nkind (Left_Opnd (Actual)) = N_Identifier
2234 then
2235 Formal := First_Formal (Nam);
996ae0b0 2236 while Present (Formal) loop
996ae0b0
RK
2237 if Chars (Left_Opnd (Actual)) = Chars (Formal) then
2238 Error_Msg_N
fbf5a39b 2239 ("possible misspelling of `='>`!", Actual);
996ae0b0
RK
2240 exit;
2241 end if;
2242
2243 Next_Formal (Formal);
2244 end loop;
2245 end if;
2246
2247 if All_Errors_Mode then
2248 Error_Msg_Sloc := Sloc (Nam);
2249
2250 if Is_Overloadable (Nam)
2251 and then Present (Alias (Nam))
2252 and then not Comes_From_Source (Nam)
2253 then
2254 Error_Msg_NE
401093c1
ES
2255 ("\\ =='> in call to inherited operation & #!",
2256 Actual, Nam);
7324bf49
AC
2257
2258 elsif Ekind (Nam) = E_Subprogram_Type then
2259 declare
2260 Access_To_Subprogram_Typ :
2261 constant Entity_Id :=
2262 Defining_Identifier
2263 (Associated_Node_For_Itype (Nam));
2264 begin
2265 Error_Msg_NE (
401093c1 2266 "\\ =='> in call to dereference of &#!",
7324bf49
AC
2267 Actual, Access_To_Subprogram_Typ);
2268 end;
2269
996ae0b0 2270 else
401093c1
ES
2271 Error_Msg_NE
2272 ("\\ =='> in call to &#!", Actual, Nam);
7324bf49 2273
996ae0b0
RK
2274 end if;
2275 end if;
2276 end if;
2277
2278 return;
2279 end if;
2280
2281 else
2282 -- Normalize_Actuals has verified that a default value exists
2283 -- for this formal. Current actual names a subsequent formal.
2284
2285 Next_Formal (Formal);
2286 end if;
2287 end loop;
2288
4c46b835 2289 -- On exit, all actuals match
996ae0b0 2290
fbf5a39b 2291 Indicate_Name_And_Type;
996ae0b0
RK
2292 end if;
2293 end Analyze_One_Call;
2294
15ce9ca2
AC
2295 ---------------------------
2296 -- Analyze_Operator_Call --
2297 ---------------------------
996ae0b0
RK
2298
2299 procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
2300 Op_Name : constant Name_Id := Chars (Op_Id);
2301 Act1 : constant Node_Id := First_Actual (N);
2302 Act2 : constant Node_Id := Next_Actual (Act1);
2303
2304 begin
4c46b835
AC
2305 -- Binary operator case
2306
996ae0b0
RK
2307 if Present (Act2) then
2308
4c46b835 2309 -- If more than two operands, then not binary operator after all
996ae0b0
RK
2310
2311 if Present (Next_Actual (Act2)) then
996ae0b0
RK
2312 return;
2313
2314 elsif Op_Name = Name_Op_Add
2315 or else Op_Name = Name_Op_Subtract
2316 or else Op_Name = Name_Op_Multiply
2317 or else Op_Name = Name_Op_Divide
2318 or else Op_Name = Name_Op_Mod
2319 or else Op_Name = Name_Op_Rem
2320 or else Op_Name = Name_Op_Expon
2321 then
2322 Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
2323
2324 elsif Op_Name = Name_Op_And
2325 or else Op_Name = Name_Op_Or
2326 or else Op_Name = Name_Op_Xor
2327 then
2328 Find_Boolean_Types (Act1, Act2, Op_Id, N);
2329
2330 elsif Op_Name = Name_Op_Lt
2331 or else Op_Name = Name_Op_Le
2332 or else Op_Name = Name_Op_Gt
2333 or else Op_Name = Name_Op_Ge
2334 then
2335 Find_Comparison_Types (Act1, Act2, Op_Id, N);
2336
2337 elsif Op_Name = Name_Op_Eq
2338 or else Op_Name = Name_Op_Ne
2339 then
2340 Find_Equality_Types (Act1, Act2, Op_Id, N);
2341
2342 elsif Op_Name = Name_Op_Concat then
2343 Find_Concatenation_Types (Act1, Act2, Op_Id, N);
2344
2345 -- Is this else null correct, or should it be an abort???
2346
2347 else
2348 null;
2349 end if;
2350
4c46b835 2351 -- Unary operator case
996ae0b0 2352
4c46b835 2353 else
996ae0b0
RK
2354 if Op_Name = Name_Op_Subtract or else
2355 Op_Name = Name_Op_Add or else
2356 Op_Name = Name_Op_Abs
2357 then
2358 Find_Unary_Types (Act1, Op_Id, N);
2359
2360 elsif
2361 Op_Name = Name_Op_Not
2362 then
2363 Find_Negation_Types (Act1, Op_Id, N);
2364
2365 -- Is this else null correct, or should it be an abort???
2366
2367 else
2368 null;
2369 end if;
2370 end if;
2371 end Analyze_Operator_Call;
2372
2373 -------------------------------------------
2374 -- Analyze_Overloaded_Selected_Component --
2375 -------------------------------------------
2376
2377 procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
fbf5a39b
AC
2378 Nam : constant Node_Id := Prefix (N);
2379 Sel : constant Node_Id := Selector_Name (N);
996ae0b0 2380 Comp : Entity_Id;
996ae0b0
RK
2381 I : Interp_Index;
2382 It : Interp;
2383 T : Entity_Id;
2384
2385 begin
4c46b835 2386 Set_Etype (Sel, Any_Type);
996ae0b0 2387
4c46b835 2388 Get_First_Interp (Nam, I, It);
996ae0b0
RK
2389 while Present (It.Typ) loop
2390 if Is_Access_Type (It.Typ) then
2391 T := Designated_Type (It.Typ);
fbf5a39b 2392 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
996ae0b0
RK
2393 else
2394 T := It.Typ;
2395 end if;
2396
2397 if Is_Record_Type (T) then
2398 Comp := First_Entity (T);
996ae0b0 2399 while Present (Comp) loop
996ae0b0
RK
2400 if Chars (Comp) = Chars (Sel)
2401 and then Is_Visible_Component (Comp)
2402 then
b67a385c 2403 Set_Entity (Sel, Comp);
996ae0b0
RK
2404 Set_Etype (Sel, Etype (Comp));
2405 Add_One_Interp (N, Etype (Comp), Etype (Comp));
2406
2407 -- This also specifies a candidate to resolve the name.
2408 -- Further overloading will be resolved from context.
2409
2410 Set_Etype (Nam, It.Typ);
2411 end if;
2412
2413 Next_Entity (Comp);
2414 end loop;
2415
2416 elsif Is_Concurrent_Type (T) then
2417 Comp := First_Entity (T);
996ae0b0
RK
2418 while Present (Comp)
2419 and then Comp /= First_Private_Entity (T)
2420 loop
2421 if Chars (Comp) = Chars (Sel) then
2422 if Is_Overloadable (Comp) then
2423 Add_One_Interp (Sel, Comp, Etype (Comp));
2424 else
2425 Set_Entity_With_Style_Check (Sel, Comp);
2426 Generate_Reference (Comp, Sel);
2427 end if;
2428
2429 Set_Etype (Sel, Etype (Comp));
2430 Set_Etype (N, Etype (Comp));
2431 Set_Etype (Nam, It.Typ);
2432
2433 -- For access type case, introduce explicit deference for
2434 -- more uniform treatment of entry calls.
2435
2436 if Is_Access_Type (Etype (Nam)) then
2437 Insert_Explicit_Dereference (Nam);
fbf5a39b
AC
2438 Error_Msg_NW
2439 (Warn_On_Dereference, "?implicit dereference", N);
996ae0b0
RK
2440 end if;
2441 end if;
2442
2443 Next_Entity (Comp);
2444 end loop;
2445
2446 Set_Is_Overloaded (N, Is_Overloaded (Sel));
996ae0b0
RK
2447 end if;
2448
2449 Get_Next_Interp (I, It);
2450 end loop;
2451
0a36105d
JM
2452 if Etype (N) = Any_Type
2453 and then not Try_Object_Operation (N)
2454 then
996ae0b0
RK
2455 Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
2456 Set_Entity (Sel, Any_Id);
2457 Set_Etype (Sel, Any_Type);
2458 end if;
996ae0b0
RK
2459 end Analyze_Overloaded_Selected_Component;
2460
2461 ----------------------------------
2462 -- Analyze_Qualified_Expression --
2463 ----------------------------------
2464
2465 procedure Analyze_Qualified_Expression (N : Node_Id) is
2466 Mark : constant Entity_Id := Subtype_Mark (N);
2467 T : Entity_Id;
2468
2469 begin
2470 Set_Etype (N, Any_Type);
2471 Find_Type (Mark);
2472 T := Entity (Mark);
2473
2474 if T = Any_Type then
2475 return;
2476 end if;
996ae0b0 2477
4c46b835 2478 Check_Fully_Declared (T, N);
996ae0b0
RK
2479 Analyze_Expression (Expression (N));
2480 Set_Etype (N, T);
2481 end Analyze_Qualified_Expression;
2482
2483 -------------------
2484 -- Analyze_Range --
2485 -------------------
2486
2487 procedure Analyze_Range (N : Node_Id) is
2488 L : constant Node_Id := Low_Bound (N);
2489 H : constant Node_Id := High_Bound (N);
2490 I1, I2 : Interp_Index;
2491 It1, It2 : Interp;
2492
2493 procedure Check_Common_Type (T1, T2 : Entity_Id);
2494 -- Verify the compatibility of two types, and choose the
2495 -- non universal one if the other is universal.
2496
2497 procedure Check_High_Bound (T : Entity_Id);
2498 -- Test one interpretation of the low bound against all those
2499 -- of the high bound.
2500
fbf5a39b
AC
2501 procedure Check_Universal_Expression (N : Node_Id);
2502 -- In Ada83, reject bounds of a universal range that are not
2503 -- literals or entity names.
2504
996ae0b0
RK
2505 -----------------------
2506 -- Check_Common_Type --
2507 -----------------------
2508
2509 procedure Check_Common_Type (T1, T2 : Entity_Id) is
2510 begin
2511 if Covers (T1, T2) or else Covers (T2, T1) then
2512 if T1 = Universal_Integer
2513 or else T1 = Universal_Real
2514 or else T1 = Any_Character
2515 then
2516 Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
2517
fbf5a39b 2518 elsif T1 = T2 then
996ae0b0
RK
2519 Add_One_Interp (N, T1, T1);
2520
2521 else
2522 Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
2523 end if;
2524 end if;
2525 end Check_Common_Type;
2526
2527 ----------------------
2528 -- Check_High_Bound --
2529 ----------------------
2530
2531 procedure Check_High_Bound (T : Entity_Id) is
2532 begin
2533 if not Is_Overloaded (H) then
2534 Check_Common_Type (T, Etype (H));
2535 else
2536 Get_First_Interp (H, I2, It2);
996ae0b0
RK
2537 while Present (It2.Typ) loop
2538 Check_Common_Type (T, It2.Typ);
2539 Get_Next_Interp (I2, It2);
2540 end loop;
2541 end if;
2542 end Check_High_Bound;
2543
fbf5a39b
AC
2544 -----------------------------
2545 -- Is_Universal_Expression --
2546 -----------------------------
2547
2548 procedure Check_Universal_Expression (N : Node_Id) is
2549 begin
2550 if Etype (N) = Universal_Integer
2551 and then Nkind (N) /= N_Integer_Literal
2552 and then not Is_Entity_Name (N)
2553 and then Nkind (N) /= N_Attribute_Reference
2554 then
2555 Error_Msg_N ("illegal bound in discrete range", N);
2556 end if;
2557 end Check_Universal_Expression;
2558
996ae0b0
RK
2559 -- Start of processing for Analyze_Range
2560
2561 begin
2562 Set_Etype (N, Any_Type);
2563 Analyze_Expression (L);
2564 Analyze_Expression (H);
2565
2566 if Etype (L) = Any_Type or else Etype (H) = Any_Type then
2567 return;
2568
2569 else
2570 if not Is_Overloaded (L) then
2571 Check_High_Bound (Etype (L));
2572 else
2573 Get_First_Interp (L, I1, It1);
996ae0b0
RK
2574 while Present (It1.Typ) loop
2575 Check_High_Bound (It1.Typ);
2576 Get_Next_Interp (I1, It1);
2577 end loop;
2578 end if;
2579
2580 -- If result is Any_Type, then we did not find a compatible pair
2581
2582 if Etype (N) = Any_Type then
2583 Error_Msg_N ("incompatible types in range ", N);
2584 end if;
2585 end if;
fbf5a39b 2586
0ab80019 2587 if Ada_Version = Ada_83
fbf5a39b
AC
2588 and then
2589 (Nkind (Parent (N)) = N_Loop_Parameter_Specification
4c46b835 2590 or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
fbf5a39b
AC
2591 then
2592 Check_Universal_Expression (L);
2593 Check_Universal_Expression (H);
2594 end if;
996ae0b0
RK
2595 end Analyze_Range;
2596
2597 -----------------------
2598 -- Analyze_Reference --
2599 -----------------------
2600
2601 procedure Analyze_Reference (N : Node_Id) is
2602 P : constant Node_Id := Prefix (N);
2603 Acc_Type : Entity_Id;
996ae0b0
RK
2604 begin
2605 Analyze (P);
2606 Acc_Type := Create_Itype (E_Allocator_Type, N);
2607 Set_Etype (Acc_Type, Acc_Type);
2608 Init_Size_Align (Acc_Type);
2609 Set_Directly_Designated_Type (Acc_Type, Etype (P));
2610 Set_Etype (N, Acc_Type);
2611 end Analyze_Reference;
2612
2613 --------------------------------
2614 -- Analyze_Selected_Component --
2615 --------------------------------
2616
2617 -- Prefix is a record type or a task or protected type. In the
2618 -- later case, the selector must denote a visible entry.
2619
2620 procedure Analyze_Selected_Component (N : Node_Id) is
2621 Name : constant Node_Id := Prefix (N);
2622 Sel : constant Node_Id := Selector_Name (N);
2623 Comp : Entity_Id;
996ae0b0 2624 Prefix_Type : Entity_Id;
401093c1
ES
2625
2626 Type_To_Use : Entity_Id;
2627 -- In most cases this is the Prefix_Type, but if the Prefix_Type is
2628 -- a class-wide type, we use its root type, whose components are
2629 -- present in the class-wide type.
2630
6e73e3ab 2631 Pent : Entity_Id := Empty;
996ae0b0
RK
2632 Act_Decl : Node_Id;
2633 In_Scope : Boolean;
2634 Parent_N : Node_Id;
2635
2636 -- Start of processing for Analyze_Selected_Component
2637
2638 begin
2639 Set_Etype (N, Any_Type);
2640
2641 if Is_Overloaded (Name) then
2642 Analyze_Overloaded_Selected_Component (N);
2643 return;
2644
2645 elsif Etype (Name) = Any_Type then
2646 Set_Entity (Sel, Any_Id);
2647 Set_Etype (Sel, Any_Type);
2648 return;
2649
2650 else
996ae0b0
RK
2651 Prefix_Type := Etype (Name);
2652 end if;
2653
2654 if Is_Access_Type (Prefix_Type) then
07fc65c4
GB
2655
2656 -- A RACW object can never be used as prefix of a selected
2657 -- component since that means it is dereferenced without
2658 -- being a controlling operand of a dispatching operation
2659 -- (RM E.2.2(15)).
2660
996ae0b0
RK
2661 if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
2662 and then Comes_From_Source (N)
2663 then
996ae0b0
RK
2664 Error_Msg_N
2665 ("invalid dereference of a remote access to class-wide value",
2666 N);
07fc65c4
GB
2667
2668 -- Normal case of selected component applied to access type
2669
2670 else
fbf5a39b 2671 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
da709d08 2672
6e73e3ab
AC
2673 if Is_Entity_Name (Name) then
2674 Pent := Entity (Name);
2675 elsif Nkind (Name) = N_Selected_Component
2676 and then Is_Entity_Name (Selector_Name (Name))
2677 then
2678 Pent := Entity (Selector_Name (Name));
2679 end if;
da709d08 2680
6e73e3ab 2681 Process_Implicit_Dereference_Prefix (Pent, Name);
996ae0b0 2682 end if;
07fc65c4 2683
996ae0b0 2684 Prefix_Type := Designated_Type (Prefix_Type);
b67a385c 2685
aab883ec 2686 end if;
b67a385c 2687
aab883ec
ES
2688 -- (Ada 2005): if the prefix is the limited view of a type, and
2689 -- the context already includes the full view, use the full view
2690 -- in what follows, either to retrieve a component of to find
2691 -- a primitive operation. If the prefix is an explicit dereference,
2692 -- set the type of the prefix to reflect this transformation.
401093c1
ES
2693 -- If the non-limited view is itself an incomplete type, get the
2694 -- full view if available.
aab883ec
ES
2695
2696 if Is_Incomplete_Type (Prefix_Type)
2697 and then From_With_Type (Prefix_Type)
2698 and then Present (Non_Limited_View (Prefix_Type))
2699 then
401093c1 2700 Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
aab883ec
ES
2701
2702 if Nkind (N) = N_Explicit_Dereference then
2703 Set_Etype (Prefix (N), Prefix_Type);
2704 end if;
2705
2706 elsif Ekind (Prefix_Type) = E_Class_Wide_Type
2707 and then From_With_Type (Prefix_Type)
2708 and then Present (Non_Limited_View (Etype (Prefix_Type)))
2709 then
2710 Prefix_Type :=
2711 Class_Wide_Type (Non_Limited_View (Etype (Prefix_Type)));
2712
2713 if Nkind (N) = N_Explicit_Dereference then
2714 Set_Etype (Prefix (N), Prefix_Type);
b67a385c 2715 end if;
996ae0b0
RK
2716 end if;
2717
2718 if Ekind (Prefix_Type) = E_Private_Subtype then
2719 Prefix_Type := Base_Type (Prefix_Type);
2720 end if;
2721
401093c1 2722 Type_To_Use := Prefix_Type;
996ae0b0
RK
2723
2724 -- For class-wide types, use the entity list of the root type. This
2725 -- indirection is specially important for private extensions because
2726 -- only the root type get switched (not the class-wide type).
2727
2728 if Is_Class_Wide_Type (Prefix_Type) then
401093c1 2729 Type_To_Use := Root_Type (Prefix_Type);
996ae0b0
RK
2730 end if;
2731
401093c1 2732 Comp := First_Entity (Type_To_Use);
996ae0b0
RK
2733
2734 -- If the selector has an original discriminant, the node appears in
2735 -- an instance. Replace the discriminant with the corresponding one
2736 -- in the current discriminated type. For nested generics, this must
2737 -- be done transitively, so note the new original discriminant.
2738
2739 if Nkind (Sel) = N_Identifier
2740 and then Present (Original_Discriminant (Sel))
2741 then
2742 Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
2743
2744 -- Mark entity before rewriting, for completeness and because
2745 -- subsequent semantic checks might examine the original node.
2746
2747 Set_Entity (Sel, Comp);
2748 Rewrite (Selector_Name (N),
2749 New_Occurrence_Of (Comp, Sloc (N)));
2750 Set_Original_Discriminant (Selector_Name (N), Comp);
2751 Set_Etype (N, Etype (Comp));
2752
2753 if Is_Access_Type (Etype (Name)) then
2754 Insert_Explicit_Dereference (Name);
fbf5a39b 2755 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
996ae0b0
RK
2756 end if;
2757
2758 elsif Is_Record_Type (Prefix_Type) then
2759
2760 -- Find component with given name
2761
2762 while Present (Comp) loop
996ae0b0
RK
2763 if Chars (Comp) = Chars (Sel)
2764 and then Is_Visible_Component (Comp)
2765 then
2766 Set_Entity_With_Style_Check (Sel, Comp);
996ae0b0
RK
2767 Set_Etype (Sel, Etype (Comp));
2768
2769 if Ekind (Comp) = E_Discriminant then
5d09245e 2770 if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
996ae0b0
RK
2771 Error_Msg_N
2772 ("cannot reference discriminant of Unchecked_Union",
2773 Sel);
2774 end if;
2775
2776 if Is_Generic_Type (Prefix_Type)
2777 or else
2778 Is_Generic_Type (Root_Type (Prefix_Type))
2779 then
2780 Set_Original_Discriminant (Sel, Comp);
2781 end if;
2782 end if;
2783
2784 -- Resolve the prefix early otherwise it is not possible to
2785 -- build the actual subtype of the component: it may need
2786 -- to duplicate this prefix and duplication is only allowed
2787 -- on fully resolved expressions.
2788
fbf5a39b 2789 Resolve (Name);
996ae0b0 2790
b67a385c
ES
2791 -- Ada 2005 (AI-50217): Check wrong use of incomplete types or
2792 -- subtypes in a package specification.
28be29ce
ES
2793 -- Example:
2794
2795 -- limited with Pkg;
2796 -- package Pkg is
2797 -- type Acc_Inc is access Pkg.T;
2798 -- X : Acc_Inc;
b67a385c
ES
2799 -- N : Natural := X.all.Comp; -- ERROR, limited view
2800 -- end Pkg; -- Comp is not visible
28be29ce
ES
2801
2802 if Nkind (Name) = N_Explicit_Dereference
2803 and then From_With_Type (Etype (Prefix (Name)))
2804 and then not Is_Potentially_Use_Visible (Etype (Name))
b67a385c
ES
2805 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
2806 N_Package_Specification
28be29ce
ES
2807 then
2808 Error_Msg_NE
2809 ("premature usage of incomplete}", Prefix (Name),
2810 Etype (Prefix (Name)));
2811 end if;
2812
996ae0b0
RK
2813 -- We never need an actual subtype for the case of a selection
2814 -- for a indexed component of a non-packed array, since in
2815 -- this case gigi generates all the checks and can find the
2816 -- necessary bounds information.
2817
2818 -- We also do not need an actual subtype for the case of
2819 -- a first, last, length, or range attribute applied to a
2820 -- non-packed array, since gigi can again get the bounds in
2821 -- these cases (gigi cannot handle the packed case, since it
2822 -- has the bounds of the packed array type, not the original
2823 -- bounds of the type). However, if the prefix is itself a
2824 -- selected component, as in a.b.c (i), gigi may regard a.b.c
2825 -- as a dynamic-sized temporary, so we do generate an actual
2826 -- subtype for this case.
2827
2828 Parent_N := Parent (N);
2829
2830 if not Is_Packed (Etype (Comp))
2831 and then
2832 ((Nkind (Parent_N) = N_Indexed_Component
2833 and then Nkind (Name) /= N_Selected_Component)
2834 or else
2835 (Nkind (Parent_N) = N_Attribute_Reference
2836 and then (Attribute_Name (Parent_N) = Name_First
ffe9aba8 2837 or else
996ae0b0 2838 Attribute_Name (Parent_N) = Name_Last
ffe9aba8 2839 or else
996ae0b0 2840 Attribute_Name (Parent_N) = Name_Length
ffe9aba8 2841 or else
996ae0b0
RK
2842 Attribute_Name (Parent_N) = Name_Range)))
2843 then
2844 Set_Etype (N, Etype (Comp));
2845
98123480
ES
2846 -- If full analysis is not enabled, we do not generate an
2847 -- actual subtype, because in the absence of expansion
2848 -- reference to a formal of a protected type, for example,
2849 -- will not be properly transformed, and will lead to
2850 -- out-of-scope references in gigi.
2851
2852 -- In all other cases, we currently build an actual subtype.
2853 -- It seems likely that many of these cases can be avoided,
2854 -- but right now, the front end makes direct references to the
fbf5a39b 2855 -- bounds (e.g. in generating a length check), and if we do
996ae0b0 2856 -- not make an actual subtype, we end up getting a direct
98123480 2857 -- reference to a discriminant, which will not do.
996ae0b0 2858
98123480 2859 elsif Full_Analysis then
996ae0b0
RK
2860 Act_Decl :=
2861 Build_Actual_Subtype_Of_Component (Etype (Comp), N);
2862 Insert_Action (N, Act_Decl);
2863
2864 if No (Act_Decl) then
2865 Set_Etype (N, Etype (Comp));
2866
2867 else
2868 -- Component type depends on discriminants. Enter the
2869 -- main attributes of the subtype.
2870
2871 declare
fbf5a39b
AC
2872 Subt : constant Entity_Id :=
2873 Defining_Identifier (Act_Decl);
996ae0b0
RK
2874
2875 begin
2876 Set_Etype (Subt, Base_Type (Etype (Comp)));
2877 Set_Ekind (Subt, Ekind (Etype (Comp)));
2878 Set_Etype (N, Subt);
2879 end;
2880 end if;
98123480
ES
2881
2882 -- If Full_Analysis not enabled, just set the Etype
2883
2884 else
2885 Set_Etype (N, Etype (Comp));
996ae0b0
RK
2886 end if;
2887
2888 return;
2889 end if;
2890
aab883ec
ES
2891 -- If the prefix is a private extension, check only the visible
2892 -- components of the partial view.
2893
401093c1
ES
2894 if Ekind (Type_To_Use) = E_Record_Type_With_Private then
2895 exit when Comp = Last_Entity (Type_To_Use);
aab883ec
ES
2896 end if;
2897
996ae0b0
RK
2898 Next_Entity (Comp);
2899 end loop;
2900
35ae2ed8
AC
2901 -- Ada 2005 (AI-252)
2902
2903 if Ada_Version >= Ada_05
2904 and then Is_Tagged_Type (Prefix_Type)
2905 and then Try_Object_Operation (N)
2906 then
2907 return;
4c46b835 2908
98123480
ES
2909 -- If the transformation fails, it will be necessary to redo the
2910 -- analysis with all errors enabled, to indicate candidate
2911 -- interpretations and reasons for each failure ???
4c46b835 2912
35ae2ed8
AC
2913 end if;
2914
996ae0b0 2915 elsif Is_Private_Type (Prefix_Type) then
98123480
ES
2916 -- Allow access only to discriminants of the type. If the type has
2917 -- no full view, gigi uses the parent type for the components, so we
2918 -- do the same here.
996ae0b0
RK
2919
2920 if No (Full_View (Prefix_Type)) then
401093c1
ES
2921 Type_To_Use := Root_Type (Base_Type (Prefix_Type));
2922 Comp := First_Entity (Type_To_Use);
996ae0b0
RK
2923 end if;
2924
2925 while Present (Comp) loop
996ae0b0
RK
2926 if Chars (Comp) = Chars (Sel) then
2927 if Ekind (Comp) = E_Discriminant then
2928 Set_Entity_With_Style_Check (Sel, Comp);
2929 Generate_Reference (Comp, Sel);
2930
2931 Set_Etype (Sel, Etype (Comp));
2932 Set_Etype (N, Etype (Comp));
2933
2934 if Is_Generic_Type (Prefix_Type)
2935 or else
2936 Is_Generic_Type (Root_Type (Prefix_Type))
2937 then
2938 Set_Original_Discriminant (Sel, Comp);
2939 end if;
2940
aab883ec
ES
2941 -- Before declararing an error, check whether this is tagged
2942 -- private type and a call to a primitive operation.
2943
2944 elsif Ada_Version >= Ada_05
2945 and then Is_Tagged_Type (Prefix_Type)
2946 and then Try_Object_Operation (N)
2947 then
2948 return;
2949
996ae0b0
RK
2950 else
2951 Error_Msg_NE
2952 ("invisible selector for }",
2953 N, First_Subtype (Prefix_Type));
2954 Set_Entity (Sel, Any_Id);
2955 Set_Etype (N, Any_Type);
2956 end if;
2957
2958 return;
2959 end if;
2960
2961 Next_Entity (Comp);
2962 end loop;
2963
2964 elsif Is_Concurrent_Type (Prefix_Type) then
2965
2966 -- Prefix is concurrent type. Find visible operation with given name
98123480
ES
2967 -- For a task, this can only include entries or discriminants if the
2968 -- task type is not an enclosing scope. If it is an enclosing scope
2969 -- (e.g. in an inner task) then all entities are visible, but the
2970 -- prefix must denote the enclosing scope, i.e. can only be a direct
2971 -- name or an expanded name.
996ae0b0
RK
2972
2973 Set_Etype (Sel, Any_Type);
2974 In_Scope := In_Open_Scopes (Prefix_Type);
2975
2976 while Present (Comp) loop
2977 if Chars (Comp) = Chars (Sel) then
2978 if Is_Overloadable (Comp) then
2979 Add_One_Interp (Sel, Comp, Etype (Comp));
2980
2981 elsif Ekind (Comp) = E_Discriminant
2982 or else Ekind (Comp) = E_Entry_Family
2983 or else (In_Scope
2984 and then Is_Entity_Name (Name))
2985 then
2986 Set_Entity_With_Style_Check (Sel, Comp);
2987 Generate_Reference (Comp, Sel);
2988
2989 else
2990 goto Next_Comp;
2991 end if;
2992
2993 Set_Etype (Sel, Etype (Comp));
2994 Set_Etype (N, Etype (Comp));
2995
2996 if Ekind (Comp) = E_Discriminant then
2997 Set_Original_Discriminant (Sel, Comp);
2998 end if;
2999
98123480
ES
3000 -- For access type case, introduce explicit deference for more
3001 -- uniform treatment of entry calls.
996ae0b0
RK
3002
3003 if Is_Access_Type (Etype (Name)) then
3004 Insert_Explicit_Dereference (Name);
fbf5a39b
AC
3005 Error_Msg_NW
3006 (Warn_On_Dereference, "?implicit dereference", N);
996ae0b0
RK
3007 end if;
3008 end if;
3009
3010 <<Next_Comp>>
3011 Next_Entity (Comp);
3012 exit when not In_Scope
9bc856dd
AC
3013 and then
3014 Comp = First_Private_Entity (Base_Type (Prefix_Type));
996ae0b0
RK
3015 end loop;
3016
aab883ec
ES
3017 -- If there is no visible entry with the given name, and the task
3018 -- implements an interface, check whether there is some other
3019 -- primitive operation with that name.
3020
0a36105d 3021 if Ada_Version >= Ada_05
aab883ec 3022 and then Is_Tagged_Type (Prefix_Type)
aab883ec 3023 then
0a36105d
JM
3024 if Etype (N) = Any_Type
3025 and then Try_Object_Operation (N)
3026 then
3027 return;
3028
3029 -- If the context is not syntactically a procedure call, it
3030 -- may be a call to a primitive function declared outside of
3031 -- the synchronized type.
3032
3033 -- If the context is a procedure call, there might still be
3034 -- an overloading between an entry and a primitive procedure
3035 -- declared outside of the synchronized type, called in prefix
3036 -- notation. This is harder to disambiguate because in one case
3037 -- the controlling formal is implicit ???
3038
3039 elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
3040 and then Try_Object_Operation (N)
3041 then
3042 return;
3043 end if;
aab883ec
ES
3044 end if;
3045
996ae0b0
RK
3046 Set_Is_Overloaded (N, Is_Overloaded (Sel));
3047
3048 else
3049 -- Invalid prefix
3050
3051 Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
3052 end if;
3053
4c46b835 3054 -- If N still has no type, the component is not defined in the prefix
996ae0b0
RK
3055
3056 if Etype (N) = Any_Type then
3057
98123480
ES
3058 -- If the prefix is a single concurrent object, use its name in the
3059 -- error message, rather than that of its anonymous type.
996ae0b0
RK
3060
3061 if Is_Concurrent_Type (Prefix_Type)
3062 and then Is_Internal_Name (Chars (Prefix_Type))
3063 and then not Is_Derived_Type (Prefix_Type)
3064 and then Is_Entity_Name (Name)
3065 then
3066
3067 Error_Msg_Node_2 := Entity (Name);
3068 Error_Msg_NE ("no selector& for&", N, Sel);
3069
401093c1 3070 Check_Misspelled_Selector (Type_To_Use, Sel);
996ae0b0 3071
de76a39c
GB
3072 elsif Is_Generic_Type (Prefix_Type)
3073 and then Ekind (Prefix_Type) = E_Record_Type_With_Private
07fc65c4 3074 and then Prefix_Type /= Etype (Prefix_Type)
de76a39c
GB
3075 and then Is_Record_Type (Etype (Prefix_Type))
3076 then
98123480 3077 -- If this is a derived formal type, the parent may have
de76a39c
GB
3078 -- different visibility at this point. Try for an inherited
3079 -- component before reporting an error.
3080
3081 Set_Etype (Prefix (N), Etype (Prefix_Type));
3082 Analyze_Selected_Component (N);
3083 return;
3084
fbf5a39b
AC
3085 elsif Ekind (Prefix_Type) = E_Record_Subtype_With_Private
3086 and then Is_Generic_Actual_Type (Prefix_Type)
3087 and then Present (Full_View (Prefix_Type))
3088 then
4c46b835
AC
3089 -- Similarly, if this the actual for a formal derived type, the
3090 -- component inherited from the generic parent may not be visible
3091 -- in the actual, but the selected component is legal.
fbf5a39b
AC
3092
3093 declare
3094 Comp : Entity_Id;
4c46b835 3095
fbf5a39b
AC
3096 begin
3097 Comp :=
3098 First_Component (Generic_Parent_Type (Parent (Prefix_Type)));
fbf5a39b
AC
3099 while Present (Comp) loop
3100 if Chars (Comp) = Chars (Sel) then
3101 Set_Entity_With_Style_Check (Sel, Comp);
3102 Set_Etype (Sel, Etype (Comp));
3103 Set_Etype (N, Etype (Comp));
69e6a03e 3104 return;
fbf5a39b
AC
3105 end if;
3106
3107 Next_Component (Comp);
3108 end loop;
3109
3110 pragma Assert (Etype (N) /= Any_Type);
3111 end;
3112
996ae0b0
RK
3113 else
3114 if Ekind (Prefix_Type) = E_Record_Subtype then
3115
3116 -- Check whether this is a component of the base type
3117 -- which is absent from a statically constrained subtype.
3118 -- This will raise constraint error at run-time, but is
3119 -- not a compile-time error. When the selector is illegal
3120 -- for base type as well fall through and generate a
3121 -- compilation error anyway.
3122
3123 Comp := First_Component (Base_Type (Prefix_Type));
996ae0b0 3124 while Present (Comp) loop
996ae0b0
RK
3125 if Chars (Comp) = Chars (Sel)
3126 and then Is_Visible_Component (Comp)
3127 then
3128 Set_Entity_With_Style_Check (Sel, Comp);
3129 Generate_Reference (Comp, Sel);
3130 Set_Etype (Sel, Etype (Comp));
3131 Set_Etype (N, Etype (Comp));
3132
3133 -- Emit appropriate message. Gigi will replace the
3134 -- node subsequently with the appropriate Raise.
3135
3136 Apply_Compile_Time_Constraint_Error
3137 (N, "component not present in }?",
07fc65c4 3138 CE_Discriminant_Check_Failed,
996ae0b0
RK
3139 Ent => Prefix_Type, Rep => False);
3140 Set_Raises_Constraint_Error (N);
3141 return;
3142 end if;
3143
3144 Next_Component (Comp);
3145 end loop;
3146
3147 end if;
3148
3149 Error_Msg_Node_2 := First_Subtype (Prefix_Type);
3150 Error_Msg_NE ("no selector& for}", N, Sel);
3151
401093c1 3152 Check_Misspelled_Selector (Type_To_Use, Sel);
996ae0b0
RK
3153
3154 end if;
3155
3156 Set_Entity (Sel, Any_Id);
3157 Set_Etype (Sel, Any_Type);
3158 end if;
3159 end Analyze_Selected_Component;
3160
3161 ---------------------------
3162 -- Analyze_Short_Circuit --
3163 ---------------------------
3164
3165 procedure Analyze_Short_Circuit (N : Node_Id) is
3166 L : constant Node_Id := Left_Opnd (N);
3167 R : constant Node_Id := Right_Opnd (N);
3168 Ind : Interp_Index;
3169 It : Interp;
3170
3171 begin
3172 Analyze_Expression (L);
3173 Analyze_Expression (R);
3174 Set_Etype (N, Any_Type);
3175
3176 if not Is_Overloaded (L) then
3177
3178 if Root_Type (Etype (L)) = Standard_Boolean
3179 and then Has_Compatible_Type (R, Etype (L))
3180 then
3181 Add_One_Interp (N, Etype (L), Etype (L));
3182 end if;
3183
3184 else
3185 Get_First_Interp (L, Ind, It);
996ae0b0
RK
3186 while Present (It.Typ) loop
3187 if Root_Type (It.Typ) = Standard_Boolean
3188 and then Has_Compatible_Type (R, It.Typ)
3189 then
3190 Add_One_Interp (N, It.Typ, It.Typ);
3191 end if;
3192
3193 Get_Next_Interp (Ind, It);
3194 end loop;
3195 end if;
3196
3197 -- Here we have failed to find an interpretation. Clearly we
3198 -- know that it is not the case that both operands can have
3199 -- an interpretation of Boolean, but this is by far the most
3200 -- likely intended interpretation. So we simply resolve both
3201 -- operands as Booleans, and at least one of these resolutions
3202 -- will generate an error message, and we do not need to give
3203 -- a further error message on the short circuit operation itself.
3204
3205 if Etype (N) = Any_Type then
3206 Resolve (L, Standard_Boolean);
3207 Resolve (R, Standard_Boolean);
3208 Set_Etype (N, Standard_Boolean);
3209 end if;
3210 end Analyze_Short_Circuit;
3211
3212 -------------------
3213 -- Analyze_Slice --
3214 -------------------
3215
3216 procedure Analyze_Slice (N : Node_Id) is
3217 P : constant Node_Id := Prefix (N);
3218 D : constant Node_Id := Discrete_Range (N);
3219 Array_Type : Entity_Id;
3220
3221 procedure Analyze_Overloaded_Slice;
3222 -- If the prefix is overloaded, select those interpretations that
3223 -- yield a one-dimensional array type.
3224
4c46b835
AC
3225 ------------------------------
3226 -- Analyze_Overloaded_Slice --
3227 ------------------------------
3228
996ae0b0
RK
3229 procedure Analyze_Overloaded_Slice is
3230 I : Interp_Index;
3231 It : Interp;
3232 Typ : Entity_Id;
3233
3234 begin
3235 Set_Etype (N, Any_Type);
996ae0b0 3236
4c46b835 3237 Get_First_Interp (P, I, It);
996ae0b0
RK
3238 while Present (It.Nam) loop
3239 Typ := It.Typ;
3240
3241 if Is_Access_Type (Typ) then
3242 Typ := Designated_Type (Typ);
fbf5a39b 3243 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
996ae0b0
RK
3244 end if;
3245
3246 if Is_Array_Type (Typ)
3247 and then Number_Dimensions (Typ) = 1
3248 and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
3249 then
3250 Add_One_Interp (N, Typ, Typ);
3251 end if;
3252
3253 Get_Next_Interp (I, It);
3254 end loop;
3255
3256 if Etype (N) = Any_Type then
3257 Error_Msg_N ("expect array type in prefix of slice", N);
3258 end if;
3259 end Analyze_Overloaded_Slice;
3260
3261 -- Start of processing for Analyze_Slice
3262
3263 begin
523456db 3264 Analyze (P);
996ae0b0
RK
3265 Analyze (D);
3266
3267 if Is_Overloaded (P) then
3268 Analyze_Overloaded_Slice;
3269
3270 else
3271 Array_Type := Etype (P);
3272 Set_Etype (N, Any_Type);
3273
3274 if Is_Access_Type (Array_Type) then
3275 Array_Type := Designated_Type (Array_Type);
fbf5a39b 3276 Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
996ae0b0
RK
3277 end if;
3278
3279 if not Is_Array_Type (Array_Type) then
3280 Wrong_Type (P, Any_Array);
3281
3282 elsif Number_Dimensions (Array_Type) > 1 then
3283 Error_Msg_N
3284 ("type is not one-dimensional array in slice prefix", N);
3285
3286 elsif not
3287 Has_Compatible_Type (D, Etype (First_Index (Array_Type)))
3288 then
3289 Wrong_Type (D, Etype (First_Index (Array_Type)));
3290
3291 else
3292 Set_Etype (N, Array_Type);
3293 end if;
3294 end if;
3295 end Analyze_Slice;
3296
3297 -----------------------------
3298 -- Analyze_Type_Conversion --
3299 -----------------------------
3300
3301 procedure Analyze_Type_Conversion (N : Node_Id) is
3302 Expr : constant Node_Id := Expression (N);
3303 T : Entity_Id;
3304
3305 begin
3306 -- If Conversion_OK is set, then the Etype is already set, and the
3307 -- only processing required is to analyze the expression. This is
3308 -- used to construct certain "illegal" conversions which are not
3309 -- allowed by Ada semantics, but can be handled OK by Gigi, see
3310 -- Sinfo for further details.
3311
3312 if Conversion_OK (N) then
3313 Analyze (Expr);
3314 return;
3315 end if;
3316
3317 -- Otherwise full type analysis is required, as well as some semantic
3318 -- checks to make sure the argument of the conversion is appropriate.
3319
3320 Find_Type (Subtype_Mark (N));
3321 T := Entity (Subtype_Mark (N));
3322 Set_Etype (N, T);
3323 Check_Fully_Declared (T, N);
3324 Analyze_Expression (Expr);
3325 Validate_Remote_Type_Type_Conversion (N);
3326
3327 -- Only remaining step is validity checks on the argument. These
3328 -- are skipped if the conversion does not come from the source.
3329
3330 if not Comes_From_Source (N) then
3331 return;
3332
b67a385c
ES
3333 -- If there was an error in a generic unit, no need to replicate the
3334 -- error message. Conversely, constant-folding in the generic may
3335 -- transform the argument of a conversion into a string literal, which
3336 -- is legal. Therefore the following tests are not performed in an
3337 -- instance.
3338
3339 elsif In_Instance then
3340 return;
3341
996ae0b0
RK
3342 elsif Nkind (Expr) = N_Null then
3343 Error_Msg_N ("argument of conversion cannot be null", N);
3344 Error_Msg_N ("\use qualified expression instead", N);
3345 Set_Etype (N, Any_Type);
3346
3347 elsif Nkind (Expr) = N_Aggregate then
3348 Error_Msg_N ("argument of conversion cannot be aggregate", N);
3349 Error_Msg_N ("\use qualified expression instead", N);
3350
3351 elsif Nkind (Expr) = N_Allocator then
3352 Error_Msg_N ("argument of conversion cannot be an allocator", N);
3353 Error_Msg_N ("\use qualified expression instead", N);
3354
3355 elsif Nkind (Expr) = N_String_Literal then
3356 Error_Msg_N ("argument of conversion cannot be string literal", N);
3357 Error_Msg_N ("\use qualified expression instead", N);
3358
3359 elsif Nkind (Expr) = N_Character_Literal then
0ab80019 3360 if Ada_Version = Ada_83 then
996ae0b0
RK
3361 Resolve (Expr, T);
3362 else
3363 Error_Msg_N ("argument of conversion cannot be character literal",
3364 N);
3365 Error_Msg_N ("\use qualified expression instead", N);
3366 end if;
3367
3368 elsif Nkind (Expr) = N_Attribute_Reference
3369 and then
3370 (Attribute_Name (Expr) = Name_Access or else
3371 Attribute_Name (Expr) = Name_Unchecked_Access or else
3372 Attribute_Name (Expr) = Name_Unrestricted_Access)
3373 then
3374 Error_Msg_N ("argument of conversion cannot be access", N);
3375 Error_Msg_N ("\use qualified expression instead", N);
3376 end if;
996ae0b0
RK
3377 end Analyze_Type_Conversion;
3378
3379 ----------------------
3380 -- Analyze_Unary_Op --
3381 ----------------------
3382
3383 procedure Analyze_Unary_Op (N : Node_Id) is
3384 R : constant Node_Id := Right_Opnd (N);
3385 Op_Id : Entity_Id := Entity (N);
3386
3387 begin
3388 Set_Etype (N, Any_Type);
3389 Candidate_Type := Empty;
3390
3391 Analyze_Expression (R);
3392
3393 if Present (Op_Id) then
3394 if Ekind (Op_Id) = E_Operator then
3395 Find_Unary_Types (R, Op_Id, N);
3396 else
3397 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3398 end if;
3399
3400 else
3401 Op_Id := Get_Name_Entity_Id (Chars (N));
996ae0b0 3402 while Present (Op_Id) loop
996ae0b0
RK
3403 if Ekind (Op_Id) = E_Operator then
3404 if No (Next_Entity (First_Entity (Op_Id))) then
3405 Find_Unary_Types (R, Op_Id, N);
3406 end if;
3407
3408 elsif Is_Overloadable (Op_Id) then
3409 Analyze_User_Defined_Unary_Op (N, Op_Id);
3410 end if;
3411
3412 Op_Id := Homonym (Op_Id);
3413 end loop;
3414 end if;
3415
3416 Operator_Check (N);
3417 end Analyze_Unary_Op;
3418
3419 ----------------------------------
3420 -- Analyze_Unchecked_Expression --
3421 ----------------------------------
3422
3423 procedure Analyze_Unchecked_Expression (N : Node_Id) is
3424 begin
3425 Analyze (Expression (N), Suppress => All_Checks);
3426 Set_Etype (N, Etype (Expression (N)));
3427 Save_Interps (Expression (N), N);
3428 end Analyze_Unchecked_Expression;
3429
3430 ---------------------------------------
3431 -- Analyze_Unchecked_Type_Conversion --
3432 ---------------------------------------
3433
3434 procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
3435 begin
3436 Find_Type (Subtype_Mark (N));
3437 Analyze_Expression (Expression (N));
3438 Set_Etype (N, Entity (Subtype_Mark (N)));
3439 end Analyze_Unchecked_Type_Conversion;
3440
3441 ------------------------------------
3442 -- Analyze_User_Defined_Binary_Op --
3443 ------------------------------------
3444
3445 procedure Analyze_User_Defined_Binary_Op
3446 (N : Node_Id;
3447 Op_Id : Entity_Id)
3448 is
3449 begin
3450 -- Only do analysis if the operator Comes_From_Source, since otherwise
3451 -- the operator was generated by the expander, and all such operators
3452 -- always refer to the operators in package Standard.
3453
3454 if Comes_From_Source (N) then
3455 declare
3456 F1 : constant Entity_Id := First_Formal (Op_Id);
3457 F2 : constant Entity_Id := Next_Formal (F1);
3458
3459 begin
3460 -- Verify that Op_Id is a visible binary function. Note that since
3461 -- we know Op_Id is overloaded, potentially use visible means use
3462 -- visible for sure (RM 9.4(11)).
3463
3464 if Ekind (Op_Id) = E_Function
3465 and then Present (F2)
3466 and then (Is_Immediately_Visible (Op_Id)
3467 or else Is_Potentially_Use_Visible (Op_Id))
3468 and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
3469 and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
3470 then
3471 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3472
3473 if Debug_Flag_E then
3474 Write_Str ("user defined operator ");
3475 Write_Name (Chars (Op_Id));
3476 Write_Str (" on node ");
3477 Write_Int (Int (N));
3478 Write_Eol;
3479 end if;
3480 end if;
3481 end;
3482 end if;
3483 end Analyze_User_Defined_Binary_Op;
3484
3485 -----------------------------------
3486 -- Analyze_User_Defined_Unary_Op --
3487 -----------------------------------
3488
3489 procedure Analyze_User_Defined_Unary_Op
3490 (N : Node_Id;
3491 Op_Id : Entity_Id)
3492 is
3493 begin
3494 -- Only do analysis if the operator Comes_From_Source, since otherwise
3495 -- the operator was generated by the expander, and all such operators
3496 -- always refer to the operators in package Standard.
3497
3498 if Comes_From_Source (N) then
3499 declare
3500 F : constant Entity_Id := First_Formal (Op_Id);
3501
3502 begin
3503 -- Verify that Op_Id is a visible unary function. Note that since
3504 -- we know Op_Id is overloaded, potentially use visible means use
3505 -- visible for sure (RM 9.4(11)).
3506
3507 if Ekind (Op_Id) = E_Function
3508 and then No (Next_Formal (F))
3509 and then (Is_Immediately_Visible (Op_Id)
3510 or else Is_Potentially_Use_Visible (Op_Id))
3511 and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
3512 then
3513 Add_One_Interp (N, Op_Id, Etype (Op_Id));
3514 end if;
3515 end;
3516 end if;
3517 end Analyze_User_Defined_Unary_Op;
3518
3519 ---------------------------
3520 -- Check_Arithmetic_Pair --
3521 ---------------------------
3522
3523 procedure Check_Arithmetic_Pair
3524 (T1, T2 : Entity_Id;
3525 Op_Id : Entity_Id;
3526 N : Node_Id)
3527 is
401093c1 3528 Op_Name : constant Name_Id := Chars (Op_Id);
996ae0b0 3529
da709d08
AC
3530 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
3531 -- Check whether the fixed-point type Typ has a user-defined operator
3532 -- (multiplication or division) that should hide the corresponding
3533 -- predefined operator. Used to implement Ada 2005 AI-264, to make
3534 -- such operators more visible and therefore useful.
3535
996ae0b0
RK
3536 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
3537 -- Get specific type (i.e. non-universal type if there is one)
3538
da709d08
AC
3539 ------------------
3540 -- Has_Fixed_Op --
3541 ------------------
3542
3543 function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
401093c1 3544 Bas : constant Entity_Id := Base_Type (Typ);
da709d08
AC
3545 Ent : Entity_Id;
3546 F1 : Entity_Id;
3547 F2 : Entity_Id;
3548
3549 begin
3550 -- The operation is treated as primitive if it is declared in the
3551 -- same scope as the type, and therefore on the same entity chain.
3552
3553 Ent := Next_Entity (Typ);
3554 while Present (Ent) loop
3555 if Chars (Ent) = Chars (Op) then
3556 F1 := First_Formal (Ent);
3557 F2 := Next_Formal (F1);
3558
3559 -- The operation counts as primitive if either operand or
401093c1
ES
3560 -- result are of the given base type, and both operands are
3561 -- fixed point types.
da709d08 3562
401093c1 3563 if (Base_Type (Etype (F1)) = Bas
da709d08
AC
3564 and then Is_Fixed_Point_Type (Etype (F2)))
3565
3566 or else
401093c1 3567 (Base_Type (Etype (F2)) = Bas
da709d08
AC
3568 and then Is_Fixed_Point_Type (Etype (F1)))
3569
3570 or else
401093c1 3571 (Base_Type (Etype (Ent)) = Bas
da709d08
AC
3572 and then Is_Fixed_Point_Type (Etype (F1))
3573 and then Is_Fixed_Point_Type (Etype (F2)))
3574 then
3575 return True;
3576 end if;
3577 end if;
3578
3579 Next_Entity (Ent);
3580 end loop;
3581
3582 return False;
3583 end Has_Fixed_Op;
3584
4c46b835
AC
3585 -------------------
3586 -- Specific_Type --
3587 -------------------
3588
996ae0b0
RK
3589 function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
3590 begin
3591 if T1 = Universal_Integer or else T1 = Universal_Real then
3592 return Base_Type (T2);
3593 else
3594 return Base_Type (T1);
3595 end if;
3596 end Specific_Type;
3597
3598 -- Start of processing for Check_Arithmetic_Pair
3599
3600 begin
3601 if Op_Name = Name_Op_Add or else Op_Name = Name_Op_Subtract then
3602
3603 if Is_Numeric_Type (T1)
3604 and then Is_Numeric_Type (T2)
3605 and then (Covers (T1, T2) or else Covers (T2, T1))
3606 then
3607 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
3608 end if;
3609
3610 elsif Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide then
3611
3612 if Is_Fixed_Point_Type (T1)
3613 and then (Is_Fixed_Point_Type (T2)
3614 or else T2 = Universal_Real)
3615 then
3616 -- If Treat_Fixed_As_Integer is set then the Etype is already set
3617 -- and no further processing is required (this is the case of an
3618 -- operator constructed by Exp_Fixd for a fixed point operation)
3619 -- Otherwise add one interpretation with universal fixed result
3620 -- If the operator is given in functional notation, it comes
3621 -- from source and Fixed_As_Integer cannot apply.
3622
da709d08
AC
3623 if (Nkind (N) not in N_Op
3624 or else not Treat_Fixed_As_Integer (N))
3625 and then
401093c1 3626 (not Has_Fixed_Op (T1, Op_Id)
da709d08 3627 or else Nkind (Parent (N)) = N_Type_Conversion)
fbf5a39b 3628 then
996ae0b0
RK
3629 Add_One_Interp (N, Op_Id, Universal_Fixed);
3630 end if;
3631
3632 elsif Is_Fixed_Point_Type (T2)
3633 and then (Nkind (N) not in N_Op
3634 or else not Treat_Fixed_As_Integer (N))
3635 and then T1 = Universal_Real
da709d08 3636 and then
401093c1 3637 (not Has_Fixed_Op (T1, Op_Id)
da709d08 3638 or else Nkind (Parent (N)) = N_Type_Conversion)
996ae0b0
RK
3639 then
3640 Add_One_Interp (N, Op_Id, Universal_Fixed);
3641
3642 elsif Is_Numeric_Type (T1)
3643 and then Is_Numeric_Type (T2)
3644 and then (Covers (T1, T2) or else Covers (T2, T1))
3645 then
3646 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
3647
3648 elsif Is_Fixed_Point_Type (T1)
3649 and then (Base_Type (T2) = Base_Type (Standard_Integer)
3650 or else T2 = Universal_Integer)
3651 then
3652 Add_One_Interp (N, Op_Id, T1);
3653
3654 elsif T2 = Universal_Real
3655 and then Base_Type (T1) = Base_Type (Standard_Integer)
3656 and then Op_Name = Name_Op_Multiply
3657 then
3658 Add_One_Interp (N, Op_Id, Any_Fixed);
3659
3660 elsif T1 = Universal_Real
3661 and then Base_Type (T2) = Base_Type (Standard_Integer)
3662 then
3663 Add_One_Interp (N, Op_Id, Any_Fixed);
3664
3665 elsif Is_Fixed_Point_Type (T2)
3666 and then (Base_Type (T1) = Base_Type (Standard_Integer)
3667 or else T1 = Universal_Integer)
3668 and then Op_Name = Name_Op_Multiply
3669 then
3670 Add_One_Interp (N, Op_Id, T2);
3671
3672 elsif T1 = Universal_Real and then T2 = Universal_Integer then
3673 Add_One_Interp (N, Op_Id, T1);
3674
3675 elsif T2 = Universal_Real
3676 and then T1 = Universal_Integer
3677 and then Op_Name = Name_Op_Multiply
3678 then
3679 Add_One_Interp (N, Op_Id, T2);
3680 end if;
3681
3682 elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
3683
3684 -- Note: The fixed-point operands case with Treat_Fixed_As_Integer
3685 -- set does not require any special processing, since the Etype is
3686 -- already set (case of operation constructed by Exp_Fixed).
3687
3688 if Is_Integer_Type (T1)
3689 and then (Covers (T1, T2) or else Covers (T2, T1))
3690 then
3691 Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
3692 end if;
3693
3694 elsif Op_Name = Name_Op_Expon then
996ae0b0
RK
3695 if Is_Numeric_Type (T1)
3696 and then not Is_Fixed_Point_Type (T1)
3697 and then (Base_Type (T2) = Base_Type (Standard_Integer)
3698 or else T2 = Universal_Integer)
3699 then
3700 Add_One_Interp (N, Op_Id, Base_Type (T1));
3701 end if;
3702
3703 else pragma Assert (Nkind (N) in N_Op_Shift);
3704
3705 -- If not one of the predefined operators, the node may be one
3706 -- of the intrinsic functions. Its kind is always specific, and
3707 -- we can use it directly, rather than the name of the operation.
3708
3709 if Is_Integer_Type (T1)
3710 and then (Base_Type (T2) = Base_Type (Standard_Integer)
3711 or else T2 = Universal_Integer)
3712 then
3713 Add_One_Interp (N, Op_Id, Base_Type (T1));
3714 end if;
3715 end if;
3716 end Check_Arithmetic_Pair;
3717
3718 -------------------------------
3719 -- Check_Misspelled_Selector --
3720 -------------------------------
3721
3722 procedure Check_Misspelled_Selector
3723 (Prefix : Entity_Id;
3724 Sel : Node_Id)
3725 is
3726 Max_Suggestions : constant := 2;
3727 Nr_Of_Suggestions : Natural := 0;
3728
3729 Suggestion_1 : Entity_Id := Empty;
3730 Suggestion_2 : Entity_Id := Empty;
3731
3732 Comp : Entity_Id;
3733
3734 begin
3735 -- All the components of the prefix of selector Sel are matched
3736 -- against Sel and a count is maintained of possible misspellings.
3737 -- When at the end of the analysis there are one or two (not more!)
3738 -- possible misspellings, these misspellings will be suggested as
3739 -- possible correction.
3740
4c46b835
AC
3741 if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
3742
996ae0b0 3743 -- Concurrent types should be handled as well ???
4c46b835 3744
996ae0b0
RK
3745 return;
3746 end if;
3747
3748 Get_Name_String (Chars (Sel));
3749
3750 declare
4c46b835 3751 S : constant String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
996ae0b0
RK
3752
3753 begin
3754 Comp := First_Entity (Prefix);
996ae0b0
RK
3755 while Nr_Of_Suggestions <= Max_Suggestions
3756 and then Present (Comp)
3757 loop
996ae0b0
RK
3758 if Is_Visible_Component (Comp) then
3759 Get_Name_String (Chars (Comp));
3760
3761 if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
3762 Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
3763
3764 case Nr_Of_Suggestions is
3765 when 1 => Suggestion_1 := Comp;
3766 when 2 => Suggestion_2 := Comp;
3767 when others => exit;
3768 end case;
3769 end if;
3770 end if;
3771
3772 Comp := Next_Entity (Comp);
3773 end loop;
3774
3775 -- Report at most two suggestions
3776
3777 if Nr_Of_Suggestions = 1 then
3778 Error_Msg_NE ("\possible misspelling of&", Sel, Suggestion_1);
3779
3780 elsif Nr_Of_Suggestions = 2 then
3781 Error_Msg_Node_2 := Suggestion_2;
3782 Error_Msg_NE ("\possible misspelling of& or&",
3783 Sel, Suggestion_1);
3784 end if;
3785 end;
3786 end Check_Misspelled_Selector;
3787
3788 ----------------------
3789 -- Defined_In_Scope --
3790 ----------------------
3791
3792 function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
3793 is
3794 S1 : constant Entity_Id := Scope (Base_Type (T));
996ae0b0
RK
3795 begin
3796 return S1 = S
3797 or else (S1 = System_Aux_Id and then S = Scope (S1));
3798 end Defined_In_Scope;
3799
3800 -------------------
3801 -- Diagnose_Call --
3802 -------------------
3803
3804 procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
fbf5a39b
AC
3805 Actual : Node_Id;
3806 X : Interp_Index;
3807 It : Interp;
3808 Success : Boolean;
3809 Err_Mode : Boolean;
3810 New_Nam : Node_Id;
3811 Void_Interp_Seen : Boolean := False;
996ae0b0
RK
3812
3813 begin
0ab80019 3814 if Ada_Version >= Ada_05 then
996ae0b0 3815 Actual := First_Actual (N);
996ae0b0 3816 while Present (Actual) loop
0ab80019
AC
3817
3818 -- Ada 2005 (AI-50217): Post an error in case of premature
3819 -- usage of an entity from the limited view.
19f0526a 3820
996ae0b0
RK
3821 if not Analyzed (Etype (Actual))
3822 and then From_With_Type (Etype (Actual))
3823 then
3824 Error_Msg_Qual_Level := 1;
3825 Error_Msg_NE
3826 ("missing with_clause for scope of imported type&",
3827 Actual, Etype (Actual));
3828 Error_Msg_Qual_Level := 0;
3829 end if;
3830
3831 Next_Actual (Actual);
3832 end loop;
3833 end if;
3834
fbf5a39b
AC
3835 -- Analyze each candidate call again, with full error reporting
3836 -- for each.
3837
3838 Error_Msg_N
3839 ("no candidate interpretations match the actuals:!", Nam);
3840 Err_Mode := All_Errors_Mode;
3841 All_Errors_Mode := True;
3842
3843 -- If this is a call to an operation of a concurrent type,
3844 -- the failed interpretations have been removed from the
3845 -- name. Recover them to provide full diagnostics.
3846
3847 if Nkind (Parent (Nam)) = N_Selected_Component then
3848 Set_Entity (Nam, Empty);
3849 New_Nam := New_Copy_Tree (Parent (Nam));
3850 Set_Is_Overloaded (New_Nam, False);
3851 Set_Is_Overloaded (Selector_Name (New_Nam), False);
3852 Set_Parent (New_Nam, Parent (Parent (Nam)));
3853 Analyze_Selected_Component (New_Nam);
3854 Get_First_Interp (Selector_Name (New_Nam), X, It);
3855 else
996ae0b0 3856 Get_First_Interp (Nam, X, It);
fbf5a39b 3857 end if;
996ae0b0 3858
fbf5a39b
AC
3859 while Present (It.Nam) loop
3860 if Etype (It.Nam) = Standard_Void_Type then
3861 Void_Interp_Seen := True;
996ae0b0 3862 end if;
fbf5a39b
AC
3863
3864 Analyze_One_Call (N, It.Nam, True, Success);
3865 Get_Next_Interp (X, It);
3866 end loop;
996ae0b0
RK
3867
3868 if Nkind (N) = N_Function_Call then
3869 Get_First_Interp (Nam, X, It);
996ae0b0
RK
3870 while Present (It.Nam) loop
3871 if Ekind (It.Nam) = E_Function
3872 or else Ekind (It.Nam) = E_Operator
3873 then
3874 return;
3875 else
3876 Get_Next_Interp (X, It);
3877 end if;
3878 end loop;
3879
3880 -- If all interpretations are procedures, this deserves a
3881 -- more precise message. Ditto if this appears as the prefix
3882 -- of a selected component, which may be a lexical error.
3883
4c46b835
AC
3884 Error_Msg_N
3885 ("\context requires function call, found procedure name", Nam);
996ae0b0
RK
3886
3887 if Nkind (Parent (N)) = N_Selected_Component
3888 and then N = Prefix (Parent (N))
3889 then
3890 Error_Msg_N (
3891 "\period should probably be semicolon", Parent (N));
3892 end if;
fbf5a39b
AC
3893
3894 elsif Nkind (N) = N_Procedure_Call_Statement
3895 and then not Void_Interp_Seen
3896 then
3897 Error_Msg_N (
3898 "\function name found in procedure call", Nam);
996ae0b0 3899 end if;
fbf5a39b
AC
3900
3901 All_Errors_Mode := Err_Mode;
996ae0b0
RK
3902 end Diagnose_Call;
3903
3904 ---------------------------
3905 -- Find_Arithmetic_Types --
3906 ---------------------------
3907
3908 procedure Find_Arithmetic_Types
3909 (L, R : Node_Id;
3910 Op_Id : Entity_Id;
3911 N : Node_Id)
3912 is
4c46b835
AC
3913 Index1 : Interp_Index;
3914 Index2 : Interp_Index;
3915 It1 : Interp;
3916 It2 : Interp;
996ae0b0
RK
3917
3918 procedure Check_Right_Argument (T : Entity_Id);
3919 -- Check right operand of operator
3920
4c46b835
AC
3921 --------------------------
3922 -- Check_Right_Argument --
3923 --------------------------
3924
996ae0b0
RK
3925 procedure Check_Right_Argument (T : Entity_Id) is
3926 begin
3927 if not Is_Overloaded (R) then
3928 Check_Arithmetic_Pair (T, Etype (R), Op_Id, N);
3929 else
3930 Get_First_Interp (R, Index2, It2);
996ae0b0
RK
3931 while Present (It2.Typ) loop
3932 Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
3933 Get_Next_Interp (Index2, It2);
3934 end loop;
3935 end if;
3936 end Check_Right_Argument;
3937
3938 -- Start processing for Find_Arithmetic_Types
3939
3940 begin
3941 if not Is_Overloaded (L) then
3942 Check_Right_Argument (Etype (L));
3943
3944 else
3945 Get_First_Interp (L, Index1, It1);
996ae0b0
RK
3946 while Present (It1.Typ) loop
3947 Check_Right_Argument (It1.Typ);
3948 Get_Next_Interp (Index1, It1);
3949 end loop;
3950 end if;
3951
3952 end Find_Arithmetic_Types;
3953
3954 ------------------------
3955 -- Find_Boolean_Types --
3956 ------------------------
3957
3958 procedure Find_Boolean_Types
3959 (L, R : Node_Id;
3960 Op_Id : Entity_Id;
3961 N : Node_Id)
3962 is
3963 Index : Interp_Index;
3964 It : Interp;
3965
3966 procedure Check_Numeric_Argument (T : Entity_Id);
3967 -- Special case for logical operations one of whose operands is an
3968 -- integer literal. If both are literal the result is any modular type.
3969
4c46b835
AC
3970 ----------------------------
3971 -- Check_Numeric_Argument --
3972 ----------------------------
3973
996ae0b0
RK
3974 procedure Check_Numeric_Argument (T : Entity_Id) is
3975 begin
3976 if T = Universal_Integer then
3977 Add_One_Interp (N, Op_Id, Any_Modular);
3978
3979 elsif Is_Modular_Integer_Type (T) then
3980 Add_One_Interp (N, Op_Id, T);
3981 end if;
3982 end Check_Numeric_Argument;
3983
3984 -- Start of processing for Find_Boolean_Types
3985
3986 begin
3987 if not Is_Overloaded (L) then
996ae0b0
RK
3988 if Etype (L) = Universal_Integer
3989 or else Etype (L) = Any_Modular
3990 then
3991 if not Is_Overloaded (R) then
3992 Check_Numeric_Argument (Etype (R));
3993
3994 else
3995 Get_First_Interp (R, Index, It);
996ae0b0
RK
3996 while Present (It.Typ) loop
3997 Check_Numeric_Argument (It.Typ);
996ae0b0
RK
3998 Get_Next_Interp (Index, It);
3999 end loop;
4000 end if;
4001
69e6a03e
ES
4002 -- If operands are aggregates, we must assume that they may be
4003 -- boolean arrays, and leave disambiguation for the second pass.
4004 -- If only one is an aggregate, verify that the other one has an
4005 -- interpretation as a boolean array
4006
4007 elsif Nkind (L) = N_Aggregate then
4008 if Nkind (R) = N_Aggregate then
4009 Add_One_Interp (N, Op_Id, Etype (L));
4010
4011 elsif not Is_Overloaded (R) then
4012 if Valid_Boolean_Arg (Etype (R)) then
4013 Add_One_Interp (N, Op_Id, Etype (R));
4014 end if;
4015
4016 else
4017 Get_First_Interp (R, Index, It);
4018 while Present (It.Typ) loop
4019 if Valid_Boolean_Arg (It.Typ) then
4020 Add_One_Interp (N, Op_Id, It.Typ);
4021 end if;
4022
4023 Get_Next_Interp (Index, It);
4024 end loop;
4025 end if;
4026
996ae0b0
RK
4027 elsif Valid_Boolean_Arg (Etype (L))
4028 and then Has_Compatible_Type (R, Etype (L))
4029 then
4030 Add_One_Interp (N, Op_Id, Etype (L));
4031 end if;
4032
4033 else
4034 Get_First_Interp (L, Index, It);
996ae0b0
RK
4035 while Present (It.Typ) loop
4036 if Valid_Boolean_Arg (It.Typ)
4037 and then Has_Compatible_Type (R, It.Typ)
4038 then
4039 Add_One_Interp (N, Op_Id, It.Typ);
4040 end if;
4041
4042 Get_Next_Interp (Index, It);
4043 end loop;
4044 end if;
4045 end Find_Boolean_Types;
4046
4047 ---------------------------
4048 -- Find_Comparison_Types --
4049 ---------------------------
4050
4051 procedure Find_Comparison_Types
4052 (L, R : Node_Id;
4053 Op_Id : Entity_Id;
4054 N : Node_Id)
4055 is
4056 Index : Interp_Index;
4057 It : Interp;
4058 Found : Boolean := False;
4059 I_F : Interp_Index;
4060 T_F : Entity_Id;
4061 Scop : Entity_Id := Empty;
4062
4063 procedure Try_One_Interp (T1 : Entity_Id);
4064 -- Routine to try one proposed interpretation. Note that the context
4065 -- of the operator plays no role in resolving the arguments, so that
4066 -- if there is more than one interpretation of the operands that is
4067 -- compatible with comparison, the operation is ambiguous.
4068
4c46b835
AC
4069 --------------------
4070 -- Try_One_Interp --
4071 --------------------
4072
996ae0b0
RK
4073 procedure Try_One_Interp (T1 : Entity_Id) is
4074 begin
4075
4076 -- If the operator is an expanded name, then the type of the operand
4077 -- must be defined in the corresponding scope. If the type is
4078 -- universal, the context will impose the correct type.
4079
4080 if Present (Scop)
4081 and then not Defined_In_Scope (T1, Scop)
4082 and then T1 /= Universal_Integer
4083 and then T1 /= Universal_Real
4084 and then T1 /= Any_String
4085 and then T1 /= Any_Composite
4086 then
4087 return;
4088 end if;
4089
4090 if Valid_Comparison_Arg (T1)
4091 and then Has_Compatible_Type (R, T1)
4092 then
4093 if Found
4094 and then Base_Type (T1) /= Base_Type (T_F)
4095 then
4096 It := Disambiguate (L, I_F, Index, Any_Type);
4097
4098 if It = No_Interp then
4099 Ambiguous_Operands (N);
4100 Set_Etype (L, Any_Type);
4101 return;
4102
4103 else
4104 T_F := It.Typ;
4105 end if;
4106
4107 else
4108 Found := True;
4109 T_F := T1;
4110 I_F := Index;
4111 end if;
4112
4113 Set_Etype (L, T_F);
4114 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
4115
4116 end if;
4117 end Try_One_Interp;
4118
4119 -- Start processing for Find_Comparison_Types
4120
4121 begin
fbf5a39b
AC
4122 -- If left operand is aggregate, the right operand has to
4123 -- provide a usable type for it.
4124
4125 if Nkind (L) = N_Aggregate
4126 and then Nkind (R) /= N_Aggregate
4127 then
4128 Find_Comparison_Types (R, L, Op_Id, N);
4129 return;
4130 end if;
996ae0b0
RK
4131
4132 if Nkind (N) = N_Function_Call
4133 and then Nkind (Name (N)) = N_Expanded_Name
4134 then
4135 Scop := Entity (Prefix (Name (N)));
4136
4137 -- The prefix may be a package renaming, and the subsequent test
4138 -- requires the original package.
4139
4140 if Ekind (Scop) = E_Package
4141 and then Present (Renamed_Entity (Scop))
4142 then
4143 Scop := Renamed_Entity (Scop);
4144 Set_Entity (Prefix (Name (N)), Scop);
4145 end if;
4146 end if;
4147
4148 if not Is_Overloaded (L) then
4149 Try_One_Interp (Etype (L));
4150
4151 else
4152 Get_First_Interp (L, Index, It);
996ae0b0
RK
4153 while Present (It.Typ) loop
4154 Try_One_Interp (It.Typ);
4155 Get_Next_Interp (Index, It);
4156 end loop;
4157 end if;
4158 end Find_Comparison_Types;
4159
4160 ----------------------------------------
4161 -- Find_Non_Universal_Interpretations --
4162 ----------------------------------------
4163
4164 procedure Find_Non_Universal_Interpretations
4165 (N : Node_Id;
4166 R : Node_Id;
4167 Op_Id : Entity_Id;
4168 T1 : Entity_Id)
4169 is
4170 Index : Interp_Index;
4c46b835 4171 It : Interp;
996ae0b0
RK
4172
4173 begin
4174 if T1 = Universal_Integer
4175 or else T1 = Universal_Real
4176 then
4177 if not Is_Overloaded (R) then
4178 Add_One_Interp
4179 (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
4180 else
4181 Get_First_Interp (R, Index, It);
996ae0b0
RK
4182 while Present (It.Typ) loop
4183 if Covers (It.Typ, T1) then
4184 Add_One_Interp
4185 (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
4186 end if;
4187
4188 Get_Next_Interp (Index, It);
4189 end loop;
4190 end if;
4191 else
4192 Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
4193 end if;
4194 end Find_Non_Universal_Interpretations;
4195
4196 ------------------------------
4197 -- Find_Concatenation_Types --
4198 ------------------------------
4199
4200 procedure Find_Concatenation_Types
4201 (L, R : Node_Id;
4202 Op_Id : Entity_Id;
4203 N : Node_Id)
4204 is
4205 Op_Type : constant Entity_Id := Etype (Op_Id);
4206
4207 begin
4208 if Is_Array_Type (Op_Type)
4209 and then not Is_Limited_Type (Op_Type)
4210
4211 and then (Has_Compatible_Type (L, Op_Type)
4212 or else
4213 Has_Compatible_Type (L, Component_Type (Op_Type)))
4214
4215 and then (Has_Compatible_Type (R, Op_Type)
4216 or else
4217 Has_Compatible_Type (R, Component_Type (Op_Type)))
4218 then
4219 Add_One_Interp (N, Op_Id, Op_Type);
4220 end if;
4221 end Find_Concatenation_Types;
4222
4223 -------------------------
4224 -- Find_Equality_Types --
4225 -------------------------
4226
4227 procedure Find_Equality_Types
4228 (L, R : Node_Id;
4229 Op_Id : Entity_Id;
4230 N : Node_Id)
4231 is
4232 Index : Interp_Index;
4233 It : Interp;
4234 Found : Boolean := False;
4235 I_F : Interp_Index;
4236 T_F : Entity_Id;
4237 Scop : Entity_Id := Empty;
4238
4239 procedure Try_One_Interp (T1 : Entity_Id);
4240 -- The context of the operator plays no role in resolving the
4241 -- arguments, so that if there is more than one interpretation
4242 -- of the operands that is compatible with equality, the construct
4243 -- is ambiguous and an error can be emitted now, after trying to
4244 -- disambiguate, i.e. applying preference rules.
4245
4c46b835
AC
4246 --------------------
4247 -- Try_One_Interp --
4248 --------------------
4249
996ae0b0
RK
4250 procedure Try_One_Interp (T1 : Entity_Id) is
4251 begin
996ae0b0
RK
4252 -- If the operator is an expanded name, then the type of the operand
4253 -- must be defined in the corresponding scope. If the type is
4254 -- universal, the context will impose the correct type. An anonymous
4255 -- type for a 'Access reference is also universal in this sense, as
4256 -- the actual type is obtained from context.
fe45e59e
ES
4257 -- In Ada 2005, the equality operator for anonymous access types
4258 -- is declared in Standard, and preference rules apply to it.
996ae0b0 4259
fe45e59e
ES
4260 if Present (Scop) then
4261 if Defined_In_Scope (T1, Scop)
4262 or else T1 = Universal_Integer
4263 or else T1 = Universal_Real
4264 or else T1 = Any_Access
4265 or else T1 = Any_String
4266 or else T1 = Any_Composite
4267 or else (Ekind (T1) = E_Access_Subprogram_Type
4268 and then not Comes_From_Source (T1))
4269 then
4270 null;
4271
4272 elsif Ekind (T1) = E_Anonymous_Access_Type
4273 and then Scop = Standard_Standard
4274 then
4275 null;
4276
4277 else
4278 -- The scope does not contain an operator for the type
4279
4280 return;
4281 end if;
996ae0b0
RK
4282 end if;
4283
0ab80019
AC
4284 -- Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
4285 -- Do not allow anonymous access types in equality operators.
6e937c1c 4286
0ab80019 4287 if Ada_Version < Ada_05
6e937c1c
AC
4288 and then Ekind (T1) = E_Anonymous_Access_Type
4289 then
4290 return;
4291 end if;
4292
996ae0b0
RK
4293 if T1 /= Standard_Void_Type
4294 and then not Is_Limited_Type (T1)
4295 and then not Is_Limited_Composite (T1)
996ae0b0
RK
4296 and then Has_Compatible_Type (R, T1)
4297 then
4298 if Found
4299 and then Base_Type (T1) /= Base_Type (T_F)
4300 then
4301 It := Disambiguate (L, I_F, Index, Any_Type);
4302
4303 if It = No_Interp then
4304 Ambiguous_Operands (N);
4305 Set_Etype (L, Any_Type);
4306 return;
4307
4308 else
4309 T_F := It.Typ;
4310 end if;
4311
4312 else
4313 Found := True;
4314 T_F := T1;
4315 I_F := Index;
4316 end if;
4317
4318 if not Analyzed (L) then
4319 Set_Etype (L, T_F);
4320 end if;
4321
4322 Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
4323
6e73e3ab 4324 -- Case of operator was not visible, Etype still set to Any_Type
996ae0b0 4325
6e73e3ab 4326 if Etype (N) = Any_Type then
996ae0b0
RK
4327 Found := False;
4328 end if;
fe45e59e
ES
4329
4330 elsif Scop = Standard_Standard
4331 and then Ekind (T1) = E_Anonymous_Access_Type
4332 then
4333 Found := True;
996ae0b0
RK
4334 end if;
4335 end Try_One_Interp;
4336
4337 -- Start of processing for Find_Equality_Types
4338
4339 begin
fbf5a39b
AC
4340 -- If left operand is aggregate, the right operand has to
4341 -- provide a usable type for it.
4342
4343 if Nkind (L) = N_Aggregate
4344 and then Nkind (R) /= N_Aggregate
4345 then
4346 Find_Equality_Types (R, L, Op_Id, N);
4347 return;
4348 end if;
996ae0b0
RK
4349
4350 if Nkind (N) = N_Function_Call
4351 and then Nkind (Name (N)) = N_Expanded_Name
4352 then
4353 Scop := Entity (Prefix (Name (N)));
4354
4355 -- The prefix may be a package renaming, and the subsequent test
4356 -- requires the original package.
4357
4358 if Ekind (Scop) = E_Package
4359 and then Present (Renamed_Entity (Scop))
4360 then
4361 Scop := Renamed_Entity (Scop);
4362 Set_Entity (Prefix (Name (N)), Scop);
4363 end if;
4364 end if;
4365
4366 if not Is_Overloaded (L) then
4367 Try_One_Interp (Etype (L));
996ae0b0 4368
4c46b835 4369 else
996ae0b0 4370 Get_First_Interp (L, Index, It);
996ae0b0
RK
4371 while Present (It.Typ) loop
4372 Try_One_Interp (It.Typ);
4373 Get_Next_Interp (Index, It);
4374 end loop;
4375 end if;
4376 end Find_Equality_Types;
4377
4378 -------------------------
4379 -- Find_Negation_Types --
4380 -------------------------
4381
4382 procedure Find_Negation_Types
4383 (R : Node_Id;
4384 Op_Id : Entity_Id;
4385 N : Node_Id)
4386 is
4387 Index : Interp_Index;
4388 It : Interp;
4389
4390 begin
4391 if not Is_Overloaded (R) then
996ae0b0
RK
4392 if Etype (R) = Universal_Integer then
4393 Add_One_Interp (N, Op_Id, Any_Modular);
996ae0b0
RK
4394 elsif Valid_Boolean_Arg (Etype (R)) then
4395 Add_One_Interp (N, Op_Id, Etype (R));
4396 end if;
4397
4398 else
4399 Get_First_Interp (R, Index, It);
996ae0b0
RK
4400 while Present (It.Typ) loop
4401 if Valid_Boolean_Arg (It.Typ) then
4402 Add_One_Interp (N, Op_Id, It.Typ);
4403 end if;
4404
4405 Get_Next_Interp (Index, It);
4406 end loop;
4407 end if;
4408 end Find_Negation_Types;
4409
4410 ----------------------
4411 -- Find_Unary_Types --
4412 ----------------------
4413
4414 procedure Find_Unary_Types
4415 (R : Node_Id;
4416 Op_Id : Entity_Id;
4417 N : Node_Id)
4418 is
4419 Index : Interp_Index;
4420 It : Interp;
4421
4422 begin
4423 if not Is_Overloaded (R) then
4424 if Is_Numeric_Type (Etype (R)) then
4425 Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
4426 end if;
4427
4428 else
4429 Get_First_Interp (R, Index, It);
996ae0b0
RK
4430 while Present (It.Typ) loop
4431 if Is_Numeric_Type (It.Typ) then
4432 Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
4433 end if;
4434
4435 Get_Next_Interp (Index, It);
4436 end loop;
4437 end if;
4438 end Find_Unary_Types;
4439
996ae0b0
RK
4440 ------------------
4441 -- Junk_Operand --
4442 ------------------
4443
4444 function Junk_Operand (N : Node_Id) return Boolean is
4445 Enode : Node_Id;
4446
4447 begin
4448 if Error_Posted (N) then
4449 return False;
4450 end if;
4451
4452 -- Get entity to be tested
4453
4454 if Is_Entity_Name (N)
4455 and then Present (Entity (N))
4456 then
4457 Enode := N;
4458
4459 -- An odd case, a procedure name gets converted to a very peculiar
4460 -- function call, and here is where we detect this happening.
4461
4462 elsif Nkind (N) = N_Function_Call
4463 and then Is_Entity_Name (Name (N))
4464 and then Present (Entity (Name (N)))
4465 then
4466 Enode := Name (N);
4467
4468 -- Another odd case, there are at least some cases of selected
4469 -- components where the selected component is not marked as having
4470 -- an entity, even though the selector does have an entity
4471
4472 elsif Nkind (N) = N_Selected_Component
4473 and then Present (Entity (Selector_Name (N)))
4474 then
4475 Enode := Selector_Name (N);
4476
4477 else
4478 return False;
4479 end if;
4480
9de61fcb 4481 -- Now test the entity we got to see if it is a bad case
996ae0b0
RK
4482
4483 case Ekind (Entity (Enode)) is
4484
4485 when E_Package =>
4486 Error_Msg_N
4487 ("package name cannot be used as operand", Enode);
4488
4489 when Generic_Unit_Kind =>
4490 Error_Msg_N
4491 ("generic unit name cannot be used as operand", Enode);
4492
4493 when Type_Kind =>
4494 Error_Msg_N
4495 ("subtype name cannot be used as operand", Enode);
4496
4497 when Entry_Kind =>
4498 Error_Msg_N
4499 ("entry name cannot be used as operand", Enode);
4500
4501 when E_Procedure =>
4502 Error_Msg_N
4503 ("procedure name cannot be used as operand", Enode);
4504
4505 when E_Exception =>
4506 Error_Msg_N
4507 ("exception name cannot be used as operand", Enode);
4508
4509 when E_Block | E_Label | E_Loop =>
4510 Error_Msg_N
4511 ("label name cannot be used as operand", Enode);
4512
4513 when others =>
4514 return False;
4515
4516 end case;
4517
4518 return True;
4519 end Junk_Operand;
4520
4521 --------------------
4522 -- Operator_Check --
4523 --------------------
4524
4525 procedure Operator_Check (N : Node_Id) is
4526 begin
30c20106
AC
4527 Remove_Abstract_Operations (N);
4528
996ae0b0
RK
4529 -- Test for case of no interpretation found for operator
4530
4531 if Etype (N) = Any_Type then
4532 declare
b67a385c
ES
4533 L : Node_Id;
4534 R : Node_Id;
4535 Op_Id : Entity_Id := Empty;
996ae0b0
RK
4536
4537 begin
4538 R := Right_Opnd (N);
4539
4540 if Nkind (N) in N_Binary_Op then
4541 L := Left_Opnd (N);
4542 else
4543 L := Empty;
4544 end if;
4545
4546 -- If either operand has no type, then don't complain further,
9de61fcb 4547 -- since this simply means that we have a propagated error.
996ae0b0
RK
4548
4549 if R = Error
4550 or else Etype (R) = Any_Type
4551 or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
4552 then
4553 return;
4554
4c46b835
AC
4555 -- We explicitly check for the case of concatenation of component
4556 -- with component to avoid reporting spurious matching array types
4557 -- that might happen to be lurking in distant packages (such as
4558 -- run-time packages). This also prevents inconsistencies in the
4559 -- messages for certain ACVC B tests, which can vary depending on
4560 -- types declared in run-time interfaces. Another improvement when
4561 -- aggregates are present is to look for a well-typed operand.
996ae0b0
RK
4562
4563 elsif Present (Candidate_Type)
4564 and then (Nkind (N) /= N_Op_Concat
4565 or else Is_Array_Type (Etype (L))
4566 or else Is_Array_Type (Etype (R)))
4567 then
4568
4569 if Nkind (N) = N_Op_Concat then
4570 if Etype (L) /= Any_Composite
4571 and then Is_Array_Type (Etype (L))
4572 then
4573 Candidate_Type := Etype (L);
4574
4575 elsif Etype (R) /= Any_Composite
4576 and then Is_Array_Type (Etype (R))
4577 then
4578 Candidate_Type := Etype (R);
4579 end if;
4580 end if;
4581
4582 Error_Msg_NE
4583 ("operator for} is not directly visible!",
4584 N, First_Subtype (Candidate_Type));
4585 Error_Msg_N ("use clause would make operation legal!", N);
4586 return;
4587
4588 -- If either operand is a junk operand (e.g. package name), then
4589 -- post appropriate error messages, but do not complain further.
4590
0e0eecec
ES
4591 -- Note that the use of OR in this test instead of OR ELSE is
4592 -- quite deliberate, we may as well check both operands in the
4593 -- binary operator case.
996ae0b0
RK
4594
4595 elsif Junk_Operand (R)
4596 or (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
4597 then
4598 return;
4599
4600 -- If we have a logical operator, one of whose operands is
0e0eecec
ES
4601 -- Boolean, then we know that the other operand cannot resolve to
4602 -- Boolean (since we got no interpretations), but in that case we
4603 -- pretty much know that the other operand should be Boolean, so
4604 -- resolve it that way (generating an error)
996ae0b0
RK
4605
4606 elsif Nkind (N) = N_Op_And
4607 or else
4608 Nkind (N) = N_Op_Or
4609 or else
4610 Nkind (N) = N_Op_Xor
4611 then
4612 if Etype (L) = Standard_Boolean then
4613 Resolve (R, Standard_Boolean);
4614 return;
4615 elsif Etype (R) = Standard_Boolean then
4616 Resolve (L, Standard_Boolean);
4617 return;
4618 end if;
4619
4620 -- For an arithmetic operator or comparison operator, if one
4621 -- of the operands is numeric, then we know the other operand
4622 -- is not the same numeric type. If it is a non-numeric type,
4623 -- then probably it is intended to match the other operand.
4624
4625 elsif Nkind (N) = N_Op_Add or else
4626 Nkind (N) = N_Op_Divide or else
4627 Nkind (N) = N_Op_Ge or else
4628 Nkind (N) = N_Op_Gt or else
4629 Nkind (N) = N_Op_Le or else
4630 Nkind (N) = N_Op_Lt or else
4631 Nkind (N) = N_Op_Mod or else
4632 Nkind (N) = N_Op_Multiply or else
4633 Nkind (N) = N_Op_Rem or else
4634 Nkind (N) = N_Op_Subtract
4635 then
4636 if Is_Numeric_Type (Etype (L))
4637 and then not Is_Numeric_Type (Etype (R))
4638 then
4639 Resolve (R, Etype (L));
4640 return;
4641
4642 elsif Is_Numeric_Type (Etype (R))
4643 and then not Is_Numeric_Type (Etype (L))
4644 then
4645 Resolve (L, Etype (R));
4646 return;
4647 end if;
4648
4649 -- Comparisons on A'Access are common enough to deserve a
4650 -- special message.
4651
4652 elsif (Nkind (N) = N_Op_Eq or else
4653 Nkind (N) = N_Op_Ne)
4654 and then Ekind (Etype (L)) = E_Access_Attribute_Type
4655 and then Ekind (Etype (R)) = E_Access_Attribute_Type
4656 then
4657 Error_Msg_N
4658 ("two access attributes cannot be compared directly", N);
4659 Error_Msg_N
aab883ec 4660 ("\use qualified expression for one of the operands",
996ae0b0
RK
4661 N);
4662 return;
4663
4664 -- Another one for C programmers
4665
4666 elsif Nkind (N) = N_Op_Concat
4667 and then Valid_Boolean_Arg (Etype (L))
4668 and then Valid_Boolean_Arg (Etype (R))
4669 then
4670 Error_Msg_N ("invalid operands for concatenation", N);
4671 Error_Msg_N ("\maybe AND was meant", N);
4672 return;
4673
4674 -- A special case for comparison of access parameter with null
4675
4676 elsif Nkind (N) = N_Op_Eq
4677 and then Is_Entity_Name (L)
4678 and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
4679 and then Nkind (Parameter_Type (Parent (Entity (L)))) =
4680 N_Access_Definition
4681 and then Nkind (R) = N_Null
4682 then
4683 Error_Msg_N ("access parameter is not allowed to be null", L);
4684 Error_Msg_N ("\(call would raise Constraint_Error)", L);
4685 return;
4686 end if;
4687
0e0eecec
ES
4688 -- If we fall through then just give general message. Note that in
4689 -- the following messages, if the operand is overloaded we choose
4690 -- an arbitrary type to complain about, but that is probably more
4691 -- useful than not giving a type at all.
996ae0b0
RK
4692
4693 if Nkind (N) in N_Unary_Op then
4694 Error_Msg_Node_2 := Etype (R);
4695 Error_Msg_N ("operator& not defined for}", N);
4696 return;
4697
4698 else
fbf5a39b
AC
4699 if Nkind (N) in N_Binary_Op then
4700 if not Is_Overloaded (L)
4701 and then not Is_Overloaded (R)
4702 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
4703 then
7ffd9312 4704 Error_Msg_Node_2 := First_Subtype (Etype (R));
fbf5a39b 4705 Error_Msg_N ("there is no applicable operator& for}", N);
996ae0b0 4706
fbf5a39b 4707 else
b67a385c
ES
4708 -- Another attempt to find a fix: one of the candidate
4709 -- interpretations may not be use-visible. This has
4710 -- already been checked for predefined operators, so
4711 -- we examine only user-defined functions.
4712
4713 Op_Id := Get_Name_Entity_Id (Chars (N));
4714
4715 while Present (Op_Id) loop
4716 if Ekind (Op_Id) /= E_Operator
4717 and then Is_Overloadable (Op_Id)
4718 then
4719 if not Is_Immediately_Visible (Op_Id)
4720 and then not In_Use (Scope (Op_Id))
aab883ec 4721 and then not Is_Abstract_Subprogram (Op_Id)
b67a385c
ES
4722 and then not Is_Hidden (Op_Id)
4723 and then Ekind (Scope (Op_Id)) = E_Package
4724 and then
4725 Has_Compatible_Type
4726 (L, Etype (First_Formal (Op_Id)))
4727 and then Present
4728 (Next_Formal (First_Formal (Op_Id)))
4729 and then
4730 Has_Compatible_Type
4731 (R,
4732 Etype (Next_Formal (First_Formal (Op_Id))))
4733 then
4734 Error_Msg_N
4735 ("No legal interpretation for operator&", N);
4736 Error_Msg_NE
4737 ("\use clause on& would make operation legal",
4738 N, Scope (Op_Id));
4739 exit;
4740 end if;
4741 end if;
fbf5a39b 4742
b67a385c
ES
4743 Op_Id := Homonym (Op_Id);
4744 end loop;
4745
4746 if No (Op_Id) then
4747 Error_Msg_N ("invalid operand types for operator&", N);
4748
4749 if Nkind (N) /= N_Op_Concat then
4750 Error_Msg_NE ("\left operand has}!", N, Etype (L));
4751 Error_Msg_NE ("\right operand has}!", N, Etype (R));
4752 end if;
fbf5a39b
AC
4753 end if;
4754 end if;
996ae0b0
RK
4755 end if;
4756 end if;
4757 end;
4758 end if;
4759 end Operator_Check;
4760
6e73e3ab
AC
4761 -----------------------------------------
4762 -- Process_Implicit_Dereference_Prefix --
4763 -----------------------------------------
4764
4765 procedure Process_Implicit_Dereference_Prefix
da709d08
AC
4766 (E : Entity_Id;
4767 P : Entity_Id)
6e73e3ab
AC
4768 is
4769 Ref : Node_Id;
da709d08 4770
6e73e3ab 4771 begin
1a8fae99
ES
4772 if Present (E)
4773 and then (Operating_Mode = Check_Semantics or else not Expander_Active)
4774 then
6e73e3ab
AC
4775 -- We create a dummy reference to E to ensure that the reference
4776 -- is not considered as part of an assignment (an implicit
4777 -- dereference can never assign to its prefix). The Comes_From_Source
4778 -- attribute needs to be propagated for accurate warnings.
4779
4780 Ref := New_Reference_To (E, Sloc (P));
4781 Set_Comes_From_Source (Ref, Comes_From_Source (P));
4782 Generate_Reference (E, Ref);
4783 end if;
4784 end Process_Implicit_Dereference_Prefix;
4785
30c20106
AC
4786 --------------------------------
4787 -- Remove_Abstract_Operations --
4788 --------------------------------
4789
4790 procedure Remove_Abstract_Operations (N : Node_Id) is
401093c1
ES
4791 Abstract_Op : Entity_Id := Empty;
4792 Address_Kludge : Boolean := False;
4793 I : Interp_Index;
4794 It : Interp;
30c20106 4795
0e0eecec
ES
4796 -- AI-310: If overloaded, remove abstract non-dispatching operations. We
4797 -- activate this if either extensions are enabled, or if the abstract
4798 -- operation in question comes from a predefined file. This latter test
4799 -- allows us to use abstract to make operations invisible to users. In
4800 -- particular, if type Address is non-private and abstract subprograms
4801 -- are used to hide its operators, they will be truly hidden.
30c20106 4802
5950a3ac 4803 type Operand_Position is (First_Op, Second_Op);
8a36a0cc 4804 Univ_Type : constant Entity_Id := Universal_Interpretation (N);
5950a3ac
AC
4805
4806 procedure Remove_Address_Interpretations (Op : Operand_Position);
0e0eecec
ES
4807 -- Ambiguities may arise when the operands are literal and the address
4808 -- operations in s-auxdec are visible. In that case, remove the
4809 -- interpretation of a literal as Address, to retain the semantics of
4810 -- Address as a private type.
9f4fd324
AC
4811
4812 ------------------------------------
5950a3ac 4813 -- Remove_Address_Interpretations --
9f4fd324
AC
4814 ------------------------------------
4815
5950a3ac 4816 procedure Remove_Address_Interpretations (Op : Operand_Position) is
9f4fd324
AC
4817 Formal : Entity_Id;
4818
4819 begin
4820 if Is_Overloaded (N) then
4821 Get_First_Interp (N, I, It);
4822 while Present (It.Nam) loop
4823 Formal := First_Entity (It.Nam);
4824
5950a3ac
AC
4825 if Op = Second_Op then
4826 Formal := Next_Entity (Formal);
4827 end if;
4828
4829 if Is_Descendent_Of_Address (Etype (Formal)) then
401093c1 4830 Address_Kludge := True;
9f4fd324
AC
4831 Remove_Interp (I);
4832 end if;
4833
4834 Get_Next_Interp (I, It);
4835 end loop;
4836 end if;
4837 end Remove_Address_Interpretations;
4838
4839 -- Start of processing for Remove_Abstract_Operations
4840
30c20106 4841 begin
d935a36e 4842 if Is_Overloaded (N) then
30c20106 4843 Get_First_Interp (N, I, It);
d935a36e 4844
30c20106 4845 while Present (It.Nam) loop
aab883ec
ES
4846 if Is_Overloadable (It.Nam)
4847 and then Is_Abstract_Subprogram (It.Nam)
30c20106
AC
4848 and then not Is_Dispatching_Operation (It.Nam)
4849 then
af152989 4850 Abstract_Op := It.Nam;
fe45e59e 4851
401093c1
ES
4852 if Is_Descendent_Of_Address (It.Typ) then
4853 Address_Kludge := True;
4854 Remove_Interp (I);
4855 exit;
4856
fe45e59e
ES
4857 -- In Ada 2005, this operation does not participate in Overload
4858 -- resolution. If the operation is defined in in a predefined
4859 -- unit, it is one of the operations declared abstract in some
4860 -- variants of System, and it must be removed as well.
4861
401093c1
ES
4862 elsif Ada_Version >= Ada_05
4863 or else Is_Predefined_File_Name
4864 (Unit_File_Name (Get_Source_Unit (It.Nam)))
fe45e59e
ES
4865 then
4866 Remove_Interp (I);
4867 exit;
4868 end if;
30c20106
AC
4869 end if;
4870
4871 Get_Next_Interp (I, It);
4872 end loop;
4873
af152989 4874 if No (Abstract_Op) then
fe45e59e
ES
4875
4876 -- If some interpretation yields an integer type, it is still
4877 -- possible that there are address interpretations. Remove them
4878 -- if one operand is a literal, to avoid spurious ambiguities
4879 -- on systems where Address is a visible integer type.
4880
4881 if Is_Overloaded (N)
401093c1 4882 and then Nkind (N) in N_Op
fe45e59e
ES
4883 and then Is_Integer_Type (Etype (N))
4884 then
4885 if Nkind (N) in N_Binary_Op then
4886 if Nkind (Right_Opnd (N)) = N_Integer_Literal then
4887 Remove_Address_Interpretations (Second_Op);
4888
4889 elsif Nkind (Right_Opnd (N)) = N_Integer_Literal then
4890 Remove_Address_Interpretations (First_Op);
4891 end if;
4892 end if;
4893 end if;
3984e89a
AC
4894
4895 elsif Nkind (N) in N_Op then
4c46b835 4896
fe45e59e
ES
4897 -- Remove interpretations that treat literals as addresses. This
4898 -- is never appropriate, even when Address is defined as a visible
4899 -- Integer type. The reason is that we would really prefer Address
4900 -- to behave as a private type, even in this case, which is there
4901 -- only to accomodate oddities of VMS address sizes. If Address is
4902 -- a visible integer type, we get lots of overload ambiguities.
30c20106 4903
5950a3ac
AC
4904 if Nkind (N) in N_Binary_Op then
4905 declare
4906 U1 : constant Boolean :=
4907 Present (Universal_Interpretation (Right_Opnd (N)));
4908 U2 : constant Boolean :=
4909 Present (Universal_Interpretation (Left_Opnd (N)));
30c20106 4910
5950a3ac 4911 begin
0e0eecec 4912 if U1 then
5950a3ac 4913 Remove_Address_Interpretations (Second_Op);
0e0eecec 4914 end if;
5950a3ac 4915
0e0eecec 4916 if U2 then
5950a3ac 4917 Remove_Address_Interpretations (First_Op);
30c20106
AC
4918 end if;
4919
5950a3ac
AC
4920 if not (U1 and U2) then
4921
4922 -- Remove corresponding predefined operator, which is
4923 -- always added to the overload set.
4924
4925 Get_First_Interp (N, I, It);
4926 while Present (It.Nam) loop
0ab80019
AC
4927 if Scope (It.Nam) = Standard_Standard
4928 and then Base_Type (It.Typ) =
4929 Base_Type (Etype (Abstract_Op))
4930 then
5950a3ac
AC
4931 Remove_Interp (I);
4932 end if;
4933
8a36a0cc
AC
4934 Get_Next_Interp (I, It);
4935 end loop;
4936
4937 elsif Is_Overloaded (N)
4938 and then Present (Univ_Type)
4939 then
4940 -- If both operands have a universal interpretation,
0e0eecec
ES
4941 -- it is still necessary to remove interpretations that
4942 -- yield Address. Any remaining ambiguities will be
4943 -- removed in Disambiguate.
8a36a0cc
AC
4944
4945 Get_First_Interp (N, I, It);
8a36a0cc 4946 while Present (It.Nam) loop
0e0eecec
ES
4947 if Is_Descendent_Of_Address (It.Typ) then
4948 Remove_Interp (I);
4949
4950 elsif not Is_Type (It.Nam) then
8a36a0cc 4951 Set_Entity (N, It.Nam);
8a36a0cc
AC
4952 end if;
4953
5950a3ac
AC
4954 Get_Next_Interp (I, It);
4955 end loop;
4956 end if;
4957 end;
30c20106 4958 end if;
3984e89a
AC
4959
4960 elsif Nkind (N) = N_Function_Call
4961 and then
4962 (Nkind (Name (N)) = N_Operator_Symbol
4963 or else
4964 (Nkind (Name (N)) = N_Expanded_Name
4965 and then
4966 Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
4967 then
5950a3ac 4968
3984e89a
AC
4969 declare
4970 Arg1 : constant Node_Id := First (Parameter_Associations (N));
5950a3ac
AC
4971 U1 : constant Boolean :=
4972 Present (Universal_Interpretation (Arg1));
4973 U2 : constant Boolean :=
4974 Present (Next (Arg1)) and then
4975 Present (Universal_Interpretation (Next (Arg1)));
3984e89a
AC
4976
4977 begin
0e0eecec 4978 if U1 then
5950a3ac 4979 Remove_Address_Interpretations (First_Op);
0e0eecec 4980 end if;
3984e89a 4981
0e0eecec 4982 if U2 then
5950a3ac
AC
4983 Remove_Address_Interpretations (Second_Op);
4984 end if;
4985
4986 if not (U1 and U2) then
3984e89a
AC
4987 Get_First_Interp (N, I, It);
4988 while Present (It.Nam) loop
9f4fd324
AC
4989 if Scope (It.Nam) = Standard_Standard
4990 and then It.Typ = Base_Type (Etype (Abstract_Op))
4991 then
3984e89a
AC
4992 Remove_Interp (I);
4993 end if;
4994
4995 Get_Next_Interp (I, It);
4996 end loop;
4997 end if;
4998 end;
30c20106 4999 end if;
af152989 5000
401093c1
ES
5001 -- If the removal has left no valid interpretations, emit an error
5002 -- message now and label node as illegal.
af152989
AC
5003
5004 if Present (Abstract_Op) then
5005 Get_First_Interp (N, I, It);
5006
5007 if No (It.Nam) then
5008
6e73e3ab 5009 -- Removal of abstract operation left no viable candidate
af152989
AC
5010
5011 Set_Etype (N, Any_Type);
5012 Error_Msg_Sloc := Sloc (Abstract_Op);
5013 Error_Msg_NE
5014 ("cannot call abstract operation& declared#", N, Abstract_Op);
401093c1
ES
5015
5016 -- In Ada 2005, an abstract operation may disable predefined
5017 -- operators. Since the context is not yet known, we mark the
5018 -- predefined operators as potentially hidden. Do not include
5019 -- predefined operators when addresses are involved since this
5020 -- case is handled separately.
5021
5022 elsif Ada_Version >= Ada_05
5023 and then not Address_Kludge
5024 then
5025 while Present (It.Nam) loop
5026 if Is_Numeric_Type (It.Typ)
5027 and then Scope (It.Typ) = Standard_Standard
5028 then
5029 Set_Abstract_Op (I, Abstract_Op);
5030 end if;
5031
5032 Get_Next_Interp (I, It);
5033 end loop;
af152989
AC
5034 end if;
5035 end if;
30c20106
AC
5036 end if;
5037 end Remove_Abstract_Operations;
5038
996ae0b0
RK
5039 -----------------------
5040 -- Try_Indirect_Call --
5041 -----------------------
5042
5043 function Try_Indirect_Call
91b1417d
AC
5044 (N : Node_Id;
5045 Nam : Entity_Id;
5046 Typ : Entity_Id) return Boolean
996ae0b0 5047 is
fbf5a39b
AC
5048 Actual : Node_Id;
5049 Formal : Entity_Id;
8a7988f5 5050 Call_OK : Boolean;
996ae0b0
RK
5051
5052 begin
8a7988f5 5053 Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
9de61fcb 5054
8a7988f5 5055 Actual := First_Actual (N);
fbf5a39b 5056 Formal := First_Formal (Designated_Type (Typ));
9de61fcb 5057 while Present (Actual) and then Present (Formal) loop
996ae0b0
RK
5058 if not Has_Compatible_Type (Actual, Etype (Formal)) then
5059 return False;
5060 end if;
5061
5062 Next (Actual);
5063 Next_Formal (Formal);
5064 end loop;
5065
5066 if No (Actual) and then No (Formal) then
5067 Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
5068
5069 -- Nam is a candidate interpretation for the name in the call,
5070 -- if it is not an indirect call.
5071
5072 if not Is_Type (Nam)
5073 and then Is_Entity_Name (Name (N))
5074 then
5075 Set_Entity (Name (N), Nam);
5076 end if;
5077
5078 return True;
5079 else
5080 return False;
5081 end if;
5082 end Try_Indirect_Call;
5083
5084 ----------------------
5085 -- Try_Indexed_Call --
5086 ----------------------
5087
5088 function Try_Indexed_Call
aab883ec
ES
5089 (N : Node_Id;
5090 Nam : Entity_Id;
5091 Typ : Entity_Id;
5092 Skip_First : Boolean) return Boolean
996ae0b0 5093 is
fbf5a39b
AC
5094 Actuals : constant List_Id := Parameter_Associations (N);
5095 Actual : Node_Id;
5096 Index : Entity_Id;
996ae0b0
RK
5097
5098 begin
fbf5a39b 5099 Actual := First (Actuals);
aab883ec
ES
5100
5101 -- If the call was originally written in prefix form, skip the first
5102 -- actual, which is obviously not defaulted.
5103
5104 if Skip_First then
5105 Next (Actual);
5106 end if;
5107
fbf5a39b 5108 Index := First_Index (Typ);
9de61fcb
RD
5109 while Present (Actual) and then Present (Index) loop
5110
996ae0b0
RK
5111 -- If the parameter list has a named association, the expression
5112 -- is definitely a call and not an indexed component.
5113
5114 if Nkind (Actual) = N_Parameter_Association then
5115 return False;
5116 end if;
5117
5118 if not Has_Compatible_Type (Actual, Etype (Index)) then
5119 return False;
5120 end if;
5121
5122 Next (Actual);
5123 Next_Index (Index);
5124 end loop;
5125
5126 if No (Actual) and then No (Index) then
5127 Add_One_Interp (N, Nam, Component_Type (Typ));
5128
5129 -- Nam is a candidate interpretation for the name in the call,
5130 -- if it is not an indirect call.
5131
5132 if not Is_Type (Nam)
5133 and then Is_Entity_Name (Name (N))
5134 then
5135 Set_Entity (Name (N), Nam);
5136 end if;
5137
5138 return True;
5139 else
5140 return False;
5141 end if;
996ae0b0
RK
5142 end Try_Indexed_Call;
5143
35ae2ed8
AC
5144 --------------------------
5145 -- Try_Object_Operation --
5146 --------------------------
5147
5148 function Try_Object_Operation (N : Node_Id) return Boolean is
b67a385c
ES
5149 K : constant Node_Kind := Nkind (Parent (N));
5150 Loc : constant Source_Ptr := Sloc (N);
5151 Candidate : Entity_Id := Empty;
5152 Is_Subprg_Call : constant Boolean := K = N_Procedure_Call_Statement
5153 or else K = N_Function_Call;
5154 Obj : constant Node_Id := Prefix (N);
0a36105d
JM
5155 Subprog : constant Node_Id :=
5156 Make_Identifier (Sloc (Selector_Name (N)),
5157 Chars => Chars (Selector_Name (N)));
401093c1 5158 -- Identifier on which possible interpretations will be collected
0a36105d 5159
b67a385c
ES
5160 Success : Boolean := False;
5161
5162 Report_Error : Boolean := False;
5163 -- If no candidate interpretation matches the context, redo the
5164 -- analysis with error enabled to provide additional information.
28d6470f
JM
5165
5166 Actual : Node_Id;
b67a385c 5167 New_Call_Node : Node_Id := Empty;
4c46b835 5168 Node_To_Replace : Node_Id;
28d6470f 5169 Obj_Type : Entity_Id := Etype (Obj);
4c46b835 5170
0a36105d
JM
5171 function Valid_Candidate
5172 (Success : Boolean;
5173 Call : Node_Id;
5174 Subp : Entity_Id) return Entity_Id;
5175 -- If the subprogram is a valid interpretation, record it, and add
5176 -- to the list of interpretations of Subprog.
5177
4c46b835
AC
5178 procedure Complete_Object_Operation
5179 (Call_Node : Node_Id;
0a36105d 5180 Node_To_Replace : Node_Id);
ec6078e3
ES
5181 -- Make Subprog the name of Call_Node, replace Node_To_Replace with
5182 -- Call_Node, insert the object (or its dereference) as the first actual
5183 -- in the call, and complete the analysis of the call.
4c46b835 5184
0a36105d
JM
5185 procedure Report_Ambiguity (Op : Entity_Id);
5186 -- If a prefixed procedure call is ambiguous, indicate whether the
5187 -- call includes an implicit dereference or an implicit 'Access.
5188
4c46b835
AC
5189 procedure Transform_Object_Operation
5190 (Call_Node : out Node_Id;
0a36105d 5191 Node_To_Replace : out Node_Id);
ec6078e3
ES
5192 -- Transform Obj.Operation (X, Y,,) into Operation (Obj, X, Y ..)
5193 -- Call_Node is the resulting subprogram call,
5194 -- Node_To_Replace is either N or the parent of N, and Subprog
5195 -- is a reference to the subprogram we are trying to match.
35ae2ed8
AC
5196
5197 function Try_Class_Wide_Operation
4c46b835
AC
5198 (Call_Node : Node_Id;
5199 Node_To_Replace : Node_Id) return Boolean;
ec6078e3
ES
5200 -- Traverse all ancestor types looking for a class-wide subprogram
5201 -- for which the current operation is a valid non-dispatching call.
35ae2ed8 5202
0a36105d
JM
5203 procedure Try_One_Prefix_Interpretation (T : Entity_Id);
5204 -- If prefix is overloaded, its interpretation may include different
5205 -- tagged types, and we must examine the primitive operations and
5206 -- the class-wide operations of each in order to find candidate
5207 -- interpretations for the call as a whole.
5208
4c46b835
AC
5209 function Try_Primitive_Operation
5210 (Call_Node : Node_Id;
5211 Node_To_Replace : Node_Id) return Boolean;
ec6078e3
ES
5212 -- Traverse the list of primitive subprograms looking for a dispatching
5213 -- operation for which the current node is a valid call .
4c46b835 5214
0a36105d
JM
5215 ---------------------
5216 -- Valid_Candidate --
5217 ---------------------
5218
5219 function Valid_Candidate
5220 (Success : Boolean;
5221 Call : Node_Id;
5222 Subp : Entity_Id) return Entity_Id
5223 is
5224 Comp_Type : Entity_Id;
5225
5226 begin
5227 -- If the subprogram is a valid interpretation, record it in global
5228 -- variable Subprog, to collect all possible overloadings.
5229
5230 if Success then
5231 if Subp /= Entity (Subprog) then
5232 Add_One_Interp (Subprog, Subp, Etype (Subp));
5233 end if;
5234 end if;
5235
5236 -- If the call may be an indexed call, retrieve component type
5237 -- of resulting expression, and add possible interpretation.
5238
5239 Comp_Type := Empty;
5240
5241 if Nkind (Call) = N_Function_Call
5242 and then Nkind (Parent (N)) = N_Indexed_Component
5243 and then Needs_One_Actual (Subp)
5244 then
5245 if Is_Array_Type (Etype (Subp)) then
5246 Comp_Type := Component_Type (Etype (Subp));
5247
5248 elsif Is_Access_Type (Etype (Subp))
5249 and then Is_Array_Type (Designated_Type (Etype (Subp)))
5250 then
5251 Comp_Type := Component_Type (Designated_Type (Etype (Subp)));
5252 end if;
5253 end if;
5254
5255 if Present (Comp_Type)
5256 and then Etype (Subprog) /= Comp_Type
5257 then
5258 Add_One_Interp (Subprog, Subp, Comp_Type);
5259 end if;
5260
5261 if Etype (Call) /= Any_Type then
5262 return Subp;
5263 else
5264 return Empty;
5265 end if;
5266 end Valid_Candidate;
5267
4c46b835
AC
5268 -------------------------------
5269 -- Complete_Object_Operation --
5270 -------------------------------
5271
5272 procedure Complete_Object_Operation
5273 (Call_Node : Node_Id;
0a36105d 5274 Node_To_Replace : Node_Id)
4c46b835 5275 is
fe45e59e
ES
5276 Formal_Type : constant Entity_Id :=
5277 Etype (First_Formal (Entity (Subprog)));
ec6078e3
ES
5278 First_Actual : Node_Id;
5279
4c46b835 5280 begin
0a36105d
JM
5281 -- Place the name of the operation, with its interpretations,
5282 -- on the rewritten call.
5283
ec6078e3
ES
5284 Set_Name (Call_Node, Subprog);
5285
0a36105d
JM
5286 First_Actual := First (Parameter_Associations (Call_Node));
5287
b67a385c
ES
5288 -- For cross-reference purposes, treat the new node as being in
5289 -- the source if the original one is.
5290
5291 Set_Comes_From_Source (Subprog, Comes_From_Source (N));
5292 Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
5293
ec6078e3
ES
5294 if Nkind (N) = N_Selected_Component
5295 and then not Inside_A_Generic
5296 then
5297 Set_Entity (Selector_Name (N), Entity (Subprog));
5298 end if;
5299
5300 -- If need be, rewrite first actual as an explicit dereference
0a36105d
JM
5301 -- If the call is overloaded, the rewriting can only be done
5302 -- once the primitive operation is identified.
5303
5304 if Is_Overloaded (Subprog) then
ec6078e3 5305
0a36105d
JM
5306 -- The prefix itself may be overloaded, and its interpretations
5307 -- must be propagated to the new actual in the call.
5308
5309 if Is_Overloaded (Obj) then
5310 Save_Interps (Obj, First_Actual);
5311 end if;
5312
5313 Rewrite (First_Actual, Obj);
5314
5315 elsif not Is_Access_Type (Formal_Type)
ec6078e3
ES
5316 and then Is_Access_Type (Etype (Obj))
5317 then
5318 Rewrite (First_Actual,
5319 Make_Explicit_Dereference (Sloc (Obj), Obj));
5320 Analyze (First_Actual);
fe45e59e 5321
401093c1
ES
5322 -- If we need to introduce an explicit dereference, verify that
5323 -- the resulting actual is compatible with the mode of the formal.
5324
5325 if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
5326 and then Is_Access_Constant (Etype (Obj))
5327 then
5328 Error_Msg_NE
5329 ("expect variable in call to&", Prefix (N), Entity (Subprog));
5330 end if;
5331
fe45e59e
ES
5332 -- Conversely, if the formal is an access parameter and the
5333 -- object is not, replace the actual with a 'Access reference.
5334 -- Its analysis will check that the object is aliased.
5335
5336 elsif Is_Access_Type (Formal_Type)
5337 and then not Is_Access_Type (Etype (Obj))
5338 then
5339 Rewrite (First_Actual,
5340 Make_Attribute_Reference (Loc,
5341 Attribute_Name => Name_Access,
5342 Prefix => Relocate_Node (Obj)));
0a36105d
JM
5343
5344 if not Is_Aliased_View (Obj) then
5345 Error_Msg_NE
5346 ("object in prefixed call to& must be aliased"
401093c1 5347 & " (RM-2005 4.3.1 (13))",
0a36105d
JM
5348 Prefix (First_Actual), Subprog);
5349 end if;
5350
fe45e59e
ES
5351 Analyze (First_Actual);
5352
ec6078e3 5353 else
0a36105d
JM
5354 if Is_Overloaded (Obj) then
5355 Save_Interps (Obj, First_Actual);
5356 end if;
ec6078e3 5357
0a36105d 5358 Rewrite (First_Actual, Obj);
aab883ec
ES
5359 end if;
5360
7ffd9312 5361 Rewrite (Node_To_Replace, Call_Node);
0a36105d
JM
5362
5363 -- Propagate the interpretations collected in subprog to the new
5364 -- function call node, to be resolved from context.
5365
5366 if Is_Overloaded (Subprog) then
5367 Save_Interps (Subprog, Node_To_Replace);
5368 else
5369 Analyze (Node_To_Replace);
5370 end if;
4c46b835
AC
5371 end Complete_Object_Operation;
5372
0a36105d
JM
5373 ----------------------
5374 -- Report_Ambiguity --
5375 ----------------------
5376
5377 procedure Report_Ambiguity (Op : Entity_Id) is
5378 Access_Formal : constant Boolean :=
5379 Is_Access_Type (Etype (First_Formal (Op)));
5380 Access_Actual : constant Boolean :=
5381 Is_Access_Type (Etype (Prefix (N)));
5382
5383 begin
5384 Error_Msg_Sloc := Sloc (Op);
5385
5386 if Access_Formal and then not Access_Actual then
5387 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
5388 Error_Msg_N
5389 ("\possible interpretation"
5390 & " (inherited, with implicit 'Access) #", N);
5391 else
5392 Error_Msg_N
5393 ("\possible interpretation (with implicit 'Access) #", N);
5394 end if;
5395
5396 elsif not Access_Formal and then Access_Actual then
5397 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
5398 Error_Msg_N
5399 ("\possible interpretation"
5400 & " ( inherited, with implicit dereference) #", N);
5401 else
5402 Error_Msg_N
5403 ("\possible interpretation (with implicit dereference) #", N);
5404 end if;
5405
5406 else
5407 if Nkind (Parent (Op)) = N_Full_Type_Declaration then
5408 Error_Msg_N ("\possible interpretation (inherited)#", N);
5409 else
5410 Error_Msg_N ("\possible interpretation#", N);
5411 end if;
5412 end if;
5413 end Report_Ambiguity;
5414
4c46b835
AC
5415 --------------------------------
5416 -- Transform_Object_Operation --
5417 --------------------------------
5418
5419 procedure Transform_Object_Operation
5420 (Call_Node : out Node_Id;
0a36105d 5421 Node_To_Replace : out Node_Id)
35ae2ed8 5422 is
4c46b835 5423 Parent_Node : constant Node_Id := Parent (N);
35ae2ed8 5424
ec6078e3
ES
5425 Dummy : constant Node_Id := New_Copy (Obj);
5426 -- Placeholder used as a first parameter in the call, replaced
5427 -- eventually by the proper object.
5428
5429 Actuals : List_Id;
5430 Actual : Node_Id;
5431
35ae2ed8 5432 begin
ec6078e3
ES
5433 -- Common case covering 1) Call to a procedure and 2) Call to a
5434 -- function that has some additional actuals.
35ae2ed8 5435
4c46b835
AC
5436 if (Nkind (Parent_Node) = N_Function_Call
5437 or else
5438 Nkind (Parent_Node) = N_Procedure_Call_Statement)
35ae2ed8 5439
ec6078e3
ES
5440 -- N is a selected component node containing the name of the
5441 -- subprogram. If N is not the name of the parent node we must
5442 -- not replace the parent node by the new construct. This case
5443 -- occurs when N is a parameterless call to a subprogram that
5444 -- is an actual parameter of a call to another subprogram. For
5445 -- example:
5446 -- Some_Subprogram (..., Obj.Operation, ...)
35ae2ed8 5447
ec6078e3 5448 and then Name (Parent_Node) = N
4c46b835
AC
5449 then
5450 Node_To_Replace := Parent_Node;
35ae2ed8 5451
ec6078e3 5452 Actuals := Parameter_Associations (Parent_Node);
d3e65aad 5453
ec6078e3
ES
5454 if Present (Actuals) then
5455 Prepend (Dummy, Actuals);
5456 else
5457 Actuals := New_List (Dummy);
5458 end if;
4c46b835
AC
5459
5460 if Nkind (Parent_Node) = N_Procedure_Call_Statement then
5461 Call_Node :=
5462 Make_Procedure_Call_Statement (Loc,
0a36105d 5463 Name => New_Copy (Subprog),
4c46b835
AC
5464 Parameter_Associations => Actuals);
5465
5466 else
4c46b835
AC
5467 Call_Node :=
5468 Make_Function_Call (Loc,
0a36105d 5469 Name => New_Copy (Subprog),
4c46b835 5470 Parameter_Associations => Actuals);
35ae2ed8 5471
35ae2ed8
AC
5472 end if;
5473
ec6078e3
ES
5474 -- Before analysis, the function call appears as an indexed component
5475 -- if there are no named associations.
758c442c 5476
ec6078e3
ES
5477 elsif Nkind (Parent_Node) = N_Indexed_Component
5478 and then N = Prefix (Parent_Node)
5479 then
758c442c
GD
5480 Node_To_Replace := Parent_Node;
5481
ec6078e3
ES
5482 Actuals := Expressions (Parent_Node);
5483
5484 Actual := First (Actuals);
5485 while Present (Actual) loop
5486 Analyze (Actual);
5487 Next (Actual);
5488 end loop;
5489
5490 Prepend (Dummy, Actuals);
758c442c
GD
5491
5492 Call_Node :=
5493 Make_Function_Call (Loc,
0a36105d 5494 Name => New_Copy (Subprog),
758c442c
GD
5495 Parameter_Associations => Actuals);
5496
ec6078e3 5497 -- Parameterless call: Obj.F is rewritten as F (Obj)
35ae2ed8 5498
4c46b835
AC
5499 else
5500 Node_To_Replace := N;
5501
5502 Call_Node :=
5503 Make_Function_Call (Loc,
0a36105d 5504 Name => New_Copy (Subprog),
ec6078e3 5505 Parameter_Associations => New_List (Dummy));
4c46b835
AC
5506 end if;
5507 end Transform_Object_Operation;
35ae2ed8
AC
5508
5509 ------------------------------
5510 -- Try_Class_Wide_Operation --
5511 ------------------------------
5512
5513 function Try_Class_Wide_Operation
4c46b835
AC
5514 (Call_Node : Node_Id;
5515 Node_To_Replace : Node_Id) return Boolean
35ae2ed8 5516 is
0a36105d
JM
5517 Anc_Type : Entity_Id;
5518 Matching_Op : Entity_Id := Empty;
5519 Error : Boolean;
5520
5521 procedure Traverse_Homonyms
5522 (Anc_Type : Entity_Id;
5523 Error : out Boolean);
5524 -- Traverse the homonym chain of the subprogram searching for those
5525 -- homonyms whose first formal has the Anc_Type's class-wide type,
5526 -- or an anonymous access type designating the class-wide type. If an
5527 -- ambiguity is detected, then Error is set to True.
5528
5529 procedure Traverse_Interfaces
5530 (Anc_Type : Entity_Id;
5531 Error : out Boolean);
5532 -- Traverse the list of interfaces, if any, associated with Anc_Type
5533 -- and search for acceptable class-wide homonyms associated with each
5534 -- interface. If an ambiguity is detected, then Error is set to True.
5535
5536 -----------------------
5537 -- Traverse_Homonyms --
5538 -----------------------
5539
5540 procedure Traverse_Homonyms
5541 (Anc_Type : Entity_Id;
5542 Error : out Boolean)
5543 is
5544 Cls_Type : Entity_Id;
5545 Hom : Entity_Id;
5546 Hom_Ref : Node_Id;
5547 Success : Boolean;
35ae2ed8 5548
0a36105d
JM
5549 begin
5550 Error := False;
ec6078e3 5551
b67a385c
ES
5552 Cls_Type := Class_Wide_Type (Anc_Type);
5553
4c46b835 5554 Hom := Current_Entity (Subprog);
401093c1
ES
5555
5556 -- Find operation whose first parameter is of the class-wide
5557 -- type, a subtype thereof, or an anonymous access to same.
5558
35ae2ed8
AC
5559 while Present (Hom) loop
5560 if (Ekind (Hom) = E_Procedure
4c46b835
AC
5561 or else
5562 Ekind (Hom) = E_Function)
b67a385c 5563 and then Scope (Hom) = Scope (Anc_Type)
4c46b835 5564 and then Present (First_Formal (Hom))
b67a385c 5565 and then
401093c1 5566 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
b67a385c
ES
5567 or else
5568 (Is_Access_Type (Etype (First_Formal (Hom)))
0a36105d
JM
5569 and then
5570 Ekind (Etype (First_Formal (Hom))) =
5571 E_Anonymous_Access_Type
b67a385c 5572 and then
401093c1
ES
5573 Base_Type
5574 (Designated_Type (Etype (First_Formal (Hom)))) =
0a36105d 5575 Cls_Type))
35ae2ed8 5576 then
ec6078e3 5577 Set_Etype (Call_Node, Any_Type);
0a36105d
JM
5578 Set_Is_Overloaded (Call_Node, False);
5579 Success := False;
4c46b835 5580
0a36105d
JM
5581 if No (Matching_Op) then
5582 Hom_Ref := New_Reference_To (Hom, Sloc (Subprog));
5583 Set_Etype (Call_Node, Any_Type);
5584 Set_Parent (Call_Node, Parent (Node_To_Replace));
4c46b835 5585
0a36105d 5586 Set_Name (Call_Node, Hom_Ref);
4c46b835 5587
0a36105d
JM
5588 Analyze_One_Call
5589 (N => Call_Node,
5590 Nam => Hom,
5591 Report => Report_Error,
5592 Success => Success,
5593 Skip_First => True);
4c46b835 5594
0a36105d
JM
5595 Matching_Op :=
5596 Valid_Candidate (Success, Call_Node, Hom);
4c46b835 5597
0a36105d
JM
5598 else
5599 Analyze_One_Call
5600 (N => Call_Node,
5601 Nam => Hom,
5602 Report => Report_Error,
5603 Success => Success,
5604 Skip_First => True);
5605
5606 if Present (Valid_Candidate (Success, Call_Node, Hom))
5607 and then Nkind (Call_Node) /= N_Function_Call
5608 then
5609 Error_Msg_NE ("ambiguous call to&", N, Hom);
5610 Report_Ambiguity (Matching_Op);
5611 Report_Ambiguity (Hom);
5612 Error := True;
5613 return;
5614 end if;
35ae2ed8
AC
5615 end if;
5616 end if;
5617
5618 Hom := Homonym (Hom);
5619 end loop;
0a36105d
JM
5620 end Traverse_Homonyms;
5621
5622 -------------------------
5623 -- Traverse_Interfaces --
5624 -------------------------
35ae2ed8 5625
0a36105d
JM
5626 procedure Traverse_Interfaces
5627 (Anc_Type : Entity_Id;
5628 Error : out Boolean)
5629 is
5630 Intface : Node_Id;
5631 Intface_List : constant List_Id :=
5632 Abstract_Interface_List (Anc_Type);
5633
5634 begin
5635 Error := False;
5636
5637 if Is_Non_Empty_List (Intface_List) then
5638 Intface := First (Intface_List);
5639 while Present (Intface) loop
5640
5641 -- Look for acceptable class-wide homonyms associated with
5642 -- the interface.
5643
5644 Traverse_Homonyms (Etype (Intface), Error);
5645
5646 if Error then
5647 return;
5648 end if;
5649
5650 -- Continue the search by looking at each of the interface's
5651 -- associated interface ancestors.
5652
5653 Traverse_Interfaces (Etype (Intface), Error);
5654
5655 if Error then
5656 return;
5657 end if;
5658
5659 Next (Intface);
5660 end loop;
5661 end if;
5662 end Traverse_Interfaces;
5663
5664 -- Start of processing for Try_Class_Wide_Operation
5665
5666 begin
5667 -- Loop through ancestor types (including interfaces), traversing the
5668 -- homonym chain of the subprogram, and trying out those homonyms
5669 -- whose first formal has the class-wide type of the ancestor, or an
5670 -- anonymous access type designating the class-wide type.
5671
5672 Anc_Type := Obj_Type;
5673 loop
5674 -- Look for a match among homonyms associated with the ancestor
5675
5676 Traverse_Homonyms (Anc_Type, Error);
5677
5678 if Error then
5679 return True;
5680 end if;
5681
5682 -- Continue the search for matches among homonyms associated with
5683 -- any interfaces implemented by the ancestor.
5684
5685 Traverse_Interfaces (Anc_Type, Error);
5686
5687 if Error then
5688 return True;
5689 end if;
35ae2ed8 5690
4c46b835
AC
5691 exit when Etype (Anc_Type) = Anc_Type;
5692 Anc_Type := Etype (Anc_Type);
35ae2ed8
AC
5693 end loop;
5694
0a36105d
JM
5695 if Present (Matching_Op) then
5696 Set_Etype (Call_Node, Etype (Matching_Op));
5697 end if;
ec6078e3 5698
0a36105d 5699 return Present (Matching_Op);
35ae2ed8
AC
5700 end Try_Class_Wide_Operation;
5701
0a36105d
JM
5702 -----------------------------------
5703 -- Try_One_Prefix_Interpretation --
5704 -----------------------------------
5705
5706 procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
5707 begin
5708 Obj_Type := T;
5709
5710 if Is_Access_Type (Obj_Type) then
5711 Obj_Type := Designated_Type (Obj_Type);
5712 end if;
5713
5714 if Ekind (Obj_Type) = E_Private_Subtype then
5715 Obj_Type := Base_Type (Obj_Type);
5716 end if;
5717
5718 if Is_Class_Wide_Type (Obj_Type) then
5719 Obj_Type := Etype (Class_Wide_Type (Obj_Type));
5720 end if;
5721
5722 -- The type may have be obtained through a limited_with clause,
5723 -- in which case the primitive operations are available on its
401093c1 5724 -- non-limited view. If still incomplete, retrieve full view.
0a36105d
JM
5725
5726 if Ekind (Obj_Type) = E_Incomplete_Type
5727 and then From_With_Type (Obj_Type)
5728 then
401093c1 5729 Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
0a36105d
JM
5730 end if;
5731
5732 -- If the object is not tagged, or the type is still an incomplete
5733 -- type, this is not a prefixed call.
5734
5735 if not Is_Tagged_Type (Obj_Type)
5736 or else Is_Incomplete_Type (Obj_Type)
5737 then
5738 return;
5739 end if;
5740
5741 if Try_Primitive_Operation
5742 (Call_Node => New_Call_Node,
5743 Node_To_Replace => Node_To_Replace)
5744 or else
5745 Try_Class_Wide_Operation
5746 (Call_Node => New_Call_Node,
5747 Node_To_Replace => Node_To_Replace)
5748 then
5749 null;
5750 end if;
5751 end Try_One_Prefix_Interpretation;
5752
4c46b835
AC
5753 -----------------------------
5754 -- Try_Primitive_Operation --
5755 -----------------------------
35ae2ed8 5756
4c46b835
AC
5757 function Try_Primitive_Operation
5758 (Call_Node : Node_Id;
5759 Node_To_Replace : Node_Id) return Boolean
35ae2ed8 5760 is
6e73e3ab
AC
5761 Elmt : Elmt_Id;
5762 Prim_Op : Entity_Id;
0a36105d
JM
5763 Matching_Op : Entity_Id := Empty;
5764 Prim_Op_Ref : Node_Id := Empty;
5765
5766 Corr_Type : Entity_Id := Empty;
5767 -- If the prefix is a synchronized type, the controlling type of
5768 -- the primitive operation is the corresponding record type, else
5769 -- this is the object type itself.
5770
5771 Success : Boolean := False;
35ae2ed8 5772
401093c1
ES
5773 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
5774 -- For tagged types the candidate interpretations are found in
5775 -- the list of primitive operations of the type and its ancestors.
5776 -- For formal tagged types we have to find the operations declared
5777 -- in the same scope as the type (including in the generic formal
5778 -- part) because the type itself carries no primitive operations,
5779 -- except for formal derived types that inherit the operations of
5780 -- the parent and progenitors.
5781
ec6078e3
ES
5782 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
5783 -- Verify that the prefix, dereferenced if need be, is a valid
5784 -- controlling argument in a call to Op. The remaining actuals
5785 -- are checked in the subsequent call to Analyze_One_Call.
35ae2ed8 5786
401093c1
ES
5787 ------------------------------
5788 -- Collect_Generic_Type_Ops --
5789 ------------------------------
5790
5791 function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
5792 Bas : constant Entity_Id := Base_Type (T);
5793 Candidates : constant Elist_Id := New_Elmt_List;
5794 Subp : Entity_Id;
5795 Formal : Entity_Id;
5796
5797 begin
5798 if Is_Derived_Type (T) then
5799 return Primitive_Operations (T);
5800
5801 else
5802 -- Scan the list of entities declared in the same scope as
5803 -- the type. In general this will be an open scope, given that
5804 -- the call we are analyzing can only appear within a generic
5805 -- declaration or body (either the one that declares T, or a
5806 -- child unit).
5807
5808 Subp := First_Entity (Scope (T));
5809 while Present (Subp) loop
5810 if Is_Overloadable (Subp) then
5811 Formal := First_Formal (Subp);
5812
5813 if Present (Formal)
5814 and then Is_Controlling_Formal (Formal)
5815 and then
5816 (Base_Type (Etype (Formal)) = Bas
5817 or else
5818 (Is_Access_Type (Etype (Formal))
5819 and then Designated_Type (Etype (Formal)) = Bas))
5820 then
5821 Append_Elmt (Subp, Candidates);
5822 end if;
5823 end if;
5824
5825 Next_Entity (Subp);
5826 end loop;
5827
5828 return Candidates;
5829 end if;
5830 end Collect_Generic_Type_Ops;
5831
ec6078e3
ES
5832 -----------------------------
5833 -- Valid_First_Argument_Of --
5834 -----------------------------
35ae2ed8 5835
ec6078e3
ES
5836 function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
5837 Typ : constant Entity_Id := Etype (First_Formal (Op));
35ae2ed8 5838
ec6078e3 5839 begin
0a36105d
JM
5840 -- Simple case. Object may be a subtype of the tagged type
5841 -- or may be the corresponding record of a synchronized type.
5d09245e 5842
aab883ec
ES
5843 return Obj_Type = Typ
5844 or else Base_Type (Obj_Type) = Typ
5d09245e 5845
0a36105d
JM
5846 or else Corr_Type = Typ
5847
5848 -- Prefix can be dereferenced
725e2a15 5849
ec6078e3 5850 or else
0a36105d
JM
5851 (Is_Access_Type (Corr_Type)
5852 and then Designated_Type (Corr_Type) = Typ)
5d09245e 5853
0a36105d
JM
5854 -- Formal is an access parameter, for which the object
5855 -- can provide an access.
35ae2ed8 5856
ec6078e3
ES
5857 or else
5858 (Ekind (Typ) = E_Anonymous_Access_Type
0a36105d 5859 and then Designated_Type (Typ) = Base_Type (Corr_Type));
ec6078e3 5860 end Valid_First_Argument_Of;
35ae2ed8 5861
ec6078e3 5862 -- Start of processing for Try_Primitive_Operation
35ae2ed8 5863
ec6078e3 5864 begin
0a36105d
JM
5865 -- Look for subprograms in the list of primitive operations The name
5866 -- must be identical, and the kind of call indicates the expected
5867 -- kind of operation (function or procedure). If the type is a
5868 -- (tagged) synchronized type, the primitive ops are attached to
5869 -- the corresponding record type.
aab883ec
ES
5870
5871 if Is_Concurrent_Type (Obj_Type) then
0a36105d
JM
5872 Corr_Type := Corresponding_Record_Type (Obj_Type);
5873 Elmt := First_Elmt (Primitive_Operations (Corr_Type));
401093c1
ES
5874
5875 elsif not Is_Generic_Type (Obj_Type) then
0a36105d 5876 Corr_Type := Obj_Type;
aab883ec 5877 Elmt := First_Elmt (Primitive_Operations (Obj_Type));
401093c1
ES
5878
5879 else
5880 Corr_Type := Obj_Type;
5881 Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
aab883ec 5882 end if;
35ae2ed8 5883
ec6078e3
ES
5884 while Present (Elmt) loop
5885 Prim_Op := Node (Elmt);
5886
5887 if Chars (Prim_Op) = Chars (Subprog)
5888 and then Present (First_Formal (Prim_Op))
5889 and then Valid_First_Argument_Of (Prim_Op)
fe45e59e
ES
5890 and then
5891 (Nkind (Call_Node) = N_Function_Call)
5892 = (Ekind (Prim_Op) = E_Function)
ec6078e3 5893 then
b67a385c
ES
5894 -- Ada 2005 (AI-251): If this primitive operation corresponds
5895 -- with an immediate ancestor interface there is no need to add
5896 -- it to the list of interpretations; the corresponding aliased
5897 -- primitive is also in this list of primitive operations and
5898 -- will be used instead.
fe45e59e 5899
0a36105d
JM
5900 if (Present (Abstract_Interface_Alias (Prim_Op))
5901 and then Is_Ancestor (Find_Dispatching_Type
5902 (Alias (Prim_Op)), Corr_Type))
5903 or else
5904
5905 -- Do not consider hidden primitives unless they belong to a
5906 -- generic private type with a tagged parent.
5907
5908 (Is_Hidden (Prim_Op)
5909 and then not Is_Immediately_Visible (Obj_Type))
fe45e59e
ES
5910 then
5911 goto Continue;
5912 end if;
5913
0a36105d
JM
5914 Set_Etype (Call_Node, Any_Type);
5915 Set_Is_Overloaded (Call_Node, False);
5916
5917 if No (Matching_Op) then
fe45e59e 5918 Prim_Op_Ref := New_Reference_To (Prim_Op, Sloc (Subprog));
b67a385c 5919 Candidate := Prim_Op;
35ae2ed8 5920
fe45e59e 5921 Set_Parent (Call_Node, Parent (Node_To_Replace));
35ae2ed8 5922
fe45e59e 5923 Set_Name (Call_Node, Prim_Op_Ref);
0a36105d 5924 Success := False;
35ae2ed8 5925
fe45e59e
ES
5926 Analyze_One_Call
5927 (N => Call_Node,
5928 Nam => Prim_Op,
b67a385c 5929 Report => Report_Error,
fe45e59e
ES
5930 Success => Success,
5931 Skip_First => True);
35ae2ed8 5932
0a36105d 5933 Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
fe45e59e 5934
aab883ec 5935 else
fe45e59e 5936
aab883ec 5937 -- More than one interpretation, collect for subsequent
0a36105d
JM
5938 -- disambiguation. If this is a procedure call and there
5939 -- is another match, report ambiguity now.
5940
5941 Analyze_One_Call
5942 (N => Call_Node,
5943 Nam => Prim_Op,
5944 Report => Report_Error,
5945 Success => Success,
5946 Skip_First => True);
fe45e59e 5947
0a36105d
JM
5948 if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
5949 and then Nkind (Call_Node) /= N_Function_Call
5950 then
5951 Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
5952 Report_Ambiguity (Matching_Op);
5953 Report_Ambiguity (Prim_Op);
5954 return True;
5955 end if;
4c46b835
AC
5956 end if;
5957 end if;
35ae2ed8 5958
fe45e59e 5959 <<Continue>>
4c46b835
AC
5960 Next_Elmt (Elmt);
5961 end loop;
35ae2ed8 5962
0a36105d
JM
5963 if Present (Matching_Op) then
5964 Set_Etype (Call_Node, Etype (Matching_Op));
fe45e59e
ES
5965 end if;
5966
0a36105d 5967 return Present (Matching_Op);
4c46b835 5968 end Try_Primitive_Operation;
35ae2ed8 5969
4c46b835 5970 -- Start of processing for Try_Object_Operation
35ae2ed8 5971
4c46b835 5972 begin
0a36105d 5973 Analyze_Expression (Obj);
ec6078e3 5974
0a36105d 5975 -- Analyze the actuals if node is known to be a subprogram call
28d6470f
JM
5976
5977 if Is_Subprg_Call and then N = Name (Parent (N)) then
5978 Actual := First (Parameter_Associations (Parent (N)));
5979 while Present (Actual) loop
725e2a15 5980 Analyze_Expression (Actual);
28d6470f
JM
5981 Next (Actual);
5982 end loop;
5983 end if;
5d09245e 5984
ec6078e3
ES
5985 -- Build a subprogram call node, using a copy of Obj as its first
5986 -- actual. This is a placeholder, to be replaced by an explicit
5987 -- dereference when needed.
4c46b835 5988
ec6078e3
ES
5989 Transform_Object_Operation
5990 (Call_Node => New_Call_Node,
0a36105d 5991 Node_To_Replace => Node_To_Replace);
4c46b835 5992
ec6078e3 5993 Set_Etype (New_Call_Node, Any_Type);
0a36105d 5994 Set_Etype (Subprog, Any_Type);
ec6078e3 5995 Set_Parent (New_Call_Node, Parent (Node_To_Replace));
4c46b835 5996
0a36105d
JM
5997 if not Is_Overloaded (Obj) then
5998 Try_One_Prefix_Interpretation (Obj_Type);
ec6078e3 5999
0a36105d
JM
6000 else
6001 declare
6002 I : Interp_Index;
6003 It : Interp;
6004 begin
6005 Get_First_Interp (Obj, I, It);
6006 while Present (It.Nam) loop
6007 Try_One_Prefix_Interpretation (It.Typ);
6008 Get_Next_Interp (I, It);
6009 end loop;
6010 end;
6011 end if;
6012
6013 if Etype (New_Call_Node) /= Any_Type then
6014 Complete_Object_Operation
6015 (Call_Node => New_Call_Node,
6016 Node_To_Replace => Node_To_Replace);
b67a385c
ES
6017 return True;
6018
6019 elsif Present (Candidate) then
6020
6021 -- The argument list is not type correct. Re-analyze with error
6022 -- reporting enabled, and use one of the possible candidates.
6023 -- In all_errors mode, re-analyze all failed interpretations.
6024
6025 if All_Errors_Mode then
6026 Report_Error := True;
6027 if Try_Primitive_Operation
6028 (Call_Node => New_Call_Node,
6029 Node_To_Replace => Node_To_Replace)
6030
6031 or else
6032 Try_Class_Wide_Operation
6033 (Call_Node => New_Call_Node,
6034 Node_To_Replace => Node_To_Replace)
6035 then
6036 null;
6037 end if;
6038
6039 else
6040 Analyze_One_Call
6041 (N => New_Call_Node,
6042 Nam => Candidate,
6043 Report => True,
6044 Success => Success,
6045 Skip_First => True);
6046 end if;
6047
6048 return True; -- No need for further errors.
6049
6050 else
6051 -- There was no candidate operation, so report it as an error
6052 -- in the caller: Analyze_Selected_Component.
6053
6054 return False;
6055 end if;
35ae2ed8
AC
6056 end Try_Object_Operation;
6057
996ae0b0 6058end Sem_Ch4;