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