1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Checks; use Checks;
31 with Debug; use Debug;
32 with Einfo; use Einfo;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Eval_Fat; use Eval_Fat;
36 with Nmake; use Nmake;
37 with Nlists; use Nlists;
40 with Sem_Cat; use Sem_Cat;
41 with Sem_Ch8; use Sem_Ch8;
42 with Sem_Res; use Sem_Res;
43 with Sem_Util; use Sem_Util;
44 with Sem_Type; use Sem_Type;
45 with Sem_Warn; use Sem_Warn;
46 with Sinfo; use Sinfo;
47 with Snames; use Snames;
48 with Stand; use Stand;
49 with Stringt; use Stringt;
51 package body Sem_Eval is
53 -----------------------------------------
54 -- Handling of Compile Time Evaluation --
55 -----------------------------------------
57 -- The compile time evaluation of expressions is distributed over several
58 -- Eval_xxx procedures. These procedures are called immediatedly after
59 -- a subexpression is resolved and is therefore accomplished in a bottom
60 -- up fashion. The flags are synthesized using the following approach.
62 -- Is_Static_Expression is determined by following the detailed rules
63 -- in RM 4.9(4-14). This involves testing the Is_Static_Expression
64 -- flag of the operands in many cases.
66 -- Raises_Constraint_Error is set if any of the operands have the flag
67 -- set or if an attempt to compute the value of the current expression
68 -- results in detection of a runtime constraint error.
70 -- As described in the spec, the requirement is that Is_Static_Expression
71 -- be accurately set, and in addition for nodes for which this flag is set,
72 -- Raises_Constraint_Error must also be set. Furthermore a node which has
73 -- Is_Static_Expression set, and Raises_Constraint_Error clear, then the
74 -- requirement is that the expression value must be precomputed, and the
75 -- node is either a literal, or the name of a constant entity whose value
76 -- is a static expression.
78 -- The general approach is as follows. First compute Is_Static_Expression.
79 -- If the node is not static, then the flag is left off in the node and
80 -- we are all done. Otherwise for a static node, we test if any of the
81 -- operands will raise constraint error, and if so, propagate the flag
82 -- Raises_Constraint_Error to the result node and we are done (since the
83 -- error was already posted at a lower level).
85 -- For the case of a static node whose operands do not raise constraint
86 -- error, we attempt to evaluate the node. If this evaluation succeeds,
87 -- then the node is replaced by the result of this computation. If the
88 -- evaluation raises constraint error, then we rewrite the node with
89 -- Apply_Compile_Time_Constraint_Error to raise the exception and also
90 -- to post appropriate error messages.
96 type Bits is array (Nat range <>) of Boolean;
97 -- Used to convert unsigned (modular) values for folding logical ops
99 -----------------------
100 -- Local Subprograms --
101 -----------------------
103 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean;
104 -- Bits represents the number of bits in an integer value to be computed
105 -- (but the value has not been computed yet). If this value in Bits is
106 -- reasonable, a result of True is returned, with the implication that
107 -- the caller should go ahead and complete the calculation. If the value
108 -- in Bits is unreasonably large, then an error is posted on node N, and
109 -- False is returned (and the caller skips the proposed calculation).
111 function From_Bits (B : Bits; T : Entity_Id) return Uint;
112 -- Converts a bit string of length B'Length to a Uint value to be used
113 -- for a target of type T, which is a modular type. This procedure
114 -- includes the necessary reduction by the modulus in the case of a
115 -- non-binary modulus (for a binary modulus, the bit string is the
116 -- right length any way so all is well).
118 function Get_String_Val (N : Node_Id) return Node_Id;
119 -- Given a tree node for a folded string or character value, returns
120 -- the corresponding string literal or character literal (one of the
121 -- two must be available, or the operand would not have been marked
122 -- as foldable in the earlier analysis of the operation).
124 procedure Out_Of_Range (N : Node_Id);
125 -- This procedure is called if it is determined that node N, which
126 -- appears in a non-static context, is a compile time known value
127 -- which is outside its range, i.e. the range of Etype. This is used
128 -- in contexts where this is an illegality if N is static, and should
129 -- generate a warning otherwise.
131 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id);
132 -- N and Exp are nodes representing an expression, Exp is known
133 -- to raise CE. N is rewritten in term of Exp in the optimal way.
135 function String_Type_Len (Stype : Entity_Id) return Uint;
136 -- Given a string type, determines the length of the index type, or,
137 -- if this index type is non-static, the length of the base type of
138 -- this index type. Note that if the string type is itself static,
139 -- then the index type is static, so the second case applies only
140 -- if the string type passed is non-static.
142 function Test (Cond : Boolean) return Uint;
143 pragma Inline (Test);
144 -- This function simply returns the appropriate Boolean'Pos value
145 -- corresponding to the value of Cond as a universal integer. It is
146 -- used for producing the result of the static evaluation of the
149 procedure Test_Expression_Is_Foldable
154 -- Tests to see if expression N whose single operand is Op1 is foldable,
155 -- i.e. the operand value is known at compile time. If the operation is
156 -- foldable, then Fold is True on return, and Stat indicates whether
157 -- the result is static (i.e. both operands were static). Note that it
158 -- is quite possible for Fold to be True, and Stat to be False, since
159 -- there are cases in which we know the value of an operand even though
160 -- it is not technically static (e.g. the static lower bound of a range
161 -- whose upper bound is non-static).
163 -- If Stat is set False on return, then Expression_Is_Foldable makes a
164 -- call to Check_Non_Static_Context on the operand. If Fold is False on
165 -- return, then all processing is complete, and the caller should
166 -- return, since there is nothing else to do.
168 procedure Test_Expression_Is_Foldable
174 -- Same processing, except applies to an expression N with two operands
177 procedure To_Bits (U : Uint; B : out Bits);
178 -- Converts a Uint value to a bit string of length B'Length
180 ------------------------------
181 -- Check_Non_Static_Context --
182 ------------------------------
184 procedure Check_Non_Static_Context (N : Node_Id) is
185 T : Entity_Id := Etype (N);
186 Checks_On : constant Boolean :=
187 not Index_Checks_Suppressed (T)
188 and not Range_Checks_Suppressed (T);
191 -- We need the check only for static expressions not raising CE
192 -- We can also ignore cases in which the type is Any_Type
194 if not Is_OK_Static_Expression (N)
195 or else Etype (N) = Any_Type
199 -- Skip this check for non-scalar expressions
201 elsif not Is_Scalar_Type (T) then
205 -- Here we have the case of outer level static expression of
206 -- scalar type, where the processing of this procedure is needed.
208 -- For real types, this is where we convert the value to a machine
209 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should
210 -- only need to do this if the parent is a constant declaration,
211 -- since in other cases, gigi should do the necessary conversion
212 -- correctly, but experimentation shows that this is not the case
213 -- on all machines, in particular if we do not convert all literals
214 -- to machine values in non-static contexts, then ACVC test C490001
215 -- fails on Sparc/Solaris and SGI/Irix.
217 if Nkind (N) = N_Real_Literal
218 and then not Is_Machine_Number (N)
219 and then not Is_Generic_Type (Etype (N))
220 and then Etype (N) /= Universal_Real
221 and then not Debug_Flag_S
222 and then (not Debug_Flag_T
224 (Nkind (Parent (N)) = N_Object_Declaration
225 and then Constant_Present (Parent (N))))
227 -- Check that value is in bounds before converting to machine
228 -- number, so as not to lose case where value overflows in the
229 -- least significant bit or less. See B490001.
231 if Is_Out_Of_Range (N, Base_Type (T)) then
236 -- Note: we have to copy the node, to avoid problems with conformance
237 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
239 Rewrite (N, New_Copy (N));
241 if not Is_Floating_Point_Type (T) then
243 (N, Corresponding_Integer_Value (N) * Small_Value (T));
245 elsif not UR_Is_Zero (Realval (N)) then
247 RT : constant Entity_Id := Base_Type (T);
248 X : constant Ureal := Machine (RT, Realval (N), Round);
251 -- Warn if result of static rounding actually differs from
252 -- runtime evaluation, which uses round to even.
254 if Warn_On_Biased_Rounding and Rounding_Was_Biased then
255 Error_Msg_N ("static expression does not round to even"
256 & " ('R'M 4.9(38))?", N);
263 Set_Is_Machine_Number (N);
266 -- Check for out of range universal integer. This is a non-static
267 -- context, so the integer value must be in range of the runtime
268 -- representation of universal integers.
270 -- We do this only within an expression, because that is the only
271 -- case in which non-static universal integer values can occur, and
272 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
273 -- called in contexts like the expression of a number declaration where
274 -- we certainly want to allow out of range values.
276 if Etype (N) = Universal_Integer
277 and then Nkind (N) = N_Integer_Literal
278 and then Nkind (Parent (N)) in N_Subexpr
280 (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer))
282 Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer)))
284 Apply_Compile_Time_Constraint_Error
285 (N, "non-static universal integer value out of range?");
287 -- Check out of range of base type
289 elsif Is_Out_Of_Range (N, Base_Type (T)) then
292 -- Give warning if outside subtype (where one or both of the
293 -- bounds of the subtype is static). This warning is omitted
294 -- if the expression appears in a range that could be null
295 -- (warnings are handled elsewhere for this case).
297 elsif T /= Base_Type (T)
298 and then Nkind (Parent (N)) /= N_Range
300 if Is_In_Range (N, T) then
303 elsif Is_Out_Of_Range (N, T) then
304 Apply_Compile_Time_Constraint_Error
305 (N, "value not in range of}?");
308 Enable_Range_Check (N);
311 Set_Do_Range_Check (N, False);
314 end Check_Non_Static_Context;
316 ---------------------------------
317 -- Check_String_Literal_Length --
318 ---------------------------------
320 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is
322 if not Raises_Constraint_Error (N)
323 and then Is_Constrained (Ttype)
326 UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype)
328 Apply_Compile_Time_Constraint_Error
329 (N, "string length wrong for}?",
334 end Check_String_Literal_Length;
336 --------------------------
337 -- Compile_Time_Compare --
338 --------------------------
340 function Compile_Time_Compare (L, R : Node_Id) return Compare_Result is
341 Ltyp : constant Entity_Id := Etype (L);
342 Rtyp : constant Entity_Id := Etype (R);
344 procedure Compare_Decompose
348 -- This procedure decomposes the node N into an expression node
349 -- and a signed offset, so that the value of N is equal to the
350 -- value of R plus the value V (which may be negative). If no
351 -- such decomposition is possible, then on return R is a copy
352 -- of N, and V is set to zero.
354 function Compare_Fixup (N : Node_Id) return Node_Id;
355 -- This function deals with replacing 'Last and 'First references
356 -- with their corresponding type bounds, which we then can compare.
357 -- The argument is the original node, the result is the identity,
358 -- unless we have a 'Last/'First reference in which case the value
359 -- returned is the appropriate type bound.
361 function Is_Same_Value (L, R : Node_Id) return Boolean;
362 -- Returns True iff L and R represent expressions that definitely
363 -- have identical (but not necessarily compile time known) values
364 -- Indeed the caller is expected to have already dealt with the
365 -- cases of compile time known values, so these are not tested here.
367 -----------------------
368 -- Compare_Decompose --
369 -----------------------
371 procedure Compare_Decompose
377 if Nkind (N) = N_Op_Add
378 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
381 V := Intval (Right_Opnd (N));
384 elsif Nkind (N) = N_Op_Subtract
385 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
388 V := UI_Negate (Intval (Right_Opnd (N)));
391 elsif Nkind (N) = N_Attribute_Reference then
393 if Attribute_Name (N) = Name_Succ then
394 R := First (Expressions (N));
398 elsif Attribute_Name (N) = Name_Pred then
399 R := First (Expressions (N));
407 end Compare_Decompose;
413 function Compare_Fixup (N : Node_Id) return Node_Id is
419 if Nkind (N) = N_Attribute_Reference
420 and then (Attribute_Name (N) = Name_First
422 Attribute_Name (N) = Name_Last)
424 Xtyp := Etype (Prefix (N));
426 -- If we have no type, then just abandon the attempt to do
427 -- a fixup, this is probably the result of some other error.
433 -- Dereference an access type
435 if Is_Access_Type (Xtyp) then
436 Xtyp := Designated_Type (Xtyp);
439 -- If we don't have an array type at this stage, something
440 -- is peculiar, e.g. another error, and we abandon the attempt
443 if not Is_Array_Type (Xtyp) then
447 -- Ignore unconstrained array, since bounds are not meaningful
449 if not Is_Constrained (Xtyp) then
453 if Ekind (Xtyp) = E_String_Literal_Subtype then
454 if Attribute_Name (N) = Name_First then
455 return String_Literal_Low_Bound (Xtyp);
457 else -- Attribute_Name (N) = Name_Last
458 return Make_Integer_Literal (Sloc (N),
459 Intval => Intval (String_Literal_Low_Bound (Xtyp))
460 + String_Literal_Length (Xtyp));
464 -- Find correct index type
466 Indx := First_Index (Xtyp);
468 if Present (Expressions (N)) then
469 Subs := UI_To_Int (Expr_Value (First (Expressions (N))));
471 for J in 2 .. Subs loop
472 Indx := Next_Index (Indx);
476 Xtyp := Etype (Indx);
478 if Attribute_Name (N) = Name_First then
479 return Type_Low_Bound (Xtyp);
481 else -- Attribute_Name (N) = Name_Last
482 return Type_High_Bound (Xtyp);
493 function Is_Same_Value (L, R : Node_Id) return Boolean is
494 Lf : constant Node_Id := Compare_Fixup (L);
495 Rf : constant Node_Id := Compare_Fixup (R);
498 -- Values are the same if they are the same identifier and the
499 -- identifier refers to a constant object (E_Constant)
501 if Nkind (Lf) = N_Identifier and then Nkind (Rf) = N_Identifier
502 and then Entity (Lf) = Entity (Rf)
503 and then (Ekind (Entity (Lf)) = E_Constant or else
504 Ekind (Entity (Lf)) = E_In_Parameter or else
505 Ekind (Entity (Lf)) = E_Loop_Parameter)
509 -- Or if they are compile time known and identical
511 elsif Compile_Time_Known_Value (Lf)
513 Compile_Time_Known_Value (Rf)
514 and then Expr_Value (Lf) = Expr_Value (Rf)
518 -- Or if they are both 'First or 'Last values applying to the
519 -- same entity (first and last don't change even if value does)
521 elsif Nkind (Lf) = N_Attribute_Reference
523 Nkind (Rf) = N_Attribute_Reference
524 and then Attribute_Name (Lf) = Attribute_Name (Rf)
525 and then (Attribute_Name (Lf) = Name_First
527 Attribute_Name (Lf) = Name_Last)
528 and then Is_Entity_Name (Prefix (Lf))
529 and then Is_Entity_Name (Prefix (Rf))
530 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
534 -- All other cases, we can't tell
541 -- Start of processing for Compile_Time_Compare
547 -- If expressions have no types, then do not attempt to determine
548 -- if they are the same, since something funny is going on. One
549 -- case in which this happens is during generic template analysis,
550 -- when bounds are not fully analyzed.
552 elsif No (Ltyp) or else No (Rtyp) then
555 -- We only attempt compile time analysis for scalar values
557 elsif not Is_Scalar_Type (Ltyp)
558 or else Is_Packed_Array_Type (Ltyp)
562 -- Case where comparison involves two compile time known values
564 elsif Compile_Time_Known_Value (L)
565 and then Compile_Time_Known_Value (R)
567 -- For the floating-point case, we have to be a little careful, since
568 -- at compile time we are dealing with universal exact values, but at
569 -- runtime, these will be in non-exact target form. That's why the
570 -- returned results are LE and GE below instead of LT and GT.
572 if Is_Floating_Point_Type (Ltyp)
574 Is_Floating_Point_Type (Rtyp)
577 Lo : constant Ureal := Expr_Value_R (L);
578 Hi : constant Ureal := Expr_Value_R (R);
590 -- For the integer case we know exactly (note that this includes the
591 -- fixed-point case, where we know the run time integer values now)
595 Lo : constant Uint := Expr_Value (L);
596 Hi : constant Uint := Expr_Value (R);
609 -- Cases where at least one operand is not known at compile time
612 -- Here is where we check for comparisons against maximum bounds of
613 -- types, where we know that no value can be outside the bounds of
614 -- the subtype. Note that this routine is allowed to assume that all
615 -- expressions are within their subtype bounds. Callers wishing to
616 -- deal with possibly invalid values must in any case take special
617 -- steps (e.g. conversions to larger types) to avoid this kind of
618 -- optimization, which is always considered to be valid. We do not
619 -- attempt this optimization with generic types, since the type
620 -- bounds may not be meaningful in this case.
622 if Is_Discrete_Type (Ltyp)
623 and then not Is_Generic_Type (Ltyp)
624 and then not Is_Generic_Type (Rtyp)
626 if Is_Same_Value (R, Type_High_Bound (Ltyp)) then
629 elsif Is_Same_Value (R, Type_Low_Bound (Ltyp)) then
632 elsif Is_Same_Value (L, Type_High_Bound (Rtyp)) then
635 elsif Is_Same_Value (L, Type_Low_Bound (Ltyp)) then
640 -- Next attempt is to decompose the expressions to extract
641 -- a constant offset resulting from the use of any of the forms:
648 -- Then we see if the two expressions are the same value, and if so
649 -- the result is obtained by comparing the offsets.
658 Compare_Decompose (L, Lnode, Loffs);
659 Compare_Decompose (R, Rnode, Roffs);
661 if Is_Same_Value (Lnode, Rnode) then
662 if Loffs = Roffs then
665 elsif Loffs < Roffs then
672 -- If the expressions are different, we cannot say at compile
673 -- time how they compare, so we return the Unknown indication.
680 end Compile_Time_Compare;
682 ------------------------------
683 -- Compile_Time_Known_Value --
684 ------------------------------
686 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
687 K : constant Node_Kind := Nkind (Op);
690 -- Never known at compile time if bad type or raises constraint error
691 -- or empty (latter case occurs only as a result of a previous error)
695 or else Etype (Op) = Any_Type
696 or else Raises_Constraint_Error (Op)
701 -- If we have an entity name, then see if it is the name of a constant
702 -- and if so, test the corresponding constant value, or the name of
703 -- an enumeration literal, which is always a constant.
705 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
707 E : constant Entity_Id := Entity (Op);
711 -- Never known at compile time if it is a packed array value.
712 -- We might want to try to evaluate these at compile time one
713 -- day, but we do not make that attempt now.
715 if Is_Packed_Array_Type (Etype (Op)) then
719 if Ekind (E) = E_Enumeration_Literal then
722 elsif Ekind (E) /= E_Constant then
726 V := Constant_Value (E);
727 return Present (V) and then Compile_Time_Known_Value (V);
731 -- We have a value, see if it is compile time known
734 -- Literals and NULL are known at compile time
736 if K = N_Integer_Literal
738 K = N_Character_Literal
748 -- Any reference to Null_Parameter is known at compile time. No
749 -- other attribute references (that have not already been folded)
750 -- are known at compile time.
752 elsif K = N_Attribute_Reference then
753 return Attribute_Name (Op) = Name_Null_Parameter;
755 -- All other types of values are not known at compile time
762 end Compile_Time_Known_Value;
764 --------------------------------------
765 -- Compile_Time_Known_Value_Or_Aggr --
766 --------------------------------------
768 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is
770 -- If we have an entity name, then see if it is the name of a constant
771 -- and if so, test the corresponding constant value, or the name of
772 -- an enumeration literal, which is always a constant.
774 if Is_Entity_Name (Op) then
776 E : constant Entity_Id := Entity (Op);
780 if Ekind (E) = E_Enumeration_Literal then
783 elsif Ekind (E) /= E_Constant then
787 V := Constant_Value (E);
789 and then Compile_Time_Known_Value_Or_Aggr (V);
793 -- We have a value, see if it is compile time known
796 if Compile_Time_Known_Value (Op) then
799 elsif Nkind (Op) = N_Aggregate then
801 if Present (Expressions (Op)) then
806 Expr := First (Expressions (Op));
807 while Present (Expr) loop
808 if not Compile_Time_Known_Value_Or_Aggr (Expr) then
817 if Present (Component_Associations (Op)) then
822 Cass := First (Component_Associations (Op));
823 while Present (Cass) loop
825 Compile_Time_Known_Value_Or_Aggr (Expression (Cass))
837 -- All other types of values are not known at compile time
844 end Compile_Time_Known_Value_Or_Aggr;
850 -- This is only called for actuals of functions that are not predefined
851 -- operators (which have already been rewritten as operators at this
852 -- stage), so the call can never be folded, and all that needs doing for
853 -- the actual is to do the check for a non-static context.
855 procedure Eval_Actual (N : Node_Id) is
857 Check_Non_Static_Context (N);
864 -- Allocators are never static, so all we have to do is to do the
865 -- check for a non-static context if an expression is present.
867 procedure Eval_Allocator (N : Node_Id) is
868 Expr : constant Node_Id := Expression (N);
871 if Nkind (Expr) = N_Qualified_Expression then
872 Check_Non_Static_Context (Expression (Expr));
876 ------------------------
877 -- Eval_Arithmetic_Op --
878 ------------------------
880 -- Arithmetic operations are static functions, so the result is static
881 -- if both operands are static (RM 4.9(7), 4.9(20)).
883 procedure Eval_Arithmetic_Op (N : Node_Id) is
884 Left : constant Node_Id := Left_Opnd (N);
885 Right : constant Node_Id := Right_Opnd (N);
886 Ltype : constant Entity_Id := Etype (Left);
887 Rtype : constant Entity_Id := Etype (Right);
892 -- If not foldable we are done
894 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
900 -- Fold for cases where both operands are of integer type
902 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
904 Left_Int : constant Uint := Expr_Value (Left);
905 Right_Int : constant Uint := Expr_Value (Right);
912 Result := Left_Int + Right_Int;
914 when N_Op_Subtract =>
915 Result := Left_Int - Right_Int;
917 when N_Op_Multiply =>
920 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
922 Result := Left_Int * Right_Int;
929 -- The exception Constraint_Error is raised by integer
930 -- division, rem and mod if the right operand is zero.
932 if Right_Int = 0 then
933 Apply_Compile_Time_Constraint_Error
934 (N, "division by zero");
937 Result := Left_Int / Right_Int;
942 -- The exception Constraint_Error is raised by integer
943 -- division, rem and mod if the right operand is zero.
945 if Right_Int = 0 then
946 Apply_Compile_Time_Constraint_Error
947 (N, "mod with zero divisor");
950 Result := Left_Int mod Right_Int;
955 -- The exception Constraint_Error is raised by integer
956 -- division, rem and mod if the right operand is zero.
958 if Right_Int = 0 then
959 Apply_Compile_Time_Constraint_Error
960 (N, "rem with zero divisor");
963 Result := Left_Int rem Right_Int;
970 -- Adjust the result by the modulus if the type is a modular type
972 if Is_Modular_Integer_Type (Ltype) then
973 Result := Result mod Modulus (Ltype);
976 Fold_Uint (N, Result);
979 -- Cases where at least one operand is a real. We handle the cases
980 -- of both reals, or mixed/real integer cases (the latter happen
981 -- only for divide and multiply, and the result is always real).
983 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
990 if Is_Real_Type (Ltype) then
991 Left_Real := Expr_Value_R (Left);
993 Left_Real := UR_From_Uint (Expr_Value (Left));
996 if Is_Real_Type (Rtype) then
997 Right_Real := Expr_Value_R (Right);
999 Right_Real := UR_From_Uint (Expr_Value (Right));
1002 if Nkind (N) = N_Op_Add then
1003 Result := Left_Real + Right_Real;
1005 elsif Nkind (N) = N_Op_Subtract then
1006 Result := Left_Real - Right_Real;
1008 elsif Nkind (N) = N_Op_Multiply then
1009 Result := Left_Real * Right_Real;
1011 else pragma Assert (Nkind (N) = N_Op_Divide);
1012 if UR_Is_Zero (Right_Real) then
1013 Apply_Compile_Time_Constraint_Error
1014 (N, "division by zero");
1018 Result := Left_Real / Right_Real;
1021 Fold_Ureal (N, Result);
1025 Set_Is_Static_Expression (N, Stat);
1027 end Eval_Arithmetic_Op;
1029 ----------------------------
1030 -- Eval_Character_Literal --
1031 ----------------------------
1033 -- Nothing to be done!
1035 procedure Eval_Character_Literal (N : Node_Id) is
1038 end Eval_Character_Literal;
1040 ------------------------
1041 -- Eval_Concatenation --
1042 ------------------------
1044 -- Concatenation is a static function, so the result is static if
1045 -- both operands are static (RM 4.9(7), 4.9(21)).
1047 procedure Eval_Concatenation (N : Node_Id) is
1048 Left : constant Node_Id := Left_Opnd (N);
1049 Right : constant Node_Id := Right_Opnd (N);
1052 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
1055 -- Concatenation is never static in Ada 83, so if Ada 83
1056 -- check operand non-static context
1059 and then Comes_From_Source (N)
1061 Check_Non_Static_Context (Left);
1062 Check_Non_Static_Context (Right);
1066 -- If not foldable we are done. In principle concatenation that yields
1067 -- any string type is static (i.e. an array type of character types).
1068 -- However, character types can include enumeration literals, and
1069 -- concatenation in that case cannot be described by a literal, so we
1070 -- only consider the operation static if the result is an array of
1071 -- (a descendant of) a predefined character type.
1073 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1075 if (C_Typ = Standard_Character
1076 or else C_Typ = Standard_Wide_Character)
1081 Set_Is_Static_Expression (N, False);
1085 -- Compile time string concatenation.
1087 -- ??? Note that operands that are aggregates can be marked as
1088 -- static, so we should attempt at a later stage to fold
1089 -- concatenations with such aggregates.
1092 Left_Str : constant Node_Id := Get_String_Val (Left);
1093 Right_Str : constant Node_Id := Get_String_Val (Right);
1096 -- Establish new string literal, and store left operand. We make
1097 -- sure to use the special Start_String that takes an operand if
1098 -- the left operand is a string literal. Since this is optimized
1099 -- in the case where that is the most recently created string
1100 -- literal, we ensure efficient time/space behavior for the
1101 -- case of a concatenation of a series of string literals.
1103 if Nkind (Left_Str) = N_String_Literal then
1104 Start_String (Strval (Left_Str));
1107 Store_String_Char (Char_Literal_Value (Left_Str));
1110 -- Now append the characters of the right operand
1112 if Nkind (Right_Str) = N_String_Literal then
1114 S : constant String_Id := Strval (Right_Str);
1117 for J in 1 .. String_Length (S) loop
1118 Store_String_Char (Get_String_Char (S, J));
1122 Store_String_Char (Char_Literal_Value (Right_Str));
1125 Set_Is_Static_Expression (N, Stat);
1128 Fold_Str (N, End_String);
1131 end Eval_Concatenation;
1133 ---------------------------------
1134 -- Eval_Conditional_Expression --
1135 ---------------------------------
1137 -- This GNAT internal construct can never be statically folded, so the
1138 -- only required processing is to do the check for non-static context
1139 -- for the two expression operands.
1141 procedure Eval_Conditional_Expression (N : Node_Id) is
1142 Condition : constant Node_Id := First (Expressions (N));
1143 Then_Expr : constant Node_Id := Next (Condition);
1144 Else_Expr : constant Node_Id := Next (Then_Expr);
1147 Check_Non_Static_Context (Then_Expr);
1148 Check_Non_Static_Context (Else_Expr);
1149 end Eval_Conditional_Expression;
1151 ----------------------
1152 -- Eval_Entity_Name --
1153 ----------------------
1155 -- This procedure is used for identifiers and expanded names other than
1156 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
1157 -- static if they denote a static constant (RM 4.9(6)) or if the name
1158 -- denotes an enumeration literal (RM 4.9(22)).
1160 procedure Eval_Entity_Name (N : Node_Id) is
1161 Def_Id : constant Entity_Id := Entity (N);
1165 -- Enumeration literals are always considered to be constants
1166 -- and cannot raise constraint error (RM 4.9(22)).
1168 if Ekind (Def_Id) = E_Enumeration_Literal then
1169 Set_Is_Static_Expression (N);
1172 -- A name is static if it denotes a static constant (RM 4.9(5)), and
1173 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
1174 -- it does not violate 10.2.1(8) here, since this is not a variable.
1176 elsif Ekind (Def_Id) = E_Constant then
1178 -- Deferred constants must always be treated as nonstatic
1179 -- outside the scope of their full view.
1181 if Present (Full_View (Def_Id))
1182 and then not In_Open_Scopes (Scope (Def_Id))
1186 Val := Constant_Value (Def_Id);
1189 if Present (Val) then
1190 Set_Is_Static_Expression
1191 (N, Is_Static_Expression (Val)
1192 and then Is_Static_Subtype (Etype (Def_Id)));
1193 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
1195 if not Is_Static_Expression (N)
1196 and then not Is_Generic_Type (Etype (N))
1198 Validate_Static_Object_Name (N);
1205 -- Fall through if the name is not static.
1207 Validate_Static_Object_Name (N);
1208 end Eval_Entity_Name;
1210 ----------------------------
1211 -- Eval_Indexed_Component --
1212 ----------------------------
1214 -- Indexed components are never static, so the only required processing
1215 -- is to perform the check for non-static context on the index values.
1217 procedure Eval_Indexed_Component (N : Node_Id) is
1221 Expr := First (Expressions (N));
1222 while Present (Expr) loop
1223 Check_Non_Static_Context (Expr);
1227 end Eval_Indexed_Component;
1229 --------------------------
1230 -- Eval_Integer_Literal --
1231 --------------------------
1233 -- Numeric literals are static (RM 4.9(1)), and have already been marked
1234 -- as static by the analyzer. The reason we did it that early is to allow
1235 -- the possibility of turning off the Is_Static_Expression flag after
1236 -- analysis, but before resolution, when integer literals are generated
1237 -- in the expander that do not correspond to static expressions.
1239 procedure Eval_Integer_Literal (N : Node_Id) is
1240 T : constant Entity_Id := Etype (N);
1243 -- If the literal appears in a non-expression context, then it is
1244 -- certainly appearing in a non-static context, so check it. This
1245 -- is actually a redundant check, since Check_Non_Static_Context
1246 -- would check it, but it seems worth while avoiding the call.
1248 if Nkind (Parent (N)) not in N_Subexpr then
1249 Check_Non_Static_Context (N);
1252 -- Modular integer literals must be in their base range
1254 if Is_Modular_Integer_Type (T)
1255 and then Is_Out_Of_Range (N, Base_Type (T))
1259 end Eval_Integer_Literal;
1261 ---------------------
1262 -- Eval_Logical_Op --
1263 ---------------------
1265 -- Logical operations are static functions, so the result is potentially
1266 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
1268 procedure Eval_Logical_Op (N : Node_Id) is
1269 Left : constant Node_Id := Left_Opnd (N);
1270 Right : constant Node_Id := Right_Opnd (N);
1275 -- If not foldable we are done
1277 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1283 -- Compile time evaluation of logical operation
1286 Left_Int : constant Uint := Expr_Value (Left);
1287 Right_Int : constant Uint := Expr_Value (Right);
1290 if Is_Modular_Integer_Type (Etype (N)) then
1292 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
1293 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
1296 To_Bits (Left_Int, Left_Bits);
1297 To_Bits (Right_Int, Right_Bits);
1299 -- Note: should really be able to use array ops instead of
1300 -- these loops, but they weren't working at the time ???
1302 if Nkind (N) = N_Op_And then
1303 for J in Left_Bits'Range loop
1304 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
1307 elsif Nkind (N) = N_Op_Or then
1308 for J in Left_Bits'Range loop
1309 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
1313 pragma Assert (Nkind (N) = N_Op_Xor);
1315 for J in Left_Bits'Range loop
1316 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
1320 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)));
1324 pragma Assert (Is_Boolean_Type (Etype (N)));
1326 if Nkind (N) = N_Op_And then
1328 Test (Is_True (Left_Int) and then Is_True (Right_Int)));
1330 elsif Nkind (N) = N_Op_Or then
1332 Test (Is_True (Left_Int) or else Is_True (Right_Int)));
1335 pragma Assert (Nkind (N) = N_Op_Xor);
1337 Test (Is_True (Left_Int) xor Is_True (Right_Int)));
1341 Set_Is_Static_Expression (N, Stat);
1343 end Eval_Logical_Op;
1345 ------------------------
1346 -- Eval_Membership_Op --
1347 ------------------------
1349 -- A membership test is potentially static if the expression is static,
1350 -- and the range is a potentially static range, or is a subtype mark
1351 -- denoting a static subtype (RM 4.9(12)).
1353 procedure Eval_Membership_Op (N : Node_Id) is
1354 Left : constant Node_Id := Left_Opnd (N);
1355 Right : constant Node_Id := Right_Opnd (N);
1364 -- Ignore if error in either operand, except to make sure that
1365 -- Any_Type is properly propagated to avoid junk cascaded errors.
1367 if Etype (Left) = Any_Type
1368 or else Etype (Right) = Any_Type
1370 Set_Etype (N, Any_Type);
1374 -- Case of right operand is a subtype name
1376 if Is_Entity_Name (Right) then
1377 Def_Id := Entity (Right);
1379 if (Is_Scalar_Type (Def_Id) or else Is_String_Type (Def_Id))
1380 and then Is_OK_Static_Subtype (Def_Id)
1382 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
1384 if not Fold or else not Stat then
1388 Check_Non_Static_Context (Left);
1392 -- For string membership tests we will check the length
1395 if not Is_String_Type (Def_Id) then
1396 Lo := Type_Low_Bound (Def_Id);
1397 Hi := Type_High_Bound (Def_Id);
1404 -- Case of right operand is a range
1407 if Is_Static_Range (Right) then
1408 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
1410 if not Fold or else not Stat then
1413 -- If one bound of range raises CE, then don't try to fold
1415 elsif not Is_OK_Static_Range (Right) then
1416 Check_Non_Static_Context (Left);
1421 Check_Non_Static_Context (Left);
1425 -- Here we know range is an OK static range
1427 Lo := Low_Bound (Right);
1428 Hi := High_Bound (Right);
1431 -- For strings we check that the length of the string expression is
1432 -- compatible with the string subtype if the subtype is constrained,
1433 -- or if unconstrained then the test is always true.
1435 if Is_String_Type (Etype (Right)) then
1436 if not Is_Constrained (Etype (Right)) then
1441 Typlen : constant Uint := String_Type_Len (Etype (Right));
1442 Strlen : constant Uint :=
1443 UI_From_Int (String_Length (Strval (Get_String_Val (Left))));
1445 Result := (Typlen = Strlen);
1449 -- Fold the membership test. We know we have a static range and Lo
1450 -- and Hi are set to the expressions for the end points of this range.
1452 elsif Is_Real_Type (Etype (Right)) then
1454 Leftval : constant Ureal := Expr_Value_R (Left);
1457 Result := Expr_Value_R (Lo) <= Leftval
1458 and then Leftval <= Expr_Value_R (Hi);
1463 Leftval : constant Uint := Expr_Value (Left);
1466 Result := Expr_Value (Lo) <= Leftval
1467 and then Leftval <= Expr_Value (Hi);
1471 if Nkind (N) = N_Not_In then
1472 Result := not Result;
1475 Fold_Uint (N, Test (Result));
1476 Warn_On_Known_Condition (N);
1478 end Eval_Membership_Op;
1480 ------------------------
1481 -- Eval_Named_Integer --
1482 ------------------------
1484 procedure Eval_Named_Integer (N : Node_Id) is
1487 Expr_Value (Expression (Declaration_Node (Entity (N)))));
1488 end Eval_Named_Integer;
1490 ---------------------
1491 -- Eval_Named_Real --
1492 ---------------------
1494 procedure Eval_Named_Real (N : Node_Id) is
1497 Expr_Value_R (Expression (Declaration_Node (Entity (N)))));
1498 end Eval_Named_Real;
1504 -- Exponentiation is a static functions, so the result is potentially
1505 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
1507 procedure Eval_Op_Expon (N : Node_Id) is
1508 Left : constant Node_Id := Left_Opnd (N);
1509 Right : constant Node_Id := Right_Opnd (N);
1514 -- If not foldable we are done
1516 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1522 -- Fold exponentiation operation
1525 Right_Int : constant Uint := Expr_Value (Right);
1530 if Is_Integer_Type (Etype (Left)) then
1532 Left_Int : constant Uint := Expr_Value (Left);
1536 -- Exponentiation of an integer raises the exception
1537 -- Constraint_Error for a negative exponent (RM 4.5.6)
1539 if Right_Int < 0 then
1540 Apply_Compile_Time_Constraint_Error
1541 (N, "integer exponent negative");
1545 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
1546 Result := Left_Int ** Right_Int;
1551 if Is_Modular_Integer_Type (Etype (N)) then
1552 Result := Result mod Modulus (Etype (N));
1555 Fold_Uint (N, Result);
1563 Left_Real : constant Ureal := Expr_Value_R (Left);
1566 -- Cannot have a zero base with a negative exponent
1568 if UR_Is_Zero (Left_Real) then
1570 if Right_Int < 0 then
1571 Apply_Compile_Time_Constraint_Error
1572 (N, "zero ** negative integer");
1575 Fold_Ureal (N, Ureal_0);
1579 Fold_Ureal (N, Left_Real ** Right_Int);
1584 Set_Is_Static_Expression (N, Stat);
1592 -- The not operation is a static functions, so the result is potentially
1593 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
1595 procedure Eval_Op_Not (N : Node_Id) is
1596 Right : constant Node_Id := Right_Opnd (N);
1601 -- If not foldable we are done
1603 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
1609 -- Fold not operation
1612 Rint : constant Uint := Expr_Value (Right);
1613 Typ : constant Entity_Id := Etype (N);
1616 -- Negation is equivalent to subtracting from the modulus minus
1617 -- one. For a binary modulus this is equivalent to the ones-
1618 -- component of the original value. For non-binary modulus this
1619 -- is an arbitrary but consistent definition.
1621 if Is_Modular_Integer_Type (Typ) then
1622 Fold_Uint (N, Modulus (Typ) - 1 - Rint);
1625 pragma Assert (Is_Boolean_Type (Typ));
1626 Fold_Uint (N, Test (not Is_True (Rint)));
1629 Set_Is_Static_Expression (N, Stat);
1633 -------------------------------
1634 -- Eval_Qualified_Expression --
1635 -------------------------------
1637 -- A qualified expression is potentially static if its subtype mark denotes
1638 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
1640 procedure Eval_Qualified_Expression (N : Node_Id) is
1641 Operand : constant Node_Id := Expression (N);
1642 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
1648 -- Can only fold if target is string or scalar and subtype is static
1649 -- Also, do not fold if our parent is an allocator (this is because
1650 -- the qualified expression is really part of the syntactic structure
1651 -- of an allocator, and we do not want to end up with something that
1652 -- corresponds to "new 1" where the 1 is the result of folding a
1653 -- qualified expression).
1655 if not Is_Static_Subtype (Target_Type)
1656 or else Nkind (Parent (N)) = N_Allocator
1658 Check_Non_Static_Context (Operand);
1662 -- If not foldable we are done
1664 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
1669 -- Don't try fold if target type has constraint error bounds
1671 elsif not Is_OK_Static_Subtype (Target_Type) then
1672 Set_Raises_Constraint_Error (N);
1676 -- Fold the result of qualification
1678 if Is_Discrete_Type (Target_Type) then
1679 Fold_Uint (N, Expr_Value (Operand));
1680 Set_Is_Static_Expression (N, Stat);
1682 elsif Is_Real_Type (Target_Type) then
1683 Fold_Ureal (N, Expr_Value_R (Operand));
1684 Set_Is_Static_Expression (N, Stat);
1687 Fold_Str (N, Strval (Get_String_Val (Operand)));
1690 Set_Is_Static_Expression (N, False);
1692 Check_String_Literal_Length (N, Target_Type);
1698 if Is_Out_Of_Range (N, Etype (N)) then
1702 end Eval_Qualified_Expression;
1704 -----------------------
1705 -- Eval_Real_Literal --
1706 -----------------------
1708 -- Numeric literals are static (RM 4.9(1)), and have already been marked
1709 -- as static by the analyzer. The reason we did it that early is to allow
1710 -- the possibility of turning off the Is_Static_Expression flag after
1711 -- analysis, but before resolution, when integer literals are generated
1712 -- in the expander that do not correspond to static expressions.
1714 procedure Eval_Real_Literal (N : Node_Id) is
1716 -- If the literal appears in a non-expression context, then it is
1717 -- certainly appearing in a non-static context, so check it.
1719 if Nkind (Parent (N)) not in N_Subexpr then
1720 Check_Non_Static_Context (N);
1723 end Eval_Real_Literal;
1725 ------------------------
1726 -- Eval_Relational_Op --
1727 ------------------------
1729 -- Relational operations are static functions, so the result is static
1730 -- if both operands are static (RM 4.9(7), 4.9(20)).
1732 procedure Eval_Relational_Op (N : Node_Id) is
1733 Left : constant Node_Id := Left_Opnd (N);
1734 Right : constant Node_Id := Right_Opnd (N);
1735 Typ : constant Entity_Id := Etype (Left);
1741 -- One special case to deal with first. If we can tell that
1742 -- the result will be false because the lengths of one or
1743 -- more index subtypes are compile time known and different,
1744 -- then we can replace the entire result by False. We only
1745 -- do this for one dimensional arrays, because the case of
1746 -- multi-dimensional arrays is rare and too much trouble!
1748 if Is_Array_Type (Typ)
1749 and then Number_Dimensions (Typ) = 1
1750 and then (Nkind (N) = N_Op_Eq
1751 or else Nkind (N) = N_Op_Ne)
1753 if Raises_Constraint_Error (Left)
1754 or else Raises_Constraint_Error (Right)
1760 procedure Get_Static_Length (Op : Node_Id; Len : out Uint);
1761 -- If Op is an expression for a constrained array with a
1762 -- known at compile time length, then Len is set to this
1763 -- (non-negative length). Otherwise Len is set to minus 1.
1765 procedure Get_Static_Length (Op : Node_Id; Len : out Uint) is
1769 if Nkind (Op) = N_String_Literal then
1770 Len := UI_From_Int (String_Length (Strval (Op)));
1772 elsif not Is_Constrained (Etype (Op)) then
1773 Len := Uint_Minus_1;
1776 T := Etype (First_Index (Etype (Op)));
1778 if Is_Discrete_Type (T)
1780 Compile_Time_Known_Value (Type_Low_Bound (T))
1782 Compile_Time_Known_Value (Type_High_Bound (T))
1784 Len := UI_Max (Uint_0,
1785 Expr_Value (Type_High_Bound (T)) -
1786 Expr_Value (Type_Low_Bound (T)) + 1);
1788 Len := Uint_Minus_1;
1791 end Get_Static_Length;
1797 Get_Static_Length (Left, Len_L);
1798 Get_Static_Length (Right, Len_R);
1800 if Len_L /= Uint_Minus_1
1801 and then Len_R /= Uint_Minus_1
1802 and then Len_L /= Len_R
1804 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne));
1805 Set_Is_Static_Expression (N, False);
1806 Warn_On_Known_Condition (N);
1812 -- Can only fold if type is scalar (don't fold string ops)
1814 if not Is_Scalar_Type (Typ) then
1815 Check_Non_Static_Context (Left);
1816 Check_Non_Static_Context (Right);
1820 -- If not foldable we are done
1822 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1828 -- Integer and Enumeration (discrete) type cases
1830 if Is_Discrete_Type (Typ) then
1832 Left_Int : constant Uint := Expr_Value (Left);
1833 Right_Int : constant Uint := Expr_Value (Right);
1837 when N_Op_Eq => Result := Left_Int = Right_Int;
1838 when N_Op_Ne => Result := Left_Int /= Right_Int;
1839 when N_Op_Lt => Result := Left_Int < Right_Int;
1840 when N_Op_Le => Result := Left_Int <= Right_Int;
1841 when N_Op_Gt => Result := Left_Int > Right_Int;
1842 when N_Op_Ge => Result := Left_Int >= Right_Int;
1845 raise Program_Error;
1848 Fold_Uint (N, Test (Result));
1854 pragma Assert (Is_Real_Type (Typ));
1857 Left_Real : constant Ureal := Expr_Value_R (Left);
1858 Right_Real : constant Ureal := Expr_Value_R (Right);
1862 when N_Op_Eq => Result := (Left_Real = Right_Real);
1863 when N_Op_Ne => Result := (Left_Real /= Right_Real);
1864 when N_Op_Lt => Result := (Left_Real < Right_Real);
1865 when N_Op_Le => Result := (Left_Real <= Right_Real);
1866 when N_Op_Gt => Result := (Left_Real > Right_Real);
1867 when N_Op_Ge => Result := (Left_Real >= Right_Real);
1870 raise Program_Error;
1873 Fold_Uint (N, Test (Result));
1877 Set_Is_Static_Expression (N, Stat);
1878 Warn_On_Known_Condition (N);
1879 end Eval_Relational_Op;
1885 -- Shift operations are intrinsic operations that can never be static,
1886 -- so the only processing required is to perform the required check for
1887 -- a non static context for the two operands.
1889 -- Actually we could do some compile time evaluation here some time ???
1891 procedure Eval_Shift (N : Node_Id) is
1893 Check_Non_Static_Context (Left_Opnd (N));
1894 Check_Non_Static_Context (Right_Opnd (N));
1897 ------------------------
1898 -- Eval_Short_Circuit --
1899 ------------------------
1901 -- A short circuit operation is potentially static if both operands
1902 -- are potentially static (RM 4.9 (13))
1904 procedure Eval_Short_Circuit (N : Node_Id) is
1905 Kind : constant Node_Kind := Nkind (N);
1906 Left : constant Node_Id := Left_Opnd (N);
1907 Right : constant Node_Id := Right_Opnd (N);
1909 Rstat : constant Boolean :=
1910 Is_Static_Expression (Left)
1911 and then Is_Static_Expression (Right);
1914 -- Short circuit operations are never static in Ada 83
1917 and then Comes_From_Source (N)
1919 Check_Non_Static_Context (Left);
1920 Check_Non_Static_Context (Right);
1924 -- Now look at the operands, we can't quite use the normal call to
1925 -- Test_Expression_Is_Foldable here because short circuit operations
1926 -- are a special case, they can still be foldable, even if the right
1927 -- operand raises constraint error.
1929 -- If either operand is Any_Type, just propagate to result and
1930 -- do not try to fold, this prevents cascaded errors.
1932 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
1933 Set_Etype (N, Any_Type);
1936 -- If left operand raises constraint error, then replace node N with
1937 -- the raise constraint error node, and we are obviously not foldable.
1938 -- Is_Static_Expression is set from the two operands in the normal way,
1939 -- and we check the right operand if it is in a non-static context.
1941 elsif Raises_Constraint_Error (Left) then
1943 Check_Non_Static_Context (Right);
1946 Rewrite_In_Raise_CE (N, Left);
1947 Set_Is_Static_Expression (N, Rstat);
1950 -- If the result is not static, then we won't in any case fold
1952 elsif not Rstat then
1953 Check_Non_Static_Context (Left);
1954 Check_Non_Static_Context (Right);
1958 -- Here the result is static, note that, unlike the normal processing
1959 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
1960 -- the right operand raises constraint error, that's because it is not
1961 -- significant if the left operand is decisive.
1963 Set_Is_Static_Expression (N);
1965 -- It does not matter if the right operand raises constraint error if
1966 -- it will not be evaluated. So deal specially with the cases where
1967 -- the right operand is not evaluated. Note that we will fold these
1968 -- cases even if the right operand is non-static, which is fine, but
1969 -- of course in these cases the result is not potentially static.
1971 Left_Int := Expr_Value (Left);
1973 if (Kind = N_And_Then and then Is_False (Left_Int))
1974 or else (Kind = N_Or_Else and Is_True (Left_Int))
1976 Fold_Uint (N, Left_Int);
1980 -- If first operand not decisive, then it does matter if the right
1981 -- operand raises constraint error, since it will be evaluated, so
1982 -- we simply replace the node with the right operand. Note that this
1983 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
1984 -- (both are set to True in Right).
1986 if Raises_Constraint_Error (Right) then
1987 Rewrite_In_Raise_CE (N, Right);
1988 Check_Non_Static_Context (Left);
1992 -- Otherwise the result depends on the right operand
1994 Fold_Uint (N, Expr_Value (Right));
1997 end Eval_Short_Circuit;
2003 -- Slices can never be static, so the only processing required is to
2004 -- check for non-static context if an explicit range is given.
2006 procedure Eval_Slice (N : Node_Id) is
2007 Drange : constant Node_Id := Discrete_Range (N);
2010 if Nkind (Drange) = N_Range then
2011 Check_Non_Static_Context (Low_Bound (Drange));
2012 Check_Non_Static_Context (High_Bound (Drange));
2016 -------------------------
2017 -- Eval_String_Literal --
2018 -------------------------
2020 procedure Eval_String_Literal (N : Node_Id) is
2021 T : constant Entity_Id := Etype (N);
2022 B : constant Entity_Id := Base_Type (T);
2026 -- Nothing to do if error type (handles cases like default expressions
2027 -- or generics where we have not yet fully resolved the type)
2029 if B = Any_Type or else B = Any_String then
2032 -- String literals are static if the subtype is static (RM 4.9(2)), so
2033 -- reset the static expression flag (it was set unconditionally in
2034 -- Analyze_String_Literal) if the subtype is non-static. We tell if
2035 -- the subtype is static by looking at the lower bound.
2037 elsif not Is_OK_Static_Expression (String_Literal_Low_Bound (T)) then
2038 Set_Is_Static_Expression (N, False);
2040 elsif Nkind (Original_Node (N)) = N_Type_Conversion then
2041 Set_Is_Static_Expression (N, False);
2043 -- Test for illegal Ada 95 cases. A string literal is illegal in
2044 -- Ada 95 if its bounds are outside the index base type and this
2045 -- index type is static. This can hapen in only two ways. Either
2046 -- the string literal is too long, or it is null, and the lower
2047 -- bound is type'First. In either case it is the upper bound that
2048 -- is out of range of the index type.
2051 if Root_Type (B) = Standard_String
2052 or else Root_Type (B) = Standard_Wide_String
2054 I := Standard_Positive;
2056 I := Etype (First_Index (B));
2059 if String_Literal_Length (T) > String_Type_Len (B) then
2060 Apply_Compile_Time_Constraint_Error
2061 (N, "string literal too long for}",
2063 Typ => First_Subtype (B));
2065 elsif String_Literal_Length (T) = 0
2066 and then not Is_Generic_Type (I)
2067 and then Expr_Value (String_Literal_Low_Bound (T)) =
2068 Expr_Value (Type_Low_Bound (Base_Type (I)))
2070 Apply_Compile_Time_Constraint_Error
2071 (N, "null string literal not allowed for}",
2073 Typ => First_Subtype (B));
2077 end Eval_String_Literal;
2079 --------------------------
2080 -- Eval_Type_Conversion --
2081 --------------------------
2083 -- A type conversion is potentially static if its subtype mark is for a
2084 -- static scalar subtype, and its operand expression is potentially static
2087 procedure Eval_Type_Conversion (N : Node_Id) is
2088 Operand : constant Node_Id := Expression (N);
2089 Source_Type : constant Entity_Id := Etype (Operand);
2090 Target_Type : constant Entity_Id := Etype (N);
2095 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
2096 -- Returns true if type T is an integer type, or if it is a
2097 -- fixed-point type to be treated as an integer (i.e. the flag
2098 -- Conversion_OK is set on the conversion node).
2100 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
2101 -- Returns true if type T is a floating-point type, or if it is a
2102 -- fixed-point type that is not to be treated as an integer (i.e. the
2103 -- flag Conversion_OK is not set on the conversion node).
2105 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
2109 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
2110 end To_Be_Treated_As_Integer;
2112 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
2115 Is_Floating_Point_Type (T)
2116 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
2117 end To_Be_Treated_As_Real;
2119 -- Start of processing for Eval_Type_Conversion
2122 -- Cannot fold if target type is non-static or if semantic error.
2124 if not Is_Static_Subtype (Target_Type) then
2125 Check_Non_Static_Context (Operand);
2128 elsif Error_Posted (N) then
2132 -- If not foldable we are done
2134 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
2139 -- Don't try fold if target type has constraint error bounds
2141 elsif not Is_OK_Static_Subtype (Target_Type) then
2142 Set_Raises_Constraint_Error (N);
2146 -- Remaining processing depends on operand types. Note that in the
2147 -- following type test, fixed-point counts as real unless the flag
2148 -- Conversion_OK is set, in which case it counts as integer.
2150 -- Fold conversion, case of string type. The result is not static.
2152 if Is_String_Type (Target_Type) then
2153 Fold_Str (N, Strval (Get_String_Val (Operand)));
2154 Set_Is_Static_Expression (N, False);
2158 -- Fold conversion, case of integer target type
2160 elsif To_Be_Treated_As_Integer (Target_Type) then
2165 -- Integer to integer conversion
2167 if To_Be_Treated_As_Integer (Source_Type) then
2168 Result := Expr_Value (Operand);
2170 -- Real to integer conversion
2173 Result := UR_To_Uint (Expr_Value_R (Operand));
2176 -- If fixed-point type (Conversion_OK must be set), then the
2177 -- result is logically an integer, but we must replace the
2178 -- conversion with the corresponding real literal, since the
2179 -- type from a semantic point of view is still fixed-point.
2181 if Is_Fixed_Point_Type (Target_Type) then
2183 (N, UR_From_Uint (Result) * Small_Value (Target_Type));
2185 -- Otherwise result is integer literal
2188 Fold_Uint (N, Result);
2192 -- Fold conversion, case of real target type
2194 elsif To_Be_Treated_As_Real (Target_Type) then
2199 if To_Be_Treated_As_Real (Source_Type) then
2200 Result := Expr_Value_R (Operand);
2202 Result := UR_From_Uint (Expr_Value (Operand));
2205 Fold_Ureal (N, Result);
2208 -- Enumeration types
2211 Fold_Uint (N, Expr_Value (Operand));
2214 Set_Is_Static_Expression (N, Stat);
2216 if Is_Out_Of_Range (N, Etype (N)) then
2220 end Eval_Type_Conversion;
2226 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
2227 -- are potentially static if the operand is potentially static (RM 4.9(7))
2229 procedure Eval_Unary_Op (N : Node_Id) is
2230 Right : constant Node_Id := Right_Opnd (N);
2235 -- If not foldable we are done
2237 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
2243 -- Fold for integer case
2245 if Is_Integer_Type (Etype (N)) then
2247 Rint : constant Uint := Expr_Value (Right);
2251 -- In the case of modular unary plus and abs there is no need
2252 -- to adjust the result of the operation since if the original
2253 -- operand was in bounds the result will be in the bounds of the
2254 -- modular type. However, in the case of modular unary minus the
2255 -- result may go out of the bounds of the modular type and needs
2258 if Nkind (N) = N_Op_Plus then
2261 elsif Nkind (N) = N_Op_Minus then
2262 if Is_Modular_Integer_Type (Etype (N)) then
2263 Result := (-Rint) mod Modulus (Etype (N));
2269 pragma Assert (Nkind (N) = N_Op_Abs);
2273 Fold_Uint (N, Result);
2276 -- Fold for real case
2278 elsif Is_Real_Type (Etype (N)) then
2280 Rreal : constant Ureal := Expr_Value_R (Right);
2284 if Nkind (N) = N_Op_Plus then
2287 elsif Nkind (N) = N_Op_Minus then
2288 Result := UR_Negate (Rreal);
2291 pragma Assert (Nkind (N) = N_Op_Abs);
2292 Result := abs Rreal;
2295 Fold_Ureal (N, Result);
2299 Set_Is_Static_Expression (N, Stat);
2303 -------------------------------
2304 -- Eval_Unchecked_Conversion --
2305 -------------------------------
2307 -- Unchecked conversions can never be static, so the only required
2308 -- processing is to check for a non-static context for the operand.
2310 procedure Eval_Unchecked_Conversion (N : Node_Id) is
2312 Check_Non_Static_Context (Expression (N));
2313 end Eval_Unchecked_Conversion;
2315 --------------------
2316 -- Expr_Rep_Value --
2317 --------------------
2319 function Expr_Rep_Value (N : Node_Id) return Uint is
2320 Kind : constant Node_Kind := Nkind (N);
2324 if Is_Entity_Name (N) then
2327 -- An enumeration literal that was either in the source or
2328 -- created as a result of static evaluation.
2330 if Ekind (Ent) = E_Enumeration_Literal then
2331 return Enumeration_Rep (Ent);
2333 -- A user defined static constant
2336 pragma Assert (Ekind (Ent) = E_Constant);
2337 return Expr_Rep_Value (Constant_Value (Ent));
2340 -- An integer literal that was either in the source or created
2341 -- as a result of static evaluation.
2343 elsif Kind = N_Integer_Literal then
2346 -- A real literal for a fixed-point type. This must be the fixed-point
2347 -- case, either the literal is of a fixed-point type, or it is a bound
2348 -- of a fixed-point type, with type universal real. In either case we
2349 -- obtain the desired value from Corresponding_Integer_Value.
2351 elsif Kind = N_Real_Literal then
2353 -- Apply the assertion to the Underlying_Type of the literal for
2354 -- the benefit of calls to this function in the JGNAT back end,
2355 -- where literal types can reflect private views.
2357 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
2358 return Corresponding_Integer_Value (N);
2361 pragma Assert (Kind = N_Character_Literal);
2364 -- Since Character literals of type Standard.Character don't
2365 -- have any defining character literals built for them, they
2366 -- do not have their Entity set, so just use their Char
2367 -- code. Otherwise for user-defined character literals use
2368 -- their Pos value as usual which is the same as the Rep value.
2371 return UI_From_Int (Int (Char_Literal_Value (N)));
2373 return Enumeration_Rep (Ent);
2382 function Expr_Value (N : Node_Id) return Uint is
2383 Kind : constant Node_Kind := Nkind (N);
2387 if Is_Entity_Name (N) then
2390 -- An enumeration literal that was either in the source or
2391 -- created as a result of static evaluation.
2393 if Ekind (Ent) = E_Enumeration_Literal then
2394 return Enumeration_Pos (Ent);
2396 -- A user defined static constant
2399 pragma Assert (Ekind (Ent) = E_Constant);
2400 return Expr_Value (Constant_Value (Ent));
2403 -- An integer literal that was either in the source or created
2404 -- as a result of static evaluation.
2406 elsif Kind = N_Integer_Literal then
2409 -- A real literal for a fixed-point type. This must be the fixed-point
2410 -- case, either the literal is of a fixed-point type, or it is a bound
2411 -- of a fixed-point type, with type universal real. In either case we
2412 -- obtain the desired value from Corresponding_Integer_Value.
2414 elsif Kind = N_Real_Literal then
2416 -- Apply the assertion to the Underlying_Type of the literal for
2417 -- the benefit of calls to this function in the JGNAT back end,
2418 -- where literal types can reflect private views.
2420 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
2421 return Corresponding_Integer_Value (N);
2423 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
2425 elsif Kind = N_Attribute_Reference
2426 and then Attribute_Name (N) = Name_Null_Parameter
2430 -- Otherwise must be character literal
2433 pragma Assert (Kind = N_Character_Literal);
2436 -- Since Character literals of type Standard.Character don't
2437 -- have any defining character literals built for them, they
2438 -- do not have their Entity set, so just use their Char
2439 -- code. Otherwise for user-defined character literals use
2440 -- their Pos value as usual.
2443 return UI_From_Int (Int (Char_Literal_Value (N)));
2445 return Enumeration_Pos (Ent);
2455 function Expr_Value_E (N : Node_Id) return Entity_Id is
2456 Ent : constant Entity_Id := Entity (N);
2459 if Ekind (Ent) = E_Enumeration_Literal then
2462 pragma Assert (Ekind (Ent) = E_Constant);
2463 return Expr_Value_E (Constant_Value (Ent));
2471 function Expr_Value_R (N : Node_Id) return Ureal is
2472 Kind : constant Node_Kind := Nkind (N);
2477 if Kind = N_Real_Literal then
2480 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
2482 pragma Assert (Ekind (Ent) = E_Constant);
2483 return Expr_Value_R (Constant_Value (Ent));
2485 elsif Kind = N_Integer_Literal then
2486 return UR_From_Uint (Expr_Value (N));
2488 -- Strange case of VAX literals, which are at this stage transformed
2489 -- into Vax_Type!x_To_y(IEEE_Literal). See Expand_N_Real_Literal in
2490 -- Exp_Vfpt for further details.
2492 elsif Vax_Float (Etype (N))
2493 and then Nkind (N) = N_Unchecked_Type_Conversion
2495 Expr := Expression (N);
2497 if Nkind (Expr) = N_Function_Call
2498 and then Present (Parameter_Associations (Expr))
2500 Expr := First (Parameter_Associations (Expr));
2502 if Nkind (Expr) = N_Real_Literal then
2503 return Realval (Expr);
2507 -- Peculiar VMS case, if we have xxx'Null_Parameter, return 0.0
2509 elsif Kind = N_Attribute_Reference
2510 and then Attribute_Name (N) = Name_Null_Parameter
2515 -- If we fall through, we have a node that cannot be interepreted
2516 -- as a compile time constant. That is definitely an error.
2518 raise Program_Error;
2525 function Expr_Value_S (N : Node_Id) return Node_Id is
2527 if Nkind (N) = N_String_Literal then
2530 pragma Assert (Ekind (Entity (N)) = E_Constant);
2531 return Expr_Value_S (Constant_Value (Entity (N)));
2539 procedure Fold_Str (N : Node_Id; Val : String_Id) is
2540 Loc : constant Source_Ptr := Sloc (N);
2541 Typ : constant Entity_Id := Etype (N);
2544 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
2545 Analyze_And_Resolve (N, Typ);
2552 procedure Fold_Uint (N : Node_Id; Val : Uint) is
2553 Loc : constant Source_Ptr := Sloc (N);
2554 Typ : constant Entity_Id := Etype (N);
2557 -- For a result of type integer, subsitute an N_Integer_Literal node
2558 -- for the result of the compile time evaluation of the expression.
2560 if Is_Integer_Type (Etype (N)) then
2561 Rewrite (N, Make_Integer_Literal (Loc, Val));
2563 -- Otherwise we have an enumeration type, and we substitute either
2564 -- an N_Identifier or N_Character_Literal to represent the enumeration
2565 -- literal corresponding to the given value, which must always be in
2566 -- range, because appropriate tests have already been made for this.
2568 else pragma Assert (Is_Enumeration_Type (Etype (N)));
2569 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
2572 -- We now have the literal with the right value, both the actual type
2573 -- and the expected type of this literal are taken from the expression
2574 -- that was evaluated.
2585 procedure Fold_Ureal (N : Node_Id; Val : Ureal) is
2586 Loc : constant Source_Ptr := Sloc (N);
2587 Typ : constant Entity_Id := Etype (N);
2590 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
2593 -- Both the actual and expected type comes from the original expression
2603 function From_Bits (B : Bits; T : Entity_Id) return Uint is
2607 for J in 0 .. B'Last loop
2613 if Non_Binary_Modulus (T) then
2614 V := V mod Modulus (T);
2620 --------------------
2621 -- Get_String_Val --
2622 --------------------
2624 function Get_String_Val (N : Node_Id) return Node_Id is
2626 if Nkind (N) = N_String_Literal then
2629 elsif Nkind (N) = N_Character_Literal then
2633 pragma Assert (Is_Entity_Name (N));
2634 return Get_String_Val (Constant_Value (Entity (N)));
2638 --------------------
2639 -- In_Subrange_Of --
2640 --------------------
2642 function In_Subrange_Of
2645 Fixed_Int : Boolean := False)
2655 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
2658 -- Never in range if both types are not scalar. Don't know if this can
2659 -- actually happen, but just in case.
2661 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T1) then
2665 L1 := Type_Low_Bound (T1);
2666 H1 := Type_High_Bound (T1);
2668 L2 := Type_Low_Bound (T2);
2669 H2 := Type_High_Bound (T2);
2671 -- Check bounds to see if comparison possible at compile time
2673 if Compile_Time_Compare (L1, L2) in Compare_GE
2675 Compile_Time_Compare (H1, H2) in Compare_LE
2680 -- If bounds not comparable at compile time, then the bounds of T2
2681 -- must be compile time known or we cannot answer the query.
2683 if not Compile_Time_Known_Value (L2)
2684 or else not Compile_Time_Known_Value (H2)
2689 -- If the bounds of T1 are know at compile time then use these
2690 -- ones, otherwise use the bounds of the base type (which are of
2691 -- course always static).
2693 if not Compile_Time_Known_Value (L1) then
2694 L1 := Type_Low_Bound (Base_Type (T1));
2697 if not Compile_Time_Known_Value (H1) then
2698 H1 := Type_High_Bound (Base_Type (T1));
2701 -- Fixed point types should be considered as such only if
2702 -- flag Fixed_Int is set to False.
2704 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
2705 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
2706 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
2709 Expr_Value_R (L2) <= Expr_Value_R (L1)
2711 Expr_Value_R (H2) >= Expr_Value_R (H1);
2715 Expr_Value (L2) <= Expr_Value (L1)
2717 Expr_Value (H2) >= Expr_Value (H1);
2722 -- If any exception occurs, it means that we have some bug in the compiler
2723 -- possibly triggered by a previous error, or by some unforseen peculiar
2724 -- occurrence. However, this is only an optimization attempt, so there is
2725 -- really no point in crashing the compiler. Instead we just decide, too
2726 -- bad, we can't figure out the answer in this case after all.
2731 -- Debug flag K disables this behavior (useful for debugging)
2733 if Debug_Flag_K then
2744 function Is_In_Range
2747 Fixed_Int : Boolean := False;
2748 Int_Real : Boolean := False)
2755 -- Universal types have no range limits, so always in range.
2757 if Typ = Universal_Integer or else Typ = Universal_Real then
2760 -- Never in range if not scalar type. Don't know if this can
2761 -- actually happen, but our spec allows it, so we must check!
2763 elsif not Is_Scalar_Type (Typ) then
2766 -- Never in range unless we have a compile time known value.
2768 elsif not Compile_Time_Known_Value (N) then
2773 Lo : constant Node_Id := Type_Low_Bound (Typ);
2774 Hi : constant Node_Id := Type_High_Bound (Typ);
2775 LB_Known : constant Boolean := Compile_Time_Known_Value (Lo);
2776 UB_Known : constant Boolean := Compile_Time_Known_Value (Hi);
2779 -- Fixed point types should be considered as such only in
2780 -- flag Fixed_Int is set to False.
2782 if Is_Floating_Point_Type (Typ)
2783 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
2786 Valr := Expr_Value_R (N);
2788 if LB_Known and then Valr >= Expr_Value_R (Lo)
2789 and then UB_Known and then Valr <= Expr_Value_R (Hi)
2797 Val := Expr_Value (N);
2799 if LB_Known and then Val >= Expr_Value (Lo)
2800 and then UB_Known and then Val <= Expr_Value (Hi)
2815 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
2816 Typ : constant Entity_Id := Etype (Lo);
2819 if not Compile_Time_Known_Value (Lo)
2820 or else not Compile_Time_Known_Value (Hi)
2825 if Is_Discrete_Type (Typ) then
2826 return Expr_Value (Lo) > Expr_Value (Hi);
2829 pragma Assert (Is_Real_Type (Typ));
2830 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
2834 -----------------------------
2835 -- Is_OK_Static_Expression --
2836 -----------------------------
2838 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
2840 return Is_Static_Expression (N)
2841 and then not Raises_Constraint_Error (N);
2842 end Is_OK_Static_Expression;
2844 ------------------------
2845 -- Is_OK_Static_Range --
2846 ------------------------
2848 -- A static range is a range whose bounds are static expressions, or a
2849 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
2850 -- We have already converted range attribute references, so we get the
2851 -- "or" part of this rule without needing a special test.
2853 function Is_OK_Static_Range (N : Node_Id) return Boolean is
2855 return Is_OK_Static_Expression (Low_Bound (N))
2856 and then Is_OK_Static_Expression (High_Bound (N));
2857 end Is_OK_Static_Range;
2859 --------------------------
2860 -- Is_OK_Static_Subtype --
2861 --------------------------
2863 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
2864 -- where neither bound raises constraint error when evaluated.
2866 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
2867 Base_T : constant Entity_Id := Base_Type (Typ);
2868 Anc_Subt : Entity_Id;
2871 -- First a quick check on the non static subtype flag. As described
2872 -- in further detail in Einfo, this flag is not decisive in all cases,
2873 -- but if it is set, then the subtype is definitely non-static.
2875 if Is_Non_Static_Subtype (Typ) then
2879 Anc_Subt := Ancestor_Subtype (Typ);
2881 if Anc_Subt = Empty then
2885 if Is_Generic_Type (Root_Type (Base_T))
2886 or else Is_Generic_Actual_Type (Base_T)
2892 elsif Is_String_Type (Typ) then
2894 Ekind (Typ) = E_String_Literal_Subtype
2896 (Is_OK_Static_Subtype (Component_Type (Typ))
2897 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
2901 elsif Is_Scalar_Type (Typ) then
2902 if Base_T = Typ then
2906 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so
2907 -- use Get_Type_Low,High_Bound.
2909 return Is_OK_Static_Subtype (Anc_Subt)
2910 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
2911 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
2914 -- Types other than string and scalar types are never static
2919 end Is_OK_Static_Subtype;
2921 ---------------------
2922 -- Is_Out_Of_Range --
2923 ---------------------
2925 function Is_Out_Of_Range
2928 Fixed_Int : Boolean := False;
2929 Int_Real : Boolean := False)
2936 -- Universal types have no range limits, so always in range.
2938 if Typ = Universal_Integer or else Typ = Universal_Real then
2941 -- Never out of range if not scalar type. Don't know if this can
2942 -- actually happen, but our spec allows it, so we must check!
2944 elsif not Is_Scalar_Type (Typ) then
2947 -- Never out of range if this is a generic type, since the bounds
2948 -- of generic types are junk. Note that if we only checked for
2949 -- static expressions (instead of compile time known values) below,
2950 -- we would not need this check, because values of a generic type
2951 -- can never be static, but they can be known at compile time.
2953 elsif Is_Generic_Type (Typ) then
2956 -- Never out of range unless we have a compile time known value.
2958 elsif not Compile_Time_Known_Value (N) then
2963 Lo : constant Node_Id := Type_Low_Bound (Typ);
2964 Hi : constant Node_Id := Type_High_Bound (Typ);
2965 LB_Known : constant Boolean := Compile_Time_Known_Value (Lo);
2966 UB_Known : constant Boolean := Compile_Time_Known_Value (Hi);
2969 -- Real types (note that fixed-point types are not treated
2970 -- as being of a real type if the flag Fixed_Int is set,
2971 -- since in that case they are regarded as integer types).
2973 if Is_Floating_Point_Type (Typ)
2974 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
2977 Valr := Expr_Value_R (N);
2979 if LB_Known and then Valr < Expr_Value_R (Lo) then
2982 elsif UB_Known and then Expr_Value_R (Hi) < Valr then
2990 Val := Expr_Value (N);
2992 if LB_Known and then Val < Expr_Value (Lo) then
2995 elsif UB_Known and then Expr_Value (Hi) < Val then
3004 end Is_Out_Of_Range;
3006 ---------------------
3007 -- Is_Static_Range --
3008 ---------------------
3010 -- A static range is a range whose bounds are static expressions, or a
3011 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
3012 -- We have already converted range attribute references, so we get the
3013 -- "or" part of this rule without needing a special test.
3015 function Is_Static_Range (N : Node_Id) return Boolean is
3017 return Is_Static_Expression (Low_Bound (N))
3018 and then Is_Static_Expression (High_Bound (N));
3019 end Is_Static_Range;
3021 -----------------------
3022 -- Is_Static_Subtype --
3023 -----------------------
3025 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)).
3027 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
3028 Base_T : constant Entity_Id := Base_Type (Typ);
3029 Anc_Subt : Entity_Id;
3032 -- First a quick check on the non static subtype flag. As described
3033 -- in further detail in Einfo, this flag is not decisive in all cases,
3034 -- but if it is set, then the subtype is definitely non-static.
3036 if Is_Non_Static_Subtype (Typ) then
3040 Anc_Subt := Ancestor_Subtype (Typ);
3042 if Anc_Subt = Empty then
3046 if Is_Generic_Type (Root_Type (Base_T))
3047 or else Is_Generic_Actual_Type (Base_T)
3053 elsif Is_String_Type (Typ) then
3055 Ekind (Typ) = E_String_Literal_Subtype
3057 (Is_Static_Subtype (Component_Type (Typ))
3058 and then Is_Static_Subtype (Etype (First_Index (Typ))));
3062 elsif Is_Scalar_Type (Typ) then
3063 if Base_T = Typ then
3067 return Is_Static_Subtype (Anc_Subt)
3068 and then Is_Static_Expression (Type_Low_Bound (Typ))
3069 and then Is_Static_Expression (Type_High_Bound (Typ));
3072 -- Types other than string and scalar types are never static
3077 end Is_Static_Subtype;
3079 --------------------
3080 -- Not_Null_Range --
3081 --------------------
3083 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
3084 Typ : constant Entity_Id := Etype (Lo);
3087 if not Compile_Time_Known_Value (Lo)
3088 or else not Compile_Time_Known_Value (Hi)
3093 if Is_Discrete_Type (Typ) then
3094 return Expr_Value (Lo) <= Expr_Value (Hi);
3097 pragma Assert (Is_Real_Type (Typ));
3099 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
3107 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
3109 -- We allow a maximum of 500,000 bits which seems a reasonable limit
3111 if Bits < 500_000 then
3115 Error_Msg_N ("static value too large, capacity exceeded", N);
3124 procedure Out_Of_Range (N : Node_Id) is
3126 -- If we have the static expression case, then this is an illegality
3127 -- in Ada 95 mode, except that in an instance, we never generate an
3128 -- error (if the error is legitimate, it was already diagnosed in
3129 -- the template). The expression to compute the length of a packed
3130 -- array is attached to the array type itself, and deserves a separate
3133 if Is_Static_Expression (N)
3134 and then not In_Instance
3138 if Nkind (Parent (N)) = N_Defining_Identifier
3139 and then Is_Array_Type (Parent (N))
3140 and then Present (Packed_Array_Type (Parent (N)))
3141 and then Present (First_Rep_Item (Parent (N)))
3144 ("length of packed array must not exceed Integer''Last",
3145 First_Rep_Item (Parent (N)));
3146 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
3149 Apply_Compile_Time_Constraint_Error
3150 (N, "value not in range of}");
3153 -- Here we generate a warning for the Ada 83 case, or when we are
3154 -- in an instance, or when we have a non-static expression case.
3157 Warn_On_Instance := True;
3158 Apply_Compile_Time_Constraint_Error
3159 (N, "value not in range of}?");
3160 Warn_On_Instance := False;
3164 -------------------------
3165 -- Rewrite_In_Raise_CE --
3166 -------------------------
3168 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
3169 Typ : constant Entity_Id := Etype (N);
3172 -- If we want to raise CE in the condition of a raise_CE node
3173 -- we may as well get rid of the condition
3175 if Present (Parent (N))
3176 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
3178 Set_Condition (Parent (N), Empty);
3180 -- If the expression raising CE is a N_Raise_CE node, we can use
3181 -- that one. We just preserve the type of the context
3183 elsif Nkind (Exp) = N_Raise_Constraint_Error then
3187 -- We have to build an explicit raise_ce node
3190 Rewrite (N, Make_Raise_Constraint_Error (Sloc (Exp)));
3191 Set_Raises_Constraint_Error (N);
3194 end Rewrite_In_Raise_CE;
3196 ---------------------
3197 -- String_Type_Len --
3198 ---------------------
3200 function String_Type_Len (Stype : Entity_Id) return Uint is
3201 NT : constant Entity_Id := Etype (First_Index (Stype));
3205 if Is_OK_Static_Subtype (NT) then
3208 T := Base_Type (NT);
3211 return Expr_Value (Type_High_Bound (T)) -
3212 Expr_Value (Type_Low_Bound (T)) + 1;
3213 end String_Type_Len;
3215 ------------------------------------
3216 -- Subtypes_Statically_Compatible --
3217 ------------------------------------
3219 function Subtypes_Statically_Compatible
3225 if Is_Scalar_Type (T1) then
3227 -- Definitely compatible if we match
3229 if Subtypes_Statically_Match (T1, T2) then
3232 -- If either subtype is nonstatic then they're not compatible
3234 elsif not Is_Static_Subtype (T1)
3235 or else not Is_Static_Subtype (T2)
3239 -- If either type has constraint error bounds, then consider that
3240 -- they match to avoid junk cascaded errors here.
3242 elsif not Is_OK_Static_Subtype (T1)
3243 or else not Is_OK_Static_Subtype (T2)
3247 -- Base types must match, but we don't check that (should
3248 -- we???) but we do at least check that both types are
3249 -- real, or both types are not real.
3251 elsif (Is_Real_Type (T1) /= Is_Real_Type (T2)) then
3254 -- Here we check the bounds
3258 LB1 : constant Node_Id := Type_Low_Bound (T1);
3259 HB1 : constant Node_Id := Type_High_Bound (T1);
3260 LB2 : constant Node_Id := Type_Low_Bound (T2);
3261 HB2 : constant Node_Id := Type_High_Bound (T2);
3264 if Is_Real_Type (T1) then
3266 (Expr_Value_R (LB1) > Expr_Value_R (HB1))
3268 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
3270 Expr_Value_R (HB1) <= Expr_Value_R (HB2));
3274 (Expr_Value (LB1) > Expr_Value (HB1))
3276 (Expr_Value (LB2) <= Expr_Value (LB1)
3278 Expr_Value (HB1) <= Expr_Value (HB2));
3283 elsif Is_Access_Type (T1) then
3284 return not Is_Constrained (T2)
3285 or else Subtypes_Statically_Match
3286 (Designated_Type (T1), Designated_Type (T2));
3289 return (Is_Composite_Type (T1) and then not Is_Constrained (T2))
3290 or else Subtypes_Statically_Match (T1, T2);
3292 end Subtypes_Statically_Compatible;
3294 -------------------------------
3295 -- Subtypes_Statically_Match --
3296 -------------------------------
3298 -- Subtypes statically match if they have statically matching constraints
3299 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
3300 -- they are the same identical constraint, or if they are static and the
3301 -- values match (RM 4.9.1(1)).
3303 function Subtypes_Statically_Match (T1, T2 : Entity_Id) return Boolean is
3305 -- A type always statically matches itself
3312 elsif Is_Scalar_Type (T1) then
3314 -- Base types must be the same
3316 if Base_Type (T1) /= Base_Type (T2) then
3320 -- A constrained numeric subtype never matches an unconstrained
3321 -- subtype, i.e. both types must be constrained or unconstrained.
3323 -- To understand the requirement for this test, see RM 4.9.1(1).
3324 -- As is made clear in RM 3.5.4(11), type Integer, for example
3325 -- is a constrained subtype with constraint bounds matching the
3326 -- bounds of its corresponding uncontrained base type. In this
3327 -- situation, Integer and Integer'Base do not statically match,
3328 -- even though they have the same bounds.
3330 -- We only apply this test to types in Standard and types that
3331 -- appear in user programs. That way, we do not have to be
3332 -- too careful about setting Is_Constrained right for itypes.
3334 if Is_Numeric_Type (T1)
3335 and then (Is_Constrained (T1) /= Is_Constrained (T2))
3336 and then (Scope (T1) = Standard_Standard
3337 or else Comes_From_Source (T1))
3338 and then (Scope (T2) = Standard_Standard
3339 or else Comes_From_Source (T2))
3344 -- If there was an error in either range, then just assume
3345 -- the types statically match to avoid further junk errors
3347 if Error_Posted (Scalar_Range (T1))
3349 Error_Posted (Scalar_Range (T2))
3354 -- Otherwise both types have bound that can be compared
3357 LB1 : constant Node_Id := Type_Low_Bound (T1);
3358 HB1 : constant Node_Id := Type_High_Bound (T1);
3359 LB2 : constant Node_Id := Type_Low_Bound (T2);
3360 HB2 : constant Node_Id := Type_High_Bound (T2);
3363 -- If the bounds are the same tree node, then match
3365 if LB1 = LB2 and then HB1 = HB2 then
3368 -- Otherwise bounds must be static and identical value
3371 if not Is_Static_Subtype (T1)
3372 or else not Is_Static_Subtype (T2)
3376 -- If either type has constraint error bounds, then say
3377 -- that they match to avoid junk cascaded errors here.
3379 elsif not Is_OK_Static_Subtype (T1)
3380 or else not Is_OK_Static_Subtype (T2)
3384 elsif Is_Real_Type (T1) then
3386 (Expr_Value_R (LB1) = Expr_Value_R (LB2))
3388 (Expr_Value_R (HB1) = Expr_Value_R (HB2));
3392 Expr_Value (LB1) = Expr_Value (LB2)
3394 Expr_Value (HB1) = Expr_Value (HB2);
3399 -- Type with discriminants
3401 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
3402 if Has_Discriminants (T1) /= Has_Discriminants (T2) then
3407 DL1 : constant Elist_Id := Discriminant_Constraint (T1);
3408 DL2 : constant Elist_Id := Discriminant_Constraint (T2);
3410 DA1 : Elmt_Id := First_Elmt (DL1);
3411 DA2 : Elmt_Id := First_Elmt (DL2);
3417 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
3421 while Present (DA1) loop
3423 Expr1 : constant Node_Id := Node (DA1);
3424 Expr2 : constant Node_Id := Node (DA2);
3427 if not Is_Static_Expression (Expr1)
3428 or else not Is_Static_Expression (Expr2)
3432 -- If either expression raised a constraint error,
3433 -- consider the expressions as matching, since this
3434 -- helps to prevent cascading errors.
3436 elsif Raises_Constraint_Error (Expr1)
3437 or else Raises_Constraint_Error (Expr2)
3441 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
3453 -- A definite type does not match an indefinite or classwide type.
3456 Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
3462 elsif Is_Array_Type (T1) then
3464 -- If either subtype is unconstrained then both must be,
3465 -- and if both are unconstrained then no further checking
3468 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
3469 return not (Is_Constrained (T1) or else Is_Constrained (T2));
3472 -- Both subtypes are constrained, so check that the index
3473 -- subtypes statically match.
3476 Index1 : Node_Id := First_Index (T1);
3477 Index2 : Node_Id := First_Index (T2);
3480 while Present (Index1) loop
3482 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
3487 Next_Index (Index1);
3488 Next_Index (Index2);
3494 elsif Is_Access_Type (T1) then
3495 return Subtypes_Statically_Match
3496 (Designated_Type (T1),
3497 Designated_Type (T2));
3499 -- All other types definitely match
3504 end Subtypes_Statically_Match;
3510 function Test (Cond : Boolean) return Uint is
3519 ---------------------------------
3520 -- Test_Expression_Is_Foldable --
3521 ---------------------------------
3525 procedure Test_Expression_Is_Foldable
3534 -- If operand is Any_Type, just propagate to result and do not
3535 -- try to fold, this prevents cascaded errors.
3537 if Etype (Op1) = Any_Type then
3538 Set_Etype (N, Any_Type);
3542 -- If operand raises constraint error, then replace node N with the
3543 -- raise constraint error node, and we are obviously not foldable.
3544 -- Note that this replacement inherits the Is_Static_Expression flag
3545 -- from the operand.
3547 elsif Raises_Constraint_Error (Op1) then
3548 Rewrite_In_Raise_CE (N, Op1);
3552 -- If the operand is not static, then the result is not static, and
3553 -- all we have to do is to check the operand since it is now known
3554 -- to appear in a non-static context.
3556 elsif not Is_Static_Expression (Op1) then
3557 Check_Non_Static_Context (Op1);
3558 Fold := Compile_Time_Known_Value (Op1);
3561 -- An expression of a formal modular type is not foldable because
3562 -- the modulus is unknown.
3564 elsif Is_Modular_Integer_Type (Etype (Op1))
3565 and then Is_Generic_Type (Etype (Op1))
3567 Check_Non_Static_Context (Op1);
3571 -- Here we have the case of an operand whose type is OK, which is
3572 -- static, and which does not raise constraint error, we can fold.
3575 Set_Is_Static_Expression (N);
3579 end Test_Expression_Is_Foldable;
3583 procedure Test_Expression_Is_Foldable
3590 Rstat : constant Boolean := Is_Static_Expression (Op1)
3591 and then Is_Static_Expression (Op2);
3596 -- If either operand is Any_Type, just propagate to result and
3597 -- do not try to fold, this prevents cascaded errors.
3599 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
3600 Set_Etype (N, Any_Type);
3604 -- If left operand raises constraint error, then replace node N with
3605 -- the raise constraint error node, and we are obviously not foldable.
3606 -- Is_Static_Expression is set from the two operands in the normal way,
3607 -- and we check the right operand if it is in a non-static context.
3609 elsif Raises_Constraint_Error (Op1) then
3611 Check_Non_Static_Context (Op2);
3614 Rewrite_In_Raise_CE (N, Op1);
3615 Set_Is_Static_Expression (N, Rstat);
3619 -- Similar processing for the case of the right operand. Note that
3620 -- we don't use this routine for the short-circuit case, so we do
3621 -- not have to worry about that special case here.
3623 elsif Raises_Constraint_Error (Op2) then
3625 Check_Non_Static_Context (Op1);
3628 Rewrite_In_Raise_CE (N, Op2);
3629 Set_Is_Static_Expression (N, Rstat);
3633 -- Exclude expressions of a generic modular type, as above.
3635 elsif Is_Modular_Integer_Type (Etype (Op1))
3636 and then Is_Generic_Type (Etype (Op1))
3638 Check_Non_Static_Context (Op1);
3642 -- If result is not static, then check non-static contexts on operands
3643 -- since one of them may be static and the other one may not be static
3645 elsif not Rstat then
3646 Check_Non_Static_Context (Op1);
3647 Check_Non_Static_Context (Op2);
3648 Fold := Compile_Time_Known_Value (Op1)
3649 and then Compile_Time_Known_Value (Op2);
3652 -- Else result is static and foldable. Both operands are static,
3653 -- and neither raises constraint error, so we can definitely fold.
3656 Set_Is_Static_Expression (N);
3661 end Test_Expression_Is_Foldable;
3667 procedure To_Bits (U : Uint; B : out Bits) is
3669 for J in 0 .. B'Last loop
3670 B (J) := (U / (2 ** J)) mod 2 /= 0;