1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Eval_Fat; use Eval_Fat;
33 with Exp_Util; use Exp_Util;
35 with Namet; use Namet;
36 with Nmake; use Nmake;
37 with Nlists; use Nlists;
40 with Sem_Cat; use Sem_Cat;
41 with Sem_Ch6; use Sem_Ch6;
42 with Sem_Ch8; use Sem_Ch8;
43 with Sem_Res; use Sem_Res;
44 with Sem_Util; use Sem_Util;
45 with Sem_Type; use Sem_Type;
46 with Sem_Warn; use Sem_Warn;
47 with Sinfo; use Sinfo;
48 with Snames; use Snames;
49 with Stand; use Stand;
50 with Stringt; use Stringt;
51 with Tbuild; use Tbuild;
53 package body Sem_Eval is
55 -----------------------------------------
56 -- Handling of Compile Time Evaluation --
57 -----------------------------------------
59 -- The compile time evaluation of expressions is distributed over several
60 -- Eval_xxx procedures. These procedures are called immediately after
61 -- a subexpression is resolved and is therefore accomplished in a bottom
62 -- up fashion. The flags are synthesized using the following approach.
64 -- Is_Static_Expression is determined by following the detailed rules
65 -- in RM 4.9(4-14). This involves testing the Is_Static_Expression
66 -- flag of the operands in many cases.
68 -- Raises_Constraint_Error is set if any of the operands have the flag
69 -- set or if an attempt to compute the value of the current expression
70 -- results in detection of a runtime constraint error.
72 -- As described in the spec, the requirement is that Is_Static_Expression
73 -- be accurately set, and in addition for nodes for which this flag is set,
74 -- Raises_Constraint_Error must also be set. Furthermore a node which has
75 -- Is_Static_Expression set, and Raises_Constraint_Error clear, then the
76 -- requirement is that the expression value must be precomputed, and the
77 -- node is either a literal, or the name of a constant entity whose value
78 -- is a static expression.
80 -- The general approach is as follows. First compute Is_Static_Expression.
81 -- If the node is not static, then the flag is left off in the node and
82 -- we are all done. Otherwise for a static node, we test if any of the
83 -- operands will raise constraint error, and if so, propagate the flag
84 -- Raises_Constraint_Error to the result node and we are done (since the
85 -- error was already posted at a lower level).
87 -- For the case of a static node whose operands do not raise constraint
88 -- error, we attempt to evaluate the node. If this evaluation succeeds,
89 -- then the node is replaced by the result of this computation. If the
90 -- evaluation raises constraint error, then we rewrite the node with
91 -- Apply_Compile_Time_Constraint_Error to raise the exception and also
92 -- to post appropriate error messages.
98 type Bits is array (Nat range <>) of Boolean;
99 -- Used to convert unsigned (modular) values for folding logical ops
101 -- The following definitions are used to maintain a cache of nodes that
102 -- have compile time known values. The cache is maintained only for
103 -- discrete types (the most common case), and is populated by calls to
104 -- Compile_Time_Known_Value and Expr_Value, but only used by Expr_Value
105 -- since it is possible for the status to change (in particular it is
106 -- possible for a node to get replaced by a constraint error node).
108 CV_Bits : constant := 5;
109 -- Number of low order bits of Node_Id value used to reference entries
110 -- in the cache table.
112 CV_Cache_Size : constant Nat := 2 ** CV_Bits;
113 -- Size of cache for compile time values
115 subtype CV_Range is Nat range 0 .. CV_Cache_Size;
117 type CV_Entry is record
122 type CV_Cache_Array is array (CV_Range) of CV_Entry;
124 CV_Cache : CV_Cache_Array := (others => (Node_High_Bound, Uint_0));
125 -- This is the actual cache, with entries consisting of node/value pairs,
126 -- and the impossible value Node_High_Bound used for unset entries.
128 -----------------------
129 -- Local Subprograms --
130 -----------------------
132 function From_Bits (B : Bits; T : Entity_Id) return Uint;
133 -- Converts a bit string of length B'Length to a Uint value to be used
134 -- for a target of type T, which is a modular type. This procedure
135 -- includes the necessary reduction by the modulus in the case of a
136 -- non-binary modulus (for a binary modulus, the bit string is the
137 -- right length any way so all is well).
139 function Get_String_Val (N : Node_Id) return Node_Id;
140 -- Given a tree node for a folded string or character value, returns
141 -- the corresponding string literal or character literal (one of the
142 -- two must be available, or the operand would not have been marked
143 -- as foldable in the earlier analysis of the operation).
145 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean;
146 -- Bits represents the number of bits in an integer value to be computed
147 -- (but the value has not been computed yet). If this value in Bits is
148 -- reasonable, a result of True is returned, with the implication that
149 -- the caller should go ahead and complete the calculation. If the value
150 -- in Bits is unreasonably large, then an error is posted on node N, and
151 -- False is returned (and the caller skips the proposed calculation).
153 procedure Out_Of_Range (N : Node_Id);
154 -- This procedure is called if it is determined that node N, which
155 -- appears in a non-static context, is a compile time known value
156 -- which is outside its range, i.e. the range of Etype. This is used
157 -- in contexts where this is an illegality if N is static, and should
158 -- generate a warning otherwise.
160 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id);
161 -- N and Exp are nodes representing an expression, Exp is known
162 -- to raise CE. N is rewritten in term of Exp in the optimal way.
164 function String_Type_Len (Stype : Entity_Id) return Uint;
165 -- Given a string type, determines the length of the index type, or,
166 -- if this index type is non-static, the length of the base type of
167 -- this index type. Note that if the string type is itself static,
168 -- then the index type is static, so the second case applies only
169 -- if the string type passed is non-static.
171 function Test (Cond : Boolean) return Uint;
172 pragma Inline (Test);
173 -- This function simply returns the appropriate Boolean'Pos value
174 -- corresponding to the value of Cond as a universal integer. It is
175 -- used for producing the result of the static evaluation of the
178 procedure Test_Expression_Is_Foldable
183 -- Tests to see if expression N whose single operand is Op1 is foldable,
184 -- i.e. the operand value is known at compile time. If the operation is
185 -- foldable, then Fold is True on return, and Stat indicates whether
186 -- the result is static (i.e. both operands were static). Note that it
187 -- is quite possible for Fold to be True, and Stat to be False, since
188 -- there are cases in which we know the value of an operand even though
189 -- it is not technically static (e.g. the static lower bound of a range
190 -- whose upper bound is non-static).
192 -- If Stat is set False on return, then Expression_Is_Foldable makes a
193 -- call to Check_Non_Static_Context on the operand. If Fold is False on
194 -- return, then all processing is complete, and the caller should
195 -- return, since there is nothing else to do.
197 procedure Test_Expression_Is_Foldable
203 -- Same processing, except applies to an expression N with two operands
206 procedure To_Bits (U : Uint; B : out Bits);
207 -- Converts a Uint value to a bit string of length B'Length
209 ------------------------------
210 -- Check_Non_Static_Context --
211 ------------------------------
213 procedure Check_Non_Static_Context (N : Node_Id) is
214 T : constant Entity_Id := Etype (N);
215 Checks_On : constant Boolean :=
216 not Index_Checks_Suppressed (T)
217 and not Range_Checks_Suppressed (T);
220 -- Ignore cases of non-scalar types or error types
222 if T = Any_Type or else not Is_Scalar_Type (T) then
226 -- At this stage we have a scalar type. If we have an expression
227 -- that raises CE, then we already issued a warning or error msg
228 -- so there is nothing more to be done in this routine.
230 if Raises_Constraint_Error (N) then
234 -- Now we have a scalar type which is not marked as raising a
235 -- constraint error exception. The main purpose of this routine
236 -- is to deal with static expressions appearing in a non-static
237 -- context. That means that if we do not have a static expression
238 -- then there is not much to do. The one case that we deal with
239 -- here is that if we have a floating-point value that is out of
240 -- range, then we post a warning that an infinity will result.
242 if not Is_Static_Expression (N) then
243 if Is_Floating_Point_Type (T)
244 and then Is_Out_Of_Range (N, Base_Type (T))
247 ("?float value out of range, infinity will be generated", N);
253 -- Here we have the case of outer level static expression of
254 -- scalar type, where the processing of this procedure is needed.
256 -- For real types, this is where we convert the value to a machine
257 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should
258 -- only need to do this if the parent is a constant declaration,
259 -- since in other cases, gigi should do the necessary conversion
260 -- correctly, but experimentation shows that this is not the case
261 -- on all machines, in particular if we do not convert all literals
262 -- to machine values in non-static contexts, then ACVC test C490001
263 -- fails on Sparc/Solaris and SGI/Irix.
265 if Nkind (N) = N_Real_Literal
266 and then not Is_Machine_Number (N)
267 and then not Is_Generic_Type (Etype (N))
268 and then Etype (N) /= Universal_Real
270 -- Check that value is in bounds before converting to machine
271 -- number, so as not to lose case where value overflows in the
272 -- least significant bit or less. See B490001.
274 if Is_Out_Of_Range (N, Base_Type (T)) then
279 -- Note: we have to copy the node, to avoid problems with conformance
280 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
282 Rewrite (N, New_Copy (N));
284 if not Is_Floating_Point_Type (T) then
286 (N, Corresponding_Integer_Value (N) * Small_Value (T));
288 elsif not UR_Is_Zero (Realval (N)) then
290 -- Note: even though RM 4.9(38) specifies biased rounding,
291 -- this has been modified by AI-100 in order to prevent
292 -- confusing differences in rounding between static and
293 -- non-static expressions. AI-100 specifies that the effect
294 -- of such rounding is implementation dependent, and in GNAT
295 -- we round to nearest even to match the run-time behavior.
298 (N, Machine (Base_Type (T), Realval (N), Round_Even, N));
301 Set_Is_Machine_Number (N);
304 -- Check for out of range universal integer. This is a non-static
305 -- context, so the integer value must be in range of the runtime
306 -- representation of universal integers.
308 -- We do this only within an expression, because that is the only
309 -- case in which non-static universal integer values can occur, and
310 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
311 -- called in contexts like the expression of a number declaration where
312 -- we certainly want to allow out of range values.
314 if Etype (N) = Universal_Integer
315 and then Nkind (N) = N_Integer_Literal
316 and then Nkind (Parent (N)) in N_Subexpr
318 (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer))
320 Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer)))
322 Apply_Compile_Time_Constraint_Error
323 (N, "non-static universal integer value out of range?",
324 CE_Range_Check_Failed);
326 -- Check out of range of base type
328 elsif Is_Out_Of_Range (N, Base_Type (T)) then
331 -- Give warning if outside subtype (where one or both of the
332 -- bounds of the subtype is static). This warning is omitted
333 -- if the expression appears in a range that could be null
334 -- (warnings are handled elsewhere for this case).
336 elsif T /= Base_Type (T)
337 and then Nkind (Parent (N)) /= N_Range
339 if Is_In_Range (N, T) then
342 elsif Is_Out_Of_Range (N, T) then
343 Apply_Compile_Time_Constraint_Error
344 (N, "value not in range of}?", CE_Range_Check_Failed);
347 Enable_Range_Check (N);
350 Set_Do_Range_Check (N, False);
353 end Check_Non_Static_Context;
355 ---------------------------------
356 -- Check_String_Literal_Length --
357 ---------------------------------
359 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is
361 if not Raises_Constraint_Error (N)
362 and then Is_Constrained (Ttype)
365 UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype)
367 Apply_Compile_Time_Constraint_Error
368 (N, "string length wrong for}?",
369 CE_Length_Check_Failed,
374 end Check_String_Literal_Length;
376 --------------------------
377 -- Compile_Time_Compare --
378 --------------------------
380 function Compile_Time_Compare
382 Rec : Boolean := False) return Compare_Result
384 Ltyp : constant Entity_Id := Etype (L);
385 Rtyp : constant Entity_Id := Etype (R);
387 procedure Compare_Decompose
391 -- This procedure decomposes the node N into an expression node
392 -- and a signed offset, so that the value of N is equal to the
393 -- value of R plus the value V (which may be negative). If no
394 -- such decomposition is possible, then on return R is a copy
395 -- of N, and V is set to zero.
397 function Compare_Fixup (N : Node_Id) return Node_Id;
398 -- This function deals with replacing 'Last and 'First references
399 -- with their corresponding type bounds, which we then can compare.
400 -- The argument is the original node, the result is the identity,
401 -- unless we have a 'Last/'First reference in which case the value
402 -- returned is the appropriate type bound.
404 function Is_Same_Value (L, R : Node_Id) return Boolean;
405 -- Returns True iff L and R represent expressions that definitely
406 -- have identical (but not necessarily compile time known) values
407 -- Indeed the caller is expected to have already dealt with the
408 -- cases of compile time known values, so these are not tested here.
410 -----------------------
411 -- Compare_Decompose --
412 -----------------------
414 procedure Compare_Decompose
420 if Nkind (N) = N_Op_Add
421 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
424 V := Intval (Right_Opnd (N));
427 elsif Nkind (N) = N_Op_Subtract
428 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
431 V := UI_Negate (Intval (Right_Opnd (N)));
434 elsif Nkind (N) = N_Attribute_Reference then
436 if Attribute_Name (N) = Name_Succ then
437 R := First (Expressions (N));
441 elsif Attribute_Name (N) = Name_Pred then
442 R := First (Expressions (N));
450 end Compare_Decompose;
456 function Compare_Fixup (N : Node_Id) return Node_Id is
462 if Nkind (N) = N_Attribute_Reference
463 and then (Attribute_Name (N) = Name_First
465 Attribute_Name (N) = Name_Last)
467 Xtyp := Etype (Prefix (N));
469 -- If we have no type, then just abandon the attempt to do
470 -- a fixup, this is probably the result of some other error.
476 -- Dereference an access type
478 if Is_Access_Type (Xtyp) then
479 Xtyp := Designated_Type (Xtyp);
482 -- If we don't have an array type at this stage, something
483 -- is peculiar, e.g. another error, and we abandon the attempt
486 if not Is_Array_Type (Xtyp) then
490 -- Ignore unconstrained array, since bounds are not meaningful
492 if not Is_Constrained (Xtyp) then
496 if Ekind (Xtyp) = E_String_Literal_Subtype then
497 if Attribute_Name (N) = Name_First then
498 return String_Literal_Low_Bound (Xtyp);
500 else -- Attribute_Name (N) = Name_Last
501 return Make_Integer_Literal (Sloc (N),
502 Intval => Intval (String_Literal_Low_Bound (Xtyp))
503 + String_Literal_Length (Xtyp));
507 -- Find correct index type
509 Indx := First_Index (Xtyp);
511 if Present (Expressions (N)) then
512 Subs := UI_To_Int (Expr_Value (First (Expressions (N))));
514 for J in 2 .. Subs loop
515 Indx := Next_Index (Indx);
519 Xtyp := Etype (Indx);
521 if Attribute_Name (N) = Name_First then
522 return Type_Low_Bound (Xtyp);
524 else -- Attribute_Name (N) = Name_Last
525 return Type_High_Bound (Xtyp);
536 function Is_Same_Value (L, R : Node_Id) return Boolean is
537 Lf : constant Node_Id := Compare_Fixup (L);
538 Rf : constant Node_Id := Compare_Fixup (R);
540 function Is_Same_Subscript (L, R : List_Id) return Boolean;
541 -- L, R are the Expressions values from two attribute nodes
542 -- for First or Last attributes. Either may be set to No_List
543 -- if no expressions are present (indicating subscript 1).
544 -- The result is True if both expressions represent the same
545 -- subscript (note that one case is where one subscript is
546 -- missing and the other is explicitly set to 1).
548 -----------------------
549 -- Is_Same_Subscript --
550 -----------------------
552 function Is_Same_Subscript (L, R : List_Id) return Boolean is
558 return Expr_Value (First (R)) = Uint_1;
563 return Expr_Value (First (L)) = Uint_1;
565 return Expr_Value (First (L)) = Expr_Value (First (R));
568 end Is_Same_Subscript;
570 -- Start of processing for Is_Same_Value
573 -- Values are the same if they are the same identifier and the
574 -- identifier refers to a constant object (E_Constant). This
575 -- does not however apply to Float types, since we may have two
576 -- NaN values and they should never compare equal.
578 if Nkind (Lf) = N_Identifier and then Nkind (Rf) = N_Identifier
579 and then Entity (Lf) = Entity (Rf)
580 and then not Is_Floating_Point_Type (Etype (L))
581 and then Is_Constant_Object (Entity (Lf))
585 -- Or if they are compile time known and identical
587 elsif Compile_Time_Known_Value (Lf)
589 Compile_Time_Known_Value (Rf)
590 and then Expr_Value (Lf) = Expr_Value (Rf)
594 -- Or if they are both 'First or 'Last values applying to the
595 -- same entity (first and last don't change even if value does)
597 elsif Nkind (Lf) = N_Attribute_Reference
599 Nkind (Rf) = N_Attribute_Reference
600 and then Attribute_Name (Lf) = Attribute_Name (Rf)
601 and then (Attribute_Name (Lf) = Name_First
603 Attribute_Name (Lf) = Name_Last)
604 and then Is_Entity_Name (Prefix (Lf))
605 and then Is_Entity_Name (Prefix (Rf))
606 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
607 and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf))
611 -- All other cases, we can't tell
618 -- Start of processing for Compile_Time_Compare
621 -- If either operand could raise constraint error, then we cannot
622 -- know the result at compile time (since CE may be raised!)
624 if not (Cannot_Raise_Constraint_Error (L)
626 Cannot_Raise_Constraint_Error (R))
631 -- Identical operands are most certainly equal
636 -- If expressions have no types, then do not attempt to determine
637 -- if they are the same, since something funny is going on. One
638 -- case in which this happens is during generic template analysis,
639 -- when bounds are not fully analyzed.
641 elsif No (Ltyp) or else No (Rtyp) then
644 -- We only attempt compile time analysis for scalar values, and
645 -- not for packed arrays represented as modular types, where the
646 -- semantics of comparison is quite different.
648 elsif not Is_Scalar_Type (Ltyp)
649 or else Is_Packed_Array_Type (Ltyp)
653 -- Case where comparison involves two compile time known values
655 elsif Compile_Time_Known_Value (L)
656 and then Compile_Time_Known_Value (R)
658 -- For the floating-point case, we have to be a little careful, since
659 -- at compile time we are dealing with universal exact values, but at
660 -- runtime, these will be in non-exact target form. That's why the
661 -- returned results are LE and GE below instead of LT and GT.
663 if Is_Floating_Point_Type (Ltyp)
665 Is_Floating_Point_Type (Rtyp)
668 Lo : constant Ureal := Expr_Value_R (L);
669 Hi : constant Ureal := Expr_Value_R (R);
681 -- For the integer case we know exactly (note that this includes the
682 -- fixed-point case, where we know the run time integer values now)
686 Lo : constant Uint := Expr_Value (L);
687 Hi : constant Uint := Expr_Value (R);
700 -- Cases where at least one operand is not known at compile time
703 -- Remaining checks apply only for non-generic discrete types
705 if not Is_Discrete_Type (Ltyp)
706 or else not Is_Discrete_Type (Rtyp)
707 or else Is_Generic_Type (Ltyp)
708 or else Is_Generic_Type (Rtyp)
713 -- Here is where we check for comparisons against maximum bounds of
714 -- types, where we know that no value can be outside the bounds of
715 -- the subtype. Note that this routine is allowed to assume that all
716 -- expressions are within their subtype bounds. Callers wishing to
717 -- deal with possibly invalid values must in any case take special
718 -- steps (e.g. conversions to larger types) to avoid this kind of
719 -- optimization, which is always considered to be valid. We do not
720 -- attempt this optimization with generic types, since the type
721 -- bounds may not be meaningful in this case.
723 -- We are in danger of an infinite recursion here. It does not seem
724 -- useful to go more than one level deep, so the parameter Rec is
725 -- used to protect ourselves against this infinite recursion.
729 -- See if we can get a decisive check against one operand and
730 -- a bound of the other operand (four possible tests here).
732 case Compile_Time_Compare (L, Type_Low_Bound (Rtyp), True) is
733 when LT => return LT;
734 when LE => return LE;
735 when EQ => return LE;
739 case Compile_Time_Compare (L, Type_High_Bound (Rtyp), True) is
740 when GT => return GT;
741 when GE => return GE;
742 when EQ => return GE;
746 case Compile_Time_Compare (Type_Low_Bound (Ltyp), R, True) is
747 when GT => return GT;
748 when GE => return GE;
749 when EQ => return GE;
753 case Compile_Time_Compare (Type_High_Bound (Ltyp), R, True) is
754 when LT => return LT;
755 when LE => return LE;
756 when EQ => return LE;
761 -- Next attempt is to decompose the expressions to extract
762 -- a constant offset resulting from the use of any of the forms:
769 -- Then we see if the two expressions are the same value, and if so
770 -- the result is obtained by comparing the offsets.
779 Compare_Decompose (L, Lnode, Loffs);
780 Compare_Decompose (R, Rnode, Roffs);
782 if Is_Same_Value (Lnode, Rnode) then
783 if Loffs = Roffs then
786 elsif Loffs < Roffs then
795 -- Next attempt is to see if we have an entity compared with a
796 -- compile time known value, where there is a current value
797 -- conditional for the entity which can tell us the result.
801 -- Entity variable (left operand)
804 -- Value (right operand)
807 -- If False, we have reversed the operands
810 -- Comparison operator kind from Get_Current_Value_Condition call
813 -- Value from Get_Current_Value_Condition call
818 Result : Compare_Result;
819 -- Known result before inversion
822 if Is_Entity_Name (L)
823 and then Compile_Time_Known_Value (R)
826 Val := Expr_Value (R);
829 elsif Is_Entity_Name (R)
830 and then Compile_Time_Known_Value (L)
833 Val := Expr_Value (L);
836 -- That was the last chance at finding a compile time result
842 Get_Current_Value_Condition (Var, Op, Opn);
844 -- That was the last chance, so if we got nothing return
850 Opv := Expr_Value (Opn);
852 -- We got a comparison, so we might have something interesting
854 -- Convert LE to LT and GE to GT, just so we have fewer cases
859 elsif Op = N_Op_Ge then
864 -- Deal with equality case
875 -- Deal with inequality case
877 elsif Op = N_Op_Ne then
884 -- Deal with greater than case
886 elsif Op = N_Op_Gt then
889 elsif Opv = Val - 1 then
895 -- Deal with less than case
897 else pragma Assert (Op = N_Op_Lt);
900 elsif Opv = Val + 1 then
907 -- Deal with inverting result
911 when GT => return LT;
912 when GE => return LE;
913 when LT => return GT;
914 when LE => return GE;
915 when others => return Result;
922 end Compile_Time_Compare;
924 -------------------------------
925 -- Compile_Time_Known_Bounds --
926 -------------------------------
928 function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean is
933 if not Is_Array_Type (T) then
937 Indx := First_Index (T);
938 while Present (Indx) loop
939 Typ := Underlying_Type (Etype (Indx));
940 if not Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
942 elsif not Compile_Time_Known_Value (Type_High_Bound (Typ)) then
950 end Compile_Time_Known_Bounds;
952 ------------------------------
953 -- Compile_Time_Known_Value --
954 ------------------------------
956 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
957 K : constant Node_Kind := Nkind (Op);
958 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
961 -- Never known at compile time if bad type or raises constraint error
962 -- or empty (latter case occurs only as a result of a previous error)
966 or else Etype (Op) = Any_Type
967 or else Raises_Constraint_Error (Op)
972 -- If this is not a static expression and we are in configurable run
973 -- time mode, then we consider it not known at compile time. This
974 -- avoids anomalies where whether something is permitted with a given
975 -- configurable run-time library depends on how good the compiler is
976 -- at optimizing and knowing that things are constant when they
979 if Configurable_Run_Time_Mode and then not Is_Static_Expression (Op) then
983 -- If we have an entity name, then see if it is the name of a constant
984 -- and if so, test the corresponding constant value, or the name of
985 -- an enumeration literal, which is always a constant.
987 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
989 E : constant Entity_Id := Entity (Op);
993 -- Never known at compile time if it is a packed array value.
994 -- We might want to try to evaluate these at compile time one
995 -- day, but we do not make that attempt now.
997 if Is_Packed_Array_Type (Etype (Op)) then
1001 if Ekind (E) = E_Enumeration_Literal then
1004 elsif Ekind (E) = E_Constant then
1005 V := Constant_Value (E);
1006 return Present (V) and then Compile_Time_Known_Value (V);
1010 -- We have a value, see if it is compile time known
1013 -- Integer literals are worth storing in the cache
1015 if K = N_Integer_Literal then
1017 CV_Ent.V := Intval (Op);
1020 -- Other literals and NULL are known at compile time
1023 K = N_Character_Literal
1027 K = N_String_Literal
1033 -- Any reference to Null_Parameter is known at compile time. No
1034 -- other attribute references (that have not already been folded)
1035 -- are known at compile time.
1037 elsif K = N_Attribute_Reference then
1038 return Attribute_Name (Op) = Name_Null_Parameter;
1042 -- If we fall through, not known at compile time
1046 -- If we get an exception while trying to do this test, then some error
1047 -- has occurred, and we simply say that the value is not known after all
1052 end Compile_Time_Known_Value;
1054 --------------------------------------
1055 -- Compile_Time_Known_Value_Or_Aggr --
1056 --------------------------------------
1058 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is
1060 -- If we have an entity name, then see if it is the name of a constant
1061 -- and if so, test the corresponding constant value, or the name of
1062 -- an enumeration literal, which is always a constant.
1064 if Is_Entity_Name (Op) then
1066 E : constant Entity_Id := Entity (Op);
1070 if Ekind (E) = E_Enumeration_Literal then
1073 elsif Ekind (E) /= E_Constant then
1077 V := Constant_Value (E);
1079 and then Compile_Time_Known_Value_Or_Aggr (V);
1083 -- We have a value, see if it is compile time known
1086 if Compile_Time_Known_Value (Op) then
1089 elsif Nkind (Op) = N_Aggregate then
1091 if Present (Expressions (Op)) then
1096 Expr := First (Expressions (Op));
1097 while Present (Expr) loop
1098 if not Compile_Time_Known_Value_Or_Aggr (Expr) then
1107 if Present (Component_Associations (Op)) then
1112 Cass := First (Component_Associations (Op));
1113 while Present (Cass) loop
1115 Compile_Time_Known_Value_Or_Aggr (Expression (Cass))
1127 -- All other types of values are not known at compile time
1134 end Compile_Time_Known_Value_Or_Aggr;
1140 -- This is only called for actuals of functions that are not predefined
1141 -- operators (which have already been rewritten as operators at this
1142 -- stage), so the call can never be folded, and all that needs doing for
1143 -- the actual is to do the check for a non-static context.
1145 procedure Eval_Actual (N : Node_Id) is
1147 Check_Non_Static_Context (N);
1150 --------------------
1151 -- Eval_Allocator --
1152 --------------------
1154 -- Allocators are never static, so all we have to do is to do the
1155 -- check for a non-static context if an expression is present.
1157 procedure Eval_Allocator (N : Node_Id) is
1158 Expr : constant Node_Id := Expression (N);
1161 if Nkind (Expr) = N_Qualified_Expression then
1162 Check_Non_Static_Context (Expression (Expr));
1166 ------------------------
1167 -- Eval_Arithmetic_Op --
1168 ------------------------
1170 -- Arithmetic operations are static functions, so the result is static
1171 -- if both operands are static (RM 4.9(7), 4.9(20)).
1173 procedure Eval_Arithmetic_Op (N : Node_Id) is
1174 Left : constant Node_Id := Left_Opnd (N);
1175 Right : constant Node_Id := Right_Opnd (N);
1176 Ltype : constant Entity_Id := Etype (Left);
1177 Rtype : constant Entity_Id := Etype (Right);
1182 -- If not foldable we are done
1184 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1190 -- Fold for cases where both operands are of integer type
1192 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
1194 Left_Int : constant Uint := Expr_Value (Left);
1195 Right_Int : constant Uint := Expr_Value (Right);
1202 Result := Left_Int + Right_Int;
1204 when N_Op_Subtract =>
1205 Result := Left_Int - Right_Int;
1207 when N_Op_Multiply =>
1210 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
1212 Result := Left_Int * Right_Int;
1219 -- The exception Constraint_Error is raised by integer
1220 -- division, rem and mod if the right operand is zero.
1222 if Right_Int = 0 then
1223 Apply_Compile_Time_Constraint_Error
1224 (N, "division by zero",
1230 Result := Left_Int / Right_Int;
1235 -- The exception Constraint_Error is raised by integer
1236 -- division, rem and mod if the right operand is zero.
1238 if Right_Int = 0 then
1239 Apply_Compile_Time_Constraint_Error
1240 (N, "mod with zero divisor",
1245 Result := Left_Int mod Right_Int;
1250 -- The exception Constraint_Error is raised by integer
1251 -- division, rem and mod if the right operand is zero.
1253 if Right_Int = 0 then
1254 Apply_Compile_Time_Constraint_Error
1255 (N, "rem with zero divisor",
1261 Result := Left_Int rem Right_Int;
1265 raise Program_Error;
1268 -- Adjust the result by the modulus if the type is a modular type
1270 if Is_Modular_Integer_Type (Ltype) then
1271 Result := Result mod Modulus (Ltype);
1273 -- For a signed integer type, check non-static overflow
1275 elsif (not Stat) and then Is_Signed_Integer_Type (Ltype) then
1277 BT : constant Entity_Id := Base_Type (Ltype);
1278 Lo : constant Uint := Expr_Value (Type_Low_Bound (BT));
1279 Hi : constant Uint := Expr_Value (Type_High_Bound (BT));
1281 if Result < Lo or else Result > Hi then
1282 Apply_Compile_Time_Constraint_Error
1283 (N, "value not in range of }?",
1284 CE_Overflow_Check_Failed,
1291 -- If we get here we can fold the result
1293 Fold_Uint (N, Result, Stat);
1296 -- Cases where at least one operand is a real. We handle the cases
1297 -- of both reals, or mixed/real integer cases (the latter happen
1298 -- only for divide and multiply, and the result is always real).
1300 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
1307 if Is_Real_Type (Ltype) then
1308 Left_Real := Expr_Value_R (Left);
1310 Left_Real := UR_From_Uint (Expr_Value (Left));
1313 if Is_Real_Type (Rtype) then
1314 Right_Real := Expr_Value_R (Right);
1316 Right_Real := UR_From_Uint (Expr_Value (Right));
1319 if Nkind (N) = N_Op_Add then
1320 Result := Left_Real + Right_Real;
1322 elsif Nkind (N) = N_Op_Subtract then
1323 Result := Left_Real - Right_Real;
1325 elsif Nkind (N) = N_Op_Multiply then
1326 Result := Left_Real * Right_Real;
1328 else pragma Assert (Nkind (N) = N_Op_Divide);
1329 if UR_Is_Zero (Right_Real) then
1330 Apply_Compile_Time_Constraint_Error
1331 (N, "division by zero", CE_Divide_By_Zero);
1335 Result := Left_Real / Right_Real;
1338 Fold_Ureal (N, Result, Stat);
1341 end Eval_Arithmetic_Op;
1343 ----------------------------
1344 -- Eval_Character_Literal --
1345 ----------------------------
1347 -- Nothing to be done!
1349 procedure Eval_Character_Literal (N : Node_Id) is
1350 pragma Warnings (Off, N);
1353 end Eval_Character_Literal;
1359 -- Static function calls are either calls to predefined operators
1360 -- with static arguments, or calls to functions that rename a literal.
1361 -- Only the latter case is handled here, predefined operators are
1362 -- constant-folded elsewhere.
1364 -- If the function is itself inherited (see 7423-001) the literal of
1365 -- the parent type must be explicitly converted to the return type
1368 procedure Eval_Call (N : Node_Id) is
1369 Loc : constant Source_Ptr := Sloc (N);
1370 Typ : constant Entity_Id := Etype (N);
1374 if Nkind (N) = N_Function_Call
1375 and then No (Parameter_Associations (N))
1376 and then Is_Entity_Name (Name (N))
1377 and then Present (Alias (Entity (Name (N))))
1378 and then Is_Enumeration_Type (Base_Type (Typ))
1380 Lit := Alias (Entity (Name (N)));
1381 while Present (Alias (Lit)) loop
1385 if Ekind (Lit) = E_Enumeration_Literal then
1386 if Base_Type (Etype (Lit)) /= Base_Type (Typ) then
1388 (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc)));
1390 Rewrite (N, New_Occurrence_Of (Lit, Loc));
1398 ------------------------
1399 -- Eval_Concatenation --
1400 ------------------------
1402 -- Concatenation is a static function, so the result is static if
1403 -- both operands are static (RM 4.9(7), 4.9(21)).
1405 procedure Eval_Concatenation (N : Node_Id) is
1406 Left : constant Node_Id := Left_Opnd (N);
1407 Right : constant Node_Id := Right_Opnd (N);
1408 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
1413 -- Concatenation is never static in Ada 83, so if Ada 83
1414 -- check operand non-static context
1416 if Ada_Version = Ada_83
1417 and then Comes_From_Source (N)
1419 Check_Non_Static_Context (Left);
1420 Check_Non_Static_Context (Right);
1424 -- If not foldable we are done. In principle concatenation that yields
1425 -- any string type is static (i.e. an array type of character types).
1426 -- However, character types can include enumeration literals, and
1427 -- concatenation in that case cannot be described by a literal, so we
1428 -- only consider the operation static if the result is an array of
1429 -- (a descendant of) a predefined character type.
1431 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1433 if Is_Standard_Character_Type (C_Typ)
1438 Set_Is_Static_Expression (N, False);
1442 -- Compile time string concatenation
1444 -- ??? Note that operands that are aggregates can be marked as
1445 -- static, so we should attempt at a later stage to fold
1446 -- concatenations with such aggregates.
1449 Left_Str : constant Node_Id := Get_String_Val (Left);
1451 Right_Str : constant Node_Id := Get_String_Val (Right);
1452 Folded_Val : String_Id;
1455 -- Establish new string literal, and store left operand. We make
1456 -- sure to use the special Start_String that takes an operand if
1457 -- the left operand is a string literal. Since this is optimized
1458 -- in the case where that is the most recently created string
1459 -- literal, we ensure efficient time/space behavior for the
1460 -- case of a concatenation of a series of string literals.
1462 if Nkind (Left_Str) = N_String_Literal then
1463 Left_Len := String_Length (Strval (Left_Str));
1465 -- If the left operand is the empty string, and the right operand
1466 -- is a string literal (the case of "" & "..."), the result is the
1467 -- value of the right operand. This optimization is important when
1468 -- Is_Folded_In_Parser, to avoid copying an enormous right
1471 if Left_Len = 0 and then Nkind (Right_Str) = N_String_Literal then
1472 Folded_Val := Strval (Right_Str);
1474 Start_String (Strval (Left_Str));
1479 Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str)));
1483 -- Now append the characters of the right operand, unless we
1484 -- optimized the "" & "..." case above.
1486 if Nkind (Right_Str) = N_String_Literal then
1487 if Left_Len /= 0 then
1488 Store_String_Chars (Strval (Right_Str));
1489 Folded_Val := End_String;
1492 Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str)));
1493 Folded_Val := End_String;
1496 Set_Is_Static_Expression (N, Stat);
1500 -- If left operand is the empty string, the result is the
1501 -- right operand, including its bounds if anomalous.
1504 and then Is_Array_Type (Etype (Right))
1505 and then Etype (Right) /= Any_String
1507 Set_Etype (N, Etype (Right));
1510 Fold_Str (N, Folded_Val, Static => True);
1513 end Eval_Concatenation;
1515 ---------------------------------
1516 -- Eval_Conditional_Expression --
1517 ---------------------------------
1519 -- This GNAT internal construct can never be statically folded, so the
1520 -- only required processing is to do the check for non-static context
1521 -- for the two expression operands.
1523 procedure Eval_Conditional_Expression (N : Node_Id) is
1524 Condition : constant Node_Id := First (Expressions (N));
1525 Then_Expr : constant Node_Id := Next (Condition);
1526 Else_Expr : constant Node_Id := Next (Then_Expr);
1529 Check_Non_Static_Context (Then_Expr);
1530 Check_Non_Static_Context (Else_Expr);
1531 end Eval_Conditional_Expression;
1533 ----------------------
1534 -- Eval_Entity_Name --
1535 ----------------------
1537 -- This procedure is used for identifiers and expanded names other than
1538 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
1539 -- static if they denote a static constant (RM 4.9(6)) or if the name
1540 -- denotes an enumeration literal (RM 4.9(22)).
1542 procedure Eval_Entity_Name (N : Node_Id) is
1543 Def_Id : constant Entity_Id := Entity (N);
1547 -- Enumeration literals are always considered to be constants
1548 -- and cannot raise constraint error (RM 4.9(22)).
1550 if Ekind (Def_Id) = E_Enumeration_Literal then
1551 Set_Is_Static_Expression (N);
1554 -- A name is static if it denotes a static constant (RM 4.9(5)), and
1555 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
1556 -- it does not violate 10.2.1(8) here, since this is not a variable.
1558 elsif Ekind (Def_Id) = E_Constant then
1560 -- Deferred constants must always be treated as nonstatic
1561 -- outside the scope of their full view.
1563 if Present (Full_View (Def_Id))
1564 and then not In_Open_Scopes (Scope (Def_Id))
1568 Val := Constant_Value (Def_Id);
1571 if Present (Val) then
1572 Set_Is_Static_Expression
1573 (N, Is_Static_Expression (Val)
1574 and then Is_Static_Subtype (Etype (Def_Id)));
1575 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
1577 if not Is_Static_Expression (N)
1578 and then not Is_Generic_Type (Etype (N))
1580 Validate_Static_Object_Name (N);
1587 -- Fall through if the name is not static
1589 Validate_Static_Object_Name (N);
1590 end Eval_Entity_Name;
1592 ----------------------------
1593 -- Eval_Indexed_Component --
1594 ----------------------------
1596 -- Indexed components are never static, so we need to perform the check
1597 -- for non-static context on the index values. Then, we check if the
1598 -- value can be obtained at compile time, even though it is non-static.
1600 procedure Eval_Indexed_Component (N : Node_Id) is
1604 -- Check for non-static context on index values
1606 Expr := First (Expressions (N));
1607 while Present (Expr) loop
1608 Check_Non_Static_Context (Expr);
1612 -- If the indexed component appears in an object renaming declaration
1613 -- then we do not want to try to evaluate it, since in this case we
1614 -- need the identity of the array element.
1616 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
1619 -- Similarly if the indexed component appears as the prefix of an
1620 -- attribute we don't want to evaluate it, because at least for
1621 -- some cases of attributes we need the identify (e.g. Access, Size)
1623 elsif Nkind (Parent (N)) = N_Attribute_Reference then
1627 -- Note: there are other cases, such as the left side of an assignment,
1628 -- or an OUT parameter for a call, where the replacement results in the
1629 -- illegal use of a constant, But these cases are illegal in the first
1630 -- place, so the replacement, though silly, is harmless.
1632 -- Now see if this is a constant array reference
1634 if List_Length (Expressions (N)) = 1
1635 and then Is_Entity_Name (Prefix (N))
1636 and then Ekind (Entity (Prefix (N))) = E_Constant
1637 and then Present (Constant_Value (Entity (Prefix (N))))
1640 Loc : constant Source_Ptr := Sloc (N);
1641 Arr : constant Node_Id := Constant_Value (Entity (Prefix (N)));
1642 Sub : constant Node_Id := First (Expressions (N));
1648 -- Linear one's origin subscript value for array reference
1651 -- Lower bound of the first array index
1654 -- Value from constant array
1657 Atyp := Etype (Arr);
1659 if Is_Access_Type (Atyp) then
1660 Atyp := Designated_Type (Atyp);
1663 -- If we have an array type (we should have but perhaps there
1664 -- are error cases where this is not the case), then see if we
1665 -- can do a constant evaluation of the array reference.
1667 if Is_Array_Type (Atyp) then
1668 if Ekind (Atyp) = E_String_Literal_Subtype then
1669 Lbd := String_Literal_Low_Bound (Atyp);
1671 Lbd := Type_Low_Bound (Etype (First_Index (Atyp)));
1674 if Compile_Time_Known_Value (Sub)
1675 and then Nkind (Arr) = N_Aggregate
1676 and then Compile_Time_Known_Value (Lbd)
1677 and then Is_Discrete_Type (Component_Type (Atyp))
1679 Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1;
1681 if List_Length (Expressions (Arr)) >= Lin then
1682 Elm := Pick (Expressions (Arr), Lin);
1684 -- If the resulting expression is compile time known,
1685 -- then we can rewrite the indexed component with this
1686 -- value, being sure to mark the result as non-static.
1687 -- We also reset the Sloc, in case this generates an
1688 -- error later on (e.g. 136'Access).
1690 if Compile_Time_Known_Value (Elm) then
1691 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
1692 Set_Is_Static_Expression (N, False);
1700 end Eval_Indexed_Component;
1702 --------------------------
1703 -- Eval_Integer_Literal --
1704 --------------------------
1706 -- Numeric literals are static (RM 4.9(1)), and have already been marked
1707 -- as static by the analyzer. The reason we did it that early is to allow
1708 -- the possibility of turning off the Is_Static_Expression flag after
1709 -- analysis, but before resolution, when integer literals are generated
1710 -- in the expander that do not correspond to static expressions.
1712 procedure Eval_Integer_Literal (N : Node_Id) is
1713 T : constant Entity_Id := Etype (N);
1715 function In_Any_Integer_Context return Boolean;
1716 -- If the literal is resolved with a specific type in a context
1717 -- where the expected type is Any_Integer, there are no range checks
1718 -- on the literal. By the time the literal is evaluated, it carries
1719 -- the type imposed by the enclosing expression, and we must recover
1720 -- the context to determine that Any_Integer is meant.
1722 ----------------------------
1723 -- To_Any_Integer_Context --
1724 ----------------------------
1726 function In_Any_Integer_Context return Boolean is
1727 Par : constant Node_Id := Parent (N);
1728 K : constant Node_Kind := Nkind (Par);
1731 -- Any_Integer also appears in digits specifications for real types,
1732 -- but those have bounds smaller that those of any integer base
1733 -- type, so we can safely ignore these cases.
1735 return K = N_Number_Declaration
1736 or else K = N_Attribute_Reference
1737 or else K = N_Attribute_Definition_Clause
1738 or else K = N_Modular_Type_Definition
1739 or else K = N_Signed_Integer_Type_Definition;
1740 end In_Any_Integer_Context;
1742 -- Start of processing for Eval_Integer_Literal
1746 -- If the literal appears in a non-expression context, then it is
1747 -- certainly appearing in a non-static context, so check it. This
1748 -- is actually a redundant check, since Check_Non_Static_Context
1749 -- would check it, but it seems worth while avoiding the call.
1751 if Nkind (Parent (N)) not in N_Subexpr
1752 and then not In_Any_Integer_Context
1754 Check_Non_Static_Context (N);
1757 -- Modular integer literals must be in their base range
1759 if Is_Modular_Integer_Type (T)
1760 and then Is_Out_Of_Range (N, Base_Type (T))
1764 end Eval_Integer_Literal;
1766 ---------------------
1767 -- Eval_Logical_Op --
1768 ---------------------
1770 -- Logical operations are static functions, so the result is potentially
1771 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
1773 procedure Eval_Logical_Op (N : Node_Id) is
1774 Left : constant Node_Id := Left_Opnd (N);
1775 Right : constant Node_Id := Right_Opnd (N);
1780 -- If not foldable we are done
1782 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1788 -- Compile time evaluation of logical operation
1791 Left_Int : constant Uint := Expr_Value (Left);
1792 Right_Int : constant Uint := Expr_Value (Right);
1795 if Is_Modular_Integer_Type (Etype (N)) then
1797 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
1798 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
1801 To_Bits (Left_Int, Left_Bits);
1802 To_Bits (Right_Int, Right_Bits);
1804 -- Note: should really be able to use array ops instead of
1805 -- these loops, but they weren't working at the time ???
1807 if Nkind (N) = N_Op_And then
1808 for J in Left_Bits'Range loop
1809 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
1812 elsif Nkind (N) = N_Op_Or then
1813 for J in Left_Bits'Range loop
1814 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
1818 pragma Assert (Nkind (N) = N_Op_Xor);
1820 for J in Left_Bits'Range loop
1821 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
1825 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat);
1829 pragma Assert (Is_Boolean_Type (Etype (N)));
1831 if Nkind (N) = N_Op_And then
1833 Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat);
1835 elsif Nkind (N) = N_Op_Or then
1837 Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat);
1840 pragma Assert (Nkind (N) = N_Op_Xor);
1842 Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat);
1846 end Eval_Logical_Op;
1848 ------------------------
1849 -- Eval_Membership_Op --
1850 ------------------------
1852 -- A membership test is potentially static if the expression is static,
1853 -- and the range is a potentially static range, or is a subtype mark
1854 -- denoting a static subtype (RM 4.9(12)).
1856 procedure Eval_Membership_Op (N : Node_Id) is
1857 Left : constant Node_Id := Left_Opnd (N);
1858 Right : constant Node_Id := Right_Opnd (N);
1867 -- Ignore if error in either operand, except to make sure that
1868 -- Any_Type is properly propagated to avoid junk cascaded errors.
1870 if Etype (Left) = Any_Type
1871 or else Etype (Right) = Any_Type
1873 Set_Etype (N, Any_Type);
1877 -- Case of right operand is a subtype name
1879 if Is_Entity_Name (Right) then
1880 Def_Id := Entity (Right);
1882 if (Is_Scalar_Type (Def_Id) or else Is_String_Type (Def_Id))
1883 and then Is_OK_Static_Subtype (Def_Id)
1885 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
1887 if not Fold or else not Stat then
1891 Check_Non_Static_Context (Left);
1895 -- For string membership tests we will check the length
1898 if not Is_String_Type (Def_Id) then
1899 Lo := Type_Low_Bound (Def_Id);
1900 Hi := Type_High_Bound (Def_Id);
1907 -- Case of right operand is a range
1910 if Is_Static_Range (Right) then
1911 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
1913 if not Fold or else not Stat then
1916 -- If one bound of range raises CE, then don't try to fold
1918 elsif not Is_OK_Static_Range (Right) then
1919 Check_Non_Static_Context (Left);
1924 Check_Non_Static_Context (Left);
1928 -- Here we know range is an OK static range
1930 Lo := Low_Bound (Right);
1931 Hi := High_Bound (Right);
1934 -- For strings we check that the length of the string expression is
1935 -- compatible with the string subtype if the subtype is constrained,
1936 -- or if unconstrained then the test is always true.
1938 if Is_String_Type (Etype (Right)) then
1939 if not Is_Constrained (Etype (Right)) then
1944 Typlen : constant Uint := String_Type_Len (Etype (Right));
1945 Strlen : constant Uint :=
1946 UI_From_Int (String_Length (Strval (Get_String_Val (Left))));
1948 Result := (Typlen = Strlen);
1952 -- Fold the membership test. We know we have a static range and Lo
1953 -- and Hi are set to the expressions for the end points of this range.
1955 elsif Is_Real_Type (Etype (Right)) then
1957 Leftval : constant Ureal := Expr_Value_R (Left);
1960 Result := Expr_Value_R (Lo) <= Leftval
1961 and then Leftval <= Expr_Value_R (Hi);
1966 Leftval : constant Uint := Expr_Value (Left);
1969 Result := Expr_Value (Lo) <= Leftval
1970 and then Leftval <= Expr_Value (Hi);
1974 if Nkind (N) = N_Not_In then
1975 Result := not Result;
1978 Fold_Uint (N, Test (Result), True);
1979 Warn_On_Known_Condition (N);
1980 end Eval_Membership_Op;
1982 ------------------------
1983 -- Eval_Named_Integer --
1984 ------------------------
1986 procedure Eval_Named_Integer (N : Node_Id) is
1989 Expr_Value (Expression (Declaration_Node (Entity (N)))), True);
1990 end Eval_Named_Integer;
1992 ---------------------
1993 -- Eval_Named_Real --
1994 ---------------------
1996 procedure Eval_Named_Real (N : Node_Id) is
1999 Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True);
2000 end Eval_Named_Real;
2006 -- Exponentiation is a static functions, so the result is potentially
2007 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2009 procedure Eval_Op_Expon (N : Node_Id) is
2010 Left : constant Node_Id := Left_Opnd (N);
2011 Right : constant Node_Id := Right_Opnd (N);
2016 -- If not foldable we are done
2018 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2024 -- Fold exponentiation operation
2027 Right_Int : constant Uint := Expr_Value (Right);
2032 if Is_Integer_Type (Etype (Left)) then
2034 Left_Int : constant Uint := Expr_Value (Left);
2038 -- Exponentiation of an integer raises the exception
2039 -- Constraint_Error for a negative exponent (RM 4.5.6)
2041 if Right_Int < 0 then
2042 Apply_Compile_Time_Constraint_Error
2043 (N, "integer exponent negative",
2044 CE_Range_Check_Failed,
2049 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
2050 Result := Left_Int ** Right_Int;
2055 if Is_Modular_Integer_Type (Etype (N)) then
2056 Result := Result mod Modulus (Etype (N));
2059 Fold_Uint (N, Result, Stat);
2067 Left_Real : constant Ureal := Expr_Value_R (Left);
2070 -- Cannot have a zero base with a negative exponent
2072 if UR_Is_Zero (Left_Real) then
2074 if Right_Int < 0 then
2075 Apply_Compile_Time_Constraint_Error
2076 (N, "zero ** negative integer",
2077 CE_Range_Check_Failed,
2081 Fold_Ureal (N, Ureal_0, Stat);
2085 Fold_Ureal (N, Left_Real ** Right_Int, Stat);
2096 -- The not operation is a static functions, so the result is potentially
2097 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
2099 procedure Eval_Op_Not (N : Node_Id) is
2100 Right : constant Node_Id := Right_Opnd (N);
2105 -- If not foldable we are done
2107 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
2113 -- Fold not operation
2116 Rint : constant Uint := Expr_Value (Right);
2117 Typ : constant Entity_Id := Etype (N);
2120 -- Negation is equivalent to subtracting from the modulus minus
2121 -- one. For a binary modulus this is equivalent to the ones-
2122 -- component of the original value. For non-binary modulus this
2123 -- is an arbitrary but consistent definition.
2125 if Is_Modular_Integer_Type (Typ) then
2126 Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat);
2129 pragma Assert (Is_Boolean_Type (Typ));
2130 Fold_Uint (N, Test (not Is_True (Rint)), Stat);
2133 Set_Is_Static_Expression (N, Stat);
2137 -------------------------------
2138 -- Eval_Qualified_Expression --
2139 -------------------------------
2141 -- A qualified expression is potentially static if its subtype mark denotes
2142 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
2144 procedure Eval_Qualified_Expression (N : Node_Id) is
2145 Operand : constant Node_Id := Expression (N);
2146 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
2153 -- Can only fold if target is string or scalar and subtype is static
2154 -- Also, do not fold if our parent is an allocator (this is because
2155 -- the qualified expression is really part of the syntactic structure
2156 -- of an allocator, and we do not want to end up with something that
2157 -- corresponds to "new 1" where the 1 is the result of folding a
2158 -- qualified expression).
2160 if not Is_Static_Subtype (Target_Type)
2161 or else Nkind (Parent (N)) = N_Allocator
2163 Check_Non_Static_Context (Operand);
2165 -- If operand is known to raise constraint_error, set the
2166 -- flag on the expression so it does not get optimized away.
2168 if Nkind (Operand) = N_Raise_Constraint_Error then
2169 Set_Raises_Constraint_Error (N);
2175 -- If not foldable we are done
2177 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
2182 -- Don't try fold if target type has constraint error bounds
2184 elsif not Is_OK_Static_Subtype (Target_Type) then
2185 Set_Raises_Constraint_Error (N);
2189 -- Here we will fold, save Print_In_Hex indication
2191 Hex := Nkind (Operand) = N_Integer_Literal
2192 and then Print_In_Hex (Operand);
2194 -- Fold the result of qualification
2196 if Is_Discrete_Type (Target_Type) then
2197 Fold_Uint (N, Expr_Value (Operand), Stat);
2199 -- Preserve Print_In_Hex indication
2201 if Hex and then Nkind (N) = N_Integer_Literal then
2202 Set_Print_In_Hex (N);
2205 elsif Is_Real_Type (Target_Type) then
2206 Fold_Ureal (N, Expr_Value_R (Operand), Stat);
2209 Fold_Str (N, Strval (Get_String_Val (Operand)), Stat);
2212 Set_Is_Static_Expression (N, False);
2214 Check_String_Literal_Length (N, Target_Type);
2220 -- The expression may be foldable but not static
2222 Set_Is_Static_Expression (N, Stat);
2224 if Is_Out_Of_Range (N, Etype (N)) then
2227 end Eval_Qualified_Expression;
2229 -----------------------
2230 -- Eval_Real_Literal --
2231 -----------------------
2233 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2234 -- as static by the analyzer. The reason we did it that early is to allow
2235 -- the possibility of turning off the Is_Static_Expression flag after
2236 -- analysis, but before resolution, when integer literals are generated
2237 -- in the expander that do not correspond to static expressions.
2239 procedure Eval_Real_Literal (N : Node_Id) is
2240 PK : constant Node_Kind := Nkind (Parent (N));
2243 -- If the literal appears in a non-expression context
2244 -- and not as part of a number declaration, then it is
2245 -- appearing in a non-static context, so check it.
2247 if PK not in N_Subexpr and then PK /= N_Number_Declaration then
2248 Check_Non_Static_Context (N);
2250 end Eval_Real_Literal;
2252 ------------------------
2253 -- Eval_Relational_Op --
2254 ------------------------
2256 -- Relational operations are static functions, so the result is static
2257 -- if both operands are static (RM 4.9(7), 4.9(20)).
2259 procedure Eval_Relational_Op (N : Node_Id) is
2260 Left : constant Node_Id := Left_Opnd (N);
2261 Right : constant Node_Id := Right_Opnd (N);
2262 Typ : constant Entity_Id := Etype (Left);
2268 -- One special case to deal with first. If we can tell that the result
2269 -- will be false because the lengths of one or more index subtypes are
2270 -- compile time known and different, then we can replace the entire
2271 -- result by False. We only do this for one dimensional arrays, because
2272 -- the case of multi-dimensional arrays is rare and too much trouble! If
2273 -- one of the operands is an illegal aggregate, its type might still be
2274 -- an arbitrary composite type, so nothing to do.
2276 if Is_Array_Type (Typ)
2277 and then Typ /= Any_Composite
2278 and then Number_Dimensions (Typ) = 1
2279 and then (Nkind (N) = N_Op_Eq or else Nkind (N) = N_Op_Ne)
2281 if Raises_Constraint_Error (Left)
2282 or else Raises_Constraint_Error (Right)
2287 -- OK, we have the case where we may be able to do this fold
2289 Length_Mismatch : declare
2290 procedure Get_Static_Length (Op : Node_Id; Len : out Uint);
2291 -- If Op is an expression for a constrained array with a known
2292 -- at compile time length, then Len is set to this (non-negative
2293 -- length). Otherwise Len is set to minus 1.
2295 -----------------------
2296 -- Get_Static_Length --
2297 -----------------------
2299 procedure Get_Static_Length (Op : Node_Id; Len : out Uint) is
2303 -- First easy case string literal
2305 if Nkind (Op) = N_String_Literal then
2306 Len := UI_From_Int (String_Length (Strval (Op)));
2310 -- Second easy case, not constrained subtype, so no length
2312 if not Is_Constrained (Etype (Op)) then
2313 Len := Uint_Minus_1;
2319 T := Etype (First_Index (Etype (Op)));
2321 -- The simple case, both bounds are known at compile time
2323 if Is_Discrete_Type (T)
2325 Compile_Time_Known_Value (Type_Low_Bound (T))
2327 Compile_Time_Known_Value (Type_High_Bound (T))
2329 Len := UI_Max (Uint_0,
2330 Expr_Value (Type_High_Bound (T)) -
2331 Expr_Value (Type_Low_Bound (T)) + 1);
2335 -- A more complex case, where the bounds are of the form
2336 -- X [+/- K1] .. X [+/- K2]), where X is an expression that is
2337 -- either A'First or A'Last (with A an entity name), or X is an
2338 -- entity name, and the two X's are the same and K1 and K2 are
2339 -- known at compile time, in this case, the length can also be
2340 -- computed at compile time, even though the bounds are not
2341 -- known. A common case of this is e.g. (X'First..X'First+5).
2343 Extract_Length : declare
2344 procedure Decompose_Expr
2346 Ent : out Entity_Id;
2347 Kind : out Character;
2349 -- Given an expression, see if is of the form above,
2350 -- X [+/- K]. If so Ent is set to the entity in X,
2351 -- Kind is 'F','L','E' for 'First/'Last/simple entity,
2352 -- and Cons is the value of K. If the expression is
2353 -- not of the required form, Ent is set to Empty.
2355 --------------------
2356 -- Decompose_Expr --
2357 --------------------
2359 procedure Decompose_Expr
2361 Ent : out Entity_Id;
2362 Kind : out Character;
2368 if Nkind (Expr) = N_Op_Add
2369 and then Compile_Time_Known_Value (Right_Opnd (Expr))
2371 Exp := Left_Opnd (Expr);
2372 Cons := Expr_Value (Right_Opnd (Expr));
2374 elsif Nkind (Expr) = N_Op_Subtract
2375 and then Compile_Time_Known_Value (Right_Opnd (Expr))
2377 Exp := Left_Opnd (Expr);
2378 Cons := -Expr_Value (Right_Opnd (Expr));
2385 -- At this stage Exp is set to the potential X
2387 if Nkind (Exp) = N_Attribute_Reference then
2388 if Attribute_Name (Exp) = Name_First then
2390 elsif Attribute_Name (Exp) = Name_Last then
2397 Exp := Prefix (Exp);
2403 if Is_Entity_Name (Exp)
2404 and then Present (Entity (Exp))
2406 Ent := Entity (Exp);
2414 Ent1, Ent2 : Entity_Id;
2415 Kind1, Kind2 : Character;
2416 Cons1, Cons2 : Uint;
2418 -- Start of processing for Extract_Length
2421 Decompose_Expr (Type_Low_Bound (T), Ent1, Kind1, Cons1);
2422 Decompose_Expr (Type_High_Bound (T), Ent2, Kind2, Cons2);
2425 and then Kind1 = Kind2
2426 and then Ent1 = Ent2
2428 Len := Cons2 - Cons1 + 1;
2430 Len := Uint_Minus_1;
2433 end Get_Static_Length;
2440 -- Start of processing for Length_Mismatch
2443 Get_Static_Length (Left, Len_L);
2444 Get_Static_Length (Right, Len_R);
2446 if Len_L /= Uint_Minus_1
2447 and then Len_R /= Uint_Minus_1
2448 and then Len_L /= Len_R
2450 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
2451 Warn_On_Known_Condition (N);
2454 end Length_Mismatch;
2457 -- Another special case: comparisons of access types, where one or both
2458 -- operands are known to be null, so the result can be determined.
2460 if Is_Access_Type (Typ) then
2461 if Known_Null (Left) then
2462 if Known_Null (Right) then
2463 Fold_Uint (N, Test (Nkind (N) = N_Op_Eq), False);
2464 Warn_On_Known_Condition (N);
2467 elsif Known_Non_Null (Right) then
2468 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
2469 Warn_On_Known_Condition (N);
2473 elsif Known_Non_Null (Left) then
2474 if Known_Null (Right) then
2475 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
2476 Warn_On_Known_Condition (N);
2482 -- Can only fold if type is scalar (don't fold string ops)
2484 if not Is_Scalar_Type (Typ) then
2485 Check_Non_Static_Context (Left);
2486 Check_Non_Static_Context (Right);
2490 -- If not foldable we are done
2492 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2498 -- Integer and Enumeration (discrete) type cases
2500 if Is_Discrete_Type (Typ) then
2502 Left_Int : constant Uint := Expr_Value (Left);
2503 Right_Int : constant Uint := Expr_Value (Right);
2507 when N_Op_Eq => Result := Left_Int = Right_Int;
2508 when N_Op_Ne => Result := Left_Int /= Right_Int;
2509 when N_Op_Lt => Result := Left_Int < Right_Int;
2510 when N_Op_Le => Result := Left_Int <= Right_Int;
2511 when N_Op_Gt => Result := Left_Int > Right_Int;
2512 when N_Op_Ge => Result := Left_Int >= Right_Int;
2515 raise Program_Error;
2518 Fold_Uint (N, Test (Result), Stat);
2524 pragma Assert (Is_Real_Type (Typ));
2527 Left_Real : constant Ureal := Expr_Value_R (Left);
2528 Right_Real : constant Ureal := Expr_Value_R (Right);
2532 when N_Op_Eq => Result := (Left_Real = Right_Real);
2533 when N_Op_Ne => Result := (Left_Real /= Right_Real);
2534 when N_Op_Lt => Result := (Left_Real < Right_Real);
2535 when N_Op_Le => Result := (Left_Real <= Right_Real);
2536 when N_Op_Gt => Result := (Left_Real > Right_Real);
2537 when N_Op_Ge => Result := (Left_Real >= Right_Real);
2540 raise Program_Error;
2543 Fold_Uint (N, Test (Result), Stat);
2547 Warn_On_Known_Condition (N);
2548 end Eval_Relational_Op;
2554 -- Shift operations are intrinsic operations that can never be static,
2555 -- so the only processing required is to perform the required check for
2556 -- a non static context for the two operands.
2558 -- Actually we could do some compile time evaluation here some time ???
2560 procedure Eval_Shift (N : Node_Id) is
2562 Check_Non_Static_Context (Left_Opnd (N));
2563 Check_Non_Static_Context (Right_Opnd (N));
2566 ------------------------
2567 -- Eval_Short_Circuit --
2568 ------------------------
2570 -- A short circuit operation is potentially static if both operands
2571 -- are potentially static (RM 4.9 (13))
2573 procedure Eval_Short_Circuit (N : Node_Id) is
2574 Kind : constant Node_Kind := Nkind (N);
2575 Left : constant Node_Id := Left_Opnd (N);
2576 Right : constant Node_Id := Right_Opnd (N);
2578 Rstat : constant Boolean :=
2579 Is_Static_Expression (Left)
2580 and then Is_Static_Expression (Right);
2583 -- Short circuit operations are never static in Ada 83
2585 if Ada_Version = Ada_83
2586 and then Comes_From_Source (N)
2588 Check_Non_Static_Context (Left);
2589 Check_Non_Static_Context (Right);
2593 -- Now look at the operands, we can't quite use the normal call to
2594 -- Test_Expression_Is_Foldable here because short circuit operations
2595 -- are a special case, they can still be foldable, even if the right
2596 -- operand raises constraint error.
2598 -- If either operand is Any_Type, just propagate to result and
2599 -- do not try to fold, this prevents cascaded errors.
2601 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
2602 Set_Etype (N, Any_Type);
2605 -- If left operand raises constraint error, then replace node N with
2606 -- the raise constraint error node, and we are obviously not foldable.
2607 -- Is_Static_Expression is set from the two operands in the normal way,
2608 -- and we check the right operand if it is in a non-static context.
2610 elsif Raises_Constraint_Error (Left) then
2612 Check_Non_Static_Context (Right);
2615 Rewrite_In_Raise_CE (N, Left);
2616 Set_Is_Static_Expression (N, Rstat);
2619 -- If the result is not static, then we won't in any case fold
2621 elsif not Rstat then
2622 Check_Non_Static_Context (Left);
2623 Check_Non_Static_Context (Right);
2627 -- Here the result is static, note that, unlike the normal processing
2628 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
2629 -- the right operand raises constraint error, that's because it is not
2630 -- significant if the left operand is decisive.
2632 Set_Is_Static_Expression (N);
2634 -- It does not matter if the right operand raises constraint error if
2635 -- it will not be evaluated. So deal specially with the cases where
2636 -- the right operand is not evaluated. Note that we will fold these
2637 -- cases even if the right operand is non-static, which is fine, but
2638 -- of course in these cases the result is not potentially static.
2640 Left_Int := Expr_Value (Left);
2642 if (Kind = N_And_Then and then Is_False (Left_Int))
2643 or else (Kind = N_Or_Else and Is_True (Left_Int))
2645 Fold_Uint (N, Left_Int, Rstat);
2649 -- If first operand not decisive, then it does matter if the right
2650 -- operand raises constraint error, since it will be evaluated, so
2651 -- we simply replace the node with the right operand. Note that this
2652 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
2653 -- (both are set to True in Right).
2655 if Raises_Constraint_Error (Right) then
2656 Rewrite_In_Raise_CE (N, Right);
2657 Check_Non_Static_Context (Left);
2661 -- Otherwise the result depends on the right operand
2663 Fold_Uint (N, Expr_Value (Right), Rstat);
2665 end Eval_Short_Circuit;
2671 -- Slices can never be static, so the only processing required is to
2672 -- check for non-static context if an explicit range is given.
2674 procedure Eval_Slice (N : Node_Id) is
2675 Drange : constant Node_Id := Discrete_Range (N);
2677 if Nkind (Drange) = N_Range then
2678 Check_Non_Static_Context (Low_Bound (Drange));
2679 Check_Non_Static_Context (High_Bound (Drange));
2682 -- A slice of the form A (subtype), when the subtype is the index of
2683 -- the type of A, is redundant, the slice can be replaced with A, and
2684 -- this is worth a warning.
2686 if Is_Entity_Name (Prefix (N)) then
2688 E : constant Entity_Id := Entity (Prefix (N));
2689 T : constant Entity_Id := Etype (E);
2691 if Ekind (E) = E_Constant
2692 and then Is_Array_Type (T)
2693 and then Is_Entity_Name (Drange)
2695 if Is_Entity_Name (Original_Node (First_Index (T)))
2696 and then Entity (Original_Node (First_Index (T)))
2699 if Warn_On_Redundant_Constructs then
2700 Error_Msg_N ("redundant slice denotes whole array?", N);
2703 -- The following might be a useful optimization ????
2705 -- Rewrite (N, New_Occurrence_Of (E, Sloc (N)));
2712 -------------------------
2713 -- Eval_String_Literal --
2714 -------------------------
2716 procedure Eval_String_Literal (N : Node_Id) is
2717 Typ : constant Entity_Id := Etype (N);
2718 Bas : constant Entity_Id := Base_Type (Typ);
2724 -- Nothing to do if error type (handles cases like default expressions
2725 -- or generics where we have not yet fully resolved the type)
2727 if Bas = Any_Type or else Bas = Any_String then
2731 -- String literals are static if the subtype is static (RM 4.9(2)), so
2732 -- reset the static expression flag (it was set unconditionally in
2733 -- Analyze_String_Literal) if the subtype is non-static. We tell if
2734 -- the subtype is static by looking at the lower bound.
2736 if Ekind (Typ) = E_String_Literal_Subtype then
2737 if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then
2738 Set_Is_Static_Expression (N, False);
2742 -- Here if Etype of string literal is normal Etype (not yet possible,
2743 -- but may be possible in future!)
2745 elsif not Is_OK_Static_Expression
2746 (Type_Low_Bound (Etype (First_Index (Typ))))
2748 Set_Is_Static_Expression (N, False);
2752 -- If original node was a type conversion, then result if non-static
2754 if Nkind (Original_Node (N)) = N_Type_Conversion then
2755 Set_Is_Static_Expression (N, False);
2759 -- Test for illegal Ada 95 cases. A string literal is illegal in
2760 -- Ada 95 if its bounds are outside the index base type and this
2761 -- index type is static. This can happen in only two ways. Either
2762 -- the string literal is too long, or it is null, and the lower
2763 -- bound is type'First. In either case it is the upper bound that
2764 -- is out of range of the index type.
2766 if Ada_Version >= Ada_95 then
2767 if Root_Type (Bas) = Standard_String
2769 Root_Type (Bas) = Standard_Wide_String
2771 Xtp := Standard_Positive;
2773 Xtp := Etype (First_Index (Bas));
2776 if Ekind (Typ) = E_String_Literal_Subtype then
2777 Lo := String_Literal_Low_Bound (Typ);
2779 Lo := Type_Low_Bound (Etype (First_Index (Typ)));
2782 Len := String_Length (Strval (N));
2784 if UI_From_Int (Len) > String_Type_Len (Bas) then
2785 Apply_Compile_Time_Constraint_Error
2786 (N, "string literal too long for}", CE_Length_Check_Failed,
2788 Typ => First_Subtype (Bas));
2791 and then not Is_Generic_Type (Xtp)
2793 Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp)))
2795 Apply_Compile_Time_Constraint_Error
2796 (N, "null string literal not allowed for}",
2797 CE_Length_Check_Failed,
2799 Typ => First_Subtype (Bas));
2802 end Eval_String_Literal;
2804 --------------------------
2805 -- Eval_Type_Conversion --
2806 --------------------------
2808 -- A type conversion is potentially static if its subtype mark is for a
2809 -- static scalar subtype, and its operand expression is potentially static
2812 procedure Eval_Type_Conversion (N : Node_Id) is
2813 Operand : constant Node_Id := Expression (N);
2814 Source_Type : constant Entity_Id := Etype (Operand);
2815 Target_Type : constant Entity_Id := Etype (N);
2820 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
2821 -- Returns true if type T is an integer type, or if it is a
2822 -- fixed-point type to be treated as an integer (i.e. the flag
2823 -- Conversion_OK is set on the conversion node).
2825 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
2826 -- Returns true if type T is a floating-point type, or if it is a
2827 -- fixed-point type that is not to be treated as an integer (i.e. the
2828 -- flag Conversion_OK is not set on the conversion node).
2830 ------------------------------
2831 -- To_Be_Treated_As_Integer --
2832 ------------------------------
2834 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
2838 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
2839 end To_Be_Treated_As_Integer;
2841 ---------------------------
2842 -- To_Be_Treated_As_Real --
2843 ---------------------------
2845 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
2848 Is_Floating_Point_Type (T)
2849 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
2850 end To_Be_Treated_As_Real;
2852 -- Start of processing for Eval_Type_Conversion
2855 -- Cannot fold if target type is non-static or if semantic error
2857 if not Is_Static_Subtype (Target_Type) then
2858 Check_Non_Static_Context (Operand);
2861 elsif Error_Posted (N) then
2865 -- If not foldable we are done
2867 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
2872 -- Don't try fold if target type has constraint error bounds
2874 elsif not Is_OK_Static_Subtype (Target_Type) then
2875 Set_Raises_Constraint_Error (N);
2879 -- Remaining processing depends on operand types. Note that in the
2880 -- following type test, fixed-point counts as real unless the flag
2881 -- Conversion_OK is set, in which case it counts as integer.
2883 -- Fold conversion, case of string type. The result is not static
2885 if Is_String_Type (Target_Type) then
2886 Fold_Str (N, Strval (Get_String_Val (Operand)), Static => False);
2890 -- Fold conversion, case of integer target type
2892 elsif To_Be_Treated_As_Integer (Target_Type) then
2897 -- Integer to integer conversion
2899 if To_Be_Treated_As_Integer (Source_Type) then
2900 Result := Expr_Value (Operand);
2902 -- Real to integer conversion
2905 Result := UR_To_Uint (Expr_Value_R (Operand));
2908 -- If fixed-point type (Conversion_OK must be set), then the
2909 -- result is logically an integer, but we must replace the
2910 -- conversion with the corresponding real literal, since the
2911 -- type from a semantic point of view is still fixed-point.
2913 if Is_Fixed_Point_Type (Target_Type) then
2915 (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat);
2917 -- Otherwise result is integer literal
2920 Fold_Uint (N, Result, Stat);
2924 -- Fold conversion, case of real target type
2926 elsif To_Be_Treated_As_Real (Target_Type) then
2931 if To_Be_Treated_As_Real (Source_Type) then
2932 Result := Expr_Value_R (Operand);
2934 Result := UR_From_Uint (Expr_Value (Operand));
2937 Fold_Ureal (N, Result, Stat);
2940 -- Enumeration types
2943 Fold_Uint (N, Expr_Value (Operand), Stat);
2946 if Is_Out_Of_Range (N, Etype (N)) then
2950 end Eval_Type_Conversion;
2956 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
2957 -- are potentially static if the operand is potentially static (RM 4.9(7))
2959 procedure Eval_Unary_Op (N : Node_Id) is
2960 Right : constant Node_Id := Right_Opnd (N);
2965 -- If not foldable we are done
2967 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
2973 -- Fold for integer case
2975 if Is_Integer_Type (Etype (N)) then
2977 Rint : constant Uint := Expr_Value (Right);
2981 -- In the case of modular unary plus and abs there is no need
2982 -- to adjust the result of the operation since if the original
2983 -- operand was in bounds the result will be in the bounds of the
2984 -- modular type. However, in the case of modular unary minus the
2985 -- result may go out of the bounds of the modular type and needs
2988 if Nkind (N) = N_Op_Plus then
2991 elsif Nkind (N) = N_Op_Minus then
2992 if Is_Modular_Integer_Type (Etype (N)) then
2993 Result := (-Rint) mod Modulus (Etype (N));
2999 pragma Assert (Nkind (N) = N_Op_Abs);
3003 Fold_Uint (N, Result, Stat);
3006 -- Fold for real case
3008 elsif Is_Real_Type (Etype (N)) then
3010 Rreal : constant Ureal := Expr_Value_R (Right);
3014 if Nkind (N) = N_Op_Plus then
3017 elsif Nkind (N) = N_Op_Minus then
3018 Result := UR_Negate (Rreal);
3021 pragma Assert (Nkind (N) = N_Op_Abs);
3022 Result := abs Rreal;
3025 Fold_Ureal (N, Result, Stat);
3030 -------------------------------
3031 -- Eval_Unchecked_Conversion --
3032 -------------------------------
3034 -- Unchecked conversions can never be static, so the only required
3035 -- processing is to check for a non-static context for the operand.
3037 procedure Eval_Unchecked_Conversion (N : Node_Id) is
3039 Check_Non_Static_Context (Expression (N));
3040 end Eval_Unchecked_Conversion;
3042 --------------------
3043 -- Expr_Rep_Value --
3044 --------------------
3046 function Expr_Rep_Value (N : Node_Id) return Uint is
3047 Kind : constant Node_Kind := Nkind (N);
3051 if Is_Entity_Name (N) then
3054 -- An enumeration literal that was either in the source or
3055 -- created as a result of static evaluation.
3057 if Ekind (Ent) = E_Enumeration_Literal then
3058 return Enumeration_Rep (Ent);
3060 -- A user defined static constant
3063 pragma Assert (Ekind (Ent) = E_Constant);
3064 return Expr_Rep_Value (Constant_Value (Ent));
3067 -- An integer literal that was either in the source or created
3068 -- as a result of static evaluation.
3070 elsif Kind = N_Integer_Literal then
3073 -- A real literal for a fixed-point type. This must be the fixed-point
3074 -- case, either the literal is of a fixed-point type, or it is a bound
3075 -- of a fixed-point type, with type universal real. In either case we
3076 -- obtain the desired value from Corresponding_Integer_Value.
3078 elsif Kind = N_Real_Literal then
3079 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
3080 return Corresponding_Integer_Value (N);
3082 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
3084 elsif Kind = N_Attribute_Reference
3085 and then Attribute_Name (N) = Name_Null_Parameter
3089 -- Otherwise must be character literal
3092 pragma Assert (Kind = N_Character_Literal);
3095 -- Since Character literals of type Standard.Character don't
3096 -- have any defining character literals built for them, they
3097 -- do not have their Entity set, so just use their Char
3098 -- code. Otherwise for user-defined character literals use
3099 -- their Pos value as usual which is the same as the Rep value.
3102 return Char_Literal_Value (N);
3104 return Enumeration_Rep (Ent);
3113 function Expr_Value (N : Node_Id) return Uint is
3114 Kind : constant Node_Kind := Nkind (N);
3115 CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size);
3120 -- If already in cache, then we know it's compile time known and we can
3121 -- return the value that was previously stored in the cache since
3122 -- compile time known values cannot change.
3124 if CV_Ent.N = N then
3128 -- Otherwise proceed to test value
3130 if Is_Entity_Name (N) then
3133 -- An enumeration literal that was either in the source or
3134 -- created as a result of static evaluation.
3136 if Ekind (Ent) = E_Enumeration_Literal then
3137 Val := Enumeration_Pos (Ent);
3139 -- A user defined static constant
3142 pragma Assert (Ekind (Ent) = E_Constant);
3143 Val := Expr_Value (Constant_Value (Ent));
3146 -- An integer literal that was either in the source or created
3147 -- as a result of static evaluation.
3149 elsif Kind = N_Integer_Literal then
3152 -- A real literal for a fixed-point type. This must be the fixed-point
3153 -- case, either the literal is of a fixed-point type, or it is a bound
3154 -- of a fixed-point type, with type universal real. In either case we
3155 -- obtain the desired value from Corresponding_Integer_Value.
3157 elsif Kind = N_Real_Literal then
3159 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
3160 Val := Corresponding_Integer_Value (N);
3162 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
3164 elsif Kind = N_Attribute_Reference
3165 and then Attribute_Name (N) = Name_Null_Parameter
3169 -- Otherwise must be character literal
3172 pragma Assert (Kind = N_Character_Literal);
3175 -- Since Character literals of type Standard.Character don't
3176 -- have any defining character literals built for them, they
3177 -- do not have their Entity set, so just use their Char
3178 -- code. Otherwise for user-defined character literals use
3179 -- their Pos value as usual.
3182 Val := Char_Literal_Value (N);
3184 Val := Enumeration_Pos (Ent);
3188 -- Come here with Val set to value to be returned, set cache
3199 function Expr_Value_E (N : Node_Id) return Entity_Id is
3200 Ent : constant Entity_Id := Entity (N);
3203 if Ekind (Ent) = E_Enumeration_Literal then
3206 pragma Assert (Ekind (Ent) = E_Constant);
3207 return Expr_Value_E (Constant_Value (Ent));
3215 function Expr_Value_R (N : Node_Id) return Ureal is
3216 Kind : constant Node_Kind := Nkind (N);
3221 if Kind = N_Real_Literal then
3224 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
3226 pragma Assert (Ekind (Ent) = E_Constant);
3227 return Expr_Value_R (Constant_Value (Ent));
3229 elsif Kind = N_Integer_Literal then
3230 return UR_From_Uint (Expr_Value (N));
3232 -- Strange case of VAX literals, which are at this stage transformed
3233 -- into Vax_Type!x_To_y(IEEE_Literal). See Expand_N_Real_Literal in
3234 -- Exp_Vfpt for further details.
3236 elsif Vax_Float (Etype (N))
3237 and then Nkind (N) = N_Unchecked_Type_Conversion
3239 Expr := Expression (N);
3241 if Nkind (Expr) = N_Function_Call
3242 and then Present (Parameter_Associations (Expr))
3244 Expr := First (Parameter_Associations (Expr));
3246 if Nkind (Expr) = N_Real_Literal then
3247 return Realval (Expr);
3251 -- Peculiar VMS case, if we have xxx'Null_Parameter, return 0.0
3253 elsif Kind = N_Attribute_Reference
3254 and then Attribute_Name (N) = Name_Null_Parameter
3259 -- If we fall through, we have a node that cannot be interpreted
3260 -- as a compile time constant. That is definitely an error.
3262 raise Program_Error;
3269 function Expr_Value_S (N : Node_Id) return Node_Id is
3271 if Nkind (N) = N_String_Literal then
3274 pragma Assert (Ekind (Entity (N)) = E_Constant);
3275 return Expr_Value_S (Constant_Value (Entity (N)));
3279 --------------------------
3280 -- Flag_Non_Static_Expr --
3281 --------------------------
3283 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is
3285 if Error_Posted (Expr) and then not All_Errors_Mode then
3288 Error_Msg_F (Msg, Expr);
3289 Why_Not_Static (Expr);
3291 end Flag_Non_Static_Expr;
3297 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is
3298 Loc : constant Source_Ptr := Sloc (N);
3299 Typ : constant Entity_Id := Etype (N);
3302 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
3304 -- We now have the literal with the right value, both the actual type
3305 -- and the expected type of this literal are taken from the expression
3306 -- that was evaluated.
3309 Set_Is_Static_Expression (N, Static);
3318 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is
3319 Loc : constant Source_Ptr := Sloc (N);
3320 Typ : Entity_Id := Etype (N);
3324 -- If we are folding a named number, retain the entity in the
3325 -- literal, for ASIS use.
3327 if Is_Entity_Name (N)
3328 and then Ekind (Entity (N)) = E_Named_Integer
3335 if Is_Private_Type (Typ) then
3336 Typ := Full_View (Typ);
3339 -- For a result of type integer, substitute an N_Integer_Literal node
3340 -- for the result of the compile time evaluation of the expression.
3341 -- For ASIS use, set a link to the original named number when not in
3342 -- a generic context.
3344 if Is_Integer_Type (Typ) then
3345 Rewrite (N, Make_Integer_Literal (Loc, Val));
3347 Set_Original_Entity (N, Ent);
3349 -- Otherwise we have an enumeration type, and we substitute either
3350 -- an N_Identifier or N_Character_Literal to represent the enumeration
3351 -- literal corresponding to the given value, which must always be in
3352 -- range, because appropriate tests have already been made for this.
3354 else pragma Assert (Is_Enumeration_Type (Typ));
3355 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
3358 -- We now have the literal with the right value, both the actual type
3359 -- and the expected type of this literal are taken from the expression
3360 -- that was evaluated.
3363 Set_Is_Static_Expression (N, Static);
3372 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is
3373 Loc : constant Source_Ptr := Sloc (N);
3374 Typ : constant Entity_Id := Etype (N);
3378 -- If we are folding a named number, retain the entity in the
3379 -- literal, for ASIS use.
3381 if Is_Entity_Name (N)
3382 and then Ekind (Entity (N)) = E_Named_Real
3389 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
3391 -- Set link to original named number, for ASIS use
3393 Set_Original_Entity (N, Ent);
3395 -- Both the actual and expected type comes from the original expression
3398 Set_Is_Static_Expression (N, Static);
3407 function From_Bits (B : Bits; T : Entity_Id) return Uint is
3411 for J in 0 .. B'Last loop
3417 if Non_Binary_Modulus (T) then
3418 V := V mod Modulus (T);
3424 --------------------
3425 -- Get_String_Val --
3426 --------------------
3428 function Get_String_Val (N : Node_Id) return Node_Id is
3430 if Nkind (N) = N_String_Literal then
3433 elsif Nkind (N) = N_Character_Literal then
3437 pragma Assert (Is_Entity_Name (N));
3438 return Get_String_Val (Constant_Value (Entity (N)));
3446 procedure Initialize is
3448 CV_Cache := (others => (Node_High_Bound, Uint_0));
3451 --------------------
3452 -- In_Subrange_Of --
3453 --------------------
3455 function In_Subrange_Of
3458 Fixed_Int : Boolean := False) return Boolean
3467 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
3470 -- Never in range if both types are not scalar. Don't know if this can
3471 -- actually happen, but just in case.
3473 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T1) then
3477 L1 := Type_Low_Bound (T1);
3478 H1 := Type_High_Bound (T1);
3480 L2 := Type_Low_Bound (T2);
3481 H2 := Type_High_Bound (T2);
3483 -- Check bounds to see if comparison possible at compile time
3485 if Compile_Time_Compare (L1, L2) in Compare_GE
3487 Compile_Time_Compare (H1, H2) in Compare_LE
3492 -- If bounds not comparable at compile time, then the bounds of T2
3493 -- must be compile time known or we cannot answer the query.
3495 if not Compile_Time_Known_Value (L2)
3496 or else not Compile_Time_Known_Value (H2)
3501 -- If the bounds of T1 are know at compile time then use these
3502 -- ones, otherwise use the bounds of the base type (which are of
3503 -- course always static).
3505 if not Compile_Time_Known_Value (L1) then
3506 L1 := Type_Low_Bound (Base_Type (T1));
3509 if not Compile_Time_Known_Value (H1) then
3510 H1 := Type_High_Bound (Base_Type (T1));
3513 -- Fixed point types should be considered as such only if
3514 -- flag Fixed_Int is set to False.
3516 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
3517 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
3518 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
3521 Expr_Value_R (L2) <= Expr_Value_R (L1)
3523 Expr_Value_R (H2) >= Expr_Value_R (H1);
3527 Expr_Value (L2) <= Expr_Value (L1)
3529 Expr_Value (H2) >= Expr_Value (H1);
3534 -- If any exception occurs, it means that we have some bug in the compiler
3535 -- possibly triggered by a previous error, or by some unforeseen peculiar
3536 -- occurrence. However, this is only an optimization attempt, so there is
3537 -- really no point in crashing the compiler. Instead we just decide, too
3538 -- bad, we can't figure out the answer in this case after all.
3543 -- Debug flag K disables this behavior (useful for debugging)
3545 if Debug_Flag_K then
3556 function Is_In_Range
3559 Fixed_Int : Boolean := False;
3560 Int_Real : Boolean := False) return Boolean
3566 -- Universal types have no range limits, so always in range
3568 if Typ = Universal_Integer or else Typ = Universal_Real then
3571 -- Never in range if not scalar type. Don't know if this can
3572 -- actually happen, but our spec allows it, so we must check!
3574 elsif not Is_Scalar_Type (Typ) then
3577 -- Never in range unless we have a compile time known value
3579 elsif not Compile_Time_Known_Value (N) then
3584 Lo : constant Node_Id := Type_Low_Bound (Typ);
3585 Hi : constant Node_Id := Type_High_Bound (Typ);
3586 LB_Known : constant Boolean := Compile_Time_Known_Value (Lo);
3587 UB_Known : constant Boolean := Compile_Time_Known_Value (Hi);
3590 -- Fixed point types should be considered as such only in
3591 -- flag Fixed_Int is set to False.
3593 if Is_Floating_Point_Type (Typ)
3594 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
3597 Valr := Expr_Value_R (N);
3599 if LB_Known and then Valr >= Expr_Value_R (Lo)
3600 and then UB_Known and then Valr <= Expr_Value_R (Hi)
3608 Val := Expr_Value (N);
3610 if LB_Known and then Val >= Expr_Value (Lo)
3611 and then UB_Known and then Val <= Expr_Value (Hi)
3626 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
3627 Typ : constant Entity_Id := Etype (Lo);
3630 if not Compile_Time_Known_Value (Lo)
3631 or else not Compile_Time_Known_Value (Hi)
3636 if Is_Discrete_Type (Typ) then
3637 return Expr_Value (Lo) > Expr_Value (Hi);
3640 pragma Assert (Is_Real_Type (Typ));
3641 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
3645 -----------------------------
3646 -- Is_OK_Static_Expression --
3647 -----------------------------
3649 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
3651 return Is_Static_Expression (N)
3652 and then not Raises_Constraint_Error (N);
3653 end Is_OK_Static_Expression;
3655 ------------------------
3656 -- Is_OK_Static_Range --
3657 ------------------------
3659 -- A static range is a range whose bounds are static expressions, or a
3660 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
3661 -- We have already converted range attribute references, so we get the
3662 -- "or" part of this rule without needing a special test.
3664 function Is_OK_Static_Range (N : Node_Id) return Boolean is
3666 return Is_OK_Static_Expression (Low_Bound (N))
3667 and then Is_OK_Static_Expression (High_Bound (N));
3668 end Is_OK_Static_Range;
3670 --------------------------
3671 -- Is_OK_Static_Subtype --
3672 --------------------------
3674 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
3675 -- where neither bound raises constraint error when evaluated.
3677 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
3678 Base_T : constant Entity_Id := Base_Type (Typ);
3679 Anc_Subt : Entity_Id;
3682 -- First a quick check on the non static subtype flag. As described
3683 -- in further detail in Einfo, this flag is not decisive in all cases,
3684 -- but if it is set, then the subtype is definitely non-static.
3686 if Is_Non_Static_Subtype (Typ) then
3690 Anc_Subt := Ancestor_Subtype (Typ);
3692 if Anc_Subt = Empty then
3696 if Is_Generic_Type (Root_Type (Base_T))
3697 or else Is_Generic_Actual_Type (Base_T)
3703 elsif Is_String_Type (Typ) then
3705 Ekind (Typ) = E_String_Literal_Subtype
3707 (Is_OK_Static_Subtype (Component_Type (Typ))
3708 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
3712 elsif Is_Scalar_Type (Typ) then
3713 if Base_T = Typ then
3717 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so
3718 -- use Get_Type_Low,High_Bound.
3720 return Is_OK_Static_Subtype (Anc_Subt)
3721 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
3722 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
3725 -- Types other than string and scalar types are never static
3730 end Is_OK_Static_Subtype;
3732 ---------------------
3733 -- Is_Out_Of_Range --
3734 ---------------------
3736 function Is_Out_Of_Range
3739 Fixed_Int : Boolean := False;
3740 Int_Real : Boolean := False) return Boolean
3746 -- Universal types have no range limits, so always in range
3748 if Typ = Universal_Integer or else Typ = Universal_Real then
3751 -- Never out of range if not scalar type. Don't know if this can
3752 -- actually happen, but our spec allows it, so we must check!
3754 elsif not Is_Scalar_Type (Typ) then
3757 -- Never out of range if this is a generic type, since the bounds
3758 -- of generic types are junk. Note that if we only checked for
3759 -- static expressions (instead of compile time known values) below,
3760 -- we would not need this check, because values of a generic type
3761 -- can never be static, but they can be known at compile time.
3763 elsif Is_Generic_Type (Typ) then
3766 -- Never out of range unless we have a compile time known value
3768 elsif not Compile_Time_Known_Value (N) then
3773 Lo : constant Node_Id := Type_Low_Bound (Typ);
3774 Hi : constant Node_Id := Type_High_Bound (Typ);
3775 LB_Known : constant Boolean := Compile_Time_Known_Value (Lo);
3776 UB_Known : constant Boolean := Compile_Time_Known_Value (Hi);
3779 -- Real types (note that fixed-point types are not treated
3780 -- as being of a real type if the flag Fixed_Int is set,
3781 -- since in that case they are regarded as integer types).
3783 if Is_Floating_Point_Type (Typ)
3784 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
3787 Valr := Expr_Value_R (N);
3789 if LB_Known and then Valr < Expr_Value_R (Lo) then
3792 elsif UB_Known and then Expr_Value_R (Hi) < Valr then
3800 Val := Expr_Value (N);
3802 if LB_Known and then Val < Expr_Value (Lo) then
3805 elsif UB_Known and then Expr_Value (Hi) < Val then
3814 end Is_Out_Of_Range;
3816 ---------------------
3817 -- Is_Static_Range --
3818 ---------------------
3820 -- A static range is a range whose bounds are static expressions, or a
3821 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
3822 -- We have already converted range attribute references, so we get the
3823 -- "or" part of this rule without needing a special test.
3825 function Is_Static_Range (N : Node_Id) return Boolean is
3827 return Is_Static_Expression (Low_Bound (N))
3828 and then Is_Static_Expression (High_Bound (N));
3829 end Is_Static_Range;
3831 -----------------------
3832 -- Is_Static_Subtype --
3833 -----------------------
3835 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
3837 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
3838 Base_T : constant Entity_Id := Base_Type (Typ);
3839 Anc_Subt : Entity_Id;
3842 -- First a quick check on the non static subtype flag. As described
3843 -- in further detail in Einfo, this flag is not decisive in all cases,
3844 -- but if it is set, then the subtype is definitely non-static.
3846 if Is_Non_Static_Subtype (Typ) then
3850 Anc_Subt := Ancestor_Subtype (Typ);
3852 if Anc_Subt = Empty then
3856 if Is_Generic_Type (Root_Type (Base_T))
3857 or else Is_Generic_Actual_Type (Base_T)
3863 elsif Is_String_Type (Typ) then
3865 Ekind (Typ) = E_String_Literal_Subtype
3867 (Is_Static_Subtype (Component_Type (Typ))
3868 and then Is_Static_Subtype (Etype (First_Index (Typ))));
3872 elsif Is_Scalar_Type (Typ) then
3873 if Base_T = Typ then
3877 return Is_Static_Subtype (Anc_Subt)
3878 and then Is_Static_Expression (Type_Low_Bound (Typ))
3879 and then Is_Static_Expression (Type_High_Bound (Typ));
3882 -- Types other than string and scalar types are never static
3887 end Is_Static_Subtype;
3889 --------------------
3890 -- Not_Null_Range --
3891 --------------------
3893 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
3894 Typ : constant Entity_Id := Etype (Lo);
3897 if not Compile_Time_Known_Value (Lo)
3898 or else not Compile_Time_Known_Value (Hi)
3903 if Is_Discrete_Type (Typ) then
3904 return Expr_Value (Lo) <= Expr_Value (Hi);
3907 pragma Assert (Is_Real_Type (Typ));
3909 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
3917 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
3919 -- We allow a maximum of 500,000 bits which seems a reasonable limit
3921 if Bits < 500_000 then
3925 Error_Msg_N ("static value too large, capacity exceeded", N);
3934 procedure Out_Of_Range (N : Node_Id) is
3936 -- If we have the static expression case, then this is an illegality
3937 -- in Ada 95 mode, except that in an instance, we never generate an
3938 -- error (if the error is legitimate, it was already diagnosed in
3939 -- the template). The expression to compute the length of a packed
3940 -- array is attached to the array type itself, and deserves a separate
3943 if Is_Static_Expression (N)
3944 and then not In_Instance
3945 and then not In_Inlined_Body
3946 and then Ada_Version >= Ada_95
3948 if Nkind (Parent (N)) = N_Defining_Identifier
3949 and then Is_Array_Type (Parent (N))
3950 and then Present (Packed_Array_Type (Parent (N)))
3951 and then Present (First_Rep_Item (Parent (N)))
3954 ("length of packed array must not exceed Integer''Last",
3955 First_Rep_Item (Parent (N)));
3956 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
3959 Apply_Compile_Time_Constraint_Error
3960 (N, "value not in range of}", CE_Range_Check_Failed);
3963 -- Here we generate a warning for the Ada 83 case, or when we are
3964 -- in an instance, or when we have a non-static expression case.
3967 Apply_Compile_Time_Constraint_Error
3968 (N, "value not in range of}?", CE_Range_Check_Failed);
3972 -------------------------
3973 -- Rewrite_In_Raise_CE --
3974 -------------------------
3976 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
3977 Typ : constant Entity_Id := Etype (N);
3980 -- If we want to raise CE in the condition of a raise_CE node
3981 -- we may as well get rid of the condition
3983 if Present (Parent (N))
3984 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
3986 Set_Condition (Parent (N), Empty);
3988 -- If the expression raising CE is a N_Raise_CE node, we can use
3989 -- that one. We just preserve the type of the context
3991 elsif Nkind (Exp) = N_Raise_Constraint_Error then
3995 -- We have to build an explicit raise_ce node
3999 Make_Raise_Constraint_Error (Sloc (Exp),
4000 Reason => CE_Range_Check_Failed));
4001 Set_Raises_Constraint_Error (N);
4004 end Rewrite_In_Raise_CE;
4006 ---------------------
4007 -- String_Type_Len --
4008 ---------------------
4010 function String_Type_Len (Stype : Entity_Id) return Uint is
4011 NT : constant Entity_Id := Etype (First_Index (Stype));
4015 if Is_OK_Static_Subtype (NT) then
4018 T := Base_Type (NT);
4021 return Expr_Value (Type_High_Bound (T)) -
4022 Expr_Value (Type_Low_Bound (T)) + 1;
4023 end String_Type_Len;
4025 ------------------------------------
4026 -- Subtypes_Statically_Compatible --
4027 ------------------------------------
4029 function Subtypes_Statically_Compatible
4031 T2 : Entity_Id) return Boolean
4034 if Is_Scalar_Type (T1) then
4036 -- Definitely compatible if we match
4038 if Subtypes_Statically_Match (T1, T2) then
4041 -- If either subtype is nonstatic then they're not compatible
4043 elsif not Is_Static_Subtype (T1)
4044 or else not Is_Static_Subtype (T2)
4048 -- If either type has constraint error bounds, then consider that
4049 -- they match to avoid junk cascaded errors here.
4051 elsif not Is_OK_Static_Subtype (T1)
4052 or else not Is_OK_Static_Subtype (T2)
4056 -- Base types must match, but we don't check that (should
4057 -- we???) but we do at least check that both types are
4058 -- real, or both types are not real.
4060 elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then
4063 -- Here we check the bounds
4067 LB1 : constant Node_Id := Type_Low_Bound (T1);
4068 HB1 : constant Node_Id := Type_High_Bound (T1);
4069 LB2 : constant Node_Id := Type_Low_Bound (T2);
4070 HB2 : constant Node_Id := Type_High_Bound (T2);
4073 if Is_Real_Type (T1) then
4075 (Expr_Value_R (LB1) > Expr_Value_R (HB1))
4077 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
4079 Expr_Value_R (HB1) <= Expr_Value_R (HB2));
4083 (Expr_Value (LB1) > Expr_Value (HB1))
4085 (Expr_Value (LB2) <= Expr_Value (LB1)
4087 Expr_Value (HB1) <= Expr_Value (HB2));
4092 elsif Is_Access_Type (T1) then
4093 return not Is_Constrained (T2)
4094 or else Subtypes_Statically_Match
4095 (Designated_Type (T1), Designated_Type (T2));
4098 return (Is_Composite_Type (T1) and then not Is_Constrained (T2))
4099 or else Subtypes_Statically_Match (T1, T2);
4101 end Subtypes_Statically_Compatible;
4103 -------------------------------
4104 -- Subtypes_Statically_Match --
4105 -------------------------------
4107 -- Subtypes statically match if they have statically matching constraints
4108 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
4109 -- they are the same identical constraint, or if they are static and the
4110 -- values match (RM 4.9.1(1)).
4112 function Subtypes_Statically_Match (T1, T2 : Entity_Id) return Boolean is
4114 -- A type always statically matches itself
4121 elsif Is_Scalar_Type (T1) then
4123 -- Base types must be the same
4125 if Base_Type (T1) /= Base_Type (T2) then
4129 -- A constrained numeric subtype never matches an unconstrained
4130 -- subtype, i.e. both types must be constrained or unconstrained.
4132 -- To understand the requirement for this test, see RM 4.9.1(1).
4133 -- As is made clear in RM 3.5.4(11), type Integer, for example
4134 -- is a constrained subtype with constraint bounds matching the
4135 -- bounds of its corresponding unconstrained base type. In this
4136 -- situation, Integer and Integer'Base do not statically match,
4137 -- even though they have the same bounds.
4139 -- We only apply this test to types in Standard and types that
4140 -- appear in user programs. That way, we do not have to be
4141 -- too careful about setting Is_Constrained right for itypes.
4143 if Is_Numeric_Type (T1)
4144 and then (Is_Constrained (T1) /= Is_Constrained (T2))
4145 and then (Scope (T1) = Standard_Standard
4146 or else Comes_From_Source (T1))
4147 and then (Scope (T2) = Standard_Standard
4148 or else Comes_From_Source (T2))
4152 -- A generic scalar type does not statically match its base
4153 -- type (AI-311). In this case we make sure that the formals,
4154 -- which are first subtypes of their bases, are constrained.
4156 elsif Is_Generic_Type (T1)
4157 and then Is_Generic_Type (T2)
4158 and then (Is_Constrained (T1) /= Is_Constrained (T2))
4163 -- If there was an error in either range, then just assume
4164 -- the types statically match to avoid further junk errors
4166 if Error_Posted (Scalar_Range (T1))
4168 Error_Posted (Scalar_Range (T2))
4173 -- Otherwise both types have bound that can be compared
4176 LB1 : constant Node_Id := Type_Low_Bound (T1);
4177 HB1 : constant Node_Id := Type_High_Bound (T1);
4178 LB2 : constant Node_Id := Type_Low_Bound (T2);
4179 HB2 : constant Node_Id := Type_High_Bound (T2);
4182 -- If the bounds are the same tree node, then match
4184 if LB1 = LB2 and then HB1 = HB2 then
4187 -- Otherwise bounds must be static and identical value
4190 if not Is_Static_Subtype (T1)
4191 or else not Is_Static_Subtype (T2)
4195 -- If either type has constraint error bounds, then say
4196 -- that they match to avoid junk cascaded errors here.
4198 elsif not Is_OK_Static_Subtype (T1)
4199 or else not Is_OK_Static_Subtype (T2)
4203 elsif Is_Real_Type (T1) then
4205 (Expr_Value_R (LB1) = Expr_Value_R (LB2))
4207 (Expr_Value_R (HB1) = Expr_Value_R (HB2));
4211 Expr_Value (LB1) = Expr_Value (LB2)
4213 Expr_Value (HB1) = Expr_Value (HB2);
4218 -- Type with discriminants
4220 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
4222 -- Because of view exchanges in multiple instantiations, conformance
4223 -- checking might try to match a partial view of a type with no
4224 -- discriminants with a full view that has defaulted discriminants.
4225 -- In such a case, use the discriminant constraint of the full view,
4226 -- which must exist because we know that the two subtypes have the
4229 if Has_Discriminants (T1) /= Has_Discriminants (T2) then
4231 if Is_Private_Type (T2)
4232 and then Present (Full_View (T2))
4233 and then Has_Discriminants (Full_View (T2))
4235 return Subtypes_Statically_Match (T1, Full_View (T2));
4237 elsif Is_Private_Type (T1)
4238 and then Present (Full_View (T1))
4239 and then Has_Discriminants (Full_View (T1))
4241 return Subtypes_Statically_Match (Full_View (T1), T2);
4252 DL1 : constant Elist_Id := Discriminant_Constraint (T1);
4253 DL2 : constant Elist_Id := Discriminant_Constraint (T2);
4261 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
4265 -- Now loop through the discriminant constraints
4267 -- Note: the guard here seems necessary, since it is possible at
4268 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
4270 if Present (DL1) and then Present (DL2) then
4271 DA1 := First_Elmt (DL1);
4272 DA2 := First_Elmt (DL2);
4273 while Present (DA1) loop
4275 Expr1 : constant Node_Id := Node (DA1);
4276 Expr2 : constant Node_Id := Node (DA2);
4279 if not Is_Static_Expression (Expr1)
4280 or else not Is_Static_Expression (Expr2)
4284 -- If either expression raised a constraint error,
4285 -- consider the expressions as matching, since this
4286 -- helps to prevent cascading errors.
4288 elsif Raises_Constraint_Error (Expr1)
4289 or else Raises_Constraint_Error (Expr2)
4293 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
4306 -- A definite type does not match an indefinite or classwide type
4307 -- However, a generic type with unknown discriminants may be
4308 -- instantiated with a type with no discriminants, and conformance
4309 -- checking on an inherited operation may compare the actual with
4310 -- the subtype that renames it in the instance.
4313 Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
4316 Is_Generic_Actual_Type (T1) or else Is_Generic_Actual_Type (T2);
4320 elsif Is_Array_Type (T1) then
4322 -- If either subtype is unconstrained then both must be,
4323 -- and if both are unconstrained then no further checking
4326 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
4327 return not (Is_Constrained (T1) or else Is_Constrained (T2));
4330 -- Both subtypes are constrained, so check that the index
4331 -- subtypes statically match.
4334 Index1 : Node_Id := First_Index (T1);
4335 Index2 : Node_Id := First_Index (T2);
4338 while Present (Index1) loop
4340 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
4345 Next_Index (Index1);
4346 Next_Index (Index2);
4352 elsif Is_Access_Type (T1) then
4353 if Can_Never_Be_Null (T1) /= Can_Never_Be_Null (T2) then
4356 elsif Ekind (T1) = E_Access_Subprogram_Type
4357 or else Ekind (T1) = E_Anonymous_Access_Subprogram_Type
4361 (Designated_Type (T1),
4362 Designated_Type (T2));
4365 Subtypes_Statically_Match
4366 (Designated_Type (T1),
4367 Designated_Type (T2))
4368 and then Is_Access_Constant (T1) = Is_Access_Constant (T2);
4371 -- All other types definitely match
4376 end Subtypes_Statically_Match;
4382 function Test (Cond : Boolean) return Uint is
4391 ---------------------------------
4392 -- Test_Expression_Is_Foldable --
4393 ---------------------------------
4397 procedure Test_Expression_Is_Foldable
4407 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
4411 -- If operand is Any_Type, just propagate to result and do not
4412 -- try to fold, this prevents cascaded errors.
4414 if Etype (Op1) = Any_Type then
4415 Set_Etype (N, Any_Type);
4418 -- If operand raises constraint error, then replace node N with the
4419 -- raise constraint error node, and we are obviously not foldable.
4420 -- Note that this replacement inherits the Is_Static_Expression flag
4421 -- from the operand.
4423 elsif Raises_Constraint_Error (Op1) then
4424 Rewrite_In_Raise_CE (N, Op1);
4427 -- If the operand is not static, then the result is not static, and
4428 -- all we have to do is to check the operand since it is now known
4429 -- to appear in a non-static context.
4431 elsif not Is_Static_Expression (Op1) then
4432 Check_Non_Static_Context (Op1);
4433 Fold := Compile_Time_Known_Value (Op1);
4436 -- An expression of a formal modular type is not foldable because
4437 -- the modulus is unknown.
4439 elsif Is_Modular_Integer_Type (Etype (Op1))
4440 and then Is_Generic_Type (Etype (Op1))
4442 Check_Non_Static_Context (Op1);
4445 -- Here we have the case of an operand whose type is OK, which is
4446 -- static, and which does not raise constraint error, we can fold.
4449 Set_Is_Static_Expression (N);
4453 end Test_Expression_Is_Foldable;
4457 procedure Test_Expression_Is_Foldable
4464 Rstat : constant Boolean := Is_Static_Expression (Op1)
4465 and then Is_Static_Expression (Op2);
4471 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
4475 -- If either operand is Any_Type, just propagate to result and
4476 -- do not try to fold, this prevents cascaded errors.
4478 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
4479 Set_Etype (N, Any_Type);
4482 -- If left operand raises constraint error, then replace node N with
4483 -- the raise constraint error node, and we are obviously not foldable.
4484 -- Is_Static_Expression is set from the two operands in the normal way,
4485 -- and we check the right operand if it is in a non-static context.
4487 elsif Raises_Constraint_Error (Op1) then
4489 Check_Non_Static_Context (Op2);
4492 Rewrite_In_Raise_CE (N, Op1);
4493 Set_Is_Static_Expression (N, Rstat);
4496 -- Similar processing for the case of the right operand. Note that
4497 -- we don't use this routine for the short-circuit case, so we do
4498 -- not have to worry about that special case here.
4500 elsif Raises_Constraint_Error (Op2) then
4502 Check_Non_Static_Context (Op1);
4505 Rewrite_In_Raise_CE (N, Op2);
4506 Set_Is_Static_Expression (N, Rstat);
4509 -- Exclude expressions of a generic modular type, as above
4511 elsif Is_Modular_Integer_Type (Etype (Op1))
4512 and then Is_Generic_Type (Etype (Op1))
4514 Check_Non_Static_Context (Op1);
4517 -- If result is not static, then check non-static contexts on operands
4518 -- since one of them may be static and the other one may not be static
4520 elsif not Rstat then
4521 Check_Non_Static_Context (Op1);
4522 Check_Non_Static_Context (Op2);
4523 Fold := Compile_Time_Known_Value (Op1)
4524 and then Compile_Time_Known_Value (Op2);
4527 -- Else result is static and foldable. Both operands are static,
4528 -- and neither raises constraint error, so we can definitely fold.
4531 Set_Is_Static_Expression (N);
4536 end Test_Expression_Is_Foldable;
4542 procedure To_Bits (U : Uint; B : out Bits) is
4544 for J in 0 .. B'Last loop
4545 B (J) := (U / (2 ** J)) mod 2 /= 0;
4549 --------------------
4550 -- Why_Not_Static --
4551 --------------------
4553 procedure Why_Not_Static (Expr : Node_Id) is
4554 N : constant Node_Id := Original_Node (Expr);
4558 procedure Why_Not_Static_List (L : List_Id);
4559 -- A version that can be called on a list of expressions. Finds
4560 -- all non-static violations in any element of the list.
4562 -------------------------
4563 -- Why_Not_Static_List --
4564 -------------------------
4566 procedure Why_Not_Static_List (L : List_Id) is
4570 if Is_Non_Empty_List (L) then
4572 while Present (N) loop
4577 end Why_Not_Static_List;
4579 -- Start of processing for Why_Not_Static
4582 -- If in ACATS mode (debug flag 2), then suppress all these
4583 -- messages, this avoids massive updates to the ACATS base line.
4585 if Debug_Flag_2 then
4589 -- Ignore call on error or empty node
4591 if No (Expr) or else Nkind (Expr) = N_Error then
4595 -- Preprocessing for sub expressions
4597 if Nkind (Expr) in N_Subexpr then
4599 -- Nothing to do if expression is static
4601 if Is_OK_Static_Expression (Expr) then
4605 -- Test for constraint error raised
4607 if Raises_Constraint_Error (Expr) then
4609 ("expression raises exception, cannot be static " &
4610 "(RM 4.9(34))!", N);
4614 -- If no type, then something is pretty wrong, so ignore
4616 Typ := Etype (Expr);
4622 -- Type must be scalar or string type
4624 if not Is_Scalar_Type (Typ)
4625 and then not Is_String_Type (Typ)
4628 ("static expression must have scalar or string type " &
4634 -- If we got through those checks, test particular node kind
4637 when N_Expanded_Name | N_Identifier | N_Operator_Symbol =>
4640 if Is_Named_Number (E) then
4643 elsif Ekind (E) = E_Constant then
4644 if not Is_Static_Expression (Constant_Value (E)) then
4646 ("& is not a static constant (RM 4.9(5))!", N, E);
4651 ("& is not static constant or named number " &
4652 "(RM 4.9(5))!", N, E);
4655 when N_Binary_Op | N_And_Then | N_Or_Else | N_Membership_Test =>
4656 if Nkind (N) in N_Op_Shift then
4658 ("shift functions are never static (RM 4.9(6,18))!", N);
4661 Why_Not_Static (Left_Opnd (N));
4662 Why_Not_Static (Right_Opnd (N));
4666 Why_Not_Static (Right_Opnd (N));
4668 when N_Attribute_Reference =>
4669 Why_Not_Static_List (Expressions (N));
4671 E := Etype (Prefix (N));
4673 if E = Standard_Void_Type then
4677 -- Special case non-scalar'Size since this is a common error
4679 if Attribute_Name (N) = Name_Size then
4681 ("size attribute is only static for scalar type " &
4682 "(RM 4.9(7,8))", N);
4686 elsif Is_Array_Type (E) then
4687 if Attribute_Name (N) /= Name_First
4689 Attribute_Name (N) /= Name_Last
4691 Attribute_Name (N) /= Name_Length
4694 ("static array attribute must be Length, First, or Last " &
4697 -- Since we know the expression is not-static (we already
4698 -- tested for this, must mean array is not static).
4702 ("prefix is non-static array (RM 4.9(8))!", Prefix (N));
4707 -- Special case generic types, since again this is a common
4708 -- source of confusion.
4710 elsif Is_Generic_Actual_Type (E)
4715 ("attribute of generic type is never static " &
4716 "(RM 4.9(7,8))!", N);
4718 elsif Is_Static_Subtype (E) then
4721 elsif Is_Scalar_Type (E) then
4723 ("prefix type for attribute is not static scalar subtype " &
4728 ("static attribute must apply to array/scalar type " &
4729 "(RM 4.9(7,8))!", N);
4732 when N_String_Literal =>
4734 ("subtype of string literal is non-static (RM 4.9(4))!", N);
4736 when N_Explicit_Dereference =>
4738 ("explicit dereference is never static (RM 4.9)!", N);
4740 when N_Function_Call =>
4741 Why_Not_Static_List (Parameter_Associations (N));
4742 Error_Msg_N ("non-static function call (RM 4.9(6,18))!", N);
4744 when N_Parameter_Association =>
4745 Why_Not_Static (Explicit_Actual_Parameter (N));
4747 when N_Indexed_Component =>
4749 ("indexed component is never static (RM 4.9)!", N);
4751 when N_Procedure_Call_Statement =>
4753 ("procedure call is never static (RM 4.9)!", N);
4755 when N_Qualified_Expression =>
4756 Why_Not_Static (Expression (N));
4758 when N_Aggregate | N_Extension_Aggregate =>
4760 ("an aggregate is never static (RM 4.9)!", N);
4763 Why_Not_Static (Low_Bound (N));
4764 Why_Not_Static (High_Bound (N));
4766 when N_Range_Constraint =>
4767 Why_Not_Static (Range_Expression (N));
4769 when N_Subtype_Indication =>
4770 Why_Not_Static (Constraint (N));
4772 when N_Selected_Component =>
4774 ("selected component is never static (RM 4.9)!", N);
4778 ("slice is never static (RM 4.9)!", N);
4780 when N_Type_Conversion =>
4781 Why_Not_Static (Expression (N));
4783 if not Is_Scalar_Type (Etype (Prefix (N)))
4784 or else not Is_Static_Subtype (Etype (Prefix (N)))
4787 ("static conversion requires static scalar subtype result " &
4791 when N_Unchecked_Type_Conversion =>
4793 ("unchecked type conversion is never static (RM 4.9)!", N);