OSDN Git Service

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