OSDN Git Service

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