OSDN Git Service

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