OSDN Git Service

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