OSDN Git Service

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