OSDN Git Service

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