OSDN Git Service

2005-03-29 Ed Falis <falis@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_Dereference
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          --  Error if no interpretation of the prefix has an access type
1255
1256          if Etype (N) = Any_Type then
1257             Error_Msg_N
1258               ("access type required in prefix of explicit dereference", P);
1259             Set_Etype (N, Any_Type);
1260             return;
1261          end if;
1262       end if;
1263
1264       if Is_Function_Type
1265         and then Nkind (Parent (N)) /= N_Indexed_Component
1266
1267         and then (Nkind (Parent (N)) /= N_Function_Call
1268                    or else N /= Name (Parent (N)))
1269
1270         and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
1271                    or else N /= Name (Parent (N)))
1272
1273         and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1274         and then (Nkind (Parent (N)) /= N_Attribute_Reference
1275                     or else
1276                       (Attribute_Name (Parent (N)) /= Name_Address
1277                         and then
1278                        Attribute_Name (Parent (N)) /= Name_Access))
1279       then
1280          --  Name is a function call with no actuals, in a context that
1281          --  requires deproceduring (including as an actual in an enclosing
1282          --  function or procedure call). There are some pathological cases
1283          --  where the prefix might include functions that return access to
1284          --  subprograms and others that return a regular type. Disambiguation
1285          --  of those has to take place in Resolve.
1286          --  See e.g. 7117-014 and E317-001.
1287
1288          New_N :=
1289            Make_Function_Call (Loc,
1290            Name => Make_Explicit_Dereference (Loc, P),
1291            Parameter_Associations => New_List);
1292
1293          --  If the prefix is overloaded, remove operations that have formals,
1294          --  we know that this is a parameterless call.
1295
1296          if Is_Overloaded (P) then
1297             Get_First_Interp (P, I, It);
1298             while Present (It.Nam) loop
1299                T := It.Typ;
1300
1301                if No (First_Formal (Base_Type (Designated_Type (T)))) then
1302                   Set_Etype (P, T);
1303                else
1304                   Remove_Interp (I);
1305                end if;
1306
1307                Get_Next_Interp (I, It);
1308             end loop;
1309          end if;
1310
1311          Rewrite (N, New_N);
1312          Analyze (N);
1313
1314       elsif not Is_Function_Type
1315         and then Is_Overloaded (N)
1316       then
1317          --  The prefix may include access to subprograms and other access
1318          --  types. If the context selects the interpretation that is a call,
1319          --  we cannot rewrite the node yet, but we include the result of
1320          --  the call interpretation.
1321
1322          Get_First_Interp (N, I, It);
1323          while Present (It.Nam) loop
1324             if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
1325                and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
1326             then
1327                Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
1328             end if;
1329
1330             Get_Next_Interp (I, It);
1331          end loop;
1332       end if;
1333
1334       --  A value of remote access-to-class-wide must not be dereferenced
1335       --  (RM E.2.2(16)).
1336
1337       Validate_Remote_Access_To_Class_Wide_Type (N);
1338    end Analyze_Explicit_Dereference;
1339
1340    ------------------------
1341    -- Analyze_Expression --
1342    ------------------------
1343
1344    procedure Analyze_Expression (N : Node_Id) is
1345    begin
1346       Analyze (N);
1347       Check_Parameterless_Call (N);
1348    end Analyze_Expression;
1349
1350    ------------------------------------
1351    -- Analyze_Indexed_Component_Form --
1352    ------------------------------------
1353
1354    procedure Analyze_Indexed_Component_Form (N : Node_Id) is
1355       P     : constant Node_Id := Prefix (N);
1356       Exprs : constant List_Id := Expressions (N);
1357       Exp   : Node_Id;
1358       P_T   : Entity_Id;
1359       E     : Node_Id;
1360       U_N   : Entity_Id;
1361
1362       procedure Process_Function_Call;
1363       --  Prefix in indexed component form is an overloadable entity,
1364       --  so the node is a function call. Reformat it as such.
1365
1366       procedure Process_Indexed_Component;
1367       --  Prefix in indexed component form is actually an indexed component.
1368       --  This routine processes it, knowing that the prefix is already
1369       --  resolved.
1370
1371       procedure Process_Indexed_Component_Or_Slice;
1372       --  An indexed component with a single index may designate a slice if
1373       --  the index is a subtype mark. This routine disambiguates these two
1374       --  cases by resolving the prefix to see if it is a subtype mark.
1375
1376       procedure Process_Overloaded_Indexed_Component;
1377       --  If the prefix of an indexed component is overloaded, the proper
1378       --  interpretation is selected by the index types and the context.
1379
1380       ---------------------------
1381       -- Process_Function_Call --
1382       ---------------------------
1383
1384       procedure Process_Function_Call is
1385          Actual : Node_Id;
1386
1387       begin
1388          Change_Node (N, N_Function_Call);
1389          Set_Name (N, P);
1390          Set_Parameter_Associations (N, Exprs);
1391
1392          Actual := First (Parameter_Associations (N));
1393          while Present (Actual) loop
1394             Analyze (Actual);
1395             Check_Parameterless_Call (Actual);
1396             Next_Actual (Actual);
1397          end loop;
1398
1399          Analyze_Call (N);
1400       end Process_Function_Call;
1401
1402       -------------------------------
1403       -- Process_Indexed_Component --
1404       -------------------------------
1405
1406       procedure Process_Indexed_Component is
1407          Exp          : Node_Id;
1408          Array_Type   : Entity_Id;
1409          Index        : Node_Id;
1410          Pent         : Entity_Id := Empty;
1411
1412       begin
1413          Exp := First (Exprs);
1414
1415          if Is_Overloaded (P) then
1416             Process_Overloaded_Indexed_Component;
1417
1418          else
1419             Array_Type := Etype (P);
1420
1421             if Is_Entity_Name (P) then
1422                Pent := Entity (P);
1423             elsif Nkind (P) = N_Selected_Component
1424               and then Is_Entity_Name (Selector_Name (P))
1425             then
1426                Pent := Entity (Selector_Name (P));
1427             end if;
1428
1429             --  Prefix must be appropriate for an array type, taking into
1430             --  account a possible implicit dereference.
1431
1432             if Is_Access_Type (Array_Type) then
1433                Array_Type := Designated_Type (Array_Type);
1434                Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
1435                Process_Implicit_Dereference_Prefix (Pent, P);
1436             end if;
1437
1438             if Is_Array_Type (Array_Type) then
1439                null;
1440
1441             elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
1442                Analyze (Exp);
1443                Set_Etype (N, Any_Type);
1444
1445                if not Has_Compatible_Type
1446                  (Exp, Entry_Index_Type (Pent))
1447                then
1448                   Error_Msg_N ("invalid index type in entry name", N);
1449
1450                elsif Present (Next (Exp)) then
1451                   Error_Msg_N ("too many subscripts in entry reference", N);
1452
1453                else
1454                   Set_Etype (N,  Etype (P));
1455                end if;
1456
1457                return;
1458
1459             elsif Is_Record_Type (Array_Type)
1460               and then Remote_AST_I_Dereference (P)
1461             then
1462                return;
1463
1464             elsif Array_Type = Any_Type then
1465                Set_Etype (N, Any_Type);
1466                return;
1467
1468             --  Here we definitely have a bad indexing
1469
1470             else
1471                if Nkind (Parent (N)) = N_Requeue_Statement
1472                  and then Present (Pent) and then Ekind (Pent) = E_Entry
1473                then
1474                   Error_Msg_N
1475                     ("REQUEUE does not permit parameters", First (Exprs));
1476
1477                elsif Is_Entity_Name (P)
1478                  and then Etype (P) = Standard_Void_Type
1479                then
1480                   Error_Msg_NE ("incorrect use of&", P, Entity (P));
1481
1482                else
1483                   Error_Msg_N ("array type required in indexed component", P);
1484                end if;
1485
1486                Set_Etype (N, Any_Type);
1487                return;
1488             end if;
1489
1490             Index := First_Index (Array_Type);
1491
1492             while Present (Index) and then Present (Exp) loop
1493                if not Has_Compatible_Type (Exp, Etype (Index)) then
1494                   Wrong_Type (Exp, Etype (Index));
1495                   Set_Etype (N, Any_Type);
1496                   return;
1497                end if;
1498
1499                Next_Index (Index);
1500                Next (Exp);
1501             end loop;
1502
1503             Set_Etype (N, Component_Type (Array_Type));
1504
1505             if Present (Index) then
1506                Error_Msg_N
1507                  ("too few subscripts in array reference", First (Exprs));
1508
1509             elsif Present (Exp) then
1510                Error_Msg_N ("too many subscripts in array reference", Exp);
1511             end if;
1512          end if;
1513       end Process_Indexed_Component;
1514
1515       ----------------------------------------
1516       -- Process_Indexed_Component_Or_Slice --
1517       ----------------------------------------
1518
1519       procedure Process_Indexed_Component_Or_Slice is
1520       begin
1521          Exp := First (Exprs);
1522          while Present (Exp) loop
1523             Analyze_Expression (Exp);
1524             Next (Exp);
1525          end loop;
1526
1527          Exp := First (Exprs);
1528
1529          --  If one index is present, and it is a subtype name, then the
1530          --  node denotes a slice (note that the case of an explicit range
1531          --  for a slice was already built as an N_Slice node in the first
1532          --  place, so that case is not handled here).
1533
1534          --  We use a replace rather than a rewrite here because this is one
1535          --  of the cases in which the tree built by the parser is plain wrong.
1536
1537          if No (Next (Exp))
1538            and then Is_Entity_Name (Exp)
1539            and then Is_Type (Entity (Exp))
1540          then
1541             Replace (N,
1542                Make_Slice (Sloc (N),
1543                  Prefix => P,
1544                  Discrete_Range => New_Copy (Exp)));
1545             Analyze (N);
1546
1547          --  Otherwise (more than one index present, or single index is not
1548          --  a subtype name), then we have the indexed component case.
1549
1550          else
1551             Process_Indexed_Component;
1552          end if;
1553       end Process_Indexed_Component_Or_Slice;
1554
1555       ------------------------------------------
1556       -- Process_Overloaded_Indexed_Component --
1557       ------------------------------------------
1558
1559       procedure Process_Overloaded_Indexed_Component is
1560          Exp   : Node_Id;
1561          I     : Interp_Index;
1562          It    : Interp;
1563          Typ   : Entity_Id;
1564          Index : Node_Id;
1565          Found : Boolean;
1566
1567       begin
1568          Set_Etype (N, Any_Type);
1569
1570          Get_First_Interp (P, I, It);
1571          while Present (It.Nam) loop
1572             Typ := It.Typ;
1573
1574             if Is_Access_Type (Typ) then
1575                Typ := Designated_Type (Typ);
1576                Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
1577             end if;
1578
1579             if Is_Array_Type (Typ) then
1580
1581                --  Got a candidate: verify that index types are compatible
1582
1583                Index := First_Index (Typ);
1584                Found := True;
1585                Exp := First (Exprs);
1586                while Present (Index) and then Present (Exp) loop
1587                   if Has_Compatible_Type (Exp, Etype (Index)) then
1588                      null;
1589                   else
1590                      Found := False;
1591                      Remove_Interp (I);
1592                      exit;
1593                   end if;
1594
1595                   Next_Index (Index);
1596                   Next (Exp);
1597                end loop;
1598
1599                if Found and then No (Index) and then No (Exp) then
1600                   Add_One_Interp (N,
1601                      Etype (Component_Type (Typ)),
1602                      Etype (Component_Type (Typ)));
1603                end if;
1604             end if;
1605
1606             Get_Next_Interp (I, It);
1607          end loop;
1608
1609          if Etype (N) = Any_Type then
1610             Error_Msg_N ("no legal interpetation for indexed component", N);
1611             Set_Is_Overloaded (N, False);
1612          end if;
1613
1614          End_Interp_List;
1615       end Process_Overloaded_Indexed_Component;
1616
1617    --  Start of processing for Analyze_Indexed_Component_Form
1618
1619    begin
1620       --  Get name of array, function or type
1621
1622       Analyze (P);
1623       if Nkind (N) = N_Function_Call
1624         or else Nkind (N) = N_Procedure_Call_Statement
1625       then
1626          --  If P is an explicit dereference whose prefix is of a
1627          --  remote access-to-subprogram type, then N has already
1628          --  been rewritten as a subprogram call and analyzed.
1629
1630          return;
1631       end if;
1632
1633       pragma Assert (Nkind (N) = N_Indexed_Component);
1634
1635       P_T := Base_Type (Etype (P));
1636
1637       if Is_Entity_Name (P)
1638         or else Nkind (P) = N_Operator_Symbol
1639       then
1640          U_N := Entity (P);
1641
1642          if Ekind (U_N) in Type_Kind then
1643
1644             --  Reformat node as a type conversion
1645
1646             E := Remove_Head (Exprs);
1647
1648             if Present (First (Exprs)) then
1649                Error_Msg_N
1650                 ("argument of type conversion must be single expression", N);
1651             end if;
1652
1653             Change_Node (N, N_Type_Conversion);
1654             Set_Subtype_Mark (N, P);
1655             Set_Etype (N, U_N);
1656             Set_Expression (N, E);
1657
1658             --  After changing the node, call for the specific Analysis
1659             --  routine directly, to avoid a double call to the expander.
1660
1661             Analyze_Type_Conversion (N);
1662             return;
1663          end if;
1664
1665          if Is_Overloadable (U_N) then
1666             Process_Function_Call;
1667
1668          elsif Ekind (Etype (P)) = E_Subprogram_Type
1669            or else (Is_Access_Type (Etype (P))
1670                       and then
1671                     Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type)
1672          then
1673             --  Call to access_to-subprogram with possible implicit dereference
1674
1675             Process_Function_Call;
1676
1677          elsif Is_Generic_Subprogram (U_N) then
1678
1679             --  A common beginner's (or C++ templates fan) error
1680
1681             Error_Msg_N ("generic subprogram cannot be called", N);
1682             Set_Etype (N, Any_Type);
1683             return;
1684
1685          else
1686             Process_Indexed_Component_Or_Slice;
1687          end if;
1688
1689       --  If not an entity name, prefix is an expression that may denote
1690       --  an array or an access-to-subprogram.
1691
1692       else
1693          if Ekind (P_T) = E_Subprogram_Type
1694            or else (Is_Access_Type (P_T)
1695                      and then
1696                     Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
1697          then
1698             Process_Function_Call;
1699
1700          elsif Nkind (P) = N_Selected_Component
1701            and then Is_Overloadable (Entity (Selector_Name (P)))
1702          then
1703             Process_Function_Call;
1704
1705          else
1706             --  Indexed component, slice, or a call to a member of a family
1707             --  entry, which will be converted to an entry call later.
1708
1709             Process_Indexed_Component_Or_Slice;
1710          end if;
1711       end if;
1712    end Analyze_Indexed_Component_Form;
1713
1714    ------------------------
1715    -- Analyze_Logical_Op --
1716    ------------------------
1717
1718    procedure Analyze_Logical_Op (N : Node_Id) is
1719       L     : constant Node_Id := Left_Opnd (N);
1720       R     : constant Node_Id := Right_Opnd (N);
1721       Op_Id : Entity_Id := Entity (N);
1722
1723    begin
1724       Set_Etype (N, Any_Type);
1725       Candidate_Type := Empty;
1726
1727       Analyze_Expression (L);
1728       Analyze_Expression (R);
1729
1730       if Present (Op_Id) then
1731
1732          if Ekind (Op_Id) = E_Operator then
1733             Find_Boolean_Types (L, R, Op_Id, N);
1734          else
1735             Add_One_Interp (N, Op_Id, Etype (Op_Id));
1736          end if;
1737
1738       else
1739          Op_Id := Get_Name_Entity_Id (Chars (N));
1740
1741          while Present (Op_Id) loop
1742             if Ekind (Op_Id) = E_Operator then
1743                Find_Boolean_Types (L, R, Op_Id, N);
1744             else
1745                Analyze_User_Defined_Binary_Op (N, Op_Id);
1746             end if;
1747
1748             Op_Id := Homonym (Op_Id);
1749          end loop;
1750       end if;
1751
1752       Operator_Check (N);
1753    end Analyze_Logical_Op;
1754
1755    ---------------------------
1756    -- Analyze_Membership_Op --
1757    ---------------------------
1758
1759    procedure Analyze_Membership_Op (N : Node_Id) is
1760       L     : constant Node_Id := Left_Opnd (N);
1761       R     : constant Node_Id := Right_Opnd (N);
1762
1763       Index : Interp_Index;
1764       It    : Interp;
1765       Found : Boolean := False;
1766       I_F   : Interp_Index;
1767       T_F   : Entity_Id;
1768
1769       procedure Try_One_Interp (T1 : Entity_Id);
1770       --  Routine to try one proposed interpretation. Note that the context
1771       --  of the operation plays no role in resolving the arguments, so that
1772       --  if there is more than one interpretation of the operands that is
1773       --  compatible with a membership test, the operation is ambiguous.
1774
1775       --------------------
1776       -- Try_One_Interp --
1777       --------------------
1778
1779       procedure Try_One_Interp (T1 : Entity_Id) is
1780       begin
1781          if Has_Compatible_Type (R, T1) then
1782             if Found
1783               and then Base_Type (T1) /= Base_Type (T_F)
1784             then
1785                It := Disambiguate (L, I_F, Index, Any_Type);
1786
1787                if It = No_Interp then
1788                   Ambiguous_Operands (N);
1789                   Set_Etype (L, Any_Type);
1790                   return;
1791
1792                else
1793                   T_F := It.Typ;
1794                end if;
1795
1796             else
1797                Found := True;
1798                T_F   := T1;
1799                I_F   := Index;
1800             end if;
1801
1802             Set_Etype (L, T_F);
1803          end if;
1804
1805       end Try_One_Interp;
1806
1807    --  Start of processing for Analyze_Membership_Op
1808
1809    begin
1810       Analyze_Expression (L);
1811
1812       if Nkind (R) = N_Range
1813         or else (Nkind (R) = N_Attribute_Reference
1814                   and then Attribute_Name (R) = Name_Range)
1815       then
1816          Analyze (R);
1817
1818          if not Is_Overloaded (L) then
1819             Try_One_Interp (Etype (L));
1820
1821          else
1822             Get_First_Interp (L, Index, It);
1823
1824             while Present (It.Typ) loop
1825                Try_One_Interp (It.Typ);
1826                Get_Next_Interp (Index, It);
1827             end loop;
1828          end if;
1829
1830       --  If not a range, it can only be a subtype mark, or else there
1831       --  is a more basic error, to be diagnosed in Find_Type.
1832
1833       else
1834          Find_Type (R);
1835
1836          if Is_Entity_Name (R) then
1837             Check_Fully_Declared (Entity (R), R);
1838          end if;
1839       end if;
1840
1841       --  Compatibility between expression and subtype mark or range is
1842       --  checked during resolution. The result of the operation is Boolean
1843       --  in any case.
1844
1845       Set_Etype (N, Standard_Boolean);
1846    end Analyze_Membership_Op;
1847
1848    ----------------------
1849    -- Analyze_Negation --
1850    ----------------------
1851
1852    procedure Analyze_Negation (N : Node_Id) is
1853       R     : constant Node_Id := Right_Opnd (N);
1854       Op_Id : Entity_Id := Entity (N);
1855
1856    begin
1857       Set_Etype (N, Any_Type);
1858       Candidate_Type := Empty;
1859
1860       Analyze_Expression (R);
1861
1862       if Present (Op_Id) then
1863          if Ekind (Op_Id) = E_Operator then
1864             Find_Negation_Types (R, Op_Id, N);
1865          else
1866             Add_One_Interp (N, Op_Id, Etype (Op_Id));
1867          end if;
1868
1869       else
1870          Op_Id := Get_Name_Entity_Id (Chars (N));
1871          while Present (Op_Id) loop
1872             if Ekind (Op_Id) = E_Operator then
1873                Find_Negation_Types (R, Op_Id, N);
1874             else
1875                Analyze_User_Defined_Unary_Op (N, Op_Id);
1876             end if;
1877
1878             Op_Id := Homonym (Op_Id);
1879          end loop;
1880       end if;
1881
1882       Operator_Check (N);
1883    end Analyze_Negation;
1884
1885    ------------------
1886    -- Analyze_Null --
1887    ------------------
1888
1889    procedure Analyze_Null (N : Node_Id) is
1890    begin
1891       Set_Etype (N, Any_Access);
1892    end Analyze_Null;
1893
1894    ----------------------
1895    -- Analyze_One_Call --
1896    ----------------------
1897
1898    procedure Analyze_One_Call
1899       (N       : Node_Id;
1900        Nam     : Entity_Id;
1901        Report  : Boolean;
1902        Success : out Boolean)
1903    is
1904       Actuals    : constant List_Id   := Parameter_Associations (N);
1905       Prev_T     : constant Entity_Id := Etype (N);
1906       Formal     : Entity_Id;
1907       Actual     : Node_Id;
1908       Is_Indexed : Boolean := False;
1909       Subp_Type  : constant Entity_Id := Etype (Nam);
1910       Norm_OK    : Boolean;
1911
1912       procedure Indicate_Name_And_Type;
1913       --  If candidate interpretation matches, indicate name and type of
1914       --  result on call node.
1915
1916       ----------------------------
1917       -- Indicate_Name_And_Type --
1918       ----------------------------
1919
1920       procedure Indicate_Name_And_Type is
1921       begin
1922          Add_One_Interp (N, Nam, Etype (Nam));
1923          Success := True;
1924
1925          --  If the prefix of the call is a name, indicate the entity
1926          --  being called. If it is not a name,  it is an expression that
1927          --  denotes an access to subprogram or else an entry or family. In
1928          --  the latter case, the name is a selected component, and the entity
1929          --  being called is noted on the selector.
1930
1931          if not Is_Type (Nam) then
1932             if Is_Entity_Name (Name (N))
1933               or else Nkind (Name (N)) = N_Operator_Symbol
1934             then
1935                Set_Entity (Name (N), Nam);
1936
1937             elsif Nkind (Name (N)) = N_Selected_Component then
1938                Set_Entity (Selector_Name (Name (N)),  Nam);
1939             end if;
1940          end if;
1941
1942          if Debug_Flag_E and not Report then
1943             Write_Str (" Overloaded call ");
1944             Write_Int (Int (N));
1945             Write_Str (" compatible with ");
1946             Write_Int (Int (Nam));
1947             Write_Eol;
1948          end if;
1949       end Indicate_Name_And_Type;
1950
1951    --  Start of processing for Analyze_One_Call
1952
1953    begin
1954       Success := False;
1955
1956       --  If the subprogram has no formals, or if all the formals have
1957       --  defaults, and the return type is an array type, the node may
1958       --  denote an indexing of the result of a parameterless call.
1959
1960       if Needs_No_Actuals (Nam)
1961         and then Present (Actuals)
1962       then
1963          if Is_Array_Type (Subp_Type) then
1964             Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type);
1965
1966          elsif Is_Access_Type (Subp_Type)
1967            and then Is_Array_Type (Designated_Type (Subp_Type))
1968          then
1969             Is_Indexed :=
1970               Try_Indexed_Call (N, Nam, Designated_Type (Subp_Type));
1971
1972          elsif Is_Access_Type (Subp_Type)
1973            and then Ekind (Designated_Type (Subp_Type))  = E_Subprogram_Type
1974          then
1975             Is_Indexed := Try_Indirect_Call (N, Nam, Subp_Type);
1976          end if;
1977
1978       end if;
1979
1980       Normalize_Actuals (N, Nam, (Report and not Is_Indexed), Norm_OK);
1981
1982       if not Norm_OK then
1983
1984          --  Mismatch in number or names of parameters
1985
1986          if Debug_Flag_E then
1987             Write_Str (" normalization fails in call ");
1988             Write_Int (Int (N));
1989             Write_Str (" with subprogram ");
1990             Write_Int (Int (Nam));
1991             Write_Eol;
1992          end if;
1993
1994       --  If the context expects a function call, discard any interpretation
1995       --  that is a procedure. If the node is not overloaded, leave as is for
1996       --  better error reporting when type mismatch is found.
1997
1998       elsif Nkind (N) = N_Function_Call
1999         and then Is_Overloaded (Name (N))
2000         and then Ekind (Nam) = E_Procedure
2001       then
2002          return;
2003
2004       --  Ditto for function calls in a procedure context
2005
2006       elsif Nkind (N) = N_Procedure_Call_Statement
2007          and then Is_Overloaded (Name (N))
2008          and then Etype (Nam) /= Standard_Void_Type
2009       then
2010          return;
2011
2012       elsif not Present (Actuals) then
2013
2014          --  If Normalize succeeds, then there are default parameters for
2015          --  all formals.
2016
2017          Indicate_Name_And_Type;
2018
2019       elsif Ekind (Nam) = E_Operator then
2020          if Nkind (N) = N_Procedure_Call_Statement then
2021             return;
2022          end if;
2023
2024          --  This can occur when the prefix of the call is an operator
2025          --  name or an expanded name whose selector is an operator name.
2026
2027          Analyze_Operator_Call (N, Nam);
2028
2029          if Etype (N) /= Prev_T then
2030
2031             --  There may be a user-defined operator that hides the
2032             --  current interpretation. We must check for this independently
2033             --  of the analysis of the call with the user-defined operation,
2034             --  because the parameter names may be wrong and yet the hiding
2035             --  takes place. Fixes b34014o.
2036
2037             if Is_Overloaded (Name (N)) then
2038                declare
2039                   I  : Interp_Index;
2040                   It : Interp;
2041
2042                begin
2043                   Get_First_Interp (Name (N), I, It);
2044                   while Present (It.Nam) loop
2045                      if Ekind (It.Nam) /= E_Operator
2046                         and then Hides_Op (It.Nam, Nam)
2047                         and then
2048                           Has_Compatible_Type
2049                             (First_Actual (N), Etype (First_Formal (It.Nam)))
2050                         and then (No (Next_Actual (First_Actual (N)))
2051                            or else Has_Compatible_Type
2052                             (Next_Actual (First_Actual (N)),
2053                              Etype (Next_Formal (First_Formal (It.Nam)))))
2054                      then
2055                         Set_Etype (N, Prev_T);
2056                         return;
2057                      end if;
2058
2059                      Get_Next_Interp (I, It);
2060                   end loop;
2061                end;
2062             end if;
2063
2064             --  If operator matches formals, record its name on the call.
2065             --  If the operator is overloaded, Resolve will select the
2066             --  correct one from the list of interpretations. The call
2067             --  node itself carries the first candidate.
2068
2069             Set_Entity (Name (N), Nam);
2070             Success := True;
2071
2072          elsif Report and then Etype (N) = Any_Type then
2073             Error_Msg_N ("incompatible arguments for operator", N);
2074          end if;
2075
2076       else
2077          --  Normalize_Actuals has chained the named associations in the
2078          --  correct order of the formals.
2079
2080          Actual := First_Actual (N);
2081          Formal := First_Formal (Nam);
2082          while Present (Actual) and then Present (Formal) loop
2083             if Nkind (Parent (Actual)) /= N_Parameter_Association
2084               or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
2085             then
2086                if Has_Compatible_Type (Actual, Etype (Formal)) then
2087                   Next_Actual (Actual);
2088                   Next_Formal (Formal);
2089
2090                else
2091                   if Debug_Flag_E then
2092                      Write_Str (" type checking fails in call ");
2093                      Write_Int (Int (N));
2094                      Write_Str (" with formal ");
2095                      Write_Int (Int (Formal));
2096                      Write_Str (" in subprogram ");
2097                      Write_Int (Int (Nam));
2098                      Write_Eol;
2099                   end if;
2100
2101                   if Report and not Is_Indexed then
2102                      Wrong_Type (Actual, Etype (Formal));
2103
2104                      if Nkind (Actual) = N_Op_Eq
2105                        and then Nkind (Left_Opnd (Actual)) = N_Identifier
2106                      then
2107                         Formal := First_Formal (Nam);
2108
2109                         while Present (Formal) loop
2110
2111                            if Chars (Left_Opnd (Actual)) = Chars (Formal) then
2112                               Error_Msg_N
2113                                 ("possible misspelling of `='>`!", Actual);
2114                               exit;
2115                            end if;
2116
2117                            Next_Formal (Formal);
2118                         end loop;
2119                      end if;
2120
2121                      if All_Errors_Mode then
2122                         Error_Msg_Sloc := Sloc (Nam);
2123
2124                         if Is_Overloadable (Nam)
2125                           and then Present (Alias (Nam))
2126                           and then not Comes_From_Source (Nam)
2127                         then
2128                            Error_Msg_NE
2129                              ("  =='> in call to &#(inherited)!", Actual, Nam);
2130
2131                         elsif Ekind (Nam) = E_Subprogram_Type then
2132                            declare
2133                               Access_To_Subprogram_Typ :
2134                                 constant Entity_Id :=
2135                                   Defining_Identifier
2136                                     (Associated_Node_For_Itype (Nam));
2137                            begin
2138                               Error_Msg_NE (
2139                                 "  =='> in call to dereference of &#!",
2140                                 Actual, Access_To_Subprogram_Typ);
2141                            end;
2142
2143                         else
2144                            Error_Msg_NE ("  =='> in call to &#!", Actual, Nam);
2145
2146                         end if;
2147                      end if;
2148                   end if;
2149
2150                   return;
2151                end if;
2152
2153             else
2154                --  Normalize_Actuals has verified that a default value exists
2155                --  for this formal. Current actual names a subsequent formal.
2156
2157                Next_Formal (Formal);
2158             end if;
2159          end loop;
2160
2161          --  On exit, all actuals match
2162
2163          Indicate_Name_And_Type;
2164       end if;
2165    end Analyze_One_Call;
2166
2167    ---------------------------
2168    -- Analyze_Operator_Call --
2169    ---------------------------
2170
2171    procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
2172       Op_Name : constant Name_Id := Chars (Op_Id);
2173       Act1    : constant Node_Id := First_Actual (N);
2174       Act2    : constant Node_Id := Next_Actual (Act1);
2175
2176    begin
2177       --  Binary operator case
2178
2179       if Present (Act2) then
2180
2181          --  If more than two operands, then not binary operator after all
2182
2183          if Present (Next_Actual (Act2)) then
2184             return;
2185
2186          elsif     Op_Name = Name_Op_Add
2187            or else Op_Name = Name_Op_Subtract
2188            or else Op_Name = Name_Op_Multiply
2189            or else Op_Name = Name_Op_Divide
2190            or else Op_Name = Name_Op_Mod
2191            or else Op_Name = Name_Op_Rem
2192            or else Op_Name = Name_Op_Expon
2193          then
2194             Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
2195
2196          elsif     Op_Name =  Name_Op_And
2197            or else Op_Name = Name_Op_Or
2198            or else Op_Name = Name_Op_Xor
2199          then
2200             Find_Boolean_Types (Act1, Act2, Op_Id, N);
2201
2202          elsif     Op_Name = Name_Op_Lt
2203            or else Op_Name = Name_Op_Le
2204            or else Op_Name = Name_Op_Gt
2205            or else Op_Name = Name_Op_Ge
2206          then
2207             Find_Comparison_Types (Act1, Act2, Op_Id,  N);
2208
2209          elsif     Op_Name = Name_Op_Eq
2210            or else Op_Name = Name_Op_Ne
2211          then
2212             Find_Equality_Types (Act1, Act2, Op_Id,  N);
2213
2214          elsif     Op_Name = Name_Op_Concat then
2215             Find_Concatenation_Types (Act1, Act2, Op_Id, N);
2216
2217          --  Is this else null correct, or should it be an abort???
2218
2219          else
2220             null;
2221          end if;
2222
2223       --  Unary operator case
2224
2225       else
2226          if Op_Name = Name_Op_Subtract or else
2227             Op_Name = Name_Op_Add      or else
2228             Op_Name = Name_Op_Abs
2229          then
2230             Find_Unary_Types (Act1, Op_Id, N);
2231
2232          elsif
2233             Op_Name = Name_Op_Not
2234          then
2235             Find_Negation_Types (Act1, Op_Id, N);
2236
2237          --  Is this else null correct, or should it be an abort???
2238
2239          else
2240             null;
2241          end if;
2242       end if;
2243    end Analyze_Operator_Call;
2244
2245    -------------------------------------------
2246    -- Analyze_Overloaded_Selected_Component --
2247    -------------------------------------------
2248
2249    procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
2250       Nam   : constant Node_Id := Prefix (N);
2251       Sel   : constant Node_Id := Selector_Name (N);
2252       Comp  : Entity_Id;
2253       I     : Interp_Index;
2254       It    : Interp;
2255       T     : Entity_Id;
2256
2257    begin
2258       Set_Etype (Sel, Any_Type);
2259
2260       Get_First_Interp (Nam, I, It);
2261       while Present (It.Typ) loop
2262          if Is_Access_Type (It.Typ) then
2263             T := Designated_Type (It.Typ);
2264             Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2265          else
2266             T := It.Typ;
2267          end if;
2268
2269          if Is_Record_Type (T) then
2270             Comp := First_Entity (T);
2271             while Present (Comp) loop
2272                if Chars (Comp) = Chars (Sel)
2273                  and then Is_Visible_Component (Comp)
2274                then
2275                   Set_Entity_With_Style_Check (Sel, Comp);
2276                   Generate_Reference (Comp, Sel);
2277
2278                   Set_Etype (Sel, Etype (Comp));
2279                   Add_One_Interp (N, Etype (Comp), Etype (Comp));
2280
2281                   --  This also specifies a candidate to resolve the name.
2282                   --  Further overloading will be resolved from context.
2283
2284                   Set_Etype (Nam, It.Typ);
2285                end if;
2286
2287                Next_Entity (Comp);
2288             end loop;
2289
2290          elsif Is_Concurrent_Type (T) then
2291             Comp := First_Entity (T);
2292             while Present (Comp)
2293               and then Comp /= First_Private_Entity (T)
2294             loop
2295                if Chars (Comp) = Chars (Sel) then
2296                   if Is_Overloadable (Comp) then
2297                      Add_One_Interp (Sel, Comp, Etype (Comp));
2298                   else
2299                      Set_Entity_With_Style_Check (Sel, Comp);
2300                      Generate_Reference (Comp, Sel);
2301                   end if;
2302
2303                   Set_Etype (Sel, Etype (Comp));
2304                   Set_Etype (N,   Etype (Comp));
2305                   Set_Etype (Nam, It.Typ);
2306
2307                   --  For access type case, introduce explicit deference for
2308                   --  more uniform treatment of entry calls.
2309
2310                   if Is_Access_Type (Etype (Nam)) then
2311                      Insert_Explicit_Dereference (Nam);
2312                      Error_Msg_NW
2313                        (Warn_On_Dereference, "?implicit dereference", N);
2314                   end if;
2315                end if;
2316
2317                Next_Entity (Comp);
2318             end loop;
2319
2320             Set_Is_Overloaded (N, Is_Overloaded (Sel));
2321          end if;
2322
2323          Get_Next_Interp (I, It);
2324       end loop;
2325
2326       if Etype (N) = Any_Type then
2327          Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
2328          Set_Entity (Sel, Any_Id);
2329          Set_Etype  (Sel, Any_Type);
2330       end if;
2331    end Analyze_Overloaded_Selected_Component;
2332
2333    ----------------------------------
2334    -- Analyze_Qualified_Expression --
2335    ----------------------------------
2336
2337    procedure Analyze_Qualified_Expression (N : Node_Id) is
2338       Mark : constant Entity_Id := Subtype_Mark (N);
2339       T    : Entity_Id;
2340
2341    begin
2342       Set_Etype (N, Any_Type);
2343       Find_Type (Mark);
2344       T := Entity (Mark);
2345
2346       if T = Any_Type then
2347          return;
2348       end if;
2349
2350       Check_Fully_Declared (T, N);
2351       Analyze_Expression (Expression (N));
2352       Set_Etype  (N, T);
2353    end Analyze_Qualified_Expression;
2354
2355    -------------------
2356    -- Analyze_Range --
2357    -------------------
2358
2359    procedure Analyze_Range (N : Node_Id) is
2360       L        : constant Node_Id := Low_Bound (N);
2361       H        : constant Node_Id := High_Bound (N);
2362       I1, I2   : Interp_Index;
2363       It1, It2 : Interp;
2364
2365       procedure Check_Common_Type (T1, T2 : Entity_Id);
2366       --  Verify the compatibility of two types,  and choose the
2367       --  non universal one if the other is universal.
2368
2369       procedure Check_High_Bound (T : Entity_Id);
2370       --  Test one interpretation of the low bound against all those
2371       --  of the high bound.
2372
2373       procedure Check_Universal_Expression (N : Node_Id);
2374       --  In Ada83, reject bounds of a universal range that are not
2375       --  literals or entity names.
2376
2377       -----------------------
2378       -- Check_Common_Type --
2379       -----------------------
2380
2381       procedure Check_Common_Type (T1, T2 : Entity_Id) is
2382       begin
2383          if Covers (T1, T2) or else Covers (T2, T1) then
2384             if T1 = Universal_Integer
2385               or else T1 = Universal_Real
2386               or else T1 = Any_Character
2387             then
2388                Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
2389
2390             elsif T1 = T2 then
2391                Add_One_Interp (N, T1, T1);
2392
2393             else
2394                Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
2395             end if;
2396          end if;
2397       end Check_Common_Type;
2398
2399       ----------------------
2400       -- Check_High_Bound --
2401       ----------------------
2402
2403       procedure Check_High_Bound (T : Entity_Id) is
2404       begin
2405          if not Is_Overloaded (H) then
2406             Check_Common_Type (T, Etype (H));
2407          else
2408             Get_First_Interp (H, I2, It2);
2409             while Present (It2.Typ) loop
2410                Check_Common_Type (T, It2.Typ);
2411                Get_Next_Interp (I2, It2);
2412             end loop;
2413          end if;
2414       end Check_High_Bound;
2415
2416       -----------------------------
2417       -- Is_Universal_Expression --
2418       -----------------------------
2419
2420       procedure Check_Universal_Expression (N : Node_Id) is
2421       begin
2422          if Etype (N) = Universal_Integer
2423            and then Nkind (N) /= N_Integer_Literal
2424            and then not Is_Entity_Name (N)
2425            and then Nkind (N) /= N_Attribute_Reference
2426          then
2427             Error_Msg_N ("illegal bound in discrete range", N);
2428          end if;
2429       end Check_Universal_Expression;
2430
2431    --  Start of processing for Analyze_Range
2432
2433    begin
2434       Set_Etype (N, Any_Type);
2435       Analyze_Expression (L);
2436       Analyze_Expression (H);
2437
2438       if Etype (L) = Any_Type or else Etype (H) = Any_Type then
2439          return;
2440
2441       else
2442          if not Is_Overloaded (L) then
2443             Check_High_Bound (Etype (L));
2444          else
2445             Get_First_Interp (L, I1, It1);
2446             while Present (It1.Typ) loop
2447                Check_High_Bound (It1.Typ);
2448                Get_Next_Interp (I1, It1);
2449             end loop;
2450          end if;
2451
2452          --  If result is Any_Type, then we did not find a compatible pair
2453
2454          if Etype (N) = Any_Type then
2455             Error_Msg_N ("incompatible types in range ", N);
2456          end if;
2457       end if;
2458
2459       if Ada_Version = Ada_83
2460         and then
2461           (Nkind (Parent (N)) = N_Loop_Parameter_Specification
2462              or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
2463       then
2464          Check_Universal_Expression (L);
2465          Check_Universal_Expression (H);
2466       end if;
2467    end Analyze_Range;
2468
2469    -----------------------
2470    -- Analyze_Reference --
2471    -----------------------
2472
2473    procedure Analyze_Reference (N : Node_Id) is
2474       P        : constant Node_Id := Prefix (N);
2475       Acc_Type : Entity_Id;
2476    begin
2477       Analyze (P);
2478       Acc_Type := Create_Itype (E_Allocator_Type, N);
2479       Set_Etype                    (Acc_Type,  Acc_Type);
2480       Init_Size_Align              (Acc_Type);
2481       Set_Directly_Designated_Type (Acc_Type, Etype (P));
2482       Set_Etype (N, Acc_Type);
2483    end Analyze_Reference;
2484
2485    --------------------------------
2486    -- Analyze_Selected_Component --
2487    --------------------------------
2488
2489    --  Prefix is a record type or a task or protected type. In the
2490    --  later case, the selector must denote a visible entry.
2491
2492    procedure Analyze_Selected_Component (N : Node_Id) is
2493       Name        : constant Node_Id := Prefix (N);
2494       Sel         : constant Node_Id := Selector_Name (N);
2495       Comp        : Entity_Id;
2496       Entity_List : Entity_Id;
2497       Prefix_Type : Entity_Id;
2498       Pent        : Entity_Id := Empty;
2499       Act_Decl    : Node_Id;
2500       In_Scope    : Boolean;
2501       Parent_N    : Node_Id;
2502
2503    --  Start of processing for Analyze_Selected_Component
2504
2505    begin
2506       Set_Etype (N, Any_Type);
2507
2508       if Is_Overloaded (Name) then
2509          Analyze_Overloaded_Selected_Component (N);
2510          return;
2511
2512       elsif Etype (Name) = Any_Type then
2513          Set_Entity (Sel, Any_Id);
2514          Set_Etype (Sel, Any_Type);
2515          return;
2516
2517       else
2518          --  Function calls that are prefixes of selected components must be
2519          --  fully resolved in case we need to build an actual subtype, or
2520          --  do some other operation requiring a fully resolved prefix.
2521
2522          --  Note: Resolving all Nkinds of nodes here doesn't work.
2523          --  (Breaks 2129-008) ???.
2524
2525          if Nkind (Name) = N_Function_Call then
2526             Resolve (Name);
2527          end if;
2528
2529          Prefix_Type := Etype (Name);
2530       end if;
2531
2532       if Is_Access_Type (Prefix_Type) then
2533
2534          --  A RACW object can never be used as prefix of a selected
2535          --  component since that means it is dereferenced without
2536          --  being a controlling operand of a dispatching operation
2537          --  (RM E.2.2(15)).
2538
2539          if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
2540            and then Comes_From_Source (N)
2541          then
2542             Error_Msg_N
2543               ("invalid dereference of a remote access to class-wide value",
2544                N);
2545
2546          --  Normal case of selected component applied to access type
2547
2548          else
2549             Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2550
2551             if Is_Entity_Name (Name) then
2552                Pent := Entity (Name);
2553             elsif Nkind (Name) = N_Selected_Component
2554               and then Is_Entity_Name (Selector_Name (Name))
2555             then
2556                Pent := Entity (Selector_Name (Name));
2557             end if;
2558
2559             Process_Implicit_Dereference_Prefix (Pent, Name);
2560          end if;
2561
2562          Prefix_Type := Designated_Type (Prefix_Type);
2563       end if;
2564
2565       if Ekind (Prefix_Type) = E_Private_Subtype then
2566          Prefix_Type := Base_Type (Prefix_Type);
2567       end if;
2568
2569       Entity_List := Prefix_Type;
2570
2571       --  For class-wide types, use the entity list of the root type. This
2572       --  indirection is specially important for private extensions because
2573       --  only the root type get switched (not the class-wide type).
2574
2575       if Is_Class_Wide_Type (Prefix_Type) then
2576          Entity_List := Root_Type (Prefix_Type);
2577       end if;
2578
2579       Comp := First_Entity (Entity_List);
2580
2581       --  If the selector has an original discriminant, the node appears in
2582       --  an instance. Replace the discriminant with the corresponding one
2583       --  in the current discriminated type. For nested generics, this must
2584       --  be done transitively, so note the new original discriminant.
2585
2586       if Nkind (Sel) = N_Identifier
2587         and then Present (Original_Discriminant (Sel))
2588       then
2589          Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
2590
2591          --  Mark entity before rewriting, for completeness and because
2592          --  subsequent semantic checks might examine the original node.
2593
2594          Set_Entity (Sel, Comp);
2595          Rewrite (Selector_Name (N),
2596            New_Occurrence_Of (Comp, Sloc (N)));
2597          Set_Original_Discriminant (Selector_Name (N), Comp);
2598          Set_Etype (N, Etype (Comp));
2599
2600          if Is_Access_Type (Etype (Name)) then
2601             Insert_Explicit_Dereference (Name);
2602             Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2603          end if;
2604
2605       elsif Is_Record_Type (Prefix_Type) then
2606
2607          --  Find component with given name
2608
2609          while Present (Comp) loop
2610             if Chars (Comp) = Chars (Sel)
2611               and then Is_Visible_Component (Comp)
2612             then
2613                Set_Entity_With_Style_Check (Sel, Comp);
2614                Generate_Reference (Comp, Sel);
2615
2616                Set_Etype (Sel, Etype (Comp));
2617
2618                if Ekind (Comp) = E_Discriminant then
2619                   if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
2620                      Error_Msg_N
2621                        ("cannot reference discriminant of Unchecked_Union",
2622                         Sel);
2623                   end if;
2624
2625                   if Is_Generic_Type (Prefix_Type)
2626                        or else
2627                      Is_Generic_Type (Root_Type (Prefix_Type))
2628                   then
2629                      Set_Original_Discriminant (Sel, Comp);
2630                   end if;
2631                end if;
2632
2633                --  Resolve the prefix early otherwise it is not possible to
2634                --  build the actual subtype of the component: it may need
2635                --  to duplicate this prefix and duplication is only allowed
2636                --  on fully resolved expressions.
2637
2638                Resolve (Name);
2639
2640                --  We never need an actual subtype for the case of a selection
2641                --  for a indexed component of a non-packed array, since in
2642                --  this case gigi generates all the checks and can find the
2643                --  necessary bounds information.
2644
2645                --  We also do not need an actual subtype for the case of
2646                --  a first, last, length, or range attribute applied to a
2647                --  non-packed array, since gigi can again get the bounds in
2648                --  these cases (gigi cannot handle the packed case, since it
2649                --  has the bounds of the packed array type, not the original
2650                --  bounds of the type). However, if the prefix is itself a
2651                --  selected component, as in a.b.c (i), gigi may regard a.b.c
2652                --  as a dynamic-sized temporary, so we do generate an actual
2653                --  subtype for this case.
2654
2655                Parent_N := Parent (N);
2656
2657                if not Is_Packed (Etype (Comp))
2658                  and then
2659                    ((Nkind (Parent_N) = N_Indexed_Component
2660                       and then Nkind (Name) /= N_Selected_Component)
2661                      or else
2662                       (Nkind (Parent_N) = N_Attribute_Reference
2663                          and then (Attribute_Name (Parent_N) = Name_First
2664                                      or else
2665                                    Attribute_Name (Parent_N) = Name_Last
2666                                      or else
2667                                    Attribute_Name (Parent_N) = Name_Length
2668                                      or else
2669                                    Attribute_Name (Parent_N) = Name_Range)))
2670                then
2671                   Set_Etype (N, Etype (Comp));
2672
2673                --  If full analysis is not enabled, we do not generate an
2674                --  actual subtype, because in the absence of expansion
2675                --  reference to a formal of a protected type, for example,
2676                --  will not be properly transformed, and will lead to
2677                --  out-of-scope references in gigi.
2678
2679                --  In all other cases, we currently build an actual subtype.
2680                --  It seems likely that many of these cases can be avoided,
2681                --  but right now, the front end makes direct references to the
2682                --  bounds (e.g. in generating a length check), and if we do
2683                --  not make an actual subtype, we end up getting a direct
2684                --  reference to a discriminant, which will not do.
2685
2686                elsif Full_Analysis then
2687                   Act_Decl :=
2688                     Build_Actual_Subtype_Of_Component (Etype (Comp), N);
2689                   Insert_Action (N, Act_Decl);
2690
2691                   if No (Act_Decl) then
2692                      Set_Etype (N, Etype (Comp));
2693
2694                   else
2695                      --  Component type depends on discriminants. Enter the
2696                      --  main attributes of the subtype.
2697
2698                      declare
2699                         Subt : constant Entity_Id :=
2700                                  Defining_Identifier (Act_Decl);
2701
2702                      begin
2703                         Set_Etype (Subt, Base_Type (Etype (Comp)));
2704                         Set_Ekind (Subt, Ekind (Etype (Comp)));
2705                         Set_Etype (N, Subt);
2706                      end;
2707                   end if;
2708
2709                --  If Full_Analysis not enabled, just set the Etype
2710
2711                else
2712                   Set_Etype (N, Etype (Comp));
2713                end if;
2714
2715                return;
2716             end if;
2717
2718             Next_Entity (Comp);
2719          end loop;
2720
2721          --  Ada 2005 (AI-252)
2722
2723          if Ada_Version >= Ada_05
2724            and then Is_Tagged_Type (Prefix_Type)
2725            and then Try_Object_Operation (N)
2726          then
2727             return;
2728
2729             --  If the transformation fails, it will be necessary to redo the
2730             --  analysis with all errors enabled, to indicate candidate
2731             --  interpretations and reasons for each failure ???
2732
2733          end if;
2734
2735       elsif Is_Private_Type (Prefix_Type) then
2736
2737          --  Allow access only to discriminants of the type. If the type has
2738          --  no full view, gigi uses the parent type for the components, so we
2739          --  do the same here.
2740
2741          if No (Full_View (Prefix_Type)) then
2742             Entity_List := Root_Type (Base_Type (Prefix_Type));
2743             Comp := First_Entity (Entity_List);
2744          end if;
2745
2746          while Present (Comp) loop
2747             if Chars (Comp) = Chars (Sel) then
2748                if Ekind (Comp) = E_Discriminant then
2749                   Set_Entity_With_Style_Check (Sel, Comp);
2750                   Generate_Reference (Comp, Sel);
2751
2752                   Set_Etype (Sel, Etype (Comp));
2753                   Set_Etype (N,   Etype (Comp));
2754
2755                   if Is_Generic_Type (Prefix_Type)
2756                     or else
2757                      Is_Generic_Type (Root_Type (Prefix_Type))
2758                   then
2759                      Set_Original_Discriminant (Sel, Comp);
2760                   end if;
2761
2762                else
2763                   Error_Msg_NE
2764                     ("invisible selector for }",
2765                      N, First_Subtype (Prefix_Type));
2766                   Set_Entity (Sel, Any_Id);
2767                   Set_Etype (N, Any_Type);
2768                end if;
2769
2770                return;
2771             end if;
2772
2773             Next_Entity (Comp);
2774          end loop;
2775
2776       elsif Is_Concurrent_Type (Prefix_Type) then
2777
2778          --  Prefix is concurrent type. Find visible operation with given name
2779          --  For a task, this can only include entries or discriminants if the
2780          --  task type is not an enclosing scope. If it is an enclosing scope
2781          --  (e.g. in an inner task) then all entities are visible, but the
2782          --  prefix must denote the enclosing scope, i.e. can only be a direct
2783          --  name or an expanded name.
2784
2785          Set_Etype (Sel,  Any_Type);
2786          In_Scope := In_Open_Scopes (Prefix_Type);
2787
2788          while Present (Comp) loop
2789             if Chars (Comp) = Chars (Sel) then
2790                if Is_Overloadable (Comp) then
2791                   Add_One_Interp (Sel, Comp, Etype (Comp));
2792
2793                elsif Ekind (Comp) = E_Discriminant
2794                  or else Ekind (Comp) = E_Entry_Family
2795                  or else (In_Scope
2796                    and then Is_Entity_Name (Name))
2797                then
2798                   Set_Entity_With_Style_Check (Sel, Comp);
2799                   Generate_Reference (Comp, Sel);
2800
2801                else
2802                   goto Next_Comp;
2803                end if;
2804
2805                Set_Etype (Sel, Etype (Comp));
2806                Set_Etype (N,   Etype (Comp));
2807
2808                if Ekind (Comp) = E_Discriminant then
2809                   Set_Original_Discriminant (Sel, Comp);
2810                end if;
2811
2812                --  For access type case, introduce explicit deference for more
2813                --  uniform treatment of entry calls.
2814
2815                if Is_Access_Type (Etype (Name)) then
2816                   Insert_Explicit_Dereference (Name);
2817                   Error_Msg_NW
2818                     (Warn_On_Dereference, "?implicit dereference", N);
2819                end if;
2820             end if;
2821
2822             <<Next_Comp>>
2823                Next_Entity (Comp);
2824                exit when not In_Scope
2825                  and then
2826                    Comp = First_Private_Entity (Base_Type (Prefix_Type));
2827          end loop;
2828
2829          Set_Is_Overloaded (N, Is_Overloaded (Sel));
2830
2831       else
2832          --  Invalid prefix
2833
2834          Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
2835       end if;
2836
2837       --  If N still has no type, the component is not defined in the prefix
2838
2839       if Etype (N) = Any_Type then
2840
2841          --  If the prefix is a single concurrent object, use its name in the
2842          --  error message, rather than that of its anonymous type.
2843
2844          if Is_Concurrent_Type (Prefix_Type)
2845            and then Is_Internal_Name (Chars (Prefix_Type))
2846            and then not Is_Derived_Type (Prefix_Type)
2847            and then Is_Entity_Name (Name)
2848          then
2849
2850             Error_Msg_Node_2 := Entity (Name);
2851             Error_Msg_NE ("no selector& for&", N, Sel);
2852
2853             Check_Misspelled_Selector (Entity_List, Sel);
2854
2855          elsif Is_Generic_Type (Prefix_Type)
2856            and then Ekind (Prefix_Type) = E_Record_Type_With_Private
2857            and then Prefix_Type /= Etype (Prefix_Type)
2858            and then Is_Record_Type (Etype (Prefix_Type))
2859          then
2860             --  If this is a derived formal type, the parent may have
2861             --  different visibility at this point. Try for an inherited
2862             --  component before reporting an error.
2863
2864             Set_Etype (Prefix (N), Etype (Prefix_Type));
2865             Analyze_Selected_Component (N);
2866             return;
2867
2868          elsif Ekind (Prefix_Type) = E_Record_Subtype_With_Private
2869            and then Is_Generic_Actual_Type (Prefix_Type)
2870            and then Present (Full_View (Prefix_Type))
2871          then
2872             --  Similarly, if this the actual for a formal derived type, the
2873             --  component inherited from the generic parent may not be visible
2874             --  in the actual, but the selected component is legal.
2875
2876             declare
2877                Comp : Entity_Id;
2878
2879             begin
2880                Comp :=
2881                  First_Component (Generic_Parent_Type (Parent (Prefix_Type)));
2882                while Present (Comp) loop
2883                   if Chars (Comp) = Chars (Sel) then
2884                      Set_Entity_With_Style_Check (Sel, Comp);
2885                      Set_Etype (Sel, Etype (Comp));
2886                      Set_Etype (N,   Etype (Comp));
2887                      exit;
2888                   end if;
2889
2890                   Next_Component (Comp);
2891                end loop;
2892
2893                pragma Assert (Etype (N) /= Any_Type);
2894             end;
2895
2896          else
2897             if Ekind (Prefix_Type) = E_Record_Subtype then
2898
2899                --  Check whether this is a component of the base type
2900                --  which is absent from a statically constrained subtype.
2901                --  This will raise constraint error at run-time, but is
2902                --  not a compile-time error. When the selector is illegal
2903                --  for base type as well fall through and generate a
2904                --  compilation error anyway.
2905
2906                Comp := First_Component (Base_Type (Prefix_Type));
2907                while Present (Comp) loop
2908                   if Chars (Comp) = Chars (Sel)
2909                     and then Is_Visible_Component (Comp)
2910                   then
2911                      Set_Entity_With_Style_Check (Sel, Comp);
2912                      Generate_Reference (Comp, Sel);
2913                      Set_Etype (Sel, Etype (Comp));
2914                      Set_Etype (N,   Etype (Comp));
2915
2916                      --  Emit appropriate message. Gigi will replace the
2917                      --  node subsequently with the appropriate Raise.
2918
2919                      Apply_Compile_Time_Constraint_Error
2920                        (N, "component not present in }?",
2921                         CE_Discriminant_Check_Failed,
2922                         Ent => Prefix_Type, Rep => False);
2923                      Set_Raises_Constraint_Error (N);
2924                      return;
2925                   end if;
2926
2927                   Next_Component (Comp);
2928                end loop;
2929
2930             end if;
2931
2932             Error_Msg_Node_2 := First_Subtype (Prefix_Type);
2933             Error_Msg_NE ("no selector& for}", N, Sel);
2934
2935             Check_Misspelled_Selector (Entity_List, Sel);
2936
2937          end if;
2938
2939          Set_Entity (Sel, Any_Id);
2940          Set_Etype (Sel, Any_Type);
2941       end if;
2942    end Analyze_Selected_Component;
2943
2944    ---------------------------
2945    -- Analyze_Short_Circuit --
2946    ---------------------------
2947
2948    procedure Analyze_Short_Circuit (N : Node_Id) is
2949       L   : constant Node_Id := Left_Opnd  (N);
2950       R   : constant Node_Id := Right_Opnd (N);
2951       Ind : Interp_Index;
2952       It  : Interp;
2953
2954    begin
2955       Analyze_Expression (L);
2956       Analyze_Expression (R);
2957       Set_Etype (N, Any_Type);
2958
2959       if not Is_Overloaded (L) then
2960
2961          if Root_Type (Etype (L)) = Standard_Boolean
2962            and then Has_Compatible_Type (R, Etype (L))
2963          then
2964             Add_One_Interp (N, Etype (L), Etype (L));
2965          end if;
2966
2967       else
2968          Get_First_Interp (L, Ind, It);
2969
2970          while Present (It.Typ) loop
2971             if Root_Type (It.Typ) = Standard_Boolean
2972               and then Has_Compatible_Type (R, It.Typ)
2973             then
2974                Add_One_Interp (N, It.Typ, It.Typ);
2975             end if;
2976
2977             Get_Next_Interp (Ind, It);
2978          end loop;
2979       end if;
2980
2981       --  Here we have failed to find an interpretation. Clearly we
2982       --  know that it is not the case that both operands can have
2983       --  an interpretation of Boolean, but this is by far the most
2984       --  likely intended interpretation. So we simply resolve both
2985       --  operands as Booleans, and at least one of these resolutions
2986       --  will generate an error message, and we do not need to give
2987       --  a further error message on the short circuit operation itself.
2988
2989       if Etype (N) = Any_Type then
2990          Resolve (L, Standard_Boolean);
2991          Resolve (R, Standard_Boolean);
2992          Set_Etype (N, Standard_Boolean);
2993       end if;
2994    end Analyze_Short_Circuit;
2995
2996    -------------------
2997    -- Analyze_Slice --
2998    -------------------
2999
3000    procedure Analyze_Slice (N : Node_Id) is
3001       P          : constant Node_Id := Prefix (N);
3002       D          : constant Node_Id := Discrete_Range (N);
3003       Array_Type : Entity_Id;
3004
3005       procedure Analyze_Overloaded_Slice;
3006       --  If the prefix is overloaded, select those interpretations that
3007       --  yield a one-dimensional array type.
3008
3009       ------------------------------
3010       -- Analyze_Overloaded_Slice --
3011       ------------------------------
3012
3013       procedure Analyze_Overloaded_Slice is
3014          I   : Interp_Index;
3015          It  : Interp;
3016          Typ : Entity_Id;
3017
3018       begin
3019          Set_Etype (N, Any_Type);
3020
3021          Get_First_Interp (P, I, It);
3022          while Present (It.Nam) loop
3023             Typ := It.Typ;
3024
3025             if Is_Access_Type (Typ) then
3026                Typ := Designated_Type (Typ);
3027                Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3028             end if;
3029
3030             if Is_Array_Type (Typ)
3031               and then Number_Dimensions (Typ) = 1
3032               and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
3033             then
3034                Add_One_Interp (N, Typ, Typ);
3035             end if;
3036
3037             Get_Next_Interp (I, It);
3038          end loop;
3039
3040          if Etype (N) = Any_Type then
3041             Error_Msg_N ("expect array type in prefix of slice",  N);
3042          end if;
3043       end Analyze_Overloaded_Slice;
3044
3045    --  Start of processing for Analyze_Slice
3046
3047    begin
3048       Analyze (P);
3049       Analyze (D);
3050
3051       if Is_Overloaded (P) then
3052          Analyze_Overloaded_Slice;
3053
3054       else
3055          Array_Type := Etype (P);
3056          Set_Etype (N, Any_Type);
3057
3058          if Is_Access_Type (Array_Type) then
3059             Array_Type := Designated_Type (Array_Type);
3060             Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3061          end if;
3062
3063          if not Is_Array_Type (Array_Type) then
3064             Wrong_Type (P, Any_Array);
3065
3066          elsif Number_Dimensions (Array_Type) > 1 then
3067             Error_Msg_N
3068               ("type is not one-dimensional array in slice prefix", N);
3069
3070          elsif not
3071            Has_Compatible_Type (D, Etype (First_Index (Array_Type)))
3072          then
3073             Wrong_Type (D, Etype (First_Index (Array_Type)));
3074
3075          else
3076             Set_Etype (N, Array_Type);
3077          end if;
3078       end if;
3079    end Analyze_Slice;
3080
3081    -----------------------------
3082    -- Analyze_Type_Conversion --
3083    -----------------------------
3084
3085    procedure Analyze_Type_Conversion (N : Node_Id) is
3086       Expr : constant Node_Id := Expression (N);
3087       T    : Entity_Id;
3088
3089    begin
3090       --  If Conversion_OK is set, then the Etype is already set, and the
3091       --  only processing required is to analyze the expression. This is
3092       --  used to construct certain "illegal" conversions which are not
3093       --  allowed by Ada semantics, but can be handled OK by Gigi, see
3094       --  Sinfo for further details.
3095
3096       if Conversion_OK (N) then
3097          Analyze (Expr);
3098          return;
3099       end if;
3100
3101       --  Otherwise full type analysis is required, as well as some semantic
3102       --  checks to make sure the argument of the conversion is appropriate.
3103
3104       Find_Type (Subtype_Mark (N));
3105       T := Entity (Subtype_Mark (N));
3106       Set_Etype (N, T);
3107       Check_Fully_Declared (T, N);
3108       Analyze_Expression (Expr);
3109       Validate_Remote_Type_Type_Conversion (N);
3110
3111       --  Only remaining step is validity checks on the argument. These
3112       --  are skipped if the conversion does not come from the source.
3113
3114       if not Comes_From_Source (N) then
3115          return;
3116
3117       elsif Nkind (Expr) = N_Null then
3118          Error_Msg_N ("argument of conversion cannot be null", N);
3119          Error_Msg_N ("\use qualified expression instead", N);
3120          Set_Etype (N, Any_Type);
3121
3122       elsif Nkind (Expr) = N_Aggregate then
3123          Error_Msg_N ("argument of conversion cannot be aggregate", N);
3124          Error_Msg_N ("\use qualified expression instead", N);
3125
3126       elsif Nkind (Expr) = N_Allocator then
3127          Error_Msg_N ("argument of conversion cannot be an allocator", N);
3128          Error_Msg_N ("\use qualified expression instead", N);
3129
3130       elsif Nkind (Expr) = N_String_Literal then
3131          Error_Msg_N ("argument of conversion cannot be string literal", N);
3132          Error_Msg_N ("\use qualified expression instead", N);
3133
3134       elsif Nkind (Expr) = N_Character_Literal then
3135          if Ada_Version = Ada_83 then
3136             Resolve (Expr, T);
3137          else
3138             Error_Msg_N ("argument of conversion cannot be character literal",
3139               N);
3140             Error_Msg_N ("\use qualified expression instead", N);
3141          end if;
3142
3143       elsif Nkind (Expr) = N_Attribute_Reference
3144         and then
3145           (Attribute_Name (Expr) = Name_Access            or else
3146            Attribute_Name (Expr) = Name_Unchecked_Access  or else
3147            Attribute_Name (Expr) = Name_Unrestricted_Access)
3148       then
3149          Error_Msg_N ("argument of conversion cannot be access", N);
3150          Error_Msg_N ("\use qualified expression instead", N);
3151       end if;
3152    end Analyze_Type_Conversion;
3153
3154    ----------------------
3155    -- Analyze_Unary_Op --
3156    ----------------------
3157
3158    procedure Analyze_Unary_Op (N : Node_Id) is
3159       R     : constant Node_Id := Right_Opnd (N);
3160       Op_Id : Entity_Id := Entity (N);
3161
3162    begin
3163       Set_Etype (N, Any_Type);
3164       Candidate_Type := Empty;
3165
3166       Analyze_Expression (R);
3167
3168       if Present (Op_Id) then
3169          if Ekind (Op_Id) = E_Operator then
3170             Find_Unary_Types (R, Op_Id,  N);
3171          else
3172             Add_One_Interp (N, Op_Id, Etype (Op_Id));
3173          end if;
3174
3175       else
3176          Op_Id := Get_Name_Entity_Id (Chars (N));
3177          while Present (Op_Id) loop
3178             if Ekind (Op_Id) = E_Operator then
3179                if No (Next_Entity (First_Entity (Op_Id))) then
3180                   Find_Unary_Types (R, Op_Id,  N);
3181                end if;
3182
3183             elsif Is_Overloadable (Op_Id) then
3184                Analyze_User_Defined_Unary_Op (N, Op_Id);
3185             end if;
3186
3187             Op_Id := Homonym (Op_Id);
3188          end loop;
3189       end if;
3190
3191       Operator_Check (N);
3192    end Analyze_Unary_Op;
3193
3194    ----------------------------------
3195    -- Analyze_Unchecked_Expression --
3196    ----------------------------------
3197
3198    procedure Analyze_Unchecked_Expression (N : Node_Id) is
3199    begin
3200       Analyze (Expression (N), Suppress => All_Checks);
3201       Set_Etype (N, Etype (Expression (N)));
3202       Save_Interps (Expression (N), N);
3203    end Analyze_Unchecked_Expression;
3204
3205    ---------------------------------------
3206    -- Analyze_Unchecked_Type_Conversion --
3207    ---------------------------------------
3208
3209    procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
3210    begin
3211       Find_Type (Subtype_Mark (N));
3212       Analyze_Expression (Expression (N));
3213       Set_Etype (N, Entity (Subtype_Mark (N)));
3214    end Analyze_Unchecked_Type_Conversion;
3215
3216    ------------------------------------
3217    -- Analyze_User_Defined_Binary_Op --
3218    ------------------------------------
3219
3220    procedure Analyze_User_Defined_Binary_Op
3221      (N     : Node_Id;
3222       Op_Id : Entity_Id)
3223    is
3224    begin
3225       --  Only do analysis if the operator Comes_From_Source, since otherwise
3226       --  the operator was generated by the expander, and all such operators
3227       --  always refer to the operators in package Standard.
3228
3229       if Comes_From_Source (N) then
3230          declare
3231             F1 : constant Entity_Id := First_Formal (Op_Id);
3232             F2 : constant Entity_Id := Next_Formal (F1);
3233
3234          begin
3235             --  Verify that Op_Id is a visible binary function. Note that since
3236             --  we know Op_Id is overloaded, potentially use visible means use
3237             --  visible for sure (RM 9.4(11)).
3238
3239             if Ekind (Op_Id) = E_Function
3240               and then Present (F2)
3241               and then (Is_Immediately_Visible (Op_Id)
3242                          or else Is_Potentially_Use_Visible (Op_Id))
3243               and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
3244               and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
3245             then
3246                Add_One_Interp (N, Op_Id, Etype (Op_Id));
3247
3248                if Debug_Flag_E then
3249                   Write_Str ("user defined operator ");
3250                   Write_Name (Chars (Op_Id));
3251                   Write_Str (" on node ");
3252                   Write_Int (Int (N));
3253                   Write_Eol;
3254                end if;
3255             end if;
3256          end;
3257       end if;
3258    end Analyze_User_Defined_Binary_Op;
3259
3260    -----------------------------------
3261    -- Analyze_User_Defined_Unary_Op --
3262    -----------------------------------
3263
3264    procedure Analyze_User_Defined_Unary_Op
3265      (N     : Node_Id;
3266       Op_Id : Entity_Id)
3267    is
3268    begin
3269       --  Only do analysis if the operator Comes_From_Source, since otherwise
3270       --  the operator was generated by the expander, and all such operators
3271       --  always refer to the operators in package Standard.
3272
3273       if Comes_From_Source (N) then
3274          declare
3275             F : constant Entity_Id := First_Formal (Op_Id);
3276
3277          begin
3278             --  Verify that Op_Id is a visible unary function. Note that since
3279             --  we know Op_Id is overloaded, potentially use visible means use
3280             --  visible for sure (RM 9.4(11)).
3281
3282             if Ekind (Op_Id) = E_Function
3283               and then No (Next_Formal (F))
3284               and then (Is_Immediately_Visible (Op_Id)
3285                          or else Is_Potentially_Use_Visible (Op_Id))
3286               and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
3287             then
3288                Add_One_Interp (N, Op_Id, Etype (Op_Id));
3289             end if;
3290          end;
3291       end if;
3292    end Analyze_User_Defined_Unary_Op;
3293
3294    ---------------------------
3295    -- Check_Arithmetic_Pair --
3296    ---------------------------
3297
3298    procedure Check_Arithmetic_Pair
3299      (T1, T2 : Entity_Id;
3300       Op_Id  : Entity_Id;
3301       N      : Node_Id)
3302    is
3303       Op_Name : constant Name_Id   := Chars (Op_Id);
3304
3305       function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
3306       --  Check whether the fixed-point type Typ has a user-defined operator
3307       --  (multiplication or division) that should hide the corresponding
3308       --  predefined operator. Used to implement Ada 2005 AI-264, to make
3309       --  such operators more visible and therefore useful.
3310
3311       function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
3312       --  Get specific type (i.e. non-universal type if there is one)
3313
3314       ------------------
3315       -- Has_Fixed_Op --
3316       ------------------
3317
3318       function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
3319          Ent : Entity_Id;
3320          F1  : Entity_Id;
3321          F2  : Entity_Id;
3322
3323       begin
3324          --  The operation is treated as primitive if it is declared in the
3325          --  same scope as the type, and therefore on the same entity chain.
3326
3327          Ent := Next_Entity (Typ);
3328          while Present (Ent) loop
3329             if Chars (Ent) = Chars (Op) then
3330                F1 := First_Formal (Ent);
3331                F2 := Next_Formal (F1);
3332
3333                --  The operation counts as primitive if either operand or
3334                --  result are of the given type, and both operands are fixed
3335                --  point types.
3336
3337                if (Etype (F1) = Typ
3338                     and then Is_Fixed_Point_Type (Etype (F2)))
3339
3340                  or else
3341                    (Etype (F2) = Typ
3342                      and then Is_Fixed_Point_Type (Etype (F1)))
3343
3344                  or else
3345                    (Etype (Ent) = Typ
3346                      and then Is_Fixed_Point_Type (Etype (F1))
3347                      and then Is_Fixed_Point_Type (Etype (F2)))
3348                then
3349                   return True;
3350                end if;
3351             end if;
3352
3353             Next_Entity (Ent);
3354          end loop;
3355
3356          return False;
3357       end Has_Fixed_Op;
3358
3359       -------------------
3360       -- Specific_Type --
3361       -------------------
3362
3363       function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
3364       begin
3365          if T1 = Universal_Integer or else T1 = Universal_Real then
3366             return Base_Type (T2);
3367          else
3368             return Base_Type (T1);
3369          end if;
3370       end Specific_Type;
3371
3372    --  Start of processing for Check_Arithmetic_Pair
3373
3374    begin
3375       if Op_Name = Name_Op_Add or else Op_Name = Name_Op_Subtract then
3376
3377          if Is_Numeric_Type (T1)
3378            and then Is_Numeric_Type (T2)
3379            and then (Covers (T1, T2) or else Covers (T2, T1))
3380          then
3381             Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
3382          end if;
3383
3384       elsif Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide then
3385
3386          if Is_Fixed_Point_Type (T1)
3387            and then (Is_Fixed_Point_Type (T2)
3388                        or else T2 = Universal_Real)
3389          then
3390             --  If Treat_Fixed_As_Integer is set then the Etype is already set
3391             --  and no further processing is required (this is the case of an
3392             --  operator constructed by Exp_Fixd for a fixed point operation)
3393             --  Otherwise add one interpretation with universal fixed result
3394             --  If the operator is given in  functional notation, it comes
3395             --  from source and Fixed_As_Integer cannot apply.
3396
3397             if (Nkind (N) not in N_Op
3398                  or else not Treat_Fixed_As_Integer (N))
3399               and then
3400                 (not (Ada_Version >= Ada_05 and then Has_Fixed_Op (T1, Op_Id))
3401                   or else Nkind (Parent (N)) = N_Type_Conversion)
3402             then
3403                Add_One_Interp (N, Op_Id, Universal_Fixed);
3404             end if;
3405
3406          elsif Is_Fixed_Point_Type (T2)
3407            and then (Nkind (N) not in N_Op
3408                       or else not Treat_Fixed_As_Integer (N))
3409            and then T1 = Universal_Real
3410            and then
3411              (not (Ada_Version >= Ada_05 and then Has_Fixed_Op (T1, Op_Id))
3412                or else Nkind (Parent (N)) = N_Type_Conversion)
3413          then
3414             Add_One_Interp (N, Op_Id, Universal_Fixed);
3415
3416          elsif Is_Numeric_Type (T1)
3417            and then Is_Numeric_Type (T2)
3418            and then (Covers (T1, T2) or else Covers (T2, T1))
3419          then
3420             Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
3421
3422          elsif Is_Fixed_Point_Type (T1)
3423            and then (Base_Type (T2) = Base_Type (Standard_Integer)
3424                        or else T2 = Universal_Integer)
3425          then
3426             Add_One_Interp (N, Op_Id, T1);
3427
3428          elsif T2 = Universal_Real
3429            and then Base_Type (T1) = Base_Type (Standard_Integer)
3430            and then Op_Name = Name_Op_Multiply
3431          then
3432             Add_One_Interp (N, Op_Id, Any_Fixed);
3433
3434          elsif T1 = Universal_Real
3435            and then Base_Type (T2) = Base_Type (Standard_Integer)
3436          then
3437             Add_One_Interp (N, Op_Id, Any_Fixed);
3438
3439          elsif Is_Fixed_Point_Type (T2)
3440            and then (Base_Type (T1) = Base_Type (Standard_Integer)
3441                        or else T1 = Universal_Integer)
3442            and then Op_Name = Name_Op_Multiply
3443          then
3444             Add_One_Interp (N, Op_Id, T2);
3445
3446          elsif T1 = Universal_Real and then T2 = Universal_Integer then
3447             Add_One_Interp (N, Op_Id, T1);
3448
3449          elsif T2 = Universal_Real
3450            and then T1 = Universal_Integer
3451            and then Op_Name = Name_Op_Multiply
3452          then
3453             Add_One_Interp (N, Op_Id, T2);
3454          end if;
3455
3456       elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
3457
3458          --  Note: The fixed-point operands case with Treat_Fixed_As_Integer
3459          --  set does not require any special processing, since the Etype is
3460          --  already set (case of operation constructed by Exp_Fixed).
3461
3462          if Is_Integer_Type (T1)
3463            and then (Covers (T1, T2) or else Covers (T2, T1))
3464          then
3465             Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
3466          end if;
3467
3468       elsif Op_Name = Name_Op_Expon then
3469          if Is_Numeric_Type (T1)
3470            and then not Is_Fixed_Point_Type (T1)
3471            and then (Base_Type (T2) = Base_Type (Standard_Integer)
3472                       or else T2 = Universal_Integer)
3473          then
3474             Add_One_Interp (N, Op_Id, Base_Type (T1));
3475          end if;
3476
3477       else pragma Assert (Nkind (N) in N_Op_Shift);
3478
3479          --  If not one of the predefined operators, the node may be one
3480          --  of the intrinsic functions. Its kind is always specific, and
3481          --  we can use it directly, rather than the name of the operation.
3482
3483          if Is_Integer_Type (T1)
3484            and then (Base_Type (T2) = Base_Type (Standard_Integer)
3485                       or else T2 = Universal_Integer)
3486          then
3487             Add_One_Interp (N, Op_Id, Base_Type (T1));
3488          end if;
3489       end if;
3490    end Check_Arithmetic_Pair;
3491
3492    -------------------------------
3493    -- Check_Misspelled_Selector --
3494    -------------------------------
3495
3496    procedure Check_Misspelled_Selector
3497      (Prefix : Entity_Id;
3498       Sel    : Node_Id)
3499    is
3500       Max_Suggestions   : constant := 2;
3501       Nr_Of_Suggestions : Natural := 0;
3502
3503       Suggestion_1 : Entity_Id := Empty;
3504       Suggestion_2 : Entity_Id := Empty;
3505
3506       Comp : Entity_Id;
3507
3508    begin
3509       --  All the components of the prefix of selector Sel are matched
3510       --  against  Sel and a count is maintained of possible misspellings.
3511       --  When at the end of the analysis there are one or two (not more!)
3512       --  possible misspellings, these misspellings will be suggested as
3513       --  possible correction.
3514
3515       if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
3516
3517          --  Concurrent types should be handled as well ???
3518
3519          return;
3520       end if;
3521
3522       Get_Name_String (Chars (Sel));
3523
3524       declare
3525          S  : constant String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
3526
3527       begin
3528          Comp  := First_Entity (Prefix);
3529          while Nr_Of_Suggestions <= Max_Suggestions
3530             and then Present (Comp)
3531          loop
3532             if Is_Visible_Component (Comp) then
3533                Get_Name_String (Chars (Comp));
3534
3535                if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
3536                   Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
3537
3538                   case Nr_Of_Suggestions is
3539                      when 1      => Suggestion_1 := Comp;
3540                      when 2      => Suggestion_2 := Comp;
3541                      when others => exit;
3542                   end case;
3543                end if;
3544             end if;
3545
3546             Comp := Next_Entity (Comp);
3547          end loop;
3548
3549          --  Report at most two suggestions
3550
3551          if Nr_Of_Suggestions = 1 then
3552             Error_Msg_NE ("\possible misspelling of&", Sel, Suggestion_1);
3553
3554          elsif Nr_Of_Suggestions = 2 then
3555             Error_Msg_Node_2 := Suggestion_2;
3556             Error_Msg_NE ("\possible misspelling of& or&",
3557               Sel, Suggestion_1);
3558          end if;
3559       end;
3560    end Check_Misspelled_Selector;
3561
3562    ----------------------
3563    -- Defined_In_Scope --
3564    ----------------------
3565
3566    function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
3567    is
3568       S1 : constant Entity_Id := Scope (Base_Type (T));
3569    begin
3570       return S1 = S
3571         or else (S1 = System_Aux_Id and then S = Scope (S1));
3572    end Defined_In_Scope;
3573
3574    -------------------
3575    -- Diagnose_Call --
3576    -------------------
3577
3578    procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
3579       Actual           : Node_Id;
3580       X                : Interp_Index;
3581       It               : Interp;
3582       Success          : Boolean;
3583       Err_Mode         : Boolean;
3584       New_Nam          : Node_Id;
3585       Void_Interp_Seen : Boolean := False;
3586
3587    begin
3588       if Ada_Version >= Ada_05 then
3589          Actual := First_Actual (N);
3590          while Present (Actual) loop
3591
3592             --  Ada 2005 (AI-50217): Post an error in case of premature
3593             --  usage of an entity from the limited view.
3594
3595             if not Analyzed (Etype (Actual))
3596              and then From_With_Type (Etype (Actual))
3597             then
3598                Error_Msg_Qual_Level := 1;
3599                Error_Msg_NE
3600                 ("missing with_clause for scope of imported type&",
3601                   Actual, Etype (Actual));
3602                Error_Msg_Qual_Level := 0;
3603             end if;
3604
3605             Next_Actual (Actual);
3606          end loop;
3607       end if;
3608
3609       --   Analyze each candidate call again, with full error reporting
3610       --   for each.
3611
3612       Error_Msg_N
3613         ("no candidate interpretations match the actuals:!", Nam);
3614       Err_Mode := All_Errors_Mode;
3615       All_Errors_Mode := True;
3616
3617       --  If this is a call to an operation of a concurrent type,
3618       --  the failed interpretations have been removed from the
3619       --  name. Recover them to provide full diagnostics.
3620
3621       if Nkind (Parent (Nam)) = N_Selected_Component then
3622          Set_Entity (Nam, Empty);
3623          New_Nam := New_Copy_Tree (Parent (Nam));
3624          Set_Is_Overloaded (New_Nam, False);
3625          Set_Is_Overloaded (Selector_Name (New_Nam), False);
3626          Set_Parent (New_Nam, Parent (Parent (Nam)));
3627          Analyze_Selected_Component (New_Nam);
3628          Get_First_Interp (Selector_Name (New_Nam), X, It);
3629       else
3630          Get_First_Interp (Nam, X, It);
3631       end if;
3632
3633       while Present (It.Nam) loop
3634          if Etype (It.Nam) = Standard_Void_Type then
3635             Void_Interp_Seen := True;
3636          end if;
3637
3638          Analyze_One_Call (N, It.Nam, True, Success);
3639          Get_Next_Interp (X, It);
3640       end loop;
3641
3642       if Nkind (N) = N_Function_Call then
3643          Get_First_Interp (Nam, X, It);
3644          while Present (It.Nam) loop
3645             if Ekind (It.Nam) = E_Function
3646               or else Ekind (It.Nam) = E_Operator
3647             then
3648                return;
3649             else
3650                Get_Next_Interp (X, It);
3651             end if;
3652          end loop;
3653
3654          --  If all interpretations are procedures, this deserves a
3655          --  more precise message. Ditto if this appears as the prefix
3656          --  of a selected component, which may be a lexical error.
3657
3658          Error_Msg_N
3659            ("\context requires function call, found procedure name", Nam);
3660
3661          if Nkind (Parent (N)) = N_Selected_Component
3662            and then N = Prefix (Parent (N))
3663          then
3664             Error_Msg_N (
3665               "\period should probably be semicolon", Parent (N));
3666          end if;
3667
3668       elsif Nkind (N) = N_Procedure_Call_Statement
3669         and then not Void_Interp_Seen
3670       then
3671          Error_Msg_N (
3672          "\function name found in procedure call", Nam);
3673       end if;
3674
3675       All_Errors_Mode := Err_Mode;
3676    end Diagnose_Call;
3677
3678    ---------------------------
3679    -- Find_Arithmetic_Types --
3680    ---------------------------
3681
3682    procedure Find_Arithmetic_Types
3683      (L, R  : Node_Id;
3684       Op_Id : Entity_Id;
3685       N     : Node_Id)
3686    is
3687       Index1 : Interp_Index;
3688       Index2 : Interp_Index;
3689       It1    : Interp;
3690       It2    : Interp;
3691
3692       procedure Check_Right_Argument (T : Entity_Id);
3693       --  Check right operand of operator
3694
3695       --------------------------
3696       -- Check_Right_Argument --
3697       --------------------------
3698
3699       procedure Check_Right_Argument (T : Entity_Id) is
3700       begin
3701          if not Is_Overloaded (R) then
3702             Check_Arithmetic_Pair (T, Etype (R), Op_Id,  N);
3703          else
3704             Get_First_Interp (R, Index2, It2);
3705             while Present (It2.Typ) loop
3706                Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
3707                Get_Next_Interp (Index2, It2);
3708             end loop;
3709          end if;
3710       end Check_Right_Argument;
3711
3712    --  Start processing for Find_Arithmetic_Types
3713
3714    begin
3715       if not Is_Overloaded (L) then
3716          Check_Right_Argument (Etype (L));
3717
3718       else
3719          Get_First_Interp (L, Index1, It1);
3720
3721          while Present (It1.Typ) loop
3722             Check_Right_Argument (It1.Typ);
3723             Get_Next_Interp (Index1, It1);
3724          end loop;
3725       end if;
3726
3727    end Find_Arithmetic_Types;
3728
3729    ------------------------
3730    -- Find_Boolean_Types --
3731    ------------------------
3732
3733    procedure Find_Boolean_Types
3734      (L, R  : Node_Id;
3735       Op_Id : Entity_Id;
3736       N     : Node_Id)
3737    is
3738       Index : Interp_Index;
3739       It    : Interp;
3740
3741       procedure Check_Numeric_Argument (T : Entity_Id);
3742       --  Special case for logical operations one of whose operands is an
3743       --  integer literal. If both are literal the result is any modular type.
3744
3745       ----------------------------
3746       -- Check_Numeric_Argument --
3747       ----------------------------
3748
3749       procedure Check_Numeric_Argument (T : Entity_Id) is
3750       begin
3751          if T = Universal_Integer then
3752             Add_One_Interp (N, Op_Id, Any_Modular);
3753
3754          elsif Is_Modular_Integer_Type (T) then
3755             Add_One_Interp (N, Op_Id, T);
3756          end if;
3757       end Check_Numeric_Argument;
3758
3759    --  Start of processing for Find_Boolean_Types
3760
3761    begin
3762       if not Is_Overloaded (L) then
3763          if Etype (L) = Universal_Integer
3764            or else Etype (L) = Any_Modular
3765          then
3766             if not Is_Overloaded (R) then
3767                Check_Numeric_Argument (Etype (R));
3768
3769             else
3770                Get_First_Interp (R, Index, It);
3771                while Present (It.Typ) loop
3772                   Check_Numeric_Argument (It.Typ);
3773                   Get_Next_Interp (Index, It);
3774                end loop;
3775             end if;
3776
3777          elsif Valid_Boolean_Arg (Etype (L))
3778            and then Has_Compatible_Type (R, Etype (L))
3779          then
3780             Add_One_Interp (N, Op_Id, Etype (L));
3781          end if;
3782
3783       else
3784          Get_First_Interp (L, Index, It);
3785          while Present (It.Typ) loop
3786             if Valid_Boolean_Arg (It.Typ)
3787               and then Has_Compatible_Type (R, It.Typ)
3788             then
3789                Add_One_Interp (N, Op_Id, It.Typ);
3790             end if;
3791
3792             Get_Next_Interp (Index, It);
3793          end loop;
3794       end if;
3795    end Find_Boolean_Types;
3796
3797    ---------------------------
3798    -- Find_Comparison_Types --
3799    ---------------------------
3800
3801    procedure Find_Comparison_Types
3802      (L, R  : Node_Id;
3803       Op_Id : Entity_Id;
3804       N     : Node_Id)
3805    is
3806       Index : Interp_Index;
3807       It    : Interp;
3808       Found : Boolean := False;
3809       I_F   : Interp_Index;
3810       T_F   : Entity_Id;
3811       Scop  : Entity_Id := Empty;
3812
3813       procedure Try_One_Interp (T1 : Entity_Id);
3814       --  Routine to try one proposed interpretation. Note that the context
3815       --  of the operator plays no role in resolving the arguments, so that
3816       --  if there is more than one interpretation of the operands that is
3817       --  compatible with comparison, the operation is ambiguous.
3818
3819       --------------------
3820       -- Try_One_Interp --
3821       --------------------
3822
3823       procedure Try_One_Interp (T1 : Entity_Id) is
3824       begin
3825
3826          --  If the operator is an expanded name, then the type of the operand
3827          --  must be defined in the corresponding scope. If the type is
3828          --  universal, the context will impose the correct type.
3829
3830          if Present (Scop)
3831             and then not Defined_In_Scope (T1, Scop)
3832             and then T1 /= Universal_Integer
3833             and then T1 /= Universal_Real
3834             and then T1 /= Any_String
3835             and then T1 /= Any_Composite
3836          then
3837             return;
3838          end if;
3839
3840          if Valid_Comparison_Arg (T1)
3841            and then Has_Compatible_Type (R, T1)
3842          then
3843             if Found
3844               and then Base_Type (T1) /= Base_Type (T_F)
3845             then
3846                It := Disambiguate (L, I_F, Index, Any_Type);
3847
3848                if It = No_Interp then
3849                   Ambiguous_Operands (N);
3850                   Set_Etype (L, Any_Type);
3851                   return;
3852
3853                else
3854                   T_F := It.Typ;
3855                end if;
3856
3857             else
3858                Found := True;
3859                T_F   := T1;
3860                I_F   := Index;
3861             end if;
3862
3863             Set_Etype (L, T_F);
3864             Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
3865
3866          end if;
3867       end Try_One_Interp;
3868
3869    --  Start processing for Find_Comparison_Types
3870
3871    begin
3872       --  If left operand is aggregate, the right operand has to
3873       --  provide a usable type for it.
3874
3875       if Nkind (L) = N_Aggregate
3876         and then Nkind (R) /= N_Aggregate
3877       then
3878          Find_Comparison_Types (R, L, Op_Id, N);
3879          return;
3880       end if;
3881
3882       if Nkind (N) = N_Function_Call
3883          and then Nkind (Name (N)) = N_Expanded_Name
3884       then
3885          Scop := Entity (Prefix (Name (N)));
3886
3887          --  The prefix may be a package renaming, and the subsequent test
3888          --  requires the original package.
3889
3890          if Ekind (Scop) = E_Package
3891            and then Present (Renamed_Entity (Scop))
3892          then
3893             Scop := Renamed_Entity (Scop);
3894             Set_Entity (Prefix (Name (N)), Scop);
3895          end if;
3896       end if;
3897
3898       if not Is_Overloaded (L) then
3899          Try_One_Interp (Etype (L));
3900
3901       else
3902          Get_First_Interp (L, Index, It);
3903          while Present (It.Typ) loop
3904             Try_One_Interp (It.Typ);
3905             Get_Next_Interp (Index, It);
3906          end loop;
3907       end if;
3908    end Find_Comparison_Types;
3909
3910    ----------------------------------------
3911    -- Find_Non_Universal_Interpretations --
3912    ----------------------------------------
3913
3914    procedure Find_Non_Universal_Interpretations
3915      (N     : Node_Id;
3916       R     : Node_Id;
3917       Op_Id : Entity_Id;
3918       T1    : Entity_Id)
3919    is
3920       Index : Interp_Index;
3921       It    : Interp;
3922
3923    begin
3924       if T1 = Universal_Integer
3925         or else T1 = Universal_Real
3926       then
3927          if not Is_Overloaded (R) then
3928             Add_One_Interp
3929               (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
3930          else
3931             Get_First_Interp (R, Index, It);
3932             while Present (It.Typ) loop
3933                if Covers (It.Typ, T1) then
3934                   Add_One_Interp
3935                     (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
3936                end if;
3937
3938                Get_Next_Interp (Index, It);
3939             end loop;
3940          end if;
3941       else
3942          Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
3943       end if;
3944    end Find_Non_Universal_Interpretations;
3945
3946    ------------------------------
3947    -- Find_Concatenation_Types --
3948    ------------------------------
3949
3950    procedure Find_Concatenation_Types
3951      (L, R  : Node_Id;
3952       Op_Id : Entity_Id;
3953       N     : Node_Id)
3954    is
3955       Op_Type : constant Entity_Id := Etype (Op_Id);
3956
3957    begin
3958       if Is_Array_Type (Op_Type)
3959         and then not Is_Limited_Type (Op_Type)
3960
3961         and then (Has_Compatible_Type (L, Op_Type)
3962                     or else
3963                   Has_Compatible_Type (L, Component_Type (Op_Type)))
3964
3965         and then (Has_Compatible_Type (R, Op_Type)
3966                     or else
3967                   Has_Compatible_Type (R, Component_Type (Op_Type)))
3968       then
3969          Add_One_Interp (N, Op_Id, Op_Type);
3970       end if;
3971    end Find_Concatenation_Types;
3972
3973    -------------------------
3974    -- Find_Equality_Types --
3975    -------------------------
3976
3977    procedure Find_Equality_Types
3978      (L, R  : Node_Id;
3979       Op_Id : Entity_Id;
3980       N     : Node_Id)
3981    is
3982       Index : Interp_Index;
3983       It    : Interp;
3984       Found : Boolean := False;
3985       I_F   : Interp_Index;
3986       T_F   : Entity_Id;
3987       Scop  : Entity_Id := Empty;
3988
3989       procedure Try_One_Interp (T1 : Entity_Id);
3990       --  The context of the operator plays no role in resolving the
3991       --  arguments,  so that if there is more than one interpretation
3992       --  of the operands that is compatible with equality, the construct
3993       --  is ambiguous and an error can be emitted now, after trying to
3994       --  disambiguate, i.e. applying preference rules.
3995
3996       --------------------
3997       -- Try_One_Interp --
3998       --------------------
3999
4000       procedure Try_One_Interp (T1 : Entity_Id) is
4001       begin
4002          --  If the operator is an expanded name, then the type of the operand
4003          --  must be defined in the corresponding scope. If the type is
4004          --  universal, the context will impose the correct type. An anonymous
4005          --  type for a 'Access reference is also universal in this sense, as
4006          --  the actual type is obtained from context.
4007
4008          if Present (Scop)
4009             and then not Defined_In_Scope (T1, Scop)
4010             and then T1 /= Universal_Integer
4011             and then T1 /= Universal_Real
4012             and then T1 /= Any_Access
4013             and then T1 /= Any_String
4014             and then T1 /= Any_Composite
4015             and then (Ekind (T1) /= E_Access_Subprogram_Type
4016                         or else Comes_From_Source (T1))
4017          then
4018             return;
4019          end if;
4020
4021          --  Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
4022          --  Do not allow anonymous access types in equality operators.
4023
4024          if Ada_Version < Ada_05
4025            and then Ekind (T1) = E_Anonymous_Access_Type
4026          then
4027             return;
4028          end if;
4029
4030          if T1 /= Standard_Void_Type
4031            and then not Is_Limited_Type (T1)
4032            and then not Is_Limited_Composite (T1)
4033            and then Has_Compatible_Type (R, T1)
4034          then
4035             if Found
4036               and then Base_Type (T1) /= Base_Type (T_F)
4037             then
4038                It := Disambiguate (L, I_F, Index, Any_Type);
4039
4040                if It = No_Interp then
4041                   Ambiguous_Operands (N);
4042                   Set_Etype (L, Any_Type);
4043                   return;
4044
4045                else
4046                   T_F := It.Typ;
4047                end if;
4048
4049             else
4050                Found := True;
4051                T_F   := T1;
4052                I_F   := Index;
4053             end if;
4054
4055             if not Analyzed (L) then
4056                Set_Etype (L, T_F);
4057             end if;
4058
4059             Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
4060
4061             --  Case of operator was not visible, Etype still set to Any_Type
4062
4063             if Etype (N) = Any_Type then
4064                Found := False;
4065             end if;
4066          end if;
4067       end Try_One_Interp;
4068
4069    --  Start of processing for Find_Equality_Types
4070
4071    begin
4072       --  If left operand is aggregate, the right operand has to
4073       --  provide a usable type for it.
4074
4075       if Nkind (L) = N_Aggregate
4076         and then Nkind (R) /= N_Aggregate
4077       then
4078          Find_Equality_Types (R, L, Op_Id, N);
4079          return;
4080       end if;
4081
4082       if Nkind (N) = N_Function_Call
4083          and then Nkind (Name (N)) = N_Expanded_Name
4084       then
4085          Scop := Entity (Prefix (Name (N)));
4086
4087          --  The prefix may be a package renaming, and the subsequent test
4088          --  requires the original package.
4089
4090          if Ekind (Scop) = E_Package
4091            and then Present (Renamed_Entity (Scop))
4092          then
4093             Scop := Renamed_Entity (Scop);
4094             Set_Entity (Prefix (Name (N)), Scop);
4095          end if;
4096       end if;
4097
4098       if not Is_Overloaded (L) then
4099          Try_One_Interp (Etype (L));
4100
4101       else
4102          Get_First_Interp (L, Index, It);
4103          while Present (It.Typ) loop
4104             Try_One_Interp (It.Typ);
4105             Get_Next_Interp (Index, It);
4106          end loop;
4107       end if;
4108    end Find_Equality_Types;
4109
4110    -------------------------
4111    -- Find_Negation_Types --
4112    -------------------------
4113
4114    procedure Find_Negation_Types
4115      (R     : Node_Id;
4116       Op_Id : Entity_Id;
4117       N     : Node_Id)
4118    is
4119       Index : Interp_Index;
4120       It    : Interp;
4121
4122    begin
4123       if not Is_Overloaded (R) then
4124          if Etype (R) = Universal_Integer then
4125             Add_One_Interp (N, Op_Id, Any_Modular);
4126          elsif Valid_Boolean_Arg (Etype (R)) then
4127             Add_One_Interp (N, Op_Id, Etype (R));
4128          end if;
4129
4130       else
4131          Get_First_Interp (R, Index, It);
4132          while Present (It.Typ) loop
4133             if Valid_Boolean_Arg (It.Typ) then
4134                Add_One_Interp (N, Op_Id, It.Typ);
4135             end if;
4136
4137             Get_Next_Interp (Index, It);
4138          end loop;
4139       end if;
4140    end Find_Negation_Types;
4141
4142    ----------------------
4143    -- Find_Unary_Types --
4144    ----------------------
4145
4146    procedure Find_Unary_Types
4147      (R     : Node_Id;
4148       Op_Id : Entity_Id;
4149       N     : Node_Id)
4150    is
4151       Index : Interp_Index;
4152       It    : Interp;
4153
4154    begin
4155       if not Is_Overloaded (R) then
4156          if Is_Numeric_Type (Etype (R)) then
4157             Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
4158          end if;
4159
4160       else
4161          Get_First_Interp (R, Index, It);
4162          while Present (It.Typ) loop
4163             if Is_Numeric_Type (It.Typ) then
4164                Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
4165             end if;
4166
4167             Get_Next_Interp (Index, It);
4168          end loop;
4169       end if;
4170    end Find_Unary_Types;
4171
4172    ------------------
4173    -- Junk_Operand --
4174    ------------------
4175
4176    function Junk_Operand (N : Node_Id) return Boolean is
4177       Enode : Node_Id;
4178
4179    begin
4180       if Error_Posted (N) then
4181          return False;
4182       end if;
4183
4184       --  Get entity to be tested
4185
4186       if Is_Entity_Name (N)
4187         and then Present (Entity (N))
4188       then
4189          Enode := N;
4190
4191       --  An odd case, a procedure name gets converted to a very peculiar
4192       --  function call, and here is where we detect this happening.
4193
4194       elsif Nkind (N) = N_Function_Call
4195         and then Is_Entity_Name (Name (N))
4196         and then Present (Entity (Name (N)))
4197       then
4198          Enode := Name (N);
4199
4200       --  Another odd case, there are at least some cases of selected
4201       --  components where the selected component is not marked as having
4202       --  an entity, even though the selector does have an entity
4203
4204       elsif Nkind (N) = N_Selected_Component
4205         and then Present (Entity (Selector_Name (N)))
4206       then
4207          Enode := Selector_Name (N);
4208
4209       else
4210          return False;
4211       end if;
4212
4213       --  Now test the entity we got to see if it a bad case
4214
4215       case Ekind (Entity (Enode)) is
4216
4217          when E_Package =>
4218             Error_Msg_N
4219               ("package name cannot be used as operand", Enode);
4220
4221          when Generic_Unit_Kind =>
4222             Error_Msg_N
4223               ("generic unit name cannot be used as operand", Enode);
4224
4225          when Type_Kind =>
4226             Error_Msg_N
4227               ("subtype name cannot be used as operand", Enode);
4228
4229          when Entry_Kind =>
4230             Error_Msg_N
4231               ("entry name cannot be used as operand", Enode);
4232
4233          when E_Procedure =>
4234             Error_Msg_N
4235               ("procedure name cannot be used as operand", Enode);
4236
4237          when E_Exception =>
4238             Error_Msg_N
4239               ("exception name cannot be used as operand", Enode);
4240
4241          when E_Block | E_Label | E_Loop =>
4242             Error_Msg_N
4243               ("label name cannot be used as operand", Enode);
4244
4245          when others =>
4246             return False;
4247
4248       end case;
4249
4250       return True;
4251    end Junk_Operand;
4252
4253    --------------------
4254    -- Operator_Check --
4255    --------------------
4256
4257    procedure Operator_Check (N : Node_Id) is
4258    begin
4259       Remove_Abstract_Operations (N);
4260
4261       --  Test for case of no interpretation found for operator
4262
4263       if Etype (N) = Any_Type then
4264          declare
4265             L : Node_Id;
4266             R : Node_Id;
4267
4268          begin
4269             R := Right_Opnd (N);
4270
4271             if Nkind (N) in N_Binary_Op then
4272                L := Left_Opnd (N);
4273             else
4274                L := Empty;
4275             end if;
4276
4277             --  If either operand has no type, then don't complain further,
4278             --  since this simply means that we have a propragated error.
4279
4280             if R = Error
4281               or else Etype (R) = Any_Type
4282               or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
4283             then
4284                return;
4285
4286             --  We explicitly check for the case of concatenation of component
4287             --  with component to avoid reporting spurious matching array types
4288             --  that might happen to be lurking in distant packages (such as
4289             --  run-time packages). This also prevents inconsistencies in the
4290             --  messages for certain ACVC B tests, which can vary depending on
4291             --  types declared in run-time interfaces. Another improvement when
4292             --  aggregates are present is to look for a well-typed operand.
4293
4294             elsif Present (Candidate_Type)
4295               and then (Nkind (N) /= N_Op_Concat
4296                          or else Is_Array_Type (Etype (L))
4297                          or else Is_Array_Type (Etype (R)))
4298             then
4299
4300                if Nkind (N) = N_Op_Concat then
4301                   if Etype (L) /= Any_Composite
4302                     and then Is_Array_Type (Etype (L))
4303                   then
4304                      Candidate_Type := Etype (L);
4305
4306                   elsif Etype (R) /= Any_Composite
4307                     and then Is_Array_Type (Etype (R))
4308                   then
4309                      Candidate_Type := Etype (R);
4310                   end if;
4311                end if;
4312
4313                Error_Msg_NE
4314                  ("operator for} is not directly visible!",
4315                   N, First_Subtype (Candidate_Type));
4316                Error_Msg_N ("use clause would make operation legal!",  N);
4317                return;
4318
4319             --  If either operand is a junk operand (e.g. package name), then
4320             --  post appropriate error messages, but do not complain further.
4321
4322             --  Note that the use of OR in this test instead of OR ELSE
4323             --  is quite deliberate, we may as well check both operands
4324             --  in the binary operator case.
4325
4326             elsif Junk_Operand (R)
4327               or (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
4328             then
4329                return;
4330
4331             --  If we have a logical operator, one of whose operands is
4332             --  Boolean, then we know that the other operand cannot resolve
4333             --  to Boolean (since we got no interpretations), but in that
4334             --  case we pretty much know that the other operand should be
4335             --  Boolean, so resolve it that way (generating an error)
4336
4337             elsif Nkind (N) = N_Op_And
4338                     or else
4339                   Nkind (N) = N_Op_Or
4340                     or else
4341                   Nkind (N) = N_Op_Xor
4342             then
4343                if Etype (L) = Standard_Boolean then
4344                   Resolve (R, Standard_Boolean);
4345                   return;
4346                elsif Etype (R) = Standard_Boolean then
4347                   Resolve (L, Standard_Boolean);
4348                   return;
4349                end if;
4350
4351             --  For an arithmetic operator or comparison operator, if one
4352             --  of the operands is numeric, then we know the other operand
4353             --  is not the same numeric type. If it is a non-numeric type,
4354             --  then probably it is intended to match the other operand.
4355
4356             elsif Nkind (N) = N_Op_Add      or else
4357                   Nkind (N) = N_Op_Divide   or else
4358                   Nkind (N) = N_Op_Ge       or else
4359                   Nkind (N) = N_Op_Gt       or else
4360                   Nkind (N) = N_Op_Le       or else
4361                   Nkind (N) = N_Op_Lt       or else
4362                   Nkind (N) = N_Op_Mod      or else
4363                   Nkind (N) = N_Op_Multiply or else
4364                   Nkind (N) = N_Op_Rem      or else
4365                   Nkind (N) = N_Op_Subtract
4366             then
4367                if Is_Numeric_Type (Etype (L))
4368                  and then not Is_Numeric_Type (Etype (R))
4369                then
4370                   Resolve (R, Etype (L));
4371                   return;
4372
4373                elsif Is_Numeric_Type (Etype (R))
4374                  and then not Is_Numeric_Type (Etype (L))
4375                then
4376                   Resolve (L, Etype (R));
4377                   return;
4378                end if;
4379
4380             --  Comparisons on A'Access are common enough to deserve a
4381             --  special message.
4382
4383             elsif (Nkind (N) = N_Op_Eq  or else
4384                    Nkind (N) = N_Op_Ne)
4385                and then Ekind (Etype (L)) = E_Access_Attribute_Type
4386                and then Ekind (Etype (R)) = E_Access_Attribute_Type
4387             then
4388                Error_Msg_N
4389                  ("two access attributes cannot be compared directly", N);
4390                Error_Msg_N
4391                  ("\they must be converted to an explicit type for comparison",
4392                    N);
4393                return;
4394
4395             --  Another one for C programmers
4396
4397             elsif Nkind (N) = N_Op_Concat
4398               and then Valid_Boolean_Arg (Etype (L))
4399               and then Valid_Boolean_Arg (Etype (R))
4400             then
4401                Error_Msg_N ("invalid operands for concatenation", N);
4402                Error_Msg_N ("\maybe AND was meant", N);
4403                return;
4404
4405             --  A special case for comparison of access parameter with null
4406
4407             elsif Nkind (N) = N_Op_Eq
4408               and then Is_Entity_Name (L)
4409               and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
4410               and then Nkind (Parameter_Type (Parent (Entity (L)))) =
4411                                                   N_Access_Definition
4412               and then Nkind (R) = N_Null
4413             then
4414                Error_Msg_N ("access parameter is not allowed to be null", L);
4415                Error_Msg_N ("\(call would raise Constraint_Error)", L);
4416                return;
4417             end if;
4418
4419             --  If we fall through then just give general message. Note
4420             --  that in the following messages, if the operand is overloaded
4421             --  we choose an arbitrary type to complain about, but that is
4422             --  probably more useful than not giving a type at all.
4423
4424             if Nkind (N) in N_Unary_Op then
4425                Error_Msg_Node_2 := Etype (R);
4426                Error_Msg_N ("operator& not defined for}", N);
4427                return;
4428
4429             else
4430                if Nkind (N) in N_Binary_Op then
4431                   if not Is_Overloaded (L)
4432                     and then not Is_Overloaded (R)
4433                     and then Base_Type (Etype (L)) = Base_Type (Etype (R))
4434                   then
4435                      Error_Msg_Node_2 := First_Subtype (Etype (R));
4436                      Error_Msg_N ("there is no applicable operator& for}", N);
4437
4438                   else
4439                      Error_Msg_N ("invalid operand types for operator&", N);
4440
4441                      if Nkind (N) /= N_Op_Concat then
4442                         Error_Msg_NE ("\left operand has}!",  N, Etype (L));
4443                         Error_Msg_NE ("\right operand has}!", N, Etype (R));
4444                      end if;
4445                   end if;
4446                end if;
4447             end if;
4448          end;
4449       end if;
4450    end Operator_Check;
4451
4452    -----------------------------------------
4453    -- Process_Implicit_Dereference_Prefix --
4454    -----------------------------------------
4455
4456    procedure Process_Implicit_Dereference_Prefix
4457      (E : Entity_Id;
4458       P : Entity_Id)
4459    is
4460       Ref : Node_Id;
4461
4462    begin
4463       if Present (E)
4464         and then (Operating_Mode = Check_Semantics or else not Expander_Active)
4465       then
4466          --  We create a dummy reference to E to ensure that the reference
4467          --  is not considered as part of an assignment (an implicit
4468          --  dereference can never assign to its prefix). The Comes_From_Source
4469          --  attribute needs to be propagated for accurate warnings.
4470
4471          Ref := New_Reference_To (E, Sloc (P));
4472          Set_Comes_From_Source (Ref, Comes_From_Source (P));
4473          Generate_Reference (E, Ref);
4474       end if;
4475    end Process_Implicit_Dereference_Prefix;
4476
4477    --------------------------------
4478    -- Remove_Abstract_Operations --
4479    --------------------------------
4480
4481    procedure Remove_Abstract_Operations (N : Node_Id) is
4482       I            : Interp_Index;
4483       It           : Interp;
4484       Abstract_Op  : Entity_Id := Empty;
4485
4486       --  AI-310: If overloaded, remove abstract non-dispatching
4487       --  operations. We activate this if either extensions are
4488       --  enabled, or if the abstract operation in question comes
4489       --  from a predefined file. This latter test allows us to
4490       --  use abstract to make operations invisible to users. In
4491       --  particular, if type Address is non-private and abstract
4492       --  subprograms are used to hide its operators, they will be
4493       --  truly hidden.
4494
4495       type Operand_Position is (First_Op, Second_Op);
4496       Univ_Type : constant Entity_Id := Universal_Interpretation (N);
4497
4498       procedure Remove_Address_Interpretations (Op : Operand_Position);
4499       --  Ambiguities may arise when the operands are literal and the
4500       --  address operations in s-auxdec are visible. In that case, remove
4501       --  the interpretation of a literal as Address, to retain the semantics
4502       --  of Address as a private type.
4503
4504       ------------------------------------
4505       -- Remove_Address_Interpretations --
4506       ------------------------------------
4507
4508       procedure Remove_Address_Interpretations (Op : Operand_Position) is
4509          Formal : Entity_Id;
4510
4511       begin
4512          if Is_Overloaded (N) then
4513             Get_First_Interp (N, I, It);
4514             while Present (It.Nam) loop
4515                Formal := First_Entity (It.Nam);
4516
4517                if Op = Second_Op then
4518                   Formal := Next_Entity (Formal);
4519                end if;
4520
4521                if Is_Descendent_Of_Address (Etype (Formal)) then
4522                   Remove_Interp (I);
4523                end if;
4524
4525                Get_Next_Interp (I, It);
4526             end loop;
4527          end if;
4528       end Remove_Address_Interpretations;
4529
4530    --  Start of processing for Remove_Abstract_Operations
4531
4532    begin
4533       if Is_Overloaded (N) then
4534          Get_First_Interp (N, I, It);
4535
4536          while Present (It.Nam) loop
4537             if not Is_Type (It.Nam)
4538               and then Is_Abstract (It.Nam)
4539               and then not Is_Dispatching_Operation (It.Nam)
4540               and then
4541                 (Ada_Version >= Ada_05
4542                    or else Is_Predefined_File_Name
4543                              (Unit_File_Name (Get_Source_Unit (It.Nam))))
4544
4545             then
4546                Abstract_Op := It.Nam;
4547                Remove_Interp (I);
4548                exit;
4549             end if;
4550
4551             Get_Next_Interp (I, It);
4552          end loop;
4553
4554          if No (Abstract_Op) then
4555             return;
4556
4557          elsif Nkind (N) in N_Op then
4558
4559             --  Remove interpretations that treat literals as addresses.
4560             --  This is never appropriate.
4561
4562             if Nkind (N) in N_Binary_Op then
4563                declare
4564                   U1 : constant Boolean :=
4565                      Present (Universal_Interpretation (Right_Opnd (N)));
4566                   U2 : constant Boolean :=
4567                      Present (Universal_Interpretation (Left_Opnd (N)));
4568
4569                begin
4570                   if U1 and then not U2 then
4571                      Remove_Address_Interpretations (Second_Op);
4572
4573                   elsif U2 and then not U1 then
4574                      Remove_Address_Interpretations (First_Op);
4575                   end if;
4576
4577                   if not (U1 and U2) then
4578
4579                      --  Remove corresponding predefined operator, which is
4580                      --  always added to the overload set.
4581
4582                      Get_First_Interp (N, I, It);
4583                      while Present (It.Nam) loop
4584                         if Scope (It.Nam) = Standard_Standard
4585                           and then Base_Type (It.Typ) =
4586                                    Base_Type (Etype (Abstract_Op))
4587                         then
4588                            Remove_Interp (I);
4589                         end if;
4590
4591                         Get_Next_Interp (I, It);
4592                      end loop;
4593
4594                   elsif Is_Overloaded (N)
4595                     and then Present (Univ_Type)
4596                   then
4597                      --  If both operands have a universal interpretation,
4598                      --  select the predefined operator and discard others.
4599
4600                      Get_First_Interp (N, I, It);
4601
4602                      while Present (It.Nam) loop
4603                         if Scope (It.Nam) = Standard_Standard then
4604                            Set_Etype (N, Univ_Type);
4605                            Set_Entity (N, It.Nam);
4606                            Set_Is_Overloaded (N, False);
4607                            exit;
4608                         end if;
4609
4610                         Get_Next_Interp (I, It);
4611                      end loop;
4612                   end if;
4613                end;
4614             end if;
4615
4616          elsif Nkind (N) = N_Function_Call
4617            and then
4618              (Nkind (Name (N)) = N_Operator_Symbol
4619                 or else
4620                   (Nkind (Name (N)) = N_Expanded_Name
4621                      and then
4622                        Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
4623          then
4624
4625             declare
4626                Arg1 : constant Node_Id := First (Parameter_Associations (N));
4627                U1   : constant Boolean :=
4628                         Present (Universal_Interpretation (Arg1));
4629                U2   : constant Boolean :=
4630                         Present (Next (Arg1)) and then
4631                         Present (Universal_Interpretation (Next (Arg1)));
4632
4633             begin
4634                if U1 and then not U2 then
4635                   Remove_Address_Interpretations (First_Op);
4636
4637                elsif U2 and then not U1 then
4638                   Remove_Address_Interpretations (Second_Op);
4639                end if;
4640
4641                if not (U1 and U2) then
4642                   Get_First_Interp (N, I, It);
4643                   while Present (It.Nam) loop
4644                      if Scope (It.Nam) = Standard_Standard
4645                        and then It.Typ = Base_Type (Etype (Abstract_Op))
4646                      then
4647                         Remove_Interp (I);
4648                      end if;
4649
4650                      Get_Next_Interp (I, It);
4651                   end loop;
4652                end if;
4653             end;
4654          end if;
4655
4656          --  If the removal has left no valid interpretations, emit
4657          --  error message now and label node as illegal.
4658
4659          if Present (Abstract_Op) then
4660             Get_First_Interp (N, I, It);
4661
4662             if No (It.Nam) then
4663
4664                --  Removal of abstract operation left no viable candidate
4665
4666                Set_Etype (N, Any_Type);
4667                Error_Msg_Sloc := Sloc (Abstract_Op);
4668                Error_Msg_NE
4669                  ("cannot call abstract operation& declared#", N, Abstract_Op);
4670             end if;
4671          end if;
4672       end if;
4673    end Remove_Abstract_Operations;
4674
4675    -----------------------
4676    -- Try_Indirect_Call --
4677    -----------------------
4678
4679    function Try_Indirect_Call
4680      (N   : Node_Id;
4681       Nam : Entity_Id;
4682       Typ : Entity_Id) return Boolean
4683    is
4684       Actual  : Node_Id;
4685       Formal  : Entity_Id;
4686       Call_OK : Boolean;
4687
4688    begin
4689       Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
4690       Actual := First_Actual (N);
4691       Formal := First_Formal (Designated_Type (Typ));
4692
4693       while Present (Actual)
4694         and then Present (Formal)
4695       loop
4696          if not Has_Compatible_Type (Actual, Etype (Formal)) then
4697             return False;
4698          end if;
4699
4700          Next (Actual);
4701          Next_Formal (Formal);
4702       end loop;
4703
4704       if No (Actual) and then No (Formal) then
4705          Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
4706
4707          --  Nam is a candidate interpretation for the name in the call,
4708          --  if it is not an indirect call.
4709
4710          if not Is_Type (Nam)
4711             and then Is_Entity_Name (Name (N))
4712          then
4713             Set_Entity (Name (N), Nam);
4714          end if;
4715
4716          return True;
4717       else
4718          return False;
4719       end if;
4720    end Try_Indirect_Call;
4721
4722    ----------------------
4723    -- Try_Indexed_Call --
4724    ----------------------
4725
4726    function Try_Indexed_Call
4727      (N   : Node_Id;
4728       Nam : Entity_Id;
4729       Typ : Entity_Id) return Boolean
4730    is
4731       Actuals : constant List_Id   := Parameter_Associations (N);
4732       Actual : Node_Id;
4733       Index  : Entity_Id;
4734
4735    begin
4736       Actual := First (Actuals);
4737       Index := First_Index (Typ);
4738       while Present (Actual)
4739         and then Present (Index)
4740       loop
4741          --  If the parameter list has a named association, the expression
4742          --  is definitely a call and not an indexed component.
4743
4744          if Nkind (Actual) = N_Parameter_Association then
4745             return False;
4746          end if;
4747
4748          if not Has_Compatible_Type (Actual, Etype (Index)) then
4749             return False;
4750          end if;
4751
4752          Next (Actual);
4753          Next_Index (Index);
4754       end loop;
4755
4756       if No (Actual) and then No (Index) then
4757          Add_One_Interp (N, Nam, Component_Type (Typ));
4758
4759          --  Nam is a candidate interpretation for the name in the call,
4760          --  if it is not an indirect call.
4761
4762          if not Is_Type (Nam)
4763             and then Is_Entity_Name (Name (N))
4764          then
4765             Set_Entity (Name (N), Nam);
4766          end if;
4767
4768          return True;
4769       else
4770          return False;
4771       end if;
4772    end Try_Indexed_Call;
4773
4774    --------------------------
4775    -- Try_Object_Operation --
4776    --------------------------
4777
4778    function Try_Object_Operation (N : Node_Id) return Boolean is
4779       K               : constant Node_Kind  := Nkind (Parent (N));
4780       Loc             : constant Source_Ptr := Sloc (N);
4781       Is_Subprg_Call  : constant Boolean    := K = N_Procedure_Call_Statement
4782                                                 or else K = N_Function_Call;
4783       Obj             : constant Node_Id    := Prefix (N);
4784       Subprog         : constant Node_Id    := Selector_Name (N);
4785
4786       Actual          : Node_Id;
4787       Call_Node       : Node_Id;
4788       Call_Node_Case  : Node_Id := Empty;
4789       First_Actual    : Node_Id;
4790       Node_To_Replace : Node_Id;
4791       Obj_Type        : Entity_Id := Etype (Obj);
4792
4793       procedure Complete_Object_Operation
4794         (Call_Node       : Node_Id;
4795          Node_To_Replace : Node_Id;
4796          Subprog         : Node_Id);
4797       --  Set Subprog as the name of Call_Node, replace Node_To_Replace with
4798       --  Call_Node and reanalyze Node_To_Replace.
4799
4800       procedure Transform_Object_Operation
4801         (Call_Node       : out Node_Id;
4802          First_Actual    : Node_Id;
4803          Node_To_Replace : out Node_Id;
4804          Subprog         : Node_Id);
4805       --  Transform Object.Operation (...) to Operation (Object, ...)
4806       --  Call_Node is the resulting subprogram call node, First_Actual is
4807       --  either the object Obj or an explicit dereference of Obj in certain
4808       --  cases, Node_To_Replace is either N or the parent of N, and Subprog
4809       --  is the subprogram we are trying to match.
4810
4811       function Try_Class_Wide_Operation
4812         (Call_Node       : Node_Id;
4813          Node_To_Replace : Node_Id) return Boolean;
4814       --  Traverse all the ancestor types looking for a class-wide subprogram
4815       --  that matches Subprog.
4816
4817       function Try_Primitive_Operation
4818         (Call_Node       : Node_Id;
4819          Node_To_Replace : Node_Id) return Boolean;
4820       --  Traverse the list of primitive subprograms looking for a subprogram
4821       --  than matches Subprog.
4822
4823       -------------------------------
4824       -- Complete_Object_Operation --
4825       -------------------------------
4826
4827       procedure Complete_Object_Operation
4828         (Call_Node       : Node_Id;
4829          Node_To_Replace : Node_Id;
4830          Subprog         : Node_Id)
4831       is
4832       begin
4833          Set_Name (Call_Node, New_Copy_Tree (Subprog));
4834          Set_Analyzed (Call_Node, False);
4835          Rewrite (Node_To_Replace, Call_Node);
4836          Analyze (Node_To_Replace);
4837       end Complete_Object_Operation;
4838
4839       --------------------------------
4840       -- Transform_Object_Operation --
4841       --------------------------------
4842
4843       procedure Transform_Object_Operation
4844         (Call_Node       : out Node_Id;
4845          First_Actual    : Node_Id;
4846          Node_To_Replace : out Node_Id;
4847          Subprog         : Node_Id)
4848       is
4849          Actuals     : List_Id;
4850          Parent_Node : constant Node_Id := Parent (N);
4851
4852       begin
4853          Actuals := New_List (New_Copy_Tree (First_Actual));
4854
4855          if (Nkind (Parent_Node) = N_Function_Call
4856                or else
4857              Nkind (Parent_Node) = N_Procedure_Call_Statement)
4858
4859             --  Avoid recursive calls
4860
4861            and then N /= First (Parameter_Associations (Parent_Node))
4862          then
4863             Node_To_Replace := Parent_Node;
4864
4865             --  Copy list of actuals in full before attempting to resolve call.
4866             --  This is necessary to ensure that the chaining of named actuals
4867             --  that happens during matching is done on a separate copy.
4868
4869             declare
4870                Actual : Node_Id;
4871             begin
4872                Actual := First (Parameter_Associations (Parent_Node));
4873                while Present (Actual) loop
4874                   Append (New_Copy_Tree (Actual), Actuals);
4875                   Next (Actual);
4876                end loop;
4877             end;
4878
4879             if Nkind (Parent_Node) = N_Procedure_Call_Statement then
4880                Call_Node :=
4881                  Make_Procedure_Call_Statement (Loc,
4882                    Name => New_Copy_Tree (Subprog),
4883                    Parameter_Associations => Actuals);
4884
4885             else
4886                pragma Assert (Nkind (Parent_Node) = N_Function_Call);
4887
4888                Call_Node :=
4889                  Make_Function_Call (Loc,
4890                    Name => New_Copy_Tree (Subprog),
4891                    Parameter_Associations => Actuals);
4892
4893             end if;
4894
4895          --  Parameterless call
4896
4897          else
4898             Node_To_Replace := N;
4899
4900             Call_Node :=
4901                Make_Function_Call (Loc,
4902                  Name => New_Copy_Tree (Subprog),
4903                  Parameter_Associations => Actuals);
4904
4905          end if;
4906       end Transform_Object_Operation;
4907
4908       ------------------------------
4909       -- Try_Class_Wide_Operation --
4910       ------------------------------
4911
4912       function Try_Class_Wide_Operation
4913         (Call_Node       : Node_Id;
4914          Node_To_Replace : Node_Id) return Boolean
4915       is
4916          Anc_Type : Entity_Id;
4917          Dummy    : Node_Id;
4918          Hom      : Entity_Id;
4919          Hom_Ref  : Node_Id;
4920          Success  : Boolean;
4921
4922       begin
4923          --  Loop through ancestor types, traverse their homonym chains and
4924          --  gather all interpretations of the subprogram.
4925
4926          Anc_Type := Obj_Type;
4927          loop
4928             Hom := Current_Entity (Subprog);
4929             while Present (Hom) loop
4930                if (Ekind (Hom) = E_Procedure
4931                      or else
4932                    Ekind (Hom) = E_Function)
4933                  and then Present (First_Formal (Hom))
4934                  and then Etype (First_Formal (Hom)) =
4935                             Class_Wide_Type (Anc_Type)
4936                then
4937                   Hom_Ref := New_Reference_To (Hom, Loc);
4938
4939                   --  When both the type of the object and the type of the
4940                   --  first formal of the primitive operation are tagged
4941                   --  access types, we use a node with the object as first
4942                   --  actual.
4943
4944                   if Is_Access_Type (Etype (Obj))
4945                     and then Ekind (Etype (First_Formal (Hom))) =
4946                                E_Anonymous_Access_Type
4947                   then
4948                      --  Allocate the node only once
4949
4950                      if not Present (Call_Node_Case) then
4951                         Analyze_Expression (Obj);
4952                         Set_Analyzed       (Obj);
4953
4954                         Transform_Object_Operation (
4955                           Call_Node       => Call_Node_Case,
4956                           First_Actual    => Obj,
4957                           Node_To_Replace => Dummy,
4958                           Subprog         => Subprog);
4959
4960                         Set_Etype (Call_Node_Case, Any_Type);
4961                         Set_Parent (Call_Node_Case, Parent (Node_To_Replace));
4962                      end if;
4963
4964                      Set_Name (Call_Node_Case, Hom_Ref);
4965
4966                      Analyze_One_Call (
4967                        N       => Call_Node_Case,
4968                        Nam     => Hom,
4969                        Report  => False,
4970                        Success => Success);
4971
4972                      if Success then
4973                         Complete_Object_Operation (
4974                           Call_Node       => Call_Node_Case,
4975                           Node_To_Replace => Node_To_Replace,
4976                           Subprog         => Hom_Ref);
4977
4978                         return True;
4979                      end if;
4980
4981                   --  ??? comment required
4982
4983                   else
4984                      Set_Name (Call_Node, Hom_Ref);
4985
4986                      Analyze_One_Call (
4987                        N       => Call_Node,
4988                        Nam     => Hom,
4989                        Report  => False,
4990                        Success => Success);
4991
4992                      if Success then
4993                         Complete_Object_Operation (
4994                           Call_Node       => Call_Node,
4995                           Node_To_Replace => Node_To_Replace,
4996                           Subprog         => Hom_Ref);
4997
4998                         return True;
4999                      end if;
5000                   end if;
5001                end if;
5002
5003                Hom := Homonym (Hom);
5004             end loop;
5005
5006             --  Climb to ancestor type if there is one
5007
5008             exit when Etype (Anc_Type) = Anc_Type;
5009             Anc_Type := Etype (Anc_Type);
5010          end loop;
5011
5012          return False;
5013       end Try_Class_Wide_Operation;
5014
5015       -----------------------------
5016       -- Try_Primitive_Operation --
5017       -----------------------------
5018
5019       function Try_Primitive_Operation
5020         (Call_Node       : Node_Id;
5021          Node_To_Replace : Node_Id) return Boolean
5022       is
5023          Dummy       : Node_Id;
5024          Elmt        : Elmt_Id;
5025          Prim_Op     : Entity_Id;
5026          Prim_Op_Ref : Node_Id;
5027          Success     : Boolean;
5028
5029       begin
5030          --  Look for the subprogram in the list of primitive operations
5031
5032          Elmt := First_Elmt (Primitive_Operations (Obj_Type));
5033          while Present (Elmt) loop
5034             Prim_Op := Node (Elmt);
5035
5036             if Chars (Prim_Op) = Chars (Subprog)
5037               and then Present (First_Formal (Prim_Op))
5038             then
5039                Prim_Op_Ref := New_Reference_To (Prim_Op, Loc);
5040
5041                --  When both the type of the object and the type of the first
5042                --  formal of the primitive operation are tagged access types,
5043                --  we use a node with the object as first actual.
5044
5045                if Is_Access_Type (Etype (Obj))
5046                  and then Ekind (Etype (First_Formal (Prim_Op))) =
5047                             E_Anonymous_Access_Type
5048                then
5049                   --  Allocate the node only once
5050
5051                   if not Present (Call_Node_Case) then
5052                      Analyze_Expression (Obj);
5053                      Set_Analyzed       (Obj);
5054
5055                      Transform_Object_Operation (
5056                        Call_Node       => Call_Node_Case,
5057                        First_Actual    => Obj,
5058                        Node_To_Replace => Dummy,
5059                        Subprog         => Subprog);
5060
5061                      Set_Etype (Call_Node_Case, Any_Type);
5062                      Set_Parent (Call_Node_Case, Parent (Node_To_Replace));
5063                   end if;
5064
5065                   Set_Name (Call_Node_Case, Prim_Op_Ref);
5066
5067                   Analyze_One_Call (
5068                     N       => Call_Node_Case,
5069                     Nam     => Prim_Op,
5070                     Report  => False,
5071                     Success => Success);
5072
5073                   if Success then
5074                      Complete_Object_Operation (
5075                        Call_Node       => Call_Node_Case,
5076                        Node_To_Replace => Node_To_Replace,
5077                        Subprog         => Prim_Op_Ref);
5078
5079                      return True;
5080                   end if;
5081
5082                --  Comment required ???
5083
5084                else
5085                   Set_Name (Call_Node, Prim_Op_Ref);
5086
5087                   Analyze_One_Call (
5088                     N       => Call_Node,
5089                     Nam     => Prim_Op,
5090                     Report  => False,
5091                     Success => Success);
5092
5093                   if Success then
5094                      Complete_Object_Operation (
5095                        Call_Node       => Call_Node,
5096                        Node_To_Replace => Node_To_Replace,
5097                        Subprog         => Prim_Op_Ref);
5098
5099                      return True;
5100                   end if;
5101                end if;
5102             end if;
5103
5104             Next_Elmt (Elmt);
5105          end loop;
5106
5107          return False;
5108       end Try_Primitive_Operation;
5109
5110    --  Start of processing for Try_Object_Operation
5111
5112    begin
5113       if Is_Access_Type (Obj_Type) then
5114          Obj_Type := Designated_Type (Obj_Type);
5115       end if;
5116
5117       if Ekind (Obj_Type) = E_Private_Subtype then
5118          Obj_Type := Base_Type (Obj_Type);
5119       end if;
5120
5121       if Is_Class_Wide_Type (Obj_Type) then
5122          Obj_Type := Etype (Class_Wide_Type (Obj_Type));
5123       end if;
5124
5125       --  Analyze the actuals in case of subprogram call
5126
5127       if Is_Subprg_Call and then N = Name (Parent (N)) then
5128          Actual := First (Parameter_Associations (Parent (N)));
5129          while Present (Actual) loop
5130             Analyze_Expression (Actual);
5131             Next (Actual);
5132          end loop;
5133       end if;
5134
5135       --  If the object is of an Access type, explicit dereference is
5136       --  required.
5137
5138       if Is_Access_Type (Etype (Obj)) then
5139          First_Actual :=
5140            Make_Explicit_Dereference (Sloc (Obj), Obj);
5141          Set_Etype (First_Actual, Obj_Type);
5142       else
5143          First_Actual := Obj;
5144       end if;
5145
5146       Analyze_Expression (First_Actual);
5147       Set_Analyzed       (First_Actual);
5148
5149       --  Build a subprogram call node
5150
5151       Transform_Object_Operation (
5152         Call_Node       => Call_Node,
5153         First_Actual    => First_Actual,
5154         Node_To_Replace => Node_To_Replace,
5155         Subprog         => Subprog);
5156
5157       Set_Etype (Call_Node, Any_Type);
5158       Set_Parent (Call_Node, Parent (Node_To_Replace));
5159
5160       return
5161          Try_Primitive_Operation
5162            (Call_Node       => Call_Node,
5163             Node_To_Replace => Node_To_Replace)
5164         or else
5165          Try_Class_Wide_Operation
5166            (Call_Node       => Call_Node,
5167             Node_To_Replace => Node_To_Replace);
5168    end Try_Object_Operation;
5169
5170 end Sem_Ch4;