OSDN Git Service

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