OSDN Git Service

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