OSDN Git Service

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