OSDN Git Service

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