OSDN Git Service

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