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