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