OSDN Git Service

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