1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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 immediatedly 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 (Ekind (Entity (Lf)) = E_Constant or else
582 Ekind (Entity (Lf)) = E_In_Parameter or else
583 Ekind (Entity (Lf)) = E_Loop_Parameter)
587 -- Or if they are compile time known and identical
589 elsif Compile_Time_Known_Value (Lf)
591 Compile_Time_Known_Value (Rf)
592 and then Expr_Value (Lf) = Expr_Value (Rf)
596 -- Or if they are both 'First or 'Last values applying to the
597 -- same entity (first and last don't change even if value does)
599 elsif Nkind (Lf) = N_Attribute_Reference
601 Nkind (Rf) = N_Attribute_Reference
602 and then Attribute_Name (Lf) = Attribute_Name (Rf)
603 and then (Attribute_Name (Lf) = Name_First
605 Attribute_Name (Lf) = Name_Last)
606 and then Is_Entity_Name (Prefix (Lf))
607 and then Is_Entity_Name (Prefix (Rf))
608 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
609 and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf))
613 -- All other cases, we can't tell
620 -- Start of processing for Compile_Time_Compare
623 -- If either operand could raise constraint error, then we cannot
624 -- know the result at compile time (since CE may be raised!)
626 if not (Cannot_Raise_Constraint_Error (L)
628 Cannot_Raise_Constraint_Error (R))
633 -- Identical operands are most certainly equal
638 -- If expressions have no types, then do not attempt to determine
639 -- if they are the same, since something funny is going on. One
640 -- case in which this happens is during generic template analysis,
641 -- when bounds are not fully analyzed.
643 elsif No (Ltyp) or else No (Rtyp) then
646 -- We only attempt compile time analysis for scalar values, and
647 -- not for packed arrays represented as modular types, where the
648 -- semantics of comparison is quite different.
650 elsif not Is_Scalar_Type (Ltyp)
651 or else Is_Packed_Array_Type (Ltyp)
655 -- Case where comparison involves two compile time known values
657 elsif Compile_Time_Known_Value (L)
658 and then Compile_Time_Known_Value (R)
660 -- For the floating-point case, we have to be a little careful, since
661 -- at compile time we are dealing with universal exact values, but at
662 -- runtime, these will be in non-exact target form. That's why the
663 -- returned results are LE and GE below instead of LT and GT.
665 if Is_Floating_Point_Type (Ltyp)
667 Is_Floating_Point_Type (Rtyp)
670 Lo : constant Ureal := Expr_Value_R (L);
671 Hi : constant Ureal := Expr_Value_R (R);
683 -- For the integer case we know exactly (note that this includes the
684 -- fixed-point case, where we know the run time integer values now)
688 Lo : constant Uint := Expr_Value (L);
689 Hi : constant Uint := Expr_Value (R);
702 -- Cases where at least one operand is not known at compile time
705 -- Remaining checks apply only for non-generic discrete types
707 if not Is_Discrete_Type (Ltyp)
708 or else not Is_Discrete_Type (Rtyp)
709 or else Is_Generic_Type (Ltyp)
710 or else Is_Generic_Type (Rtyp)
715 -- Here is where we check for comparisons against maximum bounds of
716 -- types, where we know that no value can be outside the bounds of
717 -- the subtype. Note that this routine is allowed to assume that all
718 -- expressions are within their subtype bounds. Callers wishing to
719 -- deal with possibly invalid values must in any case take special
720 -- steps (e.g. conversions to larger types) to avoid this kind of
721 -- optimization, which is always considered to be valid. We do not
722 -- attempt this optimization with generic types, since the type
723 -- bounds may not be meaningful in this case.
725 -- We are in danger of an infinite recursion here. It does not seem
726 -- useful to go more than one level deep, so the parameter Rec is
727 -- used to protect ourselves against this infinite recursion.
731 -- See if we can get a decisive check against one operand and
732 -- a bound of the other operand (four possible tests here).
734 case Compile_Time_Compare (L, Type_Low_Bound (Rtyp), True) is
735 when LT => return LT;
736 when LE => return LE;
737 when EQ => return LE;
741 case Compile_Time_Compare (L, Type_High_Bound (Rtyp), True) is
742 when GT => return GT;
743 when GE => return GE;
744 when EQ => return GE;
748 case Compile_Time_Compare (Type_Low_Bound (Ltyp), R, True) is
749 when GT => return GT;
750 when GE => return GE;
751 when EQ => return GE;
755 case Compile_Time_Compare (Type_High_Bound (Ltyp), R, True) is
756 when LT => return LT;
757 when LE => return LE;
758 when EQ => return LE;
763 -- Next attempt is to decompose the expressions to extract
764 -- a constant offset resulting from the use of any of the forms:
771 -- Then we see if the two expressions are the same value, and if so
772 -- the result is obtained by comparing the offsets.
781 Compare_Decompose (L, Lnode, Loffs);
782 Compare_Decompose (R, Rnode, Roffs);
784 if Is_Same_Value (Lnode, Rnode) then
785 if Loffs = Roffs then
788 elsif Loffs < Roffs then
797 -- Next attempt is to see if we have an entity compared with a
798 -- compile time known value, where there is a current value
799 -- conditional for the entity which can tell us the result.
803 -- Entity variable (left operand)
806 -- Value (right operand)
809 -- If False, we have reversed the operands
812 -- Comparison operator kind from Get_Current_Value_Condition call
815 -- Value from Get_Current_Value_Condition call
820 Result : Compare_Result;
821 -- Known result before inversion
824 if Is_Entity_Name (L)
825 and then Compile_Time_Known_Value (R)
828 Val := Expr_Value (R);
831 elsif Is_Entity_Name (R)
832 and then Compile_Time_Known_Value (L)
835 Val := Expr_Value (L);
838 -- That was the last chance at finding a compile time result
844 Get_Current_Value_Condition (Var, Op, Opn);
846 -- That was the last chance, so if we got nothing return
852 Opv := Expr_Value (Opn);
854 -- We got a comparison, so we might have something interesting
856 -- Convert LE to LT and GE to GT, just so we have fewer cases
861 elsif Op = N_Op_Ge then
866 -- Deal with equality case
877 -- Deal with inequality case
879 elsif Op = N_Op_Ne then
886 -- Deal with greater than case
888 elsif Op = N_Op_Gt then
891 elsif Opv = Val - 1 then
897 -- Deal with less than case
899 else pragma Assert (Op = N_Op_Lt);
902 elsif Opv = Val + 1 then
909 -- Deal with inverting result
913 when GT => return LT;
914 when GE => return LE;
915 when LT => return GT;
916 when LE => return GE;
917 when others => return Result;
924 end Compile_Time_Compare;
926 -------------------------------
927 -- Compile_Time_Known_Bounds --
928 -------------------------------
930 function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean is
935 if not Is_Array_Type (T) then
939 Indx := First_Index (T);
940 while Present (Indx) loop
941 Typ := Underlying_Type (Etype (Indx));
942 if not Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
944 elsif not Compile_Time_Known_Value (Type_High_Bound (Typ)) then
952 end Compile_Time_Known_Bounds;
954 ------------------------------
955 -- Compile_Time_Known_Value --
956 ------------------------------
958 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
959 K : constant Node_Kind := Nkind (Op);
960 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
963 -- Never known at compile time if bad type or raises constraint error
964 -- or empty (latter case occurs only as a result of a previous error)
968 or else Etype (Op) = Any_Type
969 or else Raises_Constraint_Error (Op)
974 -- If this is not a static expression and we are in configurable run
975 -- time mode, then we consider it not known at compile time. This
976 -- avoids anomalies where whether something is permitted with a given
977 -- configurable run-time library depends on how good the compiler is
978 -- at optimizing and knowing that things are constant when they
981 if Configurable_Run_Time_Mode and then not Is_Static_Expression (Op) then
985 -- If we have an entity name, then see if it is the name of a constant
986 -- and if so, test the corresponding constant value, or the name of
987 -- an enumeration literal, which is always a constant.
989 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
991 E : constant Entity_Id := Entity (Op);
995 -- Never known at compile time if it is a packed array value.
996 -- We might want to try to evaluate these at compile time one
997 -- day, but we do not make that attempt now.
999 if Is_Packed_Array_Type (Etype (Op)) then
1003 if Ekind (E) = E_Enumeration_Literal then
1006 elsif Ekind (E) = E_Constant then
1007 V := Constant_Value (E);
1008 return Present (V) and then Compile_Time_Known_Value (V);
1012 -- We have a value, see if it is compile time known
1015 -- Integer literals are worth storing in the cache
1017 if K = N_Integer_Literal then
1019 CV_Ent.V := Intval (Op);
1022 -- Other literals and NULL are known at compile time
1025 K = N_Character_Literal
1029 K = N_String_Literal
1035 -- Any reference to Null_Parameter is known at compile time. No
1036 -- other attribute references (that have not already been folded)
1037 -- are known at compile time.
1039 elsif K = N_Attribute_Reference then
1040 return Attribute_Name (Op) = Name_Null_Parameter;
1044 -- If we fall through, not known at compile time
1048 -- If we get an exception while trying to do this test, then some error
1049 -- has occurred, and we simply say that the value is not known after all
1054 end Compile_Time_Known_Value;
1056 --------------------------------------
1057 -- Compile_Time_Known_Value_Or_Aggr --
1058 --------------------------------------
1060 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is
1062 -- If we have an entity name, then see if it is the name of a constant
1063 -- and if so, test the corresponding constant value, or the name of
1064 -- an enumeration literal, which is always a constant.
1066 if Is_Entity_Name (Op) then
1068 E : constant Entity_Id := Entity (Op);
1072 if Ekind (E) = E_Enumeration_Literal then
1075 elsif Ekind (E) /= E_Constant then
1079 V := Constant_Value (E);
1081 and then Compile_Time_Known_Value_Or_Aggr (V);
1085 -- We have a value, see if it is compile time known
1088 if Compile_Time_Known_Value (Op) then
1091 elsif Nkind (Op) = N_Aggregate then
1093 if Present (Expressions (Op)) then
1098 Expr := First (Expressions (Op));
1099 while Present (Expr) loop
1100 if not Compile_Time_Known_Value_Or_Aggr (Expr) then
1109 if Present (Component_Associations (Op)) then
1114 Cass := First (Component_Associations (Op));
1115 while Present (Cass) loop
1117 Compile_Time_Known_Value_Or_Aggr (Expression (Cass))
1129 -- All other types of values are not known at compile time
1136 end Compile_Time_Known_Value_Or_Aggr;
1142 -- This is only called for actuals of functions that are not predefined
1143 -- operators (which have already been rewritten as operators at this
1144 -- stage), so the call can never be folded, and all that needs doing for
1145 -- the actual is to do the check for a non-static context.
1147 procedure Eval_Actual (N : Node_Id) is
1149 Check_Non_Static_Context (N);
1152 --------------------
1153 -- Eval_Allocator --
1154 --------------------
1156 -- Allocators are never static, so all we have to do is to do the
1157 -- check for a non-static context if an expression is present.
1159 procedure Eval_Allocator (N : Node_Id) is
1160 Expr : constant Node_Id := Expression (N);
1163 if Nkind (Expr) = N_Qualified_Expression then
1164 Check_Non_Static_Context (Expression (Expr));
1168 ------------------------
1169 -- Eval_Arithmetic_Op --
1170 ------------------------
1172 -- Arithmetic operations are static functions, so the result is static
1173 -- if both operands are static (RM 4.9(7), 4.9(20)).
1175 procedure Eval_Arithmetic_Op (N : Node_Id) is
1176 Left : constant Node_Id := Left_Opnd (N);
1177 Right : constant Node_Id := Right_Opnd (N);
1178 Ltype : constant Entity_Id := Etype (Left);
1179 Rtype : constant Entity_Id := Etype (Right);
1184 -- If not foldable we are done
1186 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1192 -- Fold for cases where both operands are of integer type
1194 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
1196 Left_Int : constant Uint := Expr_Value (Left);
1197 Right_Int : constant Uint := Expr_Value (Right);
1204 Result := Left_Int + Right_Int;
1206 when N_Op_Subtract =>
1207 Result := Left_Int - Right_Int;
1209 when N_Op_Multiply =>
1212 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
1214 Result := Left_Int * Right_Int;
1221 -- The exception Constraint_Error is raised by integer
1222 -- division, rem and mod if the right operand is zero.
1224 if Right_Int = 0 then
1225 Apply_Compile_Time_Constraint_Error
1226 (N, "division by zero",
1232 Result := Left_Int / Right_Int;
1237 -- The exception Constraint_Error is raised by integer
1238 -- division, rem and mod if the right operand is zero.
1240 if Right_Int = 0 then
1241 Apply_Compile_Time_Constraint_Error
1242 (N, "mod with zero divisor",
1247 Result := Left_Int mod Right_Int;
1252 -- The exception Constraint_Error is raised by integer
1253 -- division, rem and mod if the right operand is zero.
1255 if Right_Int = 0 then
1256 Apply_Compile_Time_Constraint_Error
1257 (N, "rem with zero divisor",
1263 Result := Left_Int rem Right_Int;
1267 raise Program_Error;
1270 -- Adjust the result by the modulus if the type is a modular type
1272 if Is_Modular_Integer_Type (Ltype) then
1273 Result := Result mod Modulus (Ltype);
1275 -- For a signed integer type, check non-static overflow
1277 elsif (not Stat) and then Is_Signed_Integer_Type (Ltype) then
1279 BT : constant Entity_Id := Base_Type (Ltype);
1280 Lo : constant Uint := Expr_Value (Type_Low_Bound (BT));
1281 Hi : constant Uint := Expr_Value (Type_High_Bound (BT));
1283 if Result < Lo or else Result > Hi then
1284 Apply_Compile_Time_Constraint_Error
1285 (N, "value not in range of }?",
1286 CE_Overflow_Check_Failed,
1293 -- If we get here we can fold the result
1295 Fold_Uint (N, Result, Stat);
1298 -- Cases where at least one operand is a real. We handle the cases
1299 -- of both reals, or mixed/real integer cases (the latter happen
1300 -- only for divide and multiply, and the result is always real).
1302 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
1309 if Is_Real_Type (Ltype) then
1310 Left_Real := Expr_Value_R (Left);
1312 Left_Real := UR_From_Uint (Expr_Value (Left));
1315 if Is_Real_Type (Rtype) then
1316 Right_Real := Expr_Value_R (Right);
1318 Right_Real := UR_From_Uint (Expr_Value (Right));
1321 if Nkind (N) = N_Op_Add then
1322 Result := Left_Real + Right_Real;
1324 elsif Nkind (N) = N_Op_Subtract then
1325 Result := Left_Real - Right_Real;
1327 elsif Nkind (N) = N_Op_Multiply then
1328 Result := Left_Real * Right_Real;
1330 else pragma Assert (Nkind (N) = N_Op_Divide);
1331 if UR_Is_Zero (Right_Real) then
1332 Apply_Compile_Time_Constraint_Error
1333 (N, "division by zero", CE_Divide_By_Zero);
1337 Result := Left_Real / Right_Real;
1340 Fold_Ureal (N, Result, Stat);
1343 end Eval_Arithmetic_Op;
1345 ----------------------------
1346 -- Eval_Character_Literal --
1347 ----------------------------
1349 -- Nothing to be done!
1351 procedure Eval_Character_Literal (N : Node_Id) is
1352 pragma Warnings (Off, N);
1355 end Eval_Character_Literal;
1361 -- Static function calls are either calls to predefined operators
1362 -- with static arguments, or calls to functions that rename a literal.
1363 -- Only the latter case is handled here, predefined operators are
1364 -- constant-folded elsewhere.
1366 -- If the function is itself inherited (see 7423-001) the literal of
1367 -- the parent type must be explicitly converted to the return type
1370 procedure Eval_Call (N : Node_Id) is
1371 Loc : constant Source_Ptr := Sloc (N);
1372 Typ : constant Entity_Id := Etype (N);
1376 if Nkind (N) = N_Function_Call
1377 and then No (Parameter_Associations (N))
1378 and then Is_Entity_Name (Name (N))
1379 and then Present (Alias (Entity (Name (N))))
1380 and then Is_Enumeration_Type (Base_Type (Typ))
1382 Lit := Alias (Entity (Name (N)));
1383 while Present (Alias (Lit)) loop
1387 if Ekind (Lit) = E_Enumeration_Literal then
1388 if Base_Type (Etype (Lit)) /= Base_Type (Typ) then
1390 (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc)));
1392 Rewrite (N, New_Occurrence_Of (Lit, Loc));
1400 ------------------------
1401 -- Eval_Concatenation --
1402 ------------------------
1404 -- Concatenation is a static function, so the result is static if
1405 -- both operands are static (RM 4.9(7), 4.9(21)).
1407 procedure Eval_Concatenation (N : Node_Id) is
1408 Left : constant Node_Id := Left_Opnd (N);
1409 Right : constant Node_Id := Right_Opnd (N);
1410 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
1415 -- Concatenation is never static in Ada 83, so if Ada 83
1416 -- check operand non-static context
1418 if Ada_Version = Ada_83
1419 and then Comes_From_Source (N)
1421 Check_Non_Static_Context (Left);
1422 Check_Non_Static_Context (Right);
1426 -- If not foldable we are done. In principle concatenation that yields
1427 -- any string type is static (i.e. an array type of character types).
1428 -- However, character types can include enumeration literals, and
1429 -- concatenation in that case cannot be described by a literal, so we
1430 -- only consider the operation static if the result is an array of
1431 -- (a descendant of) a predefined character type.
1433 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1435 if (C_Typ = Standard_Character
1436 or else C_Typ = Standard_Wide_Character
1437 or else C_Typ = Standard_Wide_Wide_Character)
1442 Set_Is_Static_Expression (N, False);
1446 -- Compile time string concatenation
1448 -- ??? Note that operands that are aggregates can be marked as
1449 -- static, so we should attempt at a later stage to fold
1450 -- concatenations with such aggregates.
1453 Left_Str : constant Node_Id := Get_String_Val (Left);
1455 Right_Str : constant Node_Id := Get_String_Val (Right);
1456 Folded_Val : String_Id;
1459 -- Establish new string literal, and store left operand. We make
1460 -- sure to use the special Start_String that takes an operand if
1461 -- the left operand is a string literal. Since this is optimized
1462 -- in the case where that is the most recently created string
1463 -- literal, we ensure efficient time/space behavior for the
1464 -- case of a concatenation of a series of string literals.
1466 if Nkind (Left_Str) = N_String_Literal then
1467 Left_Len := String_Length (Strval (Left_Str));
1469 -- If the left operand is the empty string, and the right operand
1470 -- is a string literal (the case of "" & "..."), the result is the
1471 -- value of the right operand. This optimization is important when
1472 -- Is_Folded_In_Parser, to avoid copying an enormous right
1475 if Left_Len = 0 and then Nkind (Right_Str) = N_String_Literal then
1476 Folded_Val := Strval (Right_Str);
1478 Start_String (Strval (Left_Str));
1483 Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str)));
1487 -- Now append the characters of the right operand, unless we
1488 -- optimized the "" & "..." case above.
1490 if Nkind (Right_Str) = N_String_Literal then
1491 if Left_Len /= 0 then
1492 Store_String_Chars (Strval (Right_Str));
1493 Folded_Val := End_String;
1496 Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str)));
1497 Folded_Val := End_String;
1500 Set_Is_Static_Expression (N, Stat);
1504 -- If left operand is the empty string, the result is the
1505 -- right operand, including its bounds if anomalous.
1508 and then Is_Array_Type (Etype (Right))
1509 and then Etype (Right) /= Any_String
1511 Set_Etype (N, Etype (Right));
1514 Fold_Str (N, Folded_Val, Static => True);
1517 end Eval_Concatenation;
1519 ---------------------------------
1520 -- Eval_Conditional_Expression --
1521 ---------------------------------
1523 -- This GNAT internal construct can never be statically folded, so the
1524 -- only required processing is to do the check for non-static context
1525 -- for the two expression operands.
1527 procedure Eval_Conditional_Expression (N : Node_Id) is
1528 Condition : constant Node_Id := First (Expressions (N));
1529 Then_Expr : constant Node_Id := Next (Condition);
1530 Else_Expr : constant Node_Id := Next (Then_Expr);
1533 Check_Non_Static_Context (Then_Expr);
1534 Check_Non_Static_Context (Else_Expr);
1535 end Eval_Conditional_Expression;
1537 ----------------------
1538 -- Eval_Entity_Name --
1539 ----------------------
1541 -- This procedure is used for identifiers and expanded names other than
1542 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
1543 -- static if they denote a static constant (RM 4.9(6)) or if the name
1544 -- denotes an enumeration literal (RM 4.9(22)).
1546 procedure Eval_Entity_Name (N : Node_Id) is
1547 Def_Id : constant Entity_Id := Entity (N);
1551 -- Enumeration literals are always considered to be constants
1552 -- and cannot raise constraint error (RM 4.9(22)).
1554 if Ekind (Def_Id) = E_Enumeration_Literal then
1555 Set_Is_Static_Expression (N);
1558 -- A name is static if it denotes a static constant (RM 4.9(5)), and
1559 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
1560 -- it does not violate 10.2.1(8) here, since this is not a variable.
1562 elsif Ekind (Def_Id) = E_Constant then
1564 -- Deferred constants must always be treated as nonstatic
1565 -- outside the scope of their full view.
1567 if Present (Full_View (Def_Id))
1568 and then not In_Open_Scopes (Scope (Def_Id))
1572 Val := Constant_Value (Def_Id);
1575 if Present (Val) then
1576 Set_Is_Static_Expression
1577 (N, Is_Static_Expression (Val)
1578 and then Is_Static_Subtype (Etype (Def_Id)));
1579 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
1581 if not Is_Static_Expression (N)
1582 and then not Is_Generic_Type (Etype (N))
1584 Validate_Static_Object_Name (N);
1591 -- Fall through if the name is not static
1593 Validate_Static_Object_Name (N);
1594 end Eval_Entity_Name;
1596 ----------------------------
1597 -- Eval_Indexed_Component --
1598 ----------------------------
1600 -- Indexed components are never static, so we need to perform the check
1601 -- for non-static context on the index values. Then, we check if the
1602 -- value can be obtained at compile time, even though it is non-static.
1604 procedure Eval_Indexed_Component (N : Node_Id) is
1608 -- Check for non-static context on index values
1610 Expr := First (Expressions (N));
1611 while Present (Expr) loop
1612 Check_Non_Static_Context (Expr);
1616 -- If the indexed component appears in an object renaming declaration
1617 -- then we do not want to try to evaluate it, since in this case we
1618 -- need the identity of the array element.
1620 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
1623 -- Similarly if the indexed component appears as the prefix of an
1624 -- attribute we don't want to evaluate it, because at least for
1625 -- some cases of attributes we need the identify (e.g. Access, Size)
1627 elsif Nkind (Parent (N)) = N_Attribute_Reference then
1631 -- Note: there are other cases, such as the left side of an assignment,
1632 -- or an OUT parameter for a call, where the replacement results in the
1633 -- illegal use of a constant, But these cases are illegal in the first
1634 -- place, so the replacement, though silly, is harmless.
1636 -- Now see if this is a constant array reference
1638 if List_Length (Expressions (N)) = 1
1639 and then Is_Entity_Name (Prefix (N))
1640 and then Ekind (Entity (Prefix (N))) = E_Constant
1641 and then Present (Constant_Value (Entity (Prefix (N))))
1644 Loc : constant Source_Ptr := Sloc (N);
1645 Arr : constant Node_Id := Constant_Value (Entity (Prefix (N)));
1646 Sub : constant Node_Id := First (Expressions (N));
1652 -- Linear one's origin subscript value for array reference
1655 -- Lower bound of the first array index
1658 -- Value from constant array
1661 Atyp := Etype (Arr);
1663 if Is_Access_Type (Atyp) then
1664 Atyp := Designated_Type (Atyp);
1667 -- If we have an array type (we should have but perhaps there
1668 -- are error cases where this is not the case), then see if we
1669 -- can do a constant evaluation of the array reference.
1671 if Is_Array_Type (Atyp) then
1672 if Ekind (Atyp) = E_String_Literal_Subtype then
1673 Lbd := String_Literal_Low_Bound (Atyp);
1675 Lbd := Type_Low_Bound (Etype (First_Index (Atyp)));
1678 if Compile_Time_Known_Value (Sub)
1679 and then Nkind (Arr) = N_Aggregate
1680 and then Compile_Time_Known_Value (Lbd)
1681 and then Is_Discrete_Type (Component_Type (Atyp))
1683 Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1;
1685 if List_Length (Expressions (Arr)) >= Lin then
1686 Elm := Pick (Expressions (Arr), Lin);
1688 -- If the resulting expression is compile time known,
1689 -- then we can rewrite the indexed component with this
1690 -- value, being sure to mark the result as non-static.
1691 -- We also reset the Sloc, in case this generates an
1692 -- error later on (e.g. 136'Access).
1694 if Compile_Time_Known_Value (Elm) then
1695 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
1696 Set_Is_Static_Expression (N, False);
1704 end Eval_Indexed_Component;
1706 --------------------------
1707 -- Eval_Integer_Literal --
1708 --------------------------
1710 -- Numeric literals are static (RM 4.9(1)), and have already been marked
1711 -- as static by the analyzer. The reason we did it that early is to allow
1712 -- the possibility of turning off the Is_Static_Expression flag after
1713 -- analysis, but before resolution, when integer literals are generated
1714 -- in the expander that do not correspond to static expressions.
1716 procedure Eval_Integer_Literal (N : Node_Id) is
1717 T : constant Entity_Id := Etype (N);
1719 function In_Any_Integer_Context return Boolean;
1720 -- If the literal is resolved with a specific type in a context
1721 -- where the expected type is Any_Integer, there are no range checks
1722 -- on the literal. By the time the literal is evaluated, it carries
1723 -- the type imposed by the enclosing expression, and we must recover
1724 -- the context to determine that Any_Integer is meant.
1726 ----------------------------
1727 -- To_Any_Integer_Context --
1728 ----------------------------
1730 function In_Any_Integer_Context return Boolean is
1731 Par : constant Node_Id := Parent (N);
1732 K : constant Node_Kind := Nkind (Par);
1735 -- Any_Integer also appears in digits specifications for real types,
1736 -- but those have bounds smaller that those of any integer base
1737 -- type, so we can safely ignore these cases.
1739 return K = N_Number_Declaration
1740 or else K = N_Attribute_Reference
1741 or else K = N_Attribute_Definition_Clause
1742 or else K = N_Modular_Type_Definition
1743 or else K = N_Signed_Integer_Type_Definition;
1744 end In_Any_Integer_Context;
1746 -- Start of processing for Eval_Integer_Literal
1750 -- If the literal appears in a non-expression context, then it is
1751 -- certainly appearing in a non-static context, so check it. This
1752 -- is actually a redundant check, since Check_Non_Static_Context
1753 -- would check it, but it seems worth while avoiding the call.
1755 if Nkind (Parent (N)) not in N_Subexpr
1756 and then not In_Any_Integer_Context
1758 Check_Non_Static_Context (N);
1761 -- Modular integer literals must be in their base range
1763 if Is_Modular_Integer_Type (T)
1764 and then Is_Out_Of_Range (N, Base_Type (T))
1768 end Eval_Integer_Literal;
1770 ---------------------
1771 -- Eval_Logical_Op --
1772 ---------------------
1774 -- Logical operations are static functions, so the result is potentially
1775 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
1777 procedure Eval_Logical_Op (N : Node_Id) is
1778 Left : constant Node_Id := Left_Opnd (N);
1779 Right : constant Node_Id := Right_Opnd (N);
1784 -- If not foldable we are done
1786 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1792 -- Compile time evaluation of logical operation
1795 Left_Int : constant Uint := Expr_Value (Left);
1796 Right_Int : constant Uint := Expr_Value (Right);
1799 if Is_Modular_Integer_Type (Etype (N)) then
1801 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
1802 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
1805 To_Bits (Left_Int, Left_Bits);
1806 To_Bits (Right_Int, Right_Bits);
1808 -- Note: should really be able to use array ops instead of
1809 -- these loops, but they weren't working at the time ???
1811 if Nkind (N) = N_Op_And then
1812 for J in Left_Bits'Range loop
1813 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
1816 elsif Nkind (N) = N_Op_Or then
1817 for J in Left_Bits'Range loop
1818 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
1822 pragma Assert (Nkind (N) = N_Op_Xor);
1824 for J in Left_Bits'Range loop
1825 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
1829 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat);
1833 pragma Assert (Is_Boolean_Type (Etype (N)));
1835 if Nkind (N) = N_Op_And then
1837 Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat);
1839 elsif Nkind (N) = N_Op_Or then
1841 Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat);
1844 pragma Assert (Nkind (N) = N_Op_Xor);
1846 Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat);
1850 end Eval_Logical_Op;
1852 ------------------------
1853 -- Eval_Membership_Op --
1854 ------------------------
1856 -- A membership test is potentially static if the expression is static,
1857 -- and the range is a potentially static range, or is a subtype mark
1858 -- denoting a static subtype (RM 4.9(12)).
1860 procedure Eval_Membership_Op (N : Node_Id) is
1861 Left : constant Node_Id := Left_Opnd (N);
1862 Right : constant Node_Id := Right_Opnd (N);
1871 -- Ignore if error in either operand, except to make sure that
1872 -- Any_Type is properly propagated to avoid junk cascaded errors.
1874 if Etype (Left) = Any_Type
1875 or else Etype (Right) = Any_Type
1877 Set_Etype (N, Any_Type);
1881 -- Case of right operand is a subtype name
1883 if Is_Entity_Name (Right) then
1884 Def_Id := Entity (Right);
1886 if (Is_Scalar_Type (Def_Id) or else Is_String_Type (Def_Id))
1887 and then Is_OK_Static_Subtype (Def_Id)
1889 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
1891 if not Fold or else not Stat then
1895 Check_Non_Static_Context (Left);
1899 -- For string membership tests we will check the length
1902 if not Is_String_Type (Def_Id) then
1903 Lo := Type_Low_Bound (Def_Id);
1904 Hi := Type_High_Bound (Def_Id);
1911 -- Case of right operand is a range
1914 if Is_Static_Range (Right) then
1915 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
1917 if not Fold or else not Stat then
1920 -- If one bound of range raises CE, then don't try to fold
1922 elsif not Is_OK_Static_Range (Right) then
1923 Check_Non_Static_Context (Left);
1928 Check_Non_Static_Context (Left);
1932 -- Here we know range is an OK static range
1934 Lo := Low_Bound (Right);
1935 Hi := High_Bound (Right);
1938 -- For strings we check that the length of the string expression is
1939 -- compatible with the string subtype if the subtype is constrained,
1940 -- or if unconstrained then the test is always true.
1942 if Is_String_Type (Etype (Right)) then
1943 if not Is_Constrained (Etype (Right)) then
1948 Typlen : constant Uint := String_Type_Len (Etype (Right));
1949 Strlen : constant Uint :=
1950 UI_From_Int (String_Length (Strval (Get_String_Val (Left))));
1952 Result := (Typlen = Strlen);
1956 -- Fold the membership test. We know we have a static range and Lo
1957 -- and Hi are set to the expressions for the end points of this range.
1959 elsif Is_Real_Type (Etype (Right)) then
1961 Leftval : constant Ureal := Expr_Value_R (Left);
1964 Result := Expr_Value_R (Lo) <= Leftval
1965 and then Leftval <= Expr_Value_R (Hi);
1970 Leftval : constant Uint := Expr_Value (Left);
1973 Result := Expr_Value (Lo) <= Leftval
1974 and then Leftval <= Expr_Value (Hi);
1978 if Nkind (N) = N_Not_In then
1979 Result := not Result;
1982 Fold_Uint (N, Test (Result), True);
1983 Warn_On_Known_Condition (N);
1984 end Eval_Membership_Op;
1986 ------------------------
1987 -- Eval_Named_Integer --
1988 ------------------------
1990 procedure Eval_Named_Integer (N : Node_Id) is
1993 Expr_Value (Expression (Declaration_Node (Entity (N)))), True);
1994 end Eval_Named_Integer;
1996 ---------------------
1997 -- Eval_Named_Real --
1998 ---------------------
2000 procedure Eval_Named_Real (N : Node_Id) is
2003 Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True);
2004 end Eval_Named_Real;
2010 -- Exponentiation is a static functions, so the result is potentially
2011 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2013 procedure Eval_Op_Expon (N : Node_Id) is
2014 Left : constant Node_Id := Left_Opnd (N);
2015 Right : constant Node_Id := Right_Opnd (N);
2020 -- If not foldable we are done
2022 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2028 -- Fold exponentiation operation
2031 Right_Int : constant Uint := Expr_Value (Right);
2036 if Is_Integer_Type (Etype (Left)) then
2038 Left_Int : constant Uint := Expr_Value (Left);
2042 -- Exponentiation of an integer raises the exception
2043 -- Constraint_Error for a negative exponent (RM 4.5.6)
2045 if Right_Int < 0 then
2046 Apply_Compile_Time_Constraint_Error
2047 (N, "integer exponent negative",
2048 CE_Range_Check_Failed,
2053 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
2054 Result := Left_Int ** Right_Int;
2059 if Is_Modular_Integer_Type (Etype (N)) then
2060 Result := Result mod Modulus (Etype (N));
2063 Fold_Uint (N, Result, Stat);
2071 Left_Real : constant Ureal := Expr_Value_R (Left);
2074 -- Cannot have a zero base with a negative exponent
2076 if UR_Is_Zero (Left_Real) then
2078 if Right_Int < 0 then
2079 Apply_Compile_Time_Constraint_Error
2080 (N, "zero ** negative integer",
2081 CE_Range_Check_Failed,
2085 Fold_Ureal (N, Ureal_0, Stat);
2089 Fold_Ureal (N, Left_Real ** Right_Int, Stat);
2100 -- The not operation is a static functions, so the result is potentially
2101 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
2103 procedure Eval_Op_Not (N : Node_Id) is
2104 Right : constant Node_Id := Right_Opnd (N);
2109 -- If not foldable we are done
2111 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
2117 -- Fold not operation
2120 Rint : constant Uint := Expr_Value (Right);
2121 Typ : constant Entity_Id := Etype (N);
2124 -- Negation is equivalent to subtracting from the modulus minus
2125 -- one. For a binary modulus this is equivalent to the ones-
2126 -- component of the original value. For non-binary modulus this
2127 -- is an arbitrary but consistent definition.
2129 if Is_Modular_Integer_Type (Typ) then
2130 Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat);
2133 pragma Assert (Is_Boolean_Type (Typ));
2134 Fold_Uint (N, Test (not Is_True (Rint)), Stat);
2137 Set_Is_Static_Expression (N, Stat);
2141 -------------------------------
2142 -- Eval_Qualified_Expression --
2143 -------------------------------
2145 -- A qualified expression is potentially static if its subtype mark denotes
2146 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
2148 procedure Eval_Qualified_Expression (N : Node_Id) is
2149 Operand : constant Node_Id := Expression (N);
2150 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
2157 -- Can only fold if target is string or scalar and subtype is static
2158 -- Also, do not fold if our parent is an allocator (this is because
2159 -- the qualified expression is really part of the syntactic structure
2160 -- of an allocator, and we do not want to end up with something that
2161 -- corresponds to "new 1" where the 1 is the result of folding a
2162 -- qualified expression).
2164 if not Is_Static_Subtype (Target_Type)
2165 or else Nkind (Parent (N)) = N_Allocator
2167 Check_Non_Static_Context (Operand);
2169 -- If operand is known to raise constraint_error, set the
2170 -- flag on the expression so it does not get optimized away.
2172 if Nkind (Operand) = N_Raise_Constraint_Error then
2173 Set_Raises_Constraint_Error (N);
2179 -- If not foldable we are done
2181 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
2186 -- Don't try fold if target type has constraint error bounds
2188 elsif not Is_OK_Static_Subtype (Target_Type) then
2189 Set_Raises_Constraint_Error (N);
2193 -- Here we will fold, save Print_In_Hex indication
2195 Hex := Nkind (Operand) = N_Integer_Literal
2196 and then Print_In_Hex (Operand);
2198 -- Fold the result of qualification
2200 if Is_Discrete_Type (Target_Type) then
2201 Fold_Uint (N, Expr_Value (Operand), Stat);
2203 -- Preserve Print_In_Hex indication
2205 if Hex and then Nkind (N) = N_Integer_Literal then
2206 Set_Print_In_Hex (N);
2209 elsif Is_Real_Type (Target_Type) then
2210 Fold_Ureal (N, Expr_Value_R (Operand), Stat);
2213 Fold_Str (N, Strval (Get_String_Val (Operand)), Stat);
2216 Set_Is_Static_Expression (N, False);
2218 Check_String_Literal_Length (N, Target_Type);
2224 -- The expression may be foldable but not static
2226 Set_Is_Static_Expression (N, Stat);
2228 if Is_Out_Of_Range (N, Etype (N)) then
2231 end Eval_Qualified_Expression;
2233 -----------------------
2234 -- Eval_Real_Literal --
2235 -----------------------
2237 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2238 -- as static by the analyzer. The reason we did it that early is to allow
2239 -- the possibility of turning off the Is_Static_Expression flag after
2240 -- analysis, but before resolution, when integer literals are generated
2241 -- in the expander that do not correspond to static expressions.
2243 procedure Eval_Real_Literal (N : Node_Id) is
2245 -- If the literal appears in a non-expression context, then it is
2246 -- certainly appearing in a non-static context, so check it.
2248 if Nkind (Parent (N)) not in N_Subexpr then
2249 Check_Non_Static_Context (N);
2252 end Eval_Real_Literal;
2254 ------------------------
2255 -- Eval_Relational_Op --
2256 ------------------------
2258 -- Relational operations are static functions, so the result is static
2259 -- if both operands are static (RM 4.9(7), 4.9(20)).
2261 procedure Eval_Relational_Op (N : Node_Id) is
2262 Left : constant Node_Id := Left_Opnd (N);
2263 Right : constant Node_Id := Right_Opnd (N);
2264 Typ : constant Entity_Id := Etype (Left);
2270 -- One special case to deal with first. If we can tell that
2271 -- the result will be false because the lengths of one or
2272 -- more index subtypes are compile time known and different,
2273 -- then we can replace the entire result by False. We only
2274 -- do this for one dimensional arrays, because the case of
2275 -- multi-dimensional arrays is rare and too much trouble!
2276 -- If one of the operands is an illegal aggregate, its type
2277 -- might still be an arbitrary composite type, so nothing to do.
2279 if Is_Array_Type (Typ)
2280 and then Typ /= Any_Composite
2281 and then Number_Dimensions (Typ) = 1
2282 and then (Nkind (N) = N_Op_Eq or else Nkind (N) = N_Op_Ne)
2284 if Raises_Constraint_Error (Left)
2285 or else Raises_Constraint_Error (Right)
2291 procedure Get_Static_Length (Op : Node_Id; Len : out Uint);
2292 -- If Op is an expression for a constrained array with a known
2293 -- at compile time length, then Len is set to this (non-negative
2294 -- length). Otherwise Len is set to minus 1.
2296 -----------------------
2297 -- Get_Static_Length --
2298 -----------------------
2300 procedure Get_Static_Length (Op : Node_Id; Len : out Uint) is
2304 if Nkind (Op) = N_String_Literal then
2305 Len := UI_From_Int (String_Length (Strval (Op)));
2307 elsif not Is_Constrained (Etype (Op)) then
2308 Len := Uint_Minus_1;
2311 T := Etype (First_Index (Etype (Op)));
2313 if Is_Discrete_Type (T)
2315 Compile_Time_Known_Value (Type_Low_Bound (T))
2317 Compile_Time_Known_Value (Type_High_Bound (T))
2319 Len := UI_Max (Uint_0,
2320 Expr_Value (Type_High_Bound (T)) -
2321 Expr_Value (Type_Low_Bound (T)) + 1);
2323 Len := Uint_Minus_1;
2326 end Get_Static_Length;
2332 Get_Static_Length (Left, Len_L);
2333 Get_Static_Length (Right, Len_R);
2335 if Len_L /= Uint_Minus_1
2336 and then Len_R /= Uint_Minus_1
2337 and then Len_L /= Len_R
2339 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
2340 Warn_On_Known_Condition (N);
2345 -- Another special case: comparisons of access types, where one or both
2346 -- operands are known to be null, so the result can be determined.
2348 elsif Is_Access_Type (Typ) then
2349 if Known_Null (Left) then
2350 if Known_Null (Right) then
2351 Fold_Uint (N, Test (Nkind (N) = N_Op_Eq), False);
2352 Warn_On_Known_Condition (N);
2355 elsif Known_Non_Null (Right) then
2356 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
2357 Warn_On_Known_Condition (N);
2361 elsif Known_Non_Null (Left) then
2362 if Known_Null (Right) then
2363 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
2364 Warn_On_Known_Condition (N);
2370 -- Can only fold if type is scalar (don't fold string ops)
2372 if not Is_Scalar_Type (Typ) then
2373 Check_Non_Static_Context (Left);
2374 Check_Non_Static_Context (Right);
2378 -- If not foldable we are done
2380 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2386 -- Integer and Enumeration (discrete) type cases
2388 if Is_Discrete_Type (Typ) then
2390 Left_Int : constant Uint := Expr_Value (Left);
2391 Right_Int : constant Uint := Expr_Value (Right);
2395 when N_Op_Eq => Result := Left_Int = Right_Int;
2396 when N_Op_Ne => Result := Left_Int /= Right_Int;
2397 when N_Op_Lt => Result := Left_Int < Right_Int;
2398 when N_Op_Le => Result := Left_Int <= Right_Int;
2399 when N_Op_Gt => Result := Left_Int > Right_Int;
2400 when N_Op_Ge => Result := Left_Int >= Right_Int;
2403 raise Program_Error;
2406 Fold_Uint (N, Test (Result), Stat);
2412 pragma Assert (Is_Real_Type (Typ));
2415 Left_Real : constant Ureal := Expr_Value_R (Left);
2416 Right_Real : constant Ureal := Expr_Value_R (Right);
2420 when N_Op_Eq => Result := (Left_Real = Right_Real);
2421 when N_Op_Ne => Result := (Left_Real /= Right_Real);
2422 when N_Op_Lt => Result := (Left_Real < Right_Real);
2423 when N_Op_Le => Result := (Left_Real <= Right_Real);
2424 when N_Op_Gt => Result := (Left_Real > Right_Real);
2425 when N_Op_Ge => Result := (Left_Real >= Right_Real);
2428 raise Program_Error;
2431 Fold_Uint (N, Test (Result), Stat);
2435 Warn_On_Known_Condition (N);
2436 end Eval_Relational_Op;
2442 -- Shift operations are intrinsic operations that can never be static,
2443 -- so the only processing required is to perform the required check for
2444 -- a non static context for the two operands.
2446 -- Actually we could do some compile time evaluation here some time ???
2448 procedure Eval_Shift (N : Node_Id) is
2450 Check_Non_Static_Context (Left_Opnd (N));
2451 Check_Non_Static_Context (Right_Opnd (N));
2454 ------------------------
2455 -- Eval_Short_Circuit --
2456 ------------------------
2458 -- A short circuit operation is potentially static if both operands
2459 -- are potentially static (RM 4.9 (13))
2461 procedure Eval_Short_Circuit (N : Node_Id) is
2462 Kind : constant Node_Kind := Nkind (N);
2463 Left : constant Node_Id := Left_Opnd (N);
2464 Right : constant Node_Id := Right_Opnd (N);
2466 Rstat : constant Boolean :=
2467 Is_Static_Expression (Left)
2468 and then Is_Static_Expression (Right);
2471 -- Short circuit operations are never static in Ada 83
2473 if Ada_Version = Ada_83
2474 and then Comes_From_Source (N)
2476 Check_Non_Static_Context (Left);
2477 Check_Non_Static_Context (Right);
2481 -- Now look at the operands, we can't quite use the normal call to
2482 -- Test_Expression_Is_Foldable here because short circuit operations
2483 -- are a special case, they can still be foldable, even if the right
2484 -- operand raises constraint error.
2486 -- If either operand is Any_Type, just propagate to result and
2487 -- do not try to fold, this prevents cascaded errors.
2489 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
2490 Set_Etype (N, Any_Type);
2493 -- If left operand raises constraint error, then replace node N with
2494 -- the raise constraint error node, and we are obviously not foldable.
2495 -- Is_Static_Expression is set from the two operands in the normal way,
2496 -- and we check the right operand if it is in a non-static context.
2498 elsif Raises_Constraint_Error (Left) then
2500 Check_Non_Static_Context (Right);
2503 Rewrite_In_Raise_CE (N, Left);
2504 Set_Is_Static_Expression (N, Rstat);
2507 -- If the result is not static, then we won't in any case fold
2509 elsif not Rstat then
2510 Check_Non_Static_Context (Left);
2511 Check_Non_Static_Context (Right);
2515 -- Here the result is static, note that, unlike the normal processing
2516 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
2517 -- the right operand raises constraint error, that's because it is not
2518 -- significant if the left operand is decisive.
2520 Set_Is_Static_Expression (N);
2522 -- It does not matter if the right operand raises constraint error if
2523 -- it will not be evaluated. So deal specially with the cases where
2524 -- the right operand is not evaluated. Note that we will fold these
2525 -- cases even if the right operand is non-static, which is fine, but
2526 -- of course in these cases the result is not potentially static.
2528 Left_Int := Expr_Value (Left);
2530 if (Kind = N_And_Then and then Is_False (Left_Int))
2531 or else (Kind = N_Or_Else and Is_True (Left_Int))
2533 Fold_Uint (N, Left_Int, Rstat);
2537 -- If first operand not decisive, then it does matter if the right
2538 -- operand raises constraint error, since it will be evaluated, so
2539 -- we simply replace the node with the right operand. Note that this
2540 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
2541 -- (both are set to True in Right).
2543 if Raises_Constraint_Error (Right) then
2544 Rewrite_In_Raise_CE (N, Right);
2545 Check_Non_Static_Context (Left);
2549 -- Otherwise the result depends on the right operand
2551 Fold_Uint (N, Expr_Value (Right), Rstat);
2553 end Eval_Short_Circuit;
2559 -- Slices can never be static, so the only processing required is to
2560 -- check for non-static context if an explicit range is given.
2562 procedure Eval_Slice (N : Node_Id) is
2563 Drange : constant Node_Id := Discrete_Range (N);
2565 if Nkind (Drange) = N_Range then
2566 Check_Non_Static_Context (Low_Bound (Drange));
2567 Check_Non_Static_Context (High_Bound (Drange));
2571 -------------------------
2572 -- Eval_String_Literal --
2573 -------------------------
2575 procedure Eval_String_Literal (N : Node_Id) is
2576 Typ : constant Entity_Id := Etype (N);
2577 Bas : constant Entity_Id := Base_Type (Typ);
2583 -- Nothing to do if error type (handles cases like default expressions
2584 -- or generics where we have not yet fully resolved the type)
2586 if Bas = Any_Type or else Bas = Any_String then
2590 -- String literals are static if the subtype is static (RM 4.9(2)), so
2591 -- reset the static expression flag (it was set unconditionally in
2592 -- Analyze_String_Literal) if the subtype is non-static. We tell if
2593 -- the subtype is static by looking at the lower bound.
2595 if Ekind (Typ) = E_String_Literal_Subtype then
2596 if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then
2597 Set_Is_Static_Expression (N, False);
2601 -- Here if Etype of string literal is normal Etype (not yet possible,
2602 -- but may be possible in future!)
2604 elsif not Is_OK_Static_Expression
2605 (Type_Low_Bound (Etype (First_Index (Typ))))
2607 Set_Is_Static_Expression (N, False);
2611 -- If original node was a type conversion, then result if non-static
2613 if Nkind (Original_Node (N)) = N_Type_Conversion then
2614 Set_Is_Static_Expression (N, False);
2618 -- Test for illegal Ada 95 cases. A string literal is illegal in
2619 -- Ada 95 if its bounds are outside the index base type and this
2620 -- index type is static. This can happen in only two ways. Either
2621 -- the string literal is too long, or it is null, and the lower
2622 -- bound is type'First. In either case it is the upper bound that
2623 -- is out of range of the index type.
2625 if Ada_Version >= Ada_95 then
2626 if Root_Type (Bas) = Standard_String
2628 Root_Type (Bas) = Standard_Wide_String
2630 Xtp := Standard_Positive;
2632 Xtp := Etype (First_Index (Bas));
2635 if Ekind (Typ) = E_String_Literal_Subtype then
2636 Lo := String_Literal_Low_Bound (Typ);
2638 Lo := Type_Low_Bound (Etype (First_Index (Typ)));
2641 Len := String_Length (Strval (N));
2643 if UI_From_Int (Len) > String_Type_Len (Bas) then
2644 Apply_Compile_Time_Constraint_Error
2645 (N, "string literal too long for}", CE_Length_Check_Failed,
2647 Typ => First_Subtype (Bas));
2650 and then not Is_Generic_Type (Xtp)
2652 Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp)))
2654 Apply_Compile_Time_Constraint_Error
2655 (N, "null string literal not allowed for}",
2656 CE_Length_Check_Failed,
2658 Typ => First_Subtype (Bas));
2661 end Eval_String_Literal;
2663 --------------------------
2664 -- Eval_Type_Conversion --
2665 --------------------------
2667 -- A type conversion is potentially static if its subtype mark is for a
2668 -- static scalar subtype, and its operand expression is potentially static
2671 procedure Eval_Type_Conversion (N : Node_Id) is
2672 Operand : constant Node_Id := Expression (N);
2673 Source_Type : constant Entity_Id := Etype (Operand);
2674 Target_Type : constant Entity_Id := Etype (N);
2679 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
2680 -- Returns true if type T is an integer type, or if it is a
2681 -- fixed-point type to be treated as an integer (i.e. the flag
2682 -- Conversion_OK is set on the conversion node).
2684 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
2685 -- Returns true if type T is a floating-point type, or if it is a
2686 -- fixed-point type that is not to be treated as an integer (i.e. the
2687 -- flag Conversion_OK is not set on the conversion node).
2689 ------------------------------
2690 -- To_Be_Treated_As_Integer --
2691 ------------------------------
2693 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
2697 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
2698 end To_Be_Treated_As_Integer;
2700 ---------------------------
2701 -- To_Be_Treated_As_Real --
2702 ---------------------------
2704 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
2707 Is_Floating_Point_Type (T)
2708 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
2709 end To_Be_Treated_As_Real;
2711 -- Start of processing for Eval_Type_Conversion
2714 -- Cannot fold if target type is non-static or if semantic error
2716 if not Is_Static_Subtype (Target_Type) then
2717 Check_Non_Static_Context (Operand);
2720 elsif Error_Posted (N) then
2724 -- If not foldable we are done
2726 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
2731 -- Don't try fold if target type has constraint error bounds
2733 elsif not Is_OK_Static_Subtype (Target_Type) then
2734 Set_Raises_Constraint_Error (N);
2738 -- Remaining processing depends on operand types. Note that in the
2739 -- following type test, fixed-point counts as real unless the flag
2740 -- Conversion_OK is set, in which case it counts as integer.
2742 -- Fold conversion, case of string type. The result is not static
2744 if Is_String_Type (Target_Type) then
2745 Fold_Str (N, Strval (Get_String_Val (Operand)), Static => False);
2749 -- Fold conversion, case of integer target type
2751 elsif To_Be_Treated_As_Integer (Target_Type) then
2756 -- Integer to integer conversion
2758 if To_Be_Treated_As_Integer (Source_Type) then
2759 Result := Expr_Value (Operand);
2761 -- Real to integer conversion
2764 Result := UR_To_Uint (Expr_Value_R (Operand));
2767 -- If fixed-point type (Conversion_OK must be set), then the
2768 -- result is logically an integer, but we must replace the
2769 -- conversion with the corresponding real literal, since the
2770 -- type from a semantic point of view is still fixed-point.
2772 if Is_Fixed_Point_Type (Target_Type) then
2774 (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat);
2776 -- Otherwise result is integer literal
2779 Fold_Uint (N, Result, Stat);
2783 -- Fold conversion, case of real target type
2785 elsif To_Be_Treated_As_Real (Target_Type) then
2790 if To_Be_Treated_As_Real (Source_Type) then
2791 Result := Expr_Value_R (Operand);
2793 Result := UR_From_Uint (Expr_Value (Operand));
2796 Fold_Ureal (N, Result, Stat);
2799 -- Enumeration types
2802 Fold_Uint (N, Expr_Value (Operand), Stat);
2805 if Is_Out_Of_Range (N, Etype (N)) then
2809 end Eval_Type_Conversion;
2815 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
2816 -- are potentially static if the operand is potentially static (RM 4.9(7))
2818 procedure Eval_Unary_Op (N : Node_Id) is
2819 Right : constant Node_Id := Right_Opnd (N);
2824 -- If not foldable we are done
2826 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
2832 -- Fold for integer case
2834 if Is_Integer_Type (Etype (N)) then
2836 Rint : constant Uint := Expr_Value (Right);
2840 -- In the case of modular unary plus and abs there is no need
2841 -- to adjust the result of the operation since if the original
2842 -- operand was in bounds the result will be in the bounds of the
2843 -- modular type. However, in the case of modular unary minus the
2844 -- result may go out of the bounds of the modular type and needs
2847 if Nkind (N) = N_Op_Plus then
2850 elsif Nkind (N) = N_Op_Minus then
2851 if Is_Modular_Integer_Type (Etype (N)) then
2852 Result := (-Rint) mod Modulus (Etype (N));
2858 pragma Assert (Nkind (N) = N_Op_Abs);
2862 Fold_Uint (N, Result, Stat);
2865 -- Fold for real case
2867 elsif Is_Real_Type (Etype (N)) then
2869 Rreal : constant Ureal := Expr_Value_R (Right);
2873 if Nkind (N) = N_Op_Plus then
2876 elsif Nkind (N) = N_Op_Minus then
2877 Result := UR_Negate (Rreal);
2880 pragma Assert (Nkind (N) = N_Op_Abs);
2881 Result := abs Rreal;
2884 Fold_Ureal (N, Result, Stat);
2889 -------------------------------
2890 -- Eval_Unchecked_Conversion --
2891 -------------------------------
2893 -- Unchecked conversions can never be static, so the only required
2894 -- processing is to check for a non-static context for the operand.
2896 procedure Eval_Unchecked_Conversion (N : Node_Id) is
2898 Check_Non_Static_Context (Expression (N));
2899 end Eval_Unchecked_Conversion;
2901 --------------------
2902 -- Expr_Rep_Value --
2903 --------------------
2905 function Expr_Rep_Value (N : Node_Id) return Uint is
2906 Kind : constant Node_Kind := Nkind (N);
2910 if Is_Entity_Name (N) then
2913 -- An enumeration literal that was either in the source or
2914 -- created as a result of static evaluation.
2916 if Ekind (Ent) = E_Enumeration_Literal then
2917 return Enumeration_Rep (Ent);
2919 -- A user defined static constant
2922 pragma Assert (Ekind (Ent) = E_Constant);
2923 return Expr_Rep_Value (Constant_Value (Ent));
2926 -- An integer literal that was either in the source or created
2927 -- as a result of static evaluation.
2929 elsif Kind = N_Integer_Literal then
2932 -- A real literal for a fixed-point type. This must be the fixed-point
2933 -- case, either the literal is of a fixed-point type, or it is a bound
2934 -- of a fixed-point type, with type universal real. In either case we
2935 -- obtain the desired value from Corresponding_Integer_Value.
2937 elsif Kind = N_Real_Literal then
2938 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
2939 return Corresponding_Integer_Value (N);
2941 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
2943 elsif Kind = N_Attribute_Reference
2944 and then Attribute_Name (N) = Name_Null_Parameter
2948 -- Otherwise must be character literal
2951 pragma Assert (Kind = N_Character_Literal);
2954 -- Since Character literals of type Standard.Character don't
2955 -- have any defining character literals built for them, they
2956 -- do not have their Entity set, so just use their Char
2957 -- code. Otherwise for user-defined character literals use
2958 -- their Pos value as usual which is the same as the Rep value.
2961 return Char_Literal_Value (N);
2963 return Enumeration_Rep (Ent);
2972 function Expr_Value (N : Node_Id) return Uint is
2973 Kind : constant Node_Kind := Nkind (N);
2974 CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size);
2979 -- If already in cache, then we know it's compile time known and we can
2980 -- return the value that was previously stored in the cache since
2981 -- compile time known values cannot change.
2983 if CV_Ent.N = N then
2987 -- Otherwise proceed to test value
2989 if Is_Entity_Name (N) then
2992 -- An enumeration literal that was either in the source or
2993 -- created as a result of static evaluation.
2995 if Ekind (Ent) = E_Enumeration_Literal then
2996 Val := Enumeration_Pos (Ent);
2998 -- A user defined static constant
3001 pragma Assert (Ekind (Ent) = E_Constant);
3002 Val := Expr_Value (Constant_Value (Ent));
3005 -- An integer literal that was either in the source or created
3006 -- as a result of static evaluation.
3008 elsif Kind = N_Integer_Literal then
3011 -- A real literal for a fixed-point type. This must be the fixed-point
3012 -- case, either the literal is of a fixed-point type, or it is a bound
3013 -- of a fixed-point type, with type universal real. In either case we
3014 -- obtain the desired value from Corresponding_Integer_Value.
3016 elsif Kind = N_Real_Literal then
3018 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
3019 Val := Corresponding_Integer_Value (N);
3021 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
3023 elsif Kind = N_Attribute_Reference
3024 and then Attribute_Name (N) = Name_Null_Parameter
3028 -- Otherwise must be character literal
3031 pragma Assert (Kind = N_Character_Literal);
3034 -- Since Character literals of type Standard.Character don't
3035 -- have any defining character literals built for them, they
3036 -- do not have their Entity set, so just use their Char
3037 -- code. Otherwise for user-defined character literals use
3038 -- their Pos value as usual.
3041 Val := Char_Literal_Value (N);
3043 Val := Enumeration_Pos (Ent);
3047 -- Come here with Val set to value to be returned, set cache
3058 function Expr_Value_E (N : Node_Id) return Entity_Id is
3059 Ent : constant Entity_Id := Entity (N);
3062 if Ekind (Ent) = E_Enumeration_Literal then
3065 pragma Assert (Ekind (Ent) = E_Constant);
3066 return Expr_Value_E (Constant_Value (Ent));
3074 function Expr_Value_R (N : Node_Id) return Ureal is
3075 Kind : constant Node_Kind := Nkind (N);
3080 if Kind = N_Real_Literal then
3083 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
3085 pragma Assert (Ekind (Ent) = E_Constant);
3086 return Expr_Value_R (Constant_Value (Ent));
3088 elsif Kind = N_Integer_Literal then
3089 return UR_From_Uint (Expr_Value (N));
3091 -- Strange case of VAX literals, which are at this stage transformed
3092 -- into Vax_Type!x_To_y(IEEE_Literal). See Expand_N_Real_Literal in
3093 -- Exp_Vfpt for further details.
3095 elsif Vax_Float (Etype (N))
3096 and then Nkind (N) = N_Unchecked_Type_Conversion
3098 Expr := Expression (N);
3100 if Nkind (Expr) = N_Function_Call
3101 and then Present (Parameter_Associations (Expr))
3103 Expr := First (Parameter_Associations (Expr));
3105 if Nkind (Expr) = N_Real_Literal then
3106 return Realval (Expr);
3110 -- Peculiar VMS case, if we have xxx'Null_Parameter, return 0.0
3112 elsif Kind = N_Attribute_Reference
3113 and then Attribute_Name (N) = Name_Null_Parameter
3118 -- If we fall through, we have a node that cannot be interepreted
3119 -- as a compile time constant. That is definitely an error.
3121 raise Program_Error;
3128 function Expr_Value_S (N : Node_Id) return Node_Id is
3130 if Nkind (N) = N_String_Literal then
3133 pragma Assert (Ekind (Entity (N)) = E_Constant);
3134 return Expr_Value_S (Constant_Value (Entity (N)));
3138 --------------------------
3139 -- Flag_Non_Static_Expr --
3140 --------------------------
3142 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is
3144 if Error_Posted (Expr) and then not All_Errors_Mode then
3147 Error_Msg_F (Msg, Expr);
3148 Why_Not_Static (Expr);
3150 end Flag_Non_Static_Expr;
3156 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is
3157 Loc : constant Source_Ptr := Sloc (N);
3158 Typ : constant Entity_Id := Etype (N);
3161 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
3163 -- We now have the literal with the right value, both the actual type
3164 -- and the expected type of this literal are taken from the expression
3165 -- that was evaluated.
3168 Set_Is_Static_Expression (N, Static);
3177 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is
3178 Loc : constant Source_Ptr := Sloc (N);
3179 Typ : Entity_Id := Etype (N);
3183 -- If we are folding a named number, retain the entity in the
3184 -- literal, for ASIS use.
3186 if Is_Entity_Name (N)
3187 and then Ekind (Entity (N)) = E_Named_Integer
3194 if Is_Private_Type (Typ) then
3195 Typ := Full_View (Typ);
3198 -- For a result of type integer, subsitute an N_Integer_Literal node
3199 -- for the result of the compile time evaluation of the expression.
3201 if Is_Integer_Type (Typ) then
3202 Rewrite (N, Make_Integer_Literal (Loc, Val));
3203 Set_Original_Entity (N, Ent);
3205 -- Otherwise we have an enumeration type, and we substitute either
3206 -- an N_Identifier or N_Character_Literal to represent the enumeration
3207 -- literal corresponding to the given value, which must always be in
3208 -- range, because appropriate tests have already been made for this.
3210 else pragma Assert (Is_Enumeration_Type (Typ));
3211 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
3214 -- We now have the literal with the right value, both the actual type
3215 -- and the expected type of this literal are taken from the expression
3216 -- that was evaluated.
3219 Set_Is_Static_Expression (N, Static);
3228 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is
3229 Loc : constant Source_Ptr := Sloc (N);
3230 Typ : constant Entity_Id := Etype (N);
3234 -- If we are folding a named number, retain the entity in the
3235 -- literal, for ASIS use.
3237 if Is_Entity_Name (N)
3238 and then Ekind (Entity (N)) = E_Named_Real
3245 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
3246 Set_Original_Entity (N, Ent);
3248 -- Both the actual and expected type comes from the original expression
3251 Set_Is_Static_Expression (N, Static);
3260 function From_Bits (B : Bits; T : Entity_Id) return Uint is
3264 for J in 0 .. B'Last loop
3270 if Non_Binary_Modulus (T) then
3271 V := V mod Modulus (T);
3277 --------------------
3278 -- Get_String_Val --
3279 --------------------
3281 function Get_String_Val (N : Node_Id) return Node_Id is
3283 if Nkind (N) = N_String_Literal then
3286 elsif Nkind (N) = N_Character_Literal then
3290 pragma Assert (Is_Entity_Name (N));
3291 return Get_String_Val (Constant_Value (Entity (N)));
3299 procedure Initialize is
3301 CV_Cache := (others => (Node_High_Bound, Uint_0));
3304 --------------------
3305 -- In_Subrange_Of --
3306 --------------------
3308 function In_Subrange_Of
3311 Fixed_Int : Boolean := False) return Boolean
3320 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
3323 -- Never in range if both types are not scalar. Don't know if this can
3324 -- actually happen, but just in case.
3326 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T1) then
3330 L1 := Type_Low_Bound (T1);
3331 H1 := Type_High_Bound (T1);
3333 L2 := Type_Low_Bound (T2);
3334 H2 := Type_High_Bound (T2);
3336 -- Check bounds to see if comparison possible at compile time
3338 if Compile_Time_Compare (L1, L2) in Compare_GE
3340 Compile_Time_Compare (H1, H2) in Compare_LE
3345 -- If bounds not comparable at compile time, then the bounds of T2
3346 -- must be compile time known or we cannot answer the query.
3348 if not Compile_Time_Known_Value (L2)
3349 or else not Compile_Time_Known_Value (H2)
3354 -- If the bounds of T1 are know at compile time then use these
3355 -- ones, otherwise use the bounds of the base type (which are of
3356 -- course always static).
3358 if not Compile_Time_Known_Value (L1) then
3359 L1 := Type_Low_Bound (Base_Type (T1));
3362 if not Compile_Time_Known_Value (H1) then
3363 H1 := Type_High_Bound (Base_Type (T1));
3366 -- Fixed point types should be considered as such only if
3367 -- flag Fixed_Int is set to False.
3369 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
3370 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
3371 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
3374 Expr_Value_R (L2) <= Expr_Value_R (L1)
3376 Expr_Value_R (H2) >= Expr_Value_R (H1);
3380 Expr_Value (L2) <= Expr_Value (L1)
3382 Expr_Value (H2) >= Expr_Value (H1);
3387 -- If any exception occurs, it means that we have some bug in the compiler
3388 -- possibly triggered by a previous error, or by some unforseen peculiar
3389 -- occurrence. However, this is only an optimization attempt, so there is
3390 -- really no point in crashing the compiler. Instead we just decide, too
3391 -- bad, we can't figure out the answer in this case after all.
3396 -- Debug flag K disables this behavior (useful for debugging)
3398 if Debug_Flag_K then
3409 function Is_In_Range
3412 Fixed_Int : Boolean := False;
3413 Int_Real : Boolean := False) return Boolean
3419 -- Universal types have no range limits, so always in range
3421 if Typ = Universal_Integer or else Typ = Universal_Real then
3424 -- Never in range if not scalar type. Don't know if this can
3425 -- actually happen, but our spec allows it, so we must check!
3427 elsif not Is_Scalar_Type (Typ) then
3430 -- Never in range unless we have a compile time known value
3432 elsif not Compile_Time_Known_Value (N) then
3437 Lo : constant Node_Id := Type_Low_Bound (Typ);
3438 Hi : constant Node_Id := Type_High_Bound (Typ);
3439 LB_Known : constant Boolean := Compile_Time_Known_Value (Lo);
3440 UB_Known : constant Boolean := Compile_Time_Known_Value (Hi);
3443 -- Fixed point types should be considered as such only in
3444 -- flag Fixed_Int is set to False.
3446 if Is_Floating_Point_Type (Typ)
3447 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
3450 Valr := Expr_Value_R (N);
3452 if LB_Known and then Valr >= Expr_Value_R (Lo)
3453 and then UB_Known and then Valr <= Expr_Value_R (Hi)
3461 Val := Expr_Value (N);
3463 if LB_Known and then Val >= Expr_Value (Lo)
3464 and then UB_Known and then Val <= Expr_Value (Hi)
3479 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
3480 Typ : constant Entity_Id := Etype (Lo);
3483 if not Compile_Time_Known_Value (Lo)
3484 or else not Compile_Time_Known_Value (Hi)
3489 if Is_Discrete_Type (Typ) then
3490 return Expr_Value (Lo) > Expr_Value (Hi);
3493 pragma Assert (Is_Real_Type (Typ));
3494 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
3498 -----------------------------
3499 -- Is_OK_Static_Expression --
3500 -----------------------------
3502 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
3504 return Is_Static_Expression (N)
3505 and then not Raises_Constraint_Error (N);
3506 end Is_OK_Static_Expression;
3508 ------------------------
3509 -- Is_OK_Static_Range --
3510 ------------------------
3512 -- A static range is a range whose bounds are static expressions, or a
3513 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
3514 -- We have already converted range attribute references, so we get the
3515 -- "or" part of this rule without needing a special test.
3517 function Is_OK_Static_Range (N : Node_Id) return Boolean is
3519 return Is_OK_Static_Expression (Low_Bound (N))
3520 and then Is_OK_Static_Expression (High_Bound (N));
3521 end Is_OK_Static_Range;
3523 --------------------------
3524 -- Is_OK_Static_Subtype --
3525 --------------------------
3527 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
3528 -- where neither bound raises constraint error when evaluated.
3530 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
3531 Base_T : constant Entity_Id := Base_Type (Typ);
3532 Anc_Subt : Entity_Id;
3535 -- First a quick check on the non static subtype flag. As described
3536 -- in further detail in Einfo, this flag is not decisive in all cases,
3537 -- but if it is set, then the subtype is definitely non-static.
3539 if Is_Non_Static_Subtype (Typ) then
3543 Anc_Subt := Ancestor_Subtype (Typ);
3545 if Anc_Subt = Empty then
3549 if Is_Generic_Type (Root_Type (Base_T))
3550 or else Is_Generic_Actual_Type (Base_T)
3556 elsif Is_String_Type (Typ) then
3558 Ekind (Typ) = E_String_Literal_Subtype
3560 (Is_OK_Static_Subtype (Component_Type (Typ))
3561 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
3565 elsif Is_Scalar_Type (Typ) then
3566 if Base_T = Typ then
3570 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so
3571 -- use Get_Type_Low,High_Bound.
3573 return Is_OK_Static_Subtype (Anc_Subt)
3574 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
3575 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
3578 -- Types other than string and scalar types are never static
3583 end Is_OK_Static_Subtype;
3585 ---------------------
3586 -- Is_Out_Of_Range --
3587 ---------------------
3589 function Is_Out_Of_Range
3592 Fixed_Int : Boolean := False;
3593 Int_Real : Boolean := False) return Boolean
3599 -- Universal types have no range limits, so always in range
3601 if Typ = Universal_Integer or else Typ = Universal_Real then
3604 -- Never out of range if not scalar type. Don't know if this can
3605 -- actually happen, but our spec allows it, so we must check!
3607 elsif not Is_Scalar_Type (Typ) then
3610 -- Never out of range if this is a generic type, since the bounds
3611 -- of generic types are junk. Note that if we only checked for
3612 -- static expressions (instead of compile time known values) below,
3613 -- we would not need this check, because values of a generic type
3614 -- can never be static, but they can be known at compile time.
3616 elsif Is_Generic_Type (Typ) then
3619 -- Never out of range unless we have a compile time known value
3621 elsif not Compile_Time_Known_Value (N) then
3626 Lo : constant Node_Id := Type_Low_Bound (Typ);
3627 Hi : constant Node_Id := Type_High_Bound (Typ);
3628 LB_Known : constant Boolean := Compile_Time_Known_Value (Lo);
3629 UB_Known : constant Boolean := Compile_Time_Known_Value (Hi);
3632 -- Real types (note that fixed-point types are not treated
3633 -- as being of a real type if the flag Fixed_Int is set,
3634 -- since in that case they are regarded as integer types).
3636 if Is_Floating_Point_Type (Typ)
3637 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
3640 Valr := Expr_Value_R (N);
3642 if LB_Known and then Valr < Expr_Value_R (Lo) then
3645 elsif UB_Known and then Expr_Value_R (Hi) < Valr then
3653 Val := Expr_Value (N);
3655 if LB_Known and then Val < Expr_Value (Lo) then
3658 elsif UB_Known and then Expr_Value (Hi) < Val then
3667 end Is_Out_Of_Range;
3669 ---------------------
3670 -- Is_Static_Range --
3671 ---------------------
3673 -- A static range is a range whose bounds are static expressions, or a
3674 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
3675 -- We have already converted range attribute references, so we get the
3676 -- "or" part of this rule without needing a special test.
3678 function Is_Static_Range (N : Node_Id) return Boolean is
3680 return Is_Static_Expression (Low_Bound (N))
3681 and then Is_Static_Expression (High_Bound (N));
3682 end Is_Static_Range;
3684 -----------------------
3685 -- Is_Static_Subtype --
3686 -----------------------
3688 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
3690 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
3691 Base_T : constant Entity_Id := Base_Type (Typ);
3692 Anc_Subt : Entity_Id;
3695 -- First a quick check on the non static subtype flag. As described
3696 -- in further detail in Einfo, this flag is not decisive in all cases,
3697 -- but if it is set, then the subtype is definitely non-static.
3699 if Is_Non_Static_Subtype (Typ) then
3703 Anc_Subt := Ancestor_Subtype (Typ);
3705 if Anc_Subt = Empty then
3709 if Is_Generic_Type (Root_Type (Base_T))
3710 or else Is_Generic_Actual_Type (Base_T)
3716 elsif Is_String_Type (Typ) then
3718 Ekind (Typ) = E_String_Literal_Subtype
3720 (Is_Static_Subtype (Component_Type (Typ))
3721 and then Is_Static_Subtype (Etype (First_Index (Typ))));
3725 elsif Is_Scalar_Type (Typ) then
3726 if Base_T = Typ then
3730 return Is_Static_Subtype (Anc_Subt)
3731 and then Is_Static_Expression (Type_Low_Bound (Typ))
3732 and then Is_Static_Expression (Type_High_Bound (Typ));
3735 -- Types other than string and scalar types are never static
3740 end Is_Static_Subtype;
3742 --------------------
3743 -- Not_Null_Range --
3744 --------------------
3746 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
3747 Typ : constant Entity_Id := Etype (Lo);
3750 if not Compile_Time_Known_Value (Lo)
3751 or else not Compile_Time_Known_Value (Hi)
3756 if Is_Discrete_Type (Typ) then
3757 return Expr_Value (Lo) <= Expr_Value (Hi);
3760 pragma Assert (Is_Real_Type (Typ));
3762 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
3770 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
3772 -- We allow a maximum of 500,000 bits which seems a reasonable limit
3774 if Bits < 500_000 then
3778 Error_Msg_N ("static value too large, capacity exceeded", N);
3787 procedure Out_Of_Range (N : Node_Id) is
3789 -- If we have the static expression case, then this is an illegality
3790 -- in Ada 95 mode, except that in an instance, we never generate an
3791 -- error (if the error is legitimate, it was already diagnosed in
3792 -- the template). The expression to compute the length of a packed
3793 -- array is attached to the array type itself, and deserves a separate
3796 if Is_Static_Expression (N)
3797 and then not In_Instance
3798 and then not In_Inlined_Body
3799 and then Ada_Version >= Ada_95
3801 if Nkind (Parent (N)) = N_Defining_Identifier
3802 and then Is_Array_Type (Parent (N))
3803 and then Present (Packed_Array_Type (Parent (N)))
3804 and then Present (First_Rep_Item (Parent (N)))
3807 ("length of packed array must not exceed Integer''Last",
3808 First_Rep_Item (Parent (N)));
3809 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
3812 Apply_Compile_Time_Constraint_Error
3813 (N, "value not in range of}", CE_Range_Check_Failed);
3816 -- Here we generate a warning for the Ada 83 case, or when we are
3817 -- in an instance, or when we have a non-static expression case.
3820 Apply_Compile_Time_Constraint_Error
3821 (N, "value not in range of}?", CE_Range_Check_Failed);
3825 -------------------------
3826 -- Rewrite_In_Raise_CE --
3827 -------------------------
3829 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
3830 Typ : constant Entity_Id := Etype (N);
3833 -- If we want to raise CE in the condition of a raise_CE node
3834 -- we may as well get rid of the condition
3836 if Present (Parent (N))
3837 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
3839 Set_Condition (Parent (N), Empty);
3841 -- If the expression raising CE is a N_Raise_CE node, we can use
3842 -- that one. We just preserve the type of the context
3844 elsif Nkind (Exp) = N_Raise_Constraint_Error then
3848 -- We have to build an explicit raise_ce node
3852 Make_Raise_Constraint_Error (Sloc (Exp),
3853 Reason => CE_Range_Check_Failed));
3854 Set_Raises_Constraint_Error (N);
3857 end Rewrite_In_Raise_CE;
3859 ---------------------
3860 -- String_Type_Len --
3861 ---------------------
3863 function String_Type_Len (Stype : Entity_Id) return Uint is
3864 NT : constant Entity_Id := Etype (First_Index (Stype));
3868 if Is_OK_Static_Subtype (NT) then
3871 T := Base_Type (NT);
3874 return Expr_Value (Type_High_Bound (T)) -
3875 Expr_Value (Type_Low_Bound (T)) + 1;
3876 end String_Type_Len;
3878 ------------------------------------
3879 -- Subtypes_Statically_Compatible --
3880 ------------------------------------
3882 function Subtypes_Statically_Compatible
3884 T2 : Entity_Id) return Boolean
3887 if Is_Scalar_Type (T1) then
3889 -- Definitely compatible if we match
3891 if Subtypes_Statically_Match (T1, T2) then
3894 -- If either subtype is nonstatic then they're not compatible
3896 elsif not Is_Static_Subtype (T1)
3897 or else not Is_Static_Subtype (T2)
3901 -- If either type has constraint error bounds, then consider that
3902 -- they match to avoid junk cascaded errors here.
3904 elsif not Is_OK_Static_Subtype (T1)
3905 or else not Is_OK_Static_Subtype (T2)
3909 -- Base types must match, but we don't check that (should
3910 -- we???) but we do at least check that both types are
3911 -- real, or both types are not real.
3913 elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then
3916 -- Here we check the bounds
3920 LB1 : constant Node_Id := Type_Low_Bound (T1);
3921 HB1 : constant Node_Id := Type_High_Bound (T1);
3922 LB2 : constant Node_Id := Type_Low_Bound (T2);
3923 HB2 : constant Node_Id := Type_High_Bound (T2);
3926 if Is_Real_Type (T1) then
3928 (Expr_Value_R (LB1) > Expr_Value_R (HB1))
3930 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
3932 Expr_Value_R (HB1) <= Expr_Value_R (HB2));
3936 (Expr_Value (LB1) > Expr_Value (HB1))
3938 (Expr_Value (LB2) <= Expr_Value (LB1)
3940 Expr_Value (HB1) <= Expr_Value (HB2));
3945 elsif Is_Access_Type (T1) then
3946 return not Is_Constrained (T2)
3947 or else Subtypes_Statically_Match
3948 (Designated_Type (T1), Designated_Type (T2));
3951 return (Is_Composite_Type (T1) and then not Is_Constrained (T2))
3952 or else Subtypes_Statically_Match (T1, T2);
3954 end Subtypes_Statically_Compatible;
3956 -------------------------------
3957 -- Subtypes_Statically_Match --
3958 -------------------------------
3960 -- Subtypes statically match if they have statically matching constraints
3961 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
3962 -- they are the same identical constraint, or if they are static and the
3963 -- values match (RM 4.9.1(1)).
3965 function Subtypes_Statically_Match (T1, T2 : Entity_Id) return Boolean is
3967 -- A type always statically matches itself
3974 elsif Is_Scalar_Type (T1) then
3976 -- Base types must be the same
3978 if Base_Type (T1) /= Base_Type (T2) then
3982 -- A constrained numeric subtype never matches an unconstrained
3983 -- subtype, i.e. both types must be constrained or unconstrained.
3985 -- To understand the requirement for this test, see RM 4.9.1(1).
3986 -- As is made clear in RM 3.5.4(11), type Integer, for example
3987 -- is a constrained subtype with constraint bounds matching the
3988 -- bounds of its corresponding uncontrained base type. In this
3989 -- situation, Integer and Integer'Base do not statically match,
3990 -- even though they have the same bounds.
3992 -- We only apply this test to types in Standard and types that
3993 -- appear in user programs. That way, we do not have to be
3994 -- too careful about setting Is_Constrained right for itypes.
3996 if Is_Numeric_Type (T1)
3997 and then (Is_Constrained (T1) /= Is_Constrained (T2))
3998 and then (Scope (T1) = Standard_Standard
3999 or else Comes_From_Source (T1))
4000 and then (Scope (T2) = Standard_Standard
4001 or else Comes_From_Source (T2))
4005 -- A generic scalar type does not statically match its base
4006 -- type (AI-311). In this case we make sure that the formals,
4007 -- which are first subtypes of their bases, are constrained.
4009 elsif Is_Generic_Type (T1)
4010 and then Is_Generic_Type (T2)
4011 and then (Is_Constrained (T1) /= Is_Constrained (T2))
4016 -- If there was an error in either range, then just assume
4017 -- the types statically match to avoid further junk errors
4019 if Error_Posted (Scalar_Range (T1))
4021 Error_Posted (Scalar_Range (T2))
4026 -- Otherwise both types have bound that can be compared
4029 LB1 : constant Node_Id := Type_Low_Bound (T1);
4030 HB1 : constant Node_Id := Type_High_Bound (T1);
4031 LB2 : constant Node_Id := Type_Low_Bound (T2);
4032 HB2 : constant Node_Id := Type_High_Bound (T2);
4035 -- If the bounds are the same tree node, then match
4037 if LB1 = LB2 and then HB1 = HB2 then
4040 -- Otherwise bounds must be static and identical value
4043 if not Is_Static_Subtype (T1)
4044 or else not Is_Static_Subtype (T2)
4048 -- If either type has constraint error bounds, then say
4049 -- that 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 elsif Is_Real_Type (T1) then
4058 (Expr_Value_R (LB1) = Expr_Value_R (LB2))
4060 (Expr_Value_R (HB1) = Expr_Value_R (HB2));
4064 Expr_Value (LB1) = Expr_Value (LB2)
4066 Expr_Value (HB1) = Expr_Value (HB2);
4071 -- Type with discriminants
4073 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
4075 -- Because of view exchanges in multiple instantiations, conformance
4076 -- checking might try to match a partial view of a type with no
4077 -- discriminants with a full view that has defaulted discriminants.
4078 -- In such a case, use the discriminant constraint of the full view,
4079 -- which must exist because we know that the two subtypes have the
4082 if Has_Discriminants (T1) /= Has_Discriminants (T2) then
4084 if Is_Private_Type (T2)
4085 and then Present (Full_View (T2))
4086 and then Has_Discriminants (Full_View (T2))
4088 return Subtypes_Statically_Match (T1, Full_View (T2));
4090 elsif Is_Private_Type (T1)
4091 and then Present (Full_View (T1))
4092 and then Has_Discriminants (Full_View (T1))
4094 return Subtypes_Statically_Match (Full_View (T1), T2);
4105 DL1 : constant Elist_Id := Discriminant_Constraint (T1);
4106 DL2 : constant Elist_Id := Discriminant_Constraint (T2);
4114 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
4118 -- Now loop through the discriminant constraints
4120 -- Note: the guard here seems necessary, since it is possible at
4121 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
4123 if Present (DL1) and then Present (DL2) then
4124 DA1 := First_Elmt (DL1);
4125 DA2 := First_Elmt (DL2);
4126 while Present (DA1) loop
4128 Expr1 : constant Node_Id := Node (DA1);
4129 Expr2 : constant Node_Id := Node (DA2);
4132 if not Is_Static_Expression (Expr1)
4133 or else not Is_Static_Expression (Expr2)
4137 -- If either expression raised a constraint error,
4138 -- consider the expressions as matching, since this
4139 -- helps to prevent cascading errors.
4141 elsif Raises_Constraint_Error (Expr1)
4142 or else Raises_Constraint_Error (Expr2)
4146 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
4159 -- A definite type does not match an indefinite or classwide type
4160 -- However, a generic type with unknown discriminants may be
4161 -- instantiated with a type with no discriminants, and conformance
4162 -- checking on an inherited operation may compare the actual with
4163 -- the subtype that renames it in the instance.
4166 Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
4169 Is_Generic_Actual_Type (T1) or else Is_Generic_Actual_Type (T2);
4173 elsif Is_Array_Type (T1) then
4175 -- If either subtype is unconstrained then both must be,
4176 -- and if both are unconstrained then no further checking
4179 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
4180 return not (Is_Constrained (T1) or else Is_Constrained (T2));
4183 -- Both subtypes are constrained, so check that the index
4184 -- subtypes statically match.
4187 Index1 : Node_Id := First_Index (T1);
4188 Index2 : Node_Id := First_Index (T2);
4191 while Present (Index1) loop
4193 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
4198 Next_Index (Index1);
4199 Next_Index (Index2);
4205 elsif Is_Access_Type (T1) then
4206 if Can_Never_Be_Null (T1) /= Can_Never_Be_Null (T2) then
4209 elsif Ekind (T1) = E_Access_Subprogram_Type
4210 or else Ekind (T1) = E_Anonymous_Access_Subprogram_Type
4214 (Designated_Type (T1),
4215 Designated_Type (T2));
4218 Subtypes_Statically_Match
4219 (Designated_Type (T1),
4220 Designated_Type (T2))
4221 and then Is_Access_Constant (T1) = Is_Access_Constant (T2);
4224 -- All other types definitely match
4229 end Subtypes_Statically_Match;
4235 function Test (Cond : Boolean) return Uint is
4244 ---------------------------------
4245 -- Test_Expression_Is_Foldable --
4246 ---------------------------------
4250 procedure Test_Expression_Is_Foldable
4260 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
4264 -- If operand is Any_Type, just propagate to result and do not
4265 -- try to fold, this prevents cascaded errors.
4267 if Etype (Op1) = Any_Type then
4268 Set_Etype (N, Any_Type);
4271 -- If operand raises constraint error, then replace node N with the
4272 -- raise constraint error node, and we are obviously not foldable.
4273 -- Note that this replacement inherits the Is_Static_Expression flag
4274 -- from the operand.
4276 elsif Raises_Constraint_Error (Op1) then
4277 Rewrite_In_Raise_CE (N, Op1);
4280 -- If the operand is not static, then the result is not static, and
4281 -- all we have to do is to check the operand since it is now known
4282 -- to appear in a non-static context.
4284 elsif not Is_Static_Expression (Op1) then
4285 Check_Non_Static_Context (Op1);
4286 Fold := Compile_Time_Known_Value (Op1);
4289 -- An expression of a formal modular type is not foldable because
4290 -- the modulus is unknown.
4292 elsif Is_Modular_Integer_Type (Etype (Op1))
4293 and then Is_Generic_Type (Etype (Op1))
4295 Check_Non_Static_Context (Op1);
4298 -- Here we have the case of an operand whose type is OK, which is
4299 -- static, and which does not raise constraint error, we can fold.
4302 Set_Is_Static_Expression (N);
4306 end Test_Expression_Is_Foldable;
4310 procedure Test_Expression_Is_Foldable
4317 Rstat : constant Boolean := Is_Static_Expression (Op1)
4318 and then Is_Static_Expression (Op2);
4324 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
4328 -- If either operand is Any_Type, just propagate to result and
4329 -- do not try to fold, this prevents cascaded errors.
4331 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
4332 Set_Etype (N, Any_Type);
4335 -- If left operand raises constraint error, then replace node N with
4336 -- the raise constraint error node, and we are obviously not foldable.
4337 -- Is_Static_Expression is set from the two operands in the normal way,
4338 -- and we check the right operand if it is in a non-static context.
4340 elsif Raises_Constraint_Error (Op1) then
4342 Check_Non_Static_Context (Op2);
4345 Rewrite_In_Raise_CE (N, Op1);
4346 Set_Is_Static_Expression (N, Rstat);
4349 -- Similar processing for the case of the right operand. Note that
4350 -- we don't use this routine for the short-circuit case, so we do
4351 -- not have to worry about that special case here.
4353 elsif Raises_Constraint_Error (Op2) then
4355 Check_Non_Static_Context (Op1);
4358 Rewrite_In_Raise_CE (N, Op2);
4359 Set_Is_Static_Expression (N, Rstat);
4362 -- Exclude expressions of a generic modular type, as above
4364 elsif Is_Modular_Integer_Type (Etype (Op1))
4365 and then Is_Generic_Type (Etype (Op1))
4367 Check_Non_Static_Context (Op1);
4370 -- If result is not static, then check non-static contexts on operands
4371 -- since one of them may be static and the other one may not be static
4373 elsif not Rstat then
4374 Check_Non_Static_Context (Op1);
4375 Check_Non_Static_Context (Op2);
4376 Fold := Compile_Time_Known_Value (Op1)
4377 and then Compile_Time_Known_Value (Op2);
4380 -- Else result is static and foldable. Both operands are static,
4381 -- and neither raises constraint error, so we can definitely fold.
4384 Set_Is_Static_Expression (N);
4389 end Test_Expression_Is_Foldable;
4395 procedure To_Bits (U : Uint; B : out Bits) is
4397 for J in 0 .. B'Last loop
4398 B (J) := (U / (2 ** J)) mod 2 /= 0;
4402 --------------------
4403 -- Why_Not_Static --
4404 --------------------
4406 procedure Why_Not_Static (Expr : Node_Id) is
4407 N : constant Node_Id := Original_Node (Expr);
4411 procedure Why_Not_Static_List (L : List_Id);
4412 -- A version that can be called on a list of expressions. Finds
4413 -- all non-static violations in any element of the list.
4415 -------------------------
4416 -- Why_Not_Static_List --
4417 -------------------------
4419 procedure Why_Not_Static_List (L : List_Id) is
4423 if Is_Non_Empty_List (L) then
4425 while Present (N) loop
4430 end Why_Not_Static_List;
4432 -- Start of processing for Why_Not_Static
4435 -- If in ACATS mode (debug flag 2), then suppress all these
4436 -- messages, this avoids massive updates to the ACATS base line.
4438 if Debug_Flag_2 then
4442 -- Ignore call on error or empty node
4444 if No (Expr) or else Nkind (Expr) = N_Error then
4448 -- Preprocessing for sub expressions
4450 if Nkind (Expr) in N_Subexpr then
4452 -- Nothing to do if expression is static
4454 if Is_OK_Static_Expression (Expr) then
4458 -- Test for constraint error raised
4460 if Raises_Constraint_Error (Expr) then
4462 ("expression raises exception, cannot be static " &
4463 "(RM 4.9(34))!", N);
4467 -- If no type, then something is pretty wrong, so ignore
4469 Typ := Etype (Expr);
4475 -- Type must be scalar or string type
4477 if not Is_Scalar_Type (Typ)
4478 and then not Is_String_Type (Typ)
4481 ("static expression must have scalar or string type " &
4487 -- If we got through those checks, test particular node kind
4490 when N_Expanded_Name | N_Identifier | N_Operator_Symbol =>
4493 if Is_Named_Number (E) then
4496 elsif Ekind (E) = E_Constant then
4497 if not Is_Static_Expression (Constant_Value (E)) then
4499 ("& is not a static constant (RM 4.9(5))!", N, E);
4504 ("& is not static constant or named number " &
4505 "(RM 4.9(5))!", N, E);
4508 when N_Binary_Op | N_And_Then | N_Or_Else | N_Membership_Test =>
4509 if Nkind (N) in N_Op_Shift then
4511 ("shift functions are never static (RM 4.9(6,18))!", N);
4514 Why_Not_Static (Left_Opnd (N));
4515 Why_Not_Static (Right_Opnd (N));
4519 Why_Not_Static (Right_Opnd (N));
4521 when N_Attribute_Reference =>
4522 Why_Not_Static_List (Expressions (N));
4524 E := Etype (Prefix (N));
4526 if E = Standard_Void_Type then
4530 -- Special case non-scalar'Size since this is a common error
4532 if Attribute_Name (N) = Name_Size then
4534 ("size attribute is only static for scalar type " &
4535 "(RM 4.9(7,8))", N);
4539 elsif Is_Array_Type (E) then
4540 if Attribute_Name (N) /= Name_First
4542 Attribute_Name (N) /= Name_Last
4544 Attribute_Name (N) /= Name_Length
4547 ("static array attribute must be Length, First, or Last " &
4550 -- Since we know the expression is not-static (we already
4551 -- tested for this, must mean array is not static).
4555 ("prefix is non-static array (RM 4.9(8))!", Prefix (N));
4560 -- Special case generic types, since again this is a common
4561 -- source of confusion.
4563 elsif Is_Generic_Actual_Type (E)
4568 ("attribute of generic type is never static " &
4569 "(RM 4.9(7,8))!", N);
4571 elsif Is_Static_Subtype (E) then
4574 elsif Is_Scalar_Type (E) then
4576 ("prefix type for attribute is not static scalar subtype " &
4581 ("static attribute must apply to array/scalar type " &
4582 "(RM 4.9(7,8))!", N);
4585 when N_String_Literal =>
4587 ("subtype of string literal is non-static (RM 4.9(4))!", N);
4589 when N_Explicit_Dereference =>
4591 ("explicit dereference is never static (RM 4.9)!", N);
4593 when N_Function_Call =>
4594 Why_Not_Static_List (Parameter_Associations (N));
4595 Error_Msg_N ("non-static function call (RM 4.9(6,18))!", N);
4597 when N_Parameter_Association =>
4598 Why_Not_Static (Explicit_Actual_Parameter (N));
4600 when N_Indexed_Component =>
4602 ("indexed component is never static (RM 4.9)!", N);
4604 when N_Procedure_Call_Statement =>
4606 ("procedure call is never static (RM 4.9)!", N);
4608 when N_Qualified_Expression =>
4609 Why_Not_Static (Expression (N));
4611 when N_Aggregate | N_Extension_Aggregate =>
4613 ("an aggregate is never static (RM 4.9)!", N);
4616 Why_Not_Static (Low_Bound (N));
4617 Why_Not_Static (High_Bound (N));
4619 when N_Range_Constraint =>
4620 Why_Not_Static (Range_Expression (N));
4622 when N_Subtype_Indication =>
4623 Why_Not_Static (Constraint (N));
4625 when N_Selected_Component =>
4627 ("selected component is never static (RM 4.9)!", N);
4631 ("slice is never static (RM 4.9)!", N);
4633 when N_Type_Conversion =>
4634 Why_Not_Static (Expression (N));
4636 if not Is_Scalar_Type (Etype (Prefix (N)))
4637 or else not Is_Static_Subtype (Etype (Prefix (N)))
4640 ("static conversion requires static scalar subtype result " &
4644 when N_Unchecked_Type_Conversion =>
4646 ("unchecked type conversion is never static (RM 4.9)!", N);