OSDN Git Service

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