OSDN Git Service

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