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