OSDN Git Service

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