OSDN Git Service

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