OSDN Git Service

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