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