OSDN Git Service

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