PR c++/29733
[official-gcc.git] / gcc / ada / sem_eval.adb
blob65005de952b23fa25ee350fa83c03b77b0a45b90
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-2006, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 Lib; use Lib;
36 with Nmake; use Nmake;
37 with Nlists; use Nlists;
38 with Opt; use Opt;
39 with Sem; use Sem;
40 with Sem_Cat; use Sem_Cat;
41 with Sem_Ch6; use Sem_Ch6;
42 with Sem_Ch8; use Sem_Ch8;
43 with Sem_Res; use Sem_Res;
44 with Sem_Util; use Sem_Util;
45 with Sem_Type; use Sem_Type;
46 with Sem_Warn; use Sem_Warn;
47 with Sinfo; use Sinfo;
48 with Snames; use Snames;
49 with Stand; use Stand;
50 with Stringt; use Stringt;
51 with Tbuild; use Tbuild;
53 package body Sem_Eval is
55 -----------------------------------------
56 -- Handling of Compile Time Evaluation --
57 -----------------------------------------
59 -- The compile time evaluation of expressions is distributed over several
60 -- Eval_xxx procedures. These procedures are called immediatedly after
61 -- a subexpression is resolved and is therefore accomplished in a bottom
62 -- up fashion. The flags are synthesized using the following approach.
64 -- Is_Static_Expression is determined by following the detailed rules
65 -- in RM 4.9(4-14). This involves testing the Is_Static_Expression
66 -- flag of the operands in many cases.
68 -- Raises_Constraint_Error is set if any of the operands have the flag
69 -- set or if an attempt to compute the value of the current expression
70 -- results in detection of a runtime constraint error.
72 -- As described in the spec, the requirement is that Is_Static_Expression
73 -- be accurately set, and in addition for nodes for which this flag is set,
74 -- Raises_Constraint_Error must also be set. Furthermore a node which has
75 -- Is_Static_Expression set, and Raises_Constraint_Error clear, then the
76 -- requirement is that the expression value must be precomputed, and the
77 -- node is either a literal, or the name of a constant entity whose value
78 -- is a static expression.
80 -- The general approach is as follows. First compute Is_Static_Expression.
81 -- If the node is not static, then the flag is left off in the node and
82 -- we are all done. Otherwise for a static node, we test if any of the
83 -- operands will raise constraint error, and if so, propagate the flag
84 -- Raises_Constraint_Error to the result node and we are done (since the
85 -- error was already posted at a lower level).
87 -- For the case of a static node whose operands do not raise constraint
88 -- error, we attempt to evaluate the node. If this evaluation succeeds,
89 -- then the node is replaced by the result of this computation. If the
90 -- evaluation raises constraint error, then we rewrite the node with
91 -- Apply_Compile_Time_Constraint_Error to raise the exception and also
92 -- to post appropriate error messages.
94 ----------------
95 -- Local Data --
96 ----------------
98 type Bits is array (Nat range <>) of Boolean;
99 -- Used to convert unsigned (modular) values for folding logical ops
101 -- The following definitions are used to maintain a cache of nodes that
102 -- have compile time known values. The cache is maintained only for
103 -- discrete types (the most common case), and is populated by calls to
104 -- Compile_Time_Known_Value and Expr_Value, but only used by Expr_Value
105 -- since it is possible for the status to change (in particular it is
106 -- possible for a node to get replaced by a constraint error node).
108 CV_Bits : constant := 5;
109 -- Number of low order bits of Node_Id value used to reference entries
110 -- in the cache table.
112 CV_Cache_Size : constant Nat := 2 ** CV_Bits;
113 -- Size of cache for compile time values
115 subtype CV_Range is Nat range 0 .. CV_Cache_Size;
117 type CV_Entry is record
118 N : Node_Id;
119 V : Uint;
120 end record;
122 type CV_Cache_Array is array (CV_Range) of CV_Entry;
124 CV_Cache : CV_Cache_Array := (others => (Node_High_Bound, Uint_0));
125 -- This is the actual cache, with entries consisting of node/value pairs,
126 -- and the impossible value Node_High_Bound used for unset entries.
128 -----------------------
129 -- Local Subprograms --
130 -----------------------
132 function From_Bits (B : Bits; T : Entity_Id) return Uint;
133 -- Converts a bit string of length B'Length to a Uint value to be used
134 -- for a target of type T, which is a modular type. This procedure
135 -- includes the necessary reduction by the modulus in the case of a
136 -- non-binary modulus (for a binary modulus, the bit string is the
137 -- right length any way so all is well).
139 function Get_String_Val (N : Node_Id) return Node_Id;
140 -- Given a tree node for a folded string or character value, returns
141 -- the corresponding string literal or character literal (one of the
142 -- two must be available, or the operand would not have been marked
143 -- as foldable in the earlier analysis of the operation).
145 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean;
146 -- Bits represents the number of bits in an integer value to be computed
147 -- (but the value has not been computed yet). If this value in Bits is
148 -- reasonable, a result of True is returned, with the implication that
149 -- the caller should go ahead and complete the calculation. If the value
150 -- in Bits is unreasonably large, then an error is posted on node N, and
151 -- False is returned (and the caller skips the proposed calculation).
153 procedure Out_Of_Range (N : Node_Id);
154 -- This procedure is called if it is determined that node N, which
155 -- appears in a non-static context, is a compile time known value
156 -- which is outside its range, i.e. the range of Etype. This is used
157 -- in contexts where this is an illegality if N is static, and should
158 -- generate a warning otherwise.
160 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id);
161 -- N and Exp are nodes representing an expression, Exp is known
162 -- to raise CE. N is rewritten in term of Exp in the optimal way.
164 function String_Type_Len (Stype : Entity_Id) return Uint;
165 -- Given a string type, determines the length of the index type, or,
166 -- if this index type is non-static, the length of the base type of
167 -- this index type. Note that if the string type is itself static,
168 -- then the index type is static, so the second case applies only
169 -- if the string type passed is non-static.
171 function Test (Cond : Boolean) return Uint;
172 pragma Inline (Test);
173 -- This function simply returns the appropriate Boolean'Pos value
174 -- corresponding to the value of Cond as a universal integer. It is
175 -- used for producing the result of the static evaluation of the
176 -- logical operators
178 procedure Test_Expression_Is_Foldable
179 (N : Node_Id;
180 Op1 : Node_Id;
181 Stat : out Boolean;
182 Fold : out Boolean);
183 -- Tests to see if expression N whose single operand is Op1 is foldable,
184 -- i.e. the operand value is known at compile time. If the operation is
185 -- foldable, then Fold is True on return, and Stat indicates whether
186 -- the result is static (i.e. both operands were static). Note that it
187 -- is quite possible for Fold to be True, and Stat to be False, since
188 -- there are cases in which we know the value of an operand even though
189 -- it is not technically static (e.g. the static lower bound of a range
190 -- whose upper bound is non-static).
192 -- If Stat is set False on return, then Expression_Is_Foldable makes a
193 -- call to Check_Non_Static_Context on the operand. If Fold is False on
194 -- return, then all processing is complete, and the caller should
195 -- return, since there is nothing else to do.
197 procedure Test_Expression_Is_Foldable
198 (N : Node_Id;
199 Op1 : Node_Id;
200 Op2 : Node_Id;
201 Stat : out Boolean;
202 Fold : out Boolean);
203 -- Same processing, except applies to an expression N with two operands
204 -- Op1 and Op2.
206 procedure To_Bits (U : Uint; B : out Bits);
207 -- Converts a Uint value to a bit string of length B'Length
209 ------------------------------
210 -- Check_Non_Static_Context --
211 ------------------------------
213 procedure Check_Non_Static_Context (N : Node_Id) is
214 T : constant Entity_Id := Etype (N);
215 Checks_On : constant Boolean :=
216 not Index_Checks_Suppressed (T)
217 and not Range_Checks_Suppressed (T);
219 begin
220 -- Ignore cases of non-scalar types or error types
222 if T = Any_Type or else not Is_Scalar_Type (T) then
223 return;
224 end if;
226 -- At this stage we have a scalar type. If we have an expression
227 -- that raises CE, then we already issued a warning or error msg
228 -- so there is nothing more to be done in this routine.
230 if Raises_Constraint_Error (N) then
231 return;
232 end if;
234 -- Now we have a scalar type which is not marked as raising a
235 -- constraint error exception. The main purpose of this routine
236 -- is to deal with static expressions appearing in a non-static
237 -- context. That means that if we do not have a static expression
238 -- then there is not much to do. The one case that we deal with
239 -- here is that if we have a floating-point value that is out of
240 -- range, then we post a warning that an infinity will result.
242 if not Is_Static_Expression (N) then
243 if Is_Floating_Point_Type (T)
244 and then Is_Out_Of_Range (N, Base_Type (T))
245 then
246 Error_Msg_N
247 ("?float value out of range, infinity will be generated", N);
248 end if;
250 return;
251 end if;
253 -- Here we have the case of outer level static expression of
254 -- scalar type, where the processing of this procedure is needed.
256 -- For real types, this is where we convert the value to a machine
257 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should
258 -- only need to do this if the parent is a constant declaration,
259 -- since in other cases, gigi should do the necessary conversion
260 -- correctly, but experimentation shows that this is not the case
261 -- on all machines, in particular if we do not convert all literals
262 -- to machine values in non-static contexts, then ACVC test C490001
263 -- fails on Sparc/Solaris and SGI/Irix.
265 if Nkind (N) = N_Real_Literal
266 and then not Is_Machine_Number (N)
267 and then not Is_Generic_Type (Etype (N))
268 and then Etype (N) /= Universal_Real
269 then
270 -- Check that value is in bounds before converting to machine
271 -- number, so as not to lose case where value overflows in the
272 -- least significant bit or less. See B490001.
274 if Is_Out_Of_Range (N, Base_Type (T)) then
275 Out_Of_Range (N);
276 return;
277 end if;
279 -- Note: we have to copy the node, to avoid problems with conformance
280 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
282 Rewrite (N, New_Copy (N));
284 if not Is_Floating_Point_Type (T) then
285 Set_Realval
286 (N, Corresponding_Integer_Value (N) * Small_Value (T));
288 elsif not UR_Is_Zero (Realval (N)) then
290 -- Note: even though RM 4.9(38) specifies biased rounding,
291 -- this has been modified by AI-100 in order to prevent
292 -- confusing differences in rounding between static and
293 -- non-static expressions. AI-100 specifies that the effect
294 -- of such rounding is implementation dependent, and in GNAT
295 -- we round to nearest even to match the run-time behavior.
297 Set_Realval
298 (N, Machine (Base_Type (T), Realval (N), Round_Even, N));
299 end if;
301 Set_Is_Machine_Number (N);
302 end if;
304 -- Check for out of range universal integer. This is a non-static
305 -- context, so the integer value must be in range of the runtime
306 -- representation of universal integers.
308 -- We do this only within an expression, because that is the only
309 -- case in which non-static universal integer values can occur, and
310 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
311 -- called in contexts like the expression of a number declaration where
312 -- we certainly want to allow out of range values.
314 if Etype (N) = Universal_Integer
315 and then Nkind (N) = N_Integer_Literal
316 and then Nkind (Parent (N)) in N_Subexpr
317 and then
318 (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer))
319 or else
320 Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer)))
321 then
322 Apply_Compile_Time_Constraint_Error
323 (N, "non-static universal integer value out of range?",
324 CE_Range_Check_Failed);
326 -- Check out of range of base type
328 elsif Is_Out_Of_Range (N, Base_Type (T)) then
329 Out_Of_Range (N);
331 -- Give warning if outside subtype (where one or both of the
332 -- bounds of the subtype is static). This warning is omitted
333 -- if the expression appears in a range that could be null
334 -- (warnings are handled elsewhere for this case).
336 elsif T /= Base_Type (T)
337 and then Nkind (Parent (N)) /= N_Range
338 then
339 if Is_In_Range (N, T) then
340 null;
342 elsif Is_Out_Of_Range (N, T) then
343 Apply_Compile_Time_Constraint_Error
344 (N, "value not in range of}?", CE_Range_Check_Failed);
346 elsif Checks_On then
347 Enable_Range_Check (N);
349 else
350 Set_Do_Range_Check (N, False);
351 end if;
352 end if;
353 end Check_Non_Static_Context;
355 ---------------------------------
356 -- Check_String_Literal_Length --
357 ---------------------------------
359 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is
360 begin
361 if not Raises_Constraint_Error (N)
362 and then Is_Constrained (Ttype)
363 then
365 UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype)
366 then
367 Apply_Compile_Time_Constraint_Error
368 (N, "string length wrong for}?",
369 CE_Length_Check_Failed,
370 Ent => Ttype,
371 Typ => Ttype);
372 end if;
373 end if;
374 end Check_String_Literal_Length;
376 --------------------------
377 -- Compile_Time_Compare --
378 --------------------------
380 function Compile_Time_Compare
381 (L, R : Node_Id;
382 Rec : Boolean := False) return Compare_Result
384 Ltyp : constant Entity_Id := Etype (L);
385 Rtyp : constant Entity_Id := Etype (R);
387 procedure Compare_Decompose
388 (N : Node_Id;
389 R : out Node_Id;
390 V : out Uint);
391 -- This procedure decomposes the node N into an expression node
392 -- and a signed offset, so that the value of N is equal to the
393 -- value of R plus the value V (which may be negative). If no
394 -- such decomposition is possible, then on return R is a copy
395 -- of N, and V is set to zero.
397 function Compare_Fixup (N : Node_Id) return Node_Id;
398 -- This function deals with replacing 'Last and 'First references
399 -- with their corresponding type bounds, which we then can compare.
400 -- The argument is the original node, the result is the identity,
401 -- unless we have a 'Last/'First reference in which case the value
402 -- returned is the appropriate type bound.
404 function Is_Same_Value (L, R : Node_Id) return Boolean;
405 -- Returns True iff L and R represent expressions that definitely
406 -- have identical (but not necessarily compile time known) values
407 -- Indeed the caller is expected to have already dealt with the
408 -- cases of compile time known values, so these are not tested here.
410 -----------------------
411 -- Compare_Decompose --
412 -----------------------
414 procedure Compare_Decompose
415 (N : Node_Id;
416 R : out Node_Id;
417 V : out Uint)
419 begin
420 if Nkind (N) = N_Op_Add
421 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
422 then
423 R := Left_Opnd (N);
424 V := Intval (Right_Opnd (N));
425 return;
427 elsif Nkind (N) = N_Op_Subtract
428 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
429 then
430 R := Left_Opnd (N);
431 V := UI_Negate (Intval (Right_Opnd (N)));
432 return;
434 elsif Nkind (N) = N_Attribute_Reference then
436 if Attribute_Name (N) = Name_Succ then
437 R := First (Expressions (N));
438 V := Uint_1;
439 return;
441 elsif Attribute_Name (N) = Name_Pred then
442 R := First (Expressions (N));
443 V := Uint_Minus_1;
444 return;
445 end if;
446 end if;
448 R := N;
449 V := Uint_0;
450 end Compare_Decompose;
452 -------------------
453 -- Compare_Fixup --
454 -------------------
456 function Compare_Fixup (N : Node_Id) return Node_Id is
457 Indx : Node_Id;
458 Xtyp : Entity_Id;
459 Subs : Nat;
461 begin
462 if Nkind (N) = N_Attribute_Reference
463 and then (Attribute_Name (N) = Name_First
464 or else
465 Attribute_Name (N) = Name_Last)
466 then
467 Xtyp := Etype (Prefix (N));
469 -- If we have no type, then just abandon the attempt to do
470 -- a fixup, this is probably the result of some other error.
472 if No (Xtyp) then
473 return N;
474 end if;
476 -- Dereference an access type
478 if Is_Access_Type (Xtyp) then
479 Xtyp := Designated_Type (Xtyp);
480 end if;
482 -- If we don't have an array type at this stage, something
483 -- is peculiar, e.g. another error, and we abandon the attempt
484 -- at a fixup.
486 if not Is_Array_Type (Xtyp) then
487 return N;
488 end if;
490 -- Ignore unconstrained array, since bounds are not meaningful
492 if not Is_Constrained (Xtyp) then
493 return N;
494 end if;
496 if Ekind (Xtyp) = E_String_Literal_Subtype then
497 if Attribute_Name (N) = Name_First then
498 return String_Literal_Low_Bound (Xtyp);
500 else -- Attribute_Name (N) = Name_Last
501 return Make_Integer_Literal (Sloc (N),
502 Intval => Intval (String_Literal_Low_Bound (Xtyp))
503 + String_Literal_Length (Xtyp));
504 end if;
505 end if;
507 -- Find correct index type
509 Indx := First_Index (Xtyp);
511 if Present (Expressions (N)) then
512 Subs := UI_To_Int (Expr_Value (First (Expressions (N))));
514 for J in 2 .. Subs loop
515 Indx := Next_Index (Indx);
516 end loop;
517 end if;
519 Xtyp := Etype (Indx);
521 if Attribute_Name (N) = Name_First then
522 return Type_Low_Bound (Xtyp);
524 else -- Attribute_Name (N) = Name_Last
525 return Type_High_Bound (Xtyp);
526 end if;
527 end if;
529 return N;
530 end Compare_Fixup;
532 -------------------
533 -- Is_Same_Value --
534 -------------------
536 function Is_Same_Value (L, R : Node_Id) return Boolean is
537 Lf : constant Node_Id := Compare_Fixup (L);
538 Rf : constant Node_Id := Compare_Fixup (R);
540 function Is_Same_Subscript (L, R : List_Id) return Boolean;
541 -- L, R are the Expressions values from two attribute nodes
542 -- for First or Last attributes. Either may be set to No_List
543 -- if no expressions are present (indicating subscript 1).
544 -- The result is True if both expressions represent the same
545 -- subscript (note that one case is where one subscript is
546 -- missing and the other is explicitly set to 1).
548 -----------------------
549 -- Is_Same_Subscript --
550 -----------------------
552 function Is_Same_Subscript (L, R : List_Id) return Boolean is
553 begin
554 if L = No_List then
555 if R = No_List then
556 return True;
557 else
558 return Expr_Value (First (R)) = Uint_1;
559 end if;
561 else
562 if R = No_List then
563 return Expr_Value (First (L)) = Uint_1;
564 else
565 return Expr_Value (First (L)) = Expr_Value (First (R));
566 end if;
567 end if;
568 end Is_Same_Subscript;
570 -- Start of processing for Is_Same_Value
572 begin
573 -- Values are the same if they are the same identifier and the
574 -- identifier refers to a constant object (E_Constant). This
575 -- does not however apply to Float types, since we may have two
576 -- NaN values and they should never compare equal.
578 if Nkind (Lf) = N_Identifier and then Nkind (Rf) = N_Identifier
579 and then Entity (Lf) = Entity (Rf)
580 and then not Is_Floating_Point_Type (Etype (L))
581 and then (Ekind (Entity (Lf)) = E_Constant or else
582 Ekind (Entity (Lf)) = E_In_Parameter or else
583 Ekind (Entity (Lf)) = E_Loop_Parameter)
584 then
585 return True;
587 -- Or if they are compile time known and identical
589 elsif Compile_Time_Known_Value (Lf)
590 and then
591 Compile_Time_Known_Value (Rf)
592 and then Expr_Value (Lf) = Expr_Value (Rf)
593 then
594 return True;
596 -- Or if they are both 'First or 'Last values applying to the
597 -- same entity (first and last don't change even if value does)
599 elsif Nkind (Lf) = N_Attribute_Reference
600 and then
601 Nkind (Rf) = N_Attribute_Reference
602 and then Attribute_Name (Lf) = Attribute_Name (Rf)
603 and then (Attribute_Name (Lf) = Name_First
604 or else
605 Attribute_Name (Lf) = Name_Last)
606 and then Is_Entity_Name (Prefix (Lf))
607 and then Is_Entity_Name (Prefix (Rf))
608 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
609 and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf))
610 then
611 return True;
613 -- All other cases, we can't tell
615 else
616 return False;
617 end if;
618 end Is_Same_Value;
620 -- Start of processing for Compile_Time_Compare
622 begin
623 -- If either operand could raise constraint error, then we cannot
624 -- know the result at compile time (since CE may be raised!)
626 if not (Cannot_Raise_Constraint_Error (L)
627 and then
628 Cannot_Raise_Constraint_Error (R))
629 then
630 return Unknown;
631 end if;
633 -- Identical operands are most certainly equal
635 if L = R then
636 return EQ;
638 -- If expressions have no types, then do not attempt to determine
639 -- if they are the same, since something funny is going on. One
640 -- case in which this happens is during generic template analysis,
641 -- when bounds are not fully analyzed.
643 elsif No (Ltyp) or else No (Rtyp) then
644 return Unknown;
646 -- We only attempt compile time analysis for scalar values, and
647 -- not for packed arrays represented as modular types, where the
648 -- semantics of comparison is quite different.
650 elsif not Is_Scalar_Type (Ltyp)
651 or else Is_Packed_Array_Type (Ltyp)
652 then
653 return Unknown;
655 -- Case where comparison involves two compile time known values
657 elsif Compile_Time_Known_Value (L)
658 and then Compile_Time_Known_Value (R)
659 then
660 -- For the floating-point case, we have to be a little careful, since
661 -- at compile time we are dealing with universal exact values, but at
662 -- runtime, these will be in non-exact target form. That's why the
663 -- returned results are LE and GE below instead of LT and GT.
665 if Is_Floating_Point_Type (Ltyp)
666 or else
667 Is_Floating_Point_Type (Rtyp)
668 then
669 declare
670 Lo : constant Ureal := Expr_Value_R (L);
671 Hi : constant Ureal := Expr_Value_R (R);
673 begin
674 if Lo < Hi then
675 return LE;
676 elsif Lo = Hi then
677 return EQ;
678 else
679 return GE;
680 end if;
681 end;
683 -- For the integer case we know exactly (note that this includes the
684 -- fixed-point case, where we know the run time integer values now)
686 else
687 declare
688 Lo : constant Uint := Expr_Value (L);
689 Hi : constant Uint := Expr_Value (R);
691 begin
692 if Lo < Hi then
693 return LT;
694 elsif Lo = Hi then
695 return EQ;
696 else
697 return GT;
698 end if;
699 end;
700 end if;
702 -- Cases where at least one operand is not known at compile time
704 else
705 -- Here is where we check for comparisons against maximum bounds of
706 -- types, where we know that no value can be outside the bounds of
707 -- the subtype. Note that this routine is allowed to assume that all
708 -- expressions are within their subtype bounds. Callers wishing to
709 -- deal with possibly invalid values must in any case take special
710 -- steps (e.g. conversions to larger types) to avoid this kind of
711 -- optimization, which is always considered to be valid. We do not
712 -- attempt this optimization with generic types, since the type
713 -- bounds may not be meaningful in this case.
715 -- We are in danger of an infinite recursion here. It does not seem
716 -- useful to go more than one level deep, so the parameter Rec is
717 -- used to protect ourselves against this infinite recursion.
719 if not Rec
720 and then Is_Discrete_Type (Ltyp)
721 and then Is_Discrete_Type (Rtyp)
722 and then not Is_Generic_Type (Ltyp)
723 and then not Is_Generic_Type (Rtyp)
724 then
725 -- See if we can get a decisive check against one operand and
726 -- a bound of the other operand (four possible tests here).
728 case Compile_Time_Compare (L, Type_Low_Bound (Rtyp), True) is
729 when LT => return LT;
730 when LE => return LE;
731 when EQ => return LE;
732 when others => null;
733 end case;
735 case Compile_Time_Compare (L, Type_High_Bound (Rtyp), True) is
736 when GT => return GT;
737 when GE => return GE;
738 when EQ => return GE;
739 when others => null;
740 end case;
742 case Compile_Time_Compare (Type_Low_Bound (Ltyp), R, True) is
743 when GT => return GT;
744 when GE => return GE;
745 when EQ => return GE;
746 when others => null;
747 end case;
749 case Compile_Time_Compare (Type_High_Bound (Ltyp), R, True) is
750 when LT => return LT;
751 when LE => return LE;
752 when EQ => return LE;
753 when others => null;
754 end case;
755 end if;
757 -- Next attempt is to decompose the expressions to extract
758 -- a constant offset resulting from the use of any of the forms:
760 -- expr + literal
761 -- expr - literal
762 -- typ'Succ (expr)
763 -- typ'Pred (expr)
765 -- Then we see if the two expressions are the same value, and if so
766 -- the result is obtained by comparing the offsets.
768 declare
769 Lnode : Node_Id;
770 Loffs : Uint;
771 Rnode : Node_Id;
772 Roffs : Uint;
774 begin
775 Compare_Decompose (L, Lnode, Loffs);
776 Compare_Decompose (R, Rnode, Roffs);
778 if Is_Same_Value (Lnode, Rnode) then
779 if Loffs = Roffs then
780 return EQ;
782 elsif Loffs < Roffs then
783 return LT;
785 else
786 return GT;
787 end if;
789 -- If the expressions are different, we cannot say at compile
790 -- time how they compare, so we return the Unknown indication.
792 else
793 return Unknown;
794 end if;
795 end;
796 end if;
797 end Compile_Time_Compare;
799 -------------------------------
800 -- Compile_Time_Known_Bounds --
801 -------------------------------
803 function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean is
804 Indx : Node_Id;
805 Typ : Entity_Id;
807 begin
808 if not Is_Array_Type (T) then
809 return False;
810 end if;
812 Indx := First_Index (T);
813 while Present (Indx) loop
814 Typ := Underlying_Type (Etype (Indx));
815 if not Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
816 return False;
817 elsif not Compile_Time_Known_Value (Type_High_Bound (Typ)) then
818 return False;
819 else
820 Next_Index (Indx);
821 end if;
822 end loop;
824 return True;
825 end Compile_Time_Known_Bounds;
827 ------------------------------
828 -- Compile_Time_Known_Value --
829 ------------------------------
831 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
832 K : constant Node_Kind := Nkind (Op);
833 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
835 begin
836 -- Never known at compile time if bad type or raises constraint error
837 -- or empty (latter case occurs only as a result of a previous error)
839 if No (Op)
840 or else Op = Error
841 or else Etype (Op) = Any_Type
842 or else Raises_Constraint_Error (Op)
843 then
844 return False;
845 end if;
847 -- If this is not a static expression and we are in configurable run
848 -- time mode, then we consider it not known at compile time. This
849 -- avoids anomalies where whether something is permitted with a given
850 -- configurable run-time library depends on how good the compiler is
851 -- at optimizing and knowing that things are constant when they
852 -- are non-static.
854 if Configurable_Run_Time_Mode and then not Is_Static_Expression (Op) then
855 return False;
856 end if;
858 -- If we have an entity name, then see if it is the name of a constant
859 -- and if so, test the corresponding constant value, or the name of
860 -- an enumeration literal, which is always a constant.
862 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
863 declare
864 E : constant Entity_Id := Entity (Op);
865 V : Node_Id;
867 begin
868 -- Never known at compile time if it is a packed array value.
869 -- We might want to try to evaluate these at compile time one
870 -- day, but we do not make that attempt now.
872 if Is_Packed_Array_Type (Etype (Op)) then
873 return False;
874 end if;
876 if Ekind (E) = E_Enumeration_Literal then
877 return True;
879 elsif Ekind (E) = E_Constant then
880 V := Constant_Value (E);
881 return Present (V) and then Compile_Time_Known_Value (V);
882 end if;
883 end;
885 -- We have a value, see if it is compile time known
887 else
888 -- Integer literals are worth storing in the cache
890 if K = N_Integer_Literal then
891 CV_Ent.N := Op;
892 CV_Ent.V := Intval (Op);
893 return True;
895 -- Other literals and NULL are known at compile time
897 elsif
898 K = N_Character_Literal
899 or else
900 K = N_Real_Literal
901 or else
902 K = N_String_Literal
903 or else
904 K = N_Null
905 then
906 return True;
908 -- Any reference to Null_Parameter is known at compile time. No
909 -- other attribute references (that have not already been folded)
910 -- are known at compile time.
912 elsif K = N_Attribute_Reference then
913 return Attribute_Name (Op) = Name_Null_Parameter;
914 end if;
915 end if;
917 -- If we fall through, not known at compile time
919 return False;
921 -- If we get an exception while trying to do this test, then some error
922 -- has occurred, and we simply say that the value is not known after all
924 exception
925 when others =>
926 return False;
927 end Compile_Time_Known_Value;
929 --------------------------------------
930 -- Compile_Time_Known_Value_Or_Aggr --
931 --------------------------------------
933 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is
934 begin
935 -- If we have an entity name, then see if it is the name of a constant
936 -- and if so, test the corresponding constant value, or the name of
937 -- an enumeration literal, which is always a constant.
939 if Is_Entity_Name (Op) then
940 declare
941 E : constant Entity_Id := Entity (Op);
942 V : Node_Id;
944 begin
945 if Ekind (E) = E_Enumeration_Literal then
946 return True;
948 elsif Ekind (E) /= E_Constant then
949 return False;
951 else
952 V := Constant_Value (E);
953 return Present (V)
954 and then Compile_Time_Known_Value_Or_Aggr (V);
955 end if;
956 end;
958 -- We have a value, see if it is compile time known
960 else
961 if Compile_Time_Known_Value (Op) then
962 return True;
964 elsif Nkind (Op) = N_Aggregate then
966 if Present (Expressions (Op)) then
967 declare
968 Expr : Node_Id;
970 begin
971 Expr := First (Expressions (Op));
972 while Present (Expr) loop
973 if not Compile_Time_Known_Value_Or_Aggr (Expr) then
974 return False;
975 end if;
977 Next (Expr);
978 end loop;
979 end;
980 end if;
982 if Present (Component_Associations (Op)) then
983 declare
984 Cass : Node_Id;
986 begin
987 Cass := First (Component_Associations (Op));
988 while Present (Cass) loop
989 if not
990 Compile_Time_Known_Value_Or_Aggr (Expression (Cass))
991 then
992 return False;
993 end if;
995 Next (Cass);
996 end loop;
997 end;
998 end if;
1000 return True;
1002 -- All other types of values are not known at compile time
1004 else
1005 return False;
1006 end if;
1008 end if;
1009 end Compile_Time_Known_Value_Or_Aggr;
1011 -----------------
1012 -- Eval_Actual --
1013 -----------------
1015 -- This is only called for actuals of functions that are not predefined
1016 -- operators (which have already been rewritten as operators at this
1017 -- stage), so the call can never be folded, and all that needs doing for
1018 -- the actual is to do the check for a non-static context.
1020 procedure Eval_Actual (N : Node_Id) is
1021 begin
1022 Check_Non_Static_Context (N);
1023 end Eval_Actual;
1025 --------------------
1026 -- Eval_Allocator --
1027 --------------------
1029 -- Allocators are never static, so all we have to do is to do the
1030 -- check for a non-static context if an expression is present.
1032 procedure Eval_Allocator (N : Node_Id) is
1033 Expr : constant Node_Id := Expression (N);
1035 begin
1036 if Nkind (Expr) = N_Qualified_Expression then
1037 Check_Non_Static_Context (Expression (Expr));
1038 end if;
1039 end Eval_Allocator;
1041 ------------------------
1042 -- Eval_Arithmetic_Op --
1043 ------------------------
1045 -- Arithmetic operations are static functions, so the result is static
1046 -- if both operands are static (RM 4.9(7), 4.9(20)).
1048 procedure Eval_Arithmetic_Op (N : Node_Id) is
1049 Left : constant Node_Id := Left_Opnd (N);
1050 Right : constant Node_Id := Right_Opnd (N);
1051 Ltype : constant Entity_Id := Etype (Left);
1052 Rtype : constant Entity_Id := Etype (Right);
1053 Stat : Boolean;
1054 Fold : Boolean;
1056 begin
1057 -- If not foldable we are done
1059 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1061 if not Fold then
1062 return;
1063 end if;
1065 -- Fold for cases where both operands are of integer type
1067 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
1068 declare
1069 Left_Int : constant Uint := Expr_Value (Left);
1070 Right_Int : constant Uint := Expr_Value (Right);
1071 Result : Uint;
1073 begin
1074 case Nkind (N) is
1076 when N_Op_Add =>
1077 Result := Left_Int + Right_Int;
1079 when N_Op_Subtract =>
1080 Result := Left_Int - Right_Int;
1082 when N_Op_Multiply =>
1083 if OK_Bits
1084 (N, UI_From_Int
1085 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
1086 then
1087 Result := Left_Int * Right_Int;
1088 else
1089 Result := Left_Int;
1090 end if;
1092 when N_Op_Divide =>
1094 -- The exception Constraint_Error is raised by integer
1095 -- division, rem and mod if the right operand is zero.
1097 if Right_Int = 0 then
1098 Apply_Compile_Time_Constraint_Error
1099 (N, "division by zero",
1100 CE_Divide_By_Zero,
1101 Warn => not Stat);
1102 return;
1104 else
1105 Result := Left_Int / Right_Int;
1106 end if;
1108 when N_Op_Mod =>
1110 -- The exception Constraint_Error is raised by integer
1111 -- division, rem and mod if the right operand is zero.
1113 if Right_Int = 0 then
1114 Apply_Compile_Time_Constraint_Error
1115 (N, "mod with zero divisor",
1116 CE_Divide_By_Zero,
1117 Warn => not Stat);
1118 return;
1119 else
1120 Result := Left_Int mod Right_Int;
1121 end if;
1123 when N_Op_Rem =>
1125 -- The exception Constraint_Error is raised by integer
1126 -- division, rem and mod if the right operand is zero.
1128 if Right_Int = 0 then
1129 Apply_Compile_Time_Constraint_Error
1130 (N, "rem with zero divisor",
1131 CE_Divide_By_Zero,
1132 Warn => not Stat);
1133 return;
1135 else
1136 Result := Left_Int rem Right_Int;
1137 end if;
1139 when others =>
1140 raise Program_Error;
1141 end case;
1143 -- Adjust the result by the modulus if the type is a modular type
1145 if Is_Modular_Integer_Type (Ltype) then
1146 Result := Result mod Modulus (Ltype);
1148 -- For a signed integer type, check non-static overflow
1150 elsif (not Stat) and then Is_Signed_Integer_Type (Ltype) then
1151 declare
1152 BT : constant Entity_Id := Base_Type (Ltype);
1153 Lo : constant Uint := Expr_Value (Type_Low_Bound (BT));
1154 Hi : constant Uint := Expr_Value (Type_High_Bound (BT));
1155 begin
1156 if Result < Lo or else Result > Hi then
1157 Apply_Compile_Time_Constraint_Error
1158 (N, "value not in range of }?",
1159 CE_Overflow_Check_Failed,
1160 Ent => BT);
1161 return;
1162 end if;
1163 end;
1164 end if;
1166 -- If we get here we can fold the result
1168 Fold_Uint (N, Result, Stat);
1169 end;
1171 -- Cases where at least one operand is a real. We handle the cases
1172 -- of both reals, or mixed/real integer cases (the latter happen
1173 -- only for divide and multiply, and the result is always real).
1175 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
1176 declare
1177 Left_Real : Ureal;
1178 Right_Real : Ureal;
1179 Result : Ureal;
1181 begin
1182 if Is_Real_Type (Ltype) then
1183 Left_Real := Expr_Value_R (Left);
1184 else
1185 Left_Real := UR_From_Uint (Expr_Value (Left));
1186 end if;
1188 if Is_Real_Type (Rtype) then
1189 Right_Real := Expr_Value_R (Right);
1190 else
1191 Right_Real := UR_From_Uint (Expr_Value (Right));
1192 end if;
1194 if Nkind (N) = N_Op_Add then
1195 Result := Left_Real + Right_Real;
1197 elsif Nkind (N) = N_Op_Subtract then
1198 Result := Left_Real - Right_Real;
1200 elsif Nkind (N) = N_Op_Multiply then
1201 Result := Left_Real * Right_Real;
1203 else pragma Assert (Nkind (N) = N_Op_Divide);
1204 if UR_Is_Zero (Right_Real) then
1205 Apply_Compile_Time_Constraint_Error
1206 (N, "division by zero", CE_Divide_By_Zero);
1207 return;
1208 end if;
1210 Result := Left_Real / Right_Real;
1211 end if;
1213 Fold_Ureal (N, Result, Stat);
1214 end;
1215 end if;
1216 end Eval_Arithmetic_Op;
1218 ----------------------------
1219 -- Eval_Character_Literal --
1220 ----------------------------
1222 -- Nothing to be done!
1224 procedure Eval_Character_Literal (N : Node_Id) is
1225 pragma Warnings (Off, N);
1226 begin
1227 null;
1228 end Eval_Character_Literal;
1230 ---------------
1231 -- Eval_Call --
1232 ---------------
1234 -- Static function calls are either calls to predefined operators
1235 -- with static arguments, or calls to functions that rename a literal.
1236 -- Only the latter case is handled here, predefined operators are
1237 -- constant-folded elsewhere.
1238 -- If the function is itself inherited (see 7423-001) the literal of
1239 -- the parent type must be explicitly converted to the return type
1240 -- of the function.
1242 procedure Eval_Call (N : Node_Id) is
1243 Loc : constant Source_Ptr := Sloc (N);
1244 Typ : constant Entity_Id := Etype (N);
1245 Lit : Entity_Id;
1247 begin
1248 if Nkind (N) = N_Function_Call
1249 and then No (Parameter_Associations (N))
1250 and then Is_Entity_Name (Name (N))
1251 and then Present (Alias (Entity (Name (N))))
1252 and then Is_Enumeration_Type (Base_Type (Typ))
1253 then
1254 Lit := Alias (Entity (Name (N)));
1256 while Present (Alias (Lit)) loop
1257 Lit := Alias (Lit);
1258 end loop;
1260 if Ekind (Lit) = E_Enumeration_Literal then
1261 if Base_Type (Etype (Lit)) /= Base_Type (Typ) then
1262 Rewrite
1263 (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc)));
1264 else
1265 Rewrite (N, New_Occurrence_Of (Lit, Loc));
1266 end if;
1268 Resolve (N, Typ);
1269 end if;
1270 end if;
1271 end Eval_Call;
1273 ------------------------
1274 -- Eval_Concatenation --
1275 ------------------------
1277 -- Concatenation is a static function, so the result is static if
1278 -- both operands are static (RM 4.9(7), 4.9(21)).
1280 procedure Eval_Concatenation (N : Node_Id) is
1281 Left : constant Node_Id := Left_Opnd (N);
1282 Right : constant Node_Id := Right_Opnd (N);
1283 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
1284 Stat : Boolean;
1285 Fold : Boolean;
1287 begin
1288 -- Concatenation is never static in Ada 83, so if Ada 83
1289 -- check operand non-static context
1291 if Ada_Version = Ada_83
1292 and then Comes_From_Source (N)
1293 then
1294 Check_Non_Static_Context (Left);
1295 Check_Non_Static_Context (Right);
1296 return;
1297 end if;
1299 -- If not foldable we are done. In principle concatenation that yields
1300 -- any string type is static (i.e. an array type of character types).
1301 -- However, character types can include enumeration literals, and
1302 -- concatenation in that case cannot be described by a literal, so we
1303 -- only consider the operation static if the result is an array of
1304 -- (a descendant of) a predefined character type.
1306 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1308 if (C_Typ = Standard_Character
1309 or else C_Typ = Standard_Wide_Character
1310 or else C_Typ = Standard_Wide_Wide_Character)
1311 and then Fold
1312 then
1313 null;
1314 else
1315 Set_Is_Static_Expression (N, False);
1316 return;
1317 end if;
1319 -- Compile time string concatenation
1321 -- ??? Note that operands that are aggregates can be marked as
1322 -- static, so we should attempt at a later stage to fold
1323 -- concatenations with such aggregates.
1325 declare
1326 Left_Str : constant Node_Id := Get_String_Val (Left);
1327 Left_Len : Nat;
1328 Right_Str : constant Node_Id := Get_String_Val (Right);
1330 begin
1331 -- Establish new string literal, and store left operand. We make
1332 -- sure to use the special Start_String that takes an operand if
1333 -- the left operand is a string literal. Since this is optimized
1334 -- in the case where that is the most recently created string
1335 -- literal, we ensure efficient time/space behavior for the
1336 -- case of a concatenation of a series of string literals.
1338 if Nkind (Left_Str) = N_String_Literal then
1339 Left_Len := String_Length (Strval (Left_Str));
1340 Start_String (Strval (Left_Str));
1341 else
1342 Start_String;
1343 Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str)));
1344 Left_Len := 1;
1345 end if;
1347 -- Now append the characters of the right operand
1349 if Nkind (Right_Str) = N_String_Literal then
1350 declare
1351 S : constant String_Id := Strval (Right_Str);
1353 begin
1354 for J in 1 .. String_Length (S) loop
1355 Store_String_Char (Get_String_Char (S, J));
1356 end loop;
1357 end;
1358 else
1359 Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str)));
1360 end if;
1362 Set_Is_Static_Expression (N, Stat);
1364 if Stat then
1366 -- If left operand is the empty string, the result is the
1367 -- right operand, including its bounds if anomalous.
1369 if Left_Len = 0
1370 and then Is_Array_Type (Etype (Right))
1371 and then Etype (Right) /= Any_String
1372 then
1373 Set_Etype (N, Etype (Right));
1374 end if;
1376 Fold_Str (N, End_String, True);
1377 end if;
1378 end;
1379 end Eval_Concatenation;
1381 ---------------------------------
1382 -- Eval_Conditional_Expression --
1383 ---------------------------------
1385 -- This GNAT internal construct can never be statically folded, so the
1386 -- only required processing is to do the check for non-static context
1387 -- for the two expression operands.
1389 procedure Eval_Conditional_Expression (N : Node_Id) is
1390 Condition : constant Node_Id := First (Expressions (N));
1391 Then_Expr : constant Node_Id := Next (Condition);
1392 Else_Expr : constant Node_Id := Next (Then_Expr);
1394 begin
1395 Check_Non_Static_Context (Then_Expr);
1396 Check_Non_Static_Context (Else_Expr);
1397 end Eval_Conditional_Expression;
1399 ----------------------
1400 -- Eval_Entity_Name --
1401 ----------------------
1403 -- This procedure is used for identifiers and expanded names other than
1404 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
1405 -- static if they denote a static constant (RM 4.9(6)) or if the name
1406 -- denotes an enumeration literal (RM 4.9(22)).
1408 procedure Eval_Entity_Name (N : Node_Id) is
1409 Def_Id : constant Entity_Id := Entity (N);
1410 Val : Node_Id;
1412 begin
1413 -- Enumeration literals are always considered to be constants
1414 -- and cannot raise constraint error (RM 4.9(22)).
1416 if Ekind (Def_Id) = E_Enumeration_Literal then
1417 Set_Is_Static_Expression (N);
1418 return;
1420 -- A name is static if it denotes a static constant (RM 4.9(5)), and
1421 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
1422 -- it does not violate 10.2.1(8) here, since this is not a variable.
1424 elsif Ekind (Def_Id) = E_Constant then
1426 -- Deferred constants must always be treated as nonstatic
1427 -- outside the scope of their full view.
1429 if Present (Full_View (Def_Id))
1430 and then not In_Open_Scopes (Scope (Def_Id))
1431 then
1432 Val := Empty;
1433 else
1434 Val := Constant_Value (Def_Id);
1435 end if;
1437 if Present (Val) then
1438 Set_Is_Static_Expression
1439 (N, Is_Static_Expression (Val)
1440 and then Is_Static_Subtype (Etype (Def_Id)));
1441 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
1443 if not Is_Static_Expression (N)
1444 and then not Is_Generic_Type (Etype (N))
1445 then
1446 Validate_Static_Object_Name (N);
1447 end if;
1449 return;
1450 end if;
1451 end if;
1453 -- Fall through if the name is not static
1455 Validate_Static_Object_Name (N);
1456 end Eval_Entity_Name;
1458 ----------------------------
1459 -- Eval_Indexed_Component --
1460 ----------------------------
1462 -- Indexed components are never static, so we need to perform the check
1463 -- for non-static context on the index values. Then, we check if the
1464 -- value can be obtained at compile time, even though it is non-static.
1466 procedure Eval_Indexed_Component (N : Node_Id) is
1467 Expr : Node_Id;
1469 begin
1470 -- Check for non-static context on index values
1472 Expr := First (Expressions (N));
1473 while Present (Expr) loop
1474 Check_Non_Static_Context (Expr);
1475 Next (Expr);
1476 end loop;
1478 -- If the indexed component appears in an object renaming declaration
1479 -- then we do not want to try to evaluate it, since in this case we
1480 -- need the identity of the array element.
1482 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
1483 return;
1485 -- Similarly if the indexed component appears as the prefix of an
1486 -- attribute we don't want to evaluate it, because at least for
1487 -- some cases of attributes we need the identify (e.g. Access, Size)
1489 elsif Nkind (Parent (N)) = N_Attribute_Reference then
1490 return;
1491 end if;
1493 -- Note: there are other cases, such as the left side of an assignment,
1494 -- or an OUT parameter for a call, where the replacement results in the
1495 -- illegal use of a constant, But these cases are illegal in the first
1496 -- place, so the replacement, though silly, is harmless.
1498 -- Now see if this is a constant array reference
1500 if List_Length (Expressions (N)) = 1
1501 and then Is_Entity_Name (Prefix (N))
1502 and then Ekind (Entity (Prefix (N))) = E_Constant
1503 and then Present (Constant_Value (Entity (Prefix (N))))
1504 then
1505 declare
1506 Loc : constant Source_Ptr := Sloc (N);
1507 Arr : constant Node_Id := Constant_Value (Entity (Prefix (N)));
1508 Sub : constant Node_Id := First (Expressions (N));
1510 Atyp : Entity_Id;
1511 -- Type of array
1513 Lin : Nat;
1514 -- Linear one's origin subscript value for array reference
1516 Lbd : Node_Id;
1517 -- Lower bound of the first array index
1519 Elm : Node_Id;
1520 -- Value from constant array
1522 begin
1523 Atyp := Etype (Arr);
1525 if Is_Access_Type (Atyp) then
1526 Atyp := Designated_Type (Atyp);
1527 end if;
1529 -- If we have an array type (we should have but perhaps there
1530 -- are error cases where this is not the case), then see if we
1531 -- can do a constant evaluation of the array reference.
1533 if Is_Array_Type (Atyp) then
1534 if Ekind (Atyp) = E_String_Literal_Subtype then
1535 Lbd := String_Literal_Low_Bound (Atyp);
1536 else
1537 Lbd := Type_Low_Bound (Etype (First_Index (Atyp)));
1538 end if;
1540 if Compile_Time_Known_Value (Sub)
1541 and then Nkind (Arr) = N_Aggregate
1542 and then Compile_Time_Known_Value (Lbd)
1543 and then Is_Discrete_Type (Component_Type (Atyp))
1544 then
1545 Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1;
1547 if List_Length (Expressions (Arr)) >= Lin then
1548 Elm := Pick (Expressions (Arr), Lin);
1550 -- If the resulting expression is compile time known,
1551 -- then we can rewrite the indexed component with this
1552 -- value, being sure to mark the result as non-static.
1553 -- We also reset the Sloc, in case this generates an
1554 -- error later on (e.g. 136'Access).
1556 if Compile_Time_Known_Value (Elm) then
1557 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
1558 Set_Is_Static_Expression (N, False);
1559 Set_Sloc (N, Loc);
1560 end if;
1561 end if;
1562 end if;
1563 end if;
1564 end;
1565 end if;
1566 end Eval_Indexed_Component;
1568 --------------------------
1569 -- Eval_Integer_Literal --
1570 --------------------------
1572 -- Numeric literals are static (RM 4.9(1)), and have already been marked
1573 -- as static by the analyzer. The reason we did it that early is to allow
1574 -- the possibility of turning off the Is_Static_Expression flag after
1575 -- analysis, but before resolution, when integer literals are generated
1576 -- in the expander that do not correspond to static expressions.
1578 procedure Eval_Integer_Literal (N : Node_Id) is
1579 T : constant Entity_Id := Etype (N);
1581 function In_Any_Integer_Context return Boolean;
1582 -- If the literal is resolved with a specific type in a context
1583 -- where the expected type is Any_Integer, there are no range checks
1584 -- on the literal. By the time the literal is evaluated, it carries
1585 -- the type imposed by the enclosing expression, and we must recover
1586 -- the context to determine that Any_Integer is meant.
1588 ----------------------------
1589 -- To_Any_Integer_Context --
1590 ----------------------------
1592 function In_Any_Integer_Context return Boolean is
1593 Par : constant Node_Id := Parent (N);
1594 K : constant Node_Kind := Nkind (Par);
1596 begin
1597 -- Any_Integer also appears in digits specifications for real types,
1598 -- but those have bounds smaller that those of any integer base
1599 -- type, so we can safely ignore these cases.
1601 return K = N_Number_Declaration
1602 or else K = N_Attribute_Reference
1603 or else K = N_Attribute_Definition_Clause
1604 or else K = N_Modular_Type_Definition
1605 or else K = N_Signed_Integer_Type_Definition;
1606 end In_Any_Integer_Context;
1608 -- Start of processing for Eval_Integer_Literal
1610 begin
1612 -- If the literal appears in a non-expression context, then it is
1613 -- certainly appearing in a non-static context, so check it. This
1614 -- is actually a redundant check, since Check_Non_Static_Context
1615 -- would check it, but it seems worth while avoiding the call.
1617 if Nkind (Parent (N)) not in N_Subexpr
1618 and then not In_Any_Integer_Context
1619 then
1620 Check_Non_Static_Context (N);
1621 end if;
1623 -- Modular integer literals must be in their base range
1625 if Is_Modular_Integer_Type (T)
1626 and then Is_Out_Of_Range (N, Base_Type (T))
1627 then
1628 Out_Of_Range (N);
1629 end if;
1630 end Eval_Integer_Literal;
1632 ---------------------
1633 -- Eval_Logical_Op --
1634 ---------------------
1636 -- Logical operations are static functions, so the result is potentially
1637 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
1639 procedure Eval_Logical_Op (N : Node_Id) is
1640 Left : constant Node_Id := Left_Opnd (N);
1641 Right : constant Node_Id := Right_Opnd (N);
1642 Stat : Boolean;
1643 Fold : Boolean;
1645 begin
1646 -- If not foldable we are done
1648 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1650 if not Fold then
1651 return;
1652 end if;
1654 -- Compile time evaluation of logical operation
1656 declare
1657 Left_Int : constant Uint := Expr_Value (Left);
1658 Right_Int : constant Uint := Expr_Value (Right);
1660 begin
1661 if Is_Modular_Integer_Type (Etype (N)) then
1662 declare
1663 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
1664 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
1666 begin
1667 To_Bits (Left_Int, Left_Bits);
1668 To_Bits (Right_Int, Right_Bits);
1670 -- Note: should really be able to use array ops instead of
1671 -- these loops, but they weren't working at the time ???
1673 if Nkind (N) = N_Op_And then
1674 for J in Left_Bits'Range loop
1675 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
1676 end loop;
1678 elsif Nkind (N) = N_Op_Or then
1679 for J in Left_Bits'Range loop
1680 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
1681 end loop;
1683 else
1684 pragma Assert (Nkind (N) = N_Op_Xor);
1686 for J in Left_Bits'Range loop
1687 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
1688 end loop;
1689 end if;
1691 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat);
1692 end;
1694 else
1695 pragma Assert (Is_Boolean_Type (Etype (N)));
1697 if Nkind (N) = N_Op_And then
1698 Fold_Uint (N,
1699 Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat);
1701 elsif Nkind (N) = N_Op_Or then
1702 Fold_Uint (N,
1703 Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat);
1705 else
1706 pragma Assert (Nkind (N) = N_Op_Xor);
1707 Fold_Uint (N,
1708 Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat);
1709 end if;
1710 end if;
1711 end;
1712 end Eval_Logical_Op;
1714 ------------------------
1715 -- Eval_Membership_Op --
1716 ------------------------
1718 -- A membership test is potentially static if the expression is static,
1719 -- and the range is a potentially static range, or is a subtype mark
1720 -- denoting a static subtype (RM 4.9(12)).
1722 procedure Eval_Membership_Op (N : Node_Id) is
1723 Left : constant Node_Id := Left_Opnd (N);
1724 Right : constant Node_Id := Right_Opnd (N);
1725 Def_Id : Entity_Id;
1726 Lo : Node_Id;
1727 Hi : Node_Id;
1728 Result : Boolean;
1729 Stat : Boolean;
1730 Fold : Boolean;
1732 begin
1733 -- Ignore if error in either operand, except to make sure that
1734 -- Any_Type is properly propagated to avoid junk cascaded errors.
1736 if Etype (Left) = Any_Type
1737 or else Etype (Right) = Any_Type
1738 then
1739 Set_Etype (N, Any_Type);
1740 return;
1741 end if;
1743 -- Case of right operand is a subtype name
1745 if Is_Entity_Name (Right) then
1746 Def_Id := Entity (Right);
1748 if (Is_Scalar_Type (Def_Id) or else Is_String_Type (Def_Id))
1749 and then Is_OK_Static_Subtype (Def_Id)
1750 then
1751 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
1753 if not Fold or else not Stat then
1754 return;
1755 end if;
1756 else
1757 Check_Non_Static_Context (Left);
1758 return;
1759 end if;
1761 -- For string membership tests we will check the length
1762 -- further below.
1764 if not Is_String_Type (Def_Id) then
1765 Lo := Type_Low_Bound (Def_Id);
1766 Hi := Type_High_Bound (Def_Id);
1768 else
1769 Lo := Empty;
1770 Hi := Empty;
1771 end if;
1773 -- Case of right operand is a range
1775 else
1776 if Is_Static_Range (Right) then
1777 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
1779 if not Fold or else not Stat then
1780 return;
1782 -- If one bound of range raises CE, then don't try to fold
1784 elsif not Is_OK_Static_Range (Right) then
1785 Check_Non_Static_Context (Left);
1786 return;
1787 end if;
1789 else
1790 Check_Non_Static_Context (Left);
1791 return;
1792 end if;
1794 -- Here we know range is an OK static range
1796 Lo := Low_Bound (Right);
1797 Hi := High_Bound (Right);
1798 end if;
1800 -- For strings we check that the length of the string expression is
1801 -- compatible with the string subtype if the subtype is constrained,
1802 -- or if unconstrained then the test is always true.
1804 if Is_String_Type (Etype (Right)) then
1805 if not Is_Constrained (Etype (Right)) then
1806 Result := True;
1808 else
1809 declare
1810 Typlen : constant Uint := String_Type_Len (Etype (Right));
1811 Strlen : constant Uint :=
1812 UI_From_Int (String_Length (Strval (Get_String_Val (Left))));
1813 begin
1814 Result := (Typlen = Strlen);
1815 end;
1816 end if;
1818 -- Fold the membership test. We know we have a static range and Lo
1819 -- and Hi are set to the expressions for the end points of this range.
1821 elsif Is_Real_Type (Etype (Right)) then
1822 declare
1823 Leftval : constant Ureal := Expr_Value_R (Left);
1825 begin
1826 Result := Expr_Value_R (Lo) <= Leftval
1827 and then Leftval <= Expr_Value_R (Hi);
1828 end;
1830 else
1831 declare
1832 Leftval : constant Uint := Expr_Value (Left);
1834 begin
1835 Result := Expr_Value (Lo) <= Leftval
1836 and then Leftval <= Expr_Value (Hi);
1837 end;
1838 end if;
1840 if Nkind (N) = N_Not_In then
1841 Result := not Result;
1842 end if;
1844 Fold_Uint (N, Test (Result), True);
1845 Warn_On_Known_Condition (N);
1846 end Eval_Membership_Op;
1848 ------------------------
1849 -- Eval_Named_Integer --
1850 ------------------------
1852 procedure Eval_Named_Integer (N : Node_Id) is
1853 begin
1854 Fold_Uint (N,
1855 Expr_Value (Expression (Declaration_Node (Entity (N)))), True);
1856 end Eval_Named_Integer;
1858 ---------------------
1859 -- Eval_Named_Real --
1860 ---------------------
1862 procedure Eval_Named_Real (N : Node_Id) is
1863 begin
1864 Fold_Ureal (N,
1865 Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True);
1866 end Eval_Named_Real;
1868 -------------------
1869 -- Eval_Op_Expon --
1870 -------------------
1872 -- Exponentiation is a static functions, so the result is potentially
1873 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
1875 procedure Eval_Op_Expon (N : Node_Id) is
1876 Left : constant Node_Id := Left_Opnd (N);
1877 Right : constant Node_Id := Right_Opnd (N);
1878 Stat : Boolean;
1879 Fold : Boolean;
1881 begin
1882 -- If not foldable we are done
1884 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1886 if not Fold then
1887 return;
1888 end if;
1890 -- Fold exponentiation operation
1892 declare
1893 Right_Int : constant Uint := Expr_Value (Right);
1895 begin
1896 -- Integer case
1898 if Is_Integer_Type (Etype (Left)) then
1899 declare
1900 Left_Int : constant Uint := Expr_Value (Left);
1901 Result : Uint;
1903 begin
1904 -- Exponentiation of an integer raises the exception
1905 -- Constraint_Error for a negative exponent (RM 4.5.6)
1907 if Right_Int < 0 then
1908 Apply_Compile_Time_Constraint_Error
1909 (N, "integer exponent negative",
1910 CE_Range_Check_Failed,
1911 Warn => not Stat);
1912 return;
1914 else
1915 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
1916 Result := Left_Int ** Right_Int;
1917 else
1918 Result := Left_Int;
1919 end if;
1921 if Is_Modular_Integer_Type (Etype (N)) then
1922 Result := Result mod Modulus (Etype (N));
1923 end if;
1925 Fold_Uint (N, Result, Stat);
1926 end if;
1927 end;
1929 -- Real case
1931 else
1932 declare
1933 Left_Real : constant Ureal := Expr_Value_R (Left);
1935 begin
1936 -- Cannot have a zero base with a negative exponent
1938 if UR_Is_Zero (Left_Real) then
1940 if Right_Int < 0 then
1941 Apply_Compile_Time_Constraint_Error
1942 (N, "zero ** negative integer",
1943 CE_Range_Check_Failed,
1944 Warn => not Stat);
1945 return;
1946 else
1947 Fold_Ureal (N, Ureal_0, Stat);
1948 end if;
1950 else
1951 Fold_Ureal (N, Left_Real ** Right_Int, Stat);
1952 end if;
1953 end;
1954 end if;
1955 end;
1956 end Eval_Op_Expon;
1958 -----------------
1959 -- Eval_Op_Not --
1960 -----------------
1962 -- The not operation is a static functions, so the result is potentially
1963 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
1965 procedure Eval_Op_Not (N : Node_Id) is
1966 Right : constant Node_Id := Right_Opnd (N);
1967 Stat : Boolean;
1968 Fold : Boolean;
1970 begin
1971 -- If not foldable we are done
1973 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
1975 if not Fold then
1976 return;
1977 end if;
1979 -- Fold not operation
1981 declare
1982 Rint : constant Uint := Expr_Value (Right);
1983 Typ : constant Entity_Id := Etype (N);
1985 begin
1986 -- Negation is equivalent to subtracting from the modulus minus
1987 -- one. For a binary modulus this is equivalent to the ones-
1988 -- component of the original value. For non-binary modulus this
1989 -- is an arbitrary but consistent definition.
1991 if Is_Modular_Integer_Type (Typ) then
1992 Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat);
1994 else
1995 pragma Assert (Is_Boolean_Type (Typ));
1996 Fold_Uint (N, Test (not Is_True (Rint)), Stat);
1997 end if;
1999 Set_Is_Static_Expression (N, Stat);
2000 end;
2001 end Eval_Op_Not;
2003 -------------------------------
2004 -- Eval_Qualified_Expression --
2005 -------------------------------
2007 -- A qualified expression is potentially static if its subtype mark denotes
2008 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
2010 procedure Eval_Qualified_Expression (N : Node_Id) is
2011 Operand : constant Node_Id := Expression (N);
2012 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
2014 Stat : Boolean;
2015 Fold : Boolean;
2016 Hex : Boolean;
2018 begin
2019 -- Can only fold if target is string or scalar and subtype is static
2020 -- Also, do not fold if our parent is an allocator (this is because
2021 -- the qualified expression is really part of the syntactic structure
2022 -- of an allocator, and we do not want to end up with something that
2023 -- corresponds to "new 1" where the 1 is the result of folding a
2024 -- qualified expression).
2026 if not Is_Static_Subtype (Target_Type)
2027 or else Nkind (Parent (N)) = N_Allocator
2028 then
2029 Check_Non_Static_Context (Operand);
2031 -- If operand is known to raise constraint_error, set the
2032 -- flag on the expression so it does not get optimized away.
2034 if Nkind (Operand) = N_Raise_Constraint_Error then
2035 Set_Raises_Constraint_Error (N);
2036 end if;
2038 return;
2039 end if;
2041 -- If not foldable we are done
2043 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
2045 if not Fold then
2046 return;
2048 -- Don't try fold if target type has constraint error bounds
2050 elsif not Is_OK_Static_Subtype (Target_Type) then
2051 Set_Raises_Constraint_Error (N);
2052 return;
2053 end if;
2055 -- Here we will fold, save Print_In_Hex indication
2057 Hex := Nkind (Operand) = N_Integer_Literal
2058 and then Print_In_Hex (Operand);
2060 -- Fold the result of qualification
2062 if Is_Discrete_Type (Target_Type) then
2063 Fold_Uint (N, Expr_Value (Operand), Stat);
2065 -- Preserve Print_In_Hex indication
2067 if Hex and then Nkind (N) = N_Integer_Literal then
2068 Set_Print_In_Hex (N);
2069 end if;
2071 elsif Is_Real_Type (Target_Type) then
2072 Fold_Ureal (N, Expr_Value_R (Operand), Stat);
2074 else
2075 Fold_Str (N, Strval (Get_String_Val (Operand)), Stat);
2077 if not Stat then
2078 Set_Is_Static_Expression (N, False);
2079 else
2080 Check_String_Literal_Length (N, Target_Type);
2081 end if;
2083 return;
2084 end if;
2086 -- The expression may be foldable but not static
2088 Set_Is_Static_Expression (N, Stat);
2090 if Is_Out_Of_Range (N, Etype (N)) then
2091 Out_Of_Range (N);
2092 end if;
2093 end Eval_Qualified_Expression;
2095 -----------------------
2096 -- Eval_Real_Literal --
2097 -----------------------
2099 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2100 -- as static by the analyzer. The reason we did it that early is to allow
2101 -- the possibility of turning off the Is_Static_Expression flag after
2102 -- analysis, but before resolution, when integer literals are generated
2103 -- in the expander that do not correspond to static expressions.
2105 procedure Eval_Real_Literal (N : Node_Id) is
2106 begin
2107 -- If the literal appears in a non-expression context, then it is
2108 -- certainly appearing in a non-static context, so check it.
2110 if Nkind (Parent (N)) not in N_Subexpr then
2111 Check_Non_Static_Context (N);
2112 end if;
2114 end Eval_Real_Literal;
2116 ------------------------
2117 -- Eval_Relational_Op --
2118 ------------------------
2120 -- Relational operations are static functions, so the result is static
2121 -- if both operands are static (RM 4.9(7), 4.9(20)).
2123 procedure Eval_Relational_Op (N : Node_Id) is
2124 Left : constant Node_Id := Left_Opnd (N);
2125 Right : constant Node_Id := Right_Opnd (N);
2126 Typ : constant Entity_Id := Etype (Left);
2127 Result : Boolean;
2128 Stat : Boolean;
2129 Fold : Boolean;
2131 begin
2132 -- One special case to deal with first. If we can tell that
2133 -- the result will be false because the lengths of one or
2134 -- more index subtypes are compile time known and different,
2135 -- then we can replace the entire result by False. We only
2136 -- do this for one dimensional arrays, because the case of
2137 -- multi-dimensional arrays is rare and too much trouble!
2139 if Is_Array_Type (Typ)
2140 and then Number_Dimensions (Typ) = 1
2141 and then (Nkind (N) = N_Op_Eq
2142 or else Nkind (N) = N_Op_Ne)
2143 then
2144 if Raises_Constraint_Error (Left)
2145 or else Raises_Constraint_Error (Right)
2146 then
2147 return;
2148 end if;
2150 declare
2151 procedure Get_Static_Length (Op : Node_Id; Len : out Uint);
2152 -- If Op is an expression for a constrained array with a
2153 -- known at compile time length, then Len is set to this
2154 -- (non-negative length). Otherwise Len is set to minus 1.
2156 -----------------------
2157 -- Get_Static_Length --
2158 -----------------------
2160 procedure Get_Static_Length (Op : Node_Id; Len : out Uint) is
2161 T : Entity_Id;
2163 begin
2164 if Nkind (Op) = N_String_Literal then
2165 Len := UI_From_Int (String_Length (Strval (Op)));
2167 elsif not Is_Constrained (Etype (Op)) then
2168 Len := Uint_Minus_1;
2170 else
2171 T := Etype (First_Index (Etype (Op)));
2173 if Is_Discrete_Type (T)
2174 and then
2175 Compile_Time_Known_Value (Type_Low_Bound (T))
2176 and then
2177 Compile_Time_Known_Value (Type_High_Bound (T))
2178 then
2179 Len := UI_Max (Uint_0,
2180 Expr_Value (Type_High_Bound (T)) -
2181 Expr_Value (Type_Low_Bound (T)) + 1);
2182 else
2183 Len := Uint_Minus_1;
2184 end if;
2185 end if;
2186 end Get_Static_Length;
2188 Len_L : Uint;
2189 Len_R : Uint;
2191 begin
2192 Get_Static_Length (Left, Len_L);
2193 Get_Static_Length (Right, Len_R);
2195 if Len_L /= Uint_Minus_1
2196 and then Len_R /= Uint_Minus_1
2197 and then Len_L /= Len_R
2198 then
2199 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
2200 Warn_On_Known_Condition (N);
2201 return;
2202 end if;
2203 end;
2205 -- Another special case: comparisons of access types, where one or both
2206 -- operands are known to be null, so the result can be determined.
2208 elsif Is_Access_Type (Typ) then
2209 if Known_Null (Left) then
2210 if Known_Null (Right) then
2211 Fold_Uint (N, Test (Nkind (N) = N_Op_Eq), False);
2212 Warn_On_Known_Condition (N);
2213 return;
2215 elsif Known_Non_Null (Right) then
2216 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
2217 Warn_On_Known_Condition (N);
2218 return;
2219 end if;
2221 elsif Known_Non_Null (Left) then
2222 if Known_Null (Right) then
2223 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
2224 Warn_On_Known_Condition (N);
2225 return;
2226 end if;
2227 end if;
2228 end if;
2230 -- Can only fold if type is scalar (don't fold string ops)
2232 if not Is_Scalar_Type (Typ) then
2233 Check_Non_Static_Context (Left);
2234 Check_Non_Static_Context (Right);
2235 return;
2236 end if;
2238 -- If not foldable we are done
2240 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2242 if not Fold then
2243 return;
2244 end if;
2246 -- Integer and Enumeration (discrete) type cases
2248 if Is_Discrete_Type (Typ) then
2249 declare
2250 Left_Int : constant Uint := Expr_Value (Left);
2251 Right_Int : constant Uint := Expr_Value (Right);
2253 begin
2254 case Nkind (N) is
2255 when N_Op_Eq => Result := Left_Int = Right_Int;
2256 when N_Op_Ne => Result := Left_Int /= Right_Int;
2257 when N_Op_Lt => Result := Left_Int < Right_Int;
2258 when N_Op_Le => Result := Left_Int <= Right_Int;
2259 when N_Op_Gt => Result := Left_Int > Right_Int;
2260 when N_Op_Ge => Result := Left_Int >= Right_Int;
2262 when others =>
2263 raise Program_Error;
2264 end case;
2266 Fold_Uint (N, Test (Result), Stat);
2267 end;
2269 -- Real type case
2271 else
2272 pragma Assert (Is_Real_Type (Typ));
2274 declare
2275 Left_Real : constant Ureal := Expr_Value_R (Left);
2276 Right_Real : constant Ureal := Expr_Value_R (Right);
2278 begin
2279 case Nkind (N) is
2280 when N_Op_Eq => Result := (Left_Real = Right_Real);
2281 when N_Op_Ne => Result := (Left_Real /= Right_Real);
2282 when N_Op_Lt => Result := (Left_Real < Right_Real);
2283 when N_Op_Le => Result := (Left_Real <= Right_Real);
2284 when N_Op_Gt => Result := (Left_Real > Right_Real);
2285 when N_Op_Ge => Result := (Left_Real >= Right_Real);
2287 when others =>
2288 raise Program_Error;
2289 end case;
2291 Fold_Uint (N, Test (Result), Stat);
2292 end;
2293 end if;
2295 Warn_On_Known_Condition (N);
2296 end Eval_Relational_Op;
2298 ----------------
2299 -- Eval_Shift --
2300 ----------------
2302 -- Shift operations are intrinsic operations that can never be static,
2303 -- so the only processing required is to perform the required check for
2304 -- a non static context for the two operands.
2306 -- Actually we could do some compile time evaluation here some time ???
2308 procedure Eval_Shift (N : Node_Id) is
2309 begin
2310 Check_Non_Static_Context (Left_Opnd (N));
2311 Check_Non_Static_Context (Right_Opnd (N));
2312 end Eval_Shift;
2314 ------------------------
2315 -- Eval_Short_Circuit --
2316 ------------------------
2318 -- A short circuit operation is potentially static if both operands
2319 -- are potentially static (RM 4.9 (13))
2321 procedure Eval_Short_Circuit (N : Node_Id) is
2322 Kind : constant Node_Kind := Nkind (N);
2323 Left : constant Node_Id := Left_Opnd (N);
2324 Right : constant Node_Id := Right_Opnd (N);
2325 Left_Int : Uint;
2326 Rstat : constant Boolean :=
2327 Is_Static_Expression (Left)
2328 and then Is_Static_Expression (Right);
2330 begin
2331 -- Short circuit operations are never static in Ada 83
2333 if Ada_Version = Ada_83
2334 and then Comes_From_Source (N)
2335 then
2336 Check_Non_Static_Context (Left);
2337 Check_Non_Static_Context (Right);
2338 return;
2339 end if;
2341 -- Now look at the operands, we can't quite use the normal call to
2342 -- Test_Expression_Is_Foldable here because short circuit operations
2343 -- are a special case, they can still be foldable, even if the right
2344 -- operand raises constraint error.
2346 -- If either operand is Any_Type, just propagate to result and
2347 -- do not try to fold, this prevents cascaded errors.
2349 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
2350 Set_Etype (N, Any_Type);
2351 return;
2353 -- If left operand raises constraint error, then replace node N with
2354 -- the raise constraint error node, and we are obviously not foldable.
2355 -- Is_Static_Expression is set from the two operands in the normal way,
2356 -- and we check the right operand if it is in a non-static context.
2358 elsif Raises_Constraint_Error (Left) then
2359 if not Rstat then
2360 Check_Non_Static_Context (Right);
2361 end if;
2363 Rewrite_In_Raise_CE (N, Left);
2364 Set_Is_Static_Expression (N, Rstat);
2365 return;
2367 -- If the result is not static, then we won't in any case fold
2369 elsif not Rstat then
2370 Check_Non_Static_Context (Left);
2371 Check_Non_Static_Context (Right);
2372 return;
2373 end if;
2375 -- Here the result is static, note that, unlike the normal processing
2376 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
2377 -- the right operand raises constraint error, that's because it is not
2378 -- significant if the left operand is decisive.
2380 Set_Is_Static_Expression (N);
2382 -- It does not matter if the right operand raises constraint error if
2383 -- it will not be evaluated. So deal specially with the cases where
2384 -- the right operand is not evaluated. Note that we will fold these
2385 -- cases even if the right operand is non-static, which is fine, but
2386 -- of course in these cases the result is not potentially static.
2388 Left_Int := Expr_Value (Left);
2390 if (Kind = N_And_Then and then Is_False (Left_Int))
2391 or else (Kind = N_Or_Else and Is_True (Left_Int))
2392 then
2393 Fold_Uint (N, Left_Int, Rstat);
2394 return;
2395 end if;
2397 -- If first operand not decisive, then it does matter if the right
2398 -- operand raises constraint error, since it will be evaluated, so
2399 -- we simply replace the node with the right operand. Note that this
2400 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
2401 -- (both are set to True in Right).
2403 if Raises_Constraint_Error (Right) then
2404 Rewrite_In_Raise_CE (N, Right);
2405 Check_Non_Static_Context (Left);
2406 return;
2407 end if;
2409 -- Otherwise the result depends on the right operand
2411 Fold_Uint (N, Expr_Value (Right), Rstat);
2412 return;
2413 end Eval_Short_Circuit;
2415 ----------------
2416 -- Eval_Slice --
2417 ----------------
2419 -- Slices can never be static, so the only processing required is to
2420 -- check for non-static context if an explicit range is given.
2422 procedure Eval_Slice (N : Node_Id) is
2423 Drange : constant Node_Id := Discrete_Range (N);
2425 begin
2426 if Nkind (Drange) = N_Range then
2427 Check_Non_Static_Context (Low_Bound (Drange));
2428 Check_Non_Static_Context (High_Bound (Drange));
2429 end if;
2430 end Eval_Slice;
2432 -------------------------
2433 -- Eval_String_Literal --
2434 -------------------------
2436 procedure Eval_String_Literal (N : Node_Id) is
2437 Typ : constant Entity_Id := Etype (N);
2438 Bas : constant Entity_Id := Base_Type (Typ);
2439 Xtp : Entity_Id;
2440 Len : Nat;
2441 Lo : Node_Id;
2443 begin
2444 -- Nothing to do if error type (handles cases like default expressions
2445 -- or generics where we have not yet fully resolved the type)
2447 if Bas = Any_Type or else Bas = Any_String then
2448 return;
2449 end if;
2451 -- String literals are static if the subtype is static (RM 4.9(2)), so
2452 -- reset the static expression flag (it was set unconditionally in
2453 -- Analyze_String_Literal) if the subtype is non-static. We tell if
2454 -- the subtype is static by looking at the lower bound.
2456 if Ekind (Typ) = E_String_Literal_Subtype then
2457 if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then
2458 Set_Is_Static_Expression (N, False);
2459 return;
2460 end if;
2462 -- Here if Etype of string literal is normal Etype (not yet possible,
2463 -- but may be possible in future!)
2465 elsif not Is_OK_Static_Expression
2466 (Type_Low_Bound (Etype (First_Index (Typ))))
2467 then
2468 Set_Is_Static_Expression (N, False);
2469 return;
2470 end if;
2472 -- If original node was a type conversion, then result if non-static
2474 if Nkind (Original_Node (N)) = N_Type_Conversion then
2475 Set_Is_Static_Expression (N, False);
2476 return;
2477 end if;
2479 -- Test for illegal Ada 95 cases. A string literal is illegal in
2480 -- Ada 95 if its bounds are outside the index base type and this
2481 -- index type is static. This can happen in only two ways. Either
2482 -- the string literal is too long, or it is null, and the lower
2483 -- bound is type'First. In either case it is the upper bound that
2484 -- is out of range of the index type.
2486 if Ada_Version >= Ada_95 then
2487 if Root_Type (Bas) = Standard_String
2488 or else
2489 Root_Type (Bas) = Standard_Wide_String
2490 then
2491 Xtp := Standard_Positive;
2492 else
2493 Xtp := Etype (First_Index (Bas));
2494 end if;
2496 if Ekind (Typ) = E_String_Literal_Subtype then
2497 Lo := String_Literal_Low_Bound (Typ);
2498 else
2499 Lo := Type_Low_Bound (Etype (First_Index (Typ)));
2500 end if;
2502 Len := String_Length (Strval (N));
2504 if UI_From_Int (Len) > String_Type_Len (Bas) then
2505 Apply_Compile_Time_Constraint_Error
2506 (N, "string literal too long for}", CE_Length_Check_Failed,
2507 Ent => Bas,
2508 Typ => First_Subtype (Bas));
2510 elsif Len = 0
2511 and then not Is_Generic_Type (Xtp)
2512 and then
2513 Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp)))
2514 then
2515 Apply_Compile_Time_Constraint_Error
2516 (N, "null string literal not allowed for}",
2517 CE_Length_Check_Failed,
2518 Ent => Bas,
2519 Typ => First_Subtype (Bas));
2520 end if;
2521 end if;
2522 end Eval_String_Literal;
2524 --------------------------
2525 -- Eval_Type_Conversion --
2526 --------------------------
2528 -- A type conversion is potentially static if its subtype mark is for a
2529 -- static scalar subtype, and its operand expression is potentially static
2530 -- (RM 4.9 (10))
2532 procedure Eval_Type_Conversion (N : Node_Id) is
2533 Operand : constant Node_Id := Expression (N);
2534 Source_Type : constant Entity_Id := Etype (Operand);
2535 Target_Type : constant Entity_Id := Etype (N);
2537 Stat : Boolean;
2538 Fold : Boolean;
2540 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
2541 -- Returns true if type T is an integer type, or if it is a
2542 -- fixed-point type to be treated as an integer (i.e. the flag
2543 -- Conversion_OK is set on the conversion node).
2545 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
2546 -- Returns true if type T is a floating-point type, or if it is a
2547 -- fixed-point type that is not to be treated as an integer (i.e. the
2548 -- flag Conversion_OK is not set on the conversion node).
2550 ------------------------------
2551 -- To_Be_Treated_As_Integer --
2552 ------------------------------
2554 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
2555 begin
2556 return
2557 Is_Integer_Type (T)
2558 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
2559 end To_Be_Treated_As_Integer;
2561 ---------------------------
2562 -- To_Be_Treated_As_Real --
2563 ---------------------------
2565 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
2566 begin
2567 return
2568 Is_Floating_Point_Type (T)
2569 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
2570 end To_Be_Treated_As_Real;
2572 -- Start of processing for Eval_Type_Conversion
2574 begin
2575 -- Cannot fold if target type is non-static or if semantic error
2577 if not Is_Static_Subtype (Target_Type) then
2578 Check_Non_Static_Context (Operand);
2579 return;
2581 elsif Error_Posted (N) then
2582 return;
2583 end if;
2585 -- If not foldable we are done
2587 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
2589 if not Fold then
2590 return;
2592 -- Don't try fold if target type has constraint error bounds
2594 elsif not Is_OK_Static_Subtype (Target_Type) then
2595 Set_Raises_Constraint_Error (N);
2596 return;
2597 end if;
2599 -- Remaining processing depends on operand types. Note that in the
2600 -- following type test, fixed-point counts as real unless the flag
2601 -- Conversion_OK is set, in which case it counts as integer.
2603 -- Fold conversion, case of string type. The result is not static
2605 if Is_String_Type (Target_Type) then
2606 Fold_Str (N, Strval (Get_String_Val (Operand)), False);
2608 return;
2610 -- Fold conversion, case of integer target type
2612 elsif To_Be_Treated_As_Integer (Target_Type) then
2613 declare
2614 Result : Uint;
2616 begin
2617 -- Integer to integer conversion
2619 if To_Be_Treated_As_Integer (Source_Type) then
2620 Result := Expr_Value (Operand);
2622 -- Real to integer conversion
2624 else
2625 Result := UR_To_Uint (Expr_Value_R (Operand));
2626 end if;
2628 -- If fixed-point type (Conversion_OK must be set), then the
2629 -- result is logically an integer, but we must replace the
2630 -- conversion with the corresponding real literal, since the
2631 -- type from a semantic point of view is still fixed-point.
2633 if Is_Fixed_Point_Type (Target_Type) then
2634 Fold_Ureal
2635 (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat);
2637 -- Otherwise result is integer literal
2639 else
2640 Fold_Uint (N, Result, Stat);
2641 end if;
2642 end;
2644 -- Fold conversion, case of real target type
2646 elsif To_Be_Treated_As_Real (Target_Type) then
2647 declare
2648 Result : Ureal;
2650 begin
2651 if To_Be_Treated_As_Real (Source_Type) then
2652 Result := Expr_Value_R (Operand);
2653 else
2654 Result := UR_From_Uint (Expr_Value (Operand));
2655 end if;
2657 Fold_Ureal (N, Result, Stat);
2658 end;
2660 -- Enumeration types
2662 else
2663 Fold_Uint (N, Expr_Value (Operand), Stat);
2664 end if;
2666 if Is_Out_Of_Range (N, Etype (N)) then
2667 Out_Of_Range (N);
2668 end if;
2670 end Eval_Type_Conversion;
2672 -------------------
2673 -- Eval_Unary_Op --
2674 -------------------
2676 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
2677 -- are potentially static if the operand is potentially static (RM 4.9(7))
2679 procedure Eval_Unary_Op (N : Node_Id) is
2680 Right : constant Node_Id := Right_Opnd (N);
2681 Stat : Boolean;
2682 Fold : Boolean;
2684 begin
2685 -- If not foldable we are done
2687 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
2689 if not Fold then
2690 return;
2691 end if;
2693 -- Fold for integer case
2695 if Is_Integer_Type (Etype (N)) then
2696 declare
2697 Rint : constant Uint := Expr_Value (Right);
2698 Result : Uint;
2700 begin
2701 -- In the case of modular unary plus and abs there is no need
2702 -- to adjust the result of the operation since if the original
2703 -- operand was in bounds the result will be in the bounds of the
2704 -- modular type. However, in the case of modular unary minus the
2705 -- result may go out of the bounds of the modular type and needs
2706 -- adjustment.
2708 if Nkind (N) = N_Op_Plus then
2709 Result := Rint;
2711 elsif Nkind (N) = N_Op_Minus then
2712 if Is_Modular_Integer_Type (Etype (N)) then
2713 Result := (-Rint) mod Modulus (Etype (N));
2714 else
2715 Result := (-Rint);
2716 end if;
2718 else
2719 pragma Assert (Nkind (N) = N_Op_Abs);
2720 Result := abs Rint;
2721 end if;
2723 Fold_Uint (N, Result, Stat);
2724 end;
2726 -- Fold for real case
2728 elsif Is_Real_Type (Etype (N)) then
2729 declare
2730 Rreal : constant Ureal := Expr_Value_R (Right);
2731 Result : Ureal;
2733 begin
2734 if Nkind (N) = N_Op_Plus then
2735 Result := Rreal;
2737 elsif Nkind (N) = N_Op_Minus then
2738 Result := UR_Negate (Rreal);
2740 else
2741 pragma Assert (Nkind (N) = N_Op_Abs);
2742 Result := abs Rreal;
2743 end if;
2745 Fold_Ureal (N, Result, Stat);
2746 end;
2747 end if;
2748 end Eval_Unary_Op;
2750 -------------------------------
2751 -- Eval_Unchecked_Conversion --
2752 -------------------------------
2754 -- Unchecked conversions can never be static, so the only required
2755 -- processing is to check for a non-static context for the operand.
2757 procedure Eval_Unchecked_Conversion (N : Node_Id) is
2758 begin
2759 Check_Non_Static_Context (Expression (N));
2760 end Eval_Unchecked_Conversion;
2762 --------------------
2763 -- Expr_Rep_Value --
2764 --------------------
2766 function Expr_Rep_Value (N : Node_Id) return Uint is
2767 Kind : constant Node_Kind := Nkind (N);
2768 Ent : Entity_Id;
2770 begin
2771 if Is_Entity_Name (N) then
2772 Ent := Entity (N);
2774 -- An enumeration literal that was either in the source or
2775 -- created as a result of static evaluation.
2777 if Ekind (Ent) = E_Enumeration_Literal then
2778 return Enumeration_Rep (Ent);
2780 -- A user defined static constant
2782 else
2783 pragma Assert (Ekind (Ent) = E_Constant);
2784 return Expr_Rep_Value (Constant_Value (Ent));
2785 end if;
2787 -- An integer literal that was either in the source or created
2788 -- as a result of static evaluation.
2790 elsif Kind = N_Integer_Literal then
2791 return Intval (N);
2793 -- A real literal for a fixed-point type. This must be the fixed-point
2794 -- case, either the literal is of a fixed-point type, or it is a bound
2795 -- of a fixed-point type, with type universal real. In either case we
2796 -- obtain the desired value from Corresponding_Integer_Value.
2798 elsif Kind = N_Real_Literal then
2799 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
2800 return Corresponding_Integer_Value (N);
2802 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
2804 elsif Kind = N_Attribute_Reference
2805 and then Attribute_Name (N) = Name_Null_Parameter
2806 then
2807 return Uint_0;
2809 -- Otherwise must be character literal
2811 else
2812 pragma Assert (Kind = N_Character_Literal);
2813 Ent := Entity (N);
2815 -- Since Character literals of type Standard.Character don't
2816 -- have any defining character literals built for them, they
2817 -- do not have their Entity set, so just use their Char
2818 -- code. Otherwise for user-defined character literals use
2819 -- their Pos value as usual which is the same as the Rep value.
2821 if No (Ent) then
2822 return Char_Literal_Value (N);
2823 else
2824 return Enumeration_Rep (Ent);
2825 end if;
2826 end if;
2827 end Expr_Rep_Value;
2829 ----------------
2830 -- Expr_Value --
2831 ----------------
2833 function Expr_Value (N : Node_Id) return Uint is
2834 Kind : constant Node_Kind := Nkind (N);
2835 CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size);
2836 Ent : Entity_Id;
2837 Val : Uint;
2839 begin
2840 -- If already in cache, then we know it's compile time known and
2841 -- we can return the value that was previously stored in the cache
2842 -- since compile time known values cannot change :-)
2844 if CV_Ent.N = N then
2845 return CV_Ent.V;
2846 end if;
2848 -- Otherwise proceed to test value
2850 if Is_Entity_Name (N) then
2851 Ent := Entity (N);
2853 -- An enumeration literal that was either in the source or
2854 -- created as a result of static evaluation.
2856 if Ekind (Ent) = E_Enumeration_Literal then
2857 Val := Enumeration_Pos (Ent);
2859 -- A user defined static constant
2861 else
2862 pragma Assert (Ekind (Ent) = E_Constant);
2863 Val := Expr_Value (Constant_Value (Ent));
2864 end if;
2866 -- An integer literal that was either in the source or created
2867 -- as a result of static evaluation.
2869 elsif Kind = N_Integer_Literal then
2870 Val := Intval (N);
2872 -- A real literal for a fixed-point type. This must be the fixed-point
2873 -- case, either the literal is of a fixed-point type, or it is a bound
2874 -- of a fixed-point type, with type universal real. In either case we
2875 -- obtain the desired value from Corresponding_Integer_Value.
2877 elsif Kind = N_Real_Literal then
2879 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
2880 Val := Corresponding_Integer_Value (N);
2882 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
2884 elsif Kind = N_Attribute_Reference
2885 and then Attribute_Name (N) = Name_Null_Parameter
2886 then
2887 Val := Uint_0;
2889 -- Otherwise must be character literal
2891 else
2892 pragma Assert (Kind = N_Character_Literal);
2893 Ent := Entity (N);
2895 -- Since Character literals of type Standard.Character don't
2896 -- have any defining character literals built for them, they
2897 -- do not have their Entity set, so just use their Char
2898 -- code. Otherwise for user-defined character literals use
2899 -- their Pos value as usual.
2901 if No (Ent) then
2902 Val := Char_Literal_Value (N);
2903 else
2904 Val := Enumeration_Pos (Ent);
2905 end if;
2906 end if;
2908 -- Come here with Val set to value to be returned, set cache
2910 CV_Ent.N := N;
2911 CV_Ent.V := Val;
2912 return Val;
2913 end Expr_Value;
2915 ------------------
2916 -- Expr_Value_E --
2917 ------------------
2919 function Expr_Value_E (N : Node_Id) return Entity_Id is
2920 Ent : constant Entity_Id := Entity (N);
2922 begin
2923 if Ekind (Ent) = E_Enumeration_Literal then
2924 return Ent;
2925 else
2926 pragma Assert (Ekind (Ent) = E_Constant);
2927 return Expr_Value_E (Constant_Value (Ent));
2928 end if;
2929 end Expr_Value_E;
2931 ------------------
2932 -- Expr_Value_R --
2933 ------------------
2935 function Expr_Value_R (N : Node_Id) return Ureal is
2936 Kind : constant Node_Kind := Nkind (N);
2937 Ent : Entity_Id;
2938 Expr : Node_Id;
2940 begin
2941 if Kind = N_Real_Literal then
2942 return Realval (N);
2944 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
2945 Ent := Entity (N);
2946 pragma Assert (Ekind (Ent) = E_Constant);
2947 return Expr_Value_R (Constant_Value (Ent));
2949 elsif Kind = N_Integer_Literal then
2950 return UR_From_Uint (Expr_Value (N));
2952 -- Strange case of VAX literals, which are at this stage transformed
2953 -- into Vax_Type!x_To_y(IEEE_Literal). See Expand_N_Real_Literal in
2954 -- Exp_Vfpt for further details.
2956 elsif Vax_Float (Etype (N))
2957 and then Nkind (N) = N_Unchecked_Type_Conversion
2958 then
2959 Expr := Expression (N);
2961 if Nkind (Expr) = N_Function_Call
2962 and then Present (Parameter_Associations (Expr))
2963 then
2964 Expr := First (Parameter_Associations (Expr));
2966 if Nkind (Expr) = N_Real_Literal then
2967 return Realval (Expr);
2968 end if;
2969 end if;
2971 -- Peculiar VMS case, if we have xxx'Null_Parameter, return 0.0
2973 elsif Kind = N_Attribute_Reference
2974 and then Attribute_Name (N) = Name_Null_Parameter
2975 then
2976 return Ureal_0;
2977 end if;
2979 -- If we fall through, we have a node that cannot be interepreted
2980 -- as a compile time constant. That is definitely an error.
2982 raise Program_Error;
2983 end Expr_Value_R;
2985 ------------------
2986 -- Expr_Value_S --
2987 ------------------
2989 function Expr_Value_S (N : Node_Id) return Node_Id is
2990 begin
2991 if Nkind (N) = N_String_Literal then
2992 return N;
2993 else
2994 pragma Assert (Ekind (Entity (N)) = E_Constant);
2995 return Expr_Value_S (Constant_Value (Entity (N)));
2996 end if;
2997 end Expr_Value_S;
2999 --------------------------
3000 -- Flag_Non_Static_Expr --
3001 --------------------------
3003 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is
3004 begin
3005 if Error_Posted (Expr) and then not All_Errors_Mode then
3006 return;
3007 else
3008 Error_Msg_F (Msg, Expr);
3009 Why_Not_Static (Expr);
3010 end if;
3011 end Flag_Non_Static_Expr;
3013 --------------
3014 -- Fold_Str --
3015 --------------
3017 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is
3018 Loc : constant Source_Ptr := Sloc (N);
3019 Typ : constant Entity_Id := Etype (N);
3021 begin
3022 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
3024 -- We now have the literal with the right value, both the actual type
3025 -- and the expected type of this literal are taken from the expression
3026 -- that was evaluated.
3028 Analyze (N);
3029 Set_Is_Static_Expression (N, Static);
3030 Set_Etype (N, Typ);
3031 Resolve (N);
3032 end Fold_Str;
3034 ---------------
3035 -- Fold_Uint --
3036 ---------------
3038 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is
3039 Loc : constant Source_Ptr := Sloc (N);
3040 Typ : Entity_Id := Etype (N);
3041 Ent : Entity_Id;
3043 begin
3044 -- If we are folding a named number, retain the entity in the
3045 -- literal, for ASIS use.
3047 if Is_Entity_Name (N)
3048 and then Ekind (Entity (N)) = E_Named_Integer
3049 then
3050 Ent := Entity (N);
3051 else
3052 Ent := Empty;
3053 end if;
3055 if Is_Private_Type (Typ) then
3056 Typ := Full_View (Typ);
3057 end if;
3059 -- For a result of type integer, subsitute an N_Integer_Literal node
3060 -- for the result of the compile time evaluation of the expression.
3062 if Is_Integer_Type (Typ) then
3063 Rewrite (N, Make_Integer_Literal (Loc, Val));
3064 Set_Original_Entity (N, Ent);
3066 -- Otherwise we have an enumeration type, and we substitute either
3067 -- an N_Identifier or N_Character_Literal to represent the enumeration
3068 -- literal corresponding to the given value, which must always be in
3069 -- range, because appropriate tests have already been made for this.
3071 else pragma Assert (Is_Enumeration_Type (Typ));
3072 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
3073 end if;
3075 -- We now have the literal with the right value, both the actual type
3076 -- and the expected type of this literal are taken from the expression
3077 -- that was evaluated.
3079 Analyze (N);
3080 Set_Is_Static_Expression (N, Static);
3081 Set_Etype (N, Typ);
3082 Resolve (N);
3083 end Fold_Uint;
3085 ----------------
3086 -- Fold_Ureal --
3087 ----------------
3089 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is
3090 Loc : constant Source_Ptr := Sloc (N);
3091 Typ : constant Entity_Id := Etype (N);
3092 Ent : Entity_Id;
3094 begin
3095 -- If we are folding a named number, retain the entity in the
3096 -- literal, for ASIS use.
3098 if Is_Entity_Name (N)
3099 and then Ekind (Entity (N)) = E_Named_Real
3100 then
3101 Ent := Entity (N);
3102 else
3103 Ent := Empty;
3104 end if;
3106 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
3107 Set_Original_Entity (N, Ent);
3109 -- Both the actual and expected type comes from the original expression
3111 Analyze (N);
3112 Set_Is_Static_Expression (N, Static);
3113 Set_Etype (N, Typ);
3114 Resolve (N);
3115 end Fold_Ureal;
3117 ---------------
3118 -- From_Bits --
3119 ---------------
3121 function From_Bits (B : Bits; T : Entity_Id) return Uint is
3122 V : Uint := Uint_0;
3124 begin
3125 for J in 0 .. B'Last loop
3126 if B (J) then
3127 V := V + 2 ** J;
3128 end if;
3129 end loop;
3131 if Non_Binary_Modulus (T) then
3132 V := V mod Modulus (T);
3133 end if;
3135 return V;
3136 end From_Bits;
3138 --------------------
3139 -- Get_String_Val --
3140 --------------------
3142 function Get_String_Val (N : Node_Id) return Node_Id is
3143 begin
3144 if Nkind (N) = N_String_Literal then
3145 return N;
3147 elsif Nkind (N) = N_Character_Literal then
3148 return N;
3150 else
3151 pragma Assert (Is_Entity_Name (N));
3152 return Get_String_Val (Constant_Value (Entity (N)));
3153 end if;
3154 end Get_String_Val;
3156 ----------------
3157 -- Initialize --
3158 ----------------
3160 procedure Initialize is
3161 begin
3162 CV_Cache := (others => (Node_High_Bound, Uint_0));
3163 end Initialize;
3165 --------------------
3166 -- In_Subrange_Of --
3167 --------------------
3169 function In_Subrange_Of
3170 (T1 : Entity_Id;
3171 T2 : Entity_Id;
3172 Fixed_Int : Boolean := False) return Boolean
3174 L1 : Node_Id;
3175 H1 : Node_Id;
3177 L2 : Node_Id;
3178 H2 : Node_Id;
3180 begin
3181 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
3182 return True;
3184 -- Never in range if both types are not scalar. Don't know if this can
3185 -- actually happen, but just in case.
3187 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T1) then
3188 return False;
3190 else
3191 L1 := Type_Low_Bound (T1);
3192 H1 := Type_High_Bound (T1);
3194 L2 := Type_Low_Bound (T2);
3195 H2 := Type_High_Bound (T2);
3197 -- Check bounds to see if comparison possible at compile time
3199 if Compile_Time_Compare (L1, L2) in Compare_GE
3200 and then
3201 Compile_Time_Compare (H1, H2) in Compare_LE
3202 then
3203 return True;
3204 end if;
3206 -- If bounds not comparable at compile time, then the bounds of T2
3207 -- must be compile time known or we cannot answer the query.
3209 if not Compile_Time_Known_Value (L2)
3210 or else not Compile_Time_Known_Value (H2)
3211 then
3212 return False;
3213 end if;
3215 -- If the bounds of T1 are know at compile time then use these
3216 -- ones, otherwise use the bounds of the base type (which are of
3217 -- course always static).
3219 if not Compile_Time_Known_Value (L1) then
3220 L1 := Type_Low_Bound (Base_Type (T1));
3221 end if;
3223 if not Compile_Time_Known_Value (H1) then
3224 H1 := Type_High_Bound (Base_Type (T1));
3225 end if;
3227 -- Fixed point types should be considered as such only if
3228 -- flag Fixed_Int is set to False.
3230 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
3231 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
3232 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
3233 then
3234 return
3235 Expr_Value_R (L2) <= Expr_Value_R (L1)
3236 and then
3237 Expr_Value_R (H2) >= Expr_Value_R (H1);
3239 else
3240 return
3241 Expr_Value (L2) <= Expr_Value (L1)
3242 and then
3243 Expr_Value (H2) >= Expr_Value (H1);
3245 end if;
3246 end if;
3248 -- If any exception occurs, it means that we have some bug in the compiler
3249 -- possibly triggered by a previous error, or by some unforseen peculiar
3250 -- occurrence. However, this is only an optimization attempt, so there is
3251 -- really no point in crashing the compiler. Instead we just decide, too
3252 -- bad, we can't figure out the answer in this case after all.
3254 exception
3255 when others =>
3257 -- Debug flag K disables this behavior (useful for debugging)
3259 if Debug_Flag_K then
3260 raise;
3261 else
3262 return False;
3263 end if;
3264 end In_Subrange_Of;
3266 -----------------
3267 -- Is_In_Range --
3268 -----------------
3270 function Is_In_Range
3271 (N : Node_Id;
3272 Typ : Entity_Id;
3273 Fixed_Int : Boolean := False;
3274 Int_Real : Boolean := False) return Boolean
3276 Val : Uint;
3277 Valr : Ureal;
3279 begin
3280 -- Universal types have no range limits, so always in range
3282 if Typ = Universal_Integer or else Typ = Universal_Real then
3283 return True;
3285 -- Never in range if not scalar type. Don't know if this can
3286 -- actually happen, but our spec allows it, so we must check!
3288 elsif not Is_Scalar_Type (Typ) then
3289 return False;
3291 -- Never in range unless we have a compile time known value
3293 elsif not Compile_Time_Known_Value (N) then
3294 return False;
3296 else
3297 declare
3298 Lo : constant Node_Id := Type_Low_Bound (Typ);
3299 Hi : constant Node_Id := Type_High_Bound (Typ);
3300 LB_Known : constant Boolean := Compile_Time_Known_Value (Lo);
3301 UB_Known : constant Boolean := Compile_Time_Known_Value (Hi);
3303 begin
3304 -- Fixed point types should be considered as such only in
3305 -- flag Fixed_Int is set to False.
3307 if Is_Floating_Point_Type (Typ)
3308 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
3309 or else Int_Real
3310 then
3311 Valr := Expr_Value_R (N);
3313 if LB_Known and then Valr >= Expr_Value_R (Lo)
3314 and then UB_Known and then Valr <= Expr_Value_R (Hi)
3315 then
3316 return True;
3317 else
3318 return False;
3319 end if;
3321 else
3322 Val := Expr_Value (N);
3324 if LB_Known and then Val >= Expr_Value (Lo)
3325 and then UB_Known and then Val <= Expr_Value (Hi)
3326 then
3327 return True;
3328 else
3329 return False;
3330 end if;
3331 end if;
3332 end;
3333 end if;
3334 end Is_In_Range;
3336 -------------------
3337 -- Is_Null_Range --
3338 -------------------
3340 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
3341 Typ : constant Entity_Id := Etype (Lo);
3343 begin
3344 if not Compile_Time_Known_Value (Lo)
3345 or else not Compile_Time_Known_Value (Hi)
3346 then
3347 return False;
3348 end if;
3350 if Is_Discrete_Type (Typ) then
3351 return Expr_Value (Lo) > Expr_Value (Hi);
3353 else
3354 pragma Assert (Is_Real_Type (Typ));
3355 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
3356 end if;
3357 end Is_Null_Range;
3359 -----------------------------
3360 -- Is_OK_Static_Expression --
3361 -----------------------------
3363 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
3364 begin
3365 return Is_Static_Expression (N)
3366 and then not Raises_Constraint_Error (N);
3367 end Is_OK_Static_Expression;
3369 ------------------------
3370 -- Is_OK_Static_Range --
3371 ------------------------
3373 -- A static range is a range whose bounds are static expressions, or a
3374 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
3375 -- We have already converted range attribute references, so we get the
3376 -- "or" part of this rule without needing a special test.
3378 function Is_OK_Static_Range (N : Node_Id) return Boolean is
3379 begin
3380 return Is_OK_Static_Expression (Low_Bound (N))
3381 and then Is_OK_Static_Expression (High_Bound (N));
3382 end Is_OK_Static_Range;
3384 --------------------------
3385 -- Is_OK_Static_Subtype --
3386 --------------------------
3388 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
3389 -- where neither bound raises constraint error when evaluated.
3391 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
3392 Base_T : constant Entity_Id := Base_Type (Typ);
3393 Anc_Subt : Entity_Id;
3395 begin
3396 -- First a quick check on the non static subtype flag. As described
3397 -- in further detail in Einfo, this flag is not decisive in all cases,
3398 -- but if it is set, then the subtype is definitely non-static.
3400 if Is_Non_Static_Subtype (Typ) then
3401 return False;
3402 end if;
3404 Anc_Subt := Ancestor_Subtype (Typ);
3406 if Anc_Subt = Empty then
3407 Anc_Subt := Base_T;
3408 end if;
3410 if Is_Generic_Type (Root_Type (Base_T))
3411 or else Is_Generic_Actual_Type (Base_T)
3412 then
3413 return False;
3415 -- String types
3417 elsif Is_String_Type (Typ) then
3418 return
3419 Ekind (Typ) = E_String_Literal_Subtype
3420 or else
3421 (Is_OK_Static_Subtype (Component_Type (Typ))
3422 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
3424 -- Scalar types
3426 elsif Is_Scalar_Type (Typ) then
3427 if Base_T = Typ then
3428 return True;
3430 else
3431 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so
3432 -- use Get_Type_Low,High_Bound.
3434 return Is_OK_Static_Subtype (Anc_Subt)
3435 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
3436 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
3437 end if;
3439 -- Types other than string and scalar types are never static
3441 else
3442 return False;
3443 end if;
3444 end Is_OK_Static_Subtype;
3446 ---------------------
3447 -- Is_Out_Of_Range --
3448 ---------------------
3450 function Is_Out_Of_Range
3451 (N : Node_Id;
3452 Typ : Entity_Id;
3453 Fixed_Int : Boolean := False;
3454 Int_Real : Boolean := False) return Boolean
3456 Val : Uint;
3457 Valr : Ureal;
3459 begin
3460 -- Universal types have no range limits, so always in range
3462 if Typ = Universal_Integer or else Typ = Universal_Real then
3463 return False;
3465 -- Never out of range if not scalar type. Don't know if this can
3466 -- actually happen, but our spec allows it, so we must check!
3468 elsif not Is_Scalar_Type (Typ) then
3469 return False;
3471 -- Never out of range if this is a generic type, since the bounds
3472 -- of generic types are junk. Note that if we only checked for
3473 -- static expressions (instead of compile time known values) below,
3474 -- we would not need this check, because values of a generic type
3475 -- can never be static, but they can be known at compile time.
3477 elsif Is_Generic_Type (Typ) then
3478 return False;
3480 -- Never out of range unless we have a compile time known value
3482 elsif not Compile_Time_Known_Value (N) then
3483 return False;
3485 else
3486 declare
3487 Lo : constant Node_Id := Type_Low_Bound (Typ);
3488 Hi : constant Node_Id := Type_High_Bound (Typ);
3489 LB_Known : constant Boolean := Compile_Time_Known_Value (Lo);
3490 UB_Known : constant Boolean := Compile_Time_Known_Value (Hi);
3492 begin
3493 -- Real types (note that fixed-point types are not treated
3494 -- as being of a real type if the flag Fixed_Int is set,
3495 -- since in that case they are regarded as integer types).
3497 if Is_Floating_Point_Type (Typ)
3498 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
3499 or else Int_Real
3500 then
3501 Valr := Expr_Value_R (N);
3503 if LB_Known and then Valr < Expr_Value_R (Lo) then
3504 return True;
3506 elsif UB_Known and then Expr_Value_R (Hi) < Valr then
3507 return True;
3509 else
3510 return False;
3511 end if;
3513 else
3514 Val := Expr_Value (N);
3516 if LB_Known and then Val < Expr_Value (Lo) then
3517 return True;
3519 elsif UB_Known and then Expr_Value (Hi) < Val then
3520 return True;
3522 else
3523 return False;
3524 end if;
3525 end if;
3526 end;
3527 end if;
3528 end Is_Out_Of_Range;
3530 ---------------------
3531 -- Is_Static_Range --
3532 ---------------------
3534 -- A static range is a range whose bounds are static expressions, or a
3535 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
3536 -- We have already converted range attribute references, so we get the
3537 -- "or" part of this rule without needing a special test.
3539 function Is_Static_Range (N : Node_Id) return Boolean is
3540 begin
3541 return Is_Static_Expression (Low_Bound (N))
3542 and then Is_Static_Expression (High_Bound (N));
3543 end Is_Static_Range;
3545 -----------------------
3546 -- Is_Static_Subtype --
3547 -----------------------
3549 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
3551 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
3552 Base_T : constant Entity_Id := Base_Type (Typ);
3553 Anc_Subt : Entity_Id;
3555 begin
3556 -- First a quick check on the non static subtype flag. As described
3557 -- in further detail in Einfo, this flag is not decisive in all cases,
3558 -- but if it is set, then the subtype is definitely non-static.
3560 if Is_Non_Static_Subtype (Typ) then
3561 return False;
3562 end if;
3564 Anc_Subt := Ancestor_Subtype (Typ);
3566 if Anc_Subt = Empty then
3567 Anc_Subt := Base_T;
3568 end if;
3570 if Is_Generic_Type (Root_Type (Base_T))
3571 or else Is_Generic_Actual_Type (Base_T)
3572 then
3573 return False;
3575 -- String types
3577 elsif Is_String_Type (Typ) then
3578 return
3579 Ekind (Typ) = E_String_Literal_Subtype
3580 or else
3581 (Is_Static_Subtype (Component_Type (Typ))
3582 and then Is_Static_Subtype (Etype (First_Index (Typ))));
3584 -- Scalar types
3586 elsif Is_Scalar_Type (Typ) then
3587 if Base_T = Typ then
3588 return True;
3590 else
3591 return Is_Static_Subtype (Anc_Subt)
3592 and then Is_Static_Expression (Type_Low_Bound (Typ))
3593 and then Is_Static_Expression (Type_High_Bound (Typ));
3594 end if;
3596 -- Types other than string and scalar types are never static
3598 else
3599 return False;
3600 end if;
3601 end Is_Static_Subtype;
3603 --------------------
3604 -- Not_Null_Range --
3605 --------------------
3607 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
3608 Typ : constant Entity_Id := Etype (Lo);
3610 begin
3611 if not Compile_Time_Known_Value (Lo)
3612 or else not Compile_Time_Known_Value (Hi)
3613 then
3614 return False;
3615 end if;
3617 if Is_Discrete_Type (Typ) then
3618 return Expr_Value (Lo) <= Expr_Value (Hi);
3620 else
3621 pragma Assert (Is_Real_Type (Typ));
3623 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
3624 end if;
3625 end Not_Null_Range;
3627 -------------
3628 -- OK_Bits --
3629 -------------
3631 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
3632 begin
3633 -- We allow a maximum of 500,000 bits which seems a reasonable limit
3635 if Bits < 500_000 then
3636 return True;
3638 else
3639 Error_Msg_N ("static value too large, capacity exceeded", N);
3640 return False;
3641 end if;
3642 end OK_Bits;
3644 ------------------
3645 -- Out_Of_Range --
3646 ------------------
3648 procedure Out_Of_Range (N : Node_Id) is
3649 begin
3650 -- If we have the static expression case, then this is an illegality
3651 -- in Ada 95 mode, except that in an instance, we never generate an
3652 -- error (if the error is legitimate, it was already diagnosed in
3653 -- the template). The expression to compute the length of a packed
3654 -- array is attached to the array type itself, and deserves a separate
3655 -- message.
3657 if Is_Static_Expression (N)
3658 and then not In_Instance
3659 and then not In_Inlined_Body
3660 and then Ada_Version >= Ada_95
3661 then
3662 if Nkind (Parent (N)) = N_Defining_Identifier
3663 and then Is_Array_Type (Parent (N))
3664 and then Present (Packed_Array_Type (Parent (N)))
3665 and then Present (First_Rep_Item (Parent (N)))
3666 then
3667 Error_Msg_N
3668 ("length of packed array must not exceed Integer''Last",
3669 First_Rep_Item (Parent (N)));
3670 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
3672 else
3673 Apply_Compile_Time_Constraint_Error
3674 (N, "value not in range of}", CE_Range_Check_Failed);
3675 end if;
3677 -- Here we generate a warning for the Ada 83 case, or when we are
3678 -- in an instance, or when we have a non-static expression case.
3680 else
3681 Apply_Compile_Time_Constraint_Error
3682 (N, "value not in range of}?", CE_Range_Check_Failed);
3683 end if;
3684 end Out_Of_Range;
3686 -------------------------
3687 -- Rewrite_In_Raise_CE --
3688 -------------------------
3690 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
3691 Typ : constant Entity_Id := Etype (N);
3693 begin
3694 -- If we want to raise CE in the condition of a raise_CE node
3695 -- we may as well get rid of the condition
3697 if Present (Parent (N))
3698 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
3699 then
3700 Set_Condition (Parent (N), Empty);
3702 -- If the expression raising CE is a N_Raise_CE node, we can use
3703 -- that one. We just preserve the type of the context
3705 elsif Nkind (Exp) = N_Raise_Constraint_Error then
3706 Rewrite (N, Exp);
3707 Set_Etype (N, Typ);
3709 -- We have to build an explicit raise_ce node
3711 else
3712 Rewrite (N,
3713 Make_Raise_Constraint_Error (Sloc (Exp),
3714 Reason => CE_Range_Check_Failed));
3715 Set_Raises_Constraint_Error (N);
3716 Set_Etype (N, Typ);
3717 end if;
3718 end Rewrite_In_Raise_CE;
3720 ---------------------
3721 -- String_Type_Len --
3722 ---------------------
3724 function String_Type_Len (Stype : Entity_Id) return Uint is
3725 NT : constant Entity_Id := Etype (First_Index (Stype));
3726 T : Entity_Id;
3728 begin
3729 if Is_OK_Static_Subtype (NT) then
3730 T := NT;
3731 else
3732 T := Base_Type (NT);
3733 end if;
3735 return Expr_Value (Type_High_Bound (T)) -
3736 Expr_Value (Type_Low_Bound (T)) + 1;
3737 end String_Type_Len;
3739 ------------------------------------
3740 -- Subtypes_Statically_Compatible --
3741 ------------------------------------
3743 function Subtypes_Statically_Compatible
3744 (T1 : Entity_Id;
3745 T2 : Entity_Id) return Boolean
3747 begin
3748 if Is_Scalar_Type (T1) then
3750 -- Definitely compatible if we match
3752 if Subtypes_Statically_Match (T1, T2) then
3753 return True;
3755 -- If either subtype is nonstatic then they're not compatible
3757 elsif not Is_Static_Subtype (T1)
3758 or else not Is_Static_Subtype (T2)
3759 then
3760 return False;
3762 -- If either type has constraint error bounds, then consider that
3763 -- they match to avoid junk cascaded errors here.
3765 elsif not Is_OK_Static_Subtype (T1)
3766 or else not Is_OK_Static_Subtype (T2)
3767 then
3768 return True;
3770 -- Base types must match, but we don't check that (should
3771 -- we???) but we do at least check that both types are
3772 -- real, or both types are not real.
3774 elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then
3775 return False;
3777 -- Here we check the bounds
3779 else
3780 declare
3781 LB1 : constant Node_Id := Type_Low_Bound (T1);
3782 HB1 : constant Node_Id := Type_High_Bound (T1);
3783 LB2 : constant Node_Id := Type_Low_Bound (T2);
3784 HB2 : constant Node_Id := Type_High_Bound (T2);
3786 begin
3787 if Is_Real_Type (T1) then
3788 return
3789 (Expr_Value_R (LB1) > Expr_Value_R (HB1))
3790 or else
3791 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
3792 and then
3793 Expr_Value_R (HB1) <= Expr_Value_R (HB2));
3795 else
3796 return
3797 (Expr_Value (LB1) > Expr_Value (HB1))
3798 or else
3799 (Expr_Value (LB2) <= Expr_Value (LB1)
3800 and then
3801 Expr_Value (HB1) <= Expr_Value (HB2));
3802 end if;
3803 end;
3804 end if;
3806 elsif Is_Access_Type (T1) then
3807 return not Is_Constrained (T2)
3808 or else Subtypes_Statically_Match
3809 (Designated_Type (T1), Designated_Type (T2));
3811 else
3812 return (Is_Composite_Type (T1) and then not Is_Constrained (T2))
3813 or else Subtypes_Statically_Match (T1, T2);
3814 end if;
3815 end Subtypes_Statically_Compatible;
3817 -------------------------------
3818 -- Subtypes_Statically_Match --
3819 -------------------------------
3821 -- Subtypes statically match if they have statically matching constraints
3822 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
3823 -- they are the same identical constraint, or if they are static and the
3824 -- values match (RM 4.9.1(1)).
3826 function Subtypes_Statically_Match (T1, T2 : Entity_Id) return Boolean is
3827 begin
3828 -- A type always statically matches itself
3830 if T1 = T2 then
3831 return True;
3833 -- Scalar types
3835 elsif Is_Scalar_Type (T1) then
3837 -- Base types must be the same
3839 if Base_Type (T1) /= Base_Type (T2) then
3840 return False;
3841 end if;
3843 -- A constrained numeric subtype never matches an unconstrained
3844 -- subtype, i.e. both types must be constrained or unconstrained.
3846 -- To understand the requirement for this test, see RM 4.9.1(1).
3847 -- As is made clear in RM 3.5.4(11), type Integer, for example
3848 -- is a constrained subtype with constraint bounds matching the
3849 -- bounds of its corresponding uncontrained base type. In this
3850 -- situation, Integer and Integer'Base do not statically match,
3851 -- even though they have the same bounds.
3853 -- We only apply this test to types in Standard and types that
3854 -- appear in user programs. That way, we do not have to be
3855 -- too careful about setting Is_Constrained right for itypes.
3857 if Is_Numeric_Type (T1)
3858 and then (Is_Constrained (T1) /= Is_Constrained (T2))
3859 and then (Scope (T1) = Standard_Standard
3860 or else Comes_From_Source (T1))
3861 and then (Scope (T2) = Standard_Standard
3862 or else Comes_From_Source (T2))
3863 then
3864 return False;
3866 -- A generic scalar type does not statically match its base
3867 -- type (AI-311). In this case we make sure that the formals,
3868 -- which are first subtypes of their bases, are constrained.
3870 elsif Is_Generic_Type (T1)
3871 and then Is_Generic_Type (T2)
3872 and then (Is_Constrained (T1) /= Is_Constrained (T2))
3873 then
3874 return False;
3875 end if;
3877 -- If there was an error in either range, then just assume
3878 -- the types statically match to avoid further junk errors
3880 if Error_Posted (Scalar_Range (T1))
3881 or else
3882 Error_Posted (Scalar_Range (T2))
3883 then
3884 return True;
3885 end if;
3887 -- Otherwise both types have bound that can be compared
3889 declare
3890 LB1 : constant Node_Id := Type_Low_Bound (T1);
3891 HB1 : constant Node_Id := Type_High_Bound (T1);
3892 LB2 : constant Node_Id := Type_Low_Bound (T2);
3893 HB2 : constant Node_Id := Type_High_Bound (T2);
3895 begin
3896 -- If the bounds are the same tree node, then match
3898 if LB1 = LB2 and then HB1 = HB2 then
3899 return True;
3901 -- Otherwise bounds must be static and identical value
3903 else
3904 if not Is_Static_Subtype (T1)
3905 or else not Is_Static_Subtype (T2)
3906 then
3907 return False;
3909 -- If either type has constraint error bounds, then say
3910 -- that they match to avoid junk cascaded errors here.
3912 elsif not Is_OK_Static_Subtype (T1)
3913 or else not Is_OK_Static_Subtype (T2)
3914 then
3915 return True;
3917 elsif Is_Real_Type (T1) then
3918 return
3919 (Expr_Value_R (LB1) = Expr_Value_R (LB2))
3920 and then
3921 (Expr_Value_R (HB1) = Expr_Value_R (HB2));
3923 else
3924 return
3925 Expr_Value (LB1) = Expr_Value (LB2)
3926 and then
3927 Expr_Value (HB1) = Expr_Value (HB2);
3928 end if;
3929 end if;
3930 end;
3932 -- Type with discriminants
3934 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
3936 -- Because of view exchanges in multiple instantiations, conformance
3937 -- checking might try to match a partial view of a type with no
3938 -- discriminants with a full view that has defaulted discriminants.
3939 -- In such a case, use the discriminant constraint of the full view,
3940 -- which must exist because we know that the two subtypes have the
3941 -- same base type.
3943 if Has_Discriminants (T1) /= Has_Discriminants (T2) then
3944 if In_Instance then
3945 if Is_Private_Type (T2)
3946 and then Present (Full_View (T2))
3947 and then Has_Discriminants (Full_View (T2))
3948 then
3949 return Subtypes_Statically_Match (T1, Full_View (T2));
3951 elsif Is_Private_Type (T1)
3952 and then Present (Full_View (T1))
3953 and then Has_Discriminants (Full_View (T1))
3954 then
3955 return Subtypes_Statically_Match (Full_View (T1), T2);
3957 else
3958 return False;
3959 end if;
3960 else
3961 return False;
3962 end if;
3963 end if;
3965 declare
3966 DL1 : constant Elist_Id := Discriminant_Constraint (T1);
3967 DL2 : constant Elist_Id := Discriminant_Constraint (T2);
3969 DA1 : Elmt_Id := First_Elmt (DL1);
3970 DA2 : Elmt_Id := First_Elmt (DL2);
3972 begin
3973 if DL1 = DL2 then
3974 return True;
3976 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
3977 return False;
3978 end if;
3980 while Present (DA1) loop
3981 declare
3982 Expr1 : constant Node_Id := Node (DA1);
3983 Expr2 : constant Node_Id := Node (DA2);
3985 begin
3986 if not Is_Static_Expression (Expr1)
3987 or else not Is_Static_Expression (Expr2)
3988 then
3989 return False;
3991 -- If either expression raised a constraint error,
3992 -- consider the expressions as matching, since this
3993 -- helps to prevent cascading errors.
3995 elsif Raises_Constraint_Error (Expr1)
3996 or else Raises_Constraint_Error (Expr2)
3997 then
3998 null;
4000 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
4001 return False;
4002 end if;
4003 end;
4005 Next_Elmt (DA1);
4006 Next_Elmt (DA2);
4007 end loop;
4008 end;
4010 return True;
4012 -- A definite type does not match an indefinite or classwide type
4013 -- However, a generic type with unknown discriminants may be
4014 -- instantiated with a type with no discriminants, and conformance
4015 -- checking on an inherited operation may compare the actual with
4016 -- the subtype that renames it in the instance.
4018 elsif
4019 Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
4020 then
4021 return
4022 Is_Generic_Actual_Type (T1) or else Is_Generic_Actual_Type (T2);
4024 -- Array type
4026 elsif Is_Array_Type (T1) then
4028 -- If either subtype is unconstrained then both must be,
4029 -- and if both are unconstrained then no further checking
4030 -- is needed.
4032 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
4033 return not (Is_Constrained (T1) or else Is_Constrained (T2));
4034 end if;
4036 -- Both subtypes are constrained, so check that the index
4037 -- subtypes statically match.
4039 declare
4040 Index1 : Node_Id := First_Index (T1);
4041 Index2 : Node_Id := First_Index (T2);
4043 begin
4044 while Present (Index1) loop
4045 if not
4046 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
4047 then
4048 return False;
4049 end if;
4051 Next_Index (Index1);
4052 Next_Index (Index2);
4053 end loop;
4055 return True;
4056 end;
4058 elsif Is_Access_Type (T1) then
4059 if Can_Never_Be_Null (T1) /= Can_Never_Be_Null (T2) then
4060 return False;
4062 elsif Ekind (T1) = E_Access_Subprogram_Type
4063 or else Ekind (T1) = E_Anonymous_Access_Subprogram_Type
4064 then
4065 return
4066 Subtype_Conformant
4067 (Designated_Type (T1),
4068 Designated_Type (T2));
4069 else
4070 return
4071 Subtypes_Statically_Match
4072 (Designated_Type (T1),
4073 Designated_Type (T2))
4074 and then Is_Access_Constant (T1) = Is_Access_Constant (T2);
4075 end if;
4077 -- All other types definitely match
4079 else
4080 return True;
4081 end if;
4082 end Subtypes_Statically_Match;
4084 ----------
4085 -- Test --
4086 ----------
4088 function Test (Cond : Boolean) return Uint is
4089 begin
4090 if Cond then
4091 return Uint_1;
4092 else
4093 return Uint_0;
4094 end if;
4095 end Test;
4097 ---------------------------------
4098 -- Test_Expression_Is_Foldable --
4099 ---------------------------------
4101 -- One operand case
4103 procedure Test_Expression_Is_Foldable
4104 (N : Node_Id;
4105 Op1 : Node_Id;
4106 Stat : out Boolean;
4107 Fold : out Boolean)
4109 begin
4110 Stat := False;
4111 Fold := False;
4113 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
4114 return;
4115 end if;
4117 -- If operand is Any_Type, just propagate to result and do not
4118 -- try to fold, this prevents cascaded errors.
4120 if Etype (Op1) = Any_Type then
4121 Set_Etype (N, Any_Type);
4122 return;
4124 -- If operand raises constraint error, then replace node N with the
4125 -- raise constraint error node, and we are obviously not foldable.
4126 -- Note that this replacement inherits the Is_Static_Expression flag
4127 -- from the operand.
4129 elsif Raises_Constraint_Error (Op1) then
4130 Rewrite_In_Raise_CE (N, Op1);
4131 return;
4133 -- If the operand is not static, then the result is not static, and
4134 -- all we have to do is to check the operand since it is now known
4135 -- to appear in a non-static context.
4137 elsif not Is_Static_Expression (Op1) then
4138 Check_Non_Static_Context (Op1);
4139 Fold := Compile_Time_Known_Value (Op1);
4140 return;
4142 -- An expression of a formal modular type is not foldable because
4143 -- the modulus is unknown.
4145 elsif Is_Modular_Integer_Type (Etype (Op1))
4146 and then Is_Generic_Type (Etype (Op1))
4147 then
4148 Check_Non_Static_Context (Op1);
4149 return;
4151 -- Here we have the case of an operand whose type is OK, which is
4152 -- static, and which does not raise constraint error, we can fold.
4154 else
4155 Set_Is_Static_Expression (N);
4156 Fold := True;
4157 Stat := True;
4158 end if;
4159 end Test_Expression_Is_Foldable;
4161 -- Two operand case
4163 procedure Test_Expression_Is_Foldable
4164 (N : Node_Id;
4165 Op1 : Node_Id;
4166 Op2 : Node_Id;
4167 Stat : out Boolean;
4168 Fold : out Boolean)
4170 Rstat : constant Boolean := Is_Static_Expression (Op1)
4171 and then Is_Static_Expression (Op2);
4173 begin
4174 Stat := False;
4175 Fold := False;
4177 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
4178 return;
4179 end if;
4181 -- If either operand is Any_Type, just propagate to result and
4182 -- do not try to fold, this prevents cascaded errors.
4184 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
4185 Set_Etype (N, Any_Type);
4186 return;
4188 -- If left operand raises constraint error, then replace node N with
4189 -- the raise constraint error node, and we are obviously not foldable.
4190 -- Is_Static_Expression is set from the two operands in the normal way,
4191 -- and we check the right operand if it is in a non-static context.
4193 elsif Raises_Constraint_Error (Op1) then
4194 if not Rstat then
4195 Check_Non_Static_Context (Op2);
4196 end if;
4198 Rewrite_In_Raise_CE (N, Op1);
4199 Set_Is_Static_Expression (N, Rstat);
4200 return;
4202 -- Similar processing for the case of the right operand. Note that
4203 -- we don't use this routine for the short-circuit case, so we do
4204 -- not have to worry about that special case here.
4206 elsif Raises_Constraint_Error (Op2) then
4207 if not Rstat then
4208 Check_Non_Static_Context (Op1);
4209 end if;
4211 Rewrite_In_Raise_CE (N, Op2);
4212 Set_Is_Static_Expression (N, Rstat);
4213 return;
4215 -- Exclude expressions of a generic modular type, as above
4217 elsif Is_Modular_Integer_Type (Etype (Op1))
4218 and then Is_Generic_Type (Etype (Op1))
4219 then
4220 Check_Non_Static_Context (Op1);
4221 return;
4223 -- If result is not static, then check non-static contexts on operands
4224 -- since one of them may be static and the other one may not be static
4226 elsif not Rstat then
4227 Check_Non_Static_Context (Op1);
4228 Check_Non_Static_Context (Op2);
4229 Fold := Compile_Time_Known_Value (Op1)
4230 and then Compile_Time_Known_Value (Op2);
4231 return;
4233 -- Else result is static and foldable. Both operands are static,
4234 -- and neither raises constraint error, so we can definitely fold.
4236 else
4237 Set_Is_Static_Expression (N);
4238 Fold := True;
4239 Stat := True;
4240 return;
4241 end if;
4242 end Test_Expression_Is_Foldable;
4244 --------------
4245 -- To_Bits --
4246 --------------
4248 procedure To_Bits (U : Uint; B : out Bits) is
4249 begin
4250 for J in 0 .. B'Last loop
4251 B (J) := (U / (2 ** J)) mod 2 /= 0;
4252 end loop;
4253 end To_Bits;
4255 --------------------
4256 -- Why_Not_Static --
4257 --------------------
4259 procedure Why_Not_Static (Expr : Node_Id) is
4260 N : constant Node_Id := Original_Node (Expr);
4261 Typ : Entity_Id;
4262 E : Entity_Id;
4264 procedure Why_Not_Static_List (L : List_Id);
4265 -- A version that can be called on a list of expressions. Finds
4266 -- all non-static violations in any element of the list.
4268 -------------------------
4269 -- Why_Not_Static_List --
4270 -------------------------
4272 procedure Why_Not_Static_List (L : List_Id) is
4273 N : Node_Id;
4275 begin
4276 if Is_Non_Empty_List (L) then
4277 N := First (L);
4278 while Present (N) loop
4279 Why_Not_Static (N);
4280 Next (N);
4281 end loop;
4282 end if;
4283 end Why_Not_Static_List;
4285 -- Start of processing for Why_Not_Static
4287 begin
4288 -- If in ACATS mode (debug flag 2), then suppress all these
4289 -- messages, this avoids massive updates to the ACATS base line.
4291 if Debug_Flag_2 then
4292 return;
4293 end if;
4295 -- Ignore call on error or empty node
4297 if No (Expr) or else Nkind (Expr) = N_Error then
4298 return;
4299 end if;
4301 -- Preprocessing for sub expressions
4303 if Nkind (Expr) in N_Subexpr then
4305 -- Nothing to do if expression is static
4307 if Is_OK_Static_Expression (Expr) then
4308 return;
4309 end if;
4311 -- Test for constraint error raised
4313 if Raises_Constraint_Error (Expr) then
4314 Error_Msg_N
4315 ("expression raises exception, cannot be static " &
4316 "('R'M 4.9(34))!", N);
4317 return;
4318 end if;
4320 -- If no type, then something is pretty wrong, so ignore
4322 Typ := Etype (Expr);
4324 if No (Typ) then
4325 return;
4326 end if;
4328 -- Type must be scalar or string type
4330 if not Is_Scalar_Type (Typ)
4331 and then not Is_String_Type (Typ)
4332 then
4333 Error_Msg_N
4334 ("static expression must have scalar or string type " &
4335 "('R'M 4.9(2))!", N);
4336 return;
4337 end if;
4338 end if;
4340 -- If we got through those checks, test particular node kind
4342 case Nkind (N) is
4343 when N_Expanded_Name | N_Identifier | N_Operator_Symbol =>
4344 E := Entity (N);
4346 if Is_Named_Number (E) then
4347 null;
4349 elsif Ekind (E) = E_Constant then
4350 if not Is_Static_Expression (Constant_Value (E)) then
4351 Error_Msg_NE
4352 ("& is not a static constant ('R'M 4.9(5))!", N, E);
4353 end if;
4355 else
4356 Error_Msg_NE
4357 ("& is not static constant or named number " &
4358 "('R'M 4.9(5))!", N, E);
4359 end if;
4361 when N_Binary_Op | N_And_Then | N_Or_Else | N_In | N_Not_In =>
4362 if Nkind (N) in N_Op_Shift then
4363 Error_Msg_N
4364 ("shift functions are never static ('R'M 4.9(6,18))!", N);
4366 else
4367 Why_Not_Static (Left_Opnd (N));
4368 Why_Not_Static (Right_Opnd (N));
4369 end if;
4371 when N_Unary_Op =>
4372 Why_Not_Static (Right_Opnd (N));
4374 when N_Attribute_Reference =>
4375 Why_Not_Static_List (Expressions (N));
4377 E := Etype (Prefix (N));
4379 if E = Standard_Void_Type then
4380 return;
4381 end if;
4383 -- Special case non-scalar'Size since this is a common error
4385 if Attribute_Name (N) = Name_Size then
4386 Error_Msg_N
4387 ("size attribute is only static for scalar type " &
4388 "('R'M 4.9(7,8))", N);
4390 -- Flag array cases
4392 elsif Is_Array_Type (E) then
4393 if Attribute_Name (N) /= Name_First
4394 and then
4395 Attribute_Name (N) /= Name_Last
4396 and then
4397 Attribute_Name (N) /= Name_Length
4398 then
4399 Error_Msg_N
4400 ("static array attribute must be Length, First, or Last " &
4401 "('R'M 4.9(8))!", N);
4403 -- Since we know the expression is not-static (we already
4404 -- tested for this, must mean array is not static).
4406 else
4407 Error_Msg_N
4408 ("prefix is non-static array ('R'M 4.9(8))!", Prefix (N));
4409 end if;
4411 return;
4413 -- Special case generic types, since again this is a common
4414 -- source of confusion.
4416 elsif Is_Generic_Actual_Type (E)
4417 or else
4418 Is_Generic_Type (E)
4419 then
4420 Error_Msg_N
4421 ("attribute of generic type is never static " &
4422 "('R'M 4.9(7,8))!", N);
4424 elsif Is_Static_Subtype (E) then
4425 null;
4427 elsif Is_Scalar_Type (E) then
4428 Error_Msg_N
4429 ("prefix type for attribute is not static scalar subtype " &
4430 "('R'M 4.9(7))!", N);
4432 else
4433 Error_Msg_N
4434 ("static attribute must apply to array/scalar type " &
4435 "('R'M 4.9(7,8))!", N);
4436 end if;
4438 when N_String_Literal =>
4439 Error_Msg_N
4440 ("subtype of string literal is non-static ('R'M 4.9(4))!", N);
4442 when N_Explicit_Dereference =>
4443 Error_Msg_N
4444 ("explicit dereference is never static ('R'M 4.9)!", N);
4446 when N_Function_Call =>
4447 Why_Not_Static_List (Parameter_Associations (N));
4448 Error_Msg_N ("non-static function call ('R'M 4.9(6,18))!", N);
4450 when N_Parameter_Association =>
4451 Why_Not_Static (Explicit_Actual_Parameter (N));
4453 when N_Indexed_Component =>
4454 Error_Msg_N
4455 ("indexed component is never static ('R'M 4.9)!", N);
4457 when N_Procedure_Call_Statement =>
4458 Error_Msg_N
4459 ("procedure call is never static ('R'M 4.9)!", N);
4461 when N_Qualified_Expression =>
4462 Why_Not_Static (Expression (N));
4464 when N_Aggregate | N_Extension_Aggregate =>
4465 Error_Msg_N
4466 ("an aggregate is never static ('R'M 4.9)!", N);
4468 when N_Range =>
4469 Why_Not_Static (Low_Bound (N));
4470 Why_Not_Static (High_Bound (N));
4472 when N_Range_Constraint =>
4473 Why_Not_Static (Range_Expression (N));
4475 when N_Subtype_Indication =>
4476 Why_Not_Static (Constraint (N));
4478 when N_Selected_Component =>
4479 Error_Msg_N
4480 ("selected component is never static ('R'M 4.9)!", N);
4482 when N_Slice =>
4483 Error_Msg_N
4484 ("slice is never static ('R'M 4.9)!", N);
4486 when N_Type_Conversion =>
4487 Why_Not_Static (Expression (N));
4489 if not Is_Scalar_Type (Etype (Prefix (N)))
4490 or else not Is_Static_Subtype (Etype (Prefix (N)))
4491 then
4492 Error_Msg_N
4493 ("static conversion requires static scalar subtype result " &
4494 "('R'M 4.9(9))!", N);
4495 end if;
4497 when N_Unchecked_Type_Conversion =>
4498 Error_Msg_N
4499 ("unchecked type conversion is never static ('R'M 4.9)!", N);
4501 when others =>
4502 null;
4504 end case;
4505 end Why_Not_Static;
4507 end Sem_Eval;