OSDN Git Service

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