OSDN Git Service

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