OSDN Git Service

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