OSDN Git Service

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