OSDN Git Service

2008-05-27 Thomas Quinot <quinot@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_res.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              S E M _ R E S                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2008, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Atree;    use Atree;
27 with Checks;   use Checks;
28 with Debug;    use Debug;
29 with Debug_A;  use Debug_A;
30 with Einfo;    use Einfo;
31 with Elists;   use Elists;
32 with Errout;   use Errout;
33 with Expander; use Expander;
34 with Exp_Disp; use Exp_Disp;
35 with Exp_Ch6;  use Exp_Ch6;
36 with Exp_Ch7;  use Exp_Ch7;
37 with Exp_Tss;  use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname;    use Fname;
40 with Freeze;   use Freeze;
41 with Itypes;   use Itypes;
42 with Lib;      use Lib;
43 with Lib.Xref; use Lib.Xref;
44 with Namet;    use Namet;
45 with Nmake;    use Nmake;
46 with Nlists;   use Nlists;
47 with Opt;      use Opt;
48 with Output;   use Output;
49 with Restrict; use Restrict;
50 with Rident;   use Rident;
51 with Rtsfind;  use Rtsfind;
52 with Sem;      use Sem;
53 with Sem_Aggr; use Sem_Aggr;
54 with Sem_Attr; use Sem_Attr;
55 with Sem_Cat;  use Sem_Cat;
56 with Sem_Ch4;  use Sem_Ch4;
57 with Sem_Ch6;  use Sem_Ch6;
58 with Sem_Ch8;  use Sem_Ch8;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Dist; use Sem_Dist;
61 with Sem_Elab; use Sem_Elab;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Intr; use Sem_Intr;
64 with Sem_Util; use Sem_Util;
65 with Sem_Type; use Sem_Type;
66 with Sem_Warn; use Sem_Warn;
67 with Sinfo;    use Sinfo;
68 with Snames;   use Snames;
69 with Stand;    use Stand;
70 with Stringt;  use Stringt;
71 with Style;    use Style;
72 with Targparm; use Targparm;
73 with Tbuild;   use Tbuild;
74 with Uintp;    use Uintp;
75 with Urealp;   use Urealp;
76
77 package body Sem_Res is
78
79    -----------------------
80    -- Local Subprograms --
81    -----------------------
82
83    --  Second pass (top-down) type checking and overload resolution procedures
84    --  Typ is the type required by context. These procedures propagate the
85    --  type information recursively to the descendants of N. If the node
86    --  is not overloaded, its Etype is established in the first pass. If
87    --  overloaded,  the Resolve routines set the correct type. For arith.
88    --  operators, the Etype is the base type of the context.
89
90    --  Note that Resolve_Attribute is separated off in Sem_Attr
91
92    procedure Check_Discriminant_Use (N : Node_Id);
93    --  Enforce the restrictions on the use of discriminants when constraining
94    --  a component of a discriminated type (record or concurrent type).
95
96    procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
97    --  Given a node for an operator associated with type T, check that
98    --  the operator is visible. Operators all of whose operands are
99    --  universal must be checked for visibility during resolution
100    --  because their type is not determinable based on their operands.
101
102    procedure Check_Fully_Declared_Prefix
103      (Typ  : Entity_Id;
104       Pref : Node_Id);
105    --  Check that the type of the prefix of a dereference is not incomplete
106
107    function Check_Infinite_Recursion (N : Node_Id) return Boolean;
108    --  Given a call node, N, which is known to occur immediately within the
109    --  subprogram being called, determines whether it is a detectable case of
110    --  an infinite recursion, and if so, outputs appropriate messages. Returns
111    --  True if an infinite recursion is detected, and False otherwise.
112
113    procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
114    --  If the type of the object being initialized uses the secondary stack
115    --  directly or indirectly, create a transient scope for the call to the
116    --  init proc. This is because we do not create transient scopes for the
117    --  initialization of individual components within the init proc itself.
118    --  Could be optimized away perhaps?
119
120    function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
121    --  Determine whether E is an access type declared by an access
122    --  declaration, and not an (anonymous) allocator type.
123
124    function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
125    --  Utility to check whether the name in the call is a predefined
126    --  operator, in which case the call is made into an operator node.
127    --  An instance of an intrinsic conversion operation may be given
128    --  an operator name, but is not treated like an operator.
129
130    procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
131    --  If a default expression in entry call N depends on the discriminants
132    --  of the task, it must be replaced with a reference to the discriminant
133    --  of the task being called.
134
135    procedure Resolve_Op_Concat_Arg
136      (N       : Node_Id;
137       Arg     : Node_Id;
138       Typ     : Entity_Id;
139       Is_Comp : Boolean);
140    --  Internal procedure for Resolve_Op_Concat to resolve one operand of
141    --  concatenation operator.  The operand is either of the array type or of
142    --  the component type. If the operand is an aggregate, and the component
143    --  type is composite, this is ambiguous if component type has aggregates.
144
145    procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
146    --  Does the first part of the work of Resolve_Op_Concat
147
148    procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
149    --  Does the "rest" of the work of Resolve_Op_Concat, after the left operand
150    --  has been resolved. See Resolve_Op_Concat for details.
151
152    procedure Resolve_Allocator                 (N : Node_Id; Typ : Entity_Id);
153    procedure Resolve_Arithmetic_Op             (N : Node_Id; Typ : Entity_Id);
154    procedure Resolve_Call                      (N : Node_Id; Typ : Entity_Id);
155    procedure Resolve_Character_Literal         (N : Node_Id; Typ : Entity_Id);
156    procedure Resolve_Comparison_Op             (N : Node_Id; Typ : Entity_Id);
157    procedure Resolve_Conditional_Expression    (N : Node_Id; Typ : Entity_Id);
158    procedure Resolve_Equality_Op               (N : Node_Id; Typ : Entity_Id);
159    procedure Resolve_Explicit_Dereference      (N : Node_Id; Typ : Entity_Id);
160    procedure Resolve_Entity_Name               (N : Node_Id; Typ : Entity_Id);
161    procedure Resolve_Indexed_Component         (N : Node_Id; Typ : Entity_Id);
162    procedure Resolve_Integer_Literal           (N : Node_Id; Typ : Entity_Id);
163    procedure Resolve_Logical_Op                (N : Node_Id; Typ : Entity_Id);
164    procedure Resolve_Membership_Op             (N : Node_Id; Typ : Entity_Id);
165    procedure Resolve_Null                      (N : Node_Id; Typ : Entity_Id);
166    procedure Resolve_Operator_Symbol           (N : Node_Id; Typ : Entity_Id);
167    procedure Resolve_Op_Concat                 (N : Node_Id; Typ : Entity_Id);
168    procedure Resolve_Op_Expon                  (N : Node_Id; Typ : Entity_Id);
169    procedure Resolve_Op_Not                    (N : Node_Id; Typ : Entity_Id);
170    procedure Resolve_Qualified_Expression      (N : Node_Id; Typ : Entity_Id);
171    procedure Resolve_Range                     (N : Node_Id; Typ : Entity_Id);
172    procedure Resolve_Real_Literal              (N : Node_Id; Typ : Entity_Id);
173    procedure Resolve_Reference                 (N : Node_Id; Typ : Entity_Id);
174    procedure Resolve_Selected_Component        (N : Node_Id; Typ : Entity_Id);
175    procedure Resolve_Shift                     (N : Node_Id; Typ : Entity_Id);
176    procedure Resolve_Short_Circuit             (N : Node_Id; Typ : Entity_Id);
177    procedure Resolve_Slice                     (N : Node_Id; Typ : Entity_Id);
178    procedure Resolve_String_Literal            (N : Node_Id; Typ : Entity_Id);
179    procedure Resolve_Subprogram_Info           (N : Node_Id; Typ : Entity_Id);
180    procedure Resolve_Type_Conversion           (N : Node_Id; Typ : Entity_Id);
181    procedure Resolve_Unary_Op                  (N : Node_Id; Typ : Entity_Id);
182    procedure Resolve_Unchecked_Expression      (N : Node_Id; Typ : Entity_Id);
183    procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
184
185    function Operator_Kind
186      (Op_Name   : Name_Id;
187       Is_Binary : Boolean) return Node_Kind;
188    --  Utility to map the name of an operator into the corresponding Node. Used
189    --  by other node rewriting procedures.
190
191    procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
192    --  Resolve actuals of call, and add default expressions for missing ones.
193    --  N is the Node_Id for the subprogram call, and Nam is the entity of the
194    --  called subprogram.
195
196    procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
197    --  Called from Resolve_Call, when the prefix denotes an entry or element
198    --  of entry family. Actuals are resolved as for subprograms, and the node
199    --  is rebuilt as an entry call. Also called for protected operations. Typ
200    --  is the context type, which is used when the operation is a protected
201    --  function with no arguments, and the return value is indexed.
202
203    procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
204    --  A call to a user-defined intrinsic operator is rewritten as a call
205    --  to the corresponding predefined operator, with suitable conversions.
206
207    procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
208    --  Ditto, for unary operators (only arithmetic ones)
209
210    procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
211    --  If an operator node resolves to a call to a user-defined operator,
212    --  rewrite the node as a function call.
213
214    procedure Make_Call_Into_Operator
215      (N     : Node_Id;
216       Typ   : Entity_Id;
217       Op_Id : Entity_Id);
218    --  Inverse transformation: if an operator is given in functional notation,
219    --  then after resolving the node, transform into an operator node, so
220    --  that operands are resolved properly. Recall that predefined operators
221    --  do not have a full signature and special resolution rules apply.
222
223    procedure Rewrite_Renamed_Operator
224      (N   : Node_Id;
225       Op  : Entity_Id;
226       Typ : Entity_Id);
227    --  An operator can rename another, e.g. in  an instantiation. In that
228    --  case, the proper operator node must be constructed and resolved.
229
230    procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
231    --  The String_Literal_Subtype is built for all strings that are not
232    --  operands of a static concatenation operation. If the argument is
233    --  not a N_String_Literal node, then the call has no effect.
234
235    procedure Set_Slice_Subtype (N : Node_Id);
236    --  Build subtype of array type, with the range specified by the slice
237
238    procedure Simplify_Type_Conversion (N : Node_Id);
239    --  Called after N has been resolved and evaluated, but before range checks
240    --  have been applied. Currently simplifies a combination of floating-point
241    --  to integer conversion and Truncation attribute.
242
243    function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
244    --  A universal_fixed expression in an universal context is unambiguous
245    --  if there is only one applicable fixed point type. Determining whether
246    --  there is only one requires a search over all visible entities, and
247    --  happens only in very pathological cases (see 6115-006).
248
249    function Valid_Conversion
250      (N       : Node_Id;
251       Target  : Entity_Id;
252       Operand : Node_Id) return Boolean;
253    --  Verify legality rules given in 4.6 (8-23). Target is the target
254    --  type of the conversion, which may be an implicit conversion of
255    --  an actual parameter to an anonymous access type (in which case
256    --  N denotes the actual parameter and N = Operand).
257
258    -------------------------
259    -- Ambiguous_Character --
260    -------------------------
261
262    procedure Ambiguous_Character (C : Node_Id) is
263       E : Entity_Id;
264
265    begin
266       if Nkind (C) = N_Character_Literal then
267          Error_Msg_N ("ambiguous character literal", C);
268
269          --  First the ones in Standard
270
271          Error_Msg_N
272            ("\\possible interpretation: Character!", C);
273          Error_Msg_N
274            ("\\possible interpretation: Wide_Character!", C);
275
276          --  Include Wide_Wide_Character in Ada 2005 mode
277
278          if Ada_Version >= Ada_05 then
279             Error_Msg_N
280               ("\\possible interpretation: Wide_Wide_Character!", C);
281          end if;
282
283          --  Now any other types that match
284
285          E := Current_Entity (C);
286          while Present (E) loop
287             Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
288             E := Homonym (E);
289          end loop;
290       end if;
291    end Ambiguous_Character;
292
293    -------------------------
294    -- Analyze_And_Resolve --
295    -------------------------
296
297    procedure Analyze_And_Resolve (N : Node_Id) is
298    begin
299       Analyze (N);
300       Resolve (N);
301    end Analyze_And_Resolve;
302
303    procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
304    begin
305       Analyze (N);
306       Resolve (N, Typ);
307    end Analyze_And_Resolve;
308
309    --  Version withs check(s) suppressed
310
311    procedure Analyze_And_Resolve
312      (N        : Node_Id;
313       Typ      : Entity_Id;
314       Suppress : Check_Id)
315    is
316       Scop : constant Entity_Id := Current_Scope;
317
318    begin
319       if Suppress = All_Checks then
320          declare
321             Svg : constant Suppress_Array := Scope_Suppress;
322          begin
323             Scope_Suppress := (others => True);
324             Analyze_And_Resolve (N, Typ);
325             Scope_Suppress := Svg;
326          end;
327
328       else
329          declare
330             Svg : constant Boolean := Scope_Suppress (Suppress);
331
332          begin
333             Scope_Suppress (Suppress) := True;
334             Analyze_And_Resolve (N, Typ);
335             Scope_Suppress (Suppress) := Svg;
336          end;
337       end if;
338
339       if Current_Scope /= Scop
340         and then Scope_Is_Transient
341       then
342          --  This can only happen if a transient scope was created
343          --  for an inner expression, which will be removed upon
344          --  completion of the analysis of an enclosing construct.
345          --  The transient scope must have the suppress status of
346          --  the enclosing environment, not of this Analyze call.
347
348          Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
349            Scope_Suppress;
350       end if;
351    end Analyze_And_Resolve;
352
353    procedure Analyze_And_Resolve
354      (N        : Node_Id;
355       Suppress : Check_Id)
356    is
357       Scop : constant Entity_Id := Current_Scope;
358
359    begin
360       if Suppress = All_Checks then
361          declare
362             Svg : constant Suppress_Array := Scope_Suppress;
363          begin
364             Scope_Suppress := (others => True);
365             Analyze_And_Resolve (N);
366             Scope_Suppress := Svg;
367          end;
368
369       else
370          declare
371             Svg : constant Boolean := Scope_Suppress (Suppress);
372
373          begin
374             Scope_Suppress (Suppress) := True;
375             Analyze_And_Resolve (N);
376             Scope_Suppress (Suppress) := Svg;
377          end;
378       end if;
379
380       if Current_Scope /= Scop
381         and then Scope_Is_Transient
382       then
383          Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
384            Scope_Suppress;
385       end if;
386    end Analyze_And_Resolve;
387
388    ----------------------------
389    -- Check_Discriminant_Use --
390    ----------------------------
391
392    procedure Check_Discriminant_Use (N : Node_Id) is
393       PN   : constant Node_Id   := Parent (N);
394       Disc : constant Entity_Id := Entity (N);
395       P    : Node_Id;
396       D    : Node_Id;
397
398    begin
399       --  Any use in a spec-expression is legal
400
401       if In_Spec_Expression then
402          null;
403
404       elsif Nkind (PN) = N_Range then
405
406          --  Discriminant cannot be used to constrain a scalar type
407
408          P := Parent (PN);
409
410          if Nkind (P) = N_Range_Constraint
411            and then Nkind (Parent (P)) = N_Subtype_Indication
412            and then Nkind (Parent (Parent (P))) = N_Component_Definition
413          then
414             Error_Msg_N ("discriminant cannot constrain scalar type", N);
415
416          elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
417
418             --  The following check catches the unusual case where
419             --  a discriminant appears within an index constraint
420             --  that is part of a larger expression within a constraint
421             --  on a component, e.g. "C : Int range 1 .. F (new A(1 .. D))".
422             --  For now we only check case of record components, and
423             --  note that a similar check should also apply in the
424             --  case of discriminant constraints below. ???
425
426             --  Note that the check for N_Subtype_Declaration below is to
427             --  detect the valid use of discriminants in the constraints of a
428             --  subtype declaration when this subtype declaration appears
429             --  inside the scope of a record type (which is syntactically
430             --  illegal, but which may be created as part of derived type
431             --  processing for records). See Sem_Ch3.Build_Derived_Record_Type
432             --  for more info.
433
434             if Ekind (Current_Scope) = E_Record_Type
435               and then Scope (Disc) = Current_Scope
436               and then not
437                 (Nkind (Parent (P)) = N_Subtype_Indication
438                   and then
439                     Nkind_In (Parent (Parent (P)), N_Component_Definition,
440                                                    N_Subtype_Declaration)
441                   and then Paren_Count (N) = 0)
442             then
443                Error_Msg_N
444                  ("discriminant must appear alone in component constraint", N);
445                return;
446             end if;
447
448             --   Detect a common beginner error:
449
450             --   type R (D : Positive := 100) is record
451             --     Name : String (1 .. D);
452             --   end record;
453
454             --  The default value causes an object of type R to be
455             --  allocated with room for Positive'Last characters.
456
457             declare
458                SI : Node_Id;
459                T  : Entity_Id;
460                TB : Node_Id;
461                CB : Entity_Id;
462
463                function Large_Storage_Type (T : Entity_Id) return Boolean;
464                --  Return True if type T has a large enough range that
465                --  any array whose index type covered the whole range of
466                --  the type would likely raise Storage_Error.
467
468                ------------------------
469                -- Large_Storage_Type --
470                ------------------------
471
472                function Large_Storage_Type (T : Entity_Id) return Boolean is
473                begin
474                   return
475                     T = Standard_Integer
476                       or else
477                     T = Standard_Positive
478                       or else
479                     T = Standard_Natural;
480                end Large_Storage_Type;
481
482             begin
483                --  Check that the Disc has a large range
484
485                if not Large_Storage_Type (Etype (Disc)) then
486                   goto No_Danger;
487                end if;
488
489                --  If the enclosing type is limited, we allocate only the
490                --  default value, not the maximum, and there is no need for
491                --  a warning.
492
493                if Is_Limited_Type (Scope (Disc)) then
494                   goto No_Danger;
495                end if;
496
497                --  Check that it is the high bound
498
499                if N /= High_Bound (PN)
500                  or else No (Discriminant_Default_Value (Disc))
501                then
502                   goto No_Danger;
503                end if;
504
505                --  Check the array allows a large range at this bound.
506                --  First find the array
507
508                SI := Parent (P);
509
510                if Nkind (SI) /= N_Subtype_Indication then
511                   goto No_Danger;
512                end if;
513
514                T := Entity (Subtype_Mark (SI));
515
516                if not Is_Array_Type (T) then
517                   goto No_Danger;
518                end if;
519
520                --  Next, find the dimension
521
522                TB := First_Index (T);
523                CB := First (Constraints (P));
524                while True
525                  and then Present (TB)
526                  and then Present (CB)
527                  and then CB /= PN
528                loop
529                   Next_Index (TB);
530                   Next (CB);
531                end loop;
532
533                if CB /= PN then
534                   goto No_Danger;
535                end if;
536
537                --  Now, check the dimension has a large range
538
539                if not Large_Storage_Type (Etype (TB)) then
540                   goto No_Danger;
541                end if;
542
543                --  Warn about the danger
544
545                Error_Msg_N
546                  ("?creation of & object may raise Storage_Error!",
547                   Scope (Disc));
548
549                <<No_Danger>>
550                   null;
551
552             end;
553          end if;
554
555       --  Legal case is in index or discriminant constraint
556
557       elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
558                           N_Discriminant_Association)
559       then
560          if Paren_Count (N) > 0 then
561             Error_Msg_N
562               ("discriminant in constraint must appear alone",  N);
563
564          elsif Nkind (N) = N_Expanded_Name
565            and then Comes_From_Source (N)
566          then
567             Error_Msg_N
568               ("discriminant must appear alone as a direct name", N);
569          end if;
570
571          return;
572
573       --  Otherwise, context is an expression. It should not be within
574       --  (i.e. a subexpression of) a constraint for a component.
575
576       else
577          D := PN;
578          P := Parent (PN);
579          while not Nkind_In (P, N_Component_Declaration,
580                                 N_Subtype_Indication,
581                                 N_Entry_Declaration)
582          loop
583             D := P;
584             P := Parent (P);
585             exit when No (P);
586          end loop;
587
588          --  If the discriminant is used in an expression that is a bound
589          --  of a scalar type, an Itype is created and the bounds are attached
590          --  to its range,  not to the original subtype indication. Such use
591          --  is of course a double fault.
592
593          if (Nkind (P) = N_Subtype_Indication
594               and then Nkind_In (Parent (P), N_Component_Definition,
595                                              N_Derived_Type_Definition)
596               and then D = Constraint (P))
597
598          --  The constraint itself may be given by a subtype indication,
599          --  rather than by a more common discrete range.
600
601            or else (Nkind (P) = N_Subtype_Indication
602                       and then
603                     Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
604            or else Nkind (P) = N_Entry_Declaration
605            or else Nkind (D) = N_Defining_Identifier
606          then
607             Error_Msg_N
608               ("discriminant in constraint must appear alone",  N);
609          end if;
610       end if;
611    end Check_Discriminant_Use;
612
613    --------------------------------
614    -- Check_For_Visible_Operator --
615    --------------------------------
616
617    procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
618    begin
619       if Is_Invisible_Operator (N, T) then
620          Error_Msg_NE
621            ("operator for} is not directly visible!", N, First_Subtype (T));
622          Error_Msg_N ("use clause would make operation legal!", N);
623       end if;
624    end Check_For_Visible_Operator;
625
626    ----------------------------------
627    --  Check_Fully_Declared_Prefix --
628    ----------------------------------
629
630    procedure Check_Fully_Declared_Prefix
631      (Typ  : Entity_Id;
632       Pref : Node_Id)
633    is
634    begin
635       --  Check that the designated type of the prefix of a dereference is
636       --  not an incomplete type. This cannot be done unconditionally, because
637       --  dereferences of private types are legal in default expressions. This
638       --  case is taken care of in Check_Fully_Declared, called below. There
639       --  are also 2005 cases where it is legal for the prefix to be unfrozen.
640
641       --  This consideration also applies to similar checks for allocators,
642       --  qualified expressions, and type conversions.
643
644       --  An additional exception concerns other per-object expressions that
645       --  are not directly related to component declarations, in particular
646       --  representation pragmas for tasks. These will be per-object
647       --  expressions if they depend on discriminants or some global entity.
648       --  If the task has access discriminants, the designated type may be
649       --  incomplete at the point the expression is resolved. This resolution
650       --  takes place within the body of the initialization procedure, where
651       --  the discriminant is replaced by its discriminal.
652
653       if Is_Entity_Name (Pref)
654         and then Ekind (Entity (Pref)) = E_In_Parameter
655       then
656          null;
657
658       --  Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
659       --  are handled by Analyze_Access_Attribute, Analyze_Assignment,
660       --  Analyze_Object_Renaming, and Freeze_Entity.
661
662       elsif Ada_Version >= Ada_05
663         and then Is_Entity_Name (Pref)
664         and then Ekind (Directly_Designated_Type (Etype (Pref))) =
665                                                        E_Incomplete_Type
666         and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
667       then
668          null;
669       else
670          Check_Fully_Declared (Typ, Parent (Pref));
671       end if;
672    end Check_Fully_Declared_Prefix;
673
674    ------------------------------
675    -- Check_Infinite_Recursion --
676    ------------------------------
677
678    function Check_Infinite_Recursion (N : Node_Id) return Boolean is
679       P : Node_Id;
680       C : Node_Id;
681
682       function Same_Argument_List return Boolean;
683       --  Check whether list of actuals is identical to list of formals
684       --  of called function (which is also the enclosing scope).
685
686       ------------------------
687       -- Same_Argument_List --
688       ------------------------
689
690       function Same_Argument_List return Boolean is
691          A    : Node_Id;
692          F    : Entity_Id;
693          Subp : Entity_Id;
694
695       begin
696          if not Is_Entity_Name (Name (N)) then
697             return False;
698          else
699             Subp := Entity (Name (N));
700          end if;
701
702          F := First_Formal (Subp);
703          A := First_Actual (N);
704          while Present (F) and then Present (A) loop
705             if not Is_Entity_Name (A)
706               or else Entity (A) /= F
707             then
708                return False;
709             end if;
710
711             Next_Actual (A);
712             Next_Formal (F);
713          end loop;
714
715          return True;
716       end Same_Argument_List;
717
718    --  Start of processing for Check_Infinite_Recursion
719
720    begin
721       --  Special case, if this is a procedure call and is a call to the
722       --  current procedure with the same argument list, then this is for
723       --  sure an infinite recursion and we insert a call to raise SE.
724
725       if Is_List_Member (N)
726         and then List_Length (List_Containing (N)) = 1
727         and then Same_Argument_List
728       then
729          declare
730             P : constant Node_Id := Parent (N);
731          begin
732             if Nkind (P) = N_Handled_Sequence_Of_Statements
733               and then Nkind (Parent (P)) = N_Subprogram_Body
734               and then Is_Empty_List (Declarations (Parent (P)))
735             then
736                Error_Msg_N ("!?infinite recursion", N);
737                Error_Msg_N ("\!?Storage_Error will be raised at run time", N);
738                Insert_Action (N,
739                  Make_Raise_Storage_Error (Sloc (N),
740                    Reason => SE_Infinite_Recursion));
741                return True;
742             end if;
743          end;
744       end if;
745
746       --  If not that special case, search up tree, quitting if we reach a
747       --  construct (e.g. a conditional) that tells us that this is not a
748       --  case for an infinite recursion warning.
749
750       C := N;
751       loop
752          P := Parent (C);
753          exit when Nkind (P) = N_Subprogram_Body;
754          if Nkind_In (P, N_Or_Else,
755                          N_And_Then,
756                          N_If_Statement,
757                          N_Case_Statement)
758          then
759             return False;
760
761          elsif Nkind (P) = N_Handled_Sequence_Of_Statements
762            and then C /= First (Statements (P))
763          then
764             --  If the call is the expression of a return statement and the
765             --  actuals are identical to the formals, it's worth a warning.
766             --  However, we skip this if there is an immediately preceding
767             --  raise statement, since the call is never executed.
768
769             --  Furthermore, this corresponds to a common idiom:
770
771             --    function F (L : Thing) return Boolean is
772             --    begin
773             --       raise Program_Error;
774             --       return F (L);
775             --    end F;
776
777             --  for generating a stub function
778
779             if Nkind (Parent (N)) = N_Simple_Return_Statement
780               and then Same_Argument_List
781             then
782                exit when not Is_List_Member (Parent (N));
783
784                --  OK, return statement is in a statement list, look for raise
785
786                declare
787                   Nod : Node_Id;
788
789                begin
790                   --  Skip past N_Freeze_Entity nodes generated by expansion
791
792                   Nod := Prev (Parent (N));
793                   while Present (Nod)
794                     and then Nkind (Nod) = N_Freeze_Entity
795                   loop
796                      Prev (Nod);
797                   end loop;
798
799                   --  If no raise statement, give warning
800
801                   exit when Nkind (Nod) /= N_Raise_Statement
802                     and then
803                       (Nkind (Nod) not in N_Raise_xxx_Error
804                          or else Present (Condition (Nod)));
805                end;
806             end if;
807
808             return False;
809
810          else
811             C := P;
812          end if;
813       end loop;
814
815       Error_Msg_N ("!?possible infinite recursion", N);
816       Error_Msg_N ("\!?Storage_Error may be raised at run time", N);
817
818       return True;
819    end Check_Infinite_Recursion;
820
821    -------------------------------
822    -- Check_Initialization_Call --
823    -------------------------------
824
825    procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
826       Typ : constant Entity_Id := Etype (First_Formal (Nam));
827
828       function Uses_SS (T : Entity_Id) return Boolean;
829       --  Check whether the creation of an object of the type will involve
830       --  use of the secondary stack. If T is a record type, this is true
831       --  if the expression for some component uses the secondary stack, e.g.
832       --  through a call to a function that returns an unconstrained value.
833       --  False if T is controlled, because cleanups occur elsewhere.
834
835       -------------
836       -- Uses_SS --
837       -------------
838
839       function Uses_SS (T : Entity_Id) return Boolean is
840          Comp      : Entity_Id;
841          Expr      : Node_Id;
842          Full_Type : Entity_Id := Underlying_Type (T);
843
844       begin
845          --  Normally we want to use the underlying type, but if it's not set
846          --  then continue with T.
847
848          if not Present (Full_Type) then
849             Full_Type := T;
850          end if;
851
852          if Is_Controlled (Full_Type) then
853             return False;
854
855          elsif Is_Array_Type (Full_Type) then
856             return Uses_SS (Component_Type (Full_Type));
857
858          elsif Is_Record_Type (Full_Type) then
859             Comp := First_Component (Full_Type);
860             while Present (Comp) loop
861                if Ekind (Comp) = E_Component
862                  and then Nkind (Parent (Comp)) = N_Component_Declaration
863                then
864                   --  The expression for a dynamic component may be rewritten
865                   --  as a dereference, so retrieve original node.
866
867                   Expr := Original_Node (Expression (Parent (Comp)));
868
869                   --  Return True if the expression is a call to a function
870                   --  (including an attribute function such as Image) with
871                   --  a result that requires a transient scope.
872
873                   if (Nkind (Expr) = N_Function_Call
874                        or else (Nkind (Expr) = N_Attribute_Reference
875                                  and then Present (Expressions (Expr))))
876                     and then Requires_Transient_Scope (Etype (Expr))
877                   then
878                      return True;
879
880                   elsif Uses_SS (Etype (Comp)) then
881                      return True;
882                   end if;
883                end if;
884
885                Next_Component (Comp);
886             end loop;
887
888             return False;
889
890          else
891             return False;
892          end if;
893       end Uses_SS;
894
895    --  Start of processing for Check_Initialization_Call
896
897    begin
898       --  Establish a transient scope if the type needs it
899
900       if Uses_SS (Typ) then
901          Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
902       end if;
903    end Check_Initialization_Call;
904
905    ------------------------------
906    -- Check_Parameterless_Call --
907    ------------------------------
908
909    procedure Check_Parameterless_Call (N : Node_Id) is
910       Nam : Node_Id;
911
912       function Prefix_Is_Access_Subp return Boolean;
913       --  If the prefix is of an access_to_subprogram type, the node must be
914       --  rewritten as a call. Ditto if the prefix is overloaded and all its
915       --  interpretations are access to subprograms.
916
917       ---------------------------
918       -- Prefix_Is_Access_Subp --
919       ---------------------------
920
921       function Prefix_Is_Access_Subp return Boolean is
922          I   : Interp_Index;
923          It  : Interp;
924
925       begin
926          if not Is_Overloaded (N) then
927             return
928               Ekind (Etype (N)) = E_Subprogram_Type
929                 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
930          else
931             Get_First_Interp (N, I, It);
932             while Present (It.Typ) loop
933                if Ekind (It.Typ) /= E_Subprogram_Type
934                  or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
935                then
936                   return False;
937                end if;
938
939                Get_Next_Interp (I, It);
940             end loop;
941
942             return True;
943          end if;
944       end Prefix_Is_Access_Subp;
945
946    --  Start of processing for Check_Parameterless_Call
947
948    begin
949       --  Defend against junk stuff if errors already detected
950
951       if Total_Errors_Detected /= 0 then
952          if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
953             return;
954          elsif Nkind (N) in N_Has_Chars
955            and then Chars (N) in Error_Name_Or_No_Name
956          then
957             return;
958          end if;
959
960          Require_Entity (N);
961       end if;
962
963       --  If the context expects a value, and the name is a procedure, this is
964       --  most likely a missing 'Access. Don't try to resolve the parameterless
965       --  call, error will be caught when the outer call is analyzed.
966
967       if Is_Entity_Name (N)
968         and then Ekind (Entity (N)) = E_Procedure
969         and then not Is_Overloaded (N)
970         and then
971          Nkind_In (Parent (N), N_Parameter_Association,
972                                N_Function_Call,
973                                N_Procedure_Call_Statement)
974       then
975          return;
976       end if;
977
978       --  Rewrite as call if overloadable entity that is (or could be, in the
979       --  overloaded case) a function call. If we know for sure that the entity
980       --  is an enumeration literal, we do not rewrite it.
981
982       if (Is_Entity_Name (N)
983             and then Is_Overloadable (Entity (N))
984             and then (Ekind (Entity (N)) /= E_Enumeration_Literal
985                         or else Is_Overloaded (N)))
986
987       --  Rewrite as call if it is an explicit deference of an expression of
988       --  a subprogram access type, and the subprogram type is not that of a
989       --  procedure or entry.
990
991       or else
992         (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
993
994       --  Rewrite as call if it is a selected component which is a function,
995       --  this is the case of a call to a protected function (which may be
996       --  overloaded with other protected operations).
997
998       or else
999         (Nkind (N) = N_Selected_Component
1000           and then (Ekind (Entity (Selector_Name (N))) = E_Function
1001                       or else
1002                         ((Ekind (Entity (Selector_Name (N))) = E_Entry
1003                             or else
1004                           Ekind (Entity (Selector_Name (N))) = E_Procedure)
1005                             and then Is_Overloaded (Selector_Name (N)))))
1006
1007       --  If one of the above three conditions is met, rewrite as call.
1008       --  Apply the rewriting only once.
1009
1010       then
1011          if Nkind (Parent (N)) /= N_Function_Call
1012            or else N /= Name (Parent (N))
1013          then
1014             Nam := New_Copy (N);
1015
1016             --  If overloaded, overload set belongs to new copy
1017
1018             Save_Interps (N, Nam);
1019
1020             --  Change node to parameterless function call (note that the
1021             --  Parameter_Associations associations field is left set to Empty,
1022             --  its normal default value since there are no parameters)
1023
1024             Change_Node (N, N_Function_Call);
1025             Set_Name (N, Nam);
1026             Set_Sloc (N, Sloc (Nam));
1027             Analyze_Call (N);
1028          end if;
1029
1030       elsif Nkind (N) = N_Parameter_Association then
1031          Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1032       end if;
1033    end Check_Parameterless_Call;
1034
1035    -----------------------------
1036    -- Is_Definite_Access_Type --
1037    -----------------------------
1038
1039    function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1040       Btyp : constant Entity_Id := Base_Type (E);
1041    begin
1042       return Ekind (Btyp) = E_Access_Type
1043         or else (Ekind (Btyp) = E_Access_Subprogram_Type
1044                   and then Comes_From_Source (Btyp));
1045    end Is_Definite_Access_Type;
1046
1047    ----------------------
1048    -- Is_Predefined_Op --
1049    ----------------------
1050
1051    function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1052    begin
1053       return Is_Intrinsic_Subprogram (Nam)
1054         and then not Is_Generic_Instance (Nam)
1055         and then Chars (Nam) in Any_Operator_Name
1056         and then (No (Alias (Nam))
1057                    or else Is_Predefined_Op (Alias (Nam)));
1058    end Is_Predefined_Op;
1059
1060    -----------------------------
1061    -- Make_Call_Into_Operator --
1062    -----------------------------
1063
1064    procedure Make_Call_Into_Operator
1065      (N     : Node_Id;
1066       Typ   : Entity_Id;
1067       Op_Id : Entity_Id)
1068    is
1069       Op_Name   : constant Name_Id := Chars (Op_Id);
1070       Act1      : Node_Id := First_Actual (N);
1071       Act2      : Node_Id := Next_Actual (Act1);
1072       Error     : Boolean := False;
1073       Func      : constant Entity_Id := Entity (Name (N));
1074       Is_Binary : constant Boolean   := Present (Act2);
1075       Op_Node   : Node_Id;
1076       Opnd_Type : Entity_Id;
1077       Orig_Type : Entity_Id := Empty;
1078       Pack      : Entity_Id;
1079
1080       type Kind_Test is access function (E : Entity_Id) return Boolean;
1081
1082       function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1083       --  If the operand is not universal, and the operator is given by a
1084       --  expanded name,  verify that the operand has an interpretation with
1085       --  a type defined in the given scope of the operator.
1086
1087       function Type_In_P (Test : Kind_Test) return Entity_Id;
1088       --  Find a type of the given class in the package Pack that contains
1089       --  the operator.
1090
1091       ---------------------------
1092       -- Operand_Type_In_Scope --
1093       ---------------------------
1094
1095       function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1096          Nod : constant Node_Id := Right_Opnd (Op_Node);
1097          I   : Interp_Index;
1098          It  : Interp;
1099
1100       begin
1101          if not Is_Overloaded (Nod) then
1102             return Scope (Base_Type (Etype (Nod))) = S;
1103
1104          else
1105             Get_First_Interp (Nod, I, It);
1106             while Present (It.Typ) loop
1107                if Scope (Base_Type (It.Typ)) = S then
1108                   return True;
1109                end if;
1110
1111                Get_Next_Interp (I, It);
1112             end loop;
1113
1114             return False;
1115          end if;
1116       end Operand_Type_In_Scope;
1117
1118       ---------------
1119       -- Type_In_P --
1120       ---------------
1121
1122       function Type_In_P (Test : Kind_Test) return Entity_Id is
1123          E : Entity_Id;
1124
1125          function In_Decl return Boolean;
1126          --  Verify that node is not part of the type declaration for the
1127          --  candidate type, which would otherwise be invisible.
1128
1129          -------------
1130          -- In_Decl --
1131          -------------
1132
1133          function In_Decl return Boolean is
1134             Decl_Node : constant Node_Id := Parent (E);
1135             N2        : Node_Id;
1136
1137          begin
1138             N2 := N;
1139
1140             if Etype (E) = Any_Type then
1141                return True;
1142
1143             elsif No (Decl_Node) then
1144                return False;
1145
1146             else
1147                while Present (N2)
1148                  and then Nkind (N2) /= N_Compilation_Unit
1149                loop
1150                   if N2 = Decl_Node then
1151                      return True;
1152                   else
1153                      N2 := Parent (N2);
1154                   end if;
1155                end loop;
1156
1157                return False;
1158             end if;
1159          end In_Decl;
1160
1161       --  Start of processing for Type_In_P
1162
1163       begin
1164          --  If the context type is declared in the prefix package, this
1165          --  is the desired base type.
1166
1167          if Scope (Base_Type (Typ)) = Pack
1168            and then Test (Typ)
1169          then
1170             return Base_Type (Typ);
1171
1172          else
1173             E := First_Entity (Pack);
1174             while Present (E) loop
1175                if Test (E)
1176                  and then not In_Decl
1177                then
1178                   return E;
1179                end if;
1180
1181                Next_Entity (E);
1182             end loop;
1183
1184             return Empty;
1185          end if;
1186       end Type_In_P;
1187
1188    --  Start of processing for Make_Call_Into_Operator
1189
1190    begin
1191       Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1192
1193       --  Binary operator
1194
1195       if Is_Binary then
1196          Set_Left_Opnd  (Op_Node, Relocate_Node (Act1));
1197          Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1198          Save_Interps (Act1, Left_Opnd  (Op_Node));
1199          Save_Interps (Act2, Right_Opnd (Op_Node));
1200          Act1 := Left_Opnd (Op_Node);
1201          Act2 := Right_Opnd (Op_Node);
1202
1203       --  Unary operator
1204
1205       else
1206          Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1207          Save_Interps (Act1, Right_Opnd (Op_Node));
1208          Act1 := Right_Opnd (Op_Node);
1209       end if;
1210
1211       --  If the operator is denoted by an expanded name, and the prefix is
1212       --  not Standard, but the operator is a predefined one whose scope is
1213       --  Standard, then this is an implicit_operator, inserted as an
1214       --  interpretation by the procedure of the same name. This procedure
1215       --  overestimates the presence of implicit operators, because it does
1216       --  not examine the type of the operands. Verify now that the operand
1217       --  type appears in the given scope. If right operand is universal,
1218       --  check the other operand. In the case of concatenation, either
1219       --  argument can be the component type, so check the type of the result.
1220       --  If both arguments are literals, look for a type of the right kind
1221       --  defined in the given scope. This elaborate nonsense is brought to
1222       --  you courtesy of b33302a. The type itself must be frozen, so we must
1223       --  find the type of the proper class in the given scope.
1224
1225       --  A final wrinkle is the multiplication operator for fixed point
1226       --  types, which is defined in Standard only, and not in the scope of
1227       --  the fixed_point type itself.
1228
1229       if Nkind (Name (N)) = N_Expanded_Name then
1230          Pack := Entity (Prefix (Name (N)));
1231
1232          --  If the entity being called is defined in the given package,
1233          --  it is a renaming of a predefined operator, and known to be
1234          --  legal.
1235
1236          if Scope (Entity (Name (N))) = Pack
1237             and then Pack /= Standard_Standard
1238          then
1239             null;
1240
1241          --  Visibility does not need to be checked in an instance: if the
1242          --  operator was not visible in the generic it has been diagnosed
1243          --  already, else there is an implicit copy of it in the instance.
1244
1245          elsif In_Instance then
1246             null;
1247
1248          elsif (Op_Name =  Name_Op_Multiply
1249               or else Op_Name = Name_Op_Divide)
1250            and then Is_Fixed_Point_Type (Etype (Left_Opnd  (Op_Node)))
1251            and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1252          then
1253             if Pack /= Standard_Standard then
1254                Error := True;
1255             end if;
1256
1257          --  Ada 2005, AI-420:  Predefined equality on Universal_Access
1258          --  is available.
1259
1260          elsif Ada_Version >= Ada_05
1261            and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1262            and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1263          then
1264             null;
1265
1266          else
1267             Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1268
1269             if Op_Name = Name_Op_Concat then
1270                Opnd_Type := Base_Type (Typ);
1271
1272             elsif (Scope (Opnd_Type) = Standard_Standard
1273                      and then Is_Binary)
1274               or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1275                         and then Is_Binary
1276                         and then not Comes_From_Source (Opnd_Type))
1277             then
1278                Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1279             end if;
1280
1281             if Scope (Opnd_Type) = Standard_Standard then
1282
1283                --  Verify that the scope contains a type that corresponds to
1284                --  the given literal. Optimize the case where Pack is Standard.
1285
1286                if Pack /= Standard_Standard then
1287
1288                   if Opnd_Type = Universal_Integer then
1289                      Orig_Type :=  Type_In_P (Is_Integer_Type'Access);
1290
1291                   elsif Opnd_Type = Universal_Real then
1292                      Orig_Type := Type_In_P (Is_Real_Type'Access);
1293
1294                   elsif Opnd_Type = Any_String then
1295                      Orig_Type := Type_In_P (Is_String_Type'Access);
1296
1297                   elsif Opnd_Type = Any_Access then
1298                      Orig_Type :=  Type_In_P (Is_Definite_Access_Type'Access);
1299
1300                   elsif Opnd_Type = Any_Composite then
1301                      Orig_Type := Type_In_P (Is_Composite_Type'Access);
1302
1303                      if Present (Orig_Type) then
1304                         if Has_Private_Component (Orig_Type) then
1305                            Orig_Type := Empty;
1306                         else
1307                            Set_Etype (Act1, Orig_Type);
1308
1309                            if Is_Binary then
1310                               Set_Etype (Act2, Orig_Type);
1311                            end if;
1312                         end if;
1313                      end if;
1314
1315                   else
1316                      Orig_Type := Empty;
1317                   end if;
1318
1319                   Error := No (Orig_Type);
1320                end if;
1321
1322             elsif Ekind (Opnd_Type) = E_Allocator_Type
1323                and then No (Type_In_P (Is_Definite_Access_Type'Access))
1324             then
1325                Error := True;
1326
1327             --  If the type is defined elsewhere, and the operator is not
1328             --  defined in the given scope (by a renaming declaration, e.g.)
1329             --  then this is an error as well. If an extension of System is
1330             --  present, and the type may be defined there, Pack must be
1331             --  System itself.
1332
1333             elsif Scope (Opnd_Type) /= Pack
1334               and then Scope (Op_Id) /= Pack
1335               and then (No (System_Aux_Id)
1336                          or else Scope (Opnd_Type) /= System_Aux_Id
1337                          or else Pack /= Scope (System_Aux_Id))
1338             then
1339                if not Is_Overloaded (Right_Opnd (Op_Node)) then
1340                   Error := True;
1341                else
1342                   Error := not Operand_Type_In_Scope (Pack);
1343                end if;
1344
1345             elsif Pack = Standard_Standard
1346               and then not Operand_Type_In_Scope (Standard_Standard)
1347             then
1348                Error := True;
1349             end if;
1350          end if;
1351
1352          if Error then
1353             Error_Msg_Node_2 := Pack;
1354             Error_Msg_NE
1355               ("& not declared in&", N, Selector_Name (Name (N)));
1356             Set_Etype (N, Any_Type);
1357             return;
1358          end if;
1359       end if;
1360
1361       Set_Chars  (Op_Node, Op_Name);
1362
1363       if not Is_Private_Type (Etype (N)) then
1364          Set_Etype (Op_Node, Base_Type (Etype (N)));
1365       else
1366          Set_Etype (Op_Node, Etype (N));
1367       end if;
1368
1369       --  If this is a call to a function that renames a predefined equality,
1370       --  the renaming declaration provides a type that must be used to
1371       --  resolve the operands. This must be done now because resolution of
1372       --  the equality node will not resolve any remaining ambiguity, and it
1373       --  assumes that the first operand is not overloaded.
1374
1375       if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1376         and then Ekind (Func) = E_Function
1377         and then Is_Overloaded (Act1)
1378       then
1379          Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1380          Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1381       end if;
1382
1383       Set_Entity (Op_Node, Op_Id);
1384       Generate_Reference (Op_Id, N, ' ');
1385
1386       --  Do rewrite setting Comes_From_Source on the result if the original
1387       --  call came from source. Although it is not strictly the case that the
1388       --  operator as such comes from the source, logically it corresponds
1389       --  exactly to the function call in the source, so it should be marked
1390       --  this way (e.g. to make sure that validity checks work fine).
1391
1392       declare
1393          CS : constant Boolean := Comes_From_Source (N);
1394       begin
1395          Rewrite (N, Op_Node);
1396          Set_Comes_From_Source (N, CS);
1397       end;
1398
1399       --  If this is an arithmetic operator and the result type is private,
1400       --  the operands and the result must be wrapped in conversion to
1401       --  expose the underlying numeric type and expand the proper checks,
1402       --  e.g. on division.
1403
1404       if Is_Private_Type (Typ) then
1405          case Nkind (N) is
1406             when N_Op_Add  | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1407             N_Op_Expon     | N_Op_Mod      | N_Op_Rem      =>
1408                Resolve_Intrinsic_Operator (N, Typ);
1409
1410             when N_Op_Plus | N_Op_Minus    | N_Op_Abs      =>
1411                Resolve_Intrinsic_Unary_Operator (N, Typ);
1412
1413             when others =>
1414                Resolve (N, Typ);
1415          end case;
1416       else
1417          Resolve (N, Typ);
1418       end if;
1419
1420       --  For predefined operators on literals, the operation freezes
1421       --  their type.
1422
1423       if Present (Orig_Type) then
1424          Set_Etype (Act1, Orig_Type);
1425          Freeze_Expression (Act1);
1426       end if;
1427    end Make_Call_Into_Operator;
1428
1429    -------------------
1430    -- Operator_Kind --
1431    -------------------
1432
1433    function Operator_Kind
1434      (Op_Name   : Name_Id;
1435       Is_Binary : Boolean) return Node_Kind
1436    is
1437       Kind : Node_Kind;
1438
1439    begin
1440       if Is_Binary then
1441          if    Op_Name =  Name_Op_And      then
1442             Kind := N_Op_And;
1443          elsif Op_Name =  Name_Op_Or       then
1444             Kind := N_Op_Or;
1445          elsif Op_Name =  Name_Op_Xor      then
1446             Kind := N_Op_Xor;
1447          elsif Op_Name =  Name_Op_Eq       then
1448             Kind := N_Op_Eq;
1449          elsif Op_Name =  Name_Op_Ne       then
1450             Kind := N_Op_Ne;
1451          elsif Op_Name =  Name_Op_Lt       then
1452             Kind := N_Op_Lt;
1453          elsif Op_Name =  Name_Op_Le       then
1454             Kind := N_Op_Le;
1455          elsif Op_Name =  Name_Op_Gt       then
1456             Kind := N_Op_Gt;
1457          elsif Op_Name =  Name_Op_Ge       then
1458             Kind := N_Op_Ge;
1459          elsif Op_Name =  Name_Op_Add      then
1460             Kind := N_Op_Add;
1461          elsif Op_Name =  Name_Op_Subtract then
1462             Kind := N_Op_Subtract;
1463          elsif Op_Name =  Name_Op_Concat   then
1464             Kind := N_Op_Concat;
1465          elsif Op_Name =  Name_Op_Multiply then
1466             Kind := N_Op_Multiply;
1467          elsif Op_Name =  Name_Op_Divide   then
1468             Kind := N_Op_Divide;
1469          elsif Op_Name =  Name_Op_Mod      then
1470             Kind := N_Op_Mod;
1471          elsif Op_Name =  Name_Op_Rem      then
1472             Kind := N_Op_Rem;
1473          elsif Op_Name =  Name_Op_Expon    then
1474             Kind := N_Op_Expon;
1475          else
1476             raise Program_Error;
1477          end if;
1478
1479       --  Unary operators
1480
1481       else
1482          if    Op_Name =  Name_Op_Add      then
1483             Kind := N_Op_Plus;
1484          elsif Op_Name =  Name_Op_Subtract then
1485             Kind := N_Op_Minus;
1486          elsif Op_Name =  Name_Op_Abs      then
1487             Kind := N_Op_Abs;
1488          elsif Op_Name =  Name_Op_Not      then
1489             Kind := N_Op_Not;
1490          else
1491             raise Program_Error;
1492          end if;
1493       end if;
1494
1495       return Kind;
1496    end Operator_Kind;
1497
1498    ----------------------------
1499    -- Preanalyze_And_Resolve --
1500    ----------------------------
1501
1502    procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1503       Save_Full_Analysis : constant Boolean := Full_Analysis;
1504
1505    begin
1506       Full_Analysis := False;
1507       Expander_Mode_Save_And_Set (False);
1508
1509       --  We suppress all checks for this analysis, since the checks will
1510       --  be applied properly, and in the right location, when the default
1511       --  expression is reanalyzed and reexpanded later on.
1512
1513       Analyze_And_Resolve (N, T, Suppress => All_Checks);
1514
1515       Expander_Mode_Restore;
1516       Full_Analysis := Save_Full_Analysis;
1517    end Preanalyze_And_Resolve;
1518
1519    --  Version without context type
1520
1521    procedure Preanalyze_And_Resolve (N : Node_Id) is
1522       Save_Full_Analysis : constant Boolean := Full_Analysis;
1523
1524    begin
1525       Full_Analysis := False;
1526       Expander_Mode_Save_And_Set (False);
1527
1528       Analyze (N);
1529       Resolve (N, Etype (N), Suppress => All_Checks);
1530
1531       Expander_Mode_Restore;
1532       Full_Analysis := Save_Full_Analysis;
1533    end Preanalyze_And_Resolve;
1534
1535    ----------------------------------
1536    -- Replace_Actual_Discriminants --
1537    ----------------------------------
1538
1539    procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1540       Loc : constant Source_Ptr := Sloc (N);
1541       Tsk : Node_Id := Empty;
1542
1543       function Process_Discr (Nod : Node_Id) return Traverse_Result;
1544
1545       -------------------
1546       -- Process_Discr --
1547       -------------------
1548
1549       function Process_Discr (Nod : Node_Id) return Traverse_Result is
1550          Ent : Entity_Id;
1551
1552       begin
1553          if Nkind (Nod) = N_Identifier then
1554             Ent := Entity (Nod);
1555
1556             if Present (Ent)
1557               and then Ekind (Ent) = E_Discriminant
1558             then
1559                Rewrite (Nod,
1560                  Make_Selected_Component (Loc,
1561                    Prefix        => New_Copy_Tree (Tsk, New_Sloc => Loc),
1562                    Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1563
1564                Set_Etype (Nod, Etype (Ent));
1565             end if;
1566
1567          end if;
1568
1569          return OK;
1570       end Process_Discr;
1571
1572       procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1573
1574    --  Start of processing for Replace_Actual_Discriminants
1575
1576    begin
1577       if not Expander_Active then
1578          return;
1579       end if;
1580
1581       if Nkind (Name (N)) = N_Selected_Component then
1582          Tsk := Prefix (Name (N));
1583
1584       elsif Nkind (Name (N)) = N_Indexed_Component then
1585          Tsk := Prefix (Prefix (Name (N)));
1586       end if;
1587
1588       if No (Tsk) then
1589          return;
1590       else
1591          Replace_Discrs (Default);
1592       end if;
1593    end Replace_Actual_Discriminants;
1594
1595    -------------
1596    -- Resolve --
1597    -------------
1598
1599    procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1600       Ambiguous : Boolean   := False;
1601       Ctx_Type  : Entity_Id := Typ;
1602       Expr_Type : Entity_Id := Empty; -- prevent junk warning
1603       Err_Type  : Entity_Id := Empty;
1604       Found     : Boolean   := False;
1605       From_Lib  : Boolean;
1606       I         : Interp_Index;
1607       I1        : Interp_Index := 0;  -- prevent junk warning
1608       It        : Interp;
1609       It1       : Interp;
1610       Seen      : Entity_Id := Empty; -- prevent junk warning
1611
1612       function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1613       --  Determine whether a node comes from a predefined library unit or
1614       --  Standard.
1615
1616       procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1617       --  Try and fix up a literal so that it matches its expected type. New
1618       --  literals are manufactured if necessary to avoid cascaded errors.
1619
1620       procedure Resolution_Failed;
1621       --  Called when attempt at resolving current expression fails
1622
1623       ------------------------------------
1624       -- Comes_From_Predefined_Lib_Unit --
1625       -------------------------------------
1626
1627       function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1628       begin
1629          return
1630            Sloc (Nod) = Standard_Location
1631              or else Is_Predefined_File_Name (Unit_File_Name (
1632                        Get_Source_Unit (Sloc (Nod))));
1633       end Comes_From_Predefined_Lib_Unit;
1634
1635       --------------------
1636       -- Patch_Up_Value --
1637       --------------------
1638
1639       procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1640       begin
1641          if Nkind (N) = N_Integer_Literal
1642            and then Is_Real_Type (Typ)
1643          then
1644             Rewrite (N,
1645               Make_Real_Literal (Sloc (N),
1646                 Realval => UR_From_Uint (Intval (N))));
1647             Set_Etype (N, Universal_Real);
1648             Set_Is_Static_Expression (N);
1649
1650          elsif Nkind (N) = N_Real_Literal
1651            and then Is_Integer_Type (Typ)
1652          then
1653             Rewrite (N,
1654               Make_Integer_Literal (Sloc (N),
1655                 Intval => UR_To_Uint (Realval (N))));
1656             Set_Etype (N, Universal_Integer);
1657             Set_Is_Static_Expression (N);
1658
1659          elsif Nkind (N) = N_String_Literal
1660            and then Is_Character_Type (Typ)
1661          then
1662             Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1663             Rewrite (N,
1664               Make_Character_Literal (Sloc (N),
1665                 Chars => Name_Find,
1666                 Char_Literal_Value =>
1667                   UI_From_Int (Character'Pos ('A'))));
1668             Set_Etype (N, Any_Character);
1669             Set_Is_Static_Expression (N);
1670
1671          elsif Nkind (N) /= N_String_Literal
1672            and then Is_String_Type (Typ)
1673          then
1674             Rewrite (N,
1675               Make_String_Literal (Sloc (N),
1676                 Strval => End_String));
1677
1678          elsif Nkind (N) = N_Range then
1679             Patch_Up_Value (Low_Bound (N), Typ);
1680             Patch_Up_Value (High_Bound (N), Typ);
1681          end if;
1682       end Patch_Up_Value;
1683
1684       -----------------------
1685       -- Resolution_Failed --
1686       -----------------------
1687
1688       procedure Resolution_Failed is
1689       begin
1690          Patch_Up_Value (N, Typ);
1691          Set_Etype (N, Typ);
1692          Debug_A_Exit ("resolving  ", N, " (done, resolution failed)");
1693          Set_Is_Overloaded (N, False);
1694
1695          --  The caller will return without calling the expander, so we need
1696          --  to set the analyzed flag. Note that it is fine to set Analyzed
1697          --  to True even if we are in the middle of a shallow analysis,
1698          --  (see the spec of sem for more details) since this is an error
1699          --  situation anyway, and there is no point in repeating the
1700          --  analysis later (indeed it won't work to repeat it later, since
1701          --  we haven't got a clear resolution of which entity is being
1702          --  referenced.)
1703
1704          Set_Analyzed (N, True);
1705          return;
1706       end Resolution_Failed;
1707
1708    --  Start of processing for Resolve
1709
1710    begin
1711       if N = Error then
1712          return;
1713       end if;
1714
1715       --  Access attribute on remote subprogram cannot be used for
1716       --  a non-remote access-to-subprogram type.
1717
1718       if Nkind (N) = N_Attribute_Reference
1719         and then (Attribute_Name (N) = Name_Access
1720                     or else Attribute_Name (N) = Name_Unrestricted_Access
1721                     or else Attribute_Name (N) = Name_Unchecked_Access)
1722         and then Comes_From_Source (N)
1723         and then Is_Entity_Name (Prefix (N))
1724         and then Is_Subprogram (Entity (Prefix (N)))
1725         and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1726         and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1727       then
1728          Error_Msg_N
1729            ("prefix must statically denote a non-remote subprogram", N);
1730       end if;
1731
1732       From_Lib := Comes_From_Predefined_Lib_Unit (N);
1733
1734       --  If the context is a Remote_Access_To_Subprogram, access attributes
1735       --  must be resolved with the corresponding fat pointer. There is no need
1736       --  to check for the attribute name since the return type of an
1737       --  attribute is never a remote type.
1738
1739       if Nkind (N) = N_Attribute_Reference
1740         and then Comes_From_Source (N)
1741         and then (Is_Remote_Call_Interface (Typ)
1742                     or else Is_Remote_Types (Typ))
1743       then
1744          declare
1745             Attr      : constant Attribute_Id :=
1746                           Get_Attribute_Id (Attribute_Name (N));
1747             Pref      : constant Node_Id      := Prefix (N);
1748             Decl      : Node_Id;
1749             Spec      : Node_Id;
1750             Is_Remote : Boolean := True;
1751
1752          begin
1753             --  Check that Typ is a remote access-to-subprogram type
1754
1755             if Is_Remote_Access_To_Subprogram_Type (Typ) then
1756                --  Prefix (N) must statically denote a remote subprogram
1757                --  declared in a package specification.
1758
1759                if Attr = Attribute_Access then
1760                   Decl := Unit_Declaration_Node (Entity (Pref));
1761
1762                   if Nkind (Decl) = N_Subprogram_Body then
1763                      Spec := Corresponding_Spec (Decl);
1764
1765                      if not No (Spec) then
1766                         Decl := Unit_Declaration_Node (Spec);
1767                      end if;
1768                   end if;
1769
1770                   Spec := Parent (Decl);
1771
1772                   if not Is_Entity_Name (Prefix (N))
1773                     or else Nkind (Spec) /= N_Package_Specification
1774                     or else
1775                       not Is_Remote_Call_Interface (Defining_Entity (Spec))
1776                   then
1777                      Is_Remote := False;
1778                      Error_Msg_N
1779                        ("prefix must statically denote a remote subprogram ",
1780                         N);
1781                   end if;
1782                end if;
1783
1784                --   If we are generating code for a distributed program.
1785                --   perform semantic checks against the corresponding
1786                --   remote entities.
1787
1788                if (Attr = Attribute_Access
1789                     or else Attr = Attribute_Unchecked_Access
1790                     or else Attr = Attribute_Unrestricted_Access)
1791                  and then Expander_Active
1792                  and then Get_PCS_Name /= Name_No_DSA
1793                then
1794                   Check_Subtype_Conformant
1795                     (New_Id  => Entity (Prefix (N)),
1796                      Old_Id  => Designated_Type
1797                        (Corresponding_Remote_Type (Typ)),
1798                      Err_Loc => N);
1799
1800                   if Is_Remote then
1801                      Process_Remote_AST_Attribute (N, Typ);
1802                   end if;
1803                end if;
1804             end if;
1805          end;
1806       end if;
1807
1808       Debug_A_Entry ("resolving  ", N);
1809
1810       if Comes_From_Source (N) then
1811          if Is_Fixed_Point_Type (Typ) then
1812             Check_Restriction (No_Fixed_Point, N);
1813
1814          elsif Is_Floating_Point_Type (Typ)
1815            and then Typ /= Universal_Real
1816            and then Typ /= Any_Real
1817          then
1818             Check_Restriction (No_Floating_Point, N);
1819          end if;
1820       end if;
1821
1822       --  Return if already analyzed
1823
1824       if Analyzed (N) then
1825          Debug_A_Exit ("resolving  ", N, "  (done, already analyzed)");
1826          return;
1827
1828       --  Return if type = Any_Type (previous error encountered)
1829
1830       elsif Etype (N) = Any_Type then
1831          Debug_A_Exit ("resolving  ", N, "  (done, Etype = Any_Type)");
1832          return;
1833       end if;
1834
1835       Check_Parameterless_Call (N);
1836
1837       --  If not overloaded, then we know the type, and all that needs doing
1838       --  is to check that this type is compatible with the context.
1839
1840       if not Is_Overloaded (N) then
1841          Found := Covers (Typ, Etype (N));
1842          Expr_Type := Etype (N);
1843
1844       --  In the overloaded case, we must select the interpretation that
1845       --  is compatible with the context (i.e. the type passed to Resolve)
1846
1847       else
1848          --  Loop through possible interpretations
1849
1850          Get_First_Interp (N, I, It);
1851          Interp_Loop : while Present (It.Typ) loop
1852
1853             --  We are only interested in interpretations that are compatible
1854             --  with the expected type, any other interpretations are ignored.
1855
1856             if not Covers (Typ, It.Typ) then
1857                if Debug_Flag_V then
1858                   Write_Str ("    interpretation incompatible with context");
1859                   Write_Eol;
1860                end if;
1861
1862             else
1863                --  Skip the current interpretation if it is disabled by an
1864                --  abstract operator. This action is performed only when the
1865                --  type against which we are resolving is the same as the
1866                --  type of the interpretation.
1867
1868                if Ada_Version >= Ada_05
1869                  and then It.Typ = Typ
1870                  and then Typ /= Universal_Integer
1871                  and then Typ /= Universal_Real
1872                  and then Present (It.Abstract_Op)
1873                then
1874                   goto Continue;
1875                end if;
1876
1877                --  First matching interpretation
1878
1879                if not Found then
1880                   Found := True;
1881                   I1    := I;
1882                   Seen  := It.Nam;
1883                   Expr_Type := It.Typ;
1884
1885                --  Matching interpretation that is not the first, maybe an
1886                --  error, but there are some cases where preference rules are
1887                --  used to choose between the two possibilities. These and
1888                --  some more obscure cases are handled in Disambiguate.
1889
1890                else
1891                   --  If the current statement is part of a predefined library
1892                   --  unit, then all interpretations which come from user level
1893                   --  packages should not be considered.
1894
1895                   if From_Lib
1896                     and then not Comes_From_Predefined_Lib_Unit (It.Nam)
1897                   then
1898                      goto Continue;
1899                   end if;
1900
1901                   Error_Msg_Sloc := Sloc (Seen);
1902                   It1 := Disambiguate (N, I1, I, Typ);
1903
1904                   --  Disambiguation has succeeded. Skip the remaining
1905                   --  interpretations.
1906
1907                   if It1 /= No_Interp then
1908                      Seen := It1.Nam;
1909                      Expr_Type := It1.Typ;
1910
1911                      while Present (It.Typ) loop
1912                         Get_Next_Interp (I, It);
1913                      end loop;
1914
1915                   else
1916                      --  Before we issue an ambiguity complaint, check for
1917                      --  the case of a subprogram call where at least one
1918                      --  of the arguments is Any_Type, and if so, suppress
1919                      --  the message, since it is a cascaded error.
1920
1921                      if Nkind_In (N, N_Function_Call,
1922                                      N_Procedure_Call_Statement)
1923                      then
1924                         declare
1925                            A : Node_Id;
1926                            E : Node_Id;
1927
1928                         begin
1929                            A := First_Actual (N);
1930                            while Present (A) loop
1931                               E := A;
1932
1933                               if Nkind (E) = N_Parameter_Association then
1934                                  E := Explicit_Actual_Parameter (E);
1935                               end if;
1936
1937                               if Etype (E) = Any_Type then
1938                                  if Debug_Flag_V then
1939                                     Write_Str ("Any_Type in call");
1940                                     Write_Eol;
1941                                  end if;
1942
1943                                  exit Interp_Loop;
1944                               end if;
1945
1946                               Next_Actual (A);
1947                            end loop;
1948                         end;
1949
1950                      elsif Nkind (N) in N_Binary_Op
1951                        and then (Etype (Left_Opnd (N)) = Any_Type
1952                                   or else Etype (Right_Opnd (N)) = Any_Type)
1953                      then
1954                         exit Interp_Loop;
1955
1956                      elsif Nkind (N) in  N_Unary_Op
1957                        and then Etype (Right_Opnd (N)) = Any_Type
1958                      then
1959                         exit Interp_Loop;
1960                      end if;
1961
1962                      --  Not that special case, so issue message using the
1963                      --  flag Ambiguous to control printing of the header
1964                      --  message only at the start of an ambiguous set.
1965
1966                      if not Ambiguous then
1967                         if Nkind (N) = N_Function_Call
1968                           and then Nkind (Name (N)) = N_Explicit_Dereference
1969                         then
1970                            Error_Msg_N
1971                              ("ambiguous expression "
1972                                & "(cannot resolve indirect call)!", N);
1973                         else
1974                            Error_Msg_NE
1975                              ("ambiguous expression (cannot resolve&)!",
1976                               N, It.Nam);
1977                         end if;
1978
1979                         Ambiguous := True;
1980
1981                         if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
1982                            Error_Msg_N
1983                              ("\\possible interpretation (inherited)#!", N);
1984                         else
1985                            Error_Msg_N ("\\possible interpretation#!", N);
1986                         end if;
1987                      end if;
1988
1989                      Error_Msg_Sloc := Sloc (It.Nam);
1990
1991                      --  By default, the error message refers to the candidate
1992                      --  interpretation. But if it is a predefined operator, it
1993                      --  is implicitly declared at the declaration of the type
1994                      --  of the operand. Recover the sloc of that declaration
1995                      --  for the error message.
1996
1997                      if Nkind (N) in N_Op
1998                        and then Scope (It.Nam) = Standard_Standard
1999                        and then not Is_Overloaded (Right_Opnd (N))
2000                        and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2001                                                              Standard_Standard
2002                      then
2003                         Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2004
2005                         if Comes_From_Source (Err_Type)
2006                           and then Present (Parent (Err_Type))
2007                         then
2008                            Error_Msg_Sloc := Sloc (Parent (Err_Type));
2009                         end if;
2010
2011                      elsif Nkind (N) in N_Binary_Op
2012                        and then Scope (It.Nam) = Standard_Standard
2013                        and then not Is_Overloaded (Left_Opnd (N))
2014                        and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2015                                                              Standard_Standard
2016                      then
2017                         Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2018
2019                         if Comes_From_Source (Err_Type)
2020                           and then Present (Parent (Err_Type))
2021                         then
2022                            Error_Msg_Sloc := Sloc (Parent (Err_Type));
2023                         end if;
2024
2025                      --  If this is an indirect call, use the subprogram_type
2026                      --  in the message, to have a meaningful location.
2027                      --  Indicate as well if this is an inherited operation,
2028                      --  created by a type declaration.
2029
2030                      elsif Nkind (N) = N_Function_Call
2031                        and then Nkind (Name (N)) = N_Explicit_Dereference
2032                        and then Is_Type (It.Nam)
2033                      then
2034                         Err_Type := It.Nam;
2035                         Error_Msg_Sloc :=
2036                           Sloc (Associated_Node_For_Itype (Err_Type));
2037                      else
2038                         Err_Type := Empty;
2039                      end if;
2040
2041                      if Nkind (N) in N_Op
2042                        and then Scope (It.Nam) = Standard_Standard
2043                        and then Present (Err_Type)
2044                      then
2045                         --  Special-case the message for universal_fixed
2046                         --  operators, which are not declared with the type
2047                         --  of the operand, but appear forever in Standard.
2048
2049                         if  It.Typ = Universal_Fixed
2050                           and then Scope (It.Nam) = Standard_Standard
2051                         then
2052                            Error_Msg_N
2053                              ("\\possible interpretation as " &
2054                                 "universal_fixed operation " &
2055                                   "(RM 4.5.5 (19))", N);
2056                         else
2057                            Error_Msg_N
2058                              ("\\possible interpretation (predefined)#!", N);
2059                         end if;
2060
2061                      elsif
2062                        Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2063                      then
2064                         Error_Msg_N
2065                           ("\\possible interpretation (inherited)#!", N);
2066                      else
2067                         Error_Msg_N ("\\possible interpretation#!", N);
2068                      end if;
2069
2070                   end if;
2071                end if;
2072
2073                --  We have a matching interpretation, Expr_Type is the type
2074                --  from this interpretation, and Seen is the entity.
2075
2076                --  For an operator, just set the entity name. The type will be
2077                --  set by the specific operator resolution routine.
2078
2079                if Nkind (N) in N_Op then
2080                   Set_Entity (N, Seen);
2081                   Generate_Reference (Seen, N);
2082
2083                elsif Nkind (N) = N_Character_Literal then
2084                   Set_Etype (N, Expr_Type);
2085
2086                --  For an explicit dereference, attribute reference, range,
2087                --  short-circuit form (which is not an operator node), or call
2088                --  with a name that is an explicit dereference, there is
2089                --  nothing to be done at this point.
2090
2091                elsif Nkind_In (N, N_Explicit_Dereference,
2092                                   N_Attribute_Reference,
2093                                   N_And_Then,
2094                                   N_Indexed_Component,
2095                                   N_Or_Else,
2096                                   N_Range,
2097                                   N_Selected_Component,
2098                                   N_Slice)
2099                  or else Nkind (Name (N)) = N_Explicit_Dereference
2100                then
2101                   null;
2102
2103                --  For procedure or function calls, set the type of the name,
2104                --  and also the entity pointer for the prefix
2105
2106                elsif Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call)
2107                  and then (Is_Entity_Name (Name (N))
2108                             or else Nkind (Name (N)) = N_Operator_Symbol)
2109                then
2110                   Set_Etype  (Name (N), Expr_Type);
2111                   Set_Entity (Name (N), Seen);
2112                   Generate_Reference (Seen, Name (N));
2113
2114                elsif Nkind (N) = N_Function_Call
2115                  and then Nkind (Name (N)) = N_Selected_Component
2116                then
2117                   Set_Etype (Name (N), Expr_Type);
2118                   Set_Entity (Selector_Name (Name (N)), Seen);
2119                   Generate_Reference (Seen, Selector_Name (Name (N)));
2120
2121                --  For all other cases, just set the type of the Name
2122
2123                else
2124                   Set_Etype (Name (N), Expr_Type);
2125                end if;
2126
2127             end if;
2128
2129             <<Continue>>
2130
2131             --  Move to next interpretation
2132
2133             exit Interp_Loop when No (It.Typ);
2134
2135             Get_Next_Interp (I, It);
2136          end loop Interp_Loop;
2137       end if;
2138
2139       --  At this stage Found indicates whether or not an acceptable
2140       --  interpretation exists. If not, then we have an error, except
2141       --  that if the context is Any_Type as a result of some other error,
2142       --  then we suppress the error report.
2143
2144       if not Found then
2145          if Typ /= Any_Type then
2146
2147             --  If type we are looking for is Void, then this is the procedure
2148             --  call case, and the error is simply that what we gave is not a
2149             --  procedure name (we think of procedure calls as expressions with
2150             --  types internally, but the user doesn't think of them this way!)
2151
2152             if Typ = Standard_Void_Type then
2153
2154                --  Special case message if function used as a procedure
2155
2156                if Nkind (N) = N_Procedure_Call_Statement
2157                  and then Is_Entity_Name (Name (N))
2158                  and then Ekind (Entity (Name (N))) = E_Function
2159                then
2160                   Error_Msg_NE
2161                     ("cannot use function & in a procedure call",
2162                      Name (N), Entity (Name (N)));
2163
2164                --  Otherwise give general message (not clear what cases this
2165                --  covers, but no harm in providing for them!)
2166
2167                else
2168                   Error_Msg_N ("expect procedure name in procedure call", N);
2169                end if;
2170
2171                Found := True;
2172
2173             --  Otherwise we do have a subexpression with the wrong type
2174
2175             --  Check for the case of an allocator which uses an access type
2176             --  instead of the designated type. This is a common error and we
2177             --  specialize the message, posting an error on the operand of the
2178             --  allocator, complaining that we expected the designated type of
2179             --  the allocator.
2180
2181             elsif Nkind (N) = N_Allocator
2182               and then Ekind (Typ) in Access_Kind
2183               and then Ekind (Etype (N)) in Access_Kind
2184               and then Designated_Type (Etype (N)) = Typ
2185             then
2186                Wrong_Type (Expression (N), Designated_Type (Typ));
2187                Found := True;
2188
2189             --  Check for view mismatch on Null in instances, for which the
2190             --  view-swapping mechanism has no identifier.
2191
2192             elsif (In_Instance or else In_Inlined_Body)
2193               and then (Nkind (N) = N_Null)
2194               and then Is_Private_Type (Typ)
2195               and then Is_Access_Type (Full_View (Typ))
2196             then
2197                Resolve (N, Full_View (Typ));
2198                Set_Etype (N, Typ);
2199                return;
2200
2201             --  Check for an aggregate. Sometimes we can get bogus aggregates
2202             --  from misuse of parentheses, and we are about to complain about
2203             --  the aggregate without even looking inside it.
2204
2205             --  Instead, if we have an aggregate of type Any_Composite, then
2206             --  analyze and resolve the component fields, and then only issue
2207             --  another message if we get no errors doing this (otherwise
2208             --  assume that the errors in the aggregate caused the problem).
2209
2210             elsif Nkind (N) = N_Aggregate
2211               and then Etype (N) = Any_Composite
2212             then
2213                --  Disable expansion in any case. If there is a type mismatch
2214                --  it may be fatal to try to expand the aggregate. The flag
2215                --  would otherwise be set to false when the error is posted.
2216
2217                Expander_Active := False;
2218
2219                declare
2220                   procedure Check_Aggr (Aggr : Node_Id);
2221                   --  Check one aggregate, and set Found to True if we have a
2222                   --  definite error in any of its elements
2223
2224                   procedure Check_Elmt (Aelmt : Node_Id);
2225                   --  Check one element of aggregate and set Found to True if
2226                   --  we definitely have an error in the element.
2227
2228                   ----------------
2229                   -- Check_Aggr --
2230                   ----------------
2231
2232                   procedure Check_Aggr (Aggr : Node_Id) is
2233                      Elmt : Node_Id;
2234
2235                   begin
2236                      if Present (Expressions (Aggr)) then
2237                         Elmt := First (Expressions (Aggr));
2238                         while Present (Elmt) loop
2239                            Check_Elmt (Elmt);
2240                            Next (Elmt);
2241                         end loop;
2242                      end if;
2243
2244                      if Present (Component_Associations (Aggr)) then
2245                         Elmt := First (Component_Associations (Aggr));
2246                         while Present (Elmt) loop
2247
2248                            --  If this is a default-initialized component, then
2249                            --  there is nothing to check. The box will be
2250                            --  replaced by the appropriate call during late
2251                            --  expansion.
2252
2253                            if not Box_Present (Elmt) then
2254                               Check_Elmt (Expression (Elmt));
2255                            end if;
2256
2257                            Next (Elmt);
2258                         end loop;
2259                      end if;
2260                   end Check_Aggr;
2261
2262                   ----------------
2263                   -- Check_Elmt --
2264                   ----------------
2265
2266                   procedure Check_Elmt (Aelmt : Node_Id) is
2267                   begin
2268                      --  If we have a nested aggregate, go inside it (to
2269                      --  attempt a naked analyze-resolve of the aggregate
2270                      --  can cause undesirable cascaded errors). Do not
2271                      --  resolve expression if it needs a type from context,
2272                      --  as for integer * fixed expression.
2273
2274                      if Nkind (Aelmt) = N_Aggregate then
2275                         Check_Aggr (Aelmt);
2276
2277                      else
2278                         Analyze (Aelmt);
2279
2280                         if not Is_Overloaded (Aelmt)
2281                           and then Etype (Aelmt) /= Any_Fixed
2282                         then
2283                            Resolve (Aelmt);
2284                         end if;
2285
2286                         if Etype (Aelmt) = Any_Type then
2287                            Found := True;
2288                         end if;
2289                      end if;
2290                   end Check_Elmt;
2291
2292                begin
2293                   Check_Aggr (N);
2294                end;
2295             end if;
2296
2297             --  If an error message was issued already, Found got reset
2298             --  to True, so if it is still False, issue the standard
2299             --  Wrong_Type message.
2300
2301             if not Found then
2302                if Is_Overloaded (N)
2303                  and then Nkind (N) = N_Function_Call
2304                then
2305                   declare
2306                      Subp_Name : Node_Id;
2307                   begin
2308                      if Is_Entity_Name (Name (N)) then
2309                         Subp_Name := Name (N);
2310
2311                      elsif Nkind (Name (N)) = N_Selected_Component then
2312
2313                         --  Protected operation: retrieve operation name
2314
2315                         Subp_Name := Selector_Name (Name (N));
2316                      else
2317                         raise Program_Error;
2318                      end if;
2319
2320                      Error_Msg_Node_2 := Typ;
2321                      Error_Msg_NE ("no visible interpretation of&" &
2322                        " matches expected type&", N, Subp_Name);
2323                   end;
2324
2325                   if All_Errors_Mode then
2326                      declare
2327                         Index : Interp_Index;
2328                         It    : Interp;
2329
2330                      begin
2331                         Error_Msg_N ("\\possible interpretations:", N);
2332
2333                         Get_First_Interp (Name (N), Index, It);
2334                         while Present (It.Nam) loop
2335                            Error_Msg_Sloc := Sloc (It.Nam);
2336                            Error_Msg_Node_2 := It.Nam;
2337                            Error_Msg_NE
2338                              ("\\  type& for & declared#", N, It.Typ);
2339                            Get_Next_Interp (Index, It);
2340                         end loop;
2341                      end;
2342
2343                   else
2344                      Error_Msg_N ("\use -gnatf for details", N);
2345                   end if;
2346                else
2347                   Wrong_Type (N, Typ);
2348                end if;
2349             end if;
2350          end if;
2351
2352          Resolution_Failed;
2353          return;
2354
2355       --  Test if we have more than one interpretation for the context
2356
2357       elsif Ambiguous then
2358          Resolution_Failed;
2359          return;
2360
2361       --  Here we have an acceptable interpretation for the context
2362
2363       else
2364          --  Propagate type information and normalize tree for various
2365          --  predefined operations. If the context only imposes a class of
2366          --  types, rather than a specific type, propagate the actual type
2367          --  downward.
2368
2369          if Typ = Any_Integer
2370            or else Typ = Any_Boolean
2371            or else Typ = Any_Modular
2372            or else Typ = Any_Real
2373            or else Typ = Any_Discrete
2374          then
2375             Ctx_Type := Expr_Type;
2376
2377             --  Any_Fixed is legal in a real context only if a specific
2378             --  fixed point type is imposed. If Norman Cohen can be
2379             --  confused by this, it deserves a separate message.
2380
2381             if Typ = Any_Real
2382               and then Expr_Type = Any_Fixed
2383             then
2384                Error_Msg_N ("illegal context for mixed mode operation", N);
2385                Set_Etype (N, Universal_Real);
2386                Ctx_Type := Universal_Real;
2387             end if;
2388          end if;
2389
2390          --  A user-defined operator is transformed into a function call at
2391          --  this point, so that further processing knows that operators are
2392          --  really operators (i.e. are predefined operators). User-defined
2393          --  operators that are intrinsic are just renamings of the predefined
2394          --  ones, and need not be turned into calls either, but if they rename
2395          --  a different operator, we must transform the node accordingly.
2396          --  Instantiations of Unchecked_Conversion are intrinsic but are
2397          --  treated as functions, even if given an operator designator.
2398
2399          if Nkind (N) in N_Op
2400            and then Present (Entity (N))
2401            and then Ekind (Entity (N)) /= E_Operator
2402          then
2403
2404             if not Is_Predefined_Op (Entity (N)) then
2405                Rewrite_Operator_As_Call (N, Entity (N));
2406
2407             elsif Present (Alias (Entity (N)))
2408               and then
2409                 Nkind (Parent (Parent (Entity (N)))) =
2410                                     N_Subprogram_Renaming_Declaration
2411             then
2412                Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2413
2414                --  If the node is rewritten, it will be fully resolved in
2415                --  Rewrite_Renamed_Operator.
2416
2417                if Analyzed (N) then
2418                   return;
2419                end if;
2420             end if;
2421          end if;
2422
2423          case N_Subexpr'(Nkind (N)) is
2424
2425             when N_Aggregate => Resolve_Aggregate                (N, Ctx_Type);
2426
2427             when N_Allocator => Resolve_Allocator                (N, Ctx_Type);
2428
2429             when N_And_Then | N_Or_Else
2430                              => Resolve_Short_Circuit            (N, Ctx_Type);
2431
2432             when N_Attribute_Reference
2433                              => Resolve_Attribute                (N, Ctx_Type);
2434
2435             when N_Character_Literal
2436                              => Resolve_Character_Literal        (N, Ctx_Type);
2437
2438             when N_Conditional_Expression
2439                              => Resolve_Conditional_Expression   (N, Ctx_Type);
2440
2441             when N_Expanded_Name
2442                              => Resolve_Entity_Name              (N, Ctx_Type);
2443
2444             when N_Extension_Aggregate
2445                              => Resolve_Extension_Aggregate      (N, Ctx_Type);
2446
2447             when N_Explicit_Dereference
2448                              => Resolve_Explicit_Dereference     (N, Ctx_Type);
2449
2450             when N_Function_Call
2451                              => Resolve_Call                     (N, Ctx_Type);
2452
2453             when N_Identifier
2454                              => Resolve_Entity_Name              (N, Ctx_Type);
2455
2456             when N_Indexed_Component
2457                              => Resolve_Indexed_Component        (N, Ctx_Type);
2458
2459             when N_Integer_Literal
2460                              => Resolve_Integer_Literal          (N, Ctx_Type);
2461
2462             when N_Membership_Test
2463                              => Resolve_Membership_Op            (N, Ctx_Type);
2464
2465             when N_Null      => Resolve_Null                     (N, Ctx_Type);
2466
2467             when N_Op_And | N_Op_Or | N_Op_Xor
2468                              => Resolve_Logical_Op               (N, Ctx_Type);
2469
2470             when N_Op_Eq | N_Op_Ne
2471                              => Resolve_Equality_Op              (N, Ctx_Type);
2472
2473             when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2474                              => Resolve_Comparison_Op            (N, Ctx_Type);
2475
2476             when N_Op_Not    => Resolve_Op_Not                   (N, Ctx_Type);
2477
2478             when N_Op_Add    | N_Op_Subtract | N_Op_Multiply |
2479                  N_Op_Divide | N_Op_Mod      | N_Op_Rem
2480
2481                              => Resolve_Arithmetic_Op            (N, Ctx_Type);
2482
2483             when N_Op_Concat => Resolve_Op_Concat                (N, Ctx_Type);
2484
2485             when N_Op_Expon  => Resolve_Op_Expon                 (N, Ctx_Type);
2486
2487             when N_Op_Plus | N_Op_Minus  | N_Op_Abs
2488                              => Resolve_Unary_Op                 (N, Ctx_Type);
2489
2490             when N_Op_Shift  => Resolve_Shift                    (N, Ctx_Type);
2491
2492             when N_Procedure_Call_Statement
2493                              => Resolve_Call                     (N, Ctx_Type);
2494
2495             when N_Operator_Symbol
2496                              => Resolve_Operator_Symbol          (N, Ctx_Type);
2497
2498             when N_Qualified_Expression
2499                              => Resolve_Qualified_Expression     (N, Ctx_Type);
2500
2501             when N_Raise_xxx_Error
2502                              => Set_Etype (N, Ctx_Type);
2503
2504             when N_Range     => Resolve_Range                    (N, Ctx_Type);
2505
2506             when N_Real_Literal
2507                              => Resolve_Real_Literal             (N, Ctx_Type);
2508
2509             when N_Reference => Resolve_Reference                (N, Ctx_Type);
2510
2511             when N_Selected_Component
2512                              => Resolve_Selected_Component       (N, Ctx_Type);
2513
2514             when N_Slice     => Resolve_Slice                    (N, Ctx_Type);
2515
2516             when N_String_Literal
2517                              => Resolve_String_Literal           (N, Ctx_Type);
2518
2519             when N_Subprogram_Info
2520                              => Resolve_Subprogram_Info          (N, Ctx_Type);
2521
2522             when N_Type_Conversion
2523                              => Resolve_Type_Conversion          (N, Ctx_Type);
2524
2525             when N_Unchecked_Expression =>
2526                Resolve_Unchecked_Expression                      (N, Ctx_Type);
2527
2528             when N_Unchecked_Type_Conversion =>
2529                Resolve_Unchecked_Type_Conversion                 (N, Ctx_Type);
2530
2531          end case;
2532
2533          --  If the subexpression was replaced by a non-subexpression, then
2534          --  all we do is to expand it. The only legitimate case we know of
2535          --  is converting procedure call statement to entry call statements,
2536          --  but there may be others, so we are making this test general.
2537
2538          if Nkind (N) not in N_Subexpr then
2539             Debug_A_Exit ("resolving  ", N, "  (done)");
2540             Expand (N);
2541             return;
2542          end if;
2543
2544          --  The expression is definitely NOT overloaded at this point, so
2545          --  we reset the Is_Overloaded flag to avoid any confusion when
2546          --  reanalyzing the node.
2547
2548          Set_Is_Overloaded (N, False);
2549
2550          --  Freeze expression type, entity if it is a name, and designated
2551          --  type if it is an allocator (RM 13.14(10,11,13)).
2552
2553          --  Now that the resolution of the type of the node is complete,
2554          --  and we did not detect an error, we can expand this node. We
2555          --  skip the expand call if we are in a default expression, see
2556          --  section "Handling of Default Expressions" in Sem spec.
2557
2558          Debug_A_Exit ("resolving  ", N, "  (done)");
2559
2560          --  We unconditionally freeze the expression, even if we are in
2561          --  default expression mode (the Freeze_Expression routine tests
2562          --  this flag and only freezes static types if it is set).
2563
2564          Freeze_Expression (N);
2565
2566          --  Now we can do the expansion
2567
2568          Expand (N);
2569       end if;
2570    end Resolve;
2571
2572    -------------
2573    -- Resolve --
2574    -------------
2575
2576    --  Version with check(s) suppressed
2577
2578    procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2579    begin
2580       if Suppress = All_Checks then
2581          declare
2582             Svg : constant Suppress_Array := Scope_Suppress;
2583          begin
2584             Scope_Suppress := (others => True);
2585             Resolve (N, Typ);
2586             Scope_Suppress := Svg;
2587          end;
2588
2589       else
2590          declare
2591             Svg : constant Boolean := Scope_Suppress (Suppress);
2592          begin
2593             Scope_Suppress (Suppress) := True;
2594             Resolve (N, Typ);
2595             Scope_Suppress (Suppress) := Svg;
2596          end;
2597       end if;
2598    end Resolve;
2599
2600    -------------
2601    -- Resolve --
2602    -------------
2603
2604    --  Version with implicit type
2605
2606    procedure Resolve (N : Node_Id) is
2607    begin
2608       Resolve (N, Etype (N));
2609    end Resolve;
2610
2611    ---------------------
2612    -- Resolve_Actuals --
2613    ---------------------
2614
2615    procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2616       Loc    : constant Source_Ptr := Sloc (N);
2617       A      : Node_Id;
2618       F      : Entity_Id;
2619       A_Typ  : Entity_Id;
2620       F_Typ  : Entity_Id;
2621       Prev   : Node_Id := Empty;
2622       Orig_A : Node_Id;
2623
2624       procedure Check_Argument_Order;
2625       --  Performs a check for the case where the actuals are all simple
2626       --  identifiers that correspond to the formal names, but in the wrong
2627       --  order, which is considered suspicious and cause for a warning.
2628
2629       procedure Check_Prefixed_Call;
2630       --  If the original node is an overloaded call in prefix notation,
2631       --  insert an 'Access or a dereference as needed over the first actual.
2632       --  Try_Object_Operation has already verified that there is a valid
2633       --  interpretation, but the form of the actual can only be determined
2634       --  once the primitive operation is identified.
2635
2636       procedure Insert_Default;
2637       --  If the actual is missing in a call, insert in the actuals list
2638       --  an instance of the default expression. The insertion is always
2639       --  a named association.
2640
2641       function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2642       --  Check whether T1 and T2, or their full views, are derived from a
2643       --  common type. Used to enforce the restrictions on array conversions
2644       --  of AI95-00246.
2645
2646       --------------------------
2647       -- Check_Argument_Order --
2648       --------------------------
2649
2650       procedure Check_Argument_Order is
2651       begin
2652          --  Nothing to do if no parameters, or original node is neither a
2653          --  function call nor a procedure call statement (happens in the
2654          --  operator-transformed-to-function call case), or the call does
2655          --  not come from source, or this warning is off.
2656
2657          if not Warn_On_Parameter_Order
2658            or else
2659              No (Parameter_Associations (N))
2660            or else
2661              not Nkind_In (Original_Node (N), N_Procedure_Call_Statement,
2662                                               N_Function_Call)
2663            or else
2664              not Comes_From_Source (N)
2665          then
2666             return;
2667          end if;
2668
2669          declare
2670             Nargs : constant Nat := List_Length (Parameter_Associations (N));
2671
2672          begin
2673             --  Nothing to do if only one parameter
2674
2675             if Nargs < 2 then
2676                return;
2677             end if;
2678
2679             --  Here if at least two arguments
2680
2681             declare
2682                Actuals : array (1 .. Nargs) of Node_Id;
2683                Actual  : Node_Id;
2684                Formal  : Node_Id;
2685
2686                Wrong_Order : Boolean := False;
2687                --  Set True if an out of order case is found
2688
2689             begin
2690                --  Collect identifier names of actuals, fail if any actual is
2691                --  not a simple identifier, and record max length of name.
2692
2693                Actual := First (Parameter_Associations (N));
2694                for J in Actuals'Range loop
2695                   if Nkind (Actual) /= N_Identifier then
2696                      return;
2697                   else
2698                      Actuals (J) := Actual;
2699                      Next (Actual);
2700                   end if;
2701                end loop;
2702
2703                --  If we got this far, all actuals are identifiers and the list
2704                --  of their names is stored in the Actuals array.
2705
2706                Formal := First_Formal (Nam);
2707                for J in Actuals'Range loop
2708
2709                   --  If we ran out of formals, that's odd, probably an error
2710                   --  which will be detected elsewhere, but abandon the search.
2711
2712                   if No (Formal) then
2713                      return;
2714                   end if;
2715
2716                   --  If name matches and is in order OK
2717
2718                   if Chars (Formal) = Chars (Actuals (J)) then
2719                      null;
2720
2721                   else
2722                      --  If no match, see if it is elsewhere in list and if so
2723                      --  flag potential wrong order if type is compatible.
2724
2725                      for K in Actuals'Range loop
2726                         if Chars (Formal) = Chars (Actuals (K))
2727                           and then
2728                             Has_Compatible_Type (Actuals (K), Etype (Formal))
2729                         then
2730                            Wrong_Order := True;
2731                            goto Continue;
2732                         end if;
2733                      end loop;
2734
2735                      --  No match
2736
2737                      return;
2738                   end if;
2739
2740                   <<Continue>> Next_Formal (Formal);
2741                end loop;
2742
2743                --  If Formals left over, also probably an error, skip warning
2744
2745                if Present (Formal) then
2746                   return;
2747                end if;
2748
2749                --  Here we give the warning if something was out of order
2750
2751                if Wrong_Order then
2752                   Error_Msg_N
2753                     ("actuals for this call may be in wrong order?", N);
2754                end if;
2755             end;
2756          end;
2757       end Check_Argument_Order;
2758
2759       -------------------------
2760       -- Check_Prefixed_Call --
2761       -------------------------
2762
2763       procedure Check_Prefixed_Call is
2764          Act    : constant Node_Id   := First_Actual (N);
2765          A_Type : constant Entity_Id := Etype (Act);
2766          F_Type : constant Entity_Id := Etype (First_Formal (Nam));
2767          Orig   : constant Node_Id := Original_Node (N);
2768          New_A  : Node_Id;
2769
2770       begin
2771          --  Check whether the call is a prefixed call, with or without
2772          --  additional actuals.
2773
2774          if Nkind (Orig) = N_Selected_Component
2775            or else
2776              (Nkind (Orig) = N_Indexed_Component
2777                and then Nkind (Prefix (Orig)) = N_Selected_Component
2778                and then Is_Entity_Name (Prefix (Prefix (Orig)))
2779                and then Is_Entity_Name (Act)
2780                and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
2781          then
2782             if Is_Access_Type (A_Type)
2783               and then not Is_Access_Type (F_Type)
2784             then
2785                --  Introduce dereference on object in prefix
2786
2787                New_A :=
2788                  Make_Explicit_Dereference (Sloc (Act),
2789                    Prefix => Relocate_Node (Act));
2790                Rewrite (Act, New_A);
2791                Analyze (Act);
2792
2793             elsif Is_Access_Type (F_Type)
2794               and then not Is_Access_Type (A_Type)
2795             then
2796                --  Introduce an implicit 'Access in prefix
2797
2798                if not Is_Aliased_View (Act) then
2799                   Error_Msg_NE
2800                     ("object in prefixed call to& must be aliased"
2801                          & " (RM-2005 4.3.1 (13))",
2802                     Prefix (Act), Nam);
2803                end if;
2804
2805                Rewrite (Act,
2806                  Make_Attribute_Reference (Loc,
2807                    Attribute_Name => Name_Access,
2808                    Prefix         => Relocate_Node (Act)));
2809             end if;
2810
2811             Analyze (Act);
2812          end if;
2813       end Check_Prefixed_Call;
2814
2815       --------------------
2816       -- Insert_Default --
2817       --------------------
2818
2819       procedure Insert_Default is
2820          Actval : Node_Id;
2821          Assoc  : Node_Id;
2822
2823       begin
2824          --  Missing argument in call, nothing to insert
2825
2826          if No (Default_Value (F)) then
2827             return;
2828
2829          else
2830             --  Note that we do a full New_Copy_Tree, so that any associated
2831             --  Itypes are properly copied. This may not be needed any more,
2832             --  but it does no harm as a safety measure! Defaults of a generic
2833             --  formal may be out of bounds of the corresponding actual (see
2834             --  cc1311b) and an additional check may be required.
2835
2836             Actval :=
2837               New_Copy_Tree
2838                 (Default_Value (F),
2839                  New_Scope => Current_Scope,
2840                  New_Sloc  => Loc);
2841
2842             if Is_Concurrent_Type (Scope (Nam))
2843               and then Has_Discriminants (Scope (Nam))
2844             then
2845                Replace_Actual_Discriminants (N, Actval);
2846             end if;
2847
2848             if Is_Overloadable (Nam)
2849               and then Present (Alias (Nam))
2850             then
2851                if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
2852                  and then not Is_Tagged_Type (Etype (F))
2853                then
2854                   --  If default is a real literal, do not introduce a
2855                   --  conversion whose effect may depend on the run-time
2856                   --  size of universal real.
2857
2858                   if Nkind (Actval) = N_Real_Literal then
2859                      Set_Etype (Actval, Base_Type (Etype (F)));
2860                   else
2861                      Actval := Unchecked_Convert_To (Etype (F), Actval);
2862                   end if;
2863                end if;
2864
2865                if Is_Scalar_Type (Etype (F)) then
2866                   Enable_Range_Check (Actval);
2867                end if;
2868
2869                Set_Parent (Actval, N);
2870
2871                --  Resolve aggregates with their base type, to avoid scope
2872                --  anomalies: the subtype was first built in the subprogram
2873                --  declaration, and the current call may be nested.
2874
2875                if Nkind (Actval) = N_Aggregate
2876                  and then Has_Discriminants (Etype (Actval))
2877                then
2878                   Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
2879                else
2880                   Analyze_And_Resolve (Actval, Etype (Actval));
2881                end if;
2882
2883             else
2884                Set_Parent (Actval, N);
2885
2886                --  See note above concerning aggregates
2887
2888                if Nkind (Actval) = N_Aggregate
2889                  and then Has_Discriminants (Etype (Actval))
2890                then
2891                   Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
2892
2893                --  Resolve entities with their own type, which may differ
2894                --  from the type of a reference in a generic context (the
2895                --  view swapping mechanism did not anticipate the re-analysis
2896                --  of default values in calls).
2897
2898                elsif Is_Entity_Name (Actval) then
2899                   Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
2900
2901                else
2902                   Analyze_And_Resolve (Actval, Etype (Actval));
2903                end if;
2904             end if;
2905
2906             --  If default is a tag indeterminate function call, propagate
2907             --  tag to obtain proper dispatching.
2908
2909             if Is_Controlling_Formal (F)
2910               and then Nkind (Default_Value (F)) = N_Function_Call
2911             then
2912                Set_Is_Controlling_Actual (Actval);
2913             end if;
2914
2915          end if;
2916
2917          --  If the default expression raises constraint error, then just
2918          --  silently replace it with an N_Raise_Constraint_Error node,
2919          --  since we already gave the warning on the subprogram spec.
2920
2921          if Raises_Constraint_Error (Actval) then
2922             Rewrite (Actval,
2923               Make_Raise_Constraint_Error (Loc,
2924                 Reason => CE_Range_Check_Failed));
2925             Set_Raises_Constraint_Error (Actval);
2926             Set_Etype (Actval, Etype (F));
2927          end if;
2928
2929          Assoc :=
2930            Make_Parameter_Association (Loc,
2931              Explicit_Actual_Parameter => Actval,
2932              Selector_Name => Make_Identifier (Loc, Chars (F)));
2933
2934          --  Case of insertion is first named actual
2935
2936          if No (Prev) or else
2937             Nkind (Parent (Prev)) /= N_Parameter_Association
2938          then
2939             Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
2940             Set_First_Named_Actual (N, Actval);
2941
2942             if No (Prev) then
2943                if No (Parameter_Associations (N)) then
2944                   Set_Parameter_Associations (N, New_List (Assoc));
2945                else
2946                   Append (Assoc, Parameter_Associations (N));
2947                end if;
2948
2949             else
2950                Insert_After (Prev, Assoc);
2951             end if;
2952
2953          --  Case of insertion is not first named actual
2954
2955          else
2956             Set_Next_Named_Actual
2957               (Assoc, Next_Named_Actual (Parent (Prev)));
2958             Set_Next_Named_Actual (Parent (Prev), Actval);
2959             Append (Assoc, Parameter_Associations (N));
2960          end if;
2961
2962          Mark_Rewrite_Insertion (Assoc);
2963          Mark_Rewrite_Insertion (Actval);
2964
2965          Prev := Actval;
2966       end Insert_Default;
2967
2968       -------------------
2969       -- Same_Ancestor --
2970       -------------------
2971
2972       function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
2973          FT1 : Entity_Id := T1;
2974          FT2 : Entity_Id := T2;
2975
2976       begin
2977          if Is_Private_Type (T1)
2978            and then Present (Full_View (T1))
2979          then
2980             FT1 := Full_View (T1);
2981          end if;
2982
2983          if Is_Private_Type (T2)
2984            and then Present (Full_View (T2))
2985          then
2986             FT2 := Full_View (T2);
2987          end if;
2988
2989          return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
2990       end Same_Ancestor;
2991
2992    --  Start of processing for Resolve_Actuals
2993
2994    begin
2995       Check_Argument_Order;
2996
2997       if Present (First_Actual (N)) then
2998          Check_Prefixed_Call;
2999       end if;
3000
3001       A := First_Actual (N);
3002       F := First_Formal (Nam);
3003       while Present (F) loop
3004          if No (A) and then Needs_No_Actuals (Nam) then
3005             null;
3006
3007          --  If we have an error in any actual or formal, indicated by
3008          --  a type of Any_Type, then abandon resolution attempt, and
3009          --  set result type to Any_Type.
3010
3011          elsif (Present (A) and then Etype (A) = Any_Type)
3012            or else Etype (F) = Any_Type
3013          then
3014             Set_Etype (N, Any_Type);
3015             return;
3016          end if;
3017
3018          --  Case where actual is present
3019
3020          --  If the actual is an entity, generate a reference to it now. We
3021          --  do this before the actual is resolved, because a formal of some
3022          --  protected subprogram, or a task discriminant, will be rewritten
3023          --  during expansion, and the reference to the source entity may
3024          --  be lost.
3025
3026          if Present (A)
3027            and then Is_Entity_Name (A)
3028            and then Comes_From_Source (N)
3029          then
3030             Orig_A := Entity (A);
3031
3032             if Present (Orig_A) then
3033                if Is_Formal (Orig_A)
3034                  and then Ekind (F) /= E_In_Parameter
3035                then
3036                   Generate_Reference (Orig_A, A, 'm');
3037                elsif not Is_Overloaded (A) then
3038                   Generate_Reference (Orig_A, A);
3039                end if;
3040             end if;
3041          end if;
3042
3043          if Present (A)
3044            and then (Nkind (Parent (A)) /= N_Parameter_Association
3045                        or else
3046                      Chars (Selector_Name (Parent (A))) = Chars (F))
3047          then
3048             --  If style checking mode on, check match of formal name
3049
3050             if Style_Check then
3051                if Nkind (Parent (A)) = N_Parameter_Association then
3052                   Check_Identifier (Selector_Name (Parent (A)), F);
3053                end if;
3054             end if;
3055
3056             --  If the formal is Out or In_Out, do not resolve and expand the
3057             --  conversion, because it is subsequently expanded into explicit
3058             --  temporaries and assignments. However, the object of the
3059             --  conversion can be resolved. An exception is the case of tagged
3060             --  type conversion with a class-wide actual. In that case we want
3061             --  the tag check to occur and no temporary will be needed (no
3062             --  representation change can occur) and the parameter is passed by
3063             --  reference, so we go ahead and resolve the type conversion.
3064             --  Another exception is the case of reference to component or
3065             --  subcomponent of a bit-packed array, in which case we want to
3066             --  defer expansion to the point the in and out assignments are
3067             --  performed.
3068
3069             if Ekind (F) /= E_In_Parameter
3070               and then Nkind (A) = N_Type_Conversion
3071               and then not Is_Class_Wide_Type (Etype (Expression (A)))
3072             then
3073                if Ekind (F) = E_In_Out_Parameter
3074                  and then Is_Array_Type (Etype (F))
3075                then
3076                   if Has_Aliased_Components (Etype (Expression (A)))
3077                     /= Has_Aliased_Components (Etype (F))
3078                   then
3079
3080                      --  In a view conversion, the conversion must be legal in
3081                      --  both directions, and thus both component types must be
3082                      --  aliased, or neither (4.6 (8)).
3083
3084                      --  The additional rule 4.6 (24.9.2) seems unduly
3085                      --  restrictive: the privacy requirement should not
3086                      --  apply to generic types, and should be checked in
3087                      --  an instance. ARG query is in order.
3088
3089                      Error_Msg_N
3090                        ("both component types in a view conversion must be"
3091                          & " aliased, or neither", A);
3092
3093                   elsif
3094                      not Same_Ancestor (Etype (F), Etype (Expression (A)))
3095                   then
3096                      if Is_By_Reference_Type (Etype (F))
3097                         or else Is_By_Reference_Type (Etype (Expression (A)))
3098                      then
3099                         Error_Msg_N
3100                           ("view conversion between unrelated by reference " &
3101                            "array types not allowed (\'A'I-00246)", A);
3102                      else
3103                         declare
3104                            Comp_Type : constant Entity_Id :=
3105                                          Component_Type
3106                                            (Etype (Expression (A)));
3107                         begin
3108                            if Comes_From_Source (A)
3109                              and then Ada_Version >= Ada_05
3110                              and then
3111                                ((Is_Private_Type (Comp_Type)
3112                                    and then not Is_Generic_Type (Comp_Type))
3113                                  or else Is_Tagged_Type (Comp_Type)
3114                                  or else Is_Volatile (Comp_Type))
3115                            then
3116                               Error_Msg_N
3117                                 ("component type of a view conversion cannot"
3118                                    & " be private, tagged, or volatile"
3119                                    & " (RM 4.6 (24))",
3120                                    Expression (A));
3121                            end if;
3122                         end;
3123                      end if;
3124                   end if;
3125                end if;
3126
3127                if (Conversion_OK (A)
3128                      or else Valid_Conversion (A, Etype (A), Expression (A)))
3129                  and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3130                then
3131                   Resolve (Expression (A));
3132                end if;
3133
3134             --  If the actual is a function call that returns a limited
3135             --  unconstrained object that needs finalization, create a
3136             --  transient scope for it, so that it can receive the proper
3137             --  finalization list.
3138
3139             elsif Nkind (A) = N_Function_Call
3140               and then Is_Limited_Record (Etype (F))
3141               and then not Is_Constrained (Etype (F))
3142               and then Expander_Active
3143               and then
3144                 (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3145             then
3146                Establish_Transient_Scope (A, False);
3147
3148             else
3149                if Nkind (A) = N_Type_Conversion
3150                  and then Is_Array_Type (Etype (F))
3151                  and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3152                  and then
3153                   (Is_Limited_Type (Etype (F))
3154                      or else Is_Limited_Type (Etype (Expression (A))))
3155                then
3156                   Error_Msg_N
3157                     ("conversion between unrelated limited array types " &
3158                      "not allowed (\A\I-00246)", A);
3159
3160                   if Is_Limited_Type (Etype (F)) then
3161                      Explain_Limited_Type (Etype (F), A);
3162                   end if;
3163
3164                   if Is_Limited_Type (Etype (Expression (A))) then
3165                      Explain_Limited_Type (Etype (Expression (A)), A);
3166                   end if;
3167                end if;
3168
3169                --  (Ada 2005: AI-251): If the actual is an allocator whose
3170                --  directly designated type is a class-wide interface, we build
3171                --  an anonymous access type to use it as the type of the
3172                --  allocator. Later, when the subprogram call is expanded, if
3173                --  the interface has a secondary dispatch table the expander
3174                --  will add a type conversion to force the correct displacement
3175                --  of the pointer.
3176
3177                if Nkind (A) = N_Allocator then
3178                   declare
3179                      DDT : constant Entity_Id :=
3180                              Directly_Designated_Type (Base_Type (Etype (F)));
3181
3182                      New_Itype : Entity_Id;
3183
3184                   begin
3185                      if Is_Class_Wide_Type (DDT)
3186                        and then Is_Interface (DDT)
3187                      then
3188                         New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3189                         Set_Etype (New_Itype, Etype (A));
3190                         Set_Directly_Designated_Type (New_Itype,
3191                           Directly_Designated_Type (Etype (A)));
3192                         Set_Etype (A, New_Itype);
3193                      end if;
3194
3195                      --  Ada 2005, AI-162:If the actual is an allocator, the
3196                      --  innermost enclosing statement is the master of the
3197                      --  created object. This needs to be done with expansion
3198                      --  enabled only, otherwise the transient scope will not
3199                      --  be removed in the expansion of the wrapped construct.
3200
3201                      if (Is_Controlled (DDT) or else Has_Task (DDT))
3202                        and then Expander_Active
3203                      then
3204                         Establish_Transient_Scope (A, False);
3205                      end if;
3206                   end;
3207                end if;
3208
3209                --  (Ada 2005): The call may be to a primitive operation of
3210                --   a tagged synchronized type, declared outside of the type.
3211                --   In this case the controlling actual must be converted to
3212                --   its corresponding record type, which is the formal type.
3213                --   The actual may be a subtype, either because of a constraint
3214                --   or because it is a generic actual, so use base type to
3215                --   locate concurrent type.
3216
3217                if Is_Concurrent_Type (Etype (A))
3218                  and then Etype (F) =
3219                             Corresponding_Record_Type (Base_Type (Etype (A)))
3220                then
3221                   Rewrite (A,
3222                     Unchecked_Convert_To
3223                       (Corresponding_Record_Type (Etype (A)), A));
3224                end if;
3225
3226                Resolve (A, Etype (F));
3227             end if;
3228
3229             A_Typ := Etype (A);
3230             F_Typ := Etype (F);
3231
3232             --  For mode IN, if actual is an entity, and the type of the formal
3233             --  has warnings suppressed, then we reset Never_Set_In_Source for
3234             --  the calling entity. The reason for this is to catch cases like
3235             --  GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3236             --  uses trickery to modify an IN parameter.
3237
3238             if Ekind (F) = E_In_Parameter
3239               and then Is_Entity_Name (A)
3240               and then Present (Entity (A))
3241               and then Ekind (Entity (A)) = E_Variable
3242               and then Has_Warnings_Off (F_Typ)
3243             then
3244                Set_Never_Set_In_Source (Entity (A), False);
3245             end if;
3246
3247             --  Perform error checks for IN and IN OUT parameters
3248
3249             if Ekind (F) /= E_Out_Parameter then
3250
3251                --  Check unset reference. For scalar parameters, it is clearly
3252                --  wrong to pass an uninitialized value as either an IN or
3253                --  IN-OUT parameter. For composites, it is also clearly an
3254                --  error to pass a completely uninitialized value as an IN
3255                --  parameter, but the case of IN OUT is trickier. We prefer
3256                --  not to give a warning here. For example, suppose there is
3257                --  a routine that sets some component of a record to False.
3258                --  It is perfectly reasonable to make this IN-OUT and allow
3259                --  either initialized or uninitialized records to be passed
3260                --  in this case.
3261
3262                --  For partially initialized composite values, we also avoid
3263                --  warnings, since it is quite likely that we are passing a
3264                --  partially initialized value and only the initialized fields
3265                --  will in fact be read in the subprogram.
3266
3267                if Is_Scalar_Type (A_Typ)
3268                  or else (Ekind (F) = E_In_Parameter
3269                             and then not Is_Partially_Initialized_Type (A_Typ))
3270                then
3271                   Check_Unset_Reference (A);
3272                end if;
3273
3274                --  In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3275                --  actual to a nested call, since this is case of reading an
3276                --  out parameter, which is not allowed.
3277
3278                if Ada_Version = Ada_83
3279                  and then Is_Entity_Name (A)
3280                  and then Ekind (Entity (A)) = E_Out_Parameter
3281                then
3282                   Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3283                end if;
3284             end if;
3285
3286             --  Case of OUT or IN OUT parameter
3287
3288             if Ekind (F) /= E_In_Parameter then
3289
3290                --  For an Out parameter, check for useless assignment. Note
3291                --  that we can't set Last_Assignment this early, because we may
3292                --  kill current values in Resolve_Call, and that call would
3293                --  clobber the Last_Assignment field.
3294
3295                --  Note: call Warn_On_Useless_Assignment before doing the check
3296                --  below for Is_OK_Variable_For_Out_Formal so that the setting
3297                --  of Referenced_As_LHS/Referenced_As_Out_Formal properly
3298                --  reflects the last assignment, not this one!
3299
3300                if Ekind (F) = E_Out_Parameter then
3301                   if Warn_On_Modified_As_Out_Parameter (F)
3302                     and then Is_Entity_Name (A)
3303                     and then Present (Entity (A))
3304                     and then Comes_From_Source (N)
3305                   then
3306                      Warn_On_Useless_Assignment (Entity (A), A);
3307                   end if;
3308                end if;
3309
3310                --  Validate the form of the actual. Note that the call to
3311                --  Is_OK_Variable_For_Out_Formal generates the required
3312                --  reference in this case.
3313
3314                if not Is_OK_Variable_For_Out_Formal (A) then
3315                   Error_Msg_NE ("actual for& must be a variable", A, F);
3316                end if;
3317
3318                --  What's the following about???
3319
3320                if Is_Entity_Name (A) then
3321                   Kill_Checks (Entity (A));
3322                else
3323                   Kill_All_Checks;
3324                end if;
3325             end if;
3326
3327             if Etype (A) = Any_Type then
3328                Set_Etype (N, Any_Type);
3329                return;
3330             end if;
3331
3332             --  Apply appropriate range checks for in, out, and in-out
3333             --  parameters. Out and in-out parameters also need a separate
3334             --  check, if there is a type conversion, to make sure the return
3335             --  value meets the constraints of the variable before the
3336             --  conversion.
3337
3338             --  Gigi looks at the check flag and uses the appropriate types.
3339             --  For now since one flag is used there is an optimization which
3340             --  might not be done in the In Out case since Gigi does not do
3341             --  any analysis. More thought required about this ???
3342
3343             if Ekind (F) = E_In_Parameter
3344               or else Ekind (F) = E_In_Out_Parameter
3345             then
3346                if Is_Scalar_Type (Etype (A)) then
3347                   Apply_Scalar_Range_Check (A, F_Typ);
3348
3349                elsif Is_Array_Type (Etype (A)) then
3350                   Apply_Length_Check (A, F_Typ);
3351
3352                elsif Is_Record_Type (F_Typ)
3353                  and then Has_Discriminants (F_Typ)
3354                  and then Is_Constrained (F_Typ)
3355                  and then (not Is_Derived_Type (F_Typ)
3356                              or else Comes_From_Source (Nam))
3357                then
3358                   Apply_Discriminant_Check (A, F_Typ);
3359
3360                elsif Is_Access_Type (F_Typ)
3361                  and then Is_Array_Type (Designated_Type (F_Typ))
3362                  and then Is_Constrained (Designated_Type (F_Typ))
3363                then
3364                   Apply_Length_Check (A, F_Typ);
3365
3366                elsif Is_Access_Type (F_Typ)
3367                  and then Has_Discriminants (Designated_Type (F_Typ))
3368                  and then Is_Constrained (Designated_Type (F_Typ))
3369                then
3370                   Apply_Discriminant_Check (A, F_Typ);
3371
3372                else
3373                   Apply_Range_Check (A, F_Typ);
3374                end if;
3375
3376                --  Ada 2005 (AI-231)
3377
3378                if Ada_Version >= Ada_05
3379                  and then Is_Access_Type (F_Typ)
3380                  and then Can_Never_Be_Null (F_Typ)
3381                  and then Known_Null (A)
3382                then
3383                   Apply_Compile_Time_Constraint_Error
3384                     (N      => A,
3385                      Msg    => "(Ada 2005) null not allowed in "
3386                                & "null-excluding formal?",
3387                      Reason => CE_Null_Not_Allowed);
3388                end if;
3389             end if;
3390
3391             if Ekind (F) = E_Out_Parameter
3392               or else Ekind (F) = E_In_Out_Parameter
3393             then
3394                if Nkind (A) = N_Type_Conversion then
3395                   if Is_Scalar_Type (A_Typ) then
3396                      Apply_Scalar_Range_Check
3397                        (Expression (A), Etype (Expression (A)), A_Typ);
3398                   else
3399                      Apply_Range_Check
3400                        (Expression (A), Etype (Expression (A)), A_Typ);
3401                   end if;
3402
3403                else
3404                   if Is_Scalar_Type (F_Typ) then
3405                      Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3406
3407                   elsif Is_Array_Type (F_Typ)
3408                     and then Ekind (F) = E_Out_Parameter
3409                   then
3410                      Apply_Length_Check (A, F_Typ);
3411
3412                   else
3413                      Apply_Range_Check (A, A_Typ, F_Typ);
3414                   end if;
3415                end if;
3416             end if;
3417
3418             --  An actual associated with an access parameter is implicitly
3419             --  converted to the anonymous access type of the formal and must
3420             --  satisfy the legality checks for access conversions.
3421
3422             if Ekind (F_Typ) = E_Anonymous_Access_Type then
3423                if not Valid_Conversion (A, F_Typ, A) then
3424                   Error_Msg_N
3425                     ("invalid implicit conversion for access parameter", A);
3426                end if;
3427             end if;
3428
3429             --  Check bad case of atomic/volatile argument (RM C.6(12))
3430
3431             if Is_By_Reference_Type (Etype (F))
3432               and then Comes_From_Source (N)
3433             then
3434                if Is_Atomic_Object (A)
3435                  and then not Is_Atomic (Etype (F))
3436                then
3437                   Error_Msg_N
3438                     ("cannot pass atomic argument to non-atomic formal",
3439                      N);
3440
3441                elsif Is_Volatile_Object (A)
3442                  and then not Is_Volatile (Etype (F))
3443                then
3444                   Error_Msg_N
3445                     ("cannot pass volatile argument to non-volatile formal",
3446                      N);
3447                end if;
3448             end if;
3449
3450             --  Check that subprograms don't have improper controlling
3451             --  arguments (RM 3.9.2 (9))
3452
3453             --  A primitive operation may have an access parameter of an
3454             --  incomplete tagged type, but a dispatching call is illegal
3455             --  if the type is still incomplete.
3456
3457             if Is_Controlling_Formal (F) then
3458                Set_Is_Controlling_Actual (A);
3459
3460                if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3461                   declare
3462                      Desig : constant Entity_Id := Designated_Type (Etype (F));
3463                   begin
3464                      if Ekind (Desig) = E_Incomplete_Type
3465                        and then No (Full_View (Desig))
3466                        and then No (Non_Limited_View (Desig))
3467                      then
3468                         Error_Msg_NE
3469                           ("premature use of incomplete type& " &
3470                            "in dispatching call", A, Desig);
3471                      end if;
3472                   end;
3473                end if;
3474
3475             elsif Nkind (A) = N_Explicit_Dereference then
3476                Validate_Remote_Access_To_Class_Wide_Type (A);
3477             end if;
3478
3479             if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
3480               and then not Is_Class_Wide_Type (F_Typ)
3481               and then not Is_Controlling_Formal (F)
3482             then
3483                Error_Msg_N ("class-wide argument not allowed here!", A);
3484
3485                if Is_Subprogram (Nam)
3486                  and then Comes_From_Source (Nam)
3487                then
3488                   Error_Msg_Node_2 := F_Typ;
3489                   Error_Msg_NE
3490                     ("& is not a dispatching operation of &!", A, Nam);
3491                end if;
3492
3493             elsif Is_Access_Type (A_Typ)
3494               and then Is_Access_Type (F_Typ)
3495               and then Ekind (F_Typ) /= E_Access_Subprogram_Type
3496               and then Ekind (F_Typ) /= E_Anonymous_Access_Subprogram_Type
3497               and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
3498                          or else (Nkind (A) = N_Attribute_Reference
3499                                    and then
3500                                   Is_Class_Wide_Type (Etype (Prefix (A)))))
3501               and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
3502               and then not Is_Controlling_Formal (F)
3503             then
3504                Error_Msg_N
3505                  ("access to class-wide argument not allowed here!", A);
3506
3507                if Is_Subprogram (Nam)
3508                  and then Comes_From_Source (Nam)
3509                then
3510                   Error_Msg_Node_2 := Designated_Type (F_Typ);
3511                   Error_Msg_NE
3512                     ("& is not a dispatching operation of &!", A, Nam);
3513                end if;
3514             end if;
3515
3516             Eval_Actual (A);
3517
3518             --  If it is a named association, treat the selector_name as
3519             --  a proper identifier, and mark the corresponding entity.
3520
3521             if Nkind (Parent (A)) = N_Parameter_Association then
3522                Set_Entity (Selector_Name (Parent (A)), F);
3523                Generate_Reference (F, Selector_Name (Parent (A)));
3524                Set_Etype (Selector_Name (Parent (A)), F_Typ);
3525                Generate_Reference (F_Typ, N, ' ');
3526             end if;
3527
3528             Prev := A;
3529
3530             if Ekind (F) /= E_Out_Parameter then
3531                Check_Unset_Reference (A);
3532             end if;
3533
3534             Next_Actual (A);
3535
3536          --  Case where actual is not present
3537
3538          else
3539             Insert_Default;
3540          end if;
3541
3542          Next_Formal (F);
3543       end loop;
3544    end Resolve_Actuals;
3545
3546    -----------------------
3547    -- Resolve_Allocator --
3548    -----------------------
3549
3550    procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
3551       E        : constant Node_Id := Expression (N);
3552       Subtyp   : Entity_Id;
3553       Discrim  : Entity_Id;
3554       Constr   : Node_Id;
3555       Aggr     : Node_Id;
3556       Assoc    : Node_Id := Empty;
3557       Disc_Exp : Node_Id;
3558
3559       procedure Check_Allocator_Discrim_Accessibility
3560         (Disc_Exp  : Node_Id;
3561          Alloc_Typ : Entity_Id);
3562       --  Check that accessibility level associated with an access discriminant
3563       --  initialized in an allocator by the expression Disc_Exp is not deeper
3564       --  than the level of the allocator type Alloc_Typ. An error message is
3565       --  issued if this condition is violated. Specialized checks are done for
3566       --  the cases of a constraint expression which is an access attribute or
3567       --  an access discriminant.
3568
3569       function In_Dispatching_Context return Boolean;
3570       --  If the allocator is an actual in a call, it is allowed to be class-
3571       --  wide when the context is not because it is a controlling actual.
3572
3573       procedure Propagate_Coextensions (Root : Node_Id);
3574       --  Propagate all nested coextensions which are located one nesting
3575       --  level down the tree to the node Root. Example:
3576       --
3577       --    Top_Record
3578       --       Level_1_Coextension
3579       --          Level_2_Coextension
3580       --
3581       --  The algorithm is paired with delay actions done by the Expander. In
3582       --  the above example, assume all coextensions are controlled types.
3583       --  The cycle of analysis, resolution and expansion will yield:
3584       --
3585       --  1) Analyze Top_Record
3586       --  2) Analyze Level_1_Coextension
3587       --  3) Analyze Level_2_Coextension
3588       --  4) Resolve Level_2_Coextension. The allocator is marked as a
3589       --       coextension.
3590       --  5) Expand Level_2_Coextension. A temporary variable Temp_1 is
3591       --       generated to capture the allocated object. Temp_1 is attached
3592       --       to the coextension chain of Level_2_Coextension.
3593       --  6) Resolve Level_1_Coextension. The allocator is marked as a
3594       --       coextension. A forward tree traversal is performed which finds
3595       --       Level_2_Coextension's list and copies its contents into its
3596       --       own list.
3597       --  7) Expand Level_1_Coextension. A temporary variable Temp_2 is
3598       --       generated to capture the allocated object. Temp_2 is attached
3599       --       to the coextension chain of Level_1_Coextension. Currently, the
3600       --       contents of the list are [Temp_2, Temp_1].
3601       --  8) Resolve Top_Record. A forward tree traversal is performed which
3602       --       finds Level_1_Coextension's list and copies its contents into
3603       --       its own list.
3604       --  9) Expand Top_Record. Generate finalization calls for Temp_1 and
3605       --       Temp_2 and attach them to Top_Record's finalization list.
3606
3607       -------------------------------------------
3608       -- Check_Allocator_Discrim_Accessibility --
3609       -------------------------------------------
3610
3611       procedure Check_Allocator_Discrim_Accessibility
3612         (Disc_Exp  : Node_Id;
3613          Alloc_Typ : Entity_Id)
3614       is
3615       begin
3616          if Type_Access_Level (Etype (Disc_Exp)) >
3617             Type_Access_Level (Alloc_Typ)
3618          then
3619             Error_Msg_N
3620               ("operand type has deeper level than allocator type", Disc_Exp);
3621
3622          --  When the expression is an Access attribute the level of the prefix
3623          --  object must not be deeper than that of the allocator's type.
3624
3625          elsif Nkind (Disc_Exp) = N_Attribute_Reference
3626            and then Get_Attribute_Id (Attribute_Name (Disc_Exp))
3627                       = Attribute_Access
3628            and then Object_Access_Level (Prefix (Disc_Exp))
3629                       > Type_Access_Level (Alloc_Typ)
3630          then
3631             Error_Msg_N
3632               ("prefix of attribute has deeper level than allocator type",
3633                Disc_Exp);
3634
3635          --  When the expression is an access discriminant the check is against
3636          --  the level of the prefix object.
3637
3638          elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
3639            and then Nkind (Disc_Exp) = N_Selected_Component
3640            and then Object_Access_Level (Prefix (Disc_Exp))
3641                       > Type_Access_Level (Alloc_Typ)
3642          then
3643             Error_Msg_N
3644               ("access discriminant has deeper level than allocator type",
3645                Disc_Exp);
3646
3647          --  All other cases are legal
3648
3649          else
3650             null;
3651          end if;
3652       end Check_Allocator_Discrim_Accessibility;
3653
3654       ----------------------------
3655       -- In_Dispatching_Context --
3656       ----------------------------
3657
3658       function In_Dispatching_Context return Boolean is
3659          Par : constant Node_Id := Parent (N);
3660       begin
3661          return Nkind_In (Par, N_Function_Call, N_Procedure_Call_Statement)
3662            and then Is_Entity_Name (Name (Par))
3663            and then Is_Dispatching_Operation (Entity (Name (Par)));
3664       end In_Dispatching_Context;
3665
3666       ----------------------------
3667       -- Propagate_Coextensions --
3668       ----------------------------
3669
3670       procedure Propagate_Coextensions (Root : Node_Id) is
3671
3672          procedure Copy_List (From : Elist_Id; To : Elist_Id);
3673          --  Copy the contents of list From into list To, preserving the
3674          --  order of elements.
3675
3676          function Process_Allocator (Nod : Node_Id) return Traverse_Result;
3677          --  Recognize an allocator or a rewritten allocator node and add it
3678          --  along with its nested coextensions to the list of Root.
3679
3680          ---------------
3681          -- Copy_List --
3682          ---------------
3683
3684          procedure Copy_List (From : Elist_Id; To : Elist_Id) is
3685             From_Elmt : Elmt_Id;
3686          begin
3687             From_Elmt := First_Elmt (From);
3688             while Present (From_Elmt) loop
3689                Append_Elmt (Node (From_Elmt), To);
3690                Next_Elmt (From_Elmt);
3691             end loop;
3692          end Copy_List;
3693
3694          -----------------------
3695          -- Process_Allocator --
3696          -----------------------
3697
3698          function Process_Allocator (Nod : Node_Id) return Traverse_Result is
3699             Orig_Nod : Node_Id := Nod;
3700
3701          begin
3702             --  This is a possible rewritten subtype indication allocator. Any
3703             --  nested coextensions will appear as discriminant constraints.
3704
3705             if Nkind (Nod) = N_Identifier
3706               and then Present (Original_Node (Nod))
3707               and then Nkind (Original_Node (Nod)) = N_Subtype_Indication
3708             then
3709                declare
3710                   Discr      : Node_Id;
3711                   Discr_Elmt : Elmt_Id;
3712
3713                begin
3714                   if Is_Record_Type (Entity (Nod)) then
3715                      Discr_Elmt :=
3716                        First_Elmt (Discriminant_Constraint (Entity (Nod)));
3717                      while Present (Discr_Elmt) loop
3718                         Discr := Node (Discr_Elmt);
3719
3720                         if Nkind (Discr) = N_Identifier
3721                           and then Present (Original_Node (Discr))
3722                           and then Nkind (Original_Node (Discr)) = N_Allocator
3723                           and then Present (Coextensions (
3724                                      Original_Node (Discr)))
3725                         then
3726                            if No (Coextensions (Root)) then
3727                               Set_Coextensions (Root, New_Elmt_List);
3728                            end if;
3729
3730                            Copy_List
3731                              (From => Coextensions (Original_Node (Discr)),
3732                               To   => Coextensions (Root));
3733                         end if;
3734
3735                         Next_Elmt (Discr_Elmt);
3736                      end loop;
3737
3738                      --  There is no need to continue the traversal of this
3739                      --  subtree since all the information has already been
3740                      --  propagated.
3741
3742                      return Skip;
3743                   end if;
3744                end;
3745
3746             --  Case of either a stand alone allocator or a rewritten allocator
3747             --  with an aggregate.
3748
3749             else
3750                if Present (Original_Node (Nod)) then
3751                   Orig_Nod := Original_Node (Nod);
3752                end if;
3753
3754                if Nkind (Orig_Nod) = N_Allocator then
3755
3756                   --  Propagate the list of nested coextensions to the Root
3757                   --  allocator. This is done through list copy since a single
3758                   --  allocator may have multiple coextensions. Do not touch
3759                   --  coextensions roots.
3760
3761                   if not Is_Coextension_Root (Orig_Nod)
3762                     and then Present (Coextensions (Orig_Nod))
3763                   then
3764                      if No (Coextensions (Root)) then
3765                         Set_Coextensions (Root, New_Elmt_List);
3766                      end if;
3767
3768                      Copy_List
3769                        (From => Coextensions (Orig_Nod),
3770                         To   => Coextensions (Root));
3771                   end if;
3772
3773                   --  There is no need to continue the traversal of this
3774                   --  subtree since all the information has already been
3775                   --  propagated.
3776
3777                   return Skip;
3778                end if;
3779             end if;
3780
3781             --  Keep on traversing, looking for the next allocator
3782
3783             return OK;
3784          end Process_Allocator;
3785
3786          procedure Process_Allocators is
3787            new Traverse_Proc (Process_Allocator);
3788
3789       --  Start of processing for Propagate_Coextensions
3790
3791       begin
3792          Process_Allocators (Expression (Root));
3793       end Propagate_Coextensions;
3794
3795    --  Start of processing for Resolve_Allocator
3796
3797    begin
3798       --  Replace general access with specific type
3799
3800       if Ekind (Etype (N)) = E_Allocator_Type then
3801          Set_Etype (N, Base_Type (Typ));
3802       end if;
3803
3804       if Is_Abstract_Type (Typ) then
3805          Error_Msg_N ("type of allocator cannot be abstract",  N);
3806       end if;
3807
3808       --  For qualified expression, resolve the expression using the
3809       --  given subtype (nothing to do for type mark, subtype indication)
3810
3811       if Nkind (E) = N_Qualified_Expression then
3812          if Is_Class_Wide_Type (Etype (E))
3813            and then not Is_Class_Wide_Type (Designated_Type (Typ))
3814            and then not In_Dispatching_Context
3815          then
3816             Error_Msg_N
3817               ("class-wide allocator not allowed for this access type", N);
3818          end if;
3819
3820          Resolve (Expression (E), Etype (E));
3821          Check_Unset_Reference (Expression (E));
3822
3823          --  A qualified expression requires an exact match of the type,
3824          --  class-wide matching is not allowed.
3825
3826          if (Is_Class_Wide_Type (Etype (Expression (E)))
3827               or else Is_Class_Wide_Type (Etype (E)))
3828            and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
3829          then
3830             Wrong_Type (Expression (E), Etype (E));
3831          end if;
3832
3833          --  A special accessibility check is needed for allocators that
3834          --  constrain access discriminants. The level of the type of the
3835          --  expression used to constrain an access discriminant cannot be
3836          --  deeper than the type of the allocator (in contrast to access
3837          --  parameters, where the level of the actual can be arbitrary).
3838
3839          --  We can't use Valid_Conversion to perform this check because
3840          --  in general the type of the allocator is unrelated to the type
3841          --  of the access discriminant.
3842
3843          if Ekind (Typ) /= E_Anonymous_Access_Type
3844            or else Is_Local_Anonymous_Access (Typ)
3845          then
3846             Subtyp := Entity (Subtype_Mark (E));
3847
3848             Aggr := Original_Node (Expression (E));
3849
3850             if Has_Discriminants (Subtyp)
3851               and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
3852             then
3853                Discrim := First_Discriminant (Base_Type (Subtyp));
3854
3855                --  Get the first component expression of the aggregate
3856
3857                if Present (Expressions (Aggr)) then
3858                   Disc_Exp := First (Expressions (Aggr));
3859
3860                elsif Present (Component_Associations (Aggr)) then
3861                   Assoc := First (Component_Associations (Aggr));
3862
3863                   if Present (Assoc) then
3864                      Disc_Exp := Expression (Assoc);
3865                   else
3866                      Disc_Exp := Empty;
3867                   end if;
3868
3869                else
3870                   Disc_Exp := Empty;
3871                end if;
3872
3873                while Present (Discrim) and then Present (Disc_Exp) loop
3874                   if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
3875                      Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
3876                   end if;
3877
3878                   Next_Discriminant (Discrim);
3879
3880                   if Present (Discrim) then
3881                      if Present (Assoc) then
3882                         Next (Assoc);
3883                         Disc_Exp := Expression (Assoc);
3884
3885                      elsif Present (Next (Disc_Exp)) then
3886                         Next (Disc_Exp);
3887
3888                      else
3889                         Assoc := First (Component_Associations (Aggr));
3890
3891                         if Present (Assoc) then
3892                            Disc_Exp := Expression (Assoc);
3893                         else
3894                            Disc_Exp := Empty;
3895                         end if;
3896                      end if;
3897                   end if;
3898                end loop;
3899             end if;
3900          end if;
3901
3902       --  For a subtype mark or subtype indication, freeze the subtype
3903
3904       else
3905          Freeze_Expression (E);
3906
3907          if Is_Access_Constant (Typ) and then not No_Initialization (N) then
3908             Error_Msg_N
3909               ("initialization required for access-to-constant allocator", N);
3910          end if;
3911
3912          --  A special accessibility check is needed for allocators that
3913          --  constrain access discriminants. The level of the type of the
3914          --  expression used to constrain an access discriminant cannot be
3915          --  deeper than the type of the allocator (in contrast to access
3916          --  parameters, where the level of the actual can be arbitrary).
3917          --  We can't use Valid_Conversion to perform this check because
3918          --  in general the type of the allocator is unrelated to the type
3919          --  of the access discriminant.
3920
3921          if Nkind (Original_Node (E)) = N_Subtype_Indication
3922            and then (Ekind (Typ) /= E_Anonymous_Access_Type
3923                       or else Is_Local_Anonymous_Access (Typ))
3924          then
3925             Subtyp := Entity (Subtype_Mark (Original_Node (E)));
3926
3927             if Has_Discriminants (Subtyp) then
3928                Discrim := First_Discriminant (Base_Type (Subtyp));
3929                Constr := First (Constraints (Constraint (Original_Node (E))));
3930                while Present (Discrim) and then Present (Constr) loop
3931                   if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
3932                      if Nkind (Constr) = N_Discriminant_Association then
3933                         Disc_Exp := Original_Node (Expression (Constr));
3934                      else
3935                         Disc_Exp := Original_Node (Constr);
3936                      end if;
3937
3938                      Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
3939                   end if;
3940
3941                   Next_Discriminant (Discrim);
3942                   Next (Constr);
3943                end loop;
3944             end if;
3945          end if;
3946       end if;
3947
3948       --  Ada 2005 (AI-344): A class-wide allocator requires an accessibility
3949       --  check that the level of the type of the created object is not deeper
3950       --  than the level of the allocator's access type, since extensions can
3951       --  now occur at deeper levels than their ancestor types. This is a
3952       --  static accessibility level check; a run-time check is also needed in
3953       --  the case of an initialized allocator with a class-wide argument (see
3954       --  Expand_Allocator_Expression).
3955
3956       if Ada_Version >= Ada_05
3957         and then Is_Class_Wide_Type (Designated_Type (Typ))
3958       then
3959          declare
3960             Exp_Typ : Entity_Id;
3961
3962          begin
3963             if Nkind (E) = N_Qualified_Expression then
3964                Exp_Typ := Etype (E);
3965             elsif Nkind (E) = N_Subtype_Indication then
3966                Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
3967             else
3968                Exp_Typ := Entity (E);
3969             end if;
3970
3971             if Type_Access_Level (Exp_Typ) > Type_Access_Level (Typ) then
3972                if In_Instance_Body then
3973                   Error_Msg_N ("?type in allocator has deeper level than" &
3974                                " designated class-wide type", E);
3975                   Error_Msg_N ("\?Program_Error will be raised at run time",
3976                                E);
3977                   Rewrite (N,
3978                     Make_Raise_Program_Error (Sloc (N),
3979                       Reason => PE_Accessibility_Check_Failed));
3980                   Set_Etype (N, Typ);
3981
3982                --  Do not apply Ada 2005 accessibility checks on a class-wide
3983                --  allocator if the type given in the allocator is a formal
3984                --  type. A run-time check will be performed in the instance.
3985
3986                elsif not Is_Generic_Type (Exp_Typ) then
3987                   Error_Msg_N ("type in allocator has deeper level than" &
3988                                " designated class-wide type", E);
3989                end if;
3990             end if;
3991          end;
3992       end if;
3993
3994       --  Check for allocation from an empty storage pool
3995
3996       if No_Pool_Assigned (Typ) then
3997          declare
3998             Loc : constant Source_Ptr := Sloc (N);
3999          begin
4000             Error_Msg_N ("?allocation from empty storage pool!", N);
4001             Error_Msg_N ("\?Storage_Error will be raised at run time!", N);
4002             Insert_Action (N,
4003               Make_Raise_Storage_Error (Loc,
4004                 Reason => SE_Empty_Storage_Pool));
4005          end;
4006
4007       --  If the context is an unchecked conversion, as may happen within
4008       --  an inlined subprogram, the allocator is being resolved with its
4009       --  own anonymous type. In that case, if the target type has a specific
4010       --  storage pool, it must be inherited explicitly by the allocator type.
4011
4012       elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4013         and then No (Associated_Storage_Pool (Typ))
4014       then
4015          Set_Associated_Storage_Pool
4016            (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4017       end if;
4018
4019       --  An erroneous allocator may be rewritten as a raise Program_Error
4020       --  statement.
4021
4022       if Nkind (N) = N_Allocator then
4023
4024          --  An anonymous access discriminant is the definition of a
4025          --  coextension.
4026
4027          if Ekind (Typ) = E_Anonymous_Access_Type
4028            and then Nkind (Associated_Node_For_Itype (Typ)) =
4029                       N_Discriminant_Specification
4030          then
4031             --  Avoid marking an allocator as a dynamic coextension if it is
4032             --  within a static construct.
4033
4034             if not Is_Static_Coextension (N) then
4035                Set_Is_Dynamic_Coextension (N);
4036             end if;
4037
4038          --  Cleanup for potential static coextensions
4039
4040          else
4041             Set_Is_Dynamic_Coextension (N, False);
4042             Set_Is_Static_Coextension  (N, False);
4043          end if;
4044
4045          --  There is no need to propagate any nested coextensions if they
4046          --  are marked as static since they will be rewritten on the spot.
4047
4048          if not Is_Static_Coextension (N) then
4049             Propagate_Coextensions (N);
4050          end if;
4051       end if;
4052    end Resolve_Allocator;
4053
4054    ---------------------------
4055    -- Resolve_Arithmetic_Op --
4056    ---------------------------
4057
4058    --  Used for resolving all arithmetic operators except exponentiation
4059
4060    procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4061       L   : constant Node_Id := Left_Opnd (N);
4062       R   : constant Node_Id := Right_Opnd (N);
4063       TL  : constant Entity_Id := Base_Type (Etype (L));
4064       TR  : constant Entity_Id := Base_Type (Etype (R));
4065       T   : Entity_Id;
4066       Rop : Node_Id;
4067
4068       B_Typ : constant Entity_Id := Base_Type (Typ);
4069       --  We do the resolution using the base type, because intermediate values
4070       --  in expressions always are of the base type, not a subtype of it.
4071
4072       function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4073       --  Returns True if N is in a context that expects "any real type"
4074
4075       function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4076       --  Return True iff given type is Integer or universal real/integer
4077
4078       procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4079       --  Choose type of integer literal in fixed-point operation to conform
4080       --  to available fixed-point type. T is the type of the other operand,
4081       --  which is needed to determine the expected type of N.
4082
4083       procedure Set_Operand_Type (N : Node_Id);
4084       --  Set operand type to T if universal
4085
4086       -------------------------------
4087       -- Expected_Type_Is_Any_Real --
4088       -------------------------------
4089
4090       function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4091       begin
4092          --  N is the expression after "delta" in a fixed_point_definition;
4093          --  see RM-3.5.9(6):
4094
4095          return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4096                                       N_Decimal_Fixed_Point_Definition,
4097
4098          --  N is one of the bounds in a real_range_specification;
4099          --  see RM-3.5.7(5):
4100
4101                                       N_Real_Range_Specification,
4102
4103          --  N is the expression of a delta_constraint;
4104          --  see RM-J.3(3):
4105
4106                                       N_Delta_Constraint);
4107       end Expected_Type_Is_Any_Real;
4108
4109       -----------------------------
4110       -- Is_Integer_Or_Universal --
4111       -----------------------------
4112
4113       function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4114          T     : Entity_Id;
4115          Index : Interp_Index;
4116          It    : Interp;
4117
4118       begin
4119          if not Is_Overloaded (N) then
4120             T := Etype (N);
4121             return Base_Type (T) = Base_Type (Standard_Integer)
4122               or else T = Universal_Integer
4123               or else T = Universal_Real;
4124          else
4125             Get_First_Interp (N, Index, It);
4126             while Present (It.Typ) loop
4127                if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4128                  or else It.Typ = Universal_Integer
4129                  or else It.Typ = Universal_Real
4130                then
4131                   return True;
4132                end if;
4133
4134                Get_Next_Interp (Index, It);
4135             end loop;
4136          end if;
4137
4138          return False;
4139       end Is_Integer_Or_Universal;
4140
4141       ----------------------------
4142       -- Set_Mixed_Mode_Operand --
4143       ----------------------------
4144
4145       procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4146          Index : Interp_Index;
4147          It    : Interp;
4148
4149       begin
4150          if Universal_Interpretation (N) = Universal_Integer then
4151
4152             --  A universal integer literal is resolved as standard integer
4153             --  except in the case of a fixed-point result, where we leave it
4154             --  as universal (to be handled by Exp_Fixd later on)
4155
4156             if Is_Fixed_Point_Type (T) then
4157                Resolve (N, Universal_Integer);
4158             else
4159                Resolve (N, Standard_Integer);
4160             end if;
4161
4162          elsif Universal_Interpretation (N) = Universal_Real
4163            and then (T = Base_Type (Standard_Integer)
4164                       or else T = Universal_Integer
4165                       or else T = Universal_Real)
4166          then
4167             --  A universal real can appear in a fixed-type context. We resolve
4168             --  the literal with that context, even though this might raise an
4169             --  exception prematurely (the other operand may be zero).
4170
4171             Resolve (N, B_Typ);
4172
4173          elsif Etype (N) = Base_Type (Standard_Integer)
4174            and then T = Universal_Real
4175            and then Is_Overloaded (N)
4176          then
4177             --  Integer arg in mixed-mode operation. Resolve with universal
4178             --  type, in case preference rule must be applied.
4179
4180             Resolve (N, Universal_Integer);
4181
4182          elsif Etype (N) = T
4183            and then B_Typ /= Universal_Fixed
4184          then
4185             --  Not a mixed-mode operation, resolve with context
4186
4187             Resolve (N, B_Typ);
4188
4189          elsif Etype (N) = Any_Fixed then
4190
4191             --  N may itself be a mixed-mode operation, so use context type
4192
4193             Resolve (N, B_Typ);
4194
4195          elsif Is_Fixed_Point_Type (T)
4196            and then B_Typ = Universal_Fixed
4197            and then Is_Overloaded (N)
4198          then
4199             --  Must be (fixed * fixed) operation, operand must have one
4200             --  compatible interpretation.
4201
4202             Resolve (N, Any_Fixed);
4203
4204          elsif Is_Fixed_Point_Type (B_Typ)
4205            and then (T = Universal_Real
4206                       or else Is_Fixed_Point_Type (T))
4207            and then Is_Overloaded (N)
4208          then
4209             --  C * F(X) in a fixed context, where C is a real literal or a
4210             --  fixed-point expression. F must have either a fixed type
4211             --  interpretation or an integer interpretation, but not both.
4212
4213             Get_First_Interp (N, Index, It);
4214             while Present (It.Typ) loop
4215                if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4216
4217                   if Analyzed (N) then
4218                      Error_Msg_N ("ambiguous operand in fixed operation", N);
4219                   else
4220                      Resolve (N, Standard_Integer);
4221                   end if;
4222
4223                elsif Is_Fixed_Point_Type (It.Typ) then
4224
4225                   if Analyzed (N) then
4226                      Error_Msg_N ("ambiguous operand in fixed operation", N);
4227                   else
4228                      Resolve (N, It.Typ);
4229                   end if;
4230                end if;
4231
4232                Get_Next_Interp (Index, It);
4233             end loop;
4234
4235             --  Reanalyze the literal with the fixed type of the context. If
4236             --  context is Universal_Fixed, we are within a conversion, leave
4237             --  the literal as a universal real because there is no usable
4238             --  fixed type, and the target of the conversion plays no role in
4239             --  the resolution.
4240
4241             declare
4242                Op2 : Node_Id;
4243                T2  : Entity_Id;
4244
4245             begin
4246                if N = L then
4247                   Op2 := R;
4248                else
4249                   Op2 := L;
4250                end if;
4251
4252                if B_Typ = Universal_Fixed
4253                   and then Nkind (Op2) = N_Real_Literal
4254                then
4255                   T2 := Universal_Real;
4256                else
4257                   T2 := B_Typ;
4258                end if;
4259
4260                Set_Analyzed (Op2, False);
4261                Resolve (Op2, T2);
4262             end;
4263
4264          else
4265             Resolve (N);
4266          end if;
4267       end Set_Mixed_Mode_Operand;
4268
4269       ----------------------
4270       -- Set_Operand_Type --
4271       ----------------------
4272
4273       procedure Set_Operand_Type (N : Node_Id) is
4274       begin
4275          if Etype (N) = Universal_Integer
4276            or else Etype (N) = Universal_Real
4277          then
4278             Set_Etype (N, T);
4279          end if;
4280       end Set_Operand_Type;
4281
4282    --  Start of processing for Resolve_Arithmetic_Op
4283
4284    begin
4285       if Comes_From_Source (N)
4286         and then Ekind (Entity (N)) = E_Function
4287         and then Is_Imported (Entity (N))
4288         and then Is_Intrinsic_Subprogram (Entity (N))
4289       then
4290          Resolve_Intrinsic_Operator (N, Typ);
4291          return;
4292
4293       --  Special-case for mixed-mode universal expressions or fixed point
4294       --  type operation: each argument is resolved separately. The same
4295       --  treatment is required if one of the operands of a fixed point
4296       --  operation is universal real, since in this case we don't do a
4297       --  conversion to a specific fixed-point type (instead the expander
4298       --  takes care of the case).
4299
4300       elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4301         and then Present (Universal_Interpretation (L))
4302         and then Present (Universal_Interpretation (R))
4303       then
4304          Resolve (L, Universal_Interpretation (L));
4305          Resolve (R, Universal_Interpretation (R));
4306          Set_Etype (N, B_Typ);
4307
4308       elsif (B_Typ = Universal_Real
4309               or else Etype (N) = Universal_Fixed
4310               or else (Etype (N) = Any_Fixed
4311                         and then Is_Fixed_Point_Type (B_Typ))
4312               or else (Is_Fixed_Point_Type (B_Typ)
4313                         and then (Is_Integer_Or_Universal (L)
4314                                    or else
4315                                   Is_Integer_Or_Universal (R))))
4316         and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4317       then
4318          if TL = Universal_Integer or else TR = Universal_Integer then
4319             Check_For_Visible_Operator (N, B_Typ);
4320          end if;
4321
4322          --  If context is a fixed type and one operand is integer, the
4323          --  other is resolved with the type of the context.
4324
4325          if Is_Fixed_Point_Type (B_Typ)
4326            and then (Base_Type (TL) = Base_Type (Standard_Integer)
4327                       or else TL = Universal_Integer)
4328          then
4329             Resolve (R, B_Typ);
4330             Resolve (L, TL);
4331
4332          elsif Is_Fixed_Point_Type (B_Typ)
4333            and then (Base_Type (TR) = Base_Type (Standard_Integer)
4334                       or else TR = Universal_Integer)
4335          then
4336             Resolve (L, B_Typ);
4337             Resolve (R, TR);
4338
4339          else
4340             Set_Mixed_Mode_Operand (L, TR);
4341             Set_Mixed_Mode_Operand (R, TL);
4342          end if;
4343
4344          --  Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4345          --  multiplying operators from being used when the expected type is
4346          --  also universal_fixed. Note that B_Typ will be Universal_Fixed in
4347          --  some cases where the expected type is actually Any_Real;
4348          --  Expected_Type_Is_Any_Real takes care of that case.
4349
4350          if Etype (N) = Universal_Fixed
4351            or else Etype (N) = Any_Fixed
4352          then
4353             if B_Typ = Universal_Fixed
4354               and then not Expected_Type_Is_Any_Real (N)
4355               and then not Nkind_In (Parent (N), N_Type_Conversion,
4356                                                  N_Unchecked_Type_Conversion)
4357             then
4358                Error_Msg_N ("type cannot be determined from context!", N);
4359                Error_Msg_N ("\explicit conversion to result type required", N);
4360
4361                Set_Etype (L, Any_Type);
4362                Set_Etype (R, Any_Type);
4363
4364             else
4365                if Ada_Version = Ada_83
4366                  and then Etype (N) = Universal_Fixed
4367                  and then not
4368                    Nkind_In (Parent (N), N_Type_Conversion,
4369                                          N_Unchecked_Type_Conversion)
4370                then
4371                   Error_Msg_N
4372                     ("(Ada 83) fixed-point operation "
4373                      & "needs explicit conversion", N);
4374                end if;
4375
4376                --  The expected type is "any real type" in contexts like
4377                --    type T is delta <universal_fixed-expression> ...
4378                --  in which case we need to set the type to Universal_Real
4379                --  so that static expression evaluation will work properly.
4380
4381                if Expected_Type_Is_Any_Real (N) then
4382                   Set_Etype (N, Universal_Real);
4383                else
4384                   Set_Etype (N, B_Typ);
4385                end if;
4386             end if;
4387
4388          elsif Is_Fixed_Point_Type (B_Typ)
4389            and then (Is_Integer_Or_Universal (L)
4390                        or else Nkind (L) = N_Real_Literal
4391                        or else Nkind (R) = N_Real_Literal
4392                        or else Is_Integer_Or_Universal (R))
4393          then
4394             Set_Etype (N, B_Typ);
4395
4396          elsif Etype (N) = Any_Fixed then
4397
4398             --  If no previous errors, this is only possible if one operand
4399             --  is overloaded and the context is universal. Resolve as such.
4400
4401             Set_Etype (N, B_Typ);
4402          end if;
4403
4404       else
4405          if (TL = Universal_Integer or else TL = Universal_Real)
4406               and then
4407             (TR = Universal_Integer or else TR = Universal_Real)
4408          then
4409             Check_For_Visible_Operator (N, B_Typ);
4410          end if;
4411
4412          --  If the context is Universal_Fixed and the operands are also
4413          --  universal fixed, this is an error, unless there is only one
4414          --  applicable fixed_point type (usually duration).
4415
4416          if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
4417             T := Unique_Fixed_Point_Type (N);
4418
4419             if T  = Any_Type then
4420                Set_Etype (N, T);
4421                return;
4422             else
4423                Resolve (L, T);
4424                Resolve (R, T);
4425             end if;
4426
4427          else
4428             Resolve (L, B_Typ);
4429             Resolve (R, B_Typ);
4430          end if;
4431
4432          --  If one of the arguments was resolved to a non-universal type.
4433          --  label the result of the operation itself with the same type.
4434          --  Do the same for the universal argument, if any.
4435
4436          T := Intersect_Types (L, R);
4437          Set_Etype (N, Base_Type (T));
4438          Set_Operand_Type (L);
4439          Set_Operand_Type (R);
4440       end if;
4441
4442       Generate_Operator_Reference (N, Typ);
4443       Eval_Arithmetic_Op (N);
4444
4445       --  Set overflow and division checking bit. Much cleverer code needed
4446       --  here eventually and perhaps the Resolve routines should be separated
4447       --  for the various arithmetic operations, since they will need
4448       --  different processing. ???
4449
4450       if Nkind (N) in N_Op then
4451          if not Overflow_Checks_Suppressed (Etype (N)) then
4452             Enable_Overflow_Check (N);
4453          end if;
4454
4455          --  Give warning if explicit division by zero
4456
4457          if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
4458            and then not Division_Checks_Suppressed (Etype (N))
4459          then
4460             Rop := Right_Opnd (N);
4461
4462             if Compile_Time_Known_Value (Rop)
4463               and then ((Is_Integer_Type (Etype (Rop))
4464                            and then Expr_Value (Rop) = Uint_0)
4465                           or else
4466                         (Is_Real_Type (Etype (Rop))
4467                            and then Expr_Value_R (Rop) = Ureal_0))
4468             then
4469                --  Specialize the warning message according to the operation
4470
4471                case Nkind (N) is
4472                   when N_Op_Divide =>
4473                      Apply_Compile_Time_Constraint_Error
4474                        (N, "division by zero?", CE_Divide_By_Zero,
4475                         Loc => Sloc (Right_Opnd (N)));
4476
4477                   when N_Op_Rem =>
4478                      Apply_Compile_Time_Constraint_Error
4479                        (N, "rem with zero divisor?", CE_Divide_By_Zero,
4480                         Loc => Sloc (Right_Opnd (N)));
4481
4482                   when N_Op_Mod =>
4483                      Apply_Compile_Time_Constraint_Error
4484                        (N, "mod with zero divisor?", CE_Divide_By_Zero,
4485                         Loc => Sloc (Right_Opnd (N)));
4486
4487                   --  Division by zero can only happen with division, rem,
4488                   --  and mod operations.
4489
4490                   when others =>
4491                      raise Program_Error;
4492                end case;
4493
4494             --  Otherwise just set the flag to check at run time
4495
4496             else
4497                Activate_Division_Check (N);
4498             end if;
4499          end if;
4500
4501          --  If Restriction No_Implicit_Conditionals is active, then it is
4502          --  violated if either operand can be negative for mod, or for rem
4503          --  if both operands can be negative.
4504
4505          if Restrictions.Set (No_Implicit_Conditionals)
4506            and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
4507          then
4508             declare
4509                Lo : Uint;
4510                Hi : Uint;
4511                OK : Boolean;
4512
4513                LNeg : Boolean;
4514                RNeg : Boolean;
4515                --  Set if corresponding operand might be negative
4516
4517             begin
4518                Determine_Range (Left_Opnd (N), OK, Lo, Hi);
4519                LNeg := (not OK) or else Lo < 0;
4520
4521                Determine_Range (Right_Opnd (N), OK, Lo, Hi);
4522                RNeg := (not OK) or else Lo < 0;
4523
4524                if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
4525                     or else
4526                   (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
4527                then
4528                   Check_Restriction (No_Implicit_Conditionals, N);
4529                end if;
4530             end;
4531          end if;
4532       end if;
4533
4534       Check_Unset_Reference (L);
4535       Check_Unset_Reference (R);
4536    end Resolve_Arithmetic_Op;
4537
4538    ------------------
4539    -- Resolve_Call --
4540    ------------------
4541
4542    procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
4543       Loc     : constant Source_Ptr := Sloc (N);
4544       Subp    : constant Node_Id    := Name (N);
4545       Nam     : Entity_Id;
4546       I       : Interp_Index;
4547       It      : Interp;
4548       Norm_OK : Boolean;
4549       Scop    : Entity_Id;
4550       Rtype   : Entity_Id;
4551
4552    begin
4553       --  The context imposes a unique interpretation with type Typ on a
4554       --  procedure or function call. Find the entity of the subprogram that
4555       --  yields the expected type, and propagate the corresponding formal
4556       --  constraints on the actuals. The caller has established that an
4557       --  interpretation exists, and emitted an error if not unique.
4558
4559       --  First deal with the case of a call to an access-to-subprogram,
4560       --  dereference made explicit in Analyze_Call.
4561
4562       if Ekind (Etype (Subp)) = E_Subprogram_Type then
4563          if not Is_Overloaded (Subp) then
4564             Nam := Etype (Subp);
4565
4566          else
4567             --  Find the interpretation whose type (a subprogram type) has a
4568             --  return type that is compatible with the context. Analysis of
4569             --  the node has established that one exists.
4570
4571             Nam := Empty;
4572
4573             Get_First_Interp (Subp,  I, It);
4574             while Present (It.Typ) loop
4575                if Covers (Typ, Etype (It.Typ)) then
4576                   Nam := It.Typ;
4577                   exit;
4578                end if;
4579
4580                Get_Next_Interp (I, It);
4581             end loop;
4582
4583             if No (Nam) then
4584                raise Program_Error;
4585             end if;
4586          end if;
4587
4588          --  If the prefix is not an entity, then resolve it
4589
4590          if not Is_Entity_Name (Subp) then
4591             Resolve (Subp, Nam);
4592          end if;
4593
4594          --  For an indirect call, we always invalidate checks, since we do not
4595          --  know whether the subprogram is local or global. Yes we could do
4596          --  better here, e.g. by knowing that there are no local subprograms,
4597          --  but it does not seem worth the effort. Similarly, we kill all
4598          --  knowledge of current constant values.
4599
4600          Kill_Current_Values;
4601
4602       --  If this is a procedure call which is really an entry call, do
4603       --  the conversion of the procedure call to an entry call. Protected
4604       --  operations use the same circuitry because the name in the call
4605       --  can be an arbitrary expression with special resolution rules.
4606
4607       elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
4608         or else (Is_Entity_Name (Subp)
4609                   and then Ekind (Entity (Subp)) = E_Entry)
4610       then
4611          Resolve_Entry_Call (N, Typ);
4612          Check_Elab_Call (N);
4613
4614          --  Kill checks and constant values, as above for indirect case
4615          --  Who knows what happens when another task is activated?
4616
4617          Kill_Current_Values;
4618          return;
4619
4620       --  Normal subprogram call with name established in Resolve
4621
4622       elsif not (Is_Type (Entity (Subp))) then
4623          Nam := Entity (Subp);
4624          Set_Entity_With_Style_Check (Subp, Nam);
4625
4626       --  Otherwise we must have the case of an overloaded call
4627
4628       else
4629          pragma Assert (Is_Overloaded (Subp));
4630          Nam := Empty;  --  We know that it will be assigned in loop below
4631
4632          Get_First_Interp (Subp,  I, It);
4633          while Present (It.Typ) loop
4634             if Covers (Typ, It.Typ) then
4635                Nam := It.Nam;
4636                Set_Entity_With_Style_Check (Subp, Nam);
4637                exit;
4638             end if;
4639
4640             Get_Next_Interp (I, It);
4641          end loop;
4642       end if;
4643
4644       --  Check that a call to Current_Task does not occur in an entry body
4645
4646       if Is_RTE (Nam, RE_Current_Task) then
4647          declare
4648             P : Node_Id;
4649
4650          begin
4651             P := N;
4652             loop
4653                P := Parent (P);
4654
4655                --  Exclude calls that occur within the default of a formal
4656                --  parameter of the entry, since those are evaluated outside
4657                --  of the body.
4658
4659                exit when No (P) or else Nkind (P) = N_Parameter_Specification;
4660
4661                if Nkind (P) = N_Entry_Body
4662                  or else (Nkind (P) = N_Subprogram_Body
4663                            and then Is_Entry_Barrier_Function (P))
4664                then
4665                   Rtype := Etype (N);
4666                   Error_Msg_NE
4667                     ("?& should not be used in entry body (RM C.7(17))",
4668                      N, Nam);
4669                   Error_Msg_NE
4670                     ("\Program_Error will be raised at run time?", N, Nam);
4671                   Rewrite (N,
4672                     Make_Raise_Program_Error (Loc,
4673                       Reason => PE_Current_Task_In_Entry_Body));
4674                   Set_Etype (N, Rtype);
4675                   return;
4676                end if;
4677             end loop;
4678          end;
4679       end if;
4680
4681       --  Check that a procedure call does not occur in the context of the
4682       --  entry call statement of a conditional or timed entry call. Note that
4683       --  the case of a call to a subprogram renaming of an entry will also be
4684       --  rejected. The test for N not being an N_Entry_Call_Statement is
4685       --  defensive, covering the possibility that the processing of entry
4686       --  calls might reach this point due to later modifications of the code
4687       --  above.
4688
4689       if Nkind (Parent (N)) = N_Entry_Call_Alternative
4690         and then Nkind (N) /= N_Entry_Call_Statement
4691         and then Entry_Call_Statement (Parent (N)) = N
4692       then
4693          if Ada_Version < Ada_05 then
4694             Error_Msg_N ("entry call required in select statement", N);
4695
4696          --  Ada 2005 (AI-345): If a procedure_call_statement is used
4697          --  for a procedure_or_entry_call, the procedure_name or pro-
4698          --  cedure_prefix of the procedure_call_statement shall denote
4699          --  an entry renamed by a procedure, or (a view of) a primitive
4700          --  subprogram of a limited interface whose first parameter is
4701          --  a controlling parameter.
4702
4703          elsif Nkind (N) = N_Procedure_Call_Statement
4704            and then not Is_Renamed_Entry (Nam)
4705            and then not Is_Controlling_Limited_Procedure (Nam)
4706          then
4707             Error_Msg_N
4708              ("entry call or dispatching primitive of interface required", N);
4709          end if;
4710       end if;
4711
4712       --  Check that this is not a call to a protected procedure or
4713       --  entry from within a protected function.
4714
4715       if Ekind (Current_Scope) = E_Function
4716         and then Ekind (Scope (Current_Scope)) = E_Protected_Type
4717         and then Ekind (Nam) /= E_Function
4718         and then Scope (Nam) = Scope (Current_Scope)
4719       then
4720          Error_Msg_N ("within protected function, protected " &
4721            "object is constant", N);
4722          Error_Msg_N ("\cannot call operation that may modify it", N);
4723       end if;
4724
4725       --  Freeze the subprogram name if not in a spec-expression. Note that we
4726       --  freeze procedure calls as well as function calls. Procedure calls are
4727       --  not frozen according to the rules (RM 13.14(14)) because it is
4728       --  impossible to have a procedure call to a non-frozen procedure in pure
4729       --  Ada, but in the code that we generate in the expander, this rule
4730       --  needs extending because we can generate procedure calls that need
4731       --  freezing.
4732
4733       if Is_Entity_Name (Subp) and then not In_Spec_Expression then
4734          Freeze_Expression (Subp);
4735       end if;
4736
4737       --  For a predefined operator, the type of the result is the type imposed
4738       --  by context, except for a predefined operation on universal fixed.
4739       --  Otherwise The type of the call is the type returned by the subprogram
4740       --  being called.
4741
4742       if Is_Predefined_Op (Nam) then
4743          if Etype (N) /= Universal_Fixed then
4744             Set_Etype (N, Typ);
4745          end if;
4746
4747       --  If the subprogram returns an array type, and the context requires the
4748       --  component type of that array type, the node is really an indexing of
4749       --  the parameterless call. Resolve as such. A pathological case occurs
4750       --  when the type of the component is an access to the array type. In
4751       --  this case the call is truly ambiguous.
4752
4753       elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
4754         and then
4755           ((Is_Array_Type (Etype (Nam))
4756                    and then Covers (Typ, Component_Type (Etype (Nam))))
4757              or else (Is_Access_Type (Etype (Nam))
4758                         and then Is_Array_Type (Designated_Type (Etype (Nam)))
4759                         and then
4760                           Covers (Typ,
4761                             Component_Type (Designated_Type (Etype (Nam))))))
4762       then
4763          declare
4764             Index_Node : Node_Id;
4765             New_Subp   : Node_Id;
4766             Ret_Type   : constant Entity_Id := Etype (Nam);
4767
4768          begin
4769             if Is_Access_Type (Ret_Type)
4770               and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
4771             then
4772                Error_Msg_N
4773                  ("cannot disambiguate function call and indexing", N);
4774             else
4775                New_Subp := Relocate_Node (Subp);
4776                Set_Entity (Subp, Nam);
4777
4778                if Component_Type (Ret_Type) /= Any_Type then
4779                   if Needs_No_Actuals (Nam) then
4780
4781                      --  Indexed call to a parameterless function
4782
4783                      Index_Node :=
4784                        Make_Indexed_Component (Loc,
4785                          Prefix =>
4786                            Make_Function_Call (Loc,
4787                              Name => New_Subp),
4788                          Expressions => Parameter_Associations (N));
4789                   else
4790                      --  An Ada 2005 prefixed call to a primitive operation
4791                      --  whose first parameter is the prefix. This prefix was
4792                      --  prepended to the parameter list, which is actually a
4793                      --  list of indices. Remove the prefix in order to build
4794                      --  the proper indexed component.
4795
4796                      Index_Node :=
4797                         Make_Indexed_Component (Loc,
4798                           Prefix =>
4799                             Make_Function_Call (Loc,
4800                                Name => New_Subp,
4801                                Parameter_Associations =>
4802                                  New_List
4803                                    (Remove_Head (Parameter_Associations (N)))),
4804                            Expressions => Parameter_Associations (N));
4805                   end if;
4806
4807                   --  Since we are correcting a node classification error made
4808                   --  by the parser, we call Replace rather than Rewrite.
4809
4810                   Replace (N, Index_Node);
4811                   Set_Etype (Prefix (N), Ret_Type);
4812                   Set_Etype (N, Typ);
4813                   Resolve_Indexed_Component (N, Typ);
4814                   Check_Elab_Call (Prefix (N));
4815                end if;
4816             end if;
4817
4818             return;
4819          end;
4820
4821       else
4822          Set_Etype (N, Etype (Nam));
4823       end if;
4824
4825       --  In the case where the call is to an overloaded subprogram, Analyze
4826       --  calls Normalize_Actuals once per overloaded subprogram. Therefore in
4827       --  such a case Normalize_Actuals needs to be called once more to order
4828       --  the actuals correctly. Otherwise the call will have the ordering
4829       --  given by the last overloaded subprogram whether this is the correct
4830       --  one being called or not.
4831
4832       if Is_Overloaded (Subp) then
4833          Normalize_Actuals (N, Nam, False, Norm_OK);
4834          pragma Assert (Norm_OK);
4835       end if;
4836
4837       --  In any case, call is fully resolved now. Reset Overload flag, to
4838       --  prevent subsequent overload resolution if node is analyzed again
4839
4840       Set_Is_Overloaded (Subp, False);
4841       Set_Is_Overloaded (N, False);
4842
4843       --  If we are calling the current subprogram from immediately within its
4844       --  body, then that is the case where we can sometimes detect cases of
4845       --  infinite recursion statically. Do not try this in case restriction
4846       --  No_Recursion is in effect anyway, and do it only for source calls.
4847
4848       if Comes_From_Source (N) then
4849          Scop := Current_Scope;
4850
4851          --  Issue warning for possible infinite recursion in the absence
4852          --  of the No_Recursion restriction.
4853
4854          if Nam = Scop
4855            and then not Restriction_Active (No_Recursion)
4856            and then Check_Infinite_Recursion (N)
4857          then
4858             --  Here we detected and flagged an infinite recursion, so we do
4859             --  not need to test the case below for further warnings. Also if
4860             --  we now have a raise SE node, we are all done.
4861
4862             if Nkind (N) = N_Raise_Storage_Error then
4863                return;
4864             end if;
4865
4866          --  If call is to immediately containing subprogram, then check for
4867          --  the case of a possible run-time detectable infinite recursion.
4868
4869          else
4870             Scope_Loop : while Scop /= Standard_Standard loop
4871                if Nam = Scop then
4872
4873                   --  Although in general case, recursion is not statically
4874                   --  checkable, the case of calling an immediately containing
4875                   --  subprogram is easy to catch.
4876
4877                   Check_Restriction (No_Recursion, N);
4878
4879                   --  If the recursive call is to a parameterless subprogram,
4880                   --  then even if we can't statically detect infinite
4881                   --  recursion, this is pretty suspicious, and we output a
4882                   --  warning. Furthermore, we will try later to detect some
4883                   --  cases here at run time by expanding checking code (see
4884                   --  Detect_Infinite_Recursion in package Exp_Ch6).
4885
4886                   --  If the recursive call is within a handler, do not emit a
4887                   --  warning, because this is a common idiom: loop until input
4888                   --  is correct, catch illegal input in handler and restart.
4889
4890                   if No (First_Formal (Nam))
4891                     and then Etype (Nam) = Standard_Void_Type
4892                     and then not Error_Posted (N)
4893                     and then Nkind (Parent (N)) /= N_Exception_Handler
4894                   then
4895                      --  For the case of a procedure call. We give the message
4896                      --  only if the call is the first statement in a sequence
4897                      --  of statements, or if all previous statements are
4898                      --  simple assignments. This is simply a heuristic to
4899                      --  decrease false positives, without losing too many good
4900                      --  warnings. The idea is that these previous statements
4901                      --  may affect global variables the procedure depends on.
4902
4903                      if Nkind (N) = N_Procedure_Call_Statement
4904                        and then Is_List_Member (N)
4905                      then
4906                         declare
4907                            P : Node_Id;
4908                         begin
4909                            P := Prev (N);
4910                            while Present (P) loop
4911                               if Nkind (P) /= N_Assignment_Statement then
4912                                  exit Scope_Loop;
4913                               end if;
4914
4915                               Prev (P);
4916                            end loop;
4917                         end;
4918                      end if;
4919
4920                      --  Do not give warning if we are in a conditional context
4921
4922                      declare
4923                         K : constant Node_Kind := Nkind (Parent (N));
4924                      begin
4925                         if (K = N_Loop_Statement
4926                             and then Present (Iteration_Scheme (Parent (N))))
4927                           or else K = N_If_Statement
4928                           or else K = N_Elsif_Part
4929                           or else K = N_Case_Statement_Alternative
4930                         then
4931                            exit Scope_Loop;
4932                         end if;
4933                      end;
4934
4935                      --  Here warning is to be issued
4936
4937                      Set_Has_Recursive_Call (Nam);
4938                      Error_Msg_N
4939                        ("?possible infinite recursion!", N);
4940                      Error_Msg_N
4941                        ("\?Storage_Error may be raised at run time!", N);
4942                   end if;
4943
4944                   exit Scope_Loop;
4945                end if;
4946
4947                Scop := Scope (Scop);
4948             end loop Scope_Loop;
4949          end if;
4950       end if;
4951
4952       --  If subprogram name is a predefined operator, it was given in
4953       --  functional notation. Replace call node with operator node, so
4954       --  that actuals can be resolved appropriately.
4955
4956       if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
4957          Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
4958          return;
4959
4960       elsif Present (Alias (Nam))
4961         and then Is_Predefined_Op (Alias (Nam))
4962       then
4963          Resolve_Actuals (N, Nam);
4964          Make_Call_Into_Operator (N, Typ, Alias (Nam));
4965          return;
4966       end if;
4967
4968       --  Create a transient scope if the resulting type requires it
4969
4970       --  There are 4 notable exceptions: in init procs, the transient scope
4971       --  overhead is not needed and even incorrect due to the actual expansion
4972       --  of adjust calls; the second case is enumeration literal pseudo calls;
4973       --  the third case is intrinsic subprograms (Unchecked_Conversion and
4974       --  source information functions) that do not use the secondary stack
4975       --  even though the return type is unconstrained; the fourth case is a
4976       --  call to a build-in-place function, since such functions may allocate
4977       --  their result directly in a target object, and cases where the result
4978       --  does get allocated in the secondary stack are checked for within the
4979       --  specialized Exp_Ch6 procedures for expanding build-in-place calls.
4980
4981       --  If this is an initialization call for a type whose initialization
4982       --  uses the secondary stack, we also need to create a transient scope
4983       --  for it, precisely because we will not do it within the init proc
4984       --  itself.
4985
4986       --  If the subprogram is marked Inline_Always, then even if it returns
4987       --  an unconstrained type the call does not require use of the secondary
4988       --  stack. However, inlining will only take place if the body to inline
4989       --  is already present. It may not be available if e.g. the subprogram is
4990       --  declared in a child instance.
4991
4992       if Is_Inlined (Nam)
4993         and then Has_Pragma_Inline_Always (Nam)
4994         and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
4995         and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
4996       then
4997          null;
4998
4999       elsif Expander_Active
5000         and then Is_Type (Etype (Nam))
5001         and then Requires_Transient_Scope (Etype (Nam))
5002         and then not Is_Build_In_Place_Function (Nam)
5003         and then Ekind (Nam) /= E_Enumeration_Literal
5004         and then not Within_Init_Proc
5005         and then not Is_Intrinsic_Subprogram (Nam)
5006       then
5007          Establish_Transient_Scope (N, Sec_Stack => True);
5008
5009          --  If the call appears within the bounds of a loop, it will
5010          --  be rewritten and reanalyzed, nothing left to do here.
5011
5012          if Nkind (N) /= N_Function_Call then
5013             return;
5014          end if;
5015
5016       elsif Is_Init_Proc (Nam)
5017         and then not Within_Init_Proc
5018       then
5019          Check_Initialization_Call (N, Nam);
5020       end if;
5021
5022       --  A protected function cannot be called within the definition of the
5023       --  enclosing protected type.
5024
5025       if Is_Protected_Type (Scope (Nam))
5026         and then In_Open_Scopes (Scope (Nam))
5027         and then not Has_Completion (Scope (Nam))
5028       then
5029          Error_Msg_NE
5030            ("& cannot be called before end of protected definition", N, Nam);
5031       end if;
5032
5033       --  Propagate interpretation to actuals, and add default expressions
5034       --  where needed.
5035
5036       if Present (First_Formal (Nam)) then
5037          Resolve_Actuals (N, Nam);
5038
5039          --  Overloaded literals are rewritten as function calls, for
5040          --  purpose of resolution. After resolution, we can replace
5041          --  the call with the literal itself.
5042
5043       elsif Ekind (Nam) = E_Enumeration_Literal then
5044          Copy_Node (Subp, N);
5045          Resolve_Entity_Name (N, Typ);
5046
5047          --  Avoid validation, since it is a static function call
5048
5049          Generate_Reference (Nam, Subp);
5050          return;
5051       end if;
5052
5053       --  If the subprogram is not global, then kill all saved values and
5054       --  checks. This is a bit conservative, since in many cases we could do
5055       --  better, but it is not worth the effort. Similarly, we kill constant
5056       --  values. However we do not need to do this for internal entities
5057       --  (unless they are inherited user-defined subprograms), since they
5058       --  are not in the business of molesting local values.
5059
5060       --  If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5061       --  kill all checks and values for calls to global subprograms. This
5062       --  takes care of the case where an access to a local subprogram is
5063       --  taken, and could be passed directly or indirectly and then called
5064       --  from almost any context.
5065
5066       --  Note: we do not do this step till after resolving the actuals. That
5067       --  way we still take advantage of the current value information while
5068       --  scanning the actuals.
5069
5070       --  We suppress killing values if we are processing the nodes associated
5071       --  with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5072       --  type kills all the values as part of analyzing the code that
5073       --  initializes the dispatch tables.
5074
5075       if Inside_Freezing_Actions = 0
5076         and then (not Is_Library_Level_Entity (Nam)
5077                    or else Suppress_Value_Tracking_On_Call (Current_Scope))
5078         and then (Comes_From_Source (Nam)
5079                    or else (Present (Alias (Nam))
5080                              and then Comes_From_Source (Alias (Nam))))
5081       then
5082          Kill_Current_Values;
5083       end if;
5084
5085       --  If we are warning about unread OUT parameters, this is the place to
5086       --  set Last_Assignment for OUT and IN OUT parameters. We have to do this
5087       --  after the above call to Kill_Current_Values (since that call clears
5088       --  the Last_Assignment field of all local variables).
5089
5090       if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5091         and then Comes_From_Source (N)
5092         and then In_Extended_Main_Source_Unit (N)
5093       then
5094          declare
5095             F : Entity_Id;
5096             A : Node_Id;
5097
5098          begin
5099             F := First_Formal (Nam);
5100             A := First_Actual (N);
5101             while Present (F) and then Present (A) loop
5102                if (Ekind (F) = E_Out_Parameter
5103                      or else Ekind (F) = E_In_Out_Parameter)
5104                  and then Warn_On_Modified_As_Out_Parameter (F)
5105                  and then Is_Entity_Name (A)
5106                  and then Present (Entity (A))
5107                  and then Comes_From_Source (N)
5108                  and then Safe_To_Capture_Value (N, Entity (A))
5109                then
5110                   Set_Last_Assignment (Entity (A), A);
5111                end if;
5112
5113                Next_Formal (F);
5114                Next_Actual (A);
5115             end loop;
5116          end;
5117       end if;
5118
5119       --  If the subprogram is a primitive operation, check whether or not
5120       --  it is a correct dispatching call.
5121
5122       if Is_Overloadable (Nam)
5123         and then Is_Dispatching_Operation (Nam)
5124       then
5125          Check_Dispatching_Call (N);
5126
5127       elsif Ekind (Nam) /= E_Subprogram_Type
5128         and then Is_Abstract_Subprogram (Nam)
5129         and then not In_Instance
5130       then
5131          Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5132       end if;
5133
5134       --  If this is a dispatching call, generate the appropriate reference,
5135       --  for better source navigation in GPS.
5136
5137       if Is_Overloadable (Nam)
5138         and then Present (Controlling_Argument (N))
5139       then
5140          Generate_Reference (Nam, Subp, 'R');
5141       else
5142          Generate_Reference (Nam, Subp);
5143       end if;
5144
5145       if Is_Intrinsic_Subprogram (Nam) then
5146          Check_Intrinsic_Call (N);
5147       end if;
5148
5149       --  All done, evaluate call and deal with elaboration issues
5150
5151       Eval_Call (N);
5152       Check_Elab_Call (N);
5153    end Resolve_Call;
5154
5155    -------------------------------
5156    -- Resolve_Character_Literal --
5157    -------------------------------
5158
5159    procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5160       B_Typ : constant Entity_Id := Base_Type (Typ);
5161       C     : Entity_Id;
5162
5163    begin
5164       --  Verify that the character does belong to the type of the context
5165
5166       Set_Etype (N, B_Typ);
5167       Eval_Character_Literal (N);
5168
5169       --  Wide_Wide_Character literals must always be defined, since the set
5170       --  of wide wide character literals is complete, i.e. if a character
5171       --  literal is accepted by the parser, then it is OK for wide wide
5172       --  character (out of range character literals are rejected).
5173
5174       if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5175          return;
5176
5177       --  Always accept character literal for type Any_Character, which
5178       --  occurs in error situations and in comparisons of literals, both
5179       --  of which should accept all literals.
5180
5181       elsif B_Typ = Any_Character then
5182          return;
5183
5184       --  For Standard.Character or a type derived from it, check that
5185       --  the literal is in range
5186
5187       elsif Root_Type (B_Typ) = Standard_Character then
5188          if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5189             return;
5190          end if;
5191
5192       --  For Standard.Wide_Character or a type derived from it, check
5193       --  that the literal is in range
5194
5195       elsif Root_Type (B_Typ) = Standard_Wide_Character then
5196          if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5197             return;
5198          end if;
5199
5200       --  For Standard.Wide_Wide_Character or a type derived from it, we
5201       --  know the literal is in range, since the parser checked!
5202
5203       elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5204          return;
5205
5206       --  If the entity is already set, this has already been resolved in
5207       --  a generic context, or comes from expansion. Nothing else to do.
5208
5209       elsif Present (Entity (N)) then
5210          return;
5211
5212       --  Otherwise we have a user defined character type, and we can use
5213       --  the standard visibility mechanisms to locate the referenced entity
5214
5215       else
5216          C := Current_Entity (N);
5217          while Present (C) loop
5218             if Etype (C) = B_Typ then
5219                Set_Entity_With_Style_Check (N, C);
5220                Generate_Reference (C, N);
5221                return;
5222             end if;
5223
5224             C := Homonym (C);
5225          end loop;
5226       end if;
5227
5228       --  If we fall through, then the literal does not match any of the
5229       --  entries of the enumeration type. This isn't just a constraint
5230       --  error situation, it is an illegality (see RM 4.2).
5231
5232       Error_Msg_NE
5233         ("character not defined for }", N, First_Subtype (B_Typ));
5234    end Resolve_Character_Literal;
5235
5236    ---------------------------
5237    -- Resolve_Comparison_Op --
5238    ---------------------------
5239
5240    --  Context requires a boolean type, and plays no role in resolution.
5241    --  Processing identical to that for equality operators. The result
5242    --  type is the base type, which matters when pathological subtypes of
5243    --  booleans with limited ranges are used.
5244
5245    procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
5246       L : constant Node_Id := Left_Opnd (N);
5247       R : constant Node_Id := Right_Opnd (N);
5248       T : Entity_Id;
5249
5250    begin
5251       --  If this is an intrinsic operation which is not predefined, use
5252       --  the types of its declared arguments to resolve the possibly
5253       --  overloaded operands. Otherwise the operands are unambiguous and
5254       --  specify the expected type.
5255
5256       if Scope (Entity (N)) /= Standard_Standard then
5257          T := Etype (First_Entity (Entity (N)));
5258
5259       else
5260          T := Find_Unique_Type (L, R);
5261
5262          if T = Any_Fixed then
5263             T := Unique_Fixed_Point_Type (L);
5264          end if;
5265       end if;
5266
5267       Set_Etype (N, Base_Type (Typ));
5268       Generate_Reference (T, N, ' ');
5269
5270       if T /= Any_Type then
5271          if T = Any_String
5272            or else T = Any_Composite
5273            or else T = Any_Character
5274          then
5275             if T = Any_Character then
5276                Ambiguous_Character (L);
5277             else
5278                Error_Msg_N ("ambiguous operands for comparison", N);
5279             end if;
5280
5281             Set_Etype (N, Any_Type);
5282             return;
5283
5284          else
5285             Resolve (L, T);
5286             Resolve (R, T);
5287             Check_Unset_Reference (L);
5288             Check_Unset_Reference (R);
5289             Generate_Operator_Reference (N, T);
5290             Eval_Relational_Op (N);
5291          end if;
5292       end if;
5293    end Resolve_Comparison_Op;
5294
5295    ------------------------------------
5296    -- Resolve_Conditional_Expression --
5297    ------------------------------------
5298
5299    procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
5300       Condition : constant Node_Id := First (Expressions (N));
5301       Then_Expr : constant Node_Id := Next (Condition);
5302       Else_Expr : constant Node_Id := Next (Then_Expr);
5303
5304    begin
5305       Resolve (Condition, Standard_Boolean);
5306       Resolve (Then_Expr, Typ);
5307       Resolve (Else_Expr, Typ);
5308
5309       Set_Etype (N, Typ);
5310       Eval_Conditional_Expression (N);
5311    end Resolve_Conditional_Expression;
5312
5313    -----------------------------------------
5314    -- Resolve_Discrete_Subtype_Indication --
5315    -----------------------------------------
5316
5317    procedure Resolve_Discrete_Subtype_Indication
5318      (N   : Node_Id;
5319       Typ : Entity_Id)
5320    is
5321       R : Node_Id;
5322       S : Entity_Id;
5323
5324    begin
5325       Analyze (Subtype_Mark (N));
5326       S := Entity (Subtype_Mark (N));
5327
5328       if Nkind (Constraint (N)) /= N_Range_Constraint then
5329          Error_Msg_N ("expect range constraint for discrete type", N);
5330          Set_Etype (N, Any_Type);
5331
5332       else
5333          R := Range_Expression (Constraint (N));
5334
5335          if R = Error then
5336             return;
5337          end if;
5338
5339          Analyze (R);
5340
5341          if Base_Type (S) /= Base_Type (Typ) then
5342             Error_Msg_NE
5343               ("expect subtype of }", N, First_Subtype (Typ));
5344
5345             --  Rewrite the constraint as a range of Typ
5346             --  to allow compilation to proceed further.
5347
5348             Set_Etype (N, Typ);
5349             Rewrite (Low_Bound (R),
5350               Make_Attribute_Reference (Sloc (Low_Bound (R)),
5351                 Prefix =>         New_Occurrence_Of (Typ, Sloc (R)),
5352                 Attribute_Name => Name_First));
5353             Rewrite (High_Bound (R),
5354               Make_Attribute_Reference (Sloc (High_Bound (R)),
5355                 Prefix =>         New_Occurrence_Of (Typ, Sloc (R)),
5356                 Attribute_Name => Name_First));
5357
5358          else
5359             Resolve (R, Typ);
5360             Set_Etype (N, Etype (R));
5361
5362             --  Additionally, we must check that the bounds are compatible
5363             --  with the given subtype, which might be different from the
5364             --  type of the context.
5365
5366             Apply_Range_Check (R, S);
5367
5368             --  ??? If the above check statically detects a Constraint_Error
5369             --  it replaces the offending bound(s) of the range R with a
5370             --  Constraint_Error node. When the itype which uses these bounds
5371             --  is frozen the resulting call to Duplicate_Subexpr generates
5372             --  a new temporary for the bounds.
5373
5374             --  Unfortunately there are other itypes that are also made depend
5375             --  on these bounds, so when Duplicate_Subexpr is called they get
5376             --  a forward reference to the newly created temporaries and Gigi
5377             --  aborts on such forward references. This is probably sign of a
5378             --  more fundamental problem somewhere else in either the order of
5379             --  itype freezing or the way certain itypes are constructed.
5380
5381             --  To get around this problem we call Remove_Side_Effects right
5382             --  away if either bounds of R are a Constraint_Error.
5383
5384             declare
5385                L : constant Node_Id := Low_Bound (R);
5386                H : constant Node_Id := High_Bound (R);
5387
5388             begin
5389                if Nkind (L) = N_Raise_Constraint_Error then
5390                   Remove_Side_Effects (L);
5391                end if;
5392
5393                if Nkind (H) = N_Raise_Constraint_Error then
5394                   Remove_Side_Effects (H);
5395                end if;
5396             end;
5397
5398             Check_Unset_Reference (Low_Bound  (R));
5399             Check_Unset_Reference (High_Bound (R));
5400          end if;
5401       end if;
5402    end Resolve_Discrete_Subtype_Indication;
5403
5404    -------------------------
5405    -- Resolve_Entity_Name --
5406    -------------------------
5407
5408    --  Used to resolve identifiers and expanded names
5409
5410    procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
5411       E : constant Entity_Id := Entity (N);
5412
5413    begin
5414       --  If garbage from errors, set to Any_Type and return
5415
5416       if No (E) and then Total_Errors_Detected /= 0 then
5417          Set_Etype (N, Any_Type);
5418          return;
5419       end if;
5420
5421       --  Replace named numbers by corresponding literals. Note that this is
5422       --  the one case where Resolve_Entity_Name must reset the Etype, since
5423       --  it is currently marked as universal.
5424
5425       if Ekind (E) = E_Named_Integer then
5426          Set_Etype (N, Typ);
5427          Eval_Named_Integer (N);
5428
5429       elsif Ekind (E) = E_Named_Real then
5430          Set_Etype (N, Typ);
5431          Eval_Named_Real (N);
5432
5433       --  Allow use of subtype only if it is a concurrent type where we are
5434       --  currently inside the body. This will eventually be expanded
5435       --  into a call to Self (for tasks) or _object (for protected
5436       --  objects). Any other use of a subtype is invalid.
5437
5438       elsif Is_Type (E) then
5439          if Is_Concurrent_Type (E)
5440            and then In_Open_Scopes (E)
5441          then
5442             null;
5443          else
5444             Error_Msg_N
5445                ("invalid use of subtype mark in expression or call", N);
5446          end if;
5447
5448       --  Check discriminant use if entity is discriminant in current scope,
5449       --  i.e. discriminant of record or concurrent type currently being
5450       --  analyzed. Uses in corresponding body are unrestricted.
5451
5452       elsif Ekind (E) = E_Discriminant
5453         and then Scope (E) = Current_Scope
5454         and then not Has_Completion (Current_Scope)
5455       then
5456          Check_Discriminant_Use (N);
5457
5458       --  A parameterless generic function cannot appear in a context that
5459       --  requires resolution.
5460
5461       elsif Ekind (E) = E_Generic_Function then
5462          Error_Msg_N ("illegal use of generic function", N);
5463
5464       elsif Ekind (E) = E_Out_Parameter
5465         and then Ada_Version = Ada_83
5466         and then (Nkind (Parent (N)) in N_Op
5467                     or else (Nkind (Parent (N)) = N_Assignment_Statement
5468                               and then N = Expression (Parent (N)))
5469                     or else Nkind (Parent (N)) = N_Explicit_Dereference)
5470       then
5471          Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
5472
5473       --  In all other cases, just do the possible static evaluation
5474
5475       else
5476          --  A deferred constant that appears in an expression must have
5477          --  a completion, unless it has been removed by in-place expansion
5478          --  of an aggregate.
5479
5480          if Ekind (E) = E_Constant
5481            and then Comes_From_Source (E)
5482            and then No (Constant_Value (E))
5483            and then Is_Frozen (Etype (E))
5484            and then not In_Spec_Expression
5485            and then not Is_Imported (E)
5486          then
5487
5488             if No_Initialization (Parent (E))
5489               or else (Present (Full_View (E))
5490                         and then No_Initialization (Parent (Full_View (E))))
5491             then
5492                null;
5493             else
5494                Error_Msg_N (
5495                  "deferred constant is frozen before completion", N);
5496             end if;
5497          end if;
5498
5499          Eval_Entity_Name (N);
5500       end if;
5501    end Resolve_Entity_Name;
5502
5503    -------------------
5504    -- Resolve_Entry --
5505    -------------------
5506
5507    procedure Resolve_Entry (Entry_Name : Node_Id) is
5508       Loc    : constant Source_Ptr := Sloc (Entry_Name);
5509       Nam    : Entity_Id;
5510       New_N  : Node_Id;
5511       S      : Entity_Id;
5512       Tsk    : Entity_Id;
5513       E_Name : Node_Id;
5514       Index  : Node_Id;
5515
5516       function Actual_Index_Type (E : Entity_Id) return Entity_Id;
5517       --  If the bounds of the entry family being called depend on task
5518       --  discriminants, build a new index subtype where a discriminant is
5519       --  replaced with the value of the discriminant of the target task.
5520       --  The target task is the prefix of the entry name in the call.
5521
5522       -----------------------
5523       -- Actual_Index_Type --
5524       -----------------------
5525
5526       function Actual_Index_Type (E : Entity_Id) return Entity_Id is
5527          Typ   : constant Entity_Id := Entry_Index_Type (E);
5528          Tsk   : constant Entity_Id := Scope (E);
5529          Lo    : constant Node_Id   := Type_Low_Bound  (Typ);
5530          Hi    : constant Node_Id   := Type_High_Bound (Typ);
5531          New_T : Entity_Id;
5532
5533          function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
5534          --  If the bound is given by a discriminant, replace with a reference
5535          --  to the discriminant of the same name in the target task.
5536          --  If the entry name is the target of a requeue statement and the
5537          --  entry is in the current protected object, the bound to be used
5538          --  is the discriminal of the object (see apply_range_checks for
5539          --  details of the transformation).
5540
5541          -----------------------------
5542          -- Actual_Discriminant_Ref --
5543          -----------------------------
5544
5545          function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
5546             Typ : constant Entity_Id := Etype (Bound);
5547             Ref : Node_Id;
5548
5549          begin
5550             Remove_Side_Effects (Bound);
5551
5552             if not Is_Entity_Name (Bound)
5553               or else Ekind (Entity (Bound)) /= E_Discriminant
5554             then
5555                return Bound;
5556
5557             elsif Is_Protected_Type (Tsk)
5558               and then In_Open_Scopes (Tsk)
5559               and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
5560             then
5561                return New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
5562
5563             else
5564                Ref :=
5565                  Make_Selected_Component (Loc,
5566                    Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
5567                    Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
5568                Analyze (Ref);
5569                Resolve (Ref, Typ);
5570                return Ref;
5571             end if;
5572          end Actual_Discriminant_Ref;
5573
5574       --  Start of processing for Actual_Index_Type
5575
5576       begin
5577          if not Has_Discriminants (Tsk)
5578            or else (not Is_Entity_Name (Lo)
5579                      and then not Is_Entity_Name (Hi))
5580          then
5581             return Entry_Index_Type (E);
5582
5583          else
5584             New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
5585             Set_Etype        (New_T, Base_Type (Typ));
5586             Set_Size_Info    (New_T, Typ);
5587             Set_RM_Size      (New_T, RM_Size (Typ));
5588             Set_Scalar_Range (New_T,
5589               Make_Range (Sloc (Entry_Name),
5590                 Low_Bound  => Actual_Discriminant_Ref (Lo),
5591                 High_Bound => Actual_Discriminant_Ref (Hi)));
5592
5593             return New_T;
5594          end if;
5595       end Actual_Index_Type;
5596
5597    --  Start of processing of Resolve_Entry
5598
5599    begin
5600       --  Find name of entry being called, and resolve prefix of name
5601       --  with its own type. The prefix can be overloaded, and the name
5602       --  and signature of the entry must be taken into account.
5603
5604       if Nkind (Entry_Name) = N_Indexed_Component then
5605
5606          --  Case of dealing with entry family within the current tasks
5607
5608          E_Name := Prefix (Entry_Name);
5609
5610       else
5611          E_Name := Entry_Name;
5612       end if;
5613
5614       if Is_Entity_Name (E_Name) then
5615          --  Entry call to an entry (or entry family) in the current task.
5616          --  This is legal even though the task will deadlock. Rewrite as
5617          --  call to current task.
5618
5619          --  This can also be a call to an entry in  an enclosing task.
5620          --  If this is a single task, we have to retrieve its name,
5621          --  because the scope of the entry is the task type, not the
5622          --  object. If the enclosing task is a task type, the identity
5623          --  of the task is given by its own self variable.
5624
5625          --  Finally this can be a requeue on an entry of the same task
5626          --  or protected object.
5627
5628          S := Scope (Entity (E_Name));
5629
5630          for J in reverse 0 .. Scope_Stack.Last loop
5631
5632             if Is_Task_Type (Scope_Stack.Table (J).Entity)
5633               and then not Comes_From_Source (S)
5634             then
5635                --  S is an enclosing task or protected object. The concurrent
5636                --  declaration has been converted into a type declaration, and
5637                --  the object itself has an object declaration that follows
5638                --  the type in the same declarative part.
5639
5640                Tsk := Next_Entity (S);
5641                while Etype (Tsk) /= S loop
5642                   Next_Entity (Tsk);
5643                end loop;
5644
5645                S := Tsk;
5646                exit;
5647
5648             elsif S = Scope_Stack.Table (J).Entity then
5649
5650                --  Call to current task. Will be transformed into call to Self
5651
5652                exit;
5653
5654             end if;
5655          end loop;
5656
5657          New_N :=
5658            Make_Selected_Component (Loc,
5659              Prefix => New_Occurrence_Of (S, Loc),
5660              Selector_Name =>
5661                New_Occurrence_Of (Entity (E_Name), Loc));
5662          Rewrite (E_Name, New_N);
5663          Analyze (E_Name);
5664
5665       elsif Nkind (Entry_Name) = N_Selected_Component
5666         and then Is_Overloaded (Prefix (Entry_Name))
5667       then
5668          --  Use the entry name (which must be unique at this point) to
5669          --  find the prefix that returns the corresponding task type or
5670          --  protected type.
5671
5672          declare
5673             Pref : constant Node_Id := Prefix (Entry_Name);
5674             Ent  : constant Entity_Id :=  Entity (Selector_Name (Entry_Name));
5675             I    : Interp_Index;
5676             It   : Interp;
5677
5678          begin
5679             Get_First_Interp (Pref, I, It);
5680             while Present (It.Typ) loop
5681                if Scope (Ent) = It.Typ then
5682                   Set_Etype (Pref, It.Typ);
5683                   exit;
5684                end if;
5685
5686                Get_Next_Interp (I, It);
5687             end loop;
5688          end;
5689       end if;
5690
5691       if Nkind (Entry_Name) = N_Selected_Component then
5692          Resolve (Prefix (Entry_Name));
5693
5694       else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
5695          Nam := Entity (Selector_Name (Prefix (Entry_Name)));
5696          Resolve (Prefix (Prefix (Entry_Name)));
5697          Index :=  First (Expressions (Entry_Name));
5698          Resolve (Index, Entry_Index_Type (Nam));
5699
5700          --  Up to this point the expression could have been the actual
5701          --  in a simple entry call, and be given by a named association.
5702
5703          if Nkind (Index) = N_Parameter_Association then
5704             Error_Msg_N ("expect expression for entry index", Index);
5705          else
5706             Apply_Range_Check (Index, Actual_Index_Type (Nam));
5707          end if;
5708       end if;
5709    end Resolve_Entry;
5710
5711    ------------------------
5712    -- Resolve_Entry_Call --
5713    ------------------------
5714
5715    procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
5716       Entry_Name  : constant Node_Id    := Name (N);
5717       Loc         : constant Source_Ptr := Sloc (Entry_Name);
5718       Actuals     : List_Id;
5719       First_Named : Node_Id;
5720       Nam         : Entity_Id;
5721       Norm_OK     : Boolean;
5722       Obj         : Node_Id;
5723       Was_Over    : Boolean;
5724
5725    begin
5726       --  We kill all checks here, because it does not seem worth the
5727       --  effort to do anything better, an entry call is a big operation.
5728
5729       Kill_All_Checks;
5730
5731       --  Processing of the name is similar for entry calls and protected
5732       --  operation calls. Once the entity is determined, we can complete
5733       --  the resolution of the actuals.
5734
5735       --  The selector may be overloaded, in the case of a protected object
5736       --  with overloaded functions. The type of the context is used for
5737       --  resolution.
5738
5739       if Nkind (Entry_Name) = N_Selected_Component
5740         and then Is_Overloaded (Selector_Name (Entry_Name))
5741         and then Typ /= Standard_Void_Type
5742       then
5743          declare
5744             I  : Interp_Index;
5745             It : Interp;
5746
5747          begin
5748             Get_First_Interp (Selector_Name (Entry_Name), I, It);
5749             while Present (It.Typ) loop
5750                if Covers (Typ, It.Typ) then
5751                   Set_Entity (Selector_Name (Entry_Name), It.Nam);
5752                   Set_Etype  (Entry_Name, It.Typ);
5753
5754                   Generate_Reference (It.Typ, N, ' ');
5755                end if;
5756
5757                Get_Next_Interp (I, It);
5758             end loop;
5759          end;
5760       end if;
5761
5762       Resolve_Entry (Entry_Name);
5763
5764       if Nkind (Entry_Name) = N_Selected_Component then
5765
5766          --  Simple entry call
5767
5768          Nam := Entity (Selector_Name (Entry_Name));
5769          Obj := Prefix (Entry_Name);
5770          Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
5771
5772       else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
5773
5774          --  Call to member of entry family
5775
5776          Nam := Entity (Selector_Name (Prefix (Entry_Name)));
5777          Obj := Prefix (Prefix (Entry_Name));
5778          Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
5779       end if;
5780
5781       --  We cannot in general check the maximum depth of protected entry
5782       --  calls at compile time. But we can tell that any protected entry
5783       --  call at all violates a specified nesting depth of zero.
5784
5785       if Is_Protected_Type (Scope (Nam)) then
5786          Check_Restriction (Max_Entry_Queue_Length, N);
5787       end if;
5788
5789       --  Use context type to disambiguate a protected function that can be
5790       --  called without actuals and that returns an array type, and where
5791       --  the argument list may be an indexing of the returned value.
5792
5793       if Ekind (Nam) = E_Function
5794         and then Needs_No_Actuals (Nam)
5795         and then Present (Parameter_Associations (N))
5796         and then
5797           ((Is_Array_Type (Etype (Nam))
5798              and then Covers (Typ, Component_Type (Etype (Nam))))
5799
5800             or else (Is_Access_Type (Etype (Nam))
5801                       and then Is_Array_Type (Designated_Type (Etype (Nam)))
5802                       and then Covers (Typ,
5803                         Component_Type (Designated_Type (Etype (Nam))))))
5804       then
5805          declare
5806             Index_Node : Node_Id;
5807
5808          begin
5809             Index_Node :=
5810               Make_Indexed_Component (Loc,
5811                 Prefix =>
5812                   Make_Function_Call (Loc,
5813                     Name => Relocate_Node (Entry_Name)),
5814                 Expressions => Parameter_Associations (N));
5815
5816             --  Since we are correcting a node classification error made by
5817             --  the parser, we call Replace rather than Rewrite.
5818
5819             Replace (N, Index_Node);
5820             Set_Etype (Prefix (N), Etype (Nam));
5821             Set_Etype (N, Typ);
5822             Resolve_Indexed_Component (N, Typ);
5823             return;
5824          end;
5825       end if;
5826
5827       --  The operation name may have been overloaded. Order the actuals
5828       --  according to the formals of the resolved entity, and set the
5829       --  return type to that of the operation.
5830
5831       if Was_Over then
5832          Normalize_Actuals (N, Nam, False, Norm_OK);
5833          pragma Assert (Norm_OK);
5834          Set_Etype (N, Etype (Nam));
5835       end if;
5836
5837       Resolve_Actuals (N, Nam);
5838       Generate_Reference (Nam, Entry_Name);
5839
5840       if Ekind (Nam) = E_Entry
5841         or else Ekind (Nam) = E_Entry_Family
5842       then
5843          Check_Potentially_Blocking_Operation (N);
5844       end if;
5845
5846       --  Verify that a procedure call cannot masquerade as an entry
5847       --  call where an entry call is expected.
5848
5849       if Ekind (Nam) = E_Procedure then
5850          if Nkind (Parent (N)) = N_Entry_Call_Alternative
5851            and then N = Entry_Call_Statement (Parent (N))
5852          then
5853             Error_Msg_N ("entry call required in select statement", N);
5854
5855          elsif Nkind (Parent (N)) = N_Triggering_Alternative
5856            and then N = Triggering_Statement (Parent (N))
5857          then
5858             Error_Msg_N ("triggering statement cannot be procedure call", N);
5859
5860          elsif Ekind (Scope (Nam)) = E_Task_Type
5861            and then not In_Open_Scopes (Scope (Nam))
5862          then
5863             Error_Msg_N ("task has no entry with this name", Entry_Name);
5864          end if;
5865       end if;
5866
5867       --  After resolution, entry calls and protected procedure calls
5868       --  are changed into entry calls, for expansion. The structure
5869       --  of the node does not change, so it can safely be done in place.
5870       --  Protected function calls must keep their structure because they
5871       --  are subexpressions.
5872
5873       if Ekind (Nam) /= E_Function then
5874
5875          --  A protected operation that is not a function may modify the
5876          --  corresponding object, and cannot apply to a constant.
5877          --  If this is an internal call, the prefix is the type itself.
5878
5879          if Is_Protected_Type (Scope (Nam))
5880            and then not Is_Variable (Obj)
5881            and then (not Is_Entity_Name (Obj)
5882                        or else not Is_Type (Entity (Obj)))
5883          then
5884             Error_Msg_N
5885               ("prefix of protected procedure or entry call must be variable",
5886                Entry_Name);
5887          end if;
5888
5889          Actuals := Parameter_Associations (N);
5890          First_Named := First_Named_Actual (N);
5891
5892          Rewrite (N,
5893            Make_Entry_Call_Statement (Loc,
5894              Name                   => Entry_Name,
5895              Parameter_Associations => Actuals));
5896
5897          Set_First_Named_Actual (N, First_Named);
5898          Set_Analyzed (N, True);
5899
5900       --  Protected functions can return on the secondary stack, in which
5901       --  case we must trigger the transient scope mechanism.
5902
5903       elsif Expander_Active
5904         and then Requires_Transient_Scope (Etype (Nam))
5905       then
5906          Establish_Transient_Scope (N, Sec_Stack => True);
5907       end if;
5908    end Resolve_Entry_Call;
5909
5910    -------------------------
5911    -- Resolve_Equality_Op --
5912    -------------------------
5913
5914    --  Both arguments must have the same type, and the boolean context
5915    --  does not participate in the resolution. The first pass verifies
5916    --  that the interpretation is not ambiguous, and the type of the left
5917    --  argument is correctly set, or is Any_Type in case of ambiguity.
5918    --  If both arguments are strings or aggregates, allocators, or Null,
5919    --  they are ambiguous even though they carry a single (universal) type.
5920    --  Diagnose this case here.
5921
5922    procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
5923       L : constant Node_Id   := Left_Opnd (N);
5924       R : constant Node_Id   := Right_Opnd (N);
5925       T : Entity_Id := Find_Unique_Type (L, R);
5926
5927       function Find_Unique_Access_Type return Entity_Id;
5928       --  In the case of allocators, make a last-ditch attempt to find a single
5929       --  access type with the right designated type. This is semantically
5930       --  dubious, and of no interest to any real code, but c48008a makes it
5931       --  all worthwhile.
5932
5933       -----------------------------
5934       -- Find_Unique_Access_Type --
5935       -----------------------------
5936
5937       function Find_Unique_Access_Type return Entity_Id is
5938          Acc : Entity_Id;
5939          E   : Entity_Id;
5940          S   : Entity_Id;
5941
5942       begin
5943          if Ekind (Etype (R)) =  E_Allocator_Type then
5944             Acc := Designated_Type (Etype (R));
5945          elsif Ekind (Etype (L)) =  E_Allocator_Type then
5946             Acc := Designated_Type (Etype (L));
5947          else
5948             return Empty;
5949          end if;
5950
5951          S := Current_Scope;
5952          while S /= Standard_Standard loop
5953             E := First_Entity (S);
5954             while Present (E) loop
5955                if Is_Type (E)
5956                  and then Is_Access_Type (E)
5957                  and then Ekind (E) /= E_Allocator_Type
5958                  and then Designated_Type (E) = Base_Type (Acc)
5959                then
5960                   return E;
5961                end if;
5962
5963                Next_Entity (E);
5964             end loop;
5965
5966             S := Scope (S);
5967          end loop;
5968
5969          return Empty;
5970       end Find_Unique_Access_Type;
5971
5972    --  Start of processing for Resolve_Equality_Op
5973
5974    begin
5975       Set_Etype (N, Base_Type (Typ));
5976       Generate_Reference (T, N, ' ');
5977
5978       if T = Any_Fixed then
5979          T := Unique_Fixed_Point_Type (L);
5980       end if;
5981
5982       if T /= Any_Type then
5983          if T = Any_String
5984            or else T = Any_Composite
5985            or else T = Any_Character
5986          then
5987             if T = Any_Character then
5988                Ambiguous_Character (L);
5989             else
5990                Error_Msg_N ("ambiguous operands for equality", N);
5991             end if;
5992
5993             Set_Etype (N, Any_Type);
5994             return;
5995
5996          elsif T = Any_Access
5997            or else Ekind (T) = E_Allocator_Type
5998            or else Ekind (T) = E_Access_Attribute_Type
5999          then
6000             T := Find_Unique_Access_Type;
6001
6002             if No (T) then
6003                Error_Msg_N ("ambiguous operands for equality", N);
6004                Set_Etype (N, Any_Type);
6005                return;
6006             end if;
6007          end if;
6008
6009          Resolve (L, T);
6010          Resolve (R, T);
6011
6012          --  If the unique type is a class-wide type then it will be expanded
6013          --  into a dispatching call to the predefined primitive. Therefore we
6014          --  check here for potential violation of such restriction.
6015
6016          if Is_Class_Wide_Type (T) then
6017             Check_Restriction (No_Dispatching_Calls, N);
6018          end if;
6019
6020          if Warn_On_Redundant_Constructs
6021            and then Comes_From_Source (N)
6022            and then Is_Entity_Name (R)
6023            and then Entity (R) = Standard_True
6024            and then Comes_From_Source (R)
6025          then
6026             Error_Msg_N ("?comparison with True is redundant!", R);
6027          end if;
6028
6029          Check_Unset_Reference (L);
6030          Check_Unset_Reference (R);
6031          Generate_Operator_Reference (N, T);
6032
6033          --  If this is an inequality, it may be the implicit inequality
6034          --  created for a user-defined operation, in which case the corres-
6035          --  ponding equality operation is not intrinsic, and the operation
6036          --  cannot be constant-folded. Else fold.
6037
6038          if Nkind (N) = N_Op_Eq
6039            or else Comes_From_Source (Entity (N))
6040            or else Ekind (Entity (N)) = E_Operator
6041            or else Is_Intrinsic_Subprogram
6042              (Corresponding_Equality (Entity (N)))
6043          then
6044             Eval_Relational_Op (N);
6045
6046          elsif Nkind (N) = N_Op_Ne
6047            and then Is_Abstract_Subprogram (Entity (N))
6048          then
6049             Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
6050          end if;
6051
6052          --  Ada 2005:  If one operand is an anonymous access type, convert
6053          --  the other operand to it, to ensure that the underlying types
6054          --  match in the back-end. Same for access_to_subprogram, and the
6055          --  conversion verifies that the types are subtype conformant.
6056
6057          --  We apply the same conversion in the case one of the operands is
6058          --  a private subtype of the type of the other.
6059
6060          --  Why the Expander_Active test here ???
6061
6062          if Expander_Active
6063            and then
6064              (Ekind (T) =  E_Anonymous_Access_Type
6065                or else Ekind (T) = E_Anonymous_Access_Subprogram_Type
6066                or else Is_Private_Type (T))
6067          then
6068             if Etype (L) /= T then
6069                Rewrite (L,
6070                  Make_Unchecked_Type_Conversion (Sloc (L),
6071                    Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
6072                    Expression   => Relocate_Node (L)));
6073                Analyze_And_Resolve (L, T);
6074             end if;
6075
6076             if (Etype (R)) /= T then
6077                Rewrite (R,
6078                   Make_Unchecked_Type_Conversion (Sloc (R),
6079                     Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
6080                     Expression   => Relocate_Node (R)));
6081                Analyze_And_Resolve (R, T);
6082             end if;
6083          end if;
6084       end if;
6085    end Resolve_Equality_Op;
6086
6087    ----------------------------------
6088    -- Resolve_Explicit_Dereference --
6089    ----------------------------------
6090
6091    procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
6092       Loc   : constant Source_Ptr := Sloc (N);
6093       New_N : Node_Id;
6094       P     : constant Node_Id := Prefix (N);
6095       I     : Interp_Index;
6096       It    : Interp;
6097
6098    begin
6099       Check_Fully_Declared_Prefix (Typ, P);
6100
6101       if Is_Overloaded (P) then
6102
6103          --  Use the context type to select the prefix that has the correct
6104          --  designated type.
6105
6106          Get_First_Interp (P, I, It);
6107          while Present (It.Typ) loop
6108             exit when Is_Access_Type (It.Typ)
6109               and then Covers (Typ, Designated_Type (It.Typ));
6110             Get_Next_Interp (I, It);
6111          end loop;
6112
6113          if Present (It.Typ) then
6114             Resolve (P, It.Typ);
6115          else
6116             --  If no interpretation covers the designated type of the prefix,
6117             --  this is the pathological case where not all implementations of
6118             --  the prefix allow the interpretation of the node as a call. Now
6119             --  that the expected type is known, Remove other interpretations
6120             --  from prefix, rewrite it as a call, and resolve again, so that
6121             --  the proper call node is generated.
6122
6123             Get_First_Interp (P, I, It);
6124             while Present (It.Typ) loop
6125                if Ekind (It.Typ) /= E_Access_Subprogram_Type then
6126                   Remove_Interp (I);
6127                end if;
6128
6129                Get_Next_Interp (I, It);
6130             end loop;
6131
6132             New_N :=
6133               Make_Function_Call (Loc,
6134                 Name =>
6135                   Make_Explicit_Dereference (Loc,
6136                     Prefix => P),
6137                 Parameter_Associations => New_List);
6138
6139             Save_Interps (N, New_N);
6140             Rewrite (N, New_N);
6141             Analyze_And_Resolve (N, Typ);
6142             return;
6143          end if;
6144
6145          Set_Etype (N, Designated_Type (It.Typ));
6146
6147       else
6148          Resolve (P);
6149       end if;
6150
6151       if Is_Access_Type (Etype (P)) then
6152          Apply_Access_Check (N);
6153       end if;
6154
6155       --  If the designated type is a packed unconstrained array type, and the
6156       --  explicit dereference is not in the context of an attribute reference,
6157       --  then we must compute and set the actual subtype, since it is needed
6158       --  by Gigi. The reason we exclude the attribute case is that this is
6159       --  handled fine by Gigi, and in fact we use such attributes to build the
6160       --  actual subtype. We also exclude generated code (which builds actual
6161       --  subtypes directly if they are needed).
6162
6163       if Is_Array_Type (Etype (N))
6164         and then Is_Packed (Etype (N))
6165         and then not Is_Constrained (Etype (N))
6166         and then Nkind (Parent (N)) /= N_Attribute_Reference
6167         and then Comes_From_Source (N)
6168       then
6169          Set_Etype (N, Get_Actual_Subtype (N));
6170       end if;
6171
6172       --  Note: there is no Eval processing required for an explicit deference,
6173       --  because the type is known to be an allocators, and allocator
6174       --  expressions can never be static.
6175
6176    end Resolve_Explicit_Dereference;
6177
6178    -------------------------------
6179    -- Resolve_Indexed_Component --
6180    -------------------------------
6181
6182    procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
6183       Name       : constant Node_Id := Prefix  (N);
6184       Expr       : Node_Id;
6185       Array_Type : Entity_Id := Empty; -- to prevent junk warning
6186       Index      : Node_Id;
6187
6188    begin
6189       if Is_Overloaded (Name) then
6190
6191          --  Use the context type to select the prefix that yields the correct
6192          --  component type.
6193
6194          declare
6195             I     : Interp_Index;
6196             It    : Interp;
6197             I1    : Interp_Index := 0;
6198             P     : constant Node_Id := Prefix (N);
6199             Found : Boolean := False;
6200
6201          begin
6202             Get_First_Interp (P, I, It);
6203             while Present (It.Typ) loop
6204                if (Is_Array_Type (It.Typ)
6205                      and then Covers (Typ, Component_Type (It.Typ)))
6206                  or else (Is_Access_Type (It.Typ)
6207                             and then Is_Array_Type (Designated_Type (It.Typ))
6208                             and then Covers
6209                               (Typ, Component_Type (Designated_Type (It.Typ))))
6210                then
6211                   if Found then
6212                      It := Disambiguate (P, I1, I, Any_Type);
6213
6214                      if It = No_Interp then
6215                         Error_Msg_N ("ambiguous prefix for indexing",  N);
6216                         Set_Etype (N, Typ);
6217                         return;
6218
6219                      else
6220                         Found := True;
6221                         Array_Type := It.Typ;
6222                         I1 := I;
6223                      end if;
6224
6225                   else
6226                      Found := True;
6227                      Array_Type := It.Typ;
6228                      I1 := I;
6229                   end if;
6230                end if;
6231
6232                Get_Next_Interp (I, It);
6233             end loop;
6234          end;
6235
6236       else
6237          Array_Type := Etype (Name);
6238       end if;
6239
6240       Resolve (Name, Array_Type);
6241       Array_Type := Get_Actual_Subtype_If_Available (Name);
6242
6243       --  If prefix is access type, dereference to get real array type.
6244       --  Note: we do not apply an access check because the expander always
6245       --  introduces an explicit dereference, and the check will happen there.
6246
6247       if Is_Access_Type (Array_Type) then
6248          Array_Type := Designated_Type (Array_Type);
6249       end if;
6250
6251       --  If name was overloaded, set component type correctly now
6252       --  If a misplaced call to an entry family (which has no index types)
6253       --  return. Error will be diagnosed from calling context.
6254
6255       if Is_Array_Type (Array_Type) then
6256          Set_Etype (N, Component_Type (Array_Type));
6257       else
6258          return;
6259       end if;
6260
6261       Index := First_Index (Array_Type);
6262       Expr  := First (Expressions (N));
6263
6264       --  The prefix may have resolved to a string literal, in which case its
6265       --  etype has a special representation. This is only possible currently
6266       --  if the prefix is a static concatenation, written in functional
6267       --  notation.
6268
6269       if Ekind (Array_Type) = E_String_Literal_Subtype then
6270          Resolve (Expr, Standard_Positive);
6271
6272       else
6273          while Present (Index) and Present (Expr) loop
6274             Resolve (Expr, Etype (Index));
6275             Check_Unset_Reference (Expr);
6276
6277             if Is_Scalar_Type (Etype (Expr)) then
6278                Apply_Scalar_Range_Check (Expr, Etype (Index));
6279             else
6280                Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
6281             end if;
6282
6283             Next_Index (Index);
6284             Next (Expr);
6285          end loop;
6286       end if;
6287
6288       --  Do not generate the warning on suspicious index if we are analyzing
6289       --  package Ada.Tags; otherwise we will report the warning with the
6290       --  Prims_Ptr field of the dispatch table.
6291
6292       if Scope (Etype (Prefix (N))) = Standard_Standard
6293         or else not
6294           Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
6295                   Ada_Tags)
6296       then
6297          Warn_On_Suspicious_Index (Name, First (Expressions (N)));
6298          Eval_Indexed_Component (N);
6299       end if;
6300    end Resolve_Indexed_Component;
6301
6302    -----------------------------
6303    -- Resolve_Integer_Literal --
6304    -----------------------------
6305
6306    procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
6307    begin
6308       Set_Etype (N, Typ);
6309       Eval_Integer_Literal (N);
6310    end Resolve_Integer_Literal;
6311
6312    --------------------------------
6313    -- Resolve_Intrinsic_Operator --
6314    --------------------------------
6315
6316    procedure Resolve_Intrinsic_Operator  (N : Node_Id; Typ : Entity_Id) is
6317       Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
6318       Op   : Entity_Id;
6319       Arg1 : Node_Id;
6320       Arg2 : Node_Id;
6321
6322    begin
6323       Op := Entity (N);
6324       while Scope (Op) /= Standard_Standard loop
6325          Op := Homonym (Op);
6326          pragma Assert (Present (Op));
6327       end loop;
6328
6329       Set_Entity (N, Op);
6330       Set_Is_Overloaded (N, False);
6331
6332       --  If the operand type is private, rewrite with suitable conversions on
6333       --  the operands and the result, to expose the proper underlying numeric
6334       --  type.
6335
6336       if Is_Private_Type (Typ) then
6337          Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd  (N));
6338
6339          if Nkind (N) = N_Op_Expon then
6340             Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
6341          else
6342             Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
6343          end if;
6344
6345          Save_Interps (Left_Opnd (N),  Expression (Arg1));
6346          Save_Interps (Right_Opnd (N), Expression (Arg2));
6347
6348          Set_Left_Opnd  (N, Arg1);
6349          Set_Right_Opnd (N, Arg2);
6350
6351          Set_Etype (N, Btyp);
6352          Rewrite (N, Unchecked_Convert_To (Typ, N));
6353          Resolve (N, Typ);
6354
6355       elsif Typ /= Etype (Left_Opnd (N))
6356         or else Typ /= Etype (Right_Opnd (N))
6357       then
6358          --  Add explicit conversion where needed, and save interpretations
6359          --  in case operands are overloaded.
6360
6361          Arg1 := Convert_To (Typ, Left_Opnd  (N));
6362          Arg2 := Convert_To (Typ, Right_Opnd (N));
6363
6364          if Nkind (Arg1) = N_Type_Conversion then
6365             Save_Interps (Left_Opnd (N), Expression (Arg1));
6366          else
6367             Save_Interps (Left_Opnd (N), Arg1);
6368          end if;
6369
6370          if Nkind (Arg2) = N_Type_Conversion then
6371             Save_Interps (Right_Opnd (N), Expression (Arg2));
6372          else
6373             Save_Interps (Right_Opnd (N), Arg2);
6374          end if;
6375
6376          Rewrite (Left_Opnd  (N), Arg1);
6377          Rewrite (Right_Opnd (N), Arg2);
6378          Analyze (Arg1);
6379          Analyze (Arg2);
6380          Resolve_Arithmetic_Op (N, Typ);
6381
6382       else
6383          Resolve_Arithmetic_Op (N, Typ);
6384       end if;
6385    end Resolve_Intrinsic_Operator;
6386
6387    --------------------------------------
6388    -- Resolve_Intrinsic_Unary_Operator --
6389    --------------------------------------
6390
6391    procedure Resolve_Intrinsic_Unary_Operator
6392      (N   : Node_Id;
6393       Typ : Entity_Id)
6394    is
6395       Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
6396       Op   : Entity_Id;
6397       Arg2 : Node_Id;
6398
6399    begin
6400       Op := Entity (N);
6401       while Scope (Op) /= Standard_Standard loop
6402          Op := Homonym (Op);
6403          pragma Assert (Present (Op));
6404       end loop;
6405
6406       Set_Entity (N, Op);
6407
6408       if Is_Private_Type (Typ) then
6409          Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
6410          Save_Interps (Right_Opnd (N), Expression (Arg2));
6411
6412          Set_Right_Opnd (N, Arg2);
6413
6414          Set_Etype (N, Btyp);
6415          Rewrite (N, Unchecked_Convert_To (Typ, N));
6416          Resolve (N, Typ);
6417
6418       else
6419          Resolve_Unary_Op (N, Typ);
6420       end if;
6421    end Resolve_Intrinsic_Unary_Operator;
6422
6423    ------------------------
6424    -- Resolve_Logical_Op --
6425    ------------------------
6426
6427    procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
6428       B_Typ : Entity_Id;
6429       N_Opr : constant Node_Kind := Nkind (N);
6430
6431    begin
6432       --  Predefined operations on scalar types yield the base type. On the
6433       --  other hand, logical operations on arrays yield the type of the
6434       --  arguments (and the context).
6435
6436       if Is_Array_Type (Typ) then
6437          B_Typ := Typ;
6438       else
6439          B_Typ := Base_Type (Typ);
6440       end if;
6441
6442       --  The following test is required because the operands of the operation
6443       --  may be literals, in which case the resulting type appears to be
6444       --  compatible with a signed integer type, when in fact it is compatible
6445       --  only with modular types. If the context itself is universal, the
6446       --  operation is illegal.
6447
6448       if not Valid_Boolean_Arg (Typ) then
6449          Error_Msg_N ("invalid context for logical operation", N);
6450          Set_Etype (N, Any_Type);
6451          return;
6452
6453       elsif Typ = Any_Modular then
6454          Error_Msg_N
6455            ("no modular type available in this context", N);
6456          Set_Etype (N, Any_Type);
6457          return;
6458       elsif Is_Modular_Integer_Type (Typ)
6459         and then Etype (Left_Opnd (N)) = Universal_Integer
6460         and then Etype (Right_Opnd (N)) = Universal_Integer
6461       then
6462          Check_For_Visible_Operator (N, B_Typ);
6463       end if;
6464
6465       Resolve (Left_Opnd (N), B_Typ);
6466       Resolve (Right_Opnd (N), B_Typ);
6467
6468       Check_Unset_Reference (Left_Opnd  (N));
6469       Check_Unset_Reference (Right_Opnd (N));
6470
6471       Set_Etype (N, B_Typ);
6472       Generate_Operator_Reference (N, B_Typ);
6473       Eval_Logical_Op (N);
6474
6475       --  Check for violation of restriction No_Direct_Boolean_Operators
6476       --  if the operator was not eliminated by the Eval_Logical_Op call.
6477
6478       if Nkind (N) = N_Opr
6479         and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
6480       then
6481          Check_Restriction (No_Direct_Boolean_Operators, N);
6482       end if;
6483    end Resolve_Logical_Op;
6484
6485    ---------------------------
6486    -- Resolve_Membership_Op --
6487    ---------------------------
6488
6489    --  The context can only be a boolean type, and does not determine
6490    --  the arguments. Arguments should be unambiguous, but the preference
6491    --  rule for universal types applies.
6492
6493    procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
6494       pragma Warnings (Off, Typ);
6495
6496       L : constant Node_Id   := Left_Opnd (N);
6497       R : constant Node_Id   := Right_Opnd (N);
6498       T : Entity_Id;
6499
6500    begin
6501       if L = Error or else R = Error then
6502          return;
6503       end if;
6504
6505       if not Is_Overloaded (R)
6506         and then
6507           (Etype (R) = Universal_Integer or else
6508            Etype (R) = Universal_Real)
6509         and then Is_Overloaded (L)
6510       then
6511          T := Etype (R);
6512
6513       --  Ada 2005 (AI-251): Give support to the following case:
6514
6515       --      type I is interface;
6516       --      type T is tagged ...
6517
6518       --      function Test (O : I'Class) is
6519       --      begin
6520       --         return O in T'Class.
6521       --      end Test;
6522
6523       --  In this case we have nothing else to do; the membership test will be
6524       --  done at run-time.
6525
6526       elsif Ada_Version >= Ada_05
6527         and then Is_Class_Wide_Type (Etype (L))
6528         and then Is_Interface (Etype (L))
6529         and then Is_Class_Wide_Type (Etype (R))
6530         and then not Is_Interface (Etype (R))
6531       then
6532          return;
6533
6534       else
6535          T := Intersect_Types (L, R);
6536       end if;
6537
6538       Resolve (L, T);
6539       Check_Unset_Reference (L);
6540
6541       if Nkind (R) = N_Range
6542         and then not Is_Scalar_Type (T)
6543       then
6544          Error_Msg_N ("scalar type required for range", R);
6545       end if;
6546
6547       if Is_Entity_Name (R) then
6548          Freeze_Expression (R);
6549       else
6550          Resolve (R, T);
6551          Check_Unset_Reference (R);
6552       end if;
6553
6554       Eval_Membership_Op (N);
6555    end Resolve_Membership_Op;
6556
6557    ------------------
6558    -- Resolve_Null --
6559    ------------------
6560
6561    procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
6562    begin
6563       --  Handle restriction against anonymous null access values This
6564       --  restriction can be turned off using -gnatdj.
6565
6566       --  Ada 2005 (AI-231): Remove restriction
6567
6568       if Ada_Version < Ada_05
6569         and then not Debug_Flag_J
6570         and then Ekind (Typ) = E_Anonymous_Access_Type
6571         and then Comes_From_Source (N)
6572       then
6573          --  In the common case of a call which uses an explicitly null
6574          --  value for an access parameter, give specialized error message.
6575
6576          if Nkind_In (Parent (N), N_Procedure_Call_Statement,
6577                                   N_Function_Call)
6578          then
6579             Error_Msg_N
6580               ("null is not allowed as argument for an access parameter", N);
6581
6582          --  Standard message for all other cases (are there any?)
6583
6584          else
6585             Error_Msg_N
6586               ("null cannot be of an anonymous access type", N);
6587          end if;
6588       end if;
6589
6590       --  In a distributed context, null for a remote access to subprogram
6591       --  may need to be replaced with a special record aggregate. In this
6592       --  case, return after having done the transformation.
6593
6594       if (Ekind (Typ) = E_Record_Type
6595            or else Is_Remote_Access_To_Subprogram_Type (Typ))
6596         and then Remote_AST_Null_Value (N, Typ)
6597       then
6598          return;
6599       end if;
6600
6601       --  The null literal takes its type from the context
6602
6603       Set_Etype (N, Typ);
6604    end Resolve_Null;
6605
6606    -----------------------
6607    -- Resolve_Op_Concat --
6608    -----------------------
6609
6610    procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
6611
6612       --  We wish to avoid deep recursion, because concatenations are often
6613       --  deeply nested, as in A&B&...&Z. Therefore, we walk down the left
6614       --  operands nonrecursively until we find something that is not a simple
6615       --  concatenation (A in this case). We resolve that, and then walk back
6616       --  up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
6617       --  to do the rest of the work at each level. The Parent pointers allow
6618       --  us to avoid recursion, and thus avoid running out of memory. See also
6619       --  Sem_Ch4.Analyze_Concatenation, where a similar hack is used.
6620
6621       NN  : Node_Id := N;
6622       Op1 : Node_Id;
6623
6624    begin
6625       --  The following code is equivalent to:
6626
6627       --    Resolve_Op_Concat_First (NN, Typ);
6628       --    Resolve_Op_Concat_Arg (N, ...);
6629       --    Resolve_Op_Concat_Rest (N, Typ);
6630
6631       --  where the Resolve_Op_Concat_Arg call recurses back here if the left
6632       --  operand is a concatenation.
6633
6634       --  Walk down left operands
6635
6636       loop
6637          Resolve_Op_Concat_First (NN, Typ);
6638          Op1 := Left_Opnd (NN);
6639          exit when not (Nkind (Op1) = N_Op_Concat
6640                          and then not Is_Array_Type (Component_Type (Typ))
6641                          and then Entity (Op1) = Entity (NN));
6642          NN := Op1;
6643       end loop;
6644
6645       --  Now (given the above example) NN is A&B and Op1 is A
6646
6647       --  First resolve Op1 ...
6648
6649       Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd  (NN));
6650
6651       --  ... then walk NN back up until we reach N (where we started), calling
6652       --  Resolve_Op_Concat_Rest along the way.
6653
6654       loop
6655          Resolve_Op_Concat_Rest (NN, Typ);
6656          exit when NN = N;
6657          NN := Parent (NN);
6658       end loop;
6659    end Resolve_Op_Concat;
6660
6661    ---------------------------
6662    -- Resolve_Op_Concat_Arg --
6663    ---------------------------
6664
6665    procedure Resolve_Op_Concat_Arg
6666      (N       : Node_Id;
6667       Arg     : Node_Id;
6668       Typ     : Entity_Id;
6669       Is_Comp : Boolean)
6670    is
6671       Btyp : constant Entity_Id := Base_Type (Typ);
6672
6673    begin
6674       if In_Instance then
6675          if Is_Comp
6676            or else (not Is_Overloaded (Arg)
6677                      and then Etype (Arg) /= Any_Composite
6678                      and then Covers (Component_Type (Typ), Etype (Arg)))
6679          then
6680             Resolve (Arg, Component_Type (Typ));
6681          else
6682             Resolve (Arg, Btyp);
6683          end if;
6684
6685       elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then
6686          if Nkind (Arg) = N_Aggregate
6687            and then Is_Composite_Type (Component_Type (Typ))
6688          then
6689             if Is_Private_Type (Component_Type (Typ)) then
6690                Resolve (Arg, Btyp);
6691             else
6692                Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
6693                Set_Etype (Arg, Any_Type);
6694             end if;
6695
6696          else
6697             if Is_Overloaded (Arg)
6698               and then Has_Compatible_Type (Arg, Typ)
6699               and then Etype (Arg) /= Any_Type
6700             then
6701                declare
6702                   I    : Interp_Index;
6703                   It   : Interp;
6704                   Func : Entity_Id;
6705
6706                begin
6707                   Get_First_Interp (Arg, I, It);
6708                   Func := It.Nam;
6709                   Get_Next_Interp (I, It);
6710
6711                   --  Special-case the error message when the overloading is
6712                   --  caused by a function that yields an array and can be
6713                   --  called without parameters.
6714
6715                   if It.Nam = Func then
6716                      Error_Msg_Sloc := Sloc (Func);
6717                      Error_Msg_N ("ambiguous call to function#", Arg);
6718                      Error_Msg_NE
6719                        ("\\interpretation as call yields&", Arg, Typ);
6720                      Error_Msg_NE
6721                        ("\\interpretation as indexing of call yields&",
6722                          Arg, Component_Type (Typ));
6723
6724                   else
6725                      Error_Msg_N
6726                        ("ambiguous operand for concatenation!", Arg);
6727                      Get_First_Interp (Arg, I, It);
6728                      while Present (It.Nam) loop
6729                         Error_Msg_Sloc := Sloc (It.Nam);
6730
6731                         if Base_Type (It.Typ) = Base_Type (Typ)
6732                           or else Base_Type (It.Typ) =
6733                             Base_Type (Component_Type (Typ))
6734                         then
6735                            Error_Msg_N ("\\possible interpretation#", Arg);
6736                         end if;
6737
6738                         Get_Next_Interp (I, It);
6739                      end loop;
6740                   end if;
6741                end;
6742             end if;
6743
6744             Resolve (Arg, Component_Type (Typ));
6745
6746             if Nkind (Arg) = N_String_Literal then
6747                Set_Etype (Arg, Component_Type (Typ));
6748             end if;
6749
6750             if Arg = Left_Opnd (N) then
6751                Set_Is_Component_Left_Opnd (N);
6752             else
6753                Set_Is_Component_Right_Opnd (N);
6754             end if;
6755          end if;
6756
6757       else
6758          Resolve (Arg, Btyp);
6759       end if;
6760
6761       Check_Unset_Reference (Arg);
6762    end Resolve_Op_Concat_Arg;
6763
6764    -----------------------------
6765    -- Resolve_Op_Concat_First --
6766    -----------------------------
6767
6768    procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
6769       Btyp : constant Entity_Id := Base_Type (Typ);
6770       Op1  : constant Node_Id := Left_Opnd (N);
6771       Op2  : constant Node_Id := Right_Opnd (N);
6772
6773    begin
6774       --  The parser folds an enormous sequence of concatenations of string
6775       --  literals into "" & "...", where the Is_Folded_In_Parser flag is set
6776       --  in the right. If the expression resolves to a predefined "&"
6777       --  operator, all is well. Otherwise, the parser's folding is wrong, so
6778       --  we give an error. See P_Simple_Expression in Par.Ch4.
6779
6780       if Nkind (Op2) = N_String_Literal
6781         and then Is_Folded_In_Parser (Op2)
6782         and then Ekind (Entity (N)) = E_Function
6783       then
6784          pragma Assert (Nkind (Op1) = N_String_Literal  --  should be ""
6785                and then String_Length (Strval (Op1)) = 0);
6786          Error_Msg_N ("too many user-defined concatenations", N);
6787          return;
6788       end if;
6789
6790       Set_Etype (N, Btyp);
6791
6792       if Is_Limited_Composite (Btyp) then
6793          Error_Msg_N ("concatenation not available for limited array", N);
6794          Explain_Limited_Type (Btyp, N);
6795       end if;
6796    end Resolve_Op_Concat_First;
6797
6798    ----------------------------
6799    -- Resolve_Op_Concat_Rest --
6800    ----------------------------
6801
6802    procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
6803       Op1  : constant Node_Id := Left_Opnd (N);
6804       Op2  : constant Node_Id := Right_Opnd (N);
6805
6806    begin
6807       Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd  (N));
6808
6809       Generate_Operator_Reference (N, Typ);
6810
6811       if Is_String_Type (Typ) then
6812          Eval_Concatenation (N);
6813       end if;
6814
6815       --  If this is not a static concatenation, but the result is a
6816       --  string type (and not an array of strings) ensure that static
6817       --  string operands have their subtypes properly constructed.
6818
6819       if Nkind (N) /= N_String_Literal
6820         and then Is_Character_Type (Component_Type (Typ))
6821       then
6822          Set_String_Literal_Subtype (Op1, Typ);
6823          Set_String_Literal_Subtype (Op2, Typ);
6824       end if;
6825    end Resolve_Op_Concat_Rest;
6826
6827    ----------------------
6828    -- Resolve_Op_Expon --
6829    ----------------------
6830
6831    procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
6832       B_Typ : constant Entity_Id := Base_Type (Typ);
6833
6834    begin
6835       --  Catch attempts to do fixed-point exponentiation with universal
6836       --  operands, which is a case where the illegality is not caught during
6837       --  normal operator analysis.
6838
6839       if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
6840          Error_Msg_N ("exponentiation not available for fixed point", N);
6841          return;
6842       end if;
6843
6844       if Comes_From_Source (N)
6845         and then Ekind (Entity (N)) = E_Function
6846         and then Is_Imported (Entity (N))
6847         and then Is_Intrinsic_Subprogram (Entity (N))
6848       then
6849          Resolve_Intrinsic_Operator (N, Typ);
6850          return;
6851       end if;
6852
6853       if Etype (Left_Opnd (N)) = Universal_Integer
6854         or else Etype (Left_Opnd (N)) = Universal_Real
6855       then
6856          Check_For_Visible_Operator (N, B_Typ);
6857       end if;
6858
6859       --  We do the resolution using the base type, because intermediate values
6860       --  in expressions always are of the base type, not a subtype of it.
6861
6862       Resolve (Left_Opnd (N), B_Typ);
6863       Resolve (Right_Opnd (N), Standard_Integer);
6864
6865       Check_Unset_Reference (Left_Opnd  (N));
6866       Check_Unset_Reference (Right_Opnd (N));
6867
6868       Set_Etype (N, B_Typ);
6869       Generate_Operator_Reference (N, B_Typ);
6870       Eval_Op_Expon (N);
6871
6872       --  Set overflow checking bit. Much cleverer code needed here eventually
6873       --  and perhaps the Resolve routines should be separated for the various
6874       --  arithmetic operations, since they will need different processing. ???
6875
6876       if Nkind (N) in N_Op then
6877          if not Overflow_Checks_Suppressed (Etype (N)) then
6878             Enable_Overflow_Check (N);
6879          end if;
6880       end if;
6881    end Resolve_Op_Expon;
6882
6883    --------------------
6884    -- Resolve_Op_Not --
6885    --------------------
6886
6887    procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
6888       B_Typ : Entity_Id;
6889
6890       function Parent_Is_Boolean return Boolean;
6891       --  This function determines if the parent node is a boolean operator
6892       --  or operation (comparison op, membership test, or short circuit form)
6893       --  and the not in question is the left operand of this operation.
6894       --  Note that if the not is in parens, then false is returned.
6895
6896       -----------------------
6897       -- Parent_Is_Boolean --
6898       -----------------------
6899
6900       function Parent_Is_Boolean return Boolean is
6901       begin
6902          if Paren_Count (N) /= 0 then
6903             return False;
6904
6905          else
6906             case Nkind (Parent (N)) is
6907                when N_Op_And   |
6908                     N_Op_Eq    |
6909                     N_Op_Ge    |
6910                     N_Op_Gt    |
6911                     N_Op_Le    |
6912                     N_Op_Lt    |
6913                     N_Op_Ne    |
6914                     N_Op_Or    |
6915                     N_Op_Xor   |
6916                     N_In       |
6917                     N_Not_In   |
6918                     N_And_Then |
6919                     N_Or_Else  =>
6920
6921                   return Left_Opnd (Parent (N)) = N;
6922
6923                when others =>
6924                   return False;
6925             end case;
6926          end if;
6927       end Parent_Is_Boolean;
6928
6929    --  Start of processing for Resolve_Op_Not
6930
6931    begin
6932       --  Predefined operations on scalar types yield the base type. On the
6933       --  other hand, logical operations on arrays yield the type of the
6934       --  arguments (and the context).
6935
6936       if Is_Array_Type (Typ) then
6937          B_Typ := Typ;
6938       else
6939          B_Typ := Base_Type (Typ);
6940       end if;
6941
6942       --  Straightforward case of incorrect arguments
6943
6944       if not Valid_Boolean_Arg (Typ) then
6945          Error_Msg_N ("invalid operand type for operator&", N);
6946          Set_Etype (N, Any_Type);
6947          return;
6948
6949       --  Special case of probable missing parens
6950
6951       elsif Typ = Universal_Integer or else Typ = Any_Modular then
6952          if Parent_Is_Boolean then
6953             Error_Msg_N
6954               ("operand of not must be enclosed in parentheses",
6955                Right_Opnd (N));
6956          else
6957             Error_Msg_N
6958               ("no modular type available in this context", N);
6959          end if;
6960
6961          Set_Etype (N, Any_Type);
6962          return;
6963
6964       --  OK resolution of not
6965
6966       else
6967          --  Warn if non-boolean types involved. This is a case like not a < b
6968          --  where a and b are modular, where we will get (not a) < b and most
6969          --  likely not (a < b) was intended.
6970
6971          if Warn_On_Questionable_Missing_Parens
6972            and then not Is_Boolean_Type (Typ)
6973            and then Parent_Is_Boolean
6974          then
6975             Error_Msg_N ("?not expression should be parenthesized here!", N);
6976          end if;
6977
6978          --  Warn on double negation if checking redundant constructs
6979
6980          if Warn_On_Redundant_Constructs
6981            and then Comes_From_Source (N)
6982            and then Comes_From_Source (Right_Opnd (N))
6983            and then Root_Type (Typ) = Standard_Boolean
6984            and then Nkind (Right_Opnd (N)) = N_Op_Not
6985          then
6986             Error_Msg_N ("redundant double negation?", N);
6987          end if;
6988
6989          --  Complete resolution and evaluation of NOT
6990
6991          Resolve (Right_Opnd (N), B_Typ);
6992          Check_Unset_Reference (Right_Opnd (N));
6993          Set_Etype (N, B_Typ);
6994          Generate_Operator_Reference (N, B_Typ);
6995          Eval_Op_Not (N);
6996       end if;
6997    end Resolve_Op_Not;
6998
6999    -----------------------------
7000    -- Resolve_Operator_Symbol --
7001    -----------------------------
7002
7003    --  Nothing to be done, all resolved already
7004
7005    procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
7006       pragma Warnings (Off, N);
7007       pragma Warnings (Off, Typ);
7008
7009    begin
7010       null;
7011    end Resolve_Operator_Symbol;
7012
7013    ----------------------------------
7014    -- Resolve_Qualified_Expression --
7015    ----------------------------------
7016
7017    procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
7018       pragma Warnings (Off, Typ);
7019
7020       Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
7021       Expr       : constant Node_Id   := Expression (N);
7022
7023    begin
7024       Resolve (Expr, Target_Typ);
7025
7026       --  A qualified expression requires an exact match of the type,
7027       --  class-wide matching is not allowed. However, if the qualifying
7028       --  type is specific and the expression has a class-wide type, it
7029       --  may still be okay, since it can be the result of the expansion
7030       --  of a call to a dispatching function, so we also have to check
7031       --  class-wideness of the type of the expression's original node.
7032
7033       if (Is_Class_Wide_Type (Target_Typ)
7034            or else
7035              (Is_Class_Wide_Type (Etype (Expr))
7036                and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
7037         and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
7038       then
7039          Wrong_Type (Expr, Target_Typ);
7040       end if;
7041
7042       --  If the target type is unconstrained, then we reset the type of
7043       --  the result from the type of the expression. For other cases, the
7044       --  actual subtype of the expression is the target type.
7045
7046       if Is_Composite_Type (Target_Typ)
7047         and then not Is_Constrained (Target_Typ)
7048       then
7049          Set_Etype (N, Etype (Expr));
7050       end if;
7051
7052       Eval_Qualified_Expression (N);
7053    end Resolve_Qualified_Expression;
7054
7055    -------------------
7056    -- Resolve_Range --
7057    -------------------
7058
7059    procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
7060       L : constant Node_Id := Low_Bound (N);
7061       H : constant Node_Id := High_Bound (N);
7062
7063    begin
7064       Set_Etype (N, Typ);
7065       Resolve (L, Typ);
7066       Resolve (H, Typ);
7067
7068       Check_Unset_Reference (L);
7069       Check_Unset_Reference (H);
7070
7071       --  We have to check the bounds for being within the base range as
7072       --  required for a non-static context. Normally this is automatic and
7073       --  done as part of evaluating expressions, but the N_Range node is an
7074       --  exception, since in GNAT we consider this node to be a subexpression,
7075       --  even though in Ada it is not. The circuit in Sem_Eval could check for
7076       --  this, but that would put the test on the main evaluation path for
7077       --  expressions.
7078
7079       Check_Non_Static_Context (L);
7080       Check_Non_Static_Context (H);
7081
7082       --  Check for an ambiguous range over character literals. This will
7083       --  happen with a membership test involving only literals.
7084
7085       if Typ = Any_Character then
7086          Ambiguous_Character (L);
7087          Set_Etype (N, Any_Type);
7088          return;
7089       end if;
7090
7091       --  If bounds are static, constant-fold them, so size computations
7092       --  are identical between front-end and back-end. Do not perform this
7093       --  transformation while analyzing generic units, as type information
7094       --  would then be lost when reanalyzing the constant node in the
7095       --  instance.
7096
7097       if Is_Discrete_Type (Typ) and then Expander_Active then
7098          if Is_OK_Static_Expression (L) then
7099             Fold_Uint  (L, Expr_Value (L), Is_Static_Expression (L));
7100          end if;
7101
7102          if Is_OK_Static_Expression (H) then
7103             Fold_Uint  (H, Expr_Value (H), Is_Static_Expression (H));
7104          end if;
7105       end if;
7106    end Resolve_Range;
7107
7108    --------------------------
7109    -- Resolve_Real_Literal --
7110    --------------------------
7111
7112    procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
7113       Actual_Typ : constant Entity_Id := Etype (N);
7114
7115    begin
7116       --  Special processing for fixed-point literals to make sure that the
7117       --  value is an exact multiple of small where this is required. We
7118       --  skip this for the universal real case, and also for generic types.
7119
7120       if Is_Fixed_Point_Type (Typ)
7121         and then Typ /= Universal_Fixed
7122         and then Typ /= Any_Fixed
7123         and then not Is_Generic_Type (Typ)
7124       then
7125          declare
7126             Val   : constant Ureal := Realval (N);
7127             Cintr : constant Ureal := Val / Small_Value (Typ);
7128             Cint  : constant Uint  := UR_Trunc (Cintr);
7129             Den   : constant Uint  := Norm_Den (Cintr);
7130             Stat  : Boolean;
7131
7132          begin
7133             --  Case of literal is not an exact multiple of the Small
7134
7135             if Den /= 1 then
7136
7137                --  For a source program literal for a decimal fixed-point
7138                --  type, this is statically illegal (RM 4.9(36)).
7139
7140                if Is_Decimal_Fixed_Point_Type (Typ)
7141                  and then Actual_Typ = Universal_Real
7142                  and then Comes_From_Source (N)
7143                then
7144                   Error_Msg_N ("value has extraneous low order digits", N);
7145                end if;
7146
7147                --  Generate a warning if literal from source
7148
7149                if Is_Static_Expression (N)
7150                  and then Warn_On_Bad_Fixed_Value
7151                then
7152                   Error_Msg_N
7153                     ("?static fixed-point value is not a multiple of Small!",
7154                      N);
7155                end if;
7156
7157                --  Replace literal by a value that is the exact representation
7158                --  of a value of the type, i.e. a multiple of the small value,
7159                --  by truncation, since Machine_Rounds is false for all GNAT
7160                --  fixed-point types (RM 4.9(38)).
7161
7162                Stat := Is_Static_Expression (N);
7163                Rewrite (N,
7164                  Make_Real_Literal (Sloc (N),
7165                    Realval => Small_Value (Typ) * Cint));
7166
7167                Set_Is_Static_Expression (N, Stat);
7168             end if;
7169
7170             --  In all cases, set the corresponding integer field
7171
7172             Set_Corresponding_Integer_Value (N, Cint);
7173          end;
7174       end if;
7175
7176       --  Now replace the actual type by the expected type as usual
7177
7178       Set_Etype (N, Typ);
7179       Eval_Real_Literal (N);
7180    end Resolve_Real_Literal;
7181
7182    -----------------------
7183    -- Resolve_Reference --
7184    -----------------------
7185
7186    procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
7187       P : constant Node_Id := Prefix (N);
7188
7189    begin
7190       --  Replace general access with specific type
7191
7192       if Ekind (Etype (N)) = E_Allocator_Type then
7193          Set_Etype (N, Base_Type (Typ));
7194       end if;
7195
7196       Resolve (P, Designated_Type (Etype (N)));
7197
7198       --  If we are taking the reference of a volatile entity, then treat
7199       --  it as a potential modification of this entity. This is much too
7200       --  conservative, but is necessary because remove side effects can
7201       --  result in transformations of normal assignments into reference
7202       --  sequences that otherwise fail to notice the modification.
7203
7204       if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
7205          Note_Possible_Modification (P, Sure => False);
7206       end if;
7207    end Resolve_Reference;
7208
7209    --------------------------------
7210    -- Resolve_Selected_Component --
7211    --------------------------------
7212
7213    procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
7214       Comp  : Entity_Id;
7215       Comp1 : Entity_Id        := Empty; -- prevent junk warning
7216       P     : constant Node_Id := Prefix  (N);
7217       S     : constant Node_Id := Selector_Name (N);
7218       T     : Entity_Id        := Etype (P);
7219       I     : Interp_Index;
7220       I1    : Interp_Index := 0; -- prevent junk warning
7221       It    : Interp;
7222       It1   : Interp;
7223       Found : Boolean;
7224
7225       function Init_Component return Boolean;
7226       --  Check whether this is the initialization of a component within an
7227       --  init proc (by assignment or call to another init proc). If true,
7228       --  there is no need for a discriminant check.
7229
7230       --------------------
7231       -- Init_Component --
7232       --------------------
7233
7234       function Init_Component return Boolean is
7235       begin
7236          return Inside_Init_Proc
7237            and then Nkind (Prefix (N)) = N_Identifier
7238            and then Chars (Prefix (N)) = Name_uInit
7239            and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
7240       end Init_Component;
7241
7242    --  Start of processing for Resolve_Selected_Component
7243
7244    begin
7245       if Is_Overloaded (P) then
7246
7247          --  Use the context type to select the prefix that has a selector
7248          --  of the correct name and type.
7249
7250          Found := False;
7251          Get_First_Interp (P, I, It);
7252
7253          Search : while Present (It.Typ) loop
7254             if Is_Access_Type (It.Typ) then
7255                T := Designated_Type (It.Typ);
7256             else
7257                T := It.Typ;
7258             end if;
7259
7260             if Is_Record_Type (T) then
7261
7262                --  The visible components of a class-wide type are those of
7263                --  the root type.
7264
7265                if Is_Class_Wide_Type (T) then
7266                   T := Etype (T);
7267                end if;
7268
7269                Comp := First_Entity (T);
7270                while Present (Comp) loop
7271                   if Chars (Comp) = Chars (S)
7272                     and then Covers (Etype (Comp), Typ)
7273                   then
7274                      if not Found then
7275                         Found := True;
7276                         I1  := I;
7277                         It1 := It;
7278                         Comp1 := Comp;
7279
7280                      else
7281                         It := Disambiguate (P, I1, I, Any_Type);
7282
7283                         if It = No_Interp then
7284                            Error_Msg_N
7285                              ("ambiguous prefix for selected component",  N);
7286                            Set_Etype (N, Typ);
7287                            return;
7288
7289                         else
7290                            It1 := It;
7291
7292                            --  There may be an implicit dereference. Retrieve
7293                            --  designated record type.
7294
7295                            if Is_Access_Type (It1.Typ) then
7296                               T := Designated_Type (It1.Typ);
7297                            else
7298                               T := It1.Typ;
7299                            end if;
7300
7301                            if Scope (Comp1) /= T then
7302
7303                               --  Resolution chooses the new interpretation.
7304                               --  Find the component with the right name.
7305
7306                               Comp1 := First_Entity (T);
7307                               while Present (Comp1)
7308                                 and then Chars (Comp1) /= Chars (S)
7309                               loop
7310                                  Comp1 := Next_Entity (Comp1);
7311                               end loop;
7312                            end if;
7313
7314                            exit Search;
7315                         end if;
7316                      end if;
7317                   end if;
7318
7319                   Comp := Next_Entity (Comp);
7320                end loop;
7321
7322             end if;
7323
7324             Get_Next_Interp (I, It);
7325          end loop Search;
7326
7327          Resolve (P, It1.Typ);
7328          Set_Etype (N, Typ);
7329          Set_Entity_With_Style_Check (S, Comp1);
7330
7331       else
7332          --  Resolve prefix with its type
7333
7334          Resolve (P, T);
7335       end if;
7336
7337       --  Generate cross-reference. We needed to wait until full overloading
7338       --  resolution was complete to do this, since otherwise we can't tell if
7339       --  we are an Lvalue of not.
7340
7341       if May_Be_Lvalue (N) then
7342          Generate_Reference (Entity (S), S, 'm');
7343       else
7344          Generate_Reference (Entity (S), S, 'r');
7345       end if;
7346
7347       --  If prefix is an access type, the node will be transformed into an
7348       --  explicit dereference during expansion. The type of the node is the
7349       --  designated type of that of the prefix.
7350
7351       if Is_Access_Type (Etype (P)) then
7352          T := Designated_Type (Etype (P));
7353          Check_Fully_Declared_Prefix (T, P);
7354       else
7355          T := Etype (P);
7356       end if;
7357
7358       if Has_Discriminants (T)
7359         and then (Ekind (Entity (S)) = E_Component
7360                    or else
7361                   Ekind (Entity (S)) = E_Discriminant)
7362         and then Present (Original_Record_Component (Entity (S)))
7363         and then Ekind (Original_Record_Component (Entity (S))) = E_Component
7364         and then Present (Discriminant_Checking_Func
7365                            (Original_Record_Component (Entity (S))))
7366         and then not Discriminant_Checks_Suppressed (T)
7367         and then not Init_Component
7368       then
7369          Set_Do_Discriminant_Check (N);
7370       end if;
7371
7372       if Ekind (Entity (S)) = E_Void then
7373          Error_Msg_N ("premature use of component", S);
7374       end if;
7375
7376       --  If the prefix is a record conversion, this may be a renamed
7377       --  discriminant whose bounds differ from those of the original
7378       --  one, so we must ensure that a range check is performed.
7379
7380       if Nkind (P) = N_Type_Conversion
7381         and then Ekind (Entity (S)) = E_Discriminant
7382         and then Is_Discrete_Type (Typ)
7383       then
7384          Set_Etype (N, Base_Type (Typ));
7385       end if;
7386
7387       --  Note: No Eval processing is required, because the prefix is of a
7388       --  record type, or protected type, and neither can possibly be static.
7389
7390    end Resolve_Selected_Component;
7391
7392    -------------------
7393    -- Resolve_Shift --
7394    -------------------
7395
7396    procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
7397       B_Typ : constant Entity_Id := Base_Type (Typ);
7398       L     : constant Node_Id   := Left_Opnd  (N);
7399       R     : constant Node_Id   := Right_Opnd (N);
7400
7401    begin
7402       --  We do the resolution using the base type, because intermediate values
7403       --  in expressions always are of the base type, not a subtype of it.
7404
7405       Resolve (L, B_Typ);
7406       Resolve (R, Standard_Natural);
7407
7408       Check_Unset_Reference (L);
7409       Check_Unset_Reference (R);
7410
7411       Set_Etype (N, B_Typ);
7412       Generate_Operator_Reference (N, B_Typ);
7413       Eval_Shift (N);
7414    end Resolve_Shift;
7415
7416    ---------------------------
7417    -- Resolve_Short_Circuit --
7418    ---------------------------
7419
7420    procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
7421       B_Typ : constant Entity_Id := Base_Type (Typ);
7422       L     : constant Node_Id   := Left_Opnd  (N);
7423       R     : constant Node_Id   := Right_Opnd (N);
7424
7425    begin
7426       Resolve (L, B_Typ);
7427       Resolve (R, B_Typ);
7428
7429       --  Check for issuing warning for always False assert/check, this happens
7430       --  when assertions are turned off, in which case the pragma Assert/Check
7431       --  was transformed into:
7432
7433       --     if False and then <condition> then ...
7434
7435       --  and we detect this pattern
7436
7437       if Warn_On_Assertion_Failure
7438         and then Is_Entity_Name (R)
7439         and then Entity (R) = Standard_False
7440         and then Nkind (Parent (N)) = N_If_Statement
7441         and then Nkind (N) = N_And_Then
7442         and then Is_Entity_Name (L)
7443         and then Entity (L) = Standard_False
7444       then
7445          declare
7446             Orig : constant Node_Id := Original_Node (Parent (N));
7447
7448          begin
7449             if Nkind (Orig) = N_Pragma
7450               and then Pragma_Name (Orig) = Name_Assert
7451             then
7452                --  Don't want to warn if original condition is explicit False
7453
7454                declare
7455                   Expr : constant Node_Id :=
7456                            Original_Node
7457                              (Expression
7458                                (First (Pragma_Argument_Associations (Orig))));
7459                begin
7460                   if Is_Entity_Name (Expr)
7461                     and then Entity (Expr) = Standard_False
7462                   then
7463                      null;
7464                   else
7465                      --  Issue warning. Note that we don't want to make this
7466                      --  an unconditional warning, because if the assert is
7467                      --  within deleted code we do not want the warning. But
7468                      --  we do not want the deletion of the IF/AND-THEN to
7469                      --  take this message with it. We achieve this by making
7470                      --  sure that the expanded code points to the Sloc of
7471                      --  the expression, not the original pragma.
7472
7473                      Error_Msg_N ("?assertion would fail at run-time", Orig);
7474                   end if;
7475                end;
7476
7477             --  Similar processing for Check pragma
7478
7479             elsif Nkind (Orig) = N_Pragma
7480               and then Pragma_Name (Orig) = Name_Check
7481             then
7482                --  Don't want to warn if original condition is explicit False
7483
7484                declare
7485                   Expr : constant Node_Id :=
7486                            Original_Node
7487                              (Expression
7488                                 (Next (First
7489                                   (Pragma_Argument_Associations (Orig)))));
7490                begin
7491                   if Is_Entity_Name (Expr)
7492                     and then Entity (Expr) = Standard_False
7493                   then
7494                      null;
7495                   else
7496                      Error_Msg_N ("?check would fail at run-time", Orig);
7497                   end if;
7498                end;
7499             end if;
7500          end;
7501       end if;
7502
7503       --  Continue with processing of short circuit
7504
7505       Check_Unset_Reference (L);
7506       Check_Unset_Reference (R);
7507
7508       Set_Etype (N, B_Typ);
7509       Eval_Short_Circuit (N);
7510    end Resolve_Short_Circuit;
7511
7512    -------------------
7513    -- Resolve_Slice --
7514    -------------------
7515
7516    procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
7517       Name       : constant Node_Id := Prefix (N);
7518       Drange     : constant Node_Id := Discrete_Range (N);
7519       Array_Type : Entity_Id        := Empty;
7520       Index      : Node_Id;
7521
7522    begin
7523       if Is_Overloaded (Name) then
7524
7525          --  Use the context type to select the prefix that yields the
7526          --  correct array type.
7527
7528          declare
7529             I      : Interp_Index;
7530             I1     : Interp_Index := 0;
7531             It     : Interp;
7532             P      : constant Node_Id := Prefix (N);
7533             Found  : Boolean := False;
7534
7535          begin
7536             Get_First_Interp (P, I,  It);
7537             while Present (It.Typ) loop
7538                if (Is_Array_Type (It.Typ)
7539                     and then Covers (Typ,  It.Typ))
7540                  or else (Is_Access_Type (It.Typ)
7541                            and then Is_Array_Type (Designated_Type (It.Typ))
7542                            and then Covers (Typ, Designated_Type (It.Typ)))
7543                then
7544                   if Found then
7545                      It := Disambiguate (P, I1, I, Any_Type);
7546
7547                      if It = No_Interp then
7548                         Error_Msg_N ("ambiguous prefix for slicing",  N);
7549                         Set_Etype (N, Typ);
7550                         return;
7551                      else
7552                         Found := True;
7553                         Array_Type := It.Typ;
7554                         I1 := I;
7555                      end if;
7556                   else
7557                      Found := True;
7558                      Array_Type := It.Typ;
7559                      I1 := I;
7560                   end if;
7561                end if;
7562
7563                Get_Next_Interp (I, It);
7564             end loop;
7565          end;
7566
7567       else
7568          Array_Type := Etype (Name);
7569       end if;
7570
7571       Resolve (Name, Array_Type);
7572
7573       if Is_Access_Type (Array_Type) then
7574          Apply_Access_Check (N);
7575          Array_Type := Designated_Type (Array_Type);
7576
7577          --  If the prefix is an access to an unconstrained array, we must use
7578          --  the actual subtype of the object to perform the index checks. The
7579          --  object denoted by the prefix is implicit in the node, so we build
7580          --  an explicit representation for it in order to compute the actual
7581          --  subtype.
7582
7583          if not Is_Constrained (Array_Type) then
7584             Remove_Side_Effects (Prefix (N));
7585
7586             declare
7587                Obj : constant Node_Id :=
7588                        Make_Explicit_Dereference (Sloc (N),
7589                          Prefix => New_Copy_Tree (Prefix (N)));
7590             begin
7591                Set_Etype (Obj, Array_Type);
7592                Set_Parent (Obj, Parent (N));
7593                Array_Type := Get_Actual_Subtype (Obj);
7594             end;
7595          end if;
7596
7597       elsif Is_Entity_Name (Name)
7598         or else (Nkind (Name) = N_Function_Call
7599                   and then not Is_Constrained (Etype (Name)))
7600       then
7601          Array_Type := Get_Actual_Subtype (Name);
7602
7603       --  If the name is a selected component that depends on discriminants,
7604       --  build an actual subtype for it. This can happen only when the name
7605       --  itself is overloaded; otherwise the actual subtype is created when
7606       --  the selected component is analyzed.
7607
7608       elsif Nkind (Name) = N_Selected_Component
7609         and then Full_Analysis
7610         and then Depends_On_Discriminant (First_Index (Array_Type))
7611       then
7612          declare
7613             Act_Decl : constant Node_Id :=
7614                          Build_Actual_Subtype_Of_Component (Array_Type, Name);
7615          begin
7616             Insert_Action (N, Act_Decl);
7617             Array_Type := Defining_Identifier (Act_Decl);
7618          end;
7619       end if;
7620
7621       --  If name was overloaded, set slice type correctly now
7622
7623       Set_Etype (N, Array_Type);
7624
7625       --  If the range is specified by a subtype mark, no resolution is
7626       --  necessary. Else resolve the bounds, and apply needed checks.
7627
7628       if not Is_Entity_Name (Drange) then
7629          Index := First_Index (Array_Type);
7630          Resolve (Drange, Base_Type (Etype (Index)));
7631
7632          if Nkind (Drange) = N_Range
7633
7634             --  Do not apply the range check to nodes associated with the
7635             --  frontend expansion of the dispatch table. We first check
7636             --  if Ada.Tags is already loaded to void the addition of an
7637             --  undesired dependence on such run-time unit.
7638
7639            and then
7640              (VM_Target /= No_VM
7641               or else not
7642                 (RTU_Loaded (Ada_Tags)
7643                   and then Nkind (Prefix (N)) = N_Selected_Component
7644                   and then Present (Entity (Selector_Name (Prefix (N))))
7645                   and then Entity (Selector_Name (Prefix (N))) =
7646                                         RTE_Record_Component (RE_Prims_Ptr)))
7647          then
7648             Apply_Range_Check (Drange, Etype (Index));
7649          end if;
7650       end if;
7651
7652       Set_Slice_Subtype (N);
7653
7654       if Nkind (Drange) = N_Range then
7655          Warn_On_Suspicious_Index (Name, Low_Bound  (Drange));
7656          Warn_On_Suspicious_Index (Name, High_Bound (Drange));
7657       end if;
7658
7659       Eval_Slice (N);
7660    end Resolve_Slice;
7661
7662    ----------------------------
7663    -- Resolve_String_Literal --
7664    ----------------------------
7665
7666    procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
7667       C_Typ      : constant Entity_Id  := Component_Type (Typ);
7668       R_Typ      : constant Entity_Id  := Root_Type (C_Typ);
7669       Loc        : constant Source_Ptr := Sloc (N);
7670       Str        : constant String_Id  := Strval (N);
7671       Strlen     : constant Nat        := String_Length (Str);
7672       Subtype_Id : Entity_Id;
7673       Need_Check : Boolean;
7674
7675    begin
7676       --  For a string appearing in a concatenation, defer creation of the
7677       --  string_literal_subtype until the end of the resolution of the
7678       --  concatenation, because the literal may be constant-folded away. This
7679       --  is a useful optimization for long concatenation expressions.
7680
7681       --  If the string is an aggregate built for a single character (which
7682       --  happens in a non-static context) or a is null string to which special
7683       --  checks may apply, we build the subtype. Wide strings must also get a
7684       --  string subtype if they come from a one character aggregate. Strings
7685       --  generated by attributes might be static, but it is often hard to
7686       --  determine whether the enclosing context is static, so we generate
7687       --  subtypes for them as well, thus losing some rarer optimizations ???
7688       --  Same for strings that come from a static conversion.
7689
7690       Need_Check :=
7691         (Strlen = 0 and then Typ /= Standard_String)
7692           or else Nkind (Parent (N)) /= N_Op_Concat
7693           or else (N /= Left_Opnd (Parent (N))
7694                     and then N /= Right_Opnd (Parent (N)))
7695           or else ((Typ = Standard_Wide_String
7696                       or else Typ = Standard_Wide_Wide_String)
7697                     and then Nkind (Original_Node (N)) /= N_String_Literal);
7698
7699       --  If the resolving type is itself a string literal subtype, we
7700       --  can just reuse it, since there is no point in creating another.
7701
7702       if Ekind (Typ) = E_String_Literal_Subtype then
7703          Subtype_Id := Typ;
7704
7705       elsif Nkind (Parent (N)) = N_Op_Concat
7706         and then not Need_Check
7707         and then not Nkind_In (Original_Node (N), N_Character_Literal,
7708                                                   N_Attribute_Reference,
7709                                                   N_Qualified_Expression,
7710                                                   N_Type_Conversion)
7711       then
7712          Subtype_Id := Typ;
7713
7714       --  Otherwise we must create a string literal subtype. Note that the
7715       --  whole idea of string literal subtypes is simply to avoid the need
7716       --  for building a full fledged array subtype for each literal.
7717
7718       else
7719          Set_String_Literal_Subtype (N, Typ);
7720          Subtype_Id := Etype (N);
7721       end if;
7722
7723       if Nkind (Parent (N)) /= N_Op_Concat
7724         or else Need_Check
7725       then
7726          Set_Etype (N, Subtype_Id);
7727          Eval_String_Literal (N);
7728       end if;
7729
7730       if Is_Limited_Composite (Typ)
7731         or else Is_Private_Composite (Typ)
7732       then
7733          Error_Msg_N ("string literal not available for private array", N);
7734          Set_Etype (N, Any_Type);
7735          return;
7736       end if;
7737
7738       --  The validity of a null string has been checked in the
7739       --  call to  Eval_String_Literal.
7740
7741       if Strlen = 0 then
7742          return;
7743
7744       --  Always accept string literal with component type Any_Character, which
7745       --  occurs in error situations and in comparisons of literals, both of
7746       --  which should accept all literals.
7747
7748       elsif R_Typ = Any_Character then
7749          return;
7750
7751       --  If the type is bit-packed, then we always transform the string
7752       --  literal into a full fledged aggregate.
7753
7754       elsif Is_Bit_Packed_Array (Typ) then
7755          null;
7756
7757       --  Deal with cases of Wide_Wide_String, Wide_String, and String
7758
7759       else
7760          --  For Standard.Wide_Wide_String, or any other type whose component
7761          --  type is Standard.Wide_Wide_Character, we know that all the
7762          --  characters in the string must be acceptable, since the parser
7763          --  accepted the characters as valid character literals.
7764
7765          if R_Typ = Standard_Wide_Wide_Character then
7766             null;
7767
7768          --  For the case of Standard.String, or any other type whose component
7769          --  type is Standard.Character, we must make sure that there are no
7770          --  wide characters in the string, i.e. that it is entirely composed
7771          --  of characters in range of type Character.
7772
7773          --  If the string literal is the result of a static concatenation, the
7774          --  test has already been performed on the components, and need not be
7775          --  repeated.
7776
7777          elsif R_Typ = Standard_Character
7778            and then Nkind (Original_Node (N)) /= N_Op_Concat
7779          then
7780             for J in 1 .. Strlen loop
7781                if not In_Character_Range (Get_String_Char (Str, J)) then
7782
7783                   --  If we are out of range, post error. This is one of the
7784                   --  very few places that we place the flag in the middle of
7785                   --  a token, right under the offending wide character.
7786
7787                   Error_Msg
7788                     ("literal out of range of type Standard.Character",
7789                      Source_Ptr (Int (Loc) + J));
7790                   return;
7791                end if;
7792             end loop;
7793
7794          --  For the case of Standard.Wide_String, or any other type whose
7795          --  component type is Standard.Wide_Character, we must make sure that
7796          --  there are no wide characters in the string, i.e. that it is
7797          --  entirely composed of characters in range of type Wide_Character.
7798
7799          --  If the string literal is the result of a static concatenation,
7800          --  the test has already been performed on the components, and need
7801          --  not be repeated.
7802
7803          elsif R_Typ = Standard_Wide_Character
7804            and then Nkind (Original_Node (N)) /= N_Op_Concat
7805          then
7806             for J in 1 .. Strlen loop
7807                if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
7808
7809                   --  If we are out of range, post error. This is one of the
7810                   --  very few places that we place the flag in the middle of
7811                   --  a token, right under the offending wide character.
7812
7813                   --  This is not quite right, because characters in general
7814                   --  will take more than one character position ???
7815
7816                   Error_Msg
7817                     ("literal out of range of type Standard.Wide_Character",
7818                      Source_Ptr (Int (Loc) + J));
7819                   return;
7820                end if;
7821             end loop;
7822
7823          --  If the root type is not a standard character, then we will convert
7824          --  the string into an aggregate and will let the aggregate code do
7825          --  the checking. Standard Wide_Wide_Character is also OK here.
7826
7827          else
7828             null;
7829          end if;
7830
7831          --  See if the component type of the array corresponding to the string
7832          --  has compile time known bounds. If yes we can directly check
7833          --  whether the evaluation of the string will raise constraint error.
7834          --  Otherwise we need to transform the string literal into the
7835          --  corresponding character aggregate and let the aggregate
7836          --  code do the checking.
7837
7838          if Is_Standard_Character_Type (R_Typ) then
7839
7840             --  Check for the case of full range, where we are definitely OK
7841
7842             if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
7843                return;
7844             end if;
7845
7846             --  Here the range is not the complete base type range, so check
7847
7848             declare
7849                Comp_Typ_Lo : constant Node_Id :=
7850                                Type_Low_Bound (Component_Type (Typ));
7851                Comp_Typ_Hi : constant Node_Id :=
7852                                Type_High_Bound (Component_Type (Typ));
7853
7854                Char_Val : Uint;
7855
7856             begin
7857                if Compile_Time_Known_Value (Comp_Typ_Lo)
7858                  and then Compile_Time_Known_Value (Comp_Typ_Hi)
7859                then
7860                   for J in 1 .. Strlen loop
7861                      Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
7862
7863                      if Char_Val < Expr_Value (Comp_Typ_Lo)
7864                        or else Char_Val > Expr_Value (Comp_Typ_Hi)
7865                      then
7866                         Apply_Compile_Time_Constraint_Error
7867                           (N, "character out of range?", CE_Range_Check_Failed,
7868                            Loc => Source_Ptr (Int (Loc) + J));
7869                      end if;
7870                   end loop;
7871
7872                   return;
7873                end if;
7874             end;
7875          end if;
7876       end if;
7877
7878       --  If we got here we meed to transform the string literal into the
7879       --  equivalent qualified positional array aggregate. This is rather
7880       --  heavy artillery for this situation, but it is hard work to avoid.
7881
7882       declare
7883          Lits : constant List_Id    := New_List;
7884          P    : Source_Ptr := Loc + 1;
7885          C    : Char_Code;
7886
7887       begin
7888          --  Build the character literals, we give them source locations that
7889          --  correspond to the string positions, which is a bit tricky given
7890          --  the possible presence of wide character escape sequences.
7891
7892          for J in 1 .. Strlen loop
7893             C := Get_String_Char (Str, J);
7894             Set_Character_Literal_Name (C);
7895
7896             Append_To (Lits,
7897               Make_Character_Literal (P,
7898                 Chars              => Name_Find,
7899                 Char_Literal_Value => UI_From_CC (C)));
7900
7901             if In_Character_Range (C) then
7902                P := P + 1;
7903
7904             --  Should we have a call to Skip_Wide here ???
7905             --  ???     else
7906             --             Skip_Wide (P);
7907
7908             end if;
7909          end loop;
7910
7911          Rewrite (N,
7912            Make_Qualified_Expression (Loc,
7913              Subtype_Mark => New_Reference_To (Typ, Loc),
7914              Expression   =>
7915                Make_Aggregate (Loc, Expressions => Lits)));
7916
7917          Analyze_And_Resolve (N, Typ);
7918       end;
7919    end Resolve_String_Literal;
7920
7921    -----------------------------
7922    -- Resolve_Subprogram_Info --
7923    -----------------------------
7924
7925    procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
7926    begin
7927       Set_Etype (N, Typ);
7928    end Resolve_Subprogram_Info;
7929
7930    -----------------------------
7931    -- Resolve_Type_Conversion --
7932    -----------------------------
7933
7934    procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
7935       Conv_OK     : constant Boolean := Conversion_OK (N);
7936       Operand     : constant Node_Id := Expression (N);
7937       Operand_Typ : constant Entity_Id := Etype (Operand);
7938       Target_Typ  : constant Entity_Id := Etype (N);
7939       Rop         : Node_Id;
7940       Orig_N      : Node_Id;
7941       Orig_T      : Node_Id;
7942
7943    begin
7944       if not Conv_OK
7945         and then not Valid_Conversion (N, Target_Typ, Operand)
7946       then
7947          return;
7948       end if;
7949
7950       if Etype (Operand) = Any_Fixed then
7951
7952          --  Mixed-mode operation involving a literal. Context must be a fixed
7953          --  type which is applied to the literal subsequently.
7954
7955          if Is_Fixed_Point_Type (Typ) then
7956             Set_Etype (Operand, Universal_Real);
7957
7958          elsif Is_Numeric_Type (Typ)
7959            and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
7960            and then (Etype (Right_Opnd (Operand)) = Universal_Real
7961                        or else
7962                      Etype (Left_Opnd  (Operand)) = Universal_Real)
7963          then
7964             --  Return if expression is ambiguous
7965
7966             if Unique_Fixed_Point_Type (N) = Any_Type then
7967                return;
7968
7969             --  If nothing else, the available fixed type is Duration
7970
7971             else
7972                Set_Etype (Operand, Standard_Duration);
7973             end if;
7974
7975             --  Resolve the real operand with largest available precision
7976
7977             if Etype (Right_Opnd (Operand)) = Universal_Real then
7978                Rop := New_Copy_Tree (Right_Opnd (Operand));
7979             else
7980                Rop := New_Copy_Tree (Left_Opnd (Operand));
7981             end if;
7982
7983             Resolve (Rop, Universal_Real);
7984
7985             --  If the operand is a literal (it could be a non-static and
7986             --  illegal exponentiation) check whether the use of Duration
7987             --  is potentially inaccurate.
7988
7989             if Nkind (Rop) = N_Real_Literal
7990               and then Realval (Rop) /= Ureal_0
7991               and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
7992             then
7993                Error_Msg_N
7994                  ("?universal real operand can only " &
7995                   "be interpreted as Duration!",
7996                   Rop);
7997                Error_Msg_N
7998                  ("\?precision will be lost in the conversion!", Rop);
7999             end if;
8000
8001          elsif Is_Numeric_Type (Typ)
8002            and then Nkind (Operand) in N_Op
8003            and then Unique_Fixed_Point_Type (N) /= Any_Type
8004          then
8005             Set_Etype (Operand, Standard_Duration);
8006
8007          else
8008             Error_Msg_N ("invalid context for mixed mode operation", N);
8009             Set_Etype (Operand, Any_Type);
8010             return;
8011          end if;
8012       end if;
8013
8014       Resolve (Operand);
8015
8016       --  Note: we do the Eval_Type_Conversion call before applying the
8017       --  required checks for a subtype conversion. This is important,
8018       --  since both are prepared under certain circumstances to change
8019       --  the type conversion to a constraint error node, but in the case
8020       --  of Eval_Type_Conversion this may reflect an illegality in the
8021       --  static case, and we would miss the illegality (getting only a
8022       --  warning message), if we applied the type conversion checks first.
8023
8024       Eval_Type_Conversion (N);
8025
8026       --  Even when evaluation is not possible, we may be able to simplify
8027       --  the conversion or its expression. This needs to be done before
8028       --  applying checks, since otherwise the checks may use the original
8029       --  expression and defeat the simplifications. This is specifically
8030       --  the case for elimination of the floating-point Truncation
8031       --  attribute in float-to-int conversions.
8032
8033       Simplify_Type_Conversion (N);
8034
8035       --  If after evaluation we still have a type conversion, then we
8036       --  may need to apply checks required for a subtype conversion.
8037
8038       --  Skip these type conversion checks if universal fixed operands
8039       --  operands involved, since range checks are handled separately for
8040       --  these cases (in the appropriate Expand routines in unit Exp_Fixd).
8041
8042       if Nkind (N) = N_Type_Conversion
8043         and then not Is_Generic_Type (Root_Type (Target_Typ))
8044         and then Target_Typ  /= Universal_Fixed
8045         and then Operand_Typ /= Universal_Fixed
8046       then
8047          Apply_Type_Conversion_Checks (N);
8048       end if;
8049
8050       --  Issue warning for conversion of simple object to its own type
8051       --  We have to test the original nodes, since they may have been
8052       --  rewritten by various optimizations.
8053
8054       Orig_N := Original_Node (N);
8055
8056       if Warn_On_Redundant_Constructs
8057         and then Comes_From_Source (Orig_N)
8058         and then Nkind (Orig_N) = N_Type_Conversion
8059         and then not In_Instance
8060       then
8061          Orig_N := Original_Node (Expression (Orig_N));
8062          Orig_T := Target_Typ;
8063
8064          --  If the node is part of a larger expression, the Target_Type
8065          --  may not be the original type of the node if the context is a
8066          --  condition. Recover original type to see if conversion is needed.
8067
8068          if Is_Boolean_Type (Orig_T)
8069           and then Nkind (Parent (N)) in N_Op
8070          then
8071             Orig_T := Etype (Parent (N));
8072          end if;
8073
8074          if Is_Entity_Name (Orig_N)
8075            and then
8076              (Etype (Entity (Orig_N)) = Orig_T
8077                 or else
8078                   (Ekind (Entity (Orig_N)) = E_Loop_Parameter
8079                      and then Covers (Orig_T, Etype (Entity (Orig_N)))))
8080          then
8081             Error_Msg_Node_2 := Orig_T;
8082             Error_Msg_NE
8083               ("?redundant conversion, & is of type &!", N, Entity (Orig_N));
8084          end if;
8085       end if;
8086
8087       --  Ada 2005 (AI-251): Handle class-wide interface type conversions.
8088       --  No need to perform any interface conversion if the type of the
8089       --  expression coincides with the target type.
8090
8091       if Ada_Version >= Ada_05
8092         and then Expander_Active
8093         and then Operand_Typ /= Target_Typ
8094       then
8095          declare
8096             Opnd   : Entity_Id := Operand_Typ;
8097             Target : Entity_Id := Target_Typ;
8098
8099          begin
8100             if Is_Access_Type (Opnd) then
8101                Opnd := Directly_Designated_Type (Opnd);
8102             end if;
8103
8104             if Is_Access_Type (Target_Typ) then
8105                Target := Directly_Designated_Type (Target);
8106             end if;
8107
8108             if Opnd = Target then
8109                null;
8110
8111             --  Conversion from interface type
8112
8113             elsif Is_Interface (Opnd) then
8114
8115                --  Ada 2005 (AI-217): Handle entities from limited views
8116
8117                if From_With_Type (Opnd) then
8118                   Error_Msg_Qual_Level := 99;
8119                   Error_Msg_NE ("missing with-clause on package &", N,
8120                     Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
8121                   Error_Msg_N
8122                     ("type conversions require visibility of the full view",
8123                      N);
8124
8125                elsif From_With_Type (Target)
8126                  and then not
8127                    (Is_Access_Type (Target_Typ)
8128                       and then Present (Non_Limited_View (Etype (Target))))
8129                then
8130                   Error_Msg_Qual_Level := 99;
8131                   Error_Msg_NE ("missing with-clause on package &", N,
8132                     Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
8133                   Error_Msg_N
8134                     ("type conversions require visibility of the full view",
8135                      N);
8136
8137                else
8138                   Expand_Interface_Conversion (N, Is_Static => False);
8139                end if;
8140
8141             --  Conversion to interface type
8142
8143             elsif Is_Interface (Target) then
8144
8145                --  Handle subtypes
8146
8147                if Ekind (Opnd) = E_Protected_Subtype
8148                  or else Ekind (Opnd) = E_Task_Subtype
8149                then
8150                   Opnd := Etype (Opnd);
8151                end if;
8152
8153                if not Interface_Present_In_Ancestor
8154                         (Typ   => Opnd,
8155                          Iface => Target)
8156                then
8157                   if Is_Class_Wide_Type (Opnd) then
8158
8159                      --  The static analysis is not enough to know if the
8160                      --  interface is implemented or not. Hence we must pass
8161                      --  the work to the expander to generate code to evaluate
8162                      --  the conversion at run-time.
8163
8164                      Expand_Interface_Conversion (N, Is_Static => False);
8165
8166                   else
8167                      Error_Msg_Name_1 := Chars (Etype (Target));
8168                      Error_Msg_Name_2 := Chars (Opnd);
8169                      Error_Msg_N
8170                        ("wrong interface conversion (% is not a progenitor " &
8171                         "of %)", N);
8172                   end if;
8173
8174                else
8175                   Expand_Interface_Conversion (N);
8176                end if;
8177             end if;
8178          end;
8179       end if;
8180    end Resolve_Type_Conversion;
8181
8182    ----------------------
8183    -- Resolve_Unary_Op --
8184    ----------------------
8185
8186    procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
8187       B_Typ : constant Entity_Id := Base_Type (Typ);
8188       R     : constant Node_Id   := Right_Opnd (N);
8189       OK    : Boolean;
8190       Lo    : Uint;
8191       Hi    : Uint;
8192
8193    begin
8194       --  Deal with intrinsic unary operators
8195
8196       if Comes_From_Source (N)
8197         and then Ekind (Entity (N)) = E_Function
8198         and then Is_Imported (Entity (N))
8199         and then Is_Intrinsic_Subprogram (Entity (N))
8200       then
8201          Resolve_Intrinsic_Unary_Operator (N, Typ);
8202          return;
8203       end if;
8204
8205       --  Deal with universal cases
8206
8207       if Etype (R) = Universal_Integer
8208            or else
8209          Etype (R) = Universal_Real
8210       then
8211          Check_For_Visible_Operator (N, B_Typ);
8212       end if;
8213
8214       Set_Etype (N, B_Typ);
8215       Resolve (R, B_Typ);
8216
8217       --  Generate warning for expressions like abs (x mod 2)
8218
8219       if Warn_On_Redundant_Constructs
8220         and then Nkind (N) = N_Op_Abs
8221       then
8222          Determine_Range (Right_Opnd (N), OK, Lo, Hi);
8223
8224          if OK and then Hi >= Lo and then Lo >= 0 then
8225             Error_Msg_N
8226              ("?abs applied to known non-negative value has no effect", N);
8227          end if;
8228       end if;
8229
8230       --  Deal with reference generation
8231
8232       Check_Unset_Reference (R);
8233       Generate_Operator_Reference (N, B_Typ);
8234       Eval_Unary_Op (N);
8235
8236       --  Set overflow checking bit. Much cleverer code needed here eventually
8237       --  and perhaps the Resolve routines should be separated for the various
8238       --  arithmetic operations, since they will need different processing ???
8239
8240       if Nkind (N) in N_Op then
8241          if not Overflow_Checks_Suppressed (Etype (N)) then
8242             Enable_Overflow_Check (N);
8243          end if;
8244       end if;
8245
8246       --  Generate warning for expressions like -5 mod 3 for integers. No
8247       --  need to worry in the floating-point case, since parens do not affect
8248       --  the result so there is no point in giving in a warning.
8249
8250       declare
8251          Norig : constant Node_Id := Original_Node (N);
8252          Rorig : Node_Id;
8253          Val   : Uint;
8254          HB    : Uint;
8255          LB    : Uint;
8256          Lval  : Uint;
8257          Opnd  : Node_Id;
8258
8259       begin
8260          if Warn_On_Questionable_Missing_Parens
8261            and then Comes_From_Source (Norig)
8262            and then Is_Integer_Type (Typ)
8263            and then Nkind (Norig) = N_Op_Minus
8264          then
8265             Rorig := Original_Node (Right_Opnd (Norig));
8266
8267             --  We are looking for cases where the right operand is not
8268             --  parenthesized, and is a binary operator, multiply, divide, or
8269             --  mod. These are the cases where the grouping can affect results.
8270
8271             if Paren_Count (Rorig) = 0
8272               and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
8273             then
8274                --  For mod, we always give the warning, since the value is
8275                --  affected by the parenthesization (e.g. (-5) mod 315 /=
8276                --  (5 mod 315)). But for the other cases, the only concern is
8277                --  overflow, e.g. for the case of 8 big signed (-(2 * 64)
8278                --  overflows, but (-2) * 64 does not). So we try to give the
8279                --  message only when overflow is possible.
8280
8281                if Nkind (Rorig) /= N_Op_Mod
8282                  and then Compile_Time_Known_Value (R)
8283                then
8284                   Val := Expr_Value (R);
8285
8286                   if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
8287                      HB := Expr_Value (Type_High_Bound (Typ));
8288                   else
8289                      HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
8290                   end if;
8291
8292                   if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
8293                      LB := Expr_Value (Type_Low_Bound (Typ));
8294                   else
8295                      LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
8296                   end if;
8297
8298                   --  Note that the test below is deliberately excluding
8299                   --  the largest negative number, since that is a potentially
8300                   --  troublesome case (e.g. -2 * x, where the result is the
8301                   --  largest negative integer has an overflow with 2 * x).
8302
8303                   if Val > LB and then Val <= HB then
8304                      return;
8305                   end if;
8306                end if;
8307
8308                --  For the multiplication case, the only case we have to worry
8309                --  about is when (-a)*b is exactly the largest negative number
8310                --  so that -(a*b) can cause overflow. This can only happen if
8311                --  a is a power of 2, and more generally if any operand is a
8312                --  constant that is not a power of 2, then the parentheses
8313                --  cannot affect whether overflow occurs. We only bother to
8314                --  test the left most operand
8315
8316                --  Loop looking at left operands for one that has known value
8317
8318                Opnd := Rorig;
8319                Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
8320                   if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
8321                      Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
8322
8323                      --  Operand value of 0 or 1 skips warning
8324
8325                      if Lval <= 1 then
8326                         return;
8327
8328                      --  Otherwise check power of 2, if power of 2, warn, if
8329                      --  anything else, skip warning.
8330
8331                      else
8332                         while Lval /= 2 loop
8333                            if Lval mod 2 = 1 then
8334                               return;
8335                            else
8336                               Lval := Lval / 2;
8337                            end if;
8338                         end loop;
8339
8340                         exit Opnd_Loop;
8341                      end if;
8342                   end if;
8343
8344                   --  Keep looking at left operands
8345
8346                   Opnd := Left_Opnd (Opnd);
8347                end loop Opnd_Loop;
8348
8349                --  For rem or "/" we can only have a problematic situation
8350                --  if the divisor has a value of minus one or one. Otherwise
8351                --  overflow is impossible (divisor > 1) or we have a case of
8352                --  division by zero in any case.
8353
8354                if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
8355                  and then Compile_Time_Known_Value (Right_Opnd (Rorig))
8356                  and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
8357                then
8358                   return;
8359                end if;
8360
8361                --  If we fall through warning should be issued
8362
8363                Error_Msg_N
8364                  ("?unary minus expression should be parenthesized here!", N);
8365             end if;
8366          end if;
8367       end;
8368    end Resolve_Unary_Op;
8369
8370    ----------------------------------
8371    -- Resolve_Unchecked_Expression --
8372    ----------------------------------
8373
8374    procedure Resolve_Unchecked_Expression
8375      (N   : Node_Id;
8376       Typ : Entity_Id)
8377    is
8378    begin
8379       Resolve (Expression (N), Typ, Suppress => All_Checks);
8380       Set_Etype (N, Typ);
8381    end Resolve_Unchecked_Expression;
8382
8383    ---------------------------------------
8384    -- Resolve_Unchecked_Type_Conversion --
8385    ---------------------------------------
8386
8387    procedure Resolve_Unchecked_Type_Conversion
8388      (N   : Node_Id;
8389       Typ : Entity_Id)
8390    is
8391       pragma Warnings (Off, Typ);
8392
8393       Operand   : constant Node_Id   := Expression (N);
8394       Opnd_Type : constant Entity_Id := Etype (Operand);
8395
8396    begin
8397       --  Resolve operand using its own type
8398
8399       Resolve (Operand, Opnd_Type);
8400       Eval_Unchecked_Conversion (N);
8401
8402    end Resolve_Unchecked_Type_Conversion;
8403
8404    ------------------------------
8405    -- Rewrite_Operator_As_Call --
8406    ------------------------------
8407
8408    procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
8409       Loc     : constant Source_Ptr := Sloc (N);
8410       Actuals : constant List_Id    := New_List;
8411       New_N   : Node_Id;
8412
8413    begin
8414       if Nkind (N) in  N_Binary_Op then
8415          Append (Left_Opnd (N), Actuals);
8416       end if;
8417
8418       Append (Right_Opnd (N), Actuals);
8419
8420       New_N :=
8421         Make_Function_Call (Sloc => Loc,
8422           Name => New_Occurrence_Of (Nam, Loc),
8423           Parameter_Associations => Actuals);
8424
8425       Preserve_Comes_From_Source (New_N, N);
8426       Preserve_Comes_From_Source (Name (New_N), N);
8427       Rewrite (N, New_N);
8428       Set_Etype (N, Etype (Nam));
8429    end Rewrite_Operator_As_Call;
8430
8431    ------------------------------
8432    -- Rewrite_Renamed_Operator --
8433    ------------------------------
8434
8435    procedure Rewrite_Renamed_Operator
8436      (N   : Node_Id;
8437       Op  : Entity_Id;
8438       Typ : Entity_Id)
8439    is
8440       Nam       : constant Name_Id := Chars (Op);
8441       Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
8442       Op_Node   : Node_Id;
8443
8444    begin
8445       --  Rewrite the operator node using the real operator, not its
8446       --  renaming. Exclude user-defined intrinsic operations of the same
8447       --  name, which are treated separately and rewritten as calls.
8448
8449       if Ekind (Op) /= E_Function
8450         or else Chars (N) /= Nam
8451       then
8452          Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
8453          Set_Chars      (Op_Node, Nam);
8454          Set_Etype      (Op_Node, Etype (N));
8455          Set_Entity     (Op_Node, Op);
8456          Set_Right_Opnd (Op_Node, Right_Opnd (N));
8457
8458          --  Indicate that both the original entity and its renaming are
8459          --  referenced at this point.
8460
8461          Generate_Reference (Entity (N), N);
8462          Generate_Reference (Op, N);
8463
8464          if Is_Binary then
8465             Set_Left_Opnd  (Op_Node, Left_Opnd  (N));
8466          end if;
8467
8468          Rewrite (N, Op_Node);
8469
8470          --  If the context type is private, add the appropriate conversions
8471          --  so that the operator is applied to the full view. This is done
8472          --  in the routines that resolve intrinsic operators,
8473
8474          if Is_Intrinsic_Subprogram (Op)
8475            and then Is_Private_Type (Typ)
8476          then
8477             case Nkind (N) is
8478                when N_Op_Add   | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
8479                     N_Op_Expon | N_Op_Mod      | N_Op_Rem      =>
8480                   Resolve_Intrinsic_Operator (N, Typ);
8481
8482                when N_Op_Plus | N_Op_Minus    | N_Op_Abs      =>
8483                   Resolve_Intrinsic_Unary_Operator (N, Typ);
8484
8485                when others =>
8486                   Resolve (N, Typ);
8487             end case;
8488          end if;
8489
8490       elsif Ekind (Op) = E_Function
8491         and then Is_Intrinsic_Subprogram (Op)
8492       then
8493          --  Operator renames a user-defined operator of the same name. Use
8494          --  the original operator in the node, which is the one that Gigi
8495          --  knows about.
8496
8497          Set_Entity (N, Op);
8498          Set_Is_Overloaded (N, False);
8499       end if;
8500    end Rewrite_Renamed_Operator;
8501
8502    -----------------------
8503    -- Set_Slice_Subtype --
8504    -----------------------
8505
8506    --  Build an implicit subtype declaration to represent the type delivered
8507    --  by the slice. This is an abbreviated version of an array subtype. We
8508    --  define an index subtype for the slice, using either the subtype name
8509    --  or the discrete range of the slice. To be consistent with index usage
8510    --  elsewhere, we create a list header to hold the single index. This list
8511    --  is not otherwise attached to the syntax tree.
8512
8513    procedure Set_Slice_Subtype (N : Node_Id) is
8514       Loc           : constant Source_Ptr := Sloc (N);
8515       Index_List    : constant List_Id    := New_List;
8516       Index         : Node_Id;
8517       Index_Subtype : Entity_Id;
8518       Index_Type    : Entity_Id;
8519       Slice_Subtype : Entity_Id;
8520       Drange        : constant Node_Id := Discrete_Range (N);
8521
8522    begin
8523       if Is_Entity_Name (Drange) then
8524          Index_Subtype := Entity (Drange);
8525
8526       else
8527          --  We force the evaluation of a range. This is definitely needed in
8528          --  the renamed case, and seems safer to do unconditionally. Note in
8529          --  any case that since we will create and insert an Itype referring
8530          --  to this range, we must make sure any side effect removal actions
8531          --  are inserted before the Itype definition.
8532
8533          if Nkind (Drange) = N_Range then
8534             Force_Evaluation (Low_Bound (Drange));
8535             Force_Evaluation (High_Bound (Drange));
8536          end if;
8537
8538          Index_Type := Base_Type (Etype (Drange));
8539
8540          Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
8541
8542          Set_Scalar_Range (Index_Subtype, Drange);
8543          Set_Etype        (Index_Subtype, Index_Type);
8544          Set_Size_Info    (Index_Subtype, Index_Type);
8545          Set_RM_Size      (Index_Subtype, RM_Size (Index_Type));
8546       end if;
8547
8548       Slice_Subtype := Create_Itype (E_Array_Subtype, N);
8549
8550       Index := New_Occurrence_Of (Index_Subtype, Loc);
8551       Set_Etype (Index, Index_Subtype);
8552       Append (Index, Index_List);
8553
8554       Set_First_Index    (Slice_Subtype, Index);
8555       Set_Etype          (Slice_Subtype, Base_Type (Etype (N)));
8556       Set_Is_Constrained (Slice_Subtype, True);
8557
8558       Check_Compile_Time_Size (Slice_Subtype);
8559
8560       --  The Etype of the existing Slice node is reset to this slice subtype.
8561       --  Its bounds are obtained from its first index.
8562
8563       Set_Etype (N, Slice_Subtype);
8564
8565       --  In the packed case, this must be immediately frozen
8566
8567       --  Couldn't we always freeze here??? and if we did, then the above
8568       --  call to Check_Compile_Time_Size could be eliminated, which would
8569       --  be nice, because then that routine could be made private to Freeze.
8570
8571       --  Why the test for In_Spec_Expression here ???
8572
8573       if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
8574          Freeze_Itype (Slice_Subtype, N);
8575       end if;
8576
8577    end Set_Slice_Subtype;
8578
8579    --------------------------------
8580    -- Set_String_Literal_Subtype --
8581    --------------------------------
8582
8583    procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
8584       Loc        : constant Source_Ptr := Sloc (N);
8585       Low_Bound  : constant Node_Id :=
8586                         Type_Low_Bound (Etype (First_Index (Typ)));
8587       Subtype_Id : Entity_Id;
8588
8589    begin
8590       if Nkind (N) /= N_String_Literal then
8591          return;
8592       end if;
8593
8594       Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
8595       Set_String_Literal_Length (Subtype_Id, UI_From_Int
8596                                                (String_Length (Strval (N))));
8597       Set_Etype          (Subtype_Id, Base_Type (Typ));
8598       Set_Is_Constrained (Subtype_Id);
8599       Set_Etype          (N, Subtype_Id);
8600
8601       if Is_OK_Static_Expression (Low_Bound) then
8602
8603       --  The low bound is set from the low bound of the corresponding
8604       --  index type. Note that we do not store the high bound in the
8605       --  string literal subtype, but it can be deduced if necessary
8606       --  from the length and the low bound.
8607
8608          Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
8609
8610       else
8611          Set_String_Literal_Low_Bound
8612            (Subtype_Id, Make_Integer_Literal (Loc, 1));
8613          Set_Etype (String_Literal_Low_Bound (Subtype_Id), Standard_Positive);
8614
8615          --  Build bona fide subtype for the string, and wrap it in an
8616          --  unchecked conversion, because the backend expects the
8617          --  String_Literal_Subtype to have a static lower bound.
8618
8619          declare
8620             Index_List    : constant List_Id    := New_List;
8621             Index_Type    : constant Entity_Id := Etype (First_Index (Typ));
8622             High_Bound    : constant Node_Id :=
8623                                Make_Op_Add (Loc,
8624                                   Left_Opnd => New_Copy_Tree (Low_Bound),
8625                                   Right_Opnd =>
8626                                     Make_Integer_Literal (Loc,
8627                                       String_Length (Strval (N)) - 1));
8628             Array_Subtype : Entity_Id;
8629             Index_Subtype : Entity_Id;
8630             Drange        : Node_Id;
8631             Index         : Node_Id;
8632
8633          begin
8634             Index_Subtype :=
8635               Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
8636             Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
8637             Set_Scalar_Range (Index_Subtype, Drange);
8638             Set_Parent (Drange, N);
8639             Analyze_And_Resolve (Drange, Index_Type);
8640
8641             --  In the context, the Index_Type may already have a constraint,
8642             --  so use common base type on string subtype. The base type may
8643             --  be used when generating attributes of the string, for example
8644             --  in the context of a slice assignment.
8645
8646             Set_Etype        (Index_Subtype, Base_Type (Index_Type));
8647             Set_Size_Info    (Index_Subtype, Index_Type);
8648             Set_RM_Size      (Index_Subtype, RM_Size (Index_Type));
8649
8650             Array_Subtype := Create_Itype (E_Array_Subtype, N);
8651
8652             Index := New_Occurrence_Of (Index_Subtype, Loc);
8653             Set_Etype (Index, Index_Subtype);
8654             Append (Index, Index_List);
8655
8656             Set_First_Index    (Array_Subtype, Index);
8657             Set_Etype          (Array_Subtype, Base_Type (Typ));
8658             Set_Is_Constrained (Array_Subtype, True);
8659
8660             Rewrite (N,
8661               Make_Unchecked_Type_Conversion (Loc,
8662                 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
8663                 Expression => Relocate_Node (N)));
8664             Set_Etype (N, Array_Subtype);
8665          end;
8666       end if;
8667    end Set_String_Literal_Subtype;
8668
8669    ------------------------------
8670    -- Simplify_Type_Conversion --
8671    ------------------------------
8672
8673    procedure Simplify_Type_Conversion (N : Node_Id) is
8674    begin
8675       if Nkind (N) = N_Type_Conversion then
8676          declare
8677             Operand    : constant Node_Id   := Expression (N);
8678             Target_Typ : constant Entity_Id := Etype (N);
8679             Opnd_Typ   : constant Entity_Id := Etype (Operand);
8680
8681          begin
8682             if Is_Floating_Point_Type (Opnd_Typ)
8683               and then
8684                 (Is_Integer_Type (Target_Typ)
8685                    or else (Is_Fixed_Point_Type (Target_Typ)
8686                               and then Conversion_OK (N)))
8687               and then Nkind (Operand) = N_Attribute_Reference
8688               and then Attribute_Name (Operand) = Name_Truncation
8689
8690             --  Special processing required if the conversion is the expression
8691             --  of a Truncation attribute reference. In this case we replace:
8692
8693             --     ityp (ftyp'Truncation (x))
8694
8695             --  by
8696
8697             --     ityp (x)
8698
8699             --  with the Float_Truncate flag set, which is more efficient
8700
8701             then
8702                Rewrite (Operand,
8703                  Relocate_Node (First (Expressions (Operand))));
8704                Set_Float_Truncate (N, True);
8705             end if;
8706          end;
8707       end if;
8708    end Simplify_Type_Conversion;
8709
8710    -----------------------------
8711    -- Unique_Fixed_Point_Type --
8712    -----------------------------
8713
8714    function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
8715       T1   : Entity_Id := Empty;
8716       T2   : Entity_Id;
8717       Item : Node_Id;
8718       Scop : Entity_Id;
8719
8720       procedure Fixed_Point_Error;
8721       --  If true ambiguity, give details
8722
8723       -----------------------
8724       -- Fixed_Point_Error --
8725       -----------------------
8726
8727       procedure Fixed_Point_Error is
8728       begin
8729          Error_Msg_N ("ambiguous universal_fixed_expression", N);
8730          Error_Msg_NE ("\\possible interpretation as}", N, T1);
8731          Error_Msg_NE ("\\possible interpretation as}", N, T2);
8732       end Fixed_Point_Error;
8733
8734    --  Start of processing for Unique_Fixed_Point_Type
8735
8736    begin
8737       --  The operations on Duration are visible, so Duration is always a
8738       --  possible interpretation.
8739
8740       T1 := Standard_Duration;
8741
8742       --  Look for fixed-point types in enclosing scopes
8743
8744       Scop := Current_Scope;
8745       while Scop /= Standard_Standard loop
8746          T2 := First_Entity (Scop);
8747          while Present (T2) loop
8748             if Is_Fixed_Point_Type (T2)
8749               and then Current_Entity (T2) = T2
8750               and then Scope (Base_Type (T2)) = Scop
8751             then
8752                if Present (T1) then
8753                   Fixed_Point_Error;
8754                   return Any_Type;
8755                else
8756                   T1 := T2;
8757                end if;
8758             end if;
8759
8760             Next_Entity (T2);
8761          end loop;
8762
8763          Scop := Scope (Scop);
8764       end loop;
8765
8766       --  Look for visible fixed type declarations in the context
8767
8768       Item := First (Context_Items (Cunit (Current_Sem_Unit)));
8769       while Present (Item) loop
8770          if Nkind (Item) = N_With_Clause then
8771             Scop := Entity (Name (Item));
8772             T2 := First_Entity (Scop);
8773             while Present (T2) loop
8774                if Is_Fixed_Point_Type (T2)
8775                  and then Scope (Base_Type (T2)) = Scop
8776                  and then (Is_Potentially_Use_Visible (T2)
8777                              or else In_Use (T2))
8778                then
8779                   if Present (T1) then
8780                      Fixed_Point_Error;
8781                      return Any_Type;
8782                   else
8783                      T1 := T2;
8784                   end if;
8785                end if;
8786
8787                Next_Entity (T2);
8788             end loop;
8789          end if;
8790
8791          Next (Item);
8792       end loop;
8793
8794       if Nkind (N) = N_Real_Literal then
8795          Error_Msg_NE ("?real literal interpreted as }!", N, T1);
8796       else
8797          Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
8798       end if;
8799
8800       return T1;
8801    end Unique_Fixed_Point_Type;
8802
8803    ----------------------
8804    -- Valid_Conversion --
8805    ----------------------
8806
8807    function Valid_Conversion
8808      (N       : Node_Id;
8809       Target  : Entity_Id;
8810       Operand : Node_Id) return Boolean
8811    is
8812       Target_Type : constant Entity_Id := Base_Type (Target);
8813       Opnd_Type   : Entity_Id := Etype (Operand);
8814
8815       function Conversion_Check
8816         (Valid : Boolean;
8817          Msg   : String) return Boolean;
8818       --  Little routine to post Msg if Valid is False, returns Valid value
8819
8820       function Valid_Tagged_Conversion
8821         (Target_Type : Entity_Id;
8822          Opnd_Type   : Entity_Id) return Boolean;
8823       --  Specifically test for validity of tagged conversions
8824
8825       function Valid_Array_Conversion return Boolean;
8826       --  Check index and component conformance, and accessibility levels
8827       --  if the component types are anonymous access types (Ada 2005)
8828
8829       ----------------------
8830       -- Conversion_Check --
8831       ----------------------
8832
8833       function Conversion_Check
8834         (Valid : Boolean;
8835          Msg   : String) return Boolean
8836       is
8837       begin
8838          if not Valid then
8839             Error_Msg_N (Msg, Operand);
8840          end if;
8841
8842          return Valid;
8843       end Conversion_Check;
8844
8845       ----------------------------
8846       -- Valid_Array_Conversion --
8847       ----------------------------
8848
8849       function Valid_Array_Conversion return Boolean
8850       is
8851          Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
8852          Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
8853
8854          Opnd_Index      : Node_Id;
8855          Opnd_Index_Type : Entity_Id;
8856
8857          Target_Comp_Type : constant Entity_Id :=
8858                               Component_Type (Target_Type);
8859          Target_Comp_Base : constant Entity_Id :=
8860                               Base_Type (Target_Comp_Type);
8861
8862          Target_Index      : Node_Id;
8863          Target_Index_Type : Entity_Id;
8864
8865       begin
8866          --  Error if wrong number of dimensions
8867
8868          if
8869            Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
8870          then
8871             Error_Msg_N
8872               ("incompatible number of dimensions for conversion", Operand);
8873             return False;
8874
8875          --  Number of dimensions matches
8876
8877          else
8878             --  Loop through indexes of the two arrays
8879
8880             Target_Index := First_Index (Target_Type);
8881             Opnd_Index   := First_Index (Opnd_Type);
8882             while Present (Target_Index) and then Present (Opnd_Index) loop
8883                Target_Index_Type := Etype (Target_Index);
8884                Opnd_Index_Type   := Etype (Opnd_Index);
8885
8886                --  Error if index types are incompatible
8887
8888                if not (Is_Integer_Type (Target_Index_Type)
8889                        and then Is_Integer_Type (Opnd_Index_Type))
8890                  and then (Root_Type (Target_Index_Type)
8891                            /= Root_Type (Opnd_Index_Type))
8892                then
8893                   Error_Msg_N
8894                     ("incompatible index types for array conversion",
8895                      Operand);
8896                   return False;
8897                end if;
8898
8899                Next_Index (Target_Index);
8900                Next_Index (Opnd_Index);
8901             end loop;
8902
8903             --  If component types have same base type, all set
8904
8905             if Target_Comp_Base  = Opnd_Comp_Base then
8906                null;
8907
8908                --  Here if base types of components are not the same. The only
8909                --  time this is allowed is if we have anonymous access types.
8910
8911                --  The conversion of arrays of anonymous access types can lead
8912                --  to dangling pointers. AI-392 formalizes the accessibility
8913                --  checks that must be applied to such conversions to prevent
8914                --  out-of-scope references.
8915
8916             elsif
8917               (Ekind (Target_Comp_Base) = E_Anonymous_Access_Type
8918                  or else
8919                Ekind (Target_Comp_Base) = E_Anonymous_Access_Subprogram_Type)
8920               and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
8921               and then
8922                 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
8923             then
8924                if Type_Access_Level (Target_Type) <
8925                    Type_Access_Level (Opnd_Type)
8926                then
8927                   if In_Instance_Body then
8928                      Error_Msg_N ("?source array type " &
8929                        "has deeper accessibility level than target", Operand);
8930                      Error_Msg_N ("\?Program_Error will be raised at run time",
8931                          Operand);
8932                      Rewrite (N,
8933                        Make_Raise_Program_Error (Sloc (N),
8934                          Reason => PE_Accessibility_Check_Failed));
8935                      Set_Etype (N, Target_Type);
8936                      return False;
8937
8938                   --  Conversion not allowed because of accessibility levels
8939
8940                   else
8941                      Error_Msg_N ("source array type " &
8942                        "has deeper accessibility level than target", Operand);
8943                      return False;
8944                   end if;
8945                else
8946                   null;
8947                end if;
8948
8949             --  All other cases where component base types do not match
8950
8951             else
8952                Error_Msg_N
8953                  ("incompatible component types for array conversion",
8954                   Operand);
8955                return False;
8956             end if;
8957
8958             --  Check that component subtypes statically match. For numeric
8959             --  types this means that both must be either constrained or
8960             --  unconstrained. For enumeration types the bounds must match.
8961             --  All of this is checked in Subtypes_Statically_Match.
8962
8963             if not Subtypes_Statically_Match
8964                             (Target_Comp_Type, Opnd_Comp_Type)
8965             then
8966                Error_Msg_N
8967                  ("component subtypes must statically match", Operand);
8968                return False;
8969             end if;
8970          end if;
8971
8972          return True;
8973       end Valid_Array_Conversion;
8974
8975       -----------------------------
8976       -- Valid_Tagged_Conversion --
8977       -----------------------------
8978
8979       function Valid_Tagged_Conversion
8980         (Target_Type : Entity_Id;
8981          Opnd_Type   : Entity_Id) return Boolean
8982       is
8983       begin
8984          --  Upward conversions are allowed (RM 4.6(22))
8985
8986          if Covers (Target_Type, Opnd_Type)
8987            or else Is_Ancestor (Target_Type, Opnd_Type)
8988          then
8989             return True;
8990
8991          --  Downward conversion are allowed if the operand is class-wide
8992          --  (RM 4.6(23)).
8993
8994          elsif Is_Class_Wide_Type (Opnd_Type)
8995            and then Covers (Opnd_Type, Target_Type)
8996          then
8997             return True;
8998
8999          elsif Covers (Opnd_Type, Target_Type)
9000            or else Is_Ancestor (Opnd_Type, Target_Type)
9001          then
9002             return
9003               Conversion_Check (False,
9004                 "downward conversion of tagged objects not allowed");
9005
9006          --  Ada 2005 (AI-251): The conversion to/from interface types is
9007          --  always valid
9008
9009          elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
9010             return True;
9011
9012          --  If the operand is a class-wide type obtained through a limited_
9013          --  with clause, and the context includes the non-limited view, use
9014          --  it to determine whether the conversion is legal.
9015
9016          elsif Is_Class_Wide_Type (Opnd_Type)
9017            and then From_With_Type (Opnd_Type)
9018            and then Present (Non_Limited_View (Etype (Opnd_Type)))
9019            and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
9020          then
9021             return True;
9022
9023          elsif Is_Access_Type (Opnd_Type)
9024            and then Is_Interface (Directly_Designated_Type (Opnd_Type))
9025          then
9026             return True;
9027
9028          else
9029             Error_Msg_NE
9030               ("invalid tagged conversion, not compatible with}",
9031                N, First_Subtype (Opnd_Type));
9032             return False;
9033          end if;
9034       end Valid_Tagged_Conversion;
9035
9036    --  Start of processing for Valid_Conversion
9037
9038    begin
9039       Check_Parameterless_Call (Operand);
9040
9041       if Is_Overloaded (Operand) then
9042          declare
9043             I   : Interp_Index;
9044             I1  : Interp_Index;
9045             It  : Interp;
9046             It1 : Interp;
9047             N1  : Entity_Id;
9048
9049          begin
9050             --  Remove procedure calls, which syntactically cannot appear
9051             --  in this context, but which cannot be removed by type checking,
9052             --  because the context does not impose a type.
9053
9054             --  When compiling for VMS, spurious ambiguities can be produced
9055             --  when arithmetic operations have a literal operand and return
9056             --  System.Address or a descendant of it. These ambiguities are
9057             --  otherwise resolved by the context, but for conversions there
9058             --  is no context type and the removal of the spurious operations
9059             --  must be done explicitly here.
9060
9061             --  The node may be labelled overloaded, but still contain only
9062             --  one interpretation because others were discarded in previous
9063             --  filters. If this is the case, retain the single interpretation
9064             --  if legal.
9065
9066             Get_First_Interp (Operand, I, It);
9067             Opnd_Type := It.Typ;
9068             Get_Next_Interp (I, It);
9069
9070             if Present (It.Typ)
9071               and then Opnd_Type /= Standard_Void_Type
9072             then
9073                --  More than one candidate interpretation is available
9074
9075                Get_First_Interp (Operand, I, It);
9076                while Present (It.Typ) loop
9077                   if It.Typ = Standard_Void_Type then
9078                      Remove_Interp (I);
9079                   end if;
9080
9081                   if Present (System_Aux_Id)
9082                     and then Is_Descendent_Of_Address (It.Typ)
9083                   then
9084                      Remove_Interp (I);
9085                   end if;
9086
9087                   Get_Next_Interp (I, It);
9088                end loop;
9089             end if;
9090
9091             Get_First_Interp (Operand, I, It);
9092             I1  := I;
9093             It1 := It;
9094
9095             if No (It.Typ) then
9096                Error_Msg_N ("illegal operand in conversion", Operand);
9097                return False;
9098             end if;
9099
9100             Get_Next_Interp (I, It);
9101
9102             if Present (It.Typ) then
9103                N1  := It1.Nam;
9104                It1 :=  Disambiguate (Operand, I1, I, Any_Type);
9105
9106                if It1 = No_Interp then
9107                   Error_Msg_N ("ambiguous operand in conversion", Operand);
9108
9109                   Error_Msg_Sloc := Sloc (It.Nam);
9110                   Error_Msg_N ("\\possible interpretation#!", Operand);
9111
9112                   Error_Msg_Sloc := Sloc (N1);
9113                   Error_Msg_N ("\\possible interpretation#!", Operand);
9114
9115                   return False;
9116                end if;
9117             end if;
9118
9119             Set_Etype (Operand, It1.Typ);
9120             Opnd_Type := It1.Typ;
9121          end;
9122       end if;
9123
9124       --  Numeric types
9125
9126       if Is_Numeric_Type (Target_Type)  then
9127
9128          --  A universal fixed expression can be converted to any numeric type
9129
9130          if Opnd_Type = Universal_Fixed then
9131             return True;
9132
9133          --  Also no need to check when in an instance or inlined body, because
9134          --  the legality has been established when the template was analyzed.
9135          --  Furthermore, numeric conversions may occur where only a private
9136          --  view of the operand type is visible at the instantiation point.
9137          --  This results in a spurious error if we check that the operand type
9138          --  is a numeric type.
9139
9140          --  Note: in a previous version of this unit, the following tests were
9141          --  applied only for generated code (Comes_From_Source set to False),
9142          --  but in fact the test is required for source code as well, since
9143          --  this situation can arise in source code.
9144
9145          elsif In_Instance or else In_Inlined_Body then
9146                return True;
9147
9148          --  Otherwise we need the conversion check
9149
9150          else
9151             return Conversion_Check
9152                     (Is_Numeric_Type (Opnd_Type),
9153                      "illegal operand for numeric conversion");
9154          end if;
9155
9156       --  Array types
9157
9158       elsif Is_Array_Type (Target_Type) then
9159          if not Is_Array_Type (Opnd_Type)
9160            or else Opnd_Type = Any_Composite
9161            or else Opnd_Type = Any_String
9162          then
9163             Error_Msg_N
9164               ("illegal operand for array conversion", Operand);
9165             return False;
9166          else
9167             return Valid_Array_Conversion;
9168          end if;
9169
9170       --  Ada 2005 (AI-251): Anonymous access types where target references an
9171       --  interface type.
9172
9173       elsif (Ekind (Target_Type) = E_General_Access_Type
9174               or else
9175              Ekind (Target_Type) = E_Anonymous_Access_Type)
9176         and then Is_Interface (Directly_Designated_Type (Target_Type))
9177       then
9178          --  Check the static accessibility rule of 4.6(17). Note that the
9179          --  check is not enforced when within an instance body, since the RM
9180          --  requires such cases to be caught at run time.
9181
9182          if Ekind (Target_Type) /= E_Anonymous_Access_Type then
9183             if Type_Access_Level (Opnd_Type) >
9184                Type_Access_Level (Target_Type)
9185             then
9186                --  In an instance, this is a run-time check, but one we know
9187                --  will fail, so generate an appropriate warning. The raise
9188                --  will be generated by Expand_N_Type_Conversion.
9189
9190                if In_Instance_Body then
9191                   Error_Msg_N
9192                     ("?cannot convert local pointer to non-local access type",
9193                      Operand);
9194                   Error_Msg_N
9195                     ("\?Program_Error will be raised at run time", Operand);
9196                else
9197                   Error_Msg_N
9198                     ("cannot convert local pointer to non-local access type",
9199                      Operand);
9200                   return False;
9201                end if;
9202
9203             --  Special accessibility checks are needed in the case of access
9204             --  discriminants declared for a limited type.
9205
9206             elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
9207               and then not Is_Local_Anonymous_Access (Opnd_Type)
9208             then
9209                --  When the operand is a selected access discriminant the check
9210                --  needs to be made against the level of the object denoted by
9211                --  the prefix of the selected name. (Object_Access_Level
9212                --  handles checking the prefix of the operand for this case.)
9213
9214                if Nkind (Operand) = N_Selected_Component
9215                  and then Object_Access_Level (Operand) >
9216                           Type_Access_Level (Target_Type)
9217                then
9218                   --  In an instance, this is a run-time check, but one we
9219                   --  know will fail, so generate an appropriate warning.
9220                   --  The raise will be generated by Expand_N_Type_Conversion.
9221
9222                   if In_Instance_Body then
9223                      Error_Msg_N
9224                        ("?cannot convert access discriminant to non-local" &
9225                         " access type", Operand);
9226                      Error_Msg_N
9227                        ("\?Program_Error will be raised at run time", Operand);
9228                   else
9229                      Error_Msg_N
9230                        ("cannot convert access discriminant to non-local" &
9231                         " access type", Operand);
9232                      return False;
9233                   end if;
9234                end if;
9235
9236                --  The case of a reference to an access discriminant from
9237                --  within a limited type declaration (which will appear as
9238                --  a discriminal) is always illegal because the level of the
9239                --  discriminant is considered to be deeper than any (nameable)
9240                --  access type.
9241
9242                if Is_Entity_Name (Operand)
9243                  and then not Is_Local_Anonymous_Access (Opnd_Type)
9244                  and then (Ekind (Entity (Operand)) = E_In_Parameter
9245                             or else Ekind (Entity (Operand)) = E_Constant)
9246                  and then Present (Discriminal_Link (Entity (Operand)))
9247                then
9248                   Error_Msg_N
9249                     ("discriminant has deeper accessibility level than target",
9250                      Operand);
9251                   return False;
9252                end if;
9253             end if;
9254          end if;
9255
9256          return True;
9257
9258       --  General and anonymous access types
9259
9260       elsif (Ekind (Target_Type) = E_General_Access_Type
9261         or else Ekind (Target_Type) = E_Anonymous_Access_Type)
9262           and then
9263             Conversion_Check
9264               (Is_Access_Type (Opnd_Type)
9265                  and then Ekind (Opnd_Type) /=
9266                    E_Access_Subprogram_Type
9267                  and then Ekind (Opnd_Type) /=
9268                    E_Access_Protected_Subprogram_Type,
9269                "must be an access-to-object type")
9270       then
9271          if Is_Access_Constant (Opnd_Type)
9272            and then not Is_Access_Constant (Target_Type)
9273          then
9274             Error_Msg_N
9275               ("access-to-constant operand type not allowed", Operand);
9276             return False;
9277          end if;
9278
9279          --  Check the static accessibility rule of 4.6(17). Note that the
9280          --  check is not enforced when within an instance body, since the RM
9281          --  requires such cases to be caught at run time.
9282
9283          if Ekind (Target_Type) /= E_Anonymous_Access_Type
9284            or else Is_Local_Anonymous_Access (Target_Type)
9285          then
9286             if Type_Access_Level (Opnd_Type)
9287               > Type_Access_Level (Target_Type)
9288             then
9289                --  In an instance, this is a run-time check, but one we
9290                --  know will fail, so generate an appropriate warning.
9291                --  The raise will be generated by Expand_N_Type_Conversion.
9292
9293                if In_Instance_Body then
9294                   Error_Msg_N
9295                     ("?cannot convert local pointer to non-local access type",
9296                      Operand);
9297                   Error_Msg_N
9298                     ("\?Program_Error will be raised at run time", Operand);
9299
9300                else
9301                   --  Avoid generation of spurious error message
9302
9303                   if not Error_Posted (N) then
9304                      Error_Msg_N
9305                       ("cannot convert local pointer to non-local access type",
9306                        Operand);
9307                   end if;
9308
9309                   return False;
9310                end if;
9311
9312             --  Special accessibility checks are needed in the case of access
9313             --  discriminants declared for a limited type.
9314
9315             elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
9316               and then not Is_Local_Anonymous_Access (Opnd_Type)
9317             then
9318
9319                --  When the operand is a selected access discriminant the check
9320                --  needs to be made against the level of the object denoted by
9321                --  the prefix of the selected name. (Object_Access_Level
9322                --  handles checking the prefix of the operand for this case.)
9323
9324                if Nkind (Operand) = N_Selected_Component
9325                  and then Object_Access_Level (Operand) >
9326                           Type_Access_Level (Target_Type)
9327                then
9328                   --  In an instance, this is a run-time check, but one we
9329                   --  know will fail, so generate an appropriate warning.
9330                   --  The raise will be generated by Expand_N_Type_Conversion.
9331
9332                   if In_Instance_Body then
9333                      Error_Msg_N
9334                        ("?cannot convert access discriminant to non-local" &
9335                         " access type", Operand);
9336                      Error_Msg_N
9337                        ("\?Program_Error will be raised at run time",
9338                         Operand);
9339
9340                   else
9341                      Error_Msg_N
9342                        ("cannot convert access discriminant to non-local" &
9343                         " access type", Operand);
9344                      return False;
9345                   end if;
9346                end if;
9347
9348                --  The case of a reference to an access discriminant from
9349                --  within a limited type declaration (which will appear as
9350                --  a discriminal) is always illegal because the level of the
9351                --  discriminant is considered to be deeper than any (nameable)
9352                --  access type.
9353
9354                if Is_Entity_Name (Operand)
9355                  and then (Ekind (Entity (Operand)) = E_In_Parameter
9356                             or else Ekind (Entity (Operand)) = E_Constant)
9357                  and then Present (Discriminal_Link (Entity (Operand)))
9358                then
9359                   Error_Msg_N
9360                     ("discriminant has deeper accessibility level than target",
9361                      Operand);
9362                   return False;
9363                end if;
9364             end if;
9365          end if;
9366
9367          declare
9368             function Full_Designated_Type (T : Entity_Id) return Entity_Id;
9369             --  Helper function to handle limited views
9370
9371             --------------------------
9372             -- Full_Designated_Type --
9373             --------------------------
9374
9375             function Full_Designated_Type (T : Entity_Id) return Entity_Id is
9376                Desig : constant Entity_Id := Designated_Type (T);
9377             begin
9378                if From_With_Type (Desig)
9379                  and then Is_Incomplete_Type (Desig)
9380                  and then Present (Non_Limited_View (Desig))
9381                then
9382                   return Non_Limited_View (Desig);
9383                else
9384                   return Desig;
9385                end if;
9386             end Full_Designated_Type;
9387
9388             Target : constant Entity_Id := Full_Designated_Type (Target_Type);
9389             Opnd   : constant Entity_Id := Full_Designated_Type (Opnd_Type);
9390
9391             Same_Base : constant Boolean :=
9392                           Base_Type (Target) = Base_Type (Opnd);
9393
9394          begin
9395             if Is_Tagged_Type (Target) then
9396                return Valid_Tagged_Conversion (Target, Opnd);
9397
9398             else
9399                if not Same_Base then
9400                   Error_Msg_NE
9401                     ("target designated type not compatible with }",
9402                      N, Base_Type (Opnd));
9403                   return False;
9404
9405                --  Ada 2005 AI-384: legality rule is symmetric in both
9406                --  designated types. The conversion is legal (with possible
9407                --  constraint check) if either designated type is
9408                --  unconstrained.
9409
9410                elsif Subtypes_Statically_Match (Target, Opnd)
9411                  or else
9412                    (Has_Discriminants (Target)
9413                      and then
9414                       (not Is_Constrained (Opnd)
9415                         or else not Is_Constrained (Target)))
9416                then
9417                   return True;
9418
9419                else
9420                   Error_Msg_NE
9421                     ("target designated subtype not compatible with }",
9422                      N, Opnd);
9423                   return False;
9424                end if;
9425             end if;
9426          end;
9427
9428       --  Subprogram access types
9429
9430       elsif (Ekind (Target_Type) = E_Access_Subprogram_Type
9431                or else
9432              Ekind (Target_Type) = E_Anonymous_Access_Subprogram_Type)
9433         and then No (Corresponding_Remote_Type (Opnd_Type))
9434       then
9435          if
9436            Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
9437          then
9438             Error_Msg_N
9439               ("illegal attempt to store anonymous access to subprogram",
9440                Operand);
9441             Error_Msg_N
9442               ("\value has deeper accessibility than any master " &
9443                "(RM 3.10.2 (13))",
9444                Operand);
9445
9446             if Is_Entity_Name (Operand)
9447               and then Ekind (Entity (Operand)) = E_In_Parameter
9448             then
9449                Error_Msg_NE
9450                  ("\use named access type for& instead of access parameter",
9451                   Operand, Entity (Operand));
9452             end if;
9453          end if;
9454
9455          --  Check that the designated types are subtype conformant
9456
9457          Check_Subtype_Conformant (New_Id  => Designated_Type (Target_Type),
9458                                    Old_Id  => Designated_Type (Opnd_Type),
9459                                    Err_Loc => N);
9460
9461          --  Check the static accessibility rule of 4.6(20)
9462
9463          if Type_Access_Level (Opnd_Type) >
9464             Type_Access_Level (Target_Type)
9465          then
9466             Error_Msg_N
9467               ("operand type has deeper accessibility level than target",
9468                Operand);
9469
9470          --  Check that if the operand type is declared in a generic body,
9471          --  then the target type must be declared within that same body
9472          --  (enforces last sentence of 4.6(20)).
9473
9474          elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
9475             declare
9476                O_Gen : constant Node_Id :=
9477                          Enclosing_Generic_Body (Opnd_Type);
9478
9479                T_Gen : Node_Id;
9480
9481             begin
9482                T_Gen := Enclosing_Generic_Body (Target_Type);
9483                while Present (T_Gen) and then T_Gen /= O_Gen loop
9484                   T_Gen := Enclosing_Generic_Body (T_Gen);
9485                end loop;
9486
9487                if T_Gen /= O_Gen then
9488                   Error_Msg_N
9489                     ("target type must be declared in same generic body"
9490                      & " as operand type", N);
9491                end if;
9492             end;
9493          end if;
9494
9495          return True;
9496
9497       --  Remote subprogram access types
9498
9499       elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
9500         and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
9501       then
9502          --  It is valid to convert from one RAS type to another provided
9503          --  that their specification statically match.
9504
9505          Check_Subtype_Conformant
9506            (New_Id  =>
9507               Designated_Type (Corresponding_Remote_Type (Target_Type)),
9508             Old_Id  =>
9509               Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
9510             Err_Loc =>
9511               N);
9512          return True;
9513
9514       --  If both are tagged types, check legality of view conversions
9515
9516       elsif Is_Tagged_Type (Target_Type)
9517         and then Is_Tagged_Type (Opnd_Type)
9518       then
9519          return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
9520
9521       --  Types derived from the same root type are convertible
9522
9523       elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
9524          return True;
9525
9526       --  In an instance or an inlined body, there may be inconsistent
9527       --  views of the same type, or of types derived from a common root.
9528
9529       elsif (In_Instance or In_Inlined_Body)
9530         and then
9531            Root_Type (Underlying_Type (Target_Type)) =
9532            Root_Type (Underlying_Type (Opnd_Type))
9533       then
9534          return True;
9535
9536       --  Special check for common access type error case
9537
9538       elsif Ekind (Target_Type) = E_Access_Type
9539          and then Is_Access_Type (Opnd_Type)
9540       then
9541          Error_Msg_N ("target type must be general access type!", N);
9542          Error_Msg_NE ("add ALL to }!", N, Target_Type);
9543
9544          return False;
9545
9546       else
9547          Error_Msg_NE ("invalid conversion, not compatible with }",
9548            N, Opnd_Type);
9549
9550          return False;
9551       end if;
9552    end Valid_Conversion;
9553
9554 end Sem_Res;