OSDN Git Service

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