OSDN Git Service

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