OSDN Git Service

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