OSDN Git Service

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