OSDN Git Service

2005-03-17 Javier Miranda <miranda@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_ch4.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              S E M _ C H 4                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2005, 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 2,  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 COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Checks;   use Checks;
29 with Debug;    use Debug;
30 with Einfo;    use Einfo;
31 with Elists;   use Elists;
32 with Errout;   use Errout;
33 with Exp_Util; use Exp_Util;
34 with Fname;    use Fname;
35 with Itypes;   use Itypes;
36 with Lib;      use Lib;
37 with Lib.Xref; use Lib.Xref;
38 with Namet;    use Namet;
39 with Nlists;   use Nlists;
40 with Nmake;    use Nmake;
41 with Opt;      use Opt;
42 with Output;   use Output;
43 with Restrict; use Restrict;
44 with Rident;   use Rident;
45 with Sem;      use Sem;
46 with Sem_Cat;  use Sem_Cat;
47 with Sem_Ch3;  use Sem_Ch3;
48 with Sem_Ch8;  use Sem_Ch8;
49 with Sem_Dist; use Sem_Dist;
50 with Sem_Eval; use Sem_Eval;
51 with Sem_Res;  use Sem_Res;
52 with Sem_Util; use Sem_Util;
53 with Sem_Type; use Sem_Type;
54 with Stand;    use Stand;
55 with Sinfo;    use Sinfo;
56 with Snames;   use Snames;
57 with Tbuild;   use Tbuild;
58
59 with GNAT.Spelling_Checker; use GNAT.Spelling_Checker;
60
61 package body Sem_Ch4 is
62
63    -----------------------
64    -- Local Subprograms --
65    -----------------------
66
67    procedure Analyze_Expression (N : Node_Id);
68    --  For expressions that are not names, this is just a call to analyze.
69    --  If the expression is a name, it may be a call to a parameterless
70    --  function, and if so must be converted into an explicit call node
71    --  and analyzed as such. This deproceduring must be done during the first
72    --  pass of overload resolution, because otherwise a procedure call with
73    --  overloaded actuals may fail to resolve. See 4327-001 for an example.
74
75    procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id);
76    --  Analyze a call of the form "+"(x, y), etc. The prefix of the call
77    --  is an operator name or an expanded name whose selector is an operator
78    --  name, and one possible interpretation is as a predefined operator.
79
80    procedure Analyze_Overloaded_Selected_Component (N : Node_Id);
81    --  If the prefix of a selected_component is overloaded, the proper
82    --  interpretation that yields a record type with the proper selector
83    --  name must be selected.
84
85    procedure Analyze_User_Defined_Binary_Op (N : Node_Id; Op_Id : Entity_Id);
86    --  Procedure to analyze a user defined binary operator, which is resolved
87    --  like a function, but instead of a list of actuals it is presented
88    --  with the left and right operands of an operator node.
89
90    procedure Analyze_User_Defined_Unary_Op (N : Node_Id; Op_Id : Entity_Id);
91    --  Procedure to analyze a user defined unary operator, which is resolved
92    --  like a function, but instead of a list of actuals, it is presented with
93    --  the operand of the operator node.
94
95    procedure Ambiguous_Operands (N : Node_Id);
96    --  for equality, membership, and comparison operators with overloaded
97    --  arguments, list possible interpretations.
98
99    procedure Analyze_One_Call
100       (N       : Node_Id;
101        Nam     : Entity_Id;
102        Report  : Boolean;
103        Success : out Boolean);
104    --  Check one interpretation of an overloaded subprogram name for
105    --  compatibility with the types of the actuals in a call. If there is a
106    --  single interpretation which does not match, post error if Report is
107    --  set to True.
108    --
109    --  Nam is the entity that provides the formals against which the actuals
110    --  are checked. Nam is either the name of a subprogram, or the internal
111    --  subprogram type constructed for an access_to_subprogram. If the actuals
112    --  are compatible with Nam, then Nam is added to the list of candidate
113    --  interpretations for N, and Success is set to True.
114
115    procedure Check_Misspelled_Selector
116      (Prefix : Entity_Id;
117       Sel    : Node_Id);
118    --  Give possible misspelling diagnostic if Sel is likely to be
119    --  a misspelling of one of the selectors of the Prefix.
120    --  This is called by Analyze_Selected_Component after producing
121    --  an invalid selector error message.
122
123    function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean;
124    --  Verify that type T is declared in scope S. Used to find intepretations
125    --  for operators given by expanded names. This is abstracted as a separate
126    --  function to handle extensions to System, where S is System, but T is
127    --  declared in the extension.
128
129    procedure Find_Arithmetic_Types
130      (L, R  : Node_Id;
131       Op_Id : Entity_Id;
132       N     : Node_Id);
133    --  L and R are the operands of an arithmetic operator. Find
134    --  consistent pairs of interpretations for L and R that have a
135    --  numeric type consistent with the semantics of the operator.
136
137    procedure Find_Comparison_Types
138      (L, R  : Node_Id;
139       Op_Id : Entity_Id;
140       N     : Node_Id);
141    --  L and R are operands of a comparison operator. Find consistent
142    --  pairs of interpretations for L and R.
143
144    procedure Find_Concatenation_Types
145      (L, R  : Node_Id;
146       Op_Id : Entity_Id;
147       N     : Node_Id);
148    --  For the four varieties of concatenation
149
150    procedure Find_Equality_Types
151      (L, R  : Node_Id;
152       Op_Id : Entity_Id;
153       N     : Node_Id);
154    --  Ditto for equality operators
155
156    procedure Find_Boolean_Types
157      (L, R  : Node_Id;
158       Op_Id : Entity_Id;
159       N     : Node_Id);
160    --  Ditto for binary logical operations
161
162    procedure Find_Negation_Types
163      (R     : Node_Id;
164       Op_Id : Entity_Id;
165       N     : Node_Id);
166    --  Find consistent interpretation for operand of negation operator
167
168    procedure Find_Non_Universal_Interpretations
169      (N     : Node_Id;
170       R     : Node_Id;
171       Op_Id : Entity_Id;
172       T1    : Entity_Id);
173    --  For equality and comparison operators, the result is always boolean,
174    --  and the legality of the operation is determined from the visibility
175    --  of the operand types. If one of the operands has a universal interpre-
176    --  tation,  the legality check uses some compatible non-universal
177    --  interpretation of the other operand. N can be an operator node, or
178    --  a function call whose name is an operator designator.
179
180    procedure Find_Unary_Types
181      (R     : Node_Id;
182       Op_Id : Entity_Id;
183       N     : Node_Id);
184    --  Unary arithmetic types: plus, minus, abs
185
186    procedure Check_Arithmetic_Pair
187      (T1, T2 : Entity_Id;
188       Op_Id  : Entity_Id;
189       N      : Node_Id);
190    --  Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid
191    --  types for left and right operand. Determine whether they constitute
192    --  a valid pair for the given operator, and record the corresponding
193    --  interpretation of the operator node. The node N may be an operator
194    --  node (the usual case) or a function call whose prefix is an operator
195    --  designator. In  both cases Op_Id is the operator name itself.
196
197    procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
198    --  Give detailed information on overloaded call where none of the
199    --  interpretations match. N is the call node, Nam the designator for
200    --  the overloaded entity being called.
201
202    function Junk_Operand (N : Node_Id) return Boolean;
203    --  Test for an operand that is an inappropriate entity (e.g. a package
204    --  name or a label). If so, issue an error message and return True. If
205    --  the operand is not an inappropriate entity kind, return False.
206
207    procedure Operator_Check (N : Node_Id);
208    --  Verify that an operator has received some valid interpretation. If none
209    --  was found, determine whether a use clause would make the operation
210    --  legal. The variable Candidate_Type (defined in Sem_Type) is set for
211    --  every type compatible with the operator, even if the operator for the
212    --  type is not directly visible. The routine uses this type to emit a more
213    --  informative message.
214
215    procedure Process_Implicit_Dereference_Prefix
216      (E : Entity_Id;
217       P : Node_Id);
218    --  Called when P is the prefix of an implicit dereference, denoting an
219    --  object E. If in semantics only mode (-gnatc or generic), record that is
220    --  a reference to E. Normally, such a reference is generated only when the
221    --  implicit dereference is expanded into an explicit one. E may be empty,
222    --  in which case this procedure does nothing.
223
224    procedure Remove_Abstract_Operations (N : Node_Id);
225    --  Ada 2005: implementation of AI-310. An abstract non-dispatching
226    --  operation is not a candidate interpretation.
227
228    function Try_Indexed_Call
229      (N   : Node_Id;
230       Nam : Entity_Id;
231       Typ : Entity_Id) return Boolean;
232    --  If a function has defaults for all its actuals, a call to it may
233    --  in fact be an indexing on the result of the call. Try_Indexed_Call
234    --  attempts the interpretation as an indexing, prior to analysis as
235    --  a call. If both are possible,  the node is overloaded with both
236    --  interpretations (same symbol but two different types).
237
238    function Try_Indirect_Call
239      (N   : Node_Id;
240       Nam : Entity_Id;
241       Typ : Entity_Id) return Boolean;
242    --  Similarly, a function F that needs no actuals can return an access
243    --  to a subprogram, and the call F (X)  interpreted as F.all (X). In
244    --  this case the call may be overloaded with both interpretations.
245
246    function Try_Object_Operation (N : Node_Id) return Boolean;
247    --  Ada 2005 (AI-252): Give support to the object operation notation
248
249    ------------------------
250    -- Ambiguous_Operands --
251    ------------------------
252
253    procedure Ambiguous_Operands (N : Node_Id) is
254       procedure List_Operand_Interps (Opnd : Node_Id);
255
256       --------------------------
257       -- List_Operand_Interps --
258       --------------------------
259
260       procedure List_Operand_Interps (Opnd : Node_Id) is
261          Nam   : Node_Id;
262          Err   : Node_Id := N;
263
264       begin
265          if Is_Overloaded (Opnd) then
266             if Nkind (Opnd) in N_Op then
267                Nam := Opnd;
268             elsif Nkind (Opnd) = N_Function_Call then
269                Nam := Name (Opnd);
270             else
271                return;
272             end if;
273
274          else
275             return;
276          end if;
277
278          if Opnd = Left_Opnd (N) then
279             Error_Msg_N
280               ("\left operand has the following interpretations", N);
281          else
282             Error_Msg_N
283               ("\right operand has the following interpretations", N);
284             Err := Opnd;
285          end if;
286
287          List_Interps (Nam, Err);
288       end List_Operand_Interps;
289
290    --  Start of processing for Ambiguous_Operands
291
292    begin
293       if Nkind (N) = N_In
294         or else Nkind (N) = N_Not_In
295       then
296          Error_Msg_N ("ambiguous operands for membership",  N);
297
298       elsif Nkind (N) = N_Op_Eq
299         or else Nkind (N) = N_Op_Ne
300       then
301          Error_Msg_N ("ambiguous operands for equality",  N);
302
303       else
304          Error_Msg_N ("ambiguous operands for comparison",  N);
305       end if;
306
307       if All_Errors_Mode then
308          List_Operand_Interps (Left_Opnd  (N));
309          List_Operand_Interps (Right_Opnd (N));
310       else
311          Error_Msg_N ("\use -gnatf switch for details", N);
312       end if;
313    end Ambiguous_Operands;
314
315    -----------------------
316    -- Analyze_Aggregate --
317    -----------------------
318
319    --  Most of the analysis of Aggregates requires that the type be known,
320    --  and is therefore put off until resolution.
321
322    procedure Analyze_Aggregate (N : Node_Id) is
323    begin
324       if No (Etype (N)) then
325          Set_Etype (N, Any_Composite);
326       end if;
327    end Analyze_Aggregate;
328
329    -----------------------
330    -- Analyze_Allocator --
331    -----------------------
332
333    procedure Analyze_Allocator (N : Node_Id) is
334       Loc      : constant Source_Ptr := Sloc (N);
335       Sav_Errs : constant Nat        := Serious_Errors_Detected;
336       E        : Node_Id            := Expression (N);
337       Acc_Type : Entity_Id;
338       Type_Id  : Entity_Id;
339
340    begin
341       Check_Restriction (No_Allocators, N);
342
343       if Nkind (E) = N_Qualified_Expression then
344          Acc_Type := Create_Itype (E_Allocator_Type, N);
345          Set_Etype (Acc_Type, Acc_Type);
346          Init_Size_Align (Acc_Type);
347          Find_Type (Subtype_Mark (E));
348          Type_Id := Entity (Subtype_Mark (E));
349          Check_Fully_Declared (Type_Id, N);
350          Set_Directly_Designated_Type (Acc_Type, Type_Id);
351
352          if Is_Limited_Type (Type_Id)
353            and then Comes_From_Source (N)
354            and then not In_Instance_Body
355          then
356             --  Ada 2005 (AI-287): Do not post an error if the expression
357             --  corresponds to a limited aggregate. Limited aggregates
358             --  are checked in sem_aggr in a per-component manner
359             --  (compare with handling of Get_Value subprogram).
360
361             if Ada_Version >= Ada_05
362               and then Nkind (Expression (E)) = N_Aggregate
363             then
364                null;
365             else
366                Error_Msg_N ("initialization not allowed for limited types", N);
367                Explain_Limited_Type (Type_Id, N);
368             end if;
369          end if;
370
371          Analyze_And_Resolve (Expression (E), Type_Id);
372
373          --  A qualified expression requires an exact match of the type,
374          --  class-wide matching is not allowed.
375
376          if Is_Class_Wide_Type (Type_Id)
377            and then Base_Type (Etype (Expression (E))) /= Base_Type (Type_Id)
378          then
379             Wrong_Type (Expression (E), Type_Id);
380          end if;
381
382          Check_Non_Static_Context (Expression (E));
383
384          --  We don't analyze the qualified expression itself because it's
385          --  part of the allocator
386
387          Set_Etype  (E, Type_Id);
388
389       --  Case where no qualified expression is present
390
391       else
392          declare
393             Def_Id : Entity_Id;
394
395          begin
396             --  If the allocator includes a N_Subtype_Indication then a
397             --  constraint is present, otherwise the node is a subtype mark.
398             --  Introduce an explicit subtype declaration into the tree
399             --  defining some anonymous subtype and rewrite the allocator to
400             --  use this subtype rather than the subtype indication.
401
402             --  It is important to introduce the explicit subtype declaration
403             --  so that the bounds of the subtype indication are attached to
404             --  the tree in case the allocator is inside a generic unit.
405
406             if Nkind (E) = N_Subtype_Indication then
407
408                --  A constraint is only allowed for a composite type in Ada
409                --  95. In Ada 83, a constraint is also allowed for an
410                --  access-to-composite type, but the constraint is ignored.
411
412                Find_Type (Subtype_Mark (E));
413
414                if Is_Elementary_Type (Entity (Subtype_Mark (E))) then
415                   if not (Ada_Version = Ada_83
416                            and then Is_Access_Type (Entity (Subtype_Mark (E))))
417                   then
418                      Error_Msg_N ("constraint not allowed here", E);
419
420                      if Nkind (Constraint (E))
421                        = N_Index_Or_Discriminant_Constraint
422                      then
423                         Error_Msg_N
424                           ("\if qualified expression was meant, " &
425                               "use apostrophe", Constraint (E));
426                      end if;
427                   end if;
428
429                   --  Get rid of the bogus constraint:
430
431                   Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
432                   Analyze_Allocator (N);
433                   return;
434                end if;
435
436                if Expander_Active then
437                   Def_Id :=
438                     Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
439
440                   Insert_Action (E,
441                     Make_Subtype_Declaration (Loc,
442                       Defining_Identifier => Def_Id,
443                       Subtype_Indication  => Relocate_Node (E)));
444
445                   if Sav_Errs /= Serious_Errors_Detected
446                     and then Nkind (Constraint (E))
447                       = N_Index_Or_Discriminant_Constraint
448                   then
449                      Error_Msg_N
450                        ("if qualified expression was meant, " &
451                            "use apostrophe!", Constraint (E));
452                   end if;
453
454                   E := New_Occurrence_Of (Def_Id, Loc);
455                   Rewrite (Expression (N), E);
456                end if;
457             end if;
458
459             Type_Id := Process_Subtype (E, N);
460             Acc_Type := Create_Itype (E_Allocator_Type, N);
461             Set_Etype                    (Acc_Type, Acc_Type);
462             Init_Size_Align              (Acc_Type);
463             Set_Directly_Designated_Type (Acc_Type, Type_Id);
464             Check_Fully_Declared (Type_Id, N);
465
466             --  Ada 2005 (AI-231)
467
468             if Can_Never_Be_Null (Type_Id) then
469                Error_Msg_N ("(Ada 2005) qualified expression required",
470                             Expression (N));
471             end if;
472
473             --  Check restriction against dynamically allocated protected
474             --  objects. Note that when limited aggregates are supported,
475             --  a similar test should be applied to an allocator with a
476             --  qualified expression ???
477
478             if Is_Protected_Type (Type_Id) then
479                Check_Restriction (No_Protected_Type_Allocators, N);
480             end if;
481
482             --  Check for missing initialization. Skip this check if we already
483             --  had errors on analyzing the allocator, since in that case these
484             --  are probably cascaded errors
485
486             if Is_Indefinite_Subtype (Type_Id)
487               and then Serious_Errors_Detected = Sav_Errs
488             then
489                if Is_Class_Wide_Type (Type_Id) then
490                   Error_Msg_N
491                     ("initialization required in class-wide allocation", N);
492                else
493                   Error_Msg_N
494                     ("initialization required in unconstrained allocation", N);
495                end if;
496             end if;
497          end;
498       end if;
499
500       if Is_Abstract (Type_Id) then
501          Error_Msg_N ("cannot allocate abstract object", E);
502       end if;
503
504       if Has_Task (Designated_Type (Acc_Type)) then
505          Check_Restriction (No_Tasking, N);
506          Check_Restriction (Max_Tasks, N);
507          Check_Restriction (No_Task_Allocators, N);
508       end if;
509
510       --  If the No_Streams restriction is set, check that the type of the
511       --  object is not, and does not contain, any subtype derived from
512       --  Ada.Streams.Root_Stream_Type. Note that we guard the call to
513       --  Has_Stream just for efficiency reasons. There is no point in
514       --  spending time on a Has_Stream check if the restriction is not set.
515
516       if Restrictions.Set (No_Streams) then
517          if Has_Stream (Designated_Type (Acc_Type)) then
518             Check_Restriction (No_Streams, N);
519          end if;
520       end if;
521
522       Set_Etype (N, Acc_Type);
523
524       if not Is_Library_Level_Entity (Acc_Type) then
525          Check_Restriction (No_Local_Allocators, N);
526       end if;
527
528       --  Ada 2005 (AI-231): Static checks
529
530       if Ada_Version >= Ada_05
531         and then (Null_Exclusion_Present (N)
532                     or else Can_Never_Be_Null (Etype (N)))
533       then
534          Null_Exclusion_Static_Checks (N);
535       end if;
536
537       if Serious_Errors_Detected > Sav_Errs then
538          Set_Error_Posted (N);
539          Set_Etype (N, Any_Type);
540       end if;
541    end Analyze_Allocator;
542
543    ---------------------------
544    -- Analyze_Arithmetic_Op --
545    ---------------------------
546
547    procedure Analyze_Arithmetic_Op (N : Node_Id) is
548       L     : constant Node_Id := Left_Opnd (N);
549       R     : constant Node_Id := Right_Opnd (N);
550       Op_Id : Entity_Id;
551
552    begin
553       Candidate_Type := Empty;
554       Analyze_Expression (L);
555       Analyze_Expression (R);
556
557       --  If the entity is already set, the node is the instantiation of
558       --  a generic node with a non-local reference, or was manufactured
559       --  by a call to Make_Op_xxx. In either case the entity is known to
560       --  be valid, and we do not need to collect interpretations, instead
561       --  we just get the single possible interpretation.
562
563       Op_Id := Entity (N);
564
565       if Present (Op_Id) then
566          if Ekind (Op_Id) = E_Operator then
567
568             if (Nkind (N) = N_Op_Divide   or else
569                 Nkind (N) = N_Op_Mod      or else
570                 Nkind (N) = N_Op_Multiply or else
571                 Nkind (N) = N_Op_Rem)
572               and then Treat_Fixed_As_Integer (N)
573             then
574                null;
575             else
576                Set_Etype (N, Any_Type);
577                Find_Arithmetic_Types (L, R, Op_Id, N);
578             end if;
579
580          else
581             Set_Etype (N, Any_Type);
582             Add_One_Interp (N, Op_Id, Etype (Op_Id));
583          end if;
584
585       --  Entity is not already set, so we do need to collect interpretations
586
587       else
588          Op_Id := Get_Name_Entity_Id (Chars (N));
589          Set_Etype (N, Any_Type);
590
591          while Present (Op_Id) loop
592             if Ekind (Op_Id) = E_Operator
593               and then Present (Next_Entity (First_Entity (Op_Id)))
594             then
595                Find_Arithmetic_Types (L, R, Op_Id, N);
596
597             --  The following may seem superfluous, because an operator cannot
598             --  be generic, but this ignores the cleverness of the author of
599             --  ACVC bc1013a.
600
601             elsif Is_Overloadable (Op_Id) then
602                Analyze_User_Defined_Binary_Op (N, Op_Id);
603             end if;
604
605             Op_Id := Homonym (Op_Id);
606          end loop;
607       end if;
608
609       Operator_Check (N);
610    end Analyze_Arithmetic_Op;
611
612    ------------------
613    -- Analyze_Call --
614    ------------------
615
616    --  Function, procedure, and entry calls are checked here. The Name in
617    --  the call may be overloaded. The actuals have been analyzed and may
618    --  themselves be overloaded. On exit from this procedure, the node N
619    --  may have zero, one or more interpretations. In the first case an
620    --  error message is produced. In the last case, the node is flagged
621    --  as overloaded and the interpretations are collected in All_Interp.
622
623    --  If the name is an Access_To_Subprogram, it cannot be overloaded, but
624    --  the type-checking is similar to that of other calls.
625
626    procedure Analyze_Call (N : Node_Id) is
627       Actuals : constant List_Id := Parameter_Associations (N);
628       Nam     : Node_Id          := Name (N);
629       X       : Interp_Index;
630       It      : Interp;
631       Nam_Ent : Entity_Id;
632       Success : Boolean := False;
633
634       function Name_Denotes_Function return Boolean;
635       --  If the type of the name is an access to subprogram, this may be
636       --  the type of a name, or the return type of the function being called.
637       --  If the name is not an entity then it can denote a protected function.
638       --  Until we distinguish Etype from Return_Type, we must use this
639       --  routine to resolve the meaning of the name in the call.
640
641       ---------------------------
642       -- Name_Denotes_Function --
643       ---------------------------
644
645       function Name_Denotes_Function return Boolean is
646       begin
647          if Is_Entity_Name (Nam) then
648             return Ekind (Entity (Nam)) = E_Function;
649
650          elsif Nkind (Nam) = N_Selected_Component then
651             return Ekind (Entity (Selector_Name (Nam))) = E_Function;
652
653          else
654             return False;
655          end if;
656       end Name_Denotes_Function;
657
658    --  Start of processing for Analyze_Call
659
660    begin
661       --  Initialize the type of the result of the call to the error type,
662       --  which will be reset if the type is successfully resolved.
663
664       Set_Etype (N, Any_Type);
665
666       if not Is_Overloaded (Nam) then
667
668          --  Only one interpretation to check
669
670          if Ekind (Etype (Nam)) = E_Subprogram_Type then
671             Nam_Ent := Etype (Nam);
672
673          elsif Is_Access_Type (Etype (Nam))
674            and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type
675            and then not Name_Denotes_Function
676          then
677             Nam_Ent := Designated_Type (Etype (Nam));
678             Insert_Explicit_Dereference (Nam);
679
680          --  Selected component case. Simple entry or protected operation,
681          --  where the entry name is given by the selector name.
682
683          elsif Nkind (Nam) = N_Selected_Component then
684             Nam_Ent := Entity (Selector_Name (Nam));
685
686             if Ekind (Nam_Ent) /= E_Entry
687               and then Ekind (Nam_Ent) /= E_Entry_Family
688               and then Ekind (Nam_Ent) /= E_Function
689               and then Ekind (Nam_Ent) /= E_Procedure
690             then
691                Error_Msg_N ("name in call is not a callable entity", Nam);
692                Set_Etype (N, Any_Type);
693                return;
694             end if;
695
696          --  If the name is an Indexed component, it can be a call to a member
697          --  of an entry family. The prefix must be a selected component whose
698          --  selector is the entry. Analyze_Procedure_Call normalizes several
699          --  kinds of call into this form.
700
701          elsif Nkind (Nam) = N_Indexed_Component then
702
703             if Nkind (Prefix (Nam)) = N_Selected_Component then
704                Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
705             else
706                Error_Msg_N ("name in call is not a callable entity", Nam);
707                Set_Etype (N, Any_Type);
708                return;
709             end if;
710
711          elsif not Is_Entity_Name (Nam) then
712             Error_Msg_N ("name in call is not a callable entity", Nam);
713             Set_Etype (N, Any_Type);
714             return;
715
716          else
717             Nam_Ent := Entity (Nam);
718
719             --  If no interpretations, give error message
720
721             if not Is_Overloadable (Nam_Ent) then
722                declare
723                   L : constant Boolean   := Is_List_Member (N);
724                   K : constant Node_Kind := Nkind (Parent (N));
725
726                begin
727                   --  If the node is in a list whose parent is not an
728                   --  expression then it must be an attempted procedure call.
729
730                   if L and then K not in N_Subexpr then
731                      if Ekind (Entity (Nam)) = E_Generic_Procedure then
732                         Error_Msg_NE
733                           ("must instantiate generic procedure& before call",
734                            Nam, Entity (Nam));
735                      else
736                         Error_Msg_N
737                           ("procedure or entry name expected", Nam);
738                      end if;
739
740                   --  Check for tasking cases where only an entry call will do
741
742                   elsif not L
743                     and then (K = N_Entry_Call_Alternative
744                                or else K = N_Triggering_Alternative)
745                   then
746                      Error_Msg_N ("entry name expected", Nam);
747
748                   --  Otherwise give general error message
749
750                   else
751                      Error_Msg_N ("invalid prefix in call", Nam);
752                   end if;
753
754                   return;
755                end;
756             end if;
757          end if;
758
759          Analyze_One_Call (N, Nam_Ent, True, Success);
760
761       else
762          --  An overloaded selected component must denote overloaded
763          --  operations of a concurrent type. The interpretations are
764          --  attached to the simple name of those operations.
765
766          if Nkind (Nam) = N_Selected_Component then
767             Nam := Selector_Name (Nam);
768          end if;
769
770          Get_First_Interp (Nam, X, It);
771
772          while Present (It.Nam) loop
773             Nam_Ent := It.Nam;
774
775             --  Name may be call that returns an access to subprogram, or more
776             --  generally an overloaded expression one of whose interpretations
777             --  yields an access to subprogram. If the name is an entity, we
778             --  do not dereference, because the node is a call that returns
779             --  the access type: note difference between f(x), where the call
780             --  may return an access subprogram type, and f(x)(y), where the
781             --  type returned by the call to f is implicitly dereferenced to
782             --  analyze the outer call.
783
784             if Is_Access_Type (Nam_Ent) then
785                Nam_Ent := Designated_Type (Nam_Ent);
786
787             elsif Is_Access_Type (Etype (Nam_Ent))
788               and then not Is_Entity_Name (Nam)
789               and then Ekind (Designated_Type (Etype (Nam_Ent)))
790                                                           = E_Subprogram_Type
791             then
792                Nam_Ent := Designated_Type (Etype (Nam_Ent));
793             end if;
794
795             Analyze_One_Call (N, Nam_Ent, False, Success);
796
797             --  If the interpretation succeeds, mark the proper type of the
798             --  prefix (any valid candidate will do). If not, remove the
799             --  candidate interpretation. This only needs to be done for
800             --  overloaded protected operations, for other entities disambi-
801             --  guation is done directly in Resolve.
802
803             if Success then
804                Set_Etype (Nam, It.Typ);
805
806             elsif Nkind (Name (N)) = N_Selected_Component
807               or else Nkind (Name (N)) = N_Function_Call
808             then
809                Remove_Interp (X);
810             end if;
811
812             Get_Next_Interp (X, It);
813          end loop;
814
815          --  If the name is the result of a function call, it can only
816          --  be a call to a function returning an access to subprogram.
817          --  Insert explicit dereference.
818
819          if Nkind (Nam) = N_Function_Call then
820             Insert_Explicit_Dereference (Nam);
821          end if;
822
823          if Etype (N) = Any_Type then
824
825             --  None of the interpretations is compatible with the actuals
826
827             Diagnose_Call (N, Nam);
828
829             --  Special checks for uninstantiated put routines
830
831             if Nkind (N) = N_Procedure_Call_Statement
832               and then Is_Entity_Name (Nam)
833               and then Chars (Nam) = Name_Put
834               and then List_Length (Actuals) = 1
835             then
836                declare
837                   Arg : constant Node_Id := First (Actuals);
838                   Typ : Entity_Id;
839
840                begin
841                   if Nkind (Arg) = N_Parameter_Association then
842                      Typ := Etype (Explicit_Actual_Parameter (Arg));
843                   else
844                      Typ := Etype (Arg);
845                   end if;
846
847                   if Is_Signed_Integer_Type (Typ) then
848                      Error_Msg_N
849                        ("possible missing instantiation of " &
850                           "'Text_'I'O.'Integer_'I'O!", Nam);
851
852                   elsif Is_Modular_Integer_Type (Typ) then
853                      Error_Msg_N
854                        ("possible missing instantiation of " &
855                           "'Text_'I'O.'Modular_'I'O!", Nam);
856
857                   elsif Is_Floating_Point_Type (Typ) then
858                      Error_Msg_N
859                        ("possible missing instantiation of " &
860                           "'Text_'I'O.'Float_'I'O!", Nam);
861
862                   elsif Is_Ordinary_Fixed_Point_Type (Typ) then
863                      Error_Msg_N
864                        ("possible missing instantiation of " &
865                           "'Text_'I'O.'Fixed_'I'O!", Nam);
866
867                   elsif Is_Decimal_Fixed_Point_Type (Typ) then
868                      Error_Msg_N
869                        ("possible missing instantiation of " &
870                           "'Text_'I'O.'Decimal_'I'O!", Nam);
871
872                   elsif Is_Enumeration_Type (Typ) then
873                      Error_Msg_N
874                        ("possible missing instantiation of " &
875                           "'Text_'I'O.'Enumeration_'I'O!", Nam);
876                   end if;
877                end;
878             end if;
879
880          elsif not Is_Overloaded (N)
881            and then Is_Entity_Name (Nam)
882          then
883             --  Resolution yields a single interpretation. Verify that
884             --  is has the proper capitalization.
885
886             Set_Entity_With_Style_Check (Nam, Entity (Nam));
887             Generate_Reference (Entity (Nam), Nam);
888
889             Set_Etype (Nam, Etype (Entity (Nam)));
890          else
891             Remove_Abstract_Operations (N);
892          end if;
893
894          End_Interp_List;
895       end if;
896    end Analyze_Call;
897
898    ---------------------------
899    -- Analyze_Comparison_Op --
900    ---------------------------
901
902    procedure Analyze_Comparison_Op (N : Node_Id) is
903       L     : constant Node_Id := Left_Opnd (N);
904       R     : constant Node_Id := Right_Opnd (N);
905       Op_Id : Entity_Id        := Entity (N);
906
907    begin
908       Set_Etype (N, Any_Type);
909       Candidate_Type := Empty;
910
911       Analyze_Expression (L);
912       Analyze_Expression (R);
913
914       if Present (Op_Id) then
915          if Ekind (Op_Id) = E_Operator then
916             Find_Comparison_Types (L, R, Op_Id, N);
917          else
918             Add_One_Interp (N, Op_Id, Etype (Op_Id));
919          end if;
920
921          if Is_Overloaded (L) then
922             Set_Etype (L, Intersect_Types (L, R));
923          end if;
924
925       else
926          Op_Id := Get_Name_Entity_Id (Chars (N));
927          while Present (Op_Id) loop
928             if Ekind (Op_Id) = E_Operator then
929                Find_Comparison_Types (L, R, Op_Id, N);
930             else
931                Analyze_User_Defined_Binary_Op (N, Op_Id);
932             end if;
933
934             Op_Id := Homonym (Op_Id);
935          end loop;
936       end if;
937
938       Operator_Check (N);
939    end Analyze_Comparison_Op;
940
941    ---------------------------
942    -- Analyze_Concatenation --
943    ---------------------------
944
945    --  If the only one-dimensional array type in scope is String,
946    --  this is the resulting type of the operation. Otherwise there
947    --  will be a concatenation operation defined for each user-defined
948    --  one-dimensional array.
949
950    procedure Analyze_Concatenation (N : Node_Id) is
951       L     : constant Node_Id := Left_Opnd (N);
952       R     : constant Node_Id := Right_Opnd (N);
953       Op_Id : Entity_Id        := Entity (N);
954       LT    : Entity_Id;
955       RT    : Entity_Id;
956
957    begin
958       Set_Etype (N, Any_Type);
959       Candidate_Type := Empty;
960
961       Analyze_Expression (L);
962       Analyze_Expression (R);
963
964       --  If the entity is present, the  node appears in an instance,
965       --  and denotes a predefined concatenation operation. The resulting
966       --  type is obtained from the arguments when possible. If the arguments
967       --  are aggregates, the array type and the concatenation type must be
968       --  visible.
969
970       if Present (Op_Id) then
971          if Ekind (Op_Id) = E_Operator then
972
973             LT := Base_Type (Etype (L));
974             RT := Base_Type (Etype (R));
975
976             if Is_Array_Type (LT)
977               and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
978             then
979                Add_One_Interp (N, Op_Id, LT);
980
981             elsif Is_Array_Type (RT)
982               and then LT = Base_Type (Component_Type (RT))
983             then
984                Add_One_Interp (N, Op_Id, RT);
985
986             --  If one operand is a string type or a user-defined array type,
987             --  and the other is a literal, result is of the specific type.
988
989             elsif
990               (Root_Type (LT) = Standard_String
991                  or else Scope (LT) /= Standard_Standard)
992               and then Etype (R) = Any_String
993             then
994                Add_One_Interp (N, Op_Id, LT);
995
996             elsif
997               (Root_Type (RT) = Standard_String
998                  or else Scope (RT) /= Standard_Standard)
999               and then Etype (L) = Any_String
1000             then
1001                Add_One_Interp (N, Op_Id, RT);
1002
1003             elsif not Is_Generic_Type (Etype (Op_Id)) then
1004                Add_One_Interp (N, Op_Id, Etype (Op_Id));
1005
1006             else
1007                --  Type and its operations must be visible
1008
1009                Set_Entity (N, Empty);
1010                Analyze_Concatenation (N);
1011             end if;
1012
1013          else
1014             Add_One_Interp (N, Op_Id, Etype (Op_Id));
1015          end if;
1016
1017       else
1018          Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
1019          while Present (Op_Id) loop
1020             if Ekind (Op_Id) = E_Operator then
1021
1022                --  Do not consider operators declared in dead code, they can
1023                --  not be part of the resolution.
1024
1025                if Is_Eliminated (Op_Id) then
1026                   null;
1027                else
1028                   Find_Concatenation_Types (L, R, Op_Id, N);
1029                end if;
1030
1031             else
1032                Analyze_User_Defined_Binary_Op (N, Op_Id);
1033             end if;
1034
1035             Op_Id := Homonym (Op_Id);
1036          end loop;
1037       end if;
1038
1039       Operator_Check (N);
1040    end Analyze_Concatenation;
1041
1042    ------------------------------------
1043    -- Analyze_Conditional_Expression --
1044    ------------------------------------
1045
1046    procedure Analyze_Conditional_Expression (N : Node_Id) is
1047       Condition : constant Node_Id := First (Expressions (N));
1048       Then_Expr : constant Node_Id := Next (Condition);
1049       Else_Expr : constant Node_Id := Next (Then_Expr);
1050    begin
1051       Analyze_Expression (Condition);
1052       Analyze_Expression (Then_Expr);
1053       Analyze_Expression (Else_Expr);
1054       Set_Etype (N, Etype (Then_Expr));
1055    end Analyze_Conditional_Expression;
1056
1057    -------------------------
1058    -- Analyze_Equality_Op --
1059    -------------------------
1060
1061    procedure Analyze_Equality_Op (N : Node_Id) is
1062       Loc   : constant Source_Ptr := Sloc (N);
1063       L     : constant Node_Id := Left_Opnd (N);
1064       R     : constant Node_Id := Right_Opnd (N);
1065       Op_Id : Entity_Id;
1066
1067    begin
1068       Set_Etype (N, Any_Type);
1069       Candidate_Type := Empty;
1070
1071       Analyze_Expression (L);
1072       Analyze_Expression (R);
1073
1074       --  If the entity is set, the node is a generic instance with a non-local
1075       --  reference to the predefined operator or to a user-defined function.
1076       --  It can also be an inequality that is expanded into the negation of a
1077       --  call to a user-defined equality operator.
1078
1079       --  For the predefined case, the result is Boolean, regardless of the
1080       --  type of the  operands. The operands may even be limited, if they are
1081       --  generic actuals. If they are overloaded, label the left argument with
1082       --  the common type that must be present, or with the type of the formal
1083       --  of the user-defined function.
1084
1085       if Present (Entity (N)) then
1086          Op_Id := Entity (N);
1087
1088          if Ekind (Op_Id) = E_Operator then
1089             Add_One_Interp (N, Op_Id, Standard_Boolean);
1090          else
1091             Add_One_Interp (N, Op_Id, Etype (Op_Id));
1092          end if;
1093
1094          if Is_Overloaded (L) then
1095             if Ekind (Op_Id) = E_Operator then
1096                Set_Etype (L, Intersect_Types (L, R));
1097             else
1098                Set_Etype (L, Etype (First_Formal (Op_Id)));
1099             end if;
1100          end if;
1101
1102       else
1103          Op_Id := Get_Name_Entity_Id (Chars (N));
1104          while Present (Op_Id) loop
1105             if Ekind (Op_Id) = E_Operator then
1106                Find_Equality_Types (L, R, Op_Id, N);
1107             else
1108                Analyze_User_Defined_Binary_Op (N, Op_Id);
1109             end if;
1110
1111             Op_Id := Homonym (Op_Id);
1112          end loop;
1113       end if;
1114
1115       --  If there was no match, and the operator is inequality, this may
1116       --  be a case where inequality has not been made explicit, as for
1117       --  tagged types. Analyze the node as the negation of an equality
1118       --  operation. This cannot be done earlier, because before analysis
1119       --  we cannot rule out the presence of an explicit inequality.
1120
1121       if Etype (N) = Any_Type
1122         and then Nkind (N) = N_Op_Ne
1123       then
1124          Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
1125
1126          while Present (Op_Id) loop
1127
1128             if Ekind (Op_Id) = E_Operator then
1129                Find_Equality_Types (L, R, Op_Id, N);
1130             else
1131                Analyze_User_Defined_Binary_Op (N, Op_Id);
1132             end if;
1133
1134             Op_Id := Homonym (Op_Id);
1135          end loop;
1136
1137          if Etype (N) /= Any_Type then
1138             Op_Id := Entity (N);
1139
1140             Rewrite (N,
1141               Make_Op_Not (Loc,
1142                 Right_Opnd =>
1143                   Make_Op_Eq (Loc,
1144                     Left_Opnd =>  Relocate_Node (Left_Opnd (N)),
1145                     Right_Opnd => Relocate_Node (Right_Opnd (N)))));
1146
1147             Set_Entity (Right_Opnd (N), Op_Id);
1148             Analyze (N);
1149          end if;
1150       end if;
1151
1152       Operator_Check (N);
1153    end Analyze_Equality_Op;
1154
1155    ----------------------------------
1156    -- Analyze_Explicit_Dereference --
1157    ----------------------------------
1158
1159    procedure Analyze_Explicit_Dereference (N : Node_Id) is
1160       Loc   : constant Source_Ptr := Sloc (N);
1161       P     : constant Node_Id := Prefix (N);
1162       T     : Entity_Id;
1163       I     : Interp_Index;
1164       It    : Interp;
1165       New_N : Node_Id;
1166
1167       function Is_Function_Type return Boolean;
1168       --  Check whether node may be interpreted as an implicit function call
1169
1170       ----------------------
1171       -- Is_Function_Type --
1172       ----------------------
1173
1174       function Is_Function_Type return Boolean is
1175          I  : Interp_Index;
1176          It : Interp;
1177
1178       begin
1179          if not Is_Overloaded (N) then
1180             return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
1181               and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
1182
1183          else
1184             Get_First_Interp (N, I, It);
1185
1186             while Present (It.Nam) loop
1187                if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
1188                  or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
1189                then
1190                   return False;
1191                end if;
1192
1193                Get_Next_Interp (I, It);
1194             end loop;
1195
1196             return True;
1197          end if;
1198       end Is_Function_Type;
1199
1200    --  Start of processing for Analyze_Explicit_Deference
1201
1202    begin
1203       Analyze (P);
1204       Set_Etype (N, Any_Type);
1205
1206       --  Test for remote access to subprogram type, and if so return
1207       --  after rewriting the original tree.
1208
1209       if Remote_AST_E_Dereference (P) then
1210          return;
1211       end if;
1212
1213       --  Normal processing for other than remote access to subprogram type
1214
1215       if not Is_Overloaded (P) then
1216          if Is_Access_Type (Etype (P)) then
1217
1218             --  Set the Etype. We need to go thru Is_For_Access_Subtypes
1219             --  to avoid other problems caused by the Private_Subtype
1220             --  and it is safe to go to the Base_Type because this is the
1221             --  same as converting the access value to its Base_Type.
1222
1223             declare
1224                DT : Entity_Id := Designated_Type (Etype (P));
1225
1226             begin
1227                if Ekind (DT) = E_Private_Subtype
1228                  and then Is_For_Access_Subtype (DT)
1229                then
1230                   DT := Base_Type (DT);
1231                end if;
1232
1233                Set_Etype (N, DT);
1234             end;
1235
1236          elsif Etype (P) /= Any_Type then
1237             Error_Msg_N ("prefix of dereference must be an access type", N);
1238             return;
1239          end if;
1240
1241       else
1242          Get_First_Interp (P, I, It);
1243
1244          while Present (It.Nam) loop
1245             T := It.Typ;
1246
1247             if Is_Access_Type (T) then
1248                Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
1249             end if;
1250
1251             Get_Next_Interp (I, It);
1252          end loop;
1253
1254          End_Interp_List;
1255
1256          --  Error if no interpretation of the prefix has an access type
1257
1258          if Etype (N) = Any_Type then
1259             Error_Msg_N
1260               ("access type required in prefix of explicit dereference", P);
1261             Set_Etype (N, Any_Type);
1262             return;
1263          end if;
1264       end if;
1265
1266       if Is_Function_Type
1267         and then Nkind (Parent (N)) /= N_Indexed_Component
1268
1269         and then (Nkind (Parent (N)) /= N_Function_Call
1270                    or else N /= Name (Parent (N)))
1271
1272         and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
1273                    or else N /= Name (Parent (N)))
1274
1275         and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1276         and then (Nkind (Parent (N)) /= N_Attribute_Reference
1277                     or else
1278                       (Attribute_Name (Parent (N)) /= Name_Address
1279                         and then
1280                        Attribute_Name (Parent (N)) /= Name_Access))
1281       then
1282          --  Name is a function call with no actuals, in a context that
1283          --  requires deproceduring (including as an actual in an enclosing
1284          --  function or procedure call). We can conceive of pathological cases
1285          --  where the prefix might include functions that return access to
1286          --  subprograms and others that return a regular type. Disambiguation
1287          --  of those will have to take place in Resolve. See e.g. 7117-014.
1288
1289          New_N :=
1290            Make_Function_Call (Loc,
1291            Name => Make_Explicit_Dereference (Loc, P),
1292            Parameter_Associations => New_List);
1293
1294          --  If the prefix is overloaded, remove operations that have formals,
1295          --  we know that this is a parameterless call.
1296
1297          if Is_Overloaded (P) then
1298             Get_First_Interp (P, I, It);
1299             while Present (It.Nam) loop
1300                T := It.Typ;
1301
1302                if No (First_Formal (Base_Type (Designated_Type (T)))) then
1303                   Set_Etype (P, T);
1304                else
1305                   Remove_Interp (I);
1306                end if;
1307
1308                Get_Next_Interp (I, It);
1309             end loop;
1310          end if;
1311
1312          Rewrite (N, New_N);
1313          Analyze (N);
1314       end if;
1315
1316       --  A value of remote access-to-class-wide must not be dereferenced
1317       --  (RM E.2.2(16)).
1318
1319       Validate_Remote_Access_To_Class_Wide_Type (N);
1320    end Analyze_Explicit_Dereference;
1321
1322    ------------------------
1323    -- Analyze_Expression --
1324    ------------------------
1325
1326    procedure Analyze_Expression (N : Node_Id) is
1327    begin
1328       Analyze (N);
1329       Check_Parameterless_Call (N);
1330    end Analyze_Expression;
1331
1332    ------------------------------------
1333    -- Analyze_Indexed_Component_Form --
1334    ------------------------------------
1335
1336    procedure Analyze_Indexed_Component_Form (N : Node_Id) is
1337       P     : constant Node_Id := Prefix (N);
1338       Exprs : constant List_Id := Expressions (N);
1339       Exp   : Node_Id;
1340       P_T   : Entity_Id;
1341       E     : Node_Id;
1342       U_N   : Entity_Id;
1343
1344       procedure Process_Function_Call;
1345       --  Prefix in indexed component form is an overloadable entity,
1346       --  so the node is a function call. Reformat it as such.
1347
1348       procedure Process_Indexed_Component;
1349       --  Prefix in indexed component form is actually an indexed component.
1350       --  This routine processes it, knowing that the prefix is already
1351       --  resolved.
1352
1353       procedure Process_Indexed_Component_Or_Slice;
1354       --  An indexed component with a single index may designate a slice if
1355       --  the index is a subtype mark. This routine disambiguates these two
1356       --  cases by resolving the prefix to see if it is a subtype mark.
1357
1358       procedure Process_Overloaded_Indexed_Component;
1359       --  If the prefix of an indexed component is overloaded, the proper
1360       --  interpretation is selected by the index types and the context.
1361
1362       ---------------------------
1363       -- Process_Function_Call --
1364       ---------------------------
1365
1366       procedure Process_Function_Call is
1367          Actual : Node_Id;
1368
1369       begin
1370          Change_Node (N, N_Function_Call);
1371          Set_Name (N, P);
1372          Set_Parameter_Associations (N, Exprs);
1373
1374          Actual := First (Parameter_Associations (N));
1375          while Present (Actual) loop
1376             Analyze (Actual);
1377             Check_Parameterless_Call (Actual);
1378             Next_Actual (Actual);
1379          end loop;
1380
1381          Analyze_Call (N);
1382       end Process_Function_Call;
1383
1384       -------------------------------
1385       -- Process_Indexed_Component --
1386       -------------------------------
1387
1388       procedure Process_Indexed_Component is
1389          Exp          : Node_Id;
1390          Array_Type   : Entity_Id;
1391          Index        : Node_Id;
1392          Pent         : Entity_Id := Empty;
1393
1394       begin
1395          Exp := First (Exprs);
1396
1397          if Is_Overloaded (P) then
1398             Process_Overloaded_Indexed_Component;
1399
1400          else
1401             Array_Type := Etype (P);
1402
1403             if Is_Entity_Name (P) then
1404                Pent := Entity (P);
1405             elsif Nkind (P) = N_Selected_Component
1406               and then Is_Entity_Name (Selector_Name (P))
1407             then
1408                Pent := Entity (Selector_Name (P));
1409             end if;
1410
1411             --  Prefix must be appropriate for an array type, taking into
1412             --  account a possible implicit dereference.
1413
1414             if Is_Access_Type (Array_Type) then
1415                Array_Type := Designated_Type (Array_Type);
1416                Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
1417                Process_Implicit_Dereference_Prefix (Pent, P);
1418             end if;
1419
1420             if Is_Array_Type (Array_Type) then
1421                null;
1422
1423             elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
1424                Analyze (Exp);
1425                Set_Etype (N, Any_Type);
1426
1427                if not Has_Compatible_Type
1428                  (Exp, Entry_Index_Type (Pent))
1429                then
1430                   Error_Msg_N ("invalid index type in entry name", N);
1431
1432                elsif Present (Next (Exp)) then
1433                   Error_Msg_N ("too many subscripts in entry reference", N);
1434
1435                else
1436                   Set_Etype (N,  Etype (P));
1437                end if;
1438
1439                return;
1440
1441             elsif Is_Record_Type (Array_Type)
1442               and then Remote_AST_I_Dereference (P)
1443             then
1444                return;
1445
1446             elsif Array_Type = Any_Type then
1447                Set_Etype (N, Any_Type);
1448                return;
1449
1450             --  Here we definitely have a bad indexing
1451
1452             else
1453                if Nkind (Parent (N)) = N_Requeue_Statement
1454                  and then Present (Pent) and then Ekind (Pent) = E_Entry
1455                then
1456                   Error_Msg_N
1457                     ("REQUEUE does not permit parameters", First (Exprs));
1458
1459                elsif Is_Entity_Name (P)
1460                  and then Etype (P) = Standard_Void_Type
1461                then
1462                   Error_Msg_NE ("incorrect use of&", P, Entity (P));
1463
1464                else
1465                   Error_Msg_N ("array type required in indexed component", P);
1466                end if;
1467
1468                Set_Etype (N, Any_Type);
1469                return;
1470             end if;
1471
1472             Index := First_Index (Array_Type);
1473
1474             while Present (Index) and then Present (Exp) loop
1475                if not Has_Compatible_Type (Exp, Etype (Index)) then
1476                   Wrong_Type (Exp, Etype (Index));
1477                   Set_Etype (N, Any_Type);
1478                   return;
1479                end if;
1480
1481                Next_Index (Index);
1482                Next (Exp);
1483             end loop;
1484
1485             Set_Etype (N, Component_Type (Array_Type));
1486
1487             if Present (Index) then
1488                Error_Msg_N
1489                  ("too few subscripts in array reference", First (Exprs));
1490
1491             elsif Present (Exp) then
1492                Error_Msg_N ("too many subscripts in array reference", Exp);
1493             end if;
1494          end if;
1495       end Process_Indexed_Component;
1496
1497       ----------------------------------------
1498       -- Process_Indexed_Component_Or_Slice --
1499       ----------------------------------------
1500
1501       procedure Process_Indexed_Component_Or_Slice is
1502       begin
1503          Exp := First (Exprs);
1504          while Present (Exp) loop
1505             Analyze_Expression (Exp);
1506             Next (Exp);
1507          end loop;
1508
1509          Exp := First (Exprs);
1510
1511          --  If one index is present, and it is a subtype name, then the
1512          --  node denotes a slice (note that the case of an explicit range
1513          --  for a slice was already built as an N_Slice node in the first
1514          --  place, so that case is not handled here).
1515
1516          --  We use a replace rather than a rewrite here because this is one
1517          --  of the cases in which the tree built by the parser is plain wrong.
1518
1519          if No (Next (Exp))
1520            and then Is_Entity_Name (Exp)
1521            and then Is_Type (Entity (Exp))
1522          then
1523             Replace (N,
1524                Make_Slice (Sloc (N),
1525                  Prefix => P,
1526                  Discrete_Range => New_Copy (Exp)));
1527             Analyze (N);
1528
1529          --  Otherwise (more than one index present, or single index is not
1530          --  a subtype name), then we have the indexed component case.
1531
1532          else
1533             Process_Indexed_Component;
1534          end if;
1535       end Process_Indexed_Component_Or_Slice;
1536
1537       ------------------------------------------
1538       -- Process_Overloaded_Indexed_Component --
1539       ------------------------------------------
1540
1541       procedure Process_Overloaded_Indexed_Component is
1542          Exp   : Node_Id;
1543          I     : Interp_Index;
1544          It    : Interp;
1545          Typ   : Entity_Id;
1546          Index : Node_Id;
1547          Found : Boolean;
1548
1549       begin
1550          Set_Etype (N, Any_Type);
1551
1552          Get_First_Interp (P, I, It);
1553          while Present (It.Nam) loop
1554             Typ := It.Typ;
1555
1556             if Is_Access_Type (Typ) then
1557                Typ := Designated_Type (Typ);
1558                Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
1559             end if;
1560
1561             if Is_Array_Type (Typ) then
1562
1563                --  Got a candidate: verify that index types are compatible
1564
1565                Index := First_Index (Typ);
1566                Found := True;
1567                Exp := First (Exprs);
1568                while Present (Index) and then Present (Exp) loop
1569                   if Has_Compatible_Type (Exp, Etype (Index)) then
1570                      null;
1571                   else
1572                      Found := False;
1573                      Remove_Interp (I);
1574                      exit;
1575                   end if;
1576
1577                   Next_Index (Index);
1578                   Next (Exp);
1579                end loop;
1580
1581                if Found and then No (Index) and then No (Exp) then
1582                   Add_One_Interp (N,
1583                      Etype (Component_Type (Typ)),
1584                      Etype (Component_Type (Typ)));
1585                end if;
1586             end if;
1587
1588             Get_Next_Interp (I, It);
1589          end loop;
1590
1591          if Etype (N) = Any_Type then
1592             Error_Msg_N ("no legal interpetation for indexed component", N);
1593             Set_Is_Overloaded (N, False);
1594          end if;
1595
1596          End_Interp_List;
1597       end Process_Overloaded_Indexed_Component;
1598
1599    --  Start of processing for Analyze_Indexed_Component_Form
1600
1601    begin
1602       --  Get name of array, function or type
1603
1604       Analyze (P);
1605       if Nkind (N) = N_Function_Call
1606         or else Nkind (N) = N_Procedure_Call_Statement
1607       then
1608          --  If P is an explicit dereference whose prefix is of a
1609          --  remote access-to-subprogram type, then N has already
1610          --  been rewritten as a subprogram call and analyzed.
1611
1612          return;
1613       end if;
1614
1615       pragma Assert (Nkind (N) = N_Indexed_Component);
1616
1617       P_T := Base_Type (Etype (P));
1618
1619       if Is_Entity_Name (P)
1620         or else Nkind (P) = N_Operator_Symbol
1621       then
1622          U_N := Entity (P);
1623
1624          if Ekind (U_N) in Type_Kind then
1625
1626             --  Reformat node as a type conversion
1627
1628             E := Remove_Head (Exprs);
1629
1630             if Present (First (Exprs)) then
1631                Error_Msg_N
1632                 ("argument of type conversion must be single expression", N);
1633             end if;
1634
1635             Change_Node (N, N_Type_Conversion);
1636             Set_Subtype_Mark (N, P);
1637             Set_Etype (N, U_N);
1638             Set_Expression (N, E);
1639
1640             --  After changing the node, call for the specific Analysis
1641             --  routine directly, to avoid a double call to the expander.
1642
1643             Analyze_Type_Conversion (N);
1644             return;
1645          end if;
1646
1647          if Is_Overloadable (U_N) then
1648             Process_Function_Call;
1649
1650          elsif Ekind (Etype (P)) = E_Subprogram_Type
1651            or else (Is_Access_Type (Etype (P))
1652                       and then
1653                     Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type)
1654          then
1655             --  Call to access_to-subprogram with possible implicit dereference
1656
1657             Process_Function_Call;
1658
1659          elsif Is_Generic_Subprogram (U_N) then
1660
1661             --  A common beginner's (or C++ templates fan) error
1662
1663             Error_Msg_N ("generic subprogram cannot be called", N);
1664             Set_Etype (N, Any_Type);
1665             return;
1666
1667          else
1668             Process_Indexed_Component_Or_Slice;
1669          end if;
1670
1671       --  If not an entity name, prefix is an expression that may denote
1672       --  an array or an access-to-subprogram.
1673
1674       else
1675          if Ekind (P_T) = E_Subprogram_Type
1676            or else (Is_Access_Type (P_T)
1677                      and then
1678                     Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
1679          then
1680             Process_Function_Call;
1681
1682          elsif Nkind (P) = N_Selected_Component
1683            and then Is_Overloadable (Entity (Selector_Name (P)))
1684          then
1685             Process_Function_Call;
1686
1687          else
1688             --  Indexed component, slice, or a call to a member of a family
1689             --  entry, which will be converted to an entry call later.
1690
1691             Process_Indexed_Component_Or_Slice;
1692          end if;
1693       end if;
1694    end Analyze_Indexed_Component_Form;
1695
1696    ------------------------
1697    -- Analyze_Logical_Op --
1698    ------------------------
1699
1700    procedure Analyze_Logical_Op (N : Node_Id) is
1701       L     : constant Node_Id := Left_Opnd (N);
1702       R     : constant Node_Id := Right_Opnd (N);
1703       Op_Id : Entity_Id := Entity (N);
1704
1705    begin
1706       Set_Etype (N, Any_Type);
1707       Candidate_Type := Empty;
1708
1709       Analyze_Expression (L);
1710       Analyze_Expression (R);
1711
1712       if Present (Op_Id) then
1713
1714          if Ekind (Op_Id) = E_Operator then
1715             Find_Boolean_Types (L, R, Op_Id, N);
1716          else
1717             Add_One_Interp (N, Op_Id, Etype (Op_Id));
1718          end if;
1719
1720       else
1721          Op_Id := Get_Name_Entity_Id (Chars (N));
1722
1723          while Present (Op_Id) loop
1724             if Ekind (Op_Id) = E_Operator then
1725                Find_Boolean_Types (L, R, Op_Id, N);
1726             else
1727                Analyze_User_Defined_Binary_Op (N, Op_Id);
1728             end if;
1729
1730             Op_Id := Homonym (Op_Id);
1731          end loop;
1732       end if;
1733
1734       Operator_Check (N);
1735    end Analyze_Logical_Op;
1736
1737    ---------------------------
1738    -- Analyze_Membership_Op --
1739    ---------------------------
1740
1741    procedure Analyze_Membership_Op (N : Node_Id) is
1742       L     : constant Node_Id := Left_Opnd (N);
1743       R     : constant Node_Id := Right_Opnd (N);
1744
1745       Index : Interp_Index;
1746       It    : Interp;
1747       Found : Boolean := False;
1748       I_F   : Interp_Index;
1749       T_F   : Entity_Id;
1750
1751       procedure Try_One_Interp (T1 : Entity_Id);
1752       --  Routine to try one proposed interpretation. Note that the context
1753       --  of the operation plays no role in resolving the arguments, so that
1754       --  if there is more than one interpretation of the operands that is
1755       --  compatible with a membership test, the operation is ambiguous.
1756
1757       --------------------
1758       -- Try_One_Interp --
1759       --------------------
1760
1761       procedure Try_One_Interp (T1 : Entity_Id) is
1762       begin
1763          if Has_Compatible_Type (R, T1) then
1764             if Found
1765               and then Base_Type (T1) /= Base_Type (T_F)
1766             then
1767                It := Disambiguate (L, I_F, Index, Any_Type);
1768
1769                if It = No_Interp then
1770                   Ambiguous_Operands (N);
1771                   Set_Etype (L, Any_Type);
1772                   return;
1773
1774                else
1775                   T_F := It.Typ;
1776                end if;
1777
1778             else
1779                Found := True;
1780                T_F   := T1;
1781                I_F   := Index;
1782             end if;
1783
1784             Set_Etype (L, T_F);
1785          end if;
1786
1787       end Try_One_Interp;
1788
1789    --  Start of processing for Analyze_Membership_Op
1790
1791    begin
1792       Analyze_Expression (L);
1793
1794       if Nkind (R) = N_Range
1795         or else (Nkind (R) = N_Attribute_Reference
1796                   and then Attribute_Name (R) = Name_Range)
1797       then
1798          Analyze (R);
1799
1800          if not Is_Overloaded (L) then
1801             Try_One_Interp (Etype (L));
1802
1803          else
1804             Get_First_Interp (L, Index, It);
1805
1806             while Present (It.Typ) loop
1807                Try_One_Interp (It.Typ);
1808                Get_Next_Interp (Index, It);
1809             end loop;
1810          end if;
1811
1812       --  If not a range, it can only be a subtype mark, or else there
1813       --  is a more basic error, to be diagnosed in Find_Type.
1814
1815       else
1816          Find_Type (R);
1817
1818          if Is_Entity_Name (R) then
1819             Check_Fully_Declared (Entity (R), R);
1820          end if;
1821       end if;
1822
1823       --  Compatibility between expression and subtype mark or range is
1824       --  checked during resolution. The result of the operation is Boolean
1825       --  in any case.
1826
1827       Set_Etype (N, Standard_Boolean);
1828    end Analyze_Membership_Op;
1829
1830    ----------------------
1831    -- Analyze_Negation --
1832    ----------------------
1833
1834    procedure Analyze_Negation (N : Node_Id) is
1835       R     : constant Node_Id := Right_Opnd (N);
1836       Op_Id : Entity_Id := Entity (N);
1837
1838    begin
1839       Set_Etype (N, Any_Type);
1840       Candidate_Type := Empty;
1841
1842       Analyze_Expression (R);
1843
1844       if Present (Op_Id) then
1845          if Ekind (Op_Id) = E_Operator then
1846             Find_Negation_Types (R, Op_Id, N);
1847          else
1848             Add_One_Interp (N, Op_Id, Etype (Op_Id));
1849          end if;
1850
1851       else
1852          Op_Id := Get_Name_Entity_Id (Chars (N));
1853          while Present (Op_Id) loop
1854             if Ekind (Op_Id) = E_Operator then
1855                Find_Negation_Types (R, Op_Id, N);
1856             else
1857                Analyze_User_Defined_Unary_Op (N, Op_Id);
1858             end if;
1859
1860             Op_Id := Homonym (Op_Id);
1861          end loop;
1862       end if;
1863
1864       Operator_Check (N);
1865    end Analyze_Negation;
1866
1867    ------------------
1868    -- Analyze_Null --
1869    ------------------
1870
1871    procedure Analyze_Null (N : Node_Id) is
1872    begin
1873       Set_Etype (N, Any_Access);
1874    end Analyze_Null;
1875
1876    ----------------------
1877    -- Analyze_One_Call --
1878    ----------------------
1879
1880    procedure Analyze_One_Call
1881       (N       : Node_Id;
1882        Nam     : Entity_Id;
1883        Report  : Boolean;
1884        Success : out Boolean)
1885    is
1886       Actuals    : constant List_Id   := Parameter_Associations (N);
1887       Prev_T     : constant Entity_Id := Etype (N);
1888       Formal     : Entity_Id;
1889       Actual     : Node_Id;
1890       Is_Indexed : Boolean := False;
1891       Subp_Type  : constant Entity_Id := Etype (Nam);
1892       Norm_OK    : Boolean;
1893
1894       procedure Indicate_Name_And_Type;
1895       --  If candidate interpretation matches, indicate name and type of
1896       --  result on call node.
1897
1898       ----------------------------
1899       -- Indicate_Name_And_Type --
1900       ----------------------------
1901
1902       procedure Indicate_Name_And_Type is
1903       begin
1904          Add_One_Interp (N, Nam, Etype (Nam));
1905          Success := True;
1906
1907          --  If the prefix of the call is a name, indicate the entity
1908          --  being called. If it is not a name,  it is an expression that
1909          --  denotes an access to subprogram or else an entry or family. In
1910          --  the latter case, the name is a selected component, and the entity
1911          --  being called is noted on the selector.
1912
1913          if not Is_Type (Nam) then
1914             if Is_Entity_Name (Name (N))
1915               or else Nkind (Name (N)) = N_Operator_Symbol
1916             then
1917                Set_Entity (Name (N), Nam);
1918
1919             elsif Nkind (Name (N)) = N_Selected_Component then
1920                Set_Entity (Selector_Name (Name (N)),  Nam);
1921             end if;
1922          end if;
1923
1924          if Debug_Flag_E and not Report then
1925             Write_Str (" Overloaded call ");
1926             Write_Int (Int (N));
1927             Write_Str (" compatible with ");
1928             Write_Int (Int (Nam));
1929             Write_Eol;
1930          end if;
1931       end Indicate_Name_And_Type;
1932
1933    --  Start of processing for Analyze_One_Call
1934
1935    begin
1936       Success := False;
1937
1938       --  If the subprogram has no formals, or if all the formals have
1939       --  defaults, and the return type is an array type, the node may
1940       --  denote an indexing of the result of a parameterless call.
1941
1942       if Needs_No_Actuals (Nam)
1943         and then Present (Actuals)
1944       then
1945          if Is_Array_Type (Subp_Type) then
1946             Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type);
1947
1948          elsif Is_Access_Type (Subp_Type)
1949            and then Is_Array_Type (Designated_Type (Subp_Type))
1950          then
1951             Is_Indexed :=
1952               Try_Indexed_Call (N, Nam, Designated_Type (Subp_Type));
1953
1954          elsif Is_Access_Type (Subp_Type)
1955            and then Ekind (Designated_Type (Subp_Type))  = E_Subprogram_Type
1956          then
1957             Is_Indexed := Try_Indirect_Call (N, Nam, Subp_Type);
1958          end if;
1959
1960       end if;
1961
1962       Normalize_Actuals (N, Nam, (Report and not Is_Indexed), Norm_OK);
1963
1964       if not Norm_OK then
1965
1966          --  Mismatch in number or names of parameters
1967
1968          if Debug_Flag_E then
1969             Write_Str (" normalization fails in call ");
1970             Write_Int (Int (N));
1971             Write_Str (" with subprogram ");
1972             Write_Int (Int (Nam));
1973             Write_Eol;
1974          end if;
1975
1976       --  If the context expects a function call, discard any interpretation
1977       --  that is a procedure. If the node is not overloaded, leave as is for
1978       --  better error reporting when type mismatch is found.
1979
1980       elsif Nkind (N) = N_Function_Call
1981         and then Is_Overloaded (Name (N))
1982         and then Ekind (Nam) = E_Procedure
1983       then
1984          return;
1985
1986       --  Ditto for function calls in a procedure context
1987
1988       elsif Nkind (N) = N_Procedure_Call_Statement
1989          and then Is_Overloaded (Name (N))
1990          and then Etype (Nam) /= Standard_Void_Type
1991       then
1992          return;
1993
1994       elsif not Present (Actuals) then
1995
1996          --  If Normalize succeeds, then there are default parameters for
1997          --  all formals.
1998
1999          Indicate_Name_And_Type;
2000
2001       elsif Ekind (Nam) = E_Operator then
2002          if Nkind (N) = N_Procedure_Call_Statement then
2003             return;
2004          end if;
2005
2006          --  This can occur when the prefix of the call is an operator
2007          --  name or an expanded name whose selector is an operator name.
2008
2009          Analyze_Operator_Call (N, Nam);
2010
2011          if Etype (N) /= Prev_T then
2012
2013             --  There may be a user-defined operator that hides the
2014             --  current interpretation. We must check for this independently
2015             --  of the analysis of the call with the user-defined operation,
2016             --  because the parameter names may be wrong and yet the hiding
2017             --  takes place. Fixes b34014o.
2018
2019             if Is_Overloaded (Name (N)) then
2020                declare
2021                   I  : Interp_Index;
2022                   It : Interp;
2023
2024                begin
2025                   Get_First_Interp (Name (N), I, It);
2026                   while Present (It.Nam) loop
2027                      if Ekind (It.Nam) /= E_Operator
2028                         and then Hides_Op (It.Nam, Nam)
2029                         and then
2030                           Has_Compatible_Type
2031                             (First_Actual (N), Etype (First_Formal (It.Nam)))
2032                         and then (No (Next_Actual (First_Actual (N)))
2033                            or else Has_Compatible_Type
2034                             (Next_Actual (First_Actual (N)),
2035                              Etype (Next_Formal (First_Formal (It.Nam)))))
2036                      then
2037                         Set_Etype (N, Prev_T);
2038                         return;
2039                      end if;
2040
2041                      Get_Next_Interp (I, It);
2042                   end loop;
2043                end;
2044             end if;
2045
2046             --  If operator matches formals, record its name on the call.
2047             --  If the operator is overloaded, Resolve will select the
2048             --  correct one from the list of interpretations. The call
2049             --  node itself carries the first candidate.
2050
2051             Set_Entity (Name (N), Nam);
2052             Success := True;
2053
2054          elsif Report and then Etype (N) = Any_Type then
2055             Error_Msg_N ("incompatible arguments for operator", N);
2056          end if;
2057
2058       else
2059          --  Normalize_Actuals has chained the named associations in the
2060          --  correct order of the formals.
2061
2062          Actual := First_Actual (N);
2063          Formal := First_Formal (Nam);
2064          while Present (Actual) and then Present (Formal) loop
2065             if Nkind (Parent (Actual)) /= N_Parameter_Association
2066               or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
2067             then
2068                if Has_Compatible_Type (Actual, Etype (Formal)) then
2069                   Next_Actual (Actual);
2070                   Next_Formal (Formal);
2071
2072                else
2073                   if Debug_Flag_E then
2074                      Write_Str (" type checking fails in call ");
2075                      Write_Int (Int (N));
2076                      Write_Str (" with formal ");
2077                      Write_Int (Int (Formal));
2078                      Write_Str (" in subprogram ");
2079                      Write_Int (Int (Nam));
2080                      Write_Eol;
2081                   end if;
2082
2083                   if Report and not Is_Indexed then
2084                      Wrong_Type (Actual, Etype (Formal));
2085
2086                      if Nkind (Actual) = N_Op_Eq
2087                        and then Nkind (Left_Opnd (Actual)) = N_Identifier
2088                      then
2089                         Formal := First_Formal (Nam);
2090
2091                         while Present (Formal) loop
2092
2093                            if Chars (Left_Opnd (Actual)) = Chars (Formal) then
2094                               Error_Msg_N
2095                                 ("possible misspelling of `='>`!", Actual);
2096                               exit;
2097                            end if;
2098
2099                            Next_Formal (Formal);
2100                         end loop;
2101                      end if;
2102
2103                      if All_Errors_Mode then
2104                         Error_Msg_Sloc := Sloc (Nam);
2105
2106                         if Is_Overloadable (Nam)
2107                           and then Present (Alias (Nam))
2108                           and then not Comes_From_Source (Nam)
2109                         then
2110                            Error_Msg_NE
2111                              ("  =='> in call to &#(inherited)!", Actual, Nam);
2112
2113                         elsif Ekind (Nam) = E_Subprogram_Type then
2114                            declare
2115                               Access_To_Subprogram_Typ :
2116                                 constant Entity_Id :=
2117                                   Defining_Identifier
2118                                     (Associated_Node_For_Itype (Nam));
2119                            begin
2120                               Error_Msg_NE (
2121                                 "  =='> in call to dereference of &#!",
2122                                 Actual, Access_To_Subprogram_Typ);
2123                            end;
2124
2125                         else
2126                            Error_Msg_NE ("  =='> in call to &#!", Actual, Nam);
2127
2128                         end if;
2129                      end if;
2130                   end if;
2131
2132                   return;
2133                end if;
2134
2135             else
2136                --  Normalize_Actuals has verified that a default value exists
2137                --  for this formal. Current actual names a subsequent formal.
2138
2139                Next_Formal (Formal);
2140             end if;
2141          end loop;
2142
2143          --  On exit, all actuals match
2144
2145          Indicate_Name_And_Type;
2146       end if;
2147    end Analyze_One_Call;
2148
2149    ---------------------------
2150    -- Analyze_Operator_Call --
2151    ---------------------------
2152
2153    procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
2154       Op_Name : constant Name_Id := Chars (Op_Id);
2155       Act1    : constant Node_Id := First_Actual (N);
2156       Act2    : constant Node_Id := Next_Actual (Act1);
2157
2158    begin
2159       --  Binary operator case
2160
2161       if Present (Act2) then
2162
2163          --  If more than two operands, then not binary operator after all
2164
2165          if Present (Next_Actual (Act2)) then
2166             return;
2167
2168          elsif     Op_Name = Name_Op_Add
2169            or else Op_Name = Name_Op_Subtract
2170            or else Op_Name = Name_Op_Multiply
2171            or else Op_Name = Name_Op_Divide
2172            or else Op_Name = Name_Op_Mod
2173            or else Op_Name = Name_Op_Rem
2174            or else Op_Name = Name_Op_Expon
2175          then
2176             Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
2177
2178          elsif     Op_Name =  Name_Op_And
2179            or else Op_Name = Name_Op_Or
2180            or else Op_Name = Name_Op_Xor
2181          then
2182             Find_Boolean_Types (Act1, Act2, Op_Id, N);
2183
2184          elsif     Op_Name = Name_Op_Lt
2185            or else Op_Name = Name_Op_Le
2186            or else Op_Name = Name_Op_Gt
2187            or else Op_Name = Name_Op_Ge
2188          then
2189             Find_Comparison_Types (Act1, Act2, Op_Id,  N);
2190
2191          elsif     Op_Name = Name_Op_Eq
2192            or else Op_Name = Name_Op_Ne
2193          then
2194             Find_Equality_Types (Act1, Act2, Op_Id,  N);
2195
2196          elsif     Op_Name = Name_Op_Concat then
2197             Find_Concatenation_Types (Act1, Act2, Op_Id, N);
2198
2199          --  Is this else null correct, or should it be an abort???
2200
2201          else
2202             null;
2203          end if;
2204
2205       --  Unary operator case
2206
2207       else
2208          if Op_Name = Name_Op_Subtract or else
2209             Op_Name = Name_Op_Add      or else
2210             Op_Name = Name_Op_Abs
2211          then
2212             Find_Unary_Types (Act1, Op_Id, N);
2213
2214          elsif
2215             Op_Name = Name_Op_Not
2216          then
2217             Find_Negation_Types (Act1, Op_Id, N);
2218
2219          --  Is this else null correct, or should it be an abort???
2220
2221          else
2222             null;
2223          end if;
2224       end if;
2225    end Analyze_Operator_Call;
2226
2227    -------------------------------------------
2228    -- Analyze_Overloaded_Selected_Component --
2229    -------------------------------------------
2230
2231    procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
2232       Nam   : constant Node_Id := Prefix (N);
2233       Sel   : constant Node_Id := Selector_Name (N);
2234       Comp  : Entity_Id;
2235       I     : Interp_Index;
2236       It    : Interp;
2237       T     : Entity_Id;
2238
2239    begin
2240       Set_Etype (Sel, Any_Type);
2241
2242       Get_First_Interp (Nam, I, It);
2243       while Present (It.Typ) loop
2244          if Is_Access_Type (It.Typ) then
2245             T := Designated_Type (It.Typ);
2246             Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2247          else
2248             T := It.Typ;
2249          end if;
2250
2251          if Is_Record_Type (T) then
2252             Comp := First_Entity (T);
2253             while Present (Comp) loop
2254                if Chars (Comp) = Chars (Sel)
2255                  and then Is_Visible_Component (Comp)
2256                then
2257                   Set_Entity_With_Style_Check (Sel, Comp);
2258                   Generate_Reference (Comp, Sel);
2259
2260                   Set_Etype (Sel, Etype (Comp));
2261                   Add_One_Interp (N, Etype (Comp), Etype (Comp));
2262
2263                   --  This also specifies a candidate to resolve the name.
2264                   --  Further overloading will be resolved from context.
2265
2266                   Set_Etype (Nam, It.Typ);
2267                end if;
2268
2269                Next_Entity (Comp);
2270             end loop;
2271
2272          elsif Is_Concurrent_Type (T) then
2273             Comp := First_Entity (T);
2274             while Present (Comp)
2275               and then Comp /= First_Private_Entity (T)
2276             loop
2277                if Chars (Comp) = Chars (Sel) then
2278                   if Is_Overloadable (Comp) then
2279                      Add_One_Interp (Sel, Comp, Etype (Comp));
2280                   else
2281                      Set_Entity_With_Style_Check (Sel, Comp);
2282                      Generate_Reference (Comp, Sel);
2283                   end if;
2284
2285                   Set_Etype (Sel, Etype (Comp));
2286                   Set_Etype (N,   Etype (Comp));
2287                   Set_Etype (Nam, It.Typ);
2288
2289                   --  For access type case, introduce explicit deference for
2290                   --  more uniform treatment of entry calls.
2291
2292                   if Is_Access_Type (Etype (Nam)) then
2293                      Insert_Explicit_Dereference (Nam);
2294                      Error_Msg_NW
2295                        (Warn_On_Dereference, "?implicit dereference", N);
2296                   end if;
2297                end if;
2298
2299                Next_Entity (Comp);
2300             end loop;
2301
2302             Set_Is_Overloaded (N, Is_Overloaded (Sel));
2303          end if;
2304
2305          Get_Next_Interp (I, It);
2306       end loop;
2307
2308       if Etype (N) = Any_Type then
2309          Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
2310          Set_Entity (Sel, Any_Id);
2311          Set_Etype  (Sel, Any_Type);
2312       end if;
2313    end Analyze_Overloaded_Selected_Component;
2314
2315    ----------------------------------
2316    -- Analyze_Qualified_Expression --
2317    ----------------------------------
2318
2319    procedure Analyze_Qualified_Expression (N : Node_Id) is
2320       Mark : constant Entity_Id := Subtype_Mark (N);
2321       T    : Entity_Id;
2322
2323    begin
2324       Set_Etype (N, Any_Type);
2325       Find_Type (Mark);
2326       T := Entity (Mark);
2327
2328       if T = Any_Type then
2329          return;
2330       end if;
2331
2332       Check_Fully_Declared (T, N);
2333       Analyze_Expression (Expression (N));
2334       Set_Etype  (N, T);
2335    end Analyze_Qualified_Expression;
2336
2337    -------------------
2338    -- Analyze_Range --
2339    -------------------
2340
2341    procedure Analyze_Range (N : Node_Id) is
2342       L        : constant Node_Id := Low_Bound (N);
2343       H        : constant Node_Id := High_Bound (N);
2344       I1, I2   : Interp_Index;
2345       It1, It2 : Interp;
2346
2347       procedure Check_Common_Type (T1, T2 : Entity_Id);
2348       --  Verify the compatibility of two types,  and choose the
2349       --  non universal one if the other is universal.
2350
2351       procedure Check_High_Bound (T : Entity_Id);
2352       --  Test one interpretation of the low bound against all those
2353       --  of the high bound.
2354
2355       procedure Check_Universal_Expression (N : Node_Id);
2356       --  In Ada83, reject bounds of a universal range that are not
2357       --  literals or entity names.
2358
2359       -----------------------
2360       -- Check_Common_Type --
2361       -----------------------
2362
2363       procedure Check_Common_Type (T1, T2 : Entity_Id) is
2364       begin
2365          if Covers (T1, T2) or else Covers (T2, T1) then
2366             if T1 = Universal_Integer
2367               or else T1 = Universal_Real
2368               or else T1 = Any_Character
2369             then
2370                Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
2371
2372             elsif T1 = T2 then
2373                Add_One_Interp (N, T1, T1);
2374
2375             else
2376                Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
2377             end if;
2378          end if;
2379       end Check_Common_Type;
2380
2381       ----------------------
2382       -- Check_High_Bound --
2383       ----------------------
2384
2385       procedure Check_High_Bound (T : Entity_Id) is
2386       begin
2387          if not Is_Overloaded (H) then
2388             Check_Common_Type (T, Etype (H));
2389          else
2390             Get_First_Interp (H, I2, It2);
2391             while Present (It2.Typ) loop
2392                Check_Common_Type (T, It2.Typ);
2393                Get_Next_Interp (I2, It2);
2394             end loop;
2395          end if;
2396       end Check_High_Bound;
2397
2398       -----------------------------
2399       -- Is_Universal_Expression --
2400       -----------------------------
2401
2402       procedure Check_Universal_Expression (N : Node_Id) is
2403       begin
2404          if Etype (N) = Universal_Integer
2405            and then Nkind (N) /= N_Integer_Literal
2406            and then not Is_Entity_Name (N)
2407            and then Nkind (N) /= N_Attribute_Reference
2408          then
2409             Error_Msg_N ("illegal bound in discrete range", N);
2410          end if;
2411       end Check_Universal_Expression;
2412
2413    --  Start of processing for Analyze_Range
2414
2415    begin
2416       Set_Etype (N, Any_Type);
2417       Analyze_Expression (L);
2418       Analyze_Expression (H);
2419
2420       if Etype (L) = Any_Type or else Etype (H) = Any_Type then
2421          return;
2422
2423       else
2424          if not Is_Overloaded (L) then
2425             Check_High_Bound (Etype (L));
2426          else
2427             Get_First_Interp (L, I1, It1);
2428             while Present (It1.Typ) loop
2429                Check_High_Bound (It1.Typ);
2430                Get_Next_Interp (I1, It1);
2431             end loop;
2432          end if;
2433
2434          --  If result is Any_Type, then we did not find a compatible pair
2435
2436          if Etype (N) = Any_Type then
2437             Error_Msg_N ("incompatible types in range ", N);
2438          end if;
2439       end if;
2440
2441       if Ada_Version = Ada_83
2442         and then
2443           (Nkind (Parent (N)) = N_Loop_Parameter_Specification
2444              or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
2445       then
2446          Check_Universal_Expression (L);
2447          Check_Universal_Expression (H);
2448       end if;
2449    end Analyze_Range;
2450
2451    -----------------------
2452    -- Analyze_Reference --
2453    -----------------------
2454
2455    procedure Analyze_Reference (N : Node_Id) is
2456       P        : constant Node_Id := Prefix (N);
2457       Acc_Type : Entity_Id;
2458    begin
2459       Analyze (P);
2460       Acc_Type := Create_Itype (E_Allocator_Type, N);
2461       Set_Etype                    (Acc_Type,  Acc_Type);
2462       Init_Size_Align              (Acc_Type);
2463       Set_Directly_Designated_Type (Acc_Type, Etype (P));
2464       Set_Etype (N, Acc_Type);
2465    end Analyze_Reference;
2466
2467    --------------------------------
2468    -- Analyze_Selected_Component --
2469    --------------------------------
2470
2471    --  Prefix is a record type or a task or protected type. In the
2472    --  later case, the selector must denote a visible entry.
2473
2474    procedure Analyze_Selected_Component (N : Node_Id) is
2475       Name        : constant Node_Id := Prefix (N);
2476       Sel         : constant Node_Id := Selector_Name (N);
2477       Comp        : Entity_Id;
2478       Entity_List : Entity_Id;
2479       Prefix_Type : Entity_Id;
2480       Pent        : Entity_Id := Empty;
2481       Act_Decl    : Node_Id;
2482       In_Scope    : Boolean;
2483       Parent_N    : Node_Id;
2484
2485    --  Start of processing for Analyze_Selected_Component
2486
2487    begin
2488       Set_Etype (N, Any_Type);
2489
2490       if Is_Overloaded (Name) then
2491          Analyze_Overloaded_Selected_Component (N);
2492          return;
2493
2494       elsif Etype (Name) = Any_Type then
2495          Set_Entity (Sel, Any_Id);
2496          Set_Etype (Sel, Any_Type);
2497          return;
2498
2499       else
2500          --  Function calls that are prefixes of selected components must be
2501          --  fully resolved in case we need to build an actual subtype, or
2502          --  do some other operation requiring a fully resolved prefix.
2503
2504          --  Note: Resolving all Nkinds of nodes here doesn't work.
2505          --  (Breaks 2129-008) ???.
2506
2507          if Nkind (Name) = N_Function_Call then
2508             Resolve (Name);
2509          end if;
2510
2511          Prefix_Type := Etype (Name);
2512       end if;
2513
2514       if Is_Access_Type (Prefix_Type) then
2515
2516          --  A RACW object can never be used as prefix of a selected
2517          --  component since that means it is dereferenced without
2518          --  being a controlling operand of a dispatching operation
2519          --  (RM E.2.2(15)).
2520
2521          if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
2522            and then Comes_From_Source (N)
2523          then
2524             Error_Msg_N
2525               ("invalid dereference of a remote access to class-wide value",
2526                N);
2527
2528          --  Normal case of selected component applied to access type
2529
2530          else
2531             Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2532
2533             if Is_Entity_Name (Name) then
2534                Pent := Entity (Name);
2535             elsif Nkind (Name) = N_Selected_Component
2536               and then Is_Entity_Name (Selector_Name (Name))
2537             then
2538                Pent := Entity (Selector_Name (Name));
2539             end if;
2540
2541             Process_Implicit_Dereference_Prefix (Pent, Name);
2542          end if;
2543
2544          Prefix_Type := Designated_Type (Prefix_Type);
2545       end if;
2546
2547       if Ekind (Prefix_Type) = E_Private_Subtype then
2548          Prefix_Type := Base_Type (Prefix_Type);
2549       end if;
2550
2551       Entity_List := Prefix_Type;
2552
2553       --  For class-wide types, use the entity list of the root type. This
2554       --  indirection is specially important for private extensions because
2555       --  only the root type get switched (not the class-wide type).
2556
2557       if Is_Class_Wide_Type (Prefix_Type) then
2558          Entity_List := Root_Type (Prefix_Type);
2559       end if;
2560
2561       Comp := First_Entity (Entity_List);
2562
2563       --  If the selector has an original discriminant, the node appears in
2564       --  an instance. Replace the discriminant with the corresponding one
2565       --  in the current discriminated type. For nested generics, this must
2566       --  be done transitively, so note the new original discriminant.
2567
2568       if Nkind (Sel) = N_Identifier
2569         and then Present (Original_Discriminant (Sel))
2570       then
2571          Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
2572
2573          --  Mark entity before rewriting, for completeness and because
2574          --  subsequent semantic checks might examine the original node.
2575
2576          Set_Entity (Sel, Comp);
2577          Rewrite (Selector_Name (N),
2578            New_Occurrence_Of (Comp, Sloc (N)));
2579          Set_Original_Discriminant (Selector_Name (N), Comp);
2580          Set_Etype (N, Etype (Comp));
2581
2582          if Is_Access_Type (Etype (Name)) then
2583             Insert_Explicit_Dereference (Name);
2584             Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2585          end if;
2586
2587       elsif Is_Record_Type (Prefix_Type) then
2588
2589          --  Find component with given name
2590
2591          while Present (Comp) loop
2592             if Chars (Comp) = Chars (Sel)
2593               and then Is_Visible_Component (Comp)
2594             then
2595                Set_Entity_With_Style_Check (Sel, Comp);
2596                Generate_Reference (Comp, Sel);
2597
2598                Set_Etype (Sel, Etype (Comp));
2599
2600                if Ekind (Comp) = E_Discriminant then
2601                   if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
2602                      Error_Msg_N
2603                        ("cannot reference discriminant of Unchecked_Union",
2604                         Sel);
2605                   end if;
2606
2607                   if Is_Generic_Type (Prefix_Type)
2608                        or else
2609                      Is_Generic_Type (Root_Type (Prefix_Type))
2610                   then
2611                      Set_Original_Discriminant (Sel, Comp);
2612                   end if;
2613                end if;
2614
2615                --  Resolve the prefix early otherwise it is not possible to
2616                --  build the actual subtype of the component: it may need
2617                --  to duplicate this prefix and duplication is only allowed
2618                --  on fully resolved expressions.
2619
2620                Resolve (Name);
2621
2622                --  We never need an actual subtype for the case of a selection
2623                --  for a indexed component of a non-packed array, since in
2624                --  this case gigi generates all the checks and can find the
2625                --  necessary bounds information.
2626
2627                --  We also do not need an actual subtype for the case of
2628                --  a first, last, length, or range attribute applied to a
2629                --  non-packed array, since gigi can again get the bounds in
2630                --  these cases (gigi cannot handle the packed case, since it
2631                --  has the bounds of the packed array type, not the original
2632                --  bounds of the type). However, if the prefix is itself a
2633                --  selected component, as in a.b.c (i), gigi may regard a.b.c
2634                --  as a dynamic-sized temporary, so we do generate an actual
2635                --  subtype for this case.
2636
2637                Parent_N := Parent (N);
2638
2639                if not Is_Packed (Etype (Comp))
2640                  and then
2641                    ((Nkind (Parent_N) = N_Indexed_Component
2642                       and then Nkind (Name) /= N_Selected_Component)
2643                      or else
2644                       (Nkind (Parent_N) = N_Attribute_Reference
2645                          and then (Attribute_Name (Parent_N) = Name_First
2646                                      or else
2647                                    Attribute_Name (Parent_N) = Name_Last
2648                                      or else
2649                                    Attribute_Name (Parent_N) = Name_Length
2650                                      or else
2651                                    Attribute_Name (Parent_N) = Name_Range)))
2652                then
2653                   Set_Etype (N, Etype (Comp));
2654
2655                --  In all other cases, we currently build an actual subtype. It
2656                --  seems likely that many of these cases can be avoided, but
2657                --  right now, the front end makes direct references to the
2658                --  bounds (e.g. in generating a length check), and if we do
2659                --  not make an actual subtype, we end up getting a direct
2660                --  reference to a discriminant which will not do.
2661
2662                else
2663                   Act_Decl :=
2664                     Build_Actual_Subtype_Of_Component (Etype (Comp), N);
2665                   Insert_Action (N, Act_Decl);
2666
2667                   if No (Act_Decl) then
2668                      Set_Etype (N, Etype (Comp));
2669
2670                   else
2671                      --  Component type depends on discriminants. Enter the
2672                      --  main attributes of the subtype.
2673
2674                      declare
2675                         Subt : constant Entity_Id :=
2676                                  Defining_Identifier (Act_Decl);
2677
2678                      begin
2679                         Set_Etype (Subt, Base_Type (Etype (Comp)));
2680                         Set_Ekind (Subt, Ekind (Etype (Comp)));
2681                         Set_Etype (N, Subt);
2682                      end;
2683                   end if;
2684                end if;
2685
2686                return;
2687             end if;
2688
2689             Next_Entity (Comp);
2690          end loop;
2691
2692          --  Ada 2005 (AI-252)
2693
2694          if Ada_Version >= Ada_05
2695            and then Is_Tagged_Type (Prefix_Type)
2696            and then Try_Object_Operation (N)
2697          then
2698             return;
2699
2700             --  If the transformation fails, it will be necessary
2701             --  to redo the analysis with all errors enabled, to indicate
2702             --  candidate interpretations and reasons for each failure ???
2703
2704          end if;
2705
2706       elsif Is_Private_Type (Prefix_Type) then
2707
2708          --  Allow access only to discriminants of the type. If the
2709          --  type has no full view, gigi uses the parent type for
2710          --  the components, so we do the same here.
2711
2712          if No (Full_View (Prefix_Type)) then
2713             Entity_List := Root_Type (Base_Type (Prefix_Type));
2714             Comp := First_Entity (Entity_List);
2715          end if;
2716
2717          while Present (Comp) loop
2718             if Chars (Comp) = Chars (Sel) then
2719                if Ekind (Comp) = E_Discriminant then
2720                   Set_Entity_With_Style_Check (Sel, Comp);
2721                   Generate_Reference (Comp, Sel);
2722
2723                   Set_Etype (Sel, Etype (Comp));
2724                   Set_Etype (N,   Etype (Comp));
2725
2726                   if Is_Generic_Type (Prefix_Type)
2727                     or else
2728                      Is_Generic_Type (Root_Type (Prefix_Type))
2729                   then
2730                      Set_Original_Discriminant (Sel, Comp);
2731                   end if;
2732
2733                else
2734                   Error_Msg_NE
2735                     ("invisible selector for }",
2736                      N, First_Subtype (Prefix_Type));
2737                   Set_Entity (Sel, Any_Id);
2738                   Set_Etype (N, Any_Type);
2739                end if;
2740
2741                return;
2742             end if;
2743
2744             Next_Entity (Comp);
2745          end loop;
2746
2747       elsif Is_Concurrent_Type (Prefix_Type) then
2748
2749          --  Prefix is concurrent type. Find visible operation with given name
2750          --  For a task, this can only include entries or discriminants if
2751          --  the task type is not an enclosing scope. If it is an enclosing
2752          --  scope (e.g. in an inner task) then all entities are visible, but
2753          --  the prefix must denote the enclosing scope, i.e. can only be
2754          --  a direct name or an expanded name.
2755
2756          Set_Etype (Sel,  Any_Type);
2757          In_Scope := In_Open_Scopes (Prefix_Type);
2758
2759          while Present (Comp) loop
2760             if Chars (Comp) = Chars (Sel) then
2761                if Is_Overloadable (Comp) then
2762                   Add_One_Interp (Sel, Comp, Etype (Comp));
2763
2764                elsif Ekind (Comp) = E_Discriminant
2765                  or else Ekind (Comp) = E_Entry_Family
2766                  or else (In_Scope
2767                    and then Is_Entity_Name (Name))
2768                then
2769                   Set_Entity_With_Style_Check (Sel, Comp);
2770                   Generate_Reference (Comp, Sel);
2771
2772                else
2773                   goto Next_Comp;
2774                end if;
2775
2776                Set_Etype (Sel, Etype (Comp));
2777                Set_Etype (N,   Etype (Comp));
2778
2779                if Ekind (Comp) = E_Discriminant then
2780                   Set_Original_Discriminant (Sel, Comp);
2781                end if;
2782
2783                --  For access type case, introduce explicit deference for
2784                --  more uniform treatment of entry calls.
2785
2786                if Is_Access_Type (Etype (Name)) then
2787                   Insert_Explicit_Dereference (Name);
2788                   Error_Msg_NW
2789                     (Warn_On_Dereference, "?implicit dereference", N);
2790                end if;
2791             end if;
2792
2793             <<Next_Comp>>
2794                Next_Entity (Comp);
2795                exit when not In_Scope
2796                  and then
2797                    Comp = First_Private_Entity (Base_Type (Prefix_Type));
2798          end loop;
2799
2800          Set_Is_Overloaded (N, Is_Overloaded (Sel));
2801
2802       else
2803          --  Invalid prefix
2804
2805          Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
2806       end if;
2807
2808       --  If N still has no type, the component is not defined in the prefix
2809
2810       if Etype (N) = Any_Type then
2811
2812          --  If the prefix is a single concurrent object, use its name in
2813          --  the error message, rather than that of its anonymous type.
2814
2815          if Is_Concurrent_Type (Prefix_Type)
2816            and then Is_Internal_Name (Chars (Prefix_Type))
2817            and then not Is_Derived_Type (Prefix_Type)
2818            and then Is_Entity_Name (Name)
2819          then
2820
2821             Error_Msg_Node_2 := Entity (Name);
2822             Error_Msg_NE ("no selector& for&", N, Sel);
2823
2824             Check_Misspelled_Selector (Entity_List, Sel);
2825
2826          elsif Is_Generic_Type (Prefix_Type)
2827            and then Ekind (Prefix_Type) = E_Record_Type_With_Private
2828            and then Prefix_Type /= Etype (Prefix_Type)
2829            and then Is_Record_Type (Etype (Prefix_Type))
2830          then
2831             --  If this is a derived formal type, the parent may have a
2832             --  different visibility at this point. Try for an inherited
2833             --  component before reporting an error.
2834
2835             Set_Etype (Prefix (N), Etype (Prefix_Type));
2836             Analyze_Selected_Component (N);
2837             return;
2838
2839          elsif Ekind (Prefix_Type) = E_Record_Subtype_With_Private
2840            and then Is_Generic_Actual_Type (Prefix_Type)
2841            and then Present (Full_View (Prefix_Type))
2842          then
2843             --  Similarly, if this the actual for a formal derived type, the
2844             --  component inherited from the generic parent may not be visible
2845             --  in the actual, but the selected component is legal.
2846
2847             declare
2848                Comp : Entity_Id;
2849
2850             begin
2851                Comp :=
2852                  First_Component (Generic_Parent_Type (Parent (Prefix_Type)));
2853                while Present (Comp) loop
2854                   if Chars (Comp) = Chars (Sel) then
2855                      Set_Entity_With_Style_Check (Sel, Comp);
2856                      Set_Etype (Sel, Etype (Comp));
2857                      Set_Etype (N,   Etype (Comp));
2858                      exit;
2859                   end if;
2860
2861                   Next_Component (Comp);
2862                end loop;
2863
2864                pragma Assert (Etype (N) /= Any_Type);
2865             end;
2866
2867          else
2868             if Ekind (Prefix_Type) = E_Record_Subtype then
2869
2870                --  Check whether this is a component of the base type
2871                --  which is absent from a statically constrained subtype.
2872                --  This will raise constraint error at run-time, but is
2873                --  not a compile-time error. When the selector is illegal
2874                --  for base type as well fall through and generate a
2875                --  compilation error anyway.
2876
2877                Comp := First_Component (Base_Type (Prefix_Type));
2878                while Present (Comp) loop
2879                   if Chars (Comp) = Chars (Sel)
2880                     and then Is_Visible_Component (Comp)
2881                   then
2882                      Set_Entity_With_Style_Check (Sel, Comp);
2883                      Generate_Reference (Comp, Sel);
2884                      Set_Etype (Sel, Etype (Comp));
2885                      Set_Etype (N,   Etype (Comp));
2886
2887                      --  Emit appropriate message. Gigi will replace the
2888                      --  node subsequently with the appropriate Raise.
2889
2890                      Apply_Compile_Time_Constraint_Error
2891                        (N, "component not present in }?",
2892                         CE_Discriminant_Check_Failed,
2893                         Ent => Prefix_Type, Rep => False);
2894                      Set_Raises_Constraint_Error (N);
2895                      return;
2896                   end if;
2897
2898                   Next_Component (Comp);
2899                end loop;
2900
2901             end if;
2902
2903             Error_Msg_Node_2 := First_Subtype (Prefix_Type);
2904             Error_Msg_NE ("no selector& for}", N, Sel);
2905
2906             Check_Misspelled_Selector (Entity_List, Sel);
2907
2908          end if;
2909
2910          Set_Entity (Sel, Any_Id);
2911          Set_Etype (Sel, Any_Type);
2912       end if;
2913    end Analyze_Selected_Component;
2914
2915    ---------------------------
2916    -- Analyze_Short_Circuit --
2917    ---------------------------
2918
2919    procedure Analyze_Short_Circuit (N : Node_Id) is
2920       L   : constant Node_Id := Left_Opnd  (N);
2921       R   : constant Node_Id := Right_Opnd (N);
2922       Ind : Interp_Index;
2923       It  : Interp;
2924
2925    begin
2926       Analyze_Expression (L);
2927       Analyze_Expression (R);
2928       Set_Etype (N, Any_Type);
2929
2930       if not Is_Overloaded (L) then
2931
2932          if Root_Type (Etype (L)) = Standard_Boolean
2933            and then Has_Compatible_Type (R, Etype (L))
2934          then
2935             Add_One_Interp (N, Etype (L), Etype (L));
2936          end if;
2937
2938       else
2939          Get_First_Interp (L, Ind, It);
2940
2941          while Present (It.Typ) loop
2942             if Root_Type (It.Typ) = Standard_Boolean
2943               and then Has_Compatible_Type (R, It.Typ)
2944             then
2945                Add_One_Interp (N, It.Typ, It.Typ);
2946             end if;
2947
2948             Get_Next_Interp (Ind, It);
2949          end loop;
2950       end if;
2951
2952       --  Here we have failed to find an interpretation. Clearly we
2953       --  know that it is not the case that both operands can have
2954       --  an interpretation of Boolean, but this is by far the most
2955       --  likely intended interpretation. So we simply resolve both
2956       --  operands as Booleans, and at least one of these resolutions
2957       --  will generate an error message, and we do not need to give
2958       --  a further error message on the short circuit operation itself.
2959
2960       if Etype (N) = Any_Type then
2961          Resolve (L, Standard_Boolean);
2962          Resolve (R, Standard_Boolean);
2963          Set_Etype (N, Standard_Boolean);
2964       end if;
2965    end Analyze_Short_Circuit;
2966
2967    -------------------
2968    -- Analyze_Slice --
2969    -------------------
2970
2971    procedure Analyze_Slice (N : Node_Id) is
2972       P          : constant Node_Id := Prefix (N);
2973       D          : constant Node_Id := Discrete_Range (N);
2974       Array_Type : Entity_Id;
2975
2976       procedure Analyze_Overloaded_Slice;
2977       --  If the prefix is overloaded, select those interpretations that
2978       --  yield a one-dimensional array type.
2979
2980       ------------------------------
2981       -- Analyze_Overloaded_Slice --
2982       ------------------------------
2983
2984       procedure Analyze_Overloaded_Slice is
2985          I   : Interp_Index;
2986          It  : Interp;
2987          Typ : Entity_Id;
2988
2989       begin
2990          Set_Etype (N, Any_Type);
2991
2992          Get_First_Interp (P, I, It);
2993          while Present (It.Nam) loop
2994             Typ := It.Typ;
2995
2996             if Is_Access_Type (Typ) then
2997                Typ := Designated_Type (Typ);
2998                Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2999             end if;
3000
3001             if Is_Array_Type (Typ)
3002               and then Number_Dimensions (Typ) = 1
3003               and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
3004             then
3005                Add_One_Interp (N, Typ, Typ);
3006             end if;
3007
3008             Get_Next_Interp (I, It);
3009          end loop;
3010
3011          if Etype (N) = Any_Type then
3012             Error_Msg_N ("expect array type in prefix of slice",  N);
3013          end if;
3014       end Analyze_Overloaded_Slice;
3015
3016    --  Start of processing for Analyze_Slice
3017
3018    begin
3019       Analyze (P);
3020       Analyze (D);
3021
3022       if Is_Overloaded (P) then
3023          Analyze_Overloaded_Slice;
3024
3025       else
3026          Array_Type := Etype (P);
3027          Set_Etype (N, Any_Type);
3028
3029          if Is_Access_Type (Array_Type) then
3030             Array_Type := Designated_Type (Array_Type);
3031             Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3032          end if;
3033
3034          if not Is_Array_Type (Array_Type) then
3035             Wrong_Type (P, Any_Array);
3036
3037          elsif Number_Dimensions (Array_Type) > 1 then
3038             Error_Msg_N
3039               ("type is not one-dimensional array in slice prefix", N);
3040
3041          elsif not
3042            Has_Compatible_Type (D, Etype (First_Index (Array_Type)))
3043          then
3044             Wrong_Type (D, Etype (First_Index (Array_Type)));
3045
3046          else
3047             Set_Etype (N, Array_Type);
3048          end if;
3049       end if;
3050    end Analyze_Slice;
3051
3052    -----------------------------
3053    -- Analyze_Type_Conversion --
3054    -----------------------------
3055
3056    procedure Analyze_Type_Conversion (N : Node_Id) is
3057       Expr : constant Node_Id := Expression (N);
3058       T    : Entity_Id;
3059
3060    begin
3061       --  If Conversion_OK is set, then the Etype is already set, and the
3062       --  only processing required is to analyze the expression. This is
3063       --  used to construct certain "illegal" conversions which are not
3064       --  allowed by Ada semantics, but can be handled OK by Gigi, see
3065       --  Sinfo for further details.
3066
3067       if Conversion_OK (N) then
3068          Analyze (Expr);
3069          return;
3070       end if;
3071
3072       --  Otherwise full type analysis is required, as well as some semantic
3073       --  checks to make sure the argument of the conversion is appropriate.
3074
3075       Find_Type (Subtype_Mark (N));
3076       T := Entity (Subtype_Mark (N));
3077       Set_Etype (N, T);
3078       Check_Fully_Declared (T, N);
3079       Analyze_Expression (Expr);
3080       Validate_Remote_Type_Type_Conversion (N);
3081
3082       --  Only remaining step is validity checks on the argument. These
3083       --  are skipped if the conversion does not come from the source.
3084
3085       if not Comes_From_Source (N) then
3086          return;
3087
3088       elsif Nkind (Expr) = N_Null then
3089          Error_Msg_N ("argument of conversion cannot be null", N);
3090          Error_Msg_N ("\use qualified expression instead", N);
3091          Set_Etype (N, Any_Type);
3092
3093       elsif Nkind (Expr) = N_Aggregate then
3094          Error_Msg_N ("argument of conversion cannot be aggregate", N);
3095          Error_Msg_N ("\use qualified expression instead", N);
3096
3097       elsif Nkind (Expr) = N_Allocator then
3098          Error_Msg_N ("argument of conversion cannot be an allocator", N);
3099          Error_Msg_N ("\use qualified expression instead", N);
3100
3101       elsif Nkind (Expr) = N_String_Literal then
3102          Error_Msg_N ("argument of conversion cannot be string literal", N);
3103          Error_Msg_N ("\use qualified expression instead", N);
3104
3105       elsif Nkind (Expr) = N_Character_Literal then
3106          if Ada_Version = Ada_83 then
3107             Resolve (Expr, T);
3108          else
3109             Error_Msg_N ("argument of conversion cannot be character literal",
3110               N);
3111             Error_Msg_N ("\use qualified expression instead", N);
3112          end if;
3113
3114       elsif Nkind (Expr) = N_Attribute_Reference
3115         and then
3116           (Attribute_Name (Expr) = Name_Access            or else
3117            Attribute_Name (Expr) = Name_Unchecked_Access  or else
3118            Attribute_Name (Expr) = Name_Unrestricted_Access)
3119       then
3120          Error_Msg_N ("argument of conversion cannot be access", N);
3121          Error_Msg_N ("\use qualified expression instead", N);
3122       end if;
3123    end Analyze_Type_Conversion;
3124
3125    ----------------------
3126    -- Analyze_Unary_Op --
3127    ----------------------
3128
3129    procedure Analyze_Unary_Op (N : Node_Id) is
3130       R     : constant Node_Id := Right_Opnd (N);
3131       Op_Id : Entity_Id := Entity (N);
3132
3133    begin
3134       Set_Etype (N, Any_Type);
3135       Candidate_Type := Empty;
3136
3137       Analyze_Expression (R);
3138
3139       if Present (Op_Id) then
3140          if Ekind (Op_Id) = E_Operator then
3141             Find_Unary_Types (R, Op_Id,  N);
3142          else
3143             Add_One_Interp (N, Op_Id, Etype (Op_Id));
3144          end if;
3145
3146       else
3147          Op_Id := Get_Name_Entity_Id (Chars (N));
3148          while Present (Op_Id) loop
3149             if Ekind (Op_Id) = E_Operator then
3150                if No (Next_Entity (First_Entity (Op_Id))) then
3151                   Find_Unary_Types (R, Op_Id,  N);
3152                end if;
3153
3154             elsif Is_Overloadable (Op_Id) then
3155                Analyze_User_Defined_Unary_Op (N, Op_Id);
3156             end if;
3157
3158             Op_Id := Homonym (Op_Id);
3159          end loop;
3160       end if;
3161
3162       Operator_Check (N);
3163    end Analyze_Unary_Op;
3164
3165    ----------------------------------
3166    -- Analyze_Unchecked_Expression --
3167    ----------------------------------
3168
3169    procedure Analyze_Unchecked_Expression (N : Node_Id) is
3170    begin
3171       Analyze (Expression (N), Suppress => All_Checks);
3172       Set_Etype (N, Etype (Expression (N)));
3173       Save_Interps (Expression (N), N);
3174    end Analyze_Unchecked_Expression;
3175
3176    ---------------------------------------
3177    -- Analyze_Unchecked_Type_Conversion --
3178    ---------------------------------------
3179
3180    procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
3181    begin
3182       Find_Type (Subtype_Mark (N));
3183       Analyze_Expression (Expression (N));
3184       Set_Etype (N, Entity (Subtype_Mark (N)));
3185    end Analyze_Unchecked_Type_Conversion;
3186
3187    ------------------------------------
3188    -- Analyze_User_Defined_Binary_Op --
3189    ------------------------------------
3190
3191    procedure Analyze_User_Defined_Binary_Op
3192      (N     : Node_Id;
3193       Op_Id : Entity_Id)
3194    is
3195    begin
3196       --  Only do analysis if the operator Comes_From_Source, since otherwise
3197       --  the operator was generated by the expander, and all such operators
3198       --  always refer to the operators in package Standard.
3199
3200       if Comes_From_Source (N) then
3201          declare
3202             F1 : constant Entity_Id := First_Formal (Op_Id);
3203             F2 : constant Entity_Id := Next_Formal (F1);
3204
3205          begin
3206             --  Verify that Op_Id is a visible binary function. Note that since
3207             --  we know Op_Id is overloaded, potentially use visible means use
3208             --  visible for sure (RM 9.4(11)).
3209
3210             if Ekind (Op_Id) = E_Function
3211               and then Present (F2)
3212               and then (Is_Immediately_Visible (Op_Id)
3213                          or else Is_Potentially_Use_Visible (Op_Id))
3214               and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
3215               and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
3216             then
3217                Add_One_Interp (N, Op_Id, Etype (Op_Id));
3218
3219                if Debug_Flag_E then
3220                   Write_Str ("user defined operator ");
3221                   Write_Name (Chars (Op_Id));
3222                   Write_Str (" on node ");
3223                   Write_Int (Int (N));
3224                   Write_Eol;
3225                end if;
3226             end if;
3227          end;
3228       end if;
3229    end Analyze_User_Defined_Binary_Op;
3230
3231    -----------------------------------
3232    -- Analyze_User_Defined_Unary_Op --
3233    -----------------------------------
3234
3235    procedure Analyze_User_Defined_Unary_Op
3236      (N     : Node_Id;
3237       Op_Id : Entity_Id)
3238    is
3239    begin
3240       --  Only do analysis if the operator Comes_From_Source, since otherwise
3241       --  the operator was generated by the expander, and all such operators
3242       --  always refer to the operators in package Standard.
3243
3244       if Comes_From_Source (N) then
3245          declare
3246             F : constant Entity_Id := First_Formal (Op_Id);
3247
3248          begin
3249             --  Verify that Op_Id is a visible unary function. Note that since
3250             --  we know Op_Id is overloaded, potentially use visible means use
3251             --  visible for sure (RM 9.4(11)).
3252
3253             if Ekind (Op_Id) = E_Function
3254               and then No (Next_Formal (F))
3255               and then (Is_Immediately_Visible (Op_Id)
3256                          or else Is_Potentially_Use_Visible (Op_Id))
3257               and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
3258             then
3259                Add_One_Interp (N, Op_Id, Etype (Op_Id));
3260             end if;
3261          end;
3262       end if;
3263    end Analyze_User_Defined_Unary_Op;
3264
3265    ---------------------------
3266    -- Check_Arithmetic_Pair --
3267    ---------------------------
3268
3269    procedure Check_Arithmetic_Pair
3270      (T1, T2 : Entity_Id;
3271       Op_Id  : Entity_Id;
3272       N      : Node_Id)
3273    is
3274       Op_Name : constant Name_Id   := Chars (Op_Id);
3275
3276       function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
3277       --  Check whether the fixed-point type Typ has a user-defined operator
3278       --  (multiplication or division) that should hide the corresponding
3279       --  predefined operator. Used to implement Ada 2005 AI-264, to make
3280       --  such operators more visible and therefore useful.
3281
3282       function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
3283       --  Get specific type (i.e. non-universal type if there is one)
3284
3285       ------------------
3286       -- Has_Fixed_Op --
3287       ------------------
3288
3289       function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
3290          Ent : Entity_Id;
3291          F1  : Entity_Id;
3292          F2  : Entity_Id;
3293
3294       begin
3295          --  The operation is treated as primitive if it is declared in the
3296          --  same scope as the type, and therefore on the same entity chain.
3297
3298          Ent := Next_Entity (Typ);
3299          while Present (Ent) loop
3300             if Chars (Ent) = Chars (Op) then
3301                F1 := First_Formal (Ent);
3302                F2 := Next_Formal (F1);
3303
3304                --  The operation counts as primitive if either operand or
3305                --  result are of the given type, and both operands are fixed
3306                --  point types.
3307
3308                if (Etype (F1) = Typ
3309                     and then Is_Fixed_Point_Type (Etype (F2)))
3310
3311                  or else
3312                    (Etype (F2) = Typ
3313                      and then Is_Fixed_Point_Type (Etype (F1)))
3314
3315                  or else
3316                    (Etype (Ent) = Typ
3317                      and then Is_Fixed_Point_Type (Etype (F1))
3318                      and then Is_Fixed_Point_Type (Etype (F2)))
3319                then
3320                   return True;
3321                end if;
3322             end if;
3323
3324             Next_Entity (Ent);
3325          end loop;
3326
3327          return False;
3328       end Has_Fixed_Op;
3329
3330       -------------------
3331       -- Specific_Type --
3332       -------------------
3333
3334       function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
3335       begin
3336          if T1 = Universal_Integer or else T1 = Universal_Real then
3337             return Base_Type (T2);
3338          else
3339             return Base_Type (T1);
3340          end if;
3341       end Specific_Type;
3342
3343    --  Start of processing for Check_Arithmetic_Pair
3344
3345    begin
3346       if Op_Name = Name_Op_Add or else Op_Name = Name_Op_Subtract then
3347
3348          if Is_Numeric_Type (T1)
3349            and then Is_Numeric_Type (T2)
3350            and then (Covers (T1, T2) or else Covers (T2, T1))
3351          then
3352             Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
3353          end if;
3354
3355       elsif Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide then
3356
3357          if Is_Fixed_Point_Type (T1)
3358            and then (Is_Fixed_Point_Type (T2)
3359                        or else T2 = Universal_Real)
3360          then
3361             --  If Treat_Fixed_As_Integer is set then the Etype is already set
3362             --  and no further processing is required (this is the case of an
3363             --  operator constructed by Exp_Fixd for a fixed point operation)
3364             --  Otherwise add one interpretation with universal fixed result
3365             --  If the operator is given in  functional notation, it comes
3366             --  from source and Fixed_As_Integer cannot apply.
3367
3368             if (Nkind (N) not in N_Op
3369                  or else not Treat_Fixed_As_Integer (N))
3370               and then
3371                 (not (Ada_Version >= Ada_05 and then Has_Fixed_Op (T1, Op_Id))
3372                   or else Nkind (Parent (N)) = N_Type_Conversion)
3373             then
3374                Add_One_Interp (N, Op_Id, Universal_Fixed);
3375             end if;
3376
3377          elsif Is_Fixed_Point_Type (T2)
3378            and then (Nkind (N) not in N_Op
3379                       or else not Treat_Fixed_As_Integer (N))
3380            and then T1 = Universal_Real
3381            and then
3382              (not (Ada_Version >= Ada_05 and then Has_Fixed_Op (T1, Op_Id))
3383                or else Nkind (Parent (N)) = N_Type_Conversion)
3384          then
3385             Add_One_Interp (N, Op_Id, Universal_Fixed);
3386
3387          elsif Is_Numeric_Type (T1)
3388            and then Is_Numeric_Type (T2)
3389            and then (Covers (T1, T2) or else Covers (T2, T1))
3390          then
3391             Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
3392
3393          elsif Is_Fixed_Point_Type (T1)
3394            and then (Base_Type (T2) = Base_Type (Standard_Integer)
3395                        or else T2 = Universal_Integer)
3396          then
3397             Add_One_Interp (N, Op_Id, T1);
3398
3399          elsif T2 = Universal_Real
3400            and then Base_Type (T1) = Base_Type (Standard_Integer)
3401            and then Op_Name = Name_Op_Multiply
3402          then
3403             Add_One_Interp (N, Op_Id, Any_Fixed);
3404
3405          elsif T1 = Universal_Real
3406            and then Base_Type (T2) = Base_Type (Standard_Integer)
3407          then
3408             Add_One_Interp (N, Op_Id, Any_Fixed);
3409
3410          elsif Is_Fixed_Point_Type (T2)
3411            and then (Base_Type (T1) = Base_Type (Standard_Integer)
3412                        or else T1 = Universal_Integer)
3413            and then Op_Name = Name_Op_Multiply
3414          then
3415             Add_One_Interp (N, Op_Id, T2);
3416
3417          elsif T1 = Universal_Real and then T2 = Universal_Integer then
3418             Add_One_Interp (N, Op_Id, T1);
3419
3420          elsif T2 = Universal_Real
3421            and then T1 = Universal_Integer
3422            and then Op_Name = Name_Op_Multiply
3423          then
3424             Add_One_Interp (N, Op_Id, T2);
3425          end if;
3426
3427       elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
3428
3429          --  Note: The fixed-point operands case with Treat_Fixed_As_Integer
3430          --  set does not require any special processing, since the Etype is
3431          --  already set (case of operation constructed by Exp_Fixed).
3432
3433          if Is_Integer_Type (T1)
3434            and then (Covers (T1, T2) or else Covers (T2, T1))
3435          then
3436             Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
3437          end if;
3438
3439       elsif Op_Name = Name_Op_Expon then
3440          if Is_Numeric_Type (T1)
3441            and then not Is_Fixed_Point_Type (T1)
3442            and then (Base_Type (T2) = Base_Type (Standard_Integer)
3443                       or else T2 = Universal_Integer)
3444          then
3445             Add_One_Interp (N, Op_Id, Base_Type (T1));
3446          end if;
3447
3448       else pragma Assert (Nkind (N) in N_Op_Shift);
3449
3450          --  If not one of the predefined operators, the node may be one
3451          --  of the intrinsic functions. Its kind is always specific, and
3452          --  we can use it directly, rather than the name of the operation.
3453
3454          if Is_Integer_Type (T1)
3455            and then (Base_Type (T2) = Base_Type (Standard_Integer)
3456                       or else T2 = Universal_Integer)
3457          then
3458             Add_One_Interp (N, Op_Id, Base_Type (T1));
3459          end if;
3460       end if;
3461    end Check_Arithmetic_Pair;
3462
3463    -------------------------------
3464    -- Check_Misspelled_Selector --
3465    -------------------------------
3466
3467    procedure Check_Misspelled_Selector
3468      (Prefix : Entity_Id;
3469       Sel    : Node_Id)
3470    is
3471       Max_Suggestions   : constant := 2;
3472       Nr_Of_Suggestions : Natural := 0;
3473
3474       Suggestion_1 : Entity_Id := Empty;
3475       Suggestion_2 : Entity_Id := Empty;
3476
3477       Comp : Entity_Id;
3478
3479    begin
3480       --  All the components of the prefix of selector Sel are matched
3481       --  against  Sel and a count is maintained of possible misspellings.
3482       --  When at the end of the analysis there are one or two (not more!)
3483       --  possible misspellings, these misspellings will be suggested as
3484       --  possible correction.
3485
3486       if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
3487
3488          --  Concurrent types should be handled as well ???
3489
3490          return;
3491       end if;
3492
3493       Get_Name_String (Chars (Sel));
3494
3495       declare
3496          S  : constant String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
3497
3498       begin
3499          Comp  := First_Entity (Prefix);
3500          while Nr_Of_Suggestions <= Max_Suggestions
3501             and then Present (Comp)
3502          loop
3503             if Is_Visible_Component (Comp) then
3504                Get_Name_String (Chars (Comp));
3505
3506                if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
3507                   Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
3508
3509                   case Nr_Of_Suggestions is
3510                      when 1      => Suggestion_1 := Comp;
3511                      when 2      => Suggestion_2 := Comp;
3512                      when others => exit;
3513                   end case;
3514                end if;
3515             end if;
3516
3517             Comp := Next_Entity (Comp);
3518          end loop;
3519
3520          --  Report at most two suggestions
3521
3522          if Nr_Of_Suggestions = 1 then
3523             Error_Msg_NE ("\possible misspelling of&", Sel, Suggestion_1);
3524
3525          elsif Nr_Of_Suggestions = 2 then
3526             Error_Msg_Node_2 := Suggestion_2;
3527             Error_Msg_NE ("\possible misspelling of& or&",
3528               Sel, Suggestion_1);
3529          end if;
3530       end;
3531    end Check_Misspelled_Selector;
3532
3533    ----------------------
3534    -- Defined_In_Scope --
3535    ----------------------
3536
3537    function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
3538    is
3539       S1 : constant Entity_Id := Scope (Base_Type (T));
3540    begin
3541       return S1 = S
3542         or else (S1 = System_Aux_Id and then S = Scope (S1));
3543    end Defined_In_Scope;
3544
3545    -------------------
3546    -- Diagnose_Call --
3547    -------------------
3548
3549    procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
3550       Actual           : Node_Id;
3551       X                : Interp_Index;
3552       It               : Interp;
3553       Success          : Boolean;
3554       Err_Mode         : Boolean;
3555       New_Nam          : Node_Id;
3556       Void_Interp_Seen : Boolean := False;
3557
3558    begin
3559       if Ada_Version >= Ada_05 then
3560          Actual := First_Actual (N);
3561          while Present (Actual) loop
3562
3563             --  Ada 2005 (AI-50217): Post an error in case of premature
3564             --  usage of an entity from the limited view.
3565
3566             if not Analyzed (Etype (Actual))
3567              and then From_With_Type (Etype (Actual))
3568             then
3569                Error_Msg_Qual_Level := 1;
3570                Error_Msg_NE
3571                 ("missing with_clause for scope of imported type&",
3572                   Actual, Etype (Actual));
3573                Error_Msg_Qual_Level := 0;
3574             end if;
3575
3576             Next_Actual (Actual);
3577          end loop;
3578       end if;
3579
3580       --   Analyze each candidate call again, with full error reporting
3581       --   for each.
3582
3583       Error_Msg_N
3584         ("no candidate interpretations match the actuals:!", Nam);
3585       Err_Mode := All_Errors_Mode;
3586       All_Errors_Mode := True;
3587
3588       --  If this is a call to an operation of a concurrent type,
3589       --  the failed interpretations have been removed from the
3590       --  name. Recover them to provide full diagnostics.
3591
3592       if Nkind (Parent (Nam)) = N_Selected_Component then
3593          Set_Entity (Nam, Empty);
3594          New_Nam := New_Copy_Tree (Parent (Nam));
3595          Set_Is_Overloaded (New_Nam, False);
3596          Set_Is_Overloaded (Selector_Name (New_Nam), False);
3597          Set_Parent (New_Nam, Parent (Parent (Nam)));
3598          Analyze_Selected_Component (New_Nam);
3599          Get_First_Interp (Selector_Name (New_Nam), X, It);
3600       else
3601          Get_First_Interp (Nam, X, It);
3602       end if;
3603
3604       while Present (It.Nam) loop
3605          if Etype (It.Nam) = Standard_Void_Type then
3606             Void_Interp_Seen := True;
3607          end if;
3608
3609          Analyze_One_Call (N, It.Nam, True, Success);
3610          Get_Next_Interp (X, It);
3611       end loop;
3612
3613       if Nkind (N) = N_Function_Call then
3614          Get_First_Interp (Nam, X, It);
3615          while Present (It.Nam) loop
3616             if Ekind (It.Nam) = E_Function
3617               or else Ekind (It.Nam) = E_Operator
3618             then
3619                return;
3620             else
3621                Get_Next_Interp (X, It);
3622             end if;
3623          end loop;
3624
3625          --  If all interpretations are procedures, this deserves a
3626          --  more precise message. Ditto if this appears as the prefix
3627          --  of a selected component, which may be a lexical error.
3628
3629          Error_Msg_N
3630            ("\context requires function call, found procedure name", Nam);
3631
3632          if Nkind (Parent (N)) = N_Selected_Component
3633            and then N = Prefix (Parent (N))
3634          then
3635             Error_Msg_N (
3636               "\period should probably be semicolon", Parent (N));
3637          end if;
3638
3639       elsif Nkind (N) = N_Procedure_Call_Statement
3640         and then not Void_Interp_Seen
3641       then
3642          Error_Msg_N (
3643          "\function name found in procedure call", Nam);
3644       end if;
3645
3646       All_Errors_Mode := Err_Mode;
3647    end Diagnose_Call;
3648
3649    ---------------------------
3650    -- Find_Arithmetic_Types --
3651    ---------------------------
3652
3653    procedure Find_Arithmetic_Types
3654      (L, R  : Node_Id;
3655       Op_Id : Entity_Id;
3656       N     : Node_Id)
3657    is
3658       Index1 : Interp_Index;
3659       Index2 : Interp_Index;
3660       It1    : Interp;
3661       It2    : Interp;
3662
3663       procedure Check_Right_Argument (T : Entity_Id);
3664       --  Check right operand of operator
3665
3666       --------------------------
3667       -- Check_Right_Argument --
3668       --------------------------
3669
3670       procedure Check_Right_Argument (T : Entity_Id) is
3671       begin
3672          if not Is_Overloaded (R) then
3673             Check_Arithmetic_Pair (T, Etype (R), Op_Id,  N);
3674          else
3675             Get_First_Interp (R, Index2, It2);
3676             while Present (It2.Typ) loop
3677                Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
3678                Get_Next_Interp (Index2, It2);
3679             end loop;
3680          end if;
3681       end Check_Right_Argument;
3682
3683    --  Start processing for Find_Arithmetic_Types
3684
3685    begin
3686       if not Is_Overloaded (L) then
3687          Check_Right_Argument (Etype (L));
3688
3689       else
3690          Get_First_Interp (L, Index1, It1);
3691
3692          while Present (It1.Typ) loop
3693             Check_Right_Argument (It1.Typ);
3694             Get_Next_Interp (Index1, It1);
3695          end loop;
3696       end if;
3697
3698    end Find_Arithmetic_Types;
3699
3700    ------------------------
3701    -- Find_Boolean_Types --
3702    ------------------------
3703
3704    procedure Find_Boolean_Types
3705      (L, R  : Node_Id;
3706       Op_Id : Entity_Id;
3707       N     : Node_Id)
3708    is
3709       Index : Interp_Index;
3710       It    : Interp;
3711
3712       procedure Check_Numeric_Argument (T : Entity_Id);
3713       --  Special case for logical operations one of whose operands is an
3714       --  integer literal. If both are literal the result is any modular type.
3715
3716       ----------------------------
3717       -- Check_Numeric_Argument --
3718       ----------------------------
3719
3720       procedure Check_Numeric_Argument (T : Entity_Id) is
3721       begin
3722          if T = Universal_Integer then
3723             Add_One_Interp (N, Op_Id, Any_Modular);
3724
3725          elsif Is_Modular_Integer_Type (T) then
3726             Add_One_Interp (N, Op_Id, T);
3727          end if;
3728       end Check_Numeric_Argument;
3729
3730    --  Start of processing for Find_Boolean_Types
3731
3732    begin
3733       if not Is_Overloaded (L) then
3734          if Etype (L) = Universal_Integer
3735            or else Etype (L) = Any_Modular
3736          then
3737             if not Is_Overloaded (R) then
3738                Check_Numeric_Argument (Etype (R));
3739
3740             else
3741                Get_First_Interp (R, Index, It);
3742                while Present (It.Typ) loop
3743                   Check_Numeric_Argument (It.Typ);
3744                   Get_Next_Interp (Index, It);
3745                end loop;
3746             end if;
3747
3748          elsif Valid_Boolean_Arg (Etype (L))
3749            and then Has_Compatible_Type (R, Etype (L))
3750          then
3751             Add_One_Interp (N, Op_Id, Etype (L));
3752          end if;
3753
3754       else
3755          Get_First_Interp (L, Index, It);
3756          while Present (It.Typ) loop
3757             if Valid_Boolean_Arg (It.Typ)
3758               and then Has_Compatible_Type (R, It.Typ)
3759             then
3760                Add_One_Interp (N, Op_Id, It.Typ);
3761             end if;
3762
3763             Get_Next_Interp (Index, It);
3764          end loop;
3765       end if;
3766    end Find_Boolean_Types;
3767
3768    ---------------------------
3769    -- Find_Comparison_Types --
3770    ---------------------------
3771
3772    procedure Find_Comparison_Types
3773      (L, R  : Node_Id;
3774       Op_Id : Entity_Id;
3775       N     : Node_Id)
3776    is
3777       Index : Interp_Index;
3778       It    : Interp;
3779       Found : Boolean := False;
3780       I_F   : Interp_Index;
3781       T_F   : Entity_Id;
3782       Scop  : Entity_Id := Empty;
3783
3784       procedure Try_One_Interp (T1 : Entity_Id);
3785       --  Routine to try one proposed interpretation. Note that the context
3786       --  of the operator plays no role in resolving the arguments, so that
3787       --  if there is more than one interpretation of the operands that is
3788       --  compatible with comparison, the operation is ambiguous.
3789
3790       --------------------
3791       -- Try_One_Interp --
3792       --------------------
3793
3794       procedure Try_One_Interp (T1 : Entity_Id) is
3795       begin
3796
3797          --  If the operator is an expanded name, then the type of the operand
3798          --  must be defined in the corresponding scope. If the type is
3799          --  universal, the context will impose the correct type.
3800
3801          if Present (Scop)
3802             and then not Defined_In_Scope (T1, Scop)
3803             and then T1 /= Universal_Integer
3804             and then T1 /= Universal_Real
3805             and then T1 /= Any_String
3806             and then T1 /= Any_Composite
3807          then
3808             return;
3809          end if;
3810
3811          if Valid_Comparison_Arg (T1)
3812            and then Has_Compatible_Type (R, T1)
3813          then
3814             if Found
3815               and then Base_Type (T1) /= Base_Type (T_F)
3816             then
3817                It := Disambiguate (L, I_F, Index, Any_Type);
3818
3819                if It = No_Interp then
3820                   Ambiguous_Operands (N);
3821                   Set_Etype (L, Any_Type);
3822                   return;
3823
3824                else
3825                   T_F := It.Typ;
3826                end if;
3827
3828             else
3829                Found := True;
3830                T_F   := T1;
3831                I_F   := Index;
3832             end if;
3833
3834             Set_Etype (L, T_F);
3835             Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
3836
3837          end if;
3838       end Try_One_Interp;
3839
3840    --  Start processing for Find_Comparison_Types
3841
3842    begin
3843       --  If left operand is aggregate, the right operand has to
3844       --  provide a usable type for it.
3845
3846       if Nkind (L) = N_Aggregate
3847         and then Nkind (R) /= N_Aggregate
3848       then
3849          Find_Comparison_Types (R, L, Op_Id, N);
3850          return;
3851       end if;
3852
3853       if Nkind (N) = N_Function_Call
3854          and then Nkind (Name (N)) = N_Expanded_Name
3855       then
3856          Scop := Entity (Prefix (Name (N)));
3857
3858          --  The prefix may be a package renaming, and the subsequent test
3859          --  requires the original package.
3860
3861          if Ekind (Scop) = E_Package
3862            and then Present (Renamed_Entity (Scop))
3863          then
3864             Scop := Renamed_Entity (Scop);
3865             Set_Entity (Prefix (Name (N)), Scop);
3866          end if;
3867       end if;
3868
3869       if not Is_Overloaded (L) then
3870          Try_One_Interp (Etype (L));
3871
3872       else
3873          Get_First_Interp (L, Index, It);
3874          while Present (It.Typ) loop
3875             Try_One_Interp (It.Typ);
3876             Get_Next_Interp (Index, It);
3877          end loop;
3878       end if;
3879    end Find_Comparison_Types;
3880
3881    ----------------------------------------
3882    -- Find_Non_Universal_Interpretations --
3883    ----------------------------------------
3884
3885    procedure Find_Non_Universal_Interpretations
3886      (N     : Node_Id;
3887       R     : Node_Id;
3888       Op_Id : Entity_Id;
3889       T1    : Entity_Id)
3890    is
3891       Index : Interp_Index;
3892       It    : Interp;
3893
3894    begin
3895       if T1 = Universal_Integer
3896         or else T1 = Universal_Real
3897       then
3898          if not Is_Overloaded (R) then
3899             Add_One_Interp
3900               (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
3901          else
3902             Get_First_Interp (R, Index, It);
3903             while Present (It.Typ) loop
3904                if Covers (It.Typ, T1) then
3905                   Add_One_Interp
3906                     (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
3907                end if;
3908
3909                Get_Next_Interp (Index, It);
3910             end loop;
3911          end if;
3912       else
3913          Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
3914       end if;
3915    end Find_Non_Universal_Interpretations;
3916
3917    ------------------------------
3918    -- Find_Concatenation_Types --
3919    ------------------------------
3920
3921    procedure Find_Concatenation_Types
3922      (L, R  : Node_Id;
3923       Op_Id : Entity_Id;
3924       N     : Node_Id)
3925    is
3926       Op_Type : constant Entity_Id := Etype (Op_Id);
3927
3928    begin
3929       if Is_Array_Type (Op_Type)
3930         and then not Is_Limited_Type (Op_Type)
3931
3932         and then (Has_Compatible_Type (L, Op_Type)
3933                     or else
3934                   Has_Compatible_Type (L, Component_Type (Op_Type)))
3935
3936         and then (Has_Compatible_Type (R, Op_Type)
3937                     or else
3938                   Has_Compatible_Type (R, Component_Type (Op_Type)))
3939       then
3940          Add_One_Interp (N, Op_Id, Op_Type);
3941       end if;
3942    end Find_Concatenation_Types;
3943
3944    -------------------------
3945    -- Find_Equality_Types --
3946    -------------------------
3947
3948    procedure Find_Equality_Types
3949      (L, R  : Node_Id;
3950       Op_Id : Entity_Id;
3951       N     : Node_Id)
3952    is
3953       Index : Interp_Index;
3954       It    : Interp;
3955       Found : Boolean := False;
3956       I_F   : Interp_Index;
3957       T_F   : Entity_Id;
3958       Scop  : Entity_Id := Empty;
3959
3960       procedure Try_One_Interp (T1 : Entity_Id);
3961       --  The context of the operator plays no role in resolving the
3962       --  arguments,  so that if there is more than one interpretation
3963       --  of the operands that is compatible with equality, the construct
3964       --  is ambiguous and an error can be emitted now, after trying to
3965       --  disambiguate, i.e. applying preference rules.
3966
3967       --------------------
3968       -- Try_One_Interp --
3969       --------------------
3970
3971       procedure Try_One_Interp (T1 : Entity_Id) is
3972       begin
3973          --  If the operator is an expanded name, then the type of the operand
3974          --  must be defined in the corresponding scope. If the type is
3975          --  universal, the context will impose the correct type. An anonymous
3976          --  type for a 'Access reference is also universal in this sense, as
3977          --  the actual type is obtained from context.
3978
3979          if Present (Scop)
3980             and then not Defined_In_Scope (T1, Scop)
3981             and then T1 /= Universal_Integer
3982             and then T1 /= Universal_Real
3983             and then T1 /= Any_Access
3984             and then T1 /= Any_String
3985             and then T1 /= Any_Composite
3986             and then (Ekind (T1) /= E_Access_Subprogram_Type
3987                         or else Comes_From_Source (T1))
3988          then
3989             return;
3990          end if;
3991
3992          --  Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
3993          --  Do not allow anonymous access types in equality operators.
3994
3995          if Ada_Version < Ada_05
3996            and then Ekind (T1) = E_Anonymous_Access_Type
3997          then
3998             return;
3999          end if;
4000
4001          if T1 /= Standard_Void_Type
4002            and then not Is_Limited_Type (T1)
4003            and then not Is_Limited_Composite (T1)
4004            and then Has_Compatible_Type (R, T1)
4005          then
4006             if Found
4007               and then Base_Type (T1) /= Base_Type (T_F)
4008             then
4009                It := Disambiguate (L, I_F, Index, Any_Type);
4010
4011                if It = No_Interp then
4012                   Ambiguous_Operands (N);
4013                   Set_Etype (L, Any_Type);
4014                   return;
4015
4016                else
4017                   T_F := It.Typ;
4018                end if;
4019
4020             else
4021                Found := True;
4022                T_F   := T1;
4023                I_F   := Index;
4024             end if;
4025
4026             if not Analyzed (L) then
4027                Set_Etype (L, T_F);
4028             end if;
4029
4030             Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
4031
4032             --  Case of operator was not visible, Etype still set to Any_Type
4033
4034             if Etype (N) = Any_Type then
4035                Found := False;
4036             end if;
4037          end if;
4038       end Try_One_Interp;
4039
4040    --  Start of processing for Find_Equality_Types
4041
4042    begin
4043       --  If left operand is aggregate, the right operand has to
4044       --  provide a usable type for it.
4045
4046       if Nkind (L) = N_Aggregate
4047         and then Nkind (R) /= N_Aggregate
4048       then
4049          Find_Equality_Types (R, L, Op_Id, N);
4050          return;
4051       end if;
4052
4053       if Nkind (N) = N_Function_Call
4054          and then Nkind (Name (N)) = N_Expanded_Name
4055       then
4056          Scop := Entity (Prefix (Name (N)));
4057
4058          --  The prefix may be a package renaming, and the subsequent test
4059          --  requires the original package.
4060
4061          if Ekind (Scop) = E_Package
4062            and then Present (Renamed_Entity (Scop))
4063          then
4064             Scop := Renamed_Entity (Scop);
4065             Set_Entity (Prefix (Name (N)), Scop);
4066          end if;
4067       end if;
4068
4069       if not Is_Overloaded (L) then
4070          Try_One_Interp (Etype (L));
4071
4072       else
4073          Get_First_Interp (L, Index, It);
4074          while Present (It.Typ) loop
4075             Try_One_Interp (It.Typ);
4076             Get_Next_Interp (Index, It);
4077          end loop;
4078       end if;
4079    end Find_Equality_Types;
4080
4081    -------------------------
4082    -- Find_Negation_Types --
4083    -------------------------
4084
4085    procedure Find_Negation_Types
4086      (R     : Node_Id;
4087       Op_Id : Entity_Id;
4088       N     : Node_Id)
4089    is
4090       Index : Interp_Index;
4091       It    : Interp;
4092
4093    begin
4094       if not Is_Overloaded (R) then
4095          if Etype (R) = Universal_Integer then
4096             Add_One_Interp (N, Op_Id, Any_Modular);
4097          elsif Valid_Boolean_Arg (Etype (R)) then
4098             Add_One_Interp (N, Op_Id, Etype (R));
4099          end if;
4100
4101       else
4102          Get_First_Interp (R, Index, It);
4103          while Present (It.Typ) loop
4104             if Valid_Boolean_Arg (It.Typ) then
4105                Add_One_Interp (N, Op_Id, It.Typ);
4106             end if;
4107
4108             Get_Next_Interp (Index, It);
4109          end loop;
4110       end if;
4111    end Find_Negation_Types;
4112
4113    ----------------------
4114    -- Find_Unary_Types --
4115    ----------------------
4116
4117    procedure Find_Unary_Types
4118      (R     : Node_Id;
4119       Op_Id : Entity_Id;
4120       N     : Node_Id)
4121    is
4122       Index : Interp_Index;
4123       It    : Interp;
4124
4125    begin
4126       if not Is_Overloaded (R) then
4127          if Is_Numeric_Type (Etype (R)) then
4128             Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
4129          end if;
4130
4131       else
4132          Get_First_Interp (R, Index, It);
4133          while Present (It.Typ) loop
4134             if Is_Numeric_Type (It.Typ) then
4135                Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
4136             end if;
4137
4138             Get_Next_Interp (Index, It);
4139          end loop;
4140       end if;
4141    end Find_Unary_Types;
4142
4143    ------------------
4144    -- Junk_Operand --
4145    ------------------
4146
4147    function Junk_Operand (N : Node_Id) return Boolean is
4148       Enode : Node_Id;
4149
4150    begin
4151       if Error_Posted (N) then
4152          return False;
4153       end if;
4154
4155       --  Get entity to be tested
4156
4157       if Is_Entity_Name (N)
4158         and then Present (Entity (N))
4159       then
4160          Enode := N;
4161
4162       --  An odd case, a procedure name gets converted to a very peculiar
4163       --  function call, and here is where we detect this happening.
4164
4165       elsif Nkind (N) = N_Function_Call
4166         and then Is_Entity_Name (Name (N))
4167         and then Present (Entity (Name (N)))
4168       then
4169          Enode := Name (N);
4170
4171       --  Another odd case, there are at least some cases of selected
4172       --  components where the selected component is not marked as having
4173       --  an entity, even though the selector does have an entity
4174
4175       elsif Nkind (N) = N_Selected_Component
4176         and then Present (Entity (Selector_Name (N)))
4177       then
4178          Enode := Selector_Name (N);
4179
4180       else
4181          return False;
4182       end if;
4183
4184       --  Now test the entity we got to see if it a bad case
4185
4186       case Ekind (Entity (Enode)) is
4187
4188          when E_Package =>
4189             Error_Msg_N
4190               ("package name cannot be used as operand", Enode);
4191
4192          when Generic_Unit_Kind =>
4193             Error_Msg_N
4194               ("generic unit name cannot be used as operand", Enode);
4195
4196          when Type_Kind =>
4197             Error_Msg_N
4198               ("subtype name cannot be used as operand", Enode);
4199
4200          when Entry_Kind =>
4201             Error_Msg_N
4202               ("entry name cannot be used as operand", Enode);
4203
4204          when E_Procedure =>
4205             Error_Msg_N
4206               ("procedure name cannot be used as operand", Enode);
4207
4208          when E_Exception =>
4209             Error_Msg_N
4210               ("exception name cannot be used as operand", Enode);
4211
4212          when E_Block | E_Label | E_Loop =>
4213             Error_Msg_N
4214               ("label name cannot be used as operand", Enode);
4215
4216          when others =>
4217             return False;
4218
4219       end case;
4220
4221       return True;
4222    end Junk_Operand;
4223
4224    --------------------
4225    -- Operator_Check --
4226    --------------------
4227
4228    procedure Operator_Check (N : Node_Id) is
4229    begin
4230       Remove_Abstract_Operations (N);
4231
4232       --  Test for case of no interpretation found for operator
4233
4234       if Etype (N) = Any_Type then
4235          declare
4236             L : Node_Id;
4237             R : Node_Id;
4238
4239          begin
4240             R := Right_Opnd (N);
4241
4242             if Nkind (N) in N_Binary_Op then
4243                L := Left_Opnd (N);
4244             else
4245                L := Empty;
4246             end if;
4247
4248             --  If either operand has no type, then don't complain further,
4249             --  since this simply means that we have a propragated error.
4250
4251             if R = Error
4252               or else Etype (R) = Any_Type
4253               or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
4254             then
4255                return;
4256
4257             --  We explicitly check for the case of concatenation of component
4258             --  with component to avoid reporting spurious matching array types
4259             --  that might happen to be lurking in distant packages (such as
4260             --  run-time packages). This also prevents inconsistencies in the
4261             --  messages for certain ACVC B tests, which can vary depending on
4262             --  types declared in run-time interfaces. Another improvement when
4263             --  aggregates are present is to look for a well-typed operand.
4264
4265             elsif Present (Candidate_Type)
4266               and then (Nkind (N) /= N_Op_Concat
4267                          or else Is_Array_Type (Etype (L))
4268                          or else Is_Array_Type (Etype (R)))
4269             then
4270
4271                if Nkind (N) = N_Op_Concat then
4272                   if Etype (L) /= Any_Composite
4273                     and then Is_Array_Type (Etype (L))
4274                   then
4275                      Candidate_Type := Etype (L);
4276
4277                   elsif Etype (R) /= Any_Composite
4278                     and then Is_Array_Type (Etype (R))
4279                   then
4280                      Candidate_Type := Etype (R);
4281                   end if;
4282                end if;
4283
4284                Error_Msg_NE
4285                  ("operator for} is not directly visible!",
4286                   N, First_Subtype (Candidate_Type));
4287                Error_Msg_N ("use clause would make operation legal!",  N);
4288                return;
4289
4290             --  If either operand is a junk operand (e.g. package name), then
4291             --  post appropriate error messages, but do not complain further.
4292
4293             --  Note that the use of OR in this test instead of OR ELSE
4294             --  is quite deliberate, we may as well check both operands
4295             --  in the binary operator case.
4296
4297             elsif Junk_Operand (R)
4298               or (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
4299             then
4300                return;
4301
4302             --  If we have a logical operator, one of whose operands is
4303             --  Boolean, then we know that the other operand cannot resolve
4304             --  to Boolean (since we got no interpretations), but in that
4305             --  case we pretty much know that the other operand should be
4306             --  Boolean, so resolve it that way (generating an error)
4307
4308             elsif Nkind (N) = N_Op_And
4309                     or else
4310                   Nkind (N) = N_Op_Or
4311                     or else
4312                   Nkind (N) = N_Op_Xor
4313             then
4314                if Etype (L) = Standard_Boolean then
4315                   Resolve (R, Standard_Boolean);
4316                   return;
4317                elsif Etype (R) = Standard_Boolean then
4318                   Resolve (L, Standard_Boolean);
4319                   return;
4320                end if;
4321
4322             --  For an arithmetic operator or comparison operator, if one
4323             --  of the operands is numeric, then we know the other operand
4324             --  is not the same numeric type. If it is a non-numeric type,
4325             --  then probably it is intended to match the other operand.
4326
4327             elsif Nkind (N) = N_Op_Add      or else
4328                   Nkind (N) = N_Op_Divide   or else
4329                   Nkind (N) = N_Op_Ge       or else
4330                   Nkind (N) = N_Op_Gt       or else
4331                   Nkind (N) = N_Op_Le       or else
4332                   Nkind (N) = N_Op_Lt       or else
4333                   Nkind (N) = N_Op_Mod      or else
4334                   Nkind (N) = N_Op_Multiply or else
4335                   Nkind (N) = N_Op_Rem      or else
4336                   Nkind (N) = N_Op_Subtract
4337             then
4338                if Is_Numeric_Type (Etype (L))
4339                  and then not Is_Numeric_Type (Etype (R))
4340                then
4341                   Resolve (R, Etype (L));
4342                   return;
4343
4344                elsif Is_Numeric_Type (Etype (R))
4345                  and then not Is_Numeric_Type (Etype (L))
4346                then
4347                   Resolve (L, Etype (R));
4348                   return;
4349                end if;
4350
4351             --  Comparisons on A'Access are common enough to deserve a
4352             --  special message.
4353
4354             elsif (Nkind (N) = N_Op_Eq  or else
4355                    Nkind (N) = N_Op_Ne)
4356                and then Ekind (Etype (L)) = E_Access_Attribute_Type
4357                and then Ekind (Etype (R)) = E_Access_Attribute_Type
4358             then
4359                Error_Msg_N
4360                  ("two access attributes cannot be compared directly", N);
4361                Error_Msg_N
4362                  ("\they must be converted to an explicit type for comparison",
4363                    N);
4364                return;
4365
4366             --  Another one for C programmers
4367
4368             elsif Nkind (N) = N_Op_Concat
4369               and then Valid_Boolean_Arg (Etype (L))
4370               and then Valid_Boolean_Arg (Etype (R))
4371             then
4372                Error_Msg_N ("invalid operands for concatenation", N);
4373                Error_Msg_N ("\maybe AND was meant", N);
4374                return;
4375
4376             --  A special case for comparison of access parameter with null
4377
4378             elsif Nkind (N) = N_Op_Eq
4379               and then Is_Entity_Name (L)
4380               and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
4381               and then Nkind (Parameter_Type (Parent (Entity (L)))) =
4382                                                   N_Access_Definition
4383               and then Nkind (R) = N_Null
4384             then
4385                Error_Msg_N ("access parameter is not allowed to be null", L);
4386                Error_Msg_N ("\(call would raise Constraint_Error)", L);
4387                return;
4388             end if;
4389
4390             --  If we fall through then just give general message. Note
4391             --  that in the following messages, if the operand is overloaded
4392             --  we choose an arbitrary type to complain about, but that is
4393             --  probably more useful than not giving a type at all.
4394
4395             if Nkind (N) in N_Unary_Op then
4396                Error_Msg_Node_2 := Etype (R);
4397                Error_Msg_N ("operator& not defined for}", N);
4398                return;
4399
4400             else
4401                if Nkind (N) in N_Binary_Op then
4402                   if not Is_Overloaded (L)
4403                     and then not Is_Overloaded (R)
4404                     and then Base_Type (Etype (L)) = Base_Type (Etype (R))
4405                   then
4406                      Error_Msg_Node_2 := First_Subtype (Etype (R));
4407                      Error_Msg_N ("there is no applicable operator& for}", N);
4408
4409                   else
4410                      Error_Msg_N ("invalid operand types for operator&", N);
4411
4412                      if Nkind (N) /= N_Op_Concat then
4413                         Error_Msg_NE ("\left operand has}!",  N, Etype (L));
4414                         Error_Msg_NE ("\right operand has}!", N, Etype (R));
4415                      end if;
4416                   end if;
4417                end if;
4418             end if;
4419          end;
4420       end if;
4421    end Operator_Check;
4422
4423    -----------------------------------------
4424    -- Process_Implicit_Dereference_Prefix --
4425    -----------------------------------------
4426
4427    procedure Process_Implicit_Dereference_Prefix
4428      (E : Entity_Id;
4429       P : Entity_Id)
4430    is
4431       Ref : Node_Id;
4432
4433    begin
4434       if Present (E)
4435         and then (Operating_Mode = Check_Semantics or else not Expander_Active)
4436       then
4437          --  We create a dummy reference to E to ensure that the reference
4438          --  is not considered as part of an assignment (an implicit
4439          --  dereference can never assign to its prefix). The Comes_From_Source
4440          --  attribute needs to be propagated for accurate warnings.
4441
4442          Ref := New_Reference_To (E, Sloc (P));
4443          Set_Comes_From_Source (Ref, Comes_From_Source (P));
4444          Generate_Reference (E, Ref);
4445       end if;
4446    end Process_Implicit_Dereference_Prefix;
4447
4448    --------------------------------
4449    -- Remove_Abstract_Operations --
4450    --------------------------------
4451
4452    procedure Remove_Abstract_Operations (N : Node_Id) is
4453       I            : Interp_Index;
4454       It           : Interp;
4455       Abstract_Op  : Entity_Id := Empty;
4456
4457       --  AI-310: If overloaded, remove abstract non-dispatching
4458       --  operations. We activate this if either extensions are
4459       --  enabled, or if the abstract operation in question comes
4460       --  from a predefined file. This latter test allows us to
4461       --  use abstract to make operations invisible to users. In
4462       --  particular, if type Address is non-private and abstract
4463       --  subprograms are used to hide its operators, they will be
4464       --  truly hidden.
4465
4466       type Operand_Position is (First_Op, Second_Op);
4467       Univ_Type : constant Entity_Id := Universal_Interpretation (N);
4468
4469       procedure Remove_Address_Interpretations (Op : Operand_Position);
4470       --  Ambiguities may arise when the operands are literal and the
4471       --  address operations in s-auxdec are visible. In that case, remove
4472       --  the interpretation of a literal as Address, to retain the semantics
4473       --  of Address as a private type.
4474
4475       ------------------------------------
4476       -- Remove_Address_Interpretations --
4477       ------------------------------------
4478
4479       procedure Remove_Address_Interpretations (Op : Operand_Position) is
4480          Formal : Entity_Id;
4481
4482       begin
4483          if Is_Overloaded (N) then
4484             Get_First_Interp (N, I, It);
4485             while Present (It.Nam) loop
4486                Formal := First_Entity (It.Nam);
4487
4488                if Op = Second_Op then
4489                   Formal := Next_Entity (Formal);
4490                end if;
4491
4492                if Is_Descendent_Of_Address (Etype (Formal)) then
4493                   Remove_Interp (I);
4494                end if;
4495
4496                Get_Next_Interp (I, It);
4497             end loop;
4498          end if;
4499       end Remove_Address_Interpretations;
4500
4501    --  Start of processing for Remove_Abstract_Operations
4502
4503    begin
4504       if Is_Overloaded (N) then
4505          Get_First_Interp (N, I, It);
4506
4507          while Present (It.Nam) loop
4508             if not Is_Type (It.Nam)
4509               and then Is_Abstract (It.Nam)
4510               and then not Is_Dispatching_Operation (It.Nam)
4511               and then
4512                 (Ada_Version >= Ada_05
4513                    or else Is_Predefined_File_Name
4514                              (Unit_File_Name (Get_Source_Unit (It.Nam))))
4515
4516             then
4517                Abstract_Op := It.Nam;
4518                Remove_Interp (I);
4519                exit;
4520             end if;
4521
4522             Get_Next_Interp (I, It);
4523          end loop;
4524
4525          if No (Abstract_Op) then
4526             return;
4527
4528          elsif Nkind (N) in N_Op then
4529
4530             --  Remove interpretations that treat literals as addresses.
4531             --  This is never appropriate.
4532
4533             if Nkind (N) in N_Binary_Op then
4534                declare
4535                   U1 : constant Boolean :=
4536                      Present (Universal_Interpretation (Right_Opnd (N)));
4537                   U2 : constant Boolean :=
4538                      Present (Universal_Interpretation (Left_Opnd (N)));
4539
4540                begin
4541                   if U1 and then not U2 then
4542                      Remove_Address_Interpretations (Second_Op);
4543
4544                   elsif U2 and then not U1 then
4545                      Remove_Address_Interpretations (First_Op);
4546                   end if;
4547
4548                   if not (U1 and U2) then
4549
4550                      --  Remove corresponding predefined operator, which is
4551                      --  always added to the overload set.
4552
4553                      Get_First_Interp (N, I, It);
4554                      while Present (It.Nam) loop
4555                         if Scope (It.Nam) = Standard_Standard
4556                           and then Base_Type (It.Typ) =
4557                                    Base_Type (Etype (Abstract_Op))
4558                         then
4559                            Remove_Interp (I);
4560                         end if;
4561
4562                         Get_Next_Interp (I, It);
4563                      end loop;
4564
4565                   elsif Is_Overloaded (N)
4566                     and then Present (Univ_Type)
4567                   then
4568                      --  If both operands have a universal interpretation,
4569                      --  select the predefined operator and discard others.
4570
4571                      Get_First_Interp (N, I, It);
4572
4573                      while Present (It.Nam) loop
4574                         if Scope (It.Nam) = Standard_Standard then
4575                            Set_Etype (N, Univ_Type);
4576                            Set_Entity (N, It.Nam);
4577                            Set_Is_Overloaded (N, False);
4578                            exit;
4579                         end if;
4580
4581                         Get_Next_Interp (I, It);
4582                      end loop;
4583                   end if;
4584                end;
4585             end if;
4586
4587          elsif Nkind (N) = N_Function_Call
4588            and then
4589              (Nkind (Name (N)) = N_Operator_Symbol
4590                 or else
4591                   (Nkind (Name (N)) = N_Expanded_Name
4592                      and then
4593                        Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
4594          then
4595
4596             declare
4597                Arg1 : constant Node_Id := First (Parameter_Associations (N));
4598                U1   : constant Boolean :=
4599                         Present (Universal_Interpretation (Arg1));
4600                U2   : constant Boolean :=
4601                         Present (Next (Arg1)) and then
4602                         Present (Universal_Interpretation (Next (Arg1)));
4603
4604             begin
4605                if U1 and then not U2 then
4606                   Remove_Address_Interpretations (First_Op);
4607
4608                elsif U2 and then not U1 then
4609                   Remove_Address_Interpretations (Second_Op);
4610                end if;
4611
4612                if not (U1 and U2) then
4613                   Get_First_Interp (N, I, It);
4614                   while Present (It.Nam) loop
4615                      if Scope (It.Nam) = Standard_Standard
4616                        and then It.Typ = Base_Type (Etype (Abstract_Op))
4617                      then
4618                         Remove_Interp (I);
4619                      end if;
4620
4621                      Get_Next_Interp (I, It);
4622                   end loop;
4623                end if;
4624             end;
4625          end if;
4626
4627          --  If the removal has left no valid interpretations, emit
4628          --  error message now and label node as illegal.
4629
4630          if Present (Abstract_Op) then
4631             Get_First_Interp (N, I, It);
4632
4633             if No (It.Nam) then
4634
4635                --  Removal of abstract operation left no viable candidate
4636
4637                Set_Etype (N, Any_Type);
4638                Error_Msg_Sloc := Sloc (Abstract_Op);
4639                Error_Msg_NE
4640                  ("cannot call abstract operation& declared#", N, Abstract_Op);
4641             end if;
4642          end if;
4643       end if;
4644    end Remove_Abstract_Operations;
4645
4646    -----------------------
4647    -- Try_Indirect_Call --
4648    -----------------------
4649
4650    function Try_Indirect_Call
4651      (N   : Node_Id;
4652       Nam : Entity_Id;
4653       Typ : Entity_Id) return Boolean
4654    is
4655       Actual  : Node_Id;
4656       Formal  : Entity_Id;
4657       Call_OK : Boolean;
4658
4659    begin
4660       Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
4661       Actual := First_Actual (N);
4662       Formal := First_Formal (Designated_Type (Typ));
4663
4664       while Present (Actual)
4665         and then Present (Formal)
4666       loop
4667          if not Has_Compatible_Type (Actual, Etype (Formal)) then
4668             return False;
4669          end if;
4670
4671          Next (Actual);
4672          Next_Formal (Formal);
4673       end loop;
4674
4675       if No (Actual) and then No (Formal) then
4676          Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
4677
4678          --  Nam is a candidate interpretation for the name in the call,
4679          --  if it is not an indirect call.
4680
4681          if not Is_Type (Nam)
4682             and then Is_Entity_Name (Name (N))
4683          then
4684             Set_Entity (Name (N), Nam);
4685          end if;
4686
4687          return True;
4688       else
4689          return False;
4690       end if;
4691    end Try_Indirect_Call;
4692
4693    ----------------------
4694    -- Try_Indexed_Call --
4695    ----------------------
4696
4697    function Try_Indexed_Call
4698      (N   : Node_Id;
4699       Nam : Entity_Id;
4700       Typ : Entity_Id) return Boolean
4701    is
4702       Actuals : constant List_Id   := Parameter_Associations (N);
4703       Actual : Node_Id;
4704       Index  : Entity_Id;
4705
4706    begin
4707       Actual := First (Actuals);
4708       Index := First_Index (Typ);
4709       while Present (Actual)
4710         and then Present (Index)
4711       loop
4712          --  If the parameter list has a named association, the expression
4713          --  is definitely a call and not an indexed component.
4714
4715          if Nkind (Actual) = N_Parameter_Association then
4716             return False;
4717          end if;
4718
4719          if not Has_Compatible_Type (Actual, Etype (Index)) then
4720             return False;
4721          end if;
4722
4723          Next (Actual);
4724          Next_Index (Index);
4725       end loop;
4726
4727       if No (Actual) and then No (Index) then
4728          Add_One_Interp (N, Nam, Component_Type (Typ));
4729
4730          --  Nam is a candidate interpretation for the name in the call,
4731          --  if it is not an indirect call.
4732
4733          if not Is_Type (Nam)
4734             and then Is_Entity_Name (Name (N))
4735          then
4736             Set_Entity (Name (N), Nam);
4737          end if;
4738
4739          return True;
4740       else
4741          return False;
4742       end if;
4743    end Try_Indexed_Call;
4744
4745    --------------------------
4746    -- Try_Object_Operation --
4747    --------------------------
4748
4749    function Try_Object_Operation (N : Node_Id) return Boolean is
4750       K               : constant Node_Kind  := Nkind (Parent (N));
4751       Loc             : constant Source_Ptr := Sloc (N);
4752       Is_Subprg_Call  : constant Boolean    := K = N_Procedure_Call_Statement
4753                                                 or else K = N_Function_Call;
4754       Obj             : constant Node_Id    := Prefix (N);
4755       Subprog         : constant Node_Id    := Selector_Name (N);
4756
4757       Actual          : Node_Id;
4758       Call_Node       : Node_Id;
4759       Call_Node_Case  : Node_Id := Empty;
4760       First_Actual    : Node_Id;
4761       Node_To_Replace : Node_Id;
4762       Obj_Type        : Entity_Id := Etype (Obj);
4763
4764       procedure Complete_Object_Operation
4765         (Call_Node       : Node_Id;
4766          Node_To_Replace : Node_Id;
4767          Subprog         : Node_Id);
4768       --  Set Subprog as the name of Call_Node, replace Node_To_Replace with
4769       --  Call_Node and reanalyze Node_To_Replace.
4770
4771       procedure Transform_Object_Operation
4772         (Call_Node       : out Node_Id;
4773          First_Actual    : Node_Id;
4774          Node_To_Replace : out Node_Id;
4775          Subprog         : Node_Id);
4776       --  Transform Object.Operation (...) to Operation (Object, ...)
4777       --  Call_Node is the resulting subprogram call node, First_Actual is
4778       --  either the object Obj or an explicit dereference of Obj in certain
4779       --  cases, Node_To_Replace is either N or the parent of N, and Subprog
4780       --  is the subprogram we are trying to match.
4781
4782       function Try_Class_Wide_Operation
4783         (Call_Node       : Node_Id;
4784          Node_To_Replace : Node_Id) return Boolean;
4785       --  Traverse all the ancestor types looking for a class-wide subprogram
4786       --  that matches Subprog.
4787
4788       function Try_Primitive_Operation
4789         (Call_Node       : Node_Id;
4790          Node_To_Replace : Node_Id) return Boolean;
4791       --  Traverse the list of primitive subprograms looking for a subprogram
4792       --  than matches Subprog.
4793
4794       -------------------------------
4795       -- Complete_Object_Operation --
4796       -------------------------------
4797
4798       procedure Complete_Object_Operation
4799         (Call_Node       : Node_Id;
4800          Node_To_Replace : Node_Id;
4801          Subprog         : Node_Id)
4802       is
4803       begin
4804          Set_Name (Call_Node, New_Copy_Tree (Subprog));
4805          Set_Analyzed (Call_Node, False);
4806          Rewrite (Node_To_Replace, Call_Node);
4807          Analyze (Node_To_Replace);
4808       end Complete_Object_Operation;
4809
4810       --------------------------------
4811       -- Transform_Object_Operation --
4812       --------------------------------
4813
4814       procedure Transform_Object_Operation
4815         (Call_Node       : out Node_Id;
4816          First_Actual    : Node_Id;
4817          Node_To_Replace : out Node_Id;
4818          Subprog         : Node_Id)
4819       is
4820          Actuals     : List_Id;
4821          Parent_Node : constant Node_Id := Parent (N);
4822
4823       begin
4824          Actuals := New_List (New_Copy_Tree (First_Actual));
4825
4826          if (Nkind (Parent_Node) = N_Function_Call
4827                or else
4828              Nkind (Parent_Node) = N_Procedure_Call_Statement)
4829
4830             --  Avoid recursive calls
4831
4832            and then N /= First (Parameter_Associations (Parent_Node))
4833          then
4834             Node_To_Replace := Parent_Node;
4835
4836             --  Copy list of actuals in full before attempting to resolve call.
4837             --  This is necessary to ensure that the chaining of named actuals
4838             --  that happens during matching is done on a separate copy.
4839
4840             declare
4841                Actual : Node_Id;
4842             begin
4843                Actual := First (Parameter_Associations (Parent_Node));
4844                while Present (Actual) loop
4845                   Append (New_Copy_Tree (Actual), Actuals);
4846                   Next (Actual);
4847                end loop;
4848             end;
4849
4850             if Nkind (Parent_Node) = N_Procedure_Call_Statement then
4851                Call_Node :=
4852                  Make_Procedure_Call_Statement (Loc,
4853                    Name => New_Copy_Tree (Subprog),
4854                    Parameter_Associations => Actuals);
4855
4856             else
4857                pragma Assert (Nkind (Parent_Node) = N_Function_Call);
4858
4859                Call_Node :=
4860                  Make_Function_Call (Loc,
4861                    Name => New_Copy_Tree (Subprog),
4862                    Parameter_Associations => Actuals);
4863
4864             end if;
4865
4866          --  Parameterless call
4867
4868          else
4869             Node_To_Replace := N;
4870
4871             Call_Node :=
4872                Make_Function_Call (Loc,
4873                  Name => New_Copy_Tree (Subprog),
4874                  Parameter_Associations => Actuals);
4875
4876          end if;
4877       end Transform_Object_Operation;
4878
4879       ------------------------------
4880       -- Try_Class_Wide_Operation --
4881       ------------------------------
4882
4883       function Try_Class_Wide_Operation
4884         (Call_Node       : Node_Id;
4885          Node_To_Replace : Node_Id) return Boolean
4886       is
4887          Anc_Type : Entity_Id;
4888          Dummy    : Node_Id;
4889          Hom      : Entity_Id;
4890          Hom_Ref  : Node_Id;
4891          Success  : Boolean;
4892
4893       begin
4894          --  Loop through ancestor types, traverse their homonym chains and
4895          --  gather all interpretations of the subprogram.
4896
4897          Anc_Type := Obj_Type;
4898          loop
4899             Hom := Current_Entity (Subprog);
4900             while Present (Hom) loop
4901                if (Ekind (Hom) = E_Procedure
4902                      or else
4903                    Ekind (Hom) = E_Function)
4904                  and then Present (First_Formal (Hom))
4905                  and then Etype (First_Formal (Hom)) =
4906                             Class_Wide_Type (Anc_Type)
4907                then
4908                   Hom_Ref := New_Reference_To (Hom, Loc);
4909
4910                   --  When both the type of the object and the type of the
4911                   --  first formal of the primitive operation are tagged
4912                   --  access types, we use a node with the object as first
4913                   --  actual.
4914
4915                   if Is_Access_Type (Etype (Obj))
4916                     and then Ekind (Etype (First_Formal (Hom))) =
4917                                E_Anonymous_Access_Type
4918                   then
4919                      --  Allocate the node only once
4920
4921                      if not Present (Call_Node_Case) then
4922                         Analyze_Expression (Obj);
4923                         Set_Analyzed       (Obj);
4924
4925                         Transform_Object_Operation (
4926                           Call_Node       => Call_Node_Case,
4927                           First_Actual    => Obj,
4928                           Node_To_Replace => Dummy,
4929                           Subprog         => Subprog);
4930
4931                         Set_Etype (Call_Node_Case, Any_Type);
4932                         Set_Parent (Call_Node_Case, Parent (Node_To_Replace));
4933                      end if;
4934
4935                      Set_Name (Call_Node_Case, Hom_Ref);
4936
4937                      Analyze_One_Call (
4938                        N       => Call_Node_Case,
4939                        Nam     => Hom,
4940                        Report  => False,
4941                        Success => Success);
4942
4943                      if Success then
4944                         Complete_Object_Operation (
4945                           Call_Node       => Call_Node_Case,
4946                           Node_To_Replace => Node_To_Replace,
4947                           Subprog         => Hom_Ref);
4948
4949                         return True;
4950                      end if;
4951
4952                   --  ??? comment required
4953
4954                   else
4955                      Set_Name (Call_Node, Hom_Ref);
4956
4957                      Analyze_One_Call (
4958                        N       => Call_Node,
4959                        Nam     => Hom,
4960                        Report  => False,
4961                        Success => Success);
4962
4963                      if Success then
4964                         Complete_Object_Operation (
4965                           Call_Node       => Call_Node,
4966                           Node_To_Replace => Node_To_Replace,
4967                           Subprog         => Hom_Ref);
4968
4969                         return True;
4970                      end if;
4971                   end if;
4972                end if;
4973
4974                Hom := Homonym (Hom);
4975             end loop;
4976
4977             --  Climb to ancestor type if there is one
4978
4979             exit when Etype (Anc_Type) = Anc_Type;
4980             Anc_Type := Etype (Anc_Type);
4981          end loop;
4982
4983          return False;
4984       end Try_Class_Wide_Operation;
4985
4986       -----------------------------
4987       -- Try_Primitive_Operation --
4988       -----------------------------
4989
4990       function Try_Primitive_Operation
4991         (Call_Node       : Node_Id;
4992          Node_To_Replace : Node_Id) return Boolean
4993       is
4994          Dummy       : Node_Id;
4995          Elmt        : Elmt_Id;
4996          Prim_Op     : Entity_Id;
4997          Prim_Op_Ref : Node_Id;
4998          Success     : Boolean;
4999
5000       begin
5001          --  Look for the subprogram in the list of primitive operations
5002
5003          Elmt := First_Elmt (Primitive_Operations (Obj_Type));
5004          while Present (Elmt) loop
5005             Prim_Op := Node (Elmt);
5006
5007             if Chars (Prim_Op) = Chars (Subprog)
5008               and then Present (First_Formal (Prim_Op))
5009             then
5010                Prim_Op_Ref := New_Reference_To (Prim_Op, Loc);
5011
5012                --  When both the type of the object and the type of the first
5013                --  formal of the primitive operation are tagged access types,
5014                --  we use a node with the object as first actual.
5015
5016                if Is_Access_Type (Etype (Obj))
5017                  and then Ekind (Etype (First_Formal (Prim_Op))) =
5018                             E_Anonymous_Access_Type
5019                then
5020                   --  Allocate the node only once
5021
5022                   if not Present (Call_Node_Case) then
5023                      Analyze_Expression (Obj);
5024                      Set_Analyzed       (Obj);
5025
5026                      Transform_Object_Operation (
5027                        Call_Node       => Call_Node_Case,
5028                        First_Actual    => Obj,
5029                        Node_To_Replace => Dummy,
5030                        Subprog         => Subprog);
5031
5032                      Set_Etype (Call_Node_Case, Any_Type);
5033                      Set_Parent (Call_Node_Case, Parent (Node_To_Replace));
5034                   end if;
5035
5036                   Set_Name (Call_Node_Case, Prim_Op_Ref);
5037
5038                   Analyze_One_Call (
5039                     N       => Call_Node_Case,
5040                     Nam     => Prim_Op,
5041                     Report  => False,
5042                     Success => Success);
5043
5044                   if Success then
5045                      Complete_Object_Operation (
5046                        Call_Node       => Call_Node_Case,
5047                        Node_To_Replace => Node_To_Replace,
5048                        Subprog         => Prim_Op_Ref);
5049
5050                      return True;
5051                   end if;
5052
5053                --  Comment required ???
5054
5055                else
5056                   Set_Name (Call_Node, Prim_Op_Ref);
5057
5058                   Analyze_One_Call (
5059                     N       => Call_Node,
5060                     Nam     => Prim_Op,
5061                     Report  => False,
5062                     Success => Success);
5063
5064                   if Success then
5065                      Complete_Object_Operation (
5066                        Call_Node       => Call_Node,
5067                        Node_To_Replace => Node_To_Replace,
5068                        Subprog         => Prim_Op_Ref);
5069
5070                      return True;
5071                   end if;
5072                end if;
5073             end if;
5074
5075             Next_Elmt (Elmt);
5076          end loop;
5077
5078          return False;
5079       end Try_Primitive_Operation;
5080
5081    --  Start of processing for Try_Object_Operation
5082
5083    begin
5084       if Is_Access_Type (Obj_Type) then
5085          Obj_Type := Designated_Type (Obj_Type);
5086       end if;
5087
5088       if Ekind (Obj_Type) = E_Private_Subtype then
5089          Obj_Type := Base_Type (Obj_Type);
5090       end if;
5091
5092       if Is_Class_Wide_Type (Obj_Type) then
5093          Obj_Type := Etype (Class_Wide_Type (Obj_Type));
5094       end if;
5095
5096       --  Analyze the actuals in case of subprogram call
5097
5098       if Is_Subprg_Call and then N = Name (Parent (N)) then
5099          Actual := First (Parameter_Associations (Parent (N)));
5100          while Present (Actual) loop
5101             Analyze_Expression (Actual);
5102             Next (Actual);
5103          end loop;
5104       end if;
5105
5106       --  If the object is of an Access type, explicit dereference is
5107       --  required.
5108
5109       if Is_Access_Type (Etype (Obj)) then
5110          First_Actual :=
5111            Make_Explicit_Dereference (Sloc (Obj), Obj);
5112          Set_Etype (First_Actual, Obj_Type);
5113       else
5114          First_Actual := Obj;
5115       end if;
5116
5117       Analyze_Expression (First_Actual);
5118       Set_Analyzed       (First_Actual);
5119
5120       --  Build a subprogram call node
5121
5122       Transform_Object_Operation (
5123         Call_Node       => Call_Node,
5124         First_Actual    => First_Actual,
5125         Node_To_Replace => Node_To_Replace,
5126         Subprog         => Subprog);
5127
5128       Set_Etype (Call_Node, Any_Type);
5129       Set_Parent (Call_Node, Parent (Node_To_Replace));
5130
5131       return
5132          Try_Primitive_Operation
5133            (Call_Node       => Call_Node,
5134             Node_To_Replace => Node_To_Replace)
5135         or else
5136          Try_Class_Wide_Operation
5137            (Call_Node       => Call_Node,
5138             Node_To_Replace => Node_To_Replace);
5139    end Try_Object_Operation;
5140
5141 end Sem_Ch4;