OSDN Git Service

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