OSDN Git Service

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