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