Mark ChangeLog
[official-gcc.git] / gcc / ada / sem_eval.adb
blobd0d536d68b6a96706a3b9561fd17707787de3f80
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-2005 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 : constant 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 -- Ignore cases of non-scalar types or error types
220 if T = Any_Type or else not Is_Scalar_Type (T) then
221 return;
222 end if;
224 -- At this stage we have a scalar type. If we have an expression
225 -- that raises CE, then we already issued a warning or error msg
226 -- so there is nothing more to be done in this routine.
228 if Raises_Constraint_Error (N) then
229 return;
230 end if;
232 -- Now we have a scalar type which is not marked as raising a
233 -- constraint error exception. The main purpose of this routine
234 -- is to deal with static expressions appearing in a non-static
235 -- context. That means that if we do not have a static expression
236 -- then there is not much to do. The one case that we deal with
237 -- here is that if we have a floating-point value that is out of
238 -- range, then we post a warning that an infinity will result.
240 if not Is_Static_Expression (N) then
241 if Is_Floating_Point_Type (T)
242 and then Is_Out_Of_Range (N, Base_Type (T))
243 then
244 Error_Msg_N
245 ("?float value out of range, infinity will be generated", N);
246 end if;
248 return;
249 end if;
251 -- Here we have the case of outer level static expression of
252 -- scalar type, where the processing of this procedure is needed.
254 -- For real types, this is where we convert the value to a machine
255 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should
256 -- only need to do this if the parent is a constant declaration,
257 -- since in other cases, gigi should do the necessary conversion
258 -- correctly, but experimentation shows that this is not the case
259 -- on all machines, in particular if we do not convert all literals
260 -- to machine values in non-static contexts, then ACVC test C490001
261 -- fails on Sparc/Solaris and SGI/Irix.
263 if Nkind (N) = N_Real_Literal
264 and then not Is_Machine_Number (N)
265 and then not Is_Generic_Type (Etype (N))
266 and then Etype (N) /= Universal_Real
267 then
268 -- Check that value is in bounds before converting to machine
269 -- number, so as not to lose case where value overflows in the
270 -- least significant bit or less. See B490001.
272 if Is_Out_Of_Range (N, Base_Type (T)) then
273 Out_Of_Range (N);
274 return;
275 end if;
277 -- Note: we have to copy the node, to avoid problems with conformance
278 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
280 Rewrite (N, New_Copy (N));
282 if not Is_Floating_Point_Type (T) then
283 Set_Realval
284 (N, Corresponding_Integer_Value (N) * Small_Value (T));
286 elsif not UR_Is_Zero (Realval (N)) then
288 -- Note: even though RM 4.9(38) specifies biased rounding,
289 -- this has been modified by AI-100 in order to prevent
290 -- confusing differences in rounding between static and
291 -- non-static expressions. AI-100 specifies that the effect
292 -- of such rounding is implementation dependent, and in GNAT
293 -- we round to nearest even to match the run-time behavior.
295 Set_Realval
296 (N, Machine (Base_Type (T), Realval (N), Round_Even, N));
297 end if;
299 Set_Is_Machine_Number (N);
300 end if;
302 -- Check for out of range universal integer. This is a non-static
303 -- context, so the integer value must be in range of the runtime
304 -- representation of universal integers.
306 -- We do this only within an expression, because that is the only
307 -- case in which non-static universal integer values can occur, and
308 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
309 -- called in contexts like the expression of a number declaration where
310 -- we certainly want to allow out of range values.
312 if Etype (N) = Universal_Integer
313 and then Nkind (N) = N_Integer_Literal
314 and then Nkind (Parent (N)) in N_Subexpr
315 and then
316 (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer))
317 or else
318 Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer)))
319 then
320 Apply_Compile_Time_Constraint_Error
321 (N, "non-static universal integer value out of range?",
322 CE_Range_Check_Failed);
324 -- Check out of range of base type
326 elsif Is_Out_Of_Range (N, Base_Type (T)) then
327 Out_Of_Range (N);
329 -- Give warning if outside subtype (where one or both of the
330 -- bounds of the subtype is static). This warning is omitted
331 -- if the expression appears in a range that could be null
332 -- (warnings are handled elsewhere for this case).
334 elsif T /= Base_Type (T)
335 and then Nkind (Parent (N)) /= N_Range
336 then
337 if Is_In_Range (N, T) then
338 null;
340 elsif Is_Out_Of_Range (N, T) then
341 Apply_Compile_Time_Constraint_Error
342 (N, "value not in range of}?", CE_Range_Check_Failed);
344 elsif Checks_On then
345 Enable_Range_Check (N);
347 else
348 Set_Do_Range_Check (N, False);
349 end if;
350 end if;
351 end Check_Non_Static_Context;
353 ---------------------------------
354 -- Check_String_Literal_Length --
355 ---------------------------------
357 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is
358 begin
359 if not Raises_Constraint_Error (N)
360 and then Is_Constrained (Ttype)
361 then
363 UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype)
364 then
365 Apply_Compile_Time_Constraint_Error
366 (N, "string length wrong for}?",
367 CE_Length_Check_Failed,
368 Ent => Ttype,
369 Typ => Ttype);
370 end if;
371 end if;
372 end Check_String_Literal_Length;
374 --------------------------
375 -- Compile_Time_Compare --
376 --------------------------
378 function Compile_Time_Compare
379 (L, R : Node_Id;
380 Rec : Boolean := False)
381 return Compare_Result
383 Ltyp : constant Entity_Id := Etype (L);
384 Rtyp : constant Entity_Id := Etype (R);
386 procedure Compare_Decompose
387 (N : Node_Id;
388 R : out Node_Id;
389 V : out Uint);
390 -- This procedure decomposes the node N into an expression node
391 -- and a signed offset, so that the value of N is equal to the
392 -- value of R plus the value V (which may be negative). If no
393 -- such decomposition is possible, then on return R is a copy
394 -- of N, and V is set to zero.
396 function Compare_Fixup (N : Node_Id) return Node_Id;
397 -- This function deals with replacing 'Last and 'First references
398 -- with their corresponding type bounds, which we then can compare.
399 -- The argument is the original node, the result is the identity,
400 -- unless we have a 'Last/'First reference in which case the value
401 -- returned is the appropriate type bound.
403 function Is_Same_Value (L, R : Node_Id) return Boolean;
404 -- Returns True iff L and R represent expressions that definitely
405 -- have identical (but not necessarily compile time known) values
406 -- Indeed the caller is expected to have already dealt with the
407 -- cases of compile time known values, so these are not tested here.
409 -----------------------
410 -- Compare_Decompose --
411 -----------------------
413 procedure Compare_Decompose
414 (N : Node_Id;
415 R : out Node_Id;
416 V : out Uint)
418 begin
419 if Nkind (N) = N_Op_Add
420 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
421 then
422 R := Left_Opnd (N);
423 V := Intval (Right_Opnd (N));
424 return;
426 elsif Nkind (N) = N_Op_Subtract
427 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
428 then
429 R := Left_Opnd (N);
430 V := UI_Negate (Intval (Right_Opnd (N)));
431 return;
433 elsif Nkind (N) = N_Attribute_Reference then
435 if Attribute_Name (N) = Name_Succ then
436 R := First (Expressions (N));
437 V := Uint_1;
438 return;
440 elsif Attribute_Name (N) = Name_Pred then
441 R := First (Expressions (N));
442 V := Uint_Minus_1;
443 return;
444 end if;
445 end if;
447 R := N;
448 V := Uint_0;
449 end Compare_Decompose;
451 -------------------
452 -- Compare_Fixup --
453 -------------------
455 function Compare_Fixup (N : Node_Id) return Node_Id is
456 Indx : Node_Id;
457 Xtyp : Entity_Id;
458 Subs : Nat;
460 begin
461 if Nkind (N) = N_Attribute_Reference
462 and then (Attribute_Name (N) = Name_First
463 or else
464 Attribute_Name (N) = Name_Last)
465 then
466 Xtyp := Etype (Prefix (N));
468 -- If we have no type, then just abandon the attempt to do
469 -- a fixup, this is probably the result of some other error.
471 if No (Xtyp) then
472 return N;
473 end if;
475 -- Dereference an access type
477 if Is_Access_Type (Xtyp) then
478 Xtyp := Designated_Type (Xtyp);
479 end if;
481 -- If we don't have an array type at this stage, something
482 -- is peculiar, e.g. another error, and we abandon the attempt
483 -- at a fixup.
485 if not Is_Array_Type (Xtyp) then
486 return N;
487 end if;
489 -- Ignore unconstrained array, since bounds are not meaningful
491 if not Is_Constrained (Xtyp) then
492 return N;
493 end if;
495 if Ekind (Xtyp) = E_String_Literal_Subtype then
496 if Attribute_Name (N) = Name_First then
497 return String_Literal_Low_Bound (Xtyp);
499 else -- Attribute_Name (N) = Name_Last
500 return Make_Integer_Literal (Sloc (N),
501 Intval => Intval (String_Literal_Low_Bound (Xtyp))
502 + String_Literal_Length (Xtyp));
503 end if;
504 end if;
506 -- Find correct index type
508 Indx := First_Index (Xtyp);
510 if Present (Expressions (N)) then
511 Subs := UI_To_Int (Expr_Value (First (Expressions (N))));
513 for J in 2 .. Subs loop
514 Indx := Next_Index (Indx);
515 end loop;
516 end if;
518 Xtyp := Etype (Indx);
520 if Attribute_Name (N) = Name_First then
521 return Type_Low_Bound (Xtyp);
523 else -- Attribute_Name (N) = Name_Last
524 return Type_High_Bound (Xtyp);
525 end if;
526 end if;
528 return N;
529 end Compare_Fixup;
531 -------------------
532 -- Is_Same_Value --
533 -------------------
535 function Is_Same_Value (L, R : Node_Id) return Boolean is
536 Lf : constant Node_Id := Compare_Fixup (L);
537 Rf : constant Node_Id := Compare_Fixup (R);
539 function Is_Same_Subscript (L, R : List_Id) return Boolean;
540 -- L, R are the Expressions values from two attribute nodes
541 -- for First or Last attributes. Either may be set to No_List
542 -- if no expressions are present (indicating subscript 1).
543 -- The result is True if both expressions represent the same
544 -- subscript (note that one case is where one subscript is
545 -- missing and the other is explicitly set to 1).
547 -----------------------
548 -- Is_Same_Subscript --
549 -----------------------
551 function Is_Same_Subscript (L, R : List_Id) return Boolean is
552 begin
553 if L = No_List then
554 if R = No_List then
555 return True;
556 else
557 return Expr_Value (First (R)) = Uint_1;
558 end if;
560 else
561 if R = No_List then
562 return Expr_Value (First (L)) = Uint_1;
563 else
564 return Expr_Value (First (L)) = Expr_Value (First (R));
565 end if;
566 end if;
567 end Is_Same_Subscript;
569 -- Start of processing for Is_Same_Value
571 begin
572 -- Values are the same if they are the same identifier and the
573 -- identifier refers to a constant object (E_Constant). This
574 -- does not however apply to Float types, since we may have two
575 -- NaN values and they should never compare equal.
577 if Nkind (Lf) = N_Identifier and then Nkind (Rf) = N_Identifier
578 and then Entity (Lf) = Entity (Rf)
579 and then not Is_Floating_Point_Type (Etype (L))
580 and then (Ekind (Entity (Lf)) = E_Constant or else
581 Ekind (Entity (Lf)) = E_In_Parameter or else
582 Ekind (Entity (Lf)) = E_Loop_Parameter)
583 then
584 return True;
586 -- Or if they are compile time known and identical
588 elsif Compile_Time_Known_Value (Lf)
589 and then
590 Compile_Time_Known_Value (Rf)
591 and then Expr_Value (Lf) = Expr_Value (Rf)
592 then
593 return True;
595 -- Or if they are both 'First or 'Last values applying to the
596 -- same entity (first and last don't change even if value does)
598 elsif Nkind (Lf) = N_Attribute_Reference
599 and then
600 Nkind (Rf) = N_Attribute_Reference
601 and then Attribute_Name (Lf) = Attribute_Name (Rf)
602 and then (Attribute_Name (Lf) = Name_First
603 or else
604 Attribute_Name (Lf) = Name_Last)
605 and then Is_Entity_Name (Prefix (Lf))
606 and then Is_Entity_Name (Prefix (Rf))
607 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
608 and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf))
609 then
610 return True;
612 -- All other cases, we can't tell
614 else
615 return False;
616 end if;
617 end Is_Same_Value;
619 -- Start of processing for Compile_Time_Compare
621 begin
622 -- If either operand could raise constraint error, then we cannot
623 -- know the result at compile time (since CE may be raised!)
625 if not (Cannot_Raise_Constraint_Error (L)
626 and then
627 Cannot_Raise_Constraint_Error (R))
628 then
629 return Unknown;
630 end if;
632 -- Identical operands are most certainly equal
634 if L = R then
635 return EQ;
637 -- If expressions have no types, then do not attempt to determine
638 -- if they are the same, since something funny is going on. One
639 -- case in which this happens is during generic template analysis,
640 -- when bounds are not fully analyzed.
642 elsif No (Ltyp) or else No (Rtyp) then
643 return Unknown;
645 -- We only attempt compile time analysis for scalar values, and
646 -- not for packed arrays represented as modular types, where the
647 -- semantics of comparison is quite different.
649 elsif not Is_Scalar_Type (Ltyp)
650 or else Is_Packed_Array_Type (Ltyp)
651 then
652 return Unknown;
654 -- Case where comparison involves two compile time known values
656 elsif Compile_Time_Known_Value (L)
657 and then Compile_Time_Known_Value (R)
658 then
659 -- For the floating-point case, we have to be a little careful, since
660 -- at compile time we are dealing with universal exact values, but at
661 -- runtime, these will be in non-exact target form. That's why the
662 -- returned results are LE and GE below instead of LT and GT.
664 if Is_Floating_Point_Type (Ltyp)
665 or else
666 Is_Floating_Point_Type (Rtyp)
667 then
668 declare
669 Lo : constant Ureal := Expr_Value_R (L);
670 Hi : constant Ureal := Expr_Value_R (R);
672 begin
673 if Lo < Hi then
674 return LE;
675 elsif Lo = Hi then
676 return EQ;
677 else
678 return GE;
679 end if;
680 end;
682 -- For the integer case we know exactly (note that this includes the
683 -- fixed-point case, where we know the run time integer values now)
685 else
686 declare
687 Lo : constant Uint := Expr_Value (L);
688 Hi : constant Uint := Expr_Value (R);
690 begin
691 if Lo < Hi then
692 return LT;
693 elsif Lo = Hi then
694 return EQ;
695 else
696 return GT;
697 end if;
698 end;
699 end if;
701 -- Cases where at least one operand is not known at compile time
703 else
704 -- Here is where we check for comparisons against maximum bounds of
705 -- types, where we know that no value can be outside the bounds of
706 -- the subtype. Note that this routine is allowed to assume that all
707 -- expressions are within their subtype bounds. Callers wishing to
708 -- deal with possibly invalid values must in any case take special
709 -- steps (e.g. conversions to larger types) to avoid this kind of
710 -- optimization, which is always considered to be valid. We do not
711 -- attempt this optimization with generic types, since the type
712 -- bounds may not be meaningful in this case.
714 -- We are in danger of an infinite recursion here. It does not seem
715 -- useful to go more than one level deep, so the parameter Rec is
716 -- used to protect ourselves against this infinite recursion.
718 if not Rec
719 and then Is_Discrete_Type (Ltyp)
720 and then Is_Discrete_Type (Rtyp)
721 and then not Is_Generic_Type (Ltyp)
722 and then not Is_Generic_Type (Rtyp)
723 then
724 -- See if we can get a decisive check against one operand and
725 -- a bound of the other operand (four possible tests here).
727 case Compile_Time_Compare (L, Type_Low_Bound (Rtyp), True) is
728 when LT => return LT;
729 when LE => return LE;
730 when EQ => return LE;
731 when others => null;
732 end case;
734 case Compile_Time_Compare (L, Type_High_Bound (Rtyp), True) is
735 when GT => return GT;
736 when GE => return GE;
737 when EQ => return GE;
738 when others => null;
739 end case;
741 case Compile_Time_Compare (Type_Low_Bound (Ltyp), R, True) is
742 when GT => return GT;
743 when GE => return GE;
744 when EQ => return GE;
745 when others => null;
746 end case;
748 case Compile_Time_Compare (Type_High_Bound (Ltyp), R, True) is
749 when LT => return LT;
750 when LE => return LE;
751 when EQ => return LE;
752 when others => null;
753 end case;
754 end if;
756 -- Next attempt is to decompose the expressions to extract
757 -- a constant offset resulting from the use of any of the forms:
759 -- expr + literal
760 -- expr - literal
761 -- typ'Succ (expr)
762 -- typ'Pred (expr)
764 -- Then we see if the two expressions are the same value, and if so
765 -- the result is obtained by comparing the offsets.
767 declare
768 Lnode : Node_Id;
769 Loffs : Uint;
770 Rnode : Node_Id;
771 Roffs : Uint;
773 begin
774 Compare_Decompose (L, Lnode, Loffs);
775 Compare_Decompose (R, Rnode, Roffs);
777 if Is_Same_Value (Lnode, Rnode) then
778 if Loffs = Roffs then
779 return EQ;
781 elsif Loffs < Roffs then
782 return LT;
784 else
785 return GT;
786 end if;
788 -- If the expressions are different, we cannot say at compile
789 -- time how they compare, so we return the Unknown indication.
791 else
792 return Unknown;
793 end if;
794 end;
795 end if;
796 end Compile_Time_Compare;
798 ------------------------------
799 -- Compile_Time_Known_Value --
800 ------------------------------
802 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
803 K : constant Node_Kind := Nkind (Op);
804 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
806 begin
807 -- Never known at compile time if bad type or raises constraint error
808 -- or empty (latter case occurs only as a result of a previous error)
810 if No (Op)
811 or else Op = Error
812 or else Etype (Op) = Any_Type
813 or else Raises_Constraint_Error (Op)
814 then
815 return False;
816 end if;
818 -- If this is not a static expression and we are in configurable run
819 -- time mode, then we consider it not known at compile time. This
820 -- avoids anomalies where whether something is permitted with a given
821 -- configurable run-time library depends on how good the compiler is
822 -- at optimizing and knowing that things are constant when they
823 -- are non-static.
825 if Configurable_Run_Time_Mode and then not Is_Static_Expression (Op) then
826 return False;
827 end if;
829 -- If we have an entity name, then see if it is the name of a constant
830 -- and if so, test the corresponding constant value, or the name of
831 -- an enumeration literal, which is always a constant.
833 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
834 declare
835 E : constant Entity_Id := Entity (Op);
836 V : Node_Id;
838 begin
839 -- Never known at compile time if it is a packed array value.
840 -- We might want to try to evaluate these at compile time one
841 -- day, but we do not make that attempt now.
843 if Is_Packed_Array_Type (Etype (Op)) then
844 return False;
845 end if;
847 if Ekind (E) = E_Enumeration_Literal then
848 return True;
850 elsif Ekind (E) = E_Constant then
851 V := Constant_Value (E);
852 return Present (V) and then Compile_Time_Known_Value (V);
853 end if;
854 end;
856 -- We have a value, see if it is compile time known
858 else
859 -- Integer literals are worth storing in the cache
861 if K = N_Integer_Literal then
862 CV_Ent.N := Op;
863 CV_Ent.V := Intval (Op);
864 return True;
866 -- Other literals and NULL are known at compile time
868 elsif
869 K = N_Character_Literal
870 or else
871 K = N_Real_Literal
872 or else
873 K = N_String_Literal
874 or else
875 K = N_Null
876 then
877 return True;
879 -- Any reference to Null_Parameter is known at compile time. No
880 -- other attribute references (that have not already been folded)
881 -- are known at compile time.
883 elsif K = N_Attribute_Reference then
884 return Attribute_Name (Op) = Name_Null_Parameter;
885 end if;
886 end if;
888 -- If we fall through, not known at compile time
890 return False;
892 -- If we get an exception while trying to do this test, then some error
893 -- has occurred, and we simply say that the value is not known after all
895 exception
896 when others =>
897 return False;
898 end Compile_Time_Known_Value;
900 --------------------------------------
901 -- Compile_Time_Known_Value_Or_Aggr --
902 --------------------------------------
904 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is
905 begin
906 -- If we have an entity name, then see if it is the name of a constant
907 -- and if so, test the corresponding constant value, or the name of
908 -- an enumeration literal, which is always a constant.
910 if Is_Entity_Name (Op) then
911 declare
912 E : constant Entity_Id := Entity (Op);
913 V : Node_Id;
915 begin
916 if Ekind (E) = E_Enumeration_Literal then
917 return True;
919 elsif Ekind (E) /= E_Constant then
920 return False;
922 else
923 V := Constant_Value (E);
924 return Present (V)
925 and then Compile_Time_Known_Value_Or_Aggr (V);
926 end if;
927 end;
929 -- We have a value, see if it is compile time known
931 else
932 if Compile_Time_Known_Value (Op) then
933 return True;
935 elsif Nkind (Op) = N_Aggregate then
937 if Present (Expressions (Op)) then
938 declare
939 Expr : Node_Id;
941 begin
942 Expr := First (Expressions (Op));
943 while Present (Expr) loop
944 if not Compile_Time_Known_Value_Or_Aggr (Expr) then
945 return False;
946 end if;
948 Next (Expr);
949 end loop;
950 end;
951 end if;
953 if Present (Component_Associations (Op)) then
954 declare
955 Cass : Node_Id;
957 begin
958 Cass := First (Component_Associations (Op));
959 while Present (Cass) loop
960 if not
961 Compile_Time_Known_Value_Or_Aggr (Expression (Cass))
962 then
963 return False;
964 end if;
966 Next (Cass);
967 end loop;
968 end;
969 end if;
971 return True;
973 -- All other types of values are not known at compile time
975 else
976 return False;
977 end if;
979 end if;
980 end Compile_Time_Known_Value_Or_Aggr;
982 -----------------
983 -- Eval_Actual --
984 -----------------
986 -- This is only called for actuals of functions that are not predefined
987 -- operators (which have already been rewritten as operators at this
988 -- stage), so the call can never be folded, and all that needs doing for
989 -- the actual is to do the check for a non-static context.
991 procedure Eval_Actual (N : Node_Id) is
992 begin
993 Check_Non_Static_Context (N);
994 end Eval_Actual;
996 --------------------
997 -- Eval_Allocator --
998 --------------------
1000 -- Allocators are never static, so all we have to do is to do the
1001 -- check for a non-static context if an expression is present.
1003 procedure Eval_Allocator (N : Node_Id) is
1004 Expr : constant Node_Id := Expression (N);
1006 begin
1007 if Nkind (Expr) = N_Qualified_Expression then
1008 Check_Non_Static_Context (Expression (Expr));
1009 end if;
1010 end Eval_Allocator;
1012 ------------------------
1013 -- Eval_Arithmetic_Op --
1014 ------------------------
1016 -- Arithmetic operations are static functions, so the result is static
1017 -- if both operands are static (RM 4.9(7), 4.9(20)).
1019 procedure Eval_Arithmetic_Op (N : Node_Id) is
1020 Left : constant Node_Id := Left_Opnd (N);
1021 Right : constant Node_Id := Right_Opnd (N);
1022 Ltype : constant Entity_Id := Etype (Left);
1023 Rtype : constant Entity_Id := Etype (Right);
1024 Stat : Boolean;
1025 Fold : Boolean;
1027 begin
1028 -- If not foldable we are done
1030 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1032 if not Fold then
1033 return;
1034 end if;
1036 -- Fold for cases where both operands are of integer type
1038 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
1039 declare
1040 Left_Int : constant Uint := Expr_Value (Left);
1041 Right_Int : constant Uint := Expr_Value (Right);
1042 Result : Uint;
1044 begin
1045 case Nkind (N) is
1047 when N_Op_Add =>
1048 Result := Left_Int + Right_Int;
1050 when N_Op_Subtract =>
1051 Result := Left_Int - Right_Int;
1053 when N_Op_Multiply =>
1054 if OK_Bits
1055 (N, UI_From_Int
1056 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
1057 then
1058 Result := Left_Int * Right_Int;
1059 else
1060 Result := Left_Int;
1061 end if;
1063 when N_Op_Divide =>
1065 -- The exception Constraint_Error is raised by integer
1066 -- division, rem and mod if the right operand is zero.
1068 if Right_Int = 0 then
1069 Apply_Compile_Time_Constraint_Error
1070 (N, "division by zero",
1071 CE_Divide_By_Zero,
1072 Warn => not Stat);
1073 return;
1075 else
1076 Result := Left_Int / Right_Int;
1077 end if;
1079 when N_Op_Mod =>
1081 -- The exception Constraint_Error is raised by integer
1082 -- division, rem and mod if the right operand is zero.
1084 if Right_Int = 0 then
1085 Apply_Compile_Time_Constraint_Error
1086 (N, "mod with zero divisor",
1087 CE_Divide_By_Zero,
1088 Warn => not Stat);
1089 return;
1090 else
1091 Result := Left_Int mod Right_Int;
1092 end if;
1094 when N_Op_Rem =>
1096 -- The exception Constraint_Error is raised by integer
1097 -- division, rem and mod if the right operand is zero.
1099 if Right_Int = 0 then
1100 Apply_Compile_Time_Constraint_Error
1101 (N, "rem with zero divisor",
1102 CE_Divide_By_Zero,
1103 Warn => not Stat);
1104 return;
1106 else
1107 Result := Left_Int rem Right_Int;
1108 end if;
1110 when others =>
1111 raise Program_Error;
1112 end case;
1114 -- Adjust the result by the modulus if the type is a modular type
1116 if Is_Modular_Integer_Type (Ltype) then
1117 Result := Result mod Modulus (Ltype);
1119 -- For a signed integer type, check non-static overflow
1121 elsif (not Stat) and then Is_Signed_Integer_Type (Ltype) then
1122 declare
1123 BT : constant Entity_Id := Base_Type (Ltype);
1124 Lo : constant Uint := Expr_Value (Type_Low_Bound (BT));
1125 Hi : constant Uint := Expr_Value (Type_High_Bound (BT));
1126 begin
1127 if Result < Lo or else Result > Hi then
1128 Apply_Compile_Time_Constraint_Error
1129 (N, "value not in range of }?",
1130 CE_Overflow_Check_Failed,
1131 Ent => BT);
1132 return;
1133 end if;
1134 end;
1135 end if;
1137 -- If we get here we can fold the result
1139 Fold_Uint (N, Result, Stat);
1140 end;
1142 -- Cases where at least one operand is a real. We handle the cases
1143 -- of both reals, or mixed/real integer cases (the latter happen
1144 -- only for divide and multiply, and the result is always real).
1146 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
1147 declare
1148 Left_Real : Ureal;
1149 Right_Real : Ureal;
1150 Result : Ureal;
1152 begin
1153 if Is_Real_Type (Ltype) then
1154 Left_Real := Expr_Value_R (Left);
1155 else
1156 Left_Real := UR_From_Uint (Expr_Value (Left));
1157 end if;
1159 if Is_Real_Type (Rtype) then
1160 Right_Real := Expr_Value_R (Right);
1161 else
1162 Right_Real := UR_From_Uint (Expr_Value (Right));
1163 end if;
1165 if Nkind (N) = N_Op_Add then
1166 Result := Left_Real + Right_Real;
1168 elsif Nkind (N) = N_Op_Subtract then
1169 Result := Left_Real - Right_Real;
1171 elsif Nkind (N) = N_Op_Multiply then
1172 Result := Left_Real * Right_Real;
1174 else pragma Assert (Nkind (N) = N_Op_Divide);
1175 if UR_Is_Zero (Right_Real) then
1176 Apply_Compile_Time_Constraint_Error
1177 (N, "division by zero", CE_Divide_By_Zero);
1178 return;
1179 end if;
1181 Result := Left_Real / Right_Real;
1182 end if;
1184 Fold_Ureal (N, Result, Stat);
1185 end;
1186 end if;
1187 end Eval_Arithmetic_Op;
1189 ----------------------------
1190 -- Eval_Character_Literal --
1191 ----------------------------
1193 -- Nothing to be done!
1195 procedure Eval_Character_Literal (N : Node_Id) is
1196 pragma Warnings (Off, N);
1197 begin
1198 null;
1199 end Eval_Character_Literal;
1201 ---------------
1202 -- Eval_Call --
1203 ---------------
1205 -- Static function calls are either calls to predefined operators
1206 -- with static arguments, or calls to functions that rename a literal.
1207 -- Only the latter case is handled here, predefined operators are
1208 -- constant-folded elsewhere.
1209 -- If the function is itself inherited (see 7423-001) the literal of
1210 -- the parent type must be explicitly converted to the return type
1211 -- of the function.
1213 procedure Eval_Call (N : Node_Id) is
1214 Loc : constant Source_Ptr := Sloc (N);
1215 Typ : constant Entity_Id := Etype (N);
1216 Lit : Entity_Id;
1218 begin
1219 if Nkind (N) = N_Function_Call
1220 and then No (Parameter_Associations (N))
1221 and then Is_Entity_Name (Name (N))
1222 and then Present (Alias (Entity (Name (N))))
1223 and then Is_Enumeration_Type (Base_Type (Typ))
1224 then
1225 Lit := Alias (Entity (Name (N)));
1227 while Present (Alias (Lit)) loop
1228 Lit := Alias (Lit);
1229 end loop;
1231 if Ekind (Lit) = E_Enumeration_Literal then
1232 if Base_Type (Etype (Lit)) /= Base_Type (Typ) then
1233 Rewrite
1234 (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc)));
1235 else
1236 Rewrite (N, New_Occurrence_Of (Lit, Loc));
1237 end if;
1239 Resolve (N, Typ);
1240 end if;
1241 end if;
1242 end Eval_Call;
1244 ------------------------
1245 -- Eval_Concatenation --
1246 ------------------------
1248 -- Concatenation is a static function, so the result is static if
1249 -- both operands are static (RM 4.9(7), 4.9(21)).
1251 procedure Eval_Concatenation (N : Node_Id) is
1252 Left : constant Node_Id := Left_Opnd (N);
1253 Right : constant Node_Id := Right_Opnd (N);
1254 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
1255 Stat : Boolean;
1256 Fold : Boolean;
1258 begin
1259 -- Concatenation is never static in Ada 83, so if Ada 83
1260 -- check operand non-static context
1262 if Ada_Version = Ada_83
1263 and then Comes_From_Source (N)
1264 then
1265 Check_Non_Static_Context (Left);
1266 Check_Non_Static_Context (Right);
1267 return;
1268 end if;
1270 -- If not foldable we are done. In principle concatenation that yields
1271 -- any string type is static (i.e. an array type of character types).
1272 -- However, character types can include enumeration literals, and
1273 -- concatenation in that case cannot be described by a literal, so we
1274 -- only consider the operation static if the result is an array of
1275 -- (a descendant of) a predefined character type.
1277 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1279 if (C_Typ = Standard_Character
1280 or else C_Typ = Standard_Wide_Character
1281 or else C_Typ = Standard_Wide_Wide_Character)
1282 and then Fold
1283 then
1284 null;
1285 else
1286 Set_Is_Static_Expression (N, False);
1287 return;
1288 end if;
1290 -- Compile time string concatenation
1292 -- ??? Note that operands that are aggregates can be marked as
1293 -- static, so we should attempt at a later stage to fold
1294 -- concatenations with such aggregates.
1296 declare
1297 Left_Str : constant Node_Id := Get_String_Val (Left);
1298 Left_Len : Nat;
1299 Right_Str : constant Node_Id := Get_String_Val (Right);
1301 begin
1302 -- Establish new string literal, and store left operand. We make
1303 -- sure to use the special Start_String that takes an operand if
1304 -- the left operand is a string literal. Since this is optimized
1305 -- in the case where that is the most recently created string
1306 -- literal, we ensure efficient time/space behavior for the
1307 -- case of a concatenation of a series of string literals.
1309 if Nkind (Left_Str) = N_String_Literal then
1310 Left_Len := String_Length (Strval (Left_Str));
1311 Start_String (Strval (Left_Str));
1312 else
1313 Start_String;
1314 Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str)));
1315 Left_Len := 1;
1316 end if;
1318 -- Now append the characters of the right operand
1320 if Nkind (Right_Str) = N_String_Literal then
1321 declare
1322 S : constant String_Id := Strval (Right_Str);
1324 begin
1325 for J in 1 .. String_Length (S) loop
1326 Store_String_Char (Get_String_Char (S, J));
1327 end loop;
1328 end;
1329 else
1330 Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str)));
1331 end if;
1333 Set_Is_Static_Expression (N, Stat);
1335 if Stat then
1337 -- If left operand is the empty string, the result is the
1338 -- right operand, including its bounds if anomalous.
1340 if Left_Len = 0
1341 and then Is_Array_Type (Etype (Right))
1342 and then Etype (Right) /= Any_String
1343 then
1344 Set_Etype (N, Etype (Right));
1345 end if;
1347 Fold_Str (N, End_String, True);
1348 end if;
1349 end;
1350 end Eval_Concatenation;
1352 ---------------------------------
1353 -- Eval_Conditional_Expression --
1354 ---------------------------------
1356 -- This GNAT internal construct can never be statically folded, so the
1357 -- only required processing is to do the check for non-static context
1358 -- for the two expression operands.
1360 procedure Eval_Conditional_Expression (N : Node_Id) is
1361 Condition : constant Node_Id := First (Expressions (N));
1362 Then_Expr : constant Node_Id := Next (Condition);
1363 Else_Expr : constant Node_Id := Next (Then_Expr);
1365 begin
1366 Check_Non_Static_Context (Then_Expr);
1367 Check_Non_Static_Context (Else_Expr);
1368 end Eval_Conditional_Expression;
1370 ----------------------
1371 -- Eval_Entity_Name --
1372 ----------------------
1374 -- This procedure is used for identifiers and expanded names other than
1375 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
1376 -- static if they denote a static constant (RM 4.9(6)) or if the name
1377 -- denotes an enumeration literal (RM 4.9(22)).
1379 procedure Eval_Entity_Name (N : Node_Id) is
1380 Def_Id : constant Entity_Id := Entity (N);
1381 Val : Node_Id;
1383 begin
1384 -- Enumeration literals are always considered to be constants
1385 -- and cannot raise constraint error (RM 4.9(22)).
1387 if Ekind (Def_Id) = E_Enumeration_Literal then
1388 Set_Is_Static_Expression (N);
1389 return;
1391 -- A name is static if it denotes a static constant (RM 4.9(5)), and
1392 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
1393 -- it does not violate 10.2.1(8) here, since this is not a variable.
1395 elsif Ekind (Def_Id) = E_Constant then
1397 -- Deferred constants must always be treated as nonstatic
1398 -- outside the scope of their full view.
1400 if Present (Full_View (Def_Id))
1401 and then not In_Open_Scopes (Scope (Def_Id))
1402 then
1403 Val := Empty;
1404 else
1405 Val := Constant_Value (Def_Id);
1406 end if;
1408 if Present (Val) then
1409 Set_Is_Static_Expression
1410 (N, Is_Static_Expression (Val)
1411 and then Is_Static_Subtype (Etype (Def_Id)));
1412 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
1414 if not Is_Static_Expression (N)
1415 and then not Is_Generic_Type (Etype (N))
1416 then
1417 Validate_Static_Object_Name (N);
1418 end if;
1420 return;
1421 end if;
1422 end if;
1424 -- Fall through if the name is not static
1426 Validate_Static_Object_Name (N);
1427 end Eval_Entity_Name;
1429 ----------------------------
1430 -- Eval_Indexed_Component --
1431 ----------------------------
1433 -- Indexed components are never static, so we need to perform the check
1434 -- for non-static context on the index values. Then, we check if the
1435 -- value can be obtained at compile time, even though it is non-static.
1437 procedure Eval_Indexed_Component (N : Node_Id) is
1438 Expr : Node_Id;
1440 begin
1441 -- Check for non-static context on index values
1443 Expr := First (Expressions (N));
1444 while Present (Expr) loop
1445 Check_Non_Static_Context (Expr);
1446 Next (Expr);
1447 end loop;
1449 -- If the indexed component appears in an object renaming declaration
1450 -- then we do not want to try to evaluate it, since in this case we
1451 -- need the identity of the array element.
1453 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
1454 return;
1456 -- Similarly if the indexed component appears as the prefix of an
1457 -- attribute we don't want to evaluate it, because at least for
1458 -- some cases of attributes we need the identify (e.g. Access, Size)
1460 elsif Nkind (Parent (N)) = N_Attribute_Reference then
1461 return;
1462 end if;
1464 -- Note: there are other cases, such as the left side of an assignment,
1465 -- or an OUT parameter for a call, where the replacement results in the
1466 -- illegal use of a constant, But these cases are illegal in the first
1467 -- place, so the replacement, though silly, is harmless.
1469 -- Now see if this is a constant array reference
1471 if List_Length (Expressions (N)) = 1
1472 and then Is_Entity_Name (Prefix (N))
1473 and then Ekind (Entity (Prefix (N))) = E_Constant
1474 and then Present (Constant_Value (Entity (Prefix (N))))
1475 then
1476 declare
1477 Loc : constant Source_Ptr := Sloc (N);
1478 Arr : constant Node_Id := Constant_Value (Entity (Prefix (N)));
1479 Sub : constant Node_Id := First (Expressions (N));
1481 Atyp : Entity_Id;
1482 -- Type of array
1484 Lin : Nat;
1485 -- Linear one's origin subscript value for array reference
1487 Lbd : Node_Id;
1488 -- Lower bound of the first array index
1490 Elm : Node_Id;
1491 -- Value from constant array
1493 begin
1494 Atyp := Etype (Arr);
1496 if Is_Access_Type (Atyp) then
1497 Atyp := Designated_Type (Atyp);
1498 end if;
1500 -- If we have an array type (we should have but perhaps there
1501 -- are error cases where this is not the case), then see if we
1502 -- can do a constant evaluation of the array reference.
1504 if Is_Array_Type (Atyp) then
1505 if Ekind (Atyp) = E_String_Literal_Subtype then
1506 Lbd := String_Literal_Low_Bound (Atyp);
1507 else
1508 Lbd := Type_Low_Bound (Etype (First_Index (Atyp)));
1509 end if;
1511 if Compile_Time_Known_Value (Sub)
1512 and then Nkind (Arr) = N_Aggregate
1513 and then Compile_Time_Known_Value (Lbd)
1514 and then Is_Discrete_Type (Component_Type (Atyp))
1515 then
1516 Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1;
1518 if List_Length (Expressions (Arr)) >= Lin then
1519 Elm := Pick (Expressions (Arr), Lin);
1521 -- If the resulting expression is compile time known,
1522 -- then we can rewrite the indexed component with this
1523 -- value, being sure to mark the result as non-static.
1524 -- We also reset the Sloc, in case this generates an
1525 -- error later on (e.g. 136'Access).
1527 if Compile_Time_Known_Value (Elm) then
1528 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
1529 Set_Is_Static_Expression (N, False);
1530 Set_Sloc (N, Loc);
1531 end if;
1532 end if;
1533 end if;
1534 end if;
1535 end;
1536 end if;
1537 end Eval_Indexed_Component;
1539 --------------------------
1540 -- Eval_Integer_Literal --
1541 --------------------------
1543 -- Numeric literals are static (RM 4.9(1)), and have already been marked
1544 -- as static by the analyzer. The reason we did it that early is to allow
1545 -- the possibility of turning off the Is_Static_Expression flag after
1546 -- analysis, but before resolution, when integer literals are generated
1547 -- in the expander that do not correspond to static expressions.
1549 procedure Eval_Integer_Literal (N : Node_Id) is
1550 T : constant Entity_Id := Etype (N);
1552 function In_Any_Integer_Context return Boolean;
1553 -- If the literal is resolved with a specific type in a context
1554 -- where the expected type is Any_Integer, there are no range checks
1555 -- on the literal. By the time the literal is evaluated, it carries
1556 -- the type imposed by the enclosing expression, and we must recover
1557 -- the context to determine that Any_Integer is meant.
1559 ----------------------------
1560 -- To_Any_Integer_Context --
1561 ----------------------------
1563 function In_Any_Integer_Context return Boolean is
1564 Par : constant Node_Id := Parent (N);
1565 K : constant Node_Kind := Nkind (Par);
1567 begin
1568 -- Any_Integer also appears in digits specifications for real types,
1569 -- but those have bounds smaller that those of any integer base
1570 -- type, so we can safely ignore these cases.
1572 return K = N_Number_Declaration
1573 or else K = N_Attribute_Reference
1574 or else K = N_Attribute_Definition_Clause
1575 or else K = N_Modular_Type_Definition
1576 or else K = N_Signed_Integer_Type_Definition;
1577 end In_Any_Integer_Context;
1579 -- Start of processing for Eval_Integer_Literal
1581 begin
1583 -- If the literal appears in a non-expression context, then it is
1584 -- certainly appearing in a non-static context, so check it. This
1585 -- is actually a redundant check, since Check_Non_Static_Context
1586 -- would check it, but it seems worth while avoiding the call.
1588 if Nkind (Parent (N)) not in N_Subexpr
1589 and then not In_Any_Integer_Context
1590 then
1591 Check_Non_Static_Context (N);
1592 end if;
1594 -- Modular integer literals must be in their base range
1596 if Is_Modular_Integer_Type (T)
1597 and then Is_Out_Of_Range (N, Base_Type (T))
1598 then
1599 Out_Of_Range (N);
1600 end if;
1601 end Eval_Integer_Literal;
1603 ---------------------
1604 -- Eval_Logical_Op --
1605 ---------------------
1607 -- Logical operations are static functions, so the result is potentially
1608 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
1610 procedure Eval_Logical_Op (N : Node_Id) is
1611 Left : constant Node_Id := Left_Opnd (N);
1612 Right : constant Node_Id := Right_Opnd (N);
1613 Stat : Boolean;
1614 Fold : Boolean;
1616 begin
1617 -- If not foldable we are done
1619 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1621 if not Fold then
1622 return;
1623 end if;
1625 -- Compile time evaluation of logical operation
1627 declare
1628 Left_Int : constant Uint := Expr_Value (Left);
1629 Right_Int : constant Uint := Expr_Value (Right);
1631 begin
1632 if Is_Modular_Integer_Type (Etype (N)) then
1633 declare
1634 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
1635 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
1637 begin
1638 To_Bits (Left_Int, Left_Bits);
1639 To_Bits (Right_Int, Right_Bits);
1641 -- Note: should really be able to use array ops instead of
1642 -- these loops, but they weren't working at the time ???
1644 if Nkind (N) = N_Op_And then
1645 for J in Left_Bits'Range loop
1646 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
1647 end loop;
1649 elsif Nkind (N) = N_Op_Or then
1650 for J in Left_Bits'Range loop
1651 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
1652 end loop;
1654 else
1655 pragma Assert (Nkind (N) = N_Op_Xor);
1657 for J in Left_Bits'Range loop
1658 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
1659 end loop;
1660 end if;
1662 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat);
1663 end;
1665 else
1666 pragma Assert (Is_Boolean_Type (Etype (N)));
1668 if Nkind (N) = N_Op_And then
1669 Fold_Uint (N,
1670 Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat);
1672 elsif Nkind (N) = N_Op_Or then
1673 Fold_Uint (N,
1674 Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat);
1676 else
1677 pragma Assert (Nkind (N) = N_Op_Xor);
1678 Fold_Uint (N,
1679 Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat);
1680 end if;
1681 end if;
1682 end;
1683 end Eval_Logical_Op;
1685 ------------------------
1686 -- Eval_Membership_Op --
1687 ------------------------
1689 -- A membership test is potentially static if the expression is static,
1690 -- and the range is a potentially static range, or is a subtype mark
1691 -- denoting a static subtype (RM 4.9(12)).
1693 procedure Eval_Membership_Op (N : Node_Id) is
1694 Left : constant Node_Id := Left_Opnd (N);
1695 Right : constant Node_Id := Right_Opnd (N);
1696 Def_Id : Entity_Id;
1697 Lo : Node_Id;
1698 Hi : Node_Id;
1699 Result : Boolean;
1700 Stat : Boolean;
1701 Fold : Boolean;
1703 begin
1704 -- Ignore if error in either operand, except to make sure that
1705 -- Any_Type is properly propagated to avoid junk cascaded errors.
1707 if Etype (Left) = Any_Type
1708 or else Etype (Right) = Any_Type
1709 then
1710 Set_Etype (N, Any_Type);
1711 return;
1712 end if;
1714 -- Case of right operand is a subtype name
1716 if Is_Entity_Name (Right) then
1717 Def_Id := Entity (Right);
1719 if (Is_Scalar_Type (Def_Id) or else Is_String_Type (Def_Id))
1720 and then Is_OK_Static_Subtype (Def_Id)
1721 then
1722 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
1724 if not Fold or else not Stat then
1725 return;
1726 end if;
1727 else
1728 Check_Non_Static_Context (Left);
1729 return;
1730 end if;
1732 -- For string membership tests we will check the length
1733 -- further below.
1735 if not Is_String_Type (Def_Id) then
1736 Lo := Type_Low_Bound (Def_Id);
1737 Hi := Type_High_Bound (Def_Id);
1739 else
1740 Lo := Empty;
1741 Hi := Empty;
1742 end if;
1744 -- Case of right operand is a range
1746 else
1747 if Is_Static_Range (Right) then
1748 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
1750 if not Fold or else not Stat then
1751 return;
1753 -- If one bound of range raises CE, then don't try to fold
1755 elsif not Is_OK_Static_Range (Right) then
1756 Check_Non_Static_Context (Left);
1757 return;
1758 end if;
1760 else
1761 Check_Non_Static_Context (Left);
1762 return;
1763 end if;
1765 -- Here we know range is an OK static range
1767 Lo := Low_Bound (Right);
1768 Hi := High_Bound (Right);
1769 end if;
1771 -- For strings we check that the length of the string expression is
1772 -- compatible with the string subtype if the subtype is constrained,
1773 -- or if unconstrained then the test is always true.
1775 if Is_String_Type (Etype (Right)) then
1776 if not Is_Constrained (Etype (Right)) then
1777 Result := True;
1779 else
1780 declare
1781 Typlen : constant Uint := String_Type_Len (Etype (Right));
1782 Strlen : constant Uint :=
1783 UI_From_Int (String_Length (Strval (Get_String_Val (Left))));
1784 begin
1785 Result := (Typlen = Strlen);
1786 end;
1787 end if;
1789 -- Fold the membership test. We know we have a static range and Lo
1790 -- and Hi are set to the expressions for the end points of this range.
1792 elsif Is_Real_Type (Etype (Right)) then
1793 declare
1794 Leftval : constant Ureal := Expr_Value_R (Left);
1796 begin
1797 Result := Expr_Value_R (Lo) <= Leftval
1798 and then Leftval <= Expr_Value_R (Hi);
1799 end;
1801 else
1802 declare
1803 Leftval : constant Uint := Expr_Value (Left);
1805 begin
1806 Result := Expr_Value (Lo) <= Leftval
1807 and then Leftval <= Expr_Value (Hi);
1808 end;
1809 end if;
1811 if Nkind (N) = N_Not_In then
1812 Result := not Result;
1813 end if;
1815 Fold_Uint (N, Test (Result), True);
1816 Warn_On_Known_Condition (N);
1817 end Eval_Membership_Op;
1819 ------------------------
1820 -- Eval_Named_Integer --
1821 ------------------------
1823 procedure Eval_Named_Integer (N : Node_Id) is
1824 begin
1825 Fold_Uint (N,
1826 Expr_Value (Expression (Declaration_Node (Entity (N)))), True);
1827 end Eval_Named_Integer;
1829 ---------------------
1830 -- Eval_Named_Real --
1831 ---------------------
1833 procedure Eval_Named_Real (N : Node_Id) is
1834 begin
1835 Fold_Ureal (N,
1836 Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True);
1837 end Eval_Named_Real;
1839 -------------------
1840 -- Eval_Op_Expon --
1841 -------------------
1843 -- Exponentiation is a static functions, so the result is potentially
1844 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
1846 procedure Eval_Op_Expon (N : Node_Id) is
1847 Left : constant Node_Id := Left_Opnd (N);
1848 Right : constant Node_Id := Right_Opnd (N);
1849 Stat : Boolean;
1850 Fold : Boolean;
1852 begin
1853 -- If not foldable we are done
1855 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1857 if not Fold then
1858 return;
1859 end if;
1861 -- Fold exponentiation operation
1863 declare
1864 Right_Int : constant Uint := Expr_Value (Right);
1866 begin
1867 -- Integer case
1869 if Is_Integer_Type (Etype (Left)) then
1870 declare
1871 Left_Int : constant Uint := Expr_Value (Left);
1872 Result : Uint;
1874 begin
1875 -- Exponentiation of an integer raises the exception
1876 -- Constraint_Error for a negative exponent (RM 4.5.6)
1878 if Right_Int < 0 then
1879 Apply_Compile_Time_Constraint_Error
1880 (N, "integer exponent negative",
1881 CE_Range_Check_Failed,
1882 Warn => not Stat);
1883 return;
1885 else
1886 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
1887 Result := Left_Int ** Right_Int;
1888 else
1889 Result := Left_Int;
1890 end if;
1892 if Is_Modular_Integer_Type (Etype (N)) then
1893 Result := Result mod Modulus (Etype (N));
1894 end if;
1896 Fold_Uint (N, Result, Stat);
1897 end if;
1898 end;
1900 -- Real case
1902 else
1903 declare
1904 Left_Real : constant Ureal := Expr_Value_R (Left);
1906 begin
1907 -- Cannot have a zero base with a negative exponent
1909 if UR_Is_Zero (Left_Real) then
1911 if Right_Int < 0 then
1912 Apply_Compile_Time_Constraint_Error
1913 (N, "zero ** negative integer",
1914 CE_Range_Check_Failed,
1915 Warn => not Stat);
1916 return;
1917 else
1918 Fold_Ureal (N, Ureal_0, Stat);
1919 end if;
1921 else
1922 Fold_Ureal (N, Left_Real ** Right_Int, Stat);
1923 end if;
1924 end;
1925 end if;
1926 end;
1927 end Eval_Op_Expon;
1929 -----------------
1930 -- Eval_Op_Not --
1931 -----------------
1933 -- The not operation is a static functions, so the result is potentially
1934 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
1936 procedure Eval_Op_Not (N : Node_Id) is
1937 Right : constant Node_Id := Right_Opnd (N);
1938 Stat : Boolean;
1939 Fold : Boolean;
1941 begin
1942 -- If not foldable we are done
1944 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
1946 if not Fold then
1947 return;
1948 end if;
1950 -- Fold not operation
1952 declare
1953 Rint : constant Uint := Expr_Value (Right);
1954 Typ : constant Entity_Id := Etype (N);
1956 begin
1957 -- Negation is equivalent to subtracting from the modulus minus
1958 -- one. For a binary modulus this is equivalent to the ones-
1959 -- component of the original value. For non-binary modulus this
1960 -- is an arbitrary but consistent definition.
1962 if Is_Modular_Integer_Type (Typ) then
1963 Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat);
1965 else
1966 pragma Assert (Is_Boolean_Type (Typ));
1967 Fold_Uint (N, Test (not Is_True (Rint)), Stat);
1968 end if;
1970 Set_Is_Static_Expression (N, Stat);
1971 end;
1972 end Eval_Op_Not;
1974 -------------------------------
1975 -- Eval_Qualified_Expression --
1976 -------------------------------
1978 -- A qualified expression is potentially static if its subtype mark denotes
1979 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
1981 procedure Eval_Qualified_Expression (N : Node_Id) is
1982 Operand : constant Node_Id := Expression (N);
1983 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
1985 Stat : Boolean;
1986 Fold : Boolean;
1987 Hex : Boolean;
1989 begin
1990 -- Can only fold if target is string or scalar and subtype is static
1991 -- Also, do not fold if our parent is an allocator (this is because
1992 -- the qualified expression is really part of the syntactic structure
1993 -- of an allocator, and we do not want to end up with something that
1994 -- corresponds to "new 1" where the 1 is the result of folding a
1995 -- qualified expression).
1997 if not Is_Static_Subtype (Target_Type)
1998 or else Nkind (Parent (N)) = N_Allocator
1999 then
2000 Check_Non_Static_Context (Operand);
2002 -- If operand is known to raise constraint_error, set the
2003 -- flag on the expression so it does not get optimized away.
2005 if Nkind (Operand) = N_Raise_Constraint_Error then
2006 Set_Raises_Constraint_Error (N);
2007 end if;
2009 return;
2010 end if;
2012 -- If not foldable we are done
2014 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
2016 if not Fold then
2017 return;
2019 -- Don't try fold if target type has constraint error bounds
2021 elsif not Is_OK_Static_Subtype (Target_Type) then
2022 Set_Raises_Constraint_Error (N);
2023 return;
2024 end if;
2026 -- Here we will fold, save Print_In_Hex indication
2028 Hex := Nkind (Operand) = N_Integer_Literal
2029 and then Print_In_Hex (Operand);
2031 -- Fold the result of qualification
2033 if Is_Discrete_Type (Target_Type) then
2034 Fold_Uint (N, Expr_Value (Operand), Stat);
2036 -- Preserve Print_In_Hex indication
2038 if Hex and then Nkind (N) = N_Integer_Literal then
2039 Set_Print_In_Hex (N);
2040 end if;
2042 elsif Is_Real_Type (Target_Type) then
2043 Fold_Ureal (N, Expr_Value_R (Operand), Stat);
2045 else
2046 Fold_Str (N, Strval (Get_String_Val (Operand)), Stat);
2048 if not Stat then
2049 Set_Is_Static_Expression (N, False);
2050 else
2051 Check_String_Literal_Length (N, Target_Type);
2052 end if;
2054 return;
2055 end if;
2057 -- The expression may be foldable but not static
2059 Set_Is_Static_Expression (N, Stat);
2061 if Is_Out_Of_Range (N, Etype (N)) then
2062 Out_Of_Range (N);
2063 end if;
2064 end Eval_Qualified_Expression;
2066 -----------------------
2067 -- Eval_Real_Literal --
2068 -----------------------
2070 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2071 -- as static by the analyzer. The reason we did it that early is to allow
2072 -- the possibility of turning off the Is_Static_Expression flag after
2073 -- analysis, but before resolution, when integer literals are generated
2074 -- in the expander that do not correspond to static expressions.
2076 procedure Eval_Real_Literal (N : Node_Id) is
2077 begin
2078 -- If the literal appears in a non-expression context, then it is
2079 -- certainly appearing in a non-static context, so check it.
2081 if Nkind (Parent (N)) not in N_Subexpr then
2082 Check_Non_Static_Context (N);
2083 end if;
2085 end Eval_Real_Literal;
2087 ------------------------
2088 -- Eval_Relational_Op --
2089 ------------------------
2091 -- Relational operations are static functions, so the result is static
2092 -- if both operands are static (RM 4.9(7), 4.9(20)).
2094 procedure Eval_Relational_Op (N : Node_Id) is
2095 Left : constant Node_Id := Left_Opnd (N);
2096 Right : constant Node_Id := Right_Opnd (N);
2097 Typ : constant Entity_Id := Etype (Left);
2098 Result : Boolean;
2099 Stat : Boolean;
2100 Fold : Boolean;
2102 begin
2103 -- One special case to deal with first. If we can tell that
2104 -- the result will be false because the lengths of one or
2105 -- more index subtypes are compile time known and different,
2106 -- then we can replace the entire result by False. We only
2107 -- do this for one dimensional arrays, because the case of
2108 -- multi-dimensional arrays is rare and too much trouble!
2110 if Is_Array_Type (Typ)
2111 and then Number_Dimensions (Typ) = 1
2112 and then (Nkind (N) = N_Op_Eq
2113 or else Nkind (N) = N_Op_Ne)
2114 then
2115 if Raises_Constraint_Error (Left)
2116 or else Raises_Constraint_Error (Right)
2117 then
2118 return;
2119 end if;
2121 declare
2122 procedure Get_Static_Length (Op : Node_Id; Len : out Uint);
2123 -- If Op is an expression for a constrained array with a
2124 -- known at compile time length, then Len is set to this
2125 -- (non-negative length). Otherwise Len is set to minus 1.
2127 -----------------------
2128 -- Get_Static_Length --
2129 -----------------------
2131 procedure Get_Static_Length (Op : Node_Id; Len : out Uint) is
2132 T : Entity_Id;
2134 begin
2135 if Nkind (Op) = N_String_Literal then
2136 Len := UI_From_Int (String_Length (Strval (Op)));
2138 elsif not Is_Constrained (Etype (Op)) then
2139 Len := Uint_Minus_1;
2141 else
2142 T := Etype (First_Index (Etype (Op)));
2144 if Is_Discrete_Type (T)
2145 and then
2146 Compile_Time_Known_Value (Type_Low_Bound (T))
2147 and then
2148 Compile_Time_Known_Value (Type_High_Bound (T))
2149 then
2150 Len := UI_Max (Uint_0,
2151 Expr_Value (Type_High_Bound (T)) -
2152 Expr_Value (Type_Low_Bound (T)) + 1);
2153 else
2154 Len := Uint_Minus_1;
2155 end if;
2156 end if;
2157 end Get_Static_Length;
2159 Len_L : Uint;
2160 Len_R : Uint;
2162 begin
2163 Get_Static_Length (Left, Len_L);
2164 Get_Static_Length (Right, Len_R);
2166 if Len_L /= Uint_Minus_1
2167 and then Len_R /= Uint_Minus_1
2168 and then Len_L /= Len_R
2169 then
2170 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
2171 Warn_On_Known_Condition (N);
2172 return;
2173 end if;
2174 end;
2175 end if;
2177 -- Can only fold if type is scalar (don't fold string ops)
2179 if not Is_Scalar_Type (Typ) then
2180 Check_Non_Static_Context (Left);
2181 Check_Non_Static_Context (Right);
2182 return;
2183 end if;
2185 -- If not foldable we are done
2187 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2189 if not Fold then
2190 return;
2191 end if;
2193 -- Integer and Enumeration (discrete) type cases
2195 if Is_Discrete_Type (Typ) then
2196 declare
2197 Left_Int : constant Uint := Expr_Value (Left);
2198 Right_Int : constant Uint := Expr_Value (Right);
2200 begin
2201 case Nkind (N) is
2202 when N_Op_Eq => Result := Left_Int = Right_Int;
2203 when N_Op_Ne => Result := Left_Int /= Right_Int;
2204 when N_Op_Lt => Result := Left_Int < Right_Int;
2205 when N_Op_Le => Result := Left_Int <= Right_Int;
2206 when N_Op_Gt => Result := Left_Int > Right_Int;
2207 when N_Op_Ge => Result := Left_Int >= Right_Int;
2209 when others =>
2210 raise Program_Error;
2211 end case;
2213 Fold_Uint (N, Test (Result), Stat);
2214 end;
2216 -- Real type case
2218 else
2219 pragma Assert (Is_Real_Type (Typ));
2221 declare
2222 Left_Real : constant Ureal := Expr_Value_R (Left);
2223 Right_Real : constant Ureal := Expr_Value_R (Right);
2225 begin
2226 case Nkind (N) is
2227 when N_Op_Eq => Result := (Left_Real = Right_Real);
2228 when N_Op_Ne => Result := (Left_Real /= Right_Real);
2229 when N_Op_Lt => Result := (Left_Real < Right_Real);
2230 when N_Op_Le => Result := (Left_Real <= Right_Real);
2231 when N_Op_Gt => Result := (Left_Real > Right_Real);
2232 when N_Op_Ge => Result := (Left_Real >= Right_Real);
2234 when others =>
2235 raise Program_Error;
2236 end case;
2238 Fold_Uint (N, Test (Result), Stat);
2239 end;
2240 end if;
2242 Warn_On_Known_Condition (N);
2243 end Eval_Relational_Op;
2245 ----------------
2246 -- Eval_Shift --
2247 ----------------
2249 -- Shift operations are intrinsic operations that can never be static,
2250 -- so the only processing required is to perform the required check for
2251 -- a non static context for the two operands.
2253 -- Actually we could do some compile time evaluation here some time ???
2255 procedure Eval_Shift (N : Node_Id) is
2256 begin
2257 Check_Non_Static_Context (Left_Opnd (N));
2258 Check_Non_Static_Context (Right_Opnd (N));
2259 end Eval_Shift;
2261 ------------------------
2262 -- Eval_Short_Circuit --
2263 ------------------------
2265 -- A short circuit operation is potentially static if both operands
2266 -- are potentially static (RM 4.9 (13))
2268 procedure Eval_Short_Circuit (N : Node_Id) is
2269 Kind : constant Node_Kind := Nkind (N);
2270 Left : constant Node_Id := Left_Opnd (N);
2271 Right : constant Node_Id := Right_Opnd (N);
2272 Left_Int : Uint;
2273 Rstat : constant Boolean :=
2274 Is_Static_Expression (Left)
2275 and then Is_Static_Expression (Right);
2277 begin
2278 -- Short circuit operations are never static in Ada 83
2280 if Ada_Version = Ada_83
2281 and then Comes_From_Source (N)
2282 then
2283 Check_Non_Static_Context (Left);
2284 Check_Non_Static_Context (Right);
2285 return;
2286 end if;
2288 -- Now look at the operands, we can't quite use the normal call to
2289 -- Test_Expression_Is_Foldable here because short circuit operations
2290 -- are a special case, they can still be foldable, even if the right
2291 -- operand raises constraint error.
2293 -- If either operand is Any_Type, just propagate to result and
2294 -- do not try to fold, this prevents cascaded errors.
2296 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
2297 Set_Etype (N, Any_Type);
2298 return;
2300 -- If left operand raises constraint error, then replace node N with
2301 -- the raise constraint error node, and we are obviously not foldable.
2302 -- Is_Static_Expression is set from the two operands in the normal way,
2303 -- and we check the right operand if it is in a non-static context.
2305 elsif Raises_Constraint_Error (Left) then
2306 if not Rstat then
2307 Check_Non_Static_Context (Right);
2308 end if;
2310 Rewrite_In_Raise_CE (N, Left);
2311 Set_Is_Static_Expression (N, Rstat);
2312 return;
2314 -- If the result is not static, then we won't in any case fold
2316 elsif not Rstat then
2317 Check_Non_Static_Context (Left);
2318 Check_Non_Static_Context (Right);
2319 return;
2320 end if;
2322 -- Here the result is static, note that, unlike the normal processing
2323 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
2324 -- the right operand raises constraint error, that's because it is not
2325 -- significant if the left operand is decisive.
2327 Set_Is_Static_Expression (N);
2329 -- It does not matter if the right operand raises constraint error if
2330 -- it will not be evaluated. So deal specially with the cases where
2331 -- the right operand is not evaluated. Note that we will fold these
2332 -- cases even if the right operand is non-static, which is fine, but
2333 -- of course in these cases the result is not potentially static.
2335 Left_Int := Expr_Value (Left);
2337 if (Kind = N_And_Then and then Is_False (Left_Int))
2338 or else (Kind = N_Or_Else and Is_True (Left_Int))
2339 then
2340 Fold_Uint (N, Left_Int, Rstat);
2341 return;
2342 end if;
2344 -- If first operand not decisive, then it does matter if the right
2345 -- operand raises constraint error, since it will be evaluated, so
2346 -- we simply replace the node with the right operand. Note that this
2347 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
2348 -- (both are set to True in Right).
2350 if Raises_Constraint_Error (Right) then
2351 Rewrite_In_Raise_CE (N, Right);
2352 Check_Non_Static_Context (Left);
2353 return;
2354 end if;
2356 -- Otherwise the result depends on the right operand
2358 Fold_Uint (N, Expr_Value (Right), Rstat);
2359 return;
2360 end Eval_Short_Circuit;
2362 ----------------
2363 -- Eval_Slice --
2364 ----------------
2366 -- Slices can never be static, so the only processing required is to
2367 -- check for non-static context if an explicit range is given.
2369 procedure Eval_Slice (N : Node_Id) is
2370 Drange : constant Node_Id := Discrete_Range (N);
2372 begin
2373 if Nkind (Drange) = N_Range then
2374 Check_Non_Static_Context (Low_Bound (Drange));
2375 Check_Non_Static_Context (High_Bound (Drange));
2376 end if;
2377 end Eval_Slice;
2379 -------------------------
2380 -- Eval_String_Literal --
2381 -------------------------
2383 procedure Eval_String_Literal (N : Node_Id) is
2384 Typ : constant Entity_Id := Etype (N);
2385 Bas : constant Entity_Id := Base_Type (Typ);
2386 Xtp : Entity_Id;
2387 Len : Nat;
2388 Lo : Node_Id;
2390 begin
2391 -- Nothing to do if error type (handles cases like default expressions
2392 -- or generics where we have not yet fully resolved the type)
2394 if Bas = Any_Type or else Bas = Any_String then
2395 return;
2396 end if;
2398 -- String literals are static if the subtype is static (RM 4.9(2)), so
2399 -- reset the static expression flag (it was set unconditionally in
2400 -- Analyze_String_Literal) if the subtype is non-static. We tell if
2401 -- the subtype is static by looking at the lower bound.
2403 if Ekind (Typ) = E_String_Literal_Subtype then
2404 if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then
2405 Set_Is_Static_Expression (N, False);
2406 return;
2407 end if;
2409 -- Here if Etype of string literal is normal Etype (not yet possible,
2410 -- but may be possible in future!)
2412 elsif not Is_OK_Static_Expression
2413 (Type_Low_Bound (Etype (First_Index (Typ))))
2414 then
2415 Set_Is_Static_Expression (N, False);
2416 return;
2417 end if;
2419 -- If original node was a type conversion, then result if non-static
2421 if Nkind (Original_Node (N)) = N_Type_Conversion then
2422 Set_Is_Static_Expression (N, False);
2423 return;
2424 end if;
2426 -- Test for illegal Ada 95 cases. A string literal is illegal in
2427 -- Ada 95 if its bounds are outside the index base type and this
2428 -- index type is static. This can happen in only two ways. Either
2429 -- the string literal is too long, or it is null, and the lower
2430 -- bound is type'First. In either case it is the upper bound that
2431 -- is out of range of the index type.
2433 if Ada_Version >= Ada_95 then
2434 if Root_Type (Bas) = Standard_String
2435 or else
2436 Root_Type (Bas) = Standard_Wide_String
2437 then
2438 Xtp := Standard_Positive;
2439 else
2440 Xtp := Etype (First_Index (Bas));
2441 end if;
2443 if Ekind (Typ) = E_String_Literal_Subtype then
2444 Lo := String_Literal_Low_Bound (Typ);
2445 else
2446 Lo := Type_Low_Bound (Etype (First_Index (Typ)));
2447 end if;
2449 Len := String_Length (Strval (N));
2451 if UI_From_Int (Len) > String_Type_Len (Bas) then
2452 Apply_Compile_Time_Constraint_Error
2453 (N, "string literal too long for}", CE_Length_Check_Failed,
2454 Ent => Bas,
2455 Typ => First_Subtype (Bas));
2457 elsif Len = 0
2458 and then not Is_Generic_Type (Xtp)
2459 and then
2460 Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp)))
2461 then
2462 Apply_Compile_Time_Constraint_Error
2463 (N, "null string literal not allowed for}",
2464 CE_Length_Check_Failed,
2465 Ent => Bas,
2466 Typ => First_Subtype (Bas));
2467 end if;
2468 end if;
2469 end Eval_String_Literal;
2471 --------------------------
2472 -- Eval_Type_Conversion --
2473 --------------------------
2475 -- A type conversion is potentially static if its subtype mark is for a
2476 -- static scalar subtype, and its operand expression is potentially static
2477 -- (RM 4.9 (10))
2479 procedure Eval_Type_Conversion (N : Node_Id) is
2480 Operand : constant Node_Id := Expression (N);
2481 Source_Type : constant Entity_Id := Etype (Operand);
2482 Target_Type : constant Entity_Id := Etype (N);
2484 Stat : Boolean;
2485 Fold : Boolean;
2487 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
2488 -- Returns true if type T is an integer type, or if it is a
2489 -- fixed-point type to be treated as an integer (i.e. the flag
2490 -- Conversion_OK is set on the conversion node).
2492 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
2493 -- Returns true if type T is a floating-point type, or if it is a
2494 -- fixed-point type that is not to be treated as an integer (i.e. the
2495 -- flag Conversion_OK is not set on the conversion node).
2497 ------------------------------
2498 -- To_Be_Treated_As_Integer --
2499 ------------------------------
2501 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
2502 begin
2503 return
2504 Is_Integer_Type (T)
2505 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
2506 end To_Be_Treated_As_Integer;
2508 ---------------------------
2509 -- To_Be_Treated_As_Real --
2510 ---------------------------
2512 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
2513 begin
2514 return
2515 Is_Floating_Point_Type (T)
2516 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
2517 end To_Be_Treated_As_Real;
2519 -- Start of processing for Eval_Type_Conversion
2521 begin
2522 -- Cannot fold if target type is non-static or if semantic error
2524 if not Is_Static_Subtype (Target_Type) then
2525 Check_Non_Static_Context (Operand);
2526 return;
2528 elsif Error_Posted (N) then
2529 return;
2530 end if;
2532 -- If not foldable we are done
2534 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
2536 if not Fold then
2537 return;
2539 -- Don't try fold if target type has constraint error bounds
2541 elsif not Is_OK_Static_Subtype (Target_Type) then
2542 Set_Raises_Constraint_Error (N);
2543 return;
2544 end if;
2546 -- Remaining processing depends on operand types. Note that in the
2547 -- following type test, fixed-point counts as real unless the flag
2548 -- Conversion_OK is set, in which case it counts as integer.
2550 -- Fold conversion, case of string type. The result is not static
2552 if Is_String_Type (Target_Type) then
2553 Fold_Str (N, Strval (Get_String_Val (Operand)), False);
2555 return;
2557 -- Fold conversion, case of integer target type
2559 elsif To_Be_Treated_As_Integer (Target_Type) then
2560 declare
2561 Result : Uint;
2563 begin
2564 -- Integer to integer conversion
2566 if To_Be_Treated_As_Integer (Source_Type) then
2567 Result := Expr_Value (Operand);
2569 -- Real to integer conversion
2571 else
2572 Result := UR_To_Uint (Expr_Value_R (Operand));
2573 end if;
2575 -- If fixed-point type (Conversion_OK must be set), then the
2576 -- result is logically an integer, but we must replace the
2577 -- conversion with the corresponding real literal, since the
2578 -- type from a semantic point of view is still fixed-point.
2580 if Is_Fixed_Point_Type (Target_Type) then
2581 Fold_Ureal
2582 (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat);
2584 -- Otherwise result is integer literal
2586 else
2587 Fold_Uint (N, Result, Stat);
2588 end if;
2589 end;
2591 -- Fold conversion, case of real target type
2593 elsif To_Be_Treated_As_Real (Target_Type) then
2594 declare
2595 Result : Ureal;
2597 begin
2598 if To_Be_Treated_As_Real (Source_Type) then
2599 Result := Expr_Value_R (Operand);
2600 else
2601 Result := UR_From_Uint (Expr_Value (Operand));
2602 end if;
2604 Fold_Ureal (N, Result, Stat);
2605 end;
2607 -- Enumeration types
2609 else
2610 Fold_Uint (N, Expr_Value (Operand), Stat);
2611 end if;
2613 if Is_Out_Of_Range (N, Etype (N)) then
2614 Out_Of_Range (N);
2615 end if;
2617 end Eval_Type_Conversion;
2619 -------------------
2620 -- Eval_Unary_Op --
2621 -------------------
2623 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
2624 -- are potentially static if the operand is potentially static (RM 4.9(7))
2626 procedure Eval_Unary_Op (N : Node_Id) is
2627 Right : constant Node_Id := Right_Opnd (N);
2628 Stat : Boolean;
2629 Fold : Boolean;
2631 begin
2632 -- If not foldable we are done
2634 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
2636 if not Fold then
2637 return;
2638 end if;
2640 -- Fold for integer case
2642 if Is_Integer_Type (Etype (N)) then
2643 declare
2644 Rint : constant Uint := Expr_Value (Right);
2645 Result : Uint;
2647 begin
2648 -- In the case of modular unary plus and abs there is no need
2649 -- to adjust the result of the operation since if the original
2650 -- operand was in bounds the result will be in the bounds of the
2651 -- modular type. However, in the case of modular unary minus the
2652 -- result may go out of the bounds of the modular type and needs
2653 -- adjustment.
2655 if Nkind (N) = N_Op_Plus then
2656 Result := Rint;
2658 elsif Nkind (N) = N_Op_Minus then
2659 if Is_Modular_Integer_Type (Etype (N)) then
2660 Result := (-Rint) mod Modulus (Etype (N));
2661 else
2662 Result := (-Rint);
2663 end if;
2665 else
2666 pragma Assert (Nkind (N) = N_Op_Abs);
2667 Result := abs Rint;
2668 end if;
2670 Fold_Uint (N, Result, Stat);
2671 end;
2673 -- Fold for real case
2675 elsif Is_Real_Type (Etype (N)) then
2676 declare
2677 Rreal : constant Ureal := Expr_Value_R (Right);
2678 Result : Ureal;
2680 begin
2681 if Nkind (N) = N_Op_Plus then
2682 Result := Rreal;
2684 elsif Nkind (N) = N_Op_Minus then
2685 Result := UR_Negate (Rreal);
2687 else
2688 pragma Assert (Nkind (N) = N_Op_Abs);
2689 Result := abs Rreal;
2690 end if;
2692 Fold_Ureal (N, Result, Stat);
2693 end;
2694 end if;
2695 end Eval_Unary_Op;
2697 -------------------------------
2698 -- Eval_Unchecked_Conversion --
2699 -------------------------------
2701 -- Unchecked conversions can never be static, so the only required
2702 -- processing is to check for a non-static context for the operand.
2704 procedure Eval_Unchecked_Conversion (N : Node_Id) is
2705 begin
2706 Check_Non_Static_Context (Expression (N));
2707 end Eval_Unchecked_Conversion;
2709 --------------------
2710 -- Expr_Rep_Value --
2711 --------------------
2713 function Expr_Rep_Value (N : Node_Id) return Uint is
2714 Kind : constant Node_Kind := Nkind (N);
2715 Ent : Entity_Id;
2717 begin
2718 if Is_Entity_Name (N) then
2719 Ent := Entity (N);
2721 -- An enumeration literal that was either in the source or
2722 -- created as a result of static evaluation.
2724 if Ekind (Ent) = E_Enumeration_Literal then
2725 return Enumeration_Rep (Ent);
2727 -- A user defined static constant
2729 else
2730 pragma Assert (Ekind (Ent) = E_Constant);
2731 return Expr_Rep_Value (Constant_Value (Ent));
2732 end if;
2734 -- An integer literal that was either in the source or created
2735 -- as a result of static evaluation.
2737 elsif Kind = N_Integer_Literal then
2738 return Intval (N);
2740 -- A real literal for a fixed-point type. This must be the fixed-point
2741 -- case, either the literal is of a fixed-point type, or it is a bound
2742 -- of a fixed-point type, with type universal real. In either case we
2743 -- obtain the desired value from Corresponding_Integer_Value.
2745 elsif Kind = N_Real_Literal then
2746 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
2747 return Corresponding_Integer_Value (N);
2749 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
2751 elsif Kind = N_Attribute_Reference
2752 and then Attribute_Name (N) = Name_Null_Parameter
2753 then
2754 return Uint_0;
2756 -- Otherwise must be character literal
2758 else
2759 pragma Assert (Kind = N_Character_Literal);
2760 Ent := Entity (N);
2762 -- Since Character literals of type Standard.Character don't
2763 -- have any defining character literals built for them, they
2764 -- do not have their Entity set, so just use their Char
2765 -- code. Otherwise for user-defined character literals use
2766 -- their Pos value as usual which is the same as the Rep value.
2768 if No (Ent) then
2769 return Char_Literal_Value (N);
2770 else
2771 return Enumeration_Rep (Ent);
2772 end if;
2773 end if;
2774 end Expr_Rep_Value;
2776 ----------------
2777 -- Expr_Value --
2778 ----------------
2780 function Expr_Value (N : Node_Id) return Uint is
2781 Kind : constant Node_Kind := Nkind (N);
2782 CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size);
2783 Ent : Entity_Id;
2784 Val : Uint;
2786 begin
2787 -- If already in cache, then we know it's compile time known and
2788 -- we can return the value that was previously stored in the cache
2789 -- since compile time known values cannot change :-)
2791 if CV_Ent.N = N then
2792 return CV_Ent.V;
2793 end if;
2795 -- Otherwise proceed to test value
2797 if Is_Entity_Name (N) then
2798 Ent := Entity (N);
2800 -- An enumeration literal that was either in the source or
2801 -- created as a result of static evaluation.
2803 if Ekind (Ent) = E_Enumeration_Literal then
2804 Val := Enumeration_Pos (Ent);
2806 -- A user defined static constant
2808 else
2809 pragma Assert (Ekind (Ent) = E_Constant);
2810 Val := Expr_Value (Constant_Value (Ent));
2811 end if;
2813 -- An integer literal that was either in the source or created
2814 -- as a result of static evaluation.
2816 elsif Kind = N_Integer_Literal then
2817 Val := Intval (N);
2819 -- A real literal for a fixed-point type. This must be the fixed-point
2820 -- case, either the literal is of a fixed-point type, or it is a bound
2821 -- of a fixed-point type, with type universal real. In either case we
2822 -- obtain the desired value from Corresponding_Integer_Value.
2824 elsif Kind = N_Real_Literal then
2826 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
2827 Val := Corresponding_Integer_Value (N);
2829 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
2831 elsif Kind = N_Attribute_Reference
2832 and then Attribute_Name (N) = Name_Null_Parameter
2833 then
2834 Val := Uint_0;
2836 -- Otherwise must be character literal
2838 else
2839 pragma Assert (Kind = N_Character_Literal);
2840 Ent := Entity (N);
2842 -- Since Character literals of type Standard.Character don't
2843 -- have any defining character literals built for them, they
2844 -- do not have their Entity set, so just use their Char
2845 -- code. Otherwise for user-defined character literals use
2846 -- their Pos value as usual.
2848 if No (Ent) then
2849 Val := Char_Literal_Value (N);
2850 else
2851 Val := Enumeration_Pos (Ent);
2852 end if;
2853 end if;
2855 -- Come here with Val set to value to be returned, set cache
2857 CV_Ent.N := N;
2858 CV_Ent.V := Val;
2859 return Val;
2860 end Expr_Value;
2862 ------------------
2863 -- Expr_Value_E --
2864 ------------------
2866 function Expr_Value_E (N : Node_Id) return Entity_Id is
2867 Ent : constant Entity_Id := Entity (N);
2869 begin
2870 if Ekind (Ent) = E_Enumeration_Literal then
2871 return Ent;
2872 else
2873 pragma Assert (Ekind (Ent) = E_Constant);
2874 return Expr_Value_E (Constant_Value (Ent));
2875 end if;
2876 end Expr_Value_E;
2878 ------------------
2879 -- Expr_Value_R --
2880 ------------------
2882 function Expr_Value_R (N : Node_Id) return Ureal is
2883 Kind : constant Node_Kind := Nkind (N);
2884 Ent : Entity_Id;
2885 Expr : Node_Id;
2887 begin
2888 if Kind = N_Real_Literal then
2889 return Realval (N);
2891 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
2892 Ent := Entity (N);
2893 pragma Assert (Ekind (Ent) = E_Constant);
2894 return Expr_Value_R (Constant_Value (Ent));
2896 elsif Kind = N_Integer_Literal then
2897 return UR_From_Uint (Expr_Value (N));
2899 -- Strange case of VAX literals, which are at this stage transformed
2900 -- into Vax_Type!x_To_y(IEEE_Literal). See Expand_N_Real_Literal in
2901 -- Exp_Vfpt for further details.
2903 elsif Vax_Float (Etype (N))
2904 and then Nkind (N) = N_Unchecked_Type_Conversion
2905 then
2906 Expr := Expression (N);
2908 if Nkind (Expr) = N_Function_Call
2909 and then Present (Parameter_Associations (Expr))
2910 then
2911 Expr := First (Parameter_Associations (Expr));
2913 if Nkind (Expr) = N_Real_Literal then
2914 return Realval (Expr);
2915 end if;
2916 end if;
2918 -- Peculiar VMS case, if we have xxx'Null_Parameter, return 0.0
2920 elsif Kind = N_Attribute_Reference
2921 and then Attribute_Name (N) = Name_Null_Parameter
2922 then
2923 return Ureal_0;
2924 end if;
2926 -- If we fall through, we have a node that cannot be interepreted
2927 -- as a compile time constant. That is definitely an error.
2929 raise Program_Error;
2930 end Expr_Value_R;
2932 ------------------
2933 -- Expr_Value_S --
2934 ------------------
2936 function Expr_Value_S (N : Node_Id) return Node_Id is
2937 begin
2938 if Nkind (N) = N_String_Literal then
2939 return N;
2940 else
2941 pragma Assert (Ekind (Entity (N)) = E_Constant);
2942 return Expr_Value_S (Constant_Value (Entity (N)));
2943 end if;
2944 end Expr_Value_S;
2946 --------------------------
2947 -- Flag_Non_Static_Expr --
2948 --------------------------
2950 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is
2951 begin
2952 if Error_Posted (Expr) and then not All_Errors_Mode then
2953 return;
2954 else
2955 Error_Msg_F (Msg, Expr);
2956 Why_Not_Static (Expr);
2957 end if;
2958 end Flag_Non_Static_Expr;
2960 --------------
2961 -- Fold_Str --
2962 --------------
2964 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is
2965 Loc : constant Source_Ptr := Sloc (N);
2966 Typ : constant Entity_Id := Etype (N);
2968 begin
2969 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
2971 -- We now have the literal with the right value, both the actual type
2972 -- and the expected type of this literal are taken from the expression
2973 -- that was evaluated.
2975 Analyze (N);
2976 Set_Is_Static_Expression (N, Static);
2977 Set_Etype (N, Typ);
2978 Resolve (N);
2979 end Fold_Str;
2981 ---------------
2982 -- Fold_Uint --
2983 ---------------
2985 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is
2986 Loc : constant Source_Ptr := Sloc (N);
2987 Typ : Entity_Id := Etype (N);
2988 Ent : Entity_Id;
2990 begin
2991 -- If we are folding a named number, retain the entity in the
2992 -- literal, for ASIS use.
2994 if Is_Entity_Name (N)
2995 and then Ekind (Entity (N)) = E_Named_Integer
2996 then
2997 Ent := Entity (N);
2998 else
2999 Ent := Empty;
3000 end if;
3002 if Is_Private_Type (Typ) then
3003 Typ := Full_View (Typ);
3004 end if;
3006 -- For a result of type integer, subsitute an N_Integer_Literal node
3007 -- for the result of the compile time evaluation of the expression.
3009 if Is_Integer_Type (Typ) then
3010 Rewrite (N, Make_Integer_Literal (Loc, Val));
3011 Set_Original_Entity (N, Ent);
3013 -- Otherwise we have an enumeration type, and we substitute either
3014 -- an N_Identifier or N_Character_Literal to represent the enumeration
3015 -- literal corresponding to the given value, which must always be in
3016 -- range, because appropriate tests have already been made for this.
3018 else pragma Assert (Is_Enumeration_Type (Typ));
3019 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
3020 end if;
3022 -- We now have the literal with the right value, both the actual type
3023 -- and the expected type of this literal are taken from the expression
3024 -- that was evaluated.
3026 Analyze (N);
3027 Set_Is_Static_Expression (N, Static);
3028 Set_Etype (N, Typ);
3029 Resolve (N);
3030 end Fold_Uint;
3032 ----------------
3033 -- Fold_Ureal --
3034 ----------------
3036 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is
3037 Loc : constant Source_Ptr := Sloc (N);
3038 Typ : constant Entity_Id := Etype (N);
3039 Ent : Entity_Id;
3041 begin
3042 -- If we are folding a named number, retain the entity in the
3043 -- literal, for ASIS use.
3045 if Is_Entity_Name (N)
3046 and then Ekind (Entity (N)) = E_Named_Real
3047 then
3048 Ent := Entity (N);
3049 else
3050 Ent := Empty;
3051 end if;
3053 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
3054 Set_Original_Entity (N, Ent);
3056 -- Both the actual and expected type comes from the original expression
3058 Analyze (N);
3059 Set_Is_Static_Expression (N, Static);
3060 Set_Etype (N, Typ);
3061 Resolve (N);
3062 end Fold_Ureal;
3064 ---------------
3065 -- From_Bits --
3066 ---------------
3068 function From_Bits (B : Bits; T : Entity_Id) return Uint is
3069 V : Uint := Uint_0;
3071 begin
3072 for J in 0 .. B'Last loop
3073 if B (J) then
3074 V := V + 2 ** J;
3075 end if;
3076 end loop;
3078 if Non_Binary_Modulus (T) then
3079 V := V mod Modulus (T);
3080 end if;
3082 return V;
3083 end From_Bits;
3085 --------------------
3086 -- Get_String_Val --
3087 --------------------
3089 function Get_String_Val (N : Node_Id) return Node_Id is
3090 begin
3091 if Nkind (N) = N_String_Literal then
3092 return N;
3094 elsif Nkind (N) = N_Character_Literal then
3095 return N;
3097 else
3098 pragma Assert (Is_Entity_Name (N));
3099 return Get_String_Val (Constant_Value (Entity (N)));
3100 end if;
3101 end Get_String_Val;
3103 ----------------
3104 -- Initialize --
3105 ----------------
3107 procedure Initialize is
3108 begin
3109 CV_Cache := (others => (Node_High_Bound, Uint_0));
3110 end Initialize;
3112 --------------------
3113 -- In_Subrange_Of --
3114 --------------------
3116 function In_Subrange_Of
3117 (T1 : Entity_Id;
3118 T2 : Entity_Id;
3119 Fixed_Int : Boolean := False)
3120 return Boolean
3122 L1 : Node_Id;
3123 H1 : Node_Id;
3125 L2 : Node_Id;
3126 H2 : Node_Id;
3128 begin
3129 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
3130 return True;
3132 -- Never in range if both types are not scalar. Don't know if this can
3133 -- actually happen, but just in case.
3135 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T1) then
3136 return False;
3138 else
3139 L1 := Type_Low_Bound (T1);
3140 H1 := Type_High_Bound (T1);
3142 L2 := Type_Low_Bound (T2);
3143 H2 := Type_High_Bound (T2);
3145 -- Check bounds to see if comparison possible at compile time
3147 if Compile_Time_Compare (L1, L2) in Compare_GE
3148 and then
3149 Compile_Time_Compare (H1, H2) in Compare_LE
3150 then
3151 return True;
3152 end if;
3154 -- If bounds not comparable at compile time, then the bounds of T2
3155 -- must be compile time known or we cannot answer the query.
3157 if not Compile_Time_Known_Value (L2)
3158 or else not Compile_Time_Known_Value (H2)
3159 then
3160 return False;
3161 end if;
3163 -- If the bounds of T1 are know at compile time then use these
3164 -- ones, otherwise use the bounds of the base type (which are of
3165 -- course always static).
3167 if not Compile_Time_Known_Value (L1) then
3168 L1 := Type_Low_Bound (Base_Type (T1));
3169 end if;
3171 if not Compile_Time_Known_Value (H1) then
3172 H1 := Type_High_Bound (Base_Type (T1));
3173 end if;
3175 -- Fixed point types should be considered as such only if
3176 -- flag Fixed_Int is set to False.
3178 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
3179 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
3180 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
3181 then
3182 return
3183 Expr_Value_R (L2) <= Expr_Value_R (L1)
3184 and then
3185 Expr_Value_R (H2) >= Expr_Value_R (H1);
3187 else
3188 return
3189 Expr_Value (L2) <= Expr_Value (L1)
3190 and then
3191 Expr_Value (H2) >= Expr_Value (H1);
3193 end if;
3194 end if;
3196 -- If any exception occurs, it means that we have some bug in the compiler
3197 -- possibly triggered by a previous error, or by some unforseen peculiar
3198 -- occurrence. However, this is only an optimization attempt, so there is
3199 -- really no point in crashing the compiler. Instead we just decide, too
3200 -- bad, we can't figure out the answer in this case after all.
3202 exception
3203 when others =>
3205 -- Debug flag K disables this behavior (useful for debugging)
3207 if Debug_Flag_K then
3208 raise;
3209 else
3210 return False;
3211 end if;
3212 end In_Subrange_Of;
3214 -----------------
3215 -- Is_In_Range --
3216 -----------------
3218 function Is_In_Range
3219 (N : Node_Id;
3220 Typ : Entity_Id;
3221 Fixed_Int : Boolean := False;
3222 Int_Real : Boolean := False)
3223 return Boolean
3225 Val : Uint;
3226 Valr : Ureal;
3228 begin
3229 -- Universal types have no range limits, so always in range
3231 if Typ = Universal_Integer or else Typ = Universal_Real then
3232 return True;
3234 -- Never in range if not scalar type. Don't know if this can
3235 -- actually happen, but our spec allows it, so we must check!
3237 elsif not Is_Scalar_Type (Typ) then
3238 return False;
3240 -- Never in range unless we have a compile time known value
3242 elsif not Compile_Time_Known_Value (N) then
3243 return False;
3245 else
3246 declare
3247 Lo : constant Node_Id := Type_Low_Bound (Typ);
3248 Hi : constant Node_Id := Type_High_Bound (Typ);
3249 LB_Known : constant Boolean := Compile_Time_Known_Value (Lo);
3250 UB_Known : constant Boolean := Compile_Time_Known_Value (Hi);
3252 begin
3253 -- Fixed point types should be considered as such only in
3254 -- flag Fixed_Int is set to False.
3256 if Is_Floating_Point_Type (Typ)
3257 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
3258 or else Int_Real
3259 then
3260 Valr := Expr_Value_R (N);
3262 if LB_Known and then Valr >= Expr_Value_R (Lo)
3263 and then UB_Known and then Valr <= Expr_Value_R (Hi)
3264 then
3265 return True;
3266 else
3267 return False;
3268 end if;
3270 else
3271 Val := Expr_Value (N);
3273 if LB_Known and then Val >= Expr_Value (Lo)
3274 and then UB_Known and then Val <= Expr_Value (Hi)
3275 then
3276 return True;
3277 else
3278 return False;
3279 end if;
3280 end if;
3281 end;
3282 end if;
3283 end Is_In_Range;
3285 -------------------
3286 -- Is_Null_Range --
3287 -------------------
3289 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
3290 Typ : constant Entity_Id := Etype (Lo);
3292 begin
3293 if not Compile_Time_Known_Value (Lo)
3294 or else not Compile_Time_Known_Value (Hi)
3295 then
3296 return False;
3297 end if;
3299 if Is_Discrete_Type (Typ) then
3300 return Expr_Value (Lo) > Expr_Value (Hi);
3302 else
3303 pragma Assert (Is_Real_Type (Typ));
3304 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
3305 end if;
3306 end Is_Null_Range;
3308 -----------------------------
3309 -- Is_OK_Static_Expression --
3310 -----------------------------
3312 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
3313 begin
3314 return Is_Static_Expression (N)
3315 and then not Raises_Constraint_Error (N);
3316 end Is_OK_Static_Expression;
3318 ------------------------
3319 -- Is_OK_Static_Range --
3320 ------------------------
3322 -- A static range is a range whose bounds are static expressions, or a
3323 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
3324 -- We have already converted range attribute references, so we get the
3325 -- "or" part of this rule without needing a special test.
3327 function Is_OK_Static_Range (N : Node_Id) return Boolean is
3328 begin
3329 return Is_OK_Static_Expression (Low_Bound (N))
3330 and then Is_OK_Static_Expression (High_Bound (N));
3331 end Is_OK_Static_Range;
3333 --------------------------
3334 -- Is_OK_Static_Subtype --
3335 --------------------------
3337 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
3338 -- where neither bound raises constraint error when evaluated.
3340 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
3341 Base_T : constant Entity_Id := Base_Type (Typ);
3342 Anc_Subt : Entity_Id;
3344 begin
3345 -- First a quick check on the non static subtype flag. As described
3346 -- in further detail in Einfo, this flag is not decisive in all cases,
3347 -- but if it is set, then the subtype is definitely non-static.
3349 if Is_Non_Static_Subtype (Typ) then
3350 return False;
3351 end if;
3353 Anc_Subt := Ancestor_Subtype (Typ);
3355 if Anc_Subt = Empty then
3356 Anc_Subt := Base_T;
3357 end if;
3359 if Is_Generic_Type (Root_Type (Base_T))
3360 or else Is_Generic_Actual_Type (Base_T)
3361 then
3362 return False;
3364 -- String types
3366 elsif Is_String_Type (Typ) then
3367 return
3368 Ekind (Typ) = E_String_Literal_Subtype
3369 or else
3370 (Is_OK_Static_Subtype (Component_Type (Typ))
3371 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
3373 -- Scalar types
3375 elsif Is_Scalar_Type (Typ) then
3376 if Base_T = Typ then
3377 return True;
3379 else
3380 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so
3381 -- use Get_Type_Low,High_Bound.
3383 return Is_OK_Static_Subtype (Anc_Subt)
3384 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
3385 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
3386 end if;
3388 -- Types other than string and scalar types are never static
3390 else
3391 return False;
3392 end if;
3393 end Is_OK_Static_Subtype;
3395 ---------------------
3396 -- Is_Out_Of_Range --
3397 ---------------------
3399 function Is_Out_Of_Range
3400 (N : Node_Id;
3401 Typ : Entity_Id;
3402 Fixed_Int : Boolean := False;
3403 Int_Real : Boolean := False)
3404 return Boolean
3406 Val : Uint;
3407 Valr : Ureal;
3409 begin
3410 -- Universal types have no range limits, so always in range
3412 if Typ = Universal_Integer or else Typ = Universal_Real then
3413 return False;
3415 -- Never out of range if not scalar type. Don't know if this can
3416 -- actually happen, but our spec allows it, so we must check!
3418 elsif not Is_Scalar_Type (Typ) then
3419 return False;
3421 -- Never out of range if this is a generic type, since the bounds
3422 -- of generic types are junk. Note that if we only checked for
3423 -- static expressions (instead of compile time known values) below,
3424 -- we would not need this check, because values of a generic type
3425 -- can never be static, but they can be known at compile time.
3427 elsif Is_Generic_Type (Typ) then
3428 return False;
3430 -- Never out of range unless we have a compile time known value
3432 elsif not Compile_Time_Known_Value (N) then
3433 return False;
3435 else
3436 declare
3437 Lo : constant Node_Id := Type_Low_Bound (Typ);
3438 Hi : constant Node_Id := Type_High_Bound (Typ);
3439 LB_Known : constant Boolean := Compile_Time_Known_Value (Lo);
3440 UB_Known : constant Boolean := Compile_Time_Known_Value (Hi);
3442 begin
3443 -- Real types (note that fixed-point types are not treated
3444 -- as being of a real type if the flag Fixed_Int is set,
3445 -- since in that case they are regarded as integer types).
3447 if Is_Floating_Point_Type (Typ)
3448 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
3449 or else Int_Real
3450 then
3451 Valr := Expr_Value_R (N);
3453 if LB_Known and then Valr < Expr_Value_R (Lo) then
3454 return True;
3456 elsif UB_Known and then Expr_Value_R (Hi) < Valr then
3457 return True;
3459 else
3460 return False;
3461 end if;
3463 else
3464 Val := Expr_Value (N);
3466 if LB_Known and then Val < Expr_Value (Lo) then
3467 return True;
3469 elsif UB_Known and then Expr_Value (Hi) < Val then
3470 return True;
3472 else
3473 return False;
3474 end if;
3475 end if;
3476 end;
3477 end if;
3478 end Is_Out_Of_Range;
3480 ---------------------
3481 -- Is_Static_Range --
3482 ---------------------
3484 -- A static range is a range whose bounds are static expressions, or a
3485 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
3486 -- We have already converted range attribute references, so we get the
3487 -- "or" part of this rule without needing a special test.
3489 function Is_Static_Range (N : Node_Id) return Boolean is
3490 begin
3491 return Is_Static_Expression (Low_Bound (N))
3492 and then Is_Static_Expression (High_Bound (N));
3493 end Is_Static_Range;
3495 -----------------------
3496 -- Is_Static_Subtype --
3497 -----------------------
3499 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
3501 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
3502 Base_T : constant Entity_Id := Base_Type (Typ);
3503 Anc_Subt : Entity_Id;
3505 begin
3506 -- First a quick check on the non static subtype flag. As described
3507 -- in further detail in Einfo, this flag is not decisive in all cases,
3508 -- but if it is set, then the subtype is definitely non-static.
3510 if Is_Non_Static_Subtype (Typ) then
3511 return False;
3512 end if;
3514 Anc_Subt := Ancestor_Subtype (Typ);
3516 if Anc_Subt = Empty then
3517 Anc_Subt := Base_T;
3518 end if;
3520 if Is_Generic_Type (Root_Type (Base_T))
3521 or else Is_Generic_Actual_Type (Base_T)
3522 then
3523 return False;
3525 -- String types
3527 elsif Is_String_Type (Typ) then
3528 return
3529 Ekind (Typ) = E_String_Literal_Subtype
3530 or else
3531 (Is_Static_Subtype (Component_Type (Typ))
3532 and then Is_Static_Subtype (Etype (First_Index (Typ))));
3534 -- Scalar types
3536 elsif Is_Scalar_Type (Typ) then
3537 if Base_T = Typ then
3538 return True;
3540 else
3541 return Is_Static_Subtype (Anc_Subt)
3542 and then Is_Static_Expression (Type_Low_Bound (Typ))
3543 and then Is_Static_Expression (Type_High_Bound (Typ));
3544 end if;
3546 -- Types other than string and scalar types are never static
3548 else
3549 return False;
3550 end if;
3551 end Is_Static_Subtype;
3553 --------------------
3554 -- Not_Null_Range --
3555 --------------------
3557 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
3558 Typ : constant Entity_Id := Etype (Lo);
3560 begin
3561 if not Compile_Time_Known_Value (Lo)
3562 or else not Compile_Time_Known_Value (Hi)
3563 then
3564 return False;
3565 end if;
3567 if Is_Discrete_Type (Typ) then
3568 return Expr_Value (Lo) <= Expr_Value (Hi);
3570 else
3571 pragma Assert (Is_Real_Type (Typ));
3573 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
3574 end if;
3575 end Not_Null_Range;
3577 -------------
3578 -- OK_Bits --
3579 -------------
3581 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
3582 begin
3583 -- We allow a maximum of 500,000 bits which seems a reasonable limit
3585 if Bits < 500_000 then
3586 return True;
3588 else
3589 Error_Msg_N ("static value too large, capacity exceeded", N);
3590 return False;
3591 end if;
3592 end OK_Bits;
3594 ------------------
3595 -- Out_Of_Range --
3596 ------------------
3598 procedure Out_Of_Range (N : Node_Id) is
3599 begin
3600 -- If we have the static expression case, then this is an illegality
3601 -- in Ada 95 mode, except that in an instance, we never generate an
3602 -- error (if the error is legitimate, it was already diagnosed in
3603 -- the template). The expression to compute the length of a packed
3604 -- array is attached to the array type itself, and deserves a separate
3605 -- message.
3607 if Is_Static_Expression (N)
3608 and then not In_Instance
3609 and then not In_Inlined_Body
3610 and then Ada_Version >= Ada_95
3611 then
3612 if Nkind (Parent (N)) = N_Defining_Identifier
3613 and then Is_Array_Type (Parent (N))
3614 and then Present (Packed_Array_Type (Parent (N)))
3615 and then Present (First_Rep_Item (Parent (N)))
3616 then
3617 Error_Msg_N
3618 ("length of packed array must not exceed Integer''Last",
3619 First_Rep_Item (Parent (N)));
3620 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
3622 else
3623 Apply_Compile_Time_Constraint_Error
3624 (N, "value not in range of}", CE_Range_Check_Failed);
3625 end if;
3627 -- Here we generate a warning for the Ada 83 case, or when we are
3628 -- in an instance, or when we have a non-static expression case.
3630 else
3631 Apply_Compile_Time_Constraint_Error
3632 (N, "value not in range of}?", CE_Range_Check_Failed);
3633 end if;
3634 end Out_Of_Range;
3636 -------------------------
3637 -- Rewrite_In_Raise_CE --
3638 -------------------------
3640 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
3641 Typ : constant Entity_Id := Etype (N);
3643 begin
3644 -- If we want to raise CE in the condition of a raise_CE node
3645 -- we may as well get rid of the condition
3647 if Present (Parent (N))
3648 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
3649 then
3650 Set_Condition (Parent (N), Empty);
3652 -- If the expression raising CE is a N_Raise_CE node, we can use
3653 -- that one. We just preserve the type of the context
3655 elsif Nkind (Exp) = N_Raise_Constraint_Error then
3656 Rewrite (N, Exp);
3657 Set_Etype (N, Typ);
3659 -- We have to build an explicit raise_ce node
3661 else
3662 Rewrite (N,
3663 Make_Raise_Constraint_Error (Sloc (Exp),
3664 Reason => CE_Range_Check_Failed));
3665 Set_Raises_Constraint_Error (N);
3666 Set_Etype (N, Typ);
3667 end if;
3668 end Rewrite_In_Raise_CE;
3670 ---------------------
3671 -- String_Type_Len --
3672 ---------------------
3674 function String_Type_Len (Stype : Entity_Id) return Uint is
3675 NT : constant Entity_Id := Etype (First_Index (Stype));
3676 T : Entity_Id;
3678 begin
3679 if Is_OK_Static_Subtype (NT) then
3680 T := NT;
3681 else
3682 T := Base_Type (NT);
3683 end if;
3685 return Expr_Value (Type_High_Bound (T)) -
3686 Expr_Value (Type_Low_Bound (T)) + 1;
3687 end String_Type_Len;
3689 ------------------------------------
3690 -- Subtypes_Statically_Compatible --
3691 ------------------------------------
3693 function Subtypes_Statically_Compatible
3694 (T1 : Entity_Id;
3695 T2 : Entity_Id)
3696 return Boolean
3698 begin
3699 if Is_Scalar_Type (T1) then
3701 -- Definitely compatible if we match
3703 if Subtypes_Statically_Match (T1, T2) then
3704 return True;
3706 -- If either subtype is nonstatic then they're not compatible
3708 elsif not Is_Static_Subtype (T1)
3709 or else not Is_Static_Subtype (T2)
3710 then
3711 return False;
3713 -- If either type has constraint error bounds, then consider that
3714 -- they match to avoid junk cascaded errors here.
3716 elsif not Is_OK_Static_Subtype (T1)
3717 or else not Is_OK_Static_Subtype (T2)
3718 then
3719 return True;
3721 -- Base types must match, but we don't check that (should
3722 -- we???) but we do at least check that both types are
3723 -- real, or both types are not real.
3725 elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then
3726 return False;
3728 -- Here we check the bounds
3730 else
3731 declare
3732 LB1 : constant Node_Id := Type_Low_Bound (T1);
3733 HB1 : constant Node_Id := Type_High_Bound (T1);
3734 LB2 : constant Node_Id := Type_Low_Bound (T2);
3735 HB2 : constant Node_Id := Type_High_Bound (T2);
3737 begin
3738 if Is_Real_Type (T1) then
3739 return
3740 (Expr_Value_R (LB1) > Expr_Value_R (HB1))
3741 or else
3742 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
3743 and then
3744 Expr_Value_R (HB1) <= Expr_Value_R (HB2));
3746 else
3747 return
3748 (Expr_Value (LB1) > Expr_Value (HB1))
3749 or else
3750 (Expr_Value (LB2) <= Expr_Value (LB1)
3751 and then
3752 Expr_Value (HB1) <= Expr_Value (HB2));
3753 end if;
3754 end;
3755 end if;
3757 elsif Is_Access_Type (T1) then
3758 return not Is_Constrained (T2)
3759 or else Subtypes_Statically_Match
3760 (Designated_Type (T1), Designated_Type (T2));
3762 else
3763 return (Is_Composite_Type (T1) and then not Is_Constrained (T2))
3764 or else Subtypes_Statically_Match (T1, T2);
3765 end if;
3766 end Subtypes_Statically_Compatible;
3768 -------------------------------
3769 -- Subtypes_Statically_Match --
3770 -------------------------------
3772 -- Subtypes statically match if they have statically matching constraints
3773 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
3774 -- they are the same identical constraint, or if they are static and the
3775 -- values match (RM 4.9.1(1)).
3777 function Subtypes_Statically_Match (T1, T2 : Entity_Id) return Boolean is
3778 begin
3779 -- A type always statically matches itself
3781 if T1 = T2 then
3782 return True;
3784 -- Scalar types
3786 elsif Is_Scalar_Type (T1) then
3788 -- Base types must be the same
3790 if Base_Type (T1) /= Base_Type (T2) then
3791 return False;
3792 end if;
3794 -- A constrained numeric subtype never matches an unconstrained
3795 -- subtype, i.e. both types must be constrained or unconstrained.
3797 -- To understand the requirement for this test, see RM 4.9.1(1).
3798 -- As is made clear in RM 3.5.4(11), type Integer, for example
3799 -- is a constrained subtype with constraint bounds matching the
3800 -- bounds of its corresponding uncontrained base type. In this
3801 -- situation, Integer and Integer'Base do not statically match,
3802 -- even though they have the same bounds.
3804 -- We only apply this test to types in Standard and types that
3805 -- appear in user programs. That way, we do not have to be
3806 -- too careful about setting Is_Constrained right for itypes.
3808 if Is_Numeric_Type (T1)
3809 and then (Is_Constrained (T1) /= Is_Constrained (T2))
3810 and then (Scope (T1) = Standard_Standard
3811 or else Comes_From_Source (T1))
3812 and then (Scope (T2) = Standard_Standard
3813 or else Comes_From_Source (T2))
3814 then
3815 return False;
3817 -- A generic scalar type does not statically match its base
3818 -- type (AI-311). In this case we make sure that the formals,
3819 -- which are first subtypes of their bases, are constrained.
3821 elsif Is_Generic_Type (T1)
3822 and then Is_Generic_Type (T2)
3823 and then (Is_Constrained (T1) /= Is_Constrained (T2))
3824 then
3825 return False;
3826 end if;
3828 -- If there was an error in either range, then just assume
3829 -- the types statically match to avoid further junk errors
3831 if Error_Posted (Scalar_Range (T1))
3832 or else
3833 Error_Posted (Scalar_Range (T2))
3834 then
3835 return True;
3836 end if;
3838 -- Otherwise both types have bound that can be compared
3840 declare
3841 LB1 : constant Node_Id := Type_Low_Bound (T1);
3842 HB1 : constant Node_Id := Type_High_Bound (T1);
3843 LB2 : constant Node_Id := Type_Low_Bound (T2);
3844 HB2 : constant Node_Id := Type_High_Bound (T2);
3846 begin
3847 -- If the bounds are the same tree node, then match
3849 if LB1 = LB2 and then HB1 = HB2 then
3850 return True;
3852 -- Otherwise bounds must be static and identical value
3854 else
3855 if not Is_Static_Subtype (T1)
3856 or else not Is_Static_Subtype (T2)
3857 then
3858 return False;
3860 -- If either type has constraint error bounds, then say
3861 -- that they match to avoid junk cascaded errors here.
3863 elsif not Is_OK_Static_Subtype (T1)
3864 or else not Is_OK_Static_Subtype (T2)
3865 then
3866 return True;
3868 elsif Is_Real_Type (T1) then
3869 return
3870 (Expr_Value_R (LB1) = Expr_Value_R (LB2))
3871 and then
3872 (Expr_Value_R (HB1) = Expr_Value_R (HB2));
3874 else
3875 return
3876 Expr_Value (LB1) = Expr_Value (LB2)
3877 and then
3878 Expr_Value (HB1) = Expr_Value (HB2);
3879 end if;
3880 end if;
3881 end;
3883 -- Type with discriminants
3885 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
3886 if Has_Discriminants (T1) /= Has_Discriminants (T2) then
3887 return False;
3888 end if;
3890 declare
3891 DL1 : constant Elist_Id := Discriminant_Constraint (T1);
3892 DL2 : constant Elist_Id := Discriminant_Constraint (T2);
3894 DA1 : Elmt_Id := First_Elmt (DL1);
3895 DA2 : Elmt_Id := First_Elmt (DL2);
3897 begin
3898 if DL1 = DL2 then
3899 return True;
3901 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
3902 return False;
3903 end if;
3905 while Present (DA1) loop
3906 declare
3907 Expr1 : constant Node_Id := Node (DA1);
3908 Expr2 : constant Node_Id := Node (DA2);
3910 begin
3911 if not Is_Static_Expression (Expr1)
3912 or else not Is_Static_Expression (Expr2)
3913 then
3914 return False;
3916 -- If either expression raised a constraint error,
3917 -- consider the expressions as matching, since this
3918 -- helps to prevent cascading errors.
3920 elsif Raises_Constraint_Error (Expr1)
3921 or else Raises_Constraint_Error (Expr2)
3922 then
3923 null;
3925 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
3926 return False;
3927 end if;
3928 end;
3930 Next_Elmt (DA1);
3931 Next_Elmt (DA2);
3932 end loop;
3933 end;
3935 return True;
3937 -- A definite type does not match an indefinite or classwide type
3939 elsif
3940 Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
3941 then
3942 return False;
3944 -- Array type
3946 elsif Is_Array_Type (T1) then
3948 -- If either subtype is unconstrained then both must be,
3949 -- and if both are unconstrained then no further checking
3950 -- is needed.
3952 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
3953 return not (Is_Constrained (T1) or else Is_Constrained (T2));
3954 end if;
3956 -- Both subtypes are constrained, so check that the index
3957 -- subtypes statically match.
3959 declare
3960 Index1 : Node_Id := First_Index (T1);
3961 Index2 : Node_Id := First_Index (T2);
3963 begin
3964 while Present (Index1) loop
3965 if not
3966 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
3967 then
3968 return False;
3969 end if;
3971 Next_Index (Index1);
3972 Next_Index (Index2);
3973 end loop;
3975 return True;
3976 end;
3978 elsif Is_Access_Type (T1) then
3979 return Subtypes_Statically_Match
3980 (Designated_Type (T1),
3981 Designated_Type (T2));
3983 -- All other types definitely match
3985 else
3986 return True;
3987 end if;
3988 end Subtypes_Statically_Match;
3990 ----------
3991 -- Test --
3992 ----------
3994 function Test (Cond : Boolean) return Uint is
3995 begin
3996 if Cond then
3997 return Uint_1;
3998 else
3999 return Uint_0;
4000 end if;
4001 end Test;
4003 ---------------------------------
4004 -- Test_Expression_Is_Foldable --
4005 ---------------------------------
4007 -- One operand case
4009 procedure Test_Expression_Is_Foldable
4010 (N : Node_Id;
4011 Op1 : Node_Id;
4012 Stat : out Boolean;
4013 Fold : out Boolean)
4015 begin
4016 Stat := False;
4018 -- If operand is Any_Type, just propagate to result and do not
4019 -- try to fold, this prevents cascaded errors.
4021 if Etype (Op1) = Any_Type then
4022 Set_Etype (N, Any_Type);
4023 Fold := False;
4024 return;
4026 -- If operand raises constraint error, then replace node N with the
4027 -- raise constraint error node, and we are obviously not foldable.
4028 -- Note that this replacement inherits the Is_Static_Expression flag
4029 -- from the operand.
4031 elsif Raises_Constraint_Error (Op1) then
4032 Rewrite_In_Raise_CE (N, Op1);
4033 Fold := False;
4034 return;
4036 -- If the operand is not static, then the result is not static, and
4037 -- all we have to do is to check the operand since it is now known
4038 -- to appear in a non-static context.
4040 elsif not Is_Static_Expression (Op1) then
4041 Check_Non_Static_Context (Op1);
4042 Fold := Compile_Time_Known_Value (Op1);
4043 return;
4045 -- An expression of a formal modular type is not foldable because
4046 -- the modulus is unknown.
4048 elsif Is_Modular_Integer_Type (Etype (Op1))
4049 and then Is_Generic_Type (Etype (Op1))
4050 then
4051 Check_Non_Static_Context (Op1);
4052 Fold := False;
4053 return;
4055 -- Here we have the case of an operand whose type is OK, which is
4056 -- static, and which does not raise constraint error, we can fold.
4058 else
4059 Set_Is_Static_Expression (N);
4060 Fold := True;
4061 Stat := True;
4062 end if;
4063 end Test_Expression_Is_Foldable;
4065 -- Two operand case
4067 procedure Test_Expression_Is_Foldable
4068 (N : Node_Id;
4069 Op1 : Node_Id;
4070 Op2 : Node_Id;
4071 Stat : out Boolean;
4072 Fold : out Boolean)
4074 Rstat : constant Boolean := Is_Static_Expression (Op1)
4075 and then Is_Static_Expression (Op2);
4077 begin
4078 Stat := False;
4080 -- If either operand is Any_Type, just propagate to result and
4081 -- do not try to fold, this prevents cascaded errors.
4083 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
4084 Set_Etype (N, Any_Type);
4085 Fold := False;
4086 return;
4088 -- If left operand raises constraint error, then replace node N with
4089 -- the raise constraint error node, and we are obviously not foldable.
4090 -- Is_Static_Expression is set from the two operands in the normal way,
4091 -- and we check the right operand if it is in a non-static context.
4093 elsif Raises_Constraint_Error (Op1) then
4094 if not Rstat then
4095 Check_Non_Static_Context (Op2);
4096 end if;
4098 Rewrite_In_Raise_CE (N, Op1);
4099 Set_Is_Static_Expression (N, Rstat);
4100 Fold := False;
4101 return;
4103 -- Similar processing for the case of the right operand. Note that
4104 -- we don't use this routine for the short-circuit case, so we do
4105 -- not have to worry about that special case here.
4107 elsif Raises_Constraint_Error (Op2) then
4108 if not Rstat then
4109 Check_Non_Static_Context (Op1);
4110 end if;
4112 Rewrite_In_Raise_CE (N, Op2);
4113 Set_Is_Static_Expression (N, Rstat);
4114 Fold := False;
4115 return;
4117 -- Exclude expressions of a generic modular type, as above
4119 elsif Is_Modular_Integer_Type (Etype (Op1))
4120 and then Is_Generic_Type (Etype (Op1))
4121 then
4122 Check_Non_Static_Context (Op1);
4123 Fold := False;
4124 return;
4126 -- If result is not static, then check non-static contexts on operands
4127 -- since one of them may be static and the other one may not be static
4129 elsif not Rstat then
4130 Check_Non_Static_Context (Op1);
4131 Check_Non_Static_Context (Op2);
4132 Fold := Compile_Time_Known_Value (Op1)
4133 and then Compile_Time_Known_Value (Op2);
4134 return;
4136 -- Else result is static and foldable. Both operands are static,
4137 -- and neither raises constraint error, so we can definitely fold.
4139 else
4140 Set_Is_Static_Expression (N);
4141 Fold := True;
4142 Stat := True;
4143 return;
4144 end if;
4145 end Test_Expression_Is_Foldable;
4147 --------------
4148 -- To_Bits --
4149 --------------
4151 procedure To_Bits (U : Uint; B : out Bits) is
4152 begin
4153 for J in 0 .. B'Last loop
4154 B (J) := (U / (2 ** J)) mod 2 /= 0;
4155 end loop;
4156 end To_Bits;
4158 --------------------
4159 -- Why_Not_Static --
4160 --------------------
4162 procedure Why_Not_Static (Expr : Node_Id) is
4163 N : constant Node_Id := Original_Node (Expr);
4164 Typ : Entity_Id;
4165 E : Entity_Id;
4167 procedure Why_Not_Static_List (L : List_Id);
4168 -- A version that can be called on a list of expressions. Finds
4169 -- all non-static violations in any element of the list.
4171 -------------------------
4172 -- Why_Not_Static_List --
4173 -------------------------
4175 procedure Why_Not_Static_List (L : List_Id) is
4176 N : Node_Id;
4178 begin
4179 if Is_Non_Empty_List (L) then
4180 N := First (L);
4181 while Present (N) loop
4182 Why_Not_Static (N);
4183 Next (N);
4184 end loop;
4185 end if;
4186 end Why_Not_Static_List;
4188 -- Start of processing for Why_Not_Static
4190 begin
4191 -- If in ACATS mode (debug flag 2), then suppress all these
4192 -- messages, this avoids massive updates to the ACATS base line.
4194 if Debug_Flag_2 then
4195 return;
4196 end if;
4198 -- Ignore call on error or empty node
4200 if No (Expr) or else Nkind (Expr) = N_Error then
4201 return;
4202 end if;
4204 -- Preprocessing for sub expressions
4206 if Nkind (Expr) in N_Subexpr then
4208 -- Nothing to do if expression is static
4210 if Is_OK_Static_Expression (Expr) then
4211 return;
4212 end if;
4214 -- Test for constraint error raised
4216 if Raises_Constraint_Error (Expr) then
4217 Error_Msg_N
4218 ("expression raises exception, cannot be static " &
4219 "('R'M 4.9(34))!", N);
4220 return;
4221 end if;
4223 -- If no type, then something is pretty wrong, so ignore
4225 Typ := Etype (Expr);
4227 if No (Typ) then
4228 return;
4229 end if;
4231 -- Type must be scalar or string type
4233 if not Is_Scalar_Type (Typ)
4234 and then not Is_String_Type (Typ)
4235 then
4236 Error_Msg_N
4237 ("static expression must have scalar or string type " &
4238 "('R'M 4.9(2))!", N);
4239 return;
4240 end if;
4241 end if;
4243 -- If we got through those checks, test particular node kind
4245 case Nkind (N) is
4246 when N_Expanded_Name | N_Identifier | N_Operator_Symbol =>
4247 E := Entity (N);
4249 if Is_Named_Number (E) then
4250 null;
4252 elsif Ekind (E) = E_Constant then
4253 if not Is_Static_Expression (Constant_Value (E)) then
4254 Error_Msg_NE
4255 ("& is not a static constant ('R'M 4.9(5))!", N, E);
4256 end if;
4258 else
4259 Error_Msg_NE
4260 ("& is not static constant or named number " &
4261 "('R'M 4.9(5))!", N, E);
4262 end if;
4264 when N_Binary_Op | N_And_Then | N_Or_Else | N_In | N_Not_In =>
4265 if Nkind (N) in N_Op_Shift then
4266 Error_Msg_N
4267 ("shift functions are never static ('R'M 4.9(6,18))!", N);
4269 else
4270 Why_Not_Static (Left_Opnd (N));
4271 Why_Not_Static (Right_Opnd (N));
4272 end if;
4274 when N_Unary_Op =>
4275 Why_Not_Static (Right_Opnd (N));
4277 when N_Attribute_Reference =>
4278 Why_Not_Static_List (Expressions (N));
4280 E := Etype (Prefix (N));
4282 if E = Standard_Void_Type then
4283 return;
4284 end if;
4286 -- Special case non-scalar'Size since this is a common error
4288 if Attribute_Name (N) = Name_Size then
4289 Error_Msg_N
4290 ("size attribute is only static for scalar type " &
4291 "('R'M 4.9(7,8))", N);
4293 -- Flag array cases
4295 elsif Is_Array_Type (E) then
4296 if Attribute_Name (N) /= Name_First
4297 and then
4298 Attribute_Name (N) /= Name_Last
4299 and then
4300 Attribute_Name (N) /= Name_Length
4301 then
4302 Error_Msg_N
4303 ("static array attribute must be Length, First, or Last " &
4304 "('R'M 4.9(8))!", N);
4306 -- Since we know the expression is not-static (we already
4307 -- tested for this, must mean array is not static).
4309 else
4310 Error_Msg_N
4311 ("prefix is non-static array ('R'M 4.9(8))!", Prefix (N));
4312 end if;
4314 return;
4316 -- Special case generic types, since again this is a common
4317 -- source of confusion.
4319 elsif Is_Generic_Actual_Type (E)
4320 or else
4321 Is_Generic_Type (E)
4322 then
4323 Error_Msg_N
4324 ("attribute of generic type is never static " &
4325 "('R'M 4.9(7,8))!", N);
4327 elsif Is_Static_Subtype (E) then
4328 null;
4330 elsif Is_Scalar_Type (E) then
4331 Error_Msg_N
4332 ("prefix type for attribute is not static scalar subtype " &
4333 "('R'M 4.9(7))!", N);
4335 else
4336 Error_Msg_N
4337 ("static attribute must apply to array/scalar type " &
4338 "('R'M 4.9(7,8))!", N);
4339 end if;
4341 when N_String_Literal =>
4342 Error_Msg_N
4343 ("subtype of string literal is non-static ('R'M 4.9(4))!", N);
4345 when N_Explicit_Dereference =>
4346 Error_Msg_N
4347 ("explicit dereference is never static ('R'M 4.9)!", N);
4349 when N_Function_Call =>
4350 Why_Not_Static_List (Parameter_Associations (N));
4351 Error_Msg_N ("non-static function call ('R'M 4.9(6,18))!", N);
4353 when N_Parameter_Association =>
4354 Why_Not_Static (Explicit_Actual_Parameter (N));
4356 when N_Indexed_Component =>
4357 Error_Msg_N
4358 ("indexed component is never static ('R'M 4.9)!", N);
4360 when N_Procedure_Call_Statement =>
4361 Error_Msg_N
4362 ("procedure call is never static ('R'M 4.9)!", N);
4364 when N_Qualified_Expression =>
4365 Why_Not_Static (Expression (N));
4367 when N_Aggregate | N_Extension_Aggregate =>
4368 Error_Msg_N
4369 ("an aggregate is never static ('R'M 4.9)!", N);
4371 when N_Range =>
4372 Why_Not_Static (Low_Bound (N));
4373 Why_Not_Static (High_Bound (N));
4375 when N_Range_Constraint =>
4376 Why_Not_Static (Range_Expression (N));
4378 when N_Subtype_Indication =>
4379 Why_Not_Static (Constraint (N));
4381 when N_Selected_Component =>
4382 Error_Msg_N
4383 ("selected component is never static ('R'M 4.9)!", N);
4385 when N_Slice =>
4386 Error_Msg_N
4387 ("slice is never static ('R'M 4.9)!", N);
4389 when N_Type_Conversion =>
4390 Why_Not_Static (Expression (N));
4392 if not Is_Scalar_Type (Etype (Prefix (N)))
4393 or else not Is_Static_Subtype (Etype (Prefix (N)))
4394 then
4395 Error_Msg_N
4396 ("static conversion requires static scalar subtype result " &
4397 "('R'M 4.9(9))!", N);
4398 end if;
4400 when N_Unchecked_Type_Conversion =>
4401 Error_Msg_N
4402 ("unchecked type conversion is never static ('R'M 4.9)!", N);
4404 when others =>
4405 null;
4407 end case;
4408 end Why_Not_Static;
4410 end Sem_Eval;