OSDN Git Service

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