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