OSDN Git Service

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