OSDN Git Service

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