OSDN Git Service

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