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