OSDN Git Service

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