OSDN Git Service

80b8479e24adb5c73c3590dcb197dbe9a633ff93
[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       function Find_Unique_Access_Type return Entity_Id;
6395       --  In the case of allocators, make a last-ditch attempt to find a single
6396       --  access type with the right designated type. This is semantically
6397       --  dubious, and of no interest to any real code, but c48008a makes it
6398       --  all worthwhile.
6399
6400       -----------------------------
6401       -- Find_Unique_Access_Type --
6402       -----------------------------
6403
6404       function Find_Unique_Access_Type return Entity_Id is
6405          Acc : Entity_Id;
6406          E   : Entity_Id;
6407          S   : Entity_Id;
6408
6409       begin
6410          if Ekind (Etype (R)) =  E_Allocator_Type then
6411             Acc := Designated_Type (Etype (R));
6412          elsif Ekind (Etype (L)) =  E_Allocator_Type then
6413             Acc := Designated_Type (Etype (L));
6414          else
6415             return Empty;
6416          end if;
6417
6418          S := Current_Scope;
6419          while S /= Standard_Standard loop
6420             E := First_Entity (S);
6421             while Present (E) loop
6422                if Is_Type (E)
6423                  and then Is_Access_Type (E)
6424                  and then Ekind (E) /= E_Allocator_Type
6425                  and then Designated_Type (E) = Base_Type (Acc)
6426                then
6427                   return E;
6428                end if;
6429
6430                Next_Entity (E);
6431             end loop;
6432
6433             S := Scope (S);
6434          end loop;
6435
6436          return Empty;
6437       end Find_Unique_Access_Type;
6438
6439    --  Start of processing for Resolve_Equality_Op
6440
6441    begin
6442       Set_Etype (N, Base_Type (Typ));
6443       Generate_Reference (T, N, ' ');
6444
6445       if T = Any_Fixed then
6446          T := Unique_Fixed_Point_Type (L);
6447       end if;
6448
6449       if T /= Any_Type then
6450          if T = Any_String
6451            or else T = Any_Composite
6452            or else T = Any_Character
6453          then
6454             if T = Any_Character then
6455                Ambiguous_Character (L);
6456             else
6457                Error_Msg_N ("ambiguous operands for equality", N);
6458             end if;
6459
6460             Set_Etype (N, Any_Type);
6461             return;
6462
6463          elsif T = Any_Access
6464            or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
6465          then
6466             T := Find_Unique_Access_Type;
6467
6468             if No (T) then
6469                Error_Msg_N ("ambiguous operands for equality", N);
6470                Set_Etype (N, Any_Type);
6471                return;
6472             end if;
6473          end if;
6474
6475          Resolve (L, T);
6476          Resolve (R, T);
6477
6478          --  If the unique type is a class-wide type then it will be expanded
6479          --  into a dispatching call to the predefined primitive. Therefore we
6480          --  check here for potential violation of such restriction.
6481
6482          if Is_Class_Wide_Type (T) then
6483             Check_Restriction (No_Dispatching_Calls, N);
6484          end if;
6485
6486          if Warn_On_Redundant_Constructs
6487            and then Comes_From_Source (N)
6488            and then Is_Entity_Name (R)
6489            and then Entity (R) = Standard_True
6490            and then Comes_From_Source (R)
6491          then
6492             Error_Msg_N -- CODEFIX
6493               ("?comparison with True is redundant!", R);
6494          end if;
6495
6496          Check_Unset_Reference (L);
6497          Check_Unset_Reference (R);
6498          Generate_Operator_Reference (N, T);
6499          Check_Low_Bound_Tested (N);
6500
6501          --  If this is an inequality, it may be the implicit inequality
6502          --  created for a user-defined operation, in which case the corres-
6503          --  ponding equality operation is not intrinsic, and the operation
6504          --  cannot be constant-folded. Else fold.
6505
6506          if Nkind (N) = N_Op_Eq
6507            or else Comes_From_Source (Entity (N))
6508            or else Ekind (Entity (N)) = E_Operator
6509            or else Is_Intrinsic_Subprogram
6510              (Corresponding_Equality (Entity (N)))
6511          then
6512             Eval_Relational_Op (N);
6513
6514          elsif Nkind (N) = N_Op_Ne
6515            and then Is_Abstract_Subprogram (Entity (N))
6516          then
6517             Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
6518          end if;
6519
6520          --  Ada 2005: If one operand is an anonymous access type, convert the
6521          --  other operand to it, to ensure that the underlying types match in
6522          --  the back-end. Same for access_to_subprogram, and the conversion
6523          --  verifies that the types are subtype conformant.
6524
6525          --  We apply the same conversion in the case one of the operands is a
6526          --  private subtype of the type of the other.
6527
6528          --  Why the Expander_Active test here ???
6529
6530          if Expander_Active
6531            and then
6532              (Ekind_In (T, E_Anonymous_Access_Type,
6533                            E_Anonymous_Access_Subprogram_Type)
6534                or else Is_Private_Type (T))
6535          then
6536             if Etype (L) /= T then
6537                Rewrite (L,
6538                  Make_Unchecked_Type_Conversion (Sloc (L),
6539                    Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
6540                    Expression   => Relocate_Node (L)));
6541                Analyze_And_Resolve (L, T);
6542             end if;
6543
6544             if (Etype (R)) /= T then
6545                Rewrite (R,
6546                   Make_Unchecked_Type_Conversion (Sloc (R),
6547                     Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
6548                     Expression   => Relocate_Node (R)));
6549                Analyze_And_Resolve (R, T);
6550             end if;
6551          end if;
6552       end if;
6553    end Resolve_Equality_Op;
6554
6555    ----------------------------------
6556    -- Resolve_Explicit_Dereference --
6557    ----------------------------------
6558
6559    procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
6560       Loc   : constant Source_Ptr := Sloc (N);
6561       New_N : Node_Id;
6562       P     : constant Node_Id := Prefix (N);
6563       I     : Interp_Index;
6564       It    : Interp;
6565
6566    begin
6567       Check_Fully_Declared_Prefix (Typ, P);
6568
6569       if Is_Overloaded (P) then
6570
6571          --  Use the context type to select the prefix that has the correct
6572          --  designated type.
6573
6574          Get_First_Interp (P, I, It);
6575          while Present (It.Typ) loop
6576             exit when Is_Access_Type (It.Typ)
6577               and then Covers (Typ, Designated_Type (It.Typ));
6578             Get_Next_Interp (I, It);
6579          end loop;
6580
6581          if Present (It.Typ) then
6582             Resolve (P, It.Typ);
6583          else
6584             --  If no interpretation covers the designated type of the prefix,
6585             --  this is the pathological case where not all implementations of
6586             --  the prefix allow the interpretation of the node as a call. Now
6587             --  that the expected type is known, Remove other interpretations
6588             --  from prefix, rewrite it as a call, and resolve again, so that
6589             --  the proper call node is generated.
6590
6591             Get_First_Interp (P, I, It);
6592             while Present (It.Typ) loop
6593                if Ekind (It.Typ) /= E_Access_Subprogram_Type then
6594                   Remove_Interp (I);
6595                end if;
6596
6597                Get_Next_Interp (I, It);
6598             end loop;
6599
6600             New_N :=
6601               Make_Function_Call (Loc,
6602                 Name =>
6603                   Make_Explicit_Dereference (Loc,
6604                     Prefix => P),
6605                 Parameter_Associations => New_List);
6606
6607             Save_Interps (N, New_N);
6608             Rewrite (N, New_N);
6609             Analyze_And_Resolve (N, Typ);
6610             return;
6611          end if;
6612
6613          Set_Etype (N, Designated_Type (It.Typ));
6614
6615       else
6616          Resolve (P);
6617       end if;
6618
6619       if Is_Access_Type (Etype (P)) then
6620          Apply_Access_Check (N);
6621       end if;
6622
6623       --  If the designated type is a packed unconstrained array type, and the
6624       --  explicit dereference is not in the context of an attribute reference,
6625       --  then we must compute and set the actual subtype, since it is needed
6626       --  by Gigi. The reason we exclude the attribute case is that this is
6627       --  handled fine by Gigi, and in fact we use such attributes to build the
6628       --  actual subtype. We also exclude generated code (which builds actual
6629       --  subtypes directly if they are needed).
6630
6631       if Is_Array_Type (Etype (N))
6632         and then Is_Packed (Etype (N))
6633         and then not Is_Constrained (Etype (N))
6634         and then Nkind (Parent (N)) /= N_Attribute_Reference
6635         and then Comes_From_Source (N)
6636       then
6637          Set_Etype (N, Get_Actual_Subtype (N));
6638       end if;
6639
6640       --  Note: No Eval processing is required for an explicit dereference,
6641       --  because such a name can never be static.
6642
6643    end Resolve_Explicit_Dereference;
6644
6645    -------------------------------------
6646    -- Resolve_Expression_With_Actions --
6647    -------------------------------------
6648
6649    procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
6650    begin
6651       Set_Etype (N, Typ);
6652    end Resolve_Expression_With_Actions;
6653
6654    -------------------------------
6655    -- Resolve_Indexed_Component --
6656    -------------------------------
6657
6658    procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
6659       Name       : constant Node_Id := Prefix  (N);
6660       Expr       : Node_Id;
6661       Array_Type : Entity_Id := Empty; -- to prevent junk warning
6662       Index      : Node_Id;
6663
6664    begin
6665       if Is_Overloaded (Name) then
6666
6667          --  Use the context type to select the prefix that yields the correct
6668          --  component type.
6669
6670          declare
6671             I     : Interp_Index;
6672             It    : Interp;
6673             I1    : Interp_Index := 0;
6674             P     : constant Node_Id := Prefix (N);
6675             Found : Boolean := False;
6676
6677          begin
6678             Get_First_Interp (P, I, It);
6679             while Present (It.Typ) loop
6680                if (Is_Array_Type (It.Typ)
6681                      and then Covers (Typ, Component_Type (It.Typ)))
6682                  or else (Is_Access_Type (It.Typ)
6683                             and then Is_Array_Type (Designated_Type (It.Typ))
6684                             and then Covers
6685                               (Typ, Component_Type (Designated_Type (It.Typ))))
6686                then
6687                   if Found then
6688                      It := Disambiguate (P, I1, I, Any_Type);
6689
6690                      if It = No_Interp then
6691                         Error_Msg_N ("ambiguous prefix for indexing",  N);
6692                         Set_Etype (N, Typ);
6693                         return;
6694
6695                      else
6696                         Found := True;
6697                         Array_Type := It.Typ;
6698                         I1 := I;
6699                      end if;
6700
6701                   else
6702                      Found := True;
6703                      Array_Type := It.Typ;
6704                      I1 := I;
6705                   end if;
6706                end if;
6707
6708                Get_Next_Interp (I, It);
6709             end loop;
6710          end;
6711
6712       else
6713          Array_Type := Etype (Name);
6714       end if;
6715
6716       Resolve (Name, Array_Type);
6717       Array_Type := Get_Actual_Subtype_If_Available (Name);
6718
6719       --  If prefix is access type, dereference to get real array type.
6720       --  Note: we do not apply an access check because the expander always
6721       --  introduces an explicit dereference, and the check will happen there.
6722
6723       if Is_Access_Type (Array_Type) then
6724          Array_Type := Designated_Type (Array_Type);
6725       end if;
6726
6727       --  If name was overloaded, set component type correctly now
6728       --  If a misplaced call to an entry family (which has no index types)
6729       --  return. Error will be diagnosed from calling context.
6730
6731       if Is_Array_Type (Array_Type) then
6732          Set_Etype (N, Component_Type (Array_Type));
6733       else
6734          return;
6735       end if;
6736
6737       Index := First_Index (Array_Type);
6738       Expr  := First (Expressions (N));
6739
6740       --  The prefix may have resolved to a string literal, in which case its
6741       --  etype has a special representation. This is only possible currently
6742       --  if the prefix is a static concatenation, written in functional
6743       --  notation.
6744
6745       if Ekind (Array_Type) = E_String_Literal_Subtype then
6746          Resolve (Expr, Standard_Positive);
6747
6748       else
6749          while Present (Index) and Present (Expr) loop
6750             Resolve (Expr, Etype (Index));
6751             Check_Unset_Reference (Expr);
6752
6753             if Is_Scalar_Type (Etype (Expr)) then
6754                Apply_Scalar_Range_Check (Expr, Etype (Index));
6755             else
6756                Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
6757             end if;
6758
6759             Next_Index (Index);
6760             Next (Expr);
6761          end loop;
6762       end if;
6763
6764       --  Do not generate the warning on suspicious index if we are analyzing
6765       --  package Ada.Tags; otherwise we will report the warning with the
6766       --  Prims_Ptr field of the dispatch table.
6767
6768       if Scope (Etype (Prefix (N))) = Standard_Standard
6769         or else not
6770           Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
6771                   Ada_Tags)
6772       then
6773          Warn_On_Suspicious_Index (Name, First (Expressions (N)));
6774          Eval_Indexed_Component (N);
6775       end if;
6776
6777       --  If the array type is atomic, and is packed, and we are in a left side
6778       --  context, then this is worth a warning, since we have a situation
6779       --  where the access to the component may cause extra read/writes of
6780       --  the atomic array object, which could be considered unexpected.
6781
6782       if Nkind (N) = N_Indexed_Component
6783         and then (Is_Atomic (Array_Type)
6784                    or else (Is_Entity_Name (Prefix (N))
6785                              and then Is_Atomic (Entity (Prefix (N)))))
6786         and then Is_Bit_Packed_Array (Array_Type)
6787         and then Is_LHS (N)
6788       then
6789          Error_Msg_N ("?assignment to component of packed atomic array",
6790                       Prefix (N));
6791          Error_Msg_N ("?\may cause unexpected accesses to atomic object",
6792                       Prefix (N));
6793       end if;
6794    end Resolve_Indexed_Component;
6795
6796    -----------------------------
6797    -- Resolve_Integer_Literal --
6798    -----------------------------
6799
6800    procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
6801    begin
6802       Set_Etype (N, Typ);
6803       Eval_Integer_Literal (N);
6804    end Resolve_Integer_Literal;
6805
6806    --------------------------------
6807    -- Resolve_Intrinsic_Operator --
6808    --------------------------------
6809
6810    procedure Resolve_Intrinsic_Operator  (N : Node_Id; Typ : Entity_Id) is
6811       Btyp    : constant Entity_Id := Base_Type (Underlying_Type (Typ));
6812       Op      : Entity_Id;
6813       Orig_Op : constant Entity_Id := Entity (N);
6814       Arg1    : Node_Id;
6815       Arg2    : Node_Id;
6816
6817    begin
6818       --  We must preserve the original entity in a generic setting, so that
6819       --  the legality of the operation can be verified in an instance.
6820
6821       if not Expander_Active then
6822          return;
6823       end if;
6824
6825       Op := Entity (N);
6826       while Scope (Op) /= Standard_Standard loop
6827          Op := Homonym (Op);
6828          pragma Assert (Present (Op));
6829       end loop;
6830
6831       Set_Entity (N, Op);
6832       Set_Is_Overloaded (N, False);
6833
6834       --  If the operand type is private, rewrite with suitable conversions on
6835       --  the operands and the result, to expose the proper underlying numeric
6836       --  type.
6837
6838       if Is_Private_Type (Typ) then
6839          Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd  (N));
6840
6841          if Nkind (N) = N_Op_Expon then
6842             Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
6843          else
6844             Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
6845          end if;
6846
6847          if Nkind (Arg1) = N_Type_Conversion then
6848             Save_Interps (Left_Opnd (N),  Expression (Arg1));
6849          end if;
6850
6851          if Nkind (Arg2) = N_Type_Conversion then
6852             Save_Interps (Right_Opnd (N), Expression (Arg2));
6853          end if;
6854
6855          Set_Left_Opnd  (N, Arg1);
6856          Set_Right_Opnd (N, Arg2);
6857
6858          Set_Etype (N, Btyp);
6859          Rewrite (N, Unchecked_Convert_To (Typ, N));
6860          Resolve (N, Typ);
6861
6862       elsif Typ /= Etype (Left_Opnd (N))
6863         or else Typ /= Etype (Right_Opnd (N))
6864       then
6865          --  Add explicit conversion where needed, and save interpretations in
6866          --  case operands are overloaded. If the context is a VMS operation,
6867          --  assert that the conversion is legal (the operands have the proper
6868          --  types to select the VMS intrinsic). Note that in rare cases the
6869          --  VMS operators may be visible, but the default System is being used
6870          --  and Address is a private type.
6871
6872          Arg1 := Convert_To (Typ, Left_Opnd  (N));
6873          Arg2 := Convert_To (Typ, Right_Opnd (N));
6874
6875          if Nkind (Arg1) = N_Type_Conversion then
6876             Save_Interps (Left_Opnd (N), Expression (Arg1));
6877
6878             if Is_VMS_Operator (Orig_Op) then
6879                Set_Conversion_OK (Arg1);
6880             end if;
6881          else
6882             Save_Interps (Left_Opnd (N), Arg1);
6883          end if;
6884
6885          if Nkind (Arg2) = N_Type_Conversion then
6886             Save_Interps (Right_Opnd (N), Expression (Arg2));
6887
6888             if Is_VMS_Operator (Orig_Op) then
6889                Set_Conversion_OK (Arg2);
6890             end if;
6891          else
6892             Save_Interps (Right_Opnd (N), Arg2);
6893          end if;
6894
6895          Rewrite (Left_Opnd  (N), Arg1);
6896          Rewrite (Right_Opnd (N), Arg2);
6897          Analyze (Arg1);
6898          Analyze (Arg2);
6899          Resolve_Arithmetic_Op (N, Typ);
6900
6901       else
6902          Resolve_Arithmetic_Op (N, Typ);
6903       end if;
6904    end Resolve_Intrinsic_Operator;
6905
6906    --------------------------------------
6907    -- Resolve_Intrinsic_Unary_Operator --
6908    --------------------------------------
6909
6910    procedure Resolve_Intrinsic_Unary_Operator
6911      (N   : Node_Id;
6912       Typ : Entity_Id)
6913    is
6914       Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
6915       Op   : Entity_Id;
6916       Arg2 : Node_Id;
6917
6918    begin
6919       Op := Entity (N);
6920       while Scope (Op) /= Standard_Standard loop
6921          Op := Homonym (Op);
6922          pragma Assert (Present (Op));
6923       end loop;
6924
6925       Set_Entity (N, Op);
6926
6927       if Is_Private_Type (Typ) then
6928          Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
6929          Save_Interps (Right_Opnd (N), Expression (Arg2));
6930
6931          Set_Right_Opnd (N, Arg2);
6932
6933          Set_Etype (N, Btyp);
6934          Rewrite (N, Unchecked_Convert_To (Typ, N));
6935          Resolve (N, Typ);
6936
6937       else
6938          Resolve_Unary_Op (N, Typ);
6939       end if;
6940    end Resolve_Intrinsic_Unary_Operator;
6941
6942    ------------------------
6943    -- Resolve_Logical_Op --
6944    ------------------------
6945
6946    procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
6947       B_Typ : Entity_Id;
6948
6949    begin
6950       Check_No_Direct_Boolean_Operators (N);
6951
6952       --  Predefined operations on scalar types yield the base type. On the
6953       --  other hand, logical operations on arrays yield the type of the
6954       --  arguments (and the context).
6955
6956       if Is_Array_Type (Typ) then
6957          B_Typ := Typ;
6958       else
6959          B_Typ := Base_Type (Typ);
6960       end if;
6961
6962       --  OK if this is a VMS-specific intrinsic operation
6963
6964       if Is_VMS_Operator (Entity (N)) then
6965          null;
6966
6967       --  The following test is required because the operands of the operation
6968       --  may be literals, in which case the resulting type appears to be
6969       --  compatible with a signed integer type, when in fact it is compatible
6970       --  only with modular types. If the context itself is universal, the
6971       --  operation is illegal.
6972
6973       elsif not Valid_Boolean_Arg (Typ) then
6974          Error_Msg_N ("invalid context for logical operation", N);
6975          Set_Etype (N, Any_Type);
6976          return;
6977
6978       elsif Typ = Any_Modular then
6979          Error_Msg_N
6980            ("no modular type available in this context", N);
6981          Set_Etype (N, Any_Type);
6982          return;
6983       elsif Is_Modular_Integer_Type (Typ)
6984         and then Etype (Left_Opnd (N)) = Universal_Integer
6985         and then Etype (Right_Opnd (N)) = Universal_Integer
6986       then
6987          Check_For_Visible_Operator (N, B_Typ);
6988       end if;
6989
6990       Resolve (Left_Opnd (N), B_Typ);
6991       Resolve (Right_Opnd (N), B_Typ);
6992
6993       Check_Unset_Reference (Left_Opnd  (N));
6994       Check_Unset_Reference (Right_Opnd (N));
6995
6996       Set_Etype (N, B_Typ);
6997       Generate_Operator_Reference (N, B_Typ);
6998       Eval_Logical_Op (N);
6999    end Resolve_Logical_Op;
7000
7001    ---------------------------
7002    -- Resolve_Membership_Op --
7003    ---------------------------
7004
7005    --  The context can only be a boolean type, and does not determine
7006    --  the arguments. Arguments should be unambiguous, but the preference
7007    --  rule for universal types applies.
7008
7009    procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
7010       pragma Warnings (Off, Typ);
7011
7012       L : constant Node_Id := Left_Opnd  (N);
7013       R : constant Node_Id := Right_Opnd (N);
7014       T : Entity_Id;
7015
7016       procedure Resolve_Set_Membership;
7017       --  Analysis has determined a unique type for the left operand.
7018       --  Use it to resolve the disjuncts.
7019
7020       ----------------------------
7021       -- Resolve_Set_Membership --
7022       ----------------------------
7023
7024       procedure Resolve_Set_Membership is
7025          Alt : Node_Id;
7026
7027       begin
7028          Resolve (L, Etype (L));
7029
7030          Alt := First (Alternatives (N));
7031          while Present (Alt) loop
7032
7033             --  Alternative is an expression, a range
7034             --  or a subtype mark.
7035
7036             if not Is_Entity_Name (Alt)
7037               or else not Is_Type (Entity (Alt))
7038             then
7039                Resolve (Alt, Etype (L));
7040             end if;
7041
7042             Next (Alt);
7043          end loop;
7044       end Resolve_Set_Membership;
7045
7046    --  Start of processing for Resolve_Membership_Op
7047
7048    begin
7049       if L = Error or else R = Error then
7050          return;
7051       end if;
7052
7053       if Present (Alternatives (N)) then
7054          Resolve_Set_Membership;
7055          return;
7056
7057       elsif not Is_Overloaded (R)
7058         and then
7059           (Etype (R) = Universal_Integer or else
7060            Etype (R) = Universal_Real)
7061         and then Is_Overloaded (L)
7062       then
7063          T := Etype (R);
7064
7065       --  Ada 2005 (AI-251): Support the following case:
7066
7067       --      type I is interface;
7068       --      type T is tagged ...
7069
7070       --      function Test (O : I'Class) is
7071       --      begin
7072       --         return O in T'Class.
7073       --      end Test;
7074
7075       --  In this case we have nothing else to do. The membership test will be
7076       --  done at run-time.
7077
7078       elsif Ada_Version >= Ada_05
7079         and then Is_Class_Wide_Type (Etype (L))
7080         and then Is_Interface (Etype (L))
7081         and then Is_Class_Wide_Type (Etype (R))
7082         and then not Is_Interface (Etype (R))
7083       then
7084          return;
7085
7086       else
7087          T := Intersect_Types (L, R);
7088       end if;
7089
7090       --  If mixed-mode operations are present and operands are all literal,
7091       --  the only interpretation involves Duration, which is probably not
7092       --  the intention of the programmer.
7093
7094       if T = Any_Fixed then
7095          T := Unique_Fixed_Point_Type (N);
7096
7097          if T = Any_Type then
7098             return;
7099          end if;
7100       end if;
7101
7102       Resolve (L, T);
7103       Check_Unset_Reference (L);
7104
7105       if Nkind (R) = N_Range
7106         and then not Is_Scalar_Type (T)
7107       then
7108          Error_Msg_N ("scalar type required for range", R);
7109       end if;
7110
7111       if Is_Entity_Name (R) then
7112          Freeze_Expression (R);
7113       else
7114          Resolve (R, T);
7115          Check_Unset_Reference (R);
7116       end if;
7117
7118       Eval_Membership_Op (N);
7119    end Resolve_Membership_Op;
7120
7121    ------------------
7122    -- Resolve_Null --
7123    ------------------
7124
7125    procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
7126       Loc : constant Source_Ptr := Sloc (N);
7127
7128    begin
7129       --  Handle restriction against anonymous null access values This
7130       --  restriction can be turned off using -gnatdj.
7131
7132       --  Ada 2005 (AI-231): Remove restriction
7133
7134       if Ada_Version < Ada_05
7135         and then not Debug_Flag_J
7136         and then Ekind (Typ) = E_Anonymous_Access_Type
7137         and then Comes_From_Source (N)
7138       then
7139          --  In the common case of a call which uses an explicitly null value
7140          --  for an access parameter, give specialized error message.
7141
7142          if Nkind_In (Parent (N), N_Procedure_Call_Statement,
7143                                   N_Function_Call)
7144          then
7145             Error_Msg_N
7146               ("null is not allowed as argument for an access parameter", N);
7147
7148          --  Standard message for all other cases (are there any?)
7149
7150          else
7151             Error_Msg_N
7152               ("null cannot be of an anonymous access type", N);
7153          end if;
7154       end if;
7155
7156       --  Ada 2005 (AI-231): Generate the null-excluding check in case of
7157       --  assignment to a null-excluding object
7158
7159       if Ada_Version >= Ada_05
7160         and then Can_Never_Be_Null (Typ)
7161         and then Nkind (Parent (N)) = N_Assignment_Statement
7162       then
7163          if not Inside_Init_Proc then
7164             Insert_Action
7165               (Compile_Time_Constraint_Error (N,
7166                  "(Ada 2005) null not allowed in null-excluding objects?"),
7167                Make_Raise_Constraint_Error (Loc,
7168                  Reason => CE_Access_Check_Failed));
7169          else
7170             Insert_Action (N,
7171               Make_Raise_Constraint_Error (Loc,
7172                 Reason => CE_Access_Check_Failed));
7173          end if;
7174       end if;
7175
7176       --  In a distributed context, null for a remote access to subprogram may
7177       --  need to be replaced with a special record aggregate. In this case,
7178       --  return after having done the transformation.
7179
7180       if (Ekind (Typ) = E_Record_Type
7181            or else Is_Remote_Access_To_Subprogram_Type (Typ))
7182         and then Remote_AST_Null_Value (N, Typ)
7183       then
7184          return;
7185       end if;
7186
7187       --  The null literal takes its type from the context
7188
7189       Set_Etype (N, Typ);
7190    end Resolve_Null;
7191
7192    -----------------------
7193    -- Resolve_Op_Concat --
7194    -----------------------
7195
7196    procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
7197
7198       --  We wish to avoid deep recursion, because concatenations are often
7199       --  deeply nested, as in A&B&...&Z. Therefore, we walk down the left
7200       --  operands nonrecursively until we find something that is not a simple
7201       --  concatenation (A in this case). We resolve that, and then walk back
7202       --  up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
7203       --  to do the rest of the work at each level. The Parent pointers allow
7204       --  us to avoid recursion, and thus avoid running out of memory. See also
7205       --  Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
7206
7207       NN  : Node_Id := N;
7208       Op1 : Node_Id;
7209
7210    begin
7211       --  The following code is equivalent to:
7212
7213       --    Resolve_Op_Concat_First (NN, Typ);
7214       --    Resolve_Op_Concat_Arg (N, ...);
7215       --    Resolve_Op_Concat_Rest (N, Typ);
7216
7217       --  where the Resolve_Op_Concat_Arg call recurses back here if the left
7218       --  operand is a concatenation.
7219
7220       --  Walk down left operands
7221
7222       loop
7223          Resolve_Op_Concat_First (NN, Typ);
7224          Op1 := Left_Opnd (NN);
7225          exit when not (Nkind (Op1) = N_Op_Concat
7226                          and then not Is_Array_Type (Component_Type (Typ))
7227                          and then Entity (Op1) = Entity (NN));
7228          NN := Op1;
7229       end loop;
7230
7231       --  Now (given the above example) NN is A&B and Op1 is A
7232
7233       --  First resolve Op1 ...
7234
7235       Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd  (NN));
7236
7237       --  ... then walk NN back up until we reach N (where we started), calling
7238       --  Resolve_Op_Concat_Rest along the way.
7239
7240       loop
7241          Resolve_Op_Concat_Rest (NN, Typ);
7242          exit when NN = N;
7243          NN := Parent (NN);
7244       end loop;
7245    end Resolve_Op_Concat;
7246
7247    ---------------------------
7248    -- Resolve_Op_Concat_Arg --
7249    ---------------------------
7250
7251    procedure Resolve_Op_Concat_Arg
7252      (N       : Node_Id;
7253       Arg     : Node_Id;
7254       Typ     : Entity_Id;
7255       Is_Comp : Boolean)
7256    is
7257       Btyp : constant Entity_Id := Base_Type (Typ);
7258
7259    begin
7260       if In_Instance then
7261          if Is_Comp
7262            or else (not Is_Overloaded (Arg)
7263                      and then Etype (Arg) /= Any_Composite
7264                      and then Covers (Component_Type (Typ), Etype (Arg)))
7265          then
7266             Resolve (Arg, Component_Type (Typ));
7267          else
7268             Resolve (Arg, Btyp);
7269          end if;
7270
7271       elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then
7272          if Nkind (Arg) = N_Aggregate
7273            and then Is_Composite_Type (Component_Type (Typ))
7274          then
7275             if Is_Private_Type (Component_Type (Typ)) then
7276                Resolve (Arg, Btyp);
7277             else
7278                Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
7279                Set_Etype (Arg, Any_Type);
7280             end if;
7281
7282          else
7283             if Is_Overloaded (Arg)
7284               and then Has_Compatible_Type (Arg, Typ)
7285               and then Etype (Arg) /= Any_Type
7286             then
7287                declare
7288                   I    : Interp_Index;
7289                   It   : Interp;
7290                   Func : Entity_Id;
7291
7292                begin
7293                   Get_First_Interp (Arg, I, It);
7294                   Func := It.Nam;
7295                   Get_Next_Interp (I, It);
7296
7297                   --  Special-case the error message when the overloading is
7298                   --  caused by a function that yields an array and can be
7299                   --  called without parameters.
7300
7301                   if It.Nam = Func then
7302                      Error_Msg_Sloc := Sloc (Func);
7303                      Error_Msg_N ("ambiguous call to function#", Arg);
7304                      Error_Msg_NE
7305                        ("\\interpretation as call yields&", Arg, Typ);
7306                      Error_Msg_NE
7307                        ("\\interpretation as indexing of call yields&",
7308                          Arg, Component_Type (Typ));
7309
7310                   else
7311                      Error_Msg_N
7312                        ("ambiguous operand for concatenation!", Arg);
7313                      Get_First_Interp (Arg, I, It);
7314                      while Present (It.Nam) loop
7315                         Error_Msg_Sloc := Sloc (It.Nam);
7316
7317                         if Base_Type (It.Typ) = Base_Type (Typ)
7318                           or else Base_Type (It.Typ) =
7319                             Base_Type (Component_Type (Typ))
7320                         then
7321                            Error_Msg_N -- CODEFIX
7322                              ("\\possible interpretation#", Arg);
7323                         end if;
7324
7325                         Get_Next_Interp (I, It);
7326                      end loop;
7327                   end if;
7328                end;
7329             end if;
7330
7331             Resolve (Arg, Component_Type (Typ));
7332
7333             if Nkind (Arg) = N_String_Literal then
7334                Set_Etype (Arg, Component_Type (Typ));
7335             end if;
7336
7337             if Arg = Left_Opnd (N) then
7338                Set_Is_Component_Left_Opnd (N);
7339             else
7340                Set_Is_Component_Right_Opnd (N);
7341             end if;
7342          end if;
7343
7344       else
7345          Resolve (Arg, Btyp);
7346       end if;
7347
7348       Check_Unset_Reference (Arg);
7349    end Resolve_Op_Concat_Arg;
7350
7351    -----------------------------
7352    -- Resolve_Op_Concat_First --
7353    -----------------------------
7354
7355    procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
7356       Btyp : constant Entity_Id := Base_Type (Typ);
7357       Op1  : constant Node_Id := Left_Opnd (N);
7358       Op2  : constant Node_Id := Right_Opnd (N);
7359
7360    begin
7361       --  The parser folds an enormous sequence of concatenations of string
7362       --  literals into "" & "...", where the Is_Folded_In_Parser flag is set
7363       --  in the right operand. If the expression resolves to a predefined "&"
7364       --  operator, all is well. Otherwise, the parser's folding is wrong, so
7365       --  we give an error. See P_Simple_Expression in Par.Ch4.
7366
7367       if Nkind (Op2) = N_String_Literal
7368         and then Is_Folded_In_Parser (Op2)
7369         and then Ekind (Entity (N)) = E_Function
7370       then
7371          pragma Assert (Nkind (Op1) = N_String_Literal  --  should be ""
7372                and then String_Length (Strval (Op1)) = 0);
7373          Error_Msg_N ("too many user-defined concatenations", N);
7374          return;
7375       end if;
7376
7377       Set_Etype (N, Btyp);
7378
7379       if Is_Limited_Composite (Btyp) then
7380          Error_Msg_N ("concatenation not available for limited array", N);
7381          Explain_Limited_Type (Btyp, N);
7382       end if;
7383    end Resolve_Op_Concat_First;
7384
7385    ----------------------------
7386    -- Resolve_Op_Concat_Rest --
7387    ----------------------------
7388
7389    procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
7390       Op1  : constant Node_Id := Left_Opnd (N);
7391       Op2  : constant Node_Id := Right_Opnd (N);
7392
7393    begin
7394       Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd  (N));
7395
7396       Generate_Operator_Reference (N, Typ);
7397
7398       if Is_String_Type (Typ) then
7399          Eval_Concatenation (N);
7400       end if;
7401
7402       --  If this is not a static concatenation, but the result is a string
7403       --  type (and not an array of strings) ensure that static string operands
7404       --  have their subtypes properly constructed.
7405
7406       if Nkind (N) /= N_String_Literal
7407         and then Is_Character_Type (Component_Type (Typ))
7408       then
7409          Set_String_Literal_Subtype (Op1, Typ);
7410          Set_String_Literal_Subtype (Op2, Typ);
7411       end if;
7412    end Resolve_Op_Concat_Rest;
7413
7414    ----------------------
7415    -- Resolve_Op_Expon --
7416    ----------------------
7417
7418    procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
7419       B_Typ : constant Entity_Id := Base_Type (Typ);
7420
7421    begin
7422       --  Catch attempts to do fixed-point exponentiation with universal
7423       --  operands, which is a case where the illegality is not caught during
7424       --  normal operator analysis.
7425
7426       if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
7427          Error_Msg_N ("exponentiation not available for fixed point", N);
7428          return;
7429       end if;
7430
7431       if Comes_From_Source (N)
7432         and then Ekind (Entity (N)) = E_Function
7433         and then Is_Imported (Entity (N))
7434         and then Is_Intrinsic_Subprogram (Entity (N))
7435       then
7436          Resolve_Intrinsic_Operator (N, Typ);
7437          return;
7438       end if;
7439
7440       if Etype (Left_Opnd (N)) = Universal_Integer
7441         or else Etype (Left_Opnd (N)) = Universal_Real
7442       then
7443          Check_For_Visible_Operator (N, B_Typ);
7444       end if;
7445
7446       --  We do the resolution using the base type, because intermediate values
7447       --  in expressions always are of the base type, not a subtype of it.
7448
7449       Resolve (Left_Opnd (N), B_Typ);
7450       Resolve (Right_Opnd (N), Standard_Integer);
7451
7452       Check_Unset_Reference (Left_Opnd  (N));
7453       Check_Unset_Reference (Right_Opnd (N));
7454
7455       Set_Etype (N, B_Typ);
7456       Generate_Operator_Reference (N, B_Typ);
7457       Eval_Op_Expon (N);
7458
7459       --  Set overflow checking bit. Much cleverer code needed here eventually
7460       --  and perhaps the Resolve routines should be separated for the various
7461       --  arithmetic operations, since they will need different processing. ???
7462
7463       if Nkind (N) in N_Op then
7464          if not Overflow_Checks_Suppressed (Etype (N)) then
7465             Enable_Overflow_Check (N);
7466          end if;
7467       end if;
7468    end Resolve_Op_Expon;
7469
7470    --------------------
7471    -- Resolve_Op_Not --
7472    --------------------
7473
7474    procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
7475       B_Typ : Entity_Id;
7476
7477       function Parent_Is_Boolean return Boolean;
7478       --  This function determines if the parent node is a boolean operator
7479       --  or operation (comparison op, membership test, or short circuit form)
7480       --  and the not in question is the left operand of this operation.
7481       --  Note that if the not is in parens, then false is returned.
7482
7483       -----------------------
7484       -- Parent_Is_Boolean --
7485       -----------------------
7486
7487       function Parent_Is_Boolean return Boolean is
7488       begin
7489          if Paren_Count (N) /= 0 then
7490             return False;
7491
7492          else
7493             case Nkind (Parent (N)) is
7494                when N_Op_And   |
7495                     N_Op_Eq    |
7496                     N_Op_Ge    |
7497                     N_Op_Gt    |
7498                     N_Op_Le    |
7499                     N_Op_Lt    |
7500                     N_Op_Ne    |
7501                     N_Op_Or    |
7502                     N_Op_Xor   |
7503                     N_In       |
7504                     N_Not_In   |
7505                     N_And_Then |
7506                     N_Or_Else  =>
7507
7508                   return Left_Opnd (Parent (N)) = N;
7509
7510                when others =>
7511                   return False;
7512             end case;
7513          end if;
7514       end Parent_Is_Boolean;
7515
7516    --  Start of processing for Resolve_Op_Not
7517
7518    begin
7519       --  Predefined operations on scalar types yield the base type. On the
7520       --  other hand, logical operations on arrays yield the type of the
7521       --  arguments (and the context).
7522
7523       if Is_Array_Type (Typ) then
7524          B_Typ := Typ;
7525       else
7526          B_Typ := Base_Type (Typ);
7527       end if;
7528
7529       if Is_VMS_Operator (Entity (N)) then
7530          null;
7531
7532       --  Straightforward case of incorrect arguments
7533
7534       elsif not Valid_Boolean_Arg (Typ) then
7535          Error_Msg_N ("invalid operand type for operator&", N);
7536          Set_Etype (N, Any_Type);
7537          return;
7538
7539       --  Special case of probable missing parens
7540
7541       elsif Typ = Universal_Integer or else Typ = Any_Modular then
7542          if Parent_Is_Boolean then
7543             Error_Msg_N
7544               ("operand of not must be enclosed in parentheses",
7545                Right_Opnd (N));
7546          else
7547             Error_Msg_N
7548               ("no modular type available in this context", N);
7549          end if;
7550
7551          Set_Etype (N, Any_Type);
7552          return;
7553
7554       --  OK resolution of not
7555
7556       else
7557          --  Warn if non-boolean types involved. This is a case like not a < b
7558          --  where a and b are modular, where we will get (not a) < b and most
7559          --  likely not (a < b) was intended.
7560
7561          if Warn_On_Questionable_Missing_Parens
7562            and then not Is_Boolean_Type (Typ)
7563            and then Parent_Is_Boolean
7564          then
7565             Error_Msg_N ("?not expression should be parenthesized here!", N);
7566          end if;
7567
7568          --  Warn on double negation if checking redundant constructs
7569
7570          if Warn_On_Redundant_Constructs
7571            and then Comes_From_Source (N)
7572            and then Comes_From_Source (Right_Opnd (N))
7573            and then Root_Type (Typ) = Standard_Boolean
7574            and then Nkind (Right_Opnd (N)) = N_Op_Not
7575          then
7576             Error_Msg_N ("redundant double negation?", N);
7577          end if;
7578
7579          --  Complete resolution and evaluation of NOT
7580
7581          Resolve (Right_Opnd (N), B_Typ);
7582          Check_Unset_Reference (Right_Opnd (N));
7583          Set_Etype (N, B_Typ);
7584          Generate_Operator_Reference (N, B_Typ);
7585          Eval_Op_Not (N);
7586       end if;
7587    end Resolve_Op_Not;
7588
7589    -----------------------------
7590    -- Resolve_Operator_Symbol --
7591    -----------------------------
7592
7593    --  Nothing to be done, all resolved already
7594
7595    procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
7596       pragma Warnings (Off, N);
7597       pragma Warnings (Off, Typ);
7598
7599    begin
7600       null;
7601    end Resolve_Operator_Symbol;
7602
7603    ----------------------------------
7604    -- Resolve_Qualified_Expression --
7605    ----------------------------------
7606
7607    procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
7608       pragma Warnings (Off, Typ);
7609
7610       Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
7611       Expr       : constant Node_Id   := Expression (N);
7612
7613    begin
7614       Resolve (Expr, Target_Typ);
7615
7616       --  A qualified expression requires an exact match of the type,
7617       --  class-wide matching is not allowed. However, if the qualifying
7618       --  type is specific and the expression has a class-wide type, it
7619       --  may still be okay, since it can be the result of the expansion
7620       --  of a call to a dispatching function, so we also have to check
7621       --  class-wideness of the type of the expression's original node.
7622
7623       if (Is_Class_Wide_Type (Target_Typ)
7624            or else
7625              (Is_Class_Wide_Type (Etype (Expr))
7626                and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
7627         and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
7628       then
7629          Wrong_Type (Expr, Target_Typ);
7630       end if;
7631
7632       --  If the target type is unconstrained, then we reset the type of
7633       --  the result from the type of the expression. For other cases, the
7634       --  actual subtype of the expression is the target type.
7635
7636       if Is_Composite_Type (Target_Typ)
7637         and then not Is_Constrained (Target_Typ)
7638       then
7639          Set_Etype (N, Etype (Expr));
7640       end if;
7641
7642       Eval_Qualified_Expression (N);
7643    end Resolve_Qualified_Expression;
7644
7645    -------------------
7646    -- Resolve_Range --
7647    -------------------
7648
7649    procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
7650       L : constant Node_Id := Low_Bound (N);
7651       H : constant Node_Id := High_Bound (N);
7652
7653       function First_Last_Ref return Boolean;
7654       --  Returns True if N is of the form X'First .. X'Last where X is the
7655       --  same entity for both attributes.
7656
7657       --------------------
7658       -- First_Last_Ref --
7659       --------------------
7660
7661       function First_Last_Ref return Boolean is
7662          Lorig : constant Node_Id := Original_Node (L);
7663          Horig : constant Node_Id := Original_Node (H);
7664
7665       begin
7666          if Nkind (Lorig) = N_Attribute_Reference
7667            and then Nkind (Horig) = N_Attribute_Reference
7668            and then Attribute_Name (Lorig) = Name_First
7669            and then Attribute_Name (Horig) = Name_Last
7670          then
7671             declare
7672                PL : constant Node_Id := Prefix (Lorig);
7673                PH : constant Node_Id := Prefix (Horig);
7674             begin
7675                if Is_Entity_Name (PL)
7676                  and then Is_Entity_Name (PH)
7677                  and then Entity (PL) = Entity (PH)
7678                then
7679                   return True;
7680                end if;
7681             end;
7682          end if;
7683
7684          return False;
7685       end First_Last_Ref;
7686
7687    --  Start of processing for Resolve_Range
7688
7689    begin
7690       Set_Etype (N, Typ);
7691       Resolve (L, Typ);
7692       Resolve (H, Typ);
7693
7694       --  Check for inappropriate range on unordered enumeration type
7695
7696       if Bad_Unordered_Enumeration_Reference (N, Typ)
7697
7698         --  Exclude X'First .. X'Last if X is the same entity for both
7699
7700         and then not First_Last_Ref
7701       then
7702          Error_Msg ("subrange of unordered enumeration type?", Sloc (N));
7703       end if;
7704
7705       Check_Unset_Reference (L);
7706       Check_Unset_Reference (H);
7707
7708       --  We have to check the bounds for being within the base range as
7709       --  required for a non-static context. Normally this is automatic and
7710       --  done as part of evaluating expressions, but the N_Range node is an
7711       --  exception, since in GNAT we consider this node to be a subexpression,
7712       --  even though in Ada it is not. The circuit in Sem_Eval could check for
7713       --  this, but that would put the test on the main evaluation path for
7714       --  expressions.
7715
7716       Check_Non_Static_Context (L);
7717       Check_Non_Static_Context (H);
7718
7719       --  Check for an ambiguous range over character literals. This will
7720       --  happen with a membership test involving only literals.
7721
7722       if Typ = Any_Character then
7723          Ambiguous_Character (L);
7724          Set_Etype (N, Any_Type);
7725          return;
7726       end if;
7727
7728       --  If bounds are static, constant-fold them, so size computations
7729       --  are identical between front-end and back-end. Do not perform this
7730       --  transformation while analyzing generic units, as type information
7731       --  would then be lost when reanalyzing the constant node in the
7732       --  instance.
7733
7734       if Is_Discrete_Type (Typ) and then Expander_Active then
7735          if Is_OK_Static_Expression (L) then
7736             Fold_Uint  (L, Expr_Value (L), Is_Static_Expression (L));
7737          end if;
7738
7739          if Is_OK_Static_Expression (H) then
7740             Fold_Uint  (H, Expr_Value (H), Is_Static_Expression (H));
7741          end if;
7742       end if;
7743    end Resolve_Range;
7744
7745    --------------------------
7746    -- Resolve_Real_Literal --
7747    --------------------------
7748
7749    procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
7750       Actual_Typ : constant Entity_Id := Etype (N);
7751
7752    begin
7753       --  Special processing for fixed-point literals to make sure that the
7754       --  value is an exact multiple of small where this is required. We
7755       --  skip this for the universal real case, and also for generic types.
7756
7757       if Is_Fixed_Point_Type (Typ)
7758         and then Typ /= Universal_Fixed
7759         and then Typ /= Any_Fixed
7760         and then not Is_Generic_Type (Typ)
7761       then
7762          declare
7763             Val   : constant Ureal := Realval (N);
7764             Cintr : constant Ureal := Val / Small_Value (Typ);
7765             Cint  : constant Uint  := UR_Trunc (Cintr);
7766             Den   : constant Uint  := Norm_Den (Cintr);
7767             Stat  : Boolean;
7768
7769          begin
7770             --  Case of literal is not an exact multiple of the Small
7771
7772             if Den /= 1 then
7773
7774                --  For a source program literal for a decimal fixed-point
7775                --  type, this is statically illegal (RM 4.9(36)).
7776
7777                if Is_Decimal_Fixed_Point_Type (Typ)
7778                  and then Actual_Typ = Universal_Real
7779                  and then Comes_From_Source (N)
7780                then
7781                   Error_Msg_N ("value has extraneous low order digits", N);
7782                end if;
7783
7784                --  Generate a warning if literal from source
7785
7786                if Is_Static_Expression (N)
7787                  and then Warn_On_Bad_Fixed_Value
7788                then
7789                   Error_Msg_N
7790                     ("?static fixed-point value is not a multiple of Small!",
7791                      N);
7792                end if;
7793
7794                --  Replace literal by a value that is the exact representation
7795                --  of a value of the type, i.e. a multiple of the small value,
7796                --  by truncation, since Machine_Rounds is false for all GNAT
7797                --  fixed-point types (RM 4.9(38)).
7798
7799                Stat := Is_Static_Expression (N);
7800                Rewrite (N,
7801                  Make_Real_Literal (Sloc (N),
7802                    Realval => Small_Value (Typ) * Cint));
7803
7804                Set_Is_Static_Expression (N, Stat);
7805             end if;
7806
7807             --  In all cases, set the corresponding integer field
7808
7809             Set_Corresponding_Integer_Value (N, Cint);
7810          end;
7811       end if;
7812
7813       --  Now replace the actual type by the expected type as usual
7814
7815       Set_Etype (N, Typ);
7816       Eval_Real_Literal (N);
7817    end Resolve_Real_Literal;
7818
7819    -----------------------
7820    -- Resolve_Reference --
7821    -----------------------
7822
7823    procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
7824       P : constant Node_Id := Prefix (N);
7825
7826    begin
7827       --  Replace general access with specific type
7828
7829       if Ekind (Etype (N)) = E_Allocator_Type then
7830          Set_Etype (N, Base_Type (Typ));
7831       end if;
7832
7833       Resolve (P, Designated_Type (Etype (N)));
7834
7835       --  If we are taking the reference of a volatile entity, then treat
7836       --  it as a potential modification of this entity. This is much too
7837       --  conservative, but is necessary because remove side effects can
7838       --  result in transformations of normal assignments into reference
7839       --  sequences that otherwise fail to notice the modification.
7840
7841       if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
7842          Note_Possible_Modification (P, Sure => False);
7843       end if;
7844    end Resolve_Reference;
7845
7846    --------------------------------
7847    -- Resolve_Selected_Component --
7848    --------------------------------
7849
7850    procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
7851       Comp  : Entity_Id;
7852       Comp1 : Entity_Id        := Empty; -- prevent junk warning
7853       P     : constant Node_Id := Prefix  (N);
7854       S     : constant Node_Id := Selector_Name (N);
7855       T     : Entity_Id        := Etype (P);
7856       I     : Interp_Index;
7857       I1    : Interp_Index := 0; -- prevent junk warning
7858       It    : Interp;
7859       It1   : Interp;
7860       Found : Boolean;
7861
7862       function Init_Component return Boolean;
7863       --  Check whether this is the initialization of a component within an
7864       --  init proc (by assignment or call to another init proc). If true,
7865       --  there is no need for a discriminant check.
7866
7867       --------------------
7868       -- Init_Component --
7869       --------------------
7870
7871       function Init_Component return Boolean is
7872       begin
7873          return Inside_Init_Proc
7874            and then Nkind (Prefix (N)) = N_Identifier
7875            and then Chars (Prefix (N)) = Name_uInit
7876            and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
7877       end Init_Component;
7878
7879    --  Start of processing for Resolve_Selected_Component
7880
7881    begin
7882       if Is_Overloaded (P) then
7883
7884          --  Use the context type to select the prefix that has a selector
7885          --  of the correct name and type.
7886
7887          Found := False;
7888          Get_First_Interp (P, I, It);
7889
7890          Search : while Present (It.Typ) loop
7891             if Is_Access_Type (It.Typ) then
7892                T := Designated_Type (It.Typ);
7893             else
7894                T := It.Typ;
7895             end if;
7896
7897             if Is_Record_Type (T) then
7898
7899                --  The visible components of a class-wide type are those of
7900                --  the root type.
7901
7902                if Is_Class_Wide_Type (T) then
7903                   T := Etype (T);
7904                end if;
7905
7906                Comp := First_Entity (T);
7907                while Present (Comp) loop
7908                   if Chars (Comp) = Chars (S)
7909                     and then Covers (Etype (Comp), Typ)
7910                   then
7911                      if not Found then
7912                         Found := True;
7913                         I1  := I;
7914                         It1 := It;
7915                         Comp1 := Comp;
7916
7917                      else
7918                         It := Disambiguate (P, I1, I, Any_Type);
7919
7920                         if It = No_Interp then
7921                            Error_Msg_N
7922                              ("ambiguous prefix for selected component",  N);
7923                            Set_Etype (N, Typ);
7924                            return;
7925
7926                         else
7927                            It1 := It;
7928
7929                            --  There may be an implicit dereference. Retrieve
7930                            --  designated record type.
7931
7932                            if Is_Access_Type (It1.Typ) then
7933                               T := Designated_Type (It1.Typ);
7934                            else
7935                               T := It1.Typ;
7936                            end if;
7937
7938                            if Scope (Comp1) /= T then
7939
7940                               --  Resolution chooses the new interpretation.
7941                               --  Find the component with the right name.
7942
7943                               Comp1 := First_Entity (T);
7944                               while Present (Comp1)
7945                                 and then Chars (Comp1) /= Chars (S)
7946                               loop
7947                                  Comp1 := Next_Entity (Comp1);
7948                               end loop;
7949                            end if;
7950
7951                            exit Search;
7952                         end if;
7953                      end if;
7954                   end if;
7955
7956                   Comp := Next_Entity (Comp);
7957                end loop;
7958             end if;
7959
7960             Get_Next_Interp (I, It);
7961          end loop Search;
7962
7963          Resolve (P, It1.Typ);
7964          Set_Etype (N, Typ);
7965          Set_Entity_With_Style_Check (S, Comp1);
7966
7967       else
7968          --  Resolve prefix with its type
7969
7970          Resolve (P, T);
7971       end if;
7972
7973       --  Generate cross-reference. We needed to wait until full overloading
7974       --  resolution was complete to do this, since otherwise we can't tell if
7975       --  we are an lvalue or not.
7976
7977       if May_Be_Lvalue (N) then
7978          Generate_Reference (Entity (S), S, 'm');
7979       else
7980          Generate_Reference (Entity (S), S, 'r');
7981       end if;
7982
7983       --  If prefix is an access type, the node will be transformed into an
7984       --  explicit dereference during expansion. The type of the node is the
7985       --  designated type of that of the prefix.
7986
7987       if Is_Access_Type (Etype (P)) then
7988          T := Designated_Type (Etype (P));
7989          Check_Fully_Declared_Prefix (T, P);
7990       else
7991          T := Etype (P);
7992       end if;
7993
7994       if Has_Discriminants (T)
7995         and then Ekind_In (Entity (S), E_Component, E_Discriminant)
7996         and then Present (Original_Record_Component (Entity (S)))
7997         and then Ekind (Original_Record_Component (Entity (S))) = E_Component
7998         and then Present (Discriminant_Checking_Func
7999                            (Original_Record_Component (Entity (S))))
8000         and then not Discriminant_Checks_Suppressed (T)
8001         and then not Init_Component
8002       then
8003          Set_Do_Discriminant_Check (N);
8004       end if;
8005
8006       if Ekind (Entity (S)) = E_Void then
8007          Error_Msg_N ("premature use of component", S);
8008       end if;
8009
8010       --  If the prefix is a record conversion, this may be a renamed
8011       --  discriminant whose bounds differ from those of the original
8012       --  one, so we must ensure that a range check is performed.
8013
8014       if Nkind (P) = N_Type_Conversion
8015         and then Ekind (Entity (S)) = E_Discriminant
8016         and then Is_Discrete_Type (Typ)
8017       then
8018          Set_Etype (N, Base_Type (Typ));
8019       end if;
8020
8021       --  Note: No Eval processing is required, because the prefix is of a
8022       --  record type, or protected type, and neither can possibly be static.
8023
8024       --  If the array type is atomic, and is packed, and we are in a left side
8025       --  context, then this is worth a warning, since we have a situation
8026       --  where the access to the component may cause extra read/writes of
8027       --  the atomic array object, which could be considered unexpected.
8028
8029       if Nkind (N) = N_Selected_Component
8030         and then (Is_Atomic (T)
8031                    or else (Is_Entity_Name (Prefix (N))
8032                              and then Is_Atomic (Entity (Prefix (N)))))
8033         and then Is_Packed (T)
8034         and then Is_LHS (N)
8035       then
8036          Error_Msg_N ("?assignment to component of packed atomic record",
8037                       Prefix (N));
8038          Error_Msg_N ("?\may cause unexpected accesses to atomic object",
8039                       Prefix (N));
8040       end if;
8041    end Resolve_Selected_Component;
8042
8043    -------------------
8044    -- Resolve_Shift --
8045    -------------------
8046
8047    procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
8048       B_Typ : constant Entity_Id := Base_Type (Typ);
8049       L     : constant Node_Id   := Left_Opnd  (N);
8050       R     : constant Node_Id   := Right_Opnd (N);
8051
8052    begin
8053       --  We do the resolution using the base type, because intermediate values
8054       --  in expressions always are of the base type, not a subtype of it.
8055
8056       Resolve (L, B_Typ);
8057       Resolve (R, Standard_Natural);
8058
8059       Check_Unset_Reference (L);
8060       Check_Unset_Reference (R);
8061
8062       Set_Etype (N, B_Typ);
8063       Generate_Operator_Reference (N, B_Typ);
8064       Eval_Shift (N);
8065    end Resolve_Shift;
8066
8067    ---------------------------
8068    -- Resolve_Short_Circuit --
8069    ---------------------------
8070
8071    procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
8072       B_Typ : constant Entity_Id := Base_Type (Typ);
8073       L     : constant Node_Id   := Left_Opnd  (N);
8074       R     : constant Node_Id   := Right_Opnd (N);
8075
8076    begin
8077       --  Why are the calls to Check_Order_Dependence commented out ???
8078       Resolve (L, B_Typ);
8079       --  Check_Order_Dependence;   --  For AI05-0144
8080       Resolve (R, B_Typ);
8081       --  Check_Order_Dependence;   --  For AI05-0144
8082
8083       --  Check for issuing warning for always False assert/check, this happens
8084       --  when assertions are turned off, in which case the pragma Assert/Check
8085       --  was transformed into:
8086
8087       --     if False and then <condition> then ...
8088
8089       --  and we detect this pattern
8090
8091       if Warn_On_Assertion_Failure
8092         and then Is_Entity_Name (R)
8093         and then Entity (R) = Standard_False
8094         and then Nkind (Parent (N)) = N_If_Statement
8095         and then Nkind (N) = N_And_Then
8096         and then Is_Entity_Name (L)
8097         and then Entity (L) = Standard_False
8098       then
8099          declare
8100             Orig : constant Node_Id := Original_Node (Parent (N));
8101
8102          begin
8103             if Nkind (Orig) = N_Pragma
8104               and then Pragma_Name (Orig) = Name_Assert
8105             then
8106                --  Don't want to warn if original condition is explicit False
8107
8108                declare
8109                   Expr : constant Node_Id :=
8110                            Original_Node
8111                              (Expression
8112                                (First (Pragma_Argument_Associations (Orig))));
8113                begin
8114                   if Is_Entity_Name (Expr)
8115                     and then Entity (Expr) = Standard_False
8116                   then
8117                      null;
8118                   else
8119                      --  Issue warning. We do not want the deletion of the
8120                      --  IF/AND-THEN to take this message with it. We achieve
8121                      --  this by making sure that the expanded code points to
8122                      --  the Sloc of the expression, not the original pragma.
8123
8124                      Error_Msg_N
8125                        ("?assertion would fail at run-time!",
8126                         Expression
8127                           (First (Pragma_Argument_Associations (Orig))));
8128                   end if;
8129                end;
8130
8131             --  Similar processing for Check pragma
8132
8133             elsif Nkind (Orig) = N_Pragma
8134               and then Pragma_Name (Orig) = Name_Check
8135             then
8136                --  Don't want to warn if original condition is explicit False
8137
8138                declare
8139                   Expr : constant Node_Id :=
8140                            Original_Node
8141                              (Expression
8142                                 (Next (First
8143                                   (Pragma_Argument_Associations (Orig)))));
8144                begin
8145                   if Is_Entity_Name (Expr)
8146                     and then Entity (Expr) = Standard_False
8147                   then
8148                      null;
8149                   else
8150                      Error_Msg_N
8151                        ("?check would fail at run-time!",
8152                         Expression
8153                           (Last (Pragma_Argument_Associations (Orig))));
8154                   end if;
8155                end;
8156             end if;
8157          end;
8158       end if;
8159
8160       --  Continue with processing of short circuit
8161
8162       Check_Unset_Reference (L);
8163       Check_Unset_Reference (R);
8164
8165       Set_Etype (N, B_Typ);
8166       Eval_Short_Circuit (N);
8167    end Resolve_Short_Circuit;
8168
8169    -------------------
8170    -- Resolve_Slice --
8171    -------------------
8172
8173    procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
8174       Name       : constant Node_Id := Prefix (N);
8175       Drange     : constant Node_Id := Discrete_Range (N);
8176       Array_Type : Entity_Id        := Empty;
8177       Index      : Node_Id;
8178
8179    begin
8180       if Is_Overloaded (Name) then
8181
8182          --  Use the context type to select the prefix that yields the correct
8183          --  array type.
8184
8185          declare
8186             I      : Interp_Index;
8187             I1     : Interp_Index := 0;
8188             It     : Interp;
8189             P      : constant Node_Id := Prefix (N);
8190             Found  : Boolean := False;
8191
8192          begin
8193             Get_First_Interp (P, I,  It);
8194             while Present (It.Typ) loop
8195                if (Is_Array_Type (It.Typ)
8196                     and then Covers (Typ,  It.Typ))
8197                  or else (Is_Access_Type (It.Typ)
8198                            and then Is_Array_Type (Designated_Type (It.Typ))
8199                            and then Covers (Typ, Designated_Type (It.Typ)))
8200                then
8201                   if Found then
8202                      It := Disambiguate (P, I1, I, Any_Type);
8203
8204                      if It = No_Interp then
8205                         Error_Msg_N ("ambiguous prefix for slicing",  N);
8206                         Set_Etype (N, Typ);
8207                         return;
8208                      else
8209                         Found := True;
8210                         Array_Type := It.Typ;
8211                         I1 := I;
8212                      end if;
8213                   else
8214                      Found := True;
8215                      Array_Type := It.Typ;
8216                      I1 := I;
8217                   end if;
8218                end if;
8219
8220                Get_Next_Interp (I, It);
8221             end loop;
8222          end;
8223
8224       else
8225          Array_Type := Etype (Name);
8226       end if;
8227
8228       Resolve (Name, Array_Type);
8229
8230       if Is_Access_Type (Array_Type) then
8231          Apply_Access_Check (N);
8232          Array_Type := Designated_Type (Array_Type);
8233
8234          --  If the prefix is an access to an unconstrained array, we must use
8235          --  the actual subtype of the object to perform the index checks. The
8236          --  object denoted by the prefix is implicit in the node, so we build
8237          --  an explicit representation for it in order to compute the actual
8238          --  subtype.
8239
8240          if not Is_Constrained (Array_Type) then
8241             Remove_Side_Effects (Prefix (N));
8242
8243             declare
8244                Obj : constant Node_Id :=
8245                        Make_Explicit_Dereference (Sloc (N),
8246                          Prefix => New_Copy_Tree (Prefix (N)));
8247             begin
8248                Set_Etype (Obj, Array_Type);
8249                Set_Parent (Obj, Parent (N));
8250                Array_Type := Get_Actual_Subtype (Obj);
8251             end;
8252          end if;
8253
8254       elsif Is_Entity_Name (Name)
8255         or else Nkind (Name) = N_Explicit_Dereference
8256         or else (Nkind (Name) = N_Function_Call
8257                   and then not Is_Constrained (Etype (Name)))
8258       then
8259          Array_Type := Get_Actual_Subtype (Name);
8260
8261       --  If the name is a selected component that depends on discriminants,
8262       --  build an actual subtype for it. This can happen only when the name
8263       --  itself is overloaded; otherwise the actual subtype is created when
8264       --  the selected component is analyzed.
8265
8266       elsif Nkind (Name) = N_Selected_Component
8267         and then Full_Analysis
8268         and then Depends_On_Discriminant (First_Index (Array_Type))
8269       then
8270          declare
8271             Act_Decl : constant Node_Id :=
8272                          Build_Actual_Subtype_Of_Component (Array_Type, Name);
8273          begin
8274             Insert_Action (N, Act_Decl);
8275             Array_Type := Defining_Identifier (Act_Decl);
8276          end;
8277
8278       --  Maybe this should just be "else", instead of checking for the
8279       --  specific case of slice??? This is needed for the case where
8280       --  the prefix is an Image attribute, which gets expanded to a
8281       --  slice, and so has a constrained subtype which we want to use
8282       --  for the slice range check applied below (the range check won't
8283       --  get done if the unconstrained subtype of the 'Image is used).
8284
8285       elsif Nkind (Name) = N_Slice then
8286          Array_Type := Etype (Name);
8287       end if;
8288
8289       --  If name was overloaded, set slice type correctly now
8290
8291       Set_Etype (N, Array_Type);
8292
8293       --  If the range is specified by a subtype mark, no resolution is
8294       --  necessary. Else resolve the bounds, and apply needed checks.
8295
8296       if not Is_Entity_Name (Drange) then
8297          Index := First_Index (Array_Type);
8298          Resolve (Drange, Base_Type (Etype (Index)));
8299
8300          if Nkind (Drange) = N_Range
8301
8302             --  Do not apply the range check to nodes associated with the
8303             --  frontend expansion of the dispatch table. We first check
8304             --  if Ada.Tags is already loaded to void the addition of an
8305             --  undesired dependence on such run-time unit.
8306
8307            and then
8308              (not Tagged_Type_Expansion
8309                or else not
8310                  (RTU_Loaded (Ada_Tags)
8311                    and then Nkind (Prefix (N)) = N_Selected_Component
8312                    and then Present (Entity (Selector_Name (Prefix (N))))
8313                    and then Entity (Selector_Name (Prefix (N))) =
8314                                          RTE_Record_Component (RE_Prims_Ptr)))
8315          then
8316             Apply_Range_Check (Drange, Etype (Index));
8317          end if;
8318       end if;
8319
8320       Set_Slice_Subtype (N);
8321
8322       if Nkind (Drange) = N_Range then
8323          Warn_On_Suspicious_Index (Name, Low_Bound  (Drange));
8324          Warn_On_Suspicious_Index (Name, High_Bound (Drange));
8325       end if;
8326
8327       Eval_Slice (N);
8328    end Resolve_Slice;
8329
8330    ----------------------------
8331    -- Resolve_String_Literal --
8332    ----------------------------
8333
8334    procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
8335       C_Typ      : constant Entity_Id  := Component_Type (Typ);
8336       R_Typ      : constant Entity_Id  := Root_Type (C_Typ);
8337       Loc        : constant Source_Ptr := Sloc (N);
8338       Str        : constant String_Id  := Strval (N);
8339       Strlen     : constant Nat        := String_Length (Str);
8340       Subtype_Id : Entity_Id;
8341       Need_Check : Boolean;
8342
8343    begin
8344       --  For a string appearing in a concatenation, defer creation of the
8345       --  string_literal_subtype until the end of the resolution of the
8346       --  concatenation, because the literal may be constant-folded away. This
8347       --  is a useful optimization for long concatenation expressions.
8348
8349       --  If the string is an aggregate built for a single character (which
8350       --  happens in a non-static context) or a is null string to which special
8351       --  checks may apply, we build the subtype. Wide strings must also get a
8352       --  string subtype if they come from a one character aggregate. Strings
8353       --  generated by attributes might be static, but it is often hard to
8354       --  determine whether the enclosing context is static, so we generate
8355       --  subtypes for them as well, thus losing some rarer optimizations ???
8356       --  Same for strings that come from a static conversion.
8357
8358       Need_Check :=
8359         (Strlen = 0 and then Typ /= Standard_String)
8360           or else Nkind (Parent (N)) /= N_Op_Concat
8361           or else (N /= Left_Opnd (Parent (N))
8362                     and then N /= Right_Opnd (Parent (N)))
8363           or else ((Typ = Standard_Wide_String
8364                       or else Typ = Standard_Wide_Wide_String)
8365                     and then Nkind (Original_Node (N)) /= N_String_Literal);
8366
8367       --  If the resolving type is itself a string literal subtype, we can just
8368       --  reuse it, since there is no point in creating another.
8369
8370       if Ekind (Typ) = E_String_Literal_Subtype then
8371          Subtype_Id := Typ;
8372
8373       elsif Nkind (Parent (N)) = N_Op_Concat
8374         and then not Need_Check
8375         and then not Nkind_In (Original_Node (N), N_Character_Literal,
8376                                                   N_Attribute_Reference,
8377                                                   N_Qualified_Expression,
8378                                                   N_Type_Conversion)
8379       then
8380          Subtype_Id := Typ;
8381
8382       --  Otherwise we must create a string literal subtype. Note that the
8383       --  whole idea of string literal subtypes is simply to avoid the need
8384       --  for building a full fledged array subtype for each literal.
8385
8386       else
8387          Set_String_Literal_Subtype (N, Typ);
8388          Subtype_Id := Etype (N);
8389       end if;
8390
8391       if Nkind (Parent (N)) /= N_Op_Concat
8392         or else Need_Check
8393       then
8394          Set_Etype (N, Subtype_Id);
8395          Eval_String_Literal (N);
8396       end if;
8397
8398       if Is_Limited_Composite (Typ)
8399         or else Is_Private_Composite (Typ)
8400       then
8401          Error_Msg_N ("string literal not available for private array", N);
8402          Set_Etype (N, Any_Type);
8403          return;
8404       end if;
8405
8406       --  The validity of a null string has been checked in the call to
8407       --  Eval_String_Literal.
8408
8409       if Strlen = 0 then
8410          return;
8411
8412       --  Always accept string literal with component type Any_Character, which
8413       --  occurs in error situations and in comparisons of literals, both of
8414       --  which should accept all literals.
8415
8416       elsif R_Typ = Any_Character then
8417          return;
8418
8419       --  If the type is bit-packed, then we always transform the string
8420       --  literal into a full fledged aggregate.
8421
8422       elsif Is_Bit_Packed_Array (Typ) then
8423          null;
8424
8425       --  Deal with cases of Wide_Wide_String, Wide_String, and String
8426
8427       else
8428          --  For Standard.Wide_Wide_String, or any other type whose component
8429          --  type is Standard.Wide_Wide_Character, we know that all the
8430          --  characters in the string must be acceptable, since the parser
8431          --  accepted the characters as valid character literals.
8432
8433          if R_Typ = Standard_Wide_Wide_Character then
8434             null;
8435
8436          --  For the case of Standard.String, or any other type whose component
8437          --  type is Standard.Character, we must make sure that there are no
8438          --  wide characters in the string, i.e. that it is entirely composed
8439          --  of characters in range of type Character.
8440
8441          --  If the string literal is the result of a static concatenation, the
8442          --  test has already been performed on the components, and need not be
8443          --  repeated.
8444
8445          elsif R_Typ = Standard_Character
8446            and then Nkind (Original_Node (N)) /= N_Op_Concat
8447          then
8448             for J in 1 .. Strlen loop
8449                if not In_Character_Range (Get_String_Char (Str, J)) then
8450
8451                   --  If we are out of range, post error. This is one of the
8452                   --  very few places that we place the flag in the middle of
8453                   --  a token, right under the offending wide character. Not
8454                   --  quite clear if this is right wrt wide character encoding
8455                   --  sequences, but it's only an error message!
8456
8457                   Error_Msg
8458                     ("literal out of range of type Standard.Character",
8459                      Source_Ptr (Int (Loc) + J));
8460                   return;
8461                end if;
8462             end loop;
8463
8464          --  For the case of Standard.Wide_String, or any other type whose
8465          --  component type is Standard.Wide_Character, we must make sure that
8466          --  there are no wide characters in the string, i.e. that it is
8467          --  entirely composed of characters in range of type Wide_Character.
8468
8469          --  If the string literal is the result of a static concatenation,
8470          --  the test has already been performed on the components, and need
8471          --  not be repeated.
8472
8473          elsif R_Typ = Standard_Wide_Character
8474            and then Nkind (Original_Node (N)) /= N_Op_Concat
8475          then
8476             for J in 1 .. Strlen loop
8477                if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
8478
8479                   --  If we are out of range, post error. This is one of the
8480                   --  very few places that we place the flag in the middle of
8481                   --  a token, right under the offending wide character.
8482
8483                   --  This is not quite right, because characters in general
8484                   --  will take more than one character position ???
8485
8486                   Error_Msg
8487                     ("literal out of range of type Standard.Wide_Character",
8488                      Source_Ptr (Int (Loc) + J));
8489                   return;
8490                end if;
8491             end loop;
8492
8493          --  If the root type is not a standard character, then we will convert
8494          --  the string into an aggregate and will let the aggregate code do
8495          --  the checking. Standard Wide_Wide_Character is also OK here.
8496
8497          else
8498             null;
8499          end if;
8500
8501          --  See if the component type of the array corresponding to the string
8502          --  has compile time known bounds. If yes we can directly check
8503          --  whether the evaluation of the string will raise constraint error.
8504          --  Otherwise we need to transform the string literal into the
8505          --  corresponding character aggregate and let the aggregate
8506          --  code do the checking.
8507
8508          if Is_Standard_Character_Type (R_Typ) then
8509
8510             --  Check for the case of full range, where we are definitely OK
8511
8512             if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
8513                return;
8514             end if;
8515
8516             --  Here the range is not the complete base type range, so check
8517
8518             declare
8519                Comp_Typ_Lo : constant Node_Id :=
8520                                Type_Low_Bound (Component_Type (Typ));
8521                Comp_Typ_Hi : constant Node_Id :=
8522                                Type_High_Bound (Component_Type (Typ));
8523
8524                Char_Val : Uint;
8525
8526             begin
8527                if Compile_Time_Known_Value (Comp_Typ_Lo)
8528                  and then Compile_Time_Known_Value (Comp_Typ_Hi)
8529                then
8530                   for J in 1 .. Strlen loop
8531                      Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
8532
8533                      if Char_Val < Expr_Value (Comp_Typ_Lo)
8534                        or else Char_Val > Expr_Value (Comp_Typ_Hi)
8535                      then
8536                         Apply_Compile_Time_Constraint_Error
8537                           (N, "character out of range?", CE_Range_Check_Failed,
8538                            Loc => Source_Ptr (Int (Loc) + J));
8539                      end if;
8540                   end loop;
8541
8542                   return;
8543                end if;
8544             end;
8545          end if;
8546       end if;
8547
8548       --  If we got here we meed to transform the string literal into the
8549       --  equivalent qualified positional array aggregate. This is rather
8550       --  heavy artillery for this situation, but it is hard work to avoid.
8551
8552       declare
8553          Lits : constant List_Id    := New_List;
8554          P    : Source_Ptr := Loc + 1;
8555          C    : Char_Code;
8556
8557       begin
8558          --  Build the character literals, we give them source locations that
8559          --  correspond to the string positions, which is a bit tricky given
8560          --  the possible presence of wide character escape sequences.
8561
8562          for J in 1 .. Strlen loop
8563             C := Get_String_Char (Str, J);
8564             Set_Character_Literal_Name (C);
8565
8566             Append_To (Lits,
8567               Make_Character_Literal (P,
8568                 Chars              => Name_Find,
8569                 Char_Literal_Value => UI_From_CC (C)));
8570
8571             if In_Character_Range (C) then
8572                P := P + 1;
8573
8574             --  Should we have a call to Skip_Wide here ???
8575             --  ???     else
8576             --             Skip_Wide (P);
8577
8578             end if;
8579          end loop;
8580
8581          Rewrite (N,
8582            Make_Qualified_Expression (Loc,
8583              Subtype_Mark => New_Reference_To (Typ, Loc),
8584              Expression   =>
8585                Make_Aggregate (Loc, Expressions => Lits)));
8586
8587          Analyze_And_Resolve (N, Typ);
8588       end;
8589    end Resolve_String_Literal;
8590
8591    -----------------------------
8592    -- Resolve_Subprogram_Info --
8593    -----------------------------
8594
8595    procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
8596    begin
8597       Set_Etype (N, Typ);
8598    end Resolve_Subprogram_Info;
8599
8600    -----------------------------
8601    -- Resolve_Type_Conversion --
8602    -----------------------------
8603
8604    procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
8605       Conv_OK     : constant Boolean   := Conversion_OK (N);
8606       Operand     : constant Node_Id   := Expression (N);
8607       Operand_Typ : constant Entity_Id := Etype (Operand);
8608       Target_Typ  : constant Entity_Id := Etype (N);
8609       Rop         : Node_Id;
8610       Orig_N      : Node_Id;
8611       Orig_T      : Node_Id;
8612
8613    begin
8614       if not Conv_OK
8615         and then not Valid_Conversion (N, Target_Typ, Operand)
8616       then
8617          return;
8618       end if;
8619
8620       if Etype (Operand) = Any_Fixed then
8621
8622          --  Mixed-mode operation involving a literal. Context must be a fixed
8623          --  type which is applied to the literal subsequently.
8624
8625          if Is_Fixed_Point_Type (Typ) then
8626             Set_Etype (Operand, Universal_Real);
8627
8628          elsif Is_Numeric_Type (Typ)
8629            and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
8630            and then (Etype (Right_Opnd (Operand)) = Universal_Real
8631                        or else
8632                      Etype (Left_Opnd  (Operand)) = Universal_Real)
8633          then
8634             --  Return if expression is ambiguous
8635
8636             if Unique_Fixed_Point_Type (N) = Any_Type then
8637                return;
8638
8639             --  If nothing else, the available fixed type is Duration
8640
8641             else
8642                Set_Etype (Operand, Standard_Duration);
8643             end if;
8644
8645             --  Resolve the real operand with largest available precision
8646
8647             if Etype (Right_Opnd (Operand)) = Universal_Real then
8648                Rop := New_Copy_Tree (Right_Opnd (Operand));
8649             else
8650                Rop := New_Copy_Tree (Left_Opnd (Operand));
8651             end if;
8652
8653             Resolve (Rop, Universal_Real);
8654
8655             --  If the operand is a literal (it could be a non-static and
8656             --  illegal exponentiation) check whether the use of Duration
8657             --  is potentially inaccurate.
8658
8659             if Nkind (Rop) = N_Real_Literal
8660               and then Realval (Rop) /= Ureal_0
8661               and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
8662             then
8663                Error_Msg_N
8664                  ("?universal real operand can only " &
8665                   "be interpreted as Duration!",
8666                   Rop);
8667                Error_Msg_N
8668                  ("\?precision will be lost in the conversion!", Rop);
8669             end if;
8670
8671          elsif Is_Numeric_Type (Typ)
8672            and then Nkind (Operand) in N_Op
8673            and then Unique_Fixed_Point_Type (N) /= Any_Type
8674          then
8675             Set_Etype (Operand, Standard_Duration);
8676
8677          else
8678             Error_Msg_N ("invalid context for mixed mode operation", N);
8679             Set_Etype (Operand, Any_Type);
8680             return;
8681          end if;
8682       end if;
8683
8684       Resolve (Operand);
8685
8686       --  Note: we do the Eval_Type_Conversion call before applying the
8687       --  required checks for a subtype conversion. This is important, since
8688       --  both are prepared under certain circumstances to change the type
8689       --  conversion to a constraint error node, but in the case of
8690       --  Eval_Type_Conversion this may reflect an illegality in the static
8691       --  case, and we would miss the illegality (getting only a warning
8692       --  message), if we applied the type conversion checks first.
8693
8694       Eval_Type_Conversion (N);
8695
8696       --  Even when evaluation is not possible, we may be able to simplify the
8697       --  conversion or its expression. This needs to be done before applying
8698       --  checks, since otherwise the checks may use the original expression
8699       --  and defeat the simplifications. This is specifically the case for
8700       --  elimination of the floating-point Truncation attribute in
8701       --  float-to-int conversions.
8702
8703       Simplify_Type_Conversion (N);
8704
8705       --  If after evaluation we still have a type conversion, then we may need
8706       --  to apply checks required for a subtype conversion.
8707
8708       --  Skip these type conversion checks if universal fixed operands
8709       --  operands involved, since range checks are handled separately for
8710       --  these cases (in the appropriate Expand routines in unit Exp_Fixd).
8711
8712       if Nkind (N) = N_Type_Conversion
8713         and then not Is_Generic_Type (Root_Type (Target_Typ))
8714         and then Target_Typ  /= Universal_Fixed
8715         and then Operand_Typ /= Universal_Fixed
8716       then
8717          Apply_Type_Conversion_Checks (N);
8718       end if;
8719
8720       --  Issue warning for conversion of simple object to its own type. We
8721       --  have to test the original nodes, since they may have been rewritten
8722       --  by various optimizations.
8723
8724       Orig_N := Original_Node (N);
8725
8726       if Warn_On_Redundant_Constructs
8727         and then Comes_From_Source (Orig_N)
8728         and then Nkind (Orig_N) = N_Type_Conversion
8729         and then not In_Instance
8730       then
8731          Orig_N := Original_Node (Expression (Orig_N));
8732          Orig_T := Target_Typ;
8733
8734          --  If the node is part of a larger expression, the Target_Type
8735          --  may not be the original type of the node if the context is a
8736          --  condition. Recover original type to see if conversion is needed.
8737
8738          if Is_Boolean_Type (Orig_T)
8739           and then Nkind (Parent (N)) in N_Op
8740          then
8741             Orig_T := Etype (Parent (N));
8742          end if;
8743
8744          if Is_Entity_Name (Orig_N)
8745            and then
8746              (Etype (Entity (Orig_N)) = Orig_T
8747                 or else
8748                   (Ekind (Entity (Orig_N)) = E_Loop_Parameter
8749                     and then Covers (Orig_T, Etype (Entity (Orig_N)))))
8750          then
8751             --  One more check, do not give warning if the analyzed conversion
8752             --  has an expression with non-static bounds, and the bounds of the
8753             --  target are static. This avoids junk warnings in cases where the
8754             --  conversion is necessary to establish staticness, for example in
8755             --  a case statement.
8756
8757             if not Is_OK_Static_Subtype (Operand_Typ)
8758               and then Is_OK_Static_Subtype (Target_Typ)
8759             then
8760                null;
8761
8762             --  Here we give the redundant conversion warning
8763
8764             else
8765                Error_Msg_Node_2 := Orig_T;
8766                Error_Msg_NE -- CODEFIX
8767                  ("?redundant conversion, & is of type &!",
8768                   N, Entity (Orig_N));
8769             end if;
8770          end if;
8771       end if;
8772
8773       --  Ada 2005 (AI-251): Handle class-wide interface type conversions.
8774       --  No need to perform any interface conversion if the type of the
8775       --  expression coincides with the target type.
8776
8777       if Ada_Version >= Ada_05
8778         and then Expander_Active
8779         and then Operand_Typ /= Target_Typ
8780       then
8781          declare
8782             Opnd   : Entity_Id := Operand_Typ;
8783             Target : Entity_Id := Target_Typ;
8784
8785          begin
8786             if Is_Access_Type (Opnd) then
8787                Opnd := Designated_Type (Opnd);
8788             end if;
8789
8790             if Is_Access_Type (Target_Typ) then
8791                Target := Designated_Type (Target);
8792             end if;
8793
8794             if Opnd = Target then
8795                null;
8796
8797             --  Conversion from interface type
8798
8799             elsif Is_Interface (Opnd) then
8800
8801                --  Ada 2005 (AI-217): Handle entities from limited views
8802
8803                if From_With_Type (Opnd) then
8804                   Error_Msg_Qual_Level := 99;
8805                   Error_Msg_NE -- CODEFIX
8806                     ("missing WITH clause on package &", N,
8807                     Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
8808                   Error_Msg_N
8809                     ("type conversions require visibility of the full view",
8810                      N);
8811
8812                elsif From_With_Type (Target)
8813                  and then not
8814                    (Is_Access_Type (Target_Typ)
8815                       and then Present (Non_Limited_View (Etype (Target))))
8816                then
8817                   Error_Msg_Qual_Level := 99;
8818                   Error_Msg_NE -- CODEFIX
8819                     ("missing WITH clause on package &", N,
8820                     Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
8821                   Error_Msg_N
8822                     ("type conversions require visibility of the full view",
8823                      N);
8824
8825                else
8826                   Expand_Interface_Conversion (N, Is_Static => False);
8827                end if;
8828
8829             --  Conversion to interface type
8830
8831             elsif Is_Interface (Target) then
8832
8833                --  Handle subtypes
8834
8835                if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
8836                   Opnd := Etype (Opnd);
8837                end if;
8838
8839                if not Interface_Present_In_Ancestor
8840                         (Typ   => Opnd,
8841                          Iface => Target)
8842                then
8843                   if Is_Class_Wide_Type (Opnd) then
8844
8845                      --  The static analysis is not enough to know if the
8846                      --  interface is implemented or not. Hence we must pass
8847                      --  the work to the expander to generate code to evaluate
8848                      --  the conversion at run-time.
8849
8850                      Expand_Interface_Conversion (N, Is_Static => False);
8851
8852                   else
8853                      Error_Msg_Name_1 := Chars (Etype (Target));
8854                      Error_Msg_Name_2 := Chars (Opnd);
8855                      Error_Msg_N
8856                        ("wrong interface conversion (% is not a progenitor " &
8857                         "of %)", N);
8858                   end if;
8859
8860                else
8861                   Expand_Interface_Conversion (N);
8862                end if;
8863             end if;
8864          end;
8865       end if;
8866    end Resolve_Type_Conversion;
8867
8868    ----------------------
8869    -- Resolve_Unary_Op --
8870    ----------------------
8871
8872    procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
8873       B_Typ : constant Entity_Id := Base_Type (Typ);
8874       R     : constant Node_Id   := Right_Opnd (N);
8875       OK    : Boolean;
8876       Lo    : Uint;
8877       Hi    : Uint;
8878
8879    begin
8880       --  Deal with intrinsic unary operators
8881
8882       if Comes_From_Source (N)
8883         and then Ekind (Entity (N)) = E_Function
8884         and then Is_Imported (Entity (N))
8885         and then Is_Intrinsic_Subprogram (Entity (N))
8886       then
8887          Resolve_Intrinsic_Unary_Operator (N, Typ);
8888          return;
8889       end if;
8890
8891       --  Deal with universal cases
8892
8893       if Etype (R) = Universal_Integer
8894            or else
8895          Etype (R) = Universal_Real
8896       then
8897          Check_For_Visible_Operator (N, B_Typ);
8898       end if;
8899
8900       Set_Etype (N, B_Typ);
8901       Resolve (R, B_Typ);
8902
8903       --  Generate warning for expressions like abs (x mod 2)
8904
8905       if Warn_On_Redundant_Constructs
8906         and then Nkind (N) = N_Op_Abs
8907       then
8908          Determine_Range (Right_Opnd (N), OK, Lo, Hi);
8909
8910          if OK and then Hi >= Lo and then Lo >= 0 then
8911             Error_Msg_N -- CODEFIX
8912              ("?abs applied to known non-negative value has no effect", N);
8913          end if;
8914       end if;
8915
8916       --  Deal with reference generation
8917
8918       Check_Unset_Reference (R);
8919       Generate_Operator_Reference (N, B_Typ);
8920       Eval_Unary_Op (N);
8921
8922       --  Set overflow checking bit. Much cleverer code needed here eventually
8923       --  and perhaps the Resolve routines should be separated for the various
8924       --  arithmetic operations, since they will need different processing ???
8925
8926       if Nkind (N) in N_Op then
8927          if not Overflow_Checks_Suppressed (Etype (N)) then
8928             Enable_Overflow_Check (N);
8929          end if;
8930       end if;
8931
8932       --  Generate warning for expressions like -5 mod 3 for integers. No need
8933       --  to worry in the floating-point case, since parens do not affect the
8934       --  result so there is no point in giving in a warning.
8935
8936       declare
8937          Norig : constant Node_Id := Original_Node (N);
8938          Rorig : Node_Id;
8939          Val   : Uint;
8940          HB    : Uint;
8941          LB    : Uint;
8942          Lval  : Uint;
8943          Opnd  : Node_Id;
8944
8945       begin
8946          if Warn_On_Questionable_Missing_Parens
8947            and then Comes_From_Source (Norig)
8948            and then Is_Integer_Type (Typ)
8949            and then Nkind (Norig) = N_Op_Minus
8950          then
8951             Rorig := Original_Node (Right_Opnd (Norig));
8952
8953             --  We are looking for cases where the right operand is not
8954             --  parenthesized, and is a binary operator, multiply, divide, or
8955             --  mod. These are the cases where the grouping can affect results.
8956
8957             if Paren_Count (Rorig) = 0
8958               and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
8959             then
8960                --  For mod, we always give the warning, since the value is
8961                --  affected by the parenthesization (e.g. (-5) mod 315 /=
8962                --  -(5 mod 315)). But for the other cases, the only concern is
8963                --  overflow, e.g. for the case of 8 big signed (-(2 * 64)
8964                --  overflows, but (-2) * 64 does not). So we try to give the
8965                --  message only when overflow is possible.
8966
8967                if Nkind (Rorig) /= N_Op_Mod
8968                  and then Compile_Time_Known_Value (R)
8969                then
8970                   Val := Expr_Value (R);
8971
8972                   if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
8973                      HB := Expr_Value (Type_High_Bound (Typ));
8974                   else
8975                      HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
8976                   end if;
8977
8978                   if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
8979                      LB := Expr_Value (Type_Low_Bound (Typ));
8980                   else
8981                      LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
8982                   end if;
8983
8984                   --  Note that the test below is deliberately excluding the
8985                   --  largest negative number, since that is a potentially
8986                   --  troublesome case (e.g. -2 * x, where the result is the
8987                   --  largest negative integer has an overflow with 2 * x).
8988
8989                   if Val > LB and then Val <= HB then
8990                      return;
8991                   end if;
8992                end if;
8993
8994                --  For the multiplication case, the only case we have to worry
8995                --  about is when (-a)*b is exactly the largest negative number
8996                --  so that -(a*b) can cause overflow. This can only happen if
8997                --  a is a power of 2, and more generally if any operand is a
8998                --  constant that is not a power of 2, then the parentheses
8999                --  cannot affect whether overflow occurs. We only bother to
9000                --  test the left most operand
9001
9002                --  Loop looking at left operands for one that has known value
9003
9004                Opnd := Rorig;
9005                Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
9006                   if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
9007                      Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
9008
9009                      --  Operand value of 0 or 1 skips warning
9010
9011                      if Lval <= 1 then
9012                         return;
9013
9014                      --  Otherwise check power of 2, if power of 2, warn, if
9015                      --  anything else, skip warning.
9016
9017                      else
9018                         while Lval /= 2 loop
9019                            if Lval mod 2 = 1 then
9020                               return;
9021                            else
9022                               Lval := Lval / 2;
9023                            end if;
9024                         end loop;
9025
9026                         exit Opnd_Loop;
9027                      end if;
9028                   end if;
9029
9030                   --  Keep looking at left operands
9031
9032                   Opnd := Left_Opnd (Opnd);
9033                end loop Opnd_Loop;
9034
9035                --  For rem or "/" we can only have a problematic situation
9036                --  if the divisor has a value of minus one or one. Otherwise
9037                --  overflow is impossible (divisor > 1) or we have a case of
9038                --  division by zero in any case.
9039
9040                if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
9041                  and then Compile_Time_Known_Value (Right_Opnd (Rorig))
9042                  and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
9043                then
9044                   return;
9045                end if;
9046
9047                --  If we fall through warning should be issued
9048
9049                Error_Msg_N
9050                  ("?unary minus expression should be parenthesized here!", N);
9051             end if;
9052          end if;
9053       end;
9054    end Resolve_Unary_Op;
9055
9056    ----------------------------------
9057    -- Resolve_Unchecked_Expression --
9058    ----------------------------------
9059
9060    procedure Resolve_Unchecked_Expression
9061      (N   : Node_Id;
9062       Typ : Entity_Id)
9063    is
9064    begin
9065       Resolve (Expression (N), Typ, Suppress => All_Checks);
9066       Set_Etype (N, Typ);
9067    end Resolve_Unchecked_Expression;
9068
9069    ---------------------------------------
9070    -- Resolve_Unchecked_Type_Conversion --
9071    ---------------------------------------
9072
9073    procedure Resolve_Unchecked_Type_Conversion
9074      (N   : Node_Id;
9075       Typ : Entity_Id)
9076    is
9077       pragma Warnings (Off, Typ);
9078
9079       Operand   : constant Node_Id   := Expression (N);
9080       Opnd_Type : constant Entity_Id := Etype (Operand);
9081
9082    begin
9083       --  Resolve operand using its own type
9084
9085       Resolve (Operand, Opnd_Type);
9086       Eval_Unchecked_Conversion (N);
9087
9088    end Resolve_Unchecked_Type_Conversion;
9089
9090    ------------------------------
9091    -- Rewrite_Operator_As_Call --
9092    ------------------------------
9093
9094    procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
9095       Loc     : constant Source_Ptr := Sloc (N);
9096       Actuals : constant List_Id    := New_List;
9097       New_N   : Node_Id;
9098
9099    begin
9100       if Nkind (N) in  N_Binary_Op then
9101          Append (Left_Opnd (N), Actuals);
9102       end if;
9103
9104       Append (Right_Opnd (N), Actuals);
9105
9106       New_N :=
9107         Make_Function_Call (Sloc => Loc,
9108           Name => New_Occurrence_Of (Nam, Loc),
9109           Parameter_Associations => Actuals);
9110
9111       Preserve_Comes_From_Source (New_N, N);
9112       Preserve_Comes_From_Source (Name (New_N), N);
9113       Rewrite (N, New_N);
9114       Set_Etype (N, Etype (Nam));
9115    end Rewrite_Operator_As_Call;
9116
9117    ------------------------------
9118    -- Rewrite_Renamed_Operator --
9119    ------------------------------
9120
9121    procedure Rewrite_Renamed_Operator
9122      (N   : Node_Id;
9123       Op  : Entity_Id;
9124       Typ : Entity_Id)
9125    is
9126       Nam       : constant Name_Id := Chars (Op);
9127       Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
9128       Op_Node   : Node_Id;
9129
9130    begin
9131       --  Rewrite the operator node using the real operator, not its renaming.
9132       --  Exclude user-defined intrinsic operations of the same name, which are
9133       --  treated separately and rewritten as calls.
9134
9135       if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
9136          Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
9137          Set_Chars      (Op_Node, Nam);
9138          Set_Etype      (Op_Node, Etype (N));
9139          Set_Entity     (Op_Node, Op);
9140          Set_Right_Opnd (Op_Node, Right_Opnd (N));
9141
9142          --  Indicate that both the original entity and its renaming are
9143          --  referenced at this point.
9144
9145          Generate_Reference (Entity (N), N);
9146          Generate_Reference (Op, N);
9147
9148          if Is_Binary then
9149             Set_Left_Opnd  (Op_Node, Left_Opnd  (N));
9150          end if;
9151
9152          Rewrite (N, Op_Node);
9153
9154          --  If the context type is private, add the appropriate conversions
9155          --  so that the operator is applied to the full view. This is done
9156          --  in the routines that resolve intrinsic operators,
9157
9158          if Is_Intrinsic_Subprogram (Op)
9159            and then Is_Private_Type (Typ)
9160          then
9161             case Nkind (N) is
9162                when N_Op_Add   | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
9163                     N_Op_Expon | N_Op_Mod      | N_Op_Rem      =>
9164                   Resolve_Intrinsic_Operator (N, Typ);
9165
9166                when N_Op_Plus  | N_Op_Minus    | N_Op_Abs      =>
9167                   Resolve_Intrinsic_Unary_Operator (N, Typ);
9168
9169                when others =>
9170                   Resolve (N, Typ);
9171             end case;
9172          end if;
9173
9174       elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
9175
9176          --  Operator renames a user-defined operator of the same name. Use
9177          --  the original operator in the node, which is the one that Gigi
9178          --  knows about.
9179
9180          Set_Entity (N, Op);
9181          Set_Is_Overloaded (N, False);
9182       end if;
9183    end Rewrite_Renamed_Operator;
9184
9185    -----------------------
9186    -- Set_Slice_Subtype --
9187    -----------------------
9188
9189    --  Build an implicit subtype declaration to represent the type delivered
9190    --  by the slice. This is an abbreviated version of an array subtype. We
9191    --  define an index subtype for the slice, using either the subtype name
9192    --  or the discrete range of the slice. To be consistent with index usage
9193    --  elsewhere, we create a list header to hold the single index. This list
9194    --  is not otherwise attached to the syntax tree.
9195
9196    procedure Set_Slice_Subtype (N : Node_Id) is
9197       Loc           : constant Source_Ptr := Sloc (N);
9198       Index_List    : constant List_Id    := New_List;
9199       Index         : Node_Id;
9200       Index_Subtype : Entity_Id;
9201       Index_Type    : Entity_Id;
9202       Slice_Subtype : Entity_Id;
9203       Drange        : constant Node_Id := Discrete_Range (N);
9204
9205    begin
9206       if Is_Entity_Name (Drange) then
9207          Index_Subtype := Entity (Drange);
9208
9209       else
9210          --  We force the evaluation of a range. This is definitely needed in
9211          --  the renamed case, and seems safer to do unconditionally. Note in
9212          --  any case that since we will create and insert an Itype referring
9213          --  to this range, we must make sure any side effect removal actions
9214          --  are inserted before the Itype definition.
9215
9216          if Nkind (Drange) = N_Range then
9217             Force_Evaluation (Low_Bound (Drange));
9218             Force_Evaluation (High_Bound (Drange));
9219          end if;
9220
9221          Index_Type := Base_Type (Etype (Drange));
9222
9223          Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9224
9225          --  Take a new copy of Drange (where bounds have been rewritten to
9226          --  reference side-effect-vree names). Using a separate tree ensures
9227          --  that further expansion (e.g while rewriting a slice assignment
9228          --  into a FOR loop) does not attempt to remove side effects on the
9229          --  bounds again (which would cause the bounds in the index subtype
9230          --  definition to refer to temporaries before they are defined) (the
9231          --  reason is that some names are considered side effect free here
9232          --  for the subtype, but not in the context of a loop iteration
9233          --  scheme).
9234
9235          Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
9236          Set_Etype        (Index_Subtype, Index_Type);
9237          Set_Size_Info    (Index_Subtype, Index_Type);
9238          Set_RM_Size      (Index_Subtype, RM_Size (Index_Type));
9239       end if;
9240
9241       Slice_Subtype := Create_Itype (E_Array_Subtype, N);
9242
9243       Index := New_Occurrence_Of (Index_Subtype, Loc);
9244       Set_Etype (Index, Index_Subtype);
9245       Append (Index, Index_List);
9246
9247       Set_First_Index    (Slice_Subtype, Index);
9248       Set_Etype          (Slice_Subtype, Base_Type (Etype (N)));
9249       Set_Is_Constrained (Slice_Subtype, True);
9250
9251       Check_Compile_Time_Size (Slice_Subtype);
9252
9253       --  The Etype of the existing Slice node is reset to this slice subtype.
9254       --  Its bounds are obtained from its first index.
9255
9256       Set_Etype (N, Slice_Subtype);
9257
9258       --  For packed slice subtypes, freeze immediately (except in the
9259       --  case of being in a "spec expression" where we never freeze
9260       --  when we first see the expression).
9261
9262       if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
9263          Freeze_Itype (Slice_Subtype, N);
9264
9265       --  For all other cases insert an itype reference in the slice's actions
9266       --  so that the itype is frozen at the proper place in the tree (i.e. at
9267       --  the point where actions for the slice are analyzed). Note that this
9268       --  is different from freezing the itype immediately, which might be
9269       --  premature (e.g. if the slice is within a transient scope).
9270
9271       else
9272          Ensure_Defined (Typ => Slice_Subtype, N => N);
9273       end if;
9274    end Set_Slice_Subtype;
9275
9276    --------------------------------
9277    -- Set_String_Literal_Subtype --
9278    --------------------------------
9279
9280    procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
9281       Loc        : constant Source_Ptr := Sloc (N);
9282       Low_Bound  : constant Node_Id :=
9283                      Type_Low_Bound (Etype (First_Index (Typ)));
9284       Subtype_Id : Entity_Id;
9285
9286    begin
9287       if Nkind (N) /= N_String_Literal then
9288          return;
9289       end if;
9290
9291       Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
9292       Set_String_Literal_Length (Subtype_Id, UI_From_Int
9293                                                (String_Length (Strval (N))));
9294       Set_Etype          (Subtype_Id, Base_Type (Typ));
9295       Set_Is_Constrained (Subtype_Id);
9296       Set_Etype          (N, Subtype_Id);
9297
9298       if Is_OK_Static_Expression (Low_Bound) then
9299
9300       --  The low bound is set from the low bound of the corresponding
9301       --  index type. Note that we do not store the high bound in the
9302       --  string literal subtype, but it can be deduced if necessary
9303       --  from the length and the low bound.
9304
9305          Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
9306
9307       else
9308          Set_String_Literal_Low_Bound
9309            (Subtype_Id, Make_Integer_Literal (Loc, 1));
9310          Set_Etype (String_Literal_Low_Bound (Subtype_Id), Standard_Positive);
9311
9312          --  Build bona fide subtype for the string, and wrap it in an
9313          --  unchecked conversion, because the backend expects the
9314          --  String_Literal_Subtype to have a static lower bound.
9315
9316          declare
9317             Index_List    : constant List_Id    := New_List;
9318             Index_Type    : constant Entity_Id := Etype (First_Index (Typ));
9319             High_Bound    : constant Node_Id :=
9320                                Make_Op_Add (Loc,
9321                                   Left_Opnd => New_Copy_Tree (Low_Bound),
9322                                   Right_Opnd =>
9323                                     Make_Integer_Literal (Loc,
9324                                       String_Length (Strval (N)) - 1));
9325             Array_Subtype : Entity_Id;
9326             Index_Subtype : Entity_Id;
9327             Drange        : Node_Id;
9328             Index         : Node_Id;
9329
9330          begin
9331             Index_Subtype :=
9332               Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
9333             Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
9334             Set_Scalar_Range (Index_Subtype, Drange);
9335             Set_Parent (Drange, N);
9336             Analyze_And_Resolve (Drange, Index_Type);
9337
9338             --  In the context, the Index_Type may already have a constraint,
9339             --  so use common base type on string subtype. The base type may
9340             --  be used when generating attributes of the string, for example
9341             --  in the context of a slice assignment.
9342
9343             Set_Etype        (Index_Subtype, Base_Type (Index_Type));
9344             Set_Size_Info    (Index_Subtype, Index_Type);
9345             Set_RM_Size      (Index_Subtype, RM_Size (Index_Type));
9346
9347             Array_Subtype := Create_Itype (E_Array_Subtype, N);
9348
9349             Index := New_Occurrence_Of (Index_Subtype, Loc);
9350             Set_Etype (Index, Index_Subtype);
9351             Append (Index, Index_List);
9352
9353             Set_First_Index    (Array_Subtype, Index);
9354             Set_Etype          (Array_Subtype, Base_Type (Typ));
9355             Set_Is_Constrained (Array_Subtype, True);
9356
9357             Rewrite (N,
9358               Make_Unchecked_Type_Conversion (Loc,
9359                 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
9360                 Expression => Relocate_Node (N)));
9361             Set_Etype (N, Array_Subtype);
9362          end;
9363       end if;
9364    end Set_String_Literal_Subtype;
9365
9366    ------------------------------
9367    -- Simplify_Type_Conversion --
9368    ------------------------------
9369
9370    procedure Simplify_Type_Conversion (N : Node_Id) is
9371    begin
9372       if Nkind (N) = N_Type_Conversion then
9373          declare
9374             Operand    : constant Node_Id   := Expression (N);
9375             Target_Typ : constant Entity_Id := Etype (N);
9376             Opnd_Typ   : constant Entity_Id := Etype (Operand);
9377
9378          begin
9379             if Is_Floating_Point_Type (Opnd_Typ)
9380               and then
9381                 (Is_Integer_Type (Target_Typ)
9382                    or else (Is_Fixed_Point_Type (Target_Typ)
9383                               and then Conversion_OK (N)))
9384               and then Nkind (Operand) = N_Attribute_Reference
9385               and then Attribute_Name (Operand) = Name_Truncation
9386
9387             --  Special processing required if the conversion is the expression
9388             --  of a Truncation attribute reference. In this case we replace:
9389
9390             --     ityp (ftyp'Truncation (x))
9391
9392             --  by
9393
9394             --     ityp (x)
9395
9396             --  with the Float_Truncate flag set, which is more efficient
9397
9398             then
9399                Rewrite (Operand,
9400                  Relocate_Node (First (Expressions (Operand))));
9401                Set_Float_Truncate (N, True);
9402             end if;
9403          end;
9404       end if;
9405    end Simplify_Type_Conversion;
9406
9407    -----------------------------
9408    -- Unique_Fixed_Point_Type --
9409    -----------------------------
9410
9411    function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
9412       T1   : Entity_Id := Empty;
9413       T2   : Entity_Id;
9414       Item : Node_Id;
9415       Scop : Entity_Id;
9416
9417       procedure Fixed_Point_Error;
9418       --  Give error messages for true ambiguity. Messages are posted on node
9419       --  N, and entities T1, T2 are the possible interpretations.
9420
9421       -----------------------
9422       -- Fixed_Point_Error --
9423       -----------------------
9424
9425       procedure Fixed_Point_Error is
9426       begin
9427          Error_Msg_N ("ambiguous universal_fixed_expression", N);
9428          Error_Msg_NE ("\\possible interpretation as}", N, T1);
9429          Error_Msg_NE ("\\possible interpretation as}", N, T2);
9430       end Fixed_Point_Error;
9431
9432    --  Start of processing for Unique_Fixed_Point_Type
9433
9434    begin
9435       --  The operations on Duration are visible, so Duration is always a
9436       --  possible interpretation.
9437
9438       T1 := Standard_Duration;
9439
9440       --  Look for fixed-point types in enclosing scopes
9441
9442       Scop := Current_Scope;
9443       while Scop /= Standard_Standard loop
9444          T2 := First_Entity (Scop);
9445          while Present (T2) loop
9446             if Is_Fixed_Point_Type (T2)
9447               and then Current_Entity (T2) = T2
9448               and then Scope (Base_Type (T2)) = Scop
9449             then
9450                if Present (T1) then
9451                   Fixed_Point_Error;
9452                   return Any_Type;
9453                else
9454                   T1 := T2;
9455                end if;
9456             end if;
9457
9458             Next_Entity (T2);
9459          end loop;
9460
9461          Scop := Scope (Scop);
9462       end loop;
9463
9464       --  Look for visible fixed type declarations in the context
9465
9466       Item := First (Context_Items (Cunit (Current_Sem_Unit)));
9467       while Present (Item) loop
9468          if Nkind (Item) = N_With_Clause then
9469             Scop := Entity (Name (Item));
9470             T2 := First_Entity (Scop);
9471             while Present (T2) loop
9472                if Is_Fixed_Point_Type (T2)
9473                  and then Scope (Base_Type (T2)) = Scop
9474                  and then (Is_Potentially_Use_Visible (T2)
9475                              or else In_Use (T2))
9476                then
9477                   if Present (T1) then
9478                      Fixed_Point_Error;
9479                      return Any_Type;
9480                   else
9481                      T1 := T2;
9482                   end if;
9483                end if;
9484
9485                Next_Entity (T2);
9486             end loop;
9487          end if;
9488
9489          Next (Item);
9490       end loop;
9491
9492       if Nkind (N) = N_Real_Literal then
9493          Error_Msg_NE ("?real literal interpreted as }!", N, T1);
9494       else
9495          Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
9496       end if;
9497
9498       return T1;
9499    end Unique_Fixed_Point_Type;
9500
9501    ----------------------
9502    -- Valid_Conversion --
9503    ----------------------
9504
9505    function Valid_Conversion
9506      (N       : Node_Id;
9507       Target  : Entity_Id;
9508       Operand : Node_Id) return Boolean
9509    is
9510       Target_Type : constant Entity_Id := Base_Type (Target);
9511       Opnd_Type   : Entity_Id := Etype (Operand);
9512
9513       function Conversion_Check
9514         (Valid : Boolean;
9515          Msg   : String) return Boolean;
9516       --  Little routine to post Msg if Valid is False, returns Valid value
9517
9518       function Valid_Tagged_Conversion
9519         (Target_Type : Entity_Id;
9520          Opnd_Type   : Entity_Id) return Boolean;
9521       --  Specifically test for validity of tagged conversions
9522
9523       function Valid_Array_Conversion return Boolean;
9524       --  Check index and component conformance, and accessibility levels
9525       --  if the component types are anonymous access types (Ada 2005)
9526
9527       ----------------------
9528       -- Conversion_Check --
9529       ----------------------
9530
9531       function Conversion_Check
9532         (Valid : Boolean;
9533          Msg   : String) return Boolean
9534       is
9535       begin
9536          if not Valid then
9537             Error_Msg_N (Msg, Operand);
9538          end if;
9539
9540          return Valid;
9541       end Conversion_Check;
9542
9543       ----------------------------
9544       -- Valid_Array_Conversion --
9545       ----------------------------
9546
9547       function Valid_Array_Conversion return Boolean
9548       is
9549          Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
9550          Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
9551
9552          Opnd_Index      : Node_Id;
9553          Opnd_Index_Type : Entity_Id;
9554
9555          Target_Comp_Type : constant Entity_Id :=
9556                               Component_Type (Target_Type);
9557          Target_Comp_Base : constant Entity_Id :=
9558                               Base_Type (Target_Comp_Type);
9559
9560          Target_Index      : Node_Id;
9561          Target_Index_Type : Entity_Id;
9562
9563       begin
9564          --  Error if wrong number of dimensions
9565
9566          if
9567            Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
9568          then
9569             Error_Msg_N
9570               ("incompatible number of dimensions for conversion", Operand);
9571             return False;
9572
9573          --  Number of dimensions matches
9574
9575          else
9576             --  Loop through indexes of the two arrays
9577
9578             Target_Index := First_Index (Target_Type);
9579             Opnd_Index   := First_Index (Opnd_Type);
9580             while Present (Target_Index) and then Present (Opnd_Index) loop
9581                Target_Index_Type := Etype (Target_Index);
9582                Opnd_Index_Type   := Etype (Opnd_Index);
9583
9584                --  Error if index types are incompatible
9585
9586                if not (Is_Integer_Type (Target_Index_Type)
9587                        and then Is_Integer_Type (Opnd_Index_Type))
9588                  and then (Root_Type (Target_Index_Type)
9589                            /= Root_Type (Opnd_Index_Type))
9590                then
9591                   Error_Msg_N
9592                     ("incompatible index types for array conversion",
9593                      Operand);
9594                   return False;
9595                end if;
9596
9597                Next_Index (Target_Index);
9598                Next_Index (Opnd_Index);
9599             end loop;
9600
9601             --  If component types have same base type, all set
9602
9603             if Target_Comp_Base  = Opnd_Comp_Base then
9604                null;
9605
9606                --  Here if base types of components are not the same. The only
9607                --  time this is allowed is if we have anonymous access types.
9608
9609                --  The conversion of arrays of anonymous access types can lead
9610                --  to dangling pointers. AI-392 formalizes the accessibility
9611                --  checks that must be applied to such conversions to prevent
9612                --  out-of-scope references.
9613
9614             elsif
9615               Ekind_In (Target_Comp_Base, E_Anonymous_Access_Type,
9616                                           E_Anonymous_Access_Subprogram_Type)
9617               and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
9618               and then
9619                 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
9620             then
9621                if Type_Access_Level (Target_Type) <
9622                    Type_Access_Level (Opnd_Type)
9623                then
9624                   if In_Instance_Body then
9625                      Error_Msg_N ("?source array type " &
9626                        "has deeper accessibility level than target", Operand);
9627                      Error_Msg_N ("\?Program_Error will be raised at run time",
9628                          Operand);
9629                      Rewrite (N,
9630                        Make_Raise_Program_Error (Sloc (N),
9631                          Reason => PE_Accessibility_Check_Failed));
9632                      Set_Etype (N, Target_Type);
9633                      return False;
9634
9635                   --  Conversion not allowed because of accessibility levels
9636
9637                   else
9638                      Error_Msg_N ("source array type " &
9639                        "has deeper accessibility level than target", Operand);
9640                      return False;
9641                   end if;
9642                else
9643                   null;
9644                end if;
9645
9646             --  All other cases where component base types do not match
9647
9648             else
9649                Error_Msg_N
9650                  ("incompatible component types for array conversion",
9651                   Operand);
9652                return False;
9653             end if;
9654
9655             --  Check that component subtypes statically match. For numeric
9656             --  types this means that both must be either constrained or
9657             --  unconstrained. For enumeration types the bounds must match.
9658             --  All of this is checked in Subtypes_Statically_Match.
9659
9660             if not Subtypes_Statically_Match
9661                             (Target_Comp_Type, Opnd_Comp_Type)
9662             then
9663                Error_Msg_N
9664                  ("component subtypes must statically match", Operand);
9665                return False;
9666             end if;
9667          end if;
9668
9669          return True;
9670       end Valid_Array_Conversion;
9671
9672       -----------------------------
9673       -- Valid_Tagged_Conversion --
9674       -----------------------------
9675
9676       function Valid_Tagged_Conversion
9677         (Target_Type : Entity_Id;
9678          Opnd_Type   : Entity_Id) return Boolean
9679       is
9680       begin
9681          --  Upward conversions are allowed (RM 4.6(22))
9682
9683          if Covers (Target_Type, Opnd_Type)
9684            or else Is_Ancestor (Target_Type, Opnd_Type)
9685          then
9686             return True;
9687
9688          --  Downward conversion are allowed if the operand is class-wide
9689          --  (RM 4.6(23)).
9690
9691          elsif Is_Class_Wide_Type (Opnd_Type)
9692            and then Covers (Opnd_Type, Target_Type)
9693          then
9694             return True;
9695
9696          elsif Covers (Opnd_Type, Target_Type)
9697            or else Is_Ancestor (Opnd_Type, Target_Type)
9698          then
9699             return
9700               Conversion_Check (False,
9701                 "downward conversion of tagged objects not allowed");
9702
9703          --  Ada 2005 (AI-251): The conversion to/from interface types is
9704          --  always valid
9705
9706          elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
9707             return True;
9708
9709          --  If the operand is a class-wide type obtained through a limited_
9710          --  with clause, and the context includes the non-limited view, use
9711          --  it to determine whether the conversion is legal.
9712
9713          elsif Is_Class_Wide_Type (Opnd_Type)
9714            and then From_With_Type (Opnd_Type)
9715            and then Present (Non_Limited_View (Etype (Opnd_Type)))
9716            and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
9717          then
9718             return True;
9719
9720          elsif Is_Access_Type (Opnd_Type)
9721            and then Is_Interface (Directly_Designated_Type (Opnd_Type))
9722          then
9723             return True;
9724
9725          else
9726             Error_Msg_NE
9727               ("invalid tagged conversion, not compatible with}",
9728                N, First_Subtype (Opnd_Type));
9729             return False;
9730          end if;
9731       end Valid_Tagged_Conversion;
9732
9733    --  Start of processing for Valid_Conversion
9734
9735    begin
9736       Check_Parameterless_Call (Operand);
9737
9738       if Is_Overloaded (Operand) then
9739          declare
9740             I   : Interp_Index;
9741             I1  : Interp_Index;
9742             It  : Interp;
9743             It1 : Interp;
9744             N1  : Entity_Id;
9745             T1  : Entity_Id;
9746
9747          begin
9748             --  Remove procedure calls, which syntactically cannot appear in
9749             --  this context, but which cannot be removed by type checking,
9750             --  because the context does not impose a type.
9751
9752             --  When compiling for VMS, spurious ambiguities can be produced
9753             --  when arithmetic operations have a literal operand and return
9754             --  System.Address or a descendant of it. These ambiguities are
9755             --  otherwise resolved by the context, but for conversions there
9756             --  is no context type and the removal of the spurious operations
9757             --  must be done explicitly here.
9758
9759             --  The node may be labelled overloaded, but still contain only
9760             --  one interpretation because others were discarded in previous
9761             --  filters. If this is the case, retain the single interpretation
9762             --  if legal.
9763
9764             Get_First_Interp (Operand, I, It);
9765             Opnd_Type := It.Typ;
9766             Get_Next_Interp (I, It);
9767
9768             if Present (It.Typ)
9769               and then Opnd_Type /= Standard_Void_Type
9770             then
9771                --  More than one candidate interpretation is available
9772
9773                Get_First_Interp (Operand, I, It);
9774                while Present (It.Typ) loop
9775                   if It.Typ = Standard_Void_Type then
9776                      Remove_Interp (I);
9777                   end if;
9778
9779                   if Present (System_Aux_Id)
9780                     and then Is_Descendent_Of_Address (It.Typ)
9781                   then
9782                      Remove_Interp (I);
9783                   end if;
9784
9785                   Get_Next_Interp (I, It);
9786                end loop;
9787             end if;
9788
9789             Get_First_Interp (Operand, I, It);
9790             I1  := I;
9791             It1 := It;
9792
9793             if No (It.Typ) then
9794                Error_Msg_N ("illegal operand in conversion", Operand);
9795                return False;
9796             end if;
9797
9798             Get_Next_Interp (I, It);
9799
9800             if Present (It.Typ) then
9801                N1  := It1.Nam;
9802                T1  := It1.Typ;
9803                It1 :=  Disambiguate (Operand, I1, I, Any_Type);
9804
9805                if It1 = No_Interp then
9806                   Error_Msg_N ("ambiguous operand in conversion", Operand);
9807
9808                   --  If the interpretation involves a standard operator, use
9809                   --  the location of the type, which may be user-defined.
9810
9811                   if Sloc (It.Nam) = Standard_Location then
9812                      Error_Msg_Sloc := Sloc (It.Typ);
9813                   else
9814                      Error_Msg_Sloc := Sloc (It.Nam);
9815                   end if;
9816
9817                   Error_Msg_N -- CODEFIX
9818                     ("\\possible interpretation#!", Operand);
9819
9820                   if Sloc (N1) = Standard_Location then
9821                      Error_Msg_Sloc := Sloc (T1);
9822                   else
9823                      Error_Msg_Sloc := Sloc (N1);
9824                   end if;
9825
9826                   Error_Msg_N -- CODEFIX
9827                     ("\\possible interpretation#!", Operand);
9828
9829                   return False;
9830                end if;
9831             end if;
9832
9833             Set_Etype (Operand, It1.Typ);
9834             Opnd_Type := It1.Typ;
9835          end;
9836       end if;
9837
9838       --  Numeric types
9839
9840       if Is_Numeric_Type (Target_Type)  then
9841
9842          --  A universal fixed expression can be converted to any numeric type
9843
9844          if Opnd_Type = Universal_Fixed then
9845             return True;
9846
9847          --  Also no need to check when in an instance or inlined body, because
9848          --  the legality has been established when the template was analyzed.
9849          --  Furthermore, numeric conversions may occur where only a private
9850          --  view of the operand type is visible at the instantiation point.
9851          --  This results in a spurious error if we check that the operand type
9852          --  is a numeric type.
9853
9854          --  Note: in a previous version of this unit, the following tests were
9855          --  applied only for generated code (Comes_From_Source set to False),
9856          --  but in fact the test is required for source code as well, since
9857          --  this situation can arise in source code.
9858
9859          elsif In_Instance or else In_Inlined_Body then
9860                return True;
9861
9862          --  Otherwise we need the conversion check
9863
9864          else
9865             return Conversion_Check
9866                     (Is_Numeric_Type (Opnd_Type),
9867                      "illegal operand for numeric conversion");
9868          end if;
9869
9870       --  Array types
9871
9872       elsif Is_Array_Type (Target_Type) then
9873          if not Is_Array_Type (Opnd_Type)
9874            or else Opnd_Type = Any_Composite
9875            or else Opnd_Type = Any_String
9876          then
9877             Error_Msg_N
9878               ("illegal operand for array conversion", Operand);
9879             return False;
9880          else
9881             return Valid_Array_Conversion;
9882          end if;
9883
9884       --  Ada 2005 (AI-251): Anonymous access types where target references an
9885       --  interface type.
9886
9887       elsif Ekind_In (Target_Type, E_General_Access_Type,
9888                                    E_Anonymous_Access_Type)
9889         and then Is_Interface (Directly_Designated_Type (Target_Type))
9890       then
9891          --  Check the static accessibility rule of 4.6(17). Note that the
9892          --  check is not enforced when within an instance body, since the
9893          --  RM requires such cases to be caught at run time.
9894
9895          if Ekind (Target_Type) /= E_Anonymous_Access_Type then
9896             if Type_Access_Level (Opnd_Type) >
9897                Type_Access_Level (Target_Type)
9898             then
9899                --  In an instance, this is a run-time check, but one we know
9900                --  will fail, so generate an appropriate warning. The raise
9901                --  will be generated by Expand_N_Type_Conversion.
9902
9903                if In_Instance_Body then
9904                   Error_Msg_N
9905                     ("?cannot convert local pointer to non-local access type",
9906                      Operand);
9907                   Error_Msg_N
9908                     ("\?Program_Error will be raised at run time", Operand);
9909                else
9910                   Error_Msg_N
9911                     ("cannot convert local pointer to non-local access type",
9912                      Operand);
9913                   return False;
9914                end if;
9915
9916             --  Special accessibility checks are needed in the case of access
9917             --  discriminants declared for a limited type.
9918
9919             elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
9920               and then not Is_Local_Anonymous_Access (Opnd_Type)
9921             then
9922                --  When the operand is a selected access discriminant the check
9923                --  needs to be made against the level of the object denoted by
9924                --  the prefix of the selected name (Object_Access_Level handles
9925                --  checking the prefix of the operand for this case).
9926
9927                if Nkind (Operand) = N_Selected_Component
9928                  and then Object_Access_Level (Operand) >
9929                           Type_Access_Level (Target_Type)
9930                then
9931                   --  In an instance, this is a run-time check, but one we know
9932                   --  will fail, so generate an appropriate warning. The raise
9933                   --  will be generated by Expand_N_Type_Conversion.
9934
9935                   if In_Instance_Body then
9936                      Error_Msg_N
9937                        ("?cannot convert access discriminant to non-local" &
9938                         " access type", Operand);
9939                      Error_Msg_N
9940                        ("\?Program_Error will be raised at run time", Operand);
9941                   else
9942                      Error_Msg_N
9943                        ("cannot convert access discriminant to non-local" &
9944                         " access type", Operand);
9945                      return False;
9946                   end if;
9947                end if;
9948
9949                --  The case of a reference to an access discriminant from
9950                --  within a limited type declaration (which will appear as
9951                --  a discriminal) is always illegal because the level of the
9952                --  discriminant is considered to be deeper than any (nameable)
9953                --  access type.
9954
9955                if Is_Entity_Name (Operand)
9956                  and then not Is_Local_Anonymous_Access (Opnd_Type)
9957                  and then
9958                    Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
9959                  and then Present (Discriminal_Link (Entity (Operand)))
9960                then
9961                   Error_Msg_N
9962                     ("discriminant has deeper accessibility level than target",
9963                      Operand);
9964                   return False;
9965                end if;
9966             end if;
9967          end if;
9968
9969          return True;
9970
9971       --  General and anonymous access types
9972
9973       elsif Ekind_In (Target_Type, E_General_Access_Type,
9974                                    E_Anonymous_Access_Type)
9975           and then
9976             Conversion_Check
9977               (Is_Access_Type (Opnd_Type)
9978                 and then not
9979                   Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
9980                                        E_Access_Protected_Subprogram_Type),
9981                "must be an access-to-object type")
9982       then
9983          if Is_Access_Constant (Opnd_Type)
9984            and then not Is_Access_Constant (Target_Type)
9985          then
9986             Error_Msg_N
9987               ("access-to-constant operand type not allowed", Operand);
9988             return False;
9989          end if;
9990
9991          --  Check the static accessibility rule of 4.6(17). Note that the
9992          --  check is not enforced when within an instance body, since the RM
9993          --  requires such cases to be caught at run time.
9994
9995          if Ekind (Target_Type) /= E_Anonymous_Access_Type
9996            or else Is_Local_Anonymous_Access (Target_Type)
9997          then
9998             if Type_Access_Level (Opnd_Type)
9999               > Type_Access_Level (Target_Type)
10000             then
10001                --  In an instance, this is a run-time check, but one we know
10002                --  will fail, so generate an appropriate warning. The raise
10003                --  will be generated by Expand_N_Type_Conversion.
10004
10005                if In_Instance_Body then
10006                   Error_Msg_N
10007                     ("?cannot convert local pointer to non-local access type",
10008                      Operand);
10009                   Error_Msg_N
10010                     ("\?Program_Error will be raised at run time", Operand);
10011
10012                else
10013                   --  Avoid generation of spurious error message
10014
10015                   if not Error_Posted (N) then
10016                      Error_Msg_N
10017                       ("cannot convert local pointer to non-local access type",
10018                        Operand);
10019                   end if;
10020
10021                   return False;
10022                end if;
10023
10024             --  Special accessibility checks are needed in the case of access
10025             --  discriminants declared for a limited type.
10026
10027             elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
10028               and then not Is_Local_Anonymous_Access (Opnd_Type)
10029             then
10030                --  When the operand is a selected access discriminant the check
10031                --  needs to be made against the level of the object denoted by
10032                --  the prefix of the selected name (Object_Access_Level handles
10033                --  checking the prefix of the operand for this case).
10034
10035                if Nkind (Operand) = N_Selected_Component
10036                  and then Object_Access_Level (Operand) >
10037                           Type_Access_Level (Target_Type)
10038                then
10039                   --  In an instance, this is a run-time check, but one we know
10040                   --  will fail, so generate an appropriate warning. The raise
10041                   --  will be generated by Expand_N_Type_Conversion.
10042
10043                   if In_Instance_Body then
10044                      Error_Msg_N
10045                        ("?cannot convert access discriminant to non-local" &
10046                         " access type", Operand);
10047                      Error_Msg_N
10048                        ("\?Program_Error will be raised at run time",
10049                         Operand);
10050
10051                   else
10052                      Error_Msg_N
10053                        ("cannot convert access discriminant to non-local" &
10054                         " access type", Operand);
10055                      return False;
10056                   end if;
10057                end if;
10058
10059                --  The case of a reference to an access discriminant from
10060                --  within a limited type declaration (which will appear as
10061                --  a discriminal) is always illegal because the level of the
10062                --  discriminant is considered to be deeper than any (nameable)
10063                --  access type.
10064
10065                if Is_Entity_Name (Operand)
10066                  and then
10067                    Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
10068                  and then Present (Discriminal_Link (Entity (Operand)))
10069                then
10070                   Error_Msg_N
10071                     ("discriminant has deeper accessibility level than target",
10072                      Operand);
10073                   return False;
10074                end if;
10075             end if;
10076          end if;
10077
10078          --  In the presence of limited_with clauses we have to use non-limited
10079          --  views, if available.
10080
10081          Check_Limited : declare
10082             function Full_Designated_Type (T : Entity_Id) return Entity_Id;
10083             --  Helper function to handle limited views
10084
10085             --------------------------
10086             -- Full_Designated_Type --
10087             --------------------------
10088
10089             function Full_Designated_Type (T : Entity_Id) return Entity_Id is
10090                Desig : constant Entity_Id := Designated_Type (T);
10091
10092             begin
10093                --  Handle the limited view of a type
10094
10095                if Is_Incomplete_Type (Desig)
10096                  and then From_With_Type (Desig)
10097                  and then Present (Non_Limited_View (Desig))
10098                then
10099                   return Available_View (Desig);
10100                else
10101                   return Desig;
10102                end if;
10103             end Full_Designated_Type;
10104
10105             --  Local Declarations
10106
10107             Target : constant Entity_Id := Full_Designated_Type (Target_Type);
10108             Opnd   : constant Entity_Id := Full_Designated_Type (Opnd_Type);
10109
10110             Same_Base : constant Boolean :=
10111                           Base_Type (Target) = Base_Type (Opnd);
10112
10113          --  Start of processing for Check_Limited
10114
10115          begin
10116             if Is_Tagged_Type (Target) then
10117                return Valid_Tagged_Conversion (Target, Opnd);
10118
10119             else
10120                if not Same_Base then
10121                   Error_Msg_NE
10122                     ("target designated type not compatible with }",
10123                      N, Base_Type (Opnd));
10124                   return False;
10125
10126                --  Ada 2005 AI-384: legality rule is symmetric in both
10127                --  designated types. The conversion is legal (with possible
10128                --  constraint check) if either designated type is
10129                --  unconstrained.
10130
10131                elsif Subtypes_Statically_Match (Target, Opnd)
10132                  or else
10133                    (Has_Discriminants (Target)
10134                      and then
10135                       (not Is_Constrained (Opnd)
10136                         or else not Is_Constrained (Target)))
10137                then
10138                   --  Special case, if Value_Size has been used to make the
10139                   --  sizes different, the conversion is not allowed even
10140                   --  though the subtypes statically match.
10141
10142                   if Known_Static_RM_Size (Target)
10143                     and then Known_Static_RM_Size (Opnd)
10144                     and then RM_Size (Target) /= RM_Size (Opnd)
10145                   then
10146                      Error_Msg_NE
10147                        ("target designated subtype not compatible with }",
10148                         N, Opnd);
10149                      Error_Msg_NE
10150                        ("\because sizes of the two designated subtypes differ",
10151                         N, Opnd);
10152                      return False;
10153
10154                   --  Normal case where conversion is allowed
10155
10156                   else
10157                      return True;
10158                   end if;
10159
10160                else
10161                   Error_Msg_NE
10162                     ("target designated subtype not compatible with }",
10163                      N, Opnd);
10164                   return False;
10165                end if;
10166             end if;
10167          end Check_Limited;
10168
10169       --  Access to subprogram types. If the operand is an access parameter,
10170       --  the type has a deeper accessibility that any master, and cannot
10171       --  be assigned. We must make an exception if the conversion is part
10172       --  of an assignment and the target is the return object of an extended
10173       --  return statement, because in that case the accessibility check
10174       --  takes place after the return.
10175
10176       elsif Is_Access_Subprogram_Type (Target_Type)
10177         and then No (Corresponding_Remote_Type (Opnd_Type))
10178       then
10179          if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
10180            and then Is_Entity_Name (Operand)
10181            and then Ekind (Entity (Operand)) = E_In_Parameter
10182            and then
10183              (Nkind (Parent (N)) /= N_Assignment_Statement
10184                or else not Is_Entity_Name (Name (Parent (N)))
10185                or else not Is_Return_Object (Entity (Name (Parent (N)))))
10186          then
10187             Error_Msg_N
10188               ("illegal attempt to store anonymous access to subprogram",
10189                Operand);
10190             Error_Msg_N
10191               ("\value has deeper accessibility than any master " &
10192                "(RM 3.10.2 (13))",
10193                Operand);
10194
10195             Error_Msg_NE
10196              ("\use named access type for& instead of access parameter",
10197                Operand, Entity (Operand));
10198          end if;
10199
10200          --  Check that the designated types are subtype conformant
10201
10202          Check_Subtype_Conformant (New_Id  => Designated_Type (Target_Type),
10203                                    Old_Id  => Designated_Type (Opnd_Type),
10204                                    Err_Loc => N);
10205
10206          --  Check the static accessibility rule of 4.6(20)
10207
10208          if Type_Access_Level (Opnd_Type) >
10209             Type_Access_Level (Target_Type)
10210          then
10211             Error_Msg_N
10212               ("operand type has deeper accessibility level than target",
10213                Operand);
10214
10215          --  Check that if the operand type is declared in a generic body,
10216          --  then the target type must be declared within that same body
10217          --  (enforces last sentence of 4.6(20)).
10218
10219          elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
10220             declare
10221                O_Gen : constant Node_Id :=
10222                          Enclosing_Generic_Body (Opnd_Type);
10223
10224                T_Gen : Node_Id;
10225
10226             begin
10227                T_Gen := Enclosing_Generic_Body (Target_Type);
10228                while Present (T_Gen) and then T_Gen /= O_Gen loop
10229                   T_Gen := Enclosing_Generic_Body (T_Gen);
10230                end loop;
10231
10232                if T_Gen /= O_Gen then
10233                   Error_Msg_N
10234                     ("target type must be declared in same generic body"
10235                      & " as operand type", N);
10236                end if;
10237             end;
10238          end if;
10239
10240          return True;
10241
10242       --  Remote subprogram access types
10243
10244       elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
10245         and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
10246       then
10247          --  It is valid to convert from one RAS type to another provided
10248          --  that their specification statically match.
10249
10250          Check_Subtype_Conformant
10251            (New_Id  =>
10252               Designated_Type (Corresponding_Remote_Type (Target_Type)),
10253             Old_Id  =>
10254               Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
10255             Err_Loc =>
10256               N);
10257          return True;
10258
10259       --  If both are tagged types, check legality of view conversions
10260
10261       elsif Is_Tagged_Type (Target_Type)
10262         and then Is_Tagged_Type (Opnd_Type)
10263       then
10264          return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
10265
10266       --  Types derived from the same root type are convertible
10267
10268       elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
10269          return True;
10270
10271       --  In an instance or an inlined body, there may be inconsistent
10272       --  views of the same type, or of types derived from a common root.
10273
10274       elsif (In_Instance or In_Inlined_Body)
10275         and then
10276           Root_Type (Underlying_Type (Target_Type)) =
10277           Root_Type (Underlying_Type (Opnd_Type))
10278       then
10279          return True;
10280
10281       --  Special check for common access type error case
10282
10283       elsif Ekind (Target_Type) = E_Access_Type
10284          and then Is_Access_Type (Opnd_Type)
10285       then
10286          Error_Msg_N ("target type must be general access type!", N);
10287          Error_Msg_NE -- CODEFIX
10288             ("add ALL to }!", N, Target_Type);
10289          return False;
10290
10291       else
10292          Error_Msg_NE ("invalid conversion, not compatible with }",
10293            N, Opnd_Type);
10294          return False;
10295       end if;
10296    end Valid_Conversion;
10297
10298 end Sem_Res;