OSDN Git Service

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