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