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, conditional expressions are allowed:
1528       --    * if they have no ELSE part, in which case the expression is
1529       --      equivalent to
1530       --        NOT Condition OR ELSE Then_Expr
1531       --    * in pre- and postconditions, where the Condition cannot have side-
1532       --      effects (in ALFA) and thus the expression is equivalent to
1533       --        (Condition AND THEN Then_Expr)
1534       --          and (NOT Condition AND THEN Then_Expr)
1535
1536       if Present (Else_Expr) and then not In_Pre_Post_Expression then
1537          Mark_Non_ALFA_Subprogram;
1538       end if;
1539
1540       if Comes_From_Source (N) then
1541          Check_Compiler_Unit (N);
1542       end if;
1543
1544       Analyze_Expression (Condition);
1545       Analyze_Expression (Then_Expr);
1546
1547       if Present (Else_Expr) then
1548          Analyze_Expression (Else_Expr);
1549       end if;
1550
1551       --  If then expression not overloaded, then that decides the type
1552
1553       if not Is_Overloaded (Then_Expr) then
1554          Set_Etype (N, Etype (Then_Expr));
1555
1556       --  Case where then expression is overloaded
1557
1558       else
1559          declare
1560             I  : Interp_Index;
1561             It : Interp;
1562
1563          begin
1564             Set_Etype (N, Any_Type);
1565
1566             --  Shouldn't the following statement be down in the ELSE of the
1567             --  following loop? ???
1568
1569             Get_First_Interp (Then_Expr, I, It);
1570
1571             --  if no Else_Expression the conditional must be boolean
1572
1573             if No (Else_Expr) then
1574                Set_Etype (N, Standard_Boolean);
1575
1576             --  Else_Expression Present. For each possible intepretation of
1577             --  the Then_Expression, add it only if the Else_Expression has
1578             --  a compatible type.
1579
1580             else
1581                while Present (It.Nam) loop
1582                   if Has_Compatible_Type (Else_Expr, It.Typ) then
1583                      Add_One_Interp (N, It.Typ, It.Typ);
1584                   end if;
1585
1586                   Get_Next_Interp (I, It);
1587                end loop;
1588             end if;
1589          end;
1590       end if;
1591    end Analyze_Conditional_Expression;
1592
1593    -------------------------
1594    -- Analyze_Equality_Op --
1595    -------------------------
1596
1597    procedure Analyze_Equality_Op (N : Node_Id) is
1598       Loc   : constant Source_Ptr := Sloc (N);
1599       L     : constant Node_Id := Left_Opnd (N);
1600       R     : constant Node_Id := Right_Opnd (N);
1601       Op_Id : Entity_Id;
1602
1603    begin
1604       Set_Etype (N, Any_Type);
1605       Candidate_Type := Empty;
1606
1607       Analyze_Expression (L);
1608       Analyze_Expression (R);
1609
1610       --  If the entity is set, the node is a generic instance with a non-local
1611       --  reference to the predefined operator or to a user-defined function.
1612       --  It can also be an inequality that is expanded into the negation of a
1613       --  call to a user-defined equality operator.
1614
1615       --  For the predefined case, the result is Boolean, regardless of the
1616       --  type of the  operands. The operands may even be limited, if they are
1617       --  generic actuals. If they are overloaded, label the left argument with
1618       --  the common type that must be present, or with the type of the formal
1619       --  of the user-defined function.
1620
1621       if Present (Entity (N)) then
1622          Op_Id := Entity (N);
1623
1624          if Ekind (Op_Id) = E_Operator then
1625             Add_One_Interp (N, Op_Id, Standard_Boolean);
1626          else
1627             Add_One_Interp (N, Op_Id, Etype (Op_Id));
1628          end if;
1629
1630          if Is_Overloaded (L) then
1631             if Ekind (Op_Id) = E_Operator then
1632                Set_Etype (L, Intersect_Types (L, R));
1633             else
1634                Set_Etype (L, Etype (First_Formal (Op_Id)));
1635             end if;
1636          end if;
1637
1638       else
1639          Op_Id := Get_Name_Entity_Id (Chars (N));
1640          while Present (Op_Id) loop
1641             if Ekind (Op_Id) = E_Operator then
1642                Find_Equality_Types (L, R, Op_Id, N);
1643             else
1644                Analyze_User_Defined_Binary_Op (N, Op_Id);
1645             end if;
1646
1647             Op_Id := Homonym (Op_Id);
1648          end loop;
1649       end if;
1650
1651       --  If there was no match, and the operator is inequality, this may
1652       --  be a case where inequality has not been made explicit, as for
1653       --  tagged types. Analyze the node as the negation of an equality
1654       --  operation. This cannot be done earlier, because before analysis
1655       --  we cannot rule out the presence of an explicit inequality.
1656
1657       if Etype (N) = Any_Type
1658         and then Nkind (N) = N_Op_Ne
1659       then
1660          Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
1661          while Present (Op_Id) loop
1662             if Ekind (Op_Id) = E_Operator then
1663                Find_Equality_Types (L, R, Op_Id, N);
1664             else
1665                Analyze_User_Defined_Binary_Op (N, Op_Id);
1666             end if;
1667
1668             Op_Id := Homonym (Op_Id);
1669          end loop;
1670
1671          if Etype (N) /= Any_Type then
1672             Op_Id := Entity (N);
1673
1674             Rewrite (N,
1675               Make_Op_Not (Loc,
1676                 Right_Opnd =>
1677                   Make_Op_Eq (Loc,
1678                     Left_Opnd  => Left_Opnd (N),
1679                     Right_Opnd => Right_Opnd (N))));
1680
1681             Set_Entity (Right_Opnd (N), Op_Id);
1682             Analyze (N);
1683          end if;
1684       end if;
1685
1686       Operator_Check (N);
1687    end Analyze_Equality_Op;
1688
1689    ----------------------------------
1690    -- Analyze_Explicit_Dereference --
1691    ----------------------------------
1692
1693    procedure Analyze_Explicit_Dereference (N : Node_Id) is
1694       Loc   : constant Source_Ptr := Sloc (N);
1695       P     : constant Node_Id := Prefix (N);
1696       T     : Entity_Id;
1697       I     : Interp_Index;
1698       It    : Interp;
1699       New_N : Node_Id;
1700
1701       function Is_Function_Type return Boolean;
1702       --  Check whether node may be interpreted as an implicit function call
1703
1704       ----------------------
1705       -- Is_Function_Type --
1706       ----------------------
1707
1708       function Is_Function_Type return Boolean is
1709          I  : Interp_Index;
1710          It : Interp;
1711
1712       begin
1713          if not Is_Overloaded (N) then
1714             return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
1715               and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
1716
1717          else
1718             Get_First_Interp (N, I, It);
1719             while Present (It.Nam) loop
1720                if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
1721                  or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
1722                then
1723                   return False;
1724                end if;
1725
1726                Get_Next_Interp (I, It);
1727             end loop;
1728
1729             return True;
1730          end if;
1731       end Is_Function_Type;
1732
1733    --  Start of processing for Analyze_Explicit_Dereference
1734
1735    begin
1736       Mark_Non_ALFA_Subprogram;
1737       Check_SPARK_Restriction ("explicit dereference is not allowed", N);
1738
1739       Analyze (P);
1740       Set_Etype (N, Any_Type);
1741
1742       --  Test for remote access to subprogram type, and if so return
1743       --  after rewriting the original tree.
1744
1745       if Remote_AST_E_Dereference (P) then
1746          return;
1747       end if;
1748
1749       --  Normal processing for other than remote access to subprogram type
1750
1751       if not Is_Overloaded (P) then
1752          if Is_Access_Type (Etype (P)) then
1753
1754             --  Set the Etype. We need to go through Is_For_Access_Subtypes to
1755             --  avoid other problems caused by the Private_Subtype and it is
1756             --  safe to go to the Base_Type because this is the same as
1757             --  converting the access value to its Base_Type.
1758
1759             declare
1760                DT : Entity_Id := Designated_Type (Etype (P));
1761
1762             begin
1763                if Ekind (DT) = E_Private_Subtype
1764                  and then Is_For_Access_Subtype (DT)
1765                then
1766                   DT := Base_Type (DT);
1767                end if;
1768
1769                --  An explicit dereference is a legal occurrence of an
1770                --  incomplete type imported through a limited_with clause,
1771                --  if the full view is visible.
1772
1773                if From_With_Type (DT)
1774                  and then not From_With_Type (Scope (DT))
1775                  and then
1776                    (Is_Immediately_Visible (Scope (DT))
1777                      or else
1778                        (Is_Child_Unit (Scope (DT))
1779                           and then Is_Visible_Child_Unit (Scope (DT))))
1780                then
1781                   Set_Etype (N, Available_View (DT));
1782
1783                else
1784                   Set_Etype (N, DT);
1785                end if;
1786             end;
1787
1788          elsif Etype (P) /= Any_Type then
1789             Error_Msg_N ("prefix of dereference must be an access type", N);
1790             return;
1791          end if;
1792
1793       else
1794          Get_First_Interp (P, I, It);
1795          while Present (It.Nam) loop
1796             T := It.Typ;
1797
1798             if Is_Access_Type (T) then
1799                Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
1800             end if;
1801
1802             Get_Next_Interp (I, It);
1803          end loop;
1804
1805          --  Error if no interpretation of the prefix has an access type
1806
1807          if Etype (N) = Any_Type then
1808             Error_Msg_N
1809               ("access type required in prefix of explicit dereference", P);
1810             Set_Etype (N, Any_Type);
1811             return;
1812          end if;
1813       end if;
1814
1815       if Is_Function_Type
1816         and then Nkind (Parent (N)) /= N_Indexed_Component
1817
1818         and then (Nkind (Parent (N)) /= N_Function_Call
1819                    or else N /= Name (Parent (N)))
1820
1821         and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
1822                    or else N /= Name (Parent (N)))
1823
1824         and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1825         and then (Nkind (Parent (N)) /= N_Attribute_Reference
1826                     or else
1827                       (Attribute_Name (Parent (N)) /= Name_Address
1828                         and then
1829                        Attribute_Name (Parent (N)) /= Name_Access))
1830       then
1831          --  Name is a function call with no actuals, in a context that
1832          --  requires deproceduring (including as an actual in an enclosing
1833          --  function or procedure call). There are some pathological cases
1834          --  where the prefix might include functions that return access to
1835          --  subprograms and others that return a regular type. Disambiguation
1836          --  of those has to take place in Resolve.
1837
1838          New_N :=
1839            Make_Function_Call (Loc,
1840            Name => Make_Explicit_Dereference (Loc, P),
1841            Parameter_Associations => New_List);
1842
1843          --  If the prefix is overloaded, remove operations that have formals,
1844          --  we know that this is a parameterless call.
1845
1846          if Is_Overloaded (P) then
1847             Get_First_Interp (P, I, It);
1848             while Present (It.Nam) loop
1849                T := It.Typ;
1850
1851                if No (First_Formal (Base_Type (Designated_Type (T)))) then
1852                   Set_Etype (P, T);
1853                else
1854                   Remove_Interp (I);
1855                end if;
1856
1857                Get_Next_Interp (I, It);
1858             end loop;
1859          end if;
1860
1861          Rewrite (N, New_N);
1862          Analyze (N);
1863
1864       elsif not Is_Function_Type
1865         and then Is_Overloaded (N)
1866       then
1867          --  The prefix may include access to subprograms and other access
1868          --  types. If the context selects the interpretation that is a
1869          --  function call (not a procedure call) we cannot rewrite the node
1870          --  yet, but we include the result of the call interpretation.
1871
1872          Get_First_Interp (N, I, It);
1873          while Present (It.Nam) loop
1874             if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
1875                and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
1876                and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
1877             then
1878                Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
1879             end if;
1880
1881             Get_Next_Interp (I, It);
1882          end loop;
1883       end if;
1884
1885       --  A value of remote access-to-class-wide must not be dereferenced
1886       --  (RM E.2.2(16)).
1887
1888       Validate_Remote_Access_To_Class_Wide_Type (N);
1889    end Analyze_Explicit_Dereference;
1890
1891    ------------------------
1892    -- Analyze_Expression --
1893    ------------------------
1894
1895    procedure Analyze_Expression (N : Node_Id) is
1896    begin
1897       Analyze (N);
1898       Check_Parameterless_Call (N);
1899    end Analyze_Expression;
1900
1901    -------------------------------------
1902    -- Analyze_Expression_With_Actions --
1903    -------------------------------------
1904
1905    procedure Analyze_Expression_With_Actions (N : Node_Id) is
1906       A : Node_Id;
1907
1908    begin
1909       A := First (Actions (N));
1910       loop
1911          Analyze (A);
1912          Next (A);
1913          exit when No (A);
1914       end loop;
1915
1916       Analyze_Expression (Expression (N));
1917       Set_Etype (N, Etype (Expression (N)));
1918    end Analyze_Expression_With_Actions;
1919
1920    ------------------------------------
1921    -- Analyze_Indexed_Component_Form --
1922    ------------------------------------
1923
1924    procedure Analyze_Indexed_Component_Form (N : Node_Id) is
1925       P     : constant Node_Id := Prefix (N);
1926       Exprs : constant List_Id := Expressions (N);
1927       Exp   : Node_Id;
1928       P_T   : Entity_Id;
1929       E     : Node_Id;
1930       U_N   : Entity_Id;
1931
1932       procedure Process_Function_Call;
1933       --  Prefix in indexed component form is an overloadable entity,
1934       --  so the node is a function call. Reformat it as such.
1935
1936       procedure Process_Indexed_Component;
1937       --  Prefix in indexed component form is actually an indexed component.
1938       --  This routine processes it, knowing that the prefix is already
1939       --  resolved.
1940
1941       procedure Process_Indexed_Component_Or_Slice;
1942       --  An indexed component with a single index may designate a slice if
1943       --  the index is a subtype mark. This routine disambiguates these two
1944       --  cases by resolving the prefix to see if it is a subtype mark.
1945
1946       procedure Process_Overloaded_Indexed_Component;
1947       --  If the prefix of an indexed component is overloaded, the proper
1948       --  interpretation is selected by the index types and the context.
1949
1950       ---------------------------
1951       -- Process_Function_Call --
1952       ---------------------------
1953
1954       procedure Process_Function_Call is
1955          Actual : Node_Id;
1956
1957       begin
1958          Change_Node (N, N_Function_Call);
1959          Set_Name (N, P);
1960          Set_Parameter_Associations (N, Exprs);
1961
1962          --  Analyze actuals prior to analyzing the call itself
1963
1964          Actual := First (Parameter_Associations (N));
1965          while Present (Actual) loop
1966             Analyze (Actual);
1967             Check_Parameterless_Call (Actual);
1968
1969             --  Move to next actual. Note that we use Next, not Next_Actual
1970             --  here. The reason for this is a bit subtle. If a function call
1971             --  includes named associations, the parser recognizes the node as
1972             --  a call, and it is analyzed as such. If all associations are
1973             --  positional, the parser builds an indexed_component node, and
1974             --  it is only after analysis of the prefix that the construct
1975             --  is recognized as a call, in which case Process_Function_Call
1976             --  rewrites the node and analyzes the actuals. If the list of
1977             --  actuals is malformed, the parser may leave the node as an
1978             --  indexed component (despite the presence of named associations).
1979             --  The iterator Next_Actual is equivalent to Next if the list is
1980             --  positional, but follows the normalized chain of actuals when
1981             --  named associations are present. In this case normalization has
1982             --  not taken place, and actuals remain unanalyzed, which leads to
1983             --  subsequent crashes or loops if there is an attempt to continue
1984             --  analysis of the program.
1985
1986             Next (Actual);
1987          end loop;
1988
1989          Analyze_Call (N);
1990       end Process_Function_Call;
1991
1992       -------------------------------
1993       -- Process_Indexed_Component --
1994       -------------------------------
1995
1996       procedure Process_Indexed_Component is
1997          Exp        : Node_Id;
1998          Array_Type : Entity_Id;
1999          Index      : Node_Id;
2000          Pent       : Entity_Id := Empty;
2001
2002       begin
2003          Exp := First (Exprs);
2004
2005          if Is_Overloaded (P) then
2006             Process_Overloaded_Indexed_Component;
2007
2008          else
2009             Array_Type := Etype (P);
2010
2011             if Is_Entity_Name (P) then
2012                Pent := Entity (P);
2013             elsif Nkind (P) = N_Selected_Component
2014               and then Is_Entity_Name (Selector_Name (P))
2015             then
2016                Pent := Entity (Selector_Name (P));
2017             end if;
2018
2019             --  Prefix must be appropriate for an array type, taking into
2020             --  account a possible implicit dereference.
2021
2022             if Is_Access_Type (Array_Type) then
2023                Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2024                Array_Type := Process_Implicit_Dereference_Prefix (Pent, P);
2025             end if;
2026
2027             if Is_Array_Type (Array_Type) then
2028                null;
2029
2030             elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
2031                Analyze (Exp);
2032                Set_Etype (N, Any_Type);
2033
2034                if not Has_Compatible_Type
2035                  (Exp, Entry_Index_Type (Pent))
2036                then
2037                   Error_Msg_N ("invalid index type in entry name", N);
2038
2039                elsif Present (Next (Exp)) then
2040                   Error_Msg_N ("too many subscripts in entry reference", N);
2041
2042                else
2043                   Set_Etype (N,  Etype (P));
2044                end if;
2045
2046                return;
2047
2048             elsif Is_Record_Type (Array_Type)
2049               and then Remote_AST_I_Dereference (P)
2050             then
2051                return;
2052
2053             elsif Array_Type = Any_Type then
2054                Set_Etype (N, Any_Type);
2055
2056                --  In most cases the analysis of the prefix will have emitted
2057                --  an error already, but if the prefix may be interpreted as a
2058                --  call in prefixed notation, the report is left to the caller.
2059                --  To prevent cascaded errors, report only if no previous ones.
2060
2061                if Serious_Errors_Detected = 0 then
2062                   Error_Msg_N ("invalid prefix in indexed component", P);
2063
2064                   if Nkind (P) = N_Expanded_Name then
2065                      Error_Msg_NE ("\& is not visible", P, Selector_Name (P));
2066                   end if;
2067                end if;
2068
2069                return;
2070
2071             --  Here we definitely have a bad indexing
2072
2073             else
2074                if Nkind (Parent (N)) = N_Requeue_Statement
2075                  and then Present (Pent) and then Ekind (Pent) = E_Entry
2076                then
2077                   Error_Msg_N
2078                     ("REQUEUE does not permit parameters", First (Exprs));
2079
2080                elsif Is_Entity_Name (P)
2081                  and then Etype (P) = Standard_Void_Type
2082                then
2083                   Error_Msg_NE ("incorrect use of&", P, Entity (P));
2084
2085                else
2086                   Error_Msg_N ("array type required in indexed component", P);
2087                end if;
2088
2089                Set_Etype (N, Any_Type);
2090                return;
2091             end if;
2092
2093             Index := First_Index (Array_Type);
2094             while Present (Index) and then Present (Exp) loop
2095                if not Has_Compatible_Type (Exp, Etype (Index)) then
2096                   Wrong_Type (Exp, Etype (Index));
2097                   Set_Etype (N, Any_Type);
2098                   return;
2099                end if;
2100
2101                Next_Index (Index);
2102                Next (Exp);
2103             end loop;
2104
2105             Set_Etype (N, Component_Type (Array_Type));
2106
2107             if Present (Index) then
2108                Error_Msg_N
2109                  ("too few subscripts in array reference", First (Exprs));
2110
2111             elsif Present (Exp) then
2112                Error_Msg_N ("too many subscripts in array reference", Exp);
2113             end if;
2114          end if;
2115       end Process_Indexed_Component;
2116
2117       ----------------------------------------
2118       -- Process_Indexed_Component_Or_Slice --
2119       ----------------------------------------
2120
2121       procedure Process_Indexed_Component_Or_Slice is
2122       begin
2123          Exp := First (Exprs);
2124          while Present (Exp) loop
2125             Analyze_Expression (Exp);
2126             Next (Exp);
2127          end loop;
2128
2129          Exp := First (Exprs);
2130
2131          --  If one index is present, and it is a subtype name, then the
2132          --  node denotes a slice (note that the case of an explicit range
2133          --  for a slice was already built as an N_Slice node in the first
2134          --  place, so that case is not handled here).
2135
2136          --  We use a replace rather than a rewrite here because this is one
2137          --  of the cases in which the tree built by the parser is plain wrong.
2138
2139          if No (Next (Exp))
2140            and then Is_Entity_Name (Exp)
2141            and then Is_Type (Entity (Exp))
2142          then
2143             Replace (N,
2144                Make_Slice (Sloc (N),
2145                  Prefix => P,
2146                  Discrete_Range => New_Copy (Exp)));
2147             Analyze (N);
2148
2149          --  Otherwise (more than one index present, or single index is not
2150          --  a subtype name), then we have the indexed component case.
2151
2152          else
2153             Process_Indexed_Component;
2154          end if;
2155       end Process_Indexed_Component_Or_Slice;
2156
2157       ------------------------------------------
2158       -- Process_Overloaded_Indexed_Component --
2159       ------------------------------------------
2160
2161       procedure Process_Overloaded_Indexed_Component is
2162          Exp   : Node_Id;
2163          I     : Interp_Index;
2164          It    : Interp;
2165          Typ   : Entity_Id;
2166          Index : Node_Id;
2167          Found : Boolean;
2168
2169       begin
2170          Set_Etype (N, Any_Type);
2171
2172          Get_First_Interp (P, I, It);
2173          while Present (It.Nam) loop
2174             Typ := It.Typ;
2175
2176             if Is_Access_Type (Typ) then
2177                Typ := Designated_Type (Typ);
2178                Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2179             end if;
2180
2181             if Is_Array_Type (Typ) then
2182
2183                --  Got a candidate: verify that index types are compatible
2184
2185                Index := First_Index (Typ);
2186                Found := True;
2187                Exp := First (Exprs);
2188                while Present (Index) and then Present (Exp) loop
2189                   if Has_Compatible_Type (Exp, Etype (Index)) then
2190                      null;
2191                   else
2192                      Found := False;
2193                      Remove_Interp (I);
2194                      exit;
2195                   end if;
2196
2197                   Next_Index (Index);
2198                   Next (Exp);
2199                end loop;
2200
2201                if Found and then No (Index) and then No (Exp) then
2202                   Add_One_Interp (N,
2203                      Etype (Component_Type (Typ)),
2204                      Etype (Component_Type (Typ)));
2205                end if;
2206             end if;
2207
2208             Get_Next_Interp (I, It);
2209          end loop;
2210
2211          if Etype (N) = Any_Type then
2212             Error_Msg_N ("no legal interpretation for indexed component", N);
2213             Set_Is_Overloaded (N, False);
2214          end if;
2215
2216          End_Interp_List;
2217       end Process_Overloaded_Indexed_Component;
2218
2219    --  Start of processing for Analyze_Indexed_Component_Form
2220
2221    begin
2222       --  Get name of array, function or type
2223
2224       Analyze (P);
2225
2226       if Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement) then
2227
2228          --  If P is an explicit dereference whose prefix is of a
2229          --  remote access-to-subprogram type, then N has already
2230          --  been rewritten as a subprogram call and analyzed.
2231
2232          return;
2233       end if;
2234
2235       pragma Assert (Nkind (N) = N_Indexed_Component);
2236
2237       P_T := Base_Type (Etype (P));
2238
2239       if Is_Entity_Name (P) and then Present (Entity (P)) then
2240          U_N := Entity (P);
2241
2242          if Is_Type (U_N) then
2243
2244             --  Reformat node as a type conversion
2245
2246             E := Remove_Head (Exprs);
2247
2248             if Present (First (Exprs)) then
2249                Error_Msg_N
2250                 ("argument of type conversion must be single expression", N);
2251             end if;
2252
2253             Change_Node (N, N_Type_Conversion);
2254             Set_Subtype_Mark (N, P);
2255             Set_Etype (N, U_N);
2256             Set_Expression (N, E);
2257
2258             --  After changing the node, call for the specific Analysis
2259             --  routine directly, to avoid a double call to the expander.
2260
2261             Analyze_Type_Conversion (N);
2262             return;
2263          end if;
2264
2265          if Is_Overloadable (U_N) then
2266             Process_Function_Call;
2267
2268          elsif Ekind (Etype (P)) = E_Subprogram_Type
2269            or else (Is_Access_Type (Etype (P))
2270                       and then
2271                         Ekind (Designated_Type (Etype (P))) =
2272                                                    E_Subprogram_Type)
2273          then
2274             --  Call to access_to-subprogram with possible implicit dereference
2275
2276             Process_Function_Call;
2277
2278          elsif Is_Generic_Subprogram (U_N) then
2279
2280             --  A common beginner's (or C++ templates fan) error
2281
2282             Error_Msg_N ("generic subprogram cannot be called", N);
2283             Set_Etype (N, Any_Type);
2284             return;
2285
2286          else
2287             Process_Indexed_Component_Or_Slice;
2288          end if;
2289
2290       --  If not an entity name, prefix is an expression that may denote
2291       --  an array or an access-to-subprogram.
2292
2293       else
2294          if Ekind (P_T) = E_Subprogram_Type
2295            or else (Is_Access_Type (P_T)
2296                      and then
2297                        Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
2298          then
2299             Process_Function_Call;
2300
2301          elsif Nkind (P) = N_Selected_Component
2302            and then Is_Overloadable (Entity (Selector_Name (P)))
2303          then
2304             Process_Function_Call;
2305
2306          else
2307             --  Indexed component, slice, or a call to a member of a family
2308             --  entry, which will be converted to an entry call later.
2309
2310             Process_Indexed_Component_Or_Slice;
2311          end if;
2312       end if;
2313    end Analyze_Indexed_Component_Form;
2314
2315    ------------------------
2316    -- Analyze_Logical_Op --
2317    ------------------------
2318
2319    procedure Analyze_Logical_Op (N : Node_Id) is
2320       L     : constant Node_Id := Left_Opnd (N);
2321       R     : constant Node_Id := Right_Opnd (N);
2322       Op_Id : Entity_Id := Entity (N);
2323
2324    begin
2325       Set_Etype (N, Any_Type);
2326       Candidate_Type := Empty;
2327
2328       Analyze_Expression (L);
2329       Analyze_Expression (R);
2330
2331       if Present (Op_Id) then
2332
2333          if Ekind (Op_Id) = E_Operator then
2334             Find_Boolean_Types (L, R, Op_Id, N);
2335          else
2336             Add_One_Interp (N, Op_Id, Etype (Op_Id));
2337          end if;
2338
2339       else
2340          Op_Id := Get_Name_Entity_Id (Chars (N));
2341          while Present (Op_Id) loop
2342             if Ekind (Op_Id) = E_Operator then
2343                Find_Boolean_Types (L, R, Op_Id, N);
2344             else
2345                Analyze_User_Defined_Binary_Op (N, Op_Id);
2346             end if;
2347
2348             Op_Id := Homonym (Op_Id);
2349          end loop;
2350       end if;
2351
2352       Operator_Check (N);
2353    end Analyze_Logical_Op;
2354
2355    ---------------------------
2356    -- Analyze_Membership_Op --
2357    ---------------------------
2358
2359    procedure Analyze_Membership_Op (N : Node_Id) is
2360       Loc   : constant Source_Ptr := Sloc (N);
2361       L     : constant Node_Id    := Left_Opnd (N);
2362       R     : constant Node_Id    := Right_Opnd (N);
2363
2364       Index : Interp_Index;
2365       It    : Interp;
2366       Found : Boolean := False;
2367       I_F   : Interp_Index;
2368       T_F   : Entity_Id;
2369
2370       procedure Try_One_Interp (T1 : Entity_Id);
2371       --  Routine to try one proposed interpretation. Note that the context
2372       --  of the operation plays no role in resolving the arguments, so that
2373       --  if there is more than one interpretation of the operands that is
2374       --  compatible with a membership test, the operation is ambiguous.
2375
2376       --------------------
2377       -- Try_One_Interp --
2378       --------------------
2379
2380       procedure Try_One_Interp (T1 : Entity_Id) is
2381       begin
2382          if Has_Compatible_Type (R, T1) then
2383             if Found
2384               and then Base_Type (T1) /= Base_Type (T_F)
2385             then
2386                It := Disambiguate (L, I_F, Index, Any_Type);
2387
2388                if It = No_Interp then
2389                   Ambiguous_Operands (N);
2390                   Set_Etype (L, Any_Type);
2391                   return;
2392
2393                else
2394                   T_F := It.Typ;
2395                end if;
2396
2397             else
2398                Found := True;
2399                T_F   := T1;
2400                I_F   := Index;
2401             end if;
2402
2403             Set_Etype (L, T_F);
2404          end if;
2405       end Try_One_Interp;
2406
2407       procedure Analyze_Set_Membership;
2408       --  If a set of alternatives is present, analyze each and find the
2409       --  common type to which they must all resolve.
2410
2411       ----------------------------
2412       -- Analyze_Set_Membership --
2413       ----------------------------
2414
2415       procedure Analyze_Set_Membership is
2416          Alt               : Node_Id;
2417          Index             : Interp_Index;
2418          It                : Interp;
2419          Candidate_Interps : Node_Id;
2420          Common_Type       : Entity_Id := Empty;
2421
2422       begin
2423          Analyze (L);
2424          Candidate_Interps := L;
2425
2426          if not Is_Overloaded (L) then
2427             Common_Type := Etype (L);
2428
2429             Alt := First (Alternatives (N));
2430             while Present (Alt) loop
2431                Analyze (Alt);
2432
2433                if not Has_Compatible_Type (Alt, Common_Type) then
2434                   Wrong_Type (Alt, Common_Type);
2435                end if;
2436
2437                Next (Alt);
2438             end loop;
2439
2440          else
2441             Alt := First (Alternatives (N));
2442             while Present (Alt) loop
2443                Analyze (Alt);
2444                if not Is_Overloaded (Alt) then
2445                   Common_Type := Etype (Alt);
2446
2447                else
2448                   Get_First_Interp (Alt, Index, It);
2449                   while Present (It.Typ) loop
2450                      if not
2451                        Has_Compatible_Type (Candidate_Interps, It.Typ)
2452                      then
2453                         Remove_Interp (Index);
2454                      end if;
2455
2456                      Get_Next_Interp (Index, It);
2457                   end loop;
2458
2459                   Get_First_Interp (Alt, Index, It);
2460
2461                   if No (It.Typ) then
2462                      Error_Msg_N ("alternative has no legal type", Alt);
2463                      return;
2464                   end if;
2465
2466                   --  If alternative is not overloaded, we have a unique type
2467                   --  for all of them.
2468
2469                   Set_Etype (Alt, It.Typ);
2470                   Get_Next_Interp (Index, It);
2471
2472                   if No (It.Typ) then
2473                      Set_Is_Overloaded (Alt, False);
2474                      Common_Type := Etype (Alt);
2475                   end if;
2476
2477                   Candidate_Interps := Alt;
2478                end if;
2479
2480                Next (Alt);
2481             end loop;
2482          end if;
2483
2484          Set_Etype (N, Standard_Boolean);
2485
2486          if Present (Common_Type) then
2487             Set_Etype (L, Common_Type);
2488             Set_Is_Overloaded (L, False);
2489
2490          else
2491             Error_Msg_N ("cannot resolve membership operation", N);
2492          end if;
2493       end Analyze_Set_Membership;
2494
2495    --  Start of processing for Analyze_Membership_Op
2496
2497    begin
2498       Analyze_Expression (L);
2499
2500       if No (R)
2501         and then Ada_Version >= Ada_2012
2502       then
2503          Analyze_Set_Membership;
2504          return;
2505       end if;
2506
2507       if Nkind (R) = N_Range
2508         or else (Nkind (R) = N_Attribute_Reference
2509                   and then Attribute_Name (R) = Name_Range)
2510       then
2511          Analyze (R);
2512
2513          if not Is_Overloaded (L) then
2514             Try_One_Interp (Etype (L));
2515
2516          else
2517             Get_First_Interp (L, Index, It);
2518             while Present (It.Typ) loop
2519                Try_One_Interp (It.Typ);
2520                Get_Next_Interp (Index, It);
2521             end loop;
2522          end if;
2523
2524       --  If not a range, it can be a subtype mark, or else it is a degenerate
2525       --  membership test with a singleton value, i.e. a test for equality,
2526       --  if the types are compatible.
2527
2528       else
2529          Analyze (R);
2530          if Is_Entity_Name (R)
2531            and then Is_Type (Entity (R))
2532          then
2533             Find_Type (R);
2534             Check_Fully_Declared (Entity (R), R);
2535
2536          elsif Ada_Version >= Ada_2012
2537            and then Has_Compatible_Type (R, Etype (L))
2538          then
2539             if Nkind (N) = N_In then
2540                Rewrite (N,
2541                  Make_Op_Eq (Loc,
2542                    Left_Opnd  => L,
2543                    Right_Opnd => R));
2544             else
2545                Rewrite (N,
2546                  Make_Op_Ne (Loc,
2547                    Left_Opnd  => L,
2548                    Right_Opnd => R));
2549             end if;
2550
2551             Analyze (N);
2552             return;
2553
2554          else
2555             --  In all versions of the language, if we reach this point there
2556             --  is a previous error that will be diagnosed below.
2557
2558             Find_Type (R);
2559          end if;
2560       end if;
2561
2562       --  Compatibility between expression and subtype mark or range is
2563       --  checked during resolution. The result of the operation is Boolean
2564       --  in any case.
2565
2566       Set_Etype (N, Standard_Boolean);
2567
2568       if Comes_From_Source (N)
2569         and then Present (Right_Opnd (N))
2570         and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
2571       then
2572          Error_Msg_N ("membership test not applicable to cpp-class types", N);
2573       end if;
2574    end Analyze_Membership_Op;
2575
2576    ----------------------
2577    -- Analyze_Negation --
2578    ----------------------
2579
2580    procedure Analyze_Negation (N : Node_Id) is
2581       R     : constant Node_Id := Right_Opnd (N);
2582       Op_Id : Entity_Id := Entity (N);
2583
2584    begin
2585       Set_Etype (N, Any_Type);
2586       Candidate_Type := Empty;
2587
2588       Analyze_Expression (R);
2589
2590       if Present (Op_Id) then
2591          if Ekind (Op_Id) = E_Operator then
2592             Find_Negation_Types (R, Op_Id, N);
2593          else
2594             Add_One_Interp (N, Op_Id, Etype (Op_Id));
2595          end if;
2596
2597       else
2598          Op_Id := Get_Name_Entity_Id (Chars (N));
2599          while Present (Op_Id) loop
2600             if Ekind (Op_Id) = E_Operator then
2601                Find_Negation_Types (R, Op_Id, N);
2602             else
2603                Analyze_User_Defined_Unary_Op (N, Op_Id);
2604             end if;
2605
2606             Op_Id := Homonym (Op_Id);
2607          end loop;
2608       end if;
2609
2610       Operator_Check (N);
2611    end Analyze_Negation;
2612
2613    ------------------
2614    -- Analyze_Null --
2615    ------------------
2616
2617    procedure Analyze_Null (N : Node_Id) is
2618    begin
2619       Mark_Non_ALFA_Subprogram;
2620       Check_SPARK_Restriction ("null is not allowed", N);
2621
2622       Set_Etype (N, Any_Access);
2623    end Analyze_Null;
2624
2625    ----------------------
2626    -- Analyze_One_Call --
2627    ----------------------
2628
2629    procedure Analyze_One_Call
2630       (N          : Node_Id;
2631        Nam        : Entity_Id;
2632        Report     : Boolean;
2633        Success    : out Boolean;
2634        Skip_First : Boolean := False)
2635    is
2636       Actuals : constant List_Id   := Parameter_Associations (N);
2637       Prev_T  : constant Entity_Id := Etype (N);
2638
2639       Must_Skip  : constant Boolean := Skip_First
2640                      or else Nkind (Original_Node (N)) = N_Selected_Component
2641                      or else
2642                        (Nkind (Original_Node (N)) = N_Indexed_Component
2643                           and then Nkind (Prefix (Original_Node (N)))
2644                             = N_Selected_Component);
2645       --  The first formal must be omitted from the match when trying to find
2646       --  a primitive operation that is a possible interpretation, and also
2647       --  after the call has been rewritten, because the corresponding actual
2648       --  is already known to be compatible, and because this may be an
2649       --  indexing of a call with default parameters.
2650
2651       Formal      : Entity_Id;
2652       Actual      : Node_Id;
2653       Is_Indexed  : Boolean := False;
2654       Is_Indirect : Boolean := False;
2655       Subp_Type   : constant Entity_Id := Etype (Nam);
2656       Norm_OK     : Boolean;
2657
2658       function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
2659       --  There may be a user-defined operator that hides the current
2660       --  interpretation. We must check for this independently of the
2661       --  analysis of the call with the user-defined operation, because
2662       --  the parameter names may be wrong and yet the hiding takes place.
2663       --  This fixes a problem with ACATS test B34014O.
2664       --
2665       --  When the type Address is a visible integer type, and the DEC
2666       --  system extension is visible, the predefined operator may be
2667       --  hidden as well, by one of the address operations in auxdec.
2668       --  Finally, The abstract operations on address do not hide the
2669       --  predefined operator (this is the purpose of making them abstract).
2670
2671       procedure Indicate_Name_And_Type;
2672       --  If candidate interpretation matches, indicate name and type of
2673       --  result on call node.
2674
2675       ----------------------------
2676       -- Indicate_Name_And_Type --
2677       ----------------------------
2678
2679       procedure Indicate_Name_And_Type is
2680       begin
2681          Add_One_Interp (N, Nam, Etype (Nam));
2682          Success := True;
2683
2684          --  If the prefix of the call is a name, indicate the entity
2685          --  being called. If it is not a name,  it is an expression that
2686          --  denotes an access to subprogram or else an entry or family. In
2687          --  the latter case, the name is a selected component, and the entity
2688          --  being called is noted on the selector.
2689
2690          if not Is_Type (Nam) then
2691             if Is_Entity_Name (Name (N)) then
2692                Set_Entity (Name (N), Nam);
2693
2694             elsif Nkind (Name (N)) = N_Selected_Component then
2695                Set_Entity (Selector_Name (Name (N)),  Nam);
2696             end if;
2697          end if;
2698
2699          if Debug_Flag_E and not Report then
2700             Write_Str (" Overloaded call ");
2701             Write_Int (Int (N));
2702             Write_Str (" compatible with ");
2703             Write_Int (Int (Nam));
2704             Write_Eol;
2705          end if;
2706       end Indicate_Name_And_Type;
2707
2708       ------------------------
2709       -- Operator_Hidden_By --
2710       ------------------------
2711
2712       function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
2713          Act1  : constant Node_Id   := First_Actual (N);
2714          Act2  : constant Node_Id   := Next_Actual (Act1);
2715          Form1 : constant Entity_Id := First_Formal (Fun);
2716          Form2 : constant Entity_Id := Next_Formal (Form1);
2717
2718       begin
2719          if Ekind (Fun) /= E_Function
2720            or else Is_Abstract_Subprogram (Fun)
2721          then
2722             return False;
2723
2724          elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
2725             return False;
2726
2727          elsif Present (Form2) then
2728             if
2729               No (Act2) or else not Has_Compatible_Type (Act2, Etype (Form2))
2730             then
2731                return False;
2732             end if;
2733
2734          elsif Present (Act2) then
2735             return False;
2736          end if;
2737
2738          --  Now we know that the arity of the operator matches the function,
2739          --  and the function call is a valid interpretation. The function
2740          --  hides the operator if it has the right signature, or if one of
2741          --  its operands is a non-abstract operation on Address when this is
2742          --  a visible integer type.
2743
2744          return Hides_Op (Fun, Nam)
2745            or else Is_Descendent_Of_Address (Etype (Form1))
2746            or else
2747              (Present (Form2)
2748                and then Is_Descendent_Of_Address (Etype (Form2)));
2749       end Operator_Hidden_By;
2750
2751    --  Start of processing for Analyze_One_Call
2752
2753    begin
2754       Success := False;
2755
2756       --  If the subprogram has no formals or if all the formals have defaults,
2757       --  and the return type is an array type, the node may denote an indexing
2758       --  of the result of a parameterless call. In Ada 2005, the subprogram
2759       --  may have one non-defaulted formal, and the call may have been written
2760       --  in prefix notation, so that the rebuilt parameter list has more than
2761       --  one actual.
2762
2763       if not Is_Overloadable (Nam)
2764         and then Ekind (Nam) /= E_Subprogram_Type
2765         and then Ekind (Nam) /= E_Entry_Family
2766       then
2767          return;
2768       end if;
2769
2770       --  An indexing requires at least one actual
2771
2772       if not Is_Empty_List (Actuals)
2773         and then
2774           (Needs_No_Actuals (Nam)
2775             or else
2776               (Needs_One_Actual (Nam)
2777                  and then Present (Next_Actual (First (Actuals)))))
2778       then
2779          if Is_Array_Type (Subp_Type) then
2780             Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
2781
2782          elsif Is_Access_Type (Subp_Type)
2783            and then Is_Array_Type (Designated_Type (Subp_Type))
2784          then
2785             Is_Indexed :=
2786               Try_Indexed_Call
2787                 (N, Nam, Designated_Type (Subp_Type), Must_Skip);
2788
2789          --  The prefix can also be a parameterless function that returns an
2790          --  access to subprogram, in which case this is an indirect call.
2791          --  If this succeeds, an explicit dereference is added later on,
2792          --  in Analyze_Call or Resolve_Call.
2793
2794          elsif Is_Access_Type (Subp_Type)
2795            and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
2796          then
2797             Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
2798          end if;
2799
2800       end if;
2801
2802       --  If the call has been transformed into a slice, it is of the form
2803       --  F (Subtype) where F is parameterless. The node has been rewritten in
2804       --  Try_Indexed_Call and there is nothing else to do.
2805
2806       if Is_Indexed
2807         and then  Nkind (N) = N_Slice
2808       then
2809          return;
2810       end if;
2811
2812       Normalize_Actuals
2813         (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
2814
2815       if not Norm_OK then
2816
2817          --  If an indirect call is a possible interpretation, indicate
2818          --  success to the caller.
2819
2820          if Is_Indirect then
2821             Success := True;
2822             return;
2823
2824          --  Mismatch in number or names of parameters
2825
2826          elsif Debug_Flag_E then
2827             Write_Str (" normalization fails in call ");
2828             Write_Int (Int (N));
2829             Write_Str (" with subprogram ");
2830             Write_Int (Int (Nam));
2831             Write_Eol;
2832          end if;
2833
2834       --  If the context expects a function call, discard any interpretation
2835       --  that is a procedure. If the node is not overloaded, leave as is for
2836       --  better error reporting when type mismatch is found.
2837
2838       elsif Nkind (N) = N_Function_Call
2839         and then Is_Overloaded (Name (N))
2840         and then Ekind (Nam) = E_Procedure
2841       then
2842          return;
2843
2844       --  Ditto for function calls in a procedure context
2845
2846       elsif Nkind (N) = N_Procedure_Call_Statement
2847          and then Is_Overloaded (Name (N))
2848          and then Etype (Nam) /= Standard_Void_Type
2849       then
2850          return;
2851
2852       elsif No (Actuals) then
2853
2854          --  If Normalize succeeds, then there are default parameters for
2855          --  all formals.
2856
2857          Indicate_Name_And_Type;
2858
2859       elsif Ekind (Nam) = E_Operator then
2860          if Nkind (N) = N_Procedure_Call_Statement then
2861             return;
2862          end if;
2863
2864          --  This can occur when the prefix of the call is an operator
2865          --  name or an expanded name whose selector is an operator name.
2866
2867          Analyze_Operator_Call (N, Nam);
2868
2869          if Etype (N) /= Prev_T then
2870
2871             --  Check that operator is not hidden by a function interpretation
2872
2873             if Is_Overloaded (Name (N)) then
2874                declare
2875                   I  : Interp_Index;
2876                   It : Interp;
2877
2878                begin
2879                   Get_First_Interp (Name (N), I, It);
2880                   while Present (It.Nam) loop
2881                      if Operator_Hidden_By (It.Nam) then
2882                         Set_Etype (N, Prev_T);
2883                         return;
2884                      end if;
2885
2886                      Get_Next_Interp (I, It);
2887                   end loop;
2888                end;
2889             end if;
2890
2891             --  If operator matches formals, record its name on the call.
2892             --  If the operator is overloaded, Resolve will select the
2893             --  correct one from the list of interpretations. The call
2894             --  node itself carries the first candidate.
2895
2896             Set_Entity (Name (N), Nam);
2897             Success := True;
2898
2899          elsif Report and then Etype (N) = Any_Type then
2900             Error_Msg_N ("incompatible arguments for operator", N);
2901          end if;
2902
2903       else
2904          --  Normalize_Actuals has chained the named associations in the
2905          --  correct order of the formals.
2906
2907          Actual := First_Actual (N);
2908          Formal := First_Formal (Nam);
2909
2910          --  If we are analyzing a call rewritten from object notation, skip
2911          --  first actual, which may be rewritten later as an explicit
2912          --  dereference.
2913
2914          if Must_Skip then
2915             Next_Actual (Actual);
2916             Next_Formal (Formal);
2917          end if;
2918
2919          while Present (Actual) and then Present (Formal) loop
2920             if Nkind (Parent (Actual)) /= N_Parameter_Association
2921               or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
2922             then
2923                --  The actual can be compatible with the formal, but we must
2924                --  also check that the context is not an address type that is
2925                --  visibly an integer type, as is the case in VMS_64. In this
2926                --  case the use of literals is illegal, except in the body of
2927                --  descendents of system, where arithmetic operations on
2928                --  address are of course used.
2929
2930                if Has_Compatible_Type (Actual, Etype (Formal))
2931                  and then
2932                   (Etype (Actual) /= Universal_Integer
2933                     or else not Is_Descendent_Of_Address (Etype (Formal))
2934                     or else
2935                       Is_Predefined_File_Name
2936                         (Unit_File_Name (Get_Source_Unit (N))))
2937                then
2938                   Next_Actual (Actual);
2939                   Next_Formal (Formal);
2940
2941                else
2942                   if Debug_Flag_E then
2943                      Write_Str (" type checking fails in call ");
2944                      Write_Int (Int (N));
2945                      Write_Str (" with formal ");
2946                      Write_Int (Int (Formal));
2947                      Write_Str (" in subprogram ");
2948                      Write_Int (Int (Nam));
2949                      Write_Eol;
2950                   end if;
2951
2952                   if Report and not Is_Indexed and not Is_Indirect then
2953
2954                      --  Ada 2005 (AI-251): Complete the error notification
2955                      --  to help new Ada 2005 users.
2956
2957                      if Is_Class_Wide_Type (Etype (Formal))
2958                        and then Is_Interface (Etype (Etype (Formal)))
2959                        and then not Interface_Present_In_Ancestor
2960                                       (Typ   => Etype (Actual),
2961                                        Iface => Etype (Etype (Formal)))
2962                      then
2963                         Error_Msg_NE
2964                           ("(Ada 2005) does not implement interface }",
2965                            Actual, Etype (Etype (Formal)));
2966                      end if;
2967
2968                      Wrong_Type (Actual, Etype (Formal));
2969
2970                      if Nkind (Actual) = N_Op_Eq
2971                        and then Nkind (Left_Opnd (Actual)) = N_Identifier
2972                      then
2973                         Formal := First_Formal (Nam);
2974                         while Present (Formal) loop
2975                            if Chars (Left_Opnd (Actual)) = Chars (Formal) then
2976                               Error_Msg_N -- CODEFIX
2977                                 ("possible misspelling of `='>`!", Actual);
2978                               exit;
2979                            end if;
2980
2981                            Next_Formal (Formal);
2982                         end loop;
2983                      end if;
2984
2985                      if All_Errors_Mode then
2986                         Error_Msg_Sloc := Sloc (Nam);
2987
2988                         if Etype (Formal) = Any_Type then
2989                            Error_Msg_N
2990                              ("there is no legal actual parameter", Actual);
2991                         end if;
2992
2993                         if Is_Overloadable (Nam)
2994                           and then Present (Alias (Nam))
2995                           and then not Comes_From_Source (Nam)
2996                         then
2997                            Error_Msg_NE
2998                              ("\\  =='> in call to inherited operation & #!",
2999                               Actual, Nam);
3000
3001                         elsif Ekind (Nam) = E_Subprogram_Type then
3002                            declare
3003                               Access_To_Subprogram_Typ :
3004                                 constant Entity_Id :=
3005                                   Defining_Identifier
3006                                     (Associated_Node_For_Itype (Nam));
3007                            begin
3008                               Error_Msg_NE (
3009                                 "\\  =='> in call to dereference of &#!",
3010                                 Actual, Access_To_Subprogram_Typ);
3011                            end;
3012
3013                         else
3014                            Error_Msg_NE
3015                              ("\\  =='> in call to &#!", Actual, Nam);
3016
3017                         end if;
3018                      end if;
3019                   end if;
3020
3021                   return;
3022                end if;
3023
3024             else
3025                --  Normalize_Actuals has verified that a default value exists
3026                --  for this formal. Current actual names a subsequent formal.
3027
3028                Next_Formal (Formal);
3029             end if;
3030          end loop;
3031
3032          --  On exit, all actuals match
3033
3034          Indicate_Name_And_Type;
3035       end if;
3036    end Analyze_One_Call;
3037
3038    ---------------------------
3039    -- Analyze_Operator_Call --
3040    ---------------------------
3041
3042    procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
3043       Op_Name : constant Name_Id := Chars (Op_Id);
3044       Act1    : constant Node_Id := First_Actual (N);
3045       Act2    : constant Node_Id := Next_Actual (Act1);
3046
3047    begin
3048       --  Binary operator case
3049
3050       if Present (Act2) then
3051
3052          --  If more than two operands, then not binary operator after all
3053
3054          if Present (Next_Actual (Act2)) then
3055             return;
3056
3057          elsif     Op_Name = Name_Op_Add
3058            or else Op_Name = Name_Op_Subtract
3059            or else Op_Name = Name_Op_Multiply
3060            or else Op_Name = Name_Op_Divide
3061            or else Op_Name = Name_Op_Mod
3062            or else Op_Name = Name_Op_Rem
3063            or else Op_Name = Name_Op_Expon
3064          then
3065             Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
3066
3067          elsif     Op_Name =  Name_Op_And
3068            or else Op_Name = Name_Op_Or
3069            or else Op_Name = Name_Op_Xor
3070          then
3071             Find_Boolean_Types (Act1, Act2, Op_Id, N);
3072
3073          elsif     Op_Name = Name_Op_Lt
3074            or else Op_Name = Name_Op_Le
3075            or else Op_Name = Name_Op_Gt
3076            or else Op_Name = Name_Op_Ge
3077          then
3078             Find_Comparison_Types (Act1, Act2, Op_Id,  N);
3079
3080          elsif     Op_Name = Name_Op_Eq
3081            or else Op_Name = Name_Op_Ne
3082          then
3083             Find_Equality_Types (Act1, Act2, Op_Id,  N);
3084
3085          elsif     Op_Name = Name_Op_Concat then
3086             Find_Concatenation_Types (Act1, Act2, Op_Id, N);
3087
3088          --  Is this else null correct, or should it be an abort???
3089
3090          else
3091             null;
3092          end if;
3093
3094       --  Unary operator case
3095
3096       else
3097          if Op_Name = Name_Op_Subtract or else
3098             Op_Name = Name_Op_Add      or else
3099             Op_Name = Name_Op_Abs
3100          then
3101             Find_Unary_Types (Act1, Op_Id, N);
3102
3103          elsif
3104             Op_Name = Name_Op_Not
3105          then
3106             Find_Negation_Types (Act1, Op_Id, N);
3107
3108          --  Is this else null correct, or should it be an abort???
3109
3110          else
3111             null;
3112          end if;
3113       end if;
3114    end Analyze_Operator_Call;
3115
3116    -------------------------------------------
3117    -- Analyze_Overloaded_Selected_Component --
3118    -------------------------------------------
3119
3120    procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
3121       Nam   : constant Node_Id := Prefix (N);
3122       Sel   : constant Node_Id := Selector_Name (N);
3123       Comp  : Entity_Id;
3124       I     : Interp_Index;
3125       It    : Interp;
3126       T     : Entity_Id;
3127
3128    begin
3129       Set_Etype (Sel, Any_Type);
3130
3131       Get_First_Interp (Nam, I, It);
3132       while Present (It.Typ) loop
3133          if Is_Access_Type (It.Typ) then
3134             T := Designated_Type (It.Typ);
3135             Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3136          else
3137             T := It.Typ;
3138          end if;
3139
3140          --  Locate the component. For a private prefix the selector can denote
3141          --  a discriminant.
3142
3143          if Is_Record_Type (T) or else Is_Private_Type (T) then
3144
3145             --  If the prefix is a class-wide type, the visible components are
3146             --  those of the base type.
3147
3148             if Is_Class_Wide_Type (T) then
3149                T := Etype (T);
3150             end if;
3151
3152             Comp := First_Entity (T);
3153             while Present (Comp) loop
3154                if Chars (Comp) = Chars (Sel)
3155                  and then Is_Visible_Component (Comp)
3156                then
3157
3158                   --  AI05-105:  if the context is an object renaming with
3159                   --  an anonymous access type, the expected type of the
3160                   --  object must be anonymous. This is a name resolution rule.
3161
3162                   if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
3163                     or else No (Access_Definition (Parent (N)))
3164                     or else Ekind (Etype (Comp)) = E_Anonymous_Access_Type
3165                     or else
3166                       Ekind (Etype (Comp)) = E_Anonymous_Access_Subprogram_Type
3167                   then
3168                      Set_Entity (Sel, Comp);
3169                      Set_Etype (Sel, Etype (Comp));
3170                      Add_One_Interp (N, Etype (Comp), Etype (Comp));
3171
3172                      --  This also specifies a candidate to resolve the name.
3173                      --  Further overloading will be resolved from context.
3174                      --  The selector name itself does not carry overloading
3175                      --  information.
3176
3177                      Set_Etype (Nam, It.Typ);
3178
3179                   else
3180                      --  Named access type in the context of a renaming
3181                      --  declaration with an access definition. Remove
3182                      --  inapplicable candidate.
3183
3184                      Remove_Interp (I);
3185                   end if;
3186                end if;
3187
3188                Next_Entity (Comp);
3189             end loop;
3190
3191          elsif Is_Concurrent_Type (T) then
3192             Comp := First_Entity (T);
3193             while Present (Comp)
3194               and then Comp /= First_Private_Entity (T)
3195             loop
3196                if Chars (Comp) = Chars (Sel) then
3197                   if Is_Overloadable (Comp) then
3198                      Add_One_Interp (Sel, Comp, Etype (Comp));
3199                   else
3200                      Set_Entity_With_Style_Check (Sel, Comp);
3201                      Generate_Reference (Comp, Sel);
3202                   end if;
3203
3204                   Set_Etype (Sel, Etype (Comp));
3205                   Set_Etype (N,   Etype (Comp));
3206                   Set_Etype (Nam, It.Typ);
3207
3208                   --  For access type case, introduce explicit dereference for
3209                   --  more uniform treatment of entry calls. Do this only once
3210                   --  if several interpretations yield an access type.
3211
3212                   if Is_Access_Type (Etype (Nam))
3213                     and then Nkind (Nam) /= N_Explicit_Dereference
3214                   then
3215                      Insert_Explicit_Dereference (Nam);
3216                      Error_Msg_NW
3217                        (Warn_On_Dereference, "?implicit dereference", N);
3218                   end if;
3219                end if;
3220
3221                Next_Entity (Comp);
3222             end loop;
3223
3224             Set_Is_Overloaded (N, Is_Overloaded (Sel));
3225          end if;
3226
3227          Get_Next_Interp (I, It);
3228       end loop;
3229
3230       if Etype (N) = Any_Type
3231         and then not Try_Object_Operation (N)
3232       then
3233          Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
3234          Set_Entity (Sel, Any_Id);
3235          Set_Etype  (Sel, Any_Type);
3236       end if;
3237    end Analyze_Overloaded_Selected_Component;
3238
3239    ----------------------------------
3240    -- Analyze_Qualified_Expression --
3241    ----------------------------------
3242
3243    procedure Analyze_Qualified_Expression (N : Node_Id) is
3244       Mark : constant Entity_Id := Subtype_Mark (N);
3245       Expr : constant Node_Id   := Expression (N);
3246       I    : Interp_Index;
3247       It   : Interp;
3248       T    : Entity_Id;
3249
3250    begin
3251       Mark_Non_ALFA_Subprogram;
3252
3253       Analyze_Expression (Expr);
3254
3255       Set_Etype (N, Any_Type);
3256       Find_Type (Mark);
3257       T := Entity (Mark);
3258       Set_Etype (N, T);
3259
3260       if T = Any_Type then
3261          return;
3262       end if;
3263
3264       Check_Fully_Declared (T, N);
3265
3266       --  If expected type is class-wide, check for exact match before
3267       --  expansion, because if the expression is a dispatching call it
3268       --  may be rewritten as explicit dereference with class-wide result.
3269       --  If expression is overloaded, retain only interpretations that
3270       --  will yield exact matches.
3271
3272       if Is_Class_Wide_Type (T) then
3273          if not Is_Overloaded (Expr) then
3274             if  Base_Type (Etype (Expr)) /= Base_Type (T) then
3275                if Nkind (Expr) = N_Aggregate then
3276                   Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
3277                else
3278                   Wrong_Type (Expr, T);
3279                end if;
3280             end if;
3281
3282          else
3283             Get_First_Interp (Expr, I, It);
3284
3285             while Present (It.Nam) loop
3286                if Base_Type (It.Typ) /= Base_Type (T) then
3287                   Remove_Interp (I);
3288                end if;
3289
3290                Get_Next_Interp (I, It);
3291             end loop;
3292          end if;
3293       end if;
3294
3295       Set_Etype  (N, T);
3296    end Analyze_Qualified_Expression;
3297
3298    -----------------------------------
3299    -- Analyze_Quantified_Expression --
3300    -----------------------------------
3301
3302    procedure Analyze_Quantified_Expression (N : Node_Id) is
3303       Loc : constant Source_Ptr := Sloc (N);
3304       Ent : constant Entity_Id :=
3305               New_Internal_Entity
3306                 (E_Loop, Current_Scope, Sloc (N), 'L');
3307
3308       Iterator : Node_Id;
3309
3310    begin
3311       Mark_Non_ALFA_Subprogram;
3312       Check_SPARK_Restriction ("quantified expression is not allowed", N);
3313
3314       Set_Etype  (Ent,  Standard_Void_Type);
3315       Set_Parent (Ent, N);
3316
3317       if Present (Loop_Parameter_Specification (N)) then
3318          Iterator :=
3319            Make_Iteration_Scheme (Loc,
3320              Loop_Parameter_Specification =>
3321                Loop_Parameter_Specification (N));
3322       else
3323          Iterator :=
3324            Make_Iteration_Scheme (Loc,
3325               Iterator_Specification =>
3326                 Iterator_Specification (N));
3327       end if;
3328
3329       Push_Scope (Ent);
3330       Set_Parent (Iterator, N);
3331       Analyze_Iteration_Scheme (Iterator);
3332
3333       --  The loop specification may have been converted into an
3334       --  iterator specification during its analysis. Update the
3335       --  quantified node accordingly.
3336
3337       if Present (Iterator_Specification (Iterator)) then
3338          Set_Iterator_Specification
3339            (N, Iterator_Specification (Iterator));
3340          Set_Loop_Parameter_Specification (N, Empty);
3341       end if;
3342
3343       Analyze (Condition (N));
3344       End_Scope;
3345
3346       Set_Etype (N, Standard_Boolean);
3347    end Analyze_Quantified_Expression;
3348
3349    -------------------
3350    -- Analyze_Range --
3351    -------------------
3352
3353    procedure Analyze_Range (N : Node_Id) is
3354       L        : constant Node_Id := Low_Bound (N);
3355       H        : constant Node_Id := High_Bound (N);
3356       I1, I2   : Interp_Index;
3357       It1, It2 : Interp;
3358
3359       procedure Check_Common_Type (T1, T2 : Entity_Id);
3360       --  Verify the compatibility of two types,  and choose the
3361       --  non universal one if the other is universal.
3362
3363       procedure Check_High_Bound (T : Entity_Id);
3364       --  Test one interpretation of the low bound against all those
3365       --  of the high bound.
3366
3367       procedure Check_Universal_Expression (N : Node_Id);
3368       --  In Ada83, reject bounds of a universal range that are not
3369       --  literals or entity names.
3370
3371       -----------------------
3372       -- Check_Common_Type --
3373       -----------------------
3374
3375       procedure Check_Common_Type (T1, T2 : Entity_Id) is
3376       begin
3377          if Covers (T1 => T1, T2 => T2)
3378               or else
3379             Covers (T1 => T2, T2 => T1)
3380          then
3381             if T1 = Universal_Integer
3382               or else T1 = Universal_Real
3383               or else T1 = Any_Character
3384             then
3385                Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
3386
3387             elsif T1 = T2 then
3388                Add_One_Interp (N, T1, T1);
3389
3390             else
3391                Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
3392             end if;
3393          end if;
3394       end Check_Common_Type;
3395
3396       ----------------------
3397       -- Check_High_Bound --
3398       ----------------------
3399
3400       procedure Check_High_Bound (T : Entity_Id) is
3401       begin
3402          if not Is_Overloaded (H) then
3403             Check_Common_Type (T, Etype (H));
3404          else
3405             Get_First_Interp (H, I2, It2);
3406             while Present (It2.Typ) loop
3407                Check_Common_Type (T, It2.Typ);
3408                Get_Next_Interp (I2, It2);
3409             end loop;
3410          end if;
3411       end Check_High_Bound;
3412
3413       -----------------------------
3414       -- Is_Universal_Expression --
3415       -----------------------------
3416
3417       procedure Check_Universal_Expression (N : Node_Id) is
3418       begin
3419          if Etype (N) = Universal_Integer
3420            and then Nkind (N) /= N_Integer_Literal
3421            and then not Is_Entity_Name (N)
3422            and then Nkind (N) /= N_Attribute_Reference
3423          then
3424             Error_Msg_N ("illegal bound in discrete range", N);
3425          end if;
3426       end Check_Universal_Expression;
3427
3428    --  Start of processing for Analyze_Range
3429
3430    begin
3431       Set_Etype (N, Any_Type);
3432       Analyze_Expression (L);
3433       Analyze_Expression (H);
3434
3435       if Etype (L) = Any_Type or else Etype (H) = Any_Type then
3436          return;
3437
3438       else
3439          if not Is_Overloaded (L) then
3440             Check_High_Bound (Etype (L));
3441          else
3442             Get_First_Interp (L, I1, It1);
3443             while Present (It1.Typ) loop
3444                Check_High_Bound (It1.Typ);
3445                Get_Next_Interp (I1, It1);
3446             end loop;
3447          end if;
3448
3449          --  If result is Any_Type, then we did not find a compatible pair
3450
3451          if Etype (N) = Any_Type then
3452             Error_Msg_N ("incompatible types in range ", N);
3453          end if;
3454       end if;
3455
3456       if Ada_Version = Ada_83
3457         and then
3458           (Nkind (Parent (N)) = N_Loop_Parameter_Specification
3459              or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
3460       then
3461          Check_Universal_Expression (L);
3462          Check_Universal_Expression (H);
3463       end if;
3464    end Analyze_Range;
3465
3466    -----------------------
3467    -- Analyze_Reference --
3468    -----------------------
3469
3470    procedure Analyze_Reference (N : Node_Id) is
3471       P        : constant Node_Id := Prefix (N);
3472       E        : Entity_Id;
3473       T        : Entity_Id;
3474       Acc_Type : Entity_Id;
3475
3476    begin
3477       Mark_Non_ALFA_Subprogram;
3478
3479       Analyze (P);
3480
3481       --  An interesting error check, if we take the 'Reference of an object
3482       --  for which a pragma Atomic or Volatile has been given, and the type
3483       --  of the object is not Atomic or Volatile, then we are in trouble. The
3484       --  problem is that no trace of the atomic/volatile status will remain
3485       --  for the backend to respect when it deals with the resulting pointer,
3486       --  since the pointer type will not be marked atomic (it is a pointer to
3487       --  the base type of the object).
3488
3489       --  It is not clear if that can ever occur, but in case it does, we will
3490       --  generate an error message. Not clear if this message can ever be
3491       --  generated, and pretty clear that it represents a bug if it is, still
3492       --  seems worth checking, except in CodePeer mode where we do not really
3493       --  care and don't want to bother the user.
3494
3495       T := Etype (P);
3496
3497       if Is_Entity_Name (P)
3498         and then Is_Object_Reference (P)
3499         and then not CodePeer_Mode
3500       then
3501          E := Entity (P);
3502          T := Etype (P);
3503
3504          if (Has_Atomic_Components   (E)
3505                and then not Has_Atomic_Components   (T))
3506            or else
3507             (Has_Volatile_Components (E)
3508                and then not Has_Volatile_Components (T))
3509            or else (Is_Atomic   (E) and then not Is_Atomic   (T))
3510            or else (Is_Volatile (E) and then not Is_Volatile (T))
3511          then
3512             Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
3513          end if;
3514       end if;
3515
3516       --  Carry on with normal processing
3517
3518       Acc_Type := Create_Itype (E_Allocator_Type, N);
3519       Set_Etype (Acc_Type,  Acc_Type);
3520       Set_Directly_Designated_Type (Acc_Type, Etype (P));
3521       Set_Etype (N, Acc_Type);
3522    end Analyze_Reference;
3523
3524    --------------------------------
3525    -- Analyze_Selected_Component --
3526    --------------------------------
3527
3528    --  Prefix is a record type or a task or protected type. In the latter case,
3529    --  the selector must denote a visible entry.
3530
3531    procedure Analyze_Selected_Component (N : Node_Id) is
3532       Name          : constant Node_Id := Prefix (N);
3533       Sel           : constant Node_Id := Selector_Name (N);
3534       Act_Decl      : Node_Id;
3535       Comp          : Entity_Id;
3536       Has_Candidate : Boolean := False;
3537       In_Scope      : Boolean;
3538       Parent_N      : Node_Id;
3539       Pent          : Entity_Id := Empty;
3540       Prefix_Type   : Entity_Id;
3541
3542       Type_To_Use : Entity_Id;
3543       --  In most cases this is the Prefix_Type, but if the Prefix_Type is
3544       --  a class-wide type, we use its root type, whose components are
3545       --  present in the class-wide type.
3546
3547       Is_Single_Concurrent_Object : Boolean;
3548       --  Set True if the prefix is a single task or a single protected object
3549
3550       procedure Find_Component_In_Instance (Rec : Entity_Id);
3551       --  In an instance, a component of a private extension may not be visible
3552       --  while it was visible in the generic. Search candidate scope for a
3553       --  component with the proper identifier. This is only done if all other
3554       --  searches have failed. When the match is found (it always will be),
3555       --  the Etype of both N and Sel are set from this component, and the
3556       --  entity of Sel is set to reference this component.
3557
3558       function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
3559       --  It is known that the parent of N denotes a subprogram call. Comp
3560       --  is an overloadable component of the concurrent type of the prefix.
3561       --  Determine whether all formals of the parent of N and Comp are mode
3562       --  conformant. If the parent node is not analyzed yet it may be an
3563       --  indexed component rather than a function call.
3564
3565       --------------------------------
3566       -- Find_Component_In_Instance --
3567       --------------------------------
3568
3569       procedure Find_Component_In_Instance (Rec : Entity_Id) is
3570          Comp : Entity_Id;
3571
3572       begin
3573          Comp := First_Component (Rec);
3574          while Present (Comp) loop
3575             if Chars (Comp) = Chars (Sel) then
3576                Set_Entity_With_Style_Check (Sel, Comp);
3577                Set_Etype (Sel, Etype (Comp));
3578                Set_Etype (N,   Etype (Comp));
3579                return;
3580             end if;
3581
3582             Next_Component (Comp);
3583          end loop;
3584
3585          --  This must succeed because code was legal in the generic
3586
3587          raise Program_Error;
3588       end Find_Component_In_Instance;
3589
3590       ------------------------------
3591       -- Has_Mode_Conformant_Spec --
3592       ------------------------------
3593
3594       function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
3595          Comp_Param : Entity_Id;
3596          Param      : Node_Id;
3597          Param_Typ  : Entity_Id;
3598
3599       begin
3600          Comp_Param := First_Formal (Comp);
3601
3602          if Nkind (Parent (N)) = N_Indexed_Component then
3603             Param := First (Expressions (Parent (N)));
3604          else
3605             Param := First (Parameter_Associations (Parent (N)));
3606          end if;
3607
3608          while Present (Comp_Param)
3609            and then Present (Param)
3610          loop
3611             Param_Typ := Find_Parameter_Type (Param);
3612
3613             if Present (Param_Typ)
3614               and then
3615                 not Conforming_Types
3616                      (Etype (Comp_Param), Param_Typ, Mode_Conformant)
3617             then
3618                return False;
3619             end if;
3620
3621             Next_Formal (Comp_Param);
3622             Next (Param);
3623          end loop;
3624
3625          --  One of the specs has additional formals
3626
3627          if Present (Comp_Param) or else Present (Param) then
3628             return False;
3629          end if;
3630
3631          return True;
3632       end Has_Mode_Conformant_Spec;
3633
3634    --  Start of processing for Analyze_Selected_Component
3635
3636    begin
3637       Set_Etype (N, Any_Type);
3638
3639       if Is_Overloaded (Name) then
3640          Analyze_Overloaded_Selected_Component (N);
3641          return;
3642
3643       elsif Etype (Name) = Any_Type then
3644          Set_Entity (Sel, Any_Id);
3645          Set_Etype (Sel, Any_Type);
3646          return;
3647
3648       else
3649          Prefix_Type := Etype (Name);
3650       end if;
3651
3652       if Is_Access_Type (Prefix_Type) then
3653
3654          --  A RACW object can never be used as prefix of a selected component
3655          --  since that means it is dereferenced without being a controlling
3656          --  operand of a dispatching operation (RM E.2.2(16/1)). Before
3657          --  reporting an error, we must check whether this is actually a
3658          --  dispatching call in prefix form.
3659
3660          if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
3661            and then Comes_From_Source (N)
3662          then
3663             if Try_Object_Operation (N) then
3664                return;
3665             else
3666                Error_Msg_N
3667                  ("invalid dereference of a remote access-to-class-wide value",
3668                   N);
3669             end if;
3670
3671          --  Normal case of selected component applied to access type
3672
3673          else
3674             Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3675
3676             if Is_Entity_Name (Name) then
3677                Pent := Entity (Name);
3678             elsif Nkind (Name) = N_Selected_Component
3679               and then Is_Entity_Name (Selector_Name (Name))
3680             then
3681                Pent := Entity (Selector_Name (Name));
3682             end if;
3683
3684             Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name);
3685          end if;
3686
3687       --  If we have an explicit dereference of a remote access-to-class-wide
3688       --  value, then issue an error (see RM-E.2.2(16/1)). However we first
3689       --  have to check for the case of a prefix that is a controlling operand
3690       --  of a prefixed dispatching call, as the dereference is legal in that
3691       --  case. Normally this condition is checked in Validate_Remote_Access_
3692       --  To_Class_Wide_Type, but we have to defer the checking for selected
3693       --  component prefixes because of the prefixed dispatching call case.
3694       --  Note that implicit dereferences are checked for this just above.
3695
3696       elsif Nkind (Name) = N_Explicit_Dereference
3697         and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
3698         and then Comes_From_Source (N)
3699       then
3700          if Try_Object_Operation (N) then
3701             return;
3702          else
3703             Error_Msg_N
3704               ("invalid dereference of a remote access-to-class-wide value",
3705                N);
3706          end if;
3707       end if;
3708
3709       --  (Ada 2005): if the prefix is the limited view of a type, and
3710       --  the context already includes the full view, use the full view
3711       --  in what follows, either to retrieve a component of to find
3712       --  a primitive operation. If the prefix is an explicit dereference,
3713       --  set the type of the prefix to reflect this transformation.
3714       --  If the non-limited view is itself an incomplete type, get the
3715       --  full view if available.
3716
3717       if Is_Incomplete_Type (Prefix_Type)
3718         and then From_With_Type (Prefix_Type)
3719         and then Present (Non_Limited_View (Prefix_Type))
3720       then
3721          Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
3722
3723          if Nkind (N) = N_Explicit_Dereference then
3724             Set_Etype (Prefix (N), Prefix_Type);
3725          end if;
3726
3727       elsif Ekind (Prefix_Type) = E_Class_Wide_Type
3728         and then From_With_Type (Prefix_Type)
3729         and then Present (Non_Limited_View (Etype (Prefix_Type)))
3730       then
3731          Prefix_Type :=
3732            Class_Wide_Type (Non_Limited_View (Etype (Prefix_Type)));
3733
3734          if Nkind (N) = N_Explicit_Dereference then
3735             Set_Etype (Prefix (N), Prefix_Type);
3736          end if;
3737       end if;
3738
3739       if Ekind (Prefix_Type) = E_Private_Subtype then
3740          Prefix_Type := Base_Type (Prefix_Type);
3741       end if;
3742
3743       Type_To_Use := Prefix_Type;
3744
3745       --  For class-wide types, use the entity list of the root type. This
3746       --  indirection is specially important for private extensions because
3747       --  only the root type get switched (not the class-wide type).
3748
3749       if Is_Class_Wide_Type (Prefix_Type) then
3750          Type_To_Use := Root_Type (Prefix_Type);
3751       end if;
3752
3753       --  If the prefix is a single concurrent object, use its name in error
3754       --  messages, rather than that of its anonymous type.
3755
3756       Is_Single_Concurrent_Object :=
3757         Is_Concurrent_Type (Prefix_Type)
3758           and then Is_Internal_Name (Chars (Prefix_Type))
3759           and then not Is_Derived_Type (Prefix_Type)
3760           and then Is_Entity_Name (Name);
3761
3762       Comp := First_Entity (Type_To_Use);
3763
3764       --  If the selector has an original discriminant, the node appears in
3765       --  an instance. Replace the discriminant with the corresponding one
3766       --  in the current discriminated type. For nested generics, this must
3767       --  be done transitively, so note the new original discriminant.
3768
3769       if Nkind (Sel) = N_Identifier
3770         and then In_Instance
3771         and then Present (Original_Discriminant (Sel))
3772       then
3773          Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
3774
3775          --  Mark entity before rewriting, for completeness and because
3776          --  subsequent semantic checks might examine the original node.
3777
3778          Set_Entity (Sel, Comp);
3779          Rewrite (Selector_Name (N),
3780            New_Occurrence_Of (Comp, Sloc (N)));
3781          Set_Original_Discriminant (Selector_Name (N), Comp);
3782          Set_Etype (N, Etype (Comp));
3783
3784          if Is_Access_Type (Etype (Name)) then
3785             Insert_Explicit_Dereference (Name);
3786             Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3787          end if;
3788
3789       elsif Is_Record_Type (Prefix_Type) then
3790
3791          --  Find component with given name
3792
3793          while Present (Comp) loop
3794             if Chars (Comp) = Chars (Sel)
3795               and then Is_Visible_Component (Comp)
3796             then
3797                Set_Entity_With_Style_Check (Sel, Comp);
3798                Set_Etype (Sel, Etype (Comp));
3799
3800                if Ekind (Comp) = E_Discriminant then
3801                   if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
3802                      Error_Msg_N
3803                        ("cannot reference discriminant of Unchecked_Union",
3804                         Sel);
3805                   end if;
3806
3807                   if Is_Generic_Type (Prefix_Type)
3808                        or else
3809                      Is_Generic_Type (Root_Type (Prefix_Type))
3810                   then
3811                      Set_Original_Discriminant (Sel, Comp);
3812                   end if;
3813                end if;
3814
3815                --  Resolve the prefix early otherwise it is not possible to
3816                --  build the actual subtype of the component: it may need
3817                --  to duplicate this prefix and duplication is only allowed
3818                --  on fully resolved expressions.
3819
3820                Resolve (Name);
3821
3822                --  Ada 2005 (AI-50217): Check wrong use of incomplete types or
3823                --  subtypes in a package specification.
3824                --  Example:
3825
3826                --    limited with Pkg;
3827                --    package Pkg is
3828                --       type Acc_Inc is access Pkg.T;
3829                --       X : Acc_Inc;
3830                --       N : Natural := X.all.Comp;  --  ERROR, limited view
3831                --    end Pkg;                       --  Comp is not visible
3832
3833                if Nkind (Name) = N_Explicit_Dereference
3834                  and then From_With_Type (Etype (Prefix (Name)))
3835                  and then not Is_Potentially_Use_Visible (Etype (Name))
3836                  and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
3837                             N_Package_Specification
3838                then
3839                   Error_Msg_NE
3840                     ("premature usage of incomplete}", Prefix (Name),
3841                      Etype (Prefix (Name)));
3842                end if;
3843
3844                --  We never need an actual subtype for the case of a selection
3845                --  for a indexed component of a non-packed array, since in
3846                --  this case gigi generates all the checks and can find the
3847                --  necessary bounds information.
3848
3849                --  We also do not need an actual subtype for the case of a
3850                --  first, last, length, or range attribute applied to a
3851                --  non-packed array, since gigi can again get the bounds in
3852                --  these cases (gigi cannot handle the packed case, since it
3853                --  has the bounds of the packed array type, not the original
3854                --  bounds of the type). However, if the prefix is itself a
3855                --  selected component, as in a.b.c (i), gigi may regard a.b.c
3856                --  as a dynamic-sized temporary, so we do generate an actual
3857                --  subtype for this case.
3858
3859                Parent_N := Parent (N);
3860
3861                if not Is_Packed (Etype (Comp))
3862                  and then
3863                    ((Nkind (Parent_N) = N_Indexed_Component
3864                        and then Nkind (Name) /= N_Selected_Component)
3865                      or else
3866                       (Nkind (Parent_N) = N_Attribute_Reference
3867                          and then (Attribute_Name (Parent_N) = Name_First
3868                                      or else
3869                                    Attribute_Name (Parent_N) = Name_Last
3870                                      or else
3871                                    Attribute_Name (Parent_N) = Name_Length
3872                                      or else
3873                                    Attribute_Name (Parent_N) = Name_Range)))
3874                then
3875                   Set_Etype (N, Etype (Comp));
3876
3877                --  If full analysis is not enabled, we do not generate an
3878                --  actual subtype, because in the absence of expansion
3879                --  reference to a formal of a protected type, for example,
3880                --  will not be properly transformed, and will lead to
3881                --  out-of-scope references in gigi.
3882
3883                --  In all other cases, we currently build an actual subtype.
3884                --  It seems likely that many of these cases can be avoided,
3885                --  but right now, the front end makes direct references to the
3886                --  bounds (e.g. in generating a length check), and if we do
3887                --  not make an actual subtype, we end up getting a direct
3888                --  reference to a discriminant, which will not do.
3889
3890                elsif Full_Analysis then
3891                   Act_Decl :=
3892                     Build_Actual_Subtype_Of_Component (Etype (Comp), N);
3893                   Insert_Action (N, Act_Decl);
3894
3895                   if No (Act_Decl) then
3896                      Set_Etype (N, Etype (Comp));
3897
3898                   else
3899                      --  Component type depends on discriminants. Enter the
3900                      --  main attributes of the subtype.
3901
3902                      declare
3903                         Subt : constant Entity_Id :=
3904                                  Defining_Identifier (Act_Decl);
3905
3906                      begin
3907                         Set_Etype (Subt, Base_Type (Etype (Comp)));
3908                         Set_Ekind (Subt, Ekind (Etype (Comp)));
3909                         Set_Etype (N, Subt);
3910                      end;
3911                   end if;
3912
3913                --  If Full_Analysis not enabled, just set the Etype
3914
3915                else
3916                   Set_Etype (N, Etype (Comp));
3917                end if;
3918
3919                return;
3920             end if;
3921
3922             --  If the prefix is a private extension, check only the visible
3923             --  components of the partial view. This must include the tag,
3924             --  which can appear in expanded code in a tag check.
3925
3926             if Ekind (Type_To_Use) = E_Record_Type_With_Private
3927               and then Chars (Selector_Name (N)) /= Name_uTag
3928             then
3929                exit when Comp = Last_Entity (Type_To_Use);
3930             end if;
3931
3932             Next_Entity (Comp);
3933          end loop;
3934
3935          --  Ada 2005 (AI-252): The selected component can be interpreted as
3936          --  a prefixed view of a subprogram. Depending on the context, this is
3937          --  either a name that can appear in a renaming declaration, or part
3938          --  of an enclosing call given in prefix form.
3939
3940          --  Ada 2005 (AI05-0030): In the case of dispatching requeue, the
3941          --  selected component should resolve to a name.
3942
3943          if Ada_Version >= Ada_2005
3944            and then Is_Tagged_Type (Prefix_Type)
3945            and then not Is_Concurrent_Type (Prefix_Type)
3946          then
3947             if Nkind (Parent (N)) = N_Generic_Association
3948               or else Nkind (Parent (N)) = N_Requeue_Statement
3949               or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
3950             then
3951                if Find_Primitive_Operation (N) then
3952                   return;
3953                end if;
3954
3955             elsif Try_Object_Operation (N) then
3956                return;
3957             end if;
3958
3959             --  If the transformation fails, it will be necessary to redo the
3960             --  analysis with all errors enabled, to indicate candidate
3961             --  interpretations and reasons for each failure ???
3962
3963          end if;
3964
3965       elsif Is_Private_Type (Prefix_Type) then
3966
3967          --  Allow access only to discriminants of the type. If the type has
3968          --  no full view, gigi uses the parent type for the components, so we
3969          --  do the same here.
3970
3971          if No (Full_View (Prefix_Type)) then
3972             Type_To_Use := Root_Type (Base_Type (Prefix_Type));
3973             Comp := First_Entity (Type_To_Use);
3974          end if;
3975
3976          while Present (Comp) loop
3977             if Chars (Comp) = Chars (Sel) then
3978                if Ekind (Comp) = E_Discriminant then
3979                   Set_Entity_With_Style_Check (Sel, Comp);
3980                   Generate_Reference (Comp, Sel);
3981
3982                   Set_Etype (Sel, Etype (Comp));
3983                   Set_Etype (N,   Etype (Comp));
3984
3985                   if Is_Generic_Type (Prefix_Type)
3986                     or else Is_Generic_Type (Root_Type (Prefix_Type))
3987                   then
3988                      Set_Original_Discriminant (Sel, Comp);
3989                   end if;
3990
3991                --  Before declaring an error, check whether this is tagged
3992                --  private type and a call to a primitive operation.
3993
3994                elsif Ada_Version >= Ada_2005
3995                  and then Is_Tagged_Type (Prefix_Type)
3996                  and then Try_Object_Operation (N)
3997                then
3998                   return;
3999
4000                else
4001                   Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4002                   Error_Msg_NE ("invisible selector& for }", N, Sel);
4003                   Set_Entity (Sel, Any_Id);
4004                   Set_Etype (N, Any_Type);
4005                end if;
4006
4007                return;
4008             end if;
4009
4010             Next_Entity (Comp);
4011          end loop;
4012
4013       elsif Is_Concurrent_Type (Prefix_Type) then
4014
4015          --  Find visible operation with given name. For a protected type,
4016          --  the possible candidates are discriminants, entries or protected
4017          --  procedures. For a task type, the set can only include entries or
4018          --  discriminants if the task type is not an enclosing scope. If it
4019          --  is an enclosing scope (e.g. in an inner task) then all entities
4020          --  are visible, but the prefix must denote the enclosing scope, i.e.
4021          --  can only be a direct name or an expanded name.
4022
4023          Set_Etype (Sel, Any_Type);
4024          In_Scope := In_Open_Scopes (Prefix_Type);
4025
4026          while Present (Comp) loop
4027             if Chars (Comp) = Chars (Sel) then
4028                if Is_Overloadable (Comp) then
4029                   Add_One_Interp (Sel, Comp, Etype (Comp));
4030
4031                   --  If the prefix is tagged, the correct interpretation may
4032                   --  lie in the primitive or class-wide operations of the
4033                   --  type. Perform a simple conformance check to determine
4034                   --  whether Try_Object_Operation should be invoked even if
4035                   --  a visible entity is found.
4036
4037                   if Is_Tagged_Type (Prefix_Type)
4038                     and then
4039                       Nkind_In (Parent (N), N_Procedure_Call_Statement,
4040                                             N_Function_Call,
4041                                             N_Indexed_Component)
4042                     and then Has_Mode_Conformant_Spec (Comp)
4043                   then
4044                      Has_Candidate := True;
4045                   end if;
4046
4047                --  Note: a selected component may not denote a component of a
4048                --  protected type (4.1.3(7)).
4049
4050                elsif Ekind_In (Comp, E_Discriminant, E_Entry_Family)
4051                  or else (In_Scope
4052                             and then not Is_Protected_Type (Prefix_Type)
4053                             and then Is_Entity_Name (Name))
4054                then
4055                   Set_Entity_With_Style_Check (Sel, Comp);
4056                   Generate_Reference (Comp, Sel);
4057
4058                else
4059                   goto Next_Comp;
4060                end if;
4061
4062                Set_Etype (Sel, Etype (Comp));
4063                Set_Etype (N,   Etype (Comp));
4064
4065                if Ekind (Comp) = E_Discriminant then
4066                   Set_Original_Discriminant (Sel, Comp);
4067                end if;
4068
4069                --  For access type case, introduce explicit dereference for
4070                --  more uniform treatment of entry calls.
4071
4072                if Is_Access_Type (Etype (Name)) then
4073                   Insert_Explicit_Dereference (Name);
4074                   Error_Msg_NW
4075                     (Warn_On_Dereference, "?implicit dereference", N);
4076                end if;
4077             end if;
4078
4079             <<Next_Comp>>
4080                Next_Entity (Comp);
4081                exit when not In_Scope
4082                  and then
4083                    Comp = First_Private_Entity (Base_Type (Prefix_Type));
4084          end loop;
4085
4086          --  If there is no visible entity with the given name or none of the
4087          --  visible entities are plausible interpretations, check whether
4088          --  there is some other primitive operation with that name.
4089
4090          if Ada_Version >= Ada_2005
4091            and then Is_Tagged_Type (Prefix_Type)
4092          then
4093             if (Etype (N) = Any_Type
4094                   or else not Has_Candidate)
4095               and then Try_Object_Operation (N)
4096             then
4097                return;
4098
4099             --  If the context is not syntactically a procedure call, it
4100             --  may be a call to a primitive function declared outside of
4101             --  the synchronized type.
4102
4103             --  If the context is a procedure call, there might still be
4104             --  an overloading between an entry and a primitive procedure
4105             --  declared outside of the synchronized type, called in prefix
4106             --  notation. This is harder to disambiguate because in one case
4107             --  the controlling formal is implicit ???
4108
4109             elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
4110               and then Nkind (Parent (N)) /= N_Indexed_Component
4111               and then Try_Object_Operation (N)
4112             then
4113                return;
4114             end if;
4115          end if;
4116
4117          if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
4118             --  Case of a prefix of a protected type: selector might denote
4119             --  an invisible private component.
4120
4121             Comp := First_Private_Entity (Base_Type (Prefix_Type));
4122             while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
4123                Next_Entity (Comp);
4124             end loop;
4125
4126             if Present (Comp) then
4127                if Is_Single_Concurrent_Object then
4128                   Error_Msg_Node_2 := Entity (Name);
4129                   Error_Msg_NE ("invisible selector& for &", N, Sel);
4130
4131                else
4132                   Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4133                   Error_Msg_NE ("invisible selector& for }", N, Sel);
4134                end if;
4135                return;
4136             end if;
4137          end if;
4138
4139          Set_Is_Overloaded (N, Is_Overloaded (Sel));
4140
4141       else
4142          --  Invalid prefix
4143
4144          Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
4145       end if;
4146
4147       --  If N still has no type, the component is not defined in the prefix
4148
4149       if Etype (N) = Any_Type then
4150
4151          if Is_Single_Concurrent_Object then
4152             Error_Msg_Node_2 := Entity (Name);
4153             Error_Msg_NE ("no selector& for&", N, Sel);
4154
4155             Check_Misspelled_Selector (Type_To_Use, Sel);
4156
4157          elsif Is_Generic_Type (Prefix_Type)
4158            and then Ekind (Prefix_Type) = E_Record_Type_With_Private
4159            and then Prefix_Type /= Etype (Prefix_Type)
4160            and then Is_Record_Type (Etype (Prefix_Type))
4161          then
4162             --  If this is a derived formal type, the parent may have
4163             --  different visibility at this point. Try for an inherited
4164             --  component before reporting an error.
4165
4166             Set_Etype (Prefix (N), Etype (Prefix_Type));
4167             Analyze_Selected_Component (N);
4168             return;
4169
4170          --  Similarly, if this is the actual for a formal derived type, the
4171          --  component inherited from the generic parent may not be visible
4172          --  in the actual, but the selected component is legal.
4173
4174          elsif Ekind (Prefix_Type) = E_Record_Subtype_With_Private
4175            and then Is_Generic_Actual_Type (Prefix_Type)
4176            and then Present (Full_View (Prefix_Type))
4177          then
4178
4179             Find_Component_In_Instance
4180               (Generic_Parent_Type (Parent (Prefix_Type)));
4181             return;
4182
4183          --  Finally, the formal and the actual may be private extensions,
4184          --  but the generic is declared in a child unit of the parent, and
4185          --  an additional step is needed to retrieve the proper scope.
4186
4187          elsif In_Instance
4188            and then Present (Parent_Subtype (Etype (Base_Type (Prefix_Type))))
4189          then
4190             Find_Component_In_Instance
4191               (Parent_Subtype (Etype (Base_Type (Prefix_Type))));
4192             return;
4193
4194          --  Component not found, specialize error message when appropriate
4195
4196          else
4197             if Ekind (Prefix_Type) = E_Record_Subtype then
4198
4199                --  Check whether this is a component of the base type which
4200                --  is absent from a statically constrained subtype. This will
4201                --  raise constraint error at run time, but is not a compile-
4202                --  time error. When the selector is illegal for base type as
4203                --  well fall through and generate a compilation error anyway.
4204
4205                Comp := First_Component (Base_Type (Prefix_Type));
4206                while Present (Comp) loop
4207                   if Chars (Comp) = Chars (Sel)
4208                     and then Is_Visible_Component (Comp)
4209                   then
4210                      Set_Entity_With_Style_Check (Sel, Comp);
4211                      Generate_Reference (Comp, Sel);
4212                      Set_Etype (Sel, Etype (Comp));
4213                      Set_Etype (N,   Etype (Comp));
4214
4215                      --  Emit appropriate message. Gigi will replace the
4216                      --  node subsequently with the appropriate Raise.
4217
4218                      Apply_Compile_Time_Constraint_Error
4219                        (N, "component not present in }?",
4220                         CE_Discriminant_Check_Failed,
4221                         Ent => Prefix_Type, Rep => False);
4222                      Set_Raises_Constraint_Error (N);
4223                      return;
4224                   end if;
4225
4226                   Next_Component (Comp);
4227                end loop;
4228
4229             end if;
4230
4231             Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4232             Error_Msg_NE ("no selector& for}", N, Sel);
4233
4234             Check_Misspelled_Selector (Type_To_Use, Sel);
4235          end if;
4236
4237          Set_Entity (Sel, Any_Id);
4238          Set_Etype (Sel, Any_Type);
4239       end if;
4240    end Analyze_Selected_Component;
4241
4242    ---------------------------
4243    -- Analyze_Short_Circuit --
4244    ---------------------------
4245
4246    procedure Analyze_Short_Circuit (N : Node_Id) is
4247       L   : constant Node_Id := Left_Opnd  (N);
4248       R   : constant Node_Id := Right_Opnd (N);
4249       Ind : Interp_Index;
4250       It  : Interp;
4251
4252    begin
4253       Analyze_Expression (L);
4254       Analyze_Expression (R);
4255       Set_Etype (N, Any_Type);
4256
4257       if not Is_Overloaded (L) then
4258          if Root_Type (Etype (L)) = Standard_Boolean
4259            and then Has_Compatible_Type (R, Etype (L))
4260          then
4261             Add_One_Interp (N, Etype (L), Etype (L));
4262          end if;
4263
4264       else
4265          Get_First_Interp (L, Ind, It);
4266          while Present (It.Typ) loop
4267             if Root_Type (It.Typ) = Standard_Boolean
4268               and then Has_Compatible_Type (R, It.Typ)
4269             then
4270                Add_One_Interp (N, It.Typ, It.Typ);
4271             end if;
4272
4273             Get_Next_Interp (Ind, It);
4274          end loop;
4275       end if;
4276
4277       --  Here we have failed to find an interpretation. Clearly we know that
4278       --  it is not the case that both operands can have an interpretation of
4279       --  Boolean, but this is by far the most likely intended interpretation.
4280       --  So we simply resolve both operands as Booleans, and at least one of
4281       --  these resolutions will generate an error message, and we do not need
4282       --  to give another error message on the short circuit operation itself.
4283
4284       if Etype (N) = Any_Type then
4285          Resolve (L, Standard_Boolean);
4286          Resolve (R, Standard_Boolean);
4287          Set_Etype (N, Standard_Boolean);
4288       end if;
4289    end Analyze_Short_Circuit;
4290
4291    -------------------
4292    -- Analyze_Slice --
4293    -------------------
4294
4295    procedure Analyze_Slice (N : Node_Id) is
4296       P          : constant Node_Id := Prefix (N);
4297       D          : constant Node_Id := Discrete_Range (N);
4298       Array_Type : Entity_Id;
4299
4300       procedure Analyze_Overloaded_Slice;
4301       --  If the prefix is overloaded, select those interpretations that
4302       --  yield a one-dimensional array type.
4303
4304       ------------------------------
4305       -- Analyze_Overloaded_Slice --
4306       ------------------------------
4307
4308       procedure Analyze_Overloaded_Slice is
4309          I   : Interp_Index;
4310          It  : Interp;
4311          Typ : Entity_Id;
4312
4313       begin
4314          Set_Etype (N, Any_Type);
4315
4316          Get_First_Interp (P, I, It);
4317          while Present (It.Nam) loop
4318             Typ := It.Typ;
4319
4320             if Is_Access_Type (Typ) then
4321                Typ := Designated_Type (Typ);
4322                Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
4323             end if;
4324
4325             if Is_Array_Type (Typ)
4326               and then Number_Dimensions (Typ) = 1
4327               and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
4328             then
4329                Add_One_Interp (N, Typ, Typ);
4330             end if;
4331
4332             Get_Next_Interp (I, It);
4333          end loop;
4334
4335          if Etype (N) = Any_Type then
4336             Error_Msg_N ("expect array type in prefix of slice",  N);
4337          end if;
4338       end Analyze_Overloaded_Slice;
4339
4340    --  Start of processing for Analyze_Slice
4341
4342    begin
4343       Mark_Non_ALFA_Subprogram;
4344       Check_SPARK_Restriction ("slice is not allowed", N);
4345
4346       Analyze (P);
4347       Analyze (D);
4348
4349       if Is_Overloaded (P) then
4350          Analyze_Overloaded_Slice;
4351
4352       else
4353          Array_Type := Etype (P);
4354          Set_Etype (N, Any_Type);
4355
4356          if Is_Access_Type (Array_Type) then
4357             Array_Type := Designated_Type (Array_Type);
4358             Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
4359          end if;
4360
4361          if not Is_Array_Type (Array_Type) then
4362             Wrong_Type (P, Any_Array);
4363
4364          elsif Number_Dimensions (Array_Type) > 1 then
4365             Error_Msg_N
4366               ("type is not one-dimensional array in slice prefix", N);
4367
4368          elsif not
4369            Has_Compatible_Type (D, Etype (First_Index (Array_Type)))
4370          then
4371             Wrong_Type (D, Etype (First_Index (Array_Type)));
4372
4373          else
4374             Set_Etype (N, Array_Type);
4375          end if;
4376       end if;
4377    end Analyze_Slice;
4378
4379    -----------------------------
4380    -- Analyze_Type_Conversion --
4381    -----------------------------
4382
4383    procedure Analyze_Type_Conversion (N : Node_Id) is
4384       Expr : constant Node_Id := Expression (N);
4385       T    : Entity_Id;
4386
4387    begin
4388       --  If Conversion_OK is set, then the Etype is already set, and the
4389       --  only processing required is to analyze the expression. This is
4390       --  used to construct certain "illegal" conversions which are not
4391       --  allowed by Ada semantics, but can be handled OK by Gigi, see
4392       --  Sinfo for further details.
4393
4394       if Conversion_OK (N) then
4395          Analyze (Expr);
4396          return;
4397       end if;
4398
4399       --  Otherwise full type analysis is required, as well as some semantic
4400       --  checks to make sure the argument of the conversion is appropriate.
4401
4402       Find_Type (Subtype_Mark (N));
4403       T := Entity (Subtype_Mark (N));
4404       Set_Etype (N, T);
4405       Check_Fully_Declared (T, N);
4406       Analyze_Expression (Expr);
4407       Validate_Remote_Type_Type_Conversion (N);
4408
4409       --  Type conversion between scalar types are allowed in ALFA. All other
4410       --  type conversions are not allowed.
4411
4412       if not (Is_Scalar_Type (Etype (Expr)) and then Is_Scalar_Type (T)) then
4413          Mark_Non_ALFA_Subprogram;
4414       end if;
4415
4416       --  Only remaining step is validity checks on the argument. These
4417       --  are skipped if the conversion does not come from the source.
4418
4419       if not Comes_From_Source (N) then
4420          return;
4421
4422       --  If there was an error in a generic unit, no need to replicate the
4423       --  error message. Conversely, constant-folding in the generic may
4424       --  transform the argument of a conversion into a string literal, which
4425       --  is legal. Therefore the following tests are not performed in an
4426       --  instance.
4427
4428       elsif In_Instance then
4429          return;
4430
4431       elsif Nkind (Expr) = N_Null then
4432          Error_Msg_N ("argument of conversion cannot be null", N);
4433          Error_Msg_N ("\use qualified expression instead", N);
4434          Set_Etype (N, Any_Type);
4435
4436       elsif Nkind (Expr) = N_Aggregate then
4437          Error_Msg_N ("argument of conversion cannot be aggregate", N);
4438          Error_Msg_N ("\use qualified expression instead", N);
4439
4440       elsif Nkind (Expr) = N_Allocator then
4441          Error_Msg_N ("argument of conversion cannot be an allocator", N);
4442          Error_Msg_N ("\use qualified expression instead", N);
4443
4444       elsif Nkind (Expr) = N_String_Literal then
4445          Error_Msg_N ("argument of conversion cannot be string literal", N);
4446          Error_Msg_N ("\use qualified expression instead", N);
4447
4448       elsif Nkind (Expr) = N_Character_Literal then
4449          if Ada_Version = Ada_83 then
4450             Resolve (Expr, T);
4451          else
4452             Error_Msg_N ("argument of conversion cannot be character literal",
4453               N);
4454             Error_Msg_N ("\use qualified expression instead", N);
4455          end if;
4456
4457       elsif Nkind (Expr) = N_Attribute_Reference
4458         and then
4459           (Attribute_Name (Expr) = Name_Access            or else
4460            Attribute_Name (Expr) = Name_Unchecked_Access  or else
4461            Attribute_Name (Expr) = Name_Unrestricted_Access)
4462       then
4463          Error_Msg_N ("argument of conversion cannot be access", N);
4464          Error_Msg_N ("\use qualified expression instead", N);
4465       end if;
4466    end Analyze_Type_Conversion;
4467
4468    ----------------------
4469    -- Analyze_Unary_Op --
4470    ----------------------
4471
4472    procedure Analyze_Unary_Op (N : Node_Id) is
4473       R     : constant Node_Id := Right_Opnd (N);
4474       Op_Id : Entity_Id := Entity (N);
4475
4476    begin
4477       Set_Etype (N, Any_Type);
4478       Candidate_Type := Empty;
4479
4480       Analyze_Expression (R);
4481
4482       if Present (Op_Id) then
4483          if Ekind (Op_Id) = E_Operator then
4484             Find_Unary_Types (R, Op_Id,  N);
4485          else
4486             Add_One_Interp (N, Op_Id, Etype (Op_Id));
4487          end if;
4488
4489       else
4490          Op_Id := Get_Name_Entity_Id (Chars (N));
4491          while Present (Op_Id) loop
4492             if Ekind (Op_Id) = E_Operator then
4493                if No (Next_Entity (First_Entity (Op_Id))) then
4494                   Find_Unary_Types (R, Op_Id,  N);
4495                end if;
4496
4497             elsif Is_Overloadable (Op_Id) then
4498                Analyze_User_Defined_Unary_Op (N, Op_Id);
4499             end if;
4500
4501             Op_Id := Homonym (Op_Id);
4502          end loop;
4503       end if;
4504
4505       Operator_Check (N);
4506    end Analyze_Unary_Op;
4507
4508    ----------------------------------
4509    -- Analyze_Unchecked_Expression --
4510    ----------------------------------
4511
4512    procedure Analyze_Unchecked_Expression (N : Node_Id) is
4513    begin
4514       Analyze (Expression (N), Suppress => All_Checks);
4515       Set_Etype (N, Etype (Expression (N)));
4516       Save_Interps (Expression (N), N);
4517    end Analyze_Unchecked_Expression;
4518
4519    ---------------------------------------
4520    -- Analyze_Unchecked_Type_Conversion --
4521    ---------------------------------------
4522
4523    procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
4524    begin
4525       Mark_Non_ALFA_Subprogram;
4526       Find_Type (Subtype_Mark (N));
4527       Analyze_Expression (Expression (N));
4528       Set_Etype (N, Entity (Subtype_Mark (N)));
4529    end Analyze_Unchecked_Type_Conversion;
4530
4531    ------------------------------------
4532    -- Analyze_User_Defined_Binary_Op --
4533    ------------------------------------
4534
4535    procedure Analyze_User_Defined_Binary_Op
4536      (N     : Node_Id;
4537       Op_Id : Entity_Id)
4538    is
4539    begin
4540       --  Only do analysis if the operator Comes_From_Source, since otherwise
4541       --  the operator was generated by the expander, and all such operators
4542       --  always refer to the operators in package Standard.
4543
4544       if Comes_From_Source (N) then
4545          declare
4546             F1 : constant Entity_Id := First_Formal (Op_Id);
4547             F2 : constant Entity_Id := Next_Formal (F1);
4548
4549          begin
4550             --  Verify that Op_Id is a visible binary function. Note that since
4551             --  we know Op_Id is overloaded, potentially use visible means use
4552             --  visible for sure (RM 9.4(11)).
4553
4554             if Ekind (Op_Id) = E_Function
4555               and then Present (F2)
4556               and then (Is_Immediately_Visible (Op_Id)
4557                          or else Is_Potentially_Use_Visible (Op_Id))
4558               and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
4559               and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
4560             then
4561                Add_One_Interp (N, Op_Id, Etype (Op_Id));
4562
4563                --  If the left operand is overloaded, indicate that the
4564                --  current type is a viable candidate. This is redundant
4565                --  in most cases, but for equality and comparison operators
4566                --  where the context does not impose a type on the operands,
4567                --  setting the proper type is necessary to avoid subsequent
4568                --  ambiguities during resolution, when both user-defined and
4569                --  predefined operators may be candidates.
4570
4571                if Is_Overloaded (Left_Opnd (N)) then
4572                   Set_Etype (Left_Opnd (N), Etype (F1));
4573                end if;
4574
4575                if Debug_Flag_E then
4576                   Write_Str ("user defined operator ");
4577                   Write_Name (Chars (Op_Id));
4578                   Write_Str (" on node ");
4579                   Write_Int (Int (N));
4580                   Write_Eol;
4581                end if;
4582             end if;
4583          end;
4584       end if;
4585    end Analyze_User_Defined_Binary_Op;
4586
4587    -----------------------------------
4588    -- Analyze_User_Defined_Unary_Op --
4589    -----------------------------------
4590
4591    procedure Analyze_User_Defined_Unary_Op
4592      (N     : Node_Id;
4593       Op_Id : Entity_Id)
4594    is
4595    begin
4596       --  Only do analysis if the operator Comes_From_Source, since otherwise
4597       --  the operator was generated by the expander, and all such operators
4598       --  always refer to the operators in package Standard.
4599
4600       if Comes_From_Source (N) then
4601          declare
4602             F : constant Entity_Id := First_Formal (Op_Id);
4603
4604          begin
4605             --  Verify that Op_Id is a visible unary function. Note that since
4606             --  we know Op_Id is overloaded, potentially use visible means use
4607             --  visible for sure (RM 9.4(11)).
4608
4609             if Ekind (Op_Id) = E_Function
4610               and then No (Next_Formal (F))
4611               and then (Is_Immediately_Visible (Op_Id)
4612                          or else Is_Potentially_Use_Visible (Op_Id))
4613               and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
4614             then
4615                Add_One_Interp (N, Op_Id, Etype (Op_Id));
4616             end if;
4617          end;
4618       end if;
4619    end Analyze_User_Defined_Unary_Op;
4620
4621    ---------------------------
4622    -- Check_Arithmetic_Pair --
4623    ---------------------------
4624
4625    procedure Check_Arithmetic_Pair
4626      (T1, T2 : Entity_Id;
4627       Op_Id  : Entity_Id;
4628       N      : Node_Id)
4629    is
4630       Op_Name : constant Name_Id := Chars (Op_Id);
4631
4632       function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
4633       --  Check whether the fixed-point type Typ has a user-defined operator
4634       --  (multiplication or division) that should hide the corresponding
4635       --  predefined operator. Used to implement Ada 2005 AI-264, to make
4636       --  such operators more visible and therefore useful.
4637
4638       --  If the name of the operation is an expanded name with prefix
4639       --  Standard, the predefined universal fixed operator is available,
4640       --  as specified by AI-420 (RM 4.5.5 (19.1/2)).
4641
4642       function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
4643       --  Get specific type (i.e. non-universal type if there is one)
4644
4645       ------------------
4646       -- Has_Fixed_Op --
4647       ------------------
4648
4649       function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
4650          Bas : constant Entity_Id := Base_Type (Typ);
4651          Ent : Entity_Id;
4652          F1  : Entity_Id;
4653          F2  : Entity_Id;
4654
4655       begin
4656          --  If the universal_fixed operation is given explicitly the rule
4657          --  concerning primitive operations of the type do not apply.
4658
4659          if Nkind (N) = N_Function_Call
4660            and then Nkind (Name (N)) = N_Expanded_Name
4661            and then Entity (Prefix (Name (N))) = Standard_Standard
4662          then
4663             return False;
4664          end if;
4665
4666          --  The operation is treated as primitive if it is declared in the
4667          --  same scope as the type, and therefore on the same entity chain.
4668
4669          Ent := Next_Entity (Typ);
4670          while Present (Ent) loop
4671             if Chars (Ent) = Chars (Op) then
4672                F1 := First_Formal (Ent);
4673                F2 := Next_Formal (F1);
4674
4675                --  The operation counts as primitive if either operand or
4676                --  result are of the given base type, and both operands are
4677                --  fixed point types.
4678
4679                if (Base_Type (Etype (F1)) = Bas
4680                     and then Is_Fixed_Point_Type (Etype (F2)))
4681
4682                  or else
4683                    (Base_Type (Etype (F2)) = Bas
4684                      and then Is_Fixed_Point_Type (Etype (F1)))
4685
4686                  or else
4687                    (Base_Type (Etype (Ent)) = Bas
4688                      and then Is_Fixed_Point_Type (Etype (F1))
4689                      and then Is_Fixed_Point_Type (Etype (F2)))
4690                then
4691                   return True;
4692                end if;
4693             end if;
4694
4695             Next_Entity (Ent);
4696          end loop;
4697
4698          return False;
4699       end Has_Fixed_Op;
4700
4701       -------------------
4702       -- Specific_Type --
4703       -------------------
4704
4705       function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
4706       begin
4707          if T1 = Universal_Integer or else T1 = Universal_Real then
4708             return Base_Type (T2);
4709          else
4710             return Base_Type (T1);
4711          end if;
4712       end Specific_Type;
4713
4714    --  Start of processing for Check_Arithmetic_Pair
4715
4716    begin
4717       if Op_Name = Name_Op_Add or else Op_Name = Name_Op_Subtract then
4718
4719          if Is_Numeric_Type (T1)
4720            and then Is_Numeric_Type (T2)
4721            and then (Covers (T1 => T1, T2 => T2)
4722                        or else
4723                      Covers (T1 => T2, T2 => T1))
4724          then
4725             Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4726          end if;
4727
4728       elsif Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide then
4729
4730          if Is_Fixed_Point_Type (T1)
4731            and then (Is_Fixed_Point_Type (T2)
4732                        or else T2 = Universal_Real)
4733          then
4734             --  If Treat_Fixed_As_Integer is set then the Etype is already set
4735             --  and no further processing is required (this is the case of an
4736             --  operator constructed by Exp_Fixd for a fixed point operation)
4737             --  Otherwise add one interpretation with universal fixed result
4738             --  If the operator is given in  functional notation, it comes
4739             --  from source and Fixed_As_Integer cannot apply.
4740
4741             if (Nkind (N) not in N_Op
4742                  or else not Treat_Fixed_As_Integer (N))
4743               and then
4744                 (not Has_Fixed_Op (T1, Op_Id)
4745                   or else Nkind (Parent (N)) = N_Type_Conversion)
4746             then
4747                Add_One_Interp (N, Op_Id, Universal_Fixed);
4748             end if;
4749
4750          elsif Is_Fixed_Point_Type (T2)
4751            and then (Nkind (N) not in N_Op
4752                       or else not Treat_Fixed_As_Integer (N))
4753            and then T1 = Universal_Real
4754            and then
4755              (not Has_Fixed_Op (T1, Op_Id)
4756                or else Nkind (Parent (N)) = N_Type_Conversion)
4757          then
4758             Add_One_Interp (N, Op_Id, Universal_Fixed);
4759
4760          elsif Is_Numeric_Type (T1)
4761            and then Is_Numeric_Type (T2)
4762            and then (Covers (T1 => T1, T2 => T2)
4763                        or else
4764                      Covers (T1 => T2, T2 => T1))
4765          then
4766             Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4767
4768          elsif Is_Fixed_Point_Type (T1)
4769            and then (Base_Type (T2) = Base_Type (Standard_Integer)
4770                        or else T2 = Universal_Integer)
4771          then
4772             Add_One_Interp (N, Op_Id, T1);
4773
4774          elsif T2 = Universal_Real
4775            and then Base_Type (T1) = Base_Type (Standard_Integer)
4776            and then Op_Name = Name_Op_Multiply
4777          then
4778             Add_One_Interp (N, Op_Id, Any_Fixed);
4779
4780          elsif T1 = Universal_Real
4781            and then Base_Type (T2) = Base_Type (Standard_Integer)
4782          then
4783             Add_One_Interp (N, Op_Id, Any_Fixed);
4784
4785          elsif Is_Fixed_Point_Type (T2)
4786            and then (Base_Type (T1) = Base_Type (Standard_Integer)
4787                        or else T1 = Universal_Integer)
4788            and then Op_Name = Name_Op_Multiply
4789          then
4790             Add_One_Interp (N, Op_Id, T2);
4791
4792          elsif T1 = Universal_Real and then T2 = Universal_Integer then
4793             Add_One_Interp (N, Op_Id, T1);
4794
4795          elsif T2 = Universal_Real
4796            and then T1 = Universal_Integer
4797            and then Op_Name = Name_Op_Multiply
4798          then
4799             Add_One_Interp (N, Op_Id, T2);
4800          end if;
4801
4802       elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
4803
4804          --  Note: The fixed-point operands case with Treat_Fixed_As_Integer
4805          --  set does not require any special processing, since the Etype is
4806          --  already set (case of operation constructed by Exp_Fixed).
4807
4808          if Is_Integer_Type (T1)
4809            and then (Covers (T1 => T1, T2 => T2)
4810                        or else
4811                      Covers (T1 => T2, T2 => T1))
4812          then
4813             Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4814          end if;
4815
4816       elsif Op_Name = Name_Op_Expon then
4817          if Is_Numeric_Type (T1)
4818            and then not Is_Fixed_Point_Type (T1)
4819            and then (Base_Type (T2) = Base_Type (Standard_Integer)
4820                       or else T2 = Universal_Integer)
4821          then
4822             Add_One_Interp (N, Op_Id, Base_Type (T1));
4823          end if;
4824
4825       else pragma Assert (Nkind (N) in N_Op_Shift);
4826
4827          --  If not one of the predefined operators, the node may be one
4828          --  of the intrinsic functions. Its kind is always specific, and
4829          --  we can use it directly, rather than the name of the operation.
4830
4831          if Is_Integer_Type (T1)
4832            and then (Base_Type (T2) = Base_Type (Standard_Integer)
4833                       or else T2 = Universal_Integer)
4834          then
4835             Add_One_Interp (N, Op_Id, Base_Type (T1));
4836          end if;
4837       end if;
4838    end Check_Arithmetic_Pair;
4839
4840    -------------------------------
4841    -- Check_Misspelled_Selector --
4842    -------------------------------
4843
4844    procedure Check_Misspelled_Selector
4845      (Prefix : Entity_Id;
4846       Sel    : Node_Id)
4847    is
4848       Max_Suggestions   : constant := 2;
4849       Nr_Of_Suggestions : Natural := 0;
4850
4851       Suggestion_1 : Entity_Id := Empty;
4852       Suggestion_2 : Entity_Id := Empty;
4853
4854       Comp : Entity_Id;
4855
4856    begin
4857       --  All the components of the prefix of selector Sel are matched
4858       --  against  Sel and a count is maintained of possible misspellings.
4859       --  When at the end of the analysis there are one or two (not more!)
4860       --  possible misspellings, these misspellings will be suggested as
4861       --  possible correction.
4862
4863       if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
4864
4865          --  Concurrent types should be handled as well ???
4866
4867          return;
4868       end if;
4869
4870       Comp  := First_Entity (Prefix);
4871       while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
4872          if Is_Visible_Component (Comp) then
4873             if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
4874                Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
4875
4876                case Nr_Of_Suggestions is
4877                   when 1      => Suggestion_1 := Comp;
4878                   when 2      => Suggestion_2 := Comp;
4879                   when others => exit;
4880                end case;
4881             end if;
4882          end if;
4883
4884          Comp := Next_Entity (Comp);
4885       end loop;
4886
4887       --  Report at most two suggestions
4888
4889       if Nr_Of_Suggestions = 1 then
4890          Error_Msg_NE -- CODEFIX
4891            ("\possible misspelling of&", Sel, Suggestion_1);
4892
4893       elsif Nr_Of_Suggestions = 2 then
4894          Error_Msg_Node_2 := Suggestion_2;
4895          Error_Msg_NE -- CODEFIX
4896            ("\possible misspelling of& or&", Sel, Suggestion_1);
4897       end if;
4898    end Check_Misspelled_Selector;
4899
4900    ----------------------
4901    -- Defined_In_Scope --
4902    ----------------------
4903
4904    function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
4905    is
4906       S1 : constant Entity_Id := Scope (Base_Type (T));
4907    begin
4908       return S1 = S
4909         or else (S1 = System_Aux_Id and then S = Scope (S1));
4910    end Defined_In_Scope;
4911
4912    -------------------
4913    -- Diagnose_Call --
4914    -------------------
4915
4916    procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
4917       Actual           : Node_Id;
4918       X                : Interp_Index;
4919       It               : Interp;
4920       Err_Mode         : Boolean;
4921       New_Nam          : Node_Id;
4922       Void_Interp_Seen : Boolean := False;
4923
4924       Success : Boolean;
4925       pragma Warnings (Off, Boolean);
4926
4927    begin
4928       if Ada_Version >= Ada_2005 then
4929          Actual := First_Actual (N);
4930          while Present (Actual) loop
4931
4932             --  Ada 2005 (AI-50217): Post an error in case of premature
4933             --  usage of an entity from the limited view.
4934
4935             if not Analyzed (Etype (Actual))
4936              and then From_With_Type (Etype (Actual))
4937             then
4938                Error_Msg_Qual_Level := 1;
4939                Error_Msg_NE
4940                 ("missing with_clause for scope of imported type&",
4941                   Actual, Etype (Actual));
4942                Error_Msg_Qual_Level := 0;
4943             end if;
4944
4945             Next_Actual (Actual);
4946          end loop;
4947       end if;
4948
4949       --   Analyze each candidate call again, with full error reporting
4950       --   for each.
4951
4952       Error_Msg_N
4953         ("no candidate interpretations match the actuals:!", Nam);
4954       Err_Mode := All_Errors_Mode;
4955       All_Errors_Mode := True;
4956
4957       --  If this is a call to an operation of a concurrent type,
4958       --  the failed interpretations have been removed from the
4959       --  name. Recover them to provide full diagnostics.
4960
4961       if Nkind (Parent (Nam)) = N_Selected_Component then
4962          Set_Entity (Nam, Empty);
4963          New_Nam := New_Copy_Tree (Parent (Nam));
4964          Set_Is_Overloaded (New_Nam, False);
4965          Set_Is_Overloaded (Selector_Name (New_Nam), False);
4966          Set_Parent (New_Nam, Parent (Parent (Nam)));
4967          Analyze_Selected_Component (New_Nam);
4968          Get_First_Interp (Selector_Name (New_Nam), X, It);
4969       else
4970          Get_First_Interp (Nam, X, It);
4971       end if;
4972
4973       while Present (It.Nam) loop
4974          if Etype (It.Nam) = Standard_Void_Type then
4975             Void_Interp_Seen := True;
4976          end if;
4977
4978          Analyze_One_Call (N, It.Nam, True, Success);
4979          Get_Next_Interp (X, It);
4980       end loop;
4981
4982       if Nkind (N) = N_Function_Call then
4983          Get_First_Interp (Nam, X, It);
4984          while Present (It.Nam) loop
4985             if Ekind_In (It.Nam, E_Function, E_Operator) then
4986                return;
4987             else
4988                Get_Next_Interp (X, It);
4989             end if;
4990          end loop;
4991
4992          --  If all interpretations are procedures, this deserves a
4993          --  more precise message. Ditto if this appears as the prefix
4994          --  of a selected component, which may be a lexical error.
4995
4996          Error_Msg_N
4997            ("\context requires function call, found procedure name", Nam);
4998
4999          if Nkind (Parent (N)) = N_Selected_Component
5000            and then N = Prefix (Parent (N))
5001          then
5002             Error_Msg_N -- CODEFIX
5003               ("\period should probably be semicolon", Parent (N));
5004          end if;
5005
5006       elsif Nkind (N) = N_Procedure_Call_Statement
5007         and then not Void_Interp_Seen
5008       then
5009          Error_Msg_N (
5010          "\function name found in procedure call", Nam);
5011       end if;
5012
5013       All_Errors_Mode := Err_Mode;
5014    end Diagnose_Call;
5015
5016    ---------------------------
5017    -- Find_Arithmetic_Types --
5018    ---------------------------
5019
5020    procedure Find_Arithmetic_Types
5021      (L, R  : Node_Id;
5022       Op_Id : Entity_Id;
5023       N     : Node_Id)
5024    is
5025       Index1 : Interp_Index;
5026       Index2 : Interp_Index;
5027       It1    : Interp;
5028       It2    : Interp;
5029
5030       procedure Check_Right_Argument (T : Entity_Id);
5031       --  Check right operand of operator
5032
5033       --------------------------
5034       -- Check_Right_Argument --
5035       --------------------------
5036
5037       procedure Check_Right_Argument (T : Entity_Id) is
5038       begin
5039          if not Is_Overloaded (R) then
5040             Check_Arithmetic_Pair (T, Etype (R), Op_Id,  N);
5041          else
5042             Get_First_Interp (R, Index2, It2);
5043             while Present (It2.Typ) loop
5044                Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
5045                Get_Next_Interp (Index2, It2);
5046             end loop;
5047          end if;
5048       end Check_Right_Argument;
5049
5050    --  Start of processing for Find_Arithmetic_Types
5051
5052    begin
5053       if not Is_Overloaded (L) then
5054          Check_Right_Argument (Etype (L));
5055
5056       else
5057          Get_First_Interp (L, Index1, It1);
5058          while Present (It1.Typ) loop
5059             Check_Right_Argument (It1.Typ);
5060             Get_Next_Interp (Index1, It1);
5061          end loop;
5062       end if;
5063
5064    end Find_Arithmetic_Types;
5065
5066    ------------------------
5067    -- Find_Boolean_Types --
5068    ------------------------
5069
5070    procedure Find_Boolean_Types
5071      (L, R  : Node_Id;
5072       Op_Id : Entity_Id;
5073       N     : Node_Id)
5074    is
5075       Index : Interp_Index;
5076       It    : Interp;
5077
5078       procedure Check_Numeric_Argument (T : Entity_Id);
5079       --  Special case for logical operations one of whose operands is an
5080       --  integer literal. If both are literal the result is any modular type.
5081
5082       ----------------------------
5083       -- Check_Numeric_Argument --
5084       ----------------------------
5085
5086       procedure Check_Numeric_Argument (T : Entity_Id) is
5087       begin
5088          if T = Universal_Integer then
5089             Add_One_Interp (N, Op_Id, Any_Modular);
5090
5091          elsif Is_Modular_Integer_Type (T) then
5092             Add_One_Interp (N, Op_Id, T);
5093          end if;
5094       end Check_Numeric_Argument;
5095
5096    --  Start of processing for Find_Boolean_Types
5097
5098    begin
5099       if not Is_Overloaded (L) then
5100          if Etype (L) = Universal_Integer
5101            or else Etype (L) = Any_Modular
5102          then
5103             if not Is_Overloaded (R) then
5104                Check_Numeric_Argument (Etype (R));
5105
5106             else
5107                Get_First_Interp (R, Index, It);
5108                while Present (It.Typ) loop
5109                   Check_Numeric_Argument (It.Typ);
5110                   Get_Next_Interp (Index, It);
5111                end loop;
5112             end if;
5113
5114          --  If operands are aggregates, we must assume that they may be
5115          --  boolean arrays, and leave disambiguation for the second pass.
5116          --  If only one is an aggregate, verify that the other one has an
5117          --  interpretation as a boolean array
5118
5119          elsif Nkind (L) = N_Aggregate then
5120             if Nkind (R) = N_Aggregate then
5121                Add_One_Interp (N, Op_Id, Etype (L));
5122
5123             elsif not Is_Overloaded (R) then
5124                if Valid_Boolean_Arg (Etype (R)) then
5125                   Add_One_Interp (N, Op_Id, Etype (R));
5126                end if;
5127
5128             else
5129                Get_First_Interp (R, Index, It);
5130                while Present (It.Typ) loop
5131                   if Valid_Boolean_Arg (It.Typ) then
5132                      Add_One_Interp (N, Op_Id, It.Typ);
5133                   end if;
5134
5135                   Get_Next_Interp (Index, It);
5136                end loop;
5137             end if;
5138
5139          elsif Valid_Boolean_Arg (Etype (L))
5140            and then Has_Compatible_Type (R, Etype (L))
5141          then
5142             Add_One_Interp (N, Op_Id, Etype (L));
5143          end if;
5144
5145       else
5146          Get_First_Interp (L, Index, It);
5147          while Present (It.Typ) loop
5148             if Valid_Boolean_Arg (It.Typ)
5149               and then Has_Compatible_Type (R, It.Typ)
5150             then
5151                Add_One_Interp (N, Op_Id, It.Typ);
5152             end if;
5153
5154             Get_Next_Interp (Index, It);
5155          end loop;
5156       end if;
5157    end Find_Boolean_Types;
5158
5159    ---------------------------
5160    -- Find_Comparison_Types --
5161    ---------------------------
5162
5163    procedure Find_Comparison_Types
5164      (L, R  : Node_Id;
5165       Op_Id : Entity_Id;
5166       N     : Node_Id)
5167    is
5168       Index : Interp_Index;
5169       It    : Interp;
5170       Found : Boolean := False;
5171       I_F   : Interp_Index;
5172       T_F   : Entity_Id;
5173       Scop  : Entity_Id := Empty;
5174
5175       procedure Try_One_Interp (T1 : Entity_Id);
5176       --  Routine to try one proposed interpretation. Note that the context
5177       --  of the operator plays no role in resolving the arguments, so that
5178       --  if there is more than one interpretation of the operands that is
5179       --  compatible with comparison, the operation is ambiguous.
5180
5181       --------------------
5182       -- Try_One_Interp --
5183       --------------------
5184
5185       procedure Try_One_Interp (T1 : Entity_Id) is
5186       begin
5187
5188          --  If the operator is an expanded name, then the type of the operand
5189          --  must be defined in the corresponding scope. If the type is
5190          --  universal, the context will impose the correct type.
5191
5192          if Present (Scop)
5193             and then not Defined_In_Scope (T1, Scop)
5194             and then T1 /= Universal_Integer
5195             and then T1 /= Universal_Real
5196             and then T1 /= Any_String
5197             and then T1 /= Any_Composite
5198          then
5199             return;
5200          end if;
5201
5202          if Valid_Comparison_Arg (T1)
5203            and then Has_Compatible_Type (R, T1)
5204          then
5205             if Found
5206               and then Base_Type (T1) /= Base_Type (T_F)
5207             then
5208                It := Disambiguate (L, I_F, Index, Any_Type);
5209
5210                if It = No_Interp then
5211                   Ambiguous_Operands (N);
5212                   Set_Etype (L, Any_Type);
5213                   return;
5214
5215                else
5216                   T_F := It.Typ;
5217                end if;
5218
5219             else
5220                Found := True;
5221                T_F   := T1;
5222                I_F   := Index;
5223             end if;
5224
5225             Set_Etype (L, T_F);
5226             Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
5227
5228          end if;
5229       end Try_One_Interp;
5230
5231    --  Start of processing for Find_Comparison_Types
5232
5233    begin
5234       --  If left operand is aggregate, the right operand has to
5235       --  provide a usable type for it.
5236
5237       if Nkind (L) = N_Aggregate
5238         and then Nkind (R) /= N_Aggregate
5239       then
5240          Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N);
5241          return;
5242       end if;
5243
5244       if Nkind (N) = N_Function_Call
5245          and then Nkind (Name (N)) = N_Expanded_Name
5246       then
5247          Scop := Entity (Prefix (Name (N)));
5248
5249          --  The prefix may be a package renaming, and the subsequent test
5250          --  requires the original package.
5251
5252          if Ekind (Scop) = E_Package
5253            and then Present (Renamed_Entity (Scop))
5254          then
5255             Scop := Renamed_Entity (Scop);
5256             Set_Entity (Prefix (Name (N)), Scop);
5257          end if;
5258       end if;
5259
5260       if not Is_Overloaded (L) then
5261          Try_One_Interp (Etype (L));
5262
5263       else
5264          Get_First_Interp (L, Index, It);
5265          while Present (It.Typ) loop
5266             Try_One_Interp (It.Typ);
5267             Get_Next_Interp (Index, It);
5268          end loop;
5269       end if;
5270    end Find_Comparison_Types;
5271
5272    ----------------------------------------
5273    -- Find_Non_Universal_Interpretations --
5274    ----------------------------------------
5275
5276    procedure Find_Non_Universal_Interpretations
5277      (N     : Node_Id;
5278       R     : Node_Id;
5279       Op_Id : Entity_Id;
5280       T1    : Entity_Id)
5281    is
5282       Index : Interp_Index;
5283       It    : Interp;
5284
5285    begin
5286       if T1 = Universal_Integer
5287         or else T1 = Universal_Real
5288       then
5289          if not Is_Overloaded (R) then
5290             Add_One_Interp
5291               (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
5292          else
5293             Get_First_Interp (R, Index, It);
5294             while Present (It.Typ) loop
5295                if Covers (It.Typ, T1) then
5296                   Add_One_Interp
5297                     (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
5298                end if;
5299
5300                Get_Next_Interp (Index, It);
5301             end loop;
5302          end if;
5303       else
5304          Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
5305       end if;
5306    end Find_Non_Universal_Interpretations;
5307
5308    ------------------------------
5309    -- Find_Concatenation_Types --
5310    ------------------------------
5311
5312    procedure Find_Concatenation_Types
5313      (L, R  : Node_Id;
5314       Op_Id : Entity_Id;
5315       N     : Node_Id)
5316    is
5317       Op_Type : constant Entity_Id := Etype (Op_Id);
5318
5319    begin
5320       if Is_Array_Type (Op_Type)
5321         and then not Is_Limited_Type (Op_Type)
5322
5323         and then (Has_Compatible_Type (L, Op_Type)
5324                     or else
5325                   Has_Compatible_Type (L, Component_Type (Op_Type)))
5326
5327         and then (Has_Compatible_Type (R, Op_Type)
5328                     or else
5329                   Has_Compatible_Type (R, Component_Type (Op_Type)))
5330       then
5331          Add_One_Interp (N, Op_Id, Op_Type);
5332       end if;
5333    end Find_Concatenation_Types;
5334
5335    -------------------------
5336    -- Find_Equality_Types --
5337    -------------------------
5338
5339    procedure Find_Equality_Types
5340      (L, R  : Node_Id;
5341       Op_Id : Entity_Id;
5342       N     : Node_Id)
5343    is
5344       Index : Interp_Index;
5345       It    : Interp;
5346       Found : Boolean := False;
5347       I_F   : Interp_Index;
5348       T_F   : Entity_Id;
5349       Scop  : Entity_Id := Empty;
5350
5351       procedure Try_One_Interp (T1 : Entity_Id);
5352       --  The context of the equality operator plays no role in resolving the
5353       --  arguments, so that if there is more than one interpretation of the
5354       --  operands that is compatible with equality, the construct is ambiguous
5355       --  and an error can be emitted now, after trying to disambiguate, i.e.
5356       --  applying preference rules.
5357
5358       --------------------
5359       -- Try_One_Interp --
5360       --------------------
5361
5362       procedure Try_One_Interp (T1 : Entity_Id) is
5363          Bas : constant Entity_Id := Base_Type (T1);
5364
5365       begin
5366          --  If the operator is an expanded name, then the type of the operand
5367          --  must be defined in the corresponding scope. If the type is
5368          --  universal, the context will impose the correct type. An anonymous
5369          --  type for a 'Access reference is also universal in this sense, as
5370          --  the actual type is obtained from context.
5371          --  In Ada 2005, the equality operator for anonymous access types
5372          --  is declared in Standard, and preference rules apply to it.
5373
5374          if Present (Scop) then
5375             if Defined_In_Scope (T1, Scop)
5376               or else T1 = Universal_Integer
5377               or else T1 = Universal_Real
5378               or else T1 = Any_Access
5379               or else T1 = Any_String
5380               or else T1 = Any_Composite
5381               or else (Ekind (T1) = E_Access_Subprogram_Type
5382                         and then not Comes_From_Source (T1))
5383             then
5384                null;
5385
5386             elsif Ekind (T1) = E_Anonymous_Access_Type
5387               and then Scop = Standard_Standard
5388             then
5389                null;
5390
5391             else
5392                --  The scope does not contain an operator for the type
5393
5394                return;
5395             end if;
5396
5397          --  If we have infix notation, the operator must be usable.
5398          --  Within an instance, if the type is already established we
5399          --  know it is correct.
5400          --  In Ada 2005, the equality on anonymous access types is declared
5401          --  in Standard, and is always visible.
5402
5403          elsif In_Open_Scopes (Scope (Bas))
5404            or else Is_Potentially_Use_Visible (Bas)
5405            or else In_Use (Bas)
5406            or else (In_Use (Scope (Bas))
5407                      and then not Is_Hidden (Bas))
5408            or else (In_Instance
5409                      and then First_Subtype (T1) = First_Subtype (Etype (R)))
5410            or else Ekind (T1) = E_Anonymous_Access_Type
5411          then
5412             null;
5413
5414          else
5415             --  Save candidate type for subsequent error message, if any
5416
5417             if not Is_Limited_Type (T1) then
5418                Candidate_Type := T1;
5419             end if;
5420
5421             return;
5422          end if;
5423
5424          --  Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
5425          --  Do not allow anonymous access types in equality operators.
5426
5427          if Ada_Version < Ada_2005
5428            and then Ekind (T1) = E_Anonymous_Access_Type
5429          then
5430             return;
5431          end if;
5432
5433          if T1 /= Standard_Void_Type
5434            and then not Is_Limited_Type (T1)
5435            and then not Is_Limited_Composite (T1)
5436            and then Has_Compatible_Type (R, T1)
5437          then
5438             if Found
5439               and then Base_Type (T1) /= Base_Type (T_F)
5440             then
5441                It := Disambiguate (L, I_F, Index, Any_Type);
5442
5443                if It = No_Interp then
5444                   Ambiguous_Operands (N);
5445                   Set_Etype (L, Any_Type);
5446                   return;
5447
5448                else
5449                   T_F := It.Typ;
5450                end if;
5451
5452             else
5453                Found := True;
5454                T_F   := T1;
5455                I_F   := Index;
5456             end if;
5457
5458             if not Analyzed (L) then
5459                Set_Etype (L, T_F);
5460             end if;
5461
5462             Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
5463
5464             --  Case of operator was not visible, Etype still set to Any_Type
5465
5466             if Etype (N) = Any_Type then
5467                Found := False;
5468             end if;
5469
5470          elsif Scop = Standard_Standard
5471            and then Ekind (T1) = E_Anonymous_Access_Type
5472          then
5473             Found := True;
5474          end if;
5475       end Try_One_Interp;
5476
5477    --  Start of processing for Find_Equality_Types
5478
5479    begin
5480       --  If left operand is aggregate, the right operand has to
5481       --  provide a usable type for it.
5482
5483       if Nkind (L) = N_Aggregate
5484         and then Nkind (R) /= N_Aggregate
5485       then
5486          Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N);
5487          return;
5488       end if;
5489
5490       if Nkind (N) = N_Function_Call
5491          and then Nkind (Name (N)) = N_Expanded_Name
5492       then
5493          Scop := Entity (Prefix (Name (N)));
5494
5495          --  The prefix may be a package renaming, and the subsequent test
5496          --  requires the original package.
5497
5498          if Ekind (Scop) = E_Package
5499            and then Present (Renamed_Entity (Scop))
5500          then
5501             Scop := Renamed_Entity (Scop);
5502             Set_Entity (Prefix (Name (N)), Scop);
5503          end if;
5504       end if;
5505
5506       if not Is_Overloaded (L) then
5507          Try_One_Interp (Etype (L));
5508
5509       else
5510          Get_First_Interp (L, Index, It);
5511          while Present (It.Typ) loop
5512             Try_One_Interp (It.Typ);
5513             Get_Next_Interp (Index, It);
5514          end loop;
5515       end if;
5516    end Find_Equality_Types;
5517
5518    -------------------------
5519    -- Find_Negation_Types --
5520    -------------------------
5521
5522    procedure Find_Negation_Types
5523      (R     : Node_Id;
5524       Op_Id : Entity_Id;
5525       N     : Node_Id)
5526    is
5527       Index : Interp_Index;
5528       It    : Interp;
5529
5530    begin
5531       if not Is_Overloaded (R) then
5532          if Etype (R) = Universal_Integer then
5533             Add_One_Interp (N, Op_Id, Any_Modular);
5534          elsif Valid_Boolean_Arg (Etype (R)) then
5535             Add_One_Interp (N, Op_Id, Etype (R));
5536          end if;
5537
5538       else
5539          Get_First_Interp (R, Index, It);
5540          while Present (It.Typ) loop
5541             if Valid_Boolean_Arg (It.Typ) then
5542                Add_One_Interp (N, Op_Id, It.Typ);
5543             end if;
5544
5545             Get_Next_Interp (Index, It);
5546          end loop;
5547       end if;
5548    end Find_Negation_Types;
5549
5550    ------------------------------
5551    -- Find_Primitive_Operation --
5552    ------------------------------
5553
5554    function Find_Primitive_Operation (N : Node_Id) return Boolean is
5555       Obj : constant Node_Id := Prefix (N);
5556       Op  : constant Node_Id := Selector_Name (N);
5557
5558       Prim  : Elmt_Id;
5559       Prims : Elist_Id;
5560       Typ   : Entity_Id;
5561
5562    begin
5563       Set_Etype (Op, Any_Type);
5564
5565       if Is_Access_Type (Etype (Obj)) then
5566          Typ := Designated_Type (Etype (Obj));
5567       else
5568          Typ := Etype (Obj);
5569       end if;
5570
5571       if Is_Class_Wide_Type (Typ) then
5572          Typ := Root_Type (Typ);
5573       end if;
5574
5575       Prims := Primitive_Operations (Typ);
5576
5577       Prim := First_Elmt (Prims);
5578       while Present (Prim) loop
5579          if Chars (Node (Prim)) = Chars (Op) then
5580             Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
5581             Set_Etype (N, Etype (Node (Prim)));
5582          end if;
5583
5584          Next_Elmt (Prim);
5585       end loop;
5586
5587       --  Now look for class-wide operations of the type or any of its
5588       --  ancestors by iterating over the homonyms of the selector.
5589
5590       declare
5591          Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
5592          Hom      : Entity_Id;
5593
5594       begin
5595          Hom := Current_Entity (Op);
5596          while Present (Hom) loop
5597             if (Ekind (Hom) = E_Procedure
5598                   or else
5599                 Ekind (Hom) = E_Function)
5600               and then Scope (Hom) = Scope (Typ)
5601               and then Present (First_Formal (Hom))
5602               and then
5603                 (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
5604                   or else
5605                     (Is_Access_Type (Etype (First_Formal (Hom)))
5606                        and then
5607                          Ekind (Etype (First_Formal (Hom))) =
5608                            E_Anonymous_Access_Type
5609                        and then
5610                          Base_Type
5611                            (Designated_Type (Etype (First_Formal (Hom)))) =
5612                                                                 Cls_Type))
5613             then
5614                Add_One_Interp (Op, Hom, Etype (Hom));
5615                Set_Etype (N, Etype (Hom));
5616             end if;
5617
5618             Hom := Homonym (Hom);
5619          end loop;
5620       end;
5621
5622       return Etype (Op) /= Any_Type;
5623    end Find_Primitive_Operation;
5624
5625    ----------------------
5626    -- Find_Unary_Types --
5627    ----------------------
5628
5629    procedure Find_Unary_Types
5630      (R     : Node_Id;
5631       Op_Id : Entity_Id;
5632       N     : Node_Id)
5633    is
5634       Index : Interp_Index;
5635       It    : Interp;
5636
5637    begin
5638       if not Is_Overloaded (R) then
5639          if Is_Numeric_Type (Etype (R)) then
5640             Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
5641          end if;
5642
5643       else
5644          Get_First_Interp (R, Index, It);
5645          while Present (It.Typ) loop
5646             if Is_Numeric_Type (It.Typ) then
5647                Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
5648             end if;
5649
5650             Get_Next_Interp (Index, It);
5651          end loop;
5652       end if;
5653    end Find_Unary_Types;
5654
5655    ------------------
5656    -- Junk_Operand --
5657    ------------------
5658
5659    function Junk_Operand (N : Node_Id) return Boolean is
5660       Enode : Node_Id;
5661
5662    begin
5663       if Error_Posted (N) then
5664          return False;
5665       end if;
5666
5667       --  Get entity to be tested
5668
5669       if Is_Entity_Name (N)
5670         and then Present (Entity (N))
5671       then
5672          Enode := N;
5673
5674       --  An odd case, a procedure name gets converted to a very peculiar
5675       --  function call, and here is where we detect this happening.
5676
5677       elsif Nkind (N) = N_Function_Call
5678         and then Is_Entity_Name (Name (N))
5679         and then Present (Entity (Name (N)))
5680       then
5681          Enode := Name (N);
5682
5683       --  Another odd case, there are at least some cases of selected
5684       --  components where the selected component is not marked as having
5685       --  an entity, even though the selector does have an entity
5686
5687       elsif Nkind (N) = N_Selected_Component
5688         and then Present (Entity (Selector_Name (N)))
5689       then
5690          Enode := Selector_Name (N);
5691
5692       else
5693          return False;
5694       end if;
5695
5696       --  Now test the entity we got to see if it is a bad case
5697
5698       case Ekind (Entity (Enode)) is
5699
5700          when E_Package =>
5701             Error_Msg_N
5702               ("package name cannot be used as operand", Enode);
5703
5704          when Generic_Unit_Kind =>
5705             Error_Msg_N
5706               ("generic unit name cannot be used as operand", Enode);
5707
5708          when Type_Kind =>
5709             Error_Msg_N
5710               ("subtype name cannot be used as operand", Enode);
5711
5712          when Entry_Kind =>
5713             Error_Msg_N
5714               ("entry name cannot be used as operand", Enode);
5715
5716          when E_Procedure =>
5717             Error_Msg_N
5718               ("procedure name cannot be used as operand", Enode);
5719
5720          when E_Exception =>
5721             Error_Msg_N
5722               ("exception name cannot be used as operand", Enode);
5723
5724          when E_Block | E_Label | E_Loop =>
5725             Error_Msg_N
5726               ("label name cannot be used as operand", Enode);
5727
5728          when others =>
5729             return False;
5730
5731       end case;
5732
5733       return True;
5734    end Junk_Operand;
5735
5736    --------------------
5737    -- Operator_Check --
5738    --------------------
5739
5740    procedure Operator_Check (N : Node_Id) is
5741    begin
5742       Remove_Abstract_Operations (N);
5743
5744       --  Test for case of no interpretation found for operator
5745
5746       if Etype (N) = Any_Type then
5747          declare
5748             L     : Node_Id;
5749             R     : Node_Id;
5750             Op_Id : Entity_Id := Empty;
5751
5752          begin
5753             R := Right_Opnd (N);
5754
5755             if Nkind (N) in N_Binary_Op then
5756                L := Left_Opnd (N);
5757             else
5758                L := Empty;
5759             end if;
5760
5761             --  If either operand has no type, then don't complain further,
5762             --  since this simply means that we have a propagated error.
5763
5764             if R = Error
5765               or else Etype (R) = Any_Type
5766               or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
5767             then
5768                return;
5769
5770             --  We explicitly check for the case of concatenation of component
5771             --  with component to avoid reporting spurious matching array types
5772             --  that might happen to be lurking in distant packages (such as
5773             --  run-time packages). This also prevents inconsistencies in the
5774             --  messages for certain ACVC B tests, which can vary depending on
5775             --  types declared in run-time interfaces. Another improvement when
5776             --  aggregates are present is to look for a well-typed operand.
5777
5778             elsif Present (Candidate_Type)
5779               and then (Nkind (N) /= N_Op_Concat
5780                          or else Is_Array_Type (Etype (L))
5781                          or else Is_Array_Type (Etype (R)))
5782             then
5783                if Nkind (N) = N_Op_Concat then
5784                   if Etype (L) /= Any_Composite
5785                     and then Is_Array_Type (Etype (L))
5786                   then
5787                      Candidate_Type := Etype (L);
5788
5789                   elsif Etype (R) /= Any_Composite
5790                     and then Is_Array_Type (Etype (R))
5791                   then
5792                      Candidate_Type := Etype (R);
5793                   end if;
5794                end if;
5795
5796                Error_Msg_NE -- CODEFIX
5797                  ("operator for} is not directly visible!",
5798                   N, First_Subtype (Candidate_Type));
5799
5800                declare
5801                   U : constant Node_Id :=
5802                         Cunit (Get_Source_Unit (Candidate_Type));
5803                begin
5804                   if Unit_Is_Visible (U) then
5805                      Error_Msg_N -- CODEFIX
5806                        ("use clause would make operation legal!",  N);
5807                   else
5808                      Error_Msg_NE  --  CODEFIX
5809                        ("add with_clause and use_clause for&!",
5810                           N, Defining_Entity (Unit (U)));
5811                   end if;
5812                end;
5813                return;
5814
5815             --  If either operand is a junk operand (e.g. package name), then
5816             --  post appropriate error messages, but do not complain further.
5817
5818             --  Note that the use of OR in this test instead of OR ELSE is
5819             --  quite deliberate, we may as well check both operands in the
5820             --  binary operator case.
5821
5822             elsif Junk_Operand (R)
5823               or (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
5824             then
5825                return;
5826
5827             --  If we have a logical operator, one of whose operands is
5828             --  Boolean, then we know that the other operand cannot resolve to
5829             --  Boolean (since we got no interpretations), but in that case we
5830             --  pretty much know that the other operand should be Boolean, so
5831             --  resolve it that way (generating an error)
5832
5833             elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then
5834                if Etype (L) = Standard_Boolean then
5835                   Resolve (R, Standard_Boolean);
5836                   return;
5837                elsif Etype (R) = Standard_Boolean then
5838                   Resolve (L, Standard_Boolean);
5839                   return;
5840                end if;
5841
5842             --  For an arithmetic operator or comparison operator, if one
5843             --  of the operands is numeric, then we know the other operand
5844             --  is not the same numeric type. If it is a non-numeric type,
5845             --  then probably it is intended to match the other operand.
5846
5847             elsif Nkind_In (N, N_Op_Add,
5848                                N_Op_Divide,
5849                                N_Op_Ge,
5850                                N_Op_Gt,
5851                                N_Op_Le)
5852               or else
5853                   Nkind_In (N, N_Op_Lt,
5854                                N_Op_Mod,
5855                                N_Op_Multiply,
5856                                N_Op_Rem,
5857                                N_Op_Subtract)
5858             then
5859                if Is_Numeric_Type (Etype (L))
5860                  and then not Is_Numeric_Type (Etype (R))
5861                then
5862                   Resolve (R, Etype (L));
5863                   return;
5864
5865                elsif Is_Numeric_Type (Etype (R))
5866                  and then not Is_Numeric_Type (Etype (L))
5867                then
5868                   Resolve (L, Etype (R));
5869                   return;
5870                end if;
5871
5872             --  Comparisons on A'Access are common enough to deserve a
5873             --  special message.
5874
5875             elsif Nkind_In (N, N_Op_Eq, N_Op_Ne)
5876                and then Ekind (Etype (L)) = E_Access_Attribute_Type
5877                and then Ekind (Etype (R)) = E_Access_Attribute_Type
5878             then
5879                Error_Msg_N
5880                  ("two access attributes cannot be compared directly", N);
5881                Error_Msg_N
5882                  ("\use qualified expression for one of the operands",
5883                    N);
5884                return;
5885
5886             --  Another one for C programmers
5887
5888             elsif Nkind (N) = N_Op_Concat
5889               and then Valid_Boolean_Arg (Etype (L))
5890               and then Valid_Boolean_Arg (Etype (R))
5891             then
5892                Error_Msg_N ("invalid operands for concatenation", N);
5893                Error_Msg_N -- CODEFIX
5894                  ("\maybe AND was meant", N);
5895                return;
5896
5897             --  A special case for comparison of access parameter with null
5898
5899             elsif Nkind (N) = N_Op_Eq
5900               and then Is_Entity_Name (L)
5901               and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
5902               and then Nkind (Parameter_Type (Parent (Entity (L)))) =
5903                                                   N_Access_Definition
5904               and then Nkind (R) = N_Null
5905             then
5906                Error_Msg_N ("access parameter is not allowed to be null", L);
5907                Error_Msg_N ("\(call would raise Constraint_Error)", L);
5908                return;
5909
5910             --  Another special case for exponentiation, where the right
5911             --  operand must be Natural, independently of the base.
5912
5913             elsif Nkind (N) = N_Op_Expon
5914               and then Is_Numeric_Type (Etype (L))
5915               and then not Is_Overloaded (R)
5916               and then
5917                 First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
5918               and then Base_Type (Etype (R)) /= Universal_Integer
5919             then
5920                Error_Msg_NE
5921                  ("exponent must be of type Natural, found}", R, Etype (R));
5922                return;
5923             end if;
5924
5925             --  If we fall through then just give general message. Note that in
5926             --  the following messages, if the operand is overloaded we choose
5927             --  an arbitrary type to complain about, but that is probably more
5928             --  useful than not giving a type at all.
5929
5930             if Nkind (N) in N_Unary_Op then
5931                Error_Msg_Node_2 := Etype (R);
5932                Error_Msg_N ("operator& not defined for}", N);
5933                return;
5934
5935             else
5936                if Nkind (N) in N_Binary_Op then
5937                   if not Is_Overloaded (L)
5938                     and then not Is_Overloaded (R)
5939                     and then Base_Type (Etype (L)) = Base_Type (Etype (R))
5940                   then
5941                      Error_Msg_Node_2 := First_Subtype (Etype (R));
5942                      Error_Msg_N ("there is no applicable operator& for}", N);
5943
5944                   else
5945                      --  Another attempt to find a fix: one of the candidate
5946                      --  interpretations may not be use-visible. This has
5947                      --  already been checked for predefined operators, so
5948                      --  we examine only user-defined functions.
5949
5950                      Op_Id := Get_Name_Entity_Id (Chars (N));
5951
5952                      while Present (Op_Id) loop
5953                         if Ekind (Op_Id) /= E_Operator
5954                           and then Is_Overloadable (Op_Id)
5955                         then
5956                            if not Is_Immediately_Visible (Op_Id)
5957                              and then not In_Use (Scope (Op_Id))
5958                              and then not Is_Abstract_Subprogram (Op_Id)
5959                              and then not Is_Hidden (Op_Id)
5960                              and then Ekind (Scope (Op_Id)) = E_Package
5961                              and then
5962                                Has_Compatible_Type
5963                                  (L, Etype (First_Formal (Op_Id)))
5964                              and then Present
5965                               (Next_Formal (First_Formal (Op_Id)))
5966                              and then
5967                                Has_Compatible_Type
5968                                  (R,
5969                                   Etype (Next_Formal (First_Formal (Op_Id))))
5970                            then
5971                               Error_Msg_N
5972                                 ("No legal interpretation for operator&", N);
5973                               Error_Msg_NE
5974                                 ("\use clause on& would make operation legal",
5975                                    N, Scope (Op_Id));
5976                               exit;
5977                            end if;
5978                         end if;
5979
5980                         Op_Id := Homonym (Op_Id);
5981                      end loop;
5982
5983                      if No (Op_Id) then
5984                         Error_Msg_N ("invalid operand types for operator&", N);
5985
5986                         if Nkind (N) /= N_Op_Concat then
5987                            Error_Msg_NE ("\left operand has}!",  N, Etype (L));
5988                            Error_Msg_NE ("\right operand has}!", N, Etype (R));
5989                         end if;
5990                      end if;
5991                   end if;
5992                end if;
5993             end if;
5994          end;
5995       end if;
5996    end Operator_Check;
5997
5998    -----------------------------------------
5999    -- Process_Implicit_Dereference_Prefix --
6000    -----------------------------------------
6001
6002    function Process_Implicit_Dereference_Prefix
6003      (E : Entity_Id;
6004       P : Entity_Id) return Entity_Id
6005    is
6006       Ref : Node_Id;
6007       Typ : constant Entity_Id := Designated_Type (Etype (P));
6008
6009    begin
6010       if Present (E)
6011         and then (Operating_Mode = Check_Semantics or else not Expander_Active)
6012       then
6013          --  We create a dummy reference to E to ensure that the reference
6014          --  is not considered as part of an assignment (an implicit
6015          --  dereference can never assign to its prefix). The Comes_From_Source
6016          --  attribute needs to be propagated for accurate warnings.
6017
6018          Ref := New_Reference_To (E, Sloc (P));
6019          Set_Comes_From_Source (Ref, Comes_From_Source (P));
6020          Generate_Reference (E, Ref);
6021       end if;
6022
6023       --  An implicit dereference is a legal occurrence of an
6024       --  incomplete type imported through a limited_with clause,
6025       --  if the full view is visible.
6026
6027       if From_With_Type (Typ)
6028         and then not From_With_Type (Scope (Typ))
6029         and then
6030           (Is_Immediately_Visible (Scope (Typ))
6031             or else
6032               (Is_Child_Unit (Scope (Typ))
6033                  and then Is_Visible_Child_Unit (Scope (Typ))))
6034       then
6035          return Available_View (Typ);
6036       else
6037          return Typ;
6038       end if;
6039
6040    end Process_Implicit_Dereference_Prefix;
6041
6042    --------------------------------
6043    -- Remove_Abstract_Operations --
6044    --------------------------------
6045
6046    procedure Remove_Abstract_Operations (N : Node_Id) is
6047       Abstract_Op    : Entity_Id := Empty;
6048       Address_Kludge : Boolean := False;
6049       I              : Interp_Index;
6050       It             : Interp;
6051
6052       --  AI-310: If overloaded, remove abstract non-dispatching operations. We
6053       --  activate this if either extensions are enabled, or if the abstract
6054       --  operation in question comes from a predefined file. This latter test
6055       --  allows us to use abstract to make operations invisible to users. In
6056       --  particular, if type Address is non-private and abstract subprograms
6057       --  are used to hide its operators, they will be truly hidden.
6058
6059       type Operand_Position is (First_Op, Second_Op);
6060       Univ_Type : constant Entity_Id := Universal_Interpretation (N);
6061
6062       procedure Remove_Address_Interpretations (Op : Operand_Position);
6063       --  Ambiguities may arise when the operands are literal and the address
6064       --  operations in s-auxdec are visible. In that case, remove the
6065       --  interpretation of a literal as Address, to retain the semantics of
6066       --  Address as a private type.
6067
6068       ------------------------------------
6069       -- Remove_Address_Interpretations --
6070       ------------------------------------
6071
6072       procedure Remove_Address_Interpretations (Op : Operand_Position) is
6073          Formal : Entity_Id;
6074
6075       begin
6076          if Is_Overloaded (N) then
6077             Get_First_Interp (N, I, It);
6078             while Present (It.Nam) loop
6079                Formal := First_Entity (It.Nam);
6080
6081                if Op = Second_Op then
6082                   Formal := Next_Entity (Formal);
6083                end if;
6084
6085                if Is_Descendent_Of_Address (Etype (Formal)) then
6086                   Address_Kludge := True;
6087                   Remove_Interp (I);
6088                end if;
6089
6090                Get_Next_Interp (I, It);
6091             end loop;
6092          end if;
6093       end Remove_Address_Interpretations;
6094
6095    --  Start of processing for Remove_Abstract_Operations
6096
6097    begin
6098       if Is_Overloaded (N) then
6099          Get_First_Interp (N, I, It);
6100
6101          while Present (It.Nam) loop
6102             if Is_Overloadable (It.Nam)
6103               and then Is_Abstract_Subprogram (It.Nam)
6104               and then not Is_Dispatching_Operation (It.Nam)
6105             then
6106                Abstract_Op := It.Nam;
6107
6108                if Is_Descendent_Of_Address (It.Typ) then
6109                   Address_Kludge := True;
6110                   Remove_Interp (I);
6111                   exit;
6112
6113                --  In Ada 2005, this operation does not participate in Overload
6114                --  resolution. If the operation is defined in a predefined
6115                --  unit, it is one of the operations declared abstract in some
6116                --  variants of System, and it must be removed as well.
6117
6118                elsif Ada_Version >= Ada_2005
6119                  or else Is_Predefined_File_Name
6120                            (Unit_File_Name (Get_Source_Unit (It.Nam)))
6121                then
6122                   Remove_Interp (I);
6123                   exit;
6124                end if;
6125             end if;
6126
6127             Get_Next_Interp (I, It);
6128          end loop;
6129
6130          if No (Abstract_Op) then
6131
6132             --  If some interpretation yields an integer type, it is still
6133             --  possible that there are address interpretations. Remove them
6134             --  if one operand is a literal, to avoid spurious ambiguities
6135             --  on systems where Address is a visible integer type.
6136
6137             if Is_Overloaded (N)
6138               and then Nkind (N) in N_Op
6139               and then Is_Integer_Type (Etype (N))
6140             then
6141                if Nkind (N) in N_Binary_Op then
6142                   if Nkind (Right_Opnd (N)) = N_Integer_Literal then
6143                      Remove_Address_Interpretations (Second_Op);
6144
6145                   elsif Nkind (Right_Opnd (N)) = N_Integer_Literal then
6146                      Remove_Address_Interpretations (First_Op);
6147                   end if;
6148                end if;
6149             end if;
6150
6151          elsif Nkind (N) in N_Op then
6152
6153             --  Remove interpretations that treat literals as addresses. This
6154             --  is never appropriate, even when Address is defined as a visible
6155             --  Integer type. The reason is that we would really prefer Address
6156             --  to behave as a private type, even in this case, which is there
6157             --  only to accommodate oddities of VMS address sizes. If Address
6158             --  is a visible integer type, we get lots of overload ambiguities.
6159
6160             if Nkind (N) in N_Binary_Op then
6161                declare
6162                   U1 : constant Boolean :=
6163                      Present (Universal_Interpretation (Right_Opnd (N)));
6164                   U2 : constant Boolean :=
6165                      Present (Universal_Interpretation (Left_Opnd (N)));
6166
6167                begin
6168                   if U1 then
6169                      Remove_Address_Interpretations (Second_Op);
6170                   end if;
6171
6172                   if U2 then
6173                      Remove_Address_Interpretations (First_Op);
6174                   end if;
6175
6176                   if not (U1 and U2) then
6177
6178                      --  Remove corresponding predefined operator, which is
6179                      --  always added to the overload set.
6180
6181                      Get_First_Interp (N, I, It);
6182                      while Present (It.Nam) loop
6183                         if Scope (It.Nam) = Standard_Standard
6184                           and then Base_Type (It.Typ) =
6185                                    Base_Type (Etype (Abstract_Op))
6186                         then
6187                            Remove_Interp (I);
6188                         end if;
6189
6190                         Get_Next_Interp (I, It);
6191                      end loop;
6192
6193                   elsif Is_Overloaded (N)
6194                     and then Present (Univ_Type)
6195                   then
6196                      --  If both operands have a universal interpretation,
6197                      --  it is still necessary to remove interpretations that
6198                      --  yield Address. Any remaining ambiguities will be
6199                      --  removed in Disambiguate.
6200
6201                      Get_First_Interp (N, I, It);
6202                      while Present (It.Nam) loop
6203                         if Is_Descendent_Of_Address (It.Typ) then
6204                            Remove_Interp (I);
6205
6206                         elsif not Is_Type (It.Nam) then
6207                            Set_Entity (N, It.Nam);
6208                         end if;
6209
6210                         Get_Next_Interp (I, It);
6211                      end loop;
6212                   end if;
6213                end;
6214             end if;
6215
6216          elsif Nkind (N) = N_Function_Call
6217            and then
6218              (Nkind (Name (N)) = N_Operator_Symbol
6219                 or else
6220                   (Nkind (Name (N)) = N_Expanded_Name
6221                      and then
6222                        Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
6223          then
6224
6225             declare
6226                Arg1 : constant Node_Id := First (Parameter_Associations (N));
6227                U1   : constant Boolean :=
6228                         Present (Universal_Interpretation (Arg1));
6229                U2   : constant Boolean :=
6230                         Present (Next (Arg1)) and then
6231                         Present (Universal_Interpretation (Next (Arg1)));
6232
6233             begin
6234                if U1 then
6235                   Remove_Address_Interpretations (First_Op);
6236                end if;
6237
6238                if U2 then
6239                   Remove_Address_Interpretations (Second_Op);
6240                end if;
6241
6242                if not (U1 and U2) then
6243                   Get_First_Interp (N, I, It);
6244                   while Present (It.Nam) loop
6245                      if Scope (It.Nam) = Standard_Standard
6246                        and then It.Typ = Base_Type (Etype (Abstract_Op))
6247                      then
6248                         Remove_Interp (I);
6249                      end if;
6250
6251                      Get_Next_Interp (I, It);
6252                   end loop;
6253                end if;
6254             end;
6255          end if;
6256
6257          --  If the removal has left no valid interpretations, emit an error
6258          --  message now and label node as illegal.
6259
6260          if Present (Abstract_Op) then
6261             Get_First_Interp (N, I, It);
6262
6263             if No (It.Nam) then
6264
6265                --  Removal of abstract operation left no viable candidate
6266
6267                Set_Etype (N, Any_Type);
6268                Error_Msg_Sloc := Sloc (Abstract_Op);
6269                Error_Msg_NE
6270                  ("cannot call abstract operation& declared#", N, Abstract_Op);
6271
6272             --  In Ada 2005, an abstract operation may disable predefined
6273             --  operators. Since the context is not yet known, we mark the
6274             --  predefined operators as potentially hidden. Do not include
6275             --  predefined operators when addresses are involved since this
6276             --  case is handled separately.
6277
6278             elsif Ada_Version >= Ada_2005
6279               and then not Address_Kludge
6280             then
6281                while Present (It.Nam) loop
6282                   if Is_Numeric_Type (It.Typ)
6283                     and then Scope (It.Typ) = Standard_Standard
6284                   then
6285                      Set_Abstract_Op (I, Abstract_Op);
6286                   end if;
6287
6288                   Get_Next_Interp (I, It);
6289                end loop;
6290             end if;
6291          end if;
6292       end if;
6293    end Remove_Abstract_Operations;
6294
6295    -----------------------
6296    -- Try_Indirect_Call --
6297    -----------------------
6298
6299    function Try_Indirect_Call
6300      (N   : Node_Id;
6301       Nam : Entity_Id;
6302       Typ : Entity_Id) return Boolean
6303    is
6304       Actual : Node_Id;
6305       Formal : Entity_Id;
6306
6307       Call_OK : Boolean;
6308       pragma Warnings (Off, Call_OK);
6309
6310    begin
6311       Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
6312
6313       Actual := First_Actual (N);
6314       Formal := First_Formal (Designated_Type (Typ));
6315       while Present (Actual) and then Present (Formal) loop
6316          if not Has_Compatible_Type (Actual, Etype (Formal)) then
6317             return False;
6318          end if;
6319
6320          Next (Actual);
6321          Next_Formal (Formal);
6322       end loop;
6323
6324       if No (Actual) and then No (Formal) then
6325          Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
6326
6327          --  Nam is a candidate interpretation for the name in the call,
6328          --  if it is not an indirect call.
6329
6330          if not Is_Type (Nam)
6331             and then Is_Entity_Name (Name (N))
6332          then
6333             Set_Entity (Name (N), Nam);
6334          end if;
6335
6336          return True;
6337       else
6338          return False;
6339       end if;
6340    end Try_Indirect_Call;
6341
6342    ----------------------
6343    -- Try_Indexed_Call --
6344    ----------------------
6345
6346    function Try_Indexed_Call
6347      (N          : Node_Id;
6348       Nam        : Entity_Id;
6349       Typ        : Entity_Id;
6350       Skip_First : Boolean) return Boolean
6351    is
6352       Loc     : constant Source_Ptr := Sloc (N);
6353       Actuals : constant List_Id    := Parameter_Associations (N);
6354       Actual  : Node_Id;
6355       Index   : Entity_Id;
6356
6357    begin
6358       Actual := First (Actuals);
6359
6360       --  If the call was originally written in prefix form, skip the first
6361       --  actual, which is obviously not defaulted.
6362
6363       if Skip_First then
6364          Next (Actual);
6365       end if;
6366
6367       Index := First_Index (Typ);
6368       while Present (Actual) and then Present (Index) loop
6369
6370          --  If the parameter list has a named association, the expression
6371          --  is definitely a call and not an indexed component.
6372
6373          if Nkind (Actual) = N_Parameter_Association then
6374             return False;
6375          end if;
6376
6377          if Is_Entity_Name (Actual)
6378            and then Is_Type (Entity (Actual))
6379            and then No (Next (Actual))
6380          then
6381             --  A single actual that is a type name indicates a slice if the
6382             --  type is discrete, and an error otherwise.
6383
6384             if Is_Discrete_Type (Entity (Actual)) then
6385                Rewrite (N,
6386                  Make_Slice (Loc,
6387                    Prefix =>
6388                      Make_Function_Call (Loc,
6389                        Name => Relocate_Node (Name (N))),
6390                    Discrete_Range =>
6391                      New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
6392
6393                Analyze (N);
6394
6395             else
6396                Error_Msg_N ("invalid use of type in expression", Actual);
6397                Set_Etype (N, Any_Type);
6398             end if;
6399
6400             return True;
6401
6402          elsif not Has_Compatible_Type (Actual, Etype (Index)) then
6403             return False;
6404          end if;
6405
6406          Next (Actual);
6407          Next_Index (Index);
6408       end loop;
6409
6410       if No (Actual) and then No (Index) then
6411          Add_One_Interp (N, Nam, Component_Type (Typ));
6412
6413          --  Nam is a candidate interpretation for the name in the call,
6414          --  if it is not an indirect call.
6415
6416          if not Is_Type (Nam)
6417             and then Is_Entity_Name (Name (N))
6418          then
6419             Set_Entity (Name (N), Nam);
6420          end if;
6421
6422          return True;
6423       else
6424          return False;
6425       end if;
6426    end Try_Indexed_Call;
6427
6428    --------------------------
6429    -- Try_Object_Operation --
6430    --------------------------
6431
6432    function Try_Object_Operation (N : Node_Id) return Boolean is
6433       K              : constant Node_Kind  := Nkind (Parent (N));
6434       Is_Subprg_Call : constant Boolean    := Nkind_In
6435                                                (K, N_Procedure_Call_Statement,
6436                                                    N_Function_Call);
6437       Loc            : constant Source_Ptr := Sloc (N);
6438       Obj            : constant Node_Id    := Prefix (N);
6439
6440       Subprog : constant Node_Id    :=
6441                   Make_Identifier (Sloc (Selector_Name (N)),
6442                     Chars => Chars (Selector_Name (N)));
6443       --  Identifier on which possible interpretations will be collected
6444
6445       Report_Error : Boolean := False;
6446       --  If no candidate interpretation matches the context, redo the
6447       --  analysis with error enabled to provide additional information.
6448
6449       Actual          : Node_Id;
6450       Candidate       : Entity_Id := Empty;
6451       New_Call_Node   : Node_Id := Empty;
6452       Node_To_Replace : Node_Id;
6453       Obj_Type        : Entity_Id := Etype (Obj);
6454       Success         : Boolean := False;
6455
6456       function Valid_Candidate
6457         (Success : Boolean;
6458          Call    : Node_Id;
6459          Subp    : Entity_Id) return Entity_Id;
6460       --  If the subprogram is a valid interpretation, record it, and add
6461       --  to the list of interpretations of Subprog.
6462
6463       procedure Complete_Object_Operation
6464         (Call_Node       : Node_Id;
6465          Node_To_Replace : Node_Id);
6466       --  Make Subprog the name of Call_Node, replace Node_To_Replace with
6467       --  Call_Node, insert the object (or its dereference) as the first actual
6468       --  in the call, and complete the analysis of the call.
6469
6470       procedure Report_Ambiguity (Op : Entity_Id);
6471       --  If a prefixed procedure call is ambiguous, indicate whether the
6472       --  call includes an implicit dereference or an implicit 'Access.
6473
6474       procedure Transform_Object_Operation
6475         (Call_Node       : out Node_Id;
6476          Node_To_Replace : out Node_Id);
6477       --  Transform Obj.Operation (X, Y,,) into Operation (Obj, X, Y ..)
6478       --  Call_Node is the resulting subprogram call, Node_To_Replace is
6479       --  either N or the parent of N, and Subprog is a reference to the
6480       --  subprogram we are trying to match.
6481
6482       function Try_Class_Wide_Operation
6483         (Call_Node       : Node_Id;
6484          Node_To_Replace : Node_Id) return Boolean;
6485       --  Traverse all ancestor types looking for a class-wide subprogram
6486       --  for which the current operation is a valid non-dispatching call.
6487
6488       procedure Try_One_Prefix_Interpretation (T : Entity_Id);
6489       --  If prefix is overloaded, its interpretation may include different
6490       --  tagged types, and we must examine the primitive operations and
6491       --  the class-wide operations of each in order to find candidate
6492       --  interpretations for the call as a whole.
6493
6494       function Try_Primitive_Operation
6495         (Call_Node       : Node_Id;
6496          Node_To_Replace : Node_Id) return Boolean;
6497       --  Traverse the list of primitive subprograms looking for a dispatching
6498       --  operation for which the current node is a valid call .
6499
6500       ---------------------
6501       -- Valid_Candidate --
6502       ---------------------
6503
6504       function Valid_Candidate
6505         (Success : Boolean;
6506          Call    : Node_Id;
6507          Subp    : Entity_Id) return Entity_Id
6508       is
6509          Arr_Type  : Entity_Id;
6510          Comp_Type : Entity_Id;
6511
6512       begin
6513          --  If the subprogram is a valid interpretation, record it in global
6514          --  variable Subprog, to collect all possible overloadings.
6515
6516          if Success then
6517             if Subp /= Entity (Subprog) then
6518                Add_One_Interp (Subprog, Subp, Etype (Subp));
6519             end if;
6520          end if;
6521
6522          --  If the call may be an indexed call, retrieve component type of
6523          --  resulting expression, and add possible interpretation.
6524
6525          Arr_Type  := Empty;
6526          Comp_Type := Empty;
6527
6528          if Nkind (Call) = N_Function_Call
6529            and then Nkind (Parent (N)) = N_Indexed_Component
6530            and then Needs_One_Actual (Subp)
6531          then
6532             if Is_Array_Type (Etype (Subp)) then
6533                Arr_Type := Etype (Subp);
6534
6535             elsif Is_Access_Type (Etype (Subp))
6536               and then Is_Array_Type (Designated_Type (Etype (Subp)))
6537             then
6538                Arr_Type := Designated_Type (Etype (Subp));
6539             end if;
6540          end if;
6541
6542          if Present (Arr_Type) then
6543
6544             --  Verify that the actuals (excluding the object) match the types
6545             --  of the indexes.
6546
6547             declare
6548                Actual : Node_Id;
6549                Index  : Node_Id;
6550
6551             begin
6552                Actual := Next (First_Actual (Call));
6553                Index  := First_Index (Arr_Type);
6554                while Present (Actual) and then Present (Index) loop
6555                   if not Has_Compatible_Type (Actual, Etype (Index)) then
6556                      Arr_Type := Empty;
6557                      exit;
6558                   end if;
6559
6560                   Next_Actual (Actual);
6561                   Next_Index  (Index);
6562                end loop;
6563
6564                if No (Actual)
6565                   and then No (Index)
6566                   and then Present (Arr_Type)
6567                then
6568                   Comp_Type := Component_Type (Arr_Type);
6569                end if;
6570             end;
6571
6572             if Present (Comp_Type)
6573               and then Etype (Subprog) /= Comp_Type
6574             then
6575                Add_One_Interp (Subprog, Subp, Comp_Type);
6576             end if;
6577          end if;
6578
6579          if Etype (Call) /= Any_Type then
6580             return Subp;
6581          else
6582             return Empty;
6583          end if;
6584       end Valid_Candidate;
6585
6586       -------------------------------
6587       -- Complete_Object_Operation --
6588       -------------------------------
6589
6590       procedure Complete_Object_Operation
6591         (Call_Node       : Node_Id;
6592          Node_To_Replace : Node_Id)
6593       is
6594          Control      : constant Entity_Id := First_Formal (Entity (Subprog));
6595          Formal_Type  : constant Entity_Id := Etype (Control);
6596          First_Actual : Node_Id;
6597
6598       begin
6599          --  Place the name of the operation, with its interpretations,
6600          --  on the rewritten call.
6601
6602          Set_Name (Call_Node, Subprog);
6603
6604          First_Actual := First (Parameter_Associations (Call_Node));
6605
6606          --  For cross-reference purposes, treat the new node as being in
6607          --  the source if the original one is.
6608
6609          Set_Comes_From_Source (Subprog, Comes_From_Source (N));
6610          Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
6611
6612          if Nkind (N) = N_Selected_Component
6613            and then not Inside_A_Generic
6614          then
6615             Set_Entity (Selector_Name (N), Entity (Subprog));
6616          end if;
6617
6618          --  If need be, rewrite first actual as an explicit dereference
6619          --  If the call is overloaded, the rewriting can only be done
6620          --  once the primitive operation is identified.
6621
6622          if Is_Overloaded (Subprog) then
6623
6624             --  The prefix itself may be overloaded, and its interpretations
6625             --  must be propagated to the new actual in the call.
6626
6627             if Is_Overloaded (Obj) then
6628                Save_Interps (Obj, First_Actual);
6629             end if;
6630
6631             Rewrite (First_Actual, Obj);
6632
6633          elsif not Is_Access_Type (Formal_Type)
6634            and then Is_Access_Type (Etype (Obj))
6635          then
6636             Rewrite (First_Actual,
6637               Make_Explicit_Dereference (Sloc (Obj), Obj));
6638             Analyze (First_Actual);
6639
6640             --  If we need to introduce an explicit dereference, verify that
6641             --  the resulting actual is compatible with the mode of the formal.
6642
6643             if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
6644               and then Is_Access_Constant (Etype (Obj))
6645             then
6646                Error_Msg_NE
6647                  ("expect variable in call to&", Prefix (N), Entity (Subprog));
6648             end if;
6649
6650          --  Conversely, if the formal is an access parameter and the object
6651          --  is not, replace the actual with a 'Access reference. Its analysis
6652          --  will check that the object is aliased.
6653
6654          elsif Is_Access_Type (Formal_Type)
6655            and then not Is_Access_Type (Etype (Obj))
6656          then
6657             --  A special case: A.all'access is illegal if A is an access to a
6658             --  constant and the context requires an access to a variable.
6659
6660             if not Is_Access_Constant (Formal_Type) then
6661                if (Nkind (Obj) = N_Explicit_Dereference
6662                     and then Is_Access_Constant (Etype (Prefix (Obj))))
6663                  or else not Is_Variable (Obj)
6664                then
6665                   Error_Msg_NE
6666                     ("actual for& must be a variable", Obj, Control);
6667                end if;
6668             end if;
6669
6670             Rewrite (First_Actual,
6671               Make_Attribute_Reference (Loc,
6672                 Attribute_Name => Name_Access,
6673                 Prefix => Relocate_Node (Obj)));
6674
6675             if not Is_Aliased_View (Obj) then
6676                Error_Msg_NE
6677                  ("object in prefixed call to& must be aliased"
6678                       & " (RM-2005 4.3.1 (13))",
6679                  Prefix (First_Actual), Subprog);
6680             end if;
6681
6682             Analyze (First_Actual);
6683
6684          else
6685             if Is_Overloaded (Obj) then
6686                Save_Interps (Obj, First_Actual);
6687             end if;
6688
6689             Rewrite (First_Actual, Obj);
6690          end if;
6691
6692          Rewrite (Node_To_Replace, Call_Node);
6693
6694          --  Propagate the interpretations collected in subprog to the new
6695          --  function call node, to be resolved from context.
6696
6697          if Is_Overloaded (Subprog) then
6698             Save_Interps (Subprog, Node_To_Replace);
6699
6700          else
6701             Analyze (Node_To_Replace);
6702
6703             --  If the operation has been rewritten into a call, which may get
6704             --  subsequently an explicit dereference, preserve the type on the
6705             --  original node (selected component or indexed component) for
6706             --  subsequent legality tests, e.g. Is_Variable. which examines
6707             --  the original node.
6708
6709             if Nkind (Node_To_Replace) = N_Function_Call then
6710                Set_Etype
6711                  (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
6712             end if;
6713          end if;
6714       end Complete_Object_Operation;
6715
6716       ----------------------
6717       -- Report_Ambiguity --
6718       ----------------------
6719
6720       procedure Report_Ambiguity (Op : Entity_Id) is
6721          Access_Formal : constant Boolean :=
6722                            Is_Access_Type (Etype (First_Formal (Op)));
6723          Access_Actual : constant Boolean :=
6724                            Is_Access_Type (Etype (Prefix (N)));
6725
6726       begin
6727          Error_Msg_Sloc := Sloc (Op);
6728
6729          if Access_Formal and then not Access_Actual then
6730             if Nkind (Parent (Op)) = N_Full_Type_Declaration then
6731                Error_Msg_N
6732                  ("\possible interpretation"
6733                    & " (inherited, with implicit 'Access) #", N);
6734             else
6735                Error_Msg_N
6736                  ("\possible interpretation (with implicit 'Access) #", N);
6737             end if;
6738
6739          elsif not Access_Formal and then Access_Actual then
6740             if Nkind (Parent (Op)) = N_Full_Type_Declaration then
6741                Error_Msg_N
6742                  ("\possible interpretation"
6743                    & " ( inherited, with implicit dereference) #", N);
6744             else
6745                Error_Msg_N
6746                  ("\possible interpretation (with implicit dereference) #", N);
6747             end if;
6748
6749          else
6750             if Nkind (Parent (Op)) = N_Full_Type_Declaration then
6751                Error_Msg_N ("\possible interpretation (inherited)#", N);
6752             else
6753                Error_Msg_N -- CODEFIX
6754                  ("\possible interpretation#", N);
6755             end if;
6756          end if;
6757       end Report_Ambiguity;
6758
6759       --------------------------------
6760       -- Transform_Object_Operation --
6761       --------------------------------
6762
6763       procedure Transform_Object_Operation
6764         (Call_Node       : out Node_Id;
6765          Node_To_Replace : out Node_Id)
6766       is
6767          Dummy : constant Node_Id := New_Copy (Obj);
6768          --  Placeholder used as a first parameter in the call, replaced
6769          --  eventually by the proper object.
6770
6771          Parent_Node : constant Node_Id := Parent (N);
6772
6773          Actual  : Node_Id;
6774          Actuals : List_Id;
6775
6776       begin
6777          --  Common case covering 1) Call to a procedure and 2) Call to a
6778          --  function that has some additional actuals.
6779
6780          if Nkind_In (Parent_Node, N_Function_Call,
6781                                    N_Procedure_Call_Statement)
6782
6783             --  N is a selected component node containing the name of the
6784             --  subprogram. If N is not the name of the parent node we must
6785             --  not replace the parent node by the new construct. This case
6786             --  occurs when N is a parameterless call to a subprogram that
6787             --  is an actual parameter of a call to another subprogram. For
6788             --  example:
6789             --            Some_Subprogram (..., Obj.Operation, ...)
6790
6791             and then Name (Parent_Node) = N
6792          then
6793             Node_To_Replace := Parent_Node;
6794
6795             Actuals := Parameter_Associations (Parent_Node);
6796
6797             if Present (Actuals) then
6798                Prepend (Dummy, Actuals);
6799             else
6800                Actuals := New_List (Dummy);
6801             end if;
6802
6803             if Nkind (Parent_Node) = N_Procedure_Call_Statement then
6804                Call_Node :=
6805                  Make_Procedure_Call_Statement (Loc,
6806                    Name => New_Copy (Subprog),
6807                    Parameter_Associations => Actuals);
6808
6809             else
6810                Call_Node :=
6811                  Make_Function_Call (Loc,
6812                    Name => New_Copy (Subprog),
6813                    Parameter_Associations => Actuals);
6814
6815             end if;
6816
6817          --  Before analysis, a function call appears as an indexed component
6818          --  if there are no named associations.
6819
6820          elsif Nkind (Parent_Node) =  N_Indexed_Component
6821            and then N = Prefix (Parent_Node)
6822          then
6823             Node_To_Replace := Parent_Node;
6824             Actuals := Expressions (Parent_Node);
6825
6826             Actual := First (Actuals);
6827             while Present (Actual) loop
6828                Analyze (Actual);
6829                Next (Actual);
6830             end loop;
6831
6832             Prepend (Dummy, Actuals);
6833
6834             Call_Node :=
6835                Make_Function_Call (Loc,
6836                  Name => New_Copy (Subprog),
6837                  Parameter_Associations => Actuals);
6838
6839          --  Parameterless call: Obj.F is rewritten as F (Obj)
6840
6841          else
6842             Node_To_Replace := N;
6843
6844             Call_Node :=
6845                Make_Function_Call (Loc,
6846                  Name => New_Copy (Subprog),
6847                  Parameter_Associations => New_List (Dummy));
6848          end if;
6849       end Transform_Object_Operation;
6850
6851       ------------------------------
6852       -- Try_Class_Wide_Operation --
6853       ------------------------------
6854
6855       function Try_Class_Wide_Operation
6856         (Call_Node       : Node_Id;
6857          Node_To_Replace : Node_Id) return Boolean
6858       is
6859          Anc_Type    : Entity_Id;
6860          Matching_Op : Entity_Id := Empty;
6861          Error       : Boolean;
6862
6863          procedure Traverse_Homonyms
6864            (Anc_Type : Entity_Id;
6865             Error    : out Boolean);
6866          --  Traverse the homonym chain of the subprogram searching for those
6867          --  homonyms whose first formal has the Anc_Type's class-wide type,
6868          --  or an anonymous access type designating the class-wide type. If
6869          --  an ambiguity is detected, then Error is set to True.
6870
6871          procedure Traverse_Interfaces
6872            (Anc_Type : Entity_Id;
6873             Error    : out Boolean);
6874          --  Traverse the list of interfaces, if any, associated with Anc_Type
6875          --  and search for acceptable class-wide homonyms associated with each
6876          --  interface. If an ambiguity is detected, then Error is set to True.
6877
6878          -----------------------
6879          -- Traverse_Homonyms --
6880          -----------------------
6881
6882          procedure Traverse_Homonyms
6883            (Anc_Type : Entity_Id;
6884             Error    : out Boolean)
6885          is
6886             Cls_Type    : Entity_Id;
6887             Hom         : Entity_Id;
6888             Hom_Ref     : Node_Id;
6889             Success     : Boolean;
6890
6891          begin
6892             Error := False;
6893
6894             Cls_Type := Class_Wide_Type (Anc_Type);
6895
6896             Hom := Current_Entity (Subprog);
6897
6898             --  Find a non-hidden operation whose first parameter is of the
6899             --  class-wide type, a subtype thereof, or an anonymous access
6900             --  to same.
6901
6902             while Present (Hom) loop
6903                if Ekind_In (Hom, E_Procedure, E_Function)
6904                  and then not Is_Hidden (Hom)
6905                  and then Scope (Hom) = Scope (Anc_Type)
6906                  and then Present (First_Formal (Hom))
6907                  and then
6908                    (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
6909                      or else
6910                        (Is_Access_Type (Etype (First_Formal (Hom)))
6911                           and then
6912                             Ekind (Etype (First_Formal (Hom))) =
6913                               E_Anonymous_Access_Type
6914                           and then
6915                             Base_Type
6916                               (Designated_Type (Etype (First_Formal (Hom)))) =
6917                                                                    Cls_Type))
6918                then
6919                   Set_Etype (Call_Node, Any_Type);
6920                   Set_Is_Overloaded (Call_Node, False);
6921                   Success := False;
6922
6923                   if No (Matching_Op) then
6924                      Hom_Ref := New_Reference_To (Hom, Sloc (Subprog));
6925                      Set_Etype (Call_Node, Any_Type);
6926                      Set_Parent (Call_Node, Parent (Node_To_Replace));
6927
6928                      Set_Name (Call_Node, Hom_Ref);
6929
6930                      Analyze_One_Call
6931                        (N          => Call_Node,
6932                         Nam        => Hom,
6933                         Report     => Report_Error,
6934                         Success    => Success,
6935                         Skip_First => True);
6936
6937                      Matching_Op :=
6938                        Valid_Candidate (Success, Call_Node, Hom);
6939
6940                   else
6941                      Analyze_One_Call
6942                        (N          => Call_Node,
6943                         Nam        => Hom,
6944                         Report     => Report_Error,
6945                         Success    => Success,
6946                         Skip_First => True);
6947
6948                      if Present (Valid_Candidate (Success, Call_Node, Hom))
6949                        and then Nkind (Call_Node) /= N_Function_Call
6950                      then
6951                         Error_Msg_NE ("ambiguous call to&", N, Hom);
6952                         Report_Ambiguity (Matching_Op);
6953                         Report_Ambiguity (Hom);
6954                         Error := True;
6955                         return;
6956                      end if;
6957                   end if;
6958                end if;
6959
6960                Hom := Homonym (Hom);
6961             end loop;
6962          end Traverse_Homonyms;
6963
6964          -------------------------
6965          -- Traverse_Interfaces --
6966          -------------------------
6967
6968          procedure Traverse_Interfaces
6969            (Anc_Type : Entity_Id;
6970             Error    : out Boolean)
6971          is
6972             Intface_List : constant List_Id :=
6973                              Abstract_Interface_List (Anc_Type);
6974             Intface      : Node_Id;
6975
6976          begin
6977             Error := False;
6978
6979             if Is_Non_Empty_List (Intface_List) then
6980                Intface := First (Intface_List);
6981                while Present (Intface) loop
6982
6983                   --  Look for acceptable class-wide homonyms associated with
6984                   --  the interface.
6985
6986                   Traverse_Homonyms (Etype (Intface), Error);
6987
6988                   if Error then
6989                      return;
6990                   end if;
6991
6992                   --  Continue the search by looking at each of the interface's
6993                   --  associated interface ancestors.
6994
6995                   Traverse_Interfaces (Etype (Intface), Error);
6996
6997                   if Error then
6998                      return;
6999                   end if;
7000
7001                   Next (Intface);
7002                end loop;
7003             end if;
7004          end Traverse_Interfaces;
7005
7006       --  Start of processing for Try_Class_Wide_Operation
7007
7008       begin
7009          --  Loop through ancestor types (including interfaces), traversing
7010          --  the homonym chain of the subprogram, trying out those homonyms
7011          --  whose first formal has the class-wide type of the ancestor, or
7012          --  an anonymous access type designating the class-wide type.
7013
7014          Anc_Type := Obj_Type;
7015          loop
7016             --  Look for a match among homonyms associated with the ancestor
7017
7018             Traverse_Homonyms (Anc_Type, Error);
7019
7020             if Error then
7021                return True;
7022             end if;
7023
7024             --  Continue the search for matches among homonyms associated with
7025             --  any interfaces implemented by the ancestor.
7026
7027             Traverse_Interfaces (Anc_Type, Error);
7028
7029             if Error then
7030                return True;
7031             end if;
7032
7033             exit when Etype (Anc_Type) = Anc_Type;
7034             Anc_Type := Etype (Anc_Type);
7035          end loop;
7036
7037          if Present (Matching_Op) then
7038             Set_Etype (Call_Node, Etype (Matching_Op));
7039          end if;
7040
7041          return Present (Matching_Op);
7042       end Try_Class_Wide_Operation;
7043
7044       -----------------------------------
7045       -- Try_One_Prefix_Interpretation --
7046       -----------------------------------
7047
7048       procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
7049       begin
7050          Obj_Type := T;
7051
7052          if Is_Access_Type (Obj_Type) then
7053             Obj_Type := Designated_Type (Obj_Type);
7054          end if;
7055
7056          if Ekind (Obj_Type) = E_Private_Subtype then
7057             Obj_Type := Base_Type (Obj_Type);
7058          end if;
7059
7060          if Is_Class_Wide_Type (Obj_Type) then
7061             Obj_Type := Etype (Class_Wide_Type (Obj_Type));
7062          end if;
7063
7064          --  The type may have be obtained through a limited_with clause,
7065          --  in which case the primitive operations are available on its
7066          --  non-limited view. If still incomplete, retrieve full view.
7067
7068          if Ekind (Obj_Type) = E_Incomplete_Type
7069            and then From_With_Type (Obj_Type)
7070          then
7071             Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
7072          end if;
7073
7074          --  If the object is not tagged, or the type is still an incomplete
7075          --  type, this is not a prefixed call.
7076
7077          if not Is_Tagged_Type (Obj_Type)
7078            or else Is_Incomplete_Type (Obj_Type)
7079          then
7080             return;
7081          end if;
7082
7083          if Try_Primitive_Operation
7084               (Call_Node       => New_Call_Node,
7085                Node_To_Replace => Node_To_Replace)
7086            or else
7087              Try_Class_Wide_Operation
7088                (Call_Node       => New_Call_Node,
7089                 Node_To_Replace => Node_To_Replace)
7090          then
7091             null;
7092          end if;
7093       end Try_One_Prefix_Interpretation;
7094
7095       -----------------------------
7096       -- Try_Primitive_Operation --
7097       -----------------------------
7098
7099       function Try_Primitive_Operation
7100         (Call_Node       : Node_Id;
7101          Node_To_Replace : Node_Id) return Boolean
7102       is
7103          Elmt        : Elmt_Id;
7104          Prim_Op     : Entity_Id;
7105          Matching_Op : Entity_Id := Empty;
7106          Prim_Op_Ref : Node_Id   := Empty;
7107
7108          Corr_Type   : Entity_Id := Empty;
7109          --  If the prefix is a synchronized type, the controlling type of
7110          --  the primitive operation is the corresponding record type, else
7111          --  this is the object type itself.
7112
7113          Success     : Boolean   := False;
7114
7115          function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
7116          --  For tagged types the candidate interpretations are found in
7117          --  the list of primitive operations of the type and its ancestors.
7118          --  For formal tagged types we have to find the operations declared
7119          --  in the same scope as the type (including in the generic formal
7120          --  part) because the type itself carries no primitive operations,
7121          --  except for formal derived types that inherit the operations of
7122          --  the parent and progenitors.
7123          --  If the context is a generic subprogram body, the generic formals
7124          --  are visible by name, but are not in the entity list of the
7125          --  subprogram because that list starts with the subprogram formals.
7126          --  We retrieve the candidate operations from the generic declaration.
7127
7128          function Is_Private_Overriding (Op : Entity_Id) return Boolean;
7129          --  An operation that overrides an inherited operation in the private
7130          --  part of its package may be hidden, but if the inherited operation
7131          --  is visible a direct call to it will dispatch to the private one,
7132          --  which is therefore a valid candidate.
7133
7134          function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
7135          --  Verify that the prefix, dereferenced if need be, is a valid
7136          --  controlling argument in a call to Op. The remaining actuals
7137          --  are checked in the subsequent call to Analyze_One_Call.
7138
7139          ------------------------------
7140          -- Collect_Generic_Type_Ops --
7141          ------------------------------
7142
7143          function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
7144             Bas        : constant Entity_Id := Base_Type (T);
7145             Candidates : constant Elist_Id := New_Elmt_List;
7146             Subp       : Entity_Id;
7147             Formal     : Entity_Id;
7148
7149             procedure Check_Candidate;
7150             --  The operation is a candidate if its first parameter is a
7151             --  controlling operand of the desired type.
7152
7153             -----------------------
7154             --  Check_Candidate; --
7155             -----------------------
7156
7157             procedure Check_Candidate is
7158             begin
7159                Formal := First_Formal (Subp);
7160
7161                if Present (Formal)
7162                  and then Is_Controlling_Formal (Formal)
7163                  and then
7164                    (Base_Type (Etype (Formal)) = Bas
7165                      or else
7166                        (Is_Access_Type (Etype (Formal))
7167                          and then Designated_Type (Etype (Formal)) = Bas))
7168                then
7169                   Append_Elmt (Subp, Candidates);
7170                end if;
7171             end Check_Candidate;
7172
7173          --  Start of processing for Collect_Generic_Type_Ops
7174
7175          begin
7176             if Is_Derived_Type (T) then
7177                return Primitive_Operations (T);
7178
7179             elsif Ekind_In (Scope (T), E_Procedure, E_Function) then
7180
7181                --  Scan the list of generic formals to find subprograms
7182                --  that may have a first controlling formal of the type.
7183
7184                if Nkind (Unit_Declaration_Node (Scope (T)))
7185                  = N_Generic_Subprogram_Declaration
7186                then
7187                   declare
7188                      Decl : Node_Id;
7189
7190                   begin
7191                      Decl :=
7192                        First (Generic_Formal_Declarations
7193                                (Unit_Declaration_Node (Scope (T))));
7194                      while Present (Decl) loop
7195                         if Nkind (Decl) in N_Formal_Subprogram_Declaration then
7196                            Subp := Defining_Entity (Decl);
7197                            Check_Candidate;
7198                         end if;
7199
7200                         Next (Decl);
7201                      end loop;
7202                   end;
7203                end if;
7204                return Candidates;
7205
7206             else
7207                --  Scan the list of entities declared in the same scope as
7208                --  the type. In general this will be an open scope, given that
7209                --  the call we are analyzing can only appear within a generic
7210                --  declaration or body (either the one that declares T, or a
7211                --  child unit).
7212
7213                --  For a subtype representing a generic actual type, go to the
7214                --  base type.
7215
7216                if Is_Generic_Actual_Type (T) then
7217                   Subp := First_Entity (Scope (Base_Type (T)));
7218                else
7219                   Subp := First_Entity (Scope (T));
7220                end if;
7221
7222                while Present (Subp) loop
7223                   if Is_Overloadable (Subp) then
7224                      Check_Candidate;
7225                   end if;
7226
7227                   Next_Entity (Subp);
7228                end loop;
7229
7230                return Candidates;
7231             end if;
7232          end Collect_Generic_Type_Ops;
7233
7234          ---------------------------
7235          -- Is_Private_Overriding --
7236          ---------------------------
7237
7238          function Is_Private_Overriding (Op : Entity_Id) return Boolean is
7239             Visible_Op : constant Entity_Id := Homonym (Op);
7240
7241          begin
7242             return Present (Visible_Op)
7243               and then Scope (Op) = Scope (Visible_Op)
7244               and then not Comes_From_Source (Visible_Op)
7245               and then Alias (Visible_Op) = Op
7246               and then not Is_Hidden (Visible_Op);
7247          end Is_Private_Overriding;
7248
7249          -----------------------------
7250          -- Valid_First_Argument_Of --
7251          -----------------------------
7252
7253          function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
7254             Typ : Entity_Id := Etype (First_Formal (Op));
7255
7256          begin
7257             if Is_Concurrent_Type (Typ)
7258               and then Present (Corresponding_Record_Type (Typ))
7259             then
7260                Typ := Corresponding_Record_Type (Typ);
7261             end if;
7262
7263             --  Simple case. Object may be a subtype of the tagged type or
7264             --  may be the corresponding record of a synchronized type.
7265
7266             return Obj_Type = Typ
7267               or else Base_Type (Obj_Type) = Typ
7268               or else Corr_Type = Typ
7269
7270                --  Prefix can be dereferenced
7271
7272               or else
7273                 (Is_Access_Type (Corr_Type)
7274                   and then Designated_Type (Corr_Type) = Typ)
7275
7276                --  Formal is an access parameter, for which the object
7277                --  can provide an access.
7278
7279               or else
7280                 (Ekind (Typ) = E_Anonymous_Access_Type
7281                   and then
7282                     Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
7283          end Valid_First_Argument_Of;
7284
7285       --  Start of processing for Try_Primitive_Operation
7286
7287       begin
7288          --  Look for subprograms in the list of primitive operations. The name
7289          --  must be identical, and the kind of call indicates the expected
7290          --  kind of operation (function or procedure). If the type is a
7291          --  (tagged) synchronized type, the primitive ops are attached to the
7292          --  corresponding record (base) type.
7293
7294          if Is_Concurrent_Type (Obj_Type) then
7295             if Present (Corresponding_Record_Type (Obj_Type)) then
7296                Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
7297                Elmt := First_Elmt (Primitive_Operations (Corr_Type));
7298             else
7299                Corr_Type := Obj_Type;
7300                Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
7301             end if;
7302
7303          elsif not Is_Generic_Type (Obj_Type) then
7304             Corr_Type := Obj_Type;
7305             Elmt := First_Elmt (Primitive_Operations (Obj_Type));
7306
7307          else
7308             Corr_Type := Obj_Type;
7309             Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
7310          end if;
7311
7312          while Present (Elmt) loop
7313             Prim_Op := Node (Elmt);
7314
7315             if Chars (Prim_Op) = Chars (Subprog)
7316               and then Present (First_Formal (Prim_Op))
7317               and then Valid_First_Argument_Of (Prim_Op)
7318               and then
7319                 (Nkind (Call_Node) = N_Function_Call)
7320                    = (Ekind (Prim_Op) = E_Function)
7321             then
7322                --  Ada 2005 (AI-251): If this primitive operation corresponds
7323                --  with an immediate ancestor interface there is no need to add
7324                --  it to the list of interpretations; the corresponding aliased
7325                --  primitive is also in this list of primitive operations and
7326                --  will be used instead.
7327
7328                if (Present (Interface_Alias (Prim_Op))
7329                     and then Is_Ancestor (Find_Dispatching_Type
7330                                             (Alias (Prim_Op)), Corr_Type))
7331
7332                  --  Do not consider hidden primitives unless the type is in an
7333                  --  open scope or we are within an instance, where visibility
7334                  --  is known to be correct, or else if this is an overriding
7335                  --  operation in the private part for an inherited operation.
7336
7337                  or else (Is_Hidden (Prim_Op)
7338                            and then not Is_Immediately_Visible (Obj_Type)
7339                            and then not In_Instance
7340                            and then not Is_Private_Overriding (Prim_Op))
7341                then
7342                   goto Continue;
7343                end if;
7344
7345                Set_Etype (Call_Node, Any_Type);
7346                Set_Is_Overloaded (Call_Node, False);
7347
7348                if No (Matching_Op) then
7349                   Prim_Op_Ref := New_Reference_To (Prim_Op, Sloc (Subprog));
7350                   Candidate := Prim_Op;
7351
7352                   Set_Parent (Call_Node, Parent (Node_To_Replace));
7353
7354                   Set_Name (Call_Node, Prim_Op_Ref);
7355                   Success := False;
7356
7357                   Analyze_One_Call
7358                     (N          => Call_Node,
7359                      Nam        => Prim_Op,
7360                      Report     => Report_Error,
7361                      Success    => Success,
7362                      Skip_First => True);
7363
7364                   Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
7365
7366                --  More than one interpretation, collect for subsequent
7367                --  disambiguation. If this is a procedure call and there
7368                --  is another match, report ambiguity now.
7369
7370                else
7371                   Analyze_One_Call
7372                     (N          => Call_Node,
7373                      Nam        => Prim_Op,
7374                      Report     => Report_Error,
7375                      Success    => Success,
7376                      Skip_First => True);
7377
7378                   if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
7379                     and then Nkind (Call_Node) /= N_Function_Call
7380                   then
7381                      Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
7382                      Report_Ambiguity (Matching_Op);
7383                      Report_Ambiguity (Prim_Op);
7384                      return True;
7385                   end if;
7386                end if;
7387             end if;
7388
7389             <<Continue>>
7390             Next_Elmt (Elmt);
7391          end loop;
7392
7393          if Present (Matching_Op) then
7394             Set_Etype (Call_Node, Etype (Matching_Op));
7395          end if;
7396
7397          return Present (Matching_Op);
7398       end Try_Primitive_Operation;
7399
7400    --  Start of processing for Try_Object_Operation
7401
7402    begin
7403       Analyze_Expression (Obj);
7404
7405       --  Analyze the actuals if node is known to be a subprogram call
7406
7407       if Is_Subprg_Call and then N = Name (Parent (N)) then
7408          Actual := First (Parameter_Associations (Parent (N)));
7409          while Present (Actual) loop
7410             Analyze_Expression (Actual);
7411             Next (Actual);
7412          end loop;
7413       end if;
7414
7415       --  Build a subprogram call node, using a copy of Obj as its first
7416       --  actual. This is a placeholder, to be replaced by an explicit
7417       --  dereference when needed.
7418
7419       Transform_Object_Operation
7420         (Call_Node       => New_Call_Node,
7421          Node_To_Replace => Node_To_Replace);
7422
7423       Set_Etype (New_Call_Node, Any_Type);
7424       Set_Etype (Subprog, Any_Type);
7425       Set_Parent (New_Call_Node, Parent (Node_To_Replace));
7426
7427       if not Is_Overloaded (Obj) then
7428          Try_One_Prefix_Interpretation (Obj_Type);
7429
7430       else
7431          declare
7432             I  : Interp_Index;
7433             It : Interp;
7434          begin
7435             Get_First_Interp (Obj, I, It);
7436             while Present (It.Nam) loop
7437                Try_One_Prefix_Interpretation (It.Typ);
7438                Get_Next_Interp (I, It);
7439             end loop;
7440          end;
7441       end if;
7442
7443       if Etype (New_Call_Node) /= Any_Type then
7444          Complete_Object_Operation
7445            (Call_Node       => New_Call_Node,
7446             Node_To_Replace => Node_To_Replace);
7447          return True;
7448
7449       elsif Present (Candidate) then
7450
7451          --  The argument list is not type correct. Re-analyze with error
7452          --  reporting enabled, and use one of the possible candidates.
7453          --  In All_Errors_Mode, re-analyze all failed interpretations.
7454
7455          if All_Errors_Mode then
7456             Report_Error := True;
7457             if Try_Primitive_Operation
7458                 (Call_Node       => New_Call_Node,
7459                  Node_To_Replace => Node_To_Replace)
7460
7461               or else
7462                 Try_Class_Wide_Operation
7463                   (Call_Node       => New_Call_Node,
7464                    Node_To_Replace => Node_To_Replace)
7465             then
7466                null;
7467             end if;
7468
7469          else
7470             Analyze_One_Call
7471               (N          => New_Call_Node,
7472                Nam        => Candidate,
7473                Report     => True,
7474                Success    => Success,
7475                Skip_First => True);
7476          end if;
7477
7478          --  No need for further errors
7479
7480          return True;
7481
7482       else
7483          --  There was no candidate operation, so report it as an error
7484          --  in the caller: Analyze_Selected_Component.
7485
7486          return False;
7487       end if;
7488    end Try_Object_Operation;
7489
7490    ---------
7491    -- wpo --
7492    ---------
7493
7494    procedure wpo (T : Entity_Id) is
7495       Op : Entity_Id;
7496       E  : Elmt_Id;
7497
7498    begin
7499       if not Is_Tagged_Type (T) then
7500          return;
7501       end if;
7502
7503       E := First_Elmt (Primitive_Operations (Base_Type (T)));
7504       while Present (E) loop
7505          Op := Node (E);
7506          Write_Int (Int (Op));
7507          Write_Str (" === ");
7508          Write_Name (Chars (Op));
7509          Write_Str (" in ");
7510          Write_Name (Chars (Scope (Op)));
7511          Next_Elmt (E);
7512          Write_Eol;
7513       end loop;
7514    end wpo;
7515
7516 end Sem_Ch4;