OSDN Git Service

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