OSDN Git Service

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