include/ChangeLog:
[official-gcc.git] / gcc / ada / sem_eval.adb
blob92bf0a14199ab4d11980909e6279d702cb410ffc
1 ---------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ E V A L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2002 Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Util; use Exp_Util;
35 with Nmake; use Nmake;
36 with Nlists; use Nlists;
37 with Opt; use Opt;
38 with Sem; use Sem;
39 with Sem_Cat; use Sem_Cat;
40 with Sem_Ch8; use Sem_Ch8;
41 with Sem_Res; use Sem_Res;
42 with Sem_Util; use Sem_Util;
43 with Sem_Type; use Sem_Type;
44 with Sem_Warn; use Sem_Warn;
45 with Sinfo; use Sinfo;
46 with Snames; use Snames;
47 with Stand; use Stand;
48 with Stringt; use Stringt;
49 with Tbuild; use Tbuild;
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.
92 ----------------
93 -- Local Data --
94 ----------------
96 type Bits is array (Nat range <>) of Boolean;
97 -- Used to convert unsigned (modular) values for folding logical ops
99 -- The following definitions are used to maintain a cache of nodes that
100 -- have compile time known values. The cache is maintained only for
101 -- discrete types (the most common case), and is populated by calls to
102 -- Compile_Time_Known_Value and Expr_Value, but only used by Expr_Value
103 -- since it is possible for the status to change (in particular it is
104 -- possible for a node to get replaced by a constraint error node).
106 CV_Bits : constant := 5;
107 -- Number of low order bits of Node_Id value used to reference entries
108 -- in the cache table.
110 CV_Cache_Size : constant Nat := 2 ** CV_Bits;
111 -- Size of cache for compile time values
113 subtype CV_Range is Nat range 0 .. CV_Cache_Size;
115 type CV_Entry is record
116 N : Node_Id;
117 V : Uint;
118 end record;
120 type CV_Cache_Array is array (CV_Range) of CV_Entry;
122 CV_Cache : CV_Cache_Array := (others => (Node_High_Bound, Uint_0));
123 -- This is the actual cache, with entries consisting of node/value pairs,
124 -- and the impossible value Node_High_Bound used for unset entries.
126 -----------------------
127 -- Local Subprograms --
128 -----------------------
130 function From_Bits (B : Bits; T : Entity_Id) return Uint;
131 -- Converts a bit string of length B'Length to a Uint value to be used
132 -- for a target of type T, which is a modular type. This procedure
133 -- includes the necessary reduction by the modulus in the case of a
134 -- non-binary modulus (for a binary modulus, the bit string is the
135 -- right length any way so all is well).
137 function Get_String_Val (N : Node_Id) return Node_Id;
138 -- Given a tree node for a folded string or character value, returns
139 -- the corresponding string literal or character literal (one of the
140 -- two must be available, or the operand would not have been marked
141 -- as foldable in the earlier analysis of the operation).
143 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean;
144 -- Bits represents the number of bits in an integer value to be computed
145 -- (but the value has not been computed yet). If this value in Bits is
146 -- reasonable, a result of True is returned, with the implication that
147 -- the caller should go ahead and complete the calculation. If the value
148 -- in Bits is unreasonably large, then an error is posted on node N, and
149 -- False is returned (and the caller skips the proposed calculation).
151 procedure Out_Of_Range (N : Node_Id);
152 -- This procedure is called if it is determined that node N, which
153 -- appears in a non-static context, is a compile time known value
154 -- which is outside its range, i.e. the range of Etype. This is used
155 -- in contexts where this is an illegality if N is static, and should
156 -- generate a warning otherwise.
158 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id);
159 -- N and Exp are nodes representing an expression, Exp is known
160 -- to raise CE. N is rewritten in term of Exp in the optimal way.
162 function String_Type_Len (Stype : Entity_Id) return Uint;
163 -- Given a string type, determines the length of the index type, or,
164 -- if this index type is non-static, the length of the base type of
165 -- this index type. Note that if the string type is itself static,
166 -- then the index type is static, so the second case applies only
167 -- if the string type passed is non-static.
169 function Test (Cond : Boolean) return Uint;
170 pragma Inline (Test);
171 -- This function simply returns the appropriate Boolean'Pos value
172 -- corresponding to the value of Cond as a universal integer. It is
173 -- used for producing the result of the static evaluation of the
174 -- logical operators
176 procedure Test_Expression_Is_Foldable
177 (N : Node_Id;
178 Op1 : Node_Id;
179 Stat : out Boolean;
180 Fold : out Boolean);
181 -- Tests to see if expression N whose single operand is Op1 is foldable,
182 -- i.e. the operand value is known at compile time. If the operation is
183 -- foldable, then Fold is True on return, and Stat indicates whether
184 -- the result is static (i.e. both operands were static). Note that it
185 -- is quite possible for Fold to be True, and Stat to be False, since
186 -- there are cases in which we know the value of an operand even though
187 -- it is not technically static (e.g. the static lower bound of a range
188 -- whose upper bound is non-static).
190 -- If Stat is set False on return, then Expression_Is_Foldable makes a
191 -- call to Check_Non_Static_Context on the operand. If Fold is False on
192 -- return, then all processing is complete, and the caller should
193 -- return, since there is nothing else to do.
195 procedure Test_Expression_Is_Foldable
196 (N : Node_Id;
197 Op1 : Node_Id;
198 Op2 : Node_Id;
199 Stat : out Boolean;
200 Fold : out Boolean);
201 -- Same processing, except applies to an expression N with two operands
202 -- Op1 and Op2.
204 procedure To_Bits (U : Uint; B : out Bits);
205 -- Converts a Uint value to a bit string of length B'Length
207 ------------------------------
208 -- Check_Non_Static_Context --
209 ------------------------------
211 procedure Check_Non_Static_Context (N : Node_Id) is
212 T : Entity_Id := Etype (N);
213 Checks_On : constant Boolean :=
214 not Index_Checks_Suppressed (T)
215 and not Range_Checks_Suppressed (T);
217 begin
218 -- We need the check only for static expressions not raising CE
219 -- We can also ignore cases in which the type is Any_Type
221 if not Is_OK_Static_Expression (N)
222 or else Etype (N) = Any_Type
223 then
224 return;
226 -- Skip this check for non-scalar expressions
228 elsif not Is_Scalar_Type (T) then
229 return;
230 end if;
232 -- Here we have the case of outer level static expression of
233 -- scalar type, where the processing of this procedure is needed.
235 -- For real types, this is where we convert the value to a machine
236 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should
237 -- only need to do this if the parent is a constant declaration,
238 -- since in other cases, gigi should do the necessary conversion
239 -- correctly, but experimentation shows that this is not the case
240 -- on all machines, in particular if we do not convert all literals
241 -- to machine values in non-static contexts, then ACVC test C490001
242 -- fails on Sparc/Solaris and SGI/Irix.
244 if Nkind (N) = N_Real_Literal
245 and then not Is_Machine_Number (N)
246 and then not Is_Generic_Type (Etype (N))
247 and then Etype (N) /= Universal_Real
248 then
249 -- Check that value is in bounds before converting to machine
250 -- number, so as not to lose case where value overflows in the
251 -- least significant bit or less. See B490001.
253 if Is_Out_Of_Range (N, Base_Type (T)) then
254 Out_Of_Range (N);
255 return;
256 end if;
258 -- Note: we have to copy the node, to avoid problems with conformance
259 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
261 Rewrite (N, New_Copy (N));
263 if not Is_Floating_Point_Type (T) then
264 Set_Realval
265 (N, Corresponding_Integer_Value (N) * Small_Value (T));
267 elsif not UR_Is_Zero (Realval (N)) then
268 declare
269 RT : constant Entity_Id := Base_Type (T);
270 X : constant Ureal := Machine (RT, Realval (N), Round);
272 begin
273 -- Warn if result of static rounding actually differs from
274 -- runtime evaluation, which uses round to even.
276 if Warn_On_Biased_Rounding and Rounding_Was_Biased then
277 Error_Msg_N ("static expression does not round to even"
278 & " ('R'M 4.9(38))?", N);
279 end if;
281 Set_Realval (N, X);
282 end;
283 end if;
285 Set_Is_Machine_Number (N);
286 end if;
288 -- Check for out of range universal integer. This is a non-static
289 -- context, so the integer value must be in range of the runtime
290 -- representation of universal integers.
292 -- We do this only within an expression, because that is the only
293 -- case in which non-static universal integer values can occur, and
294 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
295 -- called in contexts like the expression of a number declaration where
296 -- we certainly want to allow out of range values.
298 if Etype (N) = Universal_Integer
299 and then Nkind (N) = N_Integer_Literal
300 and then Nkind (Parent (N)) in N_Subexpr
301 and then
302 (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer))
303 or else
304 Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer)))
305 then
306 Apply_Compile_Time_Constraint_Error
307 (N, "non-static universal integer value out of range?",
308 CE_Range_Check_Failed);
310 -- Check out of range of base type
312 elsif Is_Out_Of_Range (N, Base_Type (T)) then
313 Out_Of_Range (N);
315 -- Give warning if outside subtype (where one or both of the
316 -- bounds of the subtype is static). This warning is omitted
317 -- if the expression appears in a range that could be null
318 -- (warnings are handled elsewhere for this case).
320 elsif T /= Base_Type (T)
321 and then Nkind (Parent (N)) /= N_Range
322 then
323 if Is_In_Range (N, T) then
324 null;
326 elsif Is_Out_Of_Range (N, T) then
327 Apply_Compile_Time_Constraint_Error
328 (N, "value not in range of}?", CE_Range_Check_Failed);
330 elsif Checks_On then
331 Enable_Range_Check (N);
333 else
334 Set_Do_Range_Check (N, False);
335 end if;
336 end if;
337 end Check_Non_Static_Context;
339 ---------------------------------
340 -- Check_String_Literal_Length --
341 ---------------------------------
343 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is
344 begin
345 if not Raises_Constraint_Error (N)
346 and then Is_Constrained (Ttype)
347 then
349 UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype)
350 then
351 Apply_Compile_Time_Constraint_Error
352 (N, "string length wrong for}?",
353 CE_Length_Check_Failed,
354 Ent => Ttype,
355 Typ => Ttype);
356 end if;
357 end if;
358 end Check_String_Literal_Length;
360 --------------------------
361 -- Compile_Time_Compare --
362 --------------------------
364 function Compile_Time_Compare (L, R : Node_Id) return Compare_Result is
365 Ltyp : constant Entity_Id := Etype (L);
366 Rtyp : constant Entity_Id := Etype (R);
368 procedure Compare_Decompose
369 (N : Node_Id;
370 R : out Node_Id;
371 V : out Uint);
372 -- This procedure decomposes the node N into an expression node
373 -- and a signed offset, so that the value of N is equal to the
374 -- value of R plus the value V (which may be negative). If no
375 -- such decomposition is possible, then on return R is a copy
376 -- of N, and V is set to zero.
378 function Compare_Fixup (N : Node_Id) return Node_Id;
379 -- This function deals with replacing 'Last and 'First references
380 -- with their corresponding type bounds, which we then can compare.
381 -- The argument is the original node, the result is the identity,
382 -- unless we have a 'Last/'First reference in which case the value
383 -- returned is the appropriate type bound.
385 function Is_Same_Value (L, R : Node_Id) return Boolean;
386 -- Returns True iff L and R represent expressions that definitely
387 -- have identical (but not necessarily compile time known) values
388 -- Indeed the caller is expected to have already dealt with the
389 -- cases of compile time known values, so these are not tested here.
391 -----------------------
392 -- Compare_Decompose --
393 -----------------------
395 procedure Compare_Decompose
396 (N : Node_Id;
397 R : out Node_Id;
398 V : out Uint)
400 begin
401 if Nkind (N) = N_Op_Add
402 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
403 then
404 R := Left_Opnd (N);
405 V := Intval (Right_Opnd (N));
406 return;
408 elsif Nkind (N) = N_Op_Subtract
409 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
410 then
411 R := Left_Opnd (N);
412 V := UI_Negate (Intval (Right_Opnd (N)));
413 return;
415 elsif Nkind (N) = N_Attribute_Reference then
417 if Attribute_Name (N) = Name_Succ then
418 R := First (Expressions (N));
419 V := Uint_1;
420 return;
422 elsif Attribute_Name (N) = Name_Pred then
423 R := First (Expressions (N));
424 V := Uint_Minus_1;
425 return;
426 end if;
427 end if;
429 R := N;
430 V := Uint_0;
431 end Compare_Decompose;
433 -------------------
434 -- Compare_Fixup --
435 -------------------
437 function Compare_Fixup (N : Node_Id) return Node_Id is
438 Indx : Node_Id;
439 Xtyp : Entity_Id;
440 Subs : Nat;
442 begin
443 if Nkind (N) = N_Attribute_Reference
444 and then (Attribute_Name (N) = Name_First
445 or else
446 Attribute_Name (N) = Name_Last)
447 then
448 Xtyp := Etype (Prefix (N));
450 -- If we have no type, then just abandon the attempt to do
451 -- a fixup, this is probably the result of some other error.
453 if No (Xtyp) then
454 return N;
455 end if;
457 -- Dereference an access type
459 if Is_Access_Type (Xtyp) then
460 Xtyp := Designated_Type (Xtyp);
461 end if;
463 -- If we don't have an array type at this stage, something
464 -- is peculiar, e.g. another error, and we abandon the attempt
465 -- at a fixup.
467 if not Is_Array_Type (Xtyp) then
468 return N;
469 end if;
471 -- Ignore unconstrained array, since bounds are not meaningful
473 if not Is_Constrained (Xtyp) then
474 return N;
475 end if;
477 if Ekind (Xtyp) = E_String_Literal_Subtype then
478 if Attribute_Name (N) = Name_First then
479 return String_Literal_Low_Bound (Xtyp);
481 else -- Attribute_Name (N) = Name_Last
482 return Make_Integer_Literal (Sloc (N),
483 Intval => Intval (String_Literal_Low_Bound (Xtyp))
484 + String_Literal_Length (Xtyp));
485 end if;
486 end if;
488 -- Find correct index type
490 Indx := First_Index (Xtyp);
492 if Present (Expressions (N)) then
493 Subs := UI_To_Int (Expr_Value (First (Expressions (N))));
495 for J in 2 .. Subs loop
496 Indx := Next_Index (Indx);
497 end loop;
498 end if;
500 Xtyp := Etype (Indx);
502 if Attribute_Name (N) = Name_First then
503 return Type_Low_Bound (Xtyp);
505 else -- Attribute_Name (N) = Name_Last
506 return Type_High_Bound (Xtyp);
507 end if;
508 end if;
510 return N;
511 end Compare_Fixup;
513 -------------------
514 -- Is_Same_Value --
515 -------------------
517 function Is_Same_Value (L, R : Node_Id) return Boolean is
518 Lf : constant Node_Id := Compare_Fixup (L);
519 Rf : constant Node_Id := Compare_Fixup (R);
521 begin
522 -- Values are the same if they are the same identifier and the
523 -- identifier refers to a constant object (E_Constant)
525 if Nkind (Lf) = N_Identifier and then Nkind (Rf) = N_Identifier
526 and then Entity (Lf) = Entity (Rf)
527 and then (Ekind (Entity (Lf)) = E_Constant or else
528 Ekind (Entity (Lf)) = E_In_Parameter or else
529 Ekind (Entity (Lf)) = E_Loop_Parameter)
530 then
531 return True;
533 -- Or if they are compile time known and identical
535 elsif Compile_Time_Known_Value (Lf)
536 and then
537 Compile_Time_Known_Value (Rf)
538 and then Expr_Value (Lf) = Expr_Value (Rf)
539 then
540 return True;
542 -- Or if they are both 'First or 'Last values applying to the
543 -- same entity (first and last don't change even if value does)
545 elsif Nkind (Lf) = N_Attribute_Reference
546 and then
547 Nkind (Rf) = N_Attribute_Reference
548 and then Attribute_Name (Lf) = Attribute_Name (Rf)
549 and then (Attribute_Name (Lf) = Name_First
550 or else
551 Attribute_Name (Lf) = Name_Last)
552 and then Is_Entity_Name (Prefix (Lf))
553 and then Is_Entity_Name (Prefix (Rf))
554 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
555 then
556 return True;
558 -- All other cases, we can't tell
560 else
561 return False;
562 end if;
563 end Is_Same_Value;
565 -- Start of processing for Compile_Time_Compare
567 begin
568 -- If either operand could raise constraint error, then we cannot
569 -- know the result at compile time (since CE may be raised!)
571 if not (Cannot_Raise_Constraint_Error (L)
572 and then
573 Cannot_Raise_Constraint_Error (R))
574 then
575 return Unknown;
576 end if;
578 -- Identical operands are most certainly equal
580 if L = R then
581 return EQ;
583 -- If expressions have no types, then do not attempt to determine
584 -- if they are the same, since something funny is going on. One
585 -- case in which this happens is during generic template analysis,
586 -- when bounds are not fully analyzed.
588 elsif No (Ltyp) or else No (Rtyp) then
589 return Unknown;
591 -- We only attempt compile time analysis for scalar values
593 elsif not Is_Scalar_Type (Ltyp)
594 or else Is_Packed_Array_Type (Ltyp)
595 then
596 return Unknown;
598 -- Case where comparison involves two compile time known values
600 elsif Compile_Time_Known_Value (L)
601 and then Compile_Time_Known_Value (R)
602 then
603 -- For the floating-point case, we have to be a little careful, since
604 -- at compile time we are dealing with universal exact values, but at
605 -- runtime, these will be in non-exact target form. That's why the
606 -- returned results are LE and GE below instead of LT and GT.
608 if Is_Floating_Point_Type (Ltyp)
609 or else
610 Is_Floating_Point_Type (Rtyp)
611 then
612 declare
613 Lo : constant Ureal := Expr_Value_R (L);
614 Hi : constant Ureal := Expr_Value_R (R);
616 begin
617 if Lo < Hi then
618 return LE;
619 elsif Lo = Hi then
620 return EQ;
621 else
622 return GE;
623 end if;
624 end;
626 -- For the integer case we know exactly (note that this includes the
627 -- fixed-point case, where we know the run time integer values now)
629 else
630 declare
631 Lo : constant Uint := Expr_Value (L);
632 Hi : constant Uint := Expr_Value (R);
634 begin
635 if Lo < Hi then
636 return LT;
637 elsif Lo = Hi then
638 return EQ;
639 else
640 return GT;
641 end if;
642 end;
643 end if;
645 -- Cases where at least one operand is not known at compile time
647 else
648 -- Here is where we check for comparisons against maximum bounds of
649 -- types, where we know that no value can be outside the bounds of
650 -- the subtype. Note that this routine is allowed to assume that all
651 -- expressions are within their subtype bounds. Callers wishing to
652 -- deal with possibly invalid values must in any case take special
653 -- steps (e.g. conversions to larger types) to avoid this kind of
654 -- optimization, which is always considered to be valid. We do not
655 -- attempt this optimization with generic types, since the type
656 -- bounds may not be meaningful in this case.
658 if Is_Discrete_Type (Ltyp)
659 and then not Is_Generic_Type (Ltyp)
660 and then not Is_Generic_Type (Rtyp)
661 then
662 if Is_Same_Value (R, Type_High_Bound (Ltyp)) then
663 return LE;
665 elsif Is_Same_Value (R, Type_Low_Bound (Ltyp)) then
666 return GE;
668 elsif Is_Same_Value (L, Type_High_Bound (Rtyp)) then
669 return GE;
671 elsif Is_Same_Value (L, Type_Low_Bound (Ltyp)) then
672 return LE;
673 end if;
674 end if;
676 -- Next attempt is to decompose the expressions to extract
677 -- a constant offset resulting from the use of any of the forms:
679 -- expr + literal
680 -- expr - literal
681 -- typ'Succ (expr)
682 -- typ'Pred (expr)
684 -- Then we see if the two expressions are the same value, and if so
685 -- the result is obtained by comparing the offsets.
687 declare
688 Lnode : Node_Id;
689 Loffs : Uint;
690 Rnode : Node_Id;
691 Roffs : Uint;
693 begin
694 Compare_Decompose (L, Lnode, Loffs);
695 Compare_Decompose (R, Rnode, Roffs);
697 if Is_Same_Value (Lnode, Rnode) then
698 if Loffs = Roffs then
699 return EQ;
701 elsif Loffs < Roffs then
702 return LT;
704 else
705 return GT;
706 end if;
708 -- If the expressions are different, we cannot say at compile
709 -- time how they compare, so we return the Unknown indication.
711 else
712 return Unknown;
713 end if;
714 end;
715 end if;
716 end Compile_Time_Compare;
718 ------------------------------
719 -- Compile_Time_Known_Value --
720 ------------------------------
722 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
723 K : constant Node_Kind := Nkind (Op);
724 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
726 begin
727 -- Never known at compile time if bad type or raises constraint error
728 -- or empty (latter case occurs only as a result of a previous error)
730 if No (Op)
731 or else Op = Error
732 or else Etype (Op) = Any_Type
733 or else Raises_Constraint_Error (Op)
734 then
735 return False;
736 end if;
738 -- If we have an entity name, then see if it is the name of a constant
739 -- and if so, test the corresponding constant value, or the name of
740 -- an enumeration literal, which is always a constant.
742 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
743 declare
744 E : constant Entity_Id := Entity (Op);
745 V : Node_Id;
747 begin
748 -- Never known at compile time if it is a packed array value.
749 -- We might want to try to evaluate these at compile time one
750 -- day, but we do not make that attempt now.
752 if Is_Packed_Array_Type (Etype (Op)) then
753 return False;
754 end if;
756 if Ekind (E) = E_Enumeration_Literal then
757 return True;
759 elsif Ekind (E) = E_Constant then
760 V := Constant_Value (E);
761 return Present (V) and then Compile_Time_Known_Value (V);
762 end if;
763 end;
765 -- We have a value, see if it is compile time known
767 else
768 -- Integer literals are worth storing in the cache
770 if K = N_Integer_Literal then
771 CV_Ent.N := Op;
772 CV_Ent.V := Intval (Op);
773 return True;
775 -- Other literals and NULL are known at compile time
777 elsif
778 K = N_Character_Literal
779 or else
780 K = N_Real_Literal
781 or else
782 K = N_String_Literal
783 or else
784 K = N_Null
785 then
786 return True;
788 -- Any reference to Null_Parameter is known at compile time. No
789 -- other attribute references (that have not already been folded)
790 -- are known at compile time.
792 elsif K = N_Attribute_Reference then
793 return Attribute_Name (Op) = Name_Null_Parameter;
794 end if;
795 end if;
797 -- If we fall through, not known at compile time
799 return False;
801 -- If we get an exception while trying to do this test, then some error
802 -- has occurred, and we simply say that the value is not known after all
804 exception
805 when others =>
806 return False;
807 end Compile_Time_Known_Value;
809 --------------------------------------
810 -- Compile_Time_Known_Value_Or_Aggr --
811 --------------------------------------
813 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is
814 begin
815 -- If we have an entity name, then see if it is the name of a constant
816 -- and if so, test the corresponding constant value, or the name of
817 -- an enumeration literal, which is always a constant.
819 if Is_Entity_Name (Op) then
820 declare
821 E : constant Entity_Id := Entity (Op);
822 V : Node_Id;
824 begin
825 if Ekind (E) = E_Enumeration_Literal then
826 return True;
828 elsif Ekind (E) /= E_Constant then
829 return False;
831 else
832 V := Constant_Value (E);
833 return Present (V)
834 and then Compile_Time_Known_Value_Or_Aggr (V);
835 end if;
836 end;
838 -- We have a value, see if it is compile time known
840 else
841 if Compile_Time_Known_Value (Op) then
842 return True;
844 elsif Nkind (Op) = N_Aggregate then
846 if Present (Expressions (Op)) then
847 declare
848 Expr : Node_Id;
850 begin
851 Expr := First (Expressions (Op));
852 while Present (Expr) loop
853 if not Compile_Time_Known_Value_Or_Aggr (Expr) then
854 return False;
855 end if;
857 Next (Expr);
858 end loop;
859 end;
860 end if;
862 if Present (Component_Associations (Op)) then
863 declare
864 Cass : Node_Id;
866 begin
867 Cass := First (Component_Associations (Op));
868 while Present (Cass) loop
869 if not
870 Compile_Time_Known_Value_Or_Aggr (Expression (Cass))
871 then
872 return False;
873 end if;
875 Next (Cass);
876 end loop;
877 end;
878 end if;
880 return True;
882 -- All other types of values are not known at compile time
884 else
885 return False;
886 end if;
888 end if;
889 end Compile_Time_Known_Value_Or_Aggr;
891 -----------------
892 -- Eval_Actual --
893 -----------------
895 -- This is only called for actuals of functions that are not predefined
896 -- operators (which have already been rewritten as operators at this
897 -- stage), so the call can never be folded, and all that needs doing for
898 -- the actual is to do the check for a non-static context.
900 procedure Eval_Actual (N : Node_Id) is
901 begin
902 Check_Non_Static_Context (N);
903 end Eval_Actual;
905 --------------------
906 -- Eval_Allocator --
907 --------------------
909 -- Allocators are never static, so all we have to do is to do the
910 -- check for a non-static context if an expression is present.
912 procedure Eval_Allocator (N : Node_Id) is
913 Expr : constant Node_Id := Expression (N);
915 begin
916 if Nkind (Expr) = N_Qualified_Expression then
917 Check_Non_Static_Context (Expression (Expr));
918 end if;
919 end Eval_Allocator;
921 ------------------------
922 -- Eval_Arithmetic_Op --
923 ------------------------
925 -- Arithmetic operations are static functions, so the result is static
926 -- if both operands are static (RM 4.9(7), 4.9(20)).
928 procedure Eval_Arithmetic_Op (N : Node_Id) is
929 Left : constant Node_Id := Left_Opnd (N);
930 Right : constant Node_Id := Right_Opnd (N);
931 Ltype : constant Entity_Id := Etype (Left);
932 Rtype : constant Entity_Id := Etype (Right);
933 Stat : Boolean;
934 Fold : Boolean;
936 begin
937 -- If not foldable we are done
939 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
941 if not Fold then
942 return;
943 end if;
945 -- Fold for cases where both operands are of integer type
947 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
948 declare
949 Left_Int : constant Uint := Expr_Value (Left);
950 Right_Int : constant Uint := Expr_Value (Right);
951 Result : Uint;
953 begin
954 case Nkind (N) is
956 when N_Op_Add =>
957 Result := Left_Int + Right_Int;
959 when N_Op_Subtract =>
960 Result := Left_Int - Right_Int;
962 when N_Op_Multiply =>
963 if OK_Bits
964 (N, UI_From_Int
965 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
966 then
967 Result := Left_Int * Right_Int;
968 else
969 Result := Left_Int;
970 end if;
972 when N_Op_Divide =>
974 -- The exception Constraint_Error is raised by integer
975 -- division, rem and mod if the right operand is zero.
977 if Right_Int = 0 then
978 Apply_Compile_Time_Constraint_Error
979 (N, "division by zero", CE_Divide_By_Zero);
980 return;
981 else
982 Result := Left_Int / Right_Int;
983 end if;
985 when N_Op_Mod =>
987 -- The exception Constraint_Error is raised by integer
988 -- division, rem and mod if the right operand is zero.
990 if Right_Int = 0 then
991 Apply_Compile_Time_Constraint_Error
992 (N, "mod with zero divisor", CE_Divide_By_Zero);
993 return;
994 else
995 Result := Left_Int mod Right_Int;
996 end if;
998 when N_Op_Rem =>
1000 -- The exception Constraint_Error is raised by integer
1001 -- division, rem and mod if the right operand is zero.
1003 if Right_Int = 0 then
1004 Apply_Compile_Time_Constraint_Error
1005 (N, "rem with zero divisor", CE_Divide_By_Zero);
1006 return;
1007 else
1008 Result := Left_Int rem Right_Int;
1009 end if;
1011 when others =>
1012 raise Program_Error;
1013 end case;
1015 -- Adjust the result by the modulus if the type is a modular type
1017 if Is_Modular_Integer_Type (Ltype) then
1018 Result := Result mod Modulus (Ltype);
1019 end if;
1021 Fold_Uint (N, Result);
1022 end;
1024 -- Cases where at least one operand is a real. We handle the cases
1025 -- of both reals, or mixed/real integer cases (the latter happen
1026 -- only for divide and multiply, and the result is always real).
1028 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
1029 declare
1030 Left_Real : Ureal;
1031 Right_Real : Ureal;
1032 Result : Ureal;
1034 begin
1035 if Is_Real_Type (Ltype) then
1036 Left_Real := Expr_Value_R (Left);
1037 else
1038 Left_Real := UR_From_Uint (Expr_Value (Left));
1039 end if;
1041 if Is_Real_Type (Rtype) then
1042 Right_Real := Expr_Value_R (Right);
1043 else
1044 Right_Real := UR_From_Uint (Expr_Value (Right));
1045 end if;
1047 if Nkind (N) = N_Op_Add then
1048 Result := Left_Real + Right_Real;
1050 elsif Nkind (N) = N_Op_Subtract then
1051 Result := Left_Real - Right_Real;
1053 elsif Nkind (N) = N_Op_Multiply then
1054 Result := Left_Real * Right_Real;
1056 else pragma Assert (Nkind (N) = N_Op_Divide);
1057 if UR_Is_Zero (Right_Real) then
1058 Apply_Compile_Time_Constraint_Error
1059 (N, "division by zero", CE_Divide_By_Zero);
1060 return;
1061 end if;
1063 Result := Left_Real / Right_Real;
1064 end if;
1066 Fold_Ureal (N, Result);
1067 end;
1068 end if;
1070 Set_Is_Static_Expression (N, Stat);
1071 end Eval_Arithmetic_Op;
1073 ----------------------------
1074 -- Eval_Character_Literal --
1075 ----------------------------
1077 -- Nothing to be done!
1079 procedure Eval_Character_Literal (N : Node_Id) is
1080 pragma Warnings (Off, N);
1082 begin
1083 null;
1084 end Eval_Character_Literal;
1086 ------------------------
1087 -- Eval_Concatenation --
1088 ------------------------
1090 -- Concatenation is a static function, so the result is static if
1091 -- both operands are static (RM 4.9(7), 4.9(21)).
1093 procedure Eval_Concatenation (N : Node_Id) is
1094 Left : constant Node_Id := Left_Opnd (N);
1095 Right : constant Node_Id := Right_Opnd (N);
1096 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
1097 Stat : Boolean;
1098 Fold : Boolean;
1100 begin
1101 -- Concatenation is never static in Ada 83, so if Ada 83
1102 -- check operand non-static context
1104 if Ada_83
1105 and then Comes_From_Source (N)
1106 then
1107 Check_Non_Static_Context (Left);
1108 Check_Non_Static_Context (Right);
1109 return;
1110 end if;
1112 -- If not foldable we are done. In principle concatenation that yields
1113 -- any string type is static (i.e. an array type of character types).
1114 -- However, character types can include enumeration literals, and
1115 -- concatenation in that case cannot be described by a literal, so we
1116 -- only consider the operation static if the result is an array of
1117 -- (a descendant of) a predefined character type.
1119 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1121 if (C_Typ = Standard_Character
1122 or else C_Typ = Standard_Wide_Character)
1123 and then Fold
1124 then
1125 null;
1126 else
1127 Set_Is_Static_Expression (N, False);
1128 return;
1129 end if;
1131 -- Compile time string concatenation.
1133 -- ??? Note that operands that are aggregates can be marked as
1134 -- static, so we should attempt at a later stage to fold
1135 -- concatenations with such aggregates.
1137 declare
1138 Left_Str : constant Node_Id := Get_String_Val (Left);
1139 Left_Len : Nat;
1140 Right_Str : constant Node_Id := Get_String_Val (Right);
1142 begin
1143 -- Establish new string literal, and store left operand. We make
1144 -- sure to use the special Start_String that takes an operand if
1145 -- the left operand is a string literal. Since this is optimized
1146 -- in the case where that is the most recently created string
1147 -- literal, we ensure efficient time/space behavior for the
1148 -- case of a concatenation of a series of string literals.
1150 if Nkind (Left_Str) = N_String_Literal then
1151 Left_Len := String_Length (Strval (Left_Str));
1152 Start_String (Strval (Left_Str));
1153 else
1154 Start_String;
1155 Store_String_Char (Char_Literal_Value (Left_Str));
1156 Left_Len := 1;
1157 end if;
1159 -- Now append the characters of the right operand
1161 if Nkind (Right_Str) = N_String_Literal then
1162 declare
1163 S : constant String_Id := Strval (Right_Str);
1165 begin
1166 for J in 1 .. String_Length (S) loop
1167 Store_String_Char (Get_String_Char (S, J));
1168 end loop;
1169 end;
1170 else
1171 Store_String_Char (Char_Literal_Value (Right_Str));
1172 end if;
1174 Set_Is_Static_Expression (N, Stat);
1176 if Stat then
1178 -- If left operand is the empty string, the result is the
1179 -- right operand, including its bounds if anomalous.
1181 if Left_Len = 0
1182 and then Is_Array_Type (Etype (Right))
1183 and then Etype (Right) /= Any_String
1184 then
1185 Set_Etype (N, Etype (Right));
1186 end if;
1188 Fold_Str (N, End_String);
1189 end if;
1190 end;
1191 end Eval_Concatenation;
1193 ---------------------------------
1194 -- Eval_Conditional_Expression --
1195 ---------------------------------
1197 -- This GNAT internal construct can never be statically folded, so the
1198 -- only required processing is to do the check for non-static context
1199 -- for the two expression operands.
1201 procedure Eval_Conditional_Expression (N : Node_Id) is
1202 Condition : constant Node_Id := First (Expressions (N));
1203 Then_Expr : constant Node_Id := Next (Condition);
1204 Else_Expr : constant Node_Id := Next (Then_Expr);
1206 begin
1207 Check_Non_Static_Context (Then_Expr);
1208 Check_Non_Static_Context (Else_Expr);
1209 end Eval_Conditional_Expression;
1211 ----------------------
1212 -- Eval_Entity_Name --
1213 ----------------------
1215 -- This procedure is used for identifiers and expanded names other than
1216 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
1217 -- static if they denote a static constant (RM 4.9(6)) or if the name
1218 -- denotes an enumeration literal (RM 4.9(22)).
1220 procedure Eval_Entity_Name (N : Node_Id) is
1221 Def_Id : constant Entity_Id := Entity (N);
1222 Val : Node_Id;
1224 begin
1225 -- Enumeration literals are always considered to be constants
1226 -- and cannot raise constraint error (RM 4.9(22)).
1228 if Ekind (Def_Id) = E_Enumeration_Literal then
1229 Set_Is_Static_Expression (N);
1230 return;
1232 -- A name is static if it denotes a static constant (RM 4.9(5)), and
1233 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
1234 -- it does not violate 10.2.1(8) here, since this is not a variable.
1236 elsif Ekind (Def_Id) = E_Constant then
1238 -- Deferred constants must always be treated as nonstatic
1239 -- outside the scope of their full view.
1241 if Present (Full_View (Def_Id))
1242 and then not In_Open_Scopes (Scope (Def_Id))
1243 then
1244 Val := Empty;
1245 else
1246 Val := Constant_Value (Def_Id);
1247 end if;
1249 if Present (Val) then
1250 Set_Is_Static_Expression
1251 (N, Is_Static_Expression (Val)
1252 and then Is_Static_Subtype (Etype (Def_Id)));
1253 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
1255 if not Is_Static_Expression (N)
1256 and then not Is_Generic_Type (Etype (N))
1257 then
1258 Validate_Static_Object_Name (N);
1259 end if;
1261 return;
1262 end if;
1263 end if;
1265 -- Fall through if the name is not static.
1267 Validate_Static_Object_Name (N);
1268 end Eval_Entity_Name;
1270 ----------------------------
1271 -- Eval_Indexed_Component --
1272 ----------------------------
1274 -- Indexed components are never static, so we need to perform the check
1275 -- for non-static context on the index values. Then, we check if the
1276 -- value can be obtained at compile time, even though it is non-static.
1278 procedure Eval_Indexed_Component (N : Node_Id) is
1279 Expr : Node_Id;
1281 begin
1282 Expr := First (Expressions (N));
1283 while Present (Expr) loop
1284 Check_Non_Static_Context (Expr);
1285 Next (Expr);
1286 end loop;
1288 -- See if this is a constant array reference
1290 if List_Length (Expressions (N)) = 1
1291 and then Is_Entity_Name (Prefix (N))
1292 and then Ekind (Entity (Prefix (N))) = E_Constant
1293 and then Present (Constant_Value (Entity (Prefix (N))))
1294 then
1295 declare
1296 Loc : constant Source_Ptr := Sloc (N);
1297 Arr : constant Node_Id := Constant_Value (Entity (Prefix (N)));
1298 Sub : constant Node_Id := First (Expressions (N));
1300 Atyp : Entity_Id;
1301 -- Type of array
1303 Lin : Nat;
1304 -- Linear one's origin subscript value for array reference
1306 Lbd : Node_Id;
1307 -- Lower bound of the first array index
1309 Elm : Node_Id;
1310 -- Value from constant array
1312 begin
1313 Atyp := Etype (Arr);
1315 if Is_Access_Type (Atyp) then
1316 Atyp := Designated_Type (Atyp);
1317 end if;
1319 -- If we have an array type (we should have but perhaps there
1320 -- are error cases where this is not the case), then see if we
1321 -- can do a constant evaluation of the array reference.
1323 if Is_Array_Type (Atyp) then
1324 if Ekind (Atyp) = E_String_Literal_Subtype then
1325 Lbd := String_Literal_Low_Bound (Atyp);
1326 else
1327 Lbd := Type_Low_Bound (Etype (First_Index (Atyp)));
1328 end if;
1330 if Compile_Time_Known_Value (Sub)
1331 and then Nkind (Arr) = N_Aggregate
1332 and then Compile_Time_Known_Value (Lbd)
1333 and then Is_Discrete_Type (Component_Type (Atyp))
1334 then
1335 Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1;
1337 if List_Length (Expressions (Arr)) >= Lin then
1338 Elm := Pick (Expressions (Arr), Lin);
1340 -- If the resulting expression is compile time known,
1341 -- then we can rewrite the indexed component with this
1342 -- value, being sure to mark the result as non-static.
1343 -- We also reset the Sloc, in case this generates an
1344 -- error later on (e.g. 136'Access).
1346 if Compile_Time_Known_Value (Elm) then
1347 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
1348 Set_Is_Static_Expression (N, False);
1349 Set_Sloc (N, Loc);
1350 end if;
1351 end if;
1352 end if;
1353 end if;
1354 end;
1355 end if;
1356 end Eval_Indexed_Component;
1358 --------------------------
1359 -- Eval_Integer_Literal --
1360 --------------------------
1362 -- Numeric literals are static (RM 4.9(1)), and have already been marked
1363 -- as static by the analyzer. The reason we did it that early is to allow
1364 -- the possibility of turning off the Is_Static_Expression flag after
1365 -- analysis, but before resolution, when integer literals are generated
1366 -- in the expander that do not correspond to static expressions.
1368 procedure Eval_Integer_Literal (N : Node_Id) is
1369 T : constant Entity_Id := Etype (N);
1371 begin
1372 -- If the literal appears in a non-expression context, then it is
1373 -- certainly appearing in a non-static context, so check it. This
1374 -- is actually a redundant check, since Check_Non_Static_Context
1375 -- would check it, but it seems worth while avoiding the call.
1377 if Nkind (Parent (N)) not in N_Subexpr then
1378 Check_Non_Static_Context (N);
1379 end if;
1381 -- Modular integer literals must be in their base range
1383 if Is_Modular_Integer_Type (T)
1384 and then Is_Out_Of_Range (N, Base_Type (T))
1385 then
1386 Out_Of_Range (N);
1387 end if;
1388 end Eval_Integer_Literal;
1390 ---------------------
1391 -- Eval_Logical_Op --
1392 ---------------------
1394 -- Logical operations are static functions, so the result is potentially
1395 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
1397 procedure Eval_Logical_Op (N : Node_Id) is
1398 Left : constant Node_Id := Left_Opnd (N);
1399 Right : constant Node_Id := Right_Opnd (N);
1400 Stat : Boolean;
1401 Fold : Boolean;
1403 begin
1404 -- If not foldable we are done
1406 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1408 if not Fold then
1409 return;
1410 end if;
1412 -- Compile time evaluation of logical operation
1414 declare
1415 Left_Int : constant Uint := Expr_Value (Left);
1416 Right_Int : constant Uint := Expr_Value (Right);
1418 begin
1419 if Is_Modular_Integer_Type (Etype (N)) then
1420 declare
1421 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
1422 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
1424 begin
1425 To_Bits (Left_Int, Left_Bits);
1426 To_Bits (Right_Int, Right_Bits);
1428 -- Note: should really be able to use array ops instead of
1429 -- these loops, but they weren't working at the time ???
1431 if Nkind (N) = N_Op_And then
1432 for J in Left_Bits'Range loop
1433 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
1434 end loop;
1436 elsif Nkind (N) = N_Op_Or then
1437 for J in Left_Bits'Range loop
1438 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
1439 end loop;
1441 else
1442 pragma Assert (Nkind (N) = N_Op_Xor);
1444 for J in Left_Bits'Range loop
1445 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
1446 end loop;
1447 end if;
1449 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)));
1450 end;
1452 else
1453 pragma Assert (Is_Boolean_Type (Etype (N)));
1455 if Nkind (N) = N_Op_And then
1456 Fold_Uint (N,
1457 Test (Is_True (Left_Int) and then Is_True (Right_Int)));
1459 elsif Nkind (N) = N_Op_Or then
1460 Fold_Uint (N,
1461 Test (Is_True (Left_Int) or else Is_True (Right_Int)));
1463 else
1464 pragma Assert (Nkind (N) = N_Op_Xor);
1465 Fold_Uint (N,
1466 Test (Is_True (Left_Int) xor Is_True (Right_Int)));
1467 end if;
1468 end if;
1470 Set_Is_Static_Expression (N, Stat);
1471 end;
1472 end Eval_Logical_Op;
1474 ------------------------
1475 -- Eval_Membership_Op --
1476 ------------------------
1478 -- A membership test is potentially static if the expression is static,
1479 -- and the range is a potentially static range, or is a subtype mark
1480 -- denoting a static subtype (RM 4.9(12)).
1482 procedure Eval_Membership_Op (N : Node_Id) is
1483 Left : constant Node_Id := Left_Opnd (N);
1484 Right : constant Node_Id := Right_Opnd (N);
1485 Def_Id : Entity_Id;
1486 Lo : Node_Id;
1487 Hi : Node_Id;
1488 Result : Boolean;
1489 Stat : Boolean;
1490 Fold : Boolean;
1492 begin
1493 -- Ignore if error in either operand, except to make sure that
1494 -- Any_Type is properly propagated to avoid junk cascaded errors.
1496 if Etype (Left) = Any_Type
1497 or else Etype (Right) = Any_Type
1498 then
1499 Set_Etype (N, Any_Type);
1500 return;
1501 end if;
1503 -- Case of right operand is a subtype name
1505 if Is_Entity_Name (Right) then
1506 Def_Id := Entity (Right);
1508 if (Is_Scalar_Type (Def_Id) or else Is_String_Type (Def_Id))
1509 and then Is_OK_Static_Subtype (Def_Id)
1510 then
1511 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
1513 if not Fold or else not Stat then
1514 return;
1515 end if;
1516 else
1517 Check_Non_Static_Context (Left);
1518 return;
1519 end if;
1521 -- For string membership tests we will check the length
1522 -- further below.
1524 if not Is_String_Type (Def_Id) then
1525 Lo := Type_Low_Bound (Def_Id);
1526 Hi := Type_High_Bound (Def_Id);
1528 else
1529 Lo := Empty;
1530 Hi := Empty;
1531 end if;
1533 -- Case of right operand is a range
1535 else
1536 if Is_Static_Range (Right) then
1537 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
1539 if not Fold or else not Stat then
1540 return;
1542 -- If one bound of range raises CE, then don't try to fold
1544 elsif not Is_OK_Static_Range (Right) then
1545 Check_Non_Static_Context (Left);
1546 return;
1547 end if;
1549 else
1550 Check_Non_Static_Context (Left);
1551 return;
1552 end if;
1554 -- Here we know range is an OK static range
1556 Lo := Low_Bound (Right);
1557 Hi := High_Bound (Right);
1558 end if;
1560 -- For strings we check that the length of the string expression is
1561 -- compatible with the string subtype if the subtype is constrained,
1562 -- or if unconstrained then the test is always true.
1564 if Is_String_Type (Etype (Right)) then
1565 if not Is_Constrained (Etype (Right)) then
1566 Result := True;
1568 else
1569 declare
1570 Typlen : constant Uint := String_Type_Len (Etype (Right));
1571 Strlen : constant Uint :=
1572 UI_From_Int (String_Length (Strval (Get_String_Val (Left))));
1573 begin
1574 Result := (Typlen = Strlen);
1575 end;
1576 end if;
1578 -- Fold the membership test. We know we have a static range and Lo
1579 -- and Hi are set to the expressions for the end points of this range.
1581 elsif Is_Real_Type (Etype (Right)) then
1582 declare
1583 Leftval : constant Ureal := Expr_Value_R (Left);
1585 begin
1586 Result := Expr_Value_R (Lo) <= Leftval
1587 and then Leftval <= Expr_Value_R (Hi);
1588 end;
1590 else
1591 declare
1592 Leftval : constant Uint := Expr_Value (Left);
1594 begin
1595 Result := Expr_Value (Lo) <= Leftval
1596 and then Leftval <= Expr_Value (Hi);
1597 end;
1598 end if;
1600 if Nkind (N) = N_Not_In then
1601 Result := not Result;
1602 end if;
1604 Fold_Uint (N, Test (Result));
1605 Warn_On_Known_Condition (N);
1607 end Eval_Membership_Op;
1609 ------------------------
1610 -- Eval_Named_Integer --
1611 ------------------------
1613 procedure Eval_Named_Integer (N : Node_Id) is
1614 begin
1615 Fold_Uint (N,
1616 Expr_Value (Expression (Declaration_Node (Entity (N)))));
1617 end Eval_Named_Integer;
1619 ---------------------
1620 -- Eval_Named_Real --
1621 ---------------------
1623 procedure Eval_Named_Real (N : Node_Id) is
1624 begin
1625 Fold_Ureal (N,
1626 Expr_Value_R (Expression (Declaration_Node (Entity (N)))));
1627 end Eval_Named_Real;
1629 -------------------
1630 -- Eval_Op_Expon --
1631 -------------------
1633 -- Exponentiation is a static functions, so the result is potentially
1634 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
1636 procedure Eval_Op_Expon (N : Node_Id) is
1637 Left : constant Node_Id := Left_Opnd (N);
1638 Right : constant Node_Id := Right_Opnd (N);
1639 Stat : Boolean;
1640 Fold : Boolean;
1642 begin
1643 -- If not foldable we are done
1645 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1647 if not Fold then
1648 return;
1649 end if;
1651 -- Fold exponentiation operation
1653 declare
1654 Right_Int : constant Uint := Expr_Value (Right);
1656 begin
1657 -- Integer case
1659 if Is_Integer_Type (Etype (Left)) then
1660 declare
1661 Left_Int : constant Uint := Expr_Value (Left);
1662 Result : Uint;
1664 begin
1665 -- Exponentiation of an integer raises the exception
1666 -- Constraint_Error for a negative exponent (RM 4.5.6)
1668 if Right_Int < 0 then
1669 Apply_Compile_Time_Constraint_Error
1670 (N, "integer exponent negative", CE_Range_Check_Failed);
1671 return;
1673 else
1674 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
1675 Result := Left_Int ** Right_Int;
1676 else
1677 Result := Left_Int;
1678 end if;
1680 if Is_Modular_Integer_Type (Etype (N)) then
1681 Result := Result mod Modulus (Etype (N));
1682 end if;
1684 Fold_Uint (N, Result);
1685 end if;
1686 end;
1688 -- Real case
1690 else
1691 declare
1692 Left_Real : constant Ureal := Expr_Value_R (Left);
1694 begin
1695 -- Cannot have a zero base with a negative exponent
1697 if UR_Is_Zero (Left_Real) then
1699 if Right_Int < 0 then
1700 Apply_Compile_Time_Constraint_Error
1701 (N, "zero ** negative integer", CE_Range_Check_Failed);
1702 return;
1703 else
1704 Fold_Ureal (N, Ureal_0);
1705 end if;
1707 else
1708 Fold_Ureal (N, Left_Real ** Right_Int);
1709 end if;
1710 end;
1711 end if;
1713 Set_Is_Static_Expression (N, Stat);
1714 end;
1715 end Eval_Op_Expon;
1717 -----------------
1718 -- Eval_Op_Not --
1719 -----------------
1721 -- The not operation is a static functions, so the result is potentially
1722 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
1724 procedure Eval_Op_Not (N : Node_Id) is
1725 Right : constant Node_Id := Right_Opnd (N);
1726 Stat : Boolean;
1727 Fold : Boolean;
1729 begin
1730 -- If not foldable we are done
1732 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
1734 if not Fold then
1735 return;
1736 end if;
1738 -- Fold not operation
1740 declare
1741 Rint : constant Uint := Expr_Value (Right);
1742 Typ : constant Entity_Id := Etype (N);
1744 begin
1745 -- Negation is equivalent to subtracting from the modulus minus
1746 -- one. For a binary modulus this is equivalent to the ones-
1747 -- component of the original value. For non-binary modulus this
1748 -- is an arbitrary but consistent definition.
1750 if Is_Modular_Integer_Type (Typ) then
1751 Fold_Uint (N, Modulus (Typ) - 1 - Rint);
1753 else
1754 pragma Assert (Is_Boolean_Type (Typ));
1755 Fold_Uint (N, Test (not Is_True (Rint)));
1756 end if;
1758 Set_Is_Static_Expression (N, Stat);
1759 end;
1760 end Eval_Op_Not;
1762 -------------------------------
1763 -- Eval_Qualified_Expression --
1764 -------------------------------
1766 -- A qualified expression is potentially static if its subtype mark denotes
1767 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
1769 procedure Eval_Qualified_Expression (N : Node_Id) is
1770 Operand : constant Node_Id := Expression (N);
1771 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
1773 Stat : Boolean;
1774 Fold : Boolean;
1775 Hex : Boolean;
1777 begin
1778 -- Can only fold if target is string or scalar and subtype is static
1779 -- Also, do not fold if our parent is an allocator (this is because
1780 -- the qualified expression is really part of the syntactic structure
1781 -- of an allocator, and we do not want to end up with something that
1782 -- corresponds to "new 1" where the 1 is the result of folding a
1783 -- qualified expression).
1785 if not Is_Static_Subtype (Target_Type)
1786 or else Nkind (Parent (N)) = N_Allocator
1787 then
1788 Check_Non_Static_Context (Operand);
1789 return;
1790 end if;
1792 -- If not foldable we are done
1794 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
1796 if not Fold then
1797 return;
1799 -- Don't try fold if target type has constraint error bounds
1801 elsif not Is_OK_Static_Subtype (Target_Type) then
1802 Set_Raises_Constraint_Error (N);
1803 return;
1804 end if;
1806 -- Here we will fold, save Print_In_Hex indication
1808 Hex := Nkind (Operand) = N_Integer_Literal
1809 and then Print_In_Hex (Operand);
1811 -- Fold the result of qualification
1813 if Is_Discrete_Type (Target_Type) then
1814 Fold_Uint (N, Expr_Value (Operand));
1815 Set_Is_Static_Expression (N, Stat);
1817 -- Preserve Print_In_Hex indication
1819 if Hex and then Nkind (N) = N_Integer_Literal then
1820 Set_Print_In_Hex (N);
1821 end if;
1823 elsif Is_Real_Type (Target_Type) then
1824 Fold_Ureal (N, Expr_Value_R (Operand));
1825 Set_Is_Static_Expression (N, Stat);
1827 else
1828 Fold_Str (N, Strval (Get_String_Val (Operand)));
1830 if not Stat then
1831 Set_Is_Static_Expression (N, False);
1832 else
1833 Check_String_Literal_Length (N, Target_Type);
1834 end if;
1836 return;
1837 end if;
1839 if Is_Out_Of_Range (N, Etype (N)) then
1840 Out_Of_Range (N);
1841 end if;
1843 end Eval_Qualified_Expression;
1845 -----------------------
1846 -- Eval_Real_Literal --
1847 -----------------------
1849 -- Numeric literals are static (RM 4.9(1)), and have already been marked
1850 -- as static by the analyzer. The reason we did it that early is to allow
1851 -- the possibility of turning off the Is_Static_Expression flag after
1852 -- analysis, but before resolution, when integer literals are generated
1853 -- in the expander that do not correspond to static expressions.
1855 procedure Eval_Real_Literal (N : Node_Id) is
1856 begin
1857 -- If the literal appears in a non-expression context, then it is
1858 -- certainly appearing in a non-static context, so check it.
1860 if Nkind (Parent (N)) not in N_Subexpr then
1861 Check_Non_Static_Context (N);
1862 end if;
1864 end Eval_Real_Literal;
1866 ------------------------
1867 -- Eval_Relational_Op --
1868 ------------------------
1870 -- Relational operations are static functions, so the result is static
1871 -- if both operands are static (RM 4.9(7), 4.9(20)).
1873 procedure Eval_Relational_Op (N : Node_Id) is
1874 Left : constant Node_Id := Left_Opnd (N);
1875 Right : constant Node_Id := Right_Opnd (N);
1876 Typ : constant Entity_Id := Etype (Left);
1877 Result : Boolean;
1878 Stat : Boolean;
1879 Fold : Boolean;
1881 begin
1882 -- One special case to deal with first. If we can tell that
1883 -- the result will be false because the lengths of one or
1884 -- more index subtypes are compile time known and different,
1885 -- then we can replace the entire result by False. We only
1886 -- do this for one dimensional arrays, because the case of
1887 -- multi-dimensional arrays is rare and too much trouble!
1889 if Is_Array_Type (Typ)
1890 and then Number_Dimensions (Typ) = 1
1891 and then (Nkind (N) = N_Op_Eq
1892 or else Nkind (N) = N_Op_Ne)
1893 then
1894 if Raises_Constraint_Error (Left)
1895 or else Raises_Constraint_Error (Right)
1896 then
1897 return;
1898 end if;
1900 declare
1901 procedure Get_Static_Length (Op : Node_Id; Len : out Uint);
1902 -- If Op is an expression for a constrained array with a
1903 -- known at compile time length, then Len is set to this
1904 -- (non-negative length). Otherwise Len is set to minus 1.
1906 procedure Get_Static_Length (Op : Node_Id; Len : out Uint) is
1907 T : Entity_Id;
1909 begin
1910 if Nkind (Op) = N_String_Literal then
1911 Len := UI_From_Int (String_Length (Strval (Op)));
1913 elsif not Is_Constrained (Etype (Op)) then
1914 Len := Uint_Minus_1;
1916 else
1917 T := Etype (First_Index (Etype (Op)));
1919 if Is_Discrete_Type (T)
1920 and then
1921 Compile_Time_Known_Value (Type_Low_Bound (T))
1922 and then
1923 Compile_Time_Known_Value (Type_High_Bound (T))
1924 then
1925 Len := UI_Max (Uint_0,
1926 Expr_Value (Type_High_Bound (T)) -
1927 Expr_Value (Type_Low_Bound (T)) + 1);
1928 else
1929 Len := Uint_Minus_1;
1930 end if;
1931 end if;
1932 end Get_Static_Length;
1934 Len_L : Uint;
1935 Len_R : Uint;
1937 begin
1938 Get_Static_Length (Left, Len_L);
1939 Get_Static_Length (Right, Len_R);
1941 if Len_L /= Uint_Minus_1
1942 and then Len_R /= Uint_Minus_1
1943 and then Len_L /= Len_R
1944 then
1945 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne));
1946 Set_Is_Static_Expression (N, False);
1947 Warn_On_Known_Condition (N);
1948 return;
1949 end if;
1950 end;
1951 end if;
1953 -- Can only fold if type is scalar (don't fold string ops)
1955 if not Is_Scalar_Type (Typ) then
1956 Check_Non_Static_Context (Left);
1957 Check_Non_Static_Context (Right);
1958 return;
1959 end if;
1961 -- If not foldable we are done
1963 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1965 if not Fold then
1966 return;
1967 end if;
1969 -- Integer and Enumeration (discrete) type cases
1971 if Is_Discrete_Type (Typ) then
1972 declare
1973 Left_Int : constant Uint := Expr_Value (Left);
1974 Right_Int : constant Uint := Expr_Value (Right);
1976 begin
1977 case Nkind (N) is
1978 when N_Op_Eq => Result := Left_Int = Right_Int;
1979 when N_Op_Ne => Result := Left_Int /= Right_Int;
1980 when N_Op_Lt => Result := Left_Int < Right_Int;
1981 when N_Op_Le => Result := Left_Int <= Right_Int;
1982 when N_Op_Gt => Result := Left_Int > Right_Int;
1983 when N_Op_Ge => Result := Left_Int >= Right_Int;
1985 when others =>
1986 raise Program_Error;
1987 end case;
1989 Fold_Uint (N, Test (Result));
1990 end;
1992 -- Real type case
1994 else
1995 pragma Assert (Is_Real_Type (Typ));
1997 declare
1998 Left_Real : constant Ureal := Expr_Value_R (Left);
1999 Right_Real : constant Ureal := Expr_Value_R (Right);
2001 begin
2002 case Nkind (N) is
2003 when N_Op_Eq => Result := (Left_Real = Right_Real);
2004 when N_Op_Ne => Result := (Left_Real /= Right_Real);
2005 when N_Op_Lt => Result := (Left_Real < Right_Real);
2006 when N_Op_Le => Result := (Left_Real <= Right_Real);
2007 when N_Op_Gt => Result := (Left_Real > Right_Real);
2008 when N_Op_Ge => Result := (Left_Real >= Right_Real);
2010 when others =>
2011 raise Program_Error;
2012 end case;
2014 Fold_Uint (N, Test (Result));
2015 end;
2016 end if;
2018 Set_Is_Static_Expression (N, Stat);
2019 Warn_On_Known_Condition (N);
2020 end Eval_Relational_Op;
2022 ----------------
2023 -- Eval_Shift --
2024 ----------------
2026 -- Shift operations are intrinsic operations that can never be static,
2027 -- so the only processing required is to perform the required check for
2028 -- a non static context for the two operands.
2030 -- Actually we could do some compile time evaluation here some time ???
2032 procedure Eval_Shift (N : Node_Id) is
2033 begin
2034 Check_Non_Static_Context (Left_Opnd (N));
2035 Check_Non_Static_Context (Right_Opnd (N));
2036 end Eval_Shift;
2038 ------------------------
2039 -- Eval_Short_Circuit --
2040 ------------------------
2042 -- A short circuit operation is potentially static if both operands
2043 -- are potentially static (RM 4.9 (13))
2045 procedure Eval_Short_Circuit (N : Node_Id) is
2046 Kind : constant Node_Kind := Nkind (N);
2047 Left : constant Node_Id := Left_Opnd (N);
2048 Right : constant Node_Id := Right_Opnd (N);
2049 Left_Int : Uint;
2050 Rstat : constant Boolean :=
2051 Is_Static_Expression (Left)
2052 and then Is_Static_Expression (Right);
2054 begin
2055 -- Short circuit operations are never static in Ada 83
2057 if Ada_83
2058 and then Comes_From_Source (N)
2059 then
2060 Check_Non_Static_Context (Left);
2061 Check_Non_Static_Context (Right);
2062 return;
2063 end if;
2065 -- Now look at the operands, we can't quite use the normal call to
2066 -- Test_Expression_Is_Foldable here because short circuit operations
2067 -- are a special case, they can still be foldable, even if the right
2068 -- operand raises constraint error.
2070 -- If either operand is Any_Type, just propagate to result and
2071 -- do not try to fold, this prevents cascaded errors.
2073 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
2074 Set_Etype (N, Any_Type);
2075 return;
2077 -- If left operand raises constraint error, then replace node N with
2078 -- the raise constraint error node, and we are obviously not foldable.
2079 -- Is_Static_Expression is set from the two operands in the normal way,
2080 -- and we check the right operand if it is in a non-static context.
2082 elsif Raises_Constraint_Error (Left) then
2083 if not Rstat then
2084 Check_Non_Static_Context (Right);
2085 end if;
2087 Rewrite_In_Raise_CE (N, Left);
2088 Set_Is_Static_Expression (N, Rstat);
2089 return;
2091 -- If the result is not static, then we won't in any case fold
2093 elsif not Rstat then
2094 Check_Non_Static_Context (Left);
2095 Check_Non_Static_Context (Right);
2096 return;
2097 end if;
2099 -- Here the result is static, note that, unlike the normal processing
2100 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
2101 -- the right operand raises constraint error, that's because it is not
2102 -- significant if the left operand is decisive.
2104 Set_Is_Static_Expression (N);
2106 -- It does not matter if the right operand raises constraint error if
2107 -- it will not be evaluated. So deal specially with the cases where
2108 -- the right operand is not evaluated. Note that we will fold these
2109 -- cases even if the right operand is non-static, which is fine, but
2110 -- of course in these cases the result is not potentially static.
2112 Left_Int := Expr_Value (Left);
2114 if (Kind = N_And_Then and then Is_False (Left_Int))
2115 or else (Kind = N_Or_Else and Is_True (Left_Int))
2116 then
2117 Fold_Uint (N, Left_Int);
2118 return;
2119 end if;
2121 -- If first operand not decisive, then it does matter if the right
2122 -- operand raises constraint error, since it will be evaluated, so
2123 -- we simply replace the node with the right operand. Note that this
2124 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
2125 -- (both are set to True in Right).
2127 if Raises_Constraint_Error (Right) then
2128 Rewrite_In_Raise_CE (N, Right);
2129 Check_Non_Static_Context (Left);
2130 return;
2131 end if;
2133 -- Otherwise the result depends on the right operand
2135 Fold_Uint (N, Expr_Value (Right));
2136 return;
2138 end Eval_Short_Circuit;
2140 ----------------
2141 -- Eval_Slice --
2142 ----------------
2144 -- Slices can never be static, so the only processing required is to
2145 -- check for non-static context if an explicit range is given.
2147 procedure Eval_Slice (N : Node_Id) is
2148 Drange : constant Node_Id := Discrete_Range (N);
2150 begin
2151 if Nkind (Drange) = N_Range then
2152 Check_Non_Static_Context (Low_Bound (Drange));
2153 Check_Non_Static_Context (High_Bound (Drange));
2154 end if;
2155 end Eval_Slice;
2157 -------------------------
2158 -- Eval_String_Literal --
2159 -------------------------
2161 procedure Eval_String_Literal (N : Node_Id) is
2162 T : constant Entity_Id := Etype (N);
2163 B : constant Entity_Id := Base_Type (T);
2164 I : Entity_Id;
2166 begin
2167 -- Nothing to do if error type (handles cases like default expressions
2168 -- or generics where we have not yet fully resolved the type)
2170 if B = Any_Type or else B = Any_String then
2171 return;
2173 -- String literals are static if the subtype is static (RM 4.9(2)), so
2174 -- reset the static expression flag (it was set unconditionally in
2175 -- Analyze_String_Literal) if the subtype is non-static. We tell if
2176 -- the subtype is static by looking at the lower bound.
2178 elsif not Is_OK_Static_Expression (String_Literal_Low_Bound (T)) then
2179 Set_Is_Static_Expression (N, False);
2181 elsif Nkind (Original_Node (N)) = N_Type_Conversion then
2182 Set_Is_Static_Expression (N, False);
2184 -- Test for illegal Ada 95 cases. A string literal is illegal in
2185 -- Ada 95 if its bounds are outside the index base type and this
2186 -- index type is static. This can hapen in only two ways. Either
2187 -- the string literal is too long, or it is null, and the lower
2188 -- bound is type'First. In either case it is the upper bound that
2189 -- is out of range of the index type.
2191 elsif Ada_95 then
2192 if Root_Type (B) = Standard_String
2193 or else Root_Type (B) = Standard_Wide_String
2194 then
2195 I := Standard_Positive;
2196 else
2197 I := Etype (First_Index (B));
2198 end if;
2200 if String_Literal_Length (T) > String_Type_Len (B) then
2201 Apply_Compile_Time_Constraint_Error
2202 (N, "string literal too long for}", CE_Length_Check_Failed,
2203 Ent => B,
2204 Typ => First_Subtype (B));
2206 elsif String_Literal_Length (T) = 0
2207 and then not Is_Generic_Type (I)
2208 and then Expr_Value (String_Literal_Low_Bound (T)) =
2209 Expr_Value (Type_Low_Bound (Base_Type (I)))
2210 then
2211 Apply_Compile_Time_Constraint_Error
2212 (N, "null string literal not allowed for}",
2213 CE_Length_Check_Failed,
2214 Ent => B,
2215 Typ => First_Subtype (B));
2216 end if;
2217 end if;
2219 end Eval_String_Literal;
2221 --------------------------
2222 -- Eval_Type_Conversion --
2223 --------------------------
2225 -- A type conversion is potentially static if its subtype mark is for a
2226 -- static scalar subtype, and its operand expression is potentially static
2227 -- (RM 4.9 (10))
2229 procedure Eval_Type_Conversion (N : Node_Id) is
2230 Operand : constant Node_Id := Expression (N);
2231 Source_Type : constant Entity_Id := Etype (Operand);
2232 Target_Type : constant Entity_Id := Etype (N);
2234 Stat : Boolean;
2235 Fold : Boolean;
2237 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
2238 -- Returns true if type T is an integer type, or if it is a
2239 -- fixed-point type to be treated as an integer (i.e. the flag
2240 -- Conversion_OK is set on the conversion node).
2242 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
2243 -- Returns true if type T is a floating-point type, or if it is a
2244 -- fixed-point type that is not to be treated as an integer (i.e. the
2245 -- flag Conversion_OK is not set on the conversion node).
2247 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
2248 begin
2249 return
2250 Is_Integer_Type (T)
2251 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
2252 end To_Be_Treated_As_Integer;
2254 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
2255 begin
2256 return
2257 Is_Floating_Point_Type (T)
2258 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
2259 end To_Be_Treated_As_Real;
2261 -- Start of processing for Eval_Type_Conversion
2263 begin
2264 -- Cannot fold if target type is non-static or if semantic error.
2266 if not Is_Static_Subtype (Target_Type) then
2267 Check_Non_Static_Context (Operand);
2268 return;
2270 elsif Error_Posted (N) then
2271 return;
2272 end if;
2274 -- If not foldable we are done
2276 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
2278 if not Fold then
2279 return;
2281 -- Don't try fold if target type has constraint error bounds
2283 elsif not Is_OK_Static_Subtype (Target_Type) then
2284 Set_Raises_Constraint_Error (N);
2285 return;
2286 end if;
2288 -- Remaining processing depends on operand types. Note that in the
2289 -- following type test, fixed-point counts as real unless the flag
2290 -- Conversion_OK is set, in which case it counts as integer.
2292 -- Fold conversion, case of string type. The result is not static.
2294 if Is_String_Type (Target_Type) then
2295 Fold_Str (N, Strval (Get_String_Val (Operand)));
2296 Set_Is_Static_Expression (N, False);
2298 return;
2300 -- Fold conversion, case of integer target type
2302 elsif To_Be_Treated_As_Integer (Target_Type) then
2303 declare
2304 Result : Uint;
2306 begin
2307 -- Integer to integer conversion
2309 if To_Be_Treated_As_Integer (Source_Type) then
2310 Result := Expr_Value (Operand);
2312 -- Real to integer conversion
2314 else
2315 Result := UR_To_Uint (Expr_Value_R (Operand));
2316 end if;
2318 -- If fixed-point type (Conversion_OK must be set), then the
2319 -- result is logically an integer, but we must replace the
2320 -- conversion with the corresponding real literal, since the
2321 -- type from a semantic point of view is still fixed-point.
2323 if Is_Fixed_Point_Type (Target_Type) then
2324 Fold_Ureal
2325 (N, UR_From_Uint (Result) * Small_Value (Target_Type));
2327 -- Otherwise result is integer literal
2329 else
2330 Fold_Uint (N, Result);
2331 end if;
2332 end;
2334 -- Fold conversion, case of real target type
2336 elsif To_Be_Treated_As_Real (Target_Type) then
2337 declare
2338 Result : Ureal;
2340 begin
2341 if To_Be_Treated_As_Real (Source_Type) then
2342 Result := Expr_Value_R (Operand);
2343 else
2344 Result := UR_From_Uint (Expr_Value (Operand));
2345 end if;
2347 Fold_Ureal (N, Result);
2348 end;
2350 -- Enumeration types
2352 else
2353 Fold_Uint (N, Expr_Value (Operand));
2354 end if;
2356 Set_Is_Static_Expression (N, Stat);
2358 if Is_Out_Of_Range (N, Etype (N)) then
2359 Out_Of_Range (N);
2360 end if;
2362 end Eval_Type_Conversion;
2364 -------------------
2365 -- Eval_Unary_Op --
2366 -------------------
2368 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
2369 -- are potentially static if the operand is potentially static (RM 4.9(7))
2371 procedure Eval_Unary_Op (N : Node_Id) is
2372 Right : constant Node_Id := Right_Opnd (N);
2373 Stat : Boolean;
2374 Fold : Boolean;
2376 begin
2377 -- If not foldable we are done
2379 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
2381 if not Fold then
2382 return;
2383 end if;
2385 -- Fold for integer case
2387 if Is_Integer_Type (Etype (N)) then
2388 declare
2389 Rint : constant Uint := Expr_Value (Right);
2390 Result : Uint;
2392 begin
2393 -- In the case of modular unary plus and abs there is no need
2394 -- to adjust the result of the operation since if the original
2395 -- operand was in bounds the result will be in the bounds of the
2396 -- modular type. However, in the case of modular unary minus the
2397 -- result may go out of the bounds of the modular type and needs
2398 -- adjustment.
2400 if Nkind (N) = N_Op_Plus then
2401 Result := Rint;
2403 elsif Nkind (N) = N_Op_Minus then
2404 if Is_Modular_Integer_Type (Etype (N)) then
2405 Result := (-Rint) mod Modulus (Etype (N));
2406 else
2407 Result := (-Rint);
2408 end if;
2410 else
2411 pragma Assert (Nkind (N) = N_Op_Abs);
2412 Result := abs Rint;
2413 end if;
2415 Fold_Uint (N, Result);
2416 end;
2418 -- Fold for real case
2420 elsif Is_Real_Type (Etype (N)) then
2421 declare
2422 Rreal : constant Ureal := Expr_Value_R (Right);
2423 Result : Ureal;
2425 begin
2426 if Nkind (N) = N_Op_Plus then
2427 Result := Rreal;
2429 elsif Nkind (N) = N_Op_Minus then
2430 Result := UR_Negate (Rreal);
2432 else
2433 pragma Assert (Nkind (N) = N_Op_Abs);
2434 Result := abs Rreal;
2435 end if;
2437 Fold_Ureal (N, Result);
2438 end;
2439 end if;
2441 Set_Is_Static_Expression (N, Stat);
2443 end Eval_Unary_Op;
2445 -------------------------------
2446 -- Eval_Unchecked_Conversion --
2447 -------------------------------
2449 -- Unchecked conversions can never be static, so the only required
2450 -- processing is to check for a non-static context for the operand.
2452 procedure Eval_Unchecked_Conversion (N : Node_Id) is
2453 begin
2454 Check_Non_Static_Context (Expression (N));
2455 end Eval_Unchecked_Conversion;
2457 --------------------
2458 -- Expr_Rep_Value --
2459 --------------------
2461 function Expr_Rep_Value (N : Node_Id) return Uint is
2462 Kind : constant Node_Kind := Nkind (N);
2463 Ent : Entity_Id;
2465 begin
2466 if Is_Entity_Name (N) then
2467 Ent := Entity (N);
2469 -- An enumeration literal that was either in the source or
2470 -- created as a result of static evaluation.
2472 if Ekind (Ent) = E_Enumeration_Literal then
2473 return Enumeration_Rep (Ent);
2475 -- A user defined static constant
2477 else
2478 pragma Assert (Ekind (Ent) = E_Constant);
2479 return Expr_Rep_Value (Constant_Value (Ent));
2480 end if;
2482 -- An integer literal that was either in the source or created
2483 -- as a result of static evaluation.
2485 elsif Kind = N_Integer_Literal then
2486 return Intval (N);
2488 -- A real literal for a fixed-point type. This must be the fixed-point
2489 -- case, either the literal is of a fixed-point type, or it is a bound
2490 -- of a fixed-point type, with type universal real. In either case we
2491 -- obtain the desired value from Corresponding_Integer_Value.
2493 elsif Kind = N_Real_Literal then
2494 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
2495 return Corresponding_Integer_Value (N);
2497 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
2499 elsif Kind = N_Attribute_Reference
2500 and then Attribute_Name (N) = Name_Null_Parameter
2501 then
2502 return Uint_0;
2504 -- Otherwise must be character literal
2506 else
2507 pragma Assert (Kind = N_Character_Literal);
2508 Ent := Entity (N);
2510 -- Since Character literals of type Standard.Character don't
2511 -- have any defining character literals built for them, they
2512 -- do not have their Entity set, so just use their Char
2513 -- code. Otherwise for user-defined character literals use
2514 -- their Pos value as usual which is the same as the Rep value.
2516 if No (Ent) then
2517 return UI_From_Int (Int (Char_Literal_Value (N)));
2518 else
2519 return Enumeration_Rep (Ent);
2520 end if;
2521 end if;
2522 end Expr_Rep_Value;
2524 ----------------
2525 -- Expr_Value --
2526 ----------------
2528 function Expr_Value (N : Node_Id) return Uint is
2529 Kind : constant Node_Kind := Nkind (N);
2530 CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size);
2531 Ent : Entity_Id;
2532 Val : Uint;
2534 begin
2535 -- If already in cache, then we know it's compile time known and
2536 -- we can return the value that was previously stored in the cache
2537 -- since compile time known values cannot change :-)
2539 if CV_Ent.N = N then
2540 return CV_Ent.V;
2541 end if;
2543 -- Otherwise proceed to test value
2545 if Is_Entity_Name (N) then
2546 Ent := Entity (N);
2548 -- An enumeration literal that was either in the source or
2549 -- created as a result of static evaluation.
2551 if Ekind (Ent) = E_Enumeration_Literal then
2552 Val := Enumeration_Pos (Ent);
2554 -- A user defined static constant
2556 else
2557 pragma Assert (Ekind (Ent) = E_Constant);
2558 Val := Expr_Value (Constant_Value (Ent));
2559 end if;
2561 -- An integer literal that was either in the source or created
2562 -- as a result of static evaluation.
2564 elsif Kind = N_Integer_Literal then
2565 Val := Intval (N);
2567 -- A real literal for a fixed-point type. This must be the fixed-point
2568 -- case, either the literal is of a fixed-point type, or it is a bound
2569 -- of a fixed-point type, with type universal real. In either case we
2570 -- obtain the desired value from Corresponding_Integer_Value.
2572 elsif Kind = N_Real_Literal then
2574 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
2575 Val := Corresponding_Integer_Value (N);
2577 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
2579 elsif Kind = N_Attribute_Reference
2580 and then Attribute_Name (N) = Name_Null_Parameter
2581 then
2582 Val := Uint_0;
2584 -- Otherwise must be character literal
2586 else
2587 pragma Assert (Kind = N_Character_Literal);
2588 Ent := Entity (N);
2590 -- Since Character literals of type Standard.Character don't
2591 -- have any defining character literals built for them, they
2592 -- do not have their Entity set, so just use their Char
2593 -- code. Otherwise for user-defined character literals use
2594 -- their Pos value as usual.
2596 if No (Ent) then
2597 Val := UI_From_Int (Int (Char_Literal_Value (N)));
2598 else
2599 Val := Enumeration_Pos (Ent);
2600 end if;
2601 end if;
2603 -- Come here with Val set to value to be returned, set cache
2605 CV_Ent.N := N;
2606 CV_Ent.V := Val;
2607 return Val;
2608 end Expr_Value;
2610 ------------------
2611 -- Expr_Value_E --
2612 ------------------
2614 function Expr_Value_E (N : Node_Id) return Entity_Id is
2615 Ent : constant Entity_Id := Entity (N);
2617 begin
2618 if Ekind (Ent) = E_Enumeration_Literal then
2619 return Ent;
2620 else
2621 pragma Assert (Ekind (Ent) = E_Constant);
2622 return Expr_Value_E (Constant_Value (Ent));
2623 end if;
2624 end Expr_Value_E;
2626 ------------------
2627 -- Expr_Value_R --
2628 ------------------
2630 function Expr_Value_R (N : Node_Id) return Ureal is
2631 Kind : constant Node_Kind := Nkind (N);
2632 Ent : Entity_Id;
2633 Expr : Node_Id;
2635 begin
2636 if Kind = N_Real_Literal then
2637 return Realval (N);
2639 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
2640 Ent := Entity (N);
2641 pragma Assert (Ekind (Ent) = E_Constant);
2642 return Expr_Value_R (Constant_Value (Ent));
2644 elsif Kind = N_Integer_Literal then
2645 return UR_From_Uint (Expr_Value (N));
2647 -- Strange case of VAX literals, which are at this stage transformed
2648 -- into Vax_Type!x_To_y(IEEE_Literal). See Expand_N_Real_Literal in
2649 -- Exp_Vfpt for further details.
2651 elsif Vax_Float (Etype (N))
2652 and then Nkind (N) = N_Unchecked_Type_Conversion
2653 then
2654 Expr := Expression (N);
2656 if Nkind (Expr) = N_Function_Call
2657 and then Present (Parameter_Associations (Expr))
2658 then
2659 Expr := First (Parameter_Associations (Expr));
2661 if Nkind (Expr) = N_Real_Literal then
2662 return Realval (Expr);
2663 end if;
2664 end if;
2666 -- Peculiar VMS case, if we have xxx'Null_Parameter, return 0.0
2668 elsif Kind = N_Attribute_Reference
2669 and then Attribute_Name (N) = Name_Null_Parameter
2670 then
2671 return Ureal_0;
2672 end if;
2674 -- If we fall through, we have a node that cannot be interepreted
2675 -- as a compile time constant. That is definitely an error.
2677 raise Program_Error;
2678 end Expr_Value_R;
2680 ------------------
2681 -- Expr_Value_S --
2682 ------------------
2684 function Expr_Value_S (N : Node_Id) return Node_Id is
2685 begin
2686 if Nkind (N) = N_String_Literal then
2687 return N;
2688 else
2689 pragma Assert (Ekind (Entity (N)) = E_Constant);
2690 return Expr_Value_S (Constant_Value (Entity (N)));
2691 end if;
2692 end Expr_Value_S;
2694 --------------
2695 -- Fold_Str --
2696 --------------
2698 procedure Fold_Str (N : Node_Id; Val : String_Id) is
2699 Loc : constant Source_Ptr := Sloc (N);
2700 Typ : constant Entity_Id := Etype (N);
2702 begin
2703 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
2704 Analyze_And_Resolve (N, Typ);
2705 end Fold_Str;
2707 ---------------
2708 -- Fold_Uint --
2709 ---------------
2711 procedure Fold_Uint (N : Node_Id; Val : Uint) is
2712 Loc : constant Source_Ptr := Sloc (N);
2713 Typ : constant Entity_Id := Etype (N);
2715 begin
2716 -- For a result of type integer, subsitute an N_Integer_Literal node
2717 -- for the result of the compile time evaluation of the expression.
2719 if Is_Integer_Type (Etype (N)) then
2720 Rewrite (N, Make_Integer_Literal (Loc, Val));
2722 -- Otherwise we have an enumeration type, and we substitute either
2723 -- an N_Identifier or N_Character_Literal to represent the enumeration
2724 -- literal corresponding to the given value, which must always be in
2725 -- range, because appropriate tests have already been made for this.
2727 else pragma Assert (Is_Enumeration_Type (Etype (N)));
2728 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
2729 end if;
2731 -- We now have the literal with the right value, both the actual type
2732 -- and the expected type of this literal are taken from the expression
2733 -- that was evaluated.
2735 Analyze (N);
2736 Set_Etype (N, Typ);
2737 Resolve (N, Typ);
2738 end Fold_Uint;
2740 ----------------
2741 -- Fold_Ureal --
2742 ----------------
2744 procedure Fold_Ureal (N : Node_Id; Val : Ureal) is
2745 Loc : constant Source_Ptr := Sloc (N);
2746 Typ : constant Entity_Id := Etype (N);
2748 begin
2749 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
2750 Analyze (N);
2752 -- Both the actual and expected type comes from the original expression
2754 Set_Etype (N, Typ);
2755 Resolve (N, Typ);
2756 end Fold_Ureal;
2758 ---------------
2759 -- From_Bits --
2760 ---------------
2762 function From_Bits (B : Bits; T : Entity_Id) return Uint is
2763 V : Uint := Uint_0;
2765 begin
2766 for J in 0 .. B'Last loop
2767 if B (J) then
2768 V := V + 2 ** J;
2769 end if;
2770 end loop;
2772 if Non_Binary_Modulus (T) then
2773 V := V mod Modulus (T);
2774 end if;
2776 return V;
2777 end From_Bits;
2779 --------------------
2780 -- Get_String_Val --
2781 --------------------
2783 function Get_String_Val (N : Node_Id) return Node_Id is
2784 begin
2785 if Nkind (N) = N_String_Literal then
2786 return N;
2788 elsif Nkind (N) = N_Character_Literal then
2789 return N;
2791 else
2792 pragma Assert (Is_Entity_Name (N));
2793 return Get_String_Val (Constant_Value (Entity (N)));
2794 end if;
2795 end Get_String_Val;
2797 --------------------
2798 -- In_Subrange_Of --
2799 --------------------
2801 function In_Subrange_Of
2802 (T1 : Entity_Id;
2803 T2 : Entity_Id;
2804 Fixed_Int : Boolean := False)
2805 return Boolean
2807 L1 : Node_Id;
2808 H1 : Node_Id;
2810 L2 : Node_Id;
2811 H2 : Node_Id;
2813 begin
2814 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
2815 return True;
2817 -- Never in range if both types are not scalar. Don't know if this can
2818 -- actually happen, but just in case.
2820 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T1) then
2821 return False;
2823 else
2824 L1 := Type_Low_Bound (T1);
2825 H1 := Type_High_Bound (T1);
2827 L2 := Type_Low_Bound (T2);
2828 H2 := Type_High_Bound (T2);
2830 -- Check bounds to see if comparison possible at compile time
2832 if Compile_Time_Compare (L1, L2) in Compare_GE
2833 and then
2834 Compile_Time_Compare (H1, H2) in Compare_LE
2835 then
2836 return True;
2837 end if;
2839 -- If bounds not comparable at compile time, then the bounds of T2
2840 -- must be compile time known or we cannot answer the query.
2842 if not Compile_Time_Known_Value (L2)
2843 or else not Compile_Time_Known_Value (H2)
2844 then
2845 return False;
2846 end if;
2848 -- If the bounds of T1 are know at compile time then use these
2849 -- ones, otherwise use the bounds of the base type (which are of
2850 -- course always static).
2852 if not Compile_Time_Known_Value (L1) then
2853 L1 := Type_Low_Bound (Base_Type (T1));
2854 end if;
2856 if not Compile_Time_Known_Value (H1) then
2857 H1 := Type_High_Bound (Base_Type (T1));
2858 end if;
2860 -- Fixed point types should be considered as such only if
2861 -- flag Fixed_Int is set to False.
2863 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
2864 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
2865 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
2866 then
2867 return
2868 Expr_Value_R (L2) <= Expr_Value_R (L1)
2869 and then
2870 Expr_Value_R (H2) >= Expr_Value_R (H1);
2872 else
2873 return
2874 Expr_Value (L2) <= Expr_Value (L1)
2875 and then
2876 Expr_Value (H2) >= Expr_Value (H1);
2878 end if;
2879 end if;
2881 -- If any exception occurs, it means that we have some bug in the compiler
2882 -- possibly triggered by a previous error, or by some unforseen peculiar
2883 -- occurrence. However, this is only an optimization attempt, so there is
2884 -- really no point in crashing the compiler. Instead we just decide, too
2885 -- bad, we can't figure out the answer in this case after all.
2887 exception
2888 when others =>
2890 -- Debug flag K disables this behavior (useful for debugging)
2892 if Debug_Flag_K then
2893 raise;
2894 else
2895 return False;
2896 end if;
2897 end In_Subrange_Of;
2899 -----------------
2900 -- Is_In_Range --
2901 -----------------
2903 function Is_In_Range
2904 (N : Node_Id;
2905 Typ : Entity_Id;
2906 Fixed_Int : Boolean := False;
2907 Int_Real : Boolean := False)
2908 return Boolean
2910 Val : Uint;
2911 Valr : Ureal;
2913 begin
2914 -- Universal types have no range limits, so always in range.
2916 if Typ = Universal_Integer or else Typ = Universal_Real then
2917 return True;
2919 -- Never in range if not scalar type. Don't know if this can
2920 -- actually happen, but our spec allows it, so we must check!
2922 elsif not Is_Scalar_Type (Typ) then
2923 return False;
2925 -- Never in range unless we have a compile time known value.
2927 elsif not Compile_Time_Known_Value (N) then
2928 return False;
2930 else
2931 declare
2932 Lo : constant Node_Id := Type_Low_Bound (Typ);
2933 Hi : constant Node_Id := Type_High_Bound (Typ);
2934 LB_Known : constant Boolean := Compile_Time_Known_Value (Lo);
2935 UB_Known : constant Boolean := Compile_Time_Known_Value (Hi);
2937 begin
2938 -- Fixed point types should be considered as such only in
2939 -- flag Fixed_Int is set to False.
2941 if Is_Floating_Point_Type (Typ)
2942 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
2943 or else Int_Real
2944 then
2945 Valr := Expr_Value_R (N);
2947 if LB_Known and then Valr >= Expr_Value_R (Lo)
2948 and then UB_Known and then Valr <= Expr_Value_R (Hi)
2949 then
2950 return True;
2951 else
2952 return False;
2953 end if;
2955 else
2956 Val := Expr_Value (N);
2958 if LB_Known and then Val >= Expr_Value (Lo)
2959 and then UB_Known and then Val <= Expr_Value (Hi)
2960 then
2961 return True;
2962 else
2963 return False;
2964 end if;
2965 end if;
2966 end;
2967 end if;
2968 end Is_In_Range;
2970 -------------------
2971 -- Is_Null_Range --
2972 -------------------
2974 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
2975 Typ : constant Entity_Id := Etype (Lo);
2977 begin
2978 if not Compile_Time_Known_Value (Lo)
2979 or else not Compile_Time_Known_Value (Hi)
2980 then
2981 return False;
2982 end if;
2984 if Is_Discrete_Type (Typ) then
2985 return Expr_Value (Lo) > Expr_Value (Hi);
2987 else
2988 pragma Assert (Is_Real_Type (Typ));
2989 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
2990 end if;
2991 end Is_Null_Range;
2993 -----------------------------
2994 -- Is_OK_Static_Expression --
2995 -----------------------------
2997 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
2998 begin
2999 return Is_Static_Expression (N)
3000 and then not Raises_Constraint_Error (N);
3001 end Is_OK_Static_Expression;
3003 ------------------------
3004 -- Is_OK_Static_Range --
3005 ------------------------
3007 -- A static range is a range whose bounds are static expressions, or a
3008 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
3009 -- We have already converted range attribute references, so we get the
3010 -- "or" part of this rule without needing a special test.
3012 function Is_OK_Static_Range (N : Node_Id) return Boolean is
3013 begin
3014 return Is_OK_Static_Expression (Low_Bound (N))
3015 and then Is_OK_Static_Expression (High_Bound (N));
3016 end Is_OK_Static_Range;
3018 --------------------------
3019 -- Is_OK_Static_Subtype --
3020 --------------------------
3022 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
3023 -- where neither bound raises constraint error when evaluated.
3025 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
3026 Base_T : constant Entity_Id := Base_Type (Typ);
3027 Anc_Subt : Entity_Id;
3029 begin
3030 -- First a quick check on the non static subtype flag. As described
3031 -- in further detail in Einfo, this flag is not decisive in all cases,
3032 -- but if it is set, then the subtype is definitely non-static.
3034 if Is_Non_Static_Subtype (Typ) then
3035 return False;
3036 end if;
3038 Anc_Subt := Ancestor_Subtype (Typ);
3040 if Anc_Subt = Empty then
3041 Anc_Subt := Base_T;
3042 end if;
3044 if Is_Generic_Type (Root_Type (Base_T))
3045 or else Is_Generic_Actual_Type (Base_T)
3046 then
3047 return False;
3049 -- String types
3051 elsif Is_String_Type (Typ) then
3052 return
3053 Ekind (Typ) = E_String_Literal_Subtype
3054 or else
3055 (Is_OK_Static_Subtype (Component_Type (Typ))
3056 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
3058 -- Scalar types
3060 elsif Is_Scalar_Type (Typ) then
3061 if Base_T = Typ then
3062 return True;
3064 else
3065 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so
3066 -- use Get_Type_Low,High_Bound.
3068 return Is_OK_Static_Subtype (Anc_Subt)
3069 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
3070 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
3071 end if;
3073 -- Types other than string and scalar types are never static
3075 else
3076 return False;
3077 end if;
3078 end Is_OK_Static_Subtype;
3080 ---------------------
3081 -- Is_Out_Of_Range --
3082 ---------------------
3084 function Is_Out_Of_Range
3085 (N : Node_Id;
3086 Typ : Entity_Id;
3087 Fixed_Int : Boolean := False;
3088 Int_Real : Boolean := False)
3089 return Boolean
3091 Val : Uint;
3092 Valr : Ureal;
3094 begin
3095 -- Universal types have no range limits, so always in range.
3097 if Typ = Universal_Integer or else Typ = Universal_Real then
3098 return False;
3100 -- Never out of range if not scalar type. Don't know if this can
3101 -- actually happen, but our spec allows it, so we must check!
3103 elsif not Is_Scalar_Type (Typ) then
3104 return False;
3106 -- Never out of range if this is a generic type, since the bounds
3107 -- of generic types are junk. Note that if we only checked for
3108 -- static expressions (instead of compile time known values) below,
3109 -- we would not need this check, because values of a generic type
3110 -- can never be static, but they can be known at compile time.
3112 elsif Is_Generic_Type (Typ) then
3113 return False;
3115 -- Never out of range unless we have a compile time known value.
3117 elsif not Compile_Time_Known_Value (N) then
3118 return False;
3120 else
3121 declare
3122 Lo : constant Node_Id := Type_Low_Bound (Typ);
3123 Hi : constant Node_Id := Type_High_Bound (Typ);
3124 LB_Known : constant Boolean := Compile_Time_Known_Value (Lo);
3125 UB_Known : constant Boolean := Compile_Time_Known_Value (Hi);
3127 begin
3128 -- Real types (note that fixed-point types are not treated
3129 -- as being of a real type if the flag Fixed_Int is set,
3130 -- since in that case they are regarded as integer types).
3132 if Is_Floating_Point_Type (Typ)
3133 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
3134 or else Int_Real
3135 then
3136 Valr := Expr_Value_R (N);
3138 if LB_Known and then Valr < Expr_Value_R (Lo) then
3139 return True;
3141 elsif UB_Known and then Expr_Value_R (Hi) < Valr then
3142 return True;
3144 else
3145 return False;
3146 end if;
3148 else
3149 Val := Expr_Value (N);
3151 if LB_Known and then Val < Expr_Value (Lo) then
3152 return True;
3154 elsif UB_Known and then Expr_Value (Hi) < Val then
3155 return True;
3157 else
3158 return False;
3159 end if;
3160 end if;
3161 end;
3162 end if;
3163 end Is_Out_Of_Range;
3165 ---------------------
3166 -- Is_Static_Range --
3167 ---------------------
3169 -- A static range is a range whose bounds are static expressions, or a
3170 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
3171 -- We have already converted range attribute references, so we get the
3172 -- "or" part of this rule without needing a special test.
3174 function Is_Static_Range (N : Node_Id) return Boolean is
3175 begin
3176 return Is_Static_Expression (Low_Bound (N))
3177 and then Is_Static_Expression (High_Bound (N));
3178 end Is_Static_Range;
3180 -----------------------
3181 -- Is_Static_Subtype --
3182 -----------------------
3184 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)).
3186 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
3187 Base_T : constant Entity_Id := Base_Type (Typ);
3188 Anc_Subt : Entity_Id;
3190 begin
3191 -- First a quick check on the non static subtype flag. As described
3192 -- in further detail in Einfo, this flag is not decisive in all cases,
3193 -- but if it is set, then the subtype is definitely non-static.
3195 if Is_Non_Static_Subtype (Typ) then
3196 return False;
3197 end if;
3199 Anc_Subt := Ancestor_Subtype (Typ);
3201 if Anc_Subt = Empty then
3202 Anc_Subt := Base_T;
3203 end if;
3205 if Is_Generic_Type (Root_Type (Base_T))
3206 or else Is_Generic_Actual_Type (Base_T)
3207 then
3208 return False;
3210 -- String types
3212 elsif Is_String_Type (Typ) then
3213 return
3214 Ekind (Typ) = E_String_Literal_Subtype
3215 or else
3216 (Is_Static_Subtype (Component_Type (Typ))
3217 and then Is_Static_Subtype (Etype (First_Index (Typ))));
3219 -- Scalar types
3221 elsif Is_Scalar_Type (Typ) then
3222 if Base_T = Typ then
3223 return True;
3225 else
3226 return Is_Static_Subtype (Anc_Subt)
3227 and then Is_Static_Expression (Type_Low_Bound (Typ))
3228 and then Is_Static_Expression (Type_High_Bound (Typ));
3229 end if;
3231 -- Types other than string and scalar types are never static
3233 else
3234 return False;
3235 end if;
3236 end Is_Static_Subtype;
3238 --------------------
3239 -- Not_Null_Range --
3240 --------------------
3242 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
3243 Typ : constant Entity_Id := Etype (Lo);
3245 begin
3246 if not Compile_Time_Known_Value (Lo)
3247 or else not Compile_Time_Known_Value (Hi)
3248 then
3249 return False;
3250 end if;
3252 if Is_Discrete_Type (Typ) then
3253 return Expr_Value (Lo) <= Expr_Value (Hi);
3255 else
3256 pragma Assert (Is_Real_Type (Typ));
3258 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
3259 end if;
3260 end Not_Null_Range;
3262 -------------
3263 -- OK_Bits --
3264 -------------
3266 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
3267 begin
3268 -- We allow a maximum of 500,000 bits which seems a reasonable limit
3270 if Bits < 500_000 then
3271 return True;
3273 else
3274 Error_Msg_N ("static value too large, capacity exceeded", N);
3275 return False;
3276 end if;
3277 end OK_Bits;
3279 ------------------
3280 -- Out_Of_Range --
3281 ------------------
3283 procedure Out_Of_Range (N : Node_Id) is
3284 begin
3285 -- If we have the static expression case, then this is an illegality
3286 -- in Ada 95 mode, except that in an instance, we never generate an
3287 -- error (if the error is legitimate, it was already diagnosed in
3288 -- the template). The expression to compute the length of a packed
3289 -- array is attached to the array type itself, and deserves a separate
3290 -- message.
3292 if Is_Static_Expression (N)
3293 and then not In_Instance
3294 and then Ada_95
3295 then
3297 if Nkind (Parent (N)) = N_Defining_Identifier
3298 and then Is_Array_Type (Parent (N))
3299 and then Present (Packed_Array_Type (Parent (N)))
3300 and then Present (First_Rep_Item (Parent (N)))
3301 then
3302 Error_Msg_N
3303 ("length of packed array must not exceed Integer''Last",
3304 First_Rep_Item (Parent (N)));
3305 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
3307 else
3308 Apply_Compile_Time_Constraint_Error
3309 (N, "value not in range of}", CE_Range_Check_Failed);
3310 end if;
3312 -- Here we generate a warning for the Ada 83 case, or when we are
3313 -- in an instance, or when we have a non-static expression case.
3315 else
3316 Warn_On_Instance := True;
3317 Apply_Compile_Time_Constraint_Error
3318 (N, "value not in range of}?", CE_Range_Check_Failed);
3319 Warn_On_Instance := False;
3320 end if;
3321 end Out_Of_Range;
3323 -------------------------
3324 -- Rewrite_In_Raise_CE --
3325 -------------------------
3327 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
3328 Typ : constant Entity_Id := Etype (N);
3330 begin
3331 -- If we want to raise CE in the condition of a raise_CE node
3332 -- we may as well get rid of the condition
3334 if Present (Parent (N))
3335 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
3336 then
3337 Set_Condition (Parent (N), Empty);
3339 -- If the expression raising CE is a N_Raise_CE node, we can use
3340 -- that one. We just preserve the type of the context
3342 elsif Nkind (Exp) = N_Raise_Constraint_Error then
3343 Rewrite (N, Exp);
3344 Set_Etype (N, Typ);
3346 -- We have to build an explicit raise_ce node
3348 else
3349 Rewrite (N,
3350 Make_Raise_Constraint_Error (Sloc (Exp),
3351 Reason => CE_Range_Check_Failed));
3352 Set_Raises_Constraint_Error (N);
3353 Set_Etype (N, Typ);
3354 end if;
3355 end Rewrite_In_Raise_CE;
3357 ---------------------
3358 -- String_Type_Len --
3359 ---------------------
3361 function String_Type_Len (Stype : Entity_Id) return Uint is
3362 NT : constant Entity_Id := Etype (First_Index (Stype));
3363 T : Entity_Id;
3365 begin
3366 if Is_OK_Static_Subtype (NT) then
3367 T := NT;
3368 else
3369 T := Base_Type (NT);
3370 end if;
3372 return Expr_Value (Type_High_Bound (T)) -
3373 Expr_Value (Type_Low_Bound (T)) + 1;
3374 end String_Type_Len;
3376 ------------------------------------
3377 -- Subtypes_Statically_Compatible --
3378 ------------------------------------
3380 function Subtypes_Statically_Compatible
3381 (T1 : Entity_Id;
3382 T2 : Entity_Id)
3383 return Boolean
3385 begin
3386 if Is_Scalar_Type (T1) then
3388 -- Definitely compatible if we match
3390 if Subtypes_Statically_Match (T1, T2) then
3391 return True;
3393 -- If either subtype is nonstatic then they're not compatible
3395 elsif not Is_Static_Subtype (T1)
3396 or else not Is_Static_Subtype (T2)
3397 then
3398 return False;
3400 -- If either type has constraint error bounds, then consider that
3401 -- they match to avoid junk cascaded errors here.
3403 elsif not Is_OK_Static_Subtype (T1)
3404 or else not Is_OK_Static_Subtype (T2)
3405 then
3406 return True;
3408 -- Base types must match, but we don't check that (should
3409 -- we???) but we do at least check that both types are
3410 -- real, or both types are not real.
3412 elsif (Is_Real_Type (T1) /= Is_Real_Type (T2)) then
3413 return False;
3415 -- Here we check the bounds
3417 else
3418 declare
3419 LB1 : constant Node_Id := Type_Low_Bound (T1);
3420 HB1 : constant Node_Id := Type_High_Bound (T1);
3421 LB2 : constant Node_Id := Type_Low_Bound (T2);
3422 HB2 : constant Node_Id := Type_High_Bound (T2);
3424 begin
3425 if Is_Real_Type (T1) then
3426 return
3427 (Expr_Value_R (LB1) > Expr_Value_R (HB1))
3428 or else
3429 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
3430 and then
3431 Expr_Value_R (HB1) <= Expr_Value_R (HB2));
3433 else
3434 return
3435 (Expr_Value (LB1) > Expr_Value (HB1))
3436 or else
3437 (Expr_Value (LB2) <= Expr_Value (LB1)
3438 and then
3439 Expr_Value (HB1) <= Expr_Value (HB2));
3440 end if;
3441 end;
3442 end if;
3444 elsif Is_Access_Type (T1) then
3445 return not Is_Constrained (T2)
3446 or else Subtypes_Statically_Match
3447 (Designated_Type (T1), Designated_Type (T2));
3449 else
3450 return (Is_Composite_Type (T1) and then not Is_Constrained (T2))
3451 or else Subtypes_Statically_Match (T1, T2);
3452 end if;
3453 end Subtypes_Statically_Compatible;
3455 -------------------------------
3456 -- Subtypes_Statically_Match --
3457 -------------------------------
3459 -- Subtypes statically match if they have statically matching constraints
3460 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
3461 -- they are the same identical constraint, or if they are static and the
3462 -- values match (RM 4.9.1(1)).
3464 function Subtypes_Statically_Match (T1, T2 : Entity_Id) return Boolean is
3465 begin
3466 -- A type always statically matches itself
3468 if T1 = T2 then
3469 return True;
3471 -- Scalar types
3473 elsif Is_Scalar_Type (T1) then
3475 -- Base types must be the same
3477 if Base_Type (T1) /= Base_Type (T2) then
3478 return False;
3479 end if;
3481 -- A constrained numeric subtype never matches an unconstrained
3482 -- subtype, i.e. both types must be constrained or unconstrained.
3484 -- To understand the requirement for this test, see RM 4.9.1(1).
3485 -- As is made clear in RM 3.5.4(11), type Integer, for example
3486 -- is a constrained subtype with constraint bounds matching the
3487 -- bounds of its corresponding uncontrained base type. In this
3488 -- situation, Integer and Integer'Base do not statically match,
3489 -- even though they have the same bounds.
3491 -- We only apply this test to types in Standard and types that
3492 -- appear in user programs. That way, we do not have to be
3493 -- too careful about setting Is_Constrained right for itypes.
3495 if Is_Numeric_Type (T1)
3496 and then (Is_Constrained (T1) /= Is_Constrained (T2))
3497 and then (Scope (T1) = Standard_Standard
3498 or else Comes_From_Source (T1))
3499 and then (Scope (T2) = Standard_Standard
3500 or else Comes_From_Source (T2))
3501 then
3502 return False;
3503 end if;
3505 -- If there was an error in either range, then just assume
3506 -- the types statically match to avoid further junk errors
3508 if Error_Posted (Scalar_Range (T1))
3509 or else
3510 Error_Posted (Scalar_Range (T2))
3511 then
3512 return True;
3513 end if;
3515 -- Otherwise both types have bound that can be compared
3517 declare
3518 LB1 : constant Node_Id := Type_Low_Bound (T1);
3519 HB1 : constant Node_Id := Type_High_Bound (T1);
3520 LB2 : constant Node_Id := Type_Low_Bound (T2);
3521 HB2 : constant Node_Id := Type_High_Bound (T2);
3523 begin
3524 -- If the bounds are the same tree node, then match
3526 if LB1 = LB2 and then HB1 = HB2 then
3527 return True;
3529 -- Otherwise bounds must be static and identical value
3531 else
3532 if not Is_Static_Subtype (T1)
3533 or else not Is_Static_Subtype (T2)
3534 then
3535 return False;
3537 -- If either type has constraint error bounds, then say
3538 -- that they match to avoid junk cascaded errors here.
3540 elsif not Is_OK_Static_Subtype (T1)
3541 or else not Is_OK_Static_Subtype (T2)
3542 then
3543 return True;
3545 elsif Is_Real_Type (T1) then
3546 return
3547 (Expr_Value_R (LB1) = Expr_Value_R (LB2))
3548 and then
3549 (Expr_Value_R (HB1) = Expr_Value_R (HB2));
3551 else
3552 return
3553 Expr_Value (LB1) = Expr_Value (LB2)
3554 and then
3555 Expr_Value (HB1) = Expr_Value (HB2);
3556 end if;
3557 end if;
3558 end;
3560 -- Type with discriminants
3562 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
3563 if Has_Discriminants (T1) /= Has_Discriminants (T2) then
3564 return False;
3565 end if;
3567 declare
3568 DL1 : constant Elist_Id := Discriminant_Constraint (T1);
3569 DL2 : constant Elist_Id := Discriminant_Constraint (T2);
3571 DA1 : Elmt_Id := First_Elmt (DL1);
3572 DA2 : Elmt_Id := First_Elmt (DL2);
3574 begin
3575 if DL1 = DL2 then
3576 return True;
3578 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
3579 return False;
3580 end if;
3582 while Present (DA1) loop
3583 declare
3584 Expr1 : constant Node_Id := Node (DA1);
3585 Expr2 : constant Node_Id := Node (DA2);
3587 begin
3588 if not Is_Static_Expression (Expr1)
3589 or else not Is_Static_Expression (Expr2)
3590 then
3591 return False;
3593 -- If either expression raised a constraint error,
3594 -- consider the expressions as matching, since this
3595 -- helps to prevent cascading errors.
3597 elsif Raises_Constraint_Error (Expr1)
3598 or else Raises_Constraint_Error (Expr2)
3599 then
3600 null;
3602 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
3603 return False;
3604 end if;
3605 end;
3607 Next_Elmt (DA1);
3608 Next_Elmt (DA2);
3609 end loop;
3610 end;
3612 return True;
3614 -- A definite type does not match an indefinite or classwide type.
3616 elsif
3617 Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
3618 then
3619 return False;
3621 -- Array type
3623 elsif Is_Array_Type (T1) then
3625 -- If either subtype is unconstrained then both must be,
3626 -- and if both are unconstrained then no further checking
3627 -- is needed.
3629 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
3630 return not (Is_Constrained (T1) or else Is_Constrained (T2));
3631 end if;
3633 -- Both subtypes are constrained, so check that the index
3634 -- subtypes statically match.
3636 declare
3637 Index1 : Node_Id := First_Index (T1);
3638 Index2 : Node_Id := First_Index (T2);
3640 begin
3641 while Present (Index1) loop
3642 if not
3643 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
3644 then
3645 return False;
3646 end if;
3648 Next_Index (Index1);
3649 Next_Index (Index2);
3650 end loop;
3652 return True;
3653 end;
3655 elsif Is_Access_Type (T1) then
3656 return Subtypes_Statically_Match
3657 (Designated_Type (T1),
3658 Designated_Type (T2));
3660 -- All other types definitely match
3662 else
3663 return True;
3664 end if;
3665 end Subtypes_Statically_Match;
3667 ----------
3668 -- Test --
3669 ----------
3671 function Test (Cond : Boolean) return Uint is
3672 begin
3673 if Cond then
3674 return Uint_1;
3675 else
3676 return Uint_0;
3677 end if;
3678 end Test;
3680 ---------------------------------
3681 -- Test_Expression_Is_Foldable --
3682 ---------------------------------
3684 -- One operand case
3686 procedure Test_Expression_Is_Foldable
3687 (N : Node_Id;
3688 Op1 : Node_Id;
3689 Stat : out Boolean;
3690 Fold : out Boolean)
3692 begin
3693 Stat := False;
3695 -- If operand is Any_Type, just propagate to result and do not
3696 -- try to fold, this prevents cascaded errors.
3698 if Etype (Op1) = Any_Type then
3699 Set_Etype (N, Any_Type);
3700 Fold := False;
3701 return;
3703 -- If operand raises constraint error, then replace node N with the
3704 -- raise constraint error node, and we are obviously not foldable.
3705 -- Note that this replacement inherits the Is_Static_Expression flag
3706 -- from the operand.
3708 elsif Raises_Constraint_Error (Op1) then
3709 Rewrite_In_Raise_CE (N, Op1);
3710 Fold := False;
3711 return;
3713 -- If the operand is not static, then the result is not static, and
3714 -- all we have to do is to check the operand since it is now known
3715 -- to appear in a non-static context.
3717 elsif not Is_Static_Expression (Op1) then
3718 Check_Non_Static_Context (Op1);
3719 Fold := Compile_Time_Known_Value (Op1);
3720 return;
3722 -- An expression of a formal modular type is not foldable because
3723 -- the modulus is unknown.
3725 elsif Is_Modular_Integer_Type (Etype (Op1))
3726 and then Is_Generic_Type (Etype (Op1))
3727 then
3728 Check_Non_Static_Context (Op1);
3729 Fold := False;
3730 return;
3732 -- Here we have the case of an operand whose type is OK, which is
3733 -- static, and which does not raise constraint error, we can fold.
3735 else
3736 Set_Is_Static_Expression (N);
3737 Fold := True;
3738 Stat := True;
3739 end if;
3740 end Test_Expression_Is_Foldable;
3742 -- Two operand case
3744 procedure Test_Expression_Is_Foldable
3745 (N : Node_Id;
3746 Op1 : Node_Id;
3747 Op2 : Node_Id;
3748 Stat : out Boolean;
3749 Fold : out Boolean)
3751 Rstat : constant Boolean := Is_Static_Expression (Op1)
3752 and then Is_Static_Expression (Op2);
3754 begin
3755 Stat := False;
3757 -- If either operand is Any_Type, just propagate to result and
3758 -- do not try to fold, this prevents cascaded errors.
3760 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
3761 Set_Etype (N, Any_Type);
3762 Fold := False;
3763 return;
3765 -- If left operand raises constraint error, then replace node N with
3766 -- the raise constraint error node, and we are obviously not foldable.
3767 -- Is_Static_Expression is set from the two operands in the normal way,
3768 -- and we check the right operand if it is in a non-static context.
3770 elsif Raises_Constraint_Error (Op1) then
3771 if not Rstat then
3772 Check_Non_Static_Context (Op2);
3773 end if;
3775 Rewrite_In_Raise_CE (N, Op1);
3776 Set_Is_Static_Expression (N, Rstat);
3777 Fold := False;
3778 return;
3780 -- Similar processing for the case of the right operand. Note that
3781 -- we don't use this routine for the short-circuit case, so we do
3782 -- not have to worry about that special case here.
3784 elsif Raises_Constraint_Error (Op2) then
3785 if not Rstat then
3786 Check_Non_Static_Context (Op1);
3787 end if;
3789 Rewrite_In_Raise_CE (N, Op2);
3790 Set_Is_Static_Expression (N, Rstat);
3791 Fold := False;
3792 return;
3794 -- Exclude expressions of a generic modular type, as above.
3796 elsif Is_Modular_Integer_Type (Etype (Op1))
3797 and then Is_Generic_Type (Etype (Op1))
3798 then
3799 Check_Non_Static_Context (Op1);
3800 Fold := False;
3801 return;
3803 -- If result is not static, then check non-static contexts on operands
3804 -- since one of them may be static and the other one may not be static
3806 elsif not Rstat then
3807 Check_Non_Static_Context (Op1);
3808 Check_Non_Static_Context (Op2);
3809 Fold := Compile_Time_Known_Value (Op1)
3810 and then Compile_Time_Known_Value (Op2);
3811 return;
3813 -- Else result is static and foldable. Both operands are static,
3814 -- and neither raises constraint error, so we can definitely fold.
3816 else
3817 Set_Is_Static_Expression (N);
3818 Fold := True;
3819 Stat := True;
3820 return;
3821 end if;
3822 end Test_Expression_Is_Foldable;
3824 --------------
3825 -- To_Bits --
3826 --------------
3828 procedure To_Bits (U : Uint; B : out Bits) is
3829 begin
3830 for J in 0 .. B'Last loop
3831 B (J) := (U / (2 ** J)) mod 2 /= 0;
3832 end loop;
3833 end To_Bits;
3835 end Sem_Eval;