OSDN Git Service

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