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