OSDN Git Service

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