OSDN Git Service

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