OSDN Git Service

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