OSDN Git Service

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