OSDN Git Service

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