OSDN Git Service

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